I added functionality to get the RSSI, BER, and Cell Neighbor for reporting connection issues to M2X
Dependencies: WncControllerK64F
Diff: WNCInterface.cpp
- Revision:
- 0:55ec71dc0347
- Child:
- 3:1d7e6ed11269
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WNCInterface.cpp Wed Sep 21 14:30:06 2016 +0000 @@ -0,0 +1,186 @@ +/* ===================================================================== + Copyright © 2016, Avnet (R) + + Contributors: + * James M Flynn, www.em.avnet.com + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + either express or implied. See the License for the specific + language governing permissions and limitations under the License. + + @file WNCInterface.cpp + @version 1.0 + @date Sept 2016 + @author James Flynn + +======================================================================== */ + + +#ifndef __MODULE__ +#define __MODULE__ "WNCInterface.cpp" +#endif + +#include "WNCInterface.h" + +///////////////////////////////////////////////////// +// NXP GPIO Pins that are used to initialize the WNC Shield +///////////////////////////////////////////////////// +DigitalOut mdm_uart2_rx_boot_mode_sel(PTC17); // on powerup, 0 = boot mode, 1 = normal boot +DigitalOut mdm_power_on(PTB9); // 0 = turn modem on, 1 = turn modem off (should be held high for >5 seconds to cycle modem) +DigitalOut mdm_wakeup_in(PTC2); // 0 = let modem sleep, 1 = keep modem awake -- Note: pulled high on shield +DigitalOut mdm_reset(PTC12); // active high +DigitalOut shield_3v3_1v8_sig_trans_ena(PTC4); // 0 = disabled (all signals high impedence, 1 = translation active +DigitalOut mdm_uart1_cts(PTD0); + +char * _fatal_err_loc; //GLOBAL::holds any error location info + +using namespace WncControllerK64F_fk; // namespace for the controller class use + +// Define pin associations for the controller class to use be careful to +// keep the order of the pins in the initialization list. +WncGpioPinListK64F wncPinList = { + &mdm_uart2_rx_boot_mode_sel, + &mdm_power_on, + &mdm_wakeup_in, + &mdm_reset, + &shield_3v3_1v8_sig_trans_ena, + &mdm_uart1_cts +}; + +static MODSERIAL mdmUart(PTD3,PTD2,256,4096); //UART for WNC Module + +#if WNC_DEBUG == 1 +static MODSERIAL debugUart(USBTX,USBRX,256,256); //UART used for stdio/stderr +static WncControllerK64F wnc(&wncPinList, &mdmUart, &debugUart); +#else +static WncControllerK64F wnc(&wncPinList, &mdmUart, NULL); +#endif + +WncControllerK64F *WNCInterface::_pwnc = &wnc; +WncIpStats WNCInterface::myNetStats; +string WNCInterface::mac; + +WNCInterface::WNCInterface() { +#if WNC_DEBUG == 1 + debugUart.baud(115200); // set stdio/stderr. use 115.2K since the WNC part uses this--keep up. +#endif +} + +void WNCInterface::doDebug( int v ) { +#if WNC_DEBUG == 1 + //basic debug = 0x01 + //more debug = 0x02 + //all debug = 0x03 + _pwnc->enableDebug( (v&1), (v&2) ); +#endif +} + +// +// Power-up the WNC module. The caller can optionally configure. +// Inputs: +// apn - Caller can specify an APN. If none is provided will use "m2m.com.attz" +// debug- specify the amount of debug the WNC controller should output: +// 1 - Basic Debug output +// 2 - Verbose Debug output +// 3 - Full Debug output +// Returns: 0 if unable to initialize the WNC module +// -1 if successfully initialized +// +int WNCInterface::init(const char* apn, int debug) { + int ret = 0; + _pwnc = &wnc; //set the pointer to the WNC controller class. Used for all WNC accesses + + if( apn==NULL ) + apn = APN_DEFAULT; + +#if WNC_DEBUG == 1 + if( debug ) + _pwnc->enableDebug( (debug&1), (debug&2) ); +#else + debug=debug; //keep the compiler from complaining. +#endif + + ret = ( _pwnc->powerWncOn(apn,40) )? 2:0; + ret |= ( _pwnc->setApnName(apn) )? 1:0; + ret |= ( _pwnc->getWncNetworkingStats(&myNetStats) )? 4:0; + + return ret; +} + +// +// check to see if we are connected to the internet or not. The +// connection is supposed to happen during init. If we are +// connected to the internet return 0 otherwise return -1 +// +int WNCInterface::connect(void) { + return ( _pwnc->getWncStatus() == WNC_GOOD )? 0 : -1; +} + +// +// ok, the user wants to disconnect. At present, this isn't possible +// with the WNC, so just fake it and say we did... +// +int WNCInterface::disconnect() { + return 0; +} + +// +// update the networking stats and return the IP Address +// +char * WNCInterface::getIPAddress() { + if ( _pwnc->getWncNetworkingStats(&myNetStats) ) { + if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ) + FATAL_WNC_ERROR(null); + return &myNetStats.ip[0]; + } + return NULL; +} + +// +// update the networking stats and return the Gateway Address +// +char * WNCInterface::getGateway() { + if ( _pwnc->getWncNetworkingStats(&myNetStats) ) { + if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ) + FATAL_WNC_ERROR(null); + return &WNCInterface::myNetStats.gateway[0]; + } + return NULL; +} + +// +// update the networking stats and return the Network Mask +// +char * WNCInterface::getNetworkMask() { + if ( _pwnc->getWncNetworkingStats(&myNetStats) ) { + if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ) + FATAL_WNC_ERROR(null); + return &WNCInterface::myNetStats.mask[0]; + } + return NULL; +} + +// +// return a pesudo-MAC address created from the ICCID +// +char* WNCInterface::getMACAddress( void ) { + string str; + + if( _pwnc->getICCID(&str) ) { + if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ) + FATAL_WNC_ERROR(null); + mac = str.substr(3,20); + mac[2]=mac[5]=mac[8]=mac[11]=mac[14]=':'; + return (char*)mac.c_str(); + } + return NULL; +} +