summaryrefslogtreecommitdiff
path: root/tmk_core/tool/mbed/mbed-sdk/libraries/net/cellular/UbloxUSBModem/UbloxModem.cpp
blob: a9fec4cbf059af17011dca039cfe2cd813746cff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/* UbloxModem.cpp */
/* Copyright (C) 2012 mbed.org, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#define __DEBUG__ 3
#ifndef __MODULE__
#define __MODULE__ "UbloxModem.cpp"
#endif

#include "core/fwk.h"
#include "sms/GSMSMSInterface.h"
#include "sms/CDMASMSInterface.h"

#include "UbloxModem.h"

UbloxModem::UbloxModem(IOStream* atStream, IOStream* pppStream) :
   m_at(atStream),                          // Construct ATCommandsInterface with the AT serial channel
   m_CdmaSms(&m_at),                         // Construct SMSInterface with the ATCommandsInterface
   m_GsmSms(&m_at),                          // Construct SMSInterface with the ATCommandsInterface
   m_ussd(&m_at),                           // Construct USSDInterface with the ATCommandsInterface
   m_linkMonitor(&m_at),                    // Construct LinkMonitor with the ATCommandsInterface
   m_ppp(pppStream ? pppStream : atStream),    // Construct PPPIPInterface with the PPP serial channel
   m_ipInit(false),                         // PPIPInterface connection is initially down
   m_smsInit(false),                        // SMSInterface starts un-initialised
   m_ussdInit(false),                       // USSDInterface starts un-initialised 
   m_linkMonitorInit(false),                // LinkMonitor subsystem starts un-initialised
   m_atOpen(false),                          // ATCommandsInterface starts in a closed state
   m_onePort(pppStream == NULL),
   m_type(UNKNOWN)
{
}


genericAtProcessor::genericAtProcessor()
{ 
    i = 0; 
    str[0] = '\0'; 
}

const char* genericAtProcessor::getResponse(void) 
{ 
    return str; 
}

int genericAtProcessor::onNewATResponseLine(ATCommandsInterface* pInst, const char* line)
{
    int l = strlen(line);
    if (i + l + 2 > sizeof(str))
        return NET_OVERFLOW;
    if (i) str[i++] = ',';
    strcat(&str[i], line);
    i += l;
    return OK;
}

int genericAtProcessor::onNewEntryPrompt(ATCommandsInterface* pInst)
{
    return OK;
}

class CREGProcessor : public IATCommandsProcessor
{
public:
  CREGProcessor(bool gsm) : status(STATUS_REGISTERING)
  {
    m_gsm = gsm;
  }
  enum REGISTERING_STATUS { STATUS_REGISTERING, STATUS_OK, STATUS_FAILED };
  REGISTERING_STATUS getStatus()
  {
    return status;
  }
  const char* getAtCommand()
  {
      return m_gsm ? "AT+CREG?" : "AT+CSS?";
  }
private:
  virtual int onNewATResponseLine(ATCommandsInterface* pInst, const char* line)
  {
    int r;
    if (m_gsm)
    {
        if( sscanf(line, "+CREG: %*d,%d", &r) == 1 )
        {
          status = (r == 1 || r == 5) ? STATUS_OK : 
                   (r == 0 || r == 2) ? STATUS_REGISTERING :
          //       (r == 3)           ? STATUS_FAILED :
                                        STATUS_FAILED;
        }
    }
    else
    {
        char bc[3] = "";
        if(sscanf(line, "%*s %*c,%2s,%*d",bc)==1)
        {
            status = (strcmp("Z", bc) == 0) ? STATUS_REGISTERING : STATUS_OK;
        }
    }
    return OK;
  }
  virtual int onNewEntryPrompt(ATCommandsInterface* pInst)
  {
    return OK;
  }
  volatile REGISTERING_STATUS status;
  bool m_gsm;
};

int UbloxModem::connect(const char* apn, const char* user, const char* password)
{
  if( !m_ipInit )
  {
    m_ipInit = true;
    m_ppp.init();
  }
  m_ppp.setup(user, password, (m_type != LISA_C200) ? DEFAULT_MSISDN_GSM : DEFAULT_MSISDN_CDMA);

  int ret = init();
  if(ret)
  {
    return ret;
  }

  if (m_onePort)
  {
     m_smsInit = false; //SMS status reset
     m_ussdInit = false; //USSD status reset
     m_linkMonitorInit = false; //Link monitor status reset
  }

  ATCommandsInterface::ATResult result;

  if(apn != NULL)
  {
    char cmd[48];
    int tries = 30;
    sprintf(cmd, "AT+CGDCONT=1,\"IP\",\"%s\"", apn);
    do //Try 30 times because for some reasons it can fail *a lot* with the K3772-Z dongle
    {
      ret = m_at.executeSimple(cmd, &result);
      DBG("Result of command: Err code=%d", ret);
      if(ret)
      {
        Thread::wait(500);
      }
    } while(ret && --tries);
    DBG("ATResult: AT return=%d (code %d)", result.result, result.code);
    DBG("APN set to %s", apn);
  }

  //Connect
  DBG("Connecting");
  if (m_onePort)
  {
    m_at.close(); // Closing AT parser
    m_atOpen = false; //Will need to be reinitialized afterwards
  }
  
  DBG("Connecting PPP");

  ret = m_ppp.connect();
  DBG("Result of connect: Err code=%d", ret);
  return ret;
}


int UbloxModem::disconnect()
{
  DBG("Disconnecting from PPP");
  int ret = m_ppp.disconnect();
  if(ret)
  {
    ERR("Disconnect returned %d, still trying to disconnect", ret);
  }

  //Ugly but leave dongle time to recover
  Thread::wait(500);

  if (m_onePort)
  {
    //ATCommandsInterface::ATResult result;
    DBG("Starting AT thread");
    ret = m_at.open();
    if(ret)
    {
      return ret;
    }
  }

  DBG("Trying to hangup");

  if (m_onePort)
  {
    //Reinit AT parser
    ret = m_at.init(false);
    DBG("Result of command: Err code=%d\n", ret);
    if(ret)
    {
      m_at.close(); // Closing AT parser
      DBG("AT Parser closed, could not complete disconnection");
      return NET_TIMEOUT;
    }
  
  }
  return OK;
}

int UbloxModem::sendSM(const char* number, const char* message)
{
  int ret = init();
  if(ret)
  {
    return ret;
  }

  ISMSInterface* sms;
  if (m_type == LISA_C200)  sms = &m_CdmaSms;
  else                      sms = &m_GsmSms;
  if(!m_smsInit)
  {
    ret = sms->init();
    if(ret)
    {
      return ret;
    }
    m_smsInit = true;
  }

  ret = sms->send(number, message);
  if(ret)
  {
    return ret;
  }

  return OK;
}

int UbloxModem::getSM(char* number, char* message, size_t maxLength)
{
  int ret = init();
  if(ret)
  {
    return ret;
  }

  ISMSInterface* sms;
  if (m_type == LISA_C200)  sms = &m_CdmaSms;
  else                      sms = &m_GsmSms;
  if(!m_smsInit)
  {
    ret = sms->init();
    if(ret)
    {
      return ret;
    }
    m_smsInit = true;
  }

  ret = sms->get(number, message, maxLength);
  if(ret)
  {
    return ret;
  }

  return OK;
}

int UbloxModem::getSMCount(size_t* pCount)
{
  int ret = init();
  if(ret)
  {
    return ret;
  }

  ISMSInterface* sms;
  if (m_type == LISA_C200)  sms = &m_CdmaSms;
  else                      sms = &m_GsmSms;
  if(!m_smsInit)
  {
    ret = sms->init();
    if(ret)
    {
      return ret;
    }
    m_smsInit = true;
  }

  ret = sms->getCount(pCount);
  if(ret)
  {
    return ret;
  }

  return OK;
}

ATCommandsInterface* UbloxModem::getATCommandsInterface()
{
  return &m_at;
}

int UbloxModem::init()
{
  if(m_atOpen)
  {
    return OK;
  }
  
  DBG("Starting AT thread if needed");
  int ret = m_at.open();
  if(ret)
  {
    return ret;
  }
  
  DBG("Sending initialisation commands");
  ret = m_at.init(false);
  if(ret)
  {
    return ret;
  }
  
  
  ATCommandsInterface::ATResult result;
  genericAtProcessor atiProcessor;
  ret = m_at.execute("ATI", &atiProcessor, &result);
  if (OK != ret)
    return ret;
  const char* info = atiProcessor.getResponse();
  INFO("Modem Identification [%s]", info);
  if (strstr(info, "LISA-C200")) {
      m_type = LISA_C200;
      m_onePort = true; // force use of only one port
  }
  else if (strstr(info, "LISA-U200")) {
      m_type = LISA_U200;
  }
  else if (strstr(info, "SARA-G350")) {
      m_type = SARA_G350;
  }
  
  // enable the network indicator 
  if (m_type == SARA_G350) {
      m_at.executeSimple("AT+UGPIOC=16,2", &result);
  }
  else if (m_type == LISA_U200) {
      m_at.executeSimple("AT+UGPIOC=20,2", &result); 
  }
  else if (m_type == LISA_C200) {
      // LISA-C200 02S/22S : GPIO1 do not support network status indication
      // m_at.executeSimple("AT+UGPIOC=20,2", &result); 
  }
  INFO("Modem Identification [%s]", info);
  
  CREGProcessor cregProcessor(m_type != LISA_C200);
  //Wait for network registration
  do
  {
    DBG("Waiting for network registration");
    ret = m_at.execute(cregProcessor.getAtCommand(), &cregProcessor, &result);
    DBG("Result of command: Err code=%d\n", ret);
    DBG("ATResult: AT return=%d (code %d)\n", result.result, result.code);
    if(cregProcessor.getStatus() == CREGProcessor::STATUS_REGISTERING)
    {
      Thread::wait(3000);
    }
  } while(cregProcessor.getStatus() == CREGProcessor::STATUS_REGISTERING);
  if(cregProcessor.getStatus() == CREGProcessor::STATUS_FAILED)
  {
    ERR("Registration denied");
    return NET_AUTH;
  }
 
  m_atOpen = true;

  return OK;
}

int UbloxModem::cleanup()
{
  if(m_ppp.isConnected())
  {
    WARN("Data connection is still open"); //Try to encourage good behaviour from the user
    m_ppp.disconnect(); 
  }
  
  m_smsInit = false;
  m_ussdInit = false;
  m_linkMonitorInit = false;
  //We don't reset m_ipInit as PPPIPInterface::init() only needs to be called once
  
  if(m_atOpen)
  {
    m_at.close();
    m_atOpen = false;
  }
  
  return OK;
}

int UbloxModem::sendUSSD(const char* command, char* result, size_t maxLength)
{
  int ret = init();
  if(ret)
  {
    return ret;
  }

  if(!m_ussdInit)
  {
    ret = m_ussd.init();
    if(ret)
    {
      return ret;
    }
    m_ussdInit = true;
  }

  ret = m_ussd.send(command, result, maxLength);
  if(ret)
  {
    return ret;
  }

  return OK;
}

int UbloxModem::getLinkState(int* pRssi, LinkMonitor::REGISTRATION_STATE* pRegistrationState, LinkMonitor::BEARER* pBearer)
{
  int ret = init();
  if(ret)
  {
    return ret;
  }
  
  if(!m_linkMonitorInit)
  {
    ret = m_linkMonitor.init(m_type != LISA_C200);
    if(ret)
    {
      return ret;
    }
    m_linkMonitorInit = true;
  }

  ret = m_linkMonitor.getState(pRssi, pRegistrationState, pBearer);
  if(ret)
  {
    return ret;
  }

  return OK;
}

int UbloxModem::getPhoneNumber(char* phoneNumber)
{
  int ret = init();
  if(ret)
  {
    return ret;
  }
  
  if(!m_linkMonitorInit)
  {
    ret = m_linkMonitor.init(m_type != LISA_C200);
    if(ret)
    {
      return ret;
    }
    m_linkMonitorInit = true;
  }

  ret = m_linkMonitor.getPhoneNumber(phoneNumber);
  if(ret)
  {
    return ret;
  }

  return OK;
}

#include "USBHost.h"
#include "UbloxGSMModemInitializer.h"
#include "UbloxCDMAModemInitializer.h"

UbloxUSBModem::UbloxUSBModem() :
   UbloxModem(&m_atStream, &m_pppStream),
   m_dongle(),                              // Construct WANDongle: USB interface with two serial channels to the modem (USBSerialStream objects)
   m_atStream(m_dongle.getSerial(1)),       // AT commands are sent down one serial channel.
   m_pppStream(m_dongle.getSerial(0)),      // PPP connections are managed via another serial channel.
   m_dongleConnected(false)                 // Dongle is initially not ready for anything
{
    USBHost* host = USBHost::getHostInst();
    m_dongle.addInitializer(new UbloxGSMModemInitializer(host));
    m_dongle.addInitializer(new UbloxCDMAModemInitializer(host));
}

int UbloxUSBModem::init()
{
  if( !m_dongleConnected )
  {
    m_dongleConnected = true;
    while( !m_dongle.connected() )
    {
      m_dongle.tryConnect();
      Thread::wait(10);
    }
    if(m_dongle.getDongleType() == WAN_DONGLE_TYPE_UBLOX_LISAU200)
    {
      INFO("Using a u-blox LISA-U200 3G/WCDMA Modem");
      m_type = LISA_U200;
    }
    else if(m_dongle.getDongleType() == WAN_DONGLE_TYPE_UBLOX_LISAC200)
    {
      INFO("Using a u-blox LISA-C200 CDMA Modem");
      m_type = LISA_C200;
      m_onePort = true;
    }
    else
    {
      WARN("Using an Unknown Dongle");
    }
  }
  return UbloxModem::init();
}

int UbloxUSBModem::cleanup()
{
  UbloxModem::cleanup();
  m_dongle.disconnect();
  m_dongleConnected = false;
  return OK;
}

UbloxSerModem::UbloxSerModem() :
   UbloxModem(&m_atStream, NULL),
   m_Serial(P0_15/*MDMTXD*/,P0_16/*MDMRXD*/),
   m_atStream(m_Serial)
{
  m_Serial.baud(115200/*MDMBAUD*/);
  m_Serial.set_flow_control(SerialBase::RTSCTS, P0_22/*MDMRTS*/, P0_17/*MDMCTS*/);
}