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;
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");
75 p->network = (
void *)sock;
86 int available =
p->rx_insert_idx -
p->rx_extract_idx;
102 uint8_t ret =
p->rx_buf[
p->rx_extract_idx];
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);
132 for (i = 0; i < byte_read; i++) {
133 p->rx_buf[
p->rx_insert_idx] = buf[i];
146 if (
p == NULL) {
return; }
147 if (
p->network == NULL) {
return; }
151 if (
p->tx_insert_idx > 0) {
152 ssize_t bytes_sent = sendto(sock->
sockfd,
p->tx_buf,
p->tx_insert_idx, MSG_DONTWAIT,
154 if (bytes_sent !=
p->tx_insert_idx) {
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",
159 (
int)bytes_sent,
p->tx_insert_idx);
162 p->tx_insert_idx = 0;
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)) {
if(GpsFixValid() &&e_identification_started)
void udp_receive(struct udp_periph *p)
Read bytes from UDP.
void udp_arch_periph_init(struct udp_periph *p, char *host, int port_out, int port_in, bool broadcast)
Initialize the UDP peripheral.
void udp_send_raw(struct udp_periph *p, long fd, uint8_t *buffer, uint16_t size)
Send a packet from another buffer.
static void * udp_thread(void *data)
check for new udp packets to receive or send.
static pthread_mutex_t udp_mutex
uint8_t udp_getch(struct udp_periph *p)
Get the last character from the receive buffer.
int udp_char_available(struct udp_periph *p)
Get number of bytes available in receive buffer.
void udp_send_message(struct udp_periph *p, long fd)
Send a message.
Functions to obtain rt priority or set the nice level.
static int get_rt_prio(int prio)
#define UDP_RX_BUFFER_SIZE
int udp_socket_create(struct UdpSocket *sock, char *host, int port_out, int port_in, bool broadcast)
Create UDP socket and bind it.
Easily create and use UDP sockets.
struct sockaddr_in addr_in
struct sockaddr_in addr_out
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
short int16_t
Typedef defining 16 bit short type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.