Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface PololuLedStripx mbed-rtos mbed nanoservice_client_1_12_X
Fork of LPC1768_LWM2M_Client by
nsdl_support.cpp@7:59c7b96ace5b, 2014-09-28 (annotated)
- Committer:
- michaeljkoster
- Date:
- Sun Sep 28 23:59:14 2014 +0000
- Revision:
- 7:59c7b96ace5b
- Parent:
- 1:09a525977925
- Child:
- 8:1358fc888c9c
Working on problem with slow or no response to received packets. UDP socket not returning anything or returning zero bytes in non blocking mode
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| michaeljkoster | 0:9101343a70cd | 1 | // NSDL library support functions |
| michaeljkoster | 0:9101343a70cd | 2 | |
| michaeljkoster | 0:9101343a70cd | 3 | #include "mbed.h" |
| michaeljkoster | 0:9101343a70cd | 4 | #include "nsdl_support.h" |
| michaeljkoster | 0:9101343a70cd | 5 | #include "mbed.h" |
| michaeljkoster | 0:9101343a70cd | 6 | #include "rtos.h" |
| michaeljkoster | 0:9101343a70cd | 7 | #include "EthernetInterface.h" |
| michaeljkoster | 1:09a525977925 | 8 | #include "UDPSocket.h" |
| michaeljkoster | 1:09a525977925 | 9 | #include "Endpoint.h" |
| michaeljkoster | 1:09a525977925 | 10 | |
| michaeljkoster | 1:09a525977925 | 11 | /* __heapvalid((__heapprt)fprintf,stdout, 1);\*/ |
| michaeljkoster | 1:09a525977925 | 12 | #define MEM_VALID(x) \ |
| michaeljkoster | 1:09a525977925 | 13 | int s##x=0;\ |
| michaeljkoster | 1:09a525977925 | 14 | int *h##x = new int [1];\ |
| michaeljkoster | 1:09a525977925 | 15 | std::printf("[stack]0x%08x\t[heap]0x%08x\t[memory avail]%d bytes \tLine: %d %s\r\n", &s##x, h##x, &s##x-h##x, __LINE__, __FILE__);\ |
| michaeljkoster | 1:09a525977925 | 16 | if (h##x > &s##x)\ |
| michaeljkoster | 1:09a525977925 | 17 | printf("collision\n");\ |
| michaeljkoster | 1:09a525977925 | 18 | else\ |
| michaeljkoster | 1:09a525977925 | 19 | delete [] h##x;\ |
| michaeljkoster | 1:09a525977925 | 20 | __nop() |
| michaeljkoster | 0:9101343a70cd | 21 | |
| michaeljkoster | 0:9101343a70cd | 22 | extern Serial pc; |
| michaeljkoster | 0:9101343a70cd | 23 | extern Endpoint nsp; |
| michaeljkoster | 0:9101343a70cd | 24 | extern UDPSocket server; |
| michaeljkoster | 1:09a525977925 | 25 | extern char endpoint_name[24]; |
| michaeljkoster | 0:9101343a70cd | 26 | extern uint8_t ep_type[]; |
| michaeljkoster | 0:9101343a70cd | 27 | extern uint8_t lifetime_ptr[]; |
| michaeljkoster | 1:09a525977925 | 28 | char null_ep_name[] = ""; |
| michaeljkoster | 1:09a525977925 | 29 | uint8_t null_ep_type[] = ""; |
| michaeljkoster | 7:59c7b96ace5b | 30 | uint8_t null_lifetime_ptr[] = "60"; |
| michaeljkoster | 1:09a525977925 | 31 | bool nsdl_reg_update_needed = false; |
| michaeljkoster | 0:9101343a70cd | 32 | |
| michaeljkoster | 0:9101343a70cd | 33 | /* The number of seconds between NSP registration messages */ |
| michaeljkoster | 1:09a525977925 | 34 | #define RD_UPDATE_PERIOD 30 |
| michaeljkoster | 0:9101343a70cd | 35 | |
| michaeljkoster | 0:9101343a70cd | 36 | void *nsdl_alloc(uint16_t size) |
| michaeljkoster | 0:9101343a70cd | 37 | { |
| michaeljkoster | 1:09a525977925 | 38 | void *buf = malloc(size); |
| michaeljkoster | 1:09a525977925 | 39 | // printf("alloc\r\n"); |
| michaeljkoster | 1:09a525977925 | 40 | // MEM_VALID(0); |
| michaeljkoster | 1:09a525977925 | 41 | return buf; |
| michaeljkoster | 0:9101343a70cd | 42 | } |
| michaeljkoster | 0:9101343a70cd | 43 | |
| michaeljkoster | 0:9101343a70cd | 44 | void nsdl_free(void* ptr_to_free) |
| michaeljkoster | 0:9101343a70cd | 45 | { |
| michaeljkoster | 0:9101343a70cd | 46 | free(ptr_to_free); |
| michaeljkoster | 1:09a525977925 | 47 | // printf("de-alloc\r\n"); |
| michaeljkoster | 1:09a525977925 | 48 | // MEM_VALID(0); |
| michaeljkoster | 0:9101343a70cd | 49 | } |
| michaeljkoster | 0:9101343a70cd | 50 | |
| michaeljkoster | 0:9101343a70cd | 51 | /* |
| michaeljkoster | 0:9101343a70cd | 52 | * Create a static resoure |
| michaeljkoster | 0:9101343a70cd | 53 | * Only get is allowed |
| michaeljkoster | 0:9101343a70cd | 54 | */ |
| michaeljkoster | 0:9101343a70cd | 55 | 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) |
| michaeljkoster | 0:9101343a70cd | 56 | { |
| michaeljkoster | 0:9101343a70cd | 57 | resource_structure->access = SN_GRS_GET_ALLOWED; |
| michaeljkoster | 0:9101343a70cd | 58 | resource_structure->mode = SN_GRS_STATIC; |
| michaeljkoster | 0:9101343a70cd | 59 | resource_structure->pathlen = pt_len; |
| michaeljkoster | 0:9101343a70cd | 60 | resource_structure->path = pt; |
| michaeljkoster | 0:9101343a70cd | 61 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; |
| michaeljkoster | 0:9101343a70cd | 62 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; |
| michaeljkoster | 0:9101343a70cd | 63 | resource_structure->resource = rsc; |
| michaeljkoster | 0:9101343a70cd | 64 | resource_structure->resourcelen = rsc_len; |
| michaeljkoster | 0:9101343a70cd | 65 | sn_nsdl_create_resource(resource_structure); |
| michaeljkoster | 0:9101343a70cd | 66 | } |
| michaeljkoster | 0:9101343a70cd | 67 | |
| michaeljkoster | 0:9101343a70cd | 68 | 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) |
| michaeljkoster | 0:9101343a70cd | 69 | { |
| michaeljkoster | 0:9101343a70cd | 70 | resource_structure->access = (sn_grs_resource_acl_e)access_right; |
| michaeljkoster | 0:9101343a70cd | 71 | resource_structure->resource = 0; |
| michaeljkoster | 0:9101343a70cd | 72 | resource_structure->resourcelen = 0; |
| michaeljkoster | 0:9101343a70cd | 73 | resource_structure->sn_grs_dyn_res_callback = callback_ptr; |
| michaeljkoster | 0:9101343a70cd | 74 | resource_structure->mode = SN_GRS_DYNAMIC; |
| michaeljkoster | 0:9101343a70cd | 75 | resource_structure->pathlen = pt_len; |
| michaeljkoster | 0:9101343a70cd | 76 | resource_structure->path = pt; |
| michaeljkoster | 0:9101343a70cd | 77 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; |
| michaeljkoster | 0:9101343a70cd | 78 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; |
| michaeljkoster | 0:9101343a70cd | 79 | resource_structure->resource_parameters_ptr->observable = is_observable; |
| michaeljkoster | 0:9101343a70cd | 80 | sn_nsdl_create_resource(resource_structure); |
| michaeljkoster | 0:9101343a70cd | 81 | } |
| michaeljkoster | 0:9101343a70cd | 82 | |
| michaeljkoster | 0:9101343a70cd | 83 | sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* typename_ptr, uint8_t *lifetime_ptr) |
| michaeljkoster | 0:9101343a70cd | 84 | { |
| michaeljkoster | 0:9101343a70cd | 85 | if (NULL == endpoint_structure) |
| michaeljkoster | 0:9101343a70cd | 86 | { |
| michaeljkoster | 0:9101343a70cd | 87 | endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s)); |
| michaeljkoster | 0:9101343a70cd | 88 | } |
| michaeljkoster | 0:9101343a70cd | 89 | if (endpoint_structure) |
| michaeljkoster | 0:9101343a70cd | 90 | { |
| michaeljkoster | 0:9101343a70cd | 91 | memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s)); |
| michaeljkoster | 0:9101343a70cd | 92 | endpoint_structure->endpoint_name_ptr = name; |
| michaeljkoster | 0:9101343a70cd | 93 | endpoint_structure->endpoint_name_len = strlen((char*)name); |
| michaeljkoster | 0:9101343a70cd | 94 | endpoint_structure->type_ptr = typename_ptr; |
| michaeljkoster | 0:9101343a70cd | 95 | endpoint_structure->type_len = strlen((char*)typename_ptr); |
| michaeljkoster | 0:9101343a70cd | 96 | endpoint_structure->lifetime_ptr = lifetime_ptr; |
| michaeljkoster | 1:09a525977925 | 97 | endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr); |
| michaeljkoster | 0:9101343a70cd | 98 | } |
| michaeljkoster | 0:9101343a70cd | 99 | return endpoint_structure; |
| michaeljkoster | 0:9101343a70cd | 100 | } |
| michaeljkoster | 0:9101343a70cd | 101 | |
| michaeljkoster | 0:9101343a70cd | 102 | void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) |
| michaeljkoster | 0:9101343a70cd | 103 | { |
| michaeljkoster | 0:9101343a70cd | 104 | if (*endpoint_structure) |
| michaeljkoster | 0:9101343a70cd | 105 | { |
| michaeljkoster | 0:9101343a70cd | 106 | nsdl_free(*endpoint_structure); |
| michaeljkoster | 0:9101343a70cd | 107 | *endpoint_structure = NULL; |
| michaeljkoster | 0:9101343a70cd | 108 | } |
| michaeljkoster | 0:9101343a70cd | 109 | } |
| michaeljkoster | 0:9101343a70cd | 110 | |
| michaeljkoster | 0:9101343a70cd | 111 | static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr) |
| michaeljkoster | 0:9101343a70cd | 112 | { |
| michaeljkoster | 0:9101343a70cd | 113 | pc.printf("TX callback!\n\rSending %d bytes\r\n", data_len); |
| michaeljkoster | 0:9101343a70cd | 114 | |
| michaeljkoster | 0:9101343a70cd | 115 | if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len) |
| michaeljkoster | 0:9101343a70cd | 116 | pc.printf("sending failed\n\r"); |
| michaeljkoster | 0:9101343a70cd | 117 | |
| michaeljkoster | 0:9101343a70cd | 118 | return 1; |
| michaeljkoster | 0:9101343a70cd | 119 | } |
| michaeljkoster | 0:9101343a70cd | 120 | |
| michaeljkoster | 0:9101343a70cd | 121 | static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) |
| michaeljkoster | 0:9101343a70cd | 122 | { |
| michaeljkoster | 0:9101343a70cd | 123 | pc.printf("RX callback!\r\n"); |
| michaeljkoster | 1:09a525977925 | 124 | pc.printf("msg_code: %d \r\n", coap_packet_ptr->msg_code); |
| michaeljkoster | 1:09a525977925 | 125 | pc.printf("Payload length: %d bytes\r\n", coap_packet_ptr->payload_len); |
| michaeljkoster | 1:09a525977925 | 126 | int i; |
| michaeljkoster | 1:09a525977925 | 127 | pc.printf("Payload:'"); |
| michaeljkoster | 1:09a525977925 | 128 | for (i=0; i < coap_packet_ptr->payload_len; i++) |
| michaeljkoster | 1:09a525977925 | 129 | pc.printf("%c", *(coap_packet_ptr->payload_ptr + i)); |
| michaeljkoster | 1:09a525977925 | 130 | pc.printf("' \r\n"); |
| michaeljkoster | 1:09a525977925 | 131 | if (coap_packet_ptr->options_list_ptr && coap_packet_ptr->options_list_ptr->location_path_ptr) |
| michaeljkoster | 1:09a525977925 | 132 | { |
| michaeljkoster | 1:09a525977925 | 133 | pc.printf("Location: /"); |
| michaeljkoster | 1:09a525977925 | 134 | int i; |
| michaeljkoster | 1:09a525977925 | 135 | for (i=0; i < coap_packet_ptr->options_list_ptr->location_path_len; i++) pc.printf("%c", (char)(coap_packet_ptr->options_list_ptr->location_path_ptr[i])); |
| michaeljkoster | 1:09a525977925 | 136 | pc.printf(" \r\n"); |
| michaeljkoster | 1:09a525977925 | 137 | } |
| michaeljkoster | 1:09a525977925 | 138 | //sn_coap_packet_debug(coap_packet_ptr); |
| michaeljkoster | 0:9101343a70cd | 139 | return 0; |
| michaeljkoster | 0:9101343a70cd | 140 | } |
| michaeljkoster | 0:9101343a70cd | 141 | |
| michaeljkoster | 0:9101343a70cd | 142 | static void registration_update_thread(void const *args) |
| michaeljkoster | 0:9101343a70cd | 143 | { |
| michaeljkoster | 0:9101343a70cd | 144 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
| michaeljkoster | 0:9101343a70cd | 145 | |
| michaeljkoster | 0:9101343a70cd | 146 | while(true) |
| michaeljkoster | 0:9101343a70cd | 147 | { |
| michaeljkoster | 0:9101343a70cd | 148 | wait(RD_UPDATE_PERIOD); |
| michaeljkoster | 1:09a525977925 | 149 | //endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
| michaeljkoster | 1:09a525977925 | 150 | //if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
| michaeljkoster | 1:09a525977925 | 151 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)null_ep_name, null_ep_type, null_lifetime_ptr); |
| michaeljkoster | 1:09a525977925 | 152 | if(sn_nsdl_update_registration(endpoint_ptr) != 0) |
| michaeljkoster | 0:9101343a70cd | 153 | pc.printf("NSP re-registering failed\r\n"); |
| michaeljkoster | 0:9101343a70cd | 154 | else |
| michaeljkoster | 0:9101343a70cd | 155 | pc.printf("NSP re-registering OK\r\n"); |
| michaeljkoster | 0:9101343a70cd | 156 | nsdl_clean_register_endpoint(&endpoint_ptr); |
| michaeljkoster | 0:9101343a70cd | 157 | } |
| michaeljkoster | 0:9101343a70cd | 158 | } |
| michaeljkoster | 0:9101343a70cd | 159 | |
| michaeljkoster | 0:9101343a70cd | 160 | void nsdl_init() |
| michaeljkoster | 0:9101343a70cd | 161 | { |
| michaeljkoster | 0:9101343a70cd | 162 | uint8_t nsp_addr[4]; |
| michaeljkoster | 0:9101343a70cd | 163 | sn_nsdl_mem_s memory_cbs; |
| michaeljkoster | 0:9101343a70cd | 164 | |
| michaeljkoster | 0:9101343a70cd | 165 | /* Initialize libNsdl */ |
| michaeljkoster | 0:9101343a70cd | 166 | memory_cbs.sn_nsdl_alloc = &nsdl_alloc; |
| michaeljkoster | 0:9101343a70cd | 167 | memory_cbs.sn_nsdl_free = &nsdl_free; |
| michaeljkoster | 0:9101343a70cd | 168 | if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) |
| michaeljkoster | 0:9101343a70cd | 169 | pc.printf("libNsdl init failed\r\n"); |
| michaeljkoster | 0:9101343a70cd | 170 | else |
| michaeljkoster | 0:9101343a70cd | 171 | pc.printf("libNsdl init done\r\n"); |
| michaeljkoster | 0:9101343a70cd | 172 | |
| michaeljkoster | 0:9101343a70cd | 173 | /* Set nsp address for library */ |
| michaeljkoster | 0:9101343a70cd | 174 | set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4); |
| michaeljkoster | 1:09a525977925 | 175 | } |
| michaeljkoster | 0:9101343a70cd | 176 | |
| michaeljkoster | 0:9101343a70cd | 177 | void nsdl_event_loop() |
| michaeljkoster | 0:9101343a70cd | 178 | { |
| michaeljkoster | 0:9101343a70cd | 179 | Thread registration_thread(registration_update_thread); |
| michaeljkoster | 0:9101343a70cd | 180 | |
| michaeljkoster | 0:9101343a70cd | 181 | sn_nsdl_addr_s received_packet_address; |
| michaeljkoster | 0:9101343a70cd | 182 | uint8_t received_address[4]; |
| michaeljkoster | 0:9101343a70cd | 183 | char buffer[1024]; |
| michaeljkoster | 0:9101343a70cd | 184 | Endpoint from; |
| michaeljkoster | 0:9101343a70cd | 185 | |
| michaeljkoster | 0:9101343a70cd | 186 | memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); |
| michaeljkoster | 0:9101343a70cd | 187 | received_packet_address.addr_ptr = received_address; |
| michaeljkoster | 0:9101343a70cd | 188 | |
| michaeljkoster | 0:9101343a70cd | 189 | while(1) |
| michaeljkoster | 0:9101343a70cd | 190 | { |
| michaeljkoster | 0:9101343a70cd | 191 | int n = server.receiveFrom(from, buffer, sizeof(buffer)); |
| michaeljkoster | 7:59c7b96ace5b | 192 | pc.printf("receive:\r\n"); |
| michaeljkoster | 0:9101343a70cd | 193 | if (n < 0) |
| michaeljkoster | 0:9101343a70cd | 194 | { |
| michaeljkoster | 0:9101343a70cd | 195 | pc.printf("Socket error\n\r"); |
| michaeljkoster | 0:9101343a70cd | 196 | } |
| michaeljkoster | 7:59c7b96ace5b | 197 | else if (n > 0) |
| michaeljkoster | 0:9101343a70cd | 198 | { |
| michaeljkoster | 0:9101343a70cd | 199 | pc.printf("Received %d bytes\r\n", n); |
| michaeljkoster | 0:9101343a70cd | 200 | sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address); |
| michaeljkoster | 0:9101343a70cd | 201 | } |
| michaeljkoster | 0:9101343a70cd | 202 | } |
| michaeljkoster | 0:9101343a70cd | 203 | } |
| michaeljkoster | 0:9101343a70cd | 204 | |
| michaeljkoster | 0:9101343a70cd | 205 |
