-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
at_cellularcontexttest.cpp
750 lines (658 loc) · 25.1 KB
/
at_cellularcontexttest.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
/*
* 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 "gtest/gtest.h"
#include <string.h>
#include "AT_CellularContext.h"
#include "events/EventQueue.h"
#include "ATHandler.h"
#include "AT_CellularDevice.h"
#include "FileHandle_stub.h"
#include "CellularLog.h"
#include "ATHandler_stub.h"
#include "AT_CellularStack.h"
#include "Semaphore_stub.h"
#include "CellularDevice_stub.h"
#include "equeue_stub.h"
#include "AT_CellularBase_stub.h"
#include "CellularUtil_stub.h"
#include "PinNames.h"
using namespace mbed;
using namespace events;
// AStyle ignored as the definition is not clear due to preprocessor usage
// *INDENT-OFF*
class TestAT_CellularContext : public testing::Test {
protected:
void SetUp()
{
ATHandler_stub::nsapi_error_value = 0;
ATHandler_stub::nsapi_error_ok_counter = 0;
ATHandler_stub::int_value = -1;
ATHandler_stub::ref_count = 0;
ATHandler_stub::timeout = 0;
ATHandler_stub::default_timeout = 0;
ATHandler_stub::debug_on = 0;
ATHandler_stub::ssize_value = 0;
ATHandler_stub::read_string_value = NULL;
ATHandler_stub::size_value = 0;
ATHandler_stub::return_given_size = false;
ATHandler_stub::bool_value = false;
ATHandler_stub::uint8_value = 0;
ATHandler_stub::fh_value = NULL;
ATHandler_stub::call_immediately = false;
ATHandler_stub::resp_info_true_counter = false;
ATHandler_stub::info_elem_true_counter = false;
ATHandler_stub::int_valid_count_table[kRead_int_table_size];
ATHandler_stub::int_count = kRead_int_table_size;
ATHandler_stub::read_string_index = kRead_string_table_size;
ATHandler_stub::read_string_table[kRead_string_table_size];
ATHandler_stub::resp_stop_success_count = kResp_stop_count_default;
CellularDevice_stub::connect_counter = 2;
CellularUtil_stub::char_table[0] = (char *)"\0";
CellularUtil_stub::table_idx = 0;
}
void TearDown()
{
}
};
// *INDENT-ON*
class my_stack : public AT_CellularStack {
public:
my_stack(ATHandler &atHandler) : AT_CellularStack(atHandler, 1, IPV4_STACK) {}
virtual int get_max_socket_count()
{
return 1;
}
virtual int get_max_packet_size()
{
return 200;
}
virtual bool is_protocol_supported(nsapi_protocol_t protocol)
{
return true;
}
virtual nsapi_error_t socket_close_impl(int sock_id)
{
return NSAPI_ERROR_OK;
}
virtual nsapi_error_t create_socket_impl(CellularSocket *socket)
{
return NSAPI_ERROR_OK;
}
virtual nsapi_size_or_error_t socket_sendto_impl(CellularSocket *socket, const SocketAddress &address,
const void *data, nsapi_size_t size)
{
return 100;
}
virtual nsapi_size_or_error_t socket_recvfrom_impl(CellularSocket *socket, SocketAddress *address,
void *buffer, nsapi_size_t size)
{
return 100;
}
virtual const char *get_ip_address()
{
return "1.2.3.4";
}
};
class my_AT_CTX : public AT_CellularContext {
public:
my_AT_CTX(ATHandler &at, CellularDevice *device, const char *apn = MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN) :
AT_CellularContext(at, device, apn), _st(at)
{
AT_CellularBase_stub::supported_bool = false;
}
virtual ~my_AT_CTX() {}
virtual NetworkStack *get_stack()
{
return &_st;
}
virtual uint32_t get_timeout_for_operation(ContextOperation op) const
{
return 10;
}
virtual void cellular_callback(nsapi_event_t ev, intptr_t ptr)
{
AT_CellularContext::cellular_callback(ev, ptr);
}
void activ_non_ip_context()
{
activate_non_ip_context();
}
void deactiv_non_ip_context()
{
deactivate_non_ip_context();
}
my_stack _st;
};
class my_AT_CTXIPV6 : public AT_CellularContext {
public:
my_AT_CTXIPV6(ATHandler &at, CellularDevice *device, const char *apn = MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN) :
AT_CellularContext(at, device, apn), _st(at) {}
virtual ~my_AT_CTXIPV6() {}
virtual NetworkStack *get_stack()
{
return &_st;
}
virtual uint32_t get_timeout_for_operation(ContextOperation op) const
{
return 10;
}
my_stack _st;
};
class def_AT_CTX : public AT_CellularContext {
public:
def_AT_CTX(ATHandler &at, CellularDevice *device, const char *apn = MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN) :
AT_CellularContext(at, device, apn) {}
virtual ~def_AT_CTX() {}
uint32_t do_op()
{
return AT_CellularContext::get_timeout_for_operation(mbed::CellularContext::ContextOperation(_op));
}
int _op;
};
static int network_cb_count;
static void network_cb(nsapi_event_t ev, intptr_t intptr)
{
network_cb_count++;
}
TEST_F(TestAT_CellularContext, Create)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
AT_CellularContext *ctx = new AT_CellularContext(at, NULL);
EXPECT_TRUE(ctx != NULL);
delete ctx;
ctx = new AT_CellularContext(at, &dev);
EXPECT_TRUE(ctx != NULL);
ctx->get_device();
EXPECT_EQ(NSAPI_STATUS_DISCONNECTED, ctx->get_connection_status());
delete ctx;
}
TEST_F(TestAT_CellularContext, set_blocking)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
AT_CellularContext ctx(at, &dev);
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
ASSERT_EQ(NSAPI_ERROR_OK, ctx.set_blocking(false));
ASSERT_EQ(NSAPI_ERROR_OK, ctx.set_blocking(true));
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR;
ASSERT_EQ(NSAPI_ERROR_OK, ctx.set_blocking(false));
ASSERT_EQ(NSAPI_ERROR_OK, ctx.set_blocking(true));
}
TEST_F(TestAT_CellularContext, get_stack)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
AT_CellularContext ctx(at, &dev);
NetworkStack *stack = ctx.get_stack();
EXPECT_TRUE(stack == NULL);
my_AT_CTX ctx1(at, NULL);
stack = ctx1.get_stack();
EXPECT_TRUE(stack != NULL);
}
TEST_F(TestAT_CellularContext, get_ip_address)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
AT_CellularContext ctx(at, &dev);
const char *ip = ctx.get_ip_address();
EXPECT_TRUE(ip == NULL);
my_AT_CTX ctx1(at, NULL);
ip = ctx1.get_ip_address();
EXPECT_TRUE(ip != NULL);
}
TEST_F(TestAT_CellularContext, attach)
{
EventQueue que;
FileHandle_stub fh1;
AT_CellularDevice dev(&fh1);
ATHandler at(&fh1, que, 0, ",");
AT_CellularContext ctx(at, &dev);
ctx.attach(&network_cb);
}
TEST_F(TestAT_CellularContext, set_plmn)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
AT_CellularContext ctx(at, &dev);
ctx.set_plmn(NULL);
ctx.set_plmn("12345");
}
TEST_F(TestAT_CellularContext, set_sim_pin)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
AT_CellularContext ctx(at, &dev);
ctx.set_sim_pin(NULL);
ctx.set_sim_pin("12345");
}
TEST_F(TestAT_CellularContext, set_credentials)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
AT_CellularContext ctx(at, &dev);
ctx.set_credentials(NULL);
ctx.set_credentials("apn");
ctx.set_credentials("apn", NULL);
ctx.set_credentials("apn", NULL, NULL);
ctx.set_credentials("apn", NULL, "passwd");
ctx.set_credentials("apn", "user", NULL);
ctx.set_credentials("apn", "user");
ctx.set_credentials("apn", "user", "passwd");
}
TEST_F(TestAT_CellularContext, get_netmask_gateway)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
AT_CellularContext ctx(at, &dev);
const char *gg = ctx.get_netmask();
EXPECT_TRUE(gg == NULL);
gg = ctx.get_gateway();
EXPECT_TRUE(gg == NULL);
}
TEST_F(TestAT_CellularContext, get_pdpcontext_params)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
AT_CellularContext cn(at, &dev);
CellularContext::pdpContextList_t list;
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR;
EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.get_pdpcontext_params(list));
// don't got to while loop
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
ATHandler_stub::bool_value = false;
ATHandler_stub::resp_info_true_counter = 0;
ASSERT_EQ(cn.get_pdpcontext_params(list), NSAPI_ERROR_OK);
EXPECT_TRUE(NULL == list.get_head());
// go to while loop and check values
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
ATHandler_stub::resp_info_true_counter = 1;
ATHandler_stub::int_count = 9;
ATHandler_stub::int_valid_count_table[8] = 1;
ATHandler_stub::int_valid_count_table[7] = 2;
ATHandler_stub::int_valid_count_table[6] = 3;
ATHandler_stub::int_valid_count_table[5] = 4;
ATHandler_stub::int_valid_count_table[4] = 5;
ATHandler_stub::int_valid_count_table[3] = 6;
ATHandler_stub::int_valid_count_table[2] = 7;
ATHandler_stub::int_valid_count_table[1] = 8;
ATHandler_stub::int_valid_count_table[0] = 9;
ATHandler_stub::read_string_index = 7;
ATHandler_stub::read_string_table[6] = (char *)"internet";
ATHandler_stub::read_string_table[5] = (char *)"1.2.3.4.5.6.7.8.9.10.11.112.13.14.15.16.1.2.3.44.55.6.7.8.9.10.11.12.13.14.15.16";
ATHandler_stub::read_string_table[4] = (char *)"23.33.44.1.2.3.55.123.225.34.11.1.0.0.123.234";
ATHandler_stub::read_string_table[3] = (char *)"1.2.3.4";
ATHandler_stub::read_string_table[2] = (char *)"0.255.0.255";
ATHandler_stub::read_string_table[1] = (char *)"25.66.77.88";
ATHandler_stub::read_string_table[0] = (char *)"004.003.002.001";
CellularUtil_stub::table_idx = 12;
CellularUtil_stub::char_table[11] = (char *)"102:304:506:708:90A:B70:D0E:F10\0";
CellularUtil_stub::char_table[10] = (char *)"102:32C:3706:708:90A:B0C:D0E:F10\0";
CellularUtil_stub::char_table[9] = (char *)"1721:2C01:203:377B:E122:B01:000:7BEA\0";
CellularUtil_stub::char_table[8] = (char *)"1.2.3.4\0";
CellularUtil_stub::char_table[7] = (char *)"1.2.3.4\0";
CellularUtil_stub::char_table[6] = (char *)"1.2.3.4\0";
CellularUtil_stub::char_table[5] = (char *)"0.255.0.255\0";
CellularUtil_stub::char_table[4] = (char *)"1.2.3.4\0";
CellularUtil_stub::char_table[3] = (char *)"25.66.77.88\0";
CellularUtil_stub::char_table[2] = (char *)"1.2.3.4\0";
CellularUtil_stub::char_table[1] = (char *)"004.003.002.001\0";
CellularUtil_stub::char_table[0] = (char *)"1.2.3.4\0";
EXPECT_TRUE(NSAPI_ERROR_OK == cn.get_pdpcontext_params(list));
CellularContext::pdpcontext_params_t *params = list.get_head();
EXPECT_TRUE(params != NULL);
EXPECT_TRUE(params->next == NULL);
ASSERT_EQ(params->cid, 1);
ASSERT_EQ(params->bearer_id, 2);
ASSERT_EQ(params->im_signalling_flag, 3);
ASSERT_EQ(params->lipa_indication, 4);
ASSERT_EQ(params->ipv4_mtu, 5);
ASSERT_EQ(params->wlan_offload, 6);
ASSERT_EQ(params->local_addr_ind, 7);
ASSERT_EQ(params->non_ip_mtu, 8);
ASSERT_EQ(params->serving_plmn_rate_control_value, 9);
ASSERT_STREQ(params->apn, "internet");
ASSERT_STREQ(params->local_addr, "102:304:506:708:90A:B70:D0E:F10");
ASSERT_STREQ(params->local_subnet_mask, "102:32C:3706:708:90A:B0C:D0E:F10");
ASSERT_STREQ(params->gateway_addr, "1721:2C01:203:377B:E122:B01:000:7BEA");
ASSERT_STREQ(params->dns_primary_addr, "1.2.3.4");
ASSERT_STREQ(params->dns_secondary_addr, "0.255.0.255");
ASSERT_STREQ(params->p_cscf_prim_addr, "25.66.77.88");
ASSERT_STREQ(params->p_cscf_sec_addr, "004.003.002.001");
}
TEST_F(TestAT_CellularContext, get_rate_control)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
AT_CellularContext cn(at, &dev);
int ur = -1;
CellularContext::RateControlExceptionReports reports = CellularContext::NotAllowedToBeSent;
CellularContext::RateControlUplinkTimeUnit timeUnit = CellularContext::Unrestricted;
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR;
ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, cn.get_rate_control(reports, timeUnit, ur));
ASSERT_EQ(reports, CellularContext::NotAllowedToBeSent);
ASSERT_EQ(timeUnit, CellularContext::Unrestricted);
ASSERT_EQ(ur, -1);
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
ATHandler_stub::int_value = 1;
ASSERT_EQ(NSAPI_ERROR_OK, cn.get_rate_control(reports, timeUnit, ur));
ASSERT_EQ(reports, CellularContext::AllowedToBeSent);
ASSERT_EQ(timeUnit, CellularContext::Minute);
ASSERT_EQ(ur, 1);
// test second if in get_rate_control
reports = CellularContext::NotAllowedToBeSent;
timeUnit = CellularContext::Unrestricted;
ur = -1;
ATHandler_stub::int_count = 1;
ATHandler_stub::int_valid_count_table[0] = 1;
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, cn.get_rate_control(reports, timeUnit, ur));
ASSERT_EQ(reports, CellularContext::NotAllowedToBeSent);
ASSERT_EQ(timeUnit, CellularContext::Unrestricted);
ASSERT_EQ(ur, -1);
// test second if in get_rate_control
ATHandler_stub::int_count = 2;
ATHandler_stub::int_valid_count_table[0] = 1;
ATHandler_stub::int_valid_count_table[1] = 1;
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, cn.get_rate_control(reports, timeUnit, ur));
ASSERT_EQ(reports, CellularContext::AllowedToBeSent);
ASSERT_EQ(timeUnit, CellularContext::Unrestricted);
ASSERT_EQ(ur, -1);
// test third if in get_rate_control
ATHandler_stub::int_count = 3;
ATHandler_stub::int_valid_count_table[0] = 3;
ATHandler_stub::int_valid_count_table[1] = 1;
ATHandler_stub::int_valid_count_table[2] = 1;
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.get_rate_control(reports, timeUnit, ur));
EXPECT_TRUE(reports == CellularContext::AllowedToBeSent);
EXPECT_TRUE(timeUnit == CellularContext::Day);
EXPECT_TRUE(ur == -1);
ATHandler_stub::int_count = 4;
ATHandler_stub::int_valid_count_table[0] = 5;
ATHandler_stub::int_valid_count_table[1] = 3;
ATHandler_stub::int_valid_count_table[2] = 1;
ATHandler_stub::int_valid_count_table[3] = 1;
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
ASSERT_EQ(NSAPI_ERROR_OK, cn.get_rate_control(reports, timeUnit, ur));
ASSERT_EQ(reports, CellularContext::AllowedToBeSent);
ASSERT_EQ(timeUnit, CellularContext::Day);
ASSERT_EQ(ur, 5);
}
TEST_F(TestAT_CellularContext, get_apn_backoff_timer)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
AT_CellularContext cn(at, &dev);
int time = -1;
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR;
ASSERT_EQ(NSAPI_ERROR_PARAMETER, cn.get_apn_backoff_timer(time));
ASSERT_EQ(time, -1);
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
ASSERT_EQ(NSAPI_ERROR_PARAMETER, cn.get_apn_backoff_timer(time));
ASSERT_EQ(time, -1);
ATHandler_stub::resp_info_true_counter = 0;
ATHandler_stub::bool_value = false;
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
cn.set_credentials("internet", NULL, NULL);
ASSERT_EQ(NSAPI_ERROR_OK, cn.get_apn_backoff_timer(time));
ASSERT_EQ(time, -1);
ATHandler_stub::resp_info_true_counter = 0;
ATHandler_stub::bool_value = true;
ATHandler_stub::int_value = 55;
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
cn.set_credentials("internet", NULL, NULL);
ASSERT_EQ(NSAPI_ERROR_OK, cn.get_apn_backoff_timer(time));
ASSERT_EQ(time, 55);
}
TEST_F(TestAT_CellularContext, set_file_handle)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
AT_CellularContext ctx(at, &dev);
ctx.set_file_handle(&fh1);
UARTSerial ss(NC, NC);
ctx.set_file_handle(&ss, PTC0, true);
ctx.enable_hup(true);
}
TEST_F(TestAT_CellularContext, connect_disconnect_sync)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
AT_CellularContext ctx(at, &dev);
ctx.attach(&network_cb);
network_cb_count = 0;
// connect in sync mode, semaphore will return 0 so timeout is returned
ASSERT_EQ(ctx.connect(), NSAPI_ERROR_TIMEOUT);
ASSERT_EQ(network_cb_count, 1);
my_AT_CTX ctx1(at, &dev);
ctx1.attach(&network_cb);
Semaphore_stub::acquire_return_value = true;
// call callback so that network is opened which is needed in disconnect
cell_callback_data_t data;
data.error = NSAPI_ERROR_OK;
ctx1.cellular_callback((nsapi_event_t)CellularDeviceReady, (intptr_t)&data);
ATHandler_stub::resp_info_true_counter = 1;
ATHandler_stub::read_string_table[0] = (char *)"APN";
ATHandler_stub::read_string_table[1] = (char *)"IP";
ATHandler_stub::read_string_index = 2;
ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_OK);
ASSERT_EQ(network_cb_count, 4);
ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_OK);
ATHandler_stub::resp_info_true_counter = 1;
ATHandler_stub::read_string_table[1] = (char *)"Non-IP";
ATHandler_stub::read_string_index = 2;
ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_OK);
ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_OK);
ATHandler_stub::resp_info_true_counter = 1;
ATHandler_stub::read_string_table[1] = (char *)"IPV6";
ATHandler_stub::read_string_index = 2;
ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_OK);
AT_CellularBase_stub::supported_bool = true;
ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_OK);
ATHandler_stub::resp_info_true_counter = 1;
ATHandler_stub::read_string_table[1] = (char *)"IPV4V6";
ATHandler_stub::read_string_index = 2;
ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_OK);
AT_CellularBase_stub::supported_bool = false;
ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_OK);
ATHandler_stub::resp_info_true_counter = 1;
ATHandler_stub::read_string_table[0] = (char *)"APN2";
ATHandler_stub::read_string_table[1] = (char *)"IPV4V6";
ATHandler_stub::read_string_index = 2;
ATHandler_stub::int_value = 10;
ctx1.set_credentials("APN");
ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_OK);
ATHandler_stub::int_value = -1;
ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_IS_CONNECTED);
EXPECT_TRUE(ctx1.is_connected() == true);
ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_OK);
EXPECT_TRUE(ctx1.is_connected() == false);
ATHandler_stub::get_debug_clear();
EXPECT_FALSE(ATHandler_stub::is_get_debug_run());
ATHandler_stub::debug_call_count_clear();
at.set_debug(true);
ASSERT_EQ(ctx1.connect("1234", "internet", "usern", "pwd"), NSAPI_ERROR_OK);
EXPECT_TRUE(ATHandler_stub::is_get_debug_run());
EXPECT_TRUE(ATHandler_stub::set_debug_call_count_get() == 3);
EXPECT_TRUE(at.get_debug());
EXPECT_TRUE(ctx1.is_connected() == true);
ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_OK);
EXPECT_TRUE(ctx1.is_connected() == false);
ATHandler_stub::get_debug_clear();
EXPECT_FALSE(ATHandler_stub::is_get_debug_run());
ATHandler_stub::debug_call_count_clear();
at.set_debug(false);
ASSERT_EQ(ctx1.connect("1234", "internet", "usern", "pwd"), NSAPI_ERROR_OK);
EXPECT_TRUE(ATHandler_stub::is_get_debug_run());
EXPECT_TRUE(ATHandler_stub::set_debug_call_count_get() == 3);
EXPECT_FALSE(at.get_debug());
EXPECT_TRUE(ctx1.is_connected() == true);
ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_OK);
EXPECT_TRUE(ctx1.is_connected() == false);
// More connect test after we are re-writted getting of PDP context...
}
TEST_F(TestAT_CellularContext, de_and_activate_non_ip_context)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
my_AT_CTX ctx(at, &dev);
ctx.attach(&network_cb);
Semaphore_stub::acquire_return_value = true;
// call callback so that network is opened which is needed in disconnect
cell_callback_data_t data;
data.error = NSAPI_ERROR_OK;
ctx.cellular_callback((nsapi_event_t)CellularDeviceReady, (intptr_t)&data);
ctx.activ_non_ip_context();
ctx.deactiv_non_ip_context();
}
TEST_F(TestAT_CellularContext, set_device_ready_sync)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
my_AT_CTX ctx(at, &dev);
cell_callback_data_t data;
data.error = NSAPI_ERROR_OK;
ctx.cellular_callback((nsapi_event_t)CellularDeviceReady, (intptr_t)&data);
ASSERT_EQ(NSAPI_ERROR_OK, ctx.set_device_ready());
}
TEST_F(TestAT_CellularContext, set_sim_ready)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
my_AT_CTX ctx(at, &dev);
ASSERT_EQ(NSAPI_ERROR_OK, ctx.set_sim_ready());
cell_callback_data_t data;
data.error = NSAPI_ERROR_OK;
ctx.cellular_callback((nsapi_event_t)CellularDeviceReady, (intptr_t)&data);
data.status_data = CellularDevice::SimStateReady;
ctx.cellular_callback((nsapi_event_t)CellularSIMStatusChanged, (intptr_t)&data);
}
TEST_F(TestAT_CellularContext, register_to_network)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
my_AT_CTX ctx(at, &dev);
ASSERT_EQ(NSAPI_ERROR_OK, ctx.register_to_network());
cell_callback_data_t data;
data.error = NSAPI_ERROR_OK;
ctx.cellular_callback((nsapi_event_t)CellularDeviceReady, (intptr_t)&data);
data.status_data = CellularNetwork::RegisteredHomeNetwork;
ctx.cellular_callback((nsapi_event_t)CellularRegistrationStatusChanged, (intptr_t)&data);
}
TEST_F(TestAT_CellularContext, attach_to_network)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
my_AT_CTX ctx(at, &dev);
ASSERT_EQ(NSAPI_ERROR_OK, ctx.attach_to_network());
cell_callback_data_t data;
data.error = NSAPI_ERROR_OK;
ctx.cellular_callback((nsapi_event_t)CellularDeviceReady, (intptr_t)&data);
data.status_data = CellularNetwork::Attached;
ctx.cellular_callback((nsapi_event_t)CellularAttachNetwork, (intptr_t)&data);
}
TEST_F(TestAT_CellularContext, connect_disconnect_async)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
network_cb_count = 0;
AT_CellularDevice dev(&fh1);
my_AT_CTX ctx1(at, &dev);
ctx1.attach(&network_cb);
ASSERT_EQ(ctx1.set_blocking(false), NSAPI_ERROR_OK);
ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_OK);
// call callback so that network is opened which is needed in disconnect
cell_callback_data_t data;
data.error = NSAPI_ERROR_OK;
ctx1.cellular_callback((nsapi_event_t)CellularDeviceReady, (intptr_t)&data);
// attach ready, ctx will continue in callback
data.status_data = CellularNetwork::Attached;
ctx1.cellular_callback((nsapi_event_t)CellularAttachNetwork, (intptr_t)&data);
ASSERT_EQ(network_cb_count, 4);
ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_IS_CONNECTED);
EXPECT_TRUE(ctx1.is_connected() == true);
ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_NO_MEMORY);
EXPECT_TRUE(ctx1.is_connected() == true);
struct equeue_event ptr;
equeue_stub.void_ptr = &ptr;
equeue_stub.call_cb_immediately = true;
ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_OK);
EXPECT_TRUE(ctx1.is_connected() == false);
// sdet CellularDevice_stub::connect_counter = 0 so device is already attached and will return NSAPI_ERROR_ALREADY to context when calling connect
equeue_stub.void_ptr = &ptr;
equeue_stub.call_cb_immediately = false;
CellularDevice_stub::connect_counter = 0;
// queue can't allocate so return NSAPI_ERROR_NO_MEMORY
ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_NO_MEMORY);
equeue_stub.void_ptr = &ptr;
equeue_stub.call_cb_immediately = true;
ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_OK);
// More connect test after we are re-writted getting of PDP context...
}
TEST_F(TestAT_CellularContext, get_timeout_for_operation)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");
AT_CellularDevice dev(&fh1);
def_AT_CTX ctx1(at, &dev);
ctx1._op = 1;
EXPECT_EQ(300 * 1000, ctx1.do_op());
ctx1._op = 0;
EXPECT_EQ(300 * 1000, ctx1.do_op());
ctx1._op = -1;
EXPECT_EQ(1800 * 1000, ctx1.do_op());
}