summaryrefslogtreecommitdiffstats
path: root/4a-hal/4a-hal-controllers/4a-hal-controllers-value-handler.c
diff options
context:
space:
mode:
authorJonathan Aillet <jonathan.aillet@iot.bzh>2018-12-19 11:01:53 +0100
committerJonathan Aillet <jonathan.aillet@iot.bzh>2018-12-20 11:10:17 +0100
commitf741fc82145c57c8dd1e483d9ab26ef2b4a92206 (patch)
tree640f24a4bcd132d4c2c17fb4ff0b761d9eab6229 /4a-hal/4a-hal-controllers/4a-hal-controllers-value-handler.c
parentcdbaaf3f0821c3f991d5a938e5343f877309315a (diff)
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 <jonathan.aillet@iot.bzh>
Diffstat (limited to '4a-hal/4a-hal-controllers/4a-hal-controllers-value-handler.c')
-rw-r--r--4a-hal/4a-hal-controllers/4a-hal-controllers-value-handler.c88
1 files changed, 88 insertions, 0 deletions
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
@@ -262,4 +262,92 @@ 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