Jan Korycan / WNCInterface

Dependencies:   WncControllerK64F

Fork of WNCInterface by Jan Korycan

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IOTSMS.cpp Source File

IOTSMS.cpp

00001 /* =====================================================================
00002    Copyright © 2016, Avnet (R)
00003 
00004    Contributors:
00005      * James M Flynn, www.em.avnet.com 
00006  
00007    Licensed under the Apache License, Version 2.0 (the "License"); 
00008    you may not use this file except in compliance with the License.
00009    You may obtain a copy of the License at
00010 
00011     http://www.apache.org/licenses/LICENSE-2.0
00012 
00013    Unless required by applicable law or agreed to in writing, 
00014    software distributed under the License is distributed on an 
00015    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
00016    either express or implied. See the License for the specific 
00017    language governing permissions and limitations under the License.
00018 
00019     @file          IOTSMS.cpp
00020     @version       1.0
00021     @date          Oct 2016
00022 
00023 ======================================================================== */
00024 
00025 #ifndef __MODULE__
00026 #define __MODULE__ "WNCSms.cpp"
00027 #endif
00028 
00029 #include "IOTSMS.h"
00030 #include "mbed.h"
00031 #include "rtos.h"
00032 
00033 #include <cstdio>
00034 #include <cstring>
00035 
00036 //
00037 // this is a user specified function that checks for SMS messages if enabled.  It will
00038 // check to see if a message is available, and if there is, it will call the users
00039 // supplied call-back function with the message.
00040 //
00041 
00042 WNCSms::WNCSms(void ) : m_smsinit(false)  
00043 {
00044     m_SMStimer = new RtosTimer(this, &WNCSms::m_smscheck, osTimerPeriodic);
00045     m_SMStimer->stop();
00046 }
00047 
00048 void WNCSms::m_smscheck( void ){
00049   
00050   //we don't know what the frequency was the user selected to check for
00051   //SMS messages, but we don't want to run into the problem where we are
00052   //not completing our tasks wihin the timeperiod.  So stop the timer when
00053   //we come in and start it when we leave.
00054   //
00055   CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), void);
00056     
00057   m_SMStimer->stop();  //stop the timer while we check for SMS messages
00058   M_LOCK;       
00059   if( WNCInterface::_pwnc->readUnreadSMSText(&m_smsmsgs, true) ) {
00060     for( int i=0; i< m_smsmsgs.msgCount; i++ ) {
00061         m_MsgText.number = m_smsmsgs.e[i].number;
00062         m_MsgText.date = m_smsmsgs.e[i].date;
00063         m_MsgText.time = m_smsmsgs.e[i].time;
00064         m_MsgText.msg = m_smsmsgs.e[i].msg;
00065         m_callback.call(m_MsgText);
00066         }
00067     }
00068   M_ULOCK;
00069   m_SMStimer->start(m_timer);  //start it back up
00070 }
00071 
00072 //
00073 // initialize the IoT SMS class.  Allow a user to set a polling period to check for
00074 // SMS messages, and if one is recevied, call a uer defined function.  A caller can
00075 // not ask for a polling period with no call-back function, and they can not provide
00076 // a callback function with no polling period.
00077 //
00078 // Prior to initializing WNCSms, you must call and create a WNCInterface class object 
00079 // because the WNCSms object utilizes services of WNCInterface.
00080 //
00081 // Input:     poll - polling frequency in sec.
00082 //      m_callback - pointer to a user defined function
00083 // Output:
00084 //      0 if no error 
00085 //   != 0 if any error
00086 //
00087 int WNCSms::init(uint32_t p, void (*cb)(WNCSmsMsg& s)) {
00088 
00089   CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
00090   
00091   if( WNCInterface::_pwnc == NULL )  //must have already initialized WNCInterface
00092     return -1;
00093 
00094   if(( p == 0 && cb != NULL ) || (p != 0 && cb == NULL)) 
00095     return -1;
00096     
00097   if( p != 0 && cb != NULL ) {
00098     m_callback.attach(cb);
00099     m_timer = p*1000;
00100     m_SMStimer->start(m_timer);
00101     }
00102     
00103   M_LOCK;
00104   WNCInterface::_pwnc->deleteSMSTextFromMem('*');  //delete any message currently in storage
00105   M_ULOCK;
00106   m_smsinit = true;
00107 
00108   return 0;
00109 }
00110 
00111 //
00112 // send a message to a user specified number.
00113 //
00114 int WNCSms::send(const string& number, const string& message) {
00115 
00116   if( !m_smsinit )
00117     return -1;
00118   M_LOCK;
00119   int ret=WNCInterface::_pwnc->sendSMSText(number.c_str(), message.c_str());
00120   M_ULOCK;
00121   return ret;
00122   }
00123 
00124 
00125 //
00126 // get a message and put it into the callers string buffer.  When returned, this method will
00127 // let the caller know that there are more SMS messages to get or 0 when all messages have
00128 // been read.
00129 //
00130 // input:  pointer to a string for the senders number
00131 //         pointer to a string for the senders message
00132 // output: 0 if no additional messages available
00133 //        !0 if additional message available
00134 //
00135 
00136 int WNCSms::get(string& number, string& message) {
00137   if( !m_smsinit )
00138     return -1;
00139 
00140   CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), fail);
00141   M_LOCK;
00142   int ret = WNCInterface::_pwnc->readUnreadSMSText(&m_smsmsgs, true);
00143   M_ULOCK;
00144   return ret;
00145   }
00146   
00147 
00148 char * WNCSms::getSMSNbr( void ) {
00149     char * ret=NULL;
00150     string iccid_str;
00151     static string msisdn_str;
00152     
00153     CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), null);
00154     M_LOCK;
00155     if( !WNCInterface::_pwnc->getICCID(&iccid_str) ) {
00156        M_ULOCK;
00157        return NULL;
00158        }
00159       
00160     CHK_WNCFE(( WNCInterface::_pwnc->getWncStatus() == FATAL_FLAG ), null);
00161 
00162     if( WNCInterface::_pwnc->convertICCIDtoMSISDN(iccid_str, &msisdn_str) )
00163         ret = (char*)msisdn_str.c_str();    
00164     M_ULOCK;
00165     return ret;
00166 }
00167