32 #include <sys/select.h>
36 #ifndef UDP_THREAD_PRIO
37 #define UDP_THREAD_PRIO 10
40 static void *
udp_thread(
void *data __attribute__((unused)));
41 static pthread_mutex_t
udp_mutex = PTHREAD_MUTEX_INITIALIZER;
45 pthread_mutex_init(&udp_mutex, NULL);
58 if (pthread_create(&tid, NULL,
udp_thread, NULL) != 0) {
59 fprintf(stderr,
"udp_arch_init: Could not create UDP reading thread.\n");
63 pthread_setname_np(tid,
"udp");
85 pthread_mutex_lock(&udp_mutex);
90 pthread_mutex_unlock(&udp_mutex);
101 pthread_mutex_lock(&udp_mutex);
104 pthread_mutex_unlock(&udp_mutex);
113 if (p == NULL) {
return; }
114 if (p->
network == NULL) {
return; }
121 if (available <= 0) {
125 socklen_t slen =
sizeof(
struct sockaddr_in);
126 ssize_t byte_read = recvfrom(sock->
sockfd, buf, available, MSG_DONTWAIT,
127 (
struct sockaddr *)&sock->
addr_in, &slen);
129 pthread_mutex_lock(&udp_mutex);
132 for (i = 0; i < byte_read; i++) {
138 pthread_mutex_unlock(&udp_mutex);
146 if (p == NULL) {
return; }
147 if (p->
network == NULL) {
return; }
155 if (bytes_sent < 0) {
156 perror(
"udp_send_message failed");
158 fprintf(stderr,
"udp_send_message: only sent %d bytes instead of %d\n",
171 if (p == NULL) {
return; }
172 if (p->
network == NULL) {
return; }
175 ssize_t test __attribute__((unused)) = sendto(sock->
sockfd, buffer, size, MSG_DONTWAIT,
192 FD_ZERO(&socks_master);
194 int fd __attribute__((unused));
197 FD_SET(
fd, &socks_master);
204 FD_SET(
fd, &socks_master);
211 FD_SET(
fd, &socks_master);
222 socks = socks_master;
224 if (select(fdmax + 1, &socks, NULL, NULL, NULL) < 0) {
225 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.
int udp_char_available(struct udp_periph *p)
Get number of bytes available in receive buffer.
Functions to obtain rt priority or set the nice level.
#define UDP_RX_BUFFER_SIZE
void * network
UDP network.
void udp_receive(struct udp_periph *p)
Read bytes from UDP.
struct sockaddr_in addr_in
static pthread_mutex_t udp_mutex
struct sockaddr_in addr_out
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)
static void * udp_thread(void *data)
check for new udp packets to receive or send.
int udp_socket_create(struct UdpSocket *sock, char *host, int port_out, int port_in, bool broadcast)
Create UDP socket and bind it.
void udp_send_raw(struct udp_periph *p, long fd, uint8_t *buffer, uint16_t size)
Send a packet from another buffer.