Ethernet-based network support for mbedConnectorInterface.

Dependencies:   libnsdl EthernetInterface mbed-rtos

Dependents:   IoT_LED_demo ServoTest uWater_Project hackathon ... more

Committer:
ansond
Date:
Fri Feb 06 04:48:29 2015 +0000
Revision:
7:ee88023ff81e
Parent:
4:c1fd1d5f18a1
Child:
13:8d864e72cc68
more updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:31292d956e92 1 // NSDL library support functions
ansond 0:31292d956e92 2
ansond 0:31292d956e92 3 #include "mbed.h"
ansond 0:31292d956e92 4 #include "nsdl_support.h"
ansond 0:31292d956e92 5
ansond 0:31292d956e92 6 #include "mbedConnectorInterface.h"
ansond 0:31292d956e92 7
ansond 0:31292d956e92 8 // we have to redefine DBG as its used differently here...
ansond 0:31292d956e92 9 #ifdef DBG
ansond 0:31292d956e92 10 #undef DBG
ansond 0:31292d956e92 11 #endif
ansond 1:cb681dfa1678 12 #define DBG std::printf
ansond 0:31292d956e92 13
ansond 7:ee88023ff81e 14 #include "EthernetInterface.h"
ansond 7:ee88023ff81e 15 #include "UDPSocket.h"
ansond 7:ee88023ff81e 16 #include "Endpoint.h"
ansond 7:ee88023ff81e 17
ansond 0:31292d956e92 18 Endpoint nsp;
ansond 0:31292d956e92 19 UDPSocket server;
ansond 0:31292d956e92 20
ansond 4:c1fd1d5f18a1 21 char null_endpoint_name[] = "";
ansond 4:c1fd1d5f18a1 22 char null_domain[] = "";
ansond 4:c1fd1d5f18a1 23 uint8_t null_ep_type[] = "";
ansond 4:c1fd1d5f18a1 24 uint8_t null_lifetime_ptr[] = "";
ansond 4:c1fd1d5f18a1 25
ansond 0:31292d956e92 26 void *nsdl_alloc(uint16_t size) {
ansond 0:31292d956e92 27 void *chunk = NULL;
ansond 0:31292d956e92 28 if (size > 0) chunk = malloc(size);
ansond 0:31292d956e92 29 if (chunk != NULL && size > 0) memset(chunk,0,size);
ansond 0:31292d956e92 30 return chunk;
ansond 0:31292d956e92 31 }
ansond 0:31292d956e92 32
ansond 0:31292d956e92 33 void nsdl_free(void* ptr_to_free) {
ansond 0:31292d956e92 34 if (ptr_to_free != NULL) free(ptr_to_free);
ansond 0:31292d956e92 35 }
ansond 0:31292d956e92 36
ansond 0:31292d956e92 37 /*
ansond 0:31292d956e92 38 * Create a static resoure
ansond 0:31292d956e92 39 * Only get is allowed
ansond 0:31292d956e92 40 */
ansond 0:31292d956e92 41 void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len)
ansond 0:31292d956e92 42 {
ansond 0:31292d956e92 43 resource_structure->access = SN_GRS_GET_ALLOWED;
ansond 0:31292d956e92 44 resource_structure->mode = SN_GRS_STATIC;
ansond 0:31292d956e92 45 resource_structure->pathlen = pt_len;
ansond 0:31292d956e92 46 resource_structure->path = pt;
ansond 0:31292d956e92 47 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
ansond 0:31292d956e92 48 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
ansond 0:31292d956e92 49 resource_structure->resource = rsc;
ansond 0:31292d956e92 50 resource_structure->resourcelen = rsc_len;
ansond 0:31292d956e92 51 sn_nsdl_create_resource(resource_structure);
ansond 0:31292d956e92 52 }
ansond 0:31292d956e92 53
ansond 0:31292d956e92 54 void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right)
ansond 0:31292d956e92 55 {
ansond 0:31292d956e92 56 resource_structure->access = (sn_grs_resource_acl_e)access_right;
ansond 0:31292d956e92 57 resource_structure->resource = 0;
ansond 0:31292d956e92 58 resource_structure->resourcelen = 0;
ansond 0:31292d956e92 59 resource_structure->sn_grs_dyn_res_callback = callback_ptr;
ansond 0:31292d956e92 60 resource_structure->mode = SN_GRS_DYNAMIC;
ansond 0:31292d956e92 61 resource_structure->pathlen = pt_len;
ansond 0:31292d956e92 62 resource_structure->path = pt;
ansond 0:31292d956e92 63 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
ansond 0:31292d956e92 64 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
ansond 0:31292d956e92 65 resource_structure->resource_parameters_ptr->observable = is_observable;
ansond 0:31292d956e92 66 sn_nsdl_create_resource(resource_structure);
ansond 0:31292d956e92 67 }
ansond 0:31292d956e92 68
ansond 1:cb681dfa1678 69 sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t *domain, uint8_t* name, uint8_t* typename_ptr, uint8_t *lifetime_ptr) {
ansond 0:31292d956e92 70 if (endpoint_structure == NULL) {
ansond 0:31292d956e92 71 endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
ansond 0:31292d956e92 72 }
ansond 0:31292d956e92 73
ansond 0:31292d956e92 74 if (endpoint_structure != NULL) {
ansond 0:31292d956e92 75 memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
ansond 0:31292d956e92 76 endpoint_structure->endpoint_name_ptr = name;
ansond 0:31292d956e92 77 endpoint_structure->endpoint_name_len = strlen((char*)name);
ansond 1:cb681dfa1678 78 endpoint_structure->domain_name_ptr = domain;
ansond 1:cb681dfa1678 79 endpoint_structure->domain_name_len = strlen((char *)domain);
ansond 0:31292d956e92 80 endpoint_structure->type_ptr = typename_ptr;
ansond 0:31292d956e92 81 endpoint_structure->type_len = strlen((char*)typename_ptr);
ansond 0:31292d956e92 82 endpoint_structure->lifetime_ptr = lifetime_ptr;
ansond 0:31292d956e92 83 endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr);
ansond 0:31292d956e92 84 }
ansond 0:31292d956e92 85
ansond 0:31292d956e92 86 return endpoint_structure;
ansond 0:31292d956e92 87 }
ansond 0:31292d956e92 88
ansond 0:31292d956e92 89 void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) {
ansond 0:31292d956e92 90 if (endpoint_structure != NULL) {
ansond 0:31292d956e92 91 if (*endpoint_structure != NULL) {
ansond 0:31292d956e92 92 nsdl_free(*endpoint_structure);
ansond 0:31292d956e92 93 *endpoint_structure = NULL;
ansond 0:31292d956e92 94 }
ansond 0:31292d956e92 95 }
ansond 0:31292d956e92 96 }
ansond 0:31292d956e92 97
ansond 0:31292d956e92 98 static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr) {
ansond 1:cb681dfa1678 99 //DBG("NSP: sending %d bytes...\r\n",data_len);
ansond 0:31292d956e92 100 int sent = server.sendTo(nsp, (char*)data_ptr, data_len);
ansond 0:31292d956e92 101 return 1;
ansond 0:31292d956e92 102 }
ansond 0:31292d956e92 103
ansond 0:31292d956e92 104 static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) {
ansond 0:31292d956e92 105 // Rx callback process it...
ansond 1:cb681dfa1678 106 //DBG("NSP: received data. processing...\r\n");
ansond 0:31292d956e92 107 return 0;
ansond 0:31292d956e92 108 }
ansond 0:31292d956e92 109
ansond 0:31292d956e92 110 void register_endpoint(bool init) {
ansond 0:31292d956e92 111 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
ansond 1:cb681dfa1678 112 if (init) {
ansond 4:c1fd1d5f18a1 113 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t *)domain_name, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
ansond 1:cb681dfa1678 114 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
ansond 1:cb681dfa1678 115 DBG("NSP initial registration failed\r\n");
ansond 1:cb681dfa1678 116 else
ansond 1:cb681dfa1678 117 DBG("NSP initial registration OK\r\n");
ansond 1:cb681dfa1678 118 }
ansond 1:cb681dfa1678 119 else {
ansond 4:c1fd1d5f18a1 120 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t *)null_domain, (uint8_t*)null_endpoint_name, null_ep_type, null_lifetime_ptr);
ansond 4:c1fd1d5f18a1 121 if(sn_nsdl_update_registration(endpoint_ptr) != 0)
ansond 1:cb681dfa1678 122 DBG("NSP re-registration failed\r\n");
ansond 1:cb681dfa1678 123 else
ansond 1:cb681dfa1678 124 DBG("NSP re-registration OK\r\n");
ansond 1:cb681dfa1678 125 }
ansond 0:31292d956e92 126 nsdl_clean_register_endpoint(&endpoint_ptr);
ansond 0:31292d956e92 127 }
ansond 0:31292d956e92 128
ansond 0:31292d956e92 129 void registration_update_thread(void const *args) {
ansond 1:cb681dfa1678 130 int count = -1;
ansond 1:cb681dfa1678 131 int registration_time = 15; // about 30 seconds if iteration every 2 seconds
ansond 0:31292d956e92 132
ansond 0:31292d956e92 133 // first we want to wait a bit... let the endpoint crank up...
ansond 1:cb681dfa1678 134 Thread::wait(NSP_RD_UPDATE_PERIOD); // wait about 30 seconds, then go ahead and start the re-registration interval...
ansond 0:31292d956e92 135
ansond 0:31292d956e92 136 // now loop this thread forever sleeping and re-registering at the right iteration.
ansond 0:31292d956e92 137 while(true) {
ansond 0:31292d956e92 138 Thread::wait(MAIN_LOOP_SLEEP);
ansond 0:31292d956e92 139 ++count;
ansond 0:31292d956e92 140 if (count%registration_time == 0) {
ansond 0:31292d956e92 141 // re-registration time!
ansond 0:31292d956e92 142 count = 0;
ansond 0:31292d956e92 143 DBG("NSP: (re)registering...\r\n");
ansond 0:31292d956e92 144 register_endpoint(false);
ansond 0:31292d956e92 145 DBG("NSP: (re)registering complete.\r\n");
ansond 0:31292d956e92 146 }
ansond 0:31292d956e92 147 }
ansond 0:31292d956e92 148 }
ansond 0:31292d956e92 149
ansond 0:31292d956e92 150 void nsdl_init() {
ansond 0:31292d956e92 151 sn_nsdl_mem_s memory_cbs;
ansond 0:31292d956e92 152
ansond 1:cb681dfa1678 153 // initilize the UDP channel
ansond 1:cb681dfa1678 154 server.init();
ansond 1:cb681dfa1678 155 server.bind(nsp_port);
ansond 1:cb681dfa1678 156
ansond 0:31292d956e92 157 /* Initialize libNsdl */
ansond 0:31292d956e92 158 memset(&memory_cbs,0,sizeof(memory_cbs));
ansond 0:31292d956e92 159 memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
ansond 0:31292d956e92 160 memory_cbs.sn_nsdl_free = &nsdl_free;
ansond 0:31292d956e92 161 if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) {
ansond 1:cb681dfa1678 162 DBG("NSP: libNsdl init failed.\r\n");
ansond 0:31292d956e92 163 }
ansond 0:31292d956e92 164 else {
ansond 1:cb681dfa1678 165 DBG("NSP: libNsdl init successful.\r\n");
ansond 0:31292d956e92 166 }
ansond 3:3e1ad8ab5071 167 }
ansond 0:31292d956e92 168
ansond 3:3e1ad8ab5071 169 void nsdl_set_nsp_address(void) {
ansond 4:c1fd1d5f18a1 170 char NSP_address_str[16];
ansond 4:c1fd1d5f18a1 171
ansond 0:31292d956e92 172 /* Set nsp address for library */
ansond 4:c1fd1d5f18a1 173 sprintf(NSP_address_str,"%d.%d.%d.%d",NSP_address_bytes[0],NSP_address_bytes[1],NSP_address_bytes[2],NSP_address_bytes[3]);
ansond 4:c1fd1d5f18a1 174 DBG("NSP: libNsdl NSP_ADDRESS: %s port: %d\r\n",NSP_address_str,nsp_port);
ansond 1:cb681dfa1678 175 set_NSP_address(NSP_address_bytes, nsp_port, SN_NSDL_ADDRESS_TYPE_IPV4);
ansond 4:c1fd1d5f18a1 176 nsp.set_address(NSP_address_str,nsp_port);
ansond 0:31292d956e92 177 }
ansond 0:31292d956e92 178
ansond 0:31292d956e92 179 // NSP event loop - spawn a re-registration thread AFTER we have initially registered and begun event processing...
ansond 0:31292d956e92 180 void nsdl_event_loop() {
ansond 0:31292d956e92 181 sn_nsdl_addr_s received_packet_address;
ansond 0:31292d956e92 182 Endpoint from;
ansond 0:31292d956e92 183 uint8_t nsp_received_address[4];
ansond 0:31292d956e92 184 char nsp_buffer[1024];
ansond 0:31292d956e92 185
ansond 0:31292d956e92 186 memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
ansond 0:31292d956e92 187 memset(nsp_received_address, 0, sizeof(nsp_received_address));
ansond 0:31292d956e92 188 received_packet_address.addr_ptr = nsp_received_address;
ansond 0:31292d956e92 189
ansond 0:31292d956e92 190 // start the registration update thread.. it will wait a bit while the endpoint gins up...
ansond 0:31292d956e92 191 Thread registration_thread(registration_update_thread);
ansond 0:31292d956e92 192
ansond 0:31292d956e92 193 // FOREVER: main loop for event processing
ansond 0:31292d956e92 194 while(true) {
ansond 1:cb681dfa1678 195 //DBG("NSP: waiting for data...\r\n");
ansond 0:31292d956e92 196 int n = server.receiveFrom(from,nsp_buffer,sizeof(nsp_buffer));
ansond 0:31292d956e92 197
ansond 1:cb681dfa1678 198 //DBG("NSP: received %d bytes... processing..\r\n.",n);
ansond 0:31292d956e92 199 if (n >= 0) sn_nsdl_process_coap((uint8_t*)nsp_buffer,n,&received_packet_address);
ansond 0:31292d956e92 200 }
ansond 0:31292d956e92 201 }