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.
Sms/IOTSMS.cpp@7:fded23f50479, 2016-10-06 (annotated)
- Committer:
- JMF
- Date:
- Thu Oct 06 21:17:18 2016 +0000
- Revision:
- 7:fded23f50479
- Child:
- 9:9f0578ff157a
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?
User | Revision | Line number | New 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 | 7:fded23f50479 | 55 | if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ) |
JMF | 7:fded23f50479 | 56 | FATAL_WNC_ERROR(void); |
JMF | 7:fded23f50479 | 57 | |
JMF | 7:fded23f50479 | 58 | m_SMStimer->stop(); //stop the timer while we check for SMS messages |
JMF | 7:fded23f50479 | 59 | M_LOCK; |
JMF | 7:fded23f50479 | 60 | if( WNCInterface::_pwnc->readUnreadSMSText(&m_smsmsgs, true) ) { |
JMF | 7:fded23f50479 | 61 | for( int i=0; i< m_smsmsgs.msgCount; i++ ) { |
JMF | 7:fded23f50479 | 62 | m_MsgText.number = m_smsmsgs.e[i].number; |
JMF | 7:fded23f50479 | 63 | m_MsgText.date = m_smsmsgs.e[i].date; |
JMF | 7:fded23f50479 | 64 | m_MsgText.time = m_smsmsgs.e[i].time; |
JMF | 7:fded23f50479 | 65 | m_MsgText.msg = m_smsmsgs.e[i].msg; |
JMF | 7:fded23f50479 | 66 | m_callback.call(m_MsgText); |
JMF | 7:fded23f50479 | 67 | } |
JMF | 7:fded23f50479 | 68 | } |
JMF | 7:fded23f50479 | 69 | M_ULOCK; |
JMF | 7:fded23f50479 | 70 | m_SMStimer->start(m_timer); //start it back up |
JMF | 7:fded23f50479 | 71 | } |
JMF | 7:fded23f50479 | 72 | |
JMF | 7:fded23f50479 | 73 | // |
JMF | 7:fded23f50479 | 74 | // initialize the IoT SMS class. Allow a user to set a polling period to check for |
JMF | 7:fded23f50479 | 75 | // SMS messages, and if one is recevied, call a uer defined function. A caller can |
JMF | 7:fded23f50479 | 76 | // not ask for a polling period with no call-back function, and they can not provide |
JMF | 7:fded23f50479 | 77 | // a callback function with no polling period. |
JMF | 7:fded23f50479 | 78 | // |
JMF | 7:fded23f50479 | 79 | // Prior to initializing WNCSms, you must call and create a WNCInterface class object |
JMF | 7:fded23f50479 | 80 | // because the WNCSms object utilizes services of WNCInterface. |
JMF | 7:fded23f50479 | 81 | // |
JMF | 7:fded23f50479 | 82 | // Input: poll - polling frequency in sec. |
JMF | 7:fded23f50479 | 83 | // m_callback - pointer to a user defined function |
JMF | 7:fded23f50479 | 84 | // Output: |
JMF | 7:fded23f50479 | 85 | // 0 if no error |
JMF | 7:fded23f50479 | 86 | // != 0 if any error |
JMF | 7:fded23f50479 | 87 | // |
JMF | 7:fded23f50479 | 88 | int WNCSms::init(uint32_t p, void (*cb)(WNCSmsMsg& s)) { |
JMF | 7:fded23f50479 | 89 | |
JMF | 7:fded23f50479 | 90 | if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ) |
JMF | 7:fded23f50479 | 91 | FATAL_WNC_ERROR(fail); |
JMF | 7:fded23f50479 | 92 | |
JMF | 7:fded23f50479 | 93 | if( WNCInterface::_pwnc == NULL ) //must have already initialized WNCInterface |
JMF | 7:fded23f50479 | 94 | return -1; |
JMF | 7:fded23f50479 | 95 | |
JMF | 7:fded23f50479 | 96 | if(( p == 0 && cb != NULL ) || (p != 0 && cb == NULL)) |
JMF | 7:fded23f50479 | 97 | return -1; |
JMF | 7:fded23f50479 | 98 | |
JMF | 7:fded23f50479 | 99 | if( p != 0 && cb != NULL ) { |
JMF | 7:fded23f50479 | 100 | m_callback.attach(cb); |
JMF | 7:fded23f50479 | 101 | m_timer = p*1000; |
JMF | 7:fded23f50479 | 102 | m_SMStimer->start(m_timer); |
JMF | 7:fded23f50479 | 103 | } |
JMF | 7:fded23f50479 | 104 | |
JMF | 7:fded23f50479 | 105 | M_LOCK; |
JMF | 7:fded23f50479 | 106 | WNCInterface::_pwnc->deleteSMSTextFromMem('*'); //delete any message currently in storage |
JMF | 7:fded23f50479 | 107 | M_ULOCK; |
JMF | 7:fded23f50479 | 108 | m_smsinit = true; |
JMF | 7:fded23f50479 | 109 | |
JMF | 7:fded23f50479 | 110 | return 0; |
JMF | 7:fded23f50479 | 111 | } |
JMF | 7:fded23f50479 | 112 | |
JMF | 7:fded23f50479 | 113 | // |
JMF | 7:fded23f50479 | 114 | // send a message to a user specified number. |
JMF | 7:fded23f50479 | 115 | // |
JMF | 7:fded23f50479 | 116 | int WNCSms::send(const string& number, const string& message) { |
JMF | 7:fded23f50479 | 117 | |
JMF | 7:fded23f50479 | 118 | if( !m_smsinit ) |
JMF | 7:fded23f50479 | 119 | return -1; |
JMF | 7:fded23f50479 | 120 | M_LOCK; |
JMF | 7:fded23f50479 | 121 | int ret=WNCInterface::_pwnc->sendSMSText(number.c_str(), message.c_str()); |
JMF | 7:fded23f50479 | 122 | M_ULOCK; |
JMF | 7:fded23f50479 | 123 | return ret; |
JMF | 7:fded23f50479 | 124 | } |
JMF | 7:fded23f50479 | 125 | |
JMF | 7:fded23f50479 | 126 | |
JMF | 7:fded23f50479 | 127 | // |
JMF | 7:fded23f50479 | 128 | // get a message and put it into the callers string buffer. When returned, this method will |
JMF | 7:fded23f50479 | 129 | // let the caller know that there are more SMS messages to get or 0 when all messages have |
JMF | 7:fded23f50479 | 130 | // been read. |
JMF | 7:fded23f50479 | 131 | // |
JMF | 7:fded23f50479 | 132 | // input: pointer to a string for the senders number |
JMF | 7:fded23f50479 | 133 | // pointer to a string for the senders message |
JMF | 7:fded23f50479 | 134 | // output: 0 if no additional messages available |
JMF | 7:fded23f50479 | 135 | // !0 if additional message available |
JMF | 7:fded23f50479 | 136 | // |
JMF | 7:fded23f50479 | 137 | |
JMF | 7:fded23f50479 | 138 | int WNCSms::get(string& number, string& message) { |
JMF | 7:fded23f50479 | 139 | if( !m_smsinit ) |
JMF | 7:fded23f50479 | 140 | return -1; |
JMF | 7:fded23f50479 | 141 | |
JMF | 7:fded23f50479 | 142 | if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ) |
JMF | 7:fded23f50479 | 143 | FATAL_WNC_ERROR(fail); |
JMF | 7:fded23f50479 | 144 | M_LOCK; |
JMF | 7:fded23f50479 | 145 | int ret = WNCInterface::_pwnc->readUnreadSMSText(&m_smsmsgs, true); |
JMF | 7:fded23f50479 | 146 | M_ULOCK; |
JMF | 7:fded23f50479 | 147 | return ret; |
JMF | 7:fded23f50479 | 148 | } |
JMF | 7:fded23f50479 | 149 | |
JMF | 7:fded23f50479 | 150 | |
JMF | 7:fded23f50479 | 151 | char * WNCSms::getSMSNbr( void ) { |
JMF | 7:fded23f50479 | 152 | char * ret=NULL; |
JMF | 7:fded23f50479 | 153 | string iccid_str; |
JMF | 7:fded23f50479 | 154 | static string msisdn_str; |
JMF | 7:fded23f50479 | 155 | |
JMF | 7:fded23f50479 | 156 | if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ) |
JMF | 7:fded23f50479 | 157 | FATAL_WNC_ERROR(null); |
JMF | 7:fded23f50479 | 158 | |
JMF | 7:fded23f50479 | 159 | M_LOCK; |
JMF | 7:fded23f50479 | 160 | if( !WNCInterface::_pwnc->getICCID(&iccid_str) ) { |
JMF | 7:fded23f50479 | 161 | M_ULOCK; |
JMF | 7:fded23f50479 | 162 | return NULL; |
JMF | 7:fded23f50479 | 163 | } |
JMF | 7:fded23f50479 | 164 | |
JMF | 7:fded23f50479 | 165 | if( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ) { |
JMF | 7:fded23f50479 | 166 | M_ULOCK; |
JMF | 7:fded23f50479 | 167 | FATAL_WNC_ERROR(null); |
JMF | 7:fded23f50479 | 168 | } |
JMF | 7:fded23f50479 | 169 | |
JMF | 7:fded23f50479 | 170 | if( WNCInterface::_pwnc->convertICCIDtoMSISDN(iccid_str, &msisdn_str) ) |
JMF | 7:fded23f50479 | 171 | ret = (char*)msisdn_str.c_str(); |
JMF | 7:fded23f50479 | 172 | M_ULOCK; |
JMF | 7:fded23f50479 | 173 | return ret; |
JMF | 7:fded23f50479 | 174 | } |
JMF | 7:fded23f50479 | 175 |