55#include <stdio.h>
66
77#include <btstack.h>
8- #include <contiki.h>
8+
99#include <tinytest_macros.h>
1010#include <tinytest.h>
1111
1212#include <pbdrv/bluetooth.h>
1313
14+ #include <pbio/os.h>
1415#include <pbio/util.h>
1516#include <pbsys/host.h>
1617#include <pbsys/main.h>
1718#include <pbsys/status.h>
1819#include <test-pbio.h>
1920
20- #include "../drv/clock/clock_test.h"
21+ static pbio_error_t test_bluetooth (pbio_os_state_t * state , void * context ) {
22+
23+ static pbio_os_state_t sub ;
2124
22- static PT_THREAD (test_bluetooth (struct pt * pt )) {
23- PT_BEGIN (pt );
25+ PBIO_OS_ASYNC_BEGIN (state );
2426
2527 pbsys_host_init ();
2628
2729 // power should be initialized to off
2830 tt_want_uint_op (pbio_test_bluetooth_get_control_state (), = = , PBIO_TEST_BLUETOOTH_STATE_OFF );
2931
30- pbdrv_bluetooth_power_on (true);
32+ PBIO_OS_AWAIT (state , & sub , pbdrv_bluetooth_power_on (& sub , false));
33+ PBIO_OS_AWAIT (state , & sub , pbdrv_bluetooth_power_on (& sub , true));
3134
32- // wait for the power on delay
33- PT_WAIT_UNTIL (pt , ({
34- pbio_test_clock_tick (1 );
35- pbio_test_bluetooth_get_control_state () == PBIO_TEST_BLUETOOTH_STATE_ON ;
36- }));
35+ tt_want_uint_op (pbio_test_bluetooth_get_control_state (), = = , PBIO_TEST_BLUETOOTH_STATE_ON );
3736
38- pbdrv_bluetooth_start_advertising ();
37+ pbdrv_bluetooth_start_advertising (true);
38+ PBIO_OS_AWAIT (state , & sub , pbdrv_bluetooth_await_advertise_or_scan_command (& sub , NULL ));
3939
40- PT_WAIT_UNTIL (pt , ({
41- pbio_test_clock_tick (1 );
42- pbio_test_bluetooth_is_advertising_enabled ();
43- }));
40+ tt_want (pbio_test_bluetooth_is_advertising_enabled ());
4441
4542 pbio_test_bluetooth_connect ();
4643
47- PT_WAIT_UNTIL (pt , ({
48- pbio_test_clock_tick (1 );
49- pbio_test_bluetooth_is_connected ();
50- }));
44+ PBIO_OS_AWAIT_UNTIL (state , pbio_test_bluetooth_is_connected ());
5145
5246 // TODO: enable pybricks service notifications and do concurrent pybricks service and uart service calls
5347
5448 pbio_test_bluetooth_enable_pybricks_service_notifications ();
5549
5650 static const char * test_data_1 = "test\n" ;
5751 static const char * test_data_2 = "test2\n" ;
58- uint32_t size ;
52+ static uint32_t size ;
5953
60- PT_WAIT_UNTIL (pt , ({
61- pbio_test_clock_tick (1 );
54+ PBIO_OS_AWAIT_UNTIL (state , ({
6255 size = strlen (test_data_1 );
6356 pbdrv_bluetooth_tx ((const uint8_t * )test_data_1 , & size ) == PBIO_SUCCESS ;
6457 }));
6558
6659 tt_want_uint_op (size , = = , strlen (test_data_1 ));
6760
68- // yielding here should allow the pbsys bluetooth process to run and queue
69- // the data in the uart buffer
70- PT_YIELD (pt );
71-
72- // this next data should get pushed in the UART buffer but wait until the
73- // previous request is finished before sending
74- PT_WAIT_UNTIL (pt , ({
75- pbio_test_clock_tick (1 );
61+ // this next data should get pushed in the buffer too
62+ PBIO_OS_AWAIT_UNTIL (state , ({
7663 size = strlen (test_data_2 );
7764 pbdrv_bluetooth_tx ((const uint8_t * )test_data_2 , & size ) == PBIO_SUCCESS ;
7865 }));
7966
8067 tt_want_uint_op (size , = = , strlen (test_data_2 ));
8168
82- PT_YIELD (pt );
83-
8469 static const char * test_data_3 = "\x06test3\n" ;
8570 size = strlen (test_data_3 );
8671 pbio_test_bluetooth_send_pybricks_command ((const uint8_t * )test_data_3 , size );
8772
8873 static uint8_t rx_data [20 ];
8974
90- PT_WAIT_UNTIL (pt , ({
91- pbio_test_clock_tick (1 );
75+ PBIO_OS_AWAIT_UNTIL (state , ({
9276 size = PBIO_ARRAY_SIZE (rx_data );
9377 pbsys_host_stdin_read (rx_data , & size ) == PBIO_SUCCESS ;
9478 }));
@@ -104,15 +88,14 @@ static PT_THREAD(test_bluetooth(struct pt *pt)) {
10488 static uint32_t count ;
10589 count = pbio_test_bluetooth_get_pybricks_service_notification_count ();
10690
107- PT_WAIT_UNTIL (pt , ({
108- pbio_test_clock_tick (1 );
91+ PBIO_OS_AWAIT_UNTIL (state , ({
10992 pbio_test_bluetooth_get_pybricks_service_notification_count () != count ;
11093 }));
11194
112- PT_END ( pt );
95+ PBIO_OS_ASYNC_END ( PBIO_SUCCESS );
11396}
11497
11598struct testcase_t pbdrv_bluetooth_tests [] = {
116- PBIO_PT_THREAD_TEST (test_bluetooth ),
99+ PBIO_PT_THREAD_TEST_WITH_PBIO_OS (test_bluetooth ),
117100 END_OF_TESTCASES
118101};
0 commit comments