aboutsummaryrefslogtreecommitdiffstats
path: root/4a-hal/4a-hal-controllers/4a-hal-controllers-value-handler.c
blob: 74d0fdd88ae5bcdeae4e0b07e78a6a4ab8e30a52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * Copyright (C) 2018 "IoT.bzh"
 * Author Jonathan Aillet <jonathan.aillet@iot.bzh>
 *
 * 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 <stdio.h>
#include <string.h>

#include <limits.h>
#include <math.h>

#include <wrap-json.h>

#include <afb/afb-binding.h>

#include "4a-hal-controllers-value-handler.h"
#include "4a-hal-controllers-alsacore-link.h"

/*******************************************************************************
 *		Simple conversion value to/from percentage functions	       *
 ******************************************************************************/

int HalCtlsConvertValueToPercentage(double val, double min, double max)
{
	double range;

	range = max - min;
	if(range <= 0)
		return -INT_MAX;

	val -= min;

	return (int) round(val / range * 100);
}

int HalCtlsConvertPercentageToValue(int percentage, int min, int max)
{
	int range;

	range = max - min;
	if(range <= 0)
		return -INT_MAX;

	return (int) round((double) percentage * (double) range * 0.01 + (double) min);
}

/*******************************************************************************
 *		Convert json object from percentage to value	 	       *
 ******************************************************************************/

int HalCtlsConvertJsonValueForIntegerControl(afb_api_t apiHandle,
					     struct CtlHalAlsaCtlProperties *alsaCtlProperties,
					     json_object *toConvertJ,
					     json_object **ConvertedJ,
					     enum ConversionType requestedConversion)
{
	int initialValue, convertedValue;

	if(! json_object_is_type(toConvertJ, json_type_int)) {
		AFB_API_ERROR(apiHandle,
			      "Can't convert json value, unrecognized json format (must be an integer) : '%s'",
			      json_object_get_string(toConvertJ));
		return -1;
	}

	initialValue = json_object_get_int(toConvertJ);

	switch(requestedConversion) {
		case CONVERSION_NORMALIZED_TO_ALSACORE:
			if(initialValue < 0 || initialValue > 100) {
				AFB_API_ERROR(apiHandle,
					      "Cannot convert '%i' value, value should be between 0 and 100 ('%s')",
					      initialValue,
					      json_object_get_string(toConvertJ));
				return -2;
			}

			convertedValue = HalCtlsConvertPercentageToValue(initialValue,
									 alsaCtlProperties->minval,
									 alsaCtlProperties->maxval);

			if(convertedValue == -INT_MAX) {
				AFB_API_ERROR(apiHandle,
					      "Didn't succeed to convert %i (using min %i et max %i)",
					      initialValue,
					      alsaCtlProperties->minval,
					      alsaCtlProperties->maxval);
				return -3;
			}

			if(alsaCtlProperties->step) {
				// Round value to the nearest step
				convertedValue = (int) round((double) convertedValue / (double) alsaCtlProperties->step);
				convertedValue *= alsaCtlProperties->step;
			}
			break;

		case CONVERSION_ALSACORE_TO_NORMALIZED:
			convertedValue = HalCtlsConvertValueToPercentage(initialValue,
									 alsaCtlProperties->minval,
									 alsaCtlProperties->maxval);

			if(convertedValue == -INT_MAX) {
				AFB_API_ERROR(apiHandle,
					      "Didn't succeed to convert %i (using min %i et max %i)",
					      initialValue,
					      alsaCtlProperties->minval,
					      alsaCtlProperties->maxval);
				return -4;
			}

			break;

		default:
			AFB_API_ERROR(apiHandle,
				      "Can't convert '%i' value, unrecognized conversion type : '%i'",
				      initialValue,
				      (int) requestedConversion);
			*ConvertedJ = NULL;
			return -5;
	}

	*ConvertedJ = json_object_new_int(convertedValue);

	return 0;
}

int HalCtlsConvertJsonValueForBooleanControl(afb_api_t apiHandle,
					     struct CtlHalAlsaCtlProperties *alsaCtlProperties,
					     json_object *toConvertJ,
					     json_object **ConvertedJ,
					     enum ConversionType requestedConversion)
{
	int initialValue;

	switch(json_object_get_type(toConvertJ)) {
		case json_type_int:
			initialValue = json_object_get_int(toConvertJ);
			break;

		case json_type_boolean:
			initialValue = json_object_get_boolean(toConvertJ);
			break;

		default:
			AFB_API_ERROR(apiHandle,
				      "Can't convert json value, unrecognized format (must be an integer or a boolean) : '%s'",
				      json_object_get_string(toConvertJ));
			return -1;
	}

	if(initialValue < 0 || initialValue > 1) {
		AFB_API_ERROR(apiHandle,
			      "Cannot convert '%i' value, value should be 0 or 1 ('%s')",
			      initialValue,
			      json_object_get_string(toConvertJ));
		return -2;
	}

	switch(requestedConversion) {
		case CONVERSION_NORMALIZED_TO_ALSACORE:
			*ConvertedJ = json_object_new_int(initialValue);
			break;

		case CONVERSION_ALSACORE_TO_NORMALIZED:
			*ConvertedJ = json_object_new_boolean(initialValue);
			break;

		default:
			AFB_API_ERROR(apiHandle,
				      "Can't convert '%i' value, unrecognized conversion type : '%i'",
				      initialValue,
				      (int) requestedConversion);
			*ConvertedJ = NULL;
			return -3;
	}

	return 0;
}

int HalCtlsConvertJsonValues(afb_api_t apiHandle,
			     struct CtlHalAlsaCtlProperties *alsaCtlProperties,
			     json_object *toConvertJ,
			     json_object **ConvertedJ,
			     enum ConversionType requestedConversion)
{
	int conversionError = 0, idx, count;

	json_type toConvertType;
	json_object *toConvertObjectJ, *convertedValueJ, *convertedArrayJ;

	*ConvertedJ = NULL;

	toConvertType = json_object_get_type(toConvertJ);
	count = (toConvertType == json_type_array) ? (int) json_object_array_length(toConvertJ) : 1;

	convertedArrayJ = json_object_new_array();

	for(idx = 0; idx < count; idx++) {
		if(toConvertType == json_type_array)
			toConvertObjectJ = json_object_array_get_idx(toConvertJ, idx);
		else
			toConvertObjectJ = toConvertJ;

		switch(alsaCtlProperties->type) {
			case SND_CTL_ELEM_TYPE_INTEGER:
			case SND_CTL_ELEM_TYPE_INTEGER64:
				if((conversionError = HalCtlsConvertJsonValueForIntegerControl(apiHandle,
											       alsaCtlProperties,
											       toConvertObjectJ,
											       &convertedValueJ,
											       requestedConversion))) {
					AFB_API_ERROR(apiHandle,
						      "Error %i happened in when trying to convert index %i for integer control ('%s')",
						      conversionError,
						      idx,
						      json_object_get_string(toConvertObjectJ));
					json_object_put(convertedArrayJ);
					return -(idx + 1);
				}
				break;

			case SND_CTL_ELEM_TYPE_BOOLEAN:
				if((conversionError = HalCtlsConvertJsonValueForBooleanControl(apiHandle,
											       alsaCtlProperties,
											       toConvertObjectJ,
											       &convertedValueJ,
											       requestedConversion))) {
					AFB_API_ERROR(apiHandle,
						      "Error %i happened in when trying to convert index %i for boolean control ('%s')",
						      conversionError,
						      idx,
						      json_object_get_string(toConvertObjectJ));
					json_object_put(convertedArrayJ);
					return -(idx + 1);
				}
				break;

			default:
				AFB_API_ERROR(apiHandle,
					      "Conversion not handle for the alsa control type %i",
					      (int) alsaCtlProperties->type);
				json_object_put(convertedArrayJ);
				return -(idx + 1);
		}

		json_object_array_put_idx(convertedArrayJ, idx, convertedValueJ);
	}

	*ConvertedJ = convertedArrayJ;

	return 0;
}

int HalCtlsChangePreviousValuesUsingJson(afb_api_t 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_API_ERROR(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_API_ERROR(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_API_ERROR(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_API_ERROR(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_API_ERROR(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;
}