BLE mbed Endpoint network stack for mbedConnectorInterface. The stack makes use of a special BLE Socket abstraction to create socket() semantics over BLE.
Dependencies: libnsdl_m0 BLE_API Base64 nRF51822 SplitterAssembler
NSDL/nsdl_support.cpp@7:203c348ccc66, 2015-02-17 (annotated)
- Committer:
- ansond
- Date:
- Tue Feb 17 06:59:54 2015 +0000
- Revision:
- 7:203c348ccc66
- Parent:
- 6:98af441fd960
- Child:
- 9:bf0cf5828378
updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:7809547930d9 | 1 | // NSDL library support functions |
ansond | 0:7809547930d9 | 2 | |
ansond | 0:7809547930d9 | 3 | #include "mbed.h" |
ansond | 0:7809547930d9 | 4 | #include "nsdl_support.h" |
ansond | 0:7809547930d9 | 5 | |
ansond | 0:7809547930d9 | 6 | #include "mbedConnectorInterface.h" |
ansond | 0:7809547930d9 | 7 | |
ansond | 0:7809547930d9 | 8 | // we have to redefine DBG as its used differently here... |
ansond | 0:7809547930d9 | 9 | #ifdef DBG |
ansond | 0:7809547930d9 | 10 | #undef DBG |
ansond | 0:7809547930d9 | 11 | #endif |
ansond | 0:7809547930d9 | 12 | #define DBG std::printf |
ansond | 0:7809547930d9 | 13 | |
ansond | 0:7809547930d9 | 14 | #include "UDPSocket.h" |
ansond | 0:7809547930d9 | 15 | #include "Endpoint.h" |
ansond | 0:7809547930d9 | 16 | |
ansond | 2:30f4a0dab604 | 17 | #include "BLEDevice.h" |
ansond | 2:30f4a0dab604 | 18 | |
ansond | 7:203c348ccc66 | 19 | #define NSDL_BUFFER_LENGTH 1024 |
ansond | 7:203c348ccc66 | 20 | |
ansond | 2:30f4a0dab604 | 21 | extern BLEDevice ble; |
ansond | 2:30f4a0dab604 | 22 | |
ansond | 0:7809547930d9 | 23 | Endpoint nsp; |
ansond | 0:7809547930d9 | 24 | UDPSocket server; |
ansond | 0:7809547930d9 | 25 | |
ansond | 0:7809547930d9 | 26 | char null_endpoint_name[] = ""; |
ansond | 0:7809547930d9 | 27 | char null_domain[] = ""; |
ansond | 0:7809547930d9 | 28 | uint8_t null_ep_type[] = ""; |
ansond | 0:7809547930d9 | 29 | uint8_t null_lifetime_ptr[] = ""; |
ansond | 0:7809547930d9 | 30 | |
ansond | 0:7809547930d9 | 31 | void *nsdl_alloc(uint16_t size) { |
ansond | 0:7809547930d9 | 32 | void *chunk = NULL; |
ansond | 0:7809547930d9 | 33 | if (size > 0) chunk = malloc(size); |
ansond | 0:7809547930d9 | 34 | if (chunk != NULL && size > 0) memset(chunk,0,size); |
ansond | 0:7809547930d9 | 35 | return chunk; |
ansond | 0:7809547930d9 | 36 | } |
ansond | 0:7809547930d9 | 37 | |
ansond | 0:7809547930d9 | 38 | void nsdl_free(void* ptr_to_free) { |
ansond | 0:7809547930d9 | 39 | if (ptr_to_free != NULL) free(ptr_to_free); |
ansond | 0:7809547930d9 | 40 | } |
ansond | 0:7809547930d9 | 41 | |
ansond | 0:7809547930d9 | 42 | /* |
ansond | 0:7809547930d9 | 43 | * Create a static resoure |
ansond | 0:7809547930d9 | 44 | * Only get is allowed |
ansond | 0:7809547930d9 | 45 | */ |
ansond | 0:7809547930d9 | 46 | 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:7809547930d9 | 47 | { |
ansond | 0:7809547930d9 | 48 | resource_structure->access = SN_GRS_GET_ALLOWED; |
ansond | 0:7809547930d9 | 49 | resource_structure->mode = SN_GRS_STATIC; |
ansond | 0:7809547930d9 | 50 | resource_structure->pathlen = pt_len; |
ansond | 0:7809547930d9 | 51 | resource_structure->path = pt; |
ansond | 0:7809547930d9 | 52 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; |
ansond | 0:7809547930d9 | 53 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; |
ansond | 0:7809547930d9 | 54 | resource_structure->resource = rsc; |
ansond | 0:7809547930d9 | 55 | resource_structure->resourcelen = rsc_len; |
ansond | 0:7809547930d9 | 56 | sn_nsdl_create_resource(resource_structure); |
ansond | 0:7809547930d9 | 57 | } |
ansond | 0:7809547930d9 | 58 | |
ansond | 0:7809547930d9 | 59 | 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:7809547930d9 | 60 | { |
ansond | 0:7809547930d9 | 61 | resource_structure->access = (sn_grs_resource_acl_e)access_right; |
ansond | 0:7809547930d9 | 62 | resource_structure->resource = 0; |
ansond | 0:7809547930d9 | 63 | resource_structure->resourcelen = 0; |
ansond | 0:7809547930d9 | 64 | resource_structure->sn_grs_dyn_res_callback = callback_ptr; |
ansond | 0:7809547930d9 | 65 | resource_structure->mode = SN_GRS_DYNAMIC; |
ansond | 0:7809547930d9 | 66 | resource_structure->pathlen = pt_len; |
ansond | 0:7809547930d9 | 67 | resource_structure->path = pt; |
ansond | 0:7809547930d9 | 68 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; |
ansond | 0:7809547930d9 | 69 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; |
ansond | 0:7809547930d9 | 70 | resource_structure->resource_parameters_ptr->observable = is_observable; |
ansond | 0:7809547930d9 | 71 | sn_nsdl_create_resource(resource_structure); |
ansond | 0:7809547930d9 | 72 | } |
ansond | 0:7809547930d9 | 73 | |
ansond | 0:7809547930d9 | 74 | 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:7809547930d9 | 75 | if (endpoint_structure == NULL) { |
ansond | 0:7809547930d9 | 76 | endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s)); |
ansond | 0:7809547930d9 | 77 | } |
ansond | 0:7809547930d9 | 78 | |
ansond | 0:7809547930d9 | 79 | if (endpoint_structure != NULL) { |
ansond | 0:7809547930d9 | 80 | memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s)); |
ansond | 0:7809547930d9 | 81 | endpoint_structure->endpoint_name_ptr = name; |
ansond | 0:7809547930d9 | 82 | endpoint_structure->endpoint_name_len = strlen((char*)name); |
ansond | 0:7809547930d9 | 83 | endpoint_structure->domain_name_ptr = domain; |
ansond | 0:7809547930d9 | 84 | endpoint_structure->domain_name_len = strlen((char *)domain); |
ansond | 0:7809547930d9 | 85 | endpoint_structure->type_ptr = typename_ptr; |
ansond | 0:7809547930d9 | 86 | endpoint_structure->type_len = strlen((char*)typename_ptr); |
ansond | 0:7809547930d9 | 87 | endpoint_structure->lifetime_ptr = lifetime_ptr; |
ansond | 0:7809547930d9 | 88 | endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr); |
ansond | 0:7809547930d9 | 89 | } |
ansond | 0:7809547930d9 | 90 | |
ansond | 0:7809547930d9 | 91 | return endpoint_structure; |
ansond | 0:7809547930d9 | 92 | } |
ansond | 0:7809547930d9 | 93 | |
ansond | 0:7809547930d9 | 94 | void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) { |
ansond | 0:7809547930d9 | 95 | if (endpoint_structure != NULL) { |
ansond | 0:7809547930d9 | 96 | if (*endpoint_structure != NULL) { |
ansond | 0:7809547930d9 | 97 | nsdl_free(*endpoint_structure); |
ansond | 0:7809547930d9 | 98 | *endpoint_structure = NULL; |
ansond | 0:7809547930d9 | 99 | } |
ansond | 0:7809547930d9 | 100 | } |
ansond | 0:7809547930d9 | 101 | } |
ansond | 0:7809547930d9 | 102 | |
ansond | 0:7809547930d9 | 103 | 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 | 6:98af441fd960 | 104 | DBG("NSP: tx_cb(): sending %d bytes...\r\n",data_len); |
ansond | 6:98af441fd960 | 105 | int sent = server.sendTo(nsp,(char*)data_ptr, data_len); |
ansond | 0:7809547930d9 | 106 | return 1; |
ansond | 0:7809547930d9 | 107 | } |
ansond | 0:7809547930d9 | 108 | |
ansond | 0:7809547930d9 | 109 | static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) { |
ansond | 0:7809547930d9 | 110 | // Rx callback process it... |
ansond | 6:98af441fd960 | 111 | DBG("NSP: rx_cb(): received data. processing...\r\n"); |
ansond | 0:7809547930d9 | 112 | return 0; |
ansond | 0:7809547930d9 | 113 | } |
ansond | 0:7809547930d9 | 114 | |
ansond | 0:7809547930d9 | 115 | void register_endpoint(bool init) { |
ansond | 0:7809547930d9 | 116 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
ansond | 0:7809547930d9 | 117 | if (init) { |
ansond | 0:7809547930d9 | 118 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t *)domain_name, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
ansond | 0:7809547930d9 | 119 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
ansond | 0:7809547930d9 | 120 | DBG("NSP initial registration failed\r\n"); |
ansond | 0:7809547930d9 | 121 | else |
ansond | 0:7809547930d9 | 122 | DBG("NSP initial registration OK\r\n"); |
ansond | 0:7809547930d9 | 123 | } |
ansond | 0:7809547930d9 | 124 | else { |
ansond | 0:7809547930d9 | 125 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t *)null_domain, (uint8_t*)null_endpoint_name, null_ep_type, null_lifetime_ptr); |
ansond | 0:7809547930d9 | 126 | if(sn_nsdl_update_registration(endpoint_ptr) != 0) |
ansond | 0:7809547930d9 | 127 | DBG("NSP re-registration failed\r\n"); |
ansond | 0:7809547930d9 | 128 | else |
ansond | 0:7809547930d9 | 129 | DBG("NSP re-registration OK\r\n"); |
ansond | 0:7809547930d9 | 130 | } |
ansond | 0:7809547930d9 | 131 | nsdl_clean_register_endpoint(&endpoint_ptr); |
ansond | 0:7809547930d9 | 132 | } |
ansond | 0:7809547930d9 | 133 | |
ansond | 2:30f4a0dab604 | 134 | // Simple Ticker-based re-registration updating... |
ansond | 2:30f4a0dab604 | 135 | void registration_update_tick(void) { |
ansond | 2:30f4a0dab604 | 136 | extern bool __registered; |
ansond | 2:30f4a0dab604 | 137 | if (__registered) { |
ansond | 2:30f4a0dab604 | 138 | DBG("NSP: (re)registering...\r\n"); |
ansond | 2:30f4a0dab604 | 139 | register_endpoint(false); |
ansond | 2:30f4a0dab604 | 140 | DBG("NSP: (re)registering complete.\r\n"); |
ansond | 2:30f4a0dab604 | 141 | } |
ansond | 0:7809547930d9 | 142 | } |
ansond | 0:7809547930d9 | 143 | |
ansond | 0:7809547930d9 | 144 | void nsdl_init() { |
ansond | 0:7809547930d9 | 145 | sn_nsdl_mem_s memory_cbs; |
ansond | 0:7809547930d9 | 146 | |
ansond | 0:7809547930d9 | 147 | // initilize the UDP channel |
ansond | 0:7809547930d9 | 148 | server.init(); |
ansond | 0:7809547930d9 | 149 | server.bind(nsp_port); |
ansond | 0:7809547930d9 | 150 | |
ansond | 0:7809547930d9 | 151 | /* Initialize libNsdl */ |
ansond | 0:7809547930d9 | 152 | memset(&memory_cbs,0,sizeof(memory_cbs)); |
ansond | 0:7809547930d9 | 153 | memory_cbs.sn_nsdl_alloc = &nsdl_alloc; |
ansond | 0:7809547930d9 | 154 | memory_cbs.sn_nsdl_free = &nsdl_free; |
ansond | 0:7809547930d9 | 155 | if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) { |
ansond | 0:7809547930d9 | 156 | DBG("NSP: libNsdl init failed.\r\n"); |
ansond | 0:7809547930d9 | 157 | } |
ansond | 0:7809547930d9 | 158 | else { |
ansond | 0:7809547930d9 | 159 | DBG("NSP: libNsdl init successful.\r\n"); |
ansond | 0:7809547930d9 | 160 | } |
ansond | 0:7809547930d9 | 161 | } |
ansond | 0:7809547930d9 | 162 | |
ansond | 0:7809547930d9 | 163 | void nsdl_set_nsp_address(void) { |
ansond | 0:7809547930d9 | 164 | char NSP_address_str[16]; |
ansond | 0:7809547930d9 | 165 | |
ansond | 0:7809547930d9 | 166 | /* Set nsp address for library */ |
ansond | 0:7809547930d9 | 167 | 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 | 0:7809547930d9 | 168 | DBG("NSP: libNsdl NSP_ADDRESS: %s port: %d\r\n",NSP_address_str,nsp_port); |
ansond | 0:7809547930d9 | 169 | set_NSP_address(NSP_address_bytes, nsp_port, SN_NSDL_ADDRESS_TYPE_IPV4); |
ansond | 0:7809547930d9 | 170 | nsp.set_address(NSP_address_str,nsp_port); |
ansond | 0:7809547930d9 | 171 | } |
ansond | 0:7809547930d9 | 172 | |
ansond | 2:30f4a0dab604 | 173 | extern "C" volatile bool __registered; |
ansond | 2:30f4a0dab604 | 174 | |
ansond | 0:7809547930d9 | 175 | // NSP event loop - spawn a re-registration thread AFTER we have initially registered and begun event processing... |
ansond | 0:7809547930d9 | 176 | void nsdl_event_loop() { |
ansond | 0:7809547930d9 | 177 | sn_nsdl_addr_s received_packet_address; |
ansond | 0:7809547930d9 | 178 | Endpoint from; |
ansond | 0:7809547930d9 | 179 | uint8_t nsp_received_address[4]; |
ansond | 7:203c348ccc66 | 180 | char nsp_buffer[NSDL_BUFFER_LENGTH]; |
ansond | 2:30f4a0dab604 | 181 | |
ansond | 0:7809547930d9 | 182 | memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); |
ansond | 0:7809547930d9 | 183 | memset(nsp_received_address, 0, sizeof(nsp_received_address)); |
ansond | 0:7809547930d9 | 184 | received_packet_address.addr_ptr = nsp_received_address; |
ansond | 0:7809547930d9 | 185 | |
ansond | 2:30f4a0dab604 | 186 | // re-registration update ticker... |
ansond | 2:30f4a0dab604 | 187 | Ticker ticker; |
ansond | 2:30f4a0dab604 | 188 | ticker.attach(registration_update_tick,30); // every 30 seconds... |
ansond | 0:7809547930d9 | 189 | |
ansond | 0:7809547930d9 | 190 | // FOREVER: main loop for event processing |
ansond | 2:30f4a0dab604 | 191 | while(true) { |
ansond | 2:30f4a0dab604 | 192 | // wait for BLE events... |
ansond | 2:30f4a0dab604 | 193 | ble.waitForEvent(); |
ansond | 2:30f4a0dab604 | 194 | |
ansond | 2:30f4a0dab604 | 195 | // only process if we are registered and thus connected... otherwise ignore |
ansond | 5:9233e88b9c83 | 196 | if (__registered == true) { |
ansond | 6:98af441fd960 | 197 | DBG("NSP: waiting for data...\r\n"); |
ansond | 7:203c348ccc66 | 198 | int n = server.receiveFrom(from,nsp_buffer,NSDL_BUFFER_LENGTH); |
ansond | 5:9233e88b9c83 | 199 | if (n >= 0) { |
ansond | 5:9233e88b9c83 | 200 | DBG("NSP: received %d bytes... processing..\r\n.",n); |
ansond | 5:9233e88b9c83 | 201 | sn_nsdl_process_coap((uint8_t*)nsp_buffer,n,&received_packet_address); |
ansond | 5:9233e88b9c83 | 202 | } |
ansond | 2:30f4a0dab604 | 203 | } |
ansond | 0:7809547930d9 | 204 | } |
ansond | 0:7809547930d9 | 205 | } |