Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed
main.cpp
00001 /* Pehr Hovey 00002 Tests with UDP for mBed 00003 */ 00004 00005 #include "mbed.h" 00006 #include "lwip/init.h" 00007 #include "lwip/opt.h" 00008 #include "lwip/udp.h" 00009 00010 00011 00012 #include "NetServer.h" 00013 00014 #include "ipv4/lwip/ip_addr.h" 00015 00016 int send_test = 0; //toggled by button 00017 00018 struct udp_pcb *pcb; 00019 00020 InterruptIn button(p8); 00021 00022 DigitalOut got_udp(LED1); 00023 00024 DigitalOut sent_udp(LED4); 00025 00026 //callback for button interrupt 00027 void sendBroadcast(){ 00028 //broadcast something 00029 struct pbuf *p; 00030 char msg[]="testing mBedUDP"; 00031 p = pbuf_alloc(PBUF_TRANSPORT,sizeof(msg),PBUF_RAM); 00032 memcpy (p->payload, msg, sizeof(msg)); 00033 err_t res = udp_sendto(pcb, p, IP_ADDR_BROADCAST, 5555); //broadcast test msg 00034 pbuf_free(p); //De-allocate packet buffer 00035 printf("Sent test message to port 1234, result is %d Waiting...\r\n",res); 00036 } 00037 00038 //Send arbitrary data 00039 static void udp_send_something(char msg[], struct udp_pcb *upcb, struct ip_addr *addr, u16_t port) 00040 { 00041 printf("Sending message to port %d: %s\r\n",port,msg); 00042 sent_udp = 1; 00043 int psize = 40; //pick a size for now... 00044 struct pbuf *q; 00045 q = pbuf_alloc(PBUF_TRANSPORT,psize, PBUF_RAM); 00046 printf("Making packet with size %d\r\n",psize); 00047 if (q != NULL) { 00048 sent_udp = 1; 00049 q->payload = msg; //Assign the string pointer 00050 00051 err_t code; 00052 /* send UDP response to IP addr and port specified */ 00053 code = udp_sendto(upcb, q, IP_ADDR_BROADCAST, port); 00054 printf("Sent, result code is %d\r\n",code); 00055 /* free the "reference" pbuf */ 00056 pbuf_free(q); 00057 }else{ 00058 printf("Could not allocate packet buffer!\r\n"); 00059 } 00060 sent_udp = 0; 00061 } 00062 00063 /**UDP recv callback */ 00064 static void udp_test_recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, struct ip_addr *addr, u16_t port) 00065 { 00066 printf("Received UDP Packet on port %d\r\n",port); 00067 // printf("Received UDP Packet from ip_addr %s\r\n", inet_ntoa(*(struct in_addr*)&(ip_addr))); 00068 LWIP_UNUSED_ARG(arg); 00069 got_udp = 1; 00070 /* if packet is valid */ 00071 if (p != NULL) { 00072 //Try to print out what we received 00073 printf("UDP Packet Received! Payload:\r\n"); 00074 printf("-- %s --\r\n",static_cast<char *>(p->payload)); 00075 err_t code = udp_sendto(upcb, p, IP_ADDR_BROADCAST, 5555); //send it back to port 5555 00076 printf("Echo'd packet, result code is %d\r\n",code); 00077 00078 00079 /* free the pbuf */ 00080 pbuf_free(p); 00081 got_udp=0; 00082 sent_udp=0; 00083 } 00084 } 00085 00086 00087 int main() { 00088 //Attach interrupt to the button to send packet on press 00089 button.rise(&sendBroadcast); 00090 00091 /*Initialize NetServer which gets us our DHCP address and 00092 gets the network interface ready 00093 */ 00094 NetServer *net = NetServer::ready(); 00095 00096 00097 //Initialize UDP 00098 00099 pcb = udp_new(); 00100 00101 00102 00103 if (pcb != NULL) { 00104 /* we have to be allowed to send broadcast packets! */ 00105 pcb->so_options |= SOF_BROADCAST; 00106 udp_bind(pcb, IP_ADDR_ANY, 4444); //Receive from any IP address, on the specified port 00107 udp_recv(pcb,udp_test_recv, NULL); 00108 }else{ 00109 printf("Could not make UDP pcb\r\n"); 00110 } 00111 00112 00113 net->waitUntilReady(); //this will print IP 00114 net->setHostname("chapala"); 00115 00116 //Send a startup packet 00117 printf("UDP Tester started...\r\n"); 00118 00119 while(1) { 00120 net->poll(); 00121 00122 00123 } 00124 }
Generated on Tue Jul 12 2022 19:17:23 by 1.7.2