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