1 /* USSDInterface.cpp */
2 /* Copyright (C) 2012 mbed.org, MIT License
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in all copies or
11 * substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #define __MODULE__ "USSDInterface.cpp"
27 #include "USSDInterface.h"
31 #define DEFAULT_TIMEOUT 10000
32 #define USSD_TIMEOUT 15000
34 USSDInterface::USSDInterface(ATCommandsInterface* pIf) : m_pIf(pIf), m_responseMtx(), m_responseSphre(1), m_result(NULL), m_maxResultLength(0)
36 m_responseSphre.wait(0); //Take ownership of the semaphore
37 m_pIf->registerEventsHandler(this); //Add us to the unsolicited result codes handlers
40 int USSDInterface::init()
42 DBG("Initialization done");
46 int USSDInterface::send(const char* command, char* result, size_t maxLength)
48 if (strlen(command) > 20) //Prevent buffer overflow
55 m_maxResultLength = maxLength;
56 m_responseMtx.unlock();
58 m_responseSphre.wait(0); //Make sure there is not a pending result that needs to be discarded
60 DBG("Send USSD command & register for unsolicited result codes");
61 //Send USSD command to the network
63 std::sprintf(cmd, "AT+CUSD=1,\"%s\"", command);
64 int ret = m_pIf->execute(cmd, this, NULL, DEFAULT_TIMEOUT);
70 //Did we already get a response (3GPP rev < 6) ?
72 //Now wait for response
73 int res = m_responseSphre.wait(USSD_TIMEOUT);
78 m_maxResultLength = 0;
79 m_responseMtx.unlock();
83 DBG("No result received");
84 ret = m_pIf->executeSimple("AT+CUSD=2", NULL, DEFAULT_TIMEOUT); //Cancel command
92 DBG("Result received: %s", result);
97 /*virtual*/ int USSDInterface::onNewATResponseLine(ATCommandsInterface* pInst, const char* line)
99 const char* pSemicol = strchr(line, ':');
100 if( ( (pSemicol - line) != strlen("+CUSD") ) || ( memcmp(line, "+CUSD", strlen("+CUSD")) != 0) )
102 WARN("Unknown code");
106 const char* pData = NULL;
107 if( pSemicol != NULL ) //Split the identifier & the result code (if it exists)
109 pData = pSemicol + 1;
112 pData++; //Suppress whitespace
114 processResult(pData);
119 /*virtual*/ int USSDInterface::onNewEntryPrompt(ATCommandsInterface* pInst)
124 /*virtual*/ bool USSDInterface::isATCodeHandled(const char* atCode) //Is this AT code handled
126 DBG("AT code is %s", atCode);
127 if( strcmp("+CUSD", atCode) == 0 )
136 /*virtual*/ void USSDInterface::onDispatchStart()
142 /*virtual*/ void USSDInterface::onDispatchStop()
147 /*virtual*/ char* USSDInterface::getEventsEnableCommand()
149 return NULL; //No need to disable events here
152 /*virtual*/ char* USSDInterface::getEventsDisableCommand()
154 return NULL; //No need to re-enable events here
157 /*virtual*/ void USSDInterface::onEvent(const char* atCode, const char* evt)
159 if( strcmp("+CUSD", atCode) != 0 )
161 WARN("Wrong AT Code");
162 return; //Not supported
168 void USSDInterface::processResult(const char* data)
170 char* pStart = (char*) strchr(data,'\"');
173 WARN("Could not find opening quote");
174 return; //Invalid/incomplete response
176 pStart++; //Point to first char of response
177 char* pEnd = (char*) strchr(pStart,'\"');
180 WARN("Could not find closing quote");
181 return; //Invalid/incomplete response
183 m_responseMtx.lock();
184 if(m_maxResultLength == 0) //No pending command
186 WARN("No pending command");
187 m_responseMtx.unlock();
190 size_t cpyLen = MIN( pEnd - pStart, m_maxResultLength - 1 );
191 memcpy((void*)m_result, pStart, cpyLen);
192 m_result[cpyLen] = '\0';
193 DBG("Got USSD response: %s", m_result);
194 m_responseMtx.unlock();
195 m_responseSphre.release(); //Signal user thread that response is ready