UbloxModem.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /* UbloxModem.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. #define __DEBUG__ 3
  20. #ifndef __MODULE__
  21. #define __MODULE__ "UbloxModem.cpp"
  22. #endif
  23. #include "core/fwk.h"
  24. #include "sms/GSMSMSInterface.h"
  25. #include "sms/CDMASMSInterface.h"
  26. #include "UbloxModem.h"
  27. UbloxModem::UbloxModem(IOStream* atStream, IOStream* pppStream) :
  28. m_at(atStream), // Construct ATCommandsInterface with the AT serial channel
  29. m_CdmaSms(&m_at), // Construct SMSInterface with the ATCommandsInterface
  30. m_GsmSms(&m_at), // Construct SMSInterface with the ATCommandsInterface
  31. m_ussd(&m_at), // Construct USSDInterface with the ATCommandsInterface
  32. m_linkMonitor(&m_at), // Construct LinkMonitor with the ATCommandsInterface
  33. m_ppp(pppStream ? pppStream : atStream), // Construct PPPIPInterface with the PPP serial channel
  34. m_ipInit(false), // PPIPInterface connection is initially down
  35. m_smsInit(false), // SMSInterface starts un-initialised
  36. m_ussdInit(false), // USSDInterface starts un-initialised
  37. m_linkMonitorInit(false), // LinkMonitor subsystem starts un-initialised
  38. m_atOpen(false), // ATCommandsInterface starts in a closed state
  39. m_onePort(pppStream == NULL),
  40. m_type(UNKNOWN)
  41. {
  42. }
  43. genericAtProcessor::genericAtProcessor()
  44. {
  45. i = 0;
  46. str[0] = '\0';
  47. }
  48. const char* genericAtProcessor::getResponse(void)
  49. {
  50. return str;
  51. }
  52. int genericAtProcessor::onNewATResponseLine(ATCommandsInterface* pInst, const char* line)
  53. {
  54. int l = strlen(line);
  55. if (i + l + 2 > sizeof(str))
  56. return NET_OVERFLOW;
  57. if (i) str[i++] = ',';
  58. strcat(&str[i], line);
  59. i += l;
  60. return OK;
  61. }
  62. int genericAtProcessor::onNewEntryPrompt(ATCommandsInterface* pInst)
  63. {
  64. return OK;
  65. }
  66. class CREGProcessor : public IATCommandsProcessor
  67. {
  68. public:
  69. CREGProcessor(bool gsm) : status(STATUS_REGISTERING)
  70. {
  71. m_gsm = gsm;
  72. }
  73. enum REGISTERING_STATUS { STATUS_REGISTERING, STATUS_OK, STATUS_FAILED };
  74. REGISTERING_STATUS getStatus()
  75. {
  76. return status;
  77. }
  78. const char* getAtCommand()
  79. {
  80. return m_gsm ? "AT+CREG?" : "AT+CSS?";
  81. }
  82. private:
  83. virtual int onNewATResponseLine(ATCommandsInterface* pInst, const char* line)
  84. {
  85. int r;
  86. if (m_gsm)
  87. {
  88. if( sscanf(line, "+CREG: %*d,%d", &r) == 1 )
  89. {
  90. status = (r == 1 || r == 5) ? STATUS_OK :
  91. (r == 0 || r == 2) ? STATUS_REGISTERING :
  92. // (r == 3) ? STATUS_FAILED :
  93. STATUS_FAILED;
  94. }
  95. }
  96. else
  97. {
  98. char bc[3] = "";
  99. if(sscanf(line, "%*s %*c,%2s,%*d",bc)==1)
  100. {
  101. status = (strcmp("Z", bc) == 0) ? STATUS_REGISTERING : STATUS_OK;
  102. }
  103. }
  104. return OK;
  105. }
  106. virtual int onNewEntryPrompt(ATCommandsInterface* pInst)
  107. {
  108. return OK;
  109. }
  110. volatile REGISTERING_STATUS status;
  111. bool m_gsm;
  112. };
  113. int UbloxModem::connect(const char* apn, const char* user, const char* password)
  114. {
  115. if( !m_ipInit )
  116. {
  117. m_ipInit = true;
  118. m_ppp.init();
  119. }
  120. m_ppp.setup(user, password, (m_type != LISA_C200) ? DEFAULT_MSISDN_GSM : DEFAULT_MSISDN_CDMA);
  121. int ret = init();
  122. if(ret)
  123. {
  124. return ret;
  125. }
  126. if (m_onePort)
  127. {
  128. m_smsInit = false; //SMS status reset
  129. m_ussdInit = false; //USSD status reset
  130. m_linkMonitorInit = false; //Link monitor status reset
  131. }
  132. ATCommandsInterface::ATResult result;
  133. if(apn != NULL)
  134. {
  135. char cmd[48];
  136. int tries = 30;
  137. sprintf(cmd, "AT+CGDCONT=1,\"IP\",\"%s\"", apn);
  138. do //Try 30 times because for some reasons it can fail *a lot* with the K3772-Z dongle
  139. {
  140. ret = m_at.executeSimple(cmd, &result);
  141. DBG("Result of command: Err code=%d", ret);
  142. if(ret)
  143. {
  144. Thread::wait(500);
  145. }
  146. } while(ret && --tries);
  147. DBG("ATResult: AT return=%d (code %d)", result.result, result.code);
  148. DBG("APN set to %s", apn);
  149. }
  150. //Connect
  151. DBG("Connecting");
  152. if (m_onePort)
  153. {
  154. m_at.close(); // Closing AT parser
  155. m_atOpen = false; //Will need to be reinitialized afterwards
  156. }
  157. DBG("Connecting PPP");
  158. ret = m_ppp.connect();
  159. DBG("Result of connect: Err code=%d", ret);
  160. return ret;
  161. }
  162. int UbloxModem::disconnect()
  163. {
  164. DBG("Disconnecting from PPP");
  165. int ret = m_ppp.disconnect();
  166. if(ret)
  167. {
  168. ERR("Disconnect returned %d, still trying to disconnect", ret);
  169. }
  170. //Ugly but leave dongle time to recover
  171. Thread::wait(500);
  172. if (m_onePort)
  173. {
  174. //ATCommandsInterface::ATResult result;
  175. DBG("Starting AT thread");
  176. ret = m_at.open();
  177. if(ret)
  178. {
  179. return ret;
  180. }
  181. }
  182. DBG("Trying to hangup");
  183. if (m_onePort)
  184. {
  185. //Reinit AT parser
  186. ret = m_at.init(false);
  187. DBG("Result of command: Err code=%d\n", ret);
  188. if(ret)
  189. {
  190. m_at.close(); // Closing AT parser
  191. DBG("AT Parser closed, could not complete disconnection");
  192. return NET_TIMEOUT;
  193. }
  194. }
  195. return OK;
  196. }
  197. int UbloxModem::sendSM(const char* number, const char* message)
  198. {
  199. int ret = init();
  200. if(ret)
  201. {
  202. return ret;
  203. }
  204. ISMSInterface* sms;
  205. if (m_type == LISA_C200) sms = &m_CdmaSms;
  206. else sms = &m_GsmSms;
  207. if(!m_smsInit)
  208. {
  209. ret = sms->init();
  210. if(ret)
  211. {
  212. return ret;
  213. }
  214. m_smsInit = true;
  215. }
  216. ret = sms->send(number, message);
  217. if(ret)
  218. {
  219. return ret;
  220. }
  221. return OK;
  222. }
  223. int UbloxModem::getSM(char* number, char* message, size_t maxLength)
  224. {
  225. int ret = init();
  226. if(ret)
  227. {
  228. return ret;
  229. }
  230. ISMSInterface* sms;
  231. if (m_type == LISA_C200) sms = &m_CdmaSms;
  232. else sms = &m_GsmSms;
  233. if(!m_smsInit)
  234. {
  235. ret = sms->init();
  236. if(ret)
  237. {
  238. return ret;
  239. }
  240. m_smsInit = true;
  241. }
  242. ret = sms->get(number, message, maxLength);
  243. if(ret)
  244. {
  245. return ret;
  246. }
  247. return OK;
  248. }
  249. int UbloxModem::getSMCount(size_t* pCount)
  250. {
  251. int ret = init();
  252. if(ret)
  253. {
  254. return ret;
  255. }
  256. ISMSInterface* sms;
  257. if (m_type == LISA_C200) sms = &m_CdmaSms;
  258. else sms = &m_GsmSms;
  259. if(!m_smsInit)
  260. {
  261. ret = sms->init();
  262. if(ret)
  263. {
  264. return ret;
  265. }
  266. m_smsInit = true;
  267. }
  268. ret = sms->getCount(pCount);
  269. if(ret)
  270. {
  271. return ret;
  272. }
  273. return OK;
  274. }
  275. ATCommandsInterface* UbloxModem::getATCommandsInterface()
  276. {
  277. return &m_at;
  278. }
  279. int UbloxModem::init()
  280. {
  281. if(m_atOpen)
  282. {
  283. return OK;
  284. }
  285. DBG("Starting AT thread if needed");
  286. int ret = m_at.open();
  287. if(ret)
  288. {
  289. return ret;
  290. }
  291. DBG("Sending initialisation commands");
  292. ret = m_at.init(false);
  293. if(ret)
  294. {
  295. return ret;
  296. }
  297. ATCommandsInterface::ATResult result;
  298. genericAtProcessor atiProcessor;
  299. ret = m_at.execute("ATI", &atiProcessor, &result);
  300. if (OK != ret)
  301. return ret;
  302. const char* info = atiProcessor.getResponse();
  303. INFO("Modem Identification [%s]", info);
  304. if (strstr(info, "LISA-C200")) {
  305. m_type = LISA_C200;
  306. m_onePort = true; // force use of only one port
  307. }
  308. else if (strstr(info, "LISA-U200")) {
  309. m_type = LISA_U200;
  310. }
  311. else if (strstr(info, "SARA-G350")) {
  312. m_type = SARA_G350;
  313. }
  314. // enable the network indicator
  315. if (m_type == SARA_G350) {
  316. m_at.executeSimple("AT+UGPIOC=16,2", &result);
  317. }
  318. else if (m_type == LISA_U200) {
  319. m_at.executeSimple("AT+UGPIOC=20,2", &result);
  320. }
  321. else if (m_type == LISA_C200) {
  322. // LISA-C200 02S/22S : GPIO1 do not support network status indication
  323. // m_at.executeSimple("AT+UGPIOC=20,2", &result);
  324. }
  325. INFO("Modem Identification [%s]", info);
  326. CREGProcessor cregProcessor(m_type != LISA_C200);
  327. //Wait for network registration
  328. do
  329. {
  330. DBG("Waiting for network registration");
  331. ret = m_at.execute(cregProcessor.getAtCommand(), &cregProcessor, &result);
  332. DBG("Result of command: Err code=%d\n", ret);
  333. DBG("ATResult: AT return=%d (code %d)\n", result.result, result.code);
  334. if(cregProcessor.getStatus() == CREGProcessor::STATUS_REGISTERING)
  335. {
  336. Thread::wait(3000);
  337. }
  338. } while(cregProcessor.getStatus() == CREGProcessor::STATUS_REGISTERING);
  339. if(cregProcessor.getStatus() == CREGProcessor::STATUS_FAILED)
  340. {
  341. ERR("Registration denied");
  342. return NET_AUTH;
  343. }
  344. m_atOpen = true;
  345. return OK;
  346. }
  347. int UbloxModem::cleanup()
  348. {
  349. if(m_ppp.isConnected())
  350. {
  351. WARN("Data connection is still open"); //Try to encourage good behaviour from the user
  352. m_ppp.disconnect();
  353. }
  354. m_smsInit = false;
  355. m_ussdInit = false;
  356. m_linkMonitorInit = false;
  357. //We don't reset m_ipInit as PPPIPInterface::init() only needs to be called once
  358. if(m_atOpen)
  359. {
  360. m_at.close();
  361. m_atOpen = false;
  362. }
  363. return OK;
  364. }
  365. int UbloxModem::sendUSSD(const char* command, char* result, size_t maxLength)
  366. {
  367. int ret = init();
  368. if(ret)
  369. {
  370. return ret;
  371. }
  372. if(!m_ussdInit)
  373. {
  374. ret = m_ussd.init();
  375. if(ret)
  376. {
  377. return ret;
  378. }
  379. m_ussdInit = true;
  380. }
  381. ret = m_ussd.send(command, result, maxLength);
  382. if(ret)
  383. {
  384. return ret;
  385. }
  386. return OK;
  387. }
  388. int UbloxModem::getLinkState(int* pRssi, LinkMonitor::REGISTRATION_STATE* pRegistrationState, LinkMonitor::BEARER* pBearer)
  389. {
  390. int ret = init();
  391. if(ret)
  392. {
  393. return ret;
  394. }
  395. if(!m_linkMonitorInit)
  396. {
  397. ret = m_linkMonitor.init(m_type != LISA_C200);
  398. if(ret)
  399. {
  400. return ret;
  401. }
  402. m_linkMonitorInit = true;
  403. }
  404. ret = m_linkMonitor.getState(pRssi, pRegistrationState, pBearer);
  405. if(ret)
  406. {
  407. return ret;
  408. }
  409. return OK;
  410. }
  411. int UbloxModem::getPhoneNumber(char* phoneNumber)
  412. {
  413. int ret = init();
  414. if(ret)
  415. {
  416. return ret;
  417. }
  418. if(!m_linkMonitorInit)
  419. {
  420. ret = m_linkMonitor.init(m_type != LISA_C200);
  421. if(ret)
  422. {
  423. return ret;
  424. }
  425. m_linkMonitorInit = true;
  426. }
  427. ret = m_linkMonitor.getPhoneNumber(phoneNumber);
  428. if(ret)
  429. {
  430. return ret;
  431. }
  432. return OK;
  433. }
  434. #include "USBHost.h"
  435. #include "UbloxGSMModemInitializer.h"
  436. #include "UbloxCDMAModemInitializer.h"
  437. UbloxUSBModem::UbloxUSBModem() :
  438. UbloxModem(&m_atStream, &m_pppStream),
  439. m_dongle(), // Construct WANDongle: USB interface with two serial channels to the modem (USBSerialStream objects)
  440. m_atStream(m_dongle.getSerial(1)), // AT commands are sent down one serial channel.
  441. m_pppStream(m_dongle.getSerial(0)), // PPP connections are managed via another serial channel.
  442. m_dongleConnected(false) // Dongle is initially not ready for anything
  443. {
  444. USBHost* host = USBHost::getHostInst();
  445. m_dongle.addInitializer(new UbloxGSMModemInitializer(host));
  446. m_dongle.addInitializer(new UbloxCDMAModemInitializer(host));
  447. }
  448. int UbloxUSBModem::init()
  449. {
  450. if( !m_dongleConnected )
  451. {
  452. m_dongleConnected = true;
  453. while( !m_dongle.connected() )
  454. {
  455. m_dongle.tryConnect();
  456. Thread::wait(10);
  457. }
  458. if(m_dongle.getDongleType() == WAN_DONGLE_TYPE_UBLOX_LISAU200)
  459. {
  460. INFO("Using a u-blox LISA-U200 3G/WCDMA Modem");
  461. m_type = LISA_U200;
  462. }
  463. else if(m_dongle.getDongleType() == WAN_DONGLE_TYPE_UBLOX_LISAC200)
  464. {
  465. INFO("Using a u-blox LISA-C200 CDMA Modem");
  466. m_type = LISA_C200;
  467. m_onePort = true;
  468. }
  469. else
  470. {
  471. WARN("Using an Unknown Dongle");
  472. }
  473. }
  474. return UbloxModem::init();
  475. }
  476. int UbloxUSBModem::cleanup()
  477. {
  478. UbloxModem::cleanup();
  479. m_dongle.disconnect();
  480. m_dongleConnected = false;
  481. return OK;
  482. }
  483. UbloxSerModem::UbloxSerModem() :
  484. UbloxModem(&m_atStream, NULL),
  485. m_Serial(P0_15/*MDMTXD*/,P0_16/*MDMRXD*/),
  486. m_atStream(m_Serial)
  487. {
  488. m_Serial.baud(115200/*MDMBAUD*/);
  489. m_Serial.set_flow_control(SerialBase::RTSCTS, P0_22/*MDMRTS*/, P0_17/*MDMCTS*/);
  490. }