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