12531c97eSMatthias Ringwald /* 22531c97eSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 32531c97eSMatthias Ringwald * 42531c97eSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 52531c97eSMatthias Ringwald * modification, are permitted provided that the following conditions 62531c97eSMatthias Ringwald * are met: 72531c97eSMatthias Ringwald * 82531c97eSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 92531c97eSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 102531c97eSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 112531c97eSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 122531c97eSMatthias Ringwald * documentation and/or other materials provided with the distribution. 132531c97eSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 142531c97eSMatthias Ringwald * contributors may be used to endorse or promote products derived 152531c97eSMatthias Ringwald * from this software without specific prior written permission. 162531c97eSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 172531c97eSMatthias Ringwald * personal benefit and not for any commercial purpose or for 182531c97eSMatthias Ringwald * monetary gain. 192531c97eSMatthias Ringwald * 202531c97eSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 212531c97eSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 222531c97eSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 232531c97eSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 242531c97eSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 252531c97eSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 262531c97eSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 272531c97eSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 282531c97eSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 292531c97eSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 302531c97eSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 312531c97eSMatthias Ringwald * SUCH DAMAGE. 322531c97eSMatthias Ringwald * 332531c97eSMatthias Ringwald * Please inquire about commercial licensing options at 342531c97eSMatthias Ringwald * [email protected] 352531c97eSMatthias Ringwald * 362531c97eSMatthias Ringwald */ 372531c97eSMatthias Ringwald 38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "le_scan.c" 39ab2c6ae4SMatthias Ringwald 402531c97eSMatthias Ringwald /* 412531c97eSMatthias Ringwald * le_scan.c 422531c97eSMatthias Ringwald */ 432531c97eSMatthias Ringwald 442531c97eSMatthias Ringwald #include <unistd.h> 452531c97eSMatthias Ringwald #include <stdio.h> 462531c97eSMatthias Ringwald #include <stdlib.h> 472531c97eSMatthias Ringwald #include <string.h> 482531c97eSMatthias Ringwald 492531c97eSMatthias Ringwald #include "btstack_client.h" 50fa968b99SMatthias Ringwald 51fa968b99SMatthias Ringwald #ifdef _WIN32 52*2ca78d18SMatthias Ringwald #include "btstack_run_loop_windows.h" 53fa968b99SMatthias Ringwald #else 54fa968b99SMatthias Ringwald #include "btstack_run_loop_posix.h" 55fa968b99SMatthias Ringwald #endif 562531c97eSMatthias Ringwald 572531c97eSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 582531c97eSMatthias Ringwald 592531c97eSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 602531c97eSMatthias Ringwald 610e2df43fSMatthias Ringwald uint8_t event = hci_event_packet_get_type(packet); 622531c97eSMatthias Ringwald 632531c97eSMatthias Ringwald switch (event){ 642531c97eSMatthias Ringwald case BTSTACK_EVENT_STATE: 652531c97eSMatthias Ringwald // bt stack activated, get started 662531c97eSMatthias Ringwald printf("- btstack state %u\n", packet[2]); 67be7cc9a0SMilanka Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 682531c97eSMatthias Ringwald bt_send_cmd(&gap_le_set_scan_parameters, 1, 0x30, 0x30); 692531c97eSMatthias Ringwald bt_send_cmd(&gap_le_scan_start); 702531c97eSMatthias Ringwald } 712531c97eSMatthias Ringwald break; 722531c97eSMatthias Ringwald case HCI_EVENT_LE_META: 7310cad102SMilanka Ringwald switch (hci_event_le_meta_get_subevent_code(packet)) { 742531c97eSMatthias Ringwald case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 752531c97eSMatthias Ringwald printf("\n- ADV: "); 762531c97eSMatthias Ringwald printf_hexdump(packet, size); 772531c97eSMatthias Ringwald break; 782531c97eSMatthias Ringwald } 792531c97eSMatthias Ringwald break; 802531c97eSMatthias Ringwald default: 812531c97eSMatthias Ringwald break; 822531c97eSMatthias Ringwald } 832531c97eSMatthias Ringwald } 842531c97eSMatthias Ringwald 852531c97eSMatthias Ringwald int main (int argc, const char * argv[]){ 862531c97eSMatthias Ringwald printf("le_scan started\n"); 872531c97eSMatthias Ringwald printf("- connecting to BTstack Daemon\n"); 882531c97eSMatthias Ringwald // start stack 89b9dcd1ccSMatthias Ringwald #ifdef _WIN32 90b9dcd1ccSMatthias Ringwald btstack_run_loop_init(btstack_run_loop_windows_get_instance()); 91fa968b99SMatthias Ringwald #else 92fa968b99SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 93b9dcd1ccSMatthias Ringwald #endif 942531c97eSMatthias Ringwald int err = bt_open(); 952531c97eSMatthias Ringwald if (err) { 962531c97eSMatthias Ringwald printf("-> Failed to open connection to BTstack Daemon\n"); 972531c97eSMatthias Ringwald return err; 982531c97eSMatthias Ringwald } 992531c97eSMatthias Ringwald 1002531c97eSMatthias Ringwald printf("- connected\n"); 1012531c97eSMatthias Ringwald printf("- send power on\n"); 1022531c97eSMatthias Ringwald 1032531c97eSMatthias Ringwald bt_register_packet_handler(packet_handler); 1042531c97eSMatthias Ringwald bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON ); 1052531c97eSMatthias Ringwald btstack_run_loop_execute(); 1062531c97eSMatthias Ringwald bt_close(); 1072531c97eSMatthias Ringwald return 0; 1082531c97eSMatthias Ringwald } 109