aboutsummaryrefslogtreecommitdiffstats
path: root/src/4a-internals-hal/4a-internals-hal-alsacore-link.c
blob: 774054cc14ca88bad47acdff7370879860698c7b (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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
/*
 * 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 <wrap-json.h>

#include <alsa/asoundlib.h>

#include <afb/afb-binding.h>

#include <ctl-config.h>

#include "4a-hal-utilities-alsa-data.h"
#include "4a-hal-utilities-data.h"

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

/*******************************************************************************
 *		Map to alsa control types				       *
 ******************************************************************************/

static const char *const snd_ctl_elem_type_names[] = {
	[SND_CTL_ELEM_TYPE_NONE] = "NONE",
	[SND_CTL_ELEM_TYPE_BOOLEAN] = "BOOLEAN",
	[SND_CTL_ELEM_TYPE_INTEGER] = "INTEGER",
	[SND_CTL_ELEM_TYPE_ENUMERATED] = "ENUMERATED",
	[SND_CTL_ELEM_TYPE_BYTES] = "BYTES",
	[SND_CTL_ELEM_TYPE_IEC958] = "IEC958",
	[SND_CTL_ELEM_TYPE_INTEGER64] = "INTEGER64",
};

/*******************************************************************************
 *		Alsa control types map from string function		       *
 ******************************************************************************/

snd_ctl_elem_type_t InternalHalMapsAlsaTypeToEnum(const char *label)
{
	int idx;
	static int length = sizeof(snd_ctl_elem_type_names) / sizeof(char *);

	for(idx = 0; idx < length; idx++) {
		if(! strcasecmp(label, snd_ctl_elem_type_names[idx]))
			return (snd_ctl_elem_type_t) idx;
	}

	return SND_CTL_ELEM_TYPE_NONE;
}

/*******************************************************************************
 *		Internals HAL - Alsacore events handler function	       *
 ******************************************************************************/

enum aslacoreEventType InternalHalGetAlsaCoreEventType(const char *evtLabel)
{
	if(! evtLabel)
		return ALSACORE_UNRECOGNIZED_EVENT;

	if(! strncmp(&evtLabel[ALSACORE_EVENT_NAME_BEGIN],
		     ALSACORE_CARD_CONTROL_EVENT_NAME_PREFIX,
		     ALSACORE_CARD_CONTROL_EVENT_NAME_PREFIX_LENGTH))
		return ALSACORE_EVENT_CARD_CONTROL_CHANGE;

	return ALSACORE_UNRECOGNIZED_EVENT;
}

int InternalHalHandleAlsaCoreCardControlEvent(afb_api_t apiHandle,
					      const char *evtLabel,
					      json_object *eventJ,
					      struct HalData *currentHalData)
{
	int numid, cardidx, controlsUsingCardCount = 0, controlUsedCount = 0;

	char cardNbString[6];

	json_object *valuesJ, *normalizedValuesJ;

	CtlSourceT source;

	struct InternalHalAlsaMap *currentHalMapControl;

	if(! apiHandle || ! evtLabel || ! eventJ || ! currentHalData) {
		AFB_API_ERROR(apiHandle, "Invalid argument(s)");
		return -1;
	}

	if(sscanf(&evtLabel[ALSACORE_CARD_CONTROL_EVENT_BEGIN], "%d", &cardidx) != 1) {
		AFB_API_ERROR(apiHandle,
			      "Did not succeed to get ALSA card number of the modified control : evtname=%s [msg=%s]",
			      evtLabel,
			      json_object_get_string(eventJ));
		return -2;
	}

	if(wrap_json_unpack(eventJ, "{s:i s:o}", "id", &numid, "val", &valuesJ)) {
		AFB_API_ERROR(apiHandle,
			      "Invalid alsacore modified control event received label=%s value=%s",
			      evtLabel,
			      json_object_get_string(eventJ));
		return -3;
	}

	if(cds_list_empty(&currentHalData->internalHalData->halMapListHead)) {
		AFB_API_ERROR(apiHandle,
			      "No halmap data is available, cannot handle alsacore event label=%s value=%s",
			      evtLabel,
			      json_object_get_string(eventJ));
		return -4;
	}

	// Search for corresponding numid in ALSA controls list, if found, launch callback (if available)
	cds_list_for_each_entry(currentHalMapControl, &currentHalData->internalHalData->halMapListHead, node) {
		if(currentHalMapControl->cardNb == cardidx)
			controlsUsingCardCount++;

		if(currentHalMapControl->cardNb != cardidx ||
		   ! currentHalMapControl->ctl.alsaCtlProperties ||
		   currentHalMapControl->ctl.numid != numid)
			continue;

		if(currentHalMapControl->action) {
			memset(&source, 0, sizeof(CtlSourceT));
			source.uid = currentHalMapControl->action->uid;
			source.api = currentHalMapControl->action->api;

			ActionExecOne(&source, currentHalMapControl->action, valuesJ);
		}
		else if(currentHalMapControl->actionJ) {
			AFB_API_WARNING(apiHandle,
					"The alsa control id '%i' is corresponding to a known control which "
					"has a registered action, but action is not ready to be executed",
					numid);
		}
		else {
			AFB_API_NOTICE(apiHandle,
				       "The alsa control id '%i' is corresponding to a known control "
				       "but without any action registered",
				       numid);
		}

		if(! currentHalMapControl->alsaControlEvent ||
		   InternalHalConvertJsonValues(apiHandle,
						currentHalMapControl->ctl.alsaCtlProperties,
						valuesJ,
						&normalizedValuesJ,
						CONVERSION_ALSACORE_TO_NORMALIZED) ||
		   (afb_event_push(currentHalMapControl->alsaControlEvent, normalizedValuesJ) < 0)) {
			AFB_API_ERROR(apiHandle,
				      "Couldn't generate an event for known halmap %s (alsa control id %i)",
				      currentHalMapControl->uid,
				      currentHalMapControl->ctl.numid);
		}

		controlUsedCount++;
	}

	if(! controlsUsingCardCount) {
		AFB_API_INFO(apiHandle,
			     "Receiving events from not handle card %i, unscubscribe from these events",
			     cardidx);
		snprintf(cardNbString, sizeof(cardNbString), "hw:%i", cardidx);
		InternalHalUnsubscribeFromAlsacoreCardEvent(apiHandle, cardNbString);
		return 1;
	}

	if(controlUsedCount)
		return 0;

	AFB_API_INFO(apiHandle,
		     "Alsacore event with an unrecognized numid: %i, evtname=%s [msg=%s]",
		     numid,
		     evtLabel,
		     json_object_get_string(eventJ));

	return 2;

}

int InternalHalHandleAlsaCoreEvents(afb_api_t apiHandle,
				    const char *evtLabel,
				    json_object *eventJ,
				    struct HalData *currentHalData)
{
	int returned;

	if(! apiHandle || ! evtLabel || ! eventJ || ! currentHalData) {
		AFB_API_ERROR(apiHandle, "Invalid argument(s)");
		return -1;
	}

	switch(InternalHalGetAlsaCoreEventType(evtLabel)) {
		case ALSACORE_EVENT_CARD_CONTROL_CHANGE:
			returned = InternalHalHandleAlsaCoreCardControlEvent(apiHandle, evtLabel, eventJ, currentHalData);
			if(returned < 0) {
				AFB_API_ERROR(apiHandle,
					      "Error %i caught when tried to handle alsacore card control event evtname=%s [msg=%s]",
					      returned,
					      evtLabel,
					      json_object_get_string(eventJ));
				return -2;
			}
			else if(returned > 0) {
				AFB_API_INFO(apiHandle,
					     "Warning %i raised when tried to handle alsacore card control event evtname=%s [msg=%s]",
					     returned,
					     evtLabel,
					     json_object_get_string(eventJ));
			}
			return 0;

		case ALSACORE_UNRECOGNIZED_EVENT:
		default:
			AFB_API_NOTICE(apiHandle,
				       "Alsacore event with an unrecognized evtname=%s [msg=%s]",
				       evtLabel,
				       json_object_get_string(eventJ));
			return 1;
	}
}

/*******************************************************************************
 *		Internals HAL - Alsacore calls funtions			       *
 ******************************************************************************/

int InternalHalGetCardInfo(afb_api_t apiHandle, json_object *requestJ, json_object **responseJ)
{
	int errorToReturn = 0;

	char *returnedError = NULL, *returnedInfo = NULL;

	if(! apiHandle || ! requestJ || ! responseJ) {
		AFB_API_ERROR(apiHandle, "Invalid argument(s)");
		return -1;
	}

	if(afb_api_call_sync(apiHandle,
			     ALSACORE_API,
			     ALSACORE_GETINFO_VERB,
			     requestJ,
			     responseJ,
			     &returnedError,
			     &returnedInfo)) {
		AFB_API_ERROR(apiHandle,
			      "Something went wrong during call to verb '%s' of api '%s' with error '%s' and info '%s'",
			      ALSACORE_GETINFO_VERB,
			      ALSACORE_API,
			      returnedError ? returnedError : "none",
			      returnedInfo ? returnedInfo : "none");
		errorToReturn = -2;
	}

	free(returnedError);
	free(returnedInfo);

	return errorToReturn;
}

int InternalHalSubscribeUnscubscribeToAlsacoreEvent(afb_api_t apiHandle,
						    json_object *subscribeQueryJ,
						    enum AlsacoreSubscribeUnsubscribeType subscriptionType)
{
	int err = 0;

	char *returnedError = NULL, *returnedInfo = NULL;

	json_object *responseJ = NULL;

	if(! apiHandle || ! subscribeQueryJ) {
		AFB_API_ERROR(apiHandle, "Invalid argument(s)");
		return -1;
	}

	if(afb_api_call_sync(apiHandle,
			     ALSACORE_API,
			     (subscriptionType == ALSACORE_SUBSCRIPTION) ? ALSACORE_SUBSCRIBE_VERB : ALSACORE_UNSUBSCRIBE_VERB,
			     subscribeQueryJ,
			     &responseJ,
			     &returnedError,
			     &returnedInfo)) {
		AFB_API_ERROR(apiHandle,
			      "Something went wrong during call to verb '%s' of api '%s' with error '%s' and info '%s'",
			      (subscriptionType == ALSACORE_SUBSCRIPTION) ? ALSACORE_SUBSCRIBE_VERB : ALSACORE_UNSUBSCRIBE_VERB,
			      ALSACORE_API,
			      returnedError ? returnedError : "not returned",
			      returnedInfo ? returnedInfo : "not returned");
		err = -2;
	}

	if(responseJ)
		json_object_put(responseJ);

	free(returnedError);
	free(returnedInfo);

	return err;
}

int InternalHalSubscribeUnscubscribeToAlsacoreCardEvent(afb_api_t apiHandle,
							char *cardId,
							enum AlsacoreSubscribeUnsubscribeType subscriptionType)
{
	int err, wrapRet;

	json_object *subscribeQueryJ = NULL;

	wrapRet = wrap_json_pack(&subscribeQueryJ,
				 "{s:s, s:s}",
				 "event", ALSACORE_CARD_CONTROL_EVENT_NAME,
				 "devid", cardId);
	if(wrapRet) {
		AFB_API_ERROR(apiHandle, "Didn't succeed to allocate subscription query json object");
		return -1;
	}

	err = InternalHalSubscribeUnscubscribeToAlsacoreEvent(apiHandle, subscribeQueryJ, subscriptionType);
	if(err) {
		AFB_API_ERROR(apiHandle,
			      "Error %i happened when tried to subscribe to %s card event",
			      err,
			      ALSACORE_API);
		if(! subscribeQueryJ)
			json_object_put(subscribeQueryJ);
		return -2;
	}

	return 0;
}

int InternalHalSubscribeToAlsacoreCardEvent(afb_api_t apiHandle, char *cardId)
{
	return InternalHalSubscribeUnscubscribeToAlsacoreCardEvent(apiHandle, cardId, ALSACORE_SUBSCRIPTION);
}

int InternalHalUnsubscribeFromAlsacoreCardEvent(afb_api_t apiHandle, char *cardId)
{
	return InternalHalSubscribeUnscubscribeToAlsacoreCardEvent(apiHandle, cardId, ALSACORE_UNSUBSCRIPTION);
}

int InternalHalGetAlsaCtlInfo(afb_api_t apiHandle, char *cardId, struct InternalHalAlsaCtl *currentAlsaCtl, json_object **returnedDataJ)
{
	int wrapRet;

	char *returnedError = NULL, *returnedInfo = NULL;

	json_object *queryJ, *responseJ = NULL;

	*returnedDataJ = NULL;

	if(! apiHandle) {
		AFB_API_ERROR(apiHandle, "Api handle not available");
		return -1;
	}

	if(! cardId) {
		AFB_API_ERROR(apiHandle, "Card id is not available");
		return -2;
	}

	if(! currentAlsaCtl) {
		AFB_API_ERROR(apiHandle, "Alsa control data structure is not available");
		return -3;
	}

	if(currentAlsaCtl->name && currentAlsaCtl->numid > 0) {
		AFB_API_DEBUG(apiHandle,
			     "Both a control name (%s) and a control id (%i) are specified, control id will be used",
			     currentAlsaCtl->name,
			     currentAlsaCtl->numid);
	}

	if(currentAlsaCtl->numid > 0) {
		wrapRet = wrap_json_pack(&queryJ,
					 "{s:s s:i s:i}",
					 "devid", cardId,
					 "ctl", currentAlsaCtl->numid,
					 "mode", 3);
	}
	else if(currentAlsaCtl->name) {
		wrapRet = wrap_json_pack(&queryJ,
					 "{s:s s:s s:i}",
					 "devid", cardId,
					 "ctl", currentAlsaCtl->name,
					 "mode", 3);
	}
	else {
		AFB_API_ERROR(apiHandle, "Need at least a control name or a control id");
		return -4;
	}

	if(wrapRet) {
		AFB_API_ERROR(apiHandle, "Didn't succeed to allocate get ALSA control query json object");
		return -5;
	}

	if(afb_api_call_sync(apiHandle,
			     ALSACORE_API,
			     ALSACORE_CTLGET_VERB,
			     queryJ,
			     &responseJ,
			     &returnedError,
			     &returnedInfo)) {
		AFB_API_ERROR(apiHandle,
			      "Something went wrong during call to verb '%s' of api '%s' with error '%s' and info '%s'",
			      ALSACORE_CTLGET_VERB,
			      ALSACORE_API,
			      returnedError ? returnedError : "not returned",
			      returnedInfo ? returnedInfo : "not returned");
		free(returnedError);
		free(returnedInfo);
		return -6;
	}
	else if(! responseJ) {
		AFB_API_ERROR(apiHandle,
			      "Seems that %s call to api %s succeed but no response was returned",
			      ALSACORE_CTLGET_VERB,
			      ALSACORE_API);
		return -7;
	}

	*returnedDataJ = responseJ;

	return 0;
}

int InternalHalUpdateAlsaCtlProperties(afb_api_t apiHandle, char *cardId, struct InternalHalAlsaCtl *currentAlsaCtl)
{
	int err;

	char *name;

	json_object *returnedDataJ = NULL;

	if(! currentAlsaCtl->alsaCtlProperties) {
		AFB_API_ERROR(apiHandle, "Data structure to store alsa control properties is not allocated");
		return -8;
	}

	err = InternalHalGetAlsaCtlInfo(apiHandle, cardId, currentAlsaCtl, &returnedDataJ);
	if(err) {
		return err;
	}
	// TBD JAI : get dblinear/dbminmax/... values
	else if(wrap_json_unpack(returnedDataJ,
				 "{s:i s:s s:{s?:i s?:i s?:i s?:i s?:i}}",
				 "id", &currentAlsaCtl->numid,
				 "name", &name,
				 "ctl",
				 "type", (int *) &currentAlsaCtl->alsaCtlProperties->type,
				 "count", &currentAlsaCtl->alsaCtlProperties->count,
				 "min", &currentAlsaCtl->alsaCtlProperties->minval,
				 "max", &currentAlsaCtl->alsaCtlProperties->maxval,
				 "step", &currentAlsaCtl->alsaCtlProperties->step)) {
		AFB_API_ERROR(apiHandle,
			      "Didn't succeed to get control %i properties on device '%s' : '%s'",
			      currentAlsaCtl->numid,
			      cardId,
			      json_object_get_string(returnedDataJ));
		err = -9;
	}
	else {
		currentAlsaCtl->name = strdup(name);
		if(! currentAlsaCtl->name) {
			AFB_API_ERROR(apiHandle, "Didn't succeed to store (allocate) control 'name' string");
			err = -10;
		}
	}

	if(returnedDataJ)
		json_object_put(returnedDataJ);

	return err;
}

int InternalHalGetAlsaCtlValues(afb_api_t apiHandle, char *cardId, struct InternalHalAlsaCtl *currentAlsaCtl, json_object **returnedValuesJ)
{
	int err = 0;

	json_object *returnedDataJ = NULL, *returnedValuesArrayJ;

	*returnedValuesJ = NULL;

	err = InternalHalGetAlsaCtlInfo(apiHandle, cardId, currentAlsaCtl, &returnedDataJ);
	if(err) {
		return err;
	}
	else if(wrap_json_unpack(returnedDataJ, "{s:o}", "val", &returnedValuesArrayJ)) {
		AFB_API_ERROR(apiHandle,
			      "Didn't succeed to get control %i values on device '%s' : '%s'",
			      currentAlsaCtl->numid,
			      cardId,
			      json_object_get_string(returnedValuesArrayJ));

		err = -8;
	}
	else if(! json_object_is_type(returnedValuesArrayJ, json_type_array)) {
		AFB_API_ERROR(apiHandle,
			      "Json returned by control %i values on device '%s' are not an array ('%s')",
			      currentAlsaCtl->numid,
			      cardId,
			      json_object_get_string(returnedValuesArrayJ));

		err = -9;
	}

	if(! err)
		*returnedValuesJ = json_object_get(returnedValuesArrayJ);

	if(returnedDataJ)
		json_object_put(returnedDataJ);

	return err;
}

int InternalHalSetAlsaCtlValue(afb_api_t apiHandle, char *cardId, int ctlId, json_object *valuesJ)
{
	int err = 0, wrapRet;

	char *returnedError = NULL, *returnedInfo = NULL;

	json_object *queryJ, *responseJ = NULL;

	if(! apiHandle) {
		AFB_API_ERROR(apiHandle, "Api handle not available");
		return -1;
	}

	if(! cardId) {
		AFB_API_ERROR(apiHandle, "Card id is not available");
		return -2;
	}

	if(ctlId <= 0) {
		AFB_API_ERROR(apiHandle, "Alsa control id is not valid");
		return -3;
	}

	if(! valuesJ) {
		AFB_API_ERROR(apiHandle, "Values to set json is not available");
		return -4;
	}

	wrapRet = wrap_json_pack(&queryJ,
				 "{s:s s:{s:i s:o}}",
				 "devid", cardId,
				 "ctl",
				 "id", ctlId,
				 "val", json_object_get(valuesJ));
	if(wrapRet) {
		AFB_API_ERROR(apiHandle, "Didn't succeed to allocate set ALSA control query json object");
		return -5;
	}

	if(afb_api_call_sync(apiHandle,
			     ALSACORE_API,
			     ALSACORE_CTLSET_VERB,
			     queryJ,
			     &responseJ,
			     &returnedError,
			     &returnedInfo)) {
		AFB_API_ERROR(apiHandle,
			      "Something went wrong during call to verb '%s' of api '%s' with error '%s' and info '%s'",
			      ALSACORE_CTLSET_VERB,
			      ALSACORE_API,
			      returnedError ? returnedError : "not returned",
			      returnedInfo ? returnedInfo : "not returned");
		err = -6;
	}

	if(responseJ)
		json_object_put(responseJ);

	free(returnedError);
	free(returnedInfo);

	return err;
}

int InternalHalCreateAlsaCtl(afb_api_t apiHandle, char *cardId, struct InternalHalAlsaCtl *alsaCtlToCreate)
{
	int err = 0, wrapRet;

	char *returnedError = NULL, *returnedInfo = NULL;

	json_object *queryJ, *responseJ = NULL;

	if(! apiHandle) {
		AFB_API_ERROR(apiHandle, "Api handle not available");
		return -1;
	}

	if(! cardId) {
		AFB_API_ERROR(apiHandle, "Card id is not available");
		return -2;
	}

	if(! alsaCtlToCreate) {
		AFB_API_ERROR(apiHandle, "Alsa control data structure is not available");
		return -3;
	}

	if(! alsaCtlToCreate->alsaCtlCreation) {
		AFB_API_ERROR(apiHandle, "Alsa control data structure for control creation is not available");
		return -4;
	}

	wrapRet = wrap_json_pack(&queryJ,
				 "{s:s s:{s:i s:s s?:i s?:i s?:i s:i s:i}}",
				 "devid", cardId,
				 "ctl",
				 "ctl", -1,
				 "name", alsaCtlToCreate->name,
				 "min", alsaCtlToCreate->alsaCtlCreation->minval,
				 "max", alsaCtlToCreate->alsaCtlCreation->maxval,
				 "step", alsaCtlToCreate->alsaCtlCreation->step,
				 "type", (int) alsaCtlToCreate->alsaCtlCreation->type,
				 "count", alsaCtlToCreate->alsaCtlCreation->count);
	if(wrapRet) {
		AFB_API_ERROR(apiHandle, "Didn't succeed to allocate create ALSA control query json object");
		return -5;
	}

	if(afb_api_call_sync(apiHandle,
			     ALSACORE_API,
			     ALSACORE_ADDCTL_VERB,
			     queryJ,
			     &responseJ,
			     &returnedError,
			     &returnedInfo)) {
		AFB_API_ERROR(apiHandle,
			      "Something went wrong during call to verb '%s' of api '%s' with error '%s' and info '%s'",
			      ALSACORE_ADDCTL_VERB,
			      ALSACORE_API,
			      returnedError ? returnedError : "not returned",
			      returnedInfo ? returnedInfo : "not returned");
		err = -6;
	}
	else if(! responseJ) {
		AFB_API_ERROR(apiHandle,
			      "Seems that %s call to api %s succeed but no response was returned",
			      ALSACORE_ADDCTL_VERB,
			      ALSACORE_API);
		err = -7;
	}
	else if(wrap_json_unpack(responseJ, "{s:i}", "id", &alsaCtlToCreate->numid)) {
		AFB_API_ERROR(apiHandle,
			      "Can't get create id from %s of %s api",
			      ALSACORE_ADDCTL_VERB,
			      ALSACORE_API);
		err = -8;
	}
	else if(wrap_json_unpack(responseJ, "{s:o}", "ctl", NULL)) {
		AFB_API_WARNING(apiHandle, "Control %s was already present but has been updated", alsaCtlToCreate->name);
	}

	if(responseJ)
		json_object_put(responseJ);

	free(returnedError);
	free(returnedInfo);

	return err;
}

/*******************************************************************************
 *		Internals HAL - Alsacore controls request callback	       *
 ******************************************************************************/

void InternalHalActionOnAlsaCtl(afb_req_t request)
{
	int wrapRet;

	char cardIdString[6];

	afb_api_t apiHandle;

	CtlConfigT *ctrlConfig;

	struct HalData *currentHalData;
	struct InternalHalAlsaMap *currentAlsaCtl;

	json_object *requestJson,
		    *valueJ,
		    *convertedJ,
		    *answerJ,
		    *previousControlValuesJ,
		    *normalizedPreviousControlValuesJ,
		    *appliedControlValuesJ,
		    *normalizedAppliedControlValuesJ;

	apiHandle = afb_req_get_api(request);
	if(! apiHandle) {
		afb_req_fail(request, "api_handle", "Can't get current internal hal api handle");
		return;
	}

	ctrlConfig = (CtlConfigT *) afb_api_get_userdata(apiHandle);
	if(! ctrlConfig) {
		afb_req_fail(request, "hal_controller_config", "Can't get current internal hal controller config");
		return;
	}

	currentHalData = (struct HalData *) getExternalData(ctrlConfig);
	if(! currentHalData) {
		afb_req_fail(request, "hal_controller_data", "Can't get current internal hal controller data");
		return;
	}

	if(currentHalData->status == HAL_STATUS_UNAVAILABLE) {
		afb_req_fail(request, "hal_unavailable", "Seems that hal is not available");
		return;
	}

	currentAlsaCtl = (struct InternalHalAlsaMap *) afb_req_get_vcbdata(request);
	if(! currentAlsaCtl) {
		afb_req_fail(request, "alsa_control_data", "Can't get current alsa control data");
		return;
	}

	if(currentAlsaCtl->ctl.numid <= 0) {
		afb_req_fail(request, "alsa_control_id", "Alsa control id is not valid");
		return;
	}

	if(! currentAlsaCtl->ctl.alsaCtlProperties) {
		afb_req_fail(request, "alsa_control_not_available", "Alsa control is not available");
		return;
	}

	snprintf(cardIdString, 6, "hw:%i", currentAlsaCtl->cardNb);

	if(InternalHalGetAlsaCtlValues(apiHandle, cardIdString, &currentAlsaCtl->ctl, &previousControlValuesJ)) {
		afb_req_fail(request, "previous_values", "Error when trying to get unchanged alsa control values");
		return;
	}
	else if(InternalHalConvertJsonValues(apiHandle,
					     currentAlsaCtl->ctl.alsaCtlProperties,
					     previousControlValuesJ,
					     &normalizedPreviousControlValuesJ,
					     CONVERSION_ALSACORE_TO_NORMALIZED)) {
		afb_req_fail_f(request,
			       "request_json",
			       "Error when trying to normalize unchanged alsa control values json '%s'",
			       json_object_get_string(previousControlValuesJ));
		json_object_put(previousControlValuesJ);
		return;
	}

	requestJson = afb_req_json(request);
	if(! requestJson) {
		wrapRet = wrap_json_pack(&answerJ,
					 "{s:o}",
					 "current", normalizedPreviousControlValuesJ);
		if(wrapRet) {
			afb_req_fail(request,
				     "currentvalues_json_object",
				     "Didn't succeed to allocate ALSA control current values response json object");
			return;
		}

		afb_req_success(request, answerJ, "Current controls values");
		json_object_put(previousControlValuesJ);
		return;
	}

	if(! json_object_is_type(requestJson, json_type_object)) {
		afb_req_fail_f(request, "request_json", "Request json is not valid '%s'", json_object_get_string(requestJson));
		json_object_put(previousControlValuesJ);
		json_object_put(normalizedPreviousControlValuesJ);
		return;
	}

	if(wrap_json_unpack(requestJson, "{s:o}", "value", &valueJ)) {
		afb_req_fail_f(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((! json_object_is_type(valueJ, json_type_string)) &&
	   InternalHalConvertJsonValues(apiHandle,
					currentAlsaCtl->ctl.alsaCtlProperties,
					valueJ,
					&convertedJ,
					CONVERSION_NORMALIZED_TO_ALSACORE)) {
		afb_req_fail_f(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) &&
		InternalHalChangePreviousValuesUsingJson(apiHandle,
							 currentAlsaCtl->ctl.alsaCtlProperties,
							 valueJ,
							 previousControlValuesJ,
							 &convertedJ)) {
		afb_req_fail_f(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(InternalHalSetAlsaCtlValue(apiHandle, cardIdString, currentAlsaCtl->ctl.numid, convertedJ)) {
		afb_req_fail_f(request,
			       "alsa_control_call_error",
			       "Error while trying to set value on alsa control %i, device '%s', converted message '%s'",
			       currentAlsaCtl->ctl.numid,
			       cardIdString,
			       json_object_get_string(convertedJ));
		json_object_put(convertedJ);
		json_object_put(normalizedPreviousControlValuesJ);
		return;
	}

	json_object_put(convertedJ);

	if(InternalHalGetAlsaCtlValues(apiHandle, cardIdString, &currentAlsaCtl->ctl, &appliedControlValuesJ)) {
		afb_req_fail(request, "applied_values", "Error when trying to get applied alsa control values");
		json_object_put(normalizedPreviousControlValuesJ);
		return;
	}
	else if(InternalHalConvertJsonValues(apiHandle,
					     currentAlsaCtl->ctl.alsaCtlProperties,
					     appliedControlValuesJ,
					     &normalizedAppliedControlValuesJ,
					     CONVERSION_ALSACORE_TO_NORMALIZED)) {
		afb_req_fail_f(request,
			       "request_json",
			       "Error when trying to normalize applied values json '%s'",
			       json_object_get_string(appliedControlValuesJ));
		json_object_put(normalizedPreviousControlValuesJ);
		json_object_put(appliedControlValuesJ);
		return;
	}

	json_object_put(appliedControlValuesJ);

	wrapRet = wrap_json_pack(&answerJ,
				 "{s:o, s:o}",
				 "previous", normalizedPreviousControlValuesJ,
				 "current", normalizedAppliedControlValuesJ);
	if(wrapRet) {
		afb_req_fail(request,
			     "values_json_object",
			     "Didn't succeed to allocate ALSA control previous and current values response json object");
		return;
	}

	afb_req_success(request, answerJ, "Values correctly applied on alsa control");
}