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
00001 #include "mbed.h" 00002 #include "config.h" 00003 #include "debug.h" 00004 00005 // Multitech Cellular includes 00006 #include "Cellular.h" 00007 #include "Endpoint.h" 00008 #include "IPStack.h" 00009 #include "MTSSerialFlowControl.h" 00010 00011 // NanoService includes 00012 #include "sn_nsdl.h" 00013 #include "sn_coap_header.h" 00014 #include "sn_coap_protocol.h" 00015 #include "sn_nsdl_lib.h" 00016 #include "sn_grs.h" 00017 #include <stdint.h> 00018 00019 using namespace mts; 00020 00021 Cellular* cellular; 00022 Endpoint nsp; 00023 static const char* NSP_ADDRESS = "176.58.89.233"; // Motivation demo server 00024 static const int NSP_PORT = 5683; 00025 char endpoint_name[] = {"mbed-cellular-mts1"}; 00026 uint8_t ep_type[] = {"mbed_kl64z_multitech"}; 00027 uint8_t lifetime_ptr[] = {"86400"}; 00028 static const char* FIRMWARE_VER = "13"; // Committed revision number 00029 00030 typedef uint8_t (*sn_grs_dyn_res_callback_t)(sn_coap_hdr_s *, sn_nsdl_addr_s *, sn_proto_info_s *); 00031 00032 // **************************************************************************** 00033 // Cellular initialization 00034 00035 static void cellular_init() 00036 { 00037 //Setup serial interface to radio 00038 MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); 00039 serial->baud(115200); 00040 00041 //Setup Cellular class 00042 cellular = Cellular::getInstance(); 00043 cellular->init(serial, PTA4, PTC9); //DCD and DTR pins for KL46Z 00044 00045 //Run status and configuration commands 00046 DEBUG("\n\r////Start Status and Configuration Commands////"); 00047 DEBUG("Command Test: %s", getCodeNames(cellular->test()).c_str()); //Make sure you can talk to the radio 00048 DEBUG("Signal Strength: %d", cellular->getSignalStrength()); //Check the signal strength should be above 8 00049 00050 //Makes sure you are reistered with cell 00051 DEBUG("Registration State: %s", Cellular::getRegistrationNames(cellular->getRegistration()).c_str()); 00052 00053 //Shows example of how to send other commands, look at AT command guide for more info 00054 DEBUG("Send Basic Command (AT): %s", getCodeNames(cellular->sendBasicCommand("AT", 1000)).c_str()); 00055 DEBUG("Send Command (AT+CSQ): %s", cellular->sendCommand("AT+CSQ", 1000).c_str()); 00056 00057 //Start Test 00058 DEBUG("\n\r////Start Network Connectivity Test////"); 00059 DEBUG("Set APN: %s", getCodeNames(cellular->setApn(CELLULAR_APN)).c_str()); //Use APN from service provider!!! 00060 00061 //Setup a data connection 00062 DEBUG("Attempting to Connect, this may take some time..."); 00063 while (!cellular->connect()) { 00064 WARNING("Failed to connect... Trying again."); 00065 wait(1); 00066 } 00067 DEBUG("Connected to the Network!"); 00068 00069 //Try pinging default server "8.8.8.8" (Google's DNS) 00070 DEBUG("Ping Valid: %s", cellular->ping() ? "true" : "false"); 00071 wait(3); 00072 00073 } 00074 00075 // **************************************************************************** 00076 // NSP initialization 00077 00078 static void nsp_connect() 00079 { 00080 nsp.set_address(NSP_ADDRESS, NSP_PORT); 00081 00082 DEBUG("EP Name: %s", endpoint_name); 00083 DEBUG("NSP Location: coap://%s:%d\n", NSP_ADDRESS, NSP_PORT); 00084 00085 // Bind the port 00086 cellular->bind(EP_PORT); 00087 00088 // Open a TCP connection 00089 while (!cellular->open(NSP_ADDRESS, NSP_PORT, mts::IPStack::TCP)) 00090 { 00091 WARNING("TCP connection failed."); 00092 wait(3); 00093 } 00094 DEBUG("TCP connection to NSP successful."); 00095 } 00096 00097 extern "C" void *nsdl_alloc(uint16_t size) 00098 { 00099 return malloc(size); 00100 } 00101 00102 extern "C" void nsdl_free(void* ptr_to_free) 00103 { 00104 free(ptr_to_free); 00105 } 00106 00107 static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr) 00108 { 00109 int buffer_len = data_len+2; 00110 DEBUG("TX callback! Sending %d bytes", buffer_len); 00111 char buffer[buffer_len]; 00112 buffer[0] = data_len >> 8; 00113 buffer[1] = data_len & 0xFF; 00114 memcpy(buffer+2, data_ptr, data_len); 00115 00116 if(cellular->write((char*)buffer, (int)buffer_len, 1000) != buffer_len) 00117 WARNING("Sending failed"); 00118 00119 return 1; 00120 } 00121 00122 static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr) 00123 { 00124 DEBUG("RX callback!"); 00125 return 0; 00126 } 00127 00128 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) 00129 { 00130 resource_structure->access = SN_GRS_GET_ALLOWED; 00131 resource_structure->mode = SN_GRS_STATIC; 00132 resource_structure->pathlen = pt_len; 00133 resource_structure->path = pt; 00134 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; 00135 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; 00136 resource_structure->resource = rsc; 00137 resource_structure->resourcelen = rsc_len; 00138 sn_nsdl_create_resource(resource_structure); 00139 } 00140 00141 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) 00142 { 00143 resource_structure->access = (sn_grs_resource_acl_e)access_right; 00144 resource_structure->resource = 0; 00145 resource_structure->resourcelen = 0; 00146 resource_structure->sn_grs_dyn_res_callback = callback_ptr; 00147 resource_structure->mode = SN_GRS_DYNAMIC; 00148 resource_structure->pathlen = pt_len; 00149 resource_structure->path = pt; 00150 resource_structure->resource_parameters_ptr->resource_type_len = rpp_len; 00151 resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr; 00152 resource_structure->resource_parameters_ptr->observable = is_observable; 00153 sn_nsdl_create_resource(resource_structure); 00154 } 00155 00156 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) 00157 { 00158 if (NULL == endpoint_structure) 00159 { 00160 endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s)); 00161 } 00162 if (endpoint_structure) 00163 { 00164 memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s)); 00165 endpoint_structure->endpoint_name_ptr = name; 00166 endpoint_structure->endpoint_name_len = strlen((char*)name); 00167 endpoint_structure->type_ptr = typename_ptr; 00168 endpoint_structure->type_len = strlen((char*)typename_ptr); 00169 endpoint_structure->lifetime_ptr = lifetime_ptr; 00170 endpoint_structure->lifetime_len = strlen((char*)lifetime_ptr); 00171 } 00172 return endpoint_structure; 00173 } 00174 00175 void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure) 00176 { 00177 if (*endpoint_structure) 00178 { 00179 nsdl_free(*endpoint_structure); 00180 *endpoint_structure = NULL; 00181 } 00182 } 00183 00184 void nsdl_init() 00185 { 00186 uint8_t nsp_addr[4]; 00187 sn_nsdl_mem_s memory_cbs; 00188 memory_cbs.sn_nsdl_alloc = &nsdl_alloc; 00189 memory_cbs.sn_nsdl_free = &nsdl_free; 00190 if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) { 00191 ERROR("libNsdl init failed"); 00192 } else { 00193 DEBUG("libNsdl init done"); 00194 } 00195 /* Set nsp address for library */ 00196 set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4); 00197 } 00198 00199 static int create_resources() 00200 { 00201 sn_nsdl_resource_info_s *resource_ptr = NULL; 00202 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; 00203 00204 DEBUG("Creating resources"); 00205 00206 /* Create resources */ 00207 resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); 00208 if(!resource_ptr) 00209 return 0; 00210 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); 00211 00212 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); 00213 if(!resource_ptr->resource_parameters_ptr) 00214 { 00215 nsdl_free(resource_ptr); 00216 return 0; 00217 } 00218 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); 00219 00220 // Static resources 00221 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); 00222 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); 00223 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); 00224 nsdl_create_static_resource(resource_ptr, sizeof("3/0/16")-1, (uint8_t*) "3/0/16", 0, 0, (uint8_t*) "TCP", sizeof("TCP")-1); 00225 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)); 00226 nsdl_create_static_resource(resource_ptr, sizeof("4/0/0")-1, (uint8_t*) "4/0/0", 0, 0, (uint8_t*) "GPRS", sizeof("GPRS")-1); 00227 nsdl_create_static_resource(resource_ptr, sizeof("4/0/1")-1, (uint8_t*) "4/0/1", 0, 0, (uint8_t*) "GPRS", sizeof("GPRS")-1); 00228 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); 00229 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); 00230 // 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); 00231 // Broken: This cause the modem driver to crash when the request is received. 00232 00233 // Dynamic resources 00234 // create_light_resource(resource_ptr); 00235 // create_gps_resource(resource_ptr); 00236 00237 /* Register with NSP */ 00238 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); 00239 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) { 00240 WARNING("NSP registering failed\r\n"); 00241 } else { 00242 DEBUG("NSP registering OK\r\n"); 00243 } 00244 nsdl_clean_register_endpoint(&endpoint_ptr); 00245 00246 nsdl_free(resource_ptr->resource_parameters_ptr); 00247 nsdl_free(resource_ptr); 00248 return 1; 00249 } 00250 00251 void nsp_register() 00252 { 00253 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; 00254 00255 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); 00256 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) { 00257 WARNING("NSP re-registration failed\r\n"); 00258 } else { 00259 DEBUG("NSP re-registration OK\r\n"); 00260 } 00261 nsdl_clean_register_endpoint(&endpoint_ptr); 00262 } 00263 00264 void socket_event_loop() 00265 { 00266 sn_nsdl_addr_s received_packet_address; 00267 uint8_t received_address[4]; 00268 char buffer[2048]; 00269 int n; 00270 00271 memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); 00272 received_packet_address.addr_ptr = received_address; 00273 00274 DEBUG("Starting socket read loop..."); 00275 while(1) 00276 { 00277 n = cellular->read(buffer, sizeof(buffer), 1000); 00278 DEBUG("Received %d bytes", n); 00279 if (n < 0) 00280 { 00281 ERROR("Socket error"); 00282 } 00283 else 00284 { 00285 uint16_t len = 0; 00286 if (n > 2) { 00287 len = 256 * buffer[0] + buffer[1]; 00288 DEBUG("CoAP length header = %d bytes", len); 00289 sn_nsdl_process_coap((uint8_t*)buffer+2, len, &received_packet_address); 00290 } 00291 } 00292 } 00293 } 00294 00295 int main() 00296 { 00297 printf("\r\n*****************************************************************************\r\n"); 00298 DEBUG("NanoService Example for KL46Z + Multitech Cellular"); 00299 00300 // Inititalize the Cellular modem 00301 cellular_init(); 00302 00303 // Bind the socket and configure NSP settings 00304 nsp_connect(); 00305 00306 // Initalize NanoService library 00307 nsdl_init(); 00308 00309 // Create resources & register with NSP 00310 create_resources(); 00311 00312 // Start socket listening loop 00313 socket_event_loop(); 00314 00315 }
Generated on Fri Jul 22 2022 11:30:09 by
1.7.2