The WDCInterface is is a drop-in replacement for an EthernetInterface class that allows the user to connect to the Internet with a Wistron NeWeb Corporation (WNC) M14A2A Series data module using the standard network Socket API's. This interface class is used in the AT&T Cellular IoT Starter Kit which is sold by Avnet (http://cloudconnectkits.org/product/att-cellular-iot-starter-kit).

Dependencies:   WncControllerK64F

Dependents:   WNCProximityMqtt Pubnub_ATT_IoT_SK_WNC_sync BluemixDemo BluemixQS ... more

See the WNCInterface README in the Wiki tab for detailed information on this library.

Committer:
JMF
Date:
Thu Oct 06 21:17:18 2016 +0000
Revision:
7:fded23f50479
Parent:
5:759dceff95b9
Child:
8:4b38bfb1704d
This version adds the SMS class and adds support for multi-threading to eliminate  re-entrant with the WncController class

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 0:55ec71dc0347 45
JMF 0:55ec71dc0347 46 using namespace WncControllerK64F_fk; // namespace for the controller class use
JMF 0:55ec71dc0347 47
JMF 0:55ec71dc0347 48 // Define pin associations for the controller class to use be careful to
JMF 0:55ec71dc0347 49 // keep the order of the pins in the initialization list.
JMF 0:55ec71dc0347 50 WncGpioPinListK64F wncPinList = {
JMF 0:55ec71dc0347 51 &mdm_uart2_rx_boot_mode_sel,
JMF 0:55ec71dc0347 52 &mdm_power_on,
JMF 0:55ec71dc0347 53 &mdm_wakeup_in,
JMF 0:55ec71dc0347 54 &mdm_reset,
JMF 0:55ec71dc0347 55 &shield_3v3_1v8_sig_trans_ena,
JMF 0:55ec71dc0347 56 &mdm_uart1_cts
JMF 0:55ec71dc0347 57 };
JMF 0:55ec71dc0347 58
JMF 0:55ec71dc0347 59 static MODSERIAL mdmUart(PTD3,PTD2,256,4096); //UART for WNC Module
JMF 0:55ec71dc0347 60
JMF 3:1d7e6ed11269 61 WncControllerK64F *WNCInterface::_pwnc;
JMF 0:55ec71dc0347 62 WncIpStats WNCInterface::myNetStats;
JMF 0:55ec71dc0347 63 string WNCInterface::mac;
JMF 7:fded23f50479 64 Mutex *WNCInterface::WNCLock;
JMF 7:fded23f50479 65
JMF 0:55ec71dc0347 66 WNCInterface::WNCInterface() {
JMF 7:fded23f50479 67 _dbgout = NULL;
JMF 7:fded23f50479 68 WNCLock = new Mutex();
JMF 7:fded23f50479 69 WNCLock->unlock();
JMF 0:55ec71dc0347 70 }
JMF 0:55ec71dc0347 71
JMF 0:55ec71dc0347 72 void WNCInterface::doDebug( int v ) {
JMF 0:55ec71dc0347 73 //basic debug = 0x01
JMF 0:55ec71dc0347 74 //more debug = 0x02
JMF 0:55ec71dc0347 75 //all debug = 0x03
JMF 7:fded23f50479 76 WNCLock->lock();
JMF 0:55ec71dc0347 77 _pwnc->enableDebug( (v&1), (v&2) );
JMF 7:fded23f50479 78 WNCLock->unlock();
JMF 0:55ec71dc0347 79 }
JMF 0:55ec71dc0347 80
JMF 0:55ec71dc0347 81 //
JMF 0:55ec71dc0347 82 // Power-up the WNC module. The caller can optionally configure.
JMF 0:55ec71dc0347 83 // Inputs:
JMF 0:55ec71dc0347 84 // apn - Caller can specify an APN. If none is provided will use "m2m.com.attz"
JMF 0:55ec71dc0347 85 // debug- specify the amount of debug the WNC controller should output:
JMF 0:55ec71dc0347 86 // 1 - Basic Debug output
JMF 0:55ec71dc0347 87 // 2 - Verbose Debug output
JMF 0:55ec71dc0347 88 // 3 - Full Debug output
JMF 0:55ec71dc0347 89 // Returns: 0 if unable to initialize the WNC module
JMF 0:55ec71dc0347 90 // -1 if successfully initialized
JMF 0:55ec71dc0347 91 //
JMF 3:1d7e6ed11269 92 int WNCInterface::init(const char* apn, MODSERIAL * debug) {
JMF 0:55ec71dc0347 93 int ret = 0;
JMF 7:fded23f50479 94
JMF 7:fded23f50479 95 WNCLock->lock();
JMF 5:759dceff95b9 96 if( debug ) {
JMF 7:fded23f50479 97 _dbgout = debug;
JMF 3:1d7e6ed11269 98 _pwnc = new WncControllerK64F_fk::WncControllerK64F::WncControllerK64F(&wncPinList, &mdmUart, debug);
JMF 5:759dceff95b9 99 #if WNC_DEBUG == 1
JMF 5:759dceff95b9 100 _pwnc->enableDebug(1,1);
JMF 5:759dceff95b9 101 #endif
JMF 5:759dceff95b9 102 }
JMF 3:1d7e6ed11269 103 else
JMF 3:1d7e6ed11269 104 _pwnc = new WncControllerK64F_fk::WncControllerK64F::WncControllerK64F(&wncPinList, &mdmUart, NULL);
JMF 3:1d7e6ed11269 105
JMF 0:55ec71dc0347 106 if( apn==NULL )
JMF 0:55ec71dc0347 107 apn = APN_DEFAULT;
JMF 0:55ec71dc0347 108
JMF 0:55ec71dc0347 109 ret = ( _pwnc->powerWncOn(apn,40) )? 2:0;
JMF 0:55ec71dc0347 110 ret |= ( _pwnc->setApnName(apn) )? 1:0;
JMF 0:55ec71dc0347 111 ret |= ( _pwnc->getWncNetworkingStats(&myNetStats) )? 4:0;
JMF 7:fded23f50479 112 WNCLock->unlock();
JMF 7:fded23f50479 113
JMF 0:55ec71dc0347 114 return ret;
JMF 0:55ec71dc0347 115 }
JMF 0:55ec71dc0347 116
JMF 0:55ec71dc0347 117 //
JMF 0:55ec71dc0347 118 // check to see if we are connected to the internet or not. The
JMF 0:55ec71dc0347 119 // connection is supposed to happen during init. If we are
JMF 0:55ec71dc0347 120 // connected to the internet return 0 otherwise return -1
JMF 0:55ec71dc0347 121 //
JMF 0:55ec71dc0347 122 int WNCInterface::connect(void) {
JMF 0:55ec71dc0347 123 return ( _pwnc->getWncStatus() == WNC_GOOD )? 0 : -1;
JMF 0:55ec71dc0347 124 }
JMF 0:55ec71dc0347 125
JMF 0:55ec71dc0347 126 //
JMF 0:55ec71dc0347 127 // ok, the user wants to disconnect. At present, this isn't possible
JMF 0:55ec71dc0347 128 // with the WNC, so just fake it and say we did...
JMF 0:55ec71dc0347 129 //
JMF 0:55ec71dc0347 130 int WNCInterface::disconnect() {
JMF 0:55ec71dc0347 131 return 0;
JMF 0:55ec71dc0347 132 }
JMF 0:55ec71dc0347 133
JMF 0:55ec71dc0347 134 //
JMF 0:55ec71dc0347 135 // update the networking stats and return the IP Address
JMF 0:55ec71dc0347 136 //
JMF 0:55ec71dc0347 137 char * WNCInterface::getIPAddress() {
JMF 7:fded23f50479 138 WNCLock->lock();
JMF 0:55ec71dc0347 139 if ( _pwnc->getWncNetworkingStats(&myNetStats) ) {
JMF 0:55ec71dc0347 140 if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
JMF 0:55ec71dc0347 141 FATAL_WNC_ERROR(null);
JMF 7:fded23f50479 142 WNCLock->unlock();
JMF 0:55ec71dc0347 143 return &myNetStats.ip[0];
JMF 0:55ec71dc0347 144 }
JMF 7:fded23f50479 145 WNCLock->unlock();
JMF 0:55ec71dc0347 146 return NULL;
JMF 0:55ec71dc0347 147 }
JMF 0:55ec71dc0347 148
JMF 0:55ec71dc0347 149 //
JMF 0:55ec71dc0347 150 // update the networking stats and return the Gateway Address
JMF 0:55ec71dc0347 151 //
JMF 0:55ec71dc0347 152 char * WNCInterface::getGateway() {
JMF 7:fded23f50479 153 WNCLock->lock();
JMF 0:55ec71dc0347 154 if ( _pwnc->getWncNetworkingStats(&myNetStats) ) {
JMF 0:55ec71dc0347 155 if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
JMF 0:55ec71dc0347 156 FATAL_WNC_ERROR(null);
JMF 7:fded23f50479 157 WNCLock->unlock();
JMF 0:55ec71dc0347 158 return &WNCInterface::myNetStats.gateway[0];
JMF 0:55ec71dc0347 159 }
JMF 7:fded23f50479 160 WNCLock->unlock();
JMF 0:55ec71dc0347 161 return NULL;
JMF 0:55ec71dc0347 162 }
JMF 0:55ec71dc0347 163
JMF 0:55ec71dc0347 164 //
JMF 0:55ec71dc0347 165 // update the networking stats and return the Network Mask
JMF 0:55ec71dc0347 166 //
JMF 0:55ec71dc0347 167 char * WNCInterface::getNetworkMask() {
JMF 7:fded23f50479 168 WNCLock->lock();
JMF 0:55ec71dc0347 169 if ( _pwnc->getWncNetworkingStats(&myNetStats) ) {
JMF 0:55ec71dc0347 170 if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
JMF 0:55ec71dc0347 171 FATAL_WNC_ERROR(null);
JMF 7:fded23f50479 172 WNCLock->unlock();
JMF 0:55ec71dc0347 173 return &WNCInterface::myNetStats.mask[0];
JMF 0:55ec71dc0347 174 }
JMF 7:fded23f50479 175 WNCLock->unlock();
JMF 0:55ec71dc0347 176 return NULL;
JMF 0:55ec71dc0347 177 }
JMF 0:55ec71dc0347 178
JMF 0:55ec71dc0347 179 //
JMF 0:55ec71dc0347 180 // return a pesudo-MAC address created from the ICCID
JMF 0:55ec71dc0347 181 //
JMF 0:55ec71dc0347 182 char* WNCInterface::getMACAddress( void ) {
JMF 0:55ec71dc0347 183 string str;
JMF 7:fded23f50479 184
JMF 7:fded23f50479 185 WNCLock->lock();
JMF 0:55ec71dc0347 186 if( _pwnc->getICCID(&str) ) {
JMF 0:55ec71dc0347 187 if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG )
JMF 0:55ec71dc0347 188 FATAL_WNC_ERROR(null);
JMF 0:55ec71dc0347 189 mac = str.substr(3,20);
JMF 0:55ec71dc0347 190 mac[2]=mac[5]=mac[8]=mac[11]=mac[14]=':';
JMF 7:fded23f50479 191 WNCLock->unlock();
JMF 7:fded23f50479 192 return (char*)mac.c_str();
JMF 0:55ec71dc0347 193 }
JMF 7:fded23f50479 194 WNCLock->unlock();
JMF 0:55ec71dc0347 195 return NULL;
JMF 0:55ec71dc0347 196 }
JMF 0:55ec71dc0347 197