Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Socket lwip-sys lwip
Fork of AbitUSBModem by
Diff: sms/SMSInterface.cpp
- Revision:
- 99:514e67a69ad6
- Parent:
- 97:7d9cc95e2ea7
--- a/sms/SMSInterface.cpp Wed Feb 18 15:32:57 2015 +0000
+++ b/sms/SMSInterface.cpp Wed Feb 25 14:34:13 2015 +0000
@@ -33,11 +33,13 @@
#include <cstring>
#define DEFAULT_TIMEOUT 10000
+#define SHORT_MAIL_CODE "128145000013"
SMSInterface::SMSInterface(ATCommandsInterface* pIf) : m_pIf(pIf), m_msg(NULL), m_maxMsgLength(0), m_msisdn(NULL)
{
DBG("registering sms");
m_pIf->registerEventsHandler(this); //Add us to the unsolicited result codes handlers
+ m_caller[0] = 0;
}
int SMSInterface::init()
@@ -51,6 +53,12 @@
{
return NET_PROTOCOL;
}
+ //Enable caller id
+ ret = m_pIf->executeSimple("AT#B1", NULL, DEFAULT_TIMEOUT);
+ if(ret != OK)
+ {
+ return NET_PROTOCOL;
+ }
DBG("Initialization done");
return OK;
@@ -69,7 +77,7 @@
//Send command
char cmd[300];
// set S register
- strcpy(cmd, "ATS202=128145000013");
+ strcpy(cmd, "ATS202=" SHORT_MAIL_CODE);
for (int i = 0; i < strlen(message); i ++) {
std::sprintf(&cmd[strlen(cmd)], "%03d", (unsigned char)message[i]);
}
@@ -108,6 +116,9 @@
{
return NET_INVALID; //Buffer too short
}
+ if (m_caller[0] == 0) {
+ return NET_INVALID; //No sms
+ }
int ret;
@@ -120,7 +131,7 @@
m_maxMsgLength = maxLength;
DBG("Get SMS");
- //List command
+ //Show register
char cmd[32] = "ATS211?";
ret = m_pIf->execute(cmd, this, NULL, DEFAULT_TIMEOUT);
if( ret != OK )
@@ -135,7 +146,8 @@
WARN("State variable is not 'SMS_CMD_PROCESSED' - returning 'NET_EMPTY'");
}
- m_msisdn[0] = 0;
+ strcpy(m_msisdn, m_caller);
+ m_caller[0] = 0;
m_state = SMS_IDLE;
return OK;
@@ -147,18 +159,22 @@
if(m_state == SMS_SEND_CMD_SENT)
{
if( strncmp(line, "ALERT", 5) == 0 ) {
- DBG("SM sent");
+ DBG("SM send ALERT");
m_state = SMS_CMD_PROCESSED;
} else
if( strncmp(line, "BUSY", 5) == 0 ) {
- DBG("SM sent");
+ DBG("SM send BUSY");
m_state = SMS_CMD_BUSY;
+ } else
+ if( strncmp(line, "NO CARRIER", 10) == 0 ) {
+ DBG("SM send NO CARRIER");
+ m_state = SMS_IDLE;
}
}
else if(m_state == SMS_GET_CMD_SENT)
{
- DBG("Header: %s", line);
- if( strncmp(line, "128145000013", 12) == 0 ) {
+ DBG("SM recv: %s", line);
+ if( strncmp(line, SHORT_MAIL_CODE, 12) == 0 ) { // light mail code
int j = 0, c = 0, len = 0;
for (int i = 12; i < strlen(line); i ++) {
c = (c * 10) + (line[i] - '0');
@@ -172,6 +188,7 @@
}
}
m_msg[len] = 0;
+ DBG("message '%s'", m_msg);
m_state = SMS_CMD_PROCESSED;
}
}
@@ -227,6 +244,26 @@
/*virtual*/ bool SMSInterface::isATCodeHandled(const char* atCode) //Is this AT code handled
{
+ if(m_state == SMS_IDLE) {
+ if( strncmp(atCode, "RING", 4) == 0 ) {
+ m_caller[0] = 0;
+ DBG("RING");
+ m_state = SMS_RING;
+ }
+ return true;
+ } else
+ if(m_state == SMS_RING) {
+ if( strncmp(atCode, "ID=", 3) == 0 ) {
+ strncpy(m_caller, &atCode[3], sizeof(m_caller));
+ m_caller[11] = 0;
+ DBG("ID %s", m_caller);
+ } else
+ if( strncmp(atCode, "NO CARRIER", 10) == 0 ) {
+ DBG("NO CARRIER");
+ m_state = SMS_IDLE;
+ }
+ return true;
+ }
return false;
}
