/* * Copyright (C) 2018 "IoT.bzh" * Author Jonathan Aillet * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define _GNU_SOURCE #include #include #include #include #include "4a-hal-utilities-data.h" #include "4a-internals-hal-cb.h" #include "4a-internals-hal-alsacore-link.h" #include "4a-internals-hal-mixer-link.h" #include "4a-internals-hal-value-handler.h" /******************************************************************************* * Internals HAL Event handler function * ******************************************************************************/ void InternalHalDispatchApiEvent(afb_api_t apiHandle, const char *evtLabel, json_object *eventJ) { int numid, idx = 0, cardidx; CtlConfigT *ctrlConfig; CtlSourceT source; struct HalData *currentHalData; struct InternalHalAlsaMapT *currentHalAlsaCtlsT; json_object *valuesJ, *normalizedValuesJ; AFB_API_DEBUG(apiHandle, "Evtname=%s [msg=%s]", evtLabel, json_object_get_string(eventJ)); ctrlConfig = (CtlConfigT *) afb_api_get_userdata(apiHandle); if(! ctrlConfig) { AFB_API_ERROR(apiHandle, "Can't get current internal hal controller config"); return; } currentHalData = (struct HalData *) getExternalData(ctrlConfig); if(! currentHalData) { AFB_API_WARNING(apiHandle, "Can't get current internal hal controller data"); return; } // Extract sound card index from event while(evtLabel[idx] != '\0' && evtLabel[idx] != ':') idx++; if(evtLabel[idx] != '\0' && sscanf(&evtLabel[idx + 1], "%d", &cardidx) == 1 && currentHalData->sndCardId == cardidx) { if(wrap_json_unpack(eventJ, "{s:i s:o !}", "id", &numid, "val", &valuesJ)) { AFB_API_ERROR(apiHandle, "Invalid Alsa Event label=%s value=%s", evtLabel, json_object_get_string(eventJ)); return; } currentHalAlsaCtlsT = currentHalData->internalHalData->alsaMapT; // Search for corresponding numid in ALSA controls list, if found, launch callback (if available) for(idx = 0; idx < currentHalAlsaCtlsT->ctlsCount; idx++) { if(currentHalAlsaCtlsT->ctls[idx].ctl.numid == numid) { if(currentHalAlsaCtlsT->ctls[idx].action) { source.uid = currentHalAlsaCtlsT->ctls[idx].action->uid; source.api = currentHalAlsaCtlsT->ctls[idx].action->api; source.request = NULL; (void) ActionExecOne(&source, currentHalAlsaCtlsT->ctls[idx].action, valuesJ); } else { AFB_API_NOTICE(apiHandle, "The alsa control id '%i' is corresponding to a known control but without any action registered", numid); } if(! currentHalAlsaCtlsT->ctls[idx].alsaControlEvent || InternalHalConvertJsonValues(apiHandle, ¤tHalAlsaCtlsT->ctls[idx].ctl.alsaCtlProperties, valuesJ, &normalizedValuesJ, CONVERSION_ALSACORE_TO_NORMALIZED) || (afb_event_push(currentHalAlsaCtlsT->ctls[idx].alsaControlEvent, normalizedValuesJ) < 0)) { AFB_API_ERROR(apiHandle, "Couldn't generate an event for known halmap %s (alsa control id %i)", currentHalAlsaCtlsT->ctls[idx].uid, currentHalAlsaCtlsT->ctls[idx].ctl.numid); } return; } } AFB_API_WARNING(apiHandle, "Alsacore event with an unrecognized numid: %i, evtname=%s [msg=%s]", numid, evtLabel, json_object_get_string(eventJ)); return; } AFB_API_INFO(apiHandle, "Not an alsacore event '%s' [msg=%s]", evtLabel, json_object_get_string(eventJ)); CtrlDispatchApiEvent(apiHandle, evtLabel, eventJ); } /******************************************************************************* * Internals HAL - 'halmixer' section parsing/handling functions * ******************************************************************************/ int InternalHalHalMixerConfig(afb_api_t apiHandle, CtlSectionT *section, json_object *MixerJ) { int err = 0; CtlConfigT *ctrlConfig; struct HalData *currentHalData; if(! apiHandle || ! section) return -1; ctrlConfig = (CtlConfigT *) afb_api_get_userdata(apiHandle); if(! ctrlConfig) return -2; currentHalData = (struct HalData *) getExternalData(ctrlConfig); if(! currentHalData) return -3; if(MixerJ) { if(json_object_is_type(MixerJ, json_type_object)) currentHalData->internalHalData->halMixerJ = MixerJ; else return -4; if(wrap_json_unpack(MixerJ, "{s:s}", "mixerapi", ¤tHalData->internalHalData->mixerApiName)) return -5; wrap_json_unpack(MixerJ, "{s?:s}", "prefix", ¤tHalData->internalHalData->prefix); } else if(currentHalData->status == HAL_STATUS_AVAILABLE) { err = InternalHalAttachToMixer(apiHandle); if(err) { AFB_API_ERROR(apiHandle, "Error %i while attaching to mixer", err); return -6; } } return 0; } /******************************************************************************* * Internals HAL - 'halmap' section parsing/handling functions * ******************************************************************************/ int InternalHalProcessOneHalMapObject(afb_api_t apiHandle, struct InternalHalAlsaMap *alsaMap, json_object *AlsaMapJ) { char *action = NULL, *typename = NULL; json_object *alsaJ = NULL, *createAlsaCtlJ = NULL; AFB_API_DEBUG(apiHandle, "AlsaMapJ=%s", json_object_get_string(AlsaMapJ)); if(wrap_json_unpack(AlsaMapJ, "{s:s s?:s s:o s?:s !}", "uid", &alsaMap->uid, "info", &alsaMap->info, "alsa", &alsaJ, "action", &action)) { AFB_API_ERROR(apiHandle, "Parsing error, map should only contains [label]|[uid]|[tag]|[info]|[alsa]|[action] in : '%s'", json_object_get_string(AlsaMapJ)); return -1; } if(wrap_json_unpack(alsaJ, "{s?:s s?:i s?:i s?:o !}", "name", &alsaMap->ctl.name, "numid", &alsaMap->ctl.numid, "value", &alsaMap->ctl.value, "create", &createAlsaCtlJ)) { AFB_API_ERROR(apiHandle, "Parsing error, alsa json should only contains [name]|[numid]||[value]|[create] in : '%s'", json_object_get_string(alsaJ)); return -2; } if(createAlsaCtlJ) { alsaMap->ctl.alsaCtlCreation = &alsaMap->ctl.alsaCtlProperties; if(wrap_json_unpack(createAlsaCtlJ, "{s:s s:i s:i s:i s:i !}", "type", &typename, "count", &alsaMap->ctl.alsaCtlCreation->count, "minval", &alsaMap->ctl.alsaCtlCreation->minval, "maxval", &alsaMap->ctl.alsaCtlCreation->maxval, "step", &alsaMap->ctl.alsaCtlCreation->step)) { AFB_API_ERROR(apiHandle, "Parsing error, alsa creation json should only contains [type]|[count]|[minval]|[maxval]|[step] in : '%s'", json_object_get_string(alsaJ)); return -3; } if(typename) { alsaMap->ctl.alsaCtlCreation->type = InternalHalMapsAlsaTypeToEnum(typename); if(alsaMap->ctl.alsaCtlCreation->type == SND_CTL_ELEM_TYPE_NONE) { AFB_API_ERROR(apiHandle, "Couldn't get alsa type from string %s in : '%s'", typename, json_object_get_string(alsaJ)); return -4; } } if(! alsaMap->ctl.name) alsaMap->ctl.name = (char *) alsaMap->uid; } else if(alsaMap->ctl.name && alsaMap->ctl.numid > 0) { AFB_API_ERROR(apiHandle, "Can't have both a control name (%s) and a control uid (%i) in alsa object : '%s'", alsaMap->ctl.name, alsaMap->ctl.numid, json_object_get_string(alsaJ)); return -5; } else if(! alsaMap->ctl.name && alsaMap->ctl.numid <= 0) { AFB_API_ERROR(apiHandle, "Need at least a control name or a control uid in alsa object : '%s'", json_object_get_string(alsaJ)); return -6; } if(action) alsaMap->actionJ = AlsaMapJ; return 0; } int InternalHalHandleOneHalMapObject(afb_api_t apiHandle, char *cardId, struct InternalHalAlsaMap *alsaMap) { int err; json_object *valueJ, *convertedValueJ = NULL; alsaMap->alsaControlEvent = afb_api_make_event(apiHandle, alsaMap->uid); if(! alsaMap->alsaControlEvent) { AFB_API_ERROR(apiHandle, "Didn't succeed to create event for current alsa control to load action using alsa object : '%s'", json_object_get_string(alsaMap->actionJ)); return -1; } if(alsaMap->ctl.alsaCtlCreation) { if(InternalHalCreateAlsaCtl(apiHandle, cardId, &alsaMap->ctl)) { AFB_API_ERROR(apiHandle, "An error happened when trying to create a new alsa control"); return -2; } } else if(InternalHalUpdateAlsaCtlProperties(apiHandle, cardId, &alsaMap->ctl)) { AFB_API_ERROR(apiHandle, "An error happened when trying to get existing alsa control info"); return -3; } if(alsaMap->ctl.value) { // TBD JAI : handle alsa controls type valueJ = json_object_new_int(alsaMap->ctl.value); if(! valueJ) { AFB_API_ERROR(apiHandle, "Didn't succeed to allocate ALSA control value json string"); return -4; } err = 0; if(InternalHalConvertJsonValues(apiHandle, &alsaMap->ctl.alsaCtlProperties, valueJ, &convertedValueJ, CONVERSION_NORMALIZED_TO_ALSACORE)) { AFB_API_ERROR(apiHandle, "Error when trying to convert initiate value json '%s'", json_object_get_string(valueJ)); err = -5; } else if(InternalHalSetAlsaCtlValue(apiHandle, cardId, alsaMap->ctl.numid, convertedValueJ)) { AFB_API_ERROR(apiHandle, "Error while trying to set initial value on alsa control %i, device '%s', value '%s'", alsaMap->ctl.numid, cardId, json_object_get_string(valueJ)); err = -6; } json_object_put(valueJ); if(convertedValueJ) json_object_put(convertedValueJ); if(err) return err; } if(alsaMap->actionJ) { alsaMap->action = calloc(1, sizeof(CtlActionT)); if(! alsaMap->action) { AFB_API_ERROR(apiHandle, "Didn't succeed to allocate structure used to store action to perform" "when the corresponding ALSA control is detected"); return -7; } if(ActionLoadOne(apiHandle, alsaMap->action, alsaMap->actionJ, 0)) { AFB_API_ERROR(apiHandle, "Didn't succeed to load action using alsa object : '%s'", json_object_get_string(alsaMap->actionJ)); return -8; } } if(afb_api_add_verb(apiHandle, alsaMap->uid, alsaMap->info, InternalHalActionOnAlsaCtl, (void *) alsaMap, NULL, 0, 0)) { AFB_API_ERROR(apiHandle, "Didn't succeed to create verb for current alsa control to load action using alsa object : '%s'", json_object_get_string(alsaMap->actionJ)); return -9; } return 0; } int InternalHalProcessAllHalMap(afb_api_t apiHandle, json_object *AlsaMapJ, struct InternalHalAlsaMapT *currentInternalHalAlsaMapT) { int idx, err = 0; struct InternalHalAlsaMap *ctlMaps; json_object *currentAlsaMapJ; json_type alsaMapType; alsaMapType = json_object_get_type(AlsaMapJ); switch(alsaMapType) { case json_type_array: currentInternalHalAlsaMapT->ctlsCount = (unsigned int) json_object_array_length(AlsaMapJ); break; case json_type_object: currentInternalHalAlsaMapT->ctlsCount = 1; break; default: currentInternalHalAlsaMapT->ctlsCount = 0; currentInternalHalAlsaMapT->ctls = NULL; AFB_API_ERROR(apiHandle, "Content of 'halmap' section is not valid ('%s')", json_object_get_string(AlsaMapJ)); return -1; } ctlMaps = calloc(currentInternalHalAlsaMapT->ctlsCount, sizeof(struct InternalHalAlsaMap)); if(! ctlMaps) { AFB_API_ERROR(apiHandle, "Didn't succeed to allocate halmap data table"); return -2; } for(idx = 0; idx < currentInternalHalAlsaMapT->ctlsCount; idx++) { currentAlsaMapJ = (alsaMapType == json_type_array) ? json_object_array_get_idx(AlsaMapJ, idx) : AlsaMapJ; err = InternalHalProcessOneHalMapObject(apiHandle, &ctlMaps[idx], currentAlsaMapJ); if(err) { AFB_API_ERROR(apiHandle, "Didn't succeed to proccess halmap object %i ('%s')", idx, json_object_get_string(currentAlsaMapJ)); HalUtlFreeAlsaCtlsMap(currentInternalHalAlsaMapT); return -3; } } currentInternalHalAlsaMapT->ctls = ctlMaps; return err; } int InternalHalHandleAllHalMap(afb_api_t apiHandle, int sndCardId, struct InternalHalAlsaMapT *currentInternalHalAlsaMapT) { int idx, err = 0; char cardIdString[6]; snprintf(cardIdString, 6, "hw:%i", sndCardId); InternalHalSubscribeToAlsaCardEvent(apiHandle, cardIdString); for(idx = 0; idx < currentInternalHalAlsaMapT->ctlsCount; idx++) { err = InternalHalHandleOneHalMapObject(apiHandle, cardIdString, ¤tInternalHalAlsaMapT->ctls[idx]); if(err) { AFB_API_ERROR(apiHandle, "Didn't succeed to handle halmap object %i ('%s')", idx, json_object_get_string(currentInternalHalAlsaMapT->ctls[idx].actionJ)); return -3; } } return err; } int InternalHalHalMapConfig(afb_api_t apiHandle, CtlSectionT *section, json_object *AlsaMapJ) { CtlConfigT *ctrlConfig; struct HalData *currentHalData; ctrlConfig = (CtlConfigT *) afb_api_get_userdata(apiHandle); if(! ctrlConfig) return -1; currentHalData = (struct HalData *) getExternalData(ctrlConfig); if(! currentHalData) return -2; if(AlsaMapJ) { currentHalData->internalHalData->alsaMapT = calloc(1, sizeof(struct InternalHalAlsaMapT)); if(! currentHalData->internalHalData->alsaMapT) { AFB_API_ERROR(apiHandle, "Didn't succeed to allocate data structure used to store halmap data table"); return -3; } if(InternalHalProcessAllHalMap(apiHandle, AlsaMapJ, currentHalData->internalHalData->alsaMapT)) { AFB_API_ERROR(apiHandle, "Failed to process 'halmap' section"); HalUtlFreeAlsaCtlsMap(currentHalData->internalHalData->alsaMapT); return -4; } } else if(currentHalData->status == HAL_STATUS_UNAVAILABLE) { AFB_API_WARNING(apiHandle, "Hal is unavailable, 'halmap' section data can't be handle"); return 1; } else if(currentHalData->sndCardId < 0) { AFB_API_ERROR(apiHandle, "Hal alsa card id is not valid, 'halmap' section data can't be handle"); return -5; } else if(! currentHalData->internalHalData->alsaMapT) { AFB_API_WARNING(apiHandle, "'halmap' section data is empty"); return 2; } else if(! (currentHalData->internalHalData->alsaMapT->ctlsCount > 0)) { AFB_API_WARNING(apiHandle, "No alsa controls defined in 'halmap' section"); return 3; } else if(InternalHalHandleAllHalMap(apiHandle, currentHalData->sndCardId, currentHalData->internalHalData->alsaMapT)) { AFB_API_ERROR(apiHandle, "Failed to handle 'halmap' section"); return -6; } return 0; } /******************************************************************************* * Internals HAL - 'haldependencies' section parsing/handling functions * ******************************************************************************/ json_type InternalHalDependencyGetExpectedContentTypeFromKey(char *key) { if(! key) return json_type_null; if(! strcmp(key, "cardNb") || ! strcmp(key, "playbackDeviceNb")) return json_type_int; if(! strcmp(key, "cardPath") || ! strcmp(key, "cardId") || ! strcmp(key, "cardShortName") || ! strcmp(key, "cardLongName") || ! strcmp(key, "cardDriver") || ! strcmp(key, "cardMixerName") || ! strcmp(key, "cardComponents") || ! strcmp(key, "playbackDeviceId") || ! strcmp(key, "playbackDeviceName")) return json_type_string; if(! strcmp(key, "AnyOf")) return json_type_object; return json_type_null; } int InternalHalDependencyIsObjectToAddValid(json_object *objectToAddJ) { struct json_object_iterator currentPropertyToAdd, lastPropertyToAdd; json_type currentPropertyValueExpectedType; if(! objectToAddJ) return -1; currentPropertyToAdd = json_object_iter_begin(objectToAddJ); lastPropertyToAdd = json_object_iter_end(objectToAddJ); while(! json_object_iter_equal(¤tPropertyToAdd, &lastPropertyToAdd)) { currentPropertyValueExpectedType = InternalHalDependencyGetExpectedContentTypeFromKey((char *) json_object_iter_peek_name(¤tPropertyToAdd)); if(! json_object_is_type(json_object_iter_peek_value(¤tPropertyToAdd), currentPropertyValueExpectedType)) return -2; json_object_iter_next(¤tPropertyToAdd); } return 0; } int InternalHalProcessOneDependencyProperty(afb_api_t apiHandle, char *key, json_object *valueJ, json_object **deviceToProbeRequestGenerationJ) { int valueIdx, previouslyGeneratedRequestIdx, valueCount, previouslyGeneratedRequestCount; json_object *previouslyGeneratedRequestJ, *updatedGeneratedRequestJ, *currentValueJ, *objectToBeefUpJ; json_type expectedContentType, contentType; if(! key || ! valueJ || ! deviceToProbeRequestGenerationJ) { AFB_API_ERROR(apiHandle, "Invalid argument(s)"); return -1; } previouslyGeneratedRequestJ = *deviceToProbeRequestGenerationJ; if(! previouslyGeneratedRequestJ) { previouslyGeneratedRequestCount = 0; } else if(! json_object_is_type(previouslyGeneratedRequestJ, json_type_array)) { AFB_API_ERROR(apiHandle, "Invalid previously generated request (not an array : '%s')", json_object_get_string(previouslyGeneratedRequestJ)); return -2; } else { previouslyGeneratedRequestCount = (int) json_object_array_length(previouslyGeneratedRequestJ); } expectedContentType = InternalHalDependencyGetExpectedContentTypeFromKey(key); if(expectedContentType == json_type_null) { AFB_API_ERROR(apiHandle, "Unrecognized key '%s'", key); return -3; } contentType = json_object_get_type(valueJ); switch(contentType) { case json_type_int: case json_type_string: valueCount = 1; break; case json_type_array: valueCount = (int) json_object_array_length(valueJ); break; default: AFB_API_ERROR(apiHandle, "Value doesn't have the expected type (value: '%s')", json_object_get_string(valueJ)); return -4; } updatedGeneratedRequestJ = json_object_new_array(); if(! updatedGeneratedRequestJ) { AFB_API_ERROR(apiHandle, "Didn't succeed to allocate updated request json array"); return -5; } for(valueIdx = 0; valueIdx < valueCount; valueIdx++) { if(contentType == json_type_array) currentValueJ = json_object_array_get_idx(valueJ, valueIdx); else currentValueJ = valueJ; if(! json_object_is_type(currentValueJ, expectedContentType)) { AFB_API_ERROR(apiHandle, "Current value '%s' (index %i in value array) does not have the expected type, " "won't be able to process property (key '%s')", json_object_get_string(currentValueJ), valueIdx, key); json_object_put(updatedGeneratedRequestJ); return -6; } previouslyGeneratedRequestIdx = 0; do { if(! previouslyGeneratedRequestCount) objectToBeefUpJ = json_object_new_object(); else objectToBeefUpJ = wrap_json_clone(json_object_array_get_idx(previouslyGeneratedRequestJ, previouslyGeneratedRequestIdx)); if(! objectToBeefUpJ) { AFB_API_ERROR(apiHandle, "Didn't succeed to allocate/clone object to beef up json object"); json_object_put(objectToBeefUpJ); json_object_put(updatedGeneratedRequestJ); return -7; } if(! json_object_is_type(currentValueJ, json_type_object)) { json_object_object_add(objectToBeefUpJ, key, wrap_json_clone(currentValueJ)); } else if(! InternalHalDependencyIsObjectToAddValid(currentValueJ)) { wrap_json_object_add(objectToBeefUpJ, wrap_json_clone(currentValueJ)); } else { AFB_API_ERROR(apiHandle, "Object '%s' is not valid", json_object_get_string(currentValueJ)); json_object_put(objectToBeefUpJ); json_object_put(updatedGeneratedRequestJ); return -8; } json_object_array_add(updatedGeneratedRequestJ, objectToBeefUpJ); previouslyGeneratedRequestIdx++; } while(previouslyGeneratedRequestIdx < previouslyGeneratedRequestCount); } *deviceToProbeRequestGenerationJ = updatedGeneratedRequestJ; json_object_put(previouslyGeneratedRequestJ); return 0; } int InternalHalProcessOneHalDependency(afb_api_t apiHandle, json_object *DependencyJ, struct InternalHalProbedDevice *halDeviceToProbe) { int ret; json_object *dependencyUidJ, *dependencyClassJ; struct json_object_iterator dependencyCurrentProperty, dependencyLastProperty; if(! apiHandle || ! DependencyJ || ! halDeviceToProbe) { AFB_API_ERROR(apiHandle, "Invalid argument(s)"); return -1; } if(! json_object_object_get_ex(DependencyJ, "uid", &dependencyUidJ)) { AFB_API_ERROR(apiHandle, "No dependency 'uid' has been specified, won't be able to process it ('%s')", json_object_get_string(DependencyJ)); return -2; } else if(! json_object_is_type(dependencyUidJ, json_type_string)) { AFB_API_ERROR(apiHandle, "Specified dependency 'uid' is not a string ('%s')", json_object_get_string(dependencyUidJ)); return -3; } halDeviceToProbe->uid = strdup(json_object_get_string(dependencyUidJ)); if(! halDeviceToProbe->uid) { AFB_API_ERROR(apiHandle, "Didn't succeed to store (allocate) dependency 'uid' string"); return -4; } if(! json_object_object_get_ex(DependencyJ, "class", &dependencyClassJ)) { AFB_API_NOTICE(apiHandle, "No class has been specified for dependency with uid:'%s', will be considered as static", halDeviceToProbe->uid); halDeviceToProbe->deviceClass = STATIC_PROBED_DEVICE; } else if(! json_object_is_type(dependencyClassJ, json_type_string)) { AFB_API_ERROR(apiHandle, "Specified dependency class is not a string ('%s')", json_object_get_string(dependencyClassJ)); return -5; } else { halDeviceToProbe->deviceClass = HalUtlGetProbedDeviceClassFromString((char *) json_object_get_string(dependencyClassJ)); if(halDeviceToProbe->deviceClass == INVALID_PROBED_DEVICE) { AFB_API_ERROR(apiHandle, "Specified device class is unknown ('%s')", json_object_get_string(dependencyClassJ)); return -6; } } dependencyCurrentProperty = json_object_iter_begin(DependencyJ); dependencyLastProperty = json_object_iter_end(DependencyJ); while(! json_object_iter_equal(&dependencyCurrentProperty, &dependencyLastProperty)) { if(! strcmp("uid", json_object_iter_peek_name(&dependencyCurrentProperty)) || ! strcmp("class", json_object_iter_peek_name(&dependencyCurrentProperty))) { json_object_iter_next(&dependencyCurrentProperty); continue; } ret = InternalHalProcessOneDependencyProperty(apiHandle, (char *) json_object_iter_peek_name(&dependencyCurrentProperty), json_object_iter_peek_value(&dependencyCurrentProperty), &halDeviceToProbe->requestedDeviceJ); if(ret) { AFB_API_ERROR(apiHandle, "Error %i while processing property with key '%s' and value '%s' in dependency '%s'", ret, json_object_iter_peek_name(&dependencyCurrentProperty), json_object_get_string(json_object_iter_peek_value(&dependencyCurrentProperty)), json_object_get_string(DependencyJ)); return -7; } json_object_iter_next(&dependencyCurrentProperty); } return 0; } int InternalHalProcessAllHalDependencies(afb_api_t apiHandle, json_object *DependenciesJ, struct InternalHalProbedDevice **halDevicesToProbeList) { int idx, dependenciesCount, ret; struct InternalHalProbedDevice *currentDeviceToProbe; json_type dependenciesJType; json_object *currentDependencyJ; if(! apiHandle || ! DependenciesJ || ! halDevicesToProbeList) { AFB_API_ERROR(apiHandle, "Invalid argument(s)"); return -1; } dependenciesJType = json_object_get_type(DependenciesJ); switch(dependenciesJType) { case json_type_object: dependenciesCount = 1; break; case json_type_array: dependenciesCount = (int) json_object_array_length(DependenciesJ); break; default: AFB_API_ERROR(apiHandle, "'haldependencies' section is malformed, won't be able to handle it ('haldependencies' : '%s')", json_object_get_string(DependenciesJ)); return -2; } for(idx = 0; idx < dependenciesCount; idx++) { if(dependenciesJType == json_type_array) currentDependencyJ = json_object_array_get_idx(DependenciesJ, idx); else currentDependencyJ = DependenciesJ; if(! json_object_is_type(currentDependencyJ, json_type_object)) { AFB_API_ERROR(apiHandle, "Dependency ('%s') is not an object, section is malformed, won't be able to handle it ('haldependencies' : '%s')", json_object_get_string(currentDependencyJ), json_object_get_string(DependenciesJ)); HalUtlRemoveAllProbedDevicesFromList(halDevicesToProbeList); return -3; } currentDeviceToProbe = HalUtlAddProbedDeviceToProbedDeviceList(halDevicesToProbeList); if(! currentDeviceToProbe) { AFB_API_ERROR(apiHandle, "Error when tried to add a device to probe into device to probe list"); HalUtlRemoveAllProbedDevicesFromList(halDevicesToProbeList); return -4; } ret = InternalHalProcessOneHalDependency(apiHandle, currentDependencyJ, currentDeviceToProbe); if(ret) { AFB_API_ERROR(apiHandle, "Error %i returned when tried to process '%s' dependency", ret, json_object_get_string(currentDependencyJ)); HalUtlRemoveAllProbedDevicesFromList(halDevicesToProbeList); return -5; } } return 0; } int InternalHalHandleInfoGetResponse(afb_api_t apiHandle, json_object *currentResponseJ, struct InternalHalDeviceData **halDeviceDataToFill) { int idx, arraySize, ret = 0; struct InternalHalDeviceData *currentProbedDeviceData, *previouslyObtainedDevice; if(! apiHandle || ! currentResponseJ || ! halDeviceDataToFill) { AFB_API_ERROR(apiHandle, "Invalid argument(s)"); return -1; } switch(json_object_get_type(currentResponseJ)) { case json_type_object: currentProbedDeviceData = HalUtlAllocateAndFillProbedDeviceDataUsingInfoGetResponse(currentResponseJ); if(! currentProbedDeviceData) { AFB_API_ERROR(apiHandle, "Unable to decode info get response object"); return -2; } if(! *halDeviceDataToFill) { *halDeviceDataToFill = currentProbedDeviceData; break; } previouslyObtainedDevice = *halDeviceDataToFill; if(previouslyObtainedDevice->cardNb != currentProbedDeviceData->cardNb || previouslyObtainedDevice->playbackDeviceNb != currentProbedDeviceData->playbackDeviceNb) { AFB_API_WARNING(apiHandle, "Different audio devices was returned by info get call"); free(previouslyObtainedDevice); ret = 1; } free(currentProbedDeviceData); break; case json_type_array: arraySize = (int) json_object_array_length(currentResponseJ); for(idx = 0; idx < arraySize; idx++) { ret = InternalHalHandleInfoGetResponse(apiHandle, json_object_array_get_idx(currentResponseJ, idx), halDeviceDataToFill); if(ret < 0) { AFB_API_ERROR(apiHandle, "Error %i while handling object %i of array ('%s') when handling info get response", ret, idx, json_object_get_string(currentResponseJ)); ret = -3; } if(ret) break; } break; case json_type_string: if(strcmp(json_object_get_string(currentResponseJ), "sndcard-not-found")) { AFB_API_ERROR(apiHandle, "Unrecognized string '%s' sent back by info get call", json_object_get_string(currentResponseJ)); return -4; } break; default: AFB_API_ERROR(apiHandle, "Unrecognized info get response format"); return -5; } return ret; } int InternalHalHandleOneHalDependencies(afb_api_t apiHandle, struct InternalHalProbedDevice **halDeviceToProbe) { int ret; struct InternalHalProbedDevice *currentDeviceToProbe; json_object *probedDeviceGetInfoResponseJ; if(! apiHandle || ! halDeviceToProbe || ! *halDeviceToProbe) { AFB_API_ERROR(apiHandle, "Invalid argument(s)"); return -1; } currentDeviceToProbe = *halDeviceToProbe; if(! currentDeviceToProbe->uid || ! currentDeviceToProbe->requestedDeviceJ) { AFB_API_ERROR(apiHandle, "Specified device to probe is not valid (uid or request is not specified)"); return -2; } if(! json_object_is_type(currentDeviceToProbe->requestedDeviceJ, json_type_array)) { AFB_API_ERROR(apiHandle, "Device to probe request is not an array"); return -3; } ret = InternalHalGetCardInfo(apiHandle, json_object_get(currentDeviceToProbe->requestedDeviceJ), &probedDeviceGetInfoResponseJ); if(ret) { AFB_API_ERROR(apiHandle, "Error %i happened when tried to get card information for dependency with uid '%s' (using request : '%s')", ret, currentDeviceToProbe->uid, json_object_get_string(currentDeviceToProbe->requestedDeviceJ)); return -4; } ret = InternalHalHandleInfoGetResponse(apiHandle, probedDeviceGetInfoResponseJ, ¤tDeviceToProbe->deviceData); if(ret < 0) { AFB_API_ERROR(apiHandle, "Error %i happened when tried to handle card get info response for dependency with uid '%s' (using response : '%s')", ret, currentDeviceToProbe->uid, json_object_get_string(probedDeviceGetInfoResponseJ)); return -5; } if(ret > 0) { AFB_API_WARNING(apiHandle, "Multiple card was found for dependency with uid '%s'(request : '%s', response : '%s')", currentDeviceToProbe->uid, json_object_get_string(currentDeviceToProbe->requestedDeviceJ), json_object_get_string(probedDeviceGetInfoResponseJ)); return 1; } else if(! currentDeviceToProbe->deviceData) { AFB_API_INFO(apiHandle, "No card was found for dependency with uid '%s'(request : '%s', response : '%s')", currentDeviceToProbe->uid, json_object_get_string(currentDeviceToProbe->requestedDeviceJ), json_object_get_string(probedDeviceGetInfoResponseJ)); return 2; } return 0; } int InternalHalHandleAllHalDependencies(afb_api_t apiHandle, struct InternalHalProbedDevice **halDevicesToProbeList) { int ret; struct InternalHalProbedDevice *halCurrentDeviceToProbe; if(! apiHandle || ! halDevicesToProbeList) { AFB_API_ERROR(apiHandle, "Invalid argument(s)"); return -1; } halCurrentDeviceToProbe = *halDevicesToProbeList; if(! halCurrentDeviceToProbe) { AFB_API_WARNING(apiHandle, "No device to probe in list, 'haldependencies' handling skipped"); return 0; } while(halCurrentDeviceToProbe) { ret = InternalHalHandleOneHalDependencies(apiHandle, &halCurrentDeviceToProbe); if(ret < 0) { AFB_API_ERROR(apiHandle, "Error %i happened when tried to handle device to probe with uid:'%s' and request:'%s'", ret, halCurrentDeviceToProbe->uid, json_object_get_string(halCurrentDeviceToProbe->requestedDeviceJ)); return -2; } if(halCurrentDeviceToProbe->deviceClass == MANDATORY_PROBED_DEVICE && ! halCurrentDeviceToProbe->deviceData) { AFB_API_WARNING(apiHandle, "Mandatory device to probe not found, uid:'%s' and request:'%s'", halCurrentDeviceToProbe->uid, json_object_get_string(halCurrentDeviceToProbe->requestedDeviceJ)); return -3; } halCurrentDeviceToProbe = halCurrentDeviceToProbe->next; } return 0; } int InternalHalHalDependenciesConfig(afb_api_t apiHandle, CtlSectionT *section, json_object *DependenciesJ) { CtlConfigT *ctrlConfig; struct HalData *currentHalData; ctrlConfig = (CtlConfigT *) afb_api_get_userdata(apiHandle); if(! ctrlConfig) return -1; currentHalData = (struct HalData *) getExternalData(ctrlConfig); if(! currentHalData) return -2; if(DependenciesJ && InternalHalProcessAllHalDependencies(apiHandle, DependenciesJ, ¤tHalData->internalHalData->probedDevicesList)) { AFB_API_ERROR(apiHandle, "Failed to process 'haldependencies' section"); return -3; } else if(! DependenciesJ && (InternalHalHandleAllHalDependencies(apiHandle, ¤tHalData->internalHalData->probedDevicesList)) < 0) { AFB_API_ERROR(apiHandle, "Failed to handle 'haldependencies' section"); return -4; } return 0; } /******************************************************************************* * Internals HAL verbs functions * ******************************************************************************/ json_object *InternalHalGetJsonArrayForMixerDataTable(afb_api_t apiHandle, struct InternalHalMixerData **mixerDataList) { int wrapRet; json_object *mixerDataArrayJ, *currentMixerDataJ; struct InternalHalMixerData *currentMixerData; if(! apiHandle) { AFB_API_ERROR(apiHandle, "Api handle is not valid"); return NULL; } mixerDataArrayJ = json_object_new_array(); if(! mixerDataArrayJ) { AFB_API_ERROR(apiHandle, "Didn't succeed to allocate requested mixer data json array"); return NULL; } currentMixerData = *mixerDataList; while(currentMixerData) { wrapRet = wrap_json_pack(¤tMixerDataJ, "{s:s s:s}", "name", currentMixerData->verb, "cardId", currentMixerData->streamCardId); if(wrapRet) { AFB_API_ERROR(apiHandle, "Didn't succeed to allocate current mixer json object"); json_object_put(mixerDataArrayJ); return NULL; } json_object_array_add(mixerDataArrayJ, currentMixerDataJ); currentMixerData = currentMixerData->next; } return mixerDataArrayJ; } json_object *InternalHalGetJsonArrayForControls(afb_api_t apiHandle, struct InternalHalAlsaMapT *currentAlsaMapDataT) { int wrapRet; unsigned int idx; json_object *alsaMapDataArray, *currentAlsaMapData; if(! apiHandle) { AFB_API_ERROR(apiHandle, "Can't get current internal hal api handle"); return NULL; } if(! currentAlsaMapDataT) { AFB_API_ERROR(apiHandle, "Can't get Alsa map data table to handle"); return NULL; } alsaMapDataArray = json_object_new_array(); if(! alsaMapDataArray) { AFB_API_ERROR(apiHandle, "Didn't succeed to allocate ALSA controls data json array"); return NULL; } for(idx = 0; idx < currentAlsaMapDataT->ctlsCount; idx++) { wrapRet = wrap_json_pack(¤tAlsaMapData, "{s:s s:s}", "name", currentAlsaMapDataT->ctls[idx].uid, "info", currentAlsaMapDataT->ctls[idx].info ? currentAlsaMapDataT->ctls[idx].info : "none"); if(wrapRet) { AFB_API_ERROR(apiHandle, "Didn't succeed to allocate current streams json object"); json_object_put(alsaMapDataArray); return NULL; } json_object_array_add(alsaMapDataArray, currentAlsaMapData); } return alsaMapDataArray; } void InternalHalInfo(afb_req_t request) { int wrapRet; char *apiToCall, *returnedError = NULL, *returnedInfo = NULL; afb_api_t apiHandle; CtlConfigT *ctrlConfig; struct HalData *currentHalData; json_object *requestJson, *toReturnJ = NULL, *requestAnswer, *streamsArray, *controlsArray; apiHandle = afb_req_get_api(request); if(! apiHandle) { afb_req_fail(request, "api_handle", "Can't get current internal hal api handle"); return; } ctrlConfig = (CtlConfigT *) afb_api_get_userdata(apiHandle); if(! ctrlConfig) { afb_req_fail(request, "hal_controller_config", "Can't get current internal hal controller config"); return; } currentHalData = (struct HalData *) getExternalData(ctrlConfig); if(! currentHalData) { afb_req_fail(request, "hal_controller_data", "Can't get current internal hal controller data"); return; } requestJson = afb_req_json(request); if(! requestJson) { AFB_REQ_NOTICE(request, "Can't get request json"); } else if(json_object_is_type(requestJson, json_type_object) && json_object_get_object(requestJson)->count > 0) { apiToCall = currentHalData->internalHalData->mixerApiName; if(! apiToCall) { afb_req_fail(request, "mixer_api", "Can't get mixer api"); return; } if(InternalHalGetInfoFromMixer(apiHandle, apiToCall, requestJson, &toReturnJ, &returnedError, &returnedInfo)) { afb_req_fail_f(request, "mixer_info", "Call to mixer info verb didn't succeed with status '%s' and info '%s'", returnedError ? returnedError : "not returned", returnedInfo ? returnedInfo : "not returned"); return; } afb_req_success(request, toReturnJ, "Mixer requested data"); return; } streamsArray = InternalHalGetJsonArrayForMixerDataTable(apiHandle, ¤tHalData->internalHalData->streamsData); if(! streamsArray) { afb_req_fail(request, "streams_data", "Didn't succeed to generate streams data array"); return; } controlsArray = InternalHalGetJsonArrayForControls(apiHandle, currentHalData->internalHalData->alsaMapT); if(! controlsArray) { afb_req_fail(request, "controls_data", "Didn't succeed to generate controls data array"); return; } wrapRet = wrap_json_pack(&requestAnswer, "{s:o s:o}", "streams", streamsArray, "controls", controlsArray); if(wrapRet) { afb_req_fail(request, "halinfo_json_object", "Didn't succeed to allocate hal info response json object"); json_object_put(streamsArray); json_object_put(controlsArray); return; } afb_req_success(request, requestAnswer, "Requested data"); } void InternalHalSubscribeUnsubscribe(afb_req_t request, enum SubscribeUnsubscribeType subscribeUnsubscribeType) { int arrayIdx, searchIdx, count, subscriptionFound, subscriptionDoneNb = 0; char *currentSubscriptionString; afb_api_t apiHandle; CtlConfigT *ctrlConfig; struct HalData *currentHalData; struct InternalHalMixerData *currentStreamData; struct InternalHalAlsaMapT *InternalHalAlsaMapT; json_object *requestJson, *requestedSubscriptionsJ, *requestedSubscriptionJ = NULL, *subscriptionDoneNbJ = NULL; json_type requestJsonType; apiHandle = afb_req_get_api(request); if(! apiHandle) { afb_req_fail(request, "api_handle", "Can't get current internal hal api handle"); return; } ctrlConfig = (CtlConfigT *) afb_api_get_userdata(apiHandle); if(! ctrlConfig) { afb_req_fail(request, "hal_controller_config", "Can't get current internal hal controller config"); return; } currentHalData = (struct HalData *) getExternalData(ctrlConfig); if(! currentHalData) { afb_req_fail(request, "hal_controller_data", "Can't get current internal hal controller data"); return; } if(! currentHalData->internalHalData) { afb_req_fail(request, "hal_controller_data", "Current internal hal data is not valid"); return; } InternalHalAlsaMapT = currentHalData->internalHalData->alsaMapT; requestJson = afb_req_json(request); if(! requestJson) { afb_req_fail(request, "request_json", "Can't get request json"); return; } if(wrap_json_unpack(requestJson, "{s:o}", "events", &requestedSubscriptionsJ)) { afb_req_fail(request, "request_json", "Request json invalid"); return; } requestJsonType = json_object_get_type(requestedSubscriptionsJ); switch(requestJsonType) { case json_type_string: count = 1; requestedSubscriptionJ = requestedSubscriptionsJ; break; case json_type_array: count = (int) json_object_array_length(requestedSubscriptionsJ); break; default: afb_req_fail(request, "request_json", "Request json invalid"); return; } for(arrayIdx = 0; arrayIdx < count; arrayIdx++) { if(requestJsonType == json_type_array) { requestedSubscriptionJ = json_object_array_get_idx(requestedSubscriptionsJ, arrayIdx); if(! json_object_is_type(requestedSubscriptionJ, json_type_string)) { afb_req_fail_f(request, "request_json", "Request json number %i in array invalid", arrayIdx); return; } } subscriptionFound = 0; currentSubscriptionString = (char *) json_object_get_string(requestedSubscriptionJ); if(! strcasecmp(currentSubscriptionString, HAL_STREAM_UPDATES_EVENT_NAME)) { if(currentHalData->internalHalData->streamUpdates && subscribeUnsubscribeType == SUBSCRIPTION && afb_req_subscribe(request, currentHalData->internalHalData->streamUpdates)) { afb_req_fail(request, "request_stream_list_updates_event", "Error while trying to subscribe to stream list updates event"); return; } else if(currentHalData->internalHalData->streamUpdates && subscribeUnsubscribeType == UNSUBSCRIPTION && afb_req_unsubscribe(request, currentHalData->internalHalData->streamUpdates)) { afb_req_fail(request, "request_stream_list_updates_event", "Error while trying to unsubscribe to stream list updates event"); return; } subscriptionFound = 1; subscriptionDoneNb++; } currentStreamData = currentHalData->internalHalData->streamsData; while(currentStreamData && ! subscriptionFound) { if(! strcasecmp(currentSubscriptionString, currentStreamData->verb)) { if(currentStreamData->event && subscribeUnsubscribeType == SUBSCRIPTION && afb_req_subscribe(request, currentStreamData->event)) { afb_req_fail_f(request, "request_stream_event", "Error while trying to subscribe to %s stream events", currentStreamData->verb); return; } else if(currentStreamData->event && subscribeUnsubscribeType == UNSUBSCRIPTION && afb_req_unsubscribe(request, currentStreamData->event)) { afb_req_fail_f(request, "request_stream_event", "Error while trying to unsubscribe to %s stream events", currentStreamData->verb); return; } subscriptionFound = 1; subscriptionDoneNb++; break; } currentStreamData = currentStreamData->next; } searchIdx = 0; while((searchIdx < (InternalHalAlsaMapT ? InternalHalAlsaMapT->ctlsCount : 0)) && ! subscriptionFound) { if(! strcasecmp(currentSubscriptionString, InternalHalAlsaMapT->ctls[searchIdx].uid)) { if(InternalHalAlsaMapT->ctls[searchIdx].alsaControlEvent && subscribeUnsubscribeType == SUBSCRIPTION && afb_req_subscribe(request, InternalHalAlsaMapT->ctls[searchIdx].alsaControlEvent)) { afb_req_fail_f(request, "request_control_event", "Error while trying to subscribe to %s halmap controls events", InternalHalAlsaMapT->ctls[searchIdx].uid); return; } else if(InternalHalAlsaMapT->ctls[searchIdx].alsaControlEvent && subscribeUnsubscribeType == UNSUBSCRIPTION && afb_req_unsubscribe(request, InternalHalAlsaMapT->ctls[searchIdx].alsaControlEvent)) { afb_req_fail_f(request, "request_stream_event", "Error while trying to unsubscribe to %s halmap controls events", InternalHalAlsaMapT->ctls[searchIdx].uid); return; } subscriptionFound = 1; subscriptionDoneNb++; break; } searchIdx++; } } if(! subscriptionDoneNb) { afb_req_fail_f(request, "events_not_found", "%s failed, event(s) were not found", subscribeUnsubscribeType == SUBSCRIPTION ? "Subscription" : "Unsubscription"); return; } else { subscriptionDoneNbJ = json_object_new_int(subscriptionDoneNb); if(! subscriptionDoneNbJ) { afb_req_fail(request, "subscriptionnb_json_integer", "Didn't succeed to allocate subscription number json integer"); return; } } if(subscriptionDoneNb == count) afb_req_success_f(request, subscriptionDoneNbJ, "%s succeed for all the %i events requested", subscribeUnsubscribeType == SUBSCRIPTION ? "Subscription" : "Unsubscription", subscriptionDoneNb); else if(subscriptionDoneNb < count) afb_req_success_f(request, subscriptionDoneNbJ, "%s succeed but only to %i events requested out of %i", subscribeUnsubscribeType == SUBSCRIPTION ? "Subscription" : "Unsubscription", subscriptionDoneNb, count); else afb_req_success_f(request, subscriptionDoneNbJ, "%s succeed but to more events than requested (%i out of %i)", subscribeUnsubscribeType == SUBSCRIPTION ? "Subscription" : "Unsubscription", subscriptionDoneNb, count); } void InternalHalSubscribe(afb_req_t request) { InternalHalSubscribeUnsubscribe(request, SUBSCRIPTION); } void InternalHalUnsubscribe(afb_req_t request) { InternalHalSubscribeUnsubscribe(request, UNSUBSCRIPTION); }