Arch Pro mbed Device Server demo exposes LEDs as LWM2M style object
Dependencies: EthernetInterface mbed-rtos mbed nsdl_lib
nsdl_support.cpp@0:f7f236605c98, 2014-10-11 (annotated)
- Committer:
- michaeljkoster
- Date:
- Sat Oct 11 06:14:44 2014 +0000
- Revision:
- 0:f7f236605c98
Initial Drop for SCU Class Demo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaeljkoster | 0:f7f236605c98 | 1 | // NSDL library support functions |
michaeljkoster | 0:f7f236605c98 | 2 | |
michaeljkoster | 0:f7f236605c98 | 3 | #include "mbed.h" |
michaeljkoster | 0:f7f236605c98 | 4 | #include "nsdl_support.h" |
michaeljkoster | 0:f7f236605c98 | 5 | #include "mbed.h" |
michaeljkoster | 0:f7f236605c98 | 6 | #include "rtos.h" |
michaeljkoster | 0:f7f236605c98 | 7 | #include "EthernetInterface.h" |
michaeljkoster | 0:f7f236605c98 | 8 | #include "UDPSocket.h" |
michaeljkoster | 0:f7f236605c98 | 9 | #include "Endpoint.h" |
michaeljkoster | 0:f7f236605c98 | 10 | |
michaeljkoster | 0:f7f236605c98 | 11 | /* __heapvalid((__heapprt)fprintf,stdout, 1);\*/ |
michaeljkoster | 0:f7f236605c98 | 12 | #define MEM_VALID(x) \ |
michaeljkoster | 0:f7f236605c98 | 13 | int s##x=0;\ |
michaeljkoster | 0:f7f236605c98 | 14 | int *h##x = new int [1];\ |
michaeljkoster | 0:f7f236605c98 | 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 | 0:f7f236605c98 | 16 | if (h##x > &s##x)\ |
michaeljkoster | 0:f7f236605c98 | 17 | printf("collision\n");\ |
michaeljkoster | 0:f7f236605c98 | 18 | else\ |
michaeljkoster | 0:f7f236605c98 | 19 | delete [] h##x;\ |
michaeljkoster | 0:f7f236605c98 | 20 | __nop() |
michaeljkoster | 0:f7f236605c98 | 21 | |
michaeljkoster | 0:f7f236605c98 | 22 | extern Serial pc; |
michaeljkoster | 0:f7f236605c98 | 23 | extern Endpoint nsp; |
michaeljkoster | 0:f7f236605c98 | 24 | extern UDPSocket server; |
michaeljkoster | 0:f7f236605c98 | 25 | extern char endpoint_name[24]; |
michaeljkoster | 0:f7f236605c98 | 26 | extern uint8_t ep_type[]; |
michaeljkoster | 0:f7f236605c98 | 27 | extern uint8_t lifetime_ptr[]; |
michaeljkoster | 0:f7f236605c98 | 28 | char null_ep_name[] = ""; |
michaeljkoster | 0:f7f236605c98 | 29 | uint8_t null_ep_type[] = ""; |
michaeljkoster | 0:f7f236605c98 | 30 | uint8_t null_lifetime_ptr[] = "60"; |
michaeljkoster | 0:f7f236605c98 | 31 | bool nsdl_reg_update_needed = false; |
michaeljkoster | 0:f7f236605c98 | 32 | |
michaeljkoster | 0:f7f236605c98 | 33 | /* The number of seconds between NSP registration messages */ |
michaeljkoster | 0:f7f236605c98 | 34 | #define RD_UPDATE_PERIOD 30 |
michaeljkoster | 0:f7f236605c98 | 35 | |
michaeljkoster | 0:f7f236605c98 | 36 | void *nsdl_alloc(uint16_t size) |
michaeljkoster | 0:f7f236605c98 | 37 | { |
michaeljkoster | 0:f7f236605c98 | 38 | void *buf = malloc(size); |
michaeljkoster | 0:f7f236605c98 | 39 | // printf("alloc\r\n"); |
michaeljkoster | 0:f7f236605c98 | 40 | // MEM_VALID(0); |
michaeljkoster | 0:f7f236605c98 | 41 | return buf; |
michaeljkoster | 0:f7f236605c98 | 42 | } |
michaeljkoster | 0:f7f236605c98 | 43 | |
michaeljkoster | 0:f7f236605c98 | 44 | void nsdl_free(void* ptr_to_free) |
michaeljkoster | 0:f7f236605c98 | 45 | { |
michaeljkoster | 0:f7f236605c98 | 46 | free(ptr_to_free); |
michaeljkoster | 0:f7f236605c98 | 47 | // printf("de-alloc\r\n"); |
michaeljkoster | 0:f7f236605c98 | 48 | // MEM_VALID(0); |
michaeljkoster | 0:f7f236605c98 | 49 | } |
michaeljkoster | 0:f7f236605c98 | 50 | |
michaeljkoster | 0:f7f236605c98 | 51 | /* |
michaeljkoster | 0:f7f236605c98 | 52 | * Create a static resoure |
michaeljkoster | 0:f7f236605c98 | 53 | * Only get is allowed |
michaeljkoster | 0:f7f236605c98 | 54 | */ |
michaeljkoster | 0:f7f236605c98 | 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:f7f236605c98 | 56 | { |
michaeljkoster | 0:f7f236605c98 | 57 | resource_structure->access = SN_GRS_GET_ALLOWED; |
michaeljkoster | 0:f7f236605c98 | 58 | resource_structure->mode = SN_GRS_STATIC; |
michaeljkoster | 0:f7f236605c98 | 59 | resource_structure->pathlen = pt_len; |
michaeljkoster | 0:f7f236605c98 | 60 | resource_structure->path = pt; |
michaeljkoster | 0:f7f236605c98 | 61 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; |
michaeljkoster | 0:f7f236605c98 | 62 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; |
michaeljkoster | 0:f7f236605c98 | 63 | resource_structure->resource = rsc; |
michaeljkoster | 0:f7f236605c98 | 64 | resource_structure->resourcelen = rsc_len; |
michaeljkoster | 0:f7f236605c98 | 65 | sn_nsdl_create_resource(resource_structure); |
michaeljkoster | 0:f7f236605c98 | 66 | } |
michaeljkoster | 0:f7f236605c98 | 67 | |
michaeljkoster | 0:f7f236605c98 | 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:f7f236605c98 | 69 | { |
michaeljkoster | 0:f7f236605c98 | 70 | resource_structure->access = (sn_grs_resource_acl_e)access_right; |
michaeljkoster | 0:f7f236605c98 | 71 | resource_structure->resource = 0; |
michaeljkoster | 0:f7f236605c98 | 72 | resource_structure->resourcelen = 0; |
michaeljkoster | 0:f7f236605c98 | 73 | resource_structure->sn_grs_dyn_res_callback = callback_ptr; |
michaeljkoster | 0:f7f236605c98 | 74 | resource_structure->mode = SN_GRS_DYNAMIC; |
michaeljkoster | 0:f7f236605c98 | 75 | resource_structure->pathlen = pt_len; |
michaeljkoster | 0:f7f236605c98 | 76 | resource_structure->path = pt; |
michaeljkoster | 0:f7f236605c98 | 77 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; |
michaeljkoster | 0:f7f236605c98 | 78 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; |
michaeljkoster | 0:f7f236605c98 | 79 | resource_structure->resource_parameters_ptr->observable = is_observable; |
michaeljkoster | 0:f7f236605c98 | 80 | sn_nsdl_create_resource(resource_structure); |
michaeljkoster | 0:f7f236605c98 | 81 | } |
michaeljkoster | 0:f7f236605c98 | 82 | |
michaeljkoster | 0:f7f236605c98 | 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:f7f236605c98 | 84 | { |
michaeljkoster | 0:f7f236605c98 | 85 | if (NULL == endpoint_structure) |
michaeljkoster | 0:f7f236605c98 | 86 | { |
michaeljkoster | 0:f7f236605c98 | 87 | endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s)); |
michaeljkoster | 0:f7f236605c98 | 88 | } |
michaeljkoster | 0:f7f236605c98 | 89 | if (endpoint_structure) |
michaeljkoster | 0:f7f236605c98 | 90 | { |
michaeljkoster | 0:f7f236605c98 | 91 | memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s)); |
michaeljkoster | 0:f7f236605c98 | 92 | endpoint_structure->endpoint_name_ptr = name; |
michaeljkoster | 0:f7f236605c98 | 93 | endpoint_structure->endpoint_name_len = strlen((char*)name); |
michaeljkoster | 0:f7f236605c98 | 94 | endpoint_structure->type_ptr = typename_ptr; |
michaeljkoster | 0:f7f236605c98 | 95 | endpoint_structure->type_len = strlen((char*)typename_ptr); |
michaeljkoster | 0:f7f236605c98 | 96 | endpoint_structure->lifetime_ptr = lifetime_ptr; |
michaeljkoster | 0:f7f236605c98 | 97 | endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr); |
michaeljkoster | 0:f7f236605c98 | 98 | } |
michaeljkoster | 0:f7f236605c98 | 99 | return endpoint_structure; |
michaeljkoster | 0:f7f236605c98 | 100 | } |
michaeljkoster | 0:f7f236605c98 | 101 | |
michaeljkoster | 0:f7f236605c98 | 102 | void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) |
michaeljkoster | 0:f7f236605c98 | 103 | { |
michaeljkoster | 0:f7f236605c98 | 104 | if (*endpoint_structure) |
michaeljkoster | 0:f7f236605c98 | 105 | { |
michaeljkoster | 0:f7f236605c98 | 106 | nsdl_free(*endpoint_structure); |
michaeljkoster | 0:f7f236605c98 | 107 | *endpoint_structure = NULL; |
michaeljkoster | 0:f7f236605c98 | 108 | } |
michaeljkoster | 0:f7f236605c98 | 109 | } |
michaeljkoster | 0:f7f236605c98 | 110 | |
michaeljkoster | 0:f7f236605c98 | 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:f7f236605c98 | 112 | { |
michaeljkoster | 0:f7f236605c98 | 113 | pc.printf("TX callback!\n\rSending %d bytes\r\n", data_len); |
michaeljkoster | 0:f7f236605c98 | 114 | |
michaeljkoster | 0:f7f236605c98 | 115 | if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len) |
michaeljkoster | 0:f7f236605c98 | 116 | pc.printf("sending failed\n\r"); |
michaeljkoster | 0:f7f236605c98 | 117 | |
michaeljkoster | 0:f7f236605c98 | 118 | return 1; |
michaeljkoster | 0:f7f236605c98 | 119 | } |
michaeljkoster | 0:f7f236605c98 | 120 | |
michaeljkoster | 0:f7f236605c98 | 121 | static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) |
michaeljkoster | 0:f7f236605c98 | 122 | { |
michaeljkoster | 0:f7f236605c98 | 123 | pc.printf("RX callback!\r\n"); |
michaeljkoster | 0:f7f236605c98 | 124 | pc.printf("msg_code: %d \r\n", coap_packet_ptr->msg_code); |
michaeljkoster | 0:f7f236605c98 | 125 | pc.printf("Payload length: %d bytes\r\n", coap_packet_ptr->payload_len); |
michaeljkoster | 0:f7f236605c98 | 126 | int i; |
michaeljkoster | 0:f7f236605c98 | 127 | pc.printf("Payload:'"); |
michaeljkoster | 0:f7f236605c98 | 128 | for (i=0; i < coap_packet_ptr->payload_len; i++) |
michaeljkoster | 0:f7f236605c98 | 129 | pc.printf("%c", *(coap_packet_ptr->payload_ptr + i)); |
michaeljkoster | 0:f7f236605c98 | 130 | pc.printf("' \r\n"); |
michaeljkoster | 0:f7f236605c98 | 131 | if (coap_packet_ptr->options_list_ptr && coap_packet_ptr->options_list_ptr->location_path_ptr) |
michaeljkoster | 0:f7f236605c98 | 132 | { |
michaeljkoster | 0:f7f236605c98 | 133 | pc.printf("Location: /"); |
michaeljkoster | 0:f7f236605c98 | 134 | int i; |
michaeljkoster | 0:f7f236605c98 | 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 | 0:f7f236605c98 | 136 | pc.printf(" \r\n"); |
michaeljkoster | 0:f7f236605c98 | 137 | } |
michaeljkoster | 0:f7f236605c98 | 138 | //sn_coap_packet_debug(coap_packet_ptr); |
michaeljkoster | 0:f7f236605c98 | 139 | return 0; |
michaeljkoster | 0:f7f236605c98 | 140 | } |
michaeljkoster | 0:f7f236605c98 | 141 | |
michaeljkoster | 0:f7f236605c98 | 142 | static void registration_update_thread(void const *args) |
michaeljkoster | 0:f7f236605c98 | 143 | { |
michaeljkoster | 0:f7f236605c98 | 144 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
michaeljkoster | 0:f7f236605c98 | 145 | |
michaeljkoster | 0:f7f236605c98 | 146 | while(true) |
michaeljkoster | 0:f7f236605c98 | 147 | { |
michaeljkoster | 0:f7f236605c98 | 148 | wait(RD_UPDATE_PERIOD); |
michaeljkoster | 0:f7f236605c98 | 149 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)null_ep_name, null_ep_type, null_lifetime_ptr); |
michaeljkoster | 0:f7f236605c98 | 150 | if(sn_nsdl_update_registration(endpoint_ptr) != 0) |
michaeljkoster | 0:f7f236605c98 | 151 | pc.printf("NSP re-registering failed\r\n"); |
michaeljkoster | 0:f7f236605c98 | 152 | else |
michaeljkoster | 0:f7f236605c98 | 153 | pc.printf("NSP re-registering OK\r\n"); |
michaeljkoster | 0:f7f236605c98 | 154 | nsdl_clean_register_endpoint(&endpoint_ptr); |
michaeljkoster | 0:f7f236605c98 | 155 | } |
michaeljkoster | 0:f7f236605c98 | 156 | } |
michaeljkoster | 0:f7f236605c98 | 157 | |
michaeljkoster | 0:f7f236605c98 | 158 | void nsdl_init() |
michaeljkoster | 0:f7f236605c98 | 159 | { |
michaeljkoster | 0:f7f236605c98 | 160 | uint8_t nsp_addr[4]; |
michaeljkoster | 0:f7f236605c98 | 161 | sn_nsdl_mem_s memory_cbs; |
michaeljkoster | 0:f7f236605c98 | 162 | |
michaeljkoster | 0:f7f236605c98 | 163 | /* Initialize libNsdl */ |
michaeljkoster | 0:f7f236605c98 | 164 | memory_cbs.sn_nsdl_alloc = &nsdl_alloc; |
michaeljkoster | 0:f7f236605c98 | 165 | memory_cbs.sn_nsdl_free = &nsdl_free; |
michaeljkoster | 0:f7f236605c98 | 166 | if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) |
michaeljkoster | 0:f7f236605c98 | 167 | pc.printf("libNsdl init failed\r\n"); |
michaeljkoster | 0:f7f236605c98 | 168 | else |
michaeljkoster | 0:f7f236605c98 | 169 | pc.printf("libNsdl init done\r\n"); |
michaeljkoster | 0:f7f236605c98 | 170 | |
michaeljkoster | 0:f7f236605c98 | 171 | /* Set nsp address for library */ |
michaeljkoster | 0:f7f236605c98 | 172 | set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4); |
michaeljkoster | 0:f7f236605c98 | 173 | } |
michaeljkoster | 0:f7f236605c98 | 174 | |
michaeljkoster | 0:f7f236605c98 | 175 | void nsdl_event_loop() |
michaeljkoster | 0:f7f236605c98 | 176 | { |
michaeljkoster | 0:f7f236605c98 | 177 | Thread registration_thread(registration_update_thread); |
michaeljkoster | 0:f7f236605c98 | 178 | |
michaeljkoster | 0:f7f236605c98 | 179 | sn_nsdl_addr_s received_packet_address; |
michaeljkoster | 0:f7f236605c98 | 180 | uint8_t received_address[4]; |
michaeljkoster | 0:f7f236605c98 | 181 | char buffer[1024]; |
michaeljkoster | 0:f7f236605c98 | 182 | Endpoint from; |
michaeljkoster | 0:f7f236605c98 | 183 | |
michaeljkoster | 0:f7f236605c98 | 184 | memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); |
michaeljkoster | 0:f7f236605c98 | 185 | received_packet_address.addr_ptr = received_address; |
michaeljkoster | 0:f7f236605c98 | 186 | |
michaeljkoster | 0:f7f236605c98 | 187 | while(1) |
michaeljkoster | 0:f7f236605c98 | 188 | { |
michaeljkoster | 0:f7f236605c98 | 189 | int n = server.receiveFrom(from, buffer, sizeof(buffer)); |
michaeljkoster | 0:f7f236605c98 | 190 | if (n < 0) |
michaeljkoster | 0:f7f236605c98 | 191 | { |
michaeljkoster | 0:f7f236605c98 | 192 | pc.printf("Socket error\n\r"); |
michaeljkoster | 0:f7f236605c98 | 193 | } |
michaeljkoster | 0:f7f236605c98 | 194 | else if (n > 0) |
michaeljkoster | 0:f7f236605c98 | 195 | { |
michaeljkoster | 0:f7f236605c98 | 196 | pc.printf("Received %d bytes\r\n", n); |
michaeljkoster | 0:f7f236605c98 | 197 | sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address); |
michaeljkoster | 0:f7f236605c98 | 198 | } |
michaeljkoster | 0:f7f236605c98 | 199 | } |
michaeljkoster | 0:f7f236605c98 | 200 | } |
michaeljkoster | 0:f7f236605c98 | 201 | |
michaeljkoster | 0:f7f236605c98 | 202 |