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