Multitech + KL46Z NanoService example with the RTOS
Dependencies: mbed SocketModem nanoservice_client_1_12
main.cpp@8:f7d1fb34d71b, 2014-02-18 (annotated)
- Committer:
- zdshelby
- Date:
- Tue Feb 18 02:03:19 2014 +0000
- Revision:
- 8:f7d1fb34d71b
- Parent:
- 7:d2c5894dcd5e
- Child:
- 9:5b116d75f41a
- NSP registration successful
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 | 8:f7d1fb34d71b | 27 | uint8_t lifetime_ptr[] = {"86400"}; |
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 | 8:f7d1fb34d71b | 109 | char coap_len[2]; |
zdshelby | 8:f7d1fb34d71b | 110 | coap_len[0] = data_len >> 8; |
zdshelby | 8:f7d1fb34d71b | 111 | coap_len[1] = data_len & 0xFF; |
zdshelby | 8:f7d1fb34d71b | 112 | |
zdshelby | 8:f7d1fb34d71b | 113 | if(cellular->write((char*)&coap_len, 2, -1) != 2) |
zdshelby | 8:f7d1fb34d71b | 114 | DEBUG("length sending failed"); |
zdshelby | 7:d2c5894dcd5e | 115 | if(cellular->write((char*)data_ptr, (int)data_len, -1) != data_len) |
zdshelby | 5:6adec9967f93 | 116 | DEBUG("sending failed"); |
zdshelby | 5:6adec9967f93 | 117 | |
zdshelby | 5:6adec9967f93 | 118 | return 1; |
zdshelby | 5:6adec9967f93 | 119 | } |
zdshelby | 5:6adec9967f93 | 120 | |
zdshelby | 5:6adec9967f93 | 121 | static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) |
zdshelby | 5:6adec9967f93 | 122 | { |
zdshelby | 5:6adec9967f93 | 123 | DEBUG("RX callback!"); |
zdshelby | 5:6adec9967f93 | 124 | return 0; |
zdshelby | 5:6adec9967f93 | 125 | } |
zdshelby | 5:6adec9967f93 | 126 | |
zdshelby | 7:d2c5894dcd5e | 127 | 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 | 128 | { |
zdshelby | 7:d2c5894dcd5e | 129 | resource_structure->access = SN_GRS_GET_ALLOWED; |
zdshelby | 7:d2c5894dcd5e | 130 | resource_structure->mode = SN_GRS_STATIC; |
zdshelby | 7:d2c5894dcd5e | 131 | resource_structure->pathlen = pt_len; |
zdshelby | 7:d2c5894dcd5e | 132 | resource_structure->path = pt; |
zdshelby | 7:d2c5894dcd5e | 133 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; |
zdshelby | 7:d2c5894dcd5e | 134 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; |
zdshelby | 7:d2c5894dcd5e | 135 | resource_structure->resource = rsc; |
zdshelby | 7:d2c5894dcd5e | 136 | resource_structure->resourcelen = rsc_len; |
zdshelby | 7:d2c5894dcd5e | 137 | sn_nsdl_create_resource(resource_structure); |
zdshelby | 7:d2c5894dcd5e | 138 | } |
zdshelby | 7:d2c5894dcd5e | 139 | |
zdshelby | 7:d2c5894dcd5e | 140 | 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 | 141 | { |
zdshelby | 7:d2c5894dcd5e | 142 | resource_structure->access = (sn_grs_resource_acl_e)access_right; |
zdshelby | 7:d2c5894dcd5e | 143 | resource_structure->resource = 0; |
zdshelby | 7:d2c5894dcd5e | 144 | resource_structure->resourcelen = 0; |
zdshelby | 7:d2c5894dcd5e | 145 | resource_structure->sn_grs_dyn_res_callback = callback_ptr; |
zdshelby | 7:d2c5894dcd5e | 146 | resource_structure->mode = SN_GRS_DYNAMIC; |
zdshelby | 7:d2c5894dcd5e | 147 | resource_structure->pathlen = pt_len; |
zdshelby | 7:d2c5894dcd5e | 148 | resource_structure->path = pt; |
zdshelby | 7:d2c5894dcd5e | 149 | resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; |
zdshelby | 7:d2c5894dcd5e | 150 | resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; |
zdshelby | 7:d2c5894dcd5e | 151 | resource_structure->resource_parameters_ptr->observable = is_observable; |
zdshelby | 7:d2c5894dcd5e | 152 | sn_nsdl_create_resource(resource_structure); |
zdshelby | 7:d2c5894dcd5e | 153 | } |
zdshelby | 7:d2c5894dcd5e | 154 | |
zdshelby | 7:d2c5894dcd5e | 155 | 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 | 156 | { |
zdshelby | 7:d2c5894dcd5e | 157 | if (NULL == endpoint_structure) |
zdshelby | 7:d2c5894dcd5e | 158 | { |
zdshelby | 7:d2c5894dcd5e | 159 | endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s)); |
zdshelby | 7:d2c5894dcd5e | 160 | } |
zdshelby | 7:d2c5894dcd5e | 161 | if (endpoint_structure) |
zdshelby | 7:d2c5894dcd5e | 162 | { |
zdshelby | 7:d2c5894dcd5e | 163 | memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s)); |
zdshelby | 7:d2c5894dcd5e | 164 | endpoint_structure->endpoint_name_ptr = name; |
zdshelby | 7:d2c5894dcd5e | 165 | endpoint_structure->endpoint_name_len = strlen((char*)name); |
zdshelby | 7:d2c5894dcd5e | 166 | endpoint_structure->type_ptr = typename_ptr; |
zdshelby | 7:d2c5894dcd5e | 167 | endpoint_structure->type_len = strlen((char*)typename_ptr); |
zdshelby | 7:d2c5894dcd5e | 168 | endpoint_structure->lifetime_ptr = lifetime_ptr; |
zdshelby | 7:d2c5894dcd5e | 169 | endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr); |
zdshelby | 7:d2c5894dcd5e | 170 | } |
zdshelby | 7:d2c5894dcd5e | 171 | return endpoint_structure; |
zdshelby | 7:d2c5894dcd5e | 172 | } |
zdshelby | 7:d2c5894dcd5e | 173 | |
zdshelby | 7:d2c5894dcd5e | 174 | void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) |
zdshelby | 7:d2c5894dcd5e | 175 | { |
zdshelby | 7:d2c5894dcd5e | 176 | if (*endpoint_structure) |
zdshelby | 7:d2c5894dcd5e | 177 | { |
zdshelby | 7:d2c5894dcd5e | 178 | nsdl_free(*endpoint_structure); |
zdshelby | 7:d2c5894dcd5e | 179 | *endpoint_structure = NULL; |
zdshelby | 7:d2c5894dcd5e | 180 | } |
zdshelby | 7:d2c5894dcd5e | 181 | } |
zdshelby | 7:d2c5894dcd5e | 182 | |
zdshelby | 5:6adec9967f93 | 183 | void nsdl_init() |
zdshelby | 5:6adec9967f93 | 184 | { |
zdshelby | 7:d2c5894dcd5e | 185 | uint8_t nsp_addr[4]; |
zdshelby | 5:6adec9967f93 | 186 | sn_nsdl_mem_s memory_cbs; |
zdshelby | 5:6adec9967f93 | 187 | memory_cbs.sn_nsdl_alloc = &nsdl_alloc; |
zdshelby | 5:6adec9967f93 | 188 | memory_cbs.sn_nsdl_free = &nsdl_free; |
zdshelby | 5:6adec9967f93 | 189 | if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) { |
zdshelby | 5:6adec9967f93 | 190 | DEBUG("libNsdl init failed"); |
zdshelby | 5:6adec9967f93 | 191 | } else { |
zdshelby | 5:6adec9967f93 | 192 | DEBUG("libNsdl init done"); |
zdshelby | 5:6adec9967f93 | 193 | } |
zdshelby | 7:d2c5894dcd5e | 194 | /* Set nsp address for library */ |
zdshelby | 7:d2c5894dcd5e | 195 | set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4); |
zdshelby | 7:d2c5894dcd5e | 196 | } |
zdshelby | 7:d2c5894dcd5e | 197 | |
zdshelby | 7:d2c5894dcd5e | 198 | static int create_resources() |
zdshelby | 7:d2c5894dcd5e | 199 | { |
zdshelby | 7:d2c5894dcd5e | 200 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
zdshelby | 7:d2c5894dcd5e | 201 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
zdshelby | 7:d2c5894dcd5e | 202 | |
zdshelby | 7:d2c5894dcd5e | 203 | DEBUG("Creating resources"); |
zdshelby | 7:d2c5894dcd5e | 204 | |
zdshelby | 7:d2c5894dcd5e | 205 | /* Create resources */ |
zdshelby | 7:d2c5894dcd5e | 206 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
zdshelby | 7:d2c5894dcd5e | 207 | if(!resource_ptr) |
zdshelby | 7:d2c5894dcd5e | 208 | return 0; |
zdshelby | 7:d2c5894dcd5e | 209 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
zdshelby | 7:d2c5894dcd5e | 210 | |
zdshelby | 7:d2c5894dcd5e | 211 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
zdshelby | 7:d2c5894dcd5e | 212 | if(!resource_ptr->resource_parameters_ptr) |
zdshelby | 7:d2c5894dcd5e | 213 | { |
zdshelby | 7:d2c5894dcd5e | 214 | nsdl_free(resource_ptr); |
zdshelby | 7:d2c5894dcd5e | 215 | return 0; |
zdshelby | 7:d2c5894dcd5e | 216 | } |
zdshelby | 7:d2c5894dcd5e | 217 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
zdshelby | 7:d2c5894dcd5e | 218 | |
zdshelby | 7:d2c5894dcd5e | 219 | // Static resources |
zdshelby | 7:d2c5894dcd5e | 220 | 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 | 221 | 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 | 222 | 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 | 223 | |
zdshelby | 7:d2c5894dcd5e | 224 | // Dynamic resources |
zdshelby | 7:d2c5894dcd5e | 225 | // create_light_resource(resource_ptr); |
zdshelby | 7:d2c5894dcd5e | 226 | // create_gps_resource(resource_ptr); |
zdshelby | 7:d2c5894dcd5e | 227 | |
zdshelby | 7:d2c5894dcd5e | 228 | /* Register with NSP */ |
zdshelby | 7:d2c5894dcd5e | 229 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
zdshelby | 7:d2c5894dcd5e | 230 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) { |
zdshelby | 7:d2c5894dcd5e | 231 | DEBUG("NSP registering failed\r\n"); |
zdshelby | 7:d2c5894dcd5e | 232 | } else { |
zdshelby | 7:d2c5894dcd5e | 233 | DEBUG("NSP registering OK\r\n"); |
zdshelby | 7:d2c5894dcd5e | 234 | } |
zdshelby | 7:d2c5894dcd5e | 235 | nsdl_clean_register_endpoint(&endpoint_ptr); |
zdshelby | 7:d2c5894dcd5e | 236 | |
zdshelby | 7:d2c5894dcd5e | 237 | nsdl_free(resource_ptr->resource_parameters_ptr); |
zdshelby | 7:d2c5894dcd5e | 238 | nsdl_free(resource_ptr); |
zdshelby | 7:d2c5894dcd5e | 239 | return 1; |
zdshelby | 7:d2c5894dcd5e | 240 | } |
zdshelby | 7:d2c5894dcd5e | 241 | |
zdshelby | 7:d2c5894dcd5e | 242 | void nsp_register() |
zdshelby | 7:d2c5894dcd5e | 243 | { |
zdshelby | 7:d2c5894dcd5e | 244 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
zdshelby | 7:d2c5894dcd5e | 245 | |
zdshelby | 7:d2c5894dcd5e | 246 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
zdshelby | 7:d2c5894dcd5e | 247 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) { |
zdshelby | 7:d2c5894dcd5e | 248 | DEBUG("NSP re-registration failed\r\n"); |
zdshelby | 7:d2c5894dcd5e | 249 | } else { |
zdshelby | 7:d2c5894dcd5e | 250 | DEBUG("NSP re-registration OK\r\n"); |
zdshelby | 7:d2c5894dcd5e | 251 | } |
zdshelby | 7:d2c5894dcd5e | 252 | nsdl_clean_register_endpoint(&endpoint_ptr); |
zdshelby | 5:6adec9967f93 | 253 | } |
zdshelby | 5:6adec9967f93 | 254 | |
zdshelby | 3:1e981a0aebfb | 255 | void socket_event_loop() |
zdshelby | 3:1e981a0aebfb | 256 | { |
zdshelby | 7:d2c5894dcd5e | 257 | sn_nsdl_addr_s received_packet_address; |
zdshelby | 7:d2c5894dcd5e | 258 | uint8_t received_address[4]; |
zdshelby | 3:1e981a0aebfb | 259 | char buffer[2048]; |
zdshelby | 3:1e981a0aebfb | 260 | |
zdshelby | 7:d2c5894dcd5e | 261 | memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); |
zdshelby | 7:d2c5894dcd5e | 262 | received_packet_address.addr_ptr = received_address; |
zdshelby | 3:1e981a0aebfb | 263 | |
zdshelby | 4:5bfd59673a99 | 264 | DEBUG("Starting socket read loop..."); |
zdshelby | 3:1e981a0aebfb | 265 | while(1) |
zdshelby | 3:1e981a0aebfb | 266 | { |
zdshelby | 3:1e981a0aebfb | 267 | int n = cellular->read(buffer, sizeof(buffer), -1); |
zdshelby | 3:1e981a0aebfb | 268 | if (n < 0) |
zdshelby | 3:1e981a0aebfb | 269 | { |
zdshelby | 3:1e981a0aebfb | 270 | DEBUG("Socket error\n\r"); |
zdshelby | 3:1e981a0aebfb | 271 | } |
zdshelby | 3:1e981a0aebfb | 272 | else |
zdshelby | 3:1e981a0aebfb | 273 | { |
zdshelby | 3:1e981a0aebfb | 274 | DEBUG("Received %d bytes\r\n", n); |
zdshelby | 7:d2c5894dcd5e | 275 | sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address); |
zdshelby | 3:1e981a0aebfb | 276 | } |
zdshelby | 3:1e981a0aebfb | 277 | } |
zdshelby | 3:1e981a0aebfb | 278 | } |
zdshelby | 0:f739ace74102 | 279 | |
zdshelby | 0:f739ace74102 | 280 | int main() |
zdshelby | 0:f739ace74102 | 281 | { |
zdshelby | 1:5147d3fa7816 | 282 | printf("\r\n*****************************************************************************\r\n"); |
zdshelby | 0:f739ace74102 | 283 | DEBUG("NanoService Example for KL46Z + Multitech Cellular"); |
zdshelby | 0:f739ace74102 | 284 | |
zdshelby | 1:5147d3fa7816 | 285 | // Inititalize the Cellular modem |
zdshelby | 1:5147d3fa7816 | 286 | cellular_init(); |
zdshelby | 1:5147d3fa7816 | 287 | |
zdshelby | 1:5147d3fa7816 | 288 | // Bind the socket and configure NSP settings |
zdshelby | 1:5147d3fa7816 | 289 | nsp_init(); |
zdshelby | 0:f739ace74102 | 290 | |
zdshelby | 4:5bfd59673a99 | 291 | // Initalize NanoService library |
zdshelby | 5:6adec9967f93 | 292 | nsdl_init(); |
zdshelby | 7:d2c5894dcd5e | 293 | |
zdshelby | 7:d2c5894dcd5e | 294 | // Create resources |
zdshelby | 7:d2c5894dcd5e | 295 | create_resources(); |
zdshelby | 5:6adec9967f93 | 296 | |
zdshelby | 3:1e981a0aebfb | 297 | // Start socket listening loop |
zdshelby | 3:1e981a0aebfb | 298 | socket_event_loop(); |
zdshelby | 5:6adec9967f93 | 299 | |
zdshelby | 0:f739ace74102 | 300 | } |