|
1 | 1 | #include "server.h" |
2 | 2 | #include "controller.h" |
3 | 3 |
|
4 | | -void conn_err(void *arg, err_t err) { |
5 | | - printf("error!\n"); |
6 | | -} |
7 | | - |
8 | | -void close_conn(struct tcp_pcb *pcb) { |
9 | | - tcp_arg(pcb, NULL); |
10 | | - tcp_sent(pcb, NULL); |
11 | | - tcp_recv(pcb, NULL); |
12 | | - tcp_poll(pcb, NULL, 0); |
13 | | - tcp_err(pcb, NULL); |
14 | | - tcp_close(pcb); |
15 | | -} |
16 | 4 |
|
17 | 5 |
|
18 | | -void reply_controller_data(struct tcp_pcb* pcb) { |
19 | | - unsigned char* buffer = (unsigned char*)malloc(14); // 14 bytes for controller data, see controller.c for more info |
20 | | - read_controller_data(buffer); |
21 | | - tcp_write(pcb, buffer, 14, 0); |
22 | | - tcp_sent(pcb, NULL); // no cb |
23 | | -} |
24 | 6 |
|
25 | | -void accept_controller_data(unsigned char* buffer) { |
| 7 | +void udp_server_recv(void *arg, struct udp_pcb *pcb, struct pbuf* p, struct ip_addr *addr, u16_t port) { |
| 8 | + |
| 9 | + LWIP_UNUSED_ARG(arg); |
| 10 | + if(p == NULL) |
| 11 | + return; |
| 12 | + unsigned char* data = (unsigned char*)p->payload; |
| 13 | + unsigned char command = data[0]; |
| 14 | + pbuf_free(p); |
| 15 | + if(command == 1) { |
| 16 | + unsigned char* cdata_buffer = (unsigned char*)malloc(14); |
| 17 | + read_controller_data(cdata_buffer); |
| 18 | + struct pbuf* t_pbuf = pbuf_alloc(PBUF_TRANSPORT, 14, PBUF_REF); |
| 19 | + t_pbuf->payload = cdata_buffer; |
| 20 | + t_pbuf->len = t_pbuf->tot_len = 14; |
| 21 | + udp_sendto(pcb, t_pbuf, addr, port); |
| 22 | + pbuf_free(t_pbuf); |
| 23 | + free(cdata_buffer); |
| 24 | + }else if(command == 2) { |
| 25 | + //TODO: turn console off |
| 26 | + }else if(command == 3) { |
| 27 | + write_controller_data(data + 1); |
| 28 | + } |
26 | 29 |
|
27 | | - write_controller_data(buffer); |
28 | 30 | } |
29 | 31 |
|
30 | | -err_t server_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { |
31 | | - |
32 | | - if(err == ERR_OK && p != NULL){ |
33 | | - //Inform about data being taken |
34 | | - tcp_recved(pcb, p->tot_len); |
35 | | - |
36 | | - |
37 | | - unsigned char command = ((unsigned char*)p->payload)[0]; |
38 | | - if(command == 1) { |
39 | | - reply_controller_data(pcb); |
40 | | - }else if(command == 2) { |
41 | | - close_conn(pcb); |
42 | | - }else if(command == 3) { |
43 | | - accept_controller_data((unsigned char*)p->payload + 1); |
44 | | - } |
45 | | - |
46 | | - pbuf_free(p); |
47 | | - |
48 | | - tcp_sent(pcb, NULL); |
49 | | - |
50 | | - }else if(err == ERR_OK && p == NULL){ |
51 | | - close_conn(pcb); |
52 | | - }else { |
53 | | - return err; |
54 | | - } |
55 | | - return ERR_OK; |
56 | | - |
57 | | -} |
58 | | - |
59 | | -err_t server_accept(void *arg, struct tcp_pcb *pcb, err_t err) { |
60 | | - LWIP_UNUSED_ARG(arg); |
61 | | - LWIP_UNUSED_ARG(err); |
62 | | - tcp_setprio(pcb, TCP_PRIO_MIN); |
63 | | - |
64 | | - tcp_arg(pcb, NULL); |
65 | | - tcp_recv(pcb, server_recv); |
66 | | - tcp_err(pcb, conn_err); |
67 | | - tcp_poll(pcb, NULL, 0); // no polling |
68 | | - return ERR_OK; |
69 | | -} |
70 | 32 |
|
71 | 33 | void start_server(void) { |
72 | 34 |
|
73 | | - listen_pcb = tcp_new(); |
74 | | - tcp_bind(listen_pcb, IP_ADDR_ANY, 1182); |
75 | | - |
76 | | - listen_pcb = tcp_listen(listen_pcb); |
77 | | - tcp_accept(listen_pcb, server_accept); |
| 35 | + upcb = udp_new(); |
| 36 | + udp_bind(upcb, IP_ADDR_ANY, 1182); |
| 37 | + udp_recv(upcb, udp_server_recv, NULL); |
78 | 38 | } |
79 | | - |
80 | | - |
81 | | - |
82 | | - |
83 | | - |
84 | | - |
0 commit comments