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