local fork

Dependencies:   Socket USBHostWANDongle_bleedingedge lwip-sys lwip

Dependents:   Encrypted

Fork of VodafoneUSBModem_bleedingedge by Donatien Garnier

Committer:
donatien
Date:
Fri Aug 17 14:31:27 2012 +0000
Revision:
22:06fb2a93a1f6
Parent:
12:66dc2c8eba2d
Child:
29:870de7db2ccb
Fork with names/headers changes to prepare for pre-built version

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 0:3b2f052c333b 64 int ret = m_pIf->executeSimple(cmd, 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 0:3b2f052c333b 69
donatien 0:3b2f052c333b 70 //Now wait for response
donatien 0:3b2f052c333b 71 int res = m_responseSphre.wait(USSD_TIMEOUT);
donatien 0:3b2f052c333b 72
donatien 0:3b2f052c333b 73 //Reset data
donatien 0:3b2f052c333b 74 m_responseMtx.lock();
donatien 0:3b2f052c333b 75 m_result = NULL;
donatien 0:3b2f052c333b 76 m_maxResultLength = 0;
donatien 0:3b2f052c333b 77 m_responseMtx.unlock();
donatien 0:3b2f052c333b 78
donatien 0:3b2f052c333b 79 if(res <= 0)
donatien 0:3b2f052c333b 80 {
donatien 0:3b2f052c333b 81 DBG("No result received");
donatien 0:3b2f052c333b 82 ret = m_pIf->executeSimple("AT+CUSD=2", NULL, DEFAULT_TIMEOUT); //Cancel command
donatien 0:3b2f052c333b 83 if( ret != OK )
donatien 0:3b2f052c333b 84 {
donatien 0:3b2f052c333b 85 return NET_PROTOCOL;
donatien 0:3b2f052c333b 86 }
donatien 0:3b2f052c333b 87 return NET_TIMEOUT;
donatien 0:3b2f052c333b 88 }
donatien 0:3b2f052c333b 89
donatien 0:3b2f052c333b 90 DBG("Result received: %s", result);
donatien 0:3b2f052c333b 91
donatien 0:3b2f052c333b 92 return OK;
donatien 0:3b2f052c333b 93
donatien 0:3b2f052c333b 94 }
donatien 0:3b2f052c333b 95
donatien 0:3b2f052c333b 96 /*virtual*/ bool USSDInterface::isATCodeHandled(const char* atCode) //Is this AT code handled
donatien 0:3b2f052c333b 97 {
donatien 0:3b2f052c333b 98 DBG("AT code is %s", atCode);
donatien 0:3b2f052c333b 99 if( strcmp("+CUSD", atCode) == 0 )
donatien 0:3b2f052c333b 100 {
donatien 0:3b2f052c333b 101 return true;
donatien 0:3b2f052c333b 102 }
donatien 0:3b2f052c333b 103
donatien 0:3b2f052c333b 104 DBG("Not handled");
donatien 0:3b2f052c333b 105 return false;
donatien 0:3b2f052c333b 106 }
donatien 0:3b2f052c333b 107
donatien 0:3b2f052c333b 108 /*virtual*/ void USSDInterface::onDispatchStart()
donatien 0:3b2f052c333b 109 {
donatien 0:3b2f052c333b 110
donatien 0:3b2f052c333b 111
donatien 0:3b2f052c333b 112 }
donatien 0:3b2f052c333b 113
donatien 0:3b2f052c333b 114 /*virtual*/ void USSDInterface::onDispatchStop()
donatien 0:3b2f052c333b 115 {
donatien 0:3b2f052c333b 116
donatien 0:3b2f052c333b 117 }
donatien 0:3b2f052c333b 118
donatien 0:3b2f052c333b 119 /*virtual*/ void USSDInterface::onEvent(const char* atCode, const char* evt)
donatien 0:3b2f052c333b 120 {
donatien 0:3b2f052c333b 121 if( strcmp("+CUSD", atCode) != 0 )
donatien 0:3b2f052c333b 122 {
donatien 0:3b2f052c333b 123 return; //Not supported
donatien 0:3b2f052c333b 124 }
donatien 0:3b2f052c333b 125
donatien 0:3b2f052c333b 126 char* pStart = (char*) strchr(evt,'\"');
donatien 0:3b2f052c333b 127 if(pStart==NULL)
donatien 0:3b2f052c333b 128 {
donatien 0:3b2f052c333b 129 return; //Invalid/incomplete response
donatien 0:3b2f052c333b 130 }
donatien 0:3b2f052c333b 131 pStart++; //Point to first char of response
donatien 0:3b2f052c333b 132 char* pEnd = (char*) strchr(pStart,'\"');
donatien 0:3b2f052c333b 133 if(pEnd==NULL)
donatien 0:3b2f052c333b 134 {
donatien 0:3b2f052c333b 135 return; //Invalid/incomplete response
donatien 0:3b2f052c333b 136 }
donatien 0:3b2f052c333b 137 m_responseMtx.lock();
donatien 0:3b2f052c333b 138 if(m_maxResultLength == 0) //No pending command
donatien 0:3b2f052c333b 139 {
donatien 0:3b2f052c333b 140 m_responseMtx.unlock();
donatien 0:3b2f052c333b 141 return;
donatien 0:3b2f052c333b 142 }
donatien 0:3b2f052c333b 143 size_t cpyLen = MIN( pEnd - pStart, m_maxResultLength - 1 );
donatien 0:3b2f052c333b 144 memcpy(m_result, pStart, cpyLen);
donatien 0:3b2f052c333b 145 m_result[cpyLen] = '\0';
donatien 0:3b2f052c333b 146 m_responseMtx.unlock();
donatien 0:3b2f052c333b 147 m_responseSphre.release(); //Signal user thread that response is ready
donatien 0:3b2f052c333b 148 }