hsp_ag.c (f41423b4b422712e5b7e6f07151d4f622b06461c) hsp_ag.c (aeadb7083ae32c74d2f1d531cc3a6443e69f8ea3)
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

--- 43 unchanged lines hidden (view full) ---

52#include "btstack_debug.h"
53#include "btstack_event.h"
54#include "btstack_memory.h"
55#include "btstack_run_loop.h"
56#include "classic/core.h"
57#include "classic/sdp_server.h"
58#include "classic/sdp_client_rfcomm.h"
59#include "classic/sdp_util.h"
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

--- 43 unchanged lines hidden (view full) ---

52#include "btstack_debug.h"
53#include "btstack_event.h"
54#include "btstack_memory.h"
55#include "btstack_run_loop.h"
56#include "classic/core.h"
57#include "classic/sdp_server.h"
58#include "classic/sdp_client_rfcomm.h"
59#include "classic/sdp_util.h"
60#include "classic/sdp_client.h"
60#include "hci.h"
61#include "hci_cmd.h"
62#include "hci_dump.h"
63#include "hsp_ag.h"
64#include "l2cap.h"
65
66#define HSP_HS_BUTTON_PRESS "AT+CKPD=200"
67#define HSP_HS_AT_CKPD "AT+CKPD\r\n"

--- 27 unchanged lines hidden (view full) ---

95static uint8_t hsp_disconnect_rfcomm = 0;
96static uint8_t hsp_establish_audio_connection = 0;
97static uint8_t hsp_release_audio_connection = 0;
98
99static btstack_packet_callback_registration_t hci_event_callback_registration;
100
101typedef enum {
102 HSP_IDLE,
61#include "hci.h"
62#include "hci_cmd.h"
63#include "hci_dump.h"
64#include "hsp_ag.h"
65#include "l2cap.h"
66
67#define HSP_HS_BUTTON_PRESS "AT+CKPD=200"
68#define HSP_HS_AT_CKPD "AT+CKPD\r\n"

--- 27 unchanged lines hidden (view full) ---

96static uint8_t hsp_disconnect_rfcomm = 0;
97static uint8_t hsp_establish_audio_connection = 0;
98static uint8_t hsp_release_audio_connection = 0;
99
100static btstack_packet_callback_registration_t hci_event_callback_registration;
101
102typedef enum {
103 HSP_IDLE,
103 HSP_SDP_QUERY_RFCOMM_CHANNEL,
104 HSP_W4_SDP_EVENT_QUERY_COMPLETE,
104 HSP_W2_SEND_SDP_QUERY,
105 HSP_W4_SDP_QUERY_COMPLETE,
105 HSP_W4_RFCOMM_CONNECTED,
106
107 HSP_RFCOMM_CONNECTION_ESTABLISHED,
108
109 HSP_W4_RING_ANSWER,
110 HSP_W4_USER_ACTION,
111 HSP_W2_CONNECT_SCO,
112 HSP_W4_SCO_CONNECTED,

--- 5 unchanged lines hidden (view full) ---

118
119 HSP_W2_DISCONNECT_RFCOMM,
120 HSP_W4_RFCOMM_DISCONNECTED,
121 HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN
122} hsp_state_t;
123
124static hsp_state_t hsp_state = HSP_IDLE;
125
106 HSP_W4_RFCOMM_CONNECTED,
107
108 HSP_RFCOMM_CONNECTION_ESTABLISHED,
109
110 HSP_W4_RING_ANSWER,
111 HSP_W4_USER_ACTION,
112 HSP_W2_CONNECT_SCO,
113 HSP_W4_SCO_CONNECTED,

--- 5 unchanged lines hidden (view full) ---

119
120 HSP_W2_DISCONNECT_RFCOMM,
121 HSP_W4_RFCOMM_DISCONNECTED,
122 HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN
123} hsp_state_t;
124
125static hsp_state_t hsp_state = HSP_IDLE;
126
126
127static btstack_context_callback_registration_t hsp_ag_handle_sdp_client_query_request;
127static btstack_packet_handler_t hsp_ag_callback;
128
129static void hsp_run(void);
130static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
131static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
132
133static void dummy_notify(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t size){
134 UNUSED(packet_type); // ok: no code

--- 142 unchanged lines hidden (view full) ---

277 hci_event_callback_registration.callback = &packet_handler;
278 hci_add_event_handler(&hci_event_callback_registration);
279
280 rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff); // reserved channel, mtu limited by l2cap
281
282 hsp_ag_reset_state();
283}
284
128static btstack_packet_handler_t hsp_ag_callback;
129
130static void hsp_run(void);
131static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
132static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
133
134static void dummy_notify(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t size){
135 UNUSED(packet_type); // ok: no code

--- 142 unchanged lines hidden (view full) ---

278 hci_event_callback_registration.callback = &packet_handler;
279 hci_add_event_handler(&hci_event_callback_registration);
280
281 rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff); // reserved channel, mtu limited by l2cap
282
283 hsp_ag_reset_state();
284}
285
286static void hsp_ag_handle_start_sdp_client_query(void * context){
287 UNUSED(context);
288 hsp_state = HSP_W4_SDP_QUERY_COMPLETE;
289 log_info("Start SDP query %s, 0x%02x", bd_addr_to_str(remote), BLUETOOTH_SERVICE_CLASS_HEADSET);
290 sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, BLUETOOTH_SERVICE_CLASS_HEADSET);
291}
292
285void hsp_ag_connect(bd_addr_t bd_addr){
286 if (hsp_state != HSP_IDLE) return;
293void hsp_ag_connect(bd_addr_t bd_addr){
294 if (hsp_state != HSP_IDLE) return;
287 hsp_state = HSP_SDP_QUERY_RFCOMM_CHANNEL;
295
288 (void)memcpy(remote, bd_addr, 6);
296 (void)memcpy(remote, bd_addr, 6);
289 hsp_run();
297 hsp_state = HSP_W2_SEND_SDP_QUERY;
298 hsp_ag_handle_sdp_client_query_request.callback = &hsp_ag_handle_start_sdp_client_query;
299 // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
300 (void) sdp_client_register_query_callback(&hsp_ag_handle_sdp_client_query_request);
290}
291
292void hsp_ag_disconnect(void){
293 hsp_ag_release_audio_connection();
294 if (hsp_state < HSP_W4_RFCOMM_CONNECTED){
295 hsp_state = HSP_IDLE;
296 return;
297 }

--- 131 unchanged lines hidden (view full) ---

429 if (hsp_disconnect_rfcomm){
430 hsp_disconnect_rfcomm = 0;
431 hsp_establish_audio_connection = 0;
432 rfcomm_disconnect(rfcomm_cid);
433 return;
434 }
435
436 switch (hsp_state){
301}
302
303void hsp_ag_disconnect(void){
304 hsp_ag_release_audio_connection();
305 if (hsp_state < HSP_W4_RFCOMM_CONNECTED){
306 hsp_state = HSP_IDLE;
307 return;
308 }

--- 131 unchanged lines hidden (view full) ---

440 if (hsp_disconnect_rfcomm){
441 hsp_disconnect_rfcomm = 0;
442 hsp_establish_audio_connection = 0;
443 rfcomm_disconnect(rfcomm_cid);
444 return;
445 }
446
447 switch (hsp_state){
437 case HSP_SDP_QUERY_RFCOMM_CHANNEL:
438 hsp_state = HSP_W4_SDP_EVENT_QUERY_COMPLETE;
439 log_info("Start SDP query %s, 0x%02x", bd_addr_to_str(remote), BLUETOOTH_SERVICE_CLASS_HEADSET);
440 sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, BLUETOOTH_SERVICE_CLASS_HEADSET);
441 break;
442
443 case HSP_W4_RING_ANSWER:
444 if (!ag_num_button_press_received) break;
445
446 if (!rfcomm_can_send_packet_now(rfcomm_cid)) {
447 rfcomm_request_can_send_now_event(rfcomm_cid);
448 return;
449 }

--- 245 unchanged lines hidden ---
448
449 case HSP_W4_RING_ANSWER:
450 if (!ag_num_button_press_received) break;
451
452 if (!rfcomm_can_send_packet_now(rfcomm_cid)) {
453 rfcomm_request_can_send_now_event(rfcomm_cid);
454 return;
455 }

--- 245 unchanged lines hidden ---