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 BLUEKITCHEN
24 * GMBH 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 #define BTSTACK_FILE__ "test.c"
39
40 /*
41 * test.c
42 *
43 * Created by Matthias Ringwald on 7/14/09.
44 */
45
46 #include <unistd.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50
51 #include "btstack_client.h"
52 #include "btstack_run_loop.h"
53 #include "hci_cmd.h"
54
55 #ifdef _WIN32
56 #include "btstack_run_loop_windows.h"
57 #else
58 #include "btstack_run_loop_posix.h"
59 #endif
60
61 // bd_addr_t addr = {0x00, 0x03, 0xc9, 0x3d, 0x77, 0x43 }; // Think Outside Keyboard
62 // bd_addr_t addr = {0x00, 0x19, 0x1d, 0x90, 0x44, 0x68 }; // WiiMote
63 // bd_addr_t addr = {0x76, 0x6d, 0x62, 0xdb, 0xca, 0x73 }; // iPad
64 bd_addr_t addr = {0x04,0x0C,0xCE,0xE4,0x85,0xD3}; // MBA
65
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)66 void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
67
68 switch (packet_type) {
69
70 case HCI_EVENT_PACKET:
71
72 switch (hci_event_packet_get_type(packet)) {
73
74 case BTSTACK_EVENT_POWERON_FAILED:
75 printf("HCI Init failed - make sure you have turned off Bluetooth in the System Settings\n");
76 exit(1);
77 break;
78
79 case BTSTACK_EVENT_STATE:
80 // bt stack activated, get started
81 if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
82 uint8_t des_serviceSearchPattern[5] = {0x35, 0x03, 0x19, 0x10, 0x02};
83 bt_send_cmd(&sdp_client_query_rfcomm_services_cmd, addr, des_serviceSearchPattern);
84 }
85 break;
86
87 case SDP_EVENT_QUERY_COMPLETE:
88 // data: event(8), len(8), status(8)
89 printf("SDP_EVENT_QUERY_COMPLETE, status %u\n", packet[2]);
90 break;
91
92 case SDP_EVENT_QUERY_RFCOMM_SERVICE:
93 // data: event(8), len(8), rfcomm channel(8), name(var)
94 printf("SDP_EVENT_QUERY_RFCOMM_SERVICE, rfcomm channel %u, name '%s'\n", packet[2], (const char*)&packet[3]);
95 break;
96
97 default:
98 // other event
99 break;
100 }
101 break;
102
103 default:
104 // other packet type
105 break;
106 }
107 }
108
main(int argc,const char * argv[])109 int main (int argc, const char * argv[]){
110 #ifdef _WIN32
111 btstack_run_loop_init(btstack_run_loop_windows_get_instance());
112 #else
113 btstack_run_loop_init(btstack_run_loop_posix_get_instance());
114 #endif
115 int err = bt_open();
116 if (err) {
117 printf("Failed to open connection to BTdaemon\n");
118 return err;
119 }
120 bt_register_packet_handler(packet_handler);
121 bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON );
122 btstack_run_loop_execute();
123 bt_close();
124 return 0;
125 }
126