summaryrefslogtreecommitdiffstats
path: root/4a-hal/4a-hal-controllers/4a-hal-controllers-value-handler.c
diff options
context:
space:
mode:
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