This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathu_cell_pwr_test.c
1684 lines (1464 loc) · 70 KB
/
u_cell_pwr_test.c
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 2019-2024 u-blox
*
* 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.
*/
/* Only #includes of u_* and the C standard library are allowed here,
* no platform stuff and no OS stuff. Anything required from
* the platform/OS must be brought in through u_port* to maintain
* portability.
*/
/** @file
* @brief Tests for the cellular power API: these should pass on all
* platforms that have a cellular module connected to them. They
* are only compiled if U_CFG_TEST_CELL_MODULE_TYPE is defined.
* IMPORTANT: see notes in u_cfg_test_platform_specific.h for the
* naming rules that must be followed when using the U_PORT_TEST_FUNCTION()
* macro.
*/
#ifdef U_CFG_TEST_CELL_MODULE_TYPE
# ifdef U_CFG_OVERRIDE
# include "u_cfg_override.h" // For a customer's configuration override
# endif
#include "stddef.h" // NULL, size_t etc.
#include "stdint.h" // int32_t etc.
#include "stdbool.h"
#include "string.h" // For memset()/memcmp()
#include "u_cfg_sw.h"
#include "u_cfg_os_platform_specific.h"
#include "u_cfg_app_platform_specific.h"
#include "u_cfg_test_platform_specific.h"
#include "u_error_common.h"
#include "u_port.h"
#include "u_port_debug.h"
#include "u_port_os.h" // Required by u_cell_private.h
#include "u_port_uart.h"
#include "u_test_util_resource_check.h"
#include "u_timeout.h"
#include "u_at_client.h"
#include "u_sock.h"
#include "u_cell_module_type.h"
#include "u_cell.h"
#include "u_cell_file.h"
#include "u_cell_net.h" // Required by u_cell_private.h
#include "u_cell_private.h" // So that we can get at some innards
#include "u_cell_pwr.h"
#include "u_cell_info.h" // In order to fetch the IMEI as a test command for power saving
#include "u_cell_sock.h" // So that we can transfer some data during E-DRX tests
#ifdef U_CELL_TEST_MUX_ALWAYS
# include "u_cell_mux.h"
#endif
#include "u_cell_test_cfg.h"
#include "u_cell_test_private.h"
#include "u_sock_test_shared_cfg.h" // For some of the test macros
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
/** The string to put at the start of all prints from this test.
*/
#define U_TEST_PREFIX "U_CELL_PWR_TEST: "
/** Print a whole line, with terminator, prefixed for this test file.
*/
#define U_TEST_PRINT_LINE(format, ...) uPortLog(U_TEST_PREFIX format "\n", ##__VA_ARGS__)
#ifndef U_CELL_PWR_TEST_ACTIVE_TIME_SECONDS
/** The active time to use during 3GPP power saving testing,
* a value known to work with the Nutaq test network we use in our
* test system.
*/
# define U_CELL_PWR_TEST_ACTIVE_TIME_SECONDS 10
#endif
#ifndef U_CELL_PWR_TEST_PERIODIC_WAKEUP_SECONDS
/** The periodic wake-up to use during 3GPP power saving testing,
* a value known to work with the Nutaq test network we use in our
* test system.
*/
# define U_CELL_PWR_TEST_PERIODIC_WAKEUP_SECONDS 300
#endif
#ifndef U_CELL_PWR_TEST_RRC_DISCONNECT_SECONDS
/** How long to wait for the module to return to idle, 10 seconds for the
* RRC connection to drop on the Nutaq box we use in testing, plus
* a little bit of margin to be sure.
*/
#define U_CELL_PWR_TEST_RRC_DISCONNECT_SECONDS 12
#endif
#ifndef U_CELL_PWR_TEST_3GPP_POWER_SAVING_MARGIN_SECONDS
/** How much longer to wait than the active time for a module to
* actually go to sleep after the RRC disconnect.
*/
#define U_CELL_PWR_TEST_3GPP_POWER_SAVING_MARGIN_SECONDS 10
#endif
#ifndef U_CELL_PWR_TEST_EDRX_SECONDS
/** The E-DRX time to use during testing in seconds.
*/
# define U_CELL_PWR_TEST_EDRX_SECONDS 10
#endif
#ifndef U_CELL_PWR_TEST_EDRX_MARGIN_SECONDS
/** How much longer to wait then the E-DRX timer for a
* module to actually go to sleep.
*/
#define U_CELL_PWR_TEST_EDRX_MARGIN_SECONDS 2
#endif
#ifndef U_CELL_PWR_TEST_PAGING_WINDOW_SECONDS
/** The paging window to use when testing E-DRX in seconds.
*/
# define U_CELL_PWR_TEST_PAGING_WINDOW_SECONDS 1
#endif
#ifndef U_CELL_PWR_TEST_ECHO_STRING
/** String to send to the echo server during power saving testing.
*/
# define U_CELL_PWR_TEST_ECHO_STRING "Hello world!"
#endif
#ifndef U_CELL_PWR_TEST_ECHO_STRING_LENGTH_BYTES
/** The length of U_CELL_PWR_TEST_ECHO_STRING, not including
* terminator, as strlen() would return.
*/
# define U_CELL_PWR_TEST_ECHO_STRING_LENGTH_BYTES 12
#endif
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
/** Structure to hold E-DRX values.
*/
typedef struct {
int32_t eDrxSecondsRequested;
int32_t eDrxSecondsExpected;
} uCellPwrTestEdrxValues_t;
/** Structure to hold all 3GPP power saving parameters, for use with
* the calllback.
*/
typedef struct {
bool onNotOff;
int32_t activeTimeSeconds;
int32_t periodicWakeupSeconds;
} uCellPwrTest3gppPowerSavingParameters_t;
/** Structure to hold all E-DRX parameters, for use with the calllback.
*/
typedef struct {
uCellNetRat_t rat;
bool onNotOff;
int32_t eDrxSecondsRequested;
int32_t eDrxSecondsAssigned;
int32_t pagingWindowSecondsAssigned;
} uCellPwrTestEdrxParameters_t;
/* ----------------------------------------------------------------
* VARIABLES
* -------------------------------------------------------------- */
/** Handles.
*/
static uCellTestPrivate_t gHandles = U_CELL_TEST_PRIVATE_DEFAULTS;
# if ((U_CFG_APP_PIN_CELL_PWR_ON >= 0) && !defined(U_CFG_TEST_CELL_PWR_DISABLE)) || \
!defined(U_CFG_CELL_DISABLE_UART_POWER_SAVING)
/** Used for keepGoingCallback() timeout.
*/
static uTimeoutStop_t gTimeoutStop;
/** A variable to track errors in the callbacks.
*/
static int32_t gCallbackErrorCode = 0;
# endif
# ifndef U_CFG_CELL_DISABLE_UART_POWER_SAVING
/** TCP socket handle.
*/
static int32_t gSockHandle = -1;
/** Test values for requested and expected E-DRX on Cat-M1;
* just a few spot-checks
*/
static uCellPwrTestEdrxValues_t gEDrxSecondsCatM1[] = {
{ 7, 10},
{ 103, 122},
{2622, 2621}
};
/** Place to store the E-DRX parameters as received by the
* E-DRX callback function.
*/
static uCellPwrTestEdrxParameters_t gEDrxParameters;
/** Place to store the 3GPP power sacing parameters as received
* by the callback function.
*/
uCellPwrTest3gppPowerSavingParameters_t g3gppPowerSavingCallbackParameter = {0};
# endif // U_CFG_CELL_DISABLE_UART_POWER_SAVING
/* ----------------------------------------------------------------
* STATIC FUNCTIONS
* -------------------------------------------------------------- */
# if ((U_CFG_APP_PIN_CELL_PWR_ON >= 0) && !defined(U_CFG_TEST_CELL_PWR_DISABLE)) || \
!defined(U_CFG_CELL_DISABLE_UART_POWER_SAVING)
// Callback function for the cellular power-down and connection process.
static bool keepGoingCallback(uDeviceHandle_t cellHandle)
{
bool keepGoing = true;
if (cellHandle != gHandles.cellHandle) {
gCallbackErrorCode = 1;
}
if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart,
gTimeoutStop.durationMs)) {
keepGoing = false;
}
return keepGoing;
}
# endif
# if (U_CFG_APP_PIN_CELL_PWR_ON >= 0) && !defined(U_CFG_TEST_CELL_PWR_DISABLE)
// Test power on/off and aliveness, parameterised by the VInt pin.
static void testPowerAliveVInt(uCellTestPrivate_t *pHandles,
int32_t pinVint)
{
bool (*pKeepGoingCallback) (uDeviceHandle_t) = NULL;
uDeviceHandle_t cellHandle;
int32_t returnCode;
bool trulyHardPowerOff = false;
const uCellPrivateModule_t *pModule;
# if U_CFG_APP_PIN_CELL_VINT < 0
uTimeoutStart_t timeoutStart;
uint32_t y;
# endif
# if U_CFG_APP_PIN_CELL_ENABLE_POWER >= 0
//lint -e(838) Suppress previously assigned value has not been used
trulyHardPowerOff = true;
# endif
uPortLog(U_TEST_PREFIX "running power-on and alive tests");
if (pinVint >= 0) {
uPortLog(" with VInt on pin %d.\n", pinVint);
} else {
uPortLog(" without VInt.\n");
}
U_TEST_PRINT_LINE("adding a cellular instance on the AT client...");
returnCode = uCellAdd(U_CFG_TEST_CELL_MODULE_TYPE,
pHandles->atClientHandle,
U_CFG_APP_PIN_CELL_ENABLE_POWER,
U_CFG_APP_PIN_CELL_PWR_ON,
pinVint, false,
&pHandles->cellHandle);
U_PORT_TEST_ASSERT_EQUAL((int32_t) U_ERROR_COMMON_SUCCESS, returnCode);
cellHandle = pHandles->cellHandle;
#if defined(U_CFG_APP_PIN_CELL_DTR) && (U_CFG_APP_PIN_CELL_DTR >= 0)
uCellPwrSetDtrPowerSavingPin(cellHandle, U_CFG_APP_PIN_CELL_DTR);
#endif
// Get the private module data as we need it for testing
pModule = pUCellPrivateGetModule(cellHandle);
U_PORT_TEST_ASSERT(pModule != NULL);
//lint -esym(613, pModule) Suppress possible use of NULL pointer
// for pModule from now on
// Let the module state settle in case it is on but still
// booting
uPortTaskBlock(pModule->bootWaitSeconds * 1000);
// If the module is on at the start, switch it off.
if (uCellPwrIsAlive(cellHandle)) {
U_TEST_PRINT_LINE("powering off to begin test.");
uCellPwrOff(cellHandle, NULL);
U_TEST_PRINT_LINE("power off completed.");
# if U_CFG_APP_PIN_CELL_VINT < 0
U_TEST_PRINT_LINE("waiting another %d second(s) to be"
" sure of a clean power off as there's"
" no VInt pin to tell us...",
pModule->powerDownWaitSeconds);
uPortTaskBlock(pModule->powerDownWaitSeconds * 1000);
# endif
}
// Do this twice so as to check transiting from
// a call to uCellPwrOff() back to a call to uCellPwrOn().
for (size_t x = 0; x < 2; x++) {
uPortLog(U_TEST_PREFIX "testing power-on and alive calls");
if (x > 0) {
uPortLog(" with a callback passed to uCellPwrOff(), and"
" a %d second power-off timer, iteration %d.\n",
pModule->powerDownWaitSeconds, x + 1);
} else {
uPortLog(" with cellPwrOff(NULL), iteration %d.\n", x + 1);
}
U_PORT_TEST_ASSERT(!uCellPwrIsAlive(cellHandle));
# if U_CFG_APP_PIN_CELL_ENABLE_POWER >= 0
U_PORT_TEST_ASSERT(!uCellPwrIsPowered(cellHandle));
# endif
// TODO Note: only use a NULL PIN as we don't support anything
// else at least that's the case on SARA-R4 when you want to
// have power saving
U_TEST_PRINT_LINE("powering on...");
U_PORT_TEST_ASSERT(uCellPwrOn(cellHandle, U_CELL_TEST_CFG_SIM_PIN,
NULL) == 0);
U_TEST_PRINT_LINE("checking that module is alive...");
U_PORT_TEST_ASSERT(uCellPwrIsAlive(cellHandle));
# ifdef U_CELL_TEST_MUX_ALWAYS
U_PORT_TEST_ASSERT(uCellMuxEnable(cellHandle) == 0);
# endif
// Give the module time to sort itself out
U_TEST_PRINT_LINE("waiting %d second(s) before powering off...",
pModule->minAwakeTimeSeconds);
uPortTaskBlock(pModule->minAwakeTimeSeconds * 1000);
// Test with and without a keep-going callback
if (x > 0) {
// Note: can't check if keepGoingCallback is being
// called here as we've no control over how long the
// module takes to power off.
pKeepGoingCallback = keepGoingCallback;
gTimeoutStop.timeoutStart = uTimeoutStart();
gTimeoutStop.durationMs = pModule->powerDownWaitSeconds * 1000;
}
# if U_CFG_APP_PIN_CELL_VINT < 0
timeoutStart = uTimeoutStart();
# endif
U_TEST_PRINT_LINE("powering off...");
uCellPwrOff(cellHandle, pKeepGoingCallback);
U_TEST_PRINT_LINE("power off completed.");
# if U_CFG_APP_PIN_CELL_VINT < 0
y = uTimeoutElapsedMs(timeoutStart);
if (y < (uint32_t) pModule->powerDownWaitSeconds * 1000) {
y = (pModule->powerDownWaitSeconds * 1000) - y;
U_TEST_PRINT_LINE("waiting another %u second(s) to be sure of a "
"clean power off as there's no VInt pin to tell us...",
(y / 1000) + 1);
uPortTaskBlock(y);
}
# endif
}
// Do this twice so as to check transiting from
// a call to uCellPwrOffHard() to a call to
// uCellPwrOn().
for (size_t x = 0; x < 2; x++) {
uPortLog(U_TEST_PREFIX "testing power-on and alive calls with "
"uCellPwrOffHard()");
if (trulyHardPowerOff) {
uPortLog(" and truly hard power off");
}
uPortLog(", iteration %d.\n", x + 1);
U_PORT_TEST_ASSERT(!uCellPwrIsAlive(cellHandle));
# if U_CFG_APP_PIN_CELL_ENABLE_POWER >= 0
U_PORT_TEST_ASSERT(!uCellPwrIsPowered(cellHandle));
# endif
U_TEST_PRINT_LINE("powering on...");
U_PORT_TEST_ASSERT(uCellPwrOn(cellHandle, U_CELL_TEST_CFG_SIM_PIN,
NULL) == 0);
U_TEST_PRINT_LINE("checking that module is alive...");
U_PORT_TEST_ASSERT(uCellPwrIsAlive(cellHandle));
// Let the module sort itself out
# ifdef U_CELL_TEST_MUX_ALWAYS
U_PORT_TEST_ASSERT(uCellMuxEnable(cellHandle) == 0);
# endif
U_TEST_PRINT_LINE("waiting %d second(s) before powering off...",
pModule->minAwakeTimeSeconds);
uPortTaskBlock(pModule->minAwakeTimeSeconds * 1000);
# if U_CFG_APP_PIN_CELL_VINT < 0
timeoutStart = uTimeoutStart();
# endif
U_TEST_PRINT_LINE("hard powering off...");
uCellPwrOffHard(cellHandle, trulyHardPowerOff, NULL);
U_TEST_PRINT_LINE("hard power off completed.");
# if U_CFG_APP_PIN_CELL_VINT < 0
y = uTimeoutElapsedMs(timeoutStart);
if (!trulyHardPowerOff && (y < (uint32_t) pModule->powerDownWaitSeconds * 1000)) {
y = (pModule->powerDownWaitSeconds * 1000) - y;
U_TEST_PRINT_LINE("waiting another %u second(s) to be sure of a "
"clean power off as there's no VInt pin to tell us...",
(y / 1000) + 1);
uPortTaskBlock(y);
}
# endif
}
U_TEST_PRINT_LINE("testing power-on and alive calls after hard power off.");
U_PORT_TEST_ASSERT(!uCellPwrIsAlive(cellHandle));
# if U_CFG_APP_PIN_CELL_ENABLE_POWER >= 0
U_PORT_TEST_ASSERT(!uCellPwrIsPowered(cellHandle));
# endif
U_TEST_PRINT_LINE("removing cellular instance...");
uCellRemove(cellHandle);
}
# if (U_CFG_APP_PIN_CELL_VINT >= 0) && !defined(U_CFG_CELL_DISABLE_UART_POWER_SAVING)
// Callback for when the 3GPP power saving parameters are indicated
// by the network
static void powerSaving3gppCallback(uDeviceHandle_t cellHandle, bool onNotOff,
int32_t activeTimeSeconds,
int32_t periodicWakeupSeconds,
void *pParameter)
{
if (cellHandle != *((uDeviceHandle_t *) pParameter)) {
gCallbackErrorCode = 2;
}
g3gppPowerSavingCallbackParameter.onNotOff = onNotOff;
g3gppPowerSavingCallbackParameter.activeTimeSeconds = activeTimeSeconds;
g3gppPowerSavingCallbackParameter.periodicWakeupSeconds = periodicWakeupSeconds;
}
// 3GPP power saving wake-up callback where the second parameter is a pointer
// to an int32_t.
static void wakeCallback(uDeviceHandle_t cellHandle, void *pParam)
{
U_PORT_TEST_ASSERT(cellHandle == gHandles.cellHandle);
(*((int32_t *) pParam))++;
// Re-disable that remarkably persistent LWM2M client for
// modules which forget that it was disabled
uCellTestPrivateLwm2mDisable(cellHandle);
}
# endif // if (U_CFG_APP_PIN_CELL_VINT >= 0) && !defined(U_CFG_CELL_DISABLE_UART_POWER_SAVING)
# endif // # if (U_CFG_APP_PIN_CELL_PWR_ON >= 0) && !defined(U_CFG_TEST_CELL_PWR_DISABLE)
# ifndef U_CFG_CELL_DISABLE_UART_POWER_SAVING
// Connect to a cellular network.
static int32_t connectNetwork(uDeviceHandle_t cellHandle)
{
gTimeoutStop.timeoutStart = uTimeoutStart();
gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000;
return uCellNetConnect(cellHandle, NULL,
# ifdef U_CELL_TEST_CFG_APN
U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN),
# else
NULL,
# endif
# ifdef U_CELL_TEST_CFG_USERNAME
U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_USERNAME),
# else
NULL,
# endif
# ifdef U_CELL_TEST_CFG_PASSWORD
U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_PASSWORD),
# else
NULL,
# endif
keepGoingCallback);
}
// Callback for base station connection status where the parameter is a
// pointer to an int32_t.
static void connectCallback(bool isConnected, void *pParameter)
{
if (isConnected) {
(*((int32_t *) pParameter))++;
} else {
(*((int32_t *) pParameter))--;
}
}
// Connect to an echo server, so that we can exchange data during tests.
static int32_t connectToEchoServer(uDeviceHandle_t cellHandle, uSockAddress_t *pEchoServerAddress)
{
int32_t sockHandle = -1;
// Init cell sockets so that we can run a data transfer
U_PORT_TEST_ASSERT(uCellSockInit() == 0);
U_PORT_TEST_ASSERT(uCellSockInitInstance(cellHandle) == 0);
// Look up the address of the server we use for TCP echo
if (uCellSockGetHostByName(cellHandle, U_SOCK_TEST_ECHO_TCP_SERVER_DOMAIN_NAME,
&(pEchoServerAddress->ipAddress)) == 0) {
// Add the port number we will use
pEchoServerAddress->port = U_SOCK_TEST_ECHO_TCP_SERVER_PORT;
U_TEST_PRINT_LINE("connecting to %s:%d...",
U_SOCK_TEST_ECHO_TCP_SERVER_DOMAIN_NAME,
pEchoServerAddress->port);
// Create a TCP socket
sockHandle = uCellSockCreate(cellHandle, U_SOCK_TYPE_STREAM, U_SOCK_PROTOCOL_TCP);
if (sockHandle >= 0) {
// ...and connect it
uCellSockConnect(cellHandle, sockHandle, pEchoServerAddress);
}
U_TEST_PRINT_LINE("socket connected is %d.", sockHandle);
}
return sockHandle;
}
// Exchange some data with the echo server
static int32_t echoData(uDeviceHandle_t cellHandle, int32_t sockHandle)
{
int32_t y;
int32_t z;
size_t count;
char buffer[U_CELL_PWR_TEST_ECHO_STRING_LENGTH_BYTES];
U_TEST_PRINT_LINE("sending \"%s\" (%d byte(s)) on socket %d...",
U_CELL_PWR_TEST_ECHO_STRING, sizeof(buffer), sockHandle);
y = 0;
count = 0;
while ((y < sizeof(buffer)) && (count < 100)) {
z = uCellSockWrite(cellHandle, sockHandle,
&U_CELL_PWR_TEST_ECHO_STRING[y],
sizeof(buffer) - y);
if (z > 0) {
y += z;
} else {
uPortTaskBlock(500);
}
count++;
}
if (y == sizeof(buffer)) {
U_TEST_PRINT_LINE("%d byte(s) sent.", y);
}
// Get the data back again
U_TEST_PRINT_LINE("receiving echoed data back...");
y = 0;
count = 0;
memset(buffer, 0, sizeof(buffer));
while ((y < sizeof(buffer)) && (count < 100)) {
z = uCellSockRead(cellHandle, sockHandle,
buffer + y, sizeof(buffer) - y);
if (z > 0) {
y += z;
} else {
uPortTaskBlock(500);
}
count++;
}
U_TEST_PRINT_LINE("%d byte(s) received back.", y);
// Compare the data
return memcmp(buffer, U_CELL_PWR_TEST_ECHO_STRING, sizeof(buffer));
}
// Disconnect the given socket
static void disconnectFromEchoServer(uDeviceHandle_t cellHandle,
int32_t *pSockHandle)
{
// Close the socket
uCellSockClose(cellHandle, *pSockHandle, NULL);
*pSockHandle = -1;
// Deinit cell sockets
uCellSockDeinit();
}
// Calllback for when E-DRX parameters are changed.
static void eDrxCallback(uDeviceHandle_t cellHandle, uCellNetRat_t rat,
bool onNotOff,
int32_t eDrxSecondsRequested,
int32_t eDrxSecondsAssigned,
int32_t pagingWindowSecondsAssigned,
void *pParameter)
{
if (cellHandle != (uDeviceHandle_t)pParameter) {
gCallbackErrorCode = 1;
}
gEDrxParameters.rat = rat;
gEDrxParameters.onNotOff = onNotOff;
gEDrxParameters.eDrxSecondsRequested = eDrxSecondsRequested;
gEDrxParameters.eDrxSecondsAssigned = eDrxSecondsAssigned;
gEDrxParameters.pagingWindowSecondsAssigned = pagingWindowSecondsAssigned;
}
// Set some E-DRX parameters and return what was actually assigned.
// On entry the values at the pointers should be set to the values
// that the requested values would resolve to: e.g. an E-DRX value
// of 7 seconds would be expected to end up as a requested
// value of 10 seconds, since that's the nearest coded value.
// On exit the values at the pointers will be the assigned values.
static bool setEdrx(uDeviceHandle_t cellHandle, int32_t *pSockHandle,
uSockAddress_t *pEchoServerAddress,
uCellNetRat_t rat, bool onNotOff,
int32_t eDrxSeconds, int32_t pagingWindowSeconds,
bool *pOnNotOff, int32_t *pEDrxSeconds,
int32_t *pPagingWindowSeconds)
{
bool onNotOffExpected = *pOnNotOff;
int32_t eDrxSecondsExpected = *pEDrxSeconds;
int32_t pagingWindowSecondsExpected = *pPagingWindowSeconds;
bool rebooted = false;
*pOnNotOff = !onNotOff;
*pEDrxSeconds = -1;
*pPagingWindowSeconds = -1;
memset(&gEDrxParameters, 0, sizeof(gEDrxParameters));
gCallbackErrorCode = 0;
U_TEST_PRINT_LINE("**REQUESTING** E-DRX %s, %d second(s), paging window"
" %d second(s).", onNotOff ? "on" : "off",
eDrxSeconds, pagingWindowSeconds);
U_PORT_TEST_ASSERT(uCellPwrSetRequestedEDrx(cellHandle, rat,
onNotOff,
eDrxSeconds,
pagingWindowSeconds) == 0);
if (uCellPwrRebootIsRequired(cellHandle)) {
U_PORT_TEST_ASSERT(uCellPwrReboot(cellHandle, NULL) == 0);
#ifdef U_CELL_TEST_MUX_ALWAYS
U_PORT_TEST_ASSERT(uCellMuxEnable(cellHandle) == 0);
#endif
// Re-make the cellular connection 'cos the request to get
// the assigned E-DRX parameters won't work otherwise
U_PORT_TEST_ASSERT(connectNetwork(cellHandle) == 0);
if ((pSockHandle != NULL) && (*pSockHandle >= 0) && (pEchoServerAddress != NULL)) {
// And reconnect the socket
*pSockHandle = connectToEchoServer(cellHandle, pEchoServerAddress);
}
// LWM2M activity can get in the way of 3GPP power saving and
// some module types don't store the disabledness of the LWM2M
// client in NVRAM, so we need to disable it again after a reboot
uCellTestPrivateLwm2mDisable(cellHandle);
rebooted = true;
}
// Wait for the callback to be called if we have an expected value to check
if (eDrxSecondsExpected >= 0) {
U_TEST_PRINT_LINE("waiting for the URC...");
for (size_t x = 0; (x < 60) && (gEDrxParameters.rat != rat) &&
(gEDrxParameters.onNotOff != onNotOff) &&
(gEDrxParameters.eDrxSecondsRequested != eDrxSecondsExpected) &&
(gEDrxParameters.eDrxSecondsAssigned != eDrxSecondsExpected) &&
// Not all modules support setting or getting paging window so
// need to allow it to be -1
((gEDrxParameters.pagingWindowSecondsAssigned != -1) ||
(gEDrxParameters.pagingWindowSecondsAssigned != pagingWindowSecondsExpected)); x++) {
uPortTaskBlock(1000);
}
U_PORT_TEST_ASSERT(gCallbackErrorCode == 0);
// Now we get the requested E-DRX parameters and they should be correct
U_PORT_TEST_ASSERT(uCellPwrGetRequestedEDrx(cellHandle, rat,
&onNotOff,
&eDrxSeconds,
&pagingWindowSeconds) == 0);
U_TEST_PRINT_LINE("E-DRX set to %s, %d second(s), paging window"
" %d second(s).", onNotOff ? "on" : "off", eDrxSeconds,
pagingWindowSeconds);
U_PORT_TEST_ASSERT(onNotOff == onNotOffExpected);
U_PORT_TEST_ASSERT(eDrxSeconds == eDrxSecondsExpected);
// Not all modules support setting or getting paging window so
// it is not possible to check it
U_PORT_TEST_ASSERT((pagingWindowSeconds == -1) ||
(pagingWindowSecondsExpected == -1) ||
(pagingWindowSeconds == pagingWindowSecondsExpected));
// Finally get the assigned E-DRX parameters
U_PORT_TEST_ASSERT(uCellPwrGetEDrx(cellHandle,
uCellNetGetActiveRat(cellHandle),
pOnNotOff, pEDrxSeconds,
pPagingWindowSeconds) == 0);
U_PORT_TEST_ASSERT(*pOnNotOff == onNotOffExpected);
U_PORT_TEST_ASSERT(*pEDrxSeconds == eDrxSecondsExpected);
// Not all modules support setting or getting paging window so
// need to allow it to be -1
U_PORT_TEST_ASSERT((*pPagingWindowSeconds == -1) ||
(pagingWindowSecondsExpected == -1) ||
(*pPagingWindowSeconds == pagingWindowSecondsExpected));
}
return rebooted;
}
# endif // U_CFG_CELL_DISABLE_UART_POWER_SAVING
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS
* -------------------------------------------------------------- */
# if (U_CFG_APP_PIN_CELL_PWR_ON >= 0) && !defined(U_CFG_TEST_CELL_PWR_DISABLE)
/** Test all the power functions apart from reboot.
*
* IMPORTANT: see notes in u_cfg_test_platform_specific.h for the
* naming rules that must be followed when using the
* U_PORT_TEST_FUNCTION() macro.
*/
U_PORT_TEST_FUNCTION("[cellPwr]", "cellPwr")
{
uAtClientStreamHandle_t stream;
int32_t resourceCount;
// In case a previous test failed
uCellTestPrivateCleanup(&gHandles);
// Obtain the initial resource count
resourceCount = uTestUtilGetDynamicResourceCount();
// Note: not using the standard preamble here as
// we need to fiddle with the parameters into
// uCellInit().
U_PORT_TEST_ASSERT(uPortInit() == 0);
#ifdef U_CFG_APP_UART_PREFIX
U_PORT_TEST_ASSERT(uPortUartPrefix(U_PORT_STRINGIFY_QUOTED(U_CFG_APP_UART_PREFIX)) == 0);
#endif
gHandles.uartHandle = uPortUartOpen(U_CFG_APP_CELL_UART,
115200, NULL,
U_CELL_UART_BUFFER_LENGTH_BYTES,
U_CFG_APP_PIN_CELL_TXD,
U_CFG_APP_PIN_CELL_RXD,
U_CFG_APP_PIN_CELL_CTS,
U_CFG_APP_PIN_CELL_RTS);
U_PORT_TEST_ASSERT(gHandles.uartHandle >= 0);
U_PORT_TEST_ASSERT(uAtClientInit() == 0);
U_TEST_PRINT_LINE("adding an AT client on UART %d...",
U_CFG_APP_CELL_UART);
stream.type = U_AT_CLIENT_STREAM_TYPE_UART;
stream.handle.int32 = gHandles.uartHandle;
gHandles.atClientHandle = uAtClientAddExt(&stream, NULL, U_CELL_AT_BUFFER_LENGTH_BYTES);
U_PORT_TEST_ASSERT(gHandles.atClientHandle != NULL);
// So that we can see what we're doing
uAtClientPrintAtSet(gHandles.atClientHandle, true);
U_PORT_TEST_ASSERT(uCellInit() == 0);
// The main bit, which is done with and
// without use of the VInt pin, even
// if it is connected
testPowerAliveVInt(&gHandles, -1);
# if U_CFG_APP_PIN_CELL_VINT >= 0
testPowerAliveVInt(&gHandles, U_CFG_APP_PIN_CELL_VINT);
# endif
U_PORT_TEST_ASSERT(gCallbackErrorCode == 0);
// Do the standard postamble, leaving the module on for the next
// test to speed things up
uCellTestPrivatePostamble(&gHandles, false);
// Check for resource leaks
uTestUtilResourceCheck(U_TEST_PREFIX, NULL, true);
resourceCount = uTestUtilGetDynamicResourceCount() - resourceCount;
U_TEST_PRINT_LINE("we have leaked %d resources(s).", resourceCount);
U_PORT_TEST_ASSERT(resourceCount <= 0);
}
# endif // if (U_CFG_APP_PIN_CELL_PWR_ON >= 0) && !defined(U_CFG_TEST_CELL_PWR_DISABLE)
# if (U_CFG_APP_PIN_CELL_PWR_ON >= 0)
/** Power on process testing for any module type.
*/
U_PORT_TEST_FUNCTION("[cellPwr]", "cellPwrAnyModule")
{
uAtClientStreamHandle_t stream;
int32_t resourceCount;
int32_t returnCode;
uDeviceHandle_t cellHandle;
const uCellPrivateModule_t *pModule;
// In case a previous test failed
uCellTestPrivateCleanup(&gHandles);
// Obtain the initial resource count
resourceCount = uTestUtilGetDynamicResourceCount();
// Note: not using the standard preamble here as
// we need to fiddle with the parameters into
// uCellInit().
U_PORT_TEST_ASSERT(uPortInit() == 0);
#ifdef U_CFG_APP_UART_PREFIX
U_PORT_TEST_ASSERT(uPortUartPrefix(U_PORT_STRINGIFY_QUOTED(U_CFG_APP_UART_PREFIX)) == 0);
#endif
gHandles.uartHandle = uPortUartOpen(U_CFG_APP_CELL_UART,
115200, NULL,
U_CELL_UART_BUFFER_LENGTH_BYTES,
U_CFG_APP_PIN_CELL_TXD,
U_CFG_APP_PIN_CELL_RXD,
U_CFG_APP_PIN_CELL_CTS,
U_CFG_APP_PIN_CELL_RTS);
U_PORT_TEST_ASSERT(gHandles.uartHandle >= 0);
U_PORT_TEST_ASSERT(uAtClientInit() == 0);
U_TEST_PRINT_LINE("adding an AT client on UART %d...",
U_CFG_APP_CELL_UART);
stream.type = U_AT_CLIENT_STREAM_TYPE_UART;
stream.handle.int32 = gHandles.uartHandle;
gHandles.atClientHandle = uAtClientAddExt(&stream, NULL, U_CELL_AT_BUFFER_LENGTH_BYTES);
U_PORT_TEST_ASSERT(gHandles.atClientHandle != NULL);
// So that we can see what we're doing
uAtClientPrintAtSet(gHandles.atClientHandle, true);
U_PORT_TEST_ASSERT(uCellInit() == 0);
U_TEST_PRINT_LINE("adding a cellular instance on the AT client...");
returnCode = uCellAdd(U_CELL_MODULE_TYPE_ANY,
gHandles.atClientHandle,
U_CFG_APP_PIN_CELL_ENABLE_POWER,
U_CFG_APP_PIN_CELL_PWR_ON,
#if U_CFG_APP_PIN_CELL_VINT >= 0
U_CFG_APP_PIN_CELL_VINT,
#else
-1,
#endif
false, &gHandles.cellHandle);
U_PORT_TEST_ASSERT_EQUAL((int32_t) U_ERROR_COMMON_SUCCESS, returnCode);
cellHandle = gHandles.cellHandle;
#if defined(U_CFG_APP_PIN_CELL_DTR) && (U_CFG_APP_PIN_CELL_DTR >= 0)
uCellPwrSetDtrPowerSavingPin(cellHandle, U_CFG_APP_PIN_CELL_DTR);
#endif
// If the module is on at the start, switch it off.
if (uCellPwrIsAlive(cellHandle)) {
U_TEST_PRINT_LINE("powering off to begin test.");
uCellPwrOff(cellHandle, NULL);
U_TEST_PRINT_LINE("power off completed.");
}
// Get the private module data as we need it for testing
pModule = pUCellPrivateGetModule(cellHandle);
U_PORT_TEST_ASSERT(pModule != NULL);
// Allow the things to settle.
uPortTaskBlock(pModule->powerDownWaitSeconds * 1000);
// check whether it is completely off
U_PORT_TEST_ASSERT(!uCellPwrIsAlive(cellHandle));
U_TEST_PRINT_LINE("powering on...");
// Power on and configure the module.
U_PORT_TEST_ASSERT(uCellPwrOn(cellHandle, U_CELL_TEST_CFG_SIM_PIN,
NULL) == 0);
U_TEST_PRINT_LINE("checking that module is alive...");
U_PORT_TEST_ASSERT(uCellPwrIsAlive(cellHandle));
# ifdef U_CELL_TEST_MUX_ALWAYS
U_PORT_TEST_ASSERT(uCellMuxEnable(cellHandle) == 0);
# endif
// Do the standard postamble, leaving the module on for the next
// test to speed things up
uCellTestPrivatePostamble(&gHandles, false);
// Check for resource leaks
uTestUtilResourceCheck(U_TEST_PREFIX, NULL, true);
resourceCount = uTestUtilGetDynamicResourceCount() - resourceCount;
U_TEST_PRINT_LINE("we have leaked %d resources(s).", resourceCount);
U_PORT_TEST_ASSERT(resourceCount <= 0);
}
# endif // if (U_CFG_APP_PIN_CELL_PWR_ON >= 0)
/** Test reboot.
*/
U_PORT_TEST_FUNCTION("[cellPwr]", "cellPwrReboot")
{
int32_t resourceCount;
// In case a previous test failed
uCellTestPrivateCleanup(&gHandles);
// Obtain the initial resource count
resourceCount = uTestUtilGetDynamicResourceCount();
// Do the standard preamble
U_PORT_TEST_ASSERT(uCellTestPrivatePreamble(U_CFG_TEST_CELL_MODULE_TYPE,
&gHandles, true) == 0);
// Not much of a test really, need to find some setting
// that is ephemeral so that we know whether a reboot has
// occurred. Anyway, this will be tested in those tests that
// change bandmask and RAT.
U_TEST_PRINT_LINE("rebooting cellular...");
U_PORT_TEST_ASSERT(uCellPwrReboot(gHandles.cellHandle, NULL) == 0);
#ifdef U_CELL_TEST_MUX_ALWAYS
U_PORT_TEST_ASSERT(uCellMuxEnable(gHandles.cellHandle) == 0);
#endif
U_PORT_TEST_ASSERT(uCellPwrIsAlive(gHandles.cellHandle));
// Do the standard postamble, leaving the module on for the next
// test to speed things up
uCellTestPrivatePostamble(&gHandles, false);
// Check for resource leaks
uTestUtilResourceCheck(U_TEST_PREFIX, NULL, true);
resourceCount = uTestUtilGetDynamicResourceCount() - resourceCount;
U_TEST_PRINT_LINE("we have leaked %d resources(s).", resourceCount);
U_PORT_TEST_ASSERT(resourceCount <= 0);
}
/** Test reset
*/
U_PORT_TEST_FUNCTION("[cellPwr]", "cellPwrReset")
{
int32_t resourceCount;
int32_t x;
// In case a previous test failed
uCellTestPrivateCleanup(&gHandles);
// Obtain the initial resource count
resourceCount = uTestUtilGetDynamicResourceCount();
// Do the standard preamble
U_PORT_TEST_ASSERT(uCellTestPrivatePreamble(U_CFG_TEST_CELL_MODULE_TYPE,
&gHandles, true) == 0);
U_TEST_PRINT_LINE("resetting cellular...");
x = uCellPwrResetHard(gHandles.cellHandle, U_CFG_APP_PIN_CELL_RESET);
# if U_CFG_APP_PIN_CELL_RESET >= 0
U_PORT_TEST_ASSERT(x == 0);
# else
U_PORT_TEST_ASSERT(x < 0);
# endif
U_PORT_TEST_ASSERT(uCellPwrIsAlive(gHandles.cellHandle));
// Do the standard postamble, leaving the module on for the next
// test to speed things up
uCellTestPrivatePostamble(&gHandles, false);
// Check for resource leaks
uTestUtilResourceCheck(U_TEST_PREFIX, NULL, true);
resourceCount = uTestUtilGetDynamicResourceCount() - resourceCount;
U_TEST_PRINT_LINE("we have leaked %d resources(s).", resourceCount);
U_PORT_TEST_ASSERT(resourceCount <= 0);
}
/** Test UART power saving.
*/
U_PORT_TEST_FUNCTION("[cellPwr]", "cellPwrSavingUart")
{
int32_t resourceCount;
uDeviceHandle_t cellHandle;
// In case a previous test failed
uCellTestPrivateCleanup(&gHandles);
// Obtain the initial resource count
resourceCount = uTestUtilGetDynamicResourceCount();
// Do the standard preamble
U_PORT_TEST_ASSERT(uCellTestPrivatePreamble(U_CFG_TEST_CELL_MODULE_TYPE,
&gHandles, true) == 0);
cellHandle = gHandles.cellHandle;
if (uCellPwrUartSleepIsEnabled(cellHandle)) {
// Check that enabling when already enabled is fine
U_PORT_TEST_ASSERT(uCellPwrEnableUartSleep(cellHandle) == 0);
// Now disable it and check that worked
U_PORT_TEST_ASSERT(uCellPwrDisableUartSleep(cellHandle) == 0);
U_PORT_TEST_ASSERT(!uCellPwrUartSleepIsEnabled(cellHandle));
// Check that disabling when already disabled is fine
U_PORT_TEST_ASSERT(uCellPwrDisableUartSleep(cellHandle) == 0);
// Now enable it again and check that worked
U_PORT_TEST_ASSERT(uCellPwrEnableUartSleep(cellHandle) == 0);
U_PORT_TEST_ASSERT(uCellPwrUartSleepIsEnabled(cellHandle));