Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed
device.cpp
00001 #include "mbed.h" 00002 00003 using namespace mbed; 00004 00005 Ethernet *eth; 00006 #ifdef __cplusplus 00007 extern "C" { 00008 #endif 00009 00010 #include "lwip/opt.h" 00011 00012 #include "lwip/def.h" 00013 #include "lwip/pbuf.h" 00014 #include "lwip/sys.h" 00015 #include "lwip/stats.h" 00016 #include "netif/etharp.h" 00017 #include "string.h" 00018 00019 #define IFNAME0 'E' 00020 #define IFNAME1 'X' 00021 00022 #define min(x,y) (((x)<(y))?(x):(y)) 00023 00024 struct netif *gnetif; 00025 00026 static err_t device_output(struct netif *netif, struct pbuf *p) { 00027 #if ETH_PAD_SIZE 00028 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */ 00029 #endif 00030 00031 do { 00032 eth->write((const char *)p->payload, p->len); 00033 } while((p = p->next)!=NULL); 00034 00035 eth->send(); 00036 00037 #if ETH_PAD_SIZE 00038 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */ 00039 #endif 00040 00041 LINK_STATS_INC(link.xmit); 00042 return ERR_OK; 00043 } 00044 00045 void device_poll() { 00046 struct eth_hdr *ethhdr; 00047 struct pbuf *frame, *p; 00048 int len, read; 00049 00050 while((len = eth->receive()) != 0) { 00051 frame = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); 00052 if(frame == NULL) { 00053 return; 00054 } 00055 p = frame; 00056 do { 00057 read = eth->read((char *)p->payload, p->len); 00058 p = p->next; 00059 } while(p != NULL && read != 0); 00060 00061 #if ETH_PAD_SIZE 00062 pbuf_header(p, ETH_PAD_SIZE); 00063 #endif 00064 00065 ethhdr = (struct eth_hdr *)(frame->payload); 00066 00067 switch(htons(ethhdr->type)) { 00068 00069 case ETHTYPE_IP: 00070 etharp_ip_input(gnetif, frame); 00071 pbuf_header(frame, -((s16_t) sizeof(struct eth_hdr))); 00072 gnetif->input(frame, gnetif); 00073 break; 00074 00075 case ETHTYPE_ARP: 00076 etharp_arp_input(gnetif, (struct eth_addr *)(gnetif->hwaddr), frame); 00077 break; 00078 00079 default: 00080 break; 00081 } 00082 pbuf_free(frame); 00083 } 00084 } 00085 00086 err_t device_init(struct netif *netif) { 00087 printf("device_init\r\n"); 00088 LWIP_ASSERT("netif != NULL", (netif != NULL)); 00089 00090 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 0x2EA); 00091 00092 /* maximum transfer unit */ 00093 netif->mtu = 0x2EA; 00094 00095 /* device capabilities */ 00096 /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */ 00097 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP; 00098 00099 netif->state = NULL; 00100 gnetif = netif; 00101 00102 netif->name[0] = IFNAME0; 00103 netif->name[1] = IFNAME1; 00104 00105 /* We directly use etharp_output() here to save a function call. 00106 * You can instead declare your own function an call etharp_output() 00107 * from it if you have to do some checks before sending (e.g. if link 00108 * is available...) */ 00109 netif->output = etharp_output; 00110 netif->linkoutput = device_output; 00111 00112 eth = new Ethernet(); 00113 00114 return ERR_OK; 00115 } 00116 00117 void device_address(char *mac) { 00118 eth->address(mac); 00119 } 00120 00121 #ifdef __cplusplus 00122 }; 00123 #endif
Generated on Tue Jul 12 2022 19:17:23 by 1.7.2