![](/media/cache/group/arm_mbed.PNG.50x50_q85.png)
Demo starter application to connect WiGo to NSP and expose on-board sensors
Dependencies: NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed nsdl_lib TEMT6200 TSI Wi-Go_eCompass_Lib_V3 WiGo_BattCharger
This is the mbed project for the IoT World Hackathon event, June 17th and 18th in Palo Also.
The setup instructions for participants are at the Setup page of this wiki:
nsdl_support.cpp@18:11b9d98ecae2, 2014-07-09 (annotated)
- Committer:
- michaeljkoster
- Date:
- Wed Jul 09 04:35:49 2014 +0000
- Revision:
- 18:11b9d98ecae2
- Parent:
- 16:d6812604cf92
Checkpoint
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaeljkoster | 0:07581223f90c | 1 | // NSDL library support functions |
michaeljkoster | 0:07581223f90c | 2 | |
michaeljkoster | 0:07581223f90c | 3 | #include "mbed.h" |
michaeljkoster | 0:07581223f90c | 4 | #include "nsdl_support.h" |
michaeljkoster | 0:07581223f90c | 5 | #include "mbed.h" |
michaeljkoster | 0:07581223f90c | 6 | //#include "rtos.h" |
michaeljkoster | 0:07581223f90c | 7 | //put re-registration in timeout loop instead of thread |
michaeljkoster | 0:07581223f90c | 8 | #include "cc3000.h" |
michaeljkoster | 0:07581223f90c | 9 | #include "UDPSocket.h" |
michaeljkoster | 0:07581223f90c | 10 | #include "Endpoint.h" |
michaeljkoster | 0:07581223f90c | 11 | |
sam_grove | 16:d6812604cf92 | 12 | /* __heapvalid((__heapprt)fprintf,stdout, 1);\*/ |
sam_grove | 16:d6812604cf92 | 13 | #define MEM_VALID(x) \ |
sam_grove | 16:d6812604cf92 | 14 | int s##x=0;\ |
sam_grove | 16:d6812604cf92 | 15 | int *h##x = new int [1];\ |
sam_grove | 16:d6812604cf92 | 16 | 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__);\ |
sam_grove | 16:d6812604cf92 | 17 | if (h##x > &s##x)\ |
sam_grove | 16:d6812604cf92 | 18 | printf("collision\n");\ |
sam_grove | 16:d6812604cf92 | 19 | else\ |
sam_grove | 16:d6812604cf92 | 20 | delete [] h##x;\ |
sam_grove | 16:d6812604cf92 | 21 | __nop() |
sam_grove | 16:d6812604cf92 | 22 | |
michaeljkoster | 0:07581223f90c | 23 | extern Serial pc; |
michaeljkoster | 0:07581223f90c | 24 | extern cc3000 wifi; |
michaeljkoster | 0:07581223f90c | 25 | extern Endpoint nsp; |
michaeljkoster | 0:07581223f90c | 26 | extern UDPSocket server; |
michaeljkoster | 7:01d01c5cf409 | 27 | extern char endpoint_name[24]; |
michaeljkoster | 0:07581223f90c | 28 | extern uint8_t ep_type[]; |
michaeljkoster | 0:07581223f90c | 29 | extern uint8_t lifetime_ptr[]; |
michaeljkoster | 8:3926a80af3e0 | 30 | char null_ep_name[] = ""; |
michaeljkoster | 8:3926a80af3e0 | 31 | uint8_t null_ep_type[] = ""; |
michaeljkoster | 8:3926a80af3e0 | 32 | uint8_t null_lifetime_ptr[] = ""; |
michaeljkoster | 0:07581223f90c | 33 | |
michaeljkoster | 0:07581223f90c | 34 | /* The number of seconds between NSP registration messages */ |
michaeljkoster | 0:07581223f90c | 35 | #define RD_UPDATE_PERIOD 60 |
michaeljkoster | 0:07581223f90c | 36 | |
michaeljkoster | 0:07581223f90c | 37 | void *nsdl_alloc(uint16_t size) |
michaeljkoster | 0:07581223f90c | 38 | { |
sam_grove | 16:d6812604cf92 | 39 | void *buf = malloc(size); |
michaeljkoster | 18:11b9d98ecae2 | 40 | // printf("alloc\r\n"); |
michaeljkoster | 18:11b9d98ecae2 | 41 | // MEM_VALID(0); |
sam_grove | 16:d6812604cf92 | 42 | return buf; |
michaeljkoster | 0:07581223f90c | 43 | } |
michaeljkoster | 0:07581223f90c | 44 | |
michaeljkoster | 0:07581223f90c | 45 | void nsdl_free(void* ptr_to_free) |
michaeljkoster | 0:07581223f90c | 46 | { |
michaeljkoster | 0:07581223f90c | 47 | free(ptr_to_free); |
michaeljkoster | 18:11b9d98ecae2 | 48 | // printf("de-alloc\r\n"); |
michaeljkoster | 18:11b9d98ecae2 | 49 | // MEM_VALID(0); |
michaeljkoster | 0:07581223f90c | 50 | } |
michaeljkoster | 0:07581223f90c | 51 | |
michaeljkoster | 0:07581223f90c | 52 | /* |
michaeljkoster | 0:07581223f90c | 53 | * Create a static resoure |
michaeljkoster | 0:07581223f90c | 54 | * Only get is allowed |
michaeljkoster | 0:07581223f90c | 55 | */ |
michaeljkoster | 0:07581223f90c | 56 | 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:07581223f90c | 57 | { |
michaeljkoster | 0:07581223f90c | 58 | resource_structure->access = SN_GRS_GET_ALLOWED; |
michaeljkoster | 0:07581223f90c | 59 | resource_structure->mode = SN_GRS_STATIC; |
michaeljkoster | 0:07581223f90c | 60 | resource_structure->pathlen = pt_len; |
michaeljkoster | 0:07581223f90c | 61 | resource_structure->path = pt; |
michaeljkoster | 0:07581223f90c | 62 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; |
michaeljkoster | 0:07581223f90c | 63 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; |
michaeljkoster | 0:07581223f90c | 64 | resource_structure->resource = rsc; |
michaeljkoster | 0:07581223f90c | 65 | resource_structure->resourcelen = rsc_len; |
michaeljkoster | 0:07581223f90c | 66 | sn_nsdl_create_resource(resource_structure); |
michaeljkoster | 0:07581223f90c | 67 | } |
michaeljkoster | 0:07581223f90c | 68 | |
michaeljkoster | 0:07581223f90c | 69 | 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:07581223f90c | 70 | { |
michaeljkoster | 0:07581223f90c | 71 | resource_structure->access = (sn_grs_resource_acl_e)access_right; |
michaeljkoster | 0:07581223f90c | 72 | resource_structure->resource = 0; |
michaeljkoster | 0:07581223f90c | 73 | resource_structure->resourcelen = 0; |
michaeljkoster | 0:07581223f90c | 74 | resource_structure->sn_grs_dyn_res_callback = callback_ptr; |
michaeljkoster | 0:07581223f90c | 75 | resource_structure->mode = SN_GRS_DYNAMIC; |
michaeljkoster | 0:07581223f90c | 76 | resource_structure->pathlen = pt_len; |
michaeljkoster | 0:07581223f90c | 77 | resource_structure->path = pt; |
michaeljkoster | 0:07581223f90c | 78 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; |
michaeljkoster | 0:07581223f90c | 79 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; |
michaeljkoster | 0:07581223f90c | 80 | resource_structure->resource_parameters_ptr->observable = is_observable; |
michaeljkoster | 0:07581223f90c | 81 | sn_nsdl_create_resource(resource_structure); |
michaeljkoster | 0:07581223f90c | 82 | } |
michaeljkoster | 0:07581223f90c | 83 | |
michaeljkoster | 0:07581223f90c | 84 | 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:07581223f90c | 85 | { |
michaeljkoster | 0:07581223f90c | 86 | if (NULL == endpoint_structure) |
michaeljkoster | 0:07581223f90c | 87 | { |
michaeljkoster | 0:07581223f90c | 88 | endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s)); |
michaeljkoster | 0:07581223f90c | 89 | } |
michaeljkoster | 0:07581223f90c | 90 | if (endpoint_structure) |
michaeljkoster | 0:07581223f90c | 91 | { |
michaeljkoster | 0:07581223f90c | 92 | memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s)); |
michaeljkoster | 0:07581223f90c | 93 | endpoint_structure->endpoint_name_ptr = name; |
michaeljkoster | 0:07581223f90c | 94 | endpoint_structure->endpoint_name_len = strlen((char*)name); |
michaeljkoster | 0:07581223f90c | 95 | endpoint_structure->type_ptr = typename_ptr; |
michaeljkoster | 0:07581223f90c | 96 | endpoint_structure->type_len = strlen((char*)typename_ptr); |
michaeljkoster | 0:07581223f90c | 97 | endpoint_structure->lifetime_ptr = lifetime_ptr; |
michaeljkoster | 0:07581223f90c | 98 | endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr); |
michaeljkoster | 0:07581223f90c | 99 | } |
michaeljkoster | 0:07581223f90c | 100 | return endpoint_structure; |
michaeljkoster | 0:07581223f90c | 101 | } |
michaeljkoster | 0:07581223f90c | 102 | |
michaeljkoster | 0:07581223f90c | 103 | void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) |
michaeljkoster | 0:07581223f90c | 104 | { |
michaeljkoster | 0:07581223f90c | 105 | if (*endpoint_structure) |
michaeljkoster | 0:07581223f90c | 106 | { |
michaeljkoster | 0:07581223f90c | 107 | nsdl_free(*endpoint_structure); |
michaeljkoster | 0:07581223f90c | 108 | *endpoint_structure = NULL; |
michaeljkoster | 0:07581223f90c | 109 | } |
michaeljkoster | 0:07581223f90c | 110 | } |
michaeljkoster | 0:07581223f90c | 111 | |
michaeljkoster | 0:07581223f90c | 112 | 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:07581223f90c | 113 | { |
michaeljkoster | 0:07581223f90c | 114 | pc.printf("TX callback!\n\rSending %d bytes\r\n", data_len); |
michaeljkoster | 0:07581223f90c | 115 | |
michaeljkoster | 0:07581223f90c | 116 | if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len) |
michaeljkoster | 0:07581223f90c | 117 | pc.printf("sending failed\n\r"); |
michaeljkoster | 0:07581223f90c | 118 | |
michaeljkoster | 0:07581223f90c | 119 | return 1; |
michaeljkoster | 0:07581223f90c | 120 | } |
michaeljkoster | 0:07581223f90c | 121 | |
michaeljkoster | 0:07581223f90c | 122 | static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) |
michaeljkoster | 0:07581223f90c | 123 | { |
michaeljkoster | 0:07581223f90c | 124 | pc.printf("RX callback!\r\n"); |
michaeljkoster | 18:11b9d98ecae2 | 125 | pc.printf("msg_code: %d \r\n", coap_packet_ptr->msg_code); |
michaeljkoster | 18:11b9d98ecae2 | 126 | pc.printf("Payload length: %d bytes\r\n", coap_packet_ptr->payload_len); |
michaeljkoster | 18:11b9d98ecae2 | 127 | int i; |
michaeljkoster | 18:11b9d98ecae2 | 128 | pc.printf("Payload:'"); |
michaeljkoster | 18:11b9d98ecae2 | 129 | for (i=0; i < coap_packet_ptr->payload_len; i++) |
michaeljkoster | 18:11b9d98ecae2 | 130 | pc.printf("%c", *(coap_packet_ptr->payload_ptr + i)); |
michaeljkoster | 18:11b9d98ecae2 | 131 | pc.printf("' \r\n"); |
michaeljkoster | 18:11b9d98ecae2 | 132 | if (coap_packet_ptr->options_list_ptr && coap_packet_ptr->options_list_ptr->location_path_ptr) |
michaeljkoster | 18:11b9d98ecae2 | 133 | { |
michaeljkoster | 18:11b9d98ecae2 | 134 | pc.printf("Location: /"); |
michaeljkoster | 18:11b9d98ecae2 | 135 | int i; |
michaeljkoster | 18:11b9d98ecae2 | 136 | 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 | 18:11b9d98ecae2 | 137 | pc.printf(" \r\n"); |
michaeljkoster | 18:11b9d98ecae2 | 138 | } |
michaeljkoster | 18:11b9d98ecae2 | 139 | //sn_coap_packet_debug(coap_packet_ptr); |
michaeljkoster | 0:07581223f90c | 140 | return 0; |
michaeljkoster | 0:07581223f90c | 141 | } |
michaeljkoster | 0:07581223f90c | 142 | |
michaeljkoster | 0:07581223f90c | 143 | static void registration_update_thread(void const *args) |
michaeljkoster | 0:07581223f90c | 144 | { |
michaeljkoster | 0:07581223f90c | 145 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
michaeljkoster | 0:07581223f90c | 146 | |
michaeljkoster | 0:07581223f90c | 147 | while(true) |
michaeljkoster | 0:07581223f90c | 148 | { |
michaeljkoster | 0:07581223f90c | 149 | wait(RD_UPDATE_PERIOD); |
michaeljkoster | 0:07581223f90c | 150 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
michaeljkoster | 0:07581223f90c | 151 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
michaeljkoster | 0:07581223f90c | 152 | pc.printf("NSP re-registering failed\r\n"); |
michaeljkoster | 0:07581223f90c | 153 | else |
michaeljkoster | 0:07581223f90c | 154 | pc.printf("NSP re-registering OK\r\n"); |
michaeljkoster | 0:07581223f90c | 155 | nsdl_clean_register_endpoint(&endpoint_ptr); |
michaeljkoster | 0:07581223f90c | 156 | } |
michaeljkoster | 0:07581223f90c | 157 | } |
michaeljkoster | 0:07581223f90c | 158 | |
michaeljkoster | 0:07581223f90c | 159 | void nsdl_init() |
michaeljkoster | 0:07581223f90c | 160 | { |
michaeljkoster | 0:07581223f90c | 161 | uint8_t nsp_addr[4]; |
michaeljkoster | 0:07581223f90c | 162 | sn_nsdl_mem_s memory_cbs; |
michaeljkoster | 0:07581223f90c | 163 | |
michaeljkoster | 0:07581223f90c | 164 | /* Initialize libNsdl */ |
michaeljkoster | 0:07581223f90c | 165 | memory_cbs.sn_nsdl_alloc = &nsdl_alloc; |
michaeljkoster | 0:07581223f90c | 166 | memory_cbs.sn_nsdl_free = &nsdl_free; |
michaeljkoster | 0:07581223f90c | 167 | if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) |
michaeljkoster | 0:07581223f90c | 168 | pc.printf("libNsdl init failed\r\n"); |
michaeljkoster | 0:07581223f90c | 169 | else |
michaeljkoster | 0:07581223f90c | 170 | pc.printf("libNsdl init done\r\n"); |
michaeljkoster | 0:07581223f90c | 171 | |
michaeljkoster | 0:07581223f90c | 172 | /* Set nsp address for library */ |
michaeljkoster | 0:07581223f90c | 173 | set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4); |
michaeljkoster | 0:07581223f90c | 174 | } |
michaeljkoster | 0:07581223f90c | 175 | |
michaeljkoster | 8:3926a80af3e0 | 176 | void nsdl_reg_update() |
michaeljkoster | 7:01d01c5cf409 | 177 | { |
michaeljkoster | 8:3926a80af3e0 | 178 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
michaeljkoster | 8:3926a80af3e0 | 179 | // endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
michaeljkoster | 8:3926a80af3e0 | 180 | // if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
michaeljkoster | 8:3926a80af3e0 | 181 | // reg update should be invoked with null parameters if nothing is changing |
michaeljkoster | 8:3926a80af3e0 | 182 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)null_ep_name, null_ep_type, null_lifetime_ptr); |
michaeljkoster | 8:3926a80af3e0 | 183 | if(sn_nsdl_update_registration(endpoint_ptr) != 0) |
michaeljkoster | 7:01d01c5cf409 | 184 | pc.printf("NSP re-registering failed\r\n"); |
michaeljkoster | 7:01d01c5cf409 | 185 | else |
michaeljkoster | 7:01d01c5cf409 | 186 | pc.printf("NSP re-registering OK\r\n"); |
michaeljkoster | 7:01d01c5cf409 | 187 | nsdl_clean_register_endpoint(&endpoint_ptr); |
michaeljkoster | 7:01d01c5cf409 | 188 | } |
michaeljkoster | 8:3926a80af3e0 | 189 | |
michaeljkoster | 8:3926a80af3e0 | 190 | Ticker regUpdateTicker; |
michaeljkoster | 7:01d01c5cf409 | 191 | |
michaeljkoster | 0:07581223f90c | 192 | void nsdl_event_loop() |
michaeljkoster | 0:07581223f90c | 193 | { |
michaeljkoster | 0:07581223f90c | 194 | // Thread registration_thread(registration_update_thread); |
michaeljkoster | 0:07581223f90c | 195 | //put re-registration in timeout loop instead of thread |
michaeljkoster | 8:3926a80af3e0 | 196 | // regUpdateTicker.attach(&nsdl_reg_update, RD_UPDATE_PERIOD); |
michaeljkoster | 0:07581223f90c | 197 | |
michaeljkoster | 0:07581223f90c | 198 | sn_nsdl_addr_s received_packet_address; |
michaeljkoster | 0:07581223f90c | 199 | uint8_t received_address[4]; |
michaeljkoster | 0:07581223f90c | 200 | char buffer[1024]; |
michaeljkoster | 0:07581223f90c | 201 | Endpoint from; |
michaeljkoster | 0:07581223f90c | 202 | |
michaeljkoster | 0:07581223f90c | 203 | memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); |
michaeljkoster | 0:07581223f90c | 204 | received_packet_address.addr_ptr = received_address; |
michaeljkoster | 0:07581223f90c | 205 | |
michaeljkoster | 0:07581223f90c | 206 | while(1) |
michaeljkoster | 0:07581223f90c | 207 | { |
michaeljkoster | 0:07581223f90c | 208 | int n = server.receiveFrom(from, buffer, sizeof(buffer)); |
michaeljkoster | 0:07581223f90c | 209 | if (n < 0) |
michaeljkoster | 0:07581223f90c | 210 | { |
michaeljkoster | 0:07581223f90c | 211 | pc.printf("Socket error %d\n\r", n); |
michaeljkoster | 0:07581223f90c | 212 | } |
michaeljkoster | 0:07581223f90c | 213 | else |
michaeljkoster | 0:07581223f90c | 214 | { |
michaeljkoster | 0:07581223f90c | 215 | pc.printf("Received %d bytes\r\n", n); |
michaeljkoster | 0:07581223f90c | 216 | sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address); |
michaeljkoster | 0:07581223f90c | 217 | } |
michaeljkoster | 0:07581223f90c | 218 | } |
michaeljkoster | 0:07581223f90c | 219 | } |
michaeljkoster | 0:07581223f90c | 220 | |
michaeljkoster | 0:07581223f90c | 221 |