Multitech + KL46Z NanoService example with the RTOS

Dependencies:   mbed SocketModem nanoservice_client_1_12

Committer:
zdshelby
Date:
Tue Feb 18 01:10:21 2014 +0000
Revision:
5:6adec9967f93
Parent:
4:5bfd59673a99
Child:
7:d2c5894dcd5e
- Added NSDL initalization

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 5:6adec9967f93 16 #include <stdint.h>
zdshelby 3:1e981a0aebfb 17
zdshelby 1:5147d3fa7816 18 using namespace mts;
zdshelby 1:5147d3fa7816 19
zdshelby 1:5147d3fa7816 20 Cellular* cellular;
zdshelby 2:1592223f12e1 21 Endpoint nsp;
zdshelby 1:5147d3fa7816 22
zdshelby 1:5147d3fa7816 23 // ****************************************************************************
zdshelby 1:5147d3fa7816 24 // Cellular initialization
zdshelby 1:5147d3fa7816 25
zdshelby 1:5147d3fa7816 26 static void cellular_init()
zdshelby 1:5147d3fa7816 27 {
zdshelby 1:5147d3fa7816 28 //Setup serial interface to radio
zdshelby 1:5147d3fa7816 29 MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8);
zdshelby 1:5147d3fa7816 30 serial->baud(115200);
zdshelby 1:5147d3fa7816 31
zdshelby 1:5147d3fa7816 32 //Setup Cellular class
zdshelby 1:5147d3fa7816 33 cellular = Cellular::getInstance();
zdshelby 1:5147d3fa7816 34 cellular->init(serial, PTA4, PTC9); //DCD and DTR pins for KL46Z
zdshelby 1:5147d3fa7816 35
zdshelby 1:5147d3fa7816 36 //Run status and configuration commands
zdshelby 1:5147d3fa7816 37 DEBUG("\n\r////Start Status and Configuration Commands////");
zdshelby 1:5147d3fa7816 38 DEBUG("Command Test: %s", getCodeNames(cellular->test()).c_str()); //Make sure you can talk to the radio
zdshelby 1:5147d3fa7816 39 DEBUG("Signal Strength: %d", cellular->getSignalStrength()); //Check the signal strength should be above 8
zdshelby 1:5147d3fa7816 40
zdshelby 1:5147d3fa7816 41 //Makes sure you are reistered with cell
zdshelby 1:5147d3fa7816 42 DEBUG("Registration State: %s", Cellular::getRegistrationNames(cellular->getRegistration()).c_str());
zdshelby 1:5147d3fa7816 43
zdshelby 1:5147d3fa7816 44 //Shows example of how to send other commands, look at AT command guide for more info
zdshelby 1:5147d3fa7816 45 DEBUG("Send Basic Command (AT): %s", getCodeNames(cellular->sendBasicCommand("AT", 1000)).c_str());
zdshelby 1:5147d3fa7816 46 DEBUG("Send Command (AT+CSQ): %s", cellular->sendCommand("AT+CSQ", 1000).c_str());
zdshelby 1:5147d3fa7816 47
zdshelby 1:5147d3fa7816 48 //Start Test
zdshelby 1:5147d3fa7816 49 DEBUG("\n\r////Start Network Connectivity Test////");
zdshelby 1:5147d3fa7816 50 DEBUG("Set APN: %s", getCodeNames(cellular->setApn(CELLULAR_APN)).c_str()); //Use APN from service provider!!!
zdshelby 1:5147d3fa7816 51
zdshelby 1:5147d3fa7816 52 //Setup a data connection
zdshelby 1:5147d3fa7816 53 DEBUG("Attempting to Connect, this may take some time...");
zdshelby 1:5147d3fa7816 54 while (!cellular->connect()) {
zdshelby 1:5147d3fa7816 55 DEBUG("Failed to connect... Trying again.");
zdshelby 1:5147d3fa7816 56 wait(1);
zdshelby 1:5147d3fa7816 57 }
zdshelby 1:5147d3fa7816 58 DEBUG("Connected to the Network!");
zdshelby 1:5147d3fa7816 59
zdshelby 1:5147d3fa7816 60 //Try pinging default server "8.8.8.8" (Google's DNS)
zdshelby 1:5147d3fa7816 61 DEBUG("Ping Valid: %s", cellular->ping() ? "true" : "false");
zdshelby 1:5147d3fa7816 62 wait(3);
zdshelby 1:5147d3fa7816 63
zdshelby 1:5147d3fa7816 64 }
zdshelby 1:5147d3fa7816 65
zdshelby 1:5147d3fa7816 66 // ****************************************************************************
zdshelby 1:5147d3fa7816 67 // NSP initialization
zdshelby 1:5147d3fa7816 68
zdshelby 1:5147d3fa7816 69 static void nsp_init()
zdshelby 1:5147d3fa7816 70 {
zdshelby 2:1592223f12e1 71 nsp.set_address(NSP_ADDRESS, NSP_PORT);
zdshelby 2:1592223f12e1 72
zdshelby 2:1592223f12e1 73 // DEBUG("name: %s", endpoint_name);
zdshelby 2:1592223f12e1 74 DEBUG("NSP Location: coap://%s:%d\n", NSP_ADDRESS, NSP_PORT);
zdshelby 2:1592223f12e1 75
zdshelby 1:5147d3fa7816 76 // Bind the port
zdshelby 1:5147d3fa7816 77 cellular->bind(EP_PORT);
zdshelby 1:5147d3fa7816 78
zdshelby 1:5147d3fa7816 79 // Open a TCP connection
zdshelby 1:5147d3fa7816 80 while (!cellular->open(NSP_ADDRESS, NSP_PORT, mts::IPStack::TCP))
zdshelby 1:5147d3fa7816 81 {
zdshelby 1:5147d3fa7816 82 DEBUG("TCP connection failed.");
zdshelby 1:5147d3fa7816 83 wait(3);
zdshelby 1:5147d3fa7816 84 }
zdshelby 1:5147d3fa7816 85 DEBUG("TCP connection to NSP successful.");
zdshelby 1:5147d3fa7816 86 }
zdshelby 1:5147d3fa7816 87
zdshelby 4:5bfd59673a99 88 extern "C" void *nsdl_alloc(uint16_t size)
zdshelby 4:5bfd59673a99 89 {
zdshelby 4:5bfd59673a99 90 return malloc(size);
zdshelby 4:5bfd59673a99 91 }
zdshelby 4:5bfd59673a99 92
zdshelby 4:5bfd59673a99 93 extern "C" void nsdl_free(void* ptr_to_free)
zdshelby 4:5bfd59673a99 94 {
zdshelby 4:5bfd59673a99 95 free(ptr_to_free);
zdshelby 4:5bfd59673a99 96 }
zdshelby 4:5bfd59673a99 97
zdshelby 5:6adec9967f93 98 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 99 {
zdshelby 5:6adec9967f93 100 DEBUG("TX callback!\n\rSending %d bytes", data_len);
zdshelby 5:6adec9967f93 101
zdshelby 5:6adec9967f93 102 if(cellular->write((char*)data_ptr, data_len, -1) != data_len)
zdshelby 5:6adec9967f93 103 DEBUG("sending failed");
zdshelby 5:6adec9967f93 104
zdshelby 5:6adec9967f93 105 return 1;
zdshelby 5:6adec9967f93 106 }
zdshelby 5:6adec9967f93 107
zdshelby 5:6adec9967f93 108 static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
zdshelby 5:6adec9967f93 109 {
zdshelby 5:6adec9967f93 110 DEBUG("RX callback!");
zdshelby 5:6adec9967f93 111 return 0;
zdshelby 5:6adec9967f93 112 }
zdshelby 5:6adec9967f93 113
zdshelby 5:6adec9967f93 114 void nsdl_init()
zdshelby 5:6adec9967f93 115 {
zdshelby 5:6adec9967f93 116 sn_nsdl_mem_s memory_cbs;
zdshelby 5:6adec9967f93 117 memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
zdshelby 5:6adec9967f93 118 memory_cbs.sn_nsdl_free = &nsdl_free;
zdshelby 5:6adec9967f93 119 if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) {
zdshelby 5:6adec9967f93 120 DEBUG("libNsdl init failed");
zdshelby 5:6adec9967f93 121 } else {
zdshelby 5:6adec9967f93 122 DEBUG("libNsdl init done");
zdshelby 5:6adec9967f93 123 }
zdshelby 5:6adec9967f93 124 }
zdshelby 5:6adec9967f93 125
zdshelby 3:1e981a0aebfb 126 void socket_event_loop()
zdshelby 3:1e981a0aebfb 127 {
zdshelby 3:1e981a0aebfb 128 //sn_nsdl_addr_s received_packet_address;
zdshelby 3:1e981a0aebfb 129 //uint8_t received_address[4];
zdshelby 3:1e981a0aebfb 130 char buffer[2048];
zdshelby 3:1e981a0aebfb 131
zdshelby 3:1e981a0aebfb 132 //memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
zdshelby 3:1e981a0aebfb 133 //received_packet_address.addr_ptr = received_address;
zdshelby 3:1e981a0aebfb 134
zdshelby 4:5bfd59673a99 135 DEBUG("Starting socket read loop...");
zdshelby 3:1e981a0aebfb 136 while(1)
zdshelby 3:1e981a0aebfb 137 {
zdshelby 3:1e981a0aebfb 138 int n = cellular->read(buffer, sizeof(buffer), -1);
zdshelby 3:1e981a0aebfb 139 if (n < 0)
zdshelby 3:1e981a0aebfb 140 {
zdshelby 3:1e981a0aebfb 141 DEBUG("Socket error\n\r");
zdshelby 3:1e981a0aebfb 142 }
zdshelby 3:1e981a0aebfb 143 else
zdshelby 3:1e981a0aebfb 144 {
zdshelby 3:1e981a0aebfb 145 DEBUG("Received %d bytes\r\n", n);
zdshelby 3:1e981a0aebfb 146 //sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address);
zdshelby 3:1e981a0aebfb 147 }
zdshelby 3:1e981a0aebfb 148 }
zdshelby 3:1e981a0aebfb 149 }
zdshelby 0:f739ace74102 150
zdshelby 0:f739ace74102 151 int main()
zdshelby 0:f739ace74102 152 {
zdshelby 1:5147d3fa7816 153 printf("\r\n*****************************************************************************\r\n");
zdshelby 0:f739ace74102 154 DEBUG("NanoService Example for KL46Z + Multitech Cellular");
zdshelby 0:f739ace74102 155
zdshelby 1:5147d3fa7816 156 // Inititalize the Cellular modem
zdshelby 1:5147d3fa7816 157 cellular_init();
zdshelby 1:5147d3fa7816 158
zdshelby 1:5147d3fa7816 159 // Bind the socket and configure NSP settings
zdshelby 1:5147d3fa7816 160 nsp_init();
zdshelby 0:f739ace74102 161
zdshelby 4:5bfd59673a99 162 // Initalize NanoService library
zdshelby 5:6adec9967f93 163 nsdl_init();
zdshelby 5:6adec9967f93 164
zdshelby 3:1e981a0aebfb 165 // Start socket listening loop
zdshelby 3:1e981a0aebfb 166 socket_event_loop();
zdshelby 5:6adec9967f93 167
zdshelby 0:f739ace74102 168 }