PHS module SMA-01 library. see: https://developer.mbed.org/users/phsfan/notebook/abitusbmodem/
Dependencies: Socket lwip-sys lwip
Dependents: AbitUSBModem_HTTPTest AbitUSBModem_MQTTTest AbitUSBModem_WebsocketTest AbitUSBModem_SMSTest
Fork of VodafoneUSBModem by
sms/SMSInterface.cpp@42:daa4dd282103, 2012-09-25 (annotated)
- Committer:
- ashleymills
- Date:
- Tue Sep 25 13:32:45 2012 +0000
- Revision:
- 42:daa4dd282103
- Parent:
- 39:85dc5ca10b7f
- Child:
- 46:c2282539c858
Typo in nicks debug message: d% instead of %d
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:3b2f052c333b | 1 | /* SMSInterface.cpp */ |
donatien | 22:06fb2a93a1f6 | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
donatien | 22:06fb2a93a1f6 | 3 | * |
donatien | 22:06fb2a93a1f6 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
donatien | 22:06fb2a93a1f6 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
donatien | 22:06fb2a93a1f6 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
donatien | 22:06fb2a93a1f6 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
donatien | 22:06fb2a93a1f6 | 8 | * furnished to do so, subject to the following conditions: |
donatien | 22:06fb2a93a1f6 | 9 | * |
donatien | 22:06fb2a93a1f6 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
donatien | 22:06fb2a93a1f6 | 11 | * substantial portions of the Software. |
donatien | 22:06fb2a93a1f6 | 12 | * |
donatien | 22:06fb2a93a1f6 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
donatien | 22:06fb2a93a1f6 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
donatien | 22:06fb2a93a1f6 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
donatien | 22:06fb2a93a1f6 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 22:06fb2a93a1f6 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
donatien | 22:06fb2a93a1f6 | 18 | */ |
donatien | 0:3b2f052c333b | 19 | |
nherriot | 39:85dc5ca10b7f | 20 | #define __DEBUG__ 4 |
donatien | 0:3b2f052c333b | 21 | #ifndef __MODULE__ |
donatien | 0:3b2f052c333b | 22 | #define __MODULE__ "SMSInterface.cpp" |
donatien | 0:3b2f052c333b | 23 | #endif |
donatien | 0:3b2f052c333b | 24 | |
donatien | 0:3b2f052c333b | 25 | #include "core/fwk.h" |
donatien | 0:3b2f052c333b | 26 | |
donatien | 0:3b2f052c333b | 27 | #include "SMSInterface.h" |
donatien | 0:3b2f052c333b | 28 | |
donatien | 0:3b2f052c333b | 29 | #include <cstdio> |
donatien | 0:3b2f052c333b | 30 | |
donatien | 0:3b2f052c333b | 31 | #define DEFAULT_TIMEOUT 10000 |
donatien | 0:3b2f052c333b | 32 | |
donatien | 26:d37501dc6c61 | 33 | SMSInterface::SMSInterface(ATCommandsInterface* pIf) : m_pIf(pIf), m_msg(NULL), m_maxMsgLength(0), m_msisdn(NULL) |
donatien | 0:3b2f052c333b | 34 | { |
donatien | 0:3b2f052c333b | 35 | m_pIf->registerEventsHandler(this); //Add us to the unsolicited result codes handlers |
donatien | 0:3b2f052c333b | 36 | } |
donatien | 0:3b2f052c333b | 37 | |
donatien | 0:3b2f052c333b | 38 | int SMSInterface::init() |
donatien | 0:3b2f052c333b | 39 | { |
donatien | 26:d37501dc6c61 | 40 | m_msgRefListCount = 0; |
donatien | 26:d37501dc6c61 | 41 | m_needsUpdate = true; |
donatien | 26:d37501dc6c61 | 42 | m_state = SMS_IDLE; |
donatien | 26:d37501dc6c61 | 43 | |
donatien | 0:3b2f052c333b | 44 | DBG("Set format"); |
donatien | 0:3b2f052c333b | 45 | //Set Text mode format |
donatien | 0:3b2f052c333b | 46 | int ret = m_pIf->executeSimple("AT+CMGF=1", NULL, DEFAULT_TIMEOUT); |
donatien | 0:3b2f052c333b | 47 | if(ret != OK) |
donatien | 0:3b2f052c333b | 48 | { |
donatien | 0:3b2f052c333b | 49 | return NET_PROTOCOL; |
donatien | 0:3b2f052c333b | 50 | } |
donatien | 0:3b2f052c333b | 51 | |
donatien | 0:3b2f052c333b | 52 | DBG("Setup new messages indication"); |
donatien | 0:3b2f052c333b | 53 | //Setup new messages indication |
donatien | 0:3b2f052c333b | 54 | ret = m_pIf->executeSimple("AT+CNMI=2,1,0,0,0", NULL, DEFAULT_TIMEOUT); |
donatien | 0:3b2f052c333b | 55 | if(ret != OK) |
donatien | 0:3b2f052c333b | 56 | { |
donatien | 0:3b2f052c333b | 57 | return NET_PROTOCOL; |
donatien | 0:3b2f052c333b | 58 | } |
donatien | 0:3b2f052c333b | 59 | |
donatien | 0:3b2f052c333b | 60 | DBG("Try to fetch inbox"); |
donatien | 0:3b2f052c333b | 61 | m_inboxMtx.lock(); |
donatien | 0:3b2f052c333b | 62 | if( m_needsUpdate ) |
donatien | 0:3b2f052c333b | 63 | { |
donatien | 0:3b2f052c333b | 64 | ret = updateInbox(); //Fetch existing messages references |
donatien | 0:3b2f052c333b | 65 | if(ret) |
donatien | 0:3b2f052c333b | 66 | { |
donatien | 0:3b2f052c333b | 67 | m_inboxMtx.unlock(); |
donatien | 0:3b2f052c333b | 68 | return NET_PROTOCOL; |
donatien | 0:3b2f052c333b | 69 | } |
donatien | 0:3b2f052c333b | 70 | } |
donatien | 0:3b2f052c333b | 71 | m_inboxMtx.unlock(); |
donatien | 0:3b2f052c333b | 72 | |
donatien | 0:3b2f052c333b | 73 | DBG("Initialization done"); |
donatien | 0:3b2f052c333b | 74 | return OK; |
donatien | 0:3b2f052c333b | 75 | } |
donatien | 0:3b2f052c333b | 76 | |
donatien | 0:3b2f052c333b | 77 | int SMSInterface::send(const char* number, const char* message) |
donatien | 0:3b2f052c333b | 78 | { |
donatien | 0:3b2f052c333b | 79 | if( strlen(number) > 16 ) |
donatien | 0:3b2f052c333b | 80 | { |
donatien | 0:3b2f052c333b | 81 | return NET_INVALID; //Number too long to match 3GPP spec |
donatien | 0:3b2f052c333b | 82 | } |
donatien | 0:3b2f052c333b | 83 | |
donatien | 0:3b2f052c333b | 84 | int ret; |
donatien | 0:3b2f052c333b | 85 | |
donatien | 0:3b2f052c333b | 86 | //Prepare infos |
donatien | 0:3b2f052c333b | 87 | m_state = SMS_SEND_CMD_SENT; |
donatien | 0:3b2f052c333b | 88 | m_msg = (char*) message; |
donatien | 0:3b2f052c333b | 89 | |
donatien | 0:3b2f052c333b | 90 | DBG("Send SM"); |
donatien | 0:3b2f052c333b | 91 | //Send command |
donatien | 0:3b2f052c333b | 92 | char cmd[32]; |
donatien | 0:3b2f052c333b | 93 | std::sprintf(cmd, "AT+CMGS=\"%s\"", number); |
donatien | 0:3b2f052c333b | 94 | ret = m_pIf->execute(cmd, this, NULL, DEFAULT_TIMEOUT); |
donatien | 0:3b2f052c333b | 95 | |
donatien | 0:3b2f052c333b | 96 | if( (ret != OK) || (m_state != SMS_CMD_PROCESSED) ) |
donatien | 0:3b2f052c333b | 97 | { |
donatien | 0:3b2f052c333b | 98 | WARN("ret %d, state %d", ret, m_state); |
donatien | 0:3b2f052c333b | 99 | m_state = SMS_IDLE; |
donatien | 0:3b2f052c333b | 100 | return NET_PROTOCOL; |
donatien | 0:3b2f052c333b | 101 | } |
donatien | 0:3b2f052c333b | 102 | |
donatien | 0:3b2f052c333b | 103 | DBG("SM sent"); |
donatien | 0:3b2f052c333b | 104 | m_state = SMS_IDLE; |
donatien | 0:3b2f052c333b | 105 | return OK; |
donatien | 0:3b2f052c333b | 106 | } |
donatien | 0:3b2f052c333b | 107 | |
donatien | 0:3b2f052c333b | 108 | |
donatien | 0:3b2f052c333b | 109 | int SMSInterface::get(char* number, char* message, size_t maxLength) |
donatien | 0:3b2f052c333b | 110 | { |
donatien | 0:3b2f052c333b | 111 | if( maxLength < 1 ) |
donatien | 0:3b2f052c333b | 112 | { |
donatien | 0:3b2f052c333b | 113 | return NET_INVALID; //Buffer too short |
donatien | 0:3b2f052c333b | 114 | } |
donatien | 0:3b2f052c333b | 115 | |
donatien | 0:3b2f052c333b | 116 | int ret; |
donatien | 0:3b2f052c333b | 117 | |
donatien | 0:3b2f052c333b | 118 | DBG("Get next message"); |
donatien | 0:3b2f052c333b | 119 | m_inboxMtx.lock(); |
donatien | 0:3b2f052c333b | 120 | if(m_msgRefListCount == 0 && m_needsUpdate) |
donatien | 0:3b2f052c333b | 121 | { |
nherriot | 39:85dc5ca10b7f | 122 | DBG("Message list count is 0 and needs updating. Running updateInbox."); |
donatien | 0:3b2f052c333b | 123 | ret = updateInbox(); |
nherriot | 39:85dc5ca10b7f | 124 | |
donatien | 0:3b2f052c333b | 125 | if (ret) |
donatien | 0:3b2f052c333b | 126 | { |
donatien | 0:3b2f052c333b | 127 | m_inboxMtx.unlock(); |
donatien | 0:3b2f052c333b | 128 | return ret; |
donatien | 0:3b2f052c333b | 129 | } |
donatien | 0:3b2f052c333b | 130 | } |
donatien | 0:3b2f052c333b | 131 | |
donatien | 0:3b2f052c333b | 132 | if(m_msgRefListCount == 0) |
donatien | 0:3b2f052c333b | 133 | { |
donatien | 0:3b2f052c333b | 134 | m_inboxMtx.unlock(); |
nherriot | 39:85dc5ca10b7f | 135 | DBG("Message list count is 0, I think it's empty and returning."); |
donatien | 0:3b2f052c333b | 136 | return NET_EMPTY; //No message to read |
donatien | 0:3b2f052c333b | 137 | } |
donatien | 0:3b2f052c333b | 138 | |
donatien | 0:3b2f052c333b | 139 | //Prepare infos |
donatien | 0:3b2f052c333b | 140 | m_state = SMS_GET_CMD_SENT; |
donatien | 0:3b2f052c333b | 141 | m_msisdn = (char*) number; |
donatien | 0:3b2f052c333b | 142 | m_msg = (char*) message; |
donatien | 0:3b2f052c333b | 143 | m_maxMsgLength = maxLength; |
donatien | 0:3b2f052c333b | 144 | |
donatien | 0:3b2f052c333b | 145 | DBG("Get SMS"); |
donatien | 0:3b2f052c333b | 146 | //List command |
donatien | 0:3b2f052c333b | 147 | char cmd[32]; |
donatien | 0:3b2f052c333b | 148 | std::sprintf(cmd, "AT+CMGR=%d", m_msgRefList[0]); |
donatien | 0:3b2f052c333b | 149 | ret = m_pIf->execute(cmd, this, NULL, DEFAULT_TIMEOUT); |
donatien | 0:3b2f052c333b | 150 | if( ret != OK ) |
donatien | 0:3b2f052c333b | 151 | { |
donatien | 0:3b2f052c333b | 152 | WARN("AT+CMGR returned %d", ret); |
donatien | 0:3b2f052c333b | 153 | m_state = SMS_IDLE; |
donatien | 0:3b2f052c333b | 154 | m_inboxMtx.unlock(); |
donatien | 0:3b2f052c333b | 155 | return NET_PROTOCOL; |
donatien | 0:3b2f052c333b | 156 | } |
donatien | 0:3b2f052c333b | 157 | |
donatien | 0:3b2f052c333b | 158 | if (m_state != SMS_CMD_PROCESSED) |
donatien | 0:3b2f052c333b | 159 | { |
nherriot | 39:85dc5ca10b7f | 160 | WARN(" State variable is not 'SMS_CMD_PROCESSED' - returning 'NET_EMPTY'"); |
donatien | 0:3b2f052c333b | 161 | m_state = SMS_IDLE; |
donatien | 0:3b2f052c333b | 162 | m_inboxMtx.unlock(); |
donatien | 0:3b2f052c333b | 163 | return NET_EMPTY; |
donatien | 0:3b2f052c333b | 164 | } |
donatien | 0:3b2f052c333b | 165 | |
donatien | 0:3b2f052c333b | 166 | m_state = SMS_IDLE; |
donatien | 0:3b2f052c333b | 167 | |
ashleymills | 42:daa4dd282103 | 168 | DBG("Deleting message from index number: %d", m_msgRefList[0] ); |
donatien | 0:3b2f052c333b | 169 | //Delete message from outbox |
donatien | 0:3b2f052c333b | 170 | std::sprintf(cmd, "AT+CMGD=%d", m_msgRefList[0]); |
donatien | 0:3b2f052c333b | 171 | ret = m_pIf->executeSimple(cmd, NULL, DEFAULT_TIMEOUT); |
donatien | 0:3b2f052c333b | 172 | if(ret != OK) |
donatien | 0:3b2f052c333b | 173 | { |
donatien | 0:3b2f052c333b | 174 | m_inboxMtx.unlock(); |
donatien | 0:3b2f052c333b | 175 | WARN("Could not delete message"); |
donatien | 0:3b2f052c333b | 176 | } |
nherriot | 39:85dc5ca10b7f | 177 | else |
nherriot | 39:85dc5ca10b7f | 178 | { |
donatien | 0:3b2f052c333b | 179 | //Remove message from list |
donatien | 0:3b2f052c333b | 180 | std::memmove(m_msgRefList, m_msgRefList+1, m_msgRefListCount-1); |
donatien | 0:3b2f052c333b | 181 | m_msgRefListCount--; |
nherriot | 39:85dc5ca10b7f | 182 | } |
donatien | 0:3b2f052c333b | 183 | m_inboxMtx.unlock(); |
donatien | 0:3b2f052c333b | 184 | |
donatien | 0:3b2f052c333b | 185 | return OK; |
donatien | 0:3b2f052c333b | 186 | } |
donatien | 0:3b2f052c333b | 187 | |
donatien | 0:3b2f052c333b | 188 | |
donatien | 0:3b2f052c333b | 189 | int SMSInterface::getCount(size_t* pCount) |
donatien | 0:3b2f052c333b | 190 | { |
donatien | 0:3b2f052c333b | 191 | int ret; |
donatien | 0:3b2f052c333b | 192 | |
donatien | 0:3b2f052c333b | 193 | m_inboxMtx.lock(); |
donatien | 0:3b2f052c333b | 194 | if( m_needsUpdate ) |
donatien | 0:3b2f052c333b | 195 | { |
donatien | 0:3b2f052c333b | 196 | ret = updateInbox(); |
donatien | 0:3b2f052c333b | 197 | if(ret) |
donatien | 0:3b2f052c333b | 198 | { |
donatien | 0:3b2f052c333b | 199 | m_inboxMtx.unlock(); |
donatien | 0:3b2f052c333b | 200 | return NET_PROTOCOL; |
donatien | 0:3b2f052c333b | 201 | } |
donatien | 0:3b2f052c333b | 202 | } |
donatien | 0:3b2f052c333b | 203 | |
donatien | 0:3b2f052c333b | 204 | *pCount = m_msgRefListCount; |
donatien | 0:3b2f052c333b | 205 | m_inboxMtx.unlock(); |
donatien | 0:3b2f052c333b | 206 | |
donatien | 0:3b2f052c333b | 207 | return OK; |
donatien | 0:3b2f052c333b | 208 | } |
donatien | 0:3b2f052c333b | 209 | |
donatien | 0:3b2f052c333b | 210 | |
donatien | 0:3b2f052c333b | 211 | /*virtual*/ int SMSInterface::onNewATResponseLine(ATCommandsInterface* pInst, const char* line) |
donatien | 0:3b2f052c333b | 212 | { |
donatien | 0:3b2f052c333b | 213 | if(m_state == SMS_SEND_CMD_SENT) |
donatien | 0:3b2f052c333b | 214 | { |
donatien | 0:3b2f052c333b | 215 | if( std::sscanf(line, "+CMGS: %*d") == 0 ) |
donatien | 0:3b2f052c333b | 216 | { |
donatien | 0:3b2f052c333b | 217 | DBG("SM sent"); |
donatien | 0:3b2f052c333b | 218 | m_state = SMS_CMD_PROCESSED; |
donatien | 0:3b2f052c333b | 219 | } |
donatien | 0:3b2f052c333b | 220 | } |
donatien | 0:3b2f052c333b | 221 | else if(m_state == SMS_GET_CMD_SENT) |
donatien | 0:3b2f052c333b | 222 | { |
donatien | 0:3b2f052c333b | 223 | DBG("Header: %s", line); |
donatien | 0:3b2f052c333b | 224 | if( std::sscanf(line, "+CMGR: %*[^,],\"%16[^\"]\"", m_msisdn) == 1 ) //Get message ref |
donatien | 0:3b2f052c333b | 225 | { |
donatien | 0:3b2f052c333b | 226 | m_state = SMS_GET_HDR_RECEIVED; |
donatien | 0:3b2f052c333b | 227 | } |
donatien | 0:3b2f052c333b | 228 | } |
donatien | 0:3b2f052c333b | 229 | else if(m_state == SMS_GET_HDR_RECEIVED) |
donatien | 0:3b2f052c333b | 230 | { |
donatien | 0:3b2f052c333b | 231 | DBG("Message: %s", line); |
donatien | 0:3b2f052c333b | 232 | size_t cpyLen = MIN( std::strlen(line), m_maxMsgLength - 1 ); |
donatien | 0:3b2f052c333b | 233 | std::memcpy( m_msg, line, cpyLen ); |
donatien | 0:3b2f052c333b | 234 | m_msg[cpyLen] = '\0'; |
donatien | 0:3b2f052c333b | 235 | m_state = SMS_CMD_PROCESSED; |
donatien | 0:3b2f052c333b | 236 | } |
donatien | 0:3b2f052c333b | 237 | else if(m_state == SMS_GET_COUNT_CMD_SENT) |
donatien | 0:3b2f052c333b | 238 | { |
donatien | 0:3b2f052c333b | 239 | DBG("Header: %s", line); |
donatien | 0:3b2f052c333b | 240 | int msgRef; |
donatien | 28:c94ffb45a848 | 241 | if( std::sscanf(line, "+CMGL: %d,\"REC", &msgRef) == 1 ) //Filter on REC READ and REC UNREAD messages |
donatien | 0:3b2f052c333b | 242 | { |
donatien | 0:3b2f052c333b | 243 | m_state = SMS_GET_COUNT_HDR_RECEIVED; |
donatien | 0:3b2f052c333b | 244 | //Add message to list |
donatien | 0:3b2f052c333b | 245 | if(m_msgRefListCount < MAX_SM) |
donatien | 0:3b2f052c333b | 246 | { |
donatien | 0:3b2f052c333b | 247 | m_msgRefList[m_msgRefListCount] = msgRef; |
donatien | 0:3b2f052c333b | 248 | } |
donatien | 0:3b2f052c333b | 249 | m_msgRefListCount++; //Always count message |
donatien | 0:3b2f052c333b | 250 | DBG("m_msgRefListCount=%d",m_msgRefListCount); |
donatien | 0:3b2f052c333b | 251 | } |
donatien | 0:3b2f052c333b | 252 | } |
donatien | 0:3b2f052c333b | 253 | else if(m_state == SMS_GET_COUNT_HDR_RECEIVED) |
donatien | 0:3b2f052c333b | 254 | { |
donatien | 0:3b2f052c333b | 255 | DBG("Message (debug only): %s", line); //For debug only |
donatien | 0:3b2f052c333b | 256 | m_state = SMS_GET_COUNT_CMD_SENT; |
donatien | 0:3b2f052c333b | 257 | } |
donatien | 0:3b2f052c333b | 258 | return OK; |
donatien | 0:3b2f052c333b | 259 | } |
donatien | 0:3b2f052c333b | 260 | |
donatien | 0:3b2f052c333b | 261 | /*virtual*/ int SMSInterface::onNewEntryPrompt(ATCommandsInterface* pInst) |
donatien | 0:3b2f052c333b | 262 | { |
donatien | 0:3b2f052c333b | 263 | if(m_state == SMS_SEND_CMD_SENT) |
donatien | 0:3b2f052c333b | 264 | { |
donatien | 0:3b2f052c333b | 265 | char* crPtr = strchr(m_msg, CR); |
donatien | 0:3b2f052c333b | 266 | if(crPtr != NULL) |
donatien | 0:3b2f052c333b | 267 | { |
donatien | 0:3b2f052c333b | 268 | int crPos = crPtr - m_msg; |
donatien | 0:3b2f052c333b | 269 | //Replace m_inputBuf[crPos] with null-terminating char |
donatien | 0:3b2f052c333b | 270 | m_msg[crPos] = '\x0'; |
donatien | 0:3b2f052c333b | 271 | |
donatien | 0:3b2f052c333b | 272 | //If there is a CR char, split message there |
donatien | 0:3b2f052c333b | 273 | |
donatien | 0:3b2f052c333b | 274 | //Do print the message |
donatien | 0:3b2f052c333b | 275 | int ret = pInst->sendData(m_msg); |
donatien | 0:3b2f052c333b | 276 | if(ret) |
donatien | 0:3b2f052c333b | 277 | { |
donatien | 0:3b2f052c333b | 278 | return ret; |
donatien | 0:3b2f052c333b | 279 | } |
donatien | 0:3b2f052c333b | 280 | |
donatien | 0:3b2f052c333b | 281 | char cr[2] = {CR, '\0'}; |
donatien | 0:3b2f052c333b | 282 | ret = pInst->sendData(cr); |
donatien | 0:3b2f052c333b | 283 | if(ret) |
donatien | 0:3b2f052c333b | 284 | { |
donatien | 0:3b2f052c333b | 285 | return ret; |
donatien | 0:3b2f052c333b | 286 | } |
donatien | 0:3b2f052c333b | 287 | |
donatien | 0:3b2f052c333b | 288 | m_msg += crPos; |
donatien | 0:3b2f052c333b | 289 | |
donatien | 0:3b2f052c333b | 290 | if(m_msg[0] == LF) |
donatien | 0:3b2f052c333b | 291 | { |
donatien | 0:3b2f052c333b | 292 | m_msg++; //Discard LF char as well |
donatien | 0:3b2f052c333b | 293 | } |
donatien | 0:3b2f052c333b | 294 | |
donatien | 0:3b2f052c333b | 295 | return NET_MOREINFO; |
donatien | 0:3b2f052c333b | 296 | } |
donatien | 0:3b2f052c333b | 297 | else |
donatien | 0:3b2f052c333b | 298 | { |
donatien | 0:3b2f052c333b | 299 | //Do print the message |
donatien | 0:3b2f052c333b | 300 | pInst->sendData(m_msg); |
donatien | 0:3b2f052c333b | 301 | return OK; |
donatien | 0:3b2f052c333b | 302 | } |
donatien | 0:3b2f052c333b | 303 | } |
donatien | 0:3b2f052c333b | 304 | |
donatien | 0:3b2f052c333b | 305 | return OK; |
donatien | 0:3b2f052c333b | 306 | } |
donatien | 0:3b2f052c333b | 307 | |
donatien | 0:3b2f052c333b | 308 | /*virtual*/ bool SMSInterface::isATCodeHandled(const char* atCode) //Is this AT code handled |
donatien | 0:3b2f052c333b | 309 | { |
donatien | 0:3b2f052c333b | 310 | DBG("AT code is %s", atCode); |
donatien | 0:3b2f052c333b | 311 | if( strcmp("+CMTI", atCode) == 0 ) |
donatien | 0:3b2f052c333b | 312 | { |
donatien | 0:3b2f052c333b | 313 | return true; |
donatien | 0:3b2f052c333b | 314 | } |
donatien | 0:3b2f052c333b | 315 | |
donatien | 0:3b2f052c333b | 316 | DBG("Not handled"); |
donatien | 0:3b2f052c333b | 317 | return false; |
donatien | 0:3b2f052c333b | 318 | } |
donatien | 0:3b2f052c333b | 319 | |
donatien | 0:3b2f052c333b | 320 | /*virtual*/ void SMSInterface::onDispatchStart() |
donatien | 0:3b2f052c333b | 321 | { |
donatien | 0:3b2f052c333b | 322 | |
donatien | 0:3b2f052c333b | 323 | |
donatien | 0:3b2f052c333b | 324 | } |
donatien | 0:3b2f052c333b | 325 | |
donatien | 0:3b2f052c333b | 326 | /*virtual*/ void SMSInterface::onDispatchStop() |
donatien | 0:3b2f052c333b | 327 | { |
donatien | 0:3b2f052c333b | 328 | |
donatien | 0:3b2f052c333b | 329 | } |
donatien | 0:3b2f052c333b | 330 | |
donatien | 0:3b2f052c333b | 331 | /*virtual*/ void SMSInterface::onEvent(const char* atCode, const char* evt) |
donatien | 0:3b2f052c333b | 332 | { |
donatien | 0:3b2f052c333b | 333 | if( strcmp("+CMTI", atCode) != 0 ) |
donatien | 0:3b2f052c333b | 334 | { |
donatien | 0:3b2f052c333b | 335 | return; //Not supported |
donatien | 0:3b2f052c333b | 336 | } |
donatien | 0:3b2f052c333b | 337 | |
donatien | 0:3b2f052c333b | 338 | DBG("Unsollicited result code: %s - %s", atCode, evt); |
donatien | 0:3b2f052c333b | 339 | |
donatien | 0:3b2f052c333b | 340 | //Get index |
donatien | 0:3b2f052c333b | 341 | int msgRef; |
donatien | 0:3b2f052c333b | 342 | if( std::sscanf(evt, "\"SM\",%d", &msgRef) == 1 ) |
donatien | 0:3b2f052c333b | 343 | { |
donatien | 0:3b2f052c333b | 344 | DBG("Adding message to list (ref %d)", msgRef); |
donatien | 0:3b2f052c333b | 345 | m_inboxMtx.lock(); |
donatien | 0:3b2f052c333b | 346 | //Add message to list |
donatien | 0:3b2f052c333b | 347 | if(m_msgRefListCount < MAX_SM) |
donatien | 0:3b2f052c333b | 348 | { |
donatien | 0:3b2f052c333b | 349 | m_msgRefList[m_msgRefListCount] = msgRef; |
donatien | 0:3b2f052c333b | 350 | } |
donatien | 0:3b2f052c333b | 351 | else |
donatien | 0:3b2f052c333b | 352 | { |
donatien | 0:3b2f052c333b | 353 | m_needsUpdate = true; |
donatien | 0:3b2f052c333b | 354 | } |
donatien | 0:3b2f052c333b | 355 | m_msgRefListCount++; //Always count message |
donatien | 0:3b2f052c333b | 356 | m_inboxMtx.unlock(); |
donatien | 0:3b2f052c333b | 357 | } |
donatien | 0:3b2f052c333b | 358 | } |
donatien | 0:3b2f052c333b | 359 | |
donatien | 0:3b2f052c333b | 360 | int SMSInterface::updateInbox() |
donatien | 0:3b2f052c333b | 361 | { |
donatien | 0:3b2f052c333b | 362 | //Get memory indexes of unread messages |
donatien | 0:3b2f052c333b | 363 | |
donatien | 0:3b2f052c333b | 364 | DBG("Updating inbox"); |
donatien | 0:3b2f052c333b | 365 | m_msgRefListCount = 0; //Reset list |
donatien | 0:3b2f052c333b | 366 | |
donatien | 28:c94ffb45a848 | 367 | //First list the "REC READ" messages that were not processed in the previous session |
donatien | 28:c94ffb45a848 | 368 | m_state = SMS_GET_COUNT_CMD_SENT; |
donatien | 28:c94ffb45a848 | 369 | int ret = m_pIf->execute("AT+CMGL=\"REC READ\"", this, NULL, DEFAULT_TIMEOUT); |
donatien | 28:c94ffb45a848 | 370 | if( ret != OK ) |
donatien | 28:c94ffb45a848 | 371 | { |
donatien | 28:c94ffb45a848 | 372 | WARN("AT+CMGL returned %d", ret); |
donatien | 28:c94ffb45a848 | 373 | m_state = SMS_IDLE; |
donatien | 28:c94ffb45a848 | 374 | m_msgRefListCount = 0; //List could be invalid |
donatien | 28:c94ffb45a848 | 375 | m_needsUpdate = true; |
donatien | 28:c94ffb45a848 | 376 | return NET_PROTOCOL; |
donatien | 28:c94ffb45a848 | 377 | } |
donatien | 28:c94ffb45a848 | 378 | |
donatien | 28:c94ffb45a848 | 379 | //Now list the "REC UNREAD" messages that were received modem since |
donatien | 28:c94ffb45a848 | 380 | m_state = SMS_GET_COUNT_CMD_SENT; |
donatien | 28:c94ffb45a848 | 381 | ret = m_pIf->execute("AT+CMGL=\"REC UNREAD\"", this, NULL, DEFAULT_TIMEOUT); |
donatien | 0:3b2f052c333b | 382 | if( ret != OK ) |
donatien | 0:3b2f052c333b | 383 | { |
donatien | 0:3b2f052c333b | 384 | WARN("AT+CMGL returned %d", ret); |
donatien | 0:3b2f052c333b | 385 | m_state = SMS_IDLE; |
donatien | 0:3b2f052c333b | 386 | m_msgRefListCount = 0; //List could be invalid |
donatien | 0:3b2f052c333b | 387 | m_needsUpdate = true; |
donatien | 0:3b2f052c333b | 388 | return NET_PROTOCOL; |
donatien | 0:3b2f052c333b | 389 | } |
donatien | 0:3b2f052c333b | 390 | |
donatien | 0:3b2f052c333b | 391 | DBG("%d incoming messages in inbox", m_msgRefListCount); |
donatien | 0:3b2f052c333b | 392 | |
donatien | 0:3b2f052c333b | 393 | if( m_msgRefListCount > MAX_SM ) |
donatien | 0:3b2f052c333b | 394 | { |
donatien | 0:3b2f052c333b | 395 | m_needsUpdate = true; |
donatien | 0:3b2f052c333b | 396 | } |
donatien | 0:3b2f052c333b | 397 | else |
donatien | 0:3b2f052c333b | 398 | { |
donatien | 0:3b2f052c333b | 399 | m_needsUpdate = false; |
donatien | 0:3b2f052c333b | 400 | } |
donatien | 0:3b2f052c333b | 401 | |
donatien | 0:3b2f052c333b | 402 | m_state = SMS_IDLE; |
donatien | 0:3b2f052c333b | 403 | |
donatien | 0:3b2f052c333b | 404 | return OK; |
donatien | 0:3b2f052c333b | 405 | } |
donatien | 0:3b2f052c333b | 406 | |
donatien | 0:3b2f052c333b | 407 |