Multitech + KL46Z NanoService example with the RTOS

Dependencies:   mbed SocketModem nanoservice_client_1_12

Committer:
zdshelby
Date:
Fri Mar 28 03:55:03 2014 +0000
Revision:
13:fbb719dc3ec4
Parent:
12:d86931458473
Child:
14:63c1fd3c95a6
- Added additional resources.; - Request to gps/loc resource still causes Telit modem or driver's TCP connection to fail.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zdshelby 0:f739ace74102 1 #include "mbed.h"
zdshelby 0:f739ace74102 2 #include "config.h"
zdshelby 0:f739ace74102 3 #include "debug.h"
zdshelby 0:f739ace74102 4
zdshelby 1:5147d3fa7816 5 // Multitech Cellular includes
zdshelby 1:5147d3fa7816 6 #include "Cellular.h"
zdshelby 1:5147d3fa7816 7 #include "Endpoint.h"
zdshelby 1:5147d3fa7816 8 #include "IPStack.h"
zdshelby 1:5147d3fa7816 9 #include "MTSSerialFlowControl.h"
zdshelby 1:5147d3fa7816 10
zdshelby 3:1e981a0aebfb 11 // NanoService includes
zdshelby 4:5bfd59673a99 12 #include "sn_nsdl.h"
zdshelby 4:5bfd59673a99 13 #include "sn_coap_header.h"
zdshelby 5:6adec9967f93 14 #include "sn_coap_protocol.h"
zdshelby 5:6adec9967f93 15 #include "sn_nsdl_lib.h"
zdshelby 7:d2c5894dcd5e 16 #include "sn_grs.h"
zdshelby 5:6adec9967f93 17 #include <stdint.h>
zdshelby 3:1e981a0aebfb 18
zdshelby 1:5147d3fa7816 19 using namespace mts;
zdshelby 1:5147d3fa7816 20
zdshelby 1:5147d3fa7816 21 Cellular* cellular;
zdshelby 2:1592223f12e1 22 Endpoint nsp;
zdshelby 13:fbb719dc3ec4 23 static const char* NSP_ADDRESS = "176.58.89.233"; // Motivation demo server
zdshelby 7:d2c5894dcd5e 24 static const int NSP_PORT = 5683;
zdshelby 13:fbb719dc3ec4 25 char endpoint_name[] = {"mbed-cellular-mts1"};
zdshelby 12:d86931458473 26 uint8_t ep_type[] = {"mbed_kl64z_multitech"};
zdshelby 8:f7d1fb34d71b 27 uint8_t lifetime_ptr[] = {"86400"};
zdshelby 13:fbb719dc3ec4 28 static const char* FIRMWARE_VER = "12"; // Committed revision number
zdshelby 7:d2c5894dcd5e 29
zdshelby 7:d2c5894dcd5e 30 typedef uint8_t (*sn_grs_dyn_res_callback_t)(sn_coap_hdr_s *, sn_nsdl_addr_s *, sn_proto_info_s *);
zdshelby 1:5147d3fa7816 31
zdshelby 1:5147d3fa7816 32 // ****************************************************************************
zdshelby 1:5147d3fa7816 33 // Cellular initialization
zdshelby 1:5147d3fa7816 34
zdshelby 1:5147d3fa7816 35 static void cellular_init()
zdshelby 1:5147d3fa7816 36 {
zdshelby 1:5147d3fa7816 37 //Setup serial interface to radio
zdshelby 1:5147d3fa7816 38 MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8);
zdshelby 1:5147d3fa7816 39 serial->baud(115200);
zdshelby 1:5147d3fa7816 40
zdshelby 1:5147d3fa7816 41 //Setup Cellular class
zdshelby 1:5147d3fa7816 42 cellular = Cellular::getInstance();
zdshelby 1:5147d3fa7816 43 cellular->init(serial, PTA4, PTC9); //DCD and DTR pins for KL46Z
zdshelby 1:5147d3fa7816 44
zdshelby 1:5147d3fa7816 45 //Run status and configuration commands
zdshelby 1:5147d3fa7816 46 DEBUG("\n\r////Start Status and Configuration Commands////");
zdshelby 1:5147d3fa7816 47 DEBUG("Command Test: %s", getCodeNames(cellular->test()).c_str()); //Make sure you can talk to the radio
zdshelby 1:5147d3fa7816 48 DEBUG("Signal Strength: %d", cellular->getSignalStrength()); //Check the signal strength should be above 8
zdshelby 1:5147d3fa7816 49
zdshelby 1:5147d3fa7816 50 //Makes sure you are reistered with cell
zdshelby 1:5147d3fa7816 51 DEBUG("Registration State: %s", Cellular::getRegistrationNames(cellular->getRegistration()).c_str());
zdshelby 1:5147d3fa7816 52
zdshelby 1:5147d3fa7816 53 //Shows example of how to send other commands, look at AT command guide for more info
zdshelby 1:5147d3fa7816 54 DEBUG("Send Basic Command (AT): %s", getCodeNames(cellular->sendBasicCommand("AT", 1000)).c_str());
zdshelby 1:5147d3fa7816 55 DEBUG("Send Command (AT+CSQ): %s", cellular->sendCommand("AT+CSQ", 1000).c_str());
zdshelby 1:5147d3fa7816 56
zdshelby 1:5147d3fa7816 57 //Start Test
zdshelby 1:5147d3fa7816 58 DEBUG("\n\r////Start Network Connectivity Test////");
zdshelby 1:5147d3fa7816 59 DEBUG("Set APN: %s", getCodeNames(cellular->setApn(CELLULAR_APN)).c_str()); //Use APN from service provider!!!
zdshelby 1:5147d3fa7816 60
zdshelby 1:5147d3fa7816 61 //Setup a data connection
zdshelby 1:5147d3fa7816 62 DEBUG("Attempting to Connect, this may take some time...");
zdshelby 1:5147d3fa7816 63 while (!cellular->connect()) {
zdshelby 9:5b116d75f41a 64 WARNING("Failed to connect... Trying again.");
zdshelby 1:5147d3fa7816 65 wait(1);
zdshelby 1:5147d3fa7816 66 }
zdshelby 1:5147d3fa7816 67 DEBUG("Connected to the Network!");
zdshelby 1:5147d3fa7816 68
zdshelby 1:5147d3fa7816 69 //Try pinging default server "8.8.8.8" (Google's DNS)
zdshelby 1:5147d3fa7816 70 DEBUG("Ping Valid: %s", cellular->ping() ? "true" : "false");
zdshelby 1:5147d3fa7816 71 wait(3);
zdshelby 1:5147d3fa7816 72
zdshelby 1:5147d3fa7816 73 }
zdshelby 1:5147d3fa7816 74
zdshelby 1:5147d3fa7816 75 // ****************************************************************************
zdshelby 1:5147d3fa7816 76 // NSP initialization
zdshelby 1:5147d3fa7816 77
zdshelby 9:5b116d75f41a 78 static void nsp_connect()
zdshelby 1:5147d3fa7816 79 {
zdshelby 2:1592223f12e1 80 nsp.set_address(NSP_ADDRESS, NSP_PORT);
zdshelby 2:1592223f12e1 81
zdshelby 7:d2c5894dcd5e 82 DEBUG("EP Name: %s", endpoint_name);
zdshelby 2:1592223f12e1 83 DEBUG("NSP Location: coap://%s:%d\n", NSP_ADDRESS, NSP_PORT);
zdshelby 2:1592223f12e1 84
zdshelby 1:5147d3fa7816 85 // Bind the port
zdshelby 1:5147d3fa7816 86 cellular->bind(EP_PORT);
zdshelby 1:5147d3fa7816 87
zdshelby 1:5147d3fa7816 88 // Open a TCP connection
zdshelby 1:5147d3fa7816 89 while (!cellular->open(NSP_ADDRESS, NSP_PORT, mts::IPStack::TCP))
zdshelby 1:5147d3fa7816 90 {
zdshelby 9:5b116d75f41a 91 WARNING("TCP connection failed.");
zdshelby 1:5147d3fa7816 92 wait(3);
zdshelby 1:5147d3fa7816 93 }
zdshelby 1:5147d3fa7816 94 DEBUG("TCP connection to NSP successful.");
zdshelby 1:5147d3fa7816 95 }
zdshelby 1:5147d3fa7816 96
zdshelby 4:5bfd59673a99 97 extern "C" void *nsdl_alloc(uint16_t size)
zdshelby 4:5bfd59673a99 98 {
zdshelby 4:5bfd59673a99 99 return malloc(size);
zdshelby 4:5bfd59673a99 100 }
zdshelby 4:5bfd59673a99 101
zdshelby 4:5bfd59673a99 102 extern "C" void nsdl_free(void* ptr_to_free)
zdshelby 4:5bfd59673a99 103 {
zdshelby 4:5bfd59673a99 104 free(ptr_to_free);
zdshelby 4:5bfd59673a99 105 }
zdshelby 4:5bfd59673a99 106
zdshelby 5:6adec9967f93 107 static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr)
zdshelby 5:6adec9967f93 108 {
zdshelby 9:5b116d75f41a 109 int buffer_len = data_len+2;
zdshelby 9:5b116d75f41a 110 DEBUG("TX callback! Sending %d bytes", buffer_len);
zdshelby 9:5b116d75f41a 111 char buffer[buffer_len];
zdshelby 9:5b116d75f41a 112 buffer[0] = data_len >> 8;
zdshelby 9:5b116d75f41a 113 buffer[1] = data_len & 0xFF;
zdshelby 9:5b116d75f41a 114 memcpy(buffer+2, data_ptr, data_len);
zdshelby 8:f7d1fb34d71b 115
zdshelby 9:5b116d75f41a 116 if(cellular->write((char*)buffer, (int)buffer_len, 1000) != buffer_len)
zdshelby 9:5b116d75f41a 117 WARNING("Sending failed");
zdshelby 5:6adec9967f93 118
zdshelby 5:6adec9967f93 119 return 1;
zdshelby 5:6adec9967f93 120 }
zdshelby 5:6adec9967f93 121
zdshelby 5:6adec9967f93 122 static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
zdshelby 5:6adec9967f93 123 {
zdshelby 5:6adec9967f93 124 DEBUG("RX callback!");
zdshelby 5:6adec9967f93 125 return 0;
zdshelby 5:6adec9967f93 126 }
zdshelby 5:6adec9967f93 127
zdshelby 7:d2c5894dcd5e 128 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)
zdshelby 7:d2c5894dcd5e 129 {
zdshelby 7:d2c5894dcd5e 130 resource_structure->access = SN_GRS_GET_ALLOWED;
zdshelby 7:d2c5894dcd5e 131 resource_structure->mode = SN_GRS_STATIC;
zdshelby 7:d2c5894dcd5e 132 resource_structure->pathlen = pt_len;
zdshelby 7:d2c5894dcd5e 133 resource_structure->path = pt;
zdshelby 7:d2c5894dcd5e 134 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
zdshelby 7:d2c5894dcd5e 135 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
zdshelby 7:d2c5894dcd5e 136 resource_structure->resource = rsc;
zdshelby 7:d2c5894dcd5e 137 resource_structure->resourcelen = rsc_len;
zdshelby 7:d2c5894dcd5e 138 sn_nsdl_create_resource(resource_structure);
zdshelby 7:d2c5894dcd5e 139 }
zdshelby 7:d2c5894dcd5e 140
zdshelby 7:d2c5894dcd5e 141 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)
zdshelby 7:d2c5894dcd5e 142 {
zdshelby 7:d2c5894dcd5e 143 resource_structure->access = (sn_grs_resource_acl_e)access_right;
zdshelby 7:d2c5894dcd5e 144 resource_structure->resource = 0;
zdshelby 7:d2c5894dcd5e 145 resource_structure->resourcelen = 0;
zdshelby 7:d2c5894dcd5e 146 resource_structure->sn_grs_dyn_res_callback = callback_ptr;
zdshelby 7:d2c5894dcd5e 147 resource_structure->mode = SN_GRS_DYNAMIC;
zdshelby 7:d2c5894dcd5e 148 resource_structure->pathlen = pt_len;
zdshelby 7:d2c5894dcd5e 149 resource_structure->path = pt;
zdshelby 7:d2c5894dcd5e 150 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
zdshelby 7:d2c5894dcd5e 151 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
zdshelby 7:d2c5894dcd5e 152 resource_structure->resource_parameters_ptr->observable = is_observable;
zdshelby 7:d2c5894dcd5e 153 sn_nsdl_create_resource(resource_structure);
zdshelby 7:d2c5894dcd5e 154 }
zdshelby 7:d2c5894dcd5e 155
zdshelby 7:d2c5894dcd5e 156 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)
zdshelby 7:d2c5894dcd5e 157 {
zdshelby 7:d2c5894dcd5e 158 if (NULL == endpoint_structure)
zdshelby 7:d2c5894dcd5e 159 {
zdshelby 7:d2c5894dcd5e 160 endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
zdshelby 7:d2c5894dcd5e 161 }
zdshelby 7:d2c5894dcd5e 162 if (endpoint_structure)
zdshelby 7:d2c5894dcd5e 163 {
zdshelby 7:d2c5894dcd5e 164 memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
zdshelby 7:d2c5894dcd5e 165 endpoint_structure->endpoint_name_ptr = name;
zdshelby 7:d2c5894dcd5e 166 endpoint_structure->endpoint_name_len = strlen((char*)name);
zdshelby 7:d2c5894dcd5e 167 endpoint_structure->type_ptr = typename_ptr;
zdshelby 7:d2c5894dcd5e 168 endpoint_structure->type_len = strlen((char*)typename_ptr);
zdshelby 7:d2c5894dcd5e 169 endpoint_structure->lifetime_ptr = lifetime_ptr;
zdshelby 7:d2c5894dcd5e 170 endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr);
zdshelby 7:d2c5894dcd5e 171 }
zdshelby 7:d2c5894dcd5e 172 return endpoint_structure;
zdshelby 7:d2c5894dcd5e 173 }
zdshelby 7:d2c5894dcd5e 174
zdshelby 7:d2c5894dcd5e 175 void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure)
zdshelby 7:d2c5894dcd5e 176 {
zdshelby 7:d2c5894dcd5e 177 if (*endpoint_structure)
zdshelby 7:d2c5894dcd5e 178 {
zdshelby 7:d2c5894dcd5e 179 nsdl_free(*endpoint_structure);
zdshelby 7:d2c5894dcd5e 180 *endpoint_structure = NULL;
zdshelby 7:d2c5894dcd5e 181 }
zdshelby 7:d2c5894dcd5e 182 }
zdshelby 7:d2c5894dcd5e 183
zdshelby 5:6adec9967f93 184 void nsdl_init()
zdshelby 5:6adec9967f93 185 {
zdshelby 7:d2c5894dcd5e 186 uint8_t nsp_addr[4];
zdshelby 5:6adec9967f93 187 sn_nsdl_mem_s memory_cbs;
zdshelby 5:6adec9967f93 188 memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
zdshelby 5:6adec9967f93 189 memory_cbs.sn_nsdl_free = &nsdl_free;
zdshelby 5:6adec9967f93 190 if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) {
zdshelby 9:5b116d75f41a 191 ERROR("libNsdl init failed");
zdshelby 5:6adec9967f93 192 } else {
zdshelby 5:6adec9967f93 193 DEBUG("libNsdl init done");
zdshelby 5:6adec9967f93 194 }
zdshelby 7:d2c5894dcd5e 195 /* Set nsp address for library */
zdshelby 7:d2c5894dcd5e 196 set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4);
zdshelby 7:d2c5894dcd5e 197 }
zdshelby 7:d2c5894dcd5e 198
zdshelby 7:d2c5894dcd5e 199 static int create_resources()
zdshelby 7:d2c5894dcd5e 200 {
zdshelby 7:d2c5894dcd5e 201 sn_nsdl_resource_info_s *resource_ptr = NULL;
zdshelby 7:d2c5894dcd5e 202 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
zdshelby 7:d2c5894dcd5e 203
zdshelby 7:d2c5894dcd5e 204 DEBUG("Creating resources");
zdshelby 7:d2c5894dcd5e 205
zdshelby 7:d2c5894dcd5e 206 /* Create resources */
zdshelby 7:d2c5894dcd5e 207 resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
zdshelby 7:d2c5894dcd5e 208 if(!resource_ptr)
zdshelby 7:d2c5894dcd5e 209 return 0;
zdshelby 7:d2c5894dcd5e 210 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
zdshelby 7:d2c5894dcd5e 211
zdshelby 7:d2c5894dcd5e 212 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
zdshelby 7:d2c5894dcd5e 213 if(!resource_ptr->resource_parameters_ptr)
zdshelby 7:d2c5894dcd5e 214 {
zdshelby 7:d2c5894dcd5e 215 nsdl_free(resource_ptr);
zdshelby 7:d2c5894dcd5e 216 return 0;
zdshelby 7:d2c5894dcd5e 217 }
zdshelby 7:d2c5894dcd5e 218 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
zdshelby 7:d2c5894dcd5e 219
zdshelby 7:d2c5894dcd5e 220 // Static resources
zdshelby 13:fbb719dc3ec4 221 nsdl_create_static_resource(resource_ptr, sizeof("1/0/1")-1, (uint8_t*) "1/0/1", 0, 0, (uint8_t*) lifetime_ptr, sizeof(lifetime_ptr)-1);
zdshelby 11:caf0bdbec3ac 222 nsdl_create_static_resource(resource_ptr, sizeof("3/0/0")-1, (uint8_t*) "3/0/0", 0, 0, (uint8_t*) "Freescale/Multitech", sizeof("Freescale/Multitech")-1);
zdshelby 10:53aad0f75e99 223 nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "KL64Z with Multitech Cellular", sizeof("KL64Z with Multitech Cellular")-1);
zdshelby 10:53aad0f75e99 224 nsdl_create_static_resource(resource_ptr, sizeof("3/0/16")-1, (uint8_t*) "3/0/16", 0, 0, (uint8_t*) "TCP", sizeof("TCP")-1);
zdshelby 13:fbb719dc3ec4 225 nsdl_create_static_resource(resource_ptr, sizeof("3/0/3")-1, (uint8_t*) "3/0/3", 0, 0, (uint8_t*) FIRMWARE_VER, strlen(FIRMWARE_VER));
zdshelby 13:fbb719dc3ec4 226 nsdl_create_static_resource(resource_ptr, sizeof("4/0/0")-1, (uint8_t*) "4/0/0", 0, 0, (uint8_t*) "GPRS", sizeof("GPRS")-1);
zdshelby 13:fbb719dc3ec4 227 nsdl_create_static_resource(resource_ptr, sizeof("4/0/1")-1, (uint8_t*) "4/0/1", 0, 0, (uint8_t*) "GPRS", sizeof("GPRS")-1);
zdshelby 13:fbb719dc3ec4 228 nsdl_create_static_resource(resource_ptr, sizeof("6/0/0")-1, (uint8_t*) "6/0/0", 0, 0, (uint8_t*) "37.959611", sizeof("37.959611")-1);
zdshelby 13:fbb719dc3ec4 229 nsdl_create_static_resource(resource_ptr, sizeof("6/0/1")-1, (uint8_t*) "6/0/1", 0, 0, (uint8_t*) "23.721136", sizeof("23.721136")-1);
zdshelby 13:fbb719dc3ec4 230 // nsdl_create_static_resource(resource_ptr, sizeof("gps/loc")-1, (uint8_t*) "gps/loc", 0, 0, (uint8_t*) "37.959611,23.721136", sizeof("37.959611,23.721136")-1);
zdshelby 12:d86931458473 231 // Broken: This cause the modem driver to crash when the request is received.
zdshelby 7:d2c5894dcd5e 232
zdshelby 7:d2c5894dcd5e 233 // Dynamic resources
zdshelby 7:d2c5894dcd5e 234 // create_light_resource(resource_ptr);
zdshelby 7:d2c5894dcd5e 235 // create_gps_resource(resource_ptr);
zdshelby 7:d2c5894dcd5e 236
zdshelby 7:d2c5894dcd5e 237 /* Register with NSP */
zdshelby 7:d2c5894dcd5e 238 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
zdshelby 7:d2c5894dcd5e 239 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) {
zdshelby 9:5b116d75f41a 240 WARNING("NSP registering failed\r\n");
zdshelby 7:d2c5894dcd5e 241 } else {
zdshelby 7:d2c5894dcd5e 242 DEBUG("NSP registering OK\r\n");
zdshelby 7:d2c5894dcd5e 243 }
zdshelby 7:d2c5894dcd5e 244 nsdl_clean_register_endpoint(&endpoint_ptr);
zdshelby 7:d2c5894dcd5e 245
zdshelby 7:d2c5894dcd5e 246 nsdl_free(resource_ptr->resource_parameters_ptr);
zdshelby 7:d2c5894dcd5e 247 nsdl_free(resource_ptr);
zdshelby 7:d2c5894dcd5e 248 return 1;
zdshelby 7:d2c5894dcd5e 249 }
zdshelby 7:d2c5894dcd5e 250
zdshelby 7:d2c5894dcd5e 251 void nsp_register()
zdshelby 7:d2c5894dcd5e 252 {
zdshelby 7:d2c5894dcd5e 253 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
zdshelby 7:d2c5894dcd5e 254
zdshelby 7:d2c5894dcd5e 255 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
zdshelby 7:d2c5894dcd5e 256 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) {
zdshelby 9:5b116d75f41a 257 WARNING("NSP re-registration failed\r\n");
zdshelby 7:d2c5894dcd5e 258 } else {
zdshelby 7:d2c5894dcd5e 259 DEBUG("NSP re-registration OK\r\n");
zdshelby 7:d2c5894dcd5e 260 }
zdshelby 7:d2c5894dcd5e 261 nsdl_clean_register_endpoint(&endpoint_ptr);
zdshelby 5:6adec9967f93 262 }
zdshelby 5:6adec9967f93 263
zdshelby 3:1e981a0aebfb 264 void socket_event_loop()
zdshelby 3:1e981a0aebfb 265 {
zdshelby 7:d2c5894dcd5e 266 sn_nsdl_addr_s received_packet_address;
zdshelby 7:d2c5894dcd5e 267 uint8_t received_address[4];
zdshelby 3:1e981a0aebfb 268 char buffer[2048];
zdshelby 9:5b116d75f41a 269 int n;
zdshelby 3:1e981a0aebfb 270
zdshelby 7:d2c5894dcd5e 271 memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
zdshelby 7:d2c5894dcd5e 272 received_packet_address.addr_ptr = received_address;
zdshelby 3:1e981a0aebfb 273
zdshelby 4:5bfd59673a99 274 DEBUG("Starting socket read loop...");
zdshelby 3:1e981a0aebfb 275 while(1)
zdshelby 3:1e981a0aebfb 276 {
zdshelby 9:5b116d75f41a 277 n = cellular->read(buffer, sizeof(buffer), 1000);
zdshelby 9:5b116d75f41a 278 DEBUG("Received %d bytes", n);
zdshelby 3:1e981a0aebfb 279 if (n < 0)
zdshelby 3:1e981a0aebfb 280 {
zdshelby 9:5b116d75f41a 281 ERROR("Socket error");
zdshelby 3:1e981a0aebfb 282 }
zdshelby 3:1e981a0aebfb 283 else
zdshelby 3:1e981a0aebfb 284 {
zdshelby 9:5b116d75f41a 285 uint16_t len = 0;
zdshelby 9:5b116d75f41a 286 if (n > 2) {
zdshelby 9:5b116d75f41a 287 len = 256 * buffer[0] + buffer[1];
zdshelby 9:5b116d75f41a 288 DEBUG("CoAP length header = %d bytes", len);
zdshelby 9:5b116d75f41a 289 sn_nsdl_process_coap((uint8_t*)buffer+2, len, &received_packet_address);
zdshelby 9:5b116d75f41a 290 }
zdshelby 9:5b116d75f41a 291 }
zdshelby 3:1e981a0aebfb 292 }
zdshelby 3:1e981a0aebfb 293 }
zdshelby 0:f739ace74102 294
zdshelby 0:f739ace74102 295 int main()
zdshelby 0:f739ace74102 296 {
zdshelby 1:5147d3fa7816 297 printf("\r\n*****************************************************************************\r\n");
zdshelby 0:f739ace74102 298 DEBUG("NanoService Example for KL46Z + Multitech Cellular");
zdshelby 0:f739ace74102 299
zdshelby 1:5147d3fa7816 300 // Inititalize the Cellular modem
zdshelby 1:5147d3fa7816 301 cellular_init();
zdshelby 9:5b116d75f41a 302
zdshelby 1:5147d3fa7816 303 // Bind the socket and configure NSP settings
zdshelby 9:5b116d75f41a 304 nsp_connect();
zdshelby 9:5b116d75f41a 305
zdshelby 4:5bfd59673a99 306 // Initalize NanoService library
zdshelby 5:6adec9967f93 307 nsdl_init();
zdshelby 7:d2c5894dcd5e 308
zdshelby 9:5b116d75f41a 309 // Create resources & register with NSP
zdshelby 7:d2c5894dcd5e 310 create_resources();
zdshelby 5:6adec9967f93 311
zdshelby 3:1e981a0aebfb 312 // Start socket listening loop
zdshelby 3:1e981a0aebfb 313 socket_event_loop();
zdshelby 5:6adec9967f93 314
zdshelby 0:f739ace74102 315 }