summaryrefslogtreecommitdiffstats
path: root/4a-hal
diff options
context:
space:
mode:
Diffstat (limited to '4a-hal')
-rw-r--r--4a-hal/4a-hal-controllers/4a-hal-controllers-alsacore-link.c27
-rw-r--r--4a-hal/4a-hal-controllers/4a-hal-controllers-value-handler.c88
-rw-r--r--4a-hal/4a-hal-controllers/4a-hal-controllers-value-handler.h7
3 files changed, 118 insertions, 4 deletions
diff --git a/4a-hal/4a-hal-controllers/4a-hal-controllers-alsacore-link.c b/4a-hal/4a-hal-controllers/4a-hal-controllers-alsacore-link.c
index f360ceb..4ae5a13 100644
--- a/4a-hal/4a-hal-controllers/4a-hal-controllers-alsacore-link.c
+++ b/4a-hal/4a-hal-controllers/4a-hal-controllers-alsacore-link.c
@@ -558,10 +558,9 @@ void HalCtlsActionOnAlsaCtl(AFB_ReqT request)
return;
}
- json_object_put(previousControlValuesJ);
-
if(! json_object_is_type(requestJson, json_type_object)) {
AFB_ReqFailF(request, "request_json", "Request json is not valid '%s'", json_object_get_string(requestJson));
+ json_object_put(previousControlValuesJ);
json_object_put(normalizedPreviousControlValuesJ);
return;
}
@@ -570,11 +569,13 @@ void HalCtlsActionOnAlsaCtl(AFB_ReqT request)
AFB_ReqFailF(request,
"request_json", "Error when trying to get request value object inside request '%s'",
json_object_get_string(requestJson));
+ json_object_put(previousControlValuesJ);
json_object_put(normalizedPreviousControlValuesJ);
return;
}
- if(HalCtlsConvertJsonValues(apiHandle,
+ if((! json_object_is_type(valueJ, json_type_string)) &&
+ HalCtlsConvertJsonValues(apiHandle,
&currentAlsaCtl->ctl.alsaCtlProperties,
valueJ,
&convertedJ,
@@ -583,14 +584,32 @@ void HalCtlsActionOnAlsaCtl(AFB_ReqT request)
"request_json",
"Error when trying to convert request values '%s'",
json_object_get_string(valueJ));
+ json_object_put(previousControlValuesJ);
json_object_put(normalizedPreviousControlValuesJ);
return;
}
+ else if(json_object_is_type(valueJ, json_type_string) &&
+ HalCtlsChangePreviousValuesUsingJson(apiHandle,
+ &currentAlsaCtl->ctl.alsaCtlProperties,
+ valueJ,
+ previousControlValuesJ,
+ &convertedJ)) {
+ AFB_ReqFailF(request,
+ "previous_values",
+ "Error when trying to generate changed alsa control values (values : '%s', previous :'%s')",
+ json_object_get_string(valueJ),
+ json_object_get_string(previousControlValuesJ));
+ json_object_put(previousControlValuesJ);
+ json_object_put(normalizedPreviousControlValuesJ);
+ return;
+ }
+
+ json_object_put(previousControlValuesJ);
if(HalCtlsSetAlsaCtlValue(apiHandle, cardIdString, currentAlsaCtl->ctl.numid, convertedJ)) {
AFB_ReqFailF(request,
"alsa_control_call_error",
- "Error while trying to set value on alsa control %i, device '%s', converted '%s'",
+ "Error while trying to set value on alsa control %i, device '%s', converted message '%s'",
currentAlsaCtl->ctl.numid,
cardIdString,
json_object_get_string(convertedJ));
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
diff --git a/4a-hal/4a-hal-controllers/4a-hal-controllers-value-handler.h b/4a-hal/4a-hal-controllers/4a-hal-controllers-value-handler.h
index f976fe8..4844952 100644
--- a/4a-hal/4a-hal-controllers/4a-hal-controllers-value-handler.h
+++ b/4a-hal/4a-hal-controllers/4a-hal-controllers-value-handler.h
@@ -41,4 +41,11 @@ int HalCtlsConvertJsonValues(AFB_ApiT apiHandle,
json_object **ConvertedJ,
enum ConversionType requestedConversion);
+// Increase/Decrease previous values using percentage passed in Json
+int HalCtlsChangePreviousValuesUsingJson(AFB_ApiT apiHandle,
+ struct CtlHalAlsaCtlProperties *alsaCtlProperties,
+ json_object *requestedPercentageVariationJ,
+ json_object *previousControlValuesJ,
+ json_object **ChangedJ);
+
#endif /* _HAL_CTLS_VALUE_CONVERSION_INCLUDE_ */ \ No newline at end of file