Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed SocketModem nanoservice_client_1_12
main.cpp@7:d2c5894dcd5e, 2014-02-18 (annotated)
- Committer:
 - zdshelby
 - Date:
 - Tue Feb 18 01:50:19 2014 +0000
 - Revision:
 - 7:d2c5894dcd5e
 - Parent:
 - 5:6adec9967f93
 - Child:
 - 8:f7d1fb34d71b
 
- NSP registration working, need to add TCP length bytes
Who changed what in which revision?
| User | Revision | Line number | New 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 | 7:d2c5894dcd5e | 23 | static const char* NSP_ADDRESS = "208.111.39.209"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ | 
| zdshelby | 7:d2c5894dcd5e | 24 | static const int NSP_PORT = 5683; | 
| zdshelby | 7:d2c5894dcd5e | 25 | char endpoint_name[] = {"mbed-multitech"}; | 
| zdshelby | 7:d2c5894dcd5e | 26 | uint8_t ep_type[] = {"mbed_lpc1768_appboard"}; | 
| zdshelby | 7:d2c5894dcd5e | 27 | uint8_t lifetime_ptr[] = {"60"}; | 
| zdshelby | 7:d2c5894dcd5e | 28 | |
| zdshelby | 7:d2c5894dcd5e | 29 | typedef uint8_t (*sn_grs_dyn_res_callback_t)(sn_coap_hdr_s *, sn_nsdl_addr_s *, sn_proto_info_s *); | 
| zdshelby | 1:5147d3fa7816 | 30 | |
| zdshelby | 1:5147d3fa7816 | 31 | // **************************************************************************** | 
| zdshelby | 1:5147d3fa7816 | 32 | // Cellular initialization | 
| zdshelby | 1:5147d3fa7816 | 33 | |
| zdshelby | 1:5147d3fa7816 | 34 | static void cellular_init() | 
| zdshelby | 1:5147d3fa7816 | 35 | { | 
| zdshelby | 1:5147d3fa7816 | 36 | //Setup serial interface to radio | 
| zdshelby | 1:5147d3fa7816 | 37 | MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); | 
| zdshelby | 1:5147d3fa7816 | 38 | serial->baud(115200); | 
| zdshelby | 1:5147d3fa7816 | 39 | |
| zdshelby | 1:5147d3fa7816 | 40 | //Setup Cellular class | 
| zdshelby | 1:5147d3fa7816 | 41 | cellular = Cellular::getInstance(); | 
| zdshelby | 1:5147d3fa7816 | 42 | cellular->init(serial, PTA4, PTC9); //DCD and DTR pins for KL46Z | 
| zdshelby | 1:5147d3fa7816 | 43 | |
| zdshelby | 1:5147d3fa7816 | 44 | //Run status and configuration commands | 
| zdshelby | 1:5147d3fa7816 | 45 | DEBUG("\n\r////Start Status and Configuration Commands////"); | 
| zdshelby | 1:5147d3fa7816 | 46 | DEBUG("Command Test: %s", getCodeNames(cellular->test()).c_str()); //Make sure you can talk to the radio | 
| zdshelby | 1:5147d3fa7816 | 47 | DEBUG("Signal Strength: %d", cellular->getSignalStrength()); //Check the signal strength should be above 8 | 
| zdshelby | 1:5147d3fa7816 | 48 | |
| zdshelby | 1:5147d3fa7816 | 49 | //Makes sure you are reistered with cell | 
| zdshelby | 1:5147d3fa7816 | 50 | DEBUG("Registration State: %s", Cellular::getRegistrationNames(cellular->getRegistration()).c_str()); | 
| zdshelby | 1:5147d3fa7816 | 51 | |
| zdshelby | 1:5147d3fa7816 | 52 | //Shows example of how to send other commands, look at AT command guide for more info | 
| zdshelby | 1:5147d3fa7816 | 53 | DEBUG("Send Basic Command (AT): %s", getCodeNames(cellular->sendBasicCommand("AT", 1000)).c_str()); | 
| zdshelby | 1:5147d3fa7816 | 54 | DEBUG("Send Command (AT+CSQ): %s", cellular->sendCommand("AT+CSQ", 1000).c_str()); | 
| zdshelby | 1:5147d3fa7816 | 55 | |
| zdshelby | 1:5147d3fa7816 | 56 | //Start Test | 
| zdshelby | 1:5147d3fa7816 | 57 | DEBUG("\n\r////Start Network Connectivity Test////"); | 
| zdshelby | 1:5147d3fa7816 | 58 | DEBUG("Set APN: %s", getCodeNames(cellular->setApn(CELLULAR_APN)).c_str()); //Use APN from service provider!!! | 
| zdshelby | 1:5147d3fa7816 | 59 | |
| zdshelby | 1:5147d3fa7816 | 60 | //Setup a data connection | 
| zdshelby | 1:5147d3fa7816 | 61 | DEBUG("Attempting to Connect, this may take some time..."); | 
| zdshelby | 1:5147d3fa7816 | 62 | while (!cellular->connect()) { | 
| zdshelby | 1:5147d3fa7816 | 63 | DEBUG("Failed to connect... Trying again."); | 
| zdshelby | 1:5147d3fa7816 | 64 | wait(1); | 
| zdshelby | 1:5147d3fa7816 | 65 | } | 
| zdshelby | 1:5147d3fa7816 | 66 | DEBUG("Connected to the Network!"); | 
| zdshelby | 1:5147d3fa7816 | 67 | |
| zdshelby | 1:5147d3fa7816 | 68 | //Try pinging default server "8.8.8.8" (Google's DNS) | 
| zdshelby | 1:5147d3fa7816 | 69 | DEBUG("Ping Valid: %s", cellular->ping() ? "true" : "false"); | 
| zdshelby | 1:5147d3fa7816 | 70 | wait(3); | 
| zdshelby | 1:5147d3fa7816 | 71 | |
| zdshelby | 1:5147d3fa7816 | 72 | } | 
| zdshelby | 1:5147d3fa7816 | 73 | |
| zdshelby | 1:5147d3fa7816 | 74 | // **************************************************************************** | 
| zdshelby | 1:5147d3fa7816 | 75 | // NSP initialization | 
| zdshelby | 1:5147d3fa7816 | 76 | |
| zdshelby | 1:5147d3fa7816 | 77 | static void nsp_init() | 
| zdshelby | 1:5147d3fa7816 | 78 | { | 
| zdshelby | 2:1592223f12e1 | 79 | nsp.set_address(NSP_ADDRESS, NSP_PORT); | 
| zdshelby | 2:1592223f12e1 | 80 | |
| zdshelby | 7:d2c5894dcd5e | 81 | DEBUG("EP Name: %s", endpoint_name); | 
| zdshelby | 2:1592223f12e1 | 82 | DEBUG("NSP Location: coap://%s:%d\n", NSP_ADDRESS, NSP_PORT); | 
| zdshelby | 2:1592223f12e1 | 83 | |
| zdshelby | 1:5147d3fa7816 | 84 | // Bind the port | 
| zdshelby | 1:5147d3fa7816 | 85 | cellular->bind(EP_PORT); | 
| zdshelby | 1:5147d3fa7816 | 86 | |
| zdshelby | 1:5147d3fa7816 | 87 | // Open a TCP connection | 
| zdshelby | 1:5147d3fa7816 | 88 | while (!cellular->open(NSP_ADDRESS, NSP_PORT, mts::IPStack::TCP)) | 
| zdshelby | 1:5147d3fa7816 | 89 | { | 
| zdshelby | 1:5147d3fa7816 | 90 | DEBUG("TCP connection failed."); | 
| zdshelby | 1:5147d3fa7816 | 91 | wait(3); | 
| zdshelby | 1:5147d3fa7816 | 92 | } | 
| zdshelby | 1:5147d3fa7816 | 93 | DEBUG("TCP connection to NSP successful."); | 
| zdshelby | 1:5147d3fa7816 | 94 | } | 
| zdshelby | 1:5147d3fa7816 | 95 | |
| zdshelby | 4:5bfd59673a99 | 96 | extern "C" void *nsdl_alloc(uint16_t size) | 
| zdshelby | 4:5bfd59673a99 | 97 | { | 
| zdshelby | 4:5bfd59673a99 | 98 | return malloc(size); | 
| zdshelby | 4:5bfd59673a99 | 99 | } | 
| zdshelby | 4:5bfd59673a99 | 100 | |
| zdshelby | 4:5bfd59673a99 | 101 | extern "C" void nsdl_free(void* ptr_to_free) | 
| zdshelby | 4:5bfd59673a99 | 102 | { | 
| zdshelby | 4:5bfd59673a99 | 103 | free(ptr_to_free); | 
| zdshelby | 4:5bfd59673a99 | 104 | } | 
| zdshelby | 4:5bfd59673a99 | 105 | |
| zdshelby | 5:6adec9967f93 | 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) | 
| zdshelby | 5:6adec9967f93 | 107 | { | 
| zdshelby | 7:d2c5894dcd5e | 108 | DEBUG("TX callback! Sending %d bytes", data_len); | 
| zdshelby | 5:6adec9967f93 | 109 | |
| zdshelby | 7:d2c5894dcd5e | 110 | if(cellular->write((char*)data_ptr, (int)data_len, -1) != data_len) | 
| zdshelby | 5:6adec9967f93 | 111 | DEBUG("sending failed"); | 
| zdshelby | 5:6adec9967f93 | 112 | |
| zdshelby | 5:6adec9967f93 | 113 | return 1; | 
| zdshelby | 5:6adec9967f93 | 114 | } | 
| zdshelby | 5:6adec9967f93 | 115 | |
| zdshelby | 5:6adec9967f93 | 116 | static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) | 
| zdshelby | 5:6adec9967f93 | 117 | { | 
| zdshelby | 5:6adec9967f93 | 118 | DEBUG("RX callback!"); | 
| zdshelby | 5:6adec9967f93 | 119 | return 0; | 
| zdshelby | 5:6adec9967f93 | 120 | } | 
| zdshelby | 5:6adec9967f93 | 121 | |
| zdshelby | 7:d2c5894dcd5e | 122 | 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 | 123 | { | 
| zdshelby | 7:d2c5894dcd5e | 124 | resource_structure->access = SN_GRS_GET_ALLOWED; | 
| zdshelby | 7:d2c5894dcd5e | 125 | resource_structure->mode = SN_GRS_STATIC; | 
| zdshelby | 7:d2c5894dcd5e | 126 | resource_structure->pathlen = pt_len; | 
| zdshelby | 7:d2c5894dcd5e | 127 | resource_structure->path = pt; | 
| zdshelby | 7:d2c5894dcd5e | 128 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; | 
| zdshelby | 7:d2c5894dcd5e | 129 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; | 
| zdshelby | 7:d2c5894dcd5e | 130 | resource_structure->resource = rsc; | 
| zdshelby | 7:d2c5894dcd5e | 131 | resource_structure->resourcelen = rsc_len; | 
| zdshelby | 7:d2c5894dcd5e | 132 | sn_nsdl_create_resource(resource_structure); | 
| zdshelby | 7:d2c5894dcd5e | 133 | } | 
| zdshelby | 7:d2c5894dcd5e | 134 | |
| zdshelby | 7:d2c5894dcd5e | 135 | 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 | 136 | { | 
| zdshelby | 7:d2c5894dcd5e | 137 | resource_structure->access = (sn_grs_resource_acl_e)access_right; | 
| zdshelby | 7:d2c5894dcd5e | 138 | resource_structure->resource = 0; | 
| zdshelby | 7:d2c5894dcd5e | 139 | resource_structure->resourcelen = 0; | 
| zdshelby | 7:d2c5894dcd5e | 140 | resource_structure->sn_grs_dyn_res_callback = callback_ptr; | 
| zdshelby | 7:d2c5894dcd5e | 141 | resource_structure->mode = SN_GRS_DYNAMIC; | 
| zdshelby | 7:d2c5894dcd5e | 142 | resource_structure->pathlen = pt_len; | 
| zdshelby | 7:d2c5894dcd5e | 143 | resource_structure->path = pt; | 
| zdshelby | 7:d2c5894dcd5e | 144 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; | 
| zdshelby | 7:d2c5894dcd5e | 145 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; | 
| zdshelby | 7:d2c5894dcd5e | 146 | resource_structure->resource_parameters_ptr->observable = is_observable; | 
| zdshelby | 7:d2c5894dcd5e | 147 | sn_nsdl_create_resource(resource_structure); | 
| zdshelby | 7:d2c5894dcd5e | 148 | } | 
| zdshelby | 7:d2c5894dcd5e | 149 | |
| zdshelby | 7:d2c5894dcd5e | 150 | 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 | 151 | { | 
| zdshelby | 7:d2c5894dcd5e | 152 | if (NULL == endpoint_structure) | 
| zdshelby | 7:d2c5894dcd5e | 153 | { | 
| zdshelby | 7:d2c5894dcd5e | 154 | endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s)); | 
| zdshelby | 7:d2c5894dcd5e | 155 | } | 
| zdshelby | 7:d2c5894dcd5e | 156 | if (endpoint_structure) | 
| zdshelby | 7:d2c5894dcd5e | 157 | { | 
| zdshelby | 7:d2c5894dcd5e | 158 | memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s)); | 
| zdshelby | 7:d2c5894dcd5e | 159 | endpoint_structure->endpoint_name_ptr = name; | 
| zdshelby | 7:d2c5894dcd5e | 160 | endpoint_structure->endpoint_name_len = strlen((char*)name); | 
| zdshelby | 7:d2c5894dcd5e | 161 | endpoint_structure->type_ptr = typename_ptr; | 
| zdshelby | 7:d2c5894dcd5e | 162 | endpoint_structure->type_len = strlen((char*)typename_ptr); | 
| zdshelby | 7:d2c5894dcd5e | 163 | endpoint_structure->lifetime_ptr = lifetime_ptr; | 
| zdshelby | 7:d2c5894dcd5e | 164 | endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr); | 
| zdshelby | 7:d2c5894dcd5e | 165 | } | 
| zdshelby | 7:d2c5894dcd5e | 166 | return endpoint_structure; | 
| zdshelby | 7:d2c5894dcd5e | 167 | } | 
| zdshelby | 7:d2c5894dcd5e | 168 | |
| zdshelby | 7:d2c5894dcd5e | 169 | void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) | 
| zdshelby | 7:d2c5894dcd5e | 170 | { | 
| zdshelby | 7:d2c5894dcd5e | 171 | if (*endpoint_structure) | 
| zdshelby | 7:d2c5894dcd5e | 172 | { | 
| zdshelby | 7:d2c5894dcd5e | 173 | nsdl_free(*endpoint_structure); | 
| zdshelby | 7:d2c5894dcd5e | 174 | *endpoint_structure = NULL; | 
| zdshelby | 7:d2c5894dcd5e | 175 | } | 
| zdshelby | 7:d2c5894dcd5e | 176 | } | 
| zdshelby | 7:d2c5894dcd5e | 177 | |
| zdshelby | 5:6adec9967f93 | 178 | void nsdl_init() | 
| zdshelby | 5:6adec9967f93 | 179 | { | 
| zdshelby | 7:d2c5894dcd5e | 180 | uint8_t nsp_addr[4]; | 
| zdshelby | 5:6adec9967f93 | 181 | sn_nsdl_mem_s memory_cbs; | 
| zdshelby | 5:6adec9967f93 | 182 | memory_cbs.sn_nsdl_alloc = &nsdl_alloc; | 
| zdshelby | 5:6adec9967f93 | 183 | memory_cbs.sn_nsdl_free = &nsdl_free; | 
| zdshelby | 5:6adec9967f93 | 184 | if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) { | 
| zdshelby | 5:6adec9967f93 | 185 | DEBUG("libNsdl init failed"); | 
| zdshelby | 5:6adec9967f93 | 186 | } else { | 
| zdshelby | 5:6adec9967f93 | 187 | DEBUG("libNsdl init done"); | 
| zdshelby | 5:6adec9967f93 | 188 | } | 
| zdshelby | 7:d2c5894dcd5e | 189 | /* Set nsp address for library */ | 
| zdshelby | 7:d2c5894dcd5e | 190 | set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4); | 
| zdshelby | 7:d2c5894dcd5e | 191 | } | 
| zdshelby | 7:d2c5894dcd5e | 192 | |
| zdshelby | 7:d2c5894dcd5e | 193 | static int create_resources() | 
| zdshelby | 7:d2c5894dcd5e | 194 | { | 
| zdshelby | 7:d2c5894dcd5e | 195 | sn_nsdl_resource_info_s *resource_ptr = NULL; | 
| zdshelby | 7:d2c5894dcd5e | 196 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; | 
| zdshelby | 7:d2c5894dcd5e | 197 | |
| zdshelby | 7:d2c5894dcd5e | 198 | DEBUG("Creating resources"); | 
| zdshelby | 7:d2c5894dcd5e | 199 | |
| zdshelby | 7:d2c5894dcd5e | 200 | /* Create resources */ | 
| zdshelby | 7:d2c5894dcd5e | 201 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); | 
| zdshelby | 7:d2c5894dcd5e | 202 | if(!resource_ptr) | 
| zdshelby | 7:d2c5894dcd5e | 203 | return 0; | 
| zdshelby | 7:d2c5894dcd5e | 204 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); | 
| zdshelby | 7:d2c5894dcd5e | 205 | |
| zdshelby | 7:d2c5894dcd5e | 206 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); | 
| zdshelby | 7:d2c5894dcd5e | 207 | if(!resource_ptr->resource_parameters_ptr) | 
| zdshelby | 7:d2c5894dcd5e | 208 | { | 
| zdshelby | 7:d2c5894dcd5e | 209 | nsdl_free(resource_ptr); | 
| zdshelby | 7:d2c5894dcd5e | 210 | return 0; | 
| zdshelby | 7:d2c5894dcd5e | 211 | } | 
| zdshelby | 7:d2c5894dcd5e | 212 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); | 
| zdshelby | 7:d2c5894dcd5e | 213 | |
| zdshelby | 7:d2c5894dcd5e | 214 | // Static resources | 
| zdshelby | 7:d2c5894dcd5e | 215 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/0")-1, (uint8_t*) "3/0/0", 0, 0, (uint8_t*) "ARM", sizeof("ARM")-1); | 
| zdshelby | 7:d2c5894dcd5e | 216 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "LPC1768 App Board", sizeof("LPC1768 App Board")-1); | 
| zdshelby | 7:d2c5894dcd5e | 217 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/16")-1, (uint8_t*) "3/0/16", 0, 0, (uint8_t*) "U", sizeof("U")-1); | 
| zdshelby | 7:d2c5894dcd5e | 218 | |
| zdshelby | 7:d2c5894dcd5e | 219 | // Dynamic resources | 
| zdshelby | 7:d2c5894dcd5e | 220 | // create_light_resource(resource_ptr); | 
| zdshelby | 7:d2c5894dcd5e | 221 | // create_gps_resource(resource_ptr); | 
| zdshelby | 7:d2c5894dcd5e | 222 | |
| zdshelby | 7:d2c5894dcd5e | 223 | /* Register with NSP */ | 
| zdshelby | 7:d2c5894dcd5e | 224 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); | 
| zdshelby | 7:d2c5894dcd5e | 225 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) { | 
| zdshelby | 7:d2c5894dcd5e | 226 | DEBUG("NSP registering failed\r\n"); | 
| zdshelby | 7:d2c5894dcd5e | 227 | } else { | 
| zdshelby | 7:d2c5894dcd5e | 228 | DEBUG("NSP registering OK\r\n"); | 
| zdshelby | 7:d2c5894dcd5e | 229 | } | 
| zdshelby | 7:d2c5894dcd5e | 230 | nsdl_clean_register_endpoint(&endpoint_ptr); | 
| zdshelby | 7:d2c5894dcd5e | 231 | |
| zdshelby | 7:d2c5894dcd5e | 232 | nsdl_free(resource_ptr->resource_parameters_ptr); | 
| zdshelby | 7:d2c5894dcd5e | 233 | nsdl_free(resource_ptr); | 
| zdshelby | 7:d2c5894dcd5e | 234 | return 1; | 
| zdshelby | 7:d2c5894dcd5e | 235 | } | 
| zdshelby | 7:d2c5894dcd5e | 236 | |
| zdshelby | 7:d2c5894dcd5e | 237 | void nsp_register() | 
| zdshelby | 7:d2c5894dcd5e | 238 | { | 
| zdshelby | 7:d2c5894dcd5e | 239 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; | 
| zdshelby | 7:d2c5894dcd5e | 240 | |
| zdshelby | 7:d2c5894dcd5e | 241 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); | 
| zdshelby | 7:d2c5894dcd5e | 242 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) { | 
| zdshelby | 7:d2c5894dcd5e | 243 | DEBUG("NSP re-registration failed\r\n"); | 
| zdshelby | 7:d2c5894dcd5e | 244 | } else { | 
| zdshelby | 7:d2c5894dcd5e | 245 | DEBUG("NSP re-registration OK\r\n"); | 
| zdshelby | 7:d2c5894dcd5e | 246 | } | 
| zdshelby | 7:d2c5894dcd5e | 247 | nsdl_clean_register_endpoint(&endpoint_ptr); | 
| zdshelby | 5:6adec9967f93 | 248 | } | 
| zdshelby | 5:6adec9967f93 | 249 | |
| zdshelby | 3:1e981a0aebfb | 250 | void socket_event_loop() | 
| zdshelby | 3:1e981a0aebfb | 251 | { | 
| zdshelby | 7:d2c5894dcd5e | 252 | sn_nsdl_addr_s received_packet_address; | 
| zdshelby | 7:d2c5894dcd5e | 253 | uint8_t received_address[4]; | 
| zdshelby | 3:1e981a0aebfb | 254 | char buffer[2048]; | 
| zdshelby | 3:1e981a0aebfb | 255 | |
| zdshelby | 7:d2c5894dcd5e | 256 | memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); | 
| zdshelby | 7:d2c5894dcd5e | 257 | received_packet_address.addr_ptr = received_address; | 
| zdshelby | 3:1e981a0aebfb | 258 | |
| zdshelby | 4:5bfd59673a99 | 259 | DEBUG("Starting socket read loop..."); | 
| zdshelby | 3:1e981a0aebfb | 260 | while(1) | 
| zdshelby | 3:1e981a0aebfb | 261 | { | 
| zdshelby | 3:1e981a0aebfb | 262 | int n = cellular->read(buffer, sizeof(buffer), -1); | 
| zdshelby | 3:1e981a0aebfb | 263 | if (n < 0) | 
| zdshelby | 3:1e981a0aebfb | 264 | { | 
| zdshelby | 3:1e981a0aebfb | 265 | DEBUG("Socket error\n\r"); | 
| zdshelby | 3:1e981a0aebfb | 266 | } | 
| zdshelby | 3:1e981a0aebfb | 267 | else | 
| zdshelby | 3:1e981a0aebfb | 268 | { | 
| zdshelby | 3:1e981a0aebfb | 269 | DEBUG("Received %d bytes\r\n", n); | 
| zdshelby | 7:d2c5894dcd5e | 270 | sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address); | 
| zdshelby | 3:1e981a0aebfb | 271 | } | 
| zdshelby | 3:1e981a0aebfb | 272 | } | 
| zdshelby | 3:1e981a0aebfb | 273 | } | 
| zdshelby | 0:f739ace74102 | 274 | |
| zdshelby | 0:f739ace74102 | 275 | int main() | 
| zdshelby | 0:f739ace74102 | 276 | { | 
| zdshelby | 1:5147d3fa7816 | 277 | printf("\r\n*****************************************************************************\r\n"); | 
| zdshelby | 0:f739ace74102 | 278 | DEBUG("NanoService Example for KL46Z + Multitech Cellular"); | 
| zdshelby | 0:f739ace74102 | 279 | |
| zdshelby | 1:5147d3fa7816 | 280 | // Inititalize the Cellular modem | 
| zdshelby | 1:5147d3fa7816 | 281 | cellular_init(); | 
| zdshelby | 1:5147d3fa7816 | 282 | |
| zdshelby | 1:5147d3fa7816 | 283 | // Bind the socket and configure NSP settings | 
| zdshelby | 1:5147d3fa7816 | 284 | nsp_init(); | 
| zdshelby | 0:f739ace74102 | 285 | |
| zdshelby | 4:5bfd59673a99 | 286 | // Initalize NanoService library | 
| zdshelby | 5:6adec9967f93 | 287 | nsdl_init(); | 
| zdshelby | 7:d2c5894dcd5e | 288 | |
| zdshelby | 7:d2c5894dcd5e | 289 | // Create resources | 
| zdshelby | 7:d2c5894dcd5e | 290 | create_resources(); | 
| zdshelby | 5:6adec9967f93 | 291 | |
| zdshelby | 3:1e981a0aebfb | 292 | // Start socket listening loop | 
| zdshelby | 3:1e981a0aebfb | 293 | socket_event_loop(); | 
| zdshelby | 5:6adec9967f93 | 294 | |
| zdshelby | 0:f739ace74102 | 295 | } | 
