Base library for cellular modem implementations
Dependencies: Socket lwip-sys lwip
Dependents: CellularUSBModem CellularUSBModem
Deprecated
This is an mbed 2 networking library. For mbed 5, the networking libraries have been revised to better support additional network stacks and thread safety here.
ussd/USSDInterface.cpp@8:944cd194963e, 2014-05-08 (annotated)
- Committer:
- mbed_official
- Date:
- Thu May 08 11:00:26 2014 +0100
- Revision:
- 8:944cd194963e
- Parent:
- 1:4a23efdf0da9
Synchronized with git revision df12bf01ac7dbb50751e2b16a351c894994e1dcf
Full URL: https://github.com/mbedmicro/mbed/commit/df12bf01ac7dbb50751e2b16a351c894994e1dcf/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 1:4a23efdf0da9 | 1 | /* USSDInterface.cpp */ |
bogdanm | 1:4a23efdf0da9 | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
bogdanm | 1:4a23efdf0da9 | 3 | * |
bogdanm | 1:4a23efdf0da9 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
bogdanm | 1:4a23efdf0da9 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
bogdanm | 1:4a23efdf0da9 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
bogdanm | 1:4a23efdf0da9 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
bogdanm | 1:4a23efdf0da9 | 8 | * furnished to do so, subject to the following conditions: |
bogdanm | 1:4a23efdf0da9 | 9 | * |
bogdanm | 1:4a23efdf0da9 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
bogdanm | 1:4a23efdf0da9 | 11 | * substantial portions of the Software. |
bogdanm | 1:4a23efdf0da9 | 12 | * |
bogdanm | 1:4a23efdf0da9 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
bogdanm | 1:4a23efdf0da9 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
bogdanm | 1:4a23efdf0da9 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
bogdanm | 1:4a23efdf0da9 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
bogdanm | 1:4a23efdf0da9 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
bogdanm | 1:4a23efdf0da9 | 18 | */ |
bogdanm | 1:4a23efdf0da9 | 19 | |
bogdanm | 1:4a23efdf0da9 | 20 | #define __DEBUG__ 0 |
bogdanm | 1:4a23efdf0da9 | 21 | #ifndef __MODULE__ |
bogdanm | 1:4a23efdf0da9 | 22 | #define __MODULE__ "USSDInterface.cpp" |
bogdanm | 1:4a23efdf0da9 | 23 | #endif |
bogdanm | 1:4a23efdf0da9 | 24 | |
bogdanm | 1:4a23efdf0da9 | 25 | #include "core/fwk.h" |
bogdanm | 1:4a23efdf0da9 | 26 | |
bogdanm | 1:4a23efdf0da9 | 27 | #include "USSDInterface.h" |
bogdanm | 1:4a23efdf0da9 | 28 | |
bogdanm | 1:4a23efdf0da9 | 29 | #include <cstdio> |
bogdanm | 1:4a23efdf0da9 | 30 | |
bogdanm | 1:4a23efdf0da9 | 31 | #define DEFAULT_TIMEOUT 10000 |
bogdanm | 1:4a23efdf0da9 | 32 | #define USSD_TIMEOUT 15000 |
bogdanm | 1:4a23efdf0da9 | 33 | |
bogdanm | 1:4a23efdf0da9 | 34 | USSDInterface::USSDInterface(ATCommandsInterface* pIf) : m_pIf(pIf), m_responseMtx(), m_responseSphre(1), m_result(NULL), m_maxResultLength(0) |
bogdanm | 1:4a23efdf0da9 | 35 | { |
bogdanm | 1:4a23efdf0da9 | 36 | m_responseSphre.wait(0); //Take ownership of the semaphore |
bogdanm | 1:4a23efdf0da9 | 37 | m_pIf->registerEventsHandler(this); //Add us to the unsolicited result codes handlers |
bogdanm | 1:4a23efdf0da9 | 38 | } |
bogdanm | 1:4a23efdf0da9 | 39 | |
bogdanm | 1:4a23efdf0da9 | 40 | int USSDInterface::init() |
bogdanm | 1:4a23efdf0da9 | 41 | { |
bogdanm | 1:4a23efdf0da9 | 42 | DBG("Initialization done"); |
bogdanm | 1:4a23efdf0da9 | 43 | return OK; |
bogdanm | 1:4a23efdf0da9 | 44 | } |
bogdanm | 1:4a23efdf0da9 | 45 | |
bogdanm | 1:4a23efdf0da9 | 46 | int USSDInterface::send(const char* command, char* result, size_t maxLength) |
bogdanm | 1:4a23efdf0da9 | 47 | { |
bogdanm | 1:4a23efdf0da9 | 48 | if (strlen(command) > 20) //Prevent buffer overflow |
bogdanm | 1:4a23efdf0da9 | 49 | { |
bogdanm | 1:4a23efdf0da9 | 50 | return NET_TOOSMALL; |
bogdanm | 1:4a23efdf0da9 | 51 | } |
bogdanm | 1:4a23efdf0da9 | 52 | |
bogdanm | 1:4a23efdf0da9 | 53 | m_responseMtx.lock(); |
bogdanm | 1:4a23efdf0da9 | 54 | m_result = result; |
bogdanm | 1:4a23efdf0da9 | 55 | m_maxResultLength = maxLength; |
bogdanm | 1:4a23efdf0da9 | 56 | m_responseMtx.unlock(); |
bogdanm | 1:4a23efdf0da9 | 57 | |
bogdanm | 1:4a23efdf0da9 | 58 | m_responseSphre.wait(0); //Make sure there is not a pending result that needs to be discarded |
bogdanm | 1:4a23efdf0da9 | 59 | |
bogdanm | 1:4a23efdf0da9 | 60 | DBG("Send USSD command & register for unsolicited result codes"); |
bogdanm | 1:4a23efdf0da9 | 61 | //Send USSD command to the network |
bogdanm | 1:4a23efdf0da9 | 62 | char cmd[32]; |
bogdanm | 1:4a23efdf0da9 | 63 | std::sprintf(cmd, "AT+CUSD=1,\"%s\"", command); |
bogdanm | 1:4a23efdf0da9 | 64 | int ret = m_pIf->execute(cmd, this, NULL, DEFAULT_TIMEOUT); |
bogdanm | 1:4a23efdf0da9 | 65 | if( ret != OK ) |
bogdanm | 1:4a23efdf0da9 | 66 | { |
bogdanm | 1:4a23efdf0da9 | 67 | return NET_PROTOCOL; |
bogdanm | 1:4a23efdf0da9 | 68 | } |
bogdanm | 1:4a23efdf0da9 | 69 | |
bogdanm | 1:4a23efdf0da9 | 70 | //Did we already get a response (3GPP rev < 6) ? |
bogdanm | 1:4a23efdf0da9 | 71 | |
bogdanm | 1:4a23efdf0da9 | 72 | //Now wait for response |
bogdanm | 1:4a23efdf0da9 | 73 | int res = m_responseSphre.wait(USSD_TIMEOUT); |
bogdanm | 1:4a23efdf0da9 | 74 | |
bogdanm | 1:4a23efdf0da9 | 75 | //Reset data |
bogdanm | 1:4a23efdf0da9 | 76 | m_responseMtx.lock(); |
bogdanm | 1:4a23efdf0da9 | 77 | m_result = NULL; |
bogdanm | 1:4a23efdf0da9 | 78 | m_maxResultLength = 0; |
bogdanm | 1:4a23efdf0da9 | 79 | m_responseMtx.unlock(); |
bogdanm | 1:4a23efdf0da9 | 80 | |
bogdanm | 1:4a23efdf0da9 | 81 | if(res <= 0) |
bogdanm | 1:4a23efdf0da9 | 82 | { |
bogdanm | 1:4a23efdf0da9 | 83 | DBG("No result received"); |
bogdanm | 1:4a23efdf0da9 | 84 | ret = m_pIf->executeSimple("AT+CUSD=2", NULL, DEFAULT_TIMEOUT); //Cancel command |
bogdanm | 1:4a23efdf0da9 | 85 | if( ret != OK ) |
bogdanm | 1:4a23efdf0da9 | 86 | { |
bogdanm | 1:4a23efdf0da9 | 87 | return NET_PROTOCOL; |
bogdanm | 1:4a23efdf0da9 | 88 | } |
bogdanm | 1:4a23efdf0da9 | 89 | return NET_TIMEOUT; |
bogdanm | 1:4a23efdf0da9 | 90 | } |
bogdanm | 1:4a23efdf0da9 | 91 | |
bogdanm | 1:4a23efdf0da9 | 92 | DBG("Result received: %s", result); |
bogdanm | 1:4a23efdf0da9 | 93 | |
bogdanm | 1:4a23efdf0da9 | 94 | return OK; |
bogdanm | 1:4a23efdf0da9 | 95 | } |
bogdanm | 1:4a23efdf0da9 | 96 | |
bogdanm | 1:4a23efdf0da9 | 97 | /*virtual*/ int USSDInterface::onNewATResponseLine(ATCommandsInterface* pInst, const char* line) |
bogdanm | 1:4a23efdf0da9 | 98 | { |
bogdanm | 1:4a23efdf0da9 | 99 | const char* pSemicol = strchr(line, ':'); |
bogdanm | 1:4a23efdf0da9 | 100 | if( ( (pSemicol - line) != strlen("+CUSD") ) || ( memcmp(line, "+CUSD", strlen("+CUSD")) != 0) ) |
bogdanm | 1:4a23efdf0da9 | 101 | { |
bogdanm | 1:4a23efdf0da9 | 102 | WARN("Unknown code"); |
bogdanm | 1:4a23efdf0da9 | 103 | return OK; |
bogdanm | 1:4a23efdf0da9 | 104 | } |
bogdanm | 1:4a23efdf0da9 | 105 | |
bogdanm | 1:4a23efdf0da9 | 106 | const char* pData = NULL; |
bogdanm | 1:4a23efdf0da9 | 107 | if( pSemicol != NULL ) //Split the identifier & the result code (if it exists) |
bogdanm | 1:4a23efdf0da9 | 108 | { |
bogdanm | 1:4a23efdf0da9 | 109 | pData = pSemicol + 1; |
bogdanm | 1:4a23efdf0da9 | 110 | if(pData[0]==' ') |
bogdanm | 1:4a23efdf0da9 | 111 | { |
bogdanm | 1:4a23efdf0da9 | 112 | pData++; //Suppress whitespace |
bogdanm | 1:4a23efdf0da9 | 113 | } |
bogdanm | 1:4a23efdf0da9 | 114 | processResult(pData); |
bogdanm | 1:4a23efdf0da9 | 115 | } |
bogdanm | 1:4a23efdf0da9 | 116 | return OK; |
bogdanm | 1:4a23efdf0da9 | 117 | } |
bogdanm | 1:4a23efdf0da9 | 118 | |
bogdanm | 1:4a23efdf0da9 | 119 | /*virtual*/ int USSDInterface::onNewEntryPrompt(ATCommandsInterface* pInst) |
bogdanm | 1:4a23efdf0da9 | 120 | { |
bogdanm | 1:4a23efdf0da9 | 121 | return OK; |
bogdanm | 1:4a23efdf0da9 | 122 | } |
bogdanm | 1:4a23efdf0da9 | 123 | |
bogdanm | 1:4a23efdf0da9 | 124 | /*virtual*/ bool USSDInterface::isATCodeHandled(const char* atCode) //Is this AT code handled |
bogdanm | 1:4a23efdf0da9 | 125 | { |
bogdanm | 1:4a23efdf0da9 | 126 | DBG("AT code is %s", atCode); |
bogdanm | 1:4a23efdf0da9 | 127 | if( strcmp("+CUSD", atCode) == 0 ) |
bogdanm | 1:4a23efdf0da9 | 128 | { |
bogdanm | 1:4a23efdf0da9 | 129 | return true; |
bogdanm | 1:4a23efdf0da9 | 130 | } |
bogdanm | 1:4a23efdf0da9 | 131 | |
bogdanm | 1:4a23efdf0da9 | 132 | DBG("Not handled"); |
bogdanm | 1:4a23efdf0da9 | 133 | return false; |
bogdanm | 1:4a23efdf0da9 | 134 | } |
bogdanm | 1:4a23efdf0da9 | 135 | |
bogdanm | 1:4a23efdf0da9 | 136 | /*virtual*/ void USSDInterface::onDispatchStart() |
bogdanm | 1:4a23efdf0da9 | 137 | { |
bogdanm | 1:4a23efdf0da9 | 138 | |
bogdanm | 1:4a23efdf0da9 | 139 | |
bogdanm | 1:4a23efdf0da9 | 140 | } |
bogdanm | 1:4a23efdf0da9 | 141 | |
bogdanm | 1:4a23efdf0da9 | 142 | /*virtual*/ void USSDInterface::onDispatchStop() |
bogdanm | 1:4a23efdf0da9 | 143 | { |
bogdanm | 1:4a23efdf0da9 | 144 | |
bogdanm | 1:4a23efdf0da9 | 145 | } |
bogdanm | 1:4a23efdf0da9 | 146 | |
bogdanm | 1:4a23efdf0da9 | 147 | /*virtual*/ char* USSDInterface::getEventsEnableCommand() |
bogdanm | 1:4a23efdf0da9 | 148 | { |
bogdanm | 1:4a23efdf0da9 | 149 | return NULL; //No need to disable events here |
bogdanm | 1:4a23efdf0da9 | 150 | } |
bogdanm | 1:4a23efdf0da9 | 151 | |
bogdanm | 1:4a23efdf0da9 | 152 | /*virtual*/ char* USSDInterface::getEventsDisableCommand() |
bogdanm | 1:4a23efdf0da9 | 153 | { |
bogdanm | 1:4a23efdf0da9 | 154 | return NULL; //No need to re-enable events here |
bogdanm | 1:4a23efdf0da9 | 155 | } |
bogdanm | 1:4a23efdf0da9 | 156 | |
bogdanm | 1:4a23efdf0da9 | 157 | /*virtual*/ void USSDInterface::onEvent(const char* atCode, const char* evt) |
bogdanm | 1:4a23efdf0da9 | 158 | { |
bogdanm | 1:4a23efdf0da9 | 159 | if( strcmp("+CUSD", atCode) != 0 ) |
bogdanm | 1:4a23efdf0da9 | 160 | { |
bogdanm | 1:4a23efdf0da9 | 161 | WARN("Wrong AT Code"); |
bogdanm | 1:4a23efdf0da9 | 162 | return; //Not supported |
bogdanm | 1:4a23efdf0da9 | 163 | } |
bogdanm | 1:4a23efdf0da9 | 164 | |
bogdanm | 1:4a23efdf0da9 | 165 | processResult(evt); |
bogdanm | 1:4a23efdf0da9 | 166 | } |
bogdanm | 1:4a23efdf0da9 | 167 | |
bogdanm | 1:4a23efdf0da9 | 168 | void USSDInterface::processResult(const char* data) |
bogdanm | 1:4a23efdf0da9 | 169 | { |
bogdanm | 1:4a23efdf0da9 | 170 | char* pStart = (char*) strchr(data,'\"'); |
bogdanm | 1:4a23efdf0da9 | 171 | if(pStart==NULL) |
bogdanm | 1:4a23efdf0da9 | 172 | { |
bogdanm | 1:4a23efdf0da9 | 173 | WARN("Could not find opening quote"); |
bogdanm | 1:4a23efdf0da9 | 174 | return; //Invalid/incomplete response |
bogdanm | 1:4a23efdf0da9 | 175 | } |
bogdanm | 1:4a23efdf0da9 | 176 | pStart++; //Point to first char of response |
bogdanm | 1:4a23efdf0da9 | 177 | char* pEnd = (char*) strchr(pStart,'\"'); |
bogdanm | 1:4a23efdf0da9 | 178 | if(pEnd==NULL) |
bogdanm | 1:4a23efdf0da9 | 179 | { |
bogdanm | 1:4a23efdf0da9 | 180 | WARN("Could not find closing quote"); |
bogdanm | 1:4a23efdf0da9 | 181 | return; //Invalid/incomplete response |
bogdanm | 1:4a23efdf0da9 | 182 | } |
bogdanm | 1:4a23efdf0da9 | 183 | m_responseMtx.lock(); |
bogdanm | 1:4a23efdf0da9 | 184 | if(m_maxResultLength == 0) //No pending command |
bogdanm | 1:4a23efdf0da9 | 185 | { |
bogdanm | 1:4a23efdf0da9 | 186 | WARN("No pending command"); |
bogdanm | 1:4a23efdf0da9 | 187 | m_responseMtx.unlock(); |
bogdanm | 1:4a23efdf0da9 | 188 | return; |
bogdanm | 1:4a23efdf0da9 | 189 | } |
bogdanm | 1:4a23efdf0da9 | 190 | size_t cpyLen = MIN( pEnd - pStart, m_maxResultLength - 1 ); |
bogdanm | 1:4a23efdf0da9 | 191 | memcpy((void*)m_result, pStart, cpyLen); |
bogdanm | 1:4a23efdf0da9 | 192 | m_result[cpyLen] = '\0'; |
bogdanm | 1:4a23efdf0da9 | 193 | DBG("Got USSD response: %s", m_result); |
bogdanm | 1:4a23efdf0da9 | 194 | m_responseMtx.unlock(); |
bogdanm | 1:4a23efdf0da9 | 195 | m_responseSphre.release(); //Signal user thread that response is ready |
bogdanm | 1:4a23efdf0da9 | 196 | } |