Skip to content

Commit 6f337c3

Browse files
committed
[test] wiring/sleep20: minor
1 parent 285be9e commit 6f337c3

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

user/tests/wiring/sleep20/sleep20.cpp

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@
2222

2323
//TODO: Run user/tests/app/tracker_wakeup to verify the additional wakeup sources for platform tracker.
2424

25+
namespace {
26+
2527
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
2628
static retained uint32_t magick = 0;
2729
static retained uint32_t phase = 0;
2830

31+
constexpr system_tick_t CLOUD_CONNECT_TIMEOUT = 10 * 60 * 1000;
32+
33+
} // anonymous
34+
2935
test(01_System_Sleep_With_Configuration_Object_Hibernate_Mode_Without_Wakeup) {
3036
if (magick != 0xdeadbeef) {
3137
magick = 0xdeadbeef;
@@ -326,7 +332,7 @@ test(11_System_Sleep_With_Configuration_Object_Stop_Mode_Wakeup_By_D0) {
326332
.gpio(D0, RISING);
327333
SystemSleepResult result = System.sleep(config);
328334

329-
while (!Serial.isConnected());
335+
waitUntil(Serial.isConnected);
330336

331337
assertEqual(result.error(), SYSTEM_ERROR_NONE);
332338
assertEqual((int)result.wakeupReason(), (int)SystemSleepWakeupReason::BY_GPIO);
@@ -346,7 +352,7 @@ test(12_System_Sleep_With_Configuration_Object_Stop_Mode_Wakeup_By_Rtc) {
346352
.duration(3s);
347353
SystemSleepResult result = System.sleep(config);
348354

349-
while (!Serial.isConnected());
355+
waitUntil(Serial.isConnected);
350356

351357
assertEqual(result.error(), SYSTEM_ERROR_NONE);
352358
assertEqual((int)result.wakeupReason(), (int)SystemSleepWakeupReason::BY_RTC);
@@ -368,7 +374,7 @@ test(13_System_Sleep_With_Configuration_Object_Stop_Mode_Wakeup_By_Ble) {
368374
.ble();
369375
SystemSleepResult result = System.sleep(config);
370376

371-
while (!Serial.isConnected());
377+
waitUntil(Serial.isConnected);
372378

373379
assertEqual(result.error(), SYSTEM_ERROR_NONE);
374380
assertEqual((int)result.wakeupReason(), (int)SystemSleepWakeupReason::BY_BLE);
@@ -387,7 +393,7 @@ test(14_System_Sleep_Mode_Stop_Wakeup_By_D0) {
387393

388394
SleepResult result = System.sleep(D0, RISING);
389395

390-
while (!Serial.isConnected());
396+
waitUntil(Serial.isConnected);
391397

392398
assertEqual(result.error(), SYSTEM_ERROR_NONE);
393399
assertEqual((int)result.reason(), (int)WAKEUP_REASON_PIN);
@@ -404,7 +410,7 @@ test(15_System_Sleep_Mode_Stop_Wakeup_By_Rtc) {
404410

405411
SleepResult result = System.sleep(nullptr, 0, nullptr, 0, 3s);
406412

407-
while (!Serial.isConnected());
413+
waitUntil(Serial.isConnected);
408414

409415
assertEqual(result.error(), SYSTEM_ERROR_NONE);
410416
assertEqual((int)result.reason(), (int)WAKEUP_REASON_RTC);
@@ -423,7 +429,7 @@ test(16_System_Sleep_With_Configuration_Object_Ultra_Low_Power_Mode_Wakeup_By_D0
423429
.gpio(D0, RISING);
424430
SystemSleepResult result = System.sleep(config);
425431

426-
while (!Serial.isConnected());
432+
waitUntil(Serial.isConnected);
427433

428434
assertEqual(result.error(), SYSTEM_ERROR_NONE);
429435
assertEqual((int)result.wakeupReason(), (int)SystemSleepWakeupReason::BY_GPIO);
@@ -443,7 +449,7 @@ test(17_System_Sleep_With_Configuration_Object_Ultra_Low_Power_Mode_Wakeup_By_Rt
443449
.duration(3s);
444450
SystemSleepResult result = System.sleep(config);
445451

446-
while (!Serial.isConnected());
452+
waitUntil(Serial.isConnected);
447453

448454
assertEqual(result.error(), SYSTEM_ERROR_NONE);
449455
assertEqual((int)result.wakeupReason(), (int)SystemSleepWakeupReason::BY_RTC);
@@ -465,7 +471,7 @@ test(18_System_Sleep_With_Configuration_Object_Ultra_Low_Power_Mode_Wakeup_By_Bl
465471
.ble();
466472
SystemSleepResult result = System.sleep(config);
467473

468-
while (!Serial.isConnected());
474+
waitUntil(Serial.isConnected);
469475

470476
assertEqual(result.error(), SYSTEM_ERROR_NONE);
471477
assertEqual((int)result.wakeupReason(), (int)SystemSleepWakeupReason::BY_BLE);
@@ -487,7 +493,7 @@ test(19_System_Sleep_With_Configuration_Object_Stop_Mode_Wakeup_By_Analog_Pin) {
487493
.analog(A0, 1500, AnalogInterruptMode::CROSS);
488494
SystemSleepResult result = System.sleep(config);
489495

490-
while (!Serial.isConnected());
496+
waitUntil(Serial.isConnected);
491497

492498
assertEqual(result.error(), SYSTEM_ERROR_NONE);
493499
assertEqual((int)result.wakeupReason(), (int)SystemSleepWakeupReason::BY_LPCOMP);
@@ -507,7 +513,7 @@ test(20_System_Sleep_With_Configuration_Object_Ultra_Low_Power_Mode_Wakeup_By_An
507513
.analog(A0, 1500, AnalogInterruptMode::CROSS);
508514
SystemSleepResult result = System.sleep(config);
509515

510-
while (!Serial.isConnected());
516+
waitUntil(Serial.isConnected);
511517

512518
assertEqual(result.error(), SYSTEM_ERROR_NONE);
513519
assertEqual((int)result.wakeupReason(), (int)SystemSleepWakeupReason::BY_LPCOMP);
@@ -531,7 +537,7 @@ test(21_System_Sleep_With_Configuration_Object_Stop_Mode_Wakeup_By_Usart) {
531537

532538
Serial1.end();
533539

534-
while (!Serial.isConnected());
540+
waitUntil(Serial.isConnected);
535541

536542
assertEqual(result.error(), SYSTEM_ERROR_NONE);
537543
assertEqual((int)result.wakeupReason(), (int)SystemSleepWakeupReason::BY_USART);
@@ -555,7 +561,7 @@ test(22_System_Sleep_With_Configuration_Object_Ultra_Low_Power_Mode_Wakeup_By_Us
555561

556562
Serial1.end();
557563

558-
while (!Serial.isConnected());
564+
waitUntil(Serial.isConnected);
559565

560566
assertEqual(result.error(), SYSTEM_ERROR_NONE);
561567
assertEqual((int)result.wakeupReason(), (int)SystemSleepWakeupReason::BY_USART);
@@ -574,15 +580,15 @@ test(23_System_Sleep_With_Configuration_Object_Stop_Mode_Wakeup_By_Cellular) {
574580
Serial.println(" >> Connecting to the cloud");
575581
Cellular.on();
576582
Particle.connect();
577-
waitUntil(Particle.connected);
583+
assertTrue(waitFor(Particle.connected, CLOUD_CONNECT_TIMEOUT));
578584
Serial.println(" >> Connected to the cloud. You'll see the RGB is turned on after waking up.");
579585

580586
SystemSleepConfiguration config;
581587
config.mode(SystemSleepMode::STOP)
582588
.network(Cellular);
583589
SystemSleepResult result = System.sleep(config);
584590

585-
while (!Serial.isConnected());
591+
waitUntil(Serial.isConnected);
586592

587593
assertEqual(result.error(), SYSTEM_ERROR_NONE);
588594
assertEqual((int)result.wakeupReason(), (int)SystemSleepWakeupReason::BY_NETWORK);
@@ -600,15 +606,15 @@ test(24_System_Sleep_With_Configuration_Object_Ultra_Low_Power_Mode_Wakeup_By_Ce
600606
Serial.println(" >> Connecting to the cloud");
601607
Cellular.on();
602608
Particle.connect();
603-
waitUntil(Particle.connected);
609+
assertTrue(waitFor(Particle.connected, CLOUD_CONNECT_TIMEOUT));
604610
Serial.println(" >> Connected to the cloud. You'll see the RGB is turned on after waking up.");
605611

606612
SystemSleepConfiguration config;
607613
config.mode(SystemSleepMode::ULTRA_LOW_POWER)
608614
.network(Cellular);
609615
SystemSleepResult result = System.sleep(config);
610616

611-
while (!Serial.isConnected());
617+
waitUntil(Serial.isConnected);
612618

613619
assertEqual(result.error(), SYSTEM_ERROR_NONE);
614620
assertEqual((int)result.wakeupReason(), (int)SystemSleepWakeupReason::BY_NETWORK);
@@ -636,7 +642,7 @@ test(25_System_Sleep_With_Configuration_Object_Stop_Mode_Wakeup_By_WiFi) {
636642
.network(WiFi);
637643
SystemSleepResult result = System.sleep(config);
638644

639-
while (!Serial.isConnected());
645+
waitUntil(Serial.isConnected);
640646

641647
assertEqual(result.error(), SYSTEM_ERROR_NONE);
642648
assertEqual((int)result.wakeupReason(), (int)SystemSleepWakeupReason::BY_NETWORK);
@@ -662,7 +668,7 @@ test(26_System_Sleep_With_Configuration_Object_Ultra_Low_Power_Mode_Wakeup_By_Wi
662668
.network(WiFi);
663669
SystemSleepResult result = System.sleep(config);
664670

665-
while (!Serial.isConnected());
671+
waitUntil(Serial.isConnected);
666672

667673
assertEqual(result.error(), SYSTEM_ERROR_NONE);
668674
assertEqual((int)result.wakeupReason(), (int)SystemSleepWakeupReason::BY_NETWORK);
@@ -682,7 +688,8 @@ test(27_System_Sleep_With_Configuration_Object_Stop_Mode_Execution_Time) {
682688
Serial.println(" >> Connecting to the cloud");
683689
Network.on();
684690
Particle.connect();
685-
waitUntil(Particle.connected);
691+
waitFor(Particle.connected, CLOUD_CONNECT_TIMEOUT);
692+
assertTrue(Particle.connected());
686693
Serial.println(" >> Connected to the cloud. You'll see the RGB is turned on after waking up.");
687694

688695
SystemSleepConfiguration config;
@@ -693,14 +700,17 @@ test(27_System_Sleep_With_Configuration_Object_Stop_Mode_Execution_Time) {
693700
SystemSleepResult result = System.sleep(config);
694701
time32_t exit = Time.now();
695702

696-
while (!Serial.isConnected());
703+
waitUntil(Serial.isConnected);
697704
assertMoreOrEqual(exit - enter, SLEEP_DURATION_S);
698705
// There might be up to 30s delay to turn off the modem for particular platforms.
699706
assertLessOrEqual(exit - enter, 30 + SLEEP_DURATION_S);
700707
Serial.printf("Sleep execution time: %ld s\r\n", exit - enter);
701708

702709
assertEqual(result.error(), SYSTEM_ERROR_NONE);
703710
assertEqual((int)result.wakeupReason(), (int)SystemSleepWakeupReason::BY_RTC);
711+
712+
// Make sure we reconnect back to the cloud
713+
assertTrue(waitFor(Particle.connected, CLOUD_CONNECT_TIMEOUT));
704714
}
705715

706716
test(28_System_Sleep_With_Configuration_Object_Ultra_Low_Power_Mode_Wakeup_Execution_Time) {
@@ -715,7 +725,8 @@ test(28_System_Sleep_With_Configuration_Object_Ultra_Low_Power_Mode_Wakeup_Execu
715725
Serial.println(" >> Connecting to the cloud");
716726
Network.on();
717727
Particle.connect();
718-
waitUntil(Particle.connected);
728+
waitFor(Particle.connected, CLOUD_CONNECT_TIMEOUT);
729+
assertTrue(Particle.connected());
719730
Serial.println(" >> Connected to the cloud. You'll see the RGB is turned on after waking up.");
720731

721732
SystemSleepConfiguration config;
@@ -726,12 +737,15 @@ test(28_System_Sleep_With_Configuration_Object_Ultra_Low_Power_Mode_Wakeup_Execu
726737
SystemSleepResult result = System.sleep(config);
727738
time32_t exit = Time.now();
728739

729-
while (!Serial.isConnected());
740+
waitUntil(Serial.isConnected);
730741
assertMoreOrEqual(exit - enter, SLEEP_DURATION_S);
731742
// There might be up to 30s delay to turn off the modem for particular platforms.
732743
assertLessOrEqual(exit - enter, 30 + SLEEP_DURATION_S);
733744
Serial.printf("Sleep execution time: %ld s\r\n", exit - enter);
734745

735746
assertEqual(result.error(), SYSTEM_ERROR_NONE);
736747
assertEqual((int)result.wakeupReason(), (int)SystemSleepWakeupReason::BY_RTC);
748+
749+
// Make sure we reconnect back to the cloud
750+
assertTrue(waitFor(Particle.connected, CLOUD_CONNECT_TIMEOUT));
737751
}

0 commit comments

Comments
 (0)