Fork of KL46Z Wifi version. Moved to KL25Z as umbrella stand now requires 2 SPIs, thus requires 46z.

Dependencies:   WIZnet_Library_ASE mbed nanoservice_client_1_12

Fork of Trenton_Doormat_FRDM-KL25Z_ETH by Eric Gowland

Committer:
erigow01
Date:
Fri Apr 04 15:17:07 2014 +0000
Revision:
16:3fb612af0dc5
Child:
17:8ca4a5801430
Seems to be working relatively reliably.

Who changed what in which revision?

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