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
- Committer:
- zdshelby
- Date:
- 2014-02-18
- Revision:
- 5:6adec9967f93
- Parent:
- 4:5bfd59673a99
- Child:
- 7:d2c5894dcd5e
File content as of revision 5:6adec9967f93:
#include "mbed.h"
#include "config.h"
#include "debug.h"
// Multitech Cellular includes
#include "Cellular.h"
#include "Endpoint.h"
#include "IPStack.h"
#include "MTSSerialFlowControl.h"
// NanoService includes
#include "sn_nsdl.h"
#include "sn_coap_header.h"
#include "sn_coap_protocol.h"
#include "sn_nsdl_lib.h"
#include <stdint.h>
using namespace mts;
Cellular* cellular;
Endpoint nsp;
// ****************************************************************************
// Cellular initialization
static void cellular_init()
{
//Setup serial interface to radio
MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8);
serial->baud(115200);
//Setup Cellular class
cellular = Cellular::getInstance();
cellular->init(serial, PTA4, PTC9); //DCD and DTR pins for KL46Z
//Run status and configuration commands
DEBUG("\n\r////Start Status and Configuration Commands////");
DEBUG("Command Test: %s", getCodeNames(cellular->test()).c_str()); //Make sure you can talk to the radio
DEBUG("Signal Strength: %d", cellular->getSignalStrength()); //Check the signal strength should be above 8
//Makes sure you are reistered with cell
DEBUG("Registration State: %s", Cellular::getRegistrationNames(cellular->getRegistration()).c_str());
//Shows example of how to send other commands, look at AT command guide for more info
DEBUG("Send Basic Command (AT): %s", getCodeNames(cellular->sendBasicCommand("AT", 1000)).c_str());
DEBUG("Send Command (AT+CSQ): %s", cellular->sendCommand("AT+CSQ", 1000).c_str());
//Start Test
DEBUG("\n\r////Start Network Connectivity Test////");
DEBUG("Set APN: %s", getCodeNames(cellular->setApn(CELLULAR_APN)).c_str()); //Use APN from service provider!!!
//Setup a data connection
DEBUG("Attempting to Connect, this may take some time...");
while (!cellular->connect()) {
DEBUG("Failed to connect... Trying again.");
wait(1);
}
DEBUG("Connected to the Network!");
//Try pinging default server "8.8.8.8" (Google's DNS)
DEBUG("Ping Valid: %s", cellular->ping() ? "true" : "false");
wait(3);
}
// ****************************************************************************
// NSP initialization
static void nsp_init()
{
nsp.set_address(NSP_ADDRESS, NSP_PORT);
// DEBUG("name: %s", endpoint_name);
DEBUG("NSP Location: coap://%s:%d\n", NSP_ADDRESS, NSP_PORT);
// Bind the port
cellular->bind(EP_PORT);
// Open a TCP connection
while (!cellular->open(NSP_ADDRESS, NSP_PORT, mts::IPStack::TCP))
{
DEBUG("TCP connection failed.");
wait(3);
}
DEBUG("TCP connection to NSP successful.");
}
extern "C" void *nsdl_alloc(uint16_t size)
{
return malloc(size);
}
extern "C" void nsdl_free(void* ptr_to_free)
{
free(ptr_to_free);
}
static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr)
{
DEBUG("TX callback!\n\rSending %d bytes", data_len);
if(cellular->write((char*)data_ptr, data_len, -1) != data_len)
DEBUG("sending failed");
return 1;
}
static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
{
DEBUG("RX callback!");
return 0;
}
void nsdl_init()
{
sn_nsdl_mem_s memory_cbs;
memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
memory_cbs.sn_nsdl_free = &nsdl_free;
if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1) {
DEBUG("libNsdl init failed");
} else {
DEBUG("libNsdl init done");
}
}
void socket_event_loop()
{
//sn_nsdl_addr_s received_packet_address;
//uint8_t received_address[4];
char buffer[2048];
//memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
//received_packet_address.addr_ptr = received_address;
DEBUG("Starting socket read loop...");
while(1)
{
int n = cellular->read(buffer, sizeof(buffer), -1);
if (n < 0)
{
DEBUG("Socket error\n\r");
}
else
{
DEBUG("Received %d bytes\r\n", n);
//sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address);
}
}
}
int main()
{
printf("\r\n*****************************************************************************\r\n");
DEBUG("NanoService Example for KL46Z + Multitech Cellular");
// Inititalize the Cellular modem
cellular_init();
// Bind the socket and configure NSP settings
nsp_init();
// Initalize NanoService library
nsdl_init();
// Start socket listening loop
socket_event_loop();
}
