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