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
- Committer:
- ansond
- Date:
- 2015-03-05
- Revision:
- 15:3c021e52addd
- Parent:
- 11:d601b867b297
- Child:
- 16:fb9c3f2af2df
File content as of revision 15:3c021e52addd:
// NSDL library support functions #include "mbed.h" #include "nsdl_support.h" #include "mbedConnectorInterface.h" // we have to redefine DBG as its used differently here... #ifdef DBG #undef DBG #endif #define DBG std::printf #include "UDPSocket.h" #include "Endpoint.h" #include "BLEDevice.h" #define NSDL_BUFFER_LENGTH 1024 extern BLEDevice ble; Endpoint nsp; UDPSocket server; Endpoint from; uint8_t nsp_received_address[4]; char nsp_buffer[NSDL_BUFFER_LENGTH]; char null_endpoint_name[] = ""; char null_domain[] = ""; uint8_t null_ep_type[] = ""; uint8_t null_lifetime_ptr[] = ""; void *nsdl_alloc(uint16_t size) { void *chunk = NULL; if (size > 0) chunk = malloc(size); if (chunk != NULL && size > 0) memset(chunk,0,size); return chunk; } void nsdl_free(void* ptr_to_free) { if (ptr_to_free != NULL) free(ptr_to_free); } /* * Create a static resoure * Only get is allowed */ 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) { resource_structure->access = SN_GRS_GET_ALLOWED; resource_structure->mode = SN_GRS_STATIC; resource_structure->pathlen = pt_len; resource_structure->path = pt; resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; resource_structure->resource = rsc; resource_structure->resourcelen = rsc_len; sn_nsdl_create_resource(resource_structure); } 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) { resource_structure->access = (sn_grs_resource_acl_e)access_right; resource_structure->resource = 0; resource_structure->resourcelen = 0; resource_structure->sn_grs_dyn_res_callback = callback_ptr; resource_structure->mode = SN_GRS_DYNAMIC; resource_structure->pathlen = pt_len; resource_structure->path = pt; resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; resource_structure->resource_parameters_ptr->observable = is_observable; sn_nsdl_create_resource(resource_structure); } 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) { if (endpoint_structure == NULL) { endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s)); } if (endpoint_structure != NULL) { memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s)); endpoint_structure->endpoint_name_ptr = name; endpoint_structure->endpoint_name_len = strlen((char*)name); endpoint_structure->domain_name_ptr = domain; endpoint_structure->domain_name_len = strlen((char *)domain); endpoint_structure->type_ptr = typename_ptr; endpoint_structure->type_len = strlen((char*)typename_ptr); endpoint_structure->lifetime_ptr = lifetime_ptr; endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr); } return endpoint_structure; } void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) { if (endpoint_structure != NULL) { if (*endpoint_structure != NULL) { nsdl_free(*endpoint_structure); *endpoint_structure = NULL; } } } static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr) { DBG("NSP: tx_cb(): sending %d bytes...\r\n",data_len); int sent = server.sendTo(nsp,(char*)data_ptr, data_len); return 1; } static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) { // Rx callback process it... DBG("NSP: rx_cb(): received data. processing...\r\n"); return 0; } void register_endpoint(bool init) { sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; if (init) { endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t *)domain_name, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) DBG("NSP initial registration failed\r\n"); else DBG("NSP initial registration OK\r\n"); } else { endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t *)null_domain, (uint8_t*)null_endpoint_name, null_ep_type, null_lifetime_ptr); if(sn_nsdl_update_registration(endpoint_ptr) != 0) DBG("NSP re-registration failed\r\n"); else DBG("NSP re-registration OK\r\n"); } nsdl_clean_register_endpoint(&endpoint_ptr); } // Simple Ticker-based re-registration updating... void registration_update_tick(void) { extern bool __registered; if (__registered) { DBG("NSP: (re)registering...\r\n"); register_endpoint(false); DBG("NSP: (re)registering complete.\r\n"); } } void nsdl_init() { sn_nsdl_mem_s memory_cbs; // initilize the UDP channel server.init(); server.bind(nsp_port); /* Initialize libNsdl */ memset(&memory_cbs,0,sizeof(memory_cbs)); memory_cbs.sn_nsdl_alloc = &nsdl_alloc; memory_cbs.sn_nsdl_free = &nsdl_free; if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) { DBG("NSP: libNsdl init failed.\r\n"); } else { DBG("NSP: libNsdl init successful.\r\n"); } } void nsdl_set_nsp_address(void) { char NSP_address_str[16]; /* Set nsp address for library */ sprintf(NSP_address_str,"%d.%d.%d.%d",NSP_address_bytes[0],NSP_address_bytes[1],NSP_address_bytes[2],NSP_address_bytes[3]); DBG("NSP: libNsdl NSP_ADDRESS: %s port: %d\r\n",NSP_address_str,nsp_port); set_NSP_address(NSP_address_bytes, nsp_port, SN_NSDL_ADDRESS_TYPE_IPV4); nsp.set_address(NSP_address_str,nsp_port); } extern "C" volatile bool __registered; // NSP event loop - spawn a re-registration thread AFTER we have initially registered and begun event processing... void nsdl_event_loop() { sn_nsdl_addr_s received_packet_address; memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); memset(nsp_received_address, 0, sizeof(nsp_received_address)); received_packet_address.addr_ptr = nsp_received_address; // re-registration update ticker... Ticker ticker; ticker.attach(registration_update_tick,30); // every 30 seconds... // FOREVER: main loop for event processing while(true) { // wait for BLE events... ble.waitForEvent(); // only process if we are registered and thus connected... otherwise ignore if (__registered == true) { //DBG("NSP: waiting for data...\r\n"); memset(nsp_buffer,0,NSDL_BUFFER_LENGTH); int n = server.receiveFrom(from,nsp_buffer,NSDL_BUFFER_LENGTH); if (n > 0) { DBG("NSP: received %d bytes... processing..\r\n.",n); sn_nsdl_process_coap((uint8_t*)nsp_buffer,n,&received_packet_address); } } } }