mbed Sensor node for Instrumented Booth over ETH.

Dependencies:   EthernetInterface-1 MaxbotixDriver Presence HTU21D_TEMP_HUMID_SENSOR_SAMPLE Resources SHARPIR mbed-rtos mbed-src WDT_K64F nsdl_lib

Fork of Trenton_Switch_LPC1768_ETH by Demo Team

Committer:
erigow01
Date:
Wed Dec 03 15:39:09 2014 +0000
Revision:
26:4cac6b346e4f
Child:
38:9066ab490386
UPdated sensor integration.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
erigow01 26:4cac6b346e4f 1 // NSDL library support functions
erigow01 26:4cac6b346e4f 2
erigow01 26:4cac6b346e4f 3 #include "mbed.h"
erigow01 26:4cac6b346e4f 4 #include "nsdl_support.h"
erigow01 26:4cac6b346e4f 5 #include "EthernetInterface.h"
erigow01 26:4cac6b346e4f 6 #include "node_cfg.h"
erigow01 26:4cac6b346e4f 7
erigow01 26:4cac6b346e4f 8
erigow01 26:4cac6b346e4f 9 #define COAP_UDP_PORT 5683
erigow01 26:4cac6b346e4f 10 extern uint8_t NSP_address_bytes[];
erigow01 26:4cac6b346e4f 11 //static ns_address_t app_dest;
erigow01 26:4cac6b346e4f 12
erigow01 26:4cac6b346e4f 13 extern UDPSocket server;
erigow01 26:4cac6b346e4f 14 extern Endpoint nsp;
erigow01 26:4cac6b346e4f 15
erigow01 26:4cac6b346e4f 16 #define MEM_VALID(x) \
erigow01 26:4cac6b346e4f 17 int s##x=0;\
erigow01 26:4cac6b346e4f 18 int *h##x = new int [1];\
erigow01 26:4cac6b346e4f 19 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__);\
erigow01 26:4cac6b346e4f 20 if (h##x > &s##x)\
erigow01 26:4cac6b346e4f 21 printf("collision\n");\
erigow01 26:4cac6b346e4f 22 else\
erigow01 26:4cac6b346e4f 23 delete [] h##x;\
erigow01 26:4cac6b346e4f 24 __nop()
erigow01 26:4cac6b346e4f 25
erigow01 26:4cac6b346e4f 26 extern char endpoint_name[24];
erigow01 26:4cac6b346e4f 27 extern uint8_t ep_type[];
erigow01 26:4cac6b346e4f 28 extern uint8_t lifetime_ptr[];
erigow01 26:4cac6b346e4f 29 char null_ep_name[] = "";
erigow01 26:4cac6b346e4f 30 uint8_t null_ep_type[] = "";
erigow01 26:4cac6b346e4f 31 uint8_t null_lifetime_ptr[] = "";
erigow01 26:4cac6b346e4f 32 bool nsdl_reg_update_needed = false;
erigow01 26:4cac6b346e4f 33
erigow01 26:4cac6b346e4f 34 extern int8_t coap_udp_socket;
erigow01 26:4cac6b346e4f 35
erigow01 26:4cac6b346e4f 36 void *nsdl_alloc(uint16_t size)
erigow01 26:4cac6b346e4f 37 {
erigow01 26:4cac6b346e4f 38 void *buf = malloc(size);
erigow01 26:4cac6b346e4f 39 return buf;
erigow01 26:4cac6b346e4f 40 }
erigow01 26:4cac6b346e4f 41
erigow01 26:4cac6b346e4f 42 void nsdl_free(void* ptr_to_free)
erigow01 26:4cac6b346e4f 43 {
erigow01 26:4cac6b346e4f 44 free(ptr_to_free);
erigow01 26:4cac6b346e4f 45 }
erigow01 26:4cac6b346e4f 46
erigow01 26:4cac6b346e4f 47 /*
erigow01 26:4cac6b346e4f 48 * Create a static resoure
erigow01 26:4cac6b346e4f 49 * Only get is allowed
erigow01 26:4cac6b346e4f 50 */
erigow01 26:4cac6b346e4f 51 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)
erigow01 26:4cac6b346e4f 52 {
erigow01 26:4cac6b346e4f 53 resource_structure->access = SN_GRS_GET_ALLOWED;
erigow01 26:4cac6b346e4f 54 resource_structure->mode = SN_GRS_STATIC;
erigow01 26:4cac6b346e4f 55 resource_structure->pathlen = pt_len;
erigow01 26:4cac6b346e4f 56 resource_structure->path = pt;
erigow01 26:4cac6b346e4f 57 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
erigow01 26:4cac6b346e4f 58 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
erigow01 26:4cac6b346e4f 59 resource_structure->resource = rsc;
erigow01 26:4cac6b346e4f 60 resource_structure->resourcelen = rsc_len;
erigow01 26:4cac6b346e4f 61 sn_nsdl_create_resource(resource_structure);
erigow01 26:4cac6b346e4f 62 }
erigow01 26:4cac6b346e4f 63
erigow01 26:4cac6b346e4f 64 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)
erigow01 26:4cac6b346e4f 65 {
erigow01 26:4cac6b346e4f 66 resource_structure->access = (sn_grs_resource_acl_e)access_right;
erigow01 26:4cac6b346e4f 67 resource_structure->resource = 0;
erigow01 26:4cac6b346e4f 68 resource_structure->resourcelen = 0;
erigow01 26:4cac6b346e4f 69 resource_structure->sn_grs_dyn_res_callback = callback_ptr;
erigow01 26:4cac6b346e4f 70 resource_structure->mode = SN_GRS_DYNAMIC;
erigow01 26:4cac6b346e4f 71 resource_structure->pathlen = pt_len;
erigow01 26:4cac6b346e4f 72 resource_structure->path = pt;
erigow01 26:4cac6b346e4f 73 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
erigow01 26:4cac6b346e4f 74 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
erigow01 26:4cac6b346e4f 75 resource_structure->resource_parameters_ptr->observable = is_observable;
erigow01 26:4cac6b346e4f 76 sn_nsdl_create_resource(resource_structure);
erigow01 26:4cac6b346e4f 77 }
erigow01 26:4cac6b346e4f 78
erigow01 26:4cac6b346e4f 79 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)
erigow01 26:4cac6b346e4f 80 {
erigow01 26:4cac6b346e4f 81 if (NULL == endpoint_structure)
erigow01 26:4cac6b346e4f 82 {
erigow01 26:4cac6b346e4f 83 endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
erigow01 26:4cac6b346e4f 84 }
erigow01 26:4cac6b346e4f 85 if (endpoint_structure)
erigow01 26:4cac6b346e4f 86 {
erigow01 26:4cac6b346e4f 87 memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
erigow01 26:4cac6b346e4f 88 endpoint_structure->endpoint_name_ptr = name;
erigow01 26:4cac6b346e4f 89 endpoint_structure->endpoint_name_len = strlen((char*)name);
erigow01 26:4cac6b346e4f 90 endpoint_structure->type_ptr = typename_ptr;
erigow01 26:4cac6b346e4f 91 endpoint_structure->type_len = strlen((char*)typename_ptr);
erigow01 26:4cac6b346e4f 92 endpoint_structure->lifetime_ptr = lifetime_ptr;
erigow01 26:4cac6b346e4f 93 endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr);
erigow01 26:4cac6b346e4f 94 }
erigow01 26:4cac6b346e4f 95 return endpoint_structure;
erigow01 26:4cac6b346e4f 96 }
erigow01 26:4cac6b346e4f 97
erigow01 26:4cac6b346e4f 98 void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure){
erigow01 26:4cac6b346e4f 99 if (*endpoint_structure)
erigow01 26:4cac6b346e4f 100 {
erigow01 26:4cac6b346e4f 101 nsdl_free(*endpoint_structure);
erigow01 26:4cac6b346e4f 102 *endpoint_structure = NULL;
erigow01 26:4cac6b346e4f 103 }
erigow01 26:4cac6b346e4f 104 }
erigow01 26:4cac6b346e4f 105
erigow01 26:4cac6b346e4f 106 static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr) {
erigow01 26:4cac6b346e4f 107 //UDP
erigow01 26:4cac6b346e4f 108 printf("TX callback!\n\rSending %d bytes\r\n", data_len);
erigow01 26:4cac6b346e4f 109 if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len)
erigow01 26:4cac6b346e4f 110 printf("sending failed\n\r");
erigow01 26:4cac6b346e4f 111 return 1;
erigow01 26:4cac6b346e4f 112 }
erigow01 26:4cac6b346e4f 113
erigow01 26:4cac6b346e4f 114 static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr){
erigow01 26:4cac6b346e4f 115 printf("RX callback!\r\n");
erigow01 26:4cac6b346e4f 116 printf("msg_code: %d \r\n", coap_packet_ptr->msg_code);
erigow01 26:4cac6b346e4f 117 printf("Payload length: %d bytes\r\n", coap_packet_ptr->payload_len);
erigow01 26:4cac6b346e4f 118 int i;
erigow01 26:4cac6b346e4f 119 printf("Payload:'");
erigow01 26:4cac6b346e4f 120 for (i=0; i < coap_packet_ptr->payload_len; i++)
erigow01 26:4cac6b346e4f 121 printf("%c", *(coap_packet_ptr->payload_ptr + i));
erigow01 26:4cac6b346e4f 122 printf("' \r\n");
erigow01 26:4cac6b346e4f 123 if (coap_packet_ptr->options_list_ptr && coap_packet_ptr->options_list_ptr->location_path_ptr)
erigow01 26:4cac6b346e4f 124 {
erigow01 26:4cac6b346e4f 125 printf("Location: /");
erigow01 26:4cac6b346e4f 126 int i;
erigow01 26:4cac6b346e4f 127 for (i=0; i < coap_packet_ptr->options_list_ptr->location_path_len; i++) printf("%c", (char)(coap_packet_ptr->options_list_ptr->location_path_ptr[i]));
erigow01 26:4cac6b346e4f 128 printf(" \r\n");
erigow01 26:4cac6b346e4f 129 }
erigow01 26:4cac6b346e4f 130 //sn_coap_packet_debug(coap_packet_ptr);
erigow01 26:4cac6b346e4f 131 return 0;
erigow01 26:4cac6b346e4f 132 }
erigow01 26:4cac6b346e4f 133
erigow01 26:4cac6b346e4f 134
erigow01 26:4cac6b346e4f 135 void NSP_registration( ) {
erigow01 26:4cac6b346e4f 136 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
erigow01 26:4cac6b346e4f 137
erigow01 26:4cac6b346e4f 138 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
erigow01 26:4cac6b346e4f 139 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
erigow01 26:4cac6b346e4f 140 printf("NSP re-registering failed\r\n");
erigow01 26:4cac6b346e4f 141 else
erigow01 26:4cac6b346e4f 142 printf("NSP re-registering OK\r\n");
erigow01 26:4cac6b346e4f 143 nsdl_clean_register_endpoint(&endpoint_ptr);
erigow01 26:4cac6b346e4f 144 }
erigow01 26:4cac6b346e4f 145
erigow01 26:4cac6b346e4f 146
erigow01 26:4cac6b346e4f 147 void nsdl_init(){
erigow01 26:4cac6b346e4f 148 //uint8_t nsp_addr[4];
erigow01 26:4cac6b346e4f 149 sn_nsdl_mem_s memory_cbs;
erigow01 26:4cac6b346e4f 150
erigow01 26:4cac6b346e4f 151 /* Initialize libNsdl */
erigow01 26:4cac6b346e4f 152 memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
erigow01 26:4cac6b346e4f 153 memory_cbs.sn_nsdl_free = &nsdl_free;
erigow01 26:4cac6b346e4f 154 if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1)
erigow01 26:4cac6b346e4f 155 printf("libNsdl init failed\r\n");
erigow01 26:4cac6b346e4f 156 else
erigow01 26:4cac6b346e4f 157 printf("libNsdl init done\r\n");
erigow01 26:4cac6b346e4f 158 server.init();
erigow01 26:4cac6b346e4f 159 server.bind(COAP_UDP_PORT);
erigow01 26:4cac6b346e4f 160 nsp.set_address(NSP_IP_ADDRESS, COAP_UDP_PORT);
erigow01 26:4cac6b346e4f 161 /* Set nsp address for library */
erigow01 26:4cac6b346e4f 162 //set_NSP_address(NSP_address_bytes, COAP_UDP_PORT, SN_NSDL_ADDRESS_TYPE_IPV6);
erigow01 26:4cac6b346e4f 163 set_NSP_address(NSP_address_bytes, COAP_UDP_PORT, SN_NSDL_ADDRESS_TYPE_IPV4);
erigow01 26:4cac6b346e4f 164 }
erigow01 26:4cac6b346e4f 165
erigow01 26:4cac6b346e4f 166 void nsdl_reg_update()
erigow01 26:4cac6b346e4f 167 {
erigow01 26:4cac6b346e4f 168 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
erigow01 26:4cac6b346e4f 169 // endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
erigow01 26:4cac6b346e4f 170 // reg update should be invoked with null parameters if nothing is changing
erigow01 26:4cac6b346e4f 171 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)null_ep_name, null_ep_type, null_lifetime_ptr);
erigow01 26:4cac6b346e4f 172 if(sn_nsdl_update_registration(endpoint_ptr) != 0)
erigow01 26:4cac6b346e4f 173 printf("NSP re-registering failed\r\n");
erigow01 26:4cac6b346e4f 174 else
erigow01 26:4cac6b346e4f 175 printf("NSP re-registering OK\r\n");
erigow01 26:4cac6b346e4f 176 nsdl_clean_register_endpoint(&endpoint_ptr);
erigow01 26:4cac6b346e4f 177 }
erigow01 26:4cac6b346e4f 178
erigow01 26:4cac6b346e4f 179 void nsdl_reg_update_timeout()
erigow01 26:4cac6b346e4f 180 {
erigow01 26:4cac6b346e4f 181 nsdl_reg_update_needed = true;
erigow01 26:4cac6b346e4f 182 }
erigow01 26:4cac6b346e4f 183
erigow01 26:4cac6b346e4f 184 void nsdl_event_loop()
erigow01 26:4cac6b346e4f 185 {
erigow01 26:4cac6b346e4f 186
erigow01 26:4cac6b346e4f 187 sn_nsdl_addr_s received_packet_address;
erigow01 26:4cac6b346e4f 188 uint8_t received_address[4];
erigow01 26:4cac6b346e4f 189
erigow01 26:4cac6b346e4f 190 memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
erigow01 26:4cac6b346e4f 191 received_packet_address.addr_ptr = received_address;
erigow01 26:4cac6b346e4f 192
erigow01 26:4cac6b346e4f 193 while(1)
erigow01 26:4cac6b346e4f 194 {
erigow01 26:4cac6b346e4f 195 // pc.printf("checking reg timeout\r\n");
erigow01 26:4cac6b346e4f 196 if (nsdl_reg_update_needed)
erigow01 26:4cac6b346e4f 197 {
erigow01 26:4cac6b346e4f 198 nsdl_reg_update_needed = false;
erigow01 26:4cac6b346e4f 199 nsdl_reg_update();
erigow01 26:4cac6b346e4f 200 }
erigow01 26:4cac6b346e4f 201 }
erigow01 26:4cac6b346e4f 202 }