00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <string.h>
00042
00043 #include "net/uip.h"
00044 #include "net/uip-udp-packet.h"
00045 #include "net/uip-netif.h"
00046 #include "net/rime/rime-udp.h"
00047 #include "net/packetbuf.h"
00048
00049 #define DEBUG 0
00050 #if DEBUG
00051 #include <stdio.h>
00052 #define PRINTF(...) printf(__VA_ARGS__)
00053 #define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_t *)addr)[15])
00054 #define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x ",(lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3],(lladdr)->addr[4], (lladdr)->addr[5])
00055 #else
00056 #define PRINTF(...)
00057 #define PRINT6ADDR(addr)
00058 #define PRINTLLADDR(addr)
00059 #endif
00060
00061 #ifndef RIME_CONF_UDP_PORT
00062 #define RIME_UDP_PORT 9508
00063 #else
00064 #define RIME_UDP_PORT RIME_CONF_UDP_PORT
00065 #endif
00066
00067 static struct uip_udp_conn *broadcast_conn;
00068 static struct uip_udp_conn *unicast_conn;
00069
00070 static void (* receiver_callback)(const struct mac_driver *);
00071
00072 PROCESS(rime_udp_process, "Rime over UDP process");
00073
00074 PROCESS_THREAD(rime_udp_process, ev, data)
00075 {
00076 static uip_ipaddr_t ipaddr;
00077
00078 PROCESS_BEGIN();
00079
00080 broadcast_conn = udp_broadcast_new(UIP_HTONS(RIME_UDP_PORT), NULL);
00081 if(broadcast_conn == NULL) {
00082 PRINTF("rime-udp: Failed to allocate a broadcast connection!\n");
00083 }
00084
00085 uip_create_unspecified(&ipaddr);
00086 unicast_conn = udp_new(&ipaddr, UIP_HTONS(RIME_UDP_PORT), NULL);
00087 if(unicast_conn == NULL) {
00088 PRINTF("rime-udp: Failed to allocate a unicast connection!\n");
00089 }
00090
00091 udp_bind(unicast_conn, UIP_HTONS(RIME_UDP_PORT));
00092
00093 while(1) {
00094 PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
00095 if(uip_newdata()) {
00096 packetbuf_clear();
00097 memmove(packetbuf_hdrptr(), uip_appdata, uip_datalen());
00098 PRINTF("rime-udp: received %d bytes\n", uip_datalen());
00099 receiver_callback(&rime_udp_driver);
00100 }
00101 }
00102
00103 PROCESS_END();
00104 }
00105
00106 static void
00107 send_packet(mac_callback_t sent_callback, void *ptr)
00108 {
00109 const rimeaddr_t *addr;
00110
00111 addr = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
00112 PRINTF("rime-udp: Sending %d bytes to %d.%d\n", packetbuf_totlen(),
00113 addr->u8[0], addr->u8[1]);
00114
00115 if(rimeaddr_cmp(&rimeaddr_null, addr)) {
00116 uip_udp_packet_send(broadcast_conn,
00117 packetbuf_hdrptr(), packetbuf_totlen());
00118 mac_call_sent_callback(sent_callback, ptr, MAC_TX_OK, 1);
00119 } else {
00120 uip_ip6addr(&unicast_conn->ripaddr, 0xfe80, 0, 0, 0, 0, 0, 0, 0);
00121 uip_netif_addr_autoconf_set(&unicast_conn->ripaddr, (uip_lladdr_t *)addr);
00122 uip_udp_packet_send(unicast_conn,
00123 packetbuf_hdrptr(), packetbuf_totlen());
00124 uip_create_unspecified(&unicast_conn->ripaddr);
00125 }
00126 return;
00127 }
00128
00129 static int
00130 input_packet(void)
00131 {
00132 packetbuf_set_datalen(uip_datalen());
00133 return uip_datalen();
00134 }
00135
00136 static void
00137 set_receive_function(void (* recv)(const struct mac_driver *))
00138 {
00139 receiver_callback = recv;
00140 }
00141
00142 static int
00143 on(void)
00144 {
00145 return 1;
00146 }
00147
00148 static int
00149 off(int keep_radio_on)
00150 {
00151 return 0;
00152 }
00153
00154 static unsigned short
00155 check_interval(void)
00156 {
00157 return 0;
00158 }
00159
00160 static int
00161 init(void)
00162 {
00163 process_start(&rime_udp_process, NULL);
00164 return 1;
00165 }
00166
00167 const struct mac_driver rime_udp_driver = {
00168 "rime-udp",
00169 init,
00170 send_packet,
00171 input_packet,
00172 on,
00173 off,
00174 check_interval,
00175 };
00176