xref: /btstack/test/ble_client/ad_parser_test.cpp (revision 1d3bd1e51ca491d6783233c8d7431c44f06daa5a)
14902524cSMatthias Ringwald /*
24902524cSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
34902524cSMatthias Ringwald  *
44902524cSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
54902524cSMatthias Ringwald  * modification, are permitted provided that the following conditions
64902524cSMatthias Ringwald  * are met:
74902524cSMatthias Ringwald  *
84902524cSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
94902524cSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
104902524cSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
114902524cSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
124902524cSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
134902524cSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
144902524cSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
154902524cSMatthias Ringwald  *    from this software without specific prior written permission.
164902524cSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
174902524cSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
184902524cSMatthias Ringwald  *    monetary gain.
194902524cSMatthias Ringwald  *
204902524cSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
214902524cSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
224902524cSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
234902524cSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
244902524cSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
254902524cSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
264902524cSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
274902524cSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
284902524cSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
294902524cSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
304902524cSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314902524cSMatthias Ringwald  * SUCH DAMAGE.
324902524cSMatthias Ringwald  *
334902524cSMatthias Ringwald  * Please inquire about commercial licensing options at
344902524cSMatthias Ringwald  * [email protected]
354902524cSMatthias Ringwald  *
364902524cSMatthias Ringwald  */
374902524cSMatthias Ringwald 
384902524cSMatthias Ringwald // *****************************************************************************
394902524cSMatthias Ringwald //
404902524cSMatthias Ringwald // test rfcomm query tests
414902524cSMatthias Ringwald //
424902524cSMatthias Ringwald // *****************************************************************************
434902524cSMatthias Ringwald 
444902524cSMatthias Ringwald 
454902524cSMatthias Ringwald #include <stdint.h>
464902524cSMatthias Ringwald #include <stdio.h>
474902524cSMatthias Ringwald #include <stdlib.h>
484902524cSMatthias Ringwald #include <string.h>
494902524cSMatthias Ringwald 
504902524cSMatthias Ringwald #include "CppUTest/TestHarness.h"
514902524cSMatthias Ringwald #include "CppUTest/CommandLineTestRunner.h"
524902524cSMatthias Ringwald 
534902524cSMatthias Ringwald #include "hci_cmd.h"
544902524cSMatthias Ringwald 
554902524cSMatthias Ringwald #include "btstack_memory.h"
564902524cSMatthias Ringwald #include "bluetooth_data_types.h"
574902524cSMatthias Ringwald #include "hci.h"
584902524cSMatthias Ringwald #include "ad_parser.h"
594902524cSMatthias Ringwald #include "l2cap.h"
604902524cSMatthias Ringwald 
61*1d3bd1e5SMatthias Ringwald extern "C" void le_handle_advertisement_report(uint8_t *packet, uint16_t size);
624902524cSMatthias Ringwald 
634902524cSMatthias Ringwald static uint8_t expected_bt_addr[] = {0x34, 0xB1, 0xF7, 0xD1, 0x77, 0x9B};
644902524cSMatthias Ringwald static uint8_t adv_multi_packet[] = {
654902524cSMatthias Ringwald     0x3E, 0x3B, 0x02, 0x03, // num_reports = 1
664902524cSMatthias Ringwald     // data_length = 9; event_size = 10 + data_length = 19 = 0x13
674902524cSMatthias Ringwald     // ( ev_type, ev_size, address type, address)
684902524cSMatthias Ringwald     0xE2, 0x01, 0x34, 0xB1, 0xF7, 0xD1, 0x77, 0x9B,
694902524cSMatthias Ringwald     0x09, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0xCC, // data len, data, rssi
704902524cSMatthias Ringwald 
714902524cSMatthias Ringwald     0xE2, 0x01, 0x34, 0xB1, 0xF7, 0xD1, 0x77, 0x9B,
724902524cSMatthias Ringwald     0x08, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xCB,       // data len, data, rssi
734902524cSMatthias Ringwald 
744902524cSMatthias Ringwald     0xE2, 0x01, 0x34, 0xB1, 0xF7, 0xD1, 0x77, 0x9B,
754902524cSMatthias Ringwald     0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xCA              // data len, data, rssi
764902524cSMatthias Ringwald };
774902524cSMatthias Ringwald 
784902524cSMatthias Ringwald static int adv_index = 0;
794902524cSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
804902524cSMatthias Ringwald 
CHECK_EQUAL_ARRAY(const uint8_t * expected,uint8_t * actual,int size)814902524cSMatthias Ringwald void CHECK_EQUAL_ARRAY(const uint8_t * expected, uint8_t * actual, int size){
824902524cSMatthias Ringwald     for (int i=0; i<size; i++){
834902524cSMatthias Ringwald         BYTES_EQUAL(expected[i], actual[i]);
844902524cSMatthias Ringwald     }
854902524cSMatthias Ringwald }
864902524cSMatthias Ringwald 
dummy_callback(void)874902524cSMatthias Ringwald static int dummy_callback(void){
884902524cSMatthias Ringwald     return 0;
894902524cSMatthias Ringwald }
904902524cSMatthias Ringwald 
914902524cSMatthias Ringwald static hci_transport_t dummy_transport = {
924902524cSMatthias Ringwald   /*  .transport.name                          = */  "DUMMY",
934902524cSMatthias Ringwald   /*  .transport.init                          = */  NULL,
944902524cSMatthias Ringwald   /*  .transport.open                          = */  NULL,
954902524cSMatthias Ringwald   /*  .transport.close                         = */  NULL,
964902524cSMatthias Ringwald   /*  .transport.register_packet_handler       = */  (void (*)(void (*)(uint8_t, uint8_t *, uint16_t))) dummy_callback,
974902524cSMatthias Ringwald   /*  .transport.can_send_packet_now           = */  NULL,
984902524cSMatthias Ringwald   /*  .transport.send_packet                   = */  NULL,
994902524cSMatthias Ringwald   /*  .transport.set_baudrate                  = */  NULL,
1004902524cSMatthias Ringwald };
1014902524cSMatthias Ringwald 
1024902524cSMatthias Ringwald 
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)1034902524cSMatthias Ringwald void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1044902524cSMatthias Ringwald     CHECK_EQUAL(0xE2, packet[2]);                   // event type
1054902524cSMatthias Ringwald     CHECK_EQUAL(0x01, packet[3]);                   // address type
1064902524cSMatthias Ringwald     CHECK_EQUAL_ARRAY(expected_bt_addr, &packet[4], 6);
1074902524cSMatthias Ringwald     CHECK_EQUAL(0xCC - adv_index, packet[10]);      // rssi
1084902524cSMatthias Ringwald     CHECK_EQUAL(0x09 - adv_index, packet[11]);      // data size
1094902524cSMatthias Ringwald 
1104902524cSMatthias Ringwald     for (int i=0; i<0x09 - adv_index; i++){         // data
1114902524cSMatthias Ringwald         CHECK_EQUAL(i, packet[12+i]);
1124902524cSMatthias Ringwald     }
1134902524cSMatthias Ringwald     adv_index++;
1144902524cSMatthias Ringwald }
1154902524cSMatthias Ringwald 
TEST_GROUP(ADParser)1164902524cSMatthias Ringwald TEST_GROUP(ADParser){
1174902524cSMatthias Ringwald     void setup(void){
1184902524cSMatthias Ringwald         hci_init(&dummy_transport, NULL);
1194902524cSMatthias Ringwald         hci_event_callback_registration.callback = &packet_handler;
1204902524cSMatthias Ringwald         hci_add_event_handler(&hci_event_callback_registration);
1214902524cSMatthias Ringwald     }
1224902524cSMatthias Ringwald };
1234902524cSMatthias Ringwald 
TEST(ADParser,TestAdvertisementEventMultipleReports)1244902524cSMatthias Ringwald TEST(ADParser, TestAdvertisementEventMultipleReports){
1254902524cSMatthias Ringwald     le_handle_advertisement_report(adv_multi_packet, sizeof(adv_multi_packet));
1264902524cSMatthias Ringwald }
1274902524cSMatthias Ringwald 
main(int argc,const char * argv[])1284902524cSMatthias Ringwald int main (int argc, const char * argv[]){
1294902524cSMatthias Ringwald     return CommandLineTestRunner::RunAllTests(argc, argv);
1304902524cSMatthias Ringwald }
131