33 #include <sys/select.h>
37 #ifndef UDP_THREAD_PRIO
38 #define UDP_THREAD_PRIO 10
41 static void *
udp_thread(
void *data __attribute__((unused)));
42 static pthread_mutex_t
udp_mutex = PTHREAD_MUTEX_INITIALIZER;
46 pthread_mutex_init(&udp_mutex, NULL);
59 if (pthread_create(&tid, NULL,
udp_thread, NULL) != 0) {
60 fprintf(stderr,
"udp_arch_init: Could not create UDP reading thread.\n");
83 pthread_mutex_lock(&udp_mutex);
88 pthread_mutex_unlock(&udp_mutex);
99 pthread_mutex_lock(&udp_mutex);
102 pthread_mutex_unlock(&udp_mutex);
111 if (p == NULL)
return;
112 if (p->
network == NULL)
return;
119 if (available <= 0) {
123 socklen_t slen =
sizeof(
struct sockaddr_in);
124 ssize_t byte_read = recvfrom(sock->
sockfd, buf, available, MSG_DONTWAIT,
125 (
struct sockaddr *)&sock->
addr_in, &slen);
127 pthread_mutex_lock(&udp_mutex);
130 for (i = 0; i < byte_read; i++) {
136 pthread_mutex_unlock(&udp_mutex);
144 if (p == NULL)
return;
145 if (p->
network == NULL)
return;
153 if (bytes_sent < 0) {
154 perror(
"udp_send_message failed");
157 fprintf(stderr,
"udp_send_message: only sent %d bytes instead of %d\n",
170 if (p == NULL)
return;
171 if (p->
network == NULL)
return;
174 ssize_t test __attribute__((unused)) = sendto(sock->
sockfd, buffer, size, MSG_DONTWAIT,
191 FD_ZERO(&socks_master);
193 int fd __attribute__((unused));
196 FD_SET(
fd, &socks_master);
203 FD_SET(
fd, &socks_master);
210 FD_SET(
fd, &socks_master);
221 socks = socks_master;
223 if (select(fdmax + 1, &socks, NULL, NULL, NULL) < 0) {
224 fprintf(stderr,
"udp_thread: select failed!");
229 if (FD_ISSET(
fd, &socks)) {
235 if (FD_ISSET(
fd, &socks)) {
241 if (FD_ISSET(
fd, &socks)) {
uint8_t tx_buf[UDP_TX_BUFFER_SIZE]
Transmit buffer.
Easily create and use UDP sockets.
void udp_send_message(struct udp_periph *p, long fd)
Send a message.
Functions to obtain rt priority or set the nice level.
#define UDP_RX_BUFFER_SIZE
uint16_t udp_char_available(struct udp_periph *p)
Get number of bytes available in receive buffer.
void * network
UDP network.
void udp_receive(struct udp_periph *p)
Read bytes from UDP.
struct sockaddr_in addr_in
struct sockaddr_in addr_out
static pthread_mutex_t udp_mutex
static void * udp_thread(void *data)
check for new udp packets to receive or send.
uint8_t rx_buf[UDP_RX_BUFFER_SIZE]
Receive buffer.
uint8_t udp_getch(struct udp_periph *p)
Get the last character from the receive buffer.
void udp_arch_periph_init(struct udp_periph *p, char *host, int port_out, int port_in, bool broadcast)
Initialize the UDP peripheral.
static int get_rt_prio(int prio)
int udp_socket_create(struct UdpSocket *sock, char *host, int port_out, int port_in, bool broadcast)
Create UDP socket and bind it.
if(PrimarySpektrumState.SpektrumTimer)
void udp_send_raw(struct udp_periph *p, long fd, uint8_t *buffer, uint16_t size)
Send a packet from another buffer.