xref: /btstack/test/hfp/hfp_h2_sync_test.cpp (revision 25ffee39891bb7a805066310f909bbd6c101ba38)
14cedf01cSMatthias Ringwald #include "CppUTest/TestHarness.h"
24cedf01cSMatthias Ringwald #include "CppUTest/CommandLineTestRunner.h"
3*25ffee39SMatthias Ringwald 
44cedf01cSMatthias Ringwald #include "btstack_debug.h"
5*25ffee39SMatthias Ringwald #include "classic/hfp.h"
64cedf01cSMatthias Ringwald 
74cedf01cSMatthias Ringwald // Test
84cedf01cSMatthias Ringwald static uint8_t test_data[HFP_H2_SYNC_FRAME_SIZE];
94cedf01cSMatthias Ringwald static uint8_t test_valid_sbc_frame[HFP_H2_SYNC_FRAME_SIZE];
104cedf01cSMatthias Ringwald static uint8_t test_invalid_sbc_frame[HFP_H2_SYNC_FRAME_SIZE];
114cedf01cSMatthias Ringwald static uint8_t test_invalid_frame[HFP_H2_SYNC_FRAME_SIZE];
124cedf01cSMatthias Ringwald static hfp_h2_sync_t test_hfp_h2_sync;
134cedf01cSMatthias Ringwald static uint8_t test_num_good_frames;
144cedf01cSMatthias Ringwald static uint8_t test_num_bad_frames;
154cedf01cSMatthias Ringwald 
test_hfp_h2_sync_callback(bool bad_frame,const uint8_t * frame_data,uint16_t frame_len)164cedf01cSMatthias Ringwald static bool test_hfp_h2_sync_callback(bool bad_frame, const uint8_t * frame_data, uint16_t frame_len){
174cedf01cSMatthias Ringwald     btstack_assert(frame_len == HFP_H2_SYNC_FRAME_SIZE);
184cedf01cSMatthias Ringwald     if (bad_frame){
194cedf01cSMatthias Ringwald         test_num_bad_frames++;
204cedf01cSMatthias Ringwald         return false;
214cedf01cSMatthias Ringwald     } else {
224cedf01cSMatthias Ringwald         // mSBC frame ok <=> sync word = 0xAD
234cedf01cSMatthias Ringwald         if (frame_data[2] == 0xAD) {
244cedf01cSMatthias Ringwald             test_num_good_frames++;
254cedf01cSMatthias Ringwald             return true;
264cedf01cSMatthias Ringwald         }
274cedf01cSMatthias Ringwald     }
284cedf01cSMatthias Ringwald     return false;
294cedf01cSMatthias Ringwald }
304cedf01cSMatthias Ringwald 
TEST_GROUP(HFP_H2_SYNC)314cedf01cSMatthias Ringwald TEST_GROUP(HFP_H2_SYNC){
324cedf01cSMatthias Ringwald     void setup(void){
334cedf01cSMatthias Ringwald         test_num_good_frames = 0;
344cedf01cSMatthias Ringwald         test_num_bad_frames = 0;
354cedf01cSMatthias Ringwald         test_valid_sbc_frame[0] = 0x01;
364cedf01cSMatthias Ringwald         test_valid_sbc_frame[1] = 0x08;
374cedf01cSMatthias Ringwald         test_valid_sbc_frame[2] = 0xAD;
384cedf01cSMatthias Ringwald         test_invalid_sbc_frame[0] = 0x01;
394cedf01cSMatthias Ringwald         test_invalid_sbc_frame[1] = 0x08;
404cedf01cSMatthias Ringwald         test_invalid_sbc_frame[2] = 0xFF;
414cedf01cSMatthias Ringwald         hfp_h2_sync_init(&test_hfp_h2_sync, &test_hfp_h2_sync_callback);
424cedf01cSMatthias Ringwald     }
434cedf01cSMatthias Ringwald     void teardown(void){
444cedf01cSMatthias Ringwald     }
454cedf01cSMatthias Ringwald };
464cedf01cSMatthias Ringwald 
474cedf01cSMatthias Ringwald // initial setting
TEST(HFP_H2_SYNC,ValidSBCFrame)484cedf01cSMatthias Ringwald TEST(HFP_H2_SYNC, ValidSBCFrame){
494cedf01cSMatthias Ringwald     hfp_h2_sync_process(&test_hfp_h2_sync, false, test_valid_sbc_frame, sizeof(test_valid_sbc_frame));
504cedf01cSMatthias Ringwald     CHECK_EQUAL(1, test_num_good_frames);
514cedf01cSMatthias Ringwald }
524cedf01cSMatthias Ringwald 
TEST(HFP_H2_SYNC,ValidSBCFramefter59)534cedf01cSMatthias Ringwald TEST(HFP_H2_SYNC, ValidSBCFramefter59){
544cedf01cSMatthias Ringwald     hfp_h2_sync_process(&test_hfp_h2_sync, false, test_invalid_frame, sizeof(test_invalid_frame)-1);
554cedf01cSMatthias Ringwald     hfp_h2_sync_process(&test_hfp_h2_sync, false, test_valid_sbc_frame, sizeof(test_valid_sbc_frame));
564cedf01cSMatthias Ringwald     CHECK_EQUAL(1, test_num_good_frames);
574cedf01cSMatthias Ringwald }
584cedf01cSMatthias Ringwald 
TEST(HFP_H2_SYNC,ValidSBCFrameAfter2)594cedf01cSMatthias Ringwald TEST(HFP_H2_SYNC, ValidSBCFrameAfter2){
604cedf01cSMatthias Ringwald     hfp_h2_sync_process(&test_hfp_h2_sync, false, test_invalid_frame, 2);
614cedf01cSMatthias Ringwald     hfp_h2_sync_process(&test_hfp_h2_sync, false, test_valid_sbc_frame, sizeof(test_valid_sbc_frame));
624cedf01cSMatthias Ringwald     CHECK_EQUAL(1, test_num_good_frames);
634cedf01cSMatthias Ringwald     CHECK_EQUAL(2, test_hfp_h2_sync.dropped_bytes);
644cedf01cSMatthias Ringwald }
654cedf01cSMatthias Ringwald 
TEST(HFP_H2_SYNC,BadAndGoodFrame)664cedf01cSMatthias Ringwald TEST(HFP_H2_SYNC, BadAndGoodFrame){
674cedf01cSMatthias Ringwald     hfp_h2_sync_process(&test_hfp_h2_sync, false, test_invalid_frame, sizeof(test_invalid_frame));
684cedf01cSMatthias Ringwald     CHECK_EQUAL(0, test_num_good_frames);
694cedf01cSMatthias Ringwald     hfp_h2_sync_process(&test_hfp_h2_sync, false, test_valid_sbc_frame, sizeof(test_valid_sbc_frame));
704cedf01cSMatthias Ringwald     CHECK_EQUAL(1, test_num_bad_frames);
714cedf01cSMatthias Ringwald     CHECK_EQUAL(1, test_num_good_frames);
724cedf01cSMatthias Ringwald }
734cedf01cSMatthias Ringwald 
TEST(HFP_H2_SYNC,BadFrameFlagA)744cedf01cSMatthias Ringwald TEST(HFP_H2_SYNC, BadFrameFlagA){
754cedf01cSMatthias Ringwald     hfp_h2_sync_process(&test_hfp_h2_sync, false, test_valid_sbc_frame, sizeof(test_data) - 1);
764cedf01cSMatthias Ringwald     hfp_h2_sync_process(&test_hfp_h2_sync, true, test_valid_sbc_frame, 1);
774cedf01cSMatthias Ringwald     CHECK_EQUAL(1, test_num_bad_frames);
784cedf01cSMatthias Ringwald }
794cedf01cSMatthias Ringwald 
TEST(HFP_H2_SYNC,BadFrameFlagB)804cedf01cSMatthias Ringwald TEST(HFP_H2_SYNC, BadFrameFlagB){
814cedf01cSMatthias Ringwald     hfp_h2_sync_process(&test_hfp_h2_sync, true, test_valid_sbc_frame, sizeof(test_valid_sbc_frame));
824cedf01cSMatthias Ringwald     CHECK_EQUAL(1, test_num_bad_frames);
834cedf01cSMatthias Ringwald }
844cedf01cSMatthias Ringwald 
TEST(HFP_H2_SYNC,InvalidSBCFrame)854cedf01cSMatthias Ringwald TEST(HFP_H2_SYNC, InvalidSBCFrame){
864cedf01cSMatthias Ringwald     hfp_h2_sync_process(&test_hfp_h2_sync, false, test_invalid_sbc_frame, sizeof(test_invalid_sbc_frame));
874cedf01cSMatthias Ringwald     hfp_h2_sync_process(&test_hfp_h2_sync, false, test_invalid_frame, 2);
884cedf01cSMatthias Ringwald     CHECK_EQUAL(1, test_num_bad_frames);
894cedf01cSMatthias Ringwald     CHECK_EQUAL(0, test_num_good_frames);
904cedf01cSMatthias Ringwald }
914cedf01cSMatthias Ringwald 
main(int argc,const char * argv[])924cedf01cSMatthias Ringwald int main (int argc, const char * argv[]){
934cedf01cSMatthias Ringwald     return CommandLineTestRunner::RunAllTests(argc, argv);
944cedf01cSMatthias Ringwald }
95