From f741fc82145c57c8dd1e483d9ab26ef2b4a92206 Mon Sep 17 00:00:00 2001 From: Jonathan Aillet Date: Wed, 19 Dec 2018 11:01:53 +0100 Subject: Add setting values of a ALSA control using a +/- Add the possibility to change halmap ALSA controls values using a +/- percentage string. Bug-AGL: SPEC-1313 Change-Id: I3b2cf141fc01171fd403c2602fb3805e654e8d68 Signed-off-by: Jonathan Aillet --- .../4a-hal-controllers-value-handler.c | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) (limited to '4a-hal/4a-hal-controllers/4a-hal-controllers-value-handler.c') diff --git a/4a-hal/4a-hal-controllers/4a-hal-controllers-value-handler.c b/4a-hal/4a-hal-controllers/4a-hal-controllers-value-handler.c index 18c4113..7a3dedd 100644 --- a/4a-hal/4a-hal-controllers/4a-hal-controllers-value-handler.c +++ b/4a-hal/4a-hal-controllers/4a-hal-controllers-value-handler.c @@ -261,5 +261,93 @@ int HalCtlsConvertJsonValues(AFB_ApiT apiHandle, *ConvertedJ = convertedArrayJ; + return 0; +} + +int HalCtlsChangePreviousValuesUsingJson(AFB_ApiT apiHandle, + struct CtlHalAlsaCtlProperties *alsaCtlProperties, + json_object *requestedPercentageVariationJ, + json_object *previousControlValuesJ, + json_object **ChangedJ) +{ + int requestedPercentageVariation, requestedeVariation, toChangeValue, changedValue, idx, count; + + char *requestedPercentageVariationString, *conversionEnd; + + json_object *toChangeObjectJ, *changedArrayJ; + + *ChangedJ = NULL; + + requestedPercentageVariationString = (char *) json_object_get_string(requestedPercentageVariationJ); + + requestedPercentageVariation = (int) strtol(requestedPercentageVariationString, &conversionEnd, 10); + if(conversionEnd == requestedPercentageVariationString) { + AFB_ApiError(apiHandle, + "Tried to increase/decrease an integer control \ + but string sent in json is not a increase/decrease string : '%s'", + json_object_get_string(requestedPercentageVariationJ)); + return -1; + } + + if(alsaCtlProperties->type != SND_CTL_ELEM_TYPE_INTEGER && + alsaCtlProperties->type != SND_CTL_ELEM_TYPE_INTEGER64) { + AFB_ApiError(apiHandle, + "Tried to increase/decrease values on a incompatible \ + control type (%i), control type must be an integer", + alsaCtlProperties->type); + return -2; + } + + if(requestedPercentageVariation < -100 || requestedPercentageVariation > 100) { + AFB_ApiError(apiHandle, + "Tried to increase/decrease values but specified change is \ + not a valid percentage, it should be between -100 and 100"); + return -3; + } + + requestedeVariation = HalCtlsConvertPercentageToValue((int) abs(requestedPercentageVariation), + alsaCtlProperties->minval, + alsaCtlProperties->maxval); + + if(requestedeVariation == -INT_MAX) { + AFB_ApiError(apiHandle, + "Didn't succeed to convert %i (using min %i et max %i)", + requestedPercentageVariation, + alsaCtlProperties->minval, + alsaCtlProperties->maxval); + return -4; + } + + if(requestedPercentageVariation < 0) + requestedeVariation = -requestedeVariation; + + count = (int) json_object_array_length(previousControlValuesJ); + + changedArrayJ = json_object_new_array(); + + for(idx = 0; idx < count; idx++) { + toChangeObjectJ = json_object_array_get_idx(previousControlValuesJ, idx); + + if(! json_object_is_type(toChangeObjectJ, json_type_int)) { + AFB_ApiError(apiHandle, + "Current json object %s is not an integer", + json_object_get_string(toChangeObjectJ)); + return -(10 + idx); + } + + toChangeValue = json_object_get_int(toChangeObjectJ); + + if((toChangeValue + requestedeVariation) < alsaCtlProperties->minval) + changedValue = alsaCtlProperties->minval; + else if((toChangeValue + requestedeVariation) > alsaCtlProperties->maxval) + changedValue = alsaCtlProperties->maxval; + else + changedValue = toChangeValue + requestedeVariation; + + json_object_array_put_idx(changedArrayJ, idx, json_object_new_int(changedValue)); + } + + *ChangedJ = changedArrayJ; + return 0; } \ No newline at end of file -- cgit 1.2.3-korg