6LowPAN mesh-based network support for mbedConnectorInterface. The Atmel-based mbed 6LowPAN shield is the assumed network hardware.
Dependencies: libnsdl Nanostack_lib
NSDL/nsdl_support.cpp@0:2a5a48a8b4d4, 2015-02-01 (annotated)
- Committer:
- ansond
- Date:
- Sun Feb 01 18:26:13 2015 +0000
- Revision:
- 0:2a5a48a8b4d4
- Child:
- 4:2c8eeaf31699
revamped
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:2a5a48a8b4d4 | 1 | // NSDL library support functions |
ansond | 0:2a5a48a8b4d4 | 2 | |
ansond | 0:2a5a48a8b4d4 | 3 | #include "configuration.h" |
ansond | 0:2a5a48a8b4d4 | 4 | #include "mbedConnectorInterface.h" |
ansond | 0:2a5a48a8b4d4 | 5 | #include "debug.h" |
ansond | 0:2a5a48a8b4d4 | 6 | #include "nsdl_support.h" |
ansond | 0:2a5a48a8b4d4 | 7 | #include "socket_api.h" |
ansond | 0:2a5a48a8b4d4 | 8 | |
ansond | 0:2a5a48a8b4d4 | 9 | // we have to redefine DBG as its used differently here... |
ansond | 0:2a5a48a8b4d4 | 10 | #ifdef DBG |
ansond | 0:2a5a48a8b4d4 | 11 | #undef DBG |
ansond | 0:2a5a48a8b4d4 | 12 | #endif |
ansond | 0:2a5a48a8b4d4 | 13 | #define DBG printf |
ansond | 0:2a5a48a8b4d4 | 14 | |
ansond | 0:2a5a48a8b4d4 | 15 | static ns_address_t app_dest; |
ansond | 0:2a5a48a8b4d4 | 16 | char null_ep_name[] = ""; |
ansond | 0:2a5a48a8b4d4 | 17 | char null_ep_domain[] = ""; |
ansond | 0:2a5a48a8b4d4 | 18 | uint8_t null_ep_type[] = ""; |
ansond | 0:2a5a48a8b4d4 | 19 | uint8_t null_lifetime_ptr[] = ""; |
ansond | 0:2a5a48a8b4d4 | 20 | bool nsdl_reg_update_needed = false; |
ansond | 0:2a5a48a8b4d4 | 21 | |
ansond | 0:2a5a48a8b4d4 | 22 | extern int8_t coap_udp_socket; |
ansond | 0:2a5a48a8b4d4 | 23 | |
ansond | 0:2a5a48a8b4d4 | 24 | void *nsdl_alloc(uint16_t size) |
ansond | 0:2a5a48a8b4d4 | 25 | { |
ansond | 0:2a5a48a8b4d4 | 26 | void *buf = malloc(size); |
ansond | 0:2a5a48a8b4d4 | 27 | return buf; |
ansond | 0:2a5a48a8b4d4 | 28 | } |
ansond | 0:2a5a48a8b4d4 | 29 | |
ansond | 0:2a5a48a8b4d4 | 30 | void nsdl_free(void* ptr_to_free) |
ansond | 0:2a5a48a8b4d4 | 31 | { |
ansond | 0:2a5a48a8b4d4 | 32 | free(ptr_to_free); |
ansond | 0:2a5a48a8b4d4 | 33 | } |
ansond | 0:2a5a48a8b4d4 | 34 | |
ansond | 0:2a5a48a8b4d4 | 35 | /* |
ansond | 0:2a5a48a8b4d4 | 36 | * Create a static resoure |
ansond | 0:2a5a48a8b4d4 | 37 | * Only get is allowed |
ansond | 0:2a5a48a8b4d4 | 38 | */ |
ansond | 0:2a5a48a8b4d4 | 39 | 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:2a5a48a8b4d4 | 40 | { |
ansond | 0:2a5a48a8b4d4 | 41 | resource_structure->access = SN_GRS_GET_ALLOWED; |
ansond | 0:2a5a48a8b4d4 | 42 | resource_structure->mode = SN_GRS_STATIC; |
ansond | 0:2a5a48a8b4d4 | 43 | resource_structure->pathlen = pt_len; |
ansond | 0:2a5a48a8b4d4 | 44 | resource_structure->path = pt; |
ansond | 0:2a5a48a8b4d4 | 45 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; |
ansond | 0:2a5a48a8b4d4 | 46 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; |
ansond | 0:2a5a48a8b4d4 | 47 | resource_structure->resource = rsc; |
ansond | 0:2a5a48a8b4d4 | 48 | resource_structure->resourcelen = rsc_len; |
ansond | 0:2a5a48a8b4d4 | 49 | sn_nsdl_create_resource(resource_structure); |
ansond | 0:2a5a48a8b4d4 | 50 | } |
ansond | 0:2a5a48a8b4d4 | 51 | |
ansond | 0:2a5a48a8b4d4 | 52 | 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:2a5a48a8b4d4 | 53 | { |
ansond | 0:2a5a48a8b4d4 | 54 | resource_structure->access = (sn_grs_resource_acl_e)access_right; |
ansond | 0:2a5a48a8b4d4 | 55 | resource_structure->resource = 0; |
ansond | 0:2a5a48a8b4d4 | 56 | resource_structure->resourcelen = 0; |
ansond | 0:2a5a48a8b4d4 | 57 | resource_structure->sn_grs_dyn_res_callback = callback_ptr; |
ansond | 0:2a5a48a8b4d4 | 58 | resource_structure->mode = SN_GRS_DYNAMIC; |
ansond | 0:2a5a48a8b4d4 | 59 | resource_structure->pathlen = pt_len; |
ansond | 0:2a5a48a8b4d4 | 60 | resource_structure->path = pt; |
ansond | 0:2a5a48a8b4d4 | 61 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; |
ansond | 0:2a5a48a8b4d4 | 62 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; |
ansond | 0:2a5a48a8b4d4 | 63 | resource_structure->resource_parameters_ptr->observable = is_observable; |
ansond | 0:2a5a48a8b4d4 | 64 | sn_nsdl_create_resource(resource_structure); |
ansond | 0:2a5a48a8b4d4 | 65 | } |
ansond | 0:2a5a48a8b4d4 | 66 | |
ansond | 0:2a5a48a8b4d4 | 67 | 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:2a5a48a8b4d4 | 68 | { |
ansond | 0:2a5a48a8b4d4 | 69 | if (endpoint_structure == NULL) |
ansond | 0:2a5a48a8b4d4 | 70 | { |
ansond | 0:2a5a48a8b4d4 | 71 | endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s)); |
ansond | 0:2a5a48a8b4d4 | 72 | } |
ansond | 0:2a5a48a8b4d4 | 73 | if (endpoint_structure != NULL) |
ansond | 0:2a5a48a8b4d4 | 74 | { |
ansond | 0:2a5a48a8b4d4 | 75 | memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s)); |
ansond | 0:2a5a48a8b4d4 | 76 | endpoint_structure->endpoint_name_ptr = name; |
ansond | 0:2a5a48a8b4d4 | 77 | endpoint_structure->endpoint_name_len = strlen((char*)name); |
ansond | 0:2a5a48a8b4d4 | 78 | endpoint_structure->domain_name_ptr = domain; |
ansond | 0:2a5a48a8b4d4 | 79 | endpoint_structure->domain_name_len = strlen((char*)domain); |
ansond | 0:2a5a48a8b4d4 | 80 | endpoint_structure->type_ptr = typename_ptr; |
ansond | 0:2a5a48a8b4d4 | 81 | endpoint_structure->type_len = strlen((char*)typename_ptr); |
ansond | 0:2a5a48a8b4d4 | 82 | endpoint_structure->lifetime_ptr = lifetime_ptr; |
ansond | 0:2a5a48a8b4d4 | 83 | endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr); |
ansond | 0:2a5a48a8b4d4 | 84 | } |
ansond | 0:2a5a48a8b4d4 | 85 | return endpoint_structure; |
ansond | 0:2a5a48a8b4d4 | 86 | } |
ansond | 0:2a5a48a8b4d4 | 87 | |
ansond | 0:2a5a48a8b4d4 | 88 | void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) |
ansond | 0:2a5a48a8b4d4 | 89 | { |
ansond | 0:2a5a48a8b4d4 | 90 | if (*endpoint_structure) |
ansond | 0:2a5a48a8b4d4 | 91 | { |
ansond | 0:2a5a48a8b4d4 | 92 | nsdl_free(*endpoint_structure); |
ansond | 0:2a5a48a8b4d4 | 93 | *endpoint_structure = NULL; |
ansond | 0:2a5a48a8b4d4 | 94 | } |
ansond | 0:2a5a48a8b4d4 | 95 | } |
ansond | 0:2a5a48a8b4d4 | 96 | |
ansond | 0:2a5a48a8b4d4 | 97 | static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t datalen, sn_nsdl_addr_s *address_prt) |
ansond | 0:2a5a48a8b4d4 | 98 | { |
ansond | 0:2a5a48a8b4d4 | 99 | DBG("TX callback!\n\rSending %d bytes...", datalen); |
ansond | 0:2a5a48a8b4d4 | 100 | //DBG("Payload:'"); |
ansond | 0:2a5a48a8b4d4 | 101 | //for (int i=0; i < datalen; i++) |
ansond | 0:2a5a48a8b4d4 | 102 | // DBG("%c", *(data_ptr + i)); |
ansond | 0:2a5a48a8b4d4 | 103 | //DBG("' \r\n"); |
ansond | 0:2a5a48a8b4d4 | 104 | |
ansond | 0:2a5a48a8b4d4 | 105 | int8_t socket_ret_value = 0; |
ansond | 0:2a5a48a8b4d4 | 106 | memcpy(app_dest.address, address_prt->addr_ptr, 16); |
ansond | 0:2a5a48a8b4d4 | 107 | app_dest.identifier = address_prt->port; |
ansond | 0:2a5a48a8b4d4 | 108 | app_dest.type = ADDRESS_IPV6; |
ansond | 0:2a5a48a8b4d4 | 109 | |
ansond | 0:2a5a48a8b4d4 | 110 | socket_ret_value = socket_sendto(coap_udp_socket, &app_dest, data_ptr, datalen); |
ansond | 0:2a5a48a8b4d4 | 111 | if(socket_ret_value != 0) |
ansond | 0:2a5a48a8b4d4 | 112 | { |
ansond | 0:2a5a48a8b4d4 | 113 | DBG("Failed\r\n"); |
ansond | 0:2a5a48a8b4d4 | 114 | return 0;//failed |
ansond | 0:2a5a48a8b4d4 | 115 | } |
ansond | 0:2a5a48a8b4d4 | 116 | else |
ansond | 0:2a5a48a8b4d4 | 117 | { |
ansond | 0:2a5a48a8b4d4 | 118 | DBG("Ok\r\n"); |
ansond | 0:2a5a48a8b4d4 | 119 | return 1; // success |
ansond | 0:2a5a48a8b4d4 | 120 | } |
ansond | 0:2a5a48a8b4d4 | 121 | } |
ansond | 0:2a5a48a8b4d4 | 122 | |
ansond | 0:2a5a48a8b4d4 | 123 | static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) |
ansond | 0:2a5a48a8b4d4 | 124 | { |
ansond | 0:2a5a48a8b4d4 | 125 | DBG("RX callback!\r\n"); |
ansond | 0:2a5a48a8b4d4 | 126 | DBG("msg_code: %d \r\n", coap_packet_ptr->msg_code); |
ansond | 0:2a5a48a8b4d4 | 127 | DBG("Payload length: %d bytes\r\n", coap_packet_ptr->payload_len); |
ansond | 0:2a5a48a8b4d4 | 128 | int i; |
ansond | 0:2a5a48a8b4d4 | 129 | DBG("Payload:'"); |
ansond | 0:2a5a48a8b4d4 | 130 | for (i=0; i < coap_packet_ptr->payload_len; i++) |
ansond | 0:2a5a48a8b4d4 | 131 | DBG("%c", *(coap_packet_ptr->payload_ptr + i)); |
ansond | 0:2a5a48a8b4d4 | 132 | DBG("' \r\n"); |
ansond | 0:2a5a48a8b4d4 | 133 | if (coap_packet_ptr->options_list_ptr && coap_packet_ptr->options_list_ptr->location_path_ptr) |
ansond | 0:2a5a48a8b4d4 | 134 | { |
ansond | 0:2a5a48a8b4d4 | 135 | DBG("Location: /"); |
ansond | 0:2a5a48a8b4d4 | 136 | int i; |
ansond | 0:2a5a48a8b4d4 | 137 | for (i=0; i < coap_packet_ptr->options_list_ptr->location_path_len; i++) DBG("%c", (char)(coap_packet_ptr->options_list_ptr->location_path_ptr[i])); |
ansond | 0:2a5a48a8b4d4 | 138 | DBG(" \r\n"); |
ansond | 0:2a5a48a8b4d4 | 139 | } |
ansond | 0:2a5a48a8b4d4 | 140 | //sn_coap_packet_debug(coap_packet_ptr); |
ansond | 0:2a5a48a8b4d4 | 141 | return 0; |
ansond | 0:2a5a48a8b4d4 | 142 | } |
ansond | 0:2a5a48a8b4d4 | 143 | |
ansond | 0:2a5a48a8b4d4 | 144 | |
ansond | 0:2a5a48a8b4d4 | 145 | void NSP_registration( ) { |
ansond | 0:2a5a48a8b4d4 | 146 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
ansond | 0:2a5a48a8b4d4 | 147 | |
ansond | 0:2a5a48a8b4d4 | 148 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t *)domain_name, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
ansond | 0:2a5a48a8b4d4 | 149 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
ansond | 0:2a5a48a8b4d4 | 150 | DBG("NSP re-registering failed\r\n"); |
ansond | 0:2a5a48a8b4d4 | 151 | else |
ansond | 0:2a5a48a8b4d4 | 152 | DBG("NSP re-registering OK\r\n"); |
ansond | 0:2a5a48a8b4d4 | 153 | nsdl_clean_register_endpoint(&endpoint_ptr); |
ansond | 0:2a5a48a8b4d4 | 154 | } |
ansond | 0:2a5a48a8b4d4 | 155 | |
ansond | 0:2a5a48a8b4d4 | 156 | |
ansond | 0:2a5a48a8b4d4 | 157 | void nsdl_init() |
ansond | 0:2a5a48a8b4d4 | 158 | { |
ansond | 0:2a5a48a8b4d4 | 159 | //uint8_t nsp_addr[4]; |
ansond | 0:2a5a48a8b4d4 | 160 | sn_nsdl_mem_s memory_cbs; |
ansond | 0:2a5a48a8b4d4 | 161 | |
ansond | 0:2a5a48a8b4d4 | 162 | /* Initialize libNsdl */ |
ansond | 0:2a5a48a8b4d4 | 163 | memory_cbs.sn_nsdl_alloc = &nsdl_alloc; |
ansond | 0:2a5a48a8b4d4 | 164 | memory_cbs.sn_nsdl_free = &nsdl_free; |
ansond | 0:2a5a48a8b4d4 | 165 | if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) |
ansond | 0:2a5a48a8b4d4 | 166 | DBG("libNsdl init failed\r\n"); |
ansond | 0:2a5a48a8b4d4 | 167 | else |
ansond | 0:2a5a48a8b4d4 | 168 | DBG("libNsdl init done\r\n"); |
ansond | 0:2a5a48a8b4d4 | 169 | /* Set nsp address for library */ |
ansond | 0:2a5a48a8b4d4 | 170 | set_NSP_address(NSP_address_bytes, NSP_COAP_UDP_PORT, SN_NSDL_ADDRESS_TYPE_IPV6); |
ansond | 0:2a5a48a8b4d4 | 171 | } |
ansond | 0:2a5a48a8b4d4 | 172 | |
ansond | 0:2a5a48a8b4d4 | 173 | void nsdl_reg_update() |
ansond | 0:2a5a48a8b4d4 | 174 | { |
ansond | 0:2a5a48a8b4d4 | 175 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
ansond | 0:2a5a48a8b4d4 | 176 | // reg update should be invoked with null parameters if nothing is changing |
ansond | 0:2a5a48a8b4d4 | 177 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t *)null_ep_domain, (uint8_t*)null_ep_name, null_ep_type, null_lifetime_ptr); |
ansond | 0:2a5a48a8b4d4 | 178 | if(sn_nsdl_update_registration(endpoint_ptr) != 0) |
ansond | 0:2a5a48a8b4d4 | 179 | DBG("NSP re-registering failed\r\n"); |
ansond | 0:2a5a48a8b4d4 | 180 | else |
ansond | 0:2a5a48a8b4d4 | 181 | DBG("NSP re-registering OK\r\n"); |
ansond | 0:2a5a48a8b4d4 | 182 | nsdl_clean_register_endpoint(&endpoint_ptr); |
ansond | 0:2a5a48a8b4d4 | 183 | } |
ansond | 0:2a5a48a8b4d4 | 184 | |
ansond | 0:2a5a48a8b4d4 | 185 | void nsdl_reg_update_timeout() |
ansond | 0:2a5a48a8b4d4 | 186 | { |
ansond | 0:2a5a48a8b4d4 | 187 | nsdl_reg_update_needed = true; |
ansond | 0:2a5a48a8b4d4 | 188 | } |
ansond | 0:2a5a48a8b4d4 | 189 | |
ansond | 0:2a5a48a8b4d4 | 190 | void nsdl_event_loop() |
ansond | 0:2a5a48a8b4d4 | 191 | { |
ansond | 0:2a5a48a8b4d4 | 192 | |
ansond | 0:2a5a48a8b4d4 | 193 | sn_nsdl_addr_s received_packet_address; |
ansond | 0:2a5a48a8b4d4 | 194 | uint8_t received_address[4]; |
ansond | 0:2a5a48a8b4d4 | 195 | |
ansond | 0:2a5a48a8b4d4 | 196 | memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); |
ansond | 0:2a5a48a8b4d4 | 197 | received_packet_address.addr_ptr = received_address; |
ansond | 0:2a5a48a8b4d4 | 198 | |
ansond | 0:2a5a48a8b4d4 | 199 | while(1) |
ansond | 0:2a5a48a8b4d4 | 200 | { |
ansond | 0:2a5a48a8b4d4 | 201 | // pc.DBG("checking reg timeout\r\n"); |
ansond | 0:2a5a48a8b4d4 | 202 | if (nsdl_reg_update_needed) |
ansond | 0:2a5a48a8b4d4 | 203 | { |
ansond | 0:2a5a48a8b4d4 | 204 | nsdl_reg_update_needed = false; |
ansond | 0:2a5a48a8b4d4 | 205 | nsdl_reg_update(); |
ansond | 0:2a5a48a8b4d4 | 206 | } |
ansond | 0:2a5a48a8b4d4 | 207 | } |
ansond | 0:2a5a48a8b4d4 | 208 | } |
ansond | 0:2a5a48a8b4d4 | 209 | |
ansond | 0:2a5a48a8b4d4 | 210 | void nsdl_run() { |
ansond | 0:2a5a48a8b4d4 | 211 | // Initialize NSDL stack |
ansond | 0:2a5a48a8b4d4 | 212 | DBG("nsdl_run: NDSL Initialization...\r\n"); |
ansond | 0:2a5a48a8b4d4 | 213 | nsdl_init(); |
ansond | 0:2a5a48a8b4d4 | 214 | |
ansond | 0:2a5a48a8b4d4 | 215 | // Customize the endpoint and its resources |
ansond | 0:2a5a48a8b4d4 | 216 | DBG("nsdl_run: Creating MDS Resources for endpoint...\r\n"); |
ansond | 0:2a5a48a8b4d4 | 217 | configure_endpoint(); |
ansond | 0:2a5a48a8b4d4 | 218 | } |