publish final code

Dependencies:   WncControllerK64F

Fork of WNCInterface by Avnet

Committer:
JMF
Date:
Mon Oct 31 13:54:21 2016 +0000
Revision:
11:75cf1e1c921c
Parent:
9:9f0578ff157a
Added changes to slow down polling of the WNC data module.  Frequent polling appears to cause problems for WNC.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:55ec71dc0347 1 /* =====================================================================
JMF 0:55ec71dc0347 2 Copyright © 2016, Avnet (R)
JMF 0:55ec71dc0347 3
JMF 0:55ec71dc0347 4 Contributors:
JMF 0:55ec71dc0347 5 * James M Flynn, www.em.avnet.com
JMF 0:55ec71dc0347 6
JMF 0:55ec71dc0347 7 Licensed under the Apache License, Version 2.0 (the "License");
JMF 0:55ec71dc0347 8 you may not use this file except in compliance with the License.
JMF 0:55ec71dc0347 9 You may obtain a copy of the License at
JMF 0:55ec71dc0347 10
JMF 0:55ec71dc0347 11 http://www.apache.org/licenses/LICENSE-2.0
JMF 0:55ec71dc0347 12
JMF 0:55ec71dc0347 13 Unless required by applicable law or agreed to in writing,
JMF 0:55ec71dc0347 14 software distributed under the License is distributed on an
JMF 0:55ec71dc0347 15 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
JMF 0:55ec71dc0347 16 either express or implied. See the License for the specific
JMF 0:55ec71dc0347 17 language governing permissions and limitations under the License.
JMF 0:55ec71dc0347 18
JMF 0:55ec71dc0347 19 @file WNCInterface.cpp
JMF 0:55ec71dc0347 20 @version 1.0
JMF 0:55ec71dc0347 21 @date Sept 2016
JMF 0:55ec71dc0347 22 @author James Flynn
JMF 0:55ec71dc0347 23
JMF 0:55ec71dc0347 24 ======================================================================== */
JMF 0:55ec71dc0347 25
JMF 0:55ec71dc0347 26
JMF 0:55ec71dc0347 27 #ifndef __MODULE__
JMF 0:55ec71dc0347 28 #define __MODULE__ "WNCInterface.cpp"
JMF 0:55ec71dc0347 29 #endif
JMF 0:55ec71dc0347 30
JMF 0:55ec71dc0347 31 #include "WNCInterface.h"
JMF 0:55ec71dc0347 32
JMF 0:55ec71dc0347 33 /////////////////////////////////////////////////////
JMF 0:55ec71dc0347 34 // NXP GPIO Pins that are used to initialize the WNC Shield
JMF 0:55ec71dc0347 35 /////////////////////////////////////////////////////
JMF 0:55ec71dc0347 36 DigitalOut mdm_uart2_rx_boot_mode_sel(PTC17); // on powerup, 0 = boot mode, 1 = normal boot
JMF 0:55ec71dc0347 37 DigitalOut mdm_power_on(PTB9); // 0 = turn modem on, 1 = turn modem off (should be held high for >5 seconds to cycle modem)
JMF 0:55ec71dc0347 38 DigitalOut mdm_wakeup_in(PTC2); // 0 = let modem sleep, 1 = keep modem awake -- Note: pulled high on shield
JMF 0:55ec71dc0347 39 DigitalOut mdm_reset(PTC12); // active high
JMF 0:55ec71dc0347 40 DigitalOut shield_3v3_1v8_sig_trans_ena(PTC4); // 0 = disabled (all signals high impedence, 1 = translation active
JMF 0:55ec71dc0347 41 DigitalOut mdm_uart1_cts(PTD0);
JMF 0:55ec71dc0347 42
JMF 0:55ec71dc0347 43 char * _fatal_err_loc; //GLOBAL::holds any error location info
JMF 7:fded23f50479 44 MODSERIAL * _dbgout;
JMF 8:4b38bfb1704d 45 Mutex _WNCLock;
JMF 0:55ec71dc0347 46
JMF 0:55ec71dc0347 47 using namespace WncControllerK64F_fk; // namespace for the controller class use
JMF 0:55ec71dc0347 48
JMF 0:55ec71dc0347 49 // Define pin associations for the controller class to use be careful to
JMF 0:55ec71dc0347 50 // keep the order of the pins in the initialization list.
JMF 0:55ec71dc0347 51 WncGpioPinListK64F wncPinList = {
JMF 0:55ec71dc0347 52 &mdm_uart2_rx_boot_mode_sel,
JMF 0:55ec71dc0347 53 &mdm_power_on,
JMF 0:55ec71dc0347 54 &mdm_wakeup_in,
JMF 0:55ec71dc0347 55 &mdm_reset,
JMF 0:55ec71dc0347 56 &shield_3v3_1v8_sig_trans_ena,
JMF 0:55ec71dc0347 57 &mdm_uart1_cts
JMF 0:55ec71dc0347 58 };
JMF 0:55ec71dc0347 59
JMF 0:55ec71dc0347 60 static MODSERIAL mdmUart(PTD3,PTD2,256,4096); //UART for WNC Module
JMF 0:55ec71dc0347 61
JMF 3:1d7e6ed11269 62 WncControllerK64F *WNCInterface::_pwnc;
JMF 0:55ec71dc0347 63 WncIpStats WNCInterface::myNetStats;
JMF 0:55ec71dc0347 64 string WNCInterface::mac;
JMF 7:fded23f50479 65
JMF 0:55ec71dc0347 66 WNCInterface::WNCInterface() {
JMF 7:fded23f50479 67 _dbgout = NULL;
JMF 0:55ec71dc0347 68 }
JMF 0:55ec71dc0347 69
JMF 0:55ec71dc0347 70 void WNCInterface::doDebug( int v ) {
JMF 0:55ec71dc0347 71 //basic debug = 0x01
JMF 0:55ec71dc0347 72 //more debug = 0x02
JMF 0:55ec71dc0347 73 //all debug = 0x03
JMF 8:4b38bfb1704d 74 M_LOCK;
JMF 0:55ec71dc0347 75 _pwnc->enableDebug( (v&1), (v&2) );
JMF 8:4b38bfb1704d 76 M_ULOCK;
JMF 0:55ec71dc0347 77 }
JMF 0:55ec71dc0347 78
JMF 0:55ec71dc0347 79 //
JMF 0:55ec71dc0347 80 // Power-up the WNC module. The caller can optionally configure.
JMF 0:55ec71dc0347 81 // Inputs:
JMF 0:55ec71dc0347 82 // apn - Caller can specify an APN. If none is provided will use "m2m.com.attz"
JMF 0:55ec71dc0347 83 // debug- specify the amount of debug the WNC controller should output:
JMF 0:55ec71dc0347 84 // 1 - Basic Debug output
JMF 0:55ec71dc0347 85 // 2 - Verbose Debug output
JMF 0:55ec71dc0347 86 // 3 - Full Debug output
JMF 0:55ec71dc0347 87 // Returns: 0 if unable to initialize the WNC module
JMF 0:55ec71dc0347 88 // -1 if successfully initialized
JMF 0:55ec71dc0347 89 //
JMF 3:1d7e6ed11269 90 int WNCInterface::init(const char* apn, MODSERIAL * debug) {
JMF 0:55ec71dc0347 91 int ret = 0;
JMF 7:fded23f50479 92
JMF 8:4b38bfb1704d 93 M_LOCK;
JMF 5:759dceff95b9 94 if( debug ) {
JMF 7:fded23f50479 95 _dbgout = debug;
JMF 3:1d7e6ed11269 96 _pwnc = new WncControllerK64F_fk::WncControllerK64F::WncControllerK64F(&wncPinList, &mdmUart, debug);
JMF 5:759dceff95b9 97 #if WNC_DEBUG == 1
JMF 5:759dceff95b9 98 _pwnc->enableDebug(1,1);
JMF 5:759dceff95b9 99 #endif
JMF 5:759dceff95b9 100 }
JMF 3:1d7e6ed11269 101 else
JMF 3:1d7e6ed11269 102 _pwnc = new WncControllerK64F_fk::WncControllerK64F::WncControllerK64F(&wncPinList, &mdmUart, NULL);
JMF 3:1d7e6ed11269 103
JMF 0:55ec71dc0347 104 if( apn==NULL )
JMF 0:55ec71dc0347 105 apn = APN_DEFAULT;
JMF 0:55ec71dc0347 106
JMF 0:55ec71dc0347 107 ret = ( _pwnc->powerWncOn(apn,40) )? 2:0;
JMF 0:55ec71dc0347 108 ret |= ( _pwnc->setApnName(apn) )? 1:0;
JMF 0:55ec71dc0347 109 ret |= ( _pwnc->getWncNetworkingStats(&myNetStats) )? 4:0;
JMF 8:4b38bfb1704d 110 M_ULOCK;
JMF 7:fded23f50479 111
JMF 0:55ec71dc0347 112 return ret;
JMF 0:55ec71dc0347 113 }
JMF 0:55ec71dc0347 114
JMF 0:55ec71dc0347 115 //
JMF 0:55ec71dc0347 116 // check to see if we are connected to the internet or not. The
JMF 0:55ec71dc0347 117 // connection is supposed to happen during init. If we are
JMF 0:55ec71dc0347 118 // connected to the internet return 0 otherwise return -1
JMF 0:55ec71dc0347 119 //
JMF 0:55ec71dc0347 120 int WNCInterface::connect(void) {
JMF 0:55ec71dc0347 121 return ( _pwnc->getWncStatus() == WNC_GOOD )? 0 : -1;
JMF 0:55ec71dc0347 122 }
JMF 0:55ec71dc0347 123
JMF 0:55ec71dc0347 124 //
JMF 0:55ec71dc0347 125 // ok, the user wants to disconnect. At present, this isn't possible
JMF 0:55ec71dc0347 126 // with the WNC, so just fake it and say we did...
JMF 0:55ec71dc0347 127 //
JMF 0:55ec71dc0347 128 int WNCInterface::disconnect() {
JMF 0:55ec71dc0347 129 return 0;
JMF 0:55ec71dc0347 130 }
JMF 0:55ec71dc0347 131
JMF 0:55ec71dc0347 132 //
JMF 0:55ec71dc0347 133 // update the networking stats and return the IP Address
JMF 0:55ec71dc0347 134 //
JMF 0:55ec71dc0347 135 char * WNCInterface::getIPAddress() {
JMF 8:4b38bfb1704d 136 M_LOCK;
JMF 0:55ec71dc0347 137 if ( _pwnc->getWncNetworkingStats(&myNetStats) ) {
JMF 9:9f0578ff157a 138 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), null);
JMF 8:4b38bfb1704d 139 M_ULOCK;
JMF 0:55ec71dc0347 140 return &myNetStats.ip[0];
JMF 0:55ec71dc0347 141 }
JMF 8:4b38bfb1704d 142 M_ULOCK;
JMF 0:55ec71dc0347 143 return NULL;
JMF 0:55ec71dc0347 144 }
JMF 0:55ec71dc0347 145
JMF 0:55ec71dc0347 146 //
JMF 0:55ec71dc0347 147 // update the networking stats and return the Gateway Address
JMF 0:55ec71dc0347 148 //
JMF 0:55ec71dc0347 149 char * WNCInterface::getGateway() {
JMF 8:4b38bfb1704d 150 M_LOCK;
JMF 0:55ec71dc0347 151 if ( _pwnc->getWncNetworkingStats(&myNetStats) ) {
JMF 9:9f0578ff157a 152 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), null);
JMF 8:4b38bfb1704d 153 M_ULOCK;
JMF 0:55ec71dc0347 154 return &WNCInterface::myNetStats.gateway[0];
JMF 0:55ec71dc0347 155 }
JMF 8:4b38bfb1704d 156 M_ULOCK;
JMF 0:55ec71dc0347 157 return NULL;
JMF 0:55ec71dc0347 158 }
JMF 0:55ec71dc0347 159
JMF 0:55ec71dc0347 160 //
JMF 0:55ec71dc0347 161 // update the networking stats and return the Network Mask
JMF 0:55ec71dc0347 162 //
JMF 0:55ec71dc0347 163 char * WNCInterface::getNetworkMask() {
JMF 8:4b38bfb1704d 164 M_LOCK;
JMF 0:55ec71dc0347 165 if ( _pwnc->getWncNetworkingStats(&myNetStats) ) {
JMF 9:9f0578ff157a 166 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), null);
JMF 8:4b38bfb1704d 167 M_ULOCK;
JMF 0:55ec71dc0347 168 return &WNCInterface::myNetStats.mask[0];
JMF 0:55ec71dc0347 169 }
JMF 8:4b38bfb1704d 170 M_ULOCK;
JMF 0:55ec71dc0347 171 return NULL;
JMF 0:55ec71dc0347 172 }
JMF 0:55ec71dc0347 173
JMF 0:55ec71dc0347 174 //
JMF 0:55ec71dc0347 175 // return a pesudo-MAC address created from the ICCID
JMF 0:55ec71dc0347 176 //
JMF 0:55ec71dc0347 177 char* WNCInterface::getMACAddress( void ) {
JMF 0:55ec71dc0347 178 string str;
JMF 7:fded23f50479 179
JMF 8:4b38bfb1704d 180 M_LOCK;
JMF 0:55ec71dc0347 181 if( _pwnc->getICCID(&str) ) {
JMF 9:9f0578ff157a 182 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), null);
JMF 0:55ec71dc0347 183 mac = str.substr(3,20);
JMF 0:55ec71dc0347 184 mac[2]=mac[5]=mac[8]=mac[11]=mac[14]=':';
JMF 8:4b38bfb1704d 185 M_ULOCK;
JMF 7:fded23f50479 186 return (char*)mac.c_str();
JMF 0:55ec71dc0347 187 }
JMF 8:4b38bfb1704d 188 M_ULOCK;
JMF 0:55ec71dc0347 189 return NULL;
JMF 0:55ec71dc0347 190 }
JMF 0:55ec71dc0347 191