From 3c2fbe2e3297804452ee84fa59c503c2864e530d Mon Sep 17 00:00:00 2001 From: Jonathan Aillet Date: Wed, 31 Oct 2018 17:28:09 +0100 Subject: hal-bt : Each plugin has its own data structure Each hal bluetooth plugin instance has its own data structure. It is made to have several hal working simultaneously with bluetooth. Change-Id: I2175bcbb327f590a6098b68c206d1cf518c8fb74 Signed-off-by: Jonathan Aillet --- plugins/lib/bluetooth/hal-bt.c | 162 ++++++++++++++++++++++++----------------- 1 file changed, 94 insertions(+), 68 deletions(-) diff --git a/plugins/lib/bluetooth/hal-bt.c b/plugins/lib/bluetooth/hal-bt.c index 7884dd6..f270593 100644 --- a/plugins/lib/bluetooth/hal-bt.c +++ b/plugins/lib/bluetooth/hal-bt.c @@ -30,14 +30,25 @@ #include "hal-bt-data.h" #include "hal-bt-mixer-link.h" -// Local (static) Hal manager data structure -static struct HalBtPluginData localHalBtPluginData; - CTLP_CAPI_REGISTER(HAL_BT_PLUGIN_NAME) // Call at initialisation time CTLP_ONLOAD(plugin, callbacks) { + struct HalBtPluginData *currentBtPluginData; + + AFB_ApiInfo(plugin->api, "%s Plugin Registering: uid='%s' 'info='%s'", HAL_BT_PLUGIN_NAME, plugin->uid, plugin->info); + + currentBtPluginData = calloc(1, sizeof(struct HalBtPluginData)); + if(! currentBtPluginData) { + AFB_ApiError(plugin->api, "An error happenned during allocation of %s plugin data structure ", HAL_BT_PLUGIN_NAME); + return -1; + } + + setPluginContext(plugin, (void *) currentBtPluginData); + + currentBtPluginData->currentHalApiHandle = plugin->api; + AFB_ApiNotice(plugin->api, "%s Plugin Registered correctly: uid='%s' 'info='%s'", HAL_BT_PLUGIN_NAME, plugin->uid, plugin->info); return 0; @@ -53,7 +64,7 @@ CTLP_INIT(plugin, callbacks) CtlConfigT *ctrlConfig; - struct SpecificHalData *currentHalData; + struct HalBtPluginData *currentBtPluginData; json_object *actionsToAdd, *returnedJ, *halMixerJ, *halOrigCaptureJ, *halNewCaptureJ, *halOrigStreamJ, *halNewStreamJ, *btCaptureJ, *btCaptureParamsJ, *btStreamJ; @@ -64,20 +75,30 @@ CTLP_INIT(plugin, callbacks) return 0; } + if(! (currentBtPluginData = (struct HalBtPluginData *) getPluginContext(plugin))) { + AFB_ApiError(plugin->api, "Can't get current %s plugin data", HAL_BT_PLUGIN_NAME); + return -1; + } + if(! (ctrlConfig = (CtlConfigT *) AFB_ApiGetUserData(plugin->api))) { AFB_ApiError(plugin->api, "Can't get current hal controller config"); - return -1; + return -2; } - if(! (currentHalData = getExternalData(ctrlConfig))) { + if(! (currentBtPluginData->currentHalData = getExternalData(ctrlConfig))) { AFB_ApiError(plugin->api, "Can't get current hal controller data"); - return -2; + return -3; + } + + if(currentBtPluginData->currentHalData->status != HAL_STATUS_AVAILABLE) { + AFB_ApiWarning(plugin->api, "Controller initialization of %s plugin cannot be done because hal is not ready to be used", HAL_BT_PLUGIN_NAME); + return 0; } - if((! currentHalData->ctlHalSpecificData) || - (! (halMixerJ = currentHalData->ctlHalSpecificData->halMixerJ))) { + if((! currentBtPluginData->currentHalData->ctlHalSpecificData) || + (! (halMixerJ = currentBtPluginData->currentHalData->ctlHalSpecificData->halMixerJ))) { AFB_ApiError(plugin->api, "Can't get current hal mixer json section"); - return -3; + return -4; } if(AFB_ServiceSync(plugin->api, BT_MANAGER_API, BT_MANAGER_GET_POWER_INFO, NULL, &returnedJ)) { @@ -95,31 +116,20 @@ CTLP_INIT(plugin, callbacks) BT_MANAGER_GET_POWER_INFO, BT_MANAGER_API, json_object_get_string(returnedJ)); - return -4; + return -5; } } if((! json_object_is_type(plugin->paramsJ, json_type_object)) || (wrap_json_unpack(plugin->paramsJ, "{s:i, s:s}", "channels", &btChannelsNumber, "zone", &btStreamZone))) { AFB_ApiError(plugin->api, "Can't get %s plugin parameters from json ('%s')", HAL_BT_PLUGIN_NAME, json_object_get_string(plugin->paramsJ)); - return -5; + return -6; } wrap_json_pack(&actionsToAdd, "{s:s s:s}", "uid", "Bluetooth-Manager/device_updated", "action", "plugin://hal-bt#events"); - if(currentHalData->status != HAL_STATUS_AVAILABLE) { - AFB_ApiWarning(plugin->api, "Controller initialization of %s plugin cannot be done because hal is not ready to be used", HAL_BT_PLUGIN_NAME); - return 0; - } - - memset(&localHalBtPluginData, '\0', sizeof(localHalBtPluginData)); - - localHalBtPluginData.currentHalApiHandle = plugin->api; - - localHalBtPluginData.currentHalData = currentHalData; - idx = 0; while(ctrlConfig->sections[idx].key && strcasecmp(ctrlConfig->sections[idx].key, "events")) idx++; @@ -127,13 +137,13 @@ CTLP_INIT(plugin, callbacks) if(! ctrlConfig->sections[idx].key) { AFB_ApiError(plugin->api, "Wasn't able to add '%s' as a new event, 'events' section not found", json_object_get_string(actionsToAdd)); json_object_put(actionsToAdd); - return -6; + return -7; } if(AddActionsToSection(plugin->api, &ctrlConfig->sections[idx], actionsToAdd, 0)) { AFB_ApiError(plugin->api, "Wasn't able to add '%s' as a new event to %s", json_object_get_string(actionsToAdd), ctrlConfig->sections[idx].key); json_object_put(actionsToAdd); - return -7; + return -8; } wrap_json_pack(&actionsToAdd, "{s:s s:s s:s}", @@ -148,13 +158,13 @@ CTLP_INIT(plugin, callbacks) if(! ctrlConfig->sections[idx].key) { AFB_ApiError(plugin->api, "Wasn't able to add '%s' as a new onload, 'onload' section not found", json_object_get_string(actionsToAdd)); json_object_put(actionsToAdd); - return -8; + return -9; } if(AddActionsToSection(plugin->api, &ctrlConfig->sections[idx], actionsToAdd, 0)) { AFB_ApiError(plugin->api, "Wasn't able to add '%s' as a new onload to %s", json_object_get_string(actionsToAdd), ctrlConfig->sections[idx].uid); json_object_put(actionsToAdd); - return -9; + return -10; } btCaptureJ = json_tokener_parse(MIXER_BT_CAPTURE_JSON_SECTION); @@ -180,7 +190,7 @@ CTLP_INIT(plugin, callbacks) } else { AFB_ApiError(plugin->api, "Unrecognized 'halmixer' captures format"); - return -10; + return -11; } btStreamJ = json_tokener_parse(MIXER_BT_STREAM_JSON_SECTION); @@ -205,10 +215,10 @@ CTLP_INIT(plugin, callbacks) } else { AFB_ApiError(plugin->api, "Unrecognized 'halmixer' streams format"); - return -11; + return -12; } - localHalBtPluginData.halBtPluginEnabled = 1; + currentBtPluginData->halBtPluginEnabled = 1; AFB_ApiNotice(plugin->api, "Plugin initialization of %s plugin correctly done", HAL_BT_PLUGIN_NAME); @@ -222,9 +232,16 @@ CTLP_CAPI(init, source, argsJ, queryJ) char *returnedInfo; + struct HalBtPluginData *currentBtPluginData; + json_object *toSendJ, *returnedJ, *returnedBtList = NULL; - if(! localHalBtPluginData.halBtPluginEnabled) { + if(! (currentBtPluginData = (struct HalBtPluginData *) getPluginContext(source->plugin))) { + AFB_ApiError(source->api, "Can't get current %s plugin data", HAL_BT_PLUGIN_NAME); + return -1; + } + + if(! currentBtPluginData->halBtPluginEnabled) { AFB_ApiWarning(source->api, "Controller onload initialization of %s plugin cannot be done because bluetooth is not reachable", HAL_BT_PLUGIN_NAME); return 0; } @@ -236,60 +253,60 @@ CTLP_CAPI(init, source, argsJ, queryJ) HAL_BT_GET_STREAMING_STATUS_VERB, "Get Bluetooth streaming status", HalBtGetStreamingStatus, - (void *) &localHalBtPluginData, + (void *) currentBtPluginData, NULL, 0, 0)) { AFB_ApiError(source->api, "Error while creating verb for bluetooth plugin : '%s'", HAL_BT_GET_STREAMING_STATUS_VERB); - return -1; + return -2; } if(AFB_ApiAddVerb(source->api, HAL_BT_SET_STREAMING_STATUS_VERB, "Set Bluetooth streaming status", HalBtSetStreamingStatus, - (void *) &localHalBtPluginData, + (void *) currentBtPluginData, NULL, 0, 0)) { AFB_ApiError(source->api, "Error while creating verb for bluetooth plugin : '%s'", HAL_BT_SET_STREAMING_STATUS_VERB); - return -2; + return -3; } if(AFB_ApiAddVerb(source->api, HAL_BT_GET_CONNECTED_A2DP_DEVICES_VERB, "Get connected Bluetooth A2DP devices list", HalBtGetA2DPBluetoothDevices, - (void *) &localHalBtPluginData, + (void *) currentBtPluginData, NULL, 0, 0)) { AFB_ApiError(source->api, "Error while creating verb for bluetooth plugin : '%s'", HAL_BT_GET_CONNECTED_A2DP_DEVICES_VERB); - return -3; + return -4; } if(AFB_ApiAddVerb(source->api, HAL_BT_GET_SELECTED_A2DP_DEVICE_VERB, "Get selected Bluetooth A2DP device", HalBtGetSelectedA2DPBluetoothDevice, - (void *) &localHalBtPluginData, + (void *) currentBtPluginData, NULL, 0, 0)) { AFB_ApiError(source->api, "Error while creating verb for bluetooth plugin : '%s'", HAL_BT_GET_SELECTED_A2DP_DEVICE_VERB); - return -4; + return -5; } if(AFB_ApiAddVerb(source->api, HAL_BT_SET_SELECTED_A2DP_DEVICE_VERB, "Set selected Bluetooth A2DP device", HalBtSetSelectedA2DPBluetoothDevice, - (void *) &localHalBtPluginData, + (void *) currentBtPluginData, NULL, 0, 0)) { AFB_ApiError(source->api, "Error while creating verb for bluetooth plugin : '%s'", HAL_BT_SET_SELECTED_A2DP_DEVICE_VERB); - return -5; + return -6; } // Register to Bluetooth manager @@ -301,7 +318,7 @@ CTLP_CAPI(init, source, argsJ, queryJ) BT_MANAGER_API, json_object_get_string(returnedJ)); - return -6; + return -7; } else if(! wrap_json_unpack(returnedJ, "{s:{s:s}}", "request", "info", &returnedInfo)) { AFB_ApiError(source->api, @@ -311,7 +328,7 @@ CTLP_CAPI(init, source, argsJ, queryJ) BT_MANAGER_API, returnedInfo); json_object_put(returnedJ); - return -6; + return -7; } json_object_put(returnedJ); @@ -330,7 +347,7 @@ CTLP_CAPI(init, source, argsJ, queryJ) BT_MANAGER_API, json_object_get_string(returnedJ)); - return -7; + return -8; } } else if(wrap_json_unpack(returnedJ, "{s:{s:o}}", "response", "list", &returnedBtList)) { @@ -339,29 +356,29 @@ CTLP_CAPI(init, source, argsJ, queryJ) BT_MANAGER_GET_DEVICES_VERB, BT_MANAGER_API); json_object_put(returnedJ); - return -7; + return -8; } - if((returnedBtList) && (err = HalBtDataHandleReceivedMutlipleBtDeviceData(&localHalBtPluginData, returnedBtList))) + if((returnedBtList) && (err = HalBtDataHandleReceivedMutlipleBtDeviceData(currentBtPluginData, returnedBtList))) return (10 * err); json_object_put(returnedJ); - if(localHalBtPluginData.selectedBtDevice) { - localHalBtPluginData.btStreamEnabled = 1; + if(currentBtPluginData->selectedBtDevice) { + currentBtPluginData->btStreamEnabled = 1; AFB_ApiInfo(source->api, "Useable bluetooth device detected at initialization, will try to use it"); if(HalBtMixerLinkSetBtStreamingSettings(source->api, - localHalBtPluginData.currentHalData->ctlHalSpecificData->mixerApiName, - localHalBtPluginData.btStreamEnabled, - localHalBtPluginData.selectedBtDevice->hci, - localHalBtPluginData.selectedBtDevice->address)) { + currentBtPluginData->currentHalData->ctlHalSpecificData->mixerApiName, + currentBtPluginData->btStreamEnabled, + currentBtPluginData->selectedBtDevice->hci, + currentBtPluginData->selectedBtDevice->address)) { AFB_ApiError(source->api, "Couldn't set bluetooth streaming settings during call to verb '%s' of api '%s'", MIXER_SET_STREAMED_BT_DEVICE_VERB, - localHalBtPluginData.currentHalData->ctlHalSpecificData->mixerApiName); - return -8; + currentBtPluginData->currentHalData->ctlHalSpecificData->mixerApiName); + return -9; } } @@ -373,38 +390,47 @@ CTLP_CAPI(init, source, argsJ, queryJ) // This receive Hal bluetooth plugin events CTLP_CAPI(events, source, argsJ, queryJ) { - struct HalBtDeviceData *previouslySelectedBtDevice = localHalBtPluginData.selectedBtDevice; + struct HalBtPluginData *currentBtPluginData; - if(! localHalBtPluginData.halBtPluginEnabled) { + struct HalBtDeviceData *previouslySelectedBtDevice; + + if(! (currentBtPluginData = (struct HalBtPluginData *) getPluginContext(source->plugin))) { + AFB_ApiError(source->api, "Can't get current %s plugin data", HAL_BT_PLUGIN_NAME); + return -1; + } + + if(! currentBtPluginData->halBtPluginEnabled) { AFB_ApiWarning(source->api, "Bluetooth event received but cannot be handled because bluetooth is not reachable"); return 0; } - if(HalBtDataHandleReceivedSingleBtDeviceData(&localHalBtPluginData, queryJ)) { + previouslySelectedBtDevice = currentBtPluginData->selectedBtDevice; + + if(HalBtDataHandleReceivedSingleBtDeviceData(currentBtPluginData, queryJ)) { AFB_ApiError(source->api, "Error while decoding bluetooth event received json (%s)", json_object_get_string(queryJ)); - return -1; + return -2; } - if(localHalBtPluginData.selectedBtDevice == previouslySelectedBtDevice) { + if(currentBtPluginData->selectedBtDevice == previouslySelectedBtDevice) { AFB_ApiDebug(source->api, "Bluetooth event received but device didn't change"); return 0; } - if(localHalBtPluginData.selectedBtDevice) - localHalBtPluginData.btStreamEnabled = 1; + if(currentBtPluginData->selectedBtDevice) + currentBtPluginData->btStreamEnabled = 1; else - localHalBtPluginData.btStreamEnabled = 0; + currentBtPluginData->btStreamEnabled = 0; if(HalBtMixerLinkSetBtStreamingSettings(source->api, - localHalBtPluginData.currentHalData->ctlHalSpecificData->mixerApiName, - localHalBtPluginData.btStreamEnabled, - localHalBtPluginData.selectedBtDevice ? localHalBtPluginData.selectedBtDevice->hci : NULL, - localHalBtPluginData.selectedBtDevice ? localHalBtPluginData.selectedBtDevice->address : NULL)) { + currentBtPluginData->currentHalData->ctlHalSpecificData->mixerApiName, + currentBtPluginData->btStreamEnabled, + currentBtPluginData->selectedBtDevice ? currentBtPluginData->selectedBtDevice->hci : NULL, + currentBtPluginData->selectedBtDevice ? currentBtPluginData->selectedBtDevice->address : NULL)) { AFB_ApiError(source->api, "Couldn't set bluetooth streaming settings during call to verb '%s' of api '%s'", MIXER_SET_STREAMED_BT_DEVICE_VERB, - localHalBtPluginData.currentHalData->ctlHalSpecificData->mixerApiName); - return -2; + currentBtPluginData->currentHalData->ctlHalSpecificData->mixerApiName); + return -3; } return 0; -- cgit 1.2.3-korg