summaryrefslogtreecommitdiffstats
path: root/plugins/lib/bluetooth/hal-bt-data.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/lib/bluetooth/hal-bt-data.c')
-rw-r--r--plugins/lib/bluetooth/hal-bt-data.c89
1 files 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", &currentBtDeviceName,
+ "{s:s s:s}",
"address", &currentBtDeviceAddress,
"adapter", &currentBtDevicePath)) {
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", &currentBtDeviceName);
+
+ 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", &currentBtType);
@@ -184,35 +188,58 @@ int HalBtDataHandleReceivedMediaBtDeviceData(struct HalBtPluginData *halBtPlugin
wrap_json_unpack(currentSingleBtMediaDataJ, "{s:s}", "action", &currentBtAction);
- 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", &currentBtDeviceRaw);
+
+ if(!currentBtDeviceRaw || strlen(currentBtDeviceRaw) < 4)
+ return 0;
+
+ currentBtDeviceAddress = strdup(&currentBtDeviceRaw[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)