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