MBED_DEMOS / Mbed 2 deprecated ArchPro_LWM2M_LED_Client

Dependencies:   EthernetInterface PololuLedStripx mbed-rtos mbed nanoservice_client_1_12_X

Fork of LPC1768_LWM2M_Client by MBED_DEMOS

Committer:
michaeljkoster
Date:
Sat Oct 04 04:57:21 2014 +0000
Revision:
11:3e5ac016a106
Parent:
8:1358fc888c9c
Child:
14:25430e88728f
test lwm2m client for KL46Z

Who changed what in which revision?

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