@@ -68,28 +68,28 @@
#define BINDER_BLUETOOTH_SERVICE_IFACE_CALLBACKS "android.hardware.bluetooth@1.0::IBluetoothHciCallbacks"
#define BINDER_BLUETOOTH_SERVICE_SLOT "default"
-// After bluetooth has been enabled we need to process pending packets (vhci -> HAL)
-// immediately before anything else for example turning bluetooth on/off can interfere.
-#define PRIORITY_PROCESS_PACKETS_ONCE (G_PRIORITY_DEFAULT - 1)
-// Priority for host write packet must be higher than the gio channel (PRIORITY_HOST_READ_PACKETS)
+// Priority for wait packet processed must be higher than the gio channel (PRIORITY_HOST_READ_PACKETS)
// otherwise we might process a second command
// before the first one was accepted.
// but it must be lower than the binder reply callback since otherwise we might send more packets
// than the remote can handle.
// it is defined in libgbinder as G_PRIORITY_DEFAULT and must stay in sync with this
-#define PRIORITY_HOST_WRITE_PACKET (G_PRIORITY_DEFAULT + 1)
-// The following two are the same since they are interleaved in the setup procedure.
-#define PRIORITY_HOST_READ_PACKETS (G_PRIORITY_DEFAULT + 2) // HAL -> vhci
-#define PRIORITY_CHECK_BT_STATE (G_PRIORITY_DEFAULT + 2)
+#define PRIORITY_WAIT_PACKET_PROCESSED (G_PRIORITY_DEFAULT + 1)
+// After bluetooth has been enabled we need to process pending packets (vhci -> HAL)
+// immediately before anything else for example turning bluetooth on/off can interfere.
+// but it still must be lower than the wait packet processed priority.
+#define PRIORITY_PROCESS_PACKETS_ONCE (G_PRIORITY_DEFAULT + 2)
+#define PRIORITY_HOST_READ_PACKETS (G_PRIORITY_DEFAULT + 3) // HAL -> vhci
+#define PRIORITY_CHECK_BT_STATE (G_PRIORITY_DEFAULT + 4)
// This one is to be executed after the setup procedure to potentially turn bluetooth off
// after the setup is complete if it was off before reboot for example.
// Needs to be higher than the rfkill channel otherwise an event from the user
// to turn bluetooth on/off might sneak in before we are done.
-#define PRIORITY_CHECK_BT_STATE_DONE (G_PRIORITY_DEFAULT + 3)
+#define PRIORITY_CHECK_BT_STATE_DONE (G_PRIORITY_DEFAULT + 5)
// Priority of turning bluetooth on and off, should wait until binder callbacks
// are completely handled.
-#define PRIORITY_RFKILL_CHANNEL (G_PRIORITY_DEFAULT + 4)
+#define PRIORITY_RFKILL_CHANNEL (G_PRIORITY_DEFAULT + 6)
enum bluetooth_codes {
INITIALIZE = GBINDER_FIRST_CALL_TRANSACTION,
@@ -133,8 +133,6 @@
GBinderRemoteObject *remote;
GBinderServiceManager *sm;
- bool bluetooth_on;
- bool turn_off_bt_after_setup;
bool bluetooth_hal_initialized;
int death_id;
@@ -201,7 +199,7 @@
gbinder_writer_append_hidl_vec(&writer, (void*)((char*)buf + 1), len - 1, sizeof(uint8_t));
proxy->binder_replies_pending++;
- g_idle_add_full(PRIORITY_HOST_WRITE_PACKET, waiting_for_binder_reply, proxy, NULL);
+ g_idle_add_full(PRIORITY_WAIT_PACKET_PROCESSED, waiting_for_binder_reply, proxy, NULL);
if (((uint8_t*)buf)[0] == HCI_COMMAND_PKT) {
gbinder_client_transact(proxy->binder_client, SEND_HCI_COMMAND, 0, local_request, handle_binder_reply, NULL, proxy);
@@ -256,7 +254,7 @@
int status = 0;
bool fail = FALSE;
- if (bluetooth_on && !proxy->bluetooth_on) {
+ if (bluetooth_on) {
GBinderRemoteReply *reply;
GBinderLocalRequest *initialize_request;
@@ -270,14 +268,14 @@
reply = gbinder_client_transact_sync_reply
(proxy->binder_client, INITIALIZE, initialize_request, &status);
- if (status != GBINDER_STATUS_OK || !reply) {
+ if (status != GBINDER_STATUS_OK) {
fprintf(stderr, "ERROR: init reply: %p, %d\n", reply, status);
fail = TRUE;
}
gbinder_remote_reply_unref(reply);
gbinder_local_request_unref(initialize_request);
- } else if (!bluetooth_on && proxy->bluetooth_on) {
+ } else {
GBinderRemoteReply *reply;
fprintf(stderr, "Turning bluetooth off\n");
@@ -286,15 +284,12 @@
reply = gbinder_client_transact_sync_reply
(proxy->binder_client, CLOSE, NULL, &status);
- if (status != GBINDER_STATUS_OK || !reply) {
+ if (status != GBINDER_STATUS_OK) {
fprintf(stderr, "ERROR: close reply: %p, %d\n", reply, status);
fail = TRUE;
}
gbinder_remote_reply_unref(reply);
- } else {
- fprintf(stderr, "WARNING: inconsistent bluetooth state: %d %d\n", proxy->bluetooth_on, bluetooth_on);
}
- proxy->bluetooth_on = bluetooth_on;
if (fail) {
binder_remote_died(NULL, proxy);
@@ -519,8 +514,7 @@
proxy->binder_client = gbinder_client_new(proxy->remote, BINDER_BLUETOOTH_SERVICE_IFACE);
- proxy->bluetooth_on = !proxy->bluetooth_on;
- configure_bt(proxy, !proxy->bluetooth_on);
+ configure_bt(proxy, FALSE);
return;
@@ -625,15 +619,6 @@
}
gboolean
-turn_off_bt_after_startup(
- gpointer user_data)
-{
- struct proxy *proxy = user_data;
- configure_bt(proxy, FALSE);
- return G_SOURCE_REMOVE;
-}
-
-gboolean
turn_on_bt_after_startup(
gpointer user_data)
{
@@ -694,12 +679,16 @@
}
close(sk);
- if (hci_test_bit(HCI_UP, &di.flags) && !hci_test_bit(HCI_INIT, &di.flags)) {
+
+ bdaddr_t zero_bdaddr;
+ memset(&zero_bdaddr, 0, sizeof(bdaddr_t));
+
+ if ((hci_test_bit(HCI_UP, &di.flags) && !hci_test_bit(HCI_INIT, &di.flags))
+ || (!hci_test_bit(HCI_UP, &di.flags) && memcmp(&zero_bdaddr, &di.bdaddr, sizeof(bdaddr_t) != 0))) {
fprintf(stderr, "successfully initialized bluetooth\n");
- if (proxy->turn_off_bt_after_setup) {
- g_idle_add_full(PRIORITY_CHECK_BT_STATE_DONE, turn_off_bt_after_startup, proxy, NULL);
- proxy->turn_off_bt_after_setup = FALSE;
- }
+#if USE_SYSTEMD
+ sd_notify(0, "READY=1");
+#endif
return G_SOURCE_REMOVE;
}
@@ -795,6 +784,24 @@
}
}
+gboolean
+turn_off_bt(
+ gpointer user_data)
+{
+ struct proxy *proxy = user_data;
+ configure_bt(proxy, FALSE);
+ return G_SOURCE_REMOVE;
+}
+
+gboolean
+turn_on_bt(
+ gpointer user_data)
+{
+ struct proxy *proxy = user_data;
+ configure_bt(proxy, TRUE);
+ return G_SOURCE_REMOVE;
+}
+
static
gboolean
rfkill_callback(
@@ -845,7 +852,11 @@
}
if (bt_event) {
- configure_bt(proxy, bluetooth_on);
+ if (bluetooth_on) {
+ g_idle_add_full(PRIORITY_RFKILL_CHANNEL, turn_on_bt, &proxy, NULL);
+ } else {
+ g_idle_add_full(PRIORITY_RFKILL_CHANNEL, turn_off_bt, &proxy, NULL);
+ }
}
return G_SOURCE_CONTINUE;
@@ -897,8 +908,6 @@
proxy.death_id = gbinder_remote_object_add_death_handler
(proxy.remote, binder_remote_died, &proxy);
- proxy.bluetooth_on = FALSE;
-
if (setup_vhci(&proxy)) {
gboolean bluetooth_on = FALSE;
@@ -931,6 +940,10 @@
}
}
+
|