xref: /btstack/platform/daemon/example/le_scan.c (revision 2531c97e167e1cae4616c774a6c07906b45ebb44)
1*2531c97eSMatthias Ringwald /*
2*2531c97eSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3*2531c97eSMatthias Ringwald  *
4*2531c97eSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*2531c97eSMatthias Ringwald  * modification, are permitted provided that the following conditions
6*2531c97eSMatthias Ringwald  * are met:
7*2531c97eSMatthias Ringwald  *
8*2531c97eSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*2531c97eSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*2531c97eSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*2531c97eSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*2531c97eSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*2531c97eSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*2531c97eSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*2531c97eSMatthias Ringwald  *    from this software without specific prior written permission.
16*2531c97eSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*2531c97eSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*2531c97eSMatthias Ringwald  *    monetary gain.
19*2531c97eSMatthias Ringwald  *
20*2531c97eSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*2531c97eSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*2531c97eSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*2531c97eSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*2531c97eSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*2531c97eSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*2531c97eSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*2531c97eSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*2531c97eSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*2531c97eSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*2531c97eSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*2531c97eSMatthias Ringwald  * SUCH DAMAGE.
32*2531c97eSMatthias Ringwald  *
33*2531c97eSMatthias Ringwald  * Please inquire about commercial licensing options at
34*2531c97eSMatthias Ringwald  * [email protected]
35*2531c97eSMatthias Ringwald  *
36*2531c97eSMatthias Ringwald  */
37*2531c97eSMatthias Ringwald 
38*2531c97eSMatthias Ringwald /*
39*2531c97eSMatthias Ringwald  *  le_scan.c
40*2531c97eSMatthias Ringwald  */
41*2531c97eSMatthias Ringwald 
42*2531c97eSMatthias Ringwald #include <unistd.h>
43*2531c97eSMatthias Ringwald #include <stdio.h>
44*2531c97eSMatthias Ringwald #include <stdlib.h>
45*2531c97eSMatthias Ringwald #include <string.h>
46*2531c97eSMatthias Ringwald 
47*2531c97eSMatthias Ringwald #include "btstack_client.h"
48*2531c97eSMatthias Ringwald #include "btstack_run_loop_posix.h"
49*2531c97eSMatthias Ringwald 
50*2531c97eSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
51*2531c97eSMatthias Ringwald 
52*2531c97eSMatthias Ringwald 	// printf("- packet_handler: pt: 0x%02x, packet[0]: 0x%02x\n", packet_type, packet[0]);
53*2531c97eSMatthias Ringwald 	if (packet_type != HCI_EVENT_PACKET) return;
54*2531c97eSMatthias Ringwald 
55*2531c97eSMatthias Ringwald 	uint8_t event = packet[0];
56*2531c97eSMatthias Ringwald 
57*2531c97eSMatthias Ringwald 	switch (event){
58*2531c97eSMatthias Ringwald 		case BTSTACK_EVENT_STATE:
59*2531c97eSMatthias Ringwald 			// bt stack activated, get started
60*2531c97eSMatthias Ringwald 			printf("- btstack state %u\n", packet[2]);
61*2531c97eSMatthias Ringwald             if (packet[2] == HCI_STATE_WORKING) {
62*2531c97eSMatthias Ringwald             	bt_send_cmd(&gap_le_set_scan_parameters, 1, 0x30, 0x30);
63*2531c97eSMatthias Ringwald 	            bt_send_cmd(&gap_le_scan_start);
64*2531c97eSMatthias Ringwald 			}
65*2531c97eSMatthias Ringwald 			break;
66*2531c97eSMatthias Ringwald 		case HCI_EVENT_LE_META:
67*2531c97eSMatthias Ringwald             switch (packet[2]) {
68*2531c97eSMatthias Ringwald                 case HCI_SUBEVENT_LE_ADVERTISING_REPORT:
69*2531c97eSMatthias Ringwald                 	printf("\n- ADV: ");
70*2531c97eSMatthias Ringwald                 	printf_hexdump(packet, size);
71*2531c97eSMatthias Ringwald                     break;
72*2531c97eSMatthias Ringwald                 }
73*2531c97eSMatthias Ringwald                 break;
74*2531c97eSMatthias Ringwald 		default:
75*2531c97eSMatthias Ringwald 			break;
76*2531c97eSMatthias Ringwald 	}
77*2531c97eSMatthias Ringwald }
78*2531c97eSMatthias Ringwald 
79*2531c97eSMatthias Ringwald int main (int argc, const char * argv[]){
80*2531c97eSMatthias Ringwald 	printf("le_scan started\n");
81*2531c97eSMatthias Ringwald 	printf("- connecting to BTstack Daemon\n");
82*2531c97eSMatthias Ringwald 	// start stack
83*2531c97eSMatthias Ringwald 	btstack_run_loop_init(btstack_run_loop_posix_get_instance());
84*2531c97eSMatthias Ringwald 	int err = bt_open();
85*2531c97eSMatthias Ringwald 	if (err) {
86*2531c97eSMatthias Ringwald 		printf("-> Failed to open connection to BTstack Daemon\n");
87*2531c97eSMatthias Ringwald 		return err;
88*2531c97eSMatthias Ringwald 	}
89*2531c97eSMatthias Ringwald 
90*2531c97eSMatthias Ringwald 	printf("- connected\n");
91*2531c97eSMatthias Ringwald 	printf("- send power on\n");
92*2531c97eSMatthias Ringwald 
93*2531c97eSMatthias Ringwald 	bt_register_packet_handler(packet_handler);
94*2531c97eSMatthias Ringwald 	bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON );
95*2531c97eSMatthias Ringwald 	btstack_run_loop_execute();
96*2531c97eSMatthias Ringwald 	bt_close();
97*2531c97eSMatthias Ringwald 	return 0;
98*2531c97eSMatthias Ringwald }
99