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