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