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