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: WncControllerK64F
Fork of WNCInterface by
WNCInterface.cpp@0:55ec71dc0347, 2016-09-21 (annotated)
- Committer:
- JMF
- Date:
- Wed Sep 21 14:30:06 2016 +0000
- Revision:
- 0:55ec71dc0347
- Child:
- 3:1d7e6ed11269
Initial Commit
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 | 0:55ec71dc0347 | 60 | #if WNC_DEBUG == 1 | 
| JMF | 0:55ec71dc0347 | 61 | static MODSERIAL debugUart(USBTX,USBRX,256,256); //UART used for stdio/stderr | 
| JMF | 0:55ec71dc0347 | 62 | static WncControllerK64F wnc(&wncPinList, &mdmUart, &debugUart); | 
| JMF | 0:55ec71dc0347 | 63 | #else | 
| JMF | 0:55ec71dc0347 | 64 | static WncControllerK64F wnc(&wncPinList, &mdmUart, NULL); | 
| JMF | 0:55ec71dc0347 | 65 | #endif | 
| JMF | 0:55ec71dc0347 | 66 | |
| JMF | 0:55ec71dc0347 | 67 | WncControllerK64F *WNCInterface::_pwnc = &wnc; | 
| JMF | 0:55ec71dc0347 | 68 | WncIpStats WNCInterface::myNetStats; | 
| JMF | 0:55ec71dc0347 | 69 | string WNCInterface::mac; | 
| JMF | 0:55ec71dc0347 | 70 | |
| JMF | 0:55ec71dc0347 | 71 | WNCInterface::WNCInterface() { | 
| JMF | 0:55ec71dc0347 | 72 | #if WNC_DEBUG == 1 | 
| JMF | 0:55ec71dc0347 | 73 | debugUart.baud(115200); // set stdio/stderr. use 115.2K since the WNC part uses this--keep up. | 
| JMF | 0:55ec71dc0347 | 74 | #endif | 
| JMF | 0:55ec71dc0347 | 75 | } | 
| JMF | 0:55ec71dc0347 | 76 | |
| JMF | 0:55ec71dc0347 | 77 | void WNCInterface::doDebug( int v ) { | 
| JMF | 0:55ec71dc0347 | 78 | #if WNC_DEBUG == 1 | 
| JMF | 0:55ec71dc0347 | 79 | //basic debug = 0x01 | 
| JMF | 0:55ec71dc0347 | 80 | //more debug = 0x02 | 
| JMF | 0:55ec71dc0347 | 81 | //all debug = 0x03 | 
| JMF | 0:55ec71dc0347 | 82 | _pwnc->enableDebug( (v&1), (v&2) ); | 
| JMF | 0:55ec71dc0347 | 83 | #endif | 
| JMF | 0:55ec71dc0347 | 84 | } | 
| JMF | 0:55ec71dc0347 | 85 | |
| JMF | 0:55ec71dc0347 | 86 | // | 
| JMF | 0:55ec71dc0347 | 87 | // Power-up the WNC module. The caller can optionally configure. | 
| JMF | 0:55ec71dc0347 | 88 | // Inputs: | 
| JMF | 0:55ec71dc0347 | 89 | // apn - Caller can specify an APN. If none is provided will use "m2m.com.attz" | 
| JMF | 0:55ec71dc0347 | 90 | // debug- specify the amount of debug the WNC controller should output: | 
| JMF | 0:55ec71dc0347 | 91 | // 1 - Basic Debug output | 
| JMF | 0:55ec71dc0347 | 92 | // 2 - Verbose Debug output | 
| JMF | 0:55ec71dc0347 | 93 | // 3 - Full Debug output | 
| JMF | 0:55ec71dc0347 | 94 | // Returns: 0 if unable to initialize the WNC module | 
| JMF | 0:55ec71dc0347 | 95 | // -1 if successfully initialized | 
| JMF | 0:55ec71dc0347 | 96 | // | 
| JMF | 0:55ec71dc0347 | 97 | int WNCInterface::init(const char* apn, int debug) { | 
| JMF | 0:55ec71dc0347 | 98 | int ret = 0; | 
| JMF | 0:55ec71dc0347 | 99 | _pwnc = &wnc; //set the pointer to the WNC controller class. Used for all WNC accesses | 
| JMF | 0:55ec71dc0347 | 100 | |
| JMF | 0:55ec71dc0347 | 101 | if( apn==NULL ) | 
| JMF | 0:55ec71dc0347 | 102 | apn = APN_DEFAULT; | 
| JMF | 0:55ec71dc0347 | 103 | |
| JMF | 0:55ec71dc0347 | 104 | #if WNC_DEBUG == 1 | 
| JMF | 0:55ec71dc0347 | 105 | if( debug ) | 
| JMF | 0:55ec71dc0347 | 106 | _pwnc->enableDebug( (debug&1), (debug&2) ); | 
| JMF | 0:55ec71dc0347 | 107 | #else | 
| JMF | 0:55ec71dc0347 | 108 | debug=debug; //keep the compiler from complaining. | 
| JMF | 0:55ec71dc0347 | 109 | #endif | 
| JMF | 0:55ec71dc0347 | 110 | |
| JMF | 0:55ec71dc0347 | 111 | ret = ( _pwnc->powerWncOn(apn,40) )? 2:0; | 
| JMF | 0:55ec71dc0347 | 112 | ret |= ( _pwnc->setApnName(apn) )? 1:0; | 
| JMF | 0:55ec71dc0347 | 113 | ret |= ( _pwnc->getWncNetworkingStats(&myNetStats) )? 4:0; | 
| JMF | 0:55ec71dc0347 | 114 | |
| JMF | 0:55ec71dc0347 | 115 | return ret; | 
| JMF | 0:55ec71dc0347 | 116 | } | 
| JMF | 0:55ec71dc0347 | 117 | |
| JMF | 0:55ec71dc0347 | 118 | // | 
| JMF | 0:55ec71dc0347 | 119 | // check to see if we are connected to the internet or not. The | 
| JMF | 0:55ec71dc0347 | 120 | // connection is supposed to happen during init. If we are | 
| JMF | 0:55ec71dc0347 | 121 | // connected to the internet return 0 otherwise return -1 | 
| JMF | 0:55ec71dc0347 | 122 | // | 
| JMF | 0:55ec71dc0347 | 123 | int WNCInterface::connect(void) { | 
| JMF | 0:55ec71dc0347 | 124 | return ( _pwnc->getWncStatus() == WNC_GOOD )? 0 : -1; | 
| JMF | 0:55ec71dc0347 | 125 | } | 
| JMF | 0:55ec71dc0347 | 126 | |
| JMF | 0:55ec71dc0347 | 127 | // | 
| JMF | 0:55ec71dc0347 | 128 | // ok, the user wants to disconnect. At present, this isn't possible | 
| JMF | 0:55ec71dc0347 | 129 | // with the WNC, so just fake it and say we did... | 
| JMF | 0:55ec71dc0347 | 130 | // | 
| JMF | 0:55ec71dc0347 | 131 | int WNCInterface::disconnect() { | 
| JMF | 0:55ec71dc0347 | 132 | return 0; | 
| JMF | 0:55ec71dc0347 | 133 | } | 
| JMF | 0:55ec71dc0347 | 134 | |
| JMF | 0:55ec71dc0347 | 135 | // | 
| JMF | 0:55ec71dc0347 | 136 | // update the networking stats and return the IP Address | 
| JMF | 0:55ec71dc0347 | 137 | // | 
| JMF | 0:55ec71dc0347 | 138 | char * WNCInterface::getIPAddress() { | 
| 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 | 0:55ec71dc0347 | 142 | return &myNetStats.ip[0]; | 
| JMF | 0:55ec71dc0347 | 143 | } | 
| JMF | 0:55ec71dc0347 | 144 | return NULL; | 
| JMF | 0:55ec71dc0347 | 145 | } | 
| JMF | 0:55ec71dc0347 | 146 | |
| JMF | 0:55ec71dc0347 | 147 | // | 
| JMF | 0:55ec71dc0347 | 148 | // update the networking stats and return the Gateway Address | 
| JMF | 0:55ec71dc0347 | 149 | // | 
| JMF | 0:55ec71dc0347 | 150 | char * WNCInterface::getGateway() { | 
| JMF | 0:55ec71dc0347 | 151 | if ( _pwnc->getWncNetworkingStats(&myNetStats) ) { | 
| JMF | 0:55ec71dc0347 | 152 | if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ) | 
| JMF | 0:55ec71dc0347 | 153 | FATAL_WNC_ERROR(null); | 
| JMF | 0:55ec71dc0347 | 154 | return &WNCInterface::myNetStats.gateway[0]; | 
| JMF | 0:55ec71dc0347 | 155 | } | 
| JMF | 0:55ec71dc0347 | 156 | return NULL; | 
| JMF | 0:55ec71dc0347 | 157 | } | 
| JMF | 0:55ec71dc0347 | 158 | |
| JMF | 0:55ec71dc0347 | 159 | // | 
| JMF | 0:55ec71dc0347 | 160 | // update the networking stats and return the Network Mask | 
| JMF | 0:55ec71dc0347 | 161 | // | 
| JMF | 0:55ec71dc0347 | 162 | char * WNCInterface::getNetworkMask() { | 
| JMF | 0:55ec71dc0347 | 163 | if ( _pwnc->getWncNetworkingStats(&myNetStats) ) { | 
| JMF | 0:55ec71dc0347 | 164 | if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ) | 
| JMF | 0:55ec71dc0347 | 165 | FATAL_WNC_ERROR(null); | 
| JMF | 0:55ec71dc0347 | 166 | return &WNCInterface::myNetStats.mask[0]; | 
| JMF | 0:55ec71dc0347 | 167 | } | 
| JMF | 0:55ec71dc0347 | 168 | return NULL; | 
| JMF | 0:55ec71dc0347 | 169 | } | 
| JMF | 0:55ec71dc0347 | 170 | |
| JMF | 0:55ec71dc0347 | 171 | // | 
| JMF | 0:55ec71dc0347 | 172 | // return a pesudo-MAC address created from the ICCID | 
| JMF | 0:55ec71dc0347 | 173 | // | 
| JMF | 0:55ec71dc0347 | 174 | char* WNCInterface::getMACAddress( void ) { | 
| JMF | 0:55ec71dc0347 | 175 | string str; | 
| JMF | 0:55ec71dc0347 | 176 | |
| JMF | 0:55ec71dc0347 | 177 | if( _pwnc->getICCID(&str) ) { | 
| JMF | 0:55ec71dc0347 | 178 | if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ) | 
| JMF | 0:55ec71dc0347 | 179 | FATAL_WNC_ERROR(null); | 
| JMF | 0:55ec71dc0347 | 180 | mac = str.substr(3,20); | 
| JMF | 0:55ec71dc0347 | 181 | mac[2]=mac[5]=mac[8]=mac[11]=mac[14]=':'; | 
| JMF | 0:55ec71dc0347 | 182 | return (char*)mac.c_str(); | 
| JMF | 0:55ec71dc0347 | 183 | } | 
| JMF | 0:55ec71dc0347 | 184 | return NULL; | 
| JMF | 0:55ec71dc0347 | 185 | } | 
| JMF | 0:55ec71dc0347 | 186 | 
