local fork

Dependencies:   Socket USBHostWANDongle_bleedingedge lwip-sys lwip

Dependents:   Encrypted

Fork of VodafoneUSBModem_bleedingedge by Donatien Garnier

Committer:
ashleymills
Date:
Fri Apr 26 16:58:07 2013 +0000
Revision:
87:23f78174a9e2
Parent:
81:d74a2a9d2b01
nothing

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