local fork

Dependencies:   Socket USBHostWANDongle_bleedingedge lwip-sys lwip

Dependents:   Encrypted

Fork of VodafoneUSBModem_bleedingedge by Donatien Garnier

Committer:
donatien
Date:
Thu Aug 30 12:58:10 2012 +0000
Revision:
29:870de7db2ccb
Parent:
22:06fb2a93a1f6
Child:
35:be311326ee06
USSD support on K3772Z (3GPP rev < 6)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:3b2f052c333b 1 /* USSDInterface.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
donatien 12:66dc2c8eba2d 20 #define __DEBUG__ 0
donatien 0:3b2f052c333b 21 #ifndef __MODULE__
donatien 0:3b2f052c333b 22 #define __MODULE__ "USSDInterface.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 "USSDInterface.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 #define USSD_TIMEOUT 15000
donatien 0:3b2f052c333b 33
donatien 0:3b2f052c333b 34 USSDInterface::USSDInterface(ATCommandsInterface* pIf) : m_pIf(pIf), m_responseMtx(), m_responseSphre(1), m_result(NULL), m_maxResultLength(0)
donatien 0:3b2f052c333b 35 {
donatien 0:3b2f052c333b 36 m_responseSphre.wait(0); //Take ownership of the semaphore
donatien 0:3b2f052c333b 37 m_pIf->registerEventsHandler(this); //Add us to the unsolicited result codes handlers
donatien 0:3b2f052c333b 38 }
donatien 0:3b2f052c333b 39
donatien 0:3b2f052c333b 40 int USSDInterface::init()
donatien 0:3b2f052c333b 41 {
donatien 0:3b2f052c333b 42 DBG("Initialization done");
donatien 0:3b2f052c333b 43 return OK;
donatien 0:3b2f052c333b 44 }
donatien 0:3b2f052c333b 45
donatien 0:3b2f052c333b 46 int USSDInterface::send(const char* command, char* result, size_t maxLength)
donatien 0:3b2f052c333b 47 {
donatien 0:3b2f052c333b 48 if (strlen(command) > 20) //Prevent buffer overflow
donatien 0:3b2f052c333b 49 {
donatien 0:3b2f052c333b 50 return NET_TOOSMALL;
donatien 0:3b2f052c333b 51 }
donatien 0:3b2f052c333b 52
donatien 0:3b2f052c333b 53 m_responseMtx.lock();
donatien 0:3b2f052c333b 54 m_result = result;
donatien 0:3b2f052c333b 55 m_maxResultLength = maxLength;
donatien 0:3b2f052c333b 56 m_responseMtx.unlock();
donatien 0:3b2f052c333b 57
donatien 0:3b2f052c333b 58 m_responseSphre.wait(0); //Make sure there is not a pending result that needs to be discarded
donatien 0:3b2f052c333b 59
donatien 0:3b2f052c333b 60 DBG("Send USSD command & register for unsolicited result codes");
donatien 0:3b2f052c333b 61 //Send USSD command to the network
donatien 0:3b2f052c333b 62 char cmd[32];
donatien 0:3b2f052c333b 63 std::sprintf(cmd, "AT+CUSD=1,\"%s\"", command);
donatien 29:870de7db2ccb 64 int ret = m_pIf->execute(cmd, this, NULL, DEFAULT_TIMEOUT);
donatien 0:3b2f052c333b 65 if( ret != OK )
donatien 0:3b2f052c333b 66 {
donatien 0:3b2f052c333b 67 return NET_PROTOCOL;
donatien 0:3b2f052c333b 68 }
donatien 29:870de7db2ccb 69
donatien 29:870de7db2ccb 70 //Did we already get a response (3GPP rev < 6) ?
donatien 0:3b2f052c333b 71
donatien 0:3b2f052c333b 72 //Now wait for response
donatien 0:3b2f052c333b 73 int res = m_responseSphre.wait(USSD_TIMEOUT);
donatien 0:3b2f052c333b 74
donatien 0:3b2f052c333b 75 //Reset data
donatien 0:3b2f052c333b 76 m_responseMtx.lock();
donatien 0:3b2f052c333b 77 m_result = NULL;
donatien 0:3b2f052c333b 78 m_maxResultLength = 0;
donatien 0:3b2f052c333b 79 m_responseMtx.unlock();
donatien 0:3b2f052c333b 80
donatien 0:3b2f052c333b 81 if(res <= 0)
donatien 0:3b2f052c333b 82 {
donatien 0:3b2f052c333b 83 DBG("No result received");
donatien 0:3b2f052c333b 84 ret = m_pIf->executeSimple("AT+CUSD=2", NULL, DEFAULT_TIMEOUT); //Cancel command
donatien 0:3b2f052c333b 85 if( ret != OK )
donatien 0:3b2f052c333b 86 {
donatien 0:3b2f052c333b 87 return NET_PROTOCOL;
donatien 0:3b2f052c333b 88 }
donatien 0:3b2f052c333b 89 return NET_TIMEOUT;
donatien 0:3b2f052c333b 90 }
donatien 0:3b2f052c333b 91
donatien 0:3b2f052c333b 92 DBG("Result received: %s", result);
donatien 0:3b2f052c333b 93
donatien 0:3b2f052c333b 94 return OK;
donatien 29:870de7db2ccb 95 }
donatien 0:3b2f052c333b 96
donatien 29:870de7db2ccb 97 /*virtual*/ int USSDInterface::onNewATResponseLine(ATCommandsInterface* pInst, const char* line)
donatien 29:870de7db2ccb 98 {
donatien 29:870de7db2ccb 99 const char* pSemicol = strchr(line, ':');
donatien 29:870de7db2ccb 100 if( ( (pSemicol - line) != strlen("+CUSD") ) || ( memcmp(line, "+CUSD", strlen("+CUSD")) != 0) )
donatien 29:870de7db2ccb 101 {
donatien 29:870de7db2ccb 102 WARN("Unknown code");
donatien 29:870de7db2ccb 103 return OK;
donatien 29:870de7db2ccb 104 }
donatien 29:870de7db2ccb 105
donatien 29:870de7db2ccb 106 const char* pData = NULL;
donatien 29:870de7db2ccb 107 if( pSemicol != NULL ) //Split the identifier & the result code (if it exists)
donatien 29:870de7db2ccb 108 {
donatien 29:870de7db2ccb 109 pData = pSemicol + 1;
donatien 29:870de7db2ccb 110 if(pData[0]==' ')
donatien 29:870de7db2ccb 111 {
donatien 29:870de7db2ccb 112 pData++; //Suppress whitespace
donatien 29:870de7db2ccb 113 }
donatien 29:870de7db2ccb 114 processResult(pData);
donatien 29:870de7db2ccb 115 }
donatien 29:870de7db2ccb 116 return OK;
donatien 29:870de7db2ccb 117 }
donatien 29:870de7db2ccb 118
donatien 29:870de7db2ccb 119 /*virtual*/ int USSDInterface::onNewEntryPrompt(ATCommandsInterface* pInst)
donatien 29:870de7db2ccb 120 {
donatien 29:870de7db2ccb 121 return OK;
donatien 0:3b2f052c333b 122 }
donatien 0:3b2f052c333b 123
donatien 0:3b2f052c333b 124 /*virtual*/ bool USSDInterface::isATCodeHandled(const char* atCode) //Is this AT code handled
donatien 0:3b2f052c333b 125 {
donatien 0:3b2f052c333b 126 DBG("AT code is %s", atCode);
donatien 0:3b2f052c333b 127 if( strcmp("+CUSD", atCode) == 0 )
donatien 0:3b2f052c333b 128 {
donatien 0:3b2f052c333b 129 return true;
donatien 0:3b2f052c333b 130 }
donatien 0:3b2f052c333b 131
donatien 0:3b2f052c333b 132 DBG("Not handled");
donatien 0:3b2f052c333b 133 return false;
donatien 0:3b2f052c333b 134 }
donatien 0:3b2f052c333b 135
donatien 0:3b2f052c333b 136 /*virtual*/ void USSDInterface::onDispatchStart()
donatien 0:3b2f052c333b 137 {
donatien 0:3b2f052c333b 138
donatien 0:3b2f052c333b 139
donatien 0:3b2f052c333b 140 }
donatien 0:3b2f052c333b 141
donatien 0:3b2f052c333b 142 /*virtual*/ void USSDInterface::onDispatchStop()
donatien 0:3b2f052c333b 143 {
donatien 0:3b2f052c333b 144
donatien 0:3b2f052c333b 145 }
donatien 0:3b2f052c333b 146
donatien 0:3b2f052c333b 147 /*virtual*/ void USSDInterface::onEvent(const char* atCode, const char* evt)
donatien 0:3b2f052c333b 148 {
donatien 0:3b2f052c333b 149 if( strcmp("+CUSD", atCode) != 0 )
donatien 0:3b2f052c333b 150 {
donatien 29:870de7db2ccb 151 WARN("Wrong AT Code");
donatien 0:3b2f052c333b 152 return; //Not supported
donatien 0:3b2f052c333b 153 }
donatien 0:3b2f052c333b 154
donatien 29:870de7db2ccb 155 processResult(evt);
donatien 29:870de7db2ccb 156 }
donatien 29:870de7db2ccb 157
donatien 29:870de7db2ccb 158 void USSDInterface::processResult(const char* data)
donatien 29:870de7db2ccb 159 {
donatien 29:870de7db2ccb 160 char* pStart = (char*) strchr(data,'\"');
donatien 0:3b2f052c333b 161 if(pStart==NULL)
donatien 0:3b2f052c333b 162 {
donatien 29:870de7db2ccb 163 WARN("Could not find opening quote");
donatien 0:3b2f052c333b 164 return; //Invalid/incomplete response
donatien 0:3b2f052c333b 165 }
donatien 0:3b2f052c333b 166 pStart++; //Point to first char of response
donatien 0:3b2f052c333b 167 char* pEnd = (char*) strchr(pStart,'\"');
donatien 0:3b2f052c333b 168 if(pEnd==NULL)
donatien 0:3b2f052c333b 169 {
donatien 29:870de7db2ccb 170 WARN("Could not find closing quote");
donatien 0:3b2f052c333b 171 return; //Invalid/incomplete response
donatien 0:3b2f052c333b 172 }
donatien 0:3b2f052c333b 173 m_responseMtx.lock();
donatien 0:3b2f052c333b 174 if(m_maxResultLength == 0) //No pending command
donatien 0:3b2f052c333b 175 {
donatien 29:870de7db2ccb 176 WARN("No pending command");
donatien 0:3b2f052c333b 177 m_responseMtx.unlock();
donatien 0:3b2f052c333b 178 return;
donatien 0:3b2f052c333b 179 }
donatien 0:3b2f052c333b 180 size_t cpyLen = MIN( pEnd - pStart, m_maxResultLength - 1 );
donatien 0:3b2f052c333b 181 memcpy(m_result, pStart, cpyLen);
donatien 0:3b2f052c333b 182 m_result[cpyLen] = '\0';
donatien 29:870de7db2ccb 183 DBG("Got USSD response: %s", m_result);
donatien 0:3b2f052c333b 184 m_responseMtx.unlock();
donatien 0:3b2f052c333b 185 m_responseSphre.release(); //Signal user thread that response is ready
donatien 0:3b2f052c333b 186 }