Changes to support Vodafone K4606
Dependencies: Socket USBHostWANDongle lwip-sys lwip
Fork of VodafoneUSBModem by
ussd/USSDInterface.cpp@8:04b6a042595f, 2012-06-26 (annotated)
- Committer:
- donatien
- Date:
- Tue Jun 26 13:44:59 2012 +0000
- Revision:
- 8:04b6a042595f
- Parent:
- main/if/ussd/USSDInterface.cpp@0:3b2f052c333b
- Child:
- 12:66dc2c8eba2d
Bleeding edge - test with two different interfaces
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:3b2f052c333b | 1 | /* USSDInterface.cpp */ |
donatien | 0:3b2f052c333b | 2 | /* |
donatien | 0:3b2f052c333b | 3 | Copyright (C) 2012 ARM Limited. |
donatien | 0:3b2f052c333b | 4 | |
donatien | 0:3b2f052c333b | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
donatien | 0:3b2f052c333b | 6 | this software and associated documentation files (the "Software"), to deal in |
donatien | 0:3b2f052c333b | 7 | the Software without restriction, including without limitation the rights to |
donatien | 0:3b2f052c333b | 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
donatien | 0:3b2f052c333b | 9 | of the Software, and to permit persons to whom the Software is furnished to do |
donatien | 0:3b2f052c333b | 10 | so, subject to the following conditions: |
donatien | 0:3b2f052c333b | 11 | |
donatien | 0:3b2f052c333b | 12 | The above copyright notice and this permission notice shall be included in all |
donatien | 0:3b2f052c333b | 13 | copies or substantial portions of the Software. |
donatien | 0:3b2f052c333b | 14 | |
donatien | 0:3b2f052c333b | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
donatien | 0:3b2f052c333b | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
donatien | 0:3b2f052c333b | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
donatien | 0:3b2f052c333b | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
donatien | 0:3b2f052c333b | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 0:3b2f052c333b | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
donatien | 0:3b2f052c333b | 21 | SOFTWARE. |
donatien | 0:3b2f052c333b | 22 | */ |
donatien | 0:3b2f052c333b | 23 | |
donatien | 0:3b2f052c333b | 24 | #define __DEBUG__ 4 |
donatien | 0:3b2f052c333b | 25 | #ifndef __MODULE__ |
donatien | 0:3b2f052c333b | 26 | #define __MODULE__ "USSDInterface.cpp" |
donatien | 0:3b2f052c333b | 27 | #endif |
donatien | 0:3b2f052c333b | 28 | |
donatien | 0:3b2f052c333b | 29 | #include "core/fwk.h" |
donatien | 0:3b2f052c333b | 30 | |
donatien | 0:3b2f052c333b | 31 | #include "USSDInterface.h" |
donatien | 0:3b2f052c333b | 32 | |
donatien | 0:3b2f052c333b | 33 | #include <cstdio> |
donatien | 0:3b2f052c333b | 34 | |
donatien | 0:3b2f052c333b | 35 | #define DEFAULT_TIMEOUT 10000 |
donatien | 0:3b2f052c333b | 36 | #define USSD_TIMEOUT 15000 |
donatien | 0:3b2f052c333b | 37 | |
donatien | 0:3b2f052c333b | 38 | USSDInterface::USSDInterface(ATCommandsInterface* pIf) : m_pIf(pIf), m_responseMtx(), m_responseSphre(1), m_result(NULL), m_maxResultLength(0) |
donatien | 0:3b2f052c333b | 39 | { |
donatien | 0:3b2f052c333b | 40 | m_responseSphre.wait(0); //Take ownership of the semaphore |
donatien | 0:3b2f052c333b | 41 | m_pIf->registerEventsHandler(this); //Add us to the unsolicited result codes handlers |
donatien | 0:3b2f052c333b | 42 | } |
donatien | 0:3b2f052c333b | 43 | |
donatien | 0:3b2f052c333b | 44 | int USSDInterface::init() |
donatien | 0:3b2f052c333b | 45 | { |
donatien | 0:3b2f052c333b | 46 | DBG("Initialization done"); |
donatien | 0:3b2f052c333b | 47 | return OK; |
donatien | 0:3b2f052c333b | 48 | } |
donatien | 0:3b2f052c333b | 49 | |
donatien | 0:3b2f052c333b | 50 | int USSDInterface::send(const char* command, char* result, size_t maxLength) |
donatien | 0:3b2f052c333b | 51 | { |
donatien | 0:3b2f052c333b | 52 | if (strlen(command) > 20) //Prevent buffer overflow |
donatien | 0:3b2f052c333b | 53 | { |
donatien | 0:3b2f052c333b | 54 | return NET_TOOSMALL; |
donatien | 0:3b2f052c333b | 55 | } |
donatien | 0:3b2f052c333b | 56 | |
donatien | 0:3b2f052c333b | 57 | m_responseMtx.lock(); |
donatien | 0:3b2f052c333b | 58 | m_result = result; |
donatien | 0:3b2f052c333b | 59 | m_maxResultLength = maxLength; |
donatien | 0:3b2f052c333b | 60 | m_responseMtx.unlock(); |
donatien | 0:3b2f052c333b | 61 | |
donatien | 0:3b2f052c333b | 62 | m_responseSphre.wait(0); //Make sure there is not a pending result that needs to be discarded |
donatien | 0:3b2f052c333b | 63 | |
donatien | 0:3b2f052c333b | 64 | DBG("Send USSD command & register for unsolicited result codes"); |
donatien | 0:3b2f052c333b | 65 | //Send USSD command to the network |
donatien | 0:3b2f052c333b | 66 | char cmd[32]; |
donatien | 0:3b2f052c333b | 67 | std::sprintf(cmd, "AT+CUSD=1,\"%s\"", command); |
donatien | 0:3b2f052c333b | 68 | int ret = m_pIf->executeSimple(cmd, NULL, DEFAULT_TIMEOUT); |
donatien | 0:3b2f052c333b | 69 | if( ret != OK ) |
donatien | 0:3b2f052c333b | 70 | { |
donatien | 0:3b2f052c333b | 71 | return NET_PROTOCOL; |
donatien | 0:3b2f052c333b | 72 | } |
donatien | 0:3b2f052c333b | 73 | |
donatien | 0:3b2f052c333b | 74 | //Now wait for response |
donatien | 0:3b2f052c333b | 75 | int res = m_responseSphre.wait(USSD_TIMEOUT); |
donatien | 0:3b2f052c333b | 76 | |
donatien | 0:3b2f052c333b | 77 | //Reset data |
donatien | 0:3b2f052c333b | 78 | m_responseMtx.lock(); |
donatien | 0:3b2f052c333b | 79 | m_result = NULL; |
donatien | 0:3b2f052c333b | 80 | m_maxResultLength = 0; |
donatien | 0:3b2f052c333b | 81 | m_responseMtx.unlock(); |
donatien | 0:3b2f052c333b | 82 | |
donatien | 0:3b2f052c333b | 83 | if(res <= 0) |
donatien | 0:3b2f052c333b | 84 | { |
donatien | 0:3b2f052c333b | 85 | DBG("No result received"); |
donatien | 0:3b2f052c333b | 86 | ret = m_pIf->executeSimple("AT+CUSD=2", NULL, DEFAULT_TIMEOUT); //Cancel command |
donatien | 0:3b2f052c333b | 87 | if( ret != OK ) |
donatien | 0:3b2f052c333b | 88 | { |
donatien | 0:3b2f052c333b | 89 | return NET_PROTOCOL; |
donatien | 0:3b2f052c333b | 90 | } |
donatien | 0:3b2f052c333b | 91 | return NET_TIMEOUT; |
donatien | 0:3b2f052c333b | 92 | } |
donatien | 0:3b2f052c333b | 93 | |
donatien | 0:3b2f052c333b | 94 | DBG("Result received: %s", result); |
donatien | 0:3b2f052c333b | 95 | |
donatien | 0:3b2f052c333b | 96 | return OK; |
donatien | 0:3b2f052c333b | 97 | |
donatien | 0:3b2f052c333b | 98 | } |
donatien | 0:3b2f052c333b | 99 | |
donatien | 0:3b2f052c333b | 100 | /*virtual*/ bool USSDInterface::isATCodeHandled(const char* atCode) //Is this AT code handled |
donatien | 0:3b2f052c333b | 101 | { |
donatien | 0:3b2f052c333b | 102 | DBG("AT code is %s", atCode); |
donatien | 0:3b2f052c333b | 103 | if( strcmp("+CUSD", atCode) == 0 ) |
donatien | 0:3b2f052c333b | 104 | { |
donatien | 0:3b2f052c333b | 105 | return true; |
donatien | 0:3b2f052c333b | 106 | } |
donatien | 0:3b2f052c333b | 107 | |
donatien | 0:3b2f052c333b | 108 | DBG("Not handled"); |
donatien | 0:3b2f052c333b | 109 | return false; |
donatien | 0:3b2f052c333b | 110 | } |
donatien | 0:3b2f052c333b | 111 | |
donatien | 0:3b2f052c333b | 112 | /*virtual*/ void USSDInterface::onDispatchStart() |
donatien | 0:3b2f052c333b | 113 | { |
donatien | 0:3b2f052c333b | 114 | |
donatien | 0:3b2f052c333b | 115 | |
donatien | 0:3b2f052c333b | 116 | } |
donatien | 0:3b2f052c333b | 117 | |
donatien | 0:3b2f052c333b | 118 | /*virtual*/ void USSDInterface::onDispatchStop() |
donatien | 0:3b2f052c333b | 119 | { |
donatien | 0:3b2f052c333b | 120 | |
donatien | 0:3b2f052c333b | 121 | } |
donatien | 0:3b2f052c333b | 122 | |
donatien | 0:3b2f052c333b | 123 | /*virtual*/ void USSDInterface::onEvent(const char* atCode, const char* evt) |
donatien | 0:3b2f052c333b | 124 | { |
donatien | 0:3b2f052c333b | 125 | if( strcmp("+CUSD", atCode) != 0 ) |
donatien | 0:3b2f052c333b | 126 | { |
donatien | 0:3b2f052c333b | 127 | return; //Not supported |
donatien | 0:3b2f052c333b | 128 | } |
donatien | 0:3b2f052c333b | 129 | |
donatien | 0:3b2f052c333b | 130 | char* pStart = (char*) strchr(evt,'\"'); |
donatien | 0:3b2f052c333b | 131 | if(pStart==NULL) |
donatien | 0:3b2f052c333b | 132 | { |
donatien | 0:3b2f052c333b | 133 | return; //Invalid/incomplete response |
donatien | 0:3b2f052c333b | 134 | } |
donatien | 0:3b2f052c333b | 135 | pStart++; //Point to first char of response |
donatien | 0:3b2f052c333b | 136 | char* pEnd = (char*) strchr(pStart,'\"'); |
donatien | 0:3b2f052c333b | 137 | if(pEnd==NULL) |
donatien | 0:3b2f052c333b | 138 | { |
donatien | 0:3b2f052c333b | 139 | return; //Invalid/incomplete response |
donatien | 0:3b2f052c333b | 140 | } |
donatien | 0:3b2f052c333b | 141 | m_responseMtx.lock(); |
donatien | 0:3b2f052c333b | 142 | if(m_maxResultLength == 0) //No pending command |
donatien | 0:3b2f052c333b | 143 | { |
donatien | 0:3b2f052c333b | 144 | m_responseMtx.unlock(); |
donatien | 0:3b2f052c333b | 145 | return; |
donatien | 0:3b2f052c333b | 146 | } |
donatien | 0:3b2f052c333b | 147 | size_t cpyLen = MIN( pEnd - pStart, m_maxResultLength - 1 ); |
donatien | 0:3b2f052c333b | 148 | memcpy(m_result, pStart, cpyLen); |
donatien | 0:3b2f052c333b | 149 | m_result[cpyLen] = '\0'; |
donatien | 0:3b2f052c333b | 150 | m_responseMtx.unlock(); |
donatien | 0:3b2f052c333b | 151 | m_responseSphre.release(); //Signal user thread that response is ready |
donatien | 0:3b2f052c333b | 152 | } |