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:
Fri Mar 24 22:26:23 2017 +0000
Revision:
29:b278b745fb4f
Parent:
9:9f0578ff157a
updated Class name of TCPSocketConnection to WncTCPSocketConnection;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 7:fded23f50479 1 /* =====================================================================
JMF 7:fded23f50479 2 Copyright © 2016, Avnet (R)
JMF 7:fded23f50479 3
JMF 7:fded23f50479 4 Contributors:
JMF 7:fded23f50479 5 * James M Flynn, www.em.avnet.com
JMF 7:fded23f50479 6
JMF 7:fded23f50479 7 Licensed under the Apache License, Version 2.0 (the "License");
JMF 7:fded23f50479 8 you may not use this file except in compliance with the License.
JMF 7:fded23f50479 9 You may obtain a copy of the License at
JMF 7:fded23f50479 10
JMF 7:fded23f50479 11 http://www.apache.org/licenses/LICENSE-2.0
JMF 7:fded23f50479 12
JMF 7:fded23f50479 13 Unless required by applicable law or agreed to in writing,
JMF 7:fded23f50479 14 software distributed under the License is distributed on an
JMF 7:fded23f50479 15 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
JMF 7:fded23f50479 16 either express or implied. See the License for the specific
JMF 7:fded23f50479 17 language governing permissions and limitations under the License.
JMF 7:fded23f50479 18
JMF 7:fded23f50479 19 @file IOTSMS.cpp
JMF 7:fded23f50479 20 @version 1.0
JMF 7:fded23f50479 21 @date Oct 2016
JMF 7:fded23f50479 22
JMF 7:fded23f50479 23 ======================================================================== */
JMF 7:fded23f50479 24
JMF 7:fded23f50479 25 #ifndef __MODULE__
JMF 7:fded23f50479 26 #define __MODULE__ "WNCSms.cpp"
JMF 7:fded23f50479 27 #endif
JMF 7:fded23f50479 28
JMF 7:fded23f50479 29 #include "IOTSMS.h"
JMF 7:fded23f50479 30 #include "mbed.h"
JMF 7:fded23f50479 31 #include "rtos.h"
JMF 7:fded23f50479 32
JMF 7:fded23f50479 33 #include <cstdio>
JMF 7:fded23f50479 34 #include <cstring>
JMF 7:fded23f50479 35
JMF 7:fded23f50479 36 //
JMF 7:fded23f50479 37 // this is a user specified function that checks for SMS messages if enabled. It will
JMF 7:fded23f50479 38 // check to see if a message is available, and if there is, it will call the users
JMF 7:fded23f50479 39 // supplied call-back function with the message.
JMF 7:fded23f50479 40 //
JMF 7:fded23f50479 41
JMF 7:fded23f50479 42 WNCSms::WNCSms(void ) : m_smsinit(false)
JMF 7:fded23f50479 43 {
JMF 7:fded23f50479 44 m_SMStimer = new RtosTimer(this, &WNCSms::m_smscheck, osTimerPeriodic);
JMF 7:fded23f50479 45 m_SMStimer->stop();
JMF 7:fded23f50479 46 }
JMF 7:fded23f50479 47
JMF 7:fded23f50479 48 void WNCSms::m_smscheck( void ){
JMF 7:fded23f50479 49
JMF 7:fded23f50479 50 //we don't know what the frequency was the user selected to check for
JMF 7:fded23f50479 51 //SMS messages, but we don't want to run into the problem where we are
JMF 7:fded23f50479 52 //not completing our tasks wihin the timeperiod. So stop the timer when
JMF 7:fded23f50479 53 //we come in and start it when we leave.
JMF 7:fded23f50479 54 //
JMF 9:9f0578ff157a 55 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void);
JMF 7:fded23f50479 56
JMF 7:fded23f50479 57 m_SMStimer->stop(); //stop the timer while we check for SMS messages
JMF 7:fded23f50479 58 M_LOCK;
JMF 7:fded23f50479 59 if( WNCInterface::_pwnc->readUnreadSMSText(&m_smsmsgs, true) ) {
JMF 7:fded23f50479 60 for( int i=0; i< m_smsmsgs.msgCount; i++ ) {
JMF 7:fded23f50479 61 m_MsgText.number = m_smsmsgs.e[i].number;
JMF 7:fded23f50479 62 m_MsgText.date = m_smsmsgs.e[i].date;
JMF 7:fded23f50479 63 m_MsgText.time = m_smsmsgs.e[i].time;
JMF 7:fded23f50479 64 m_MsgText.msg = m_smsmsgs.e[i].msg;
JMF 7:fded23f50479 65 m_callback.call(m_MsgText);
JMF 7:fded23f50479 66 }
JMF 7:fded23f50479 67 }
JMF 7:fded23f50479 68 M_ULOCK;
JMF 7:fded23f50479 69 m_SMStimer->start(m_timer); //start it back up
JMF 7:fded23f50479 70 }
JMF 7:fded23f50479 71
JMF 7:fded23f50479 72 //
JMF 7:fded23f50479 73 // initialize the IoT SMS class. Allow a user to set a polling period to check for
JMF 7:fded23f50479 74 // SMS messages, and if one is recevied, call a uer defined function. A caller can
JMF 7:fded23f50479 75 // not ask for a polling period with no call-back function, and they can not provide
JMF 7:fded23f50479 76 // a callback function with no polling period.
JMF 7:fded23f50479 77 //
JMF 7:fded23f50479 78 // Prior to initializing WNCSms, you must call and create a WNCInterface class object
JMF 7:fded23f50479 79 // because the WNCSms object utilizes services of WNCInterface.
JMF 7:fded23f50479 80 //
JMF 7:fded23f50479 81 // Input: poll - polling frequency in sec.
JMF 7:fded23f50479 82 // m_callback - pointer to a user defined function
JMF 7:fded23f50479 83 // Output:
JMF 7:fded23f50479 84 // 0 if no error
JMF 7:fded23f50479 85 // != 0 if any error
JMF 7:fded23f50479 86 //
JMF 7:fded23f50479 87 int WNCSms::init(uint32_t p, void (*cb)(WNCSmsMsg& s)) {
JMF 7:fded23f50479 88
JMF 9:9f0578ff157a 89 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
JMF 7:fded23f50479 90
JMF 7:fded23f50479 91 if( WNCInterface::_pwnc == NULL ) //must have already initialized WNCInterface
JMF 7:fded23f50479 92 return -1;
JMF 7:fded23f50479 93
JMF 7:fded23f50479 94 if(( p == 0 && cb != NULL ) || (p != 0 && cb == NULL))
JMF 7:fded23f50479 95 return -1;
JMF 7:fded23f50479 96
JMF 7:fded23f50479 97 if( p != 0 && cb != NULL ) {
JMF 7:fded23f50479 98 m_callback.attach(cb);
JMF 7:fded23f50479 99 m_timer = p*1000;
JMF 7:fded23f50479 100 m_SMStimer->start(m_timer);
JMF 7:fded23f50479 101 }
JMF 7:fded23f50479 102
JMF 7:fded23f50479 103 M_LOCK;
JMF 7:fded23f50479 104 WNCInterface::_pwnc->deleteSMSTextFromMem('*'); //delete any message currently in storage
JMF 7:fded23f50479 105 M_ULOCK;
JMF 7:fded23f50479 106 m_smsinit = true;
JMF 7:fded23f50479 107
JMF 7:fded23f50479 108 return 0;
JMF 7:fded23f50479 109 }
JMF 7:fded23f50479 110
JMF 7:fded23f50479 111 //
JMF 7:fded23f50479 112 // send a message to a user specified number.
JMF 7:fded23f50479 113 //
JMF 7:fded23f50479 114 int WNCSms::send(const string& number, const string& message) {
JMF 7:fded23f50479 115
JMF 7:fded23f50479 116 if( !m_smsinit )
JMF 7:fded23f50479 117 return -1;
JMF 7:fded23f50479 118 M_LOCK;
JMF 7:fded23f50479 119 int ret=WNCInterface::_pwnc->sendSMSText(number.c_str(), message.c_str());
JMF 7:fded23f50479 120 M_ULOCK;
JMF 7:fded23f50479 121 return ret;
JMF 7:fded23f50479 122 }
JMF 7:fded23f50479 123
JMF 7:fded23f50479 124
JMF 7:fded23f50479 125 //
JMF 7:fded23f50479 126 // get a message and put it into the callers string buffer. When returned, this method will
JMF 7:fded23f50479 127 // let the caller know that there are more SMS messages to get or 0 when all messages have
JMF 7:fded23f50479 128 // been read.
JMF 7:fded23f50479 129 //
JMF 7:fded23f50479 130 // input: pointer to a string for the senders number
JMF 7:fded23f50479 131 // pointer to a string for the senders message
JMF 7:fded23f50479 132 // output: 0 if no additional messages available
JMF 7:fded23f50479 133 // !0 if additional message available
JMF 7:fded23f50479 134 //
JMF 7:fded23f50479 135
JMF 7:fded23f50479 136 int WNCSms::get(string& number, string& message) {
JMF 7:fded23f50479 137 if( !m_smsinit )
JMF 7:fded23f50479 138 return -1;
JMF 7:fded23f50479 139
JMF 9:9f0578ff157a 140 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
JMF 7:fded23f50479 141 M_LOCK;
JMF 7:fded23f50479 142 int ret = WNCInterface::_pwnc->readUnreadSMSText(&m_smsmsgs, true);
JMF 7:fded23f50479 143 M_ULOCK;
JMF 7:fded23f50479 144 return ret;
JMF 7:fded23f50479 145 }
JMF 7:fded23f50479 146
JMF 7:fded23f50479 147
JMF 7:fded23f50479 148 char * WNCSms::getSMSNbr( void ) {
JMF 7:fded23f50479 149 char * ret=NULL;
JMF 7:fded23f50479 150 string iccid_str;
JMF 7:fded23f50479 151 static string msisdn_str;
JMF 7:fded23f50479 152
JMF 9:9f0578ff157a 153 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), null);
JMF 7:fded23f50479 154 M_LOCK;
JMF 7:fded23f50479 155 if( !WNCInterface::_pwnc->getICCID(&iccid_str) ) {
JMF 7:fded23f50479 156 M_ULOCK;
JMF 7:fded23f50479 157 return NULL;
JMF 7:fded23f50479 158 }
JMF 7:fded23f50479 159
JMF 9:9f0578ff157a 160 CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), null);
JMF 7:fded23f50479 161
JMF 7:fded23f50479 162 if( WNCInterface::_pwnc->convertICCIDtoMSISDN(iccid_str, &msisdn_str) )
JMF 7:fded23f50479 163 ret = (char*)msisdn_str.c_str();
JMF 7:fded23f50479 164 M_ULOCK;
JMF 7:fded23f50479 165 return ret;
JMF 7:fded23f50479 166 }
JMF 7:fded23f50479 167