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__ "pbap_client_demo.c" 39 40 // ***************************************************************************** 41 /* EXAMPLE_START(pbap_client_demo): Connect to Phonebook Server and get contacts. 42 * 43 * @text Note: The Bluetooth address of the remote Phonbook server is hardcoded. 44 * Change it before running example, then use the UI to connect to it, to set and 45 * query contacts. 46 */ 47 // ***************************************************************************** 48 49 50 #include "btstack_config.h" 51 52 #include <stdint.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 57 #include "btstack_run_loop.h" 58 #include "l2cap.h" 59 #include "classic/rfcomm.h" 60 #include "btstack_event.h" 61 #include "classic/goep_client.h" 62 #include "classic/pbap_client.h" 63 64 #ifdef HAVE_BTSTACK_STDIN 65 #include "btstack_stdin.h" 66 #endif 67 68 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 69 70 static bd_addr_t remote_addr; 71 // MBP2016 "F4-0F-24-3B-1B-E1" 72 // Nexus 7 "30-85-A9-54-2E-78" 73 // iPhone SE "BC:EC:5D:E6:15:03" 74 // PTS "001BDC080AA5" 75 static char * remote_addr_string = "001BDC080AA5"; 76 77 static btstack_packet_callback_registration_t hci_event_callback_registration; 78 static uint16_t pbap_cid; 79 80 #ifdef HAVE_BTSTACK_STDIN 81 82 // Testig User Interface 83 static void show_usage(void){ 84 bd_addr_t iut_address; 85 gap_local_bd_addr(iut_address); 86 87 printf("\n--- Bluetooth PBAP Client (HF) Test Console %s ---\n", bd_addr_to_str(iut_address)); 88 printf("\n"); 89 printf("a - establish PBAP connection to %s\n", bd_addr_to_str(remote_addr)); 90 printf("b - set phonebook '/telecom/pb'\n"); 91 printf("c - set phonebook '/SIM1/telecom/pb'\n"); 92 printf("d - get phonebook size\n"); 93 printf("e - pull phonebook\n"); 94 printf("f - disconnnect\n"); 95 printf("p - authenticate using password '0000'\n"); 96 printf("\n"); 97 } 98 99 static void stdin_process(char c){ 100 switch (c){ 101 case 'a': 102 printf("[+] Connecting to %s...\n", bd_addr_to_str(remote_addr)); 103 pbap_connect(&packet_handler, remote_addr, &pbap_cid); 104 break; 105 case 'b': 106 printf("[+] Set Phonebook 'telecom/pb'\n"); 107 pbap_set_phonebook(pbap_cid, "telecom/pb"); 108 break; 109 case 'c': 110 printf("[+] Set Phonebook 'SIM1/telecom/pb'\n"); 111 pbap_set_phonebook(pbap_cid, "SIM1/telecom/pb"); 112 break; 113 case 'd': 114 pbap_get_phonebook_size(pbap_cid); 115 break; 116 case 'e': 117 pbap_pull_phonebook(pbap_cid); 118 break; 119 case 'f': 120 pbap_disconnect(pbap_cid); 121 break; 122 case 'p': 123 pbap_authentication_password(pbap_cid, "0000"); 124 break; 125 default: 126 show_usage(); 127 break; 128 } 129 } 130 131 // packet handler for interactive console 132 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 133 UNUSED(channel); 134 UNUSED(size); 135 int i; 136 uint8_t status; 137 switch (packet_type){ 138 case HCI_EVENT_PACKET: 139 switch (hci_event_packet_get_type(packet)) { 140 case BTSTACK_EVENT_STATE: 141 // BTstack activated, get started 142 if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 143 show_usage(); 144 } 145 break; 146 case HCI_EVENT_PBAP_META: 147 switch (hci_event_pbap_meta_get_subevent_code(packet)){ 148 case PBAP_SUBEVENT_CONNECTION_OPENED: 149 printf("[+] Connected\n"); 150 break; 151 case PBAP_SUBEVENT_CONNECTION_CLOSED: 152 printf("[+] Connection closed\n"); 153 break; 154 case PBAP_SUBEVENT_OPERATION_COMPLETED: 155 printf("[+] Operation complete\n"); 156 break; 157 case PBAP_SUBEVENT_AUTHENTICATION_REQUEST: 158 printf("[?] Authentication requested\n"); 159 break; 160 case PBAP_SUBEVENT_PHONEBOOK_SIZE: 161 status = pbap_subevent_phonebook_size_get_status(packet); 162 if (status){ 163 printf("[+] Get Phonebook size error: 0x%x\n", status); 164 } else { 165 printf("[+] Phonebook size: %u\n", pbap_subevent_phonebook_size_get_phoneboook_size(packet)); 166 } 167 break; 168 default: 169 break; 170 } 171 break; 172 default: 173 break; 174 } 175 break; 176 case PBAP_DATA_PACKET: 177 for (i=0;i<size;i++){ 178 printf("%c", packet[i]); 179 } 180 break; 181 default: 182 break; 183 } 184 } 185 #else 186 187 // packet handler for emdded system with fixed operation sequence 188 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 189 UNUSED(channel); 190 UNUSED(size); 191 int i; 192 switch (packet_type){ 193 case HCI_EVENT_PACKET: 194 switch (hci_event_packet_get_type(packet)) { 195 case BTSTACK_EVENT_STATE: 196 // BTstack activated, get started 197 if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 198 printf("[+] Connect to Phone Book Server on %s\n", bd_addr_to_str(remote_addr)); 199 pbap_connect(&packet_handler, remote_addr, &pbap_cid); 200 } 201 break; 202 case HCI_EVENT_PBAP_META: 203 switch (hci_event_pbap_meta_get_subevent_code(packet)){ 204 case PBAP_SUBEVENT_CONNECTION_OPENED: 205 printf("[+] Connected\n"); 206 printf("[+] Pull phonebook\n"); 207 pbap_pull_phonebook(pbap_cid); 208 break; 209 case PBAP_SUBEVENT_CONNECTION_CLOSED: 210 printf("[+] Connection closed\n"); 211 break; 212 case PBAP_SUBEVENT_OPERATION_COMPLETED: 213 printf("[+] Operation complete\n"); 214 printf("[+] Pull Phonebook complete\n"); 215 pbap_disconnect(pbap_cid); 216 break; 217 default: 218 break; 219 } 220 break; 221 default: 222 break; 223 } 224 break; 225 case PBAP_DATA_PACKET: 226 for (i=0;i<size;i++){ 227 printf("%c", packet[i]); 228 } 229 break; 230 default: 231 break; 232 } 233 } 234 #endif 235 236 int btstack_main(int argc, const char * argv[]); 237 int btstack_main(int argc, const char * argv[]){ 238 239 (void)argc; 240 (void)argv; 241 242 // init L2CAP 243 l2cap_init(); 244 245 // init RFCOM 246 rfcomm_init(); 247 248 // init GOEP Client 249 goep_client_init(); 250 251 // init PBAP Client 252 pbap_client_init(); 253 254 // register for HCI events 255 hci_event_callback_registration.callback = &packet_handler; 256 hci_add_event_handler(&hci_event_callback_registration); 257 258 sscanf_bd_addr(remote_addr_string, remote_addr); 259 260 #ifdef HAVE_BTSTACK_STDIN 261 btstack_stdin_setup(stdin_process); 262 #endif 263 264 // turn on! 265 hci_power_control(HCI_POWER_ON); 266 267 return 0; 268 } 269 /* EXAMPLE_END */ 270