From 4d6096bfdd54251bc07111afce7290aa0326c6ee Mon Sep 17 00:00:00 2001 From: Matt Ranostay Date: Sat, 15 Dec 2018 16:47:28 -0800 Subject: plugins: hal-bt: use transport events for streaming enablement Use Bluetooth-Manager/media events that signal A2DP transport status to enable/disable the 4A stream. Bug-AGL: SPEC-1986 Change-Id: I1c2bffc63d0fd0802c0ea3f8590eab18c15d9f3d Signed-off-by: Matt Ranostay --- plugins/lib/bluetooth/hal-bt-data.c | 89 ++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 31 deletions(-) diff --git a/plugins/lib/bluetooth/hal-bt-data.c b/plugins/lib/bluetooth/hal-bt-data.c index 6a70495..5eb52cd 100644 --- a/plugins/lib/bluetooth/hal-bt-data.c +++ b/plugins/lib/bluetooth/hal-bt-data.c @@ -68,7 +68,7 @@ int HalBtDataRemoveSelectedBtDeviceFromList(struct HalBtDeviceData **firstBtDevi struct HalBtDeviceData *HalBtDataAddBtDeviceToBtDeviceList(struct HalBtDeviceData **firstBtDeviceData, json_object *currentSingleBtDeviceDataJ) { - char *currentBtDeviceName, *currentBtDeviceAddress, *currentBtDevicePath, *currentBtDeviceHci; + char *currentBtDeviceName = NULL, *currentBtDeviceAddress, *currentBtDevicePath, *currentBtDeviceHci; struct HalBtDeviceData *currentBtDeviceData; @@ -96,8 +96,7 @@ struct HalBtDeviceData *HalBtDataAddBtDeviceToBtDeviceList(struct HalBtDeviceDat } if(wrap_json_unpack(currentSingleBtDeviceDataJ, - "{s:s s:s s:s}", - "name", ¤tBtDeviceName, + "{s:s s:s}", "address", ¤tBtDeviceAddress, "adapter", ¤tBtDevicePath)) { HalBtDataRemoveSelectedBtDeviceFromList(firstBtDeviceData, currentBtDeviceData); @@ -118,9 +117,13 @@ struct HalBtDeviceData *HalBtDataAddBtDeviceToBtDeviceList(struct HalBtDeviceDat return NULL; } - if(! (currentBtDeviceData->name = strdup(currentBtDeviceName))) { - HalBtDataRemoveSelectedBtDeviceFromList(firstBtDeviceData, currentBtDeviceData); - return NULL; + wrap_json_unpack(currentSingleBtDeviceDataJ, "{s:s}", "name", ¤tBtDeviceName); + + if (currentBtDeviceName) { + if(! (currentBtDeviceData->name = strdup(currentBtDeviceName))) { + HalBtDataRemoveSelectedBtDeviceFromList(firstBtDeviceData, currentBtDeviceData); + return NULL; + } } if(! (currentBtDeviceData->address = strdup(currentBtDeviceAddress))) { @@ -171,11 +174,12 @@ struct HalBtDeviceData *HalBtDataSearchBtDeviceByAddress(struct HalBtDeviceData int HalBtDataHandleReceivedMediaBtDeviceData(struct HalBtPluginData *halBtPluginData, json_object *currentSingleBtMediaDataJ) { - const char *currentBtType = NULL, *currentBtAction = NULL; + const char *currentBtType = NULL, *currentBtAction = NULL, *currentBtDeviceRaw = NULL; + char *currentBtDeviceAddress = NULL, *tmp = NULL; AFB_ApiT api = halBtPluginData->currentHalApiHandle; - json_object *returnedJ, *returnedBtList = NULL; - int err = 0; + struct HalBtDeviceData *currentBtDevice = NULL; + int currentBtDeviceIsConnected; wrap_json_unpack(currentSingleBtMediaDataJ, "{s:s}", "type", ¤tBtType); @@ -184,35 +188,58 @@ int HalBtDataHandleReceivedMediaBtDeviceData(struct HalBtPluginData *halBtPlugin wrap_json_unpack(currentSingleBtMediaDataJ, "{s:s}", "action", ¤tBtAction); - if(currentBtAction && strcmp(currentBtAction, "added") && strcmp(currentBtAction, "removed")) + if(!currentBtAction) return 0; - // UGLY HACK: This should be actually detecting if a device is disconnected - // SPEC-2010 has been created to track the removal of this. - usleep(1000 * 100); + if(strcmp(currentBtAction, "added") && strcmp(currentBtAction, "removed")) + return 0; - if(AFB_ServiceSync(api, BT_MANAGER_API, BT_MANAGER_GET_DEVICES_VERB, NULL, &returnedJ)) { - AFB_ApiError(api, "Error during call to verb '%s' of '%s' api (%s)", - BT_MANAGER_GET_DEVICES_VERB, - BT_MANAGER_API, - json_object_get_string(returnedJ)); - return -1; - } - else if(wrap_json_unpack(returnedJ, "{s:{s:o}}", "response", "devices", &returnedBtList)) { - AFB_ApiError(api, - "Couldn't get bluetooth device list during call to verb '%s' of api '%s'", - BT_MANAGER_GET_DEVICES_VERB, - BT_MANAGER_API); - json_object_put(returnedJ); - return -1; + wrap_json_unpack(currentSingleBtMediaDataJ, "{s:s}", "device", ¤tBtDeviceRaw); + + if(!currentBtDeviceRaw || strlen(currentBtDeviceRaw) < 4) + return 0; + + currentBtDeviceAddress = strdup(¤tBtDeviceRaw[4]); + + // TODO: don't assume all bluetooth stacks produce device names that an + // address can be groked from. + tmp = currentBtDeviceAddress; + for (;*tmp; tmp++) + if (*tmp == '_') *tmp = ':'; + + currentBtDeviceIsConnected = !strcmp(currentBtAction, "added"); + currentBtDevice = HalBtDataSearchBtDeviceByAddress(&halBtPluginData->first, currentBtDeviceAddress); + + if(currentBtDevice && ! currentBtDeviceIsConnected) { + if(HalBtDataRemoveSelectedBtDeviceFromList(&halBtPluginData->first, currentBtDevice)) + return -1; + + AFB_ApiInfo(api, "Bluetooth device (address = %s) successfully removed from list", currentBtDeviceAddress); + + if(halBtPluginData->selectedBtDevice == currentBtDevice) { + halBtPluginData->selectedBtDevice = halBtPluginData->first; + AFB_ApiDebug(api, + "Bluetooth selected device changed to '%s'", + halBtPluginData->selectedBtDevice ? halBtPluginData->selectedBtDevice->address : "none"); + } } + else if(! currentBtDevice && currentBtDeviceIsConnected) { + json_object_object_add(currentSingleBtMediaDataJ, "address", json_object_new_string(currentBtDeviceAddress)); - if((returnedBtList) && (err = HalBtDataHandleReceivedMutlipleBtDeviceData(halBtPluginData, returnedBtList))) - err = 10 * err; + if(! HalBtDataAddBtDeviceToBtDeviceList(&halBtPluginData->first, currentSingleBtMediaDataJ)) + return -1; - json_object_put(returnedJ); + AFB_ApiInfo(api, "Bluetooth device (address = %s) successfully added to list", currentBtDeviceAddress); - return err; + if(! halBtPluginData->selectedBtDevice) { + halBtPluginData->selectedBtDevice = halBtPluginData->first; + AFB_ApiDebug(api, + "Bluetooth selected device changed to '%s'", + halBtPluginData->selectedBtDevice ? halBtPluginData->selectedBtDevice->address : "none"); + } + } + + return 0; } int HalBtDataHandleReceivedSingleBtDeviceData(struct HalBtPluginData *halBtPluginData, json_object *currentSingleBtDeviceDataJ) -- cgit 1.2.3-korg