forked from ARMmbed/mbed-os
-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathAT_CellularContext.cpp
1088 lines (943 loc) · 35.7 KB
/
AT_CellularContext.cpp
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2018, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include "AT_CellularContext.h"
#include "AT_CellularNetwork.h"
#include "AT_CellularStack.h"
#include "AT_ControlPlane_netif.h"
#include "AT_CellularDevice.h"
#include "CellularLog.h"
#if (DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
#include "drivers/BufferedSerial.h"
#endif // #if DEVICE_SERIAL
#include "rtos/ThisThread.h"
#define NETWORK_TIMEOUT 30min
#define DEVICE_TIMEOUT 5min
// Timeout to wait for URC indicating ciot optimization support from network
#define CP_OPT_NW_REPLY_TIMEOUT 3s
#if NSAPI_PPP_AVAILABLE
#define AT_SYNC_TIMEOUT 1s
#include "nsapi_ppp.h"
#endif
#if MBED_CONF_CELLULAR_USE_APN_LOOKUP
#include "CellularInformation.h"
#include "APN_db.h"
#endif //MBED_CONF_CELLULAR_USE_APN_LOOKUP
using namespace mbed_cellular_util;
using namespace mbed;
using namespace rtos;
using namespace std::chrono_literals;
AT_CellularContext::AT_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req) :
_current_op(OP_INVALID), _dcd_pin(NC), _active_high(false), _rat(CATM1), _band(BAND_20), _cp_req(cp_req), _is_connected(false), _at(at)
{
tr_info("New CellularContext %s (%p)", apn ? apn : "", this);
_nonip_req = nonip_req;
_apn = apn;
_device = device;
}
AT_CellularContext::~AT_CellularContext()
{
tr_info("Delete CellularContext with apn: [%s] (%p)", _apn ? _apn : "", this);
_is_blocking = true;
(void)disconnect();
if (_nw) {
_device->close_network();
}
if (_cp_netif) {
delete _cp_netif;
}
}
#if (DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
nsapi_error_t AT_CellularContext::configure_hup(PinName dcd_pin, bool active_high)
{
_dcd_pin = dcd_pin;
_active_high = active_high;
enable_hup(false);
return NSAPI_ERROR_OK;
}
#endif // #if DEVICE_SERIAL
void AT_CellularContext::enable_hup(bool enable)
{
if (_dcd_pin != NC) {
#if (DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
static_cast<BufferedSerial *>(_at.get_file_handle())->set_data_carrier_detect(enable ? _dcd_pin : NC, _active_high);
#endif // #if DEVICE_SERIAL
}
}
AT_CellularDevice *AT_CellularContext::get_device() const
{
return static_cast<AT_CellularDevice *>(CellularContext::get_device());
}
void AT_CellularContext::do_connect_with_retry()
{
CellularContext::do_connect_with_retry();
}
nsapi_error_t AT_CellularContext::connect()
{
tr_info("CellularContext connect");
if (_is_connected) {
return NSAPI_ERROR_IS_CONNECTED;
}
call_network_cb(NSAPI_STATUS_CONNECTING);
nsapi_error_t err = _device->attach_to_network();
_cb_data.error = check_operation(err, OP_CONNECT);
_retry_count = 0;
if (_is_blocking) {
if (_cb_data.error == NSAPI_ERROR_OK || _cb_data.error == NSAPI_ERROR_ALREADY) {
do_connect_with_retry();
}
} else {
if (_cb_data.error == NSAPI_ERROR_ALREADY) {
// device is already attached, to be async we must use queue to connect and give proper callbacks
int id = _device->get_queue()->call(this, &AT_CellularContext::do_connect_with_retry);
if (id == 0) {
return NSAPI_ERROR_NO_MEMORY;
}
return NSAPI_ERROR_OK;
}
}
if (_cb_data.error == NSAPI_ERROR_ALREADY) {
return NSAPI_ERROR_OK;
}
return _cb_data.error;
}
nsapi_error_t AT_CellularContext::set_device_ready()
{
nsapi_error_t err = _device->set_device_ready();
return check_operation(err, OP_DEVICE_READY);
}
nsapi_error_t AT_CellularContext::set_sim_ready()
{
nsapi_error_t err = _device->set_sim_ready();
return check_operation(err, OP_SIM_READY);
}
nsapi_error_t AT_CellularContext::register_to_network()
{
nsapi_error_t err = _device->register_to_network();
return check_operation(err, OP_REGISTER);
}
nsapi_error_t AT_CellularContext::attach_to_network()
{
nsapi_error_t err = _device->attach_to_network();
return check_operation(err, OP_ATTACH);
}
nsapi_error_t AT_CellularContext::check_operation(nsapi_error_t err, ContextOperation op)
{
_current_op = op;
if (err == NSAPI_ERROR_IN_PROGRESS || err == NSAPI_ERROR_OK) {
if (_is_blocking) {
auto d = std::chrono::duration<uint32_t, std::milli>(get_timeout_for_operation(op));
int sema_acq = _semaphore.try_acquire_for(d); // cellular network searching may take several minutes
if (!sema_acq) {
tr_warning("No cellular connection");
return NSAPI_ERROR_TIMEOUT;
}
return _cb_data.error;// callback might have been completed with an error, must return that error here
}
}
return err;
}
nsapi_connection_status_t AT_CellularContext::get_connection_status() const
{
return _connect_status;
}
uint32_t AT_CellularContext::get_timeout_for_operation(ContextOperation op) const
{
std::chrono::duration<uint32_t, std::milli> timeout = NETWORK_TIMEOUT; // default timeout is 30 minutes as registration and attach may take time
if (op == OP_SIM_READY || op == OP_DEVICE_READY) {
timeout = DEVICE_TIMEOUT; // use 5 minutes for device ready and sim
}
return timeout.count();
}
bool AT_CellularContext::is_connected()
{
return _is_connected;
}
NetworkStack *AT_CellularContext::get_stack()
{
#if NSAPI_PPP_AVAILABLE
// use lwIP/PPP if modem does not have IP stack
if (!_stack) {
_stack = nsapi_ppp_get_stack();
}
#endif
return _stack;
}
nsapi_error_t AT_CellularContext::get_ip_address(SocketAddress *address)
{
if (!address) {
return NSAPI_ERROR_PARAMETER;
}
#if NSAPI_PPP_AVAILABLE
address->set_ip_address(nsapi_ppp_get_ip_addr(_at.get_file_handle()));
return NSAPI_ERROR_OK;
#else
if (!_stack) {
_stack = get_stack();
}
if (_stack) {
_stack->get_ip_address(address);
return NSAPI_ERROR_OK;
}
return NSAPI_ERROR_NO_CONNECTION;
#endif
}
char *AT_CellularContext::get_interface_name(char *interface_name)
{
if (_cid < 0) {
return NULL;
}
MBED_ASSERT(interface_name);
sprintf(interface_name, "ce%d", _cid);
return interface_name;
}
void AT_CellularContext::attach(Callback<void(nsapi_event_t, intptr_t)> status_cb)
{
_status_cb = status_cb;
}
nsapi_error_t AT_CellularContext::set_blocking(bool blocking)
{
nsapi_error_t err = NSAPI_ERROR_OK;
tr_info("CellularContext set blocking %d", blocking);
#if NSAPI_PPP_AVAILABLE
err = nsapi_ppp_set_blocking(blocking);
#endif
_is_blocking = blocking;
return err;
}
void AT_CellularContext::set_plmn(const char *plmn)
{
tr_info("CellularContext plmn %s", (plmn ? plmn : "NULL"));
_device->set_plmn(plmn);
}
void AT_CellularContext::set_sim_pin(const char *sim_pin)
{
_device->set_sim_pin(sim_pin);
}
nsapi_error_t AT_CellularContext::connect(const char *sim_pin, const char *apn, const char *uname,
const char *pwd)
{
set_sim_pin(sim_pin);
set_credentials(apn, uname, pwd);
return connect();
}
void AT_CellularContext::set_credentials(const char *apn, const char *uname, const char *pwd)
{
_apn = apn;
_uname = uname;
_pwd = pwd;
}
void AT_CellularContext::set_access_technology(RadioAccessTechnologyType rat)
{
_rat = rat;
}
void AT_CellularContext::set_band(FrequencyBand band)
{
_band = band;
}
// PDP Context handling
void AT_CellularContext::delete_current_context()
{
if (_cid <= 0) {
return;
}
tr_info("Delete context %d", _cid);
_at.clear_error();
_at.at_cmd_discard("+CGDCONT", "=", "%d", _cid);
if (_at.get_last_error() == NSAPI_ERROR_OK) {
set_cid(-1);
_new_context_set = false;
}
// there is nothing we can do if deleting of context fails. No point reporting an error (for example disconnect).
_at.clear_error();
}
nsapi_error_t AT_CellularContext::do_user_authentication()
{
// if user has defined user name and password we need to call CGAUTH before activating or modifying context
if (_pwd && _uname) {
if (!get_device()->get_property(AT_CellularDevice::PROPERTY_AT_CGAUTH)) {
return NSAPI_ERROR_UNSUPPORTED;
}
const bool stored_debug_state = _at.get_debug();
_at.set_debug(false);
_at.at_cmd_discard("+CGAUTH", "=", "%d%d%s%s", _cid, _authentication_type, _uname, _pwd);
_at.set_debug(stored_debug_state);
if (_at.get_last_error() != NSAPI_ERROR_OK) {
return NSAPI_ERROR_AUTH_FAILURE;
}
}
return NSAPI_ERROR_OK;
}
AT_CellularDevice::CellularProperty AT_CellularContext::pdp_type_t_to_cellular_property(pdp_type_t pdp_type)
{
AT_CellularDevice::CellularProperty prop = AT_CellularDevice::PROPERTY_IPV4_PDP_TYPE;
if (pdp_type == IPV6_PDP_TYPE) {
prop = AT_CellularDevice::PROPERTY_IPV6_PDP_TYPE;
} else if (pdp_type == IPV4V6_PDP_TYPE) {
prop = AT_CellularDevice::PROPERTY_IPV4V6_PDP_TYPE;
} else if (pdp_type == NON_IP_PDP_TYPE) {
prop = AT_CellularDevice::PROPERTY_NON_IP_PDP_TYPE;
}
return prop;
}
bool AT_CellularContext::get_context()
{
_at.cmd_start_stop("+CGDCONT", "?");
_at.resp_start("+CGDCONT:");
set_cid(-1);
int cid_max = 0; // needed when creating new context
char apn[MAX_ACCESSPOINT_NAME_LENGTH];
int apn_len = 0;
while (_at.info_resp()) {
int cid = _at.read_int();
if (cid > cid_max) {
cid_max = cid;
}
char pdp_type_from_context[10];
int pdp_type_len = _at.read_string(pdp_type_from_context, sizeof(pdp_type_from_context));
if (pdp_type_len > 0) {
apn_len = _at.read_string(apn, sizeof(apn));
if (apn_len >= 0) {
if (_apn && (strcmp(apn, _apn) != 0)) {
tr_debug("CID %d APN \"%s\"", cid, apn);
continue;
}
// APN matched -> Check PDP type
pdp_type_t pdp_type = string_to_pdp_type(pdp_type_from_context);
tr_debug("CID %d APN \"%s\" pdp_type %u", cid, apn, pdp_type);
// Accept exact matching PDP context type or dual PDP context for modems that support both IPv4 and IPv6 stacks
if (get_device()->get_property(pdp_type_t_to_cellular_property(pdp_type)) ||
((pdp_type == IPV4V6_PDP_TYPE && (get_device()->get_property(AT_CellularDevice::PROPERTY_IPV4_PDP_TYPE) &&
get_device()->get_property(AT_CellularDevice::PROPERTY_IPV6_PDP_TYPE))) && !_nonip_req)) {
_pdp_type = pdp_type;
set_cid(cid);
}
}
}
}
_at.resp_stop();
if (_cid == -1) { // no suitable context was found so create a new one
if (!set_new_context(cid_max + 1)) {
return false;
}
}
// save the apn
if (apn_len > 0 && !_apn) {
memcpy(_found_apn, apn, apn_len + 1);
}
tr_info("Found PDP context %d", _cid);
return true;
}
const char *AT_CellularContext::get_nonip_context_type_str()
{
return "Non-IP";
}
bool AT_CellularContext::set_new_context(int cid)
{
char pdp_type_str[8 + 1] = {0};
pdp_type_t pdp_type = IPV4_PDP_TYPE;
if (_nonip_req && _cp_in_use && get_device()->get_property(AT_CellularDevice::PROPERTY_NON_IP_PDP_TYPE)) {
strncpy(pdp_type_str, get_nonip_context_type_str(), sizeof(pdp_type_str));
pdp_type = NON_IP_PDP_TYPE;
} else if (get_device()->get_property(AT_CellularDevice::PROPERTY_IPV4V6_PDP_TYPE) ||
(get_device()->get_property(AT_CellularDevice::PROPERTY_IPV4_PDP_TYPE) &&
get_device()->get_property(AT_CellularDevice::PROPERTY_IPV6_PDP_TYPE))) {
strncpy(pdp_type_str, "IPV4V6", sizeof(pdp_type_str));
pdp_type = IPV4V6_PDP_TYPE;
} else if (get_device()->get_property(AT_CellularDevice::PROPERTY_IPV6_PDP_TYPE)) {
strncpy(pdp_type_str, "IPV6", sizeof(pdp_type_str));
pdp_type = IPV6_PDP_TYPE;
} else if (get_device()->get_property(AT_CellularDevice::PROPERTY_IPV4_PDP_TYPE)) {
strncpy(pdp_type_str, "IP", sizeof(pdp_type_str));
pdp_type = IPV4_PDP_TYPE;
} else {
return false;
}
//apn: "If the value is null or omitted, then the subscription value will be requested."
bool success = (_at.at_cmd_discard("+CGDCONT", "=", "%d%s%s", cid, pdp_type_str, _apn) == NSAPI_ERROR_OK);
if (success) {
_pdp_type = pdp_type;
set_cid(cid);
_new_context_set = true;
tr_info("New PDP context %d, type %d", _cid, pdp_type);
}
return success;
}
nsapi_error_t AT_CellularContext::enable_access_technology()
{
return enable_access_technology();
}
nsapi_error_t AT_CellularContext::do_activate_context()
{
if (_nonip_req && _cp_in_use) {
return activate_non_ip_context();
}
// In IP case but also when Non-IP is requested and
// control plane optimization is not established -> activate ip context
_nonip_req = false;
return activate_ip_context();
}
nsapi_error_t AT_CellularContext::activate_ip_context()
{
nsapi_error_t ret = find_and_activate_context();
#if !NSAPI_PPP_AVAILABLE
if (ret == NSAPI_ERROR_OK) {
pdpContextList_t params_list;
if (get_pdpcontext_params(params_list) == NSAPI_ERROR_OK) {
pdpcontext_params_t *pdp = params_list.get_head();
while (pdp) {
SocketAddress addr;
if (addr.set_ip_address(pdp->dns_secondary_addr)) {
nsapi_addr_t taddr = addr.get_addr();
for (int i = 0; i < ((taddr.version == NSAPI_IPv6) ? NSAPI_IPv6_BYTES : NSAPI_IPv4_BYTES); i++) {
if (taddr.bytes[i] != 0) { // check the address is not all zero
tr_info("DNS secondary %s", pdp->dns_secondary_addr);
char ifn[5]; // "ce" + two digit _cid + zero
add_dns_server(addr, get_interface_name(ifn));
break;
}
}
}
if (addr.set_ip_address(pdp->dns_primary_addr)) {
nsapi_addr_t taddr = addr.get_addr();
for (int i = 0; i < ((taddr.version == NSAPI_IPv6) ? NSAPI_IPv6_BYTES : NSAPI_IPv4_BYTES); i++) {
if (taddr.bytes[i] != 0) { // check the address is not all zero
tr_info("DNS primary %s", pdp->dns_primary_addr);
char ifn[5]; // "ce" + two digit _cid + zero
add_dns_server(addr, get_interface_name(ifn));
break;
}
}
}
pdp = pdp->next;
}
}
}
#endif
return ret;
}
nsapi_error_t AT_CellularContext::activate_non_ip_context()
{
return find_and_activate_context();
}
void AT_CellularContext::activate_context()
{
tr_info("Activate PDP context %d", _cid);
_at.at_cmd_discard("+CGACT", "=1,", "%d", _cid);
if (_at.get_last_error() == NSAPI_ERROR_OK) {
_is_context_activated = true;
}
}
nsapi_error_t AT_CellularContext::find_and_activate_context()
{
_at.lock();
nsapi_error_t err = NSAPI_ERROR_OK;
// try to find or create context of suitable type
if (get_context()) {
#if NSAPI_PPP_AVAILABLE
_at.unlock();
// in PPP we don't activate any context but leave it to PPP stack
return err;
#else
// try to authenticate user before activating or modifying context
err = do_user_authentication();
#endif // NSAPI_PPP_AVAILABLE
} else {
err = NSAPI_ERROR_NO_CONNECTION;
}
if (err != NSAPI_ERROR_OK) {
tr_error("Failed to activate network context! (%d)", err);
} else if (!(_nonip_req && _cp_in_use) && !get_stack()) {
// do check for stack to validate that we have support for stack
tr_error("No cellular stack!");
err = NSAPI_ERROR_UNSUPPORTED;
}
_is_context_active = false;
_is_context_activated = false;
if (err == NSAPI_ERROR_OK) {
_is_context_active = _nw->is_active_context(NULL, _cid);
if (!_is_context_active) {
activate_context();
}
err = (_at.get_last_error() == NSAPI_ERROR_OK) ? NSAPI_ERROR_OK : NSAPI_ERROR_NO_CONNECTION;
}
// If new PDP context was created and failed to activate, delete it
if (err != NSAPI_ERROR_OK && _new_context_set) {
delete_current_context();
} else if (err == NSAPI_ERROR_OK) {
_is_context_active = true;
_is_context_activated = true;
}
_at.unlock();
return err;
}
void AT_CellularContext::do_connect()
{
if (!_is_context_active) {
_cb_data.error = do_activate_context();
} else {
_cb_data.error = NSAPI_ERROR_OK;
}
#if !NSAPI_PPP_AVAILABLE
// in PPP mode we did not activate any context, just searched the correct _cid
if (_status_cb) {
_status_cb((nsapi_event_t)CellularActivatePDPContext, (intptr_t)&_cb_data);
}
#endif // !NSAPI_PPP_AVAILABLE
if (_cb_data.error != NSAPI_ERROR_OK) {
_is_connected = false;
return;
}
#if NSAPI_PPP_AVAILABLE
if (_cb_data.error == NSAPI_ERROR_OK) {
_at.lock();
_cb_data.error = open_data_channel();
_at.unlock();
if (_cb_data.error != NSAPI_ERROR_OK) {
_is_connected = false;
}
}
#else
_is_connected = true;
#endif
}
#if NSAPI_PPP_AVAILABLE
nsapi_error_t AT_CellularContext::open_data_channel()
{
// If Non-IP in use fail
if (_pdp_type == NON_IP_PDP_TYPE) {
tr_error("Attempt of PPP connect over NON-IP: failed to CONNECT");
return NSAPI_ERROR_PARAMETER;
}
tr_info("CellularContext PPP connect");
if (get_device()->get_property(AT_CellularDevice::PROPERTY_AT_CGDATA)) {
_at.cmd_start_stop("+CGDATA", "=\"PPP\",", "%d", _cid);
} else {
MBED_ASSERT(_cid >= 0 && _cid <= 99);
_at.cmd_start("ATD*99***");
_at.use_delimiter(false);
_at.write_int(_cid);
_at.write_string("#", false);
_at.use_delimiter(true);
_at.cmd_stop();
}
_at.resp_start("CONNECT", true);
if (_at.get_last_error()) {
tr_error("Failed to CONNECT");
return _at.get_last_error();
}
_at.set_is_filehandle_usable(false);
enable_hup(true);
/* Initialize PPP
* If blocking: mbed_ppp_init() is a blocking call, it will block until
connected, or timeout after 30 seconds*/
nsapi_error_t err = nsapi_ppp_connect(_at.get_file_handle(), callback(this, &AT_CellularContext::ppp_status_cb), _uname, _pwd, (nsapi_ip_stack_t)_pdp_type);
if (err) {
tr_error("nsapi_ppp_connect failed");
ppp_disconnected();
}
return err;
}
void AT_CellularContext::ppp_status_cb(nsapi_event_t ev, intptr_t ptr)
{
tr_debug("ppp_status_cb: event %d, ptr %d", ev, ptr);
if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_GLOBAL_UP) {
_is_connected = true;
} else {
// catch all NSAPI_STATUS_DISCONNECTED events but send to device only when we did not ask for disconnect.
if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_DISCONNECTED) {
if (_is_connected) { // set to false in disconnect() before calling nsapi_ppp_disconnect()
_is_connected = false;
ppp_disconnected();
_device->cellular_callback(ev, ptr, this);
}
return; // return here so if we were not in connected state we don't send NSAPI_STATUS_DISCONNECTED event
}
_is_connected = false;
}
// call device's callback, it will broadcast this to here (cellular_callback)
_device->cellular_callback(ev, ptr, this);
}
void AT_CellularContext::ppp_disconnected()
{
enable_hup(false);
// after ppp disconnect if we wan't to use same at handler we need to set filehandle again to athandler so it
// will set the correct sigio and nonblocking
_at.lock();
_at.set_is_filehandle_usable(true);
if (!_at.sync(AT_SYNC_TIMEOUT)) { // consume extra characters after ppp disconnect, also it may take a while until modem listens AT commands
tr_error("AT sync failed after PPP Disconnect");
}
_at.unlock();
}
#endif //#if NSAPI_PPP_AVAILABLE
void AT_CellularContext::do_disconnect()
{
if (!_nw || !_is_connected) {
if (_new_context_set) {
delete_current_context();
}
set_cid(-1);
_cb_data.error = NSAPI_ERROR_NO_CONNECTION;
}
// set false here so callbacks know that we are not connected and so should not send DISCONNECTED
_is_connected = false;
#if NSAPI_PPP_AVAILABLE
nsapi_error_t err = nsapi_ppp_disconnect(_at.get_file_handle());
if (err != NSAPI_ERROR_OK) {
tr_error("CellularContext disconnect failed!");
// continue even in failure due to ppp disconnect in any case releases filehandle
}
ppp_disconnected();
#endif // NSAPI_PPP_AVAILABLE
_at.lock();
// deactivate a context only if we have activated
if (_is_context_activated) {
if (_nonip_req && _cp_in_use) {
deactivate_non_ip_context();
} else {
deactivate_ip_context();
}
}
// don't call multiple times disconnect if we already got that event from network urc or ppp
if (_connect_status != NSAPI_STATUS_DISCONNECTED) {
_device->cellular_callback(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_DISCONNECTED, this);
}
_is_context_active = false;
_connect_status = NSAPI_STATUS_DISCONNECTED;
if (_new_context_set) {
delete_current_context();
}
set_cid(-1);
_cb_data.error = _at.unlock_return_error();
}
nsapi_error_t AT_CellularContext::disconnect()
{
tr_info("CellularContext disconnect()");
if (_is_blocking) {
do_disconnect();
return _cb_data.error;
} else {
int event_id = _device->get_queue()->call(this, &AT_CellularContext::do_disconnect);
if (event_id == 0) {
return NSAPI_ERROR_NO_MEMORY;
}
return NSAPI_ERROR_OK;
}
}
void AT_CellularContext::deactivate_ip_context()
{
check_and_deactivate_context();
}
void AT_CellularContext::deactivate_non_ip_context()
{
check_and_deactivate_context();
}
void AT_CellularContext::deactivate_context()
{
_at.at_cmd_discard("+CGACT", "=0,", "%d", _cid);
}
void AT_CellularContext::check_and_deactivate_context()
{
// CGACT and CGATT commands might take up to 3 minutes to respond.
_at.set_at_timeout(3min);
int active_contexts_count = 0;
_is_context_active = _nw->is_active_context(&active_contexts_count, _cid);
CellularNetwork::RadioAccessTechnology rat = CellularNetwork::RAT_GSM;
// always return NSAPI_ERROR_OK
CellularNetwork::registration_params_t reg_params;
_nw->get_registration_params(reg_params);
rat = reg_params._act;
// 3GPP TS 27.007:
// For EPS, if an attempt is made to disconnect the last PDN connection, then the MT responds with ERROR
if (_is_context_active && (rat < CellularNetwork::RAT_E_UTRAN || rat == CellularNetwork::RAT_NB1 || active_contexts_count > 1)) {
_at.clear_error();
deactivate_context();
}
if (_new_context_set) {
delete_current_context();
}
_at.restore_at_timeout();
}
nsapi_error_t AT_CellularContext::get_apn_backoff_timer(int &backoff_timer)
{
// If apn is set
if (_apn) {
_at.lock();
_at.cmd_start_stop("+CABTRDP", "=", "%s", _apn);
_at.resp_start("+CABTRDP:");
if (_at.info_resp()) {
_at.skip_param();
backoff_timer = _at.read_int();
}
_at.resp_stop();
return _at.unlock_return_error();
}
return NSAPI_ERROR_PARAMETER;
}
nsapi_error_t AT_CellularContext::get_rate_control(
CellularContext::RateControlExceptionReports &reports,
CellularContext::RateControlUplinkTimeUnit &timeUnit, int &uplinkRate)
{
_at.lock();
_at.cmd_start_stop("+CGAPNRC", "=", "%d", _cid);
_at.resp_start("+CGAPNRC:");
_at.read_int();
if (_at.get_last_error() == NSAPI_ERROR_OK) {
bool comma_found = true;
int next_element = _at.read_int();
if (next_element >= 0) {
reports = (RateControlExceptionReports)next_element;
next_element = _at.read_int();
} else {
comma_found = false;
}
if (comma_found && next_element >= 0) {
timeUnit = (RateControlUplinkTimeUnit)next_element;
next_element = _at.read_int();
} else {
comma_found = false;
}
if (comma_found && next_element >= 0) {
uplinkRate = next_element;
}
if (_at.get_last_error() == NSAPI_ERROR_OK) {
tr_debug("CGAPNRC: reports %d, time %d, rate %d", reports, timeUnit, uplinkRate);
}
}
_at.resp_stop();
return _at.unlock_return_error();
}
nsapi_error_t AT_CellularContext::get_pdpcontext_params(pdpContextList_t ¶ms_list)
{
const int ipv6_subnet_size = 128;
char *ipv6_and_subnetmask = new char[ipv6_subnet_size];
_at.lock();
_at.cmd_start_stop("+CGCONTRDP", "=", "%d", _cid);
_at.resp_start("+CGCONTRDP:");
pdpcontext_params_t *params = NULL;
while (_at.info_resp()) { // response can be zero or many +CGDCONT lines
params = params_list.add_new();
params->cid = _at.read_int();
params->bearer_id = _at.read_int();
_at.read_string(params->apn, sizeof(params->apn));
// rest are optional params
ipv6_and_subnetmask[0] = '\0';
_at.read_string(ipv6_and_subnetmask, ipv6_subnet_size);
separate_ip_addresses(ipv6_and_subnetmask, params->local_addr, sizeof(params->local_addr), params->local_subnet_mask, sizeof(params->local_subnet_mask));
ipv6_and_subnetmask[0] = '\0';
_at.read_string(ipv6_and_subnetmask, ipv6_subnet_size);
separate_ip_addresses(ipv6_and_subnetmask, params->gateway_addr, sizeof(params->gateway_addr), NULL, 0);
ipv6_and_subnetmask[0] = '\0';
_at.read_string(ipv6_and_subnetmask, ipv6_subnet_size);
separate_ip_addresses(ipv6_and_subnetmask, params->dns_primary_addr, sizeof(params->dns_primary_addr), NULL, 0);
ipv6_and_subnetmask[0] = '\0';
_at.read_string(ipv6_and_subnetmask, ipv6_subnet_size);
separate_ip_addresses(ipv6_and_subnetmask, params->dns_secondary_addr, sizeof(params->dns_secondary_addr), NULL, 0);
ipv6_and_subnetmask[0] = '\0';
_at.read_string(ipv6_and_subnetmask, ipv6_subnet_size);
separate_ip_addresses(ipv6_and_subnetmask, params->p_cscf_prim_addr, sizeof(params->p_cscf_prim_addr), NULL, 0);
ipv6_and_subnetmask[0] = '\0';
_at.read_string(ipv6_and_subnetmask, ipv6_subnet_size);
separate_ip_addresses(ipv6_and_subnetmask, params->p_cscf_sec_addr, sizeof(params->p_cscf_sec_addr), NULL, 0);
params->im_signalling_flag = _at.read_int();
params->lipa_indication = _at.read_int();
params->ipv4_mtu = _at.read_int();
params->wlan_offload = _at.read_int();
params->local_addr_ind = _at.read_int();
params->non_ip_mtu = _at.read_int();
params->serving_plmn_rate_control_value = _at.read_int();
}
_at.resp_stop();
delete [] ipv6_and_subnetmask;
return _at.unlock_return_error();
}
// Called by CellularDevice for network and cellular device changes
void AT_CellularContext::cellular_callback(nsapi_event_t ev, intptr_t ptr)
{
if (ev >= NSAPI_EVENT_CELLULAR_STATUS_BASE && ev <= NSAPI_EVENT_CELLULAR_STATUS_END) {
cell_callback_data_t *data = (cell_callback_data_t *)ptr;
cellular_connection_status_t st = (cellular_connection_status_t)ev;
_cb_data.error = data->error;
_cb_data.final_try = data->final_try;
#if MBED_CONF_CELLULAR_USE_APN_LOOKUP
if (st == CellularSIMStatusChanged && data->status_data == CellularDevice::SimStateReady &&
_cb_data.error == NSAPI_ERROR_OK) {
if (!_apn) {
char imsi[MAX_IMSI_LENGTH + 1];
ThisThread::sleep_for(1000); // need to wait to access SIM in some modems
_cb_data.error = _device->open_information()->get_imsi(imsi, sizeof(imsi));
if (_cb_data.error == NSAPI_ERROR_OK) {
const char *apn_config = apnconfig(imsi);
if (apn_config) {
const char *apn = _APN_GET(apn_config);
const char *uname = _APN_GET(apn_config);
const char *pwd = _APN_GET(apn_config);
tr_info("Looked up APN %s", apn);
set_credentials(apn, uname, pwd);
}
} else {
tr_error("APN lookup failed");
if (_is_blocking) {
// operation failed, release semaphore
if (_current_op != OP_INVALID) {
_semaphore.release();
}
}
}
_device->close_information();
}
}
#endif // MBED_CONF_CELLULAR_USE_APN_LOOKUP
if (!_nw && st == CellularDeviceReady && _cb_data.error == NSAPI_ERROR_OK) {
_nw = _device->open_network();
}
#if MBED_CONF_CELLULAR_CONTROL_PLANE_OPT
if (_cp_req && !_cp_in_use && (_cb_data.error == NSAPI_ERROR_OK) &&
(st == CellularSIMStatusChanged && data->status_data == CellularDevice::SimStateReady)) {
if (setup_control_plane_opt() != NSAPI_ERROR_OK) {
tr_error("Control plane SETUP failed!");
} else {
tr_info("Control plane SETUP success!");
}
}
#endif
if (_is_blocking) {
if ((data->final_try) || (_cb_data.error != NSAPI_ERROR_OK)) {
// operation failed, release semaphore
if (_current_op != OP_INVALID) {
_current_op = OP_INVALID;
_semaphore.release();
}
} else {
if ((st == CellularDeviceReady && _current_op == OP_DEVICE_READY) ||
(st == CellularSIMStatusChanged && _current_op == OP_SIM_READY &&
data->status_data == CellularDevice::SimStateReady)) {
// target reached, release semaphore
_current_op = OP_INVALID;
_semaphore.release();
} else if (st == CellularRegistrationStatusChanged && (data->status_data == CellularNetwork::RegisteredHomeNetwork ||
data->status_data == CellularNetwork::RegisteredRoaming || data->status_data == CellularNetwork::AlreadyRegistered) && _current_op == OP_REGISTER) {
// target reached, release semaphore
_current_op = OP_INVALID;
_semaphore.release();
} else if (st == CellularAttachNetwork && (_current_op == OP_ATTACH || _current_op == OP_CONNECT) &&
data->status_data == CellularNetwork::Attached) {
// target reached, release semaphore
_current_op = OP_INVALID;
_semaphore.release();
}
}
} else {
// non blocking
if (st == CellularAttachNetwork && _current_op == OP_CONNECT && _cb_data.error == NSAPI_ERROR_OK &&
data->status_data == CellularNetwork::Attached) {
_current_op = OP_INVALID;
// forward all Cellular specific events to application
if (_status_cb) {
_status_cb(ev, ptr);
}
_retry_count = 0;
do_connect_with_retry();
return;
}
}
// forward all Cellular specific events to application
if (_status_cb) {
_status_cb(ev, ptr);
}
} else {