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@58:f1965f3f4504, 2012-10-22 (annotated)
- Committer:
- donatien
- Date:
- Mon Oct 22 15:47:01 2012 +0000
- Revision:
- 58:f1965f3f4504
- Parent:
- 50:8ad4cb12749d
- Child:
- 59:593fb493172f
Fixed messages indexes' list shifting bug
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 | 46:c2282539c858 | 20 | #define __DEBUG__ 2 |
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 | 58:f1965f3f4504 | 120 | if( ((m_msgRefListCount == 0) && m_needsUpdate) || ((m_msgRefListCount > 0) && (m_msgRefList[0] == -1)) ) |
donatien | 0:3b2f052c333b | 121 | { |
donatien | 58:f1965f3f4504 | 122 | DBG("Message list count is 0 and needs updating or next index is unknown, calling 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 | 58:f1965f3f4504 | 131 | |
donatien | 58:f1965f3f4504 | 132 | DBG("%d messages to read", m_msgRefListCount); |
donatien | 0:3b2f052c333b | 133 | |
donatien | 0:3b2f052c333b | 134 | if(m_msgRefListCount == 0) |
donatien | 0:3b2f052c333b | 135 | { |
donatien | 0:3b2f052c333b | 136 | m_inboxMtx.unlock(); |
nherriot | 39:85dc5ca10b7f | 137 | DBG("Message list count is 0, I think it's empty and returning."); |
donatien | 0:3b2f052c333b | 138 | return NET_EMPTY; //No message to read |
donatien | 0:3b2f052c333b | 139 | } |
donatien | 0:3b2f052c333b | 140 | |
donatien | 0:3b2f052c333b | 141 | //Prepare infos |
donatien | 0:3b2f052c333b | 142 | m_state = SMS_GET_CMD_SENT; |
donatien | 0:3b2f052c333b | 143 | m_msisdn = (char*) number; |
donatien | 0:3b2f052c333b | 144 | m_msg = (char*) message; |
donatien | 0:3b2f052c333b | 145 | m_maxMsgLength = maxLength; |
donatien | 0:3b2f052c333b | 146 | |
donatien | 0:3b2f052c333b | 147 | DBG("Get SMS"); |
donatien | 0:3b2f052c333b | 148 | //List command |
donatien | 0:3b2f052c333b | 149 | char cmd[32]; |
donatien | 0:3b2f052c333b | 150 | std::sprintf(cmd, "AT+CMGR=%d", m_msgRefList[0]); |
donatien | 0:3b2f052c333b | 151 | ret = m_pIf->execute(cmd, this, NULL, DEFAULT_TIMEOUT); |
donatien | 0:3b2f052c333b | 152 | if( ret != OK ) |
donatien | 0:3b2f052c333b | 153 | { |
donatien | 0:3b2f052c333b | 154 | WARN("AT+CMGR returned %d", ret); |
donatien | 0:3b2f052c333b | 155 | m_state = SMS_IDLE; |
donatien | 0:3b2f052c333b | 156 | m_inboxMtx.unlock(); |
donatien | 0:3b2f052c333b | 157 | return NET_PROTOCOL; |
donatien | 0:3b2f052c333b | 158 | } |
donatien | 0:3b2f052c333b | 159 | |
donatien | 0:3b2f052c333b | 160 | if (m_state != SMS_CMD_PROCESSED) |
donatien | 0:3b2f052c333b | 161 | { |
donatien | 50:8ad4cb12749d | 162 | WARN("State variable is not 'SMS_CMD_PROCESSED' - returning 'NET_EMPTY'"); |
donatien | 0:3b2f052c333b | 163 | } |
donatien | 0:3b2f052c333b | 164 | |
ashleymills | 42:daa4dd282103 | 165 | DBG("Deleting message from index number: %d", m_msgRefList[0] ); |
donatien | 0:3b2f052c333b | 166 | //Delete message from outbox |
donatien | 0:3b2f052c333b | 167 | std::sprintf(cmd, "AT+CMGD=%d", m_msgRefList[0]); |
donatien | 0:3b2f052c333b | 168 | ret = m_pIf->executeSimple(cmd, NULL, DEFAULT_TIMEOUT); |
donatien | 0:3b2f052c333b | 169 | if(ret != OK) |
donatien | 0:3b2f052c333b | 170 | { |
donatien | 50:8ad4cb12749d | 171 | ERR("Could not delete message"); |
donatien | 0:3b2f052c333b | 172 | } |
donatien | 0:3b2f052c333b | 173 | //Remove message from list |
donatien | 58:f1965f3f4504 | 174 | std::memmove(&m_msgRefList[0], &m_msgRefList[1], MIN(m_msgRefListCount-1,MAX_SM-1)*sizeof(m_msgRefList[0])); |
donatien | 0:3b2f052c333b | 175 | m_msgRefListCount--; |
donatien | 50:8ad4cb12749d | 176 | |
donatien | 58:f1965f3f4504 | 177 | if(m_msgRefListCount > MAX_SM - 1) //Last message index is unknown, so put -1 to tell the lib to fetch it when needed |
donatien | 58:f1965f3f4504 | 178 | { |
donatien | 58:f1965f3f4504 | 179 | DBG("Last message index is unknown, will need to be updated"); |
donatien | 58:f1965f3f4504 | 180 | m_msgRefList[MAX_SM - 1] = -1; |
donatien | 58:f1965f3f4504 | 181 | } |
donatien | 58:f1965f3f4504 | 182 | |
donatien | 58:f1965f3f4504 | 183 | DBG("%d messages to read", m_msgRefListCount); |
donatien | 58:f1965f3f4504 | 184 | |
donatien | 50:8ad4cb12749d | 185 | if (m_state != SMS_CMD_PROCESSED) |
donatien | 50:8ad4cb12749d | 186 | { |
donatien | 50:8ad4cb12749d | 187 | m_state = SMS_IDLE; |
donatien | 50:8ad4cb12749d | 188 | m_inboxMtx.unlock(); |
donatien | 50:8ad4cb12749d | 189 | return NET_EMPTY; |
donatien | 50:8ad4cb12749d | 190 | } |
donatien | 50:8ad4cb12749d | 191 | |
donatien | 50:8ad4cb12749d | 192 | m_state = SMS_IDLE; |
donatien | 0:3b2f052c333b | 193 | m_inboxMtx.unlock(); |
donatien | 0:3b2f052c333b | 194 | |
donatien | 0:3b2f052c333b | 195 | return OK; |
donatien | 0:3b2f052c333b | 196 | } |
donatien | 0:3b2f052c333b | 197 | |
donatien | 0:3b2f052c333b | 198 | |
donatien | 0:3b2f052c333b | 199 | int SMSInterface::getCount(size_t* pCount) |
donatien | 0:3b2f052c333b | 200 | { |
donatien | 0:3b2f052c333b | 201 | int ret; |
donatien | 0:3b2f052c333b | 202 | |
donatien | 0:3b2f052c333b | 203 | m_inboxMtx.lock(); |
donatien | 0:3b2f052c333b | 204 | if( m_needsUpdate ) |
donatien | 0:3b2f052c333b | 205 | { |
donatien | 0:3b2f052c333b | 206 | ret = updateInbox(); |
donatien | 0:3b2f052c333b | 207 | if(ret) |
donatien | 0:3b2f052c333b | 208 | { |
donatien | 0:3b2f052c333b | 209 | m_inboxMtx.unlock(); |
donatien | 0:3b2f052c333b | 210 | return NET_PROTOCOL; |
donatien | 0:3b2f052c333b | 211 | } |
donatien | 0:3b2f052c333b | 212 | } |
donatien | 0:3b2f052c333b | 213 | |
donatien | 0:3b2f052c333b | 214 | *pCount = m_msgRefListCount; |
donatien | 0:3b2f052c333b | 215 | m_inboxMtx.unlock(); |
donatien | 0:3b2f052c333b | 216 | |
donatien | 0:3b2f052c333b | 217 | return OK; |
donatien | 0:3b2f052c333b | 218 | } |
donatien | 0:3b2f052c333b | 219 | |
donatien | 0:3b2f052c333b | 220 | |
donatien | 0:3b2f052c333b | 221 | /*virtual*/ int SMSInterface::onNewATResponseLine(ATCommandsInterface* pInst, const char* line) |
donatien | 0:3b2f052c333b | 222 | { |
donatien | 0:3b2f052c333b | 223 | if(m_state == SMS_SEND_CMD_SENT) |
donatien | 0:3b2f052c333b | 224 | { |
donatien | 0:3b2f052c333b | 225 | if( std::sscanf(line, "+CMGS: %*d") == 0 ) |
donatien | 0:3b2f052c333b | 226 | { |
donatien | 0:3b2f052c333b | 227 | DBG("SM sent"); |
donatien | 0:3b2f052c333b | 228 | m_state = SMS_CMD_PROCESSED; |
donatien | 0:3b2f052c333b | 229 | } |
donatien | 0:3b2f052c333b | 230 | } |
donatien | 0:3b2f052c333b | 231 | else if(m_state == SMS_GET_CMD_SENT) |
donatien | 0:3b2f052c333b | 232 | { |
donatien | 0:3b2f052c333b | 233 | DBG("Header: %s", line); |
donatien | 0:3b2f052c333b | 234 | if( std::sscanf(line, "+CMGR: %*[^,],\"%16[^\"]\"", m_msisdn) == 1 ) //Get message ref |
donatien | 0:3b2f052c333b | 235 | { |
donatien | 0:3b2f052c333b | 236 | m_state = SMS_GET_HDR_RECEIVED; |
donatien | 0:3b2f052c333b | 237 | } |
donatien | 0:3b2f052c333b | 238 | } |
donatien | 0:3b2f052c333b | 239 | else if(m_state == SMS_GET_HDR_RECEIVED) |
donatien | 0:3b2f052c333b | 240 | { |
donatien | 0:3b2f052c333b | 241 | DBG("Message: %s", line); |
donatien | 0:3b2f052c333b | 242 | size_t cpyLen = MIN( std::strlen(line), m_maxMsgLength - 1 ); |
donatien | 0:3b2f052c333b | 243 | std::memcpy( m_msg, line, cpyLen ); |
donatien | 0:3b2f052c333b | 244 | m_msg[cpyLen] = '\0'; |
donatien | 0:3b2f052c333b | 245 | m_state = SMS_CMD_PROCESSED; |
donatien | 0:3b2f052c333b | 246 | } |
donatien | 0:3b2f052c333b | 247 | else if(m_state == SMS_GET_COUNT_CMD_SENT) |
donatien | 0:3b2f052c333b | 248 | { |
donatien | 0:3b2f052c333b | 249 | DBG("Header: %s", line); |
donatien | 0:3b2f052c333b | 250 | int msgRef; |
donatien | 28:c94ffb45a848 | 251 | if( std::sscanf(line, "+CMGL: %d,\"REC", &msgRef) == 1 ) //Filter on REC READ and REC UNREAD messages |
donatien | 0:3b2f052c333b | 252 | { |
donatien | 0:3b2f052c333b | 253 | m_state = SMS_GET_COUNT_HDR_RECEIVED; |
donatien | 0:3b2f052c333b | 254 | //Add message to list |
donatien | 0:3b2f052c333b | 255 | if(m_msgRefListCount < MAX_SM) |
donatien | 0:3b2f052c333b | 256 | { |
donatien | 0:3b2f052c333b | 257 | m_msgRefList[m_msgRefListCount] = msgRef; |
donatien | 0:3b2f052c333b | 258 | } |
donatien | 0:3b2f052c333b | 259 | m_msgRefListCount++; //Always count message |
donatien | 0:3b2f052c333b | 260 | DBG("m_msgRefListCount=%d",m_msgRefListCount); |
donatien | 0:3b2f052c333b | 261 | } |
donatien | 0:3b2f052c333b | 262 | } |
donatien | 0:3b2f052c333b | 263 | else if(m_state == SMS_GET_COUNT_HDR_RECEIVED) |
donatien | 0:3b2f052c333b | 264 | { |
donatien | 0:3b2f052c333b | 265 | DBG("Message (debug only): %s", line); //For debug only |
donatien | 0:3b2f052c333b | 266 | m_state = SMS_GET_COUNT_CMD_SENT; |
donatien | 0:3b2f052c333b | 267 | } |
donatien | 0:3b2f052c333b | 268 | return OK; |
donatien | 0:3b2f052c333b | 269 | } |
donatien | 0:3b2f052c333b | 270 | |
donatien | 0:3b2f052c333b | 271 | /*virtual*/ int SMSInterface::onNewEntryPrompt(ATCommandsInterface* pInst) |
donatien | 0:3b2f052c333b | 272 | { |
donatien | 0:3b2f052c333b | 273 | if(m_state == SMS_SEND_CMD_SENT) |
donatien | 0:3b2f052c333b | 274 | { |
donatien | 0:3b2f052c333b | 275 | char* crPtr = strchr(m_msg, CR); |
donatien | 0:3b2f052c333b | 276 | if(crPtr != NULL) |
donatien | 0:3b2f052c333b | 277 | { |
donatien | 0:3b2f052c333b | 278 | int crPos = crPtr - m_msg; |
donatien | 0:3b2f052c333b | 279 | //Replace m_inputBuf[crPos] with null-terminating char |
donatien | 0:3b2f052c333b | 280 | m_msg[crPos] = '\x0'; |
donatien | 0:3b2f052c333b | 281 | |
donatien | 0:3b2f052c333b | 282 | //If there is a CR char, split message there |
donatien | 0:3b2f052c333b | 283 | |
donatien | 0:3b2f052c333b | 284 | //Do print the message |
donatien | 0:3b2f052c333b | 285 | int ret = pInst->sendData(m_msg); |
donatien | 0:3b2f052c333b | 286 | if(ret) |
donatien | 0:3b2f052c333b | 287 | { |
donatien | 0:3b2f052c333b | 288 | return ret; |
donatien | 0:3b2f052c333b | 289 | } |
donatien | 0:3b2f052c333b | 290 | |
donatien | 0:3b2f052c333b | 291 | char cr[2] = {CR, '\0'}; |
donatien | 0:3b2f052c333b | 292 | ret = pInst->sendData(cr); |
donatien | 0:3b2f052c333b | 293 | if(ret) |
donatien | 0:3b2f052c333b | 294 | { |
donatien | 0:3b2f052c333b | 295 | return ret; |
donatien | 0:3b2f052c333b | 296 | } |
donatien | 0:3b2f052c333b | 297 | |
donatien | 0:3b2f052c333b | 298 | m_msg += crPos; |
donatien | 0:3b2f052c333b | 299 | |
donatien | 0:3b2f052c333b | 300 | if(m_msg[0] == LF) |
donatien | 0:3b2f052c333b | 301 | { |
donatien | 0:3b2f052c333b | 302 | m_msg++; //Discard LF char as well |
donatien | 0:3b2f052c333b | 303 | } |
donatien | 0:3b2f052c333b | 304 | |
donatien | 0:3b2f052c333b | 305 | return NET_MOREINFO; |
donatien | 0:3b2f052c333b | 306 | } |
donatien | 0:3b2f052c333b | 307 | else |
donatien | 0:3b2f052c333b | 308 | { |
donatien | 0:3b2f052c333b | 309 | //Do print the message |
donatien | 0:3b2f052c333b | 310 | pInst->sendData(m_msg); |
donatien | 0:3b2f052c333b | 311 | return OK; |
donatien | 0:3b2f052c333b | 312 | } |
donatien | 0:3b2f052c333b | 313 | } |
donatien | 0:3b2f052c333b | 314 | |
donatien | 0:3b2f052c333b | 315 | return OK; |
donatien | 0:3b2f052c333b | 316 | } |
donatien | 0:3b2f052c333b | 317 | |
donatien | 0:3b2f052c333b | 318 | /*virtual*/ bool SMSInterface::isATCodeHandled(const char* atCode) //Is this AT code handled |
donatien | 0:3b2f052c333b | 319 | { |
donatien | 0:3b2f052c333b | 320 | DBG("AT code is %s", atCode); |
donatien | 0:3b2f052c333b | 321 | if( strcmp("+CMTI", atCode) == 0 ) |
donatien | 0:3b2f052c333b | 322 | { |
donatien | 0:3b2f052c333b | 323 | return true; |
donatien | 0:3b2f052c333b | 324 | } |
donatien | 0:3b2f052c333b | 325 | |
donatien | 0:3b2f052c333b | 326 | DBG("Not handled"); |
donatien | 0:3b2f052c333b | 327 | return false; |
donatien | 0:3b2f052c333b | 328 | } |
donatien | 0:3b2f052c333b | 329 | |
donatien | 0:3b2f052c333b | 330 | /*virtual*/ void SMSInterface::onDispatchStart() |
donatien | 0:3b2f052c333b | 331 | { |
donatien | 0:3b2f052c333b | 332 | |
donatien | 0:3b2f052c333b | 333 | |
donatien | 0:3b2f052c333b | 334 | } |
donatien | 0:3b2f052c333b | 335 | |
donatien | 0:3b2f052c333b | 336 | /*virtual*/ void SMSInterface::onDispatchStop() |
donatien | 0:3b2f052c333b | 337 | { |
donatien | 0:3b2f052c333b | 338 | |
donatien | 0:3b2f052c333b | 339 | } |
donatien | 0:3b2f052c333b | 340 | |
donatien | 0:3b2f052c333b | 341 | /*virtual*/ void SMSInterface::onEvent(const char* atCode, const char* evt) |
donatien | 0:3b2f052c333b | 342 | { |
donatien | 0:3b2f052c333b | 343 | if( strcmp("+CMTI", atCode) != 0 ) |
donatien | 0:3b2f052c333b | 344 | { |
donatien | 0:3b2f052c333b | 345 | return; //Not supported |
donatien | 0:3b2f052c333b | 346 | } |
donatien | 0:3b2f052c333b | 347 | |
donatien | 0:3b2f052c333b | 348 | DBG("Unsollicited result code: %s - %s", atCode, evt); |
donatien | 0:3b2f052c333b | 349 | |
donatien | 0:3b2f052c333b | 350 | //Get index |
donatien | 0:3b2f052c333b | 351 | int msgRef; |
donatien | 0:3b2f052c333b | 352 | if( std::sscanf(evt, "\"SM\",%d", &msgRef) == 1 ) |
donatien | 0:3b2f052c333b | 353 | { |
donatien | 0:3b2f052c333b | 354 | DBG("Adding message to list (ref %d)", msgRef); |
donatien | 50:8ad4cb12749d | 355 | if(m_inboxMtx.trylock()) |
donatien | 0:3b2f052c333b | 356 | { |
donatien | 50:8ad4cb12749d | 357 | //Add message to list |
donatien | 50:8ad4cb12749d | 358 | if(m_msgRefListCount < MAX_SM) |
donatien | 50:8ad4cb12749d | 359 | { |
donatien | 50:8ad4cb12749d | 360 | m_msgRefList[m_msgRefListCount] = msgRef; |
donatien | 50:8ad4cb12749d | 361 | } |
donatien | 50:8ad4cb12749d | 362 | m_msgRefListCount++; //Always count message |
donatien | 50:8ad4cb12749d | 363 | m_inboxMtx.unlock(); |
donatien | 0:3b2f052c333b | 364 | } |
donatien | 0:3b2f052c333b | 365 | else |
donatien | 0:3b2f052c333b | 366 | { |
donatien | 50:8ad4cb12749d | 367 | WARN("Could not get lock"); |
donatien | 0:3b2f052c333b | 368 | m_needsUpdate = true; |
donatien | 0:3b2f052c333b | 369 | } |
donatien | 0:3b2f052c333b | 370 | } |
donatien | 0:3b2f052c333b | 371 | } |
donatien | 0:3b2f052c333b | 372 | |
donatien | 0:3b2f052c333b | 373 | int SMSInterface::updateInbox() |
donatien | 0:3b2f052c333b | 374 | { |
donatien | 0:3b2f052c333b | 375 | //Get memory indexes of unread messages |
donatien | 0:3b2f052c333b | 376 | |
donatien | 0:3b2f052c333b | 377 | DBG("Updating inbox"); |
donatien | 0:3b2f052c333b | 378 | m_msgRefListCount = 0; //Reset list |
donatien | 50:8ad4cb12749d | 379 | m_needsUpdate = false; //Assume we won't need update after this routine (can be set to true by an incoming SM event) |
donatien | 0:3b2f052c333b | 380 | |
donatien | 28:c94ffb45a848 | 381 | //First list the "REC READ" messages that were not processed in the previous session |
donatien | 28:c94ffb45a848 | 382 | m_state = SMS_GET_COUNT_CMD_SENT; |
donatien | 28:c94ffb45a848 | 383 | int ret = m_pIf->execute("AT+CMGL=\"REC READ\"", this, NULL, DEFAULT_TIMEOUT); |
donatien | 28:c94ffb45a848 | 384 | if( ret != OK ) |
donatien | 28:c94ffb45a848 | 385 | { |
donatien | 28:c94ffb45a848 | 386 | WARN("AT+CMGL returned %d", ret); |
donatien | 28:c94ffb45a848 | 387 | m_state = SMS_IDLE; |
donatien | 28:c94ffb45a848 | 388 | m_msgRefListCount = 0; //List could be invalid |
donatien | 28:c94ffb45a848 | 389 | m_needsUpdate = true; |
donatien | 28:c94ffb45a848 | 390 | return NET_PROTOCOL; |
donatien | 28:c94ffb45a848 | 391 | } |
donatien | 28:c94ffb45a848 | 392 | |
donatien | 50:8ad4cb12749d | 393 | //Now list the "REC UNREAD" messages that were received by the modem since |
donatien | 28:c94ffb45a848 | 394 | m_state = SMS_GET_COUNT_CMD_SENT; |
donatien | 28:c94ffb45a848 | 395 | ret = m_pIf->execute("AT+CMGL=\"REC UNREAD\"", this, NULL, DEFAULT_TIMEOUT); |
donatien | 0:3b2f052c333b | 396 | if( ret != OK ) |
donatien | 0:3b2f052c333b | 397 | { |
donatien | 0:3b2f052c333b | 398 | WARN("AT+CMGL returned %d", ret); |
donatien | 0:3b2f052c333b | 399 | m_state = SMS_IDLE; |
donatien | 0:3b2f052c333b | 400 | m_msgRefListCount = 0; //List could be invalid |
donatien | 0:3b2f052c333b | 401 | m_needsUpdate = true; |
donatien | 0:3b2f052c333b | 402 | return NET_PROTOCOL; |
donatien | 0:3b2f052c333b | 403 | } |
donatien | 0:3b2f052c333b | 404 | |
donatien | 0:3b2f052c333b | 405 | DBG("%d incoming messages in inbox", m_msgRefListCount); |
donatien | 0:3b2f052c333b | 406 | |
donatien | 0:3b2f052c333b | 407 | m_state = SMS_IDLE; |
donatien | 0:3b2f052c333b | 408 | |
donatien | 0:3b2f052c333b | 409 | return OK; |
donatien | 0:3b2f052c333b | 410 | } |
donatien | 0:3b2f052c333b | 411 | |
donatien | 0:3b2f052c333b | 412 |