xref: /btstack/test/ble_client/ad_parser_test.cpp (revision 1d3bd1e51ca491d6783233c8d7431c44f06daa5a)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 // *****************************************************************************
39 //
40 // test rfcomm query tests
41 //
42 // *****************************************************************************
43 
44 
45 #include <stdint.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #include "CppUTest/TestHarness.h"
51 #include "CppUTest/CommandLineTestRunner.h"
52 
53 #include "hci_cmd.h"
54 
55 #include "btstack_memory.h"
56 #include "bluetooth_data_types.h"
57 #include "hci.h"
58 #include "ad_parser.h"
59 #include "l2cap.h"
60 
61 extern "C" void le_handle_advertisement_report(uint8_t *packet, uint16_t size);
62 
63 static uint8_t expected_bt_addr[] = {0x34, 0xB1, 0xF7, 0xD1, 0x77, 0x9B};
64 static uint8_t adv_multi_packet[] = {
65     0x3E, 0x3B, 0x02, 0x03, // num_reports = 1
66     // data_length = 9; event_size = 10 + data_length = 19 = 0x13
67     // ( ev_type, ev_size, address type, address)
68     0xE2, 0x01, 0x34, 0xB1, 0xF7, 0xD1, 0x77, 0x9B,
69     0x09, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0xCC, // data len, data, rssi
70 
71     0xE2, 0x01, 0x34, 0xB1, 0xF7, 0xD1, 0x77, 0x9B,
72     0x08, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xCB,       // data len, data, rssi
73 
74     0xE2, 0x01, 0x34, 0xB1, 0xF7, 0xD1, 0x77, 0x9B,
75     0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xCA              // data len, data, rssi
76 };
77 
78 static int adv_index = 0;
79 static btstack_packet_callback_registration_t hci_event_callback_registration;
80 
CHECK_EQUAL_ARRAY(const uint8_t * expected,uint8_t * actual,int size)81 void CHECK_EQUAL_ARRAY(const uint8_t * expected, uint8_t * actual, int size){
82     for (int i=0; i<size; i++){
83         BYTES_EQUAL(expected[i], actual[i]);
84     }
85 }
86 
dummy_callback(void)87 static int dummy_callback(void){
88     return 0;
89 }
90 
91 static hci_transport_t dummy_transport = {
92   /*  .transport.name                          = */  "DUMMY",
93   /*  .transport.init                          = */  NULL,
94   /*  .transport.open                          = */  NULL,
95   /*  .transport.close                         = */  NULL,
96   /*  .transport.register_packet_handler       = */  (void (*)(void (*)(uint8_t, uint8_t *, uint16_t))) dummy_callback,
97   /*  .transport.can_send_packet_now           = */  NULL,
98   /*  .transport.send_packet                   = */  NULL,
99   /*  .transport.set_baudrate                  = */  NULL,
100 };
101 
102 
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)103 void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
104     CHECK_EQUAL(0xE2, packet[2]);                   // event type
105     CHECK_EQUAL(0x01, packet[3]);                   // address type
106     CHECK_EQUAL_ARRAY(expected_bt_addr, &packet[4], 6);
107     CHECK_EQUAL(0xCC - adv_index, packet[10]);      // rssi
108     CHECK_EQUAL(0x09 - adv_index, packet[11]);      // data size
109 
110     for (int i=0; i<0x09 - adv_index; i++){         // data
111         CHECK_EQUAL(i, packet[12+i]);
112     }
113     adv_index++;
114 }
115 
TEST_GROUP(ADParser)116 TEST_GROUP(ADParser){
117     void setup(void){
118         hci_init(&dummy_transport, NULL);
119         hci_event_callback_registration.callback = &packet_handler;
120         hci_add_event_handler(&hci_event_callback_registration);
121     }
122 };
123 
TEST(ADParser,TestAdvertisementEventMultipleReports)124 TEST(ADParser, TestAdvertisementEventMultipleReports){
125     le_handle_advertisement_report(adv_multi_packet, sizeof(adv_multi_packet));
126 }
127 
main(int argc,const char * argv[])128 int main (int argc, const char * argv[]){
129     return CommandLineTestRunner::RunAllTests(argc, argv);
130 }
131