summaryrefslogtreecommitdiffstats
path: root/lib/4a-hal-utilities/4a-hal-utilities-data.c
blob: 888516fad238bf9e18d6c7101325016e35e7e330 (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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
/*
 * 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 <afb/afb-binding.h>

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

/*******************************************************************************
 *	Internal Hal - Linked list cleaning function declaration	       *
 ******************************************************************************/

void HalUtlFreeSelectedProbedDeviceAllocation(struct InternalHalProbedDevice *probedDeviceToFree);
void HalUtlFreeSelectedMixerDataAllocation(struct InternalHalMixerData *mixerDataToFree);
void HalUtlFreeSelectedHalMapDataAllocation(struct InternalHalAlsaMap *halMapDataToFree);
void HalUtlFreeSelectedHalAllocation(struct HalData *halDataToFree);

/*******************************************************************************
 *	Internal Hal - Generic function for linked list			       *
 ******************************************************************************/

void *HalUtlAddNodeInList(struct cds_list_head *listHead, enum LinkedListType listType)
{
	void *returned = NULL;
	struct cds_list_head *nodeToAdd;

	if(! listHead)
		return NULL;

	switch(listType) {
		case LINKED_LIST_FOR_DEPENDENCIES_DATA:
			returned = calloc(1, sizeof(struct InternalHalProbedDevice));
			if(! returned)
				return NULL;
			nodeToAdd = &((struct InternalHalProbedDevice *) returned)->node;
			break;

		case LINKED_LIST_FOR_MIXER_DATA:
			returned = calloc(1, sizeof(struct InternalHalMixerData));
			if(! returned)
				return NULL;
			nodeToAdd = &((struct InternalHalMixerData *) returned)->node;
			break;

		case LINKED_LIST_FOR_HALMAP_DATA:
			returned = calloc(1, sizeof(struct InternalHalAlsaMap));
			if(! returned)
				return NULL;
			nodeToAdd = &((struct InternalHalAlsaMap *) returned)->node;
			((struct InternalHalAlsaMap *) returned)->cardNb = HAL_UNKNOWN_DEVICE;
			break;

		case LINKED_LIST_FOR_HAL_DATA:
			returned = calloc(1, sizeof(struct HalData));
			if(! returned)
				return NULL;
			nodeToAdd = &((struct HalData *) returned)->node;
			break;

		default:
			return NULL;
	}

	cds_list_add_tail(nodeToAdd, listHead);

	return returned;
}

int HalUtlRemoveNodeFromList(struct cds_list_head *listHead, void *dataToRemove, enum LinkedListType listType)
{
	if(! listHead || cds_list_empty(listHead) || ! dataToRemove)
		return -1;

	switch(listType) {
		case LINKED_LIST_FOR_DEPENDENCIES_DATA:
			cds_list_del(&((struct InternalHalProbedDevice *) dataToRemove)->node);

			HalUtlFreeSelectedProbedDeviceAllocation((struct InternalHalProbedDevice *) dataToRemove);
			break;

		case LINKED_LIST_FOR_MIXER_DATA:
			cds_list_del(&((struct InternalHalMixerData *) dataToRemove)->node);

			HalUtlFreeSelectedMixerDataAllocation((struct InternalHalMixerData *) dataToRemove);
			break;

		case LINKED_LIST_FOR_HALMAP_DATA:
			cds_list_del(&((struct InternalHalAlsaMap *) dataToRemove)->node);

			HalUtlFreeSelectedHalMapDataAllocation((struct InternalHalAlsaMap *) dataToRemove);
			break;

		case LINKED_LIST_FOR_HAL_DATA:
			cds_list_del(&((struct HalData *) dataToRemove)->node);

			HalUtlFreeSelectedHalAllocation((struct HalData *) dataToRemove);
			break;

		default:
			return -2;
	}

	return 0;
}

int HalUtlRemoveAllNodeFromList(struct cds_list_head *listHead, enum LinkedListType listType)
{
	int nodesRemoved = 0;

	struct cds_list_head *nodeToRemove, *savedNode;

	if(! listHead)
		return -1;

	if(cds_list_empty(listHead))
		return 0;

	cds_list_for_each_safe(nodeToRemove, savedNode, listHead) {
		switch(listType) {
			case LINKED_LIST_FOR_DEPENDENCIES_DATA:
				if(HalUtlRemoveSelectedProbedDeviceFromList(listHead,
									    cds_list_entry(nodeToRemove, struct InternalHalProbedDevice, node)))
					return -2;
				break;

			case LINKED_LIST_FOR_MIXER_DATA:
				if(HalUtlRemoveSelectedMixerData(listHead,
								 cds_list_entry(nodeToRemove, struct InternalHalMixerData, node)))
					return -3;
				break;

			case LINKED_LIST_FOR_HALMAP_DATA:
				if(HalUtlRemoveSelectedHalMapData(listHead,
								  cds_list_entry(nodeToRemove, struct InternalHalAlsaMap, node)))
					return -4;
				break;

			case LINKED_LIST_FOR_HAL_DATA:
				if(HalUtlRemoveSelectedHalFromList(listHead,
								   cds_list_entry(nodeToRemove, struct HalData, node)))
					return -5;
				break;

			default:
				return -6;
		}

		nodesRemoved++;
	}

	return nodesRemoved;
}

int HalUtlGetNumberOfNodesInList(struct cds_list_head *listHead)
{
	int numberOfNodes = 0;
	struct cds_list_head *listNode;

	if(! listHead)
		return -1;

	if(cds_list_empty(listHead))
		return 0;

	cds_list_for_each(listNode, listHead)
		numberOfNodes++;

	return numberOfNodes;
}

/*******************************************************************************
 *	Internal Hal - Probed devices structure handling functions	       *
 ******************************************************************************/

enum ProbedDeviceClasses HalUtlGetProbedDeviceClassFromString(char *probedDeviceString)
{
	if(! probedDeviceString)
		return INVALID_PROBED_DEVICE;

	if(! strcasecmp("static", probedDeviceString))
		return STATIC_PROBED_DEVICE;

	if(! strcasecmp("dynamic", probedDeviceString))
		return DYNAMIC_PROBED_DEVICE;

	if(! strcasecmp("mandatory", probedDeviceString))
		return MANDATORY_PROBED_DEVICE;

	return INVALID_PROBED_DEVICE;
}

char *HalUtlGetProbedDeviceClassString(enum ProbedDeviceClasses deviceClass)
{
	switch(deviceClass) {
		case STATIC_PROBED_DEVICE:
			return "static";

		case DYNAMIC_PROBED_DEVICE:
			return "dynamic";

		case MANDATORY_PROBED_DEVICE:
			return "mandatory";

		case INVALID_PROBED_DEVICE:
			return "invalid";

		default:
			return "unknown";
	}

	return NULL;
}

int HalUtlIsDependencySelected(struct InternalHalProbedDevice *probedDevice, enum DependencyStatus requestedStatus)
{
	if(! probedDevice) {
		return 0;
	}

	switch(requestedStatus) {
		case UNAVAILABLE_DEPENDENCY:
			if(! probedDevice->deviceData)
				return 1;
			else
				return 0;

		case AVAILABLE_DEPENDENCY:
			if(probedDevice->deviceData)
				return 1;
			else
				return 0;

		case ALL_DEPENDENCY:
			return 1;

		default:
			return 0;
	}
}

void HalUtlFreeSelectedProbedDeviceDataAllocation(struct InternalHalDeviceData *probedDeviceDataToFree)
{
	if(! probedDeviceDataToFree)
		return;

	free(probedDeviceDataToFree->extendedCardNb);
	free(probedDeviceDataToFree->cardId);
	free(probedDeviceDataToFree->cardShortName);
	free(probedDeviceDataToFree->cardLongName);
	free(probedDeviceDataToFree->cardDriver);
	free(probedDeviceDataToFree->cardMixerName);
	free(probedDeviceDataToFree->cardComponents);
	free(probedDeviceDataToFree->playbackDeviceId);
	free(probedDeviceDataToFree->playbackDeviceName);

	free(probedDeviceDataToFree);
}

void HalUtlFreeSelectedProbedDeviceAllocation(struct InternalHalProbedDevice *probedDeviceToFree)
{
	if(! probedDeviceToFree)
		return;

	free(probedDeviceToFree->uid);

	HalUtlFreeSelectedProbedDeviceDataAllocation(probedDeviceToFree->deviceData);

	if(probedDeviceToFree->requestedDeviceJ)
		json_object_put(probedDeviceToFree->requestedDeviceJ);

	free(probedDeviceToFree);
}

struct InternalHalProbedDevice *HalUtlAddProbedDeviceToProbedDeviceList(struct cds_list_head *probedDevicesListHead)
{
	return (struct InternalHalProbedDevice *) HalUtlAddNodeInList(probedDevicesListHead, LINKED_LIST_FOR_DEPENDENCIES_DATA);
}

int HalUtlRemoveSelectedProbedDeviceFromList(struct cds_list_head *probedDevicesListHead,
					     struct InternalHalProbedDevice *probedDeviceToRemove)
{
	return HalUtlRemoveNodeFromList(probedDevicesListHead, (void *) probedDeviceToRemove, LINKED_LIST_FOR_DEPENDENCIES_DATA);
}

int HalUtlRemoveAllProbedDevicesFromList(struct cds_list_head *probedDevicesListHead)
{
	return HalUtlRemoveAllNodeFromList(probedDevicesListHead, LINKED_LIST_FOR_DEPENDENCIES_DATA);
}

int HalUtlGetNumberOfProbedDevicesInList(struct cds_list_head *probedDevicesListHead)
{
	return HalUtlGetNumberOfNodesInList(probedDevicesListHead);
}

int HalUtlGetNumberOfDynamicProbedDevicesInList(struct cds_list_head *probedDevicesListHead)
{
	int numberOfDynamicProbedDevicesInList = 0;

	struct InternalHalProbedDevice *currentProbedDevice;

	if(! probedDevicesListHead)
		return -1;

	if(cds_list_empty(probedDevicesListHead))
		return 0;

	cds_list_for_each_entry(currentProbedDevice, probedDevicesListHead, node) {
		if(currentProbedDevice->deviceClass == DYNAMIC_PROBED_DEVICE)
			numberOfDynamicProbedDevicesInList++;
	}

	return numberOfDynamicProbedDevicesInList;
}

struct InternalHalProbedDevice *HalUtlSearchProbedDeviceDataById(struct cds_list_head *probedDevicesListHead,
								 char *uid)
{
	struct InternalHalProbedDevice *currentProbedDevice;

	if(! probedDevicesListHead || cds_list_empty(probedDevicesListHead) || ! uid)
		return NULL;

	cds_list_for_each_entry(currentProbedDevice, probedDevicesListHead, node) {
		if(! strcmp(uid, currentProbedDevice->uid))
			return currentProbedDevice;
	}

	return NULL;
}

int HalUtlGetProbedDeviceCardNbUsingUid(struct cds_list_head *probedDevicesListHead, char *uid)
{
	struct InternalHalProbedDevice *currentProbedDeviceData;

	currentProbedDeviceData = HalUtlSearchProbedDeviceDataById(probedDevicesListHead, uid);
	if(! currentProbedDeviceData ||
	   ! currentProbedDeviceData->deviceData)
		return HAL_UNKNOWN_DEVICE;

	return currentProbedDeviceData->deviceData->cardNb;
}

struct InternalHalDeviceData *HalUtlAllocateAndFillProbedDeviceDataUsingInfoGetResponse(json_object *responseJ)
{
	struct InternalHalDeviceData *currentProbedDeviceData;

	char *cardId,
	     *cardShortName,
	     *cardLongName,
	     *cardDriver,
	     *cardMixerName,
	     *cardComponents,
	     *playbackDeviceId = NULL,
	     *playbackDeviceName = NULL;

	if(! responseJ)
		return NULL;

	currentProbedDeviceData = calloc(1, sizeof(struct InternalHalDeviceData));
	if(! currentProbedDeviceData)
		return NULL;

	currentProbedDeviceData->cardNb = HAL_UNKNOWN_DEVICE;
	currentProbedDeviceData->playbackDeviceNb = HAL_UNKNOWN_DEVICE;

	if(wrap_json_unpack(responseJ, "{s:i, s:s, s:s, s:s, s:s, s:s, s:s, s?:i, s?:s, s?:s !}",
				       "cardNb", &currentProbedDeviceData->cardNb,
				       "cardId", &cardId,
				       "cardShortName", &cardShortName,
				       "cardLongName", &cardLongName,
				       "cardDriver", &cardDriver,
				       "cardMixerName", &cardMixerName,
				       "cardComponents", &cardComponents,
				       "playbackDeviceNb", &currentProbedDeviceData->playbackDeviceNb,
				       "playbackDeviceId", &playbackDeviceId,
				       "playbackDeviceName", &playbackDeviceName) ||
	   currentProbedDeviceData->cardNb == HAL_UNKNOWN_DEVICE ||
	   (currentProbedDeviceData->playbackDeviceNb == HAL_UNKNOWN_DEVICE &&
	    asprintf(&currentProbedDeviceData->extendedCardNb, "hw:%i", currentProbedDeviceData->cardNb) < 0) ||
	   (currentProbedDeviceData->playbackDeviceNb != HAL_UNKNOWN_DEVICE &&
	    asprintf(&currentProbedDeviceData->extendedCardNb, "hw:%i,%i",
							      currentProbedDeviceData->cardNb,
							      currentProbedDeviceData->playbackDeviceNb) < 0)) {
		HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData);
		return NULL;
	}

	currentProbedDeviceData->cardId = strdup(cardId);
	if(! currentProbedDeviceData->cardId) {
		HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData);
		return NULL;
	}

	currentProbedDeviceData->cardShortName = strdup(cardShortName);
	if(! currentProbedDeviceData->cardShortName) {
		HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData);
		return NULL;
	}

	currentProbedDeviceData->cardLongName = strdup(cardLongName);
	if(! currentProbedDeviceData->cardLongName) {
		HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData);
		return NULL;
	}

	currentProbedDeviceData->cardDriver = strdup(cardDriver);
	if(! currentProbedDeviceData->cardDriver) {
		HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData);
		return NULL;
	}

	currentProbedDeviceData->cardMixerName = strdup(cardMixerName);
	if(! currentProbedDeviceData->cardMixerName) {
		HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData);
		return NULL;
	}

	currentProbedDeviceData->cardComponents = strdup(cardComponents);
	if(! currentProbedDeviceData->cardComponents) {
		HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData);
		return NULL;
	}

	if(playbackDeviceId) {
		currentProbedDeviceData->playbackDeviceId = strdup(playbackDeviceId);
		if(! currentProbedDeviceData->playbackDeviceId) {
			HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData);
			return NULL;
		}
	}

	if(playbackDeviceName) {
		currentProbedDeviceData->playbackDeviceName = strdup(playbackDeviceName);
		if(! currentProbedDeviceData->playbackDeviceName) {
			HalUtlFreeSelectedProbedDeviceDataAllocation(currentProbedDeviceData);
			return NULL;
		}
	}

	return currentProbedDeviceData;
}

json_object *HalUtlGetCompactJsonForSpecificDependencies(afb_api_t apiHandle,
							 struct InternalHalProbedDevice *requestedProbedDevice)
{
	int wrapRet;

	char *cardNb;

	json_object *requestedProbedDeviceJ;

	if(! apiHandle) {
		AFB_API_ERROR(apiHandle, "Api handle is not valid");
		return NULL;
	}

	if(! requestedProbedDevice) {
		AFB_API_ERROR(apiHandle, "Requested dependency is not valid");
		return NULL;
	}

	if(! requestedProbedDevice->deviceData ||
	   requestedProbedDevice->deviceData->cardNb == HAL_UNKNOWN_DEVICE)
		cardNb = NULL;
	else
		cardNb = requestedProbedDevice->deviceData->extendedCardNb;

	wrapRet = wrap_json_pack(&requestedProbedDeviceJ,
				 "{s:s, s:b, s:s}",
				 "uid", requestedProbedDevice->uid,
				 "available", cardNb ? 1 : 0,
				 "cardNb", cardNb ? cardNb : "none");
	if(wrapRet) {
		AFB_API_ERROR(apiHandle,
			      "Didn't succeed to allocate (uid: '%s') dependency compact json object",
			      requestedProbedDevice->uid);
		return NULL;
	}

	return requestedProbedDeviceJ;
}

json_object *HalUtlGetFullJsonForSpecificDependencies(afb_api_t apiHandle,
						      struct InternalHalProbedDevice *requestedProbedDevice)
{
	int wrapRet;

	json_object *requestedProbedDeviceJ, *additionalInfoJ;

	if(! apiHandle) {
		AFB_API_ERROR(apiHandle, "Api handle is not valid");
		return NULL;
	}

	if(! requestedProbedDevice) {
		AFB_API_ERROR(apiHandle, "Requested dependency is not valid");
		return NULL;
	}

	wrapRet = wrap_json_pack(&requestedProbedDeviceJ,
				 "{s:s s:s s:b}",
				 "uid", requestedProbedDevice->uid,
				 "class", HalUtlGetProbedDeviceClassString(requestedProbedDevice->deviceClass),
				 "available", requestedProbedDevice->deviceData ? 1 : 0);
	if(wrapRet) {
		AFB_API_ERROR(apiHandle,
			      "Didn't succeed to allocate first part of requested (uid: '%s') dependency full json object",
			      requestedProbedDevice->uid);
		return NULL;
	}

	if(! requestedProbedDevice->deviceData ||
	   requestedProbedDevice->deviceData->cardNb == HAL_UNKNOWN_DEVICE)
		return requestedProbedDeviceJ;

	wrapRet = wrap_json_pack(&additionalInfoJ,
				 "{s:i s:s s:s s:s s:s s:s s:s s:s}",
				 "cardNb", requestedProbedDevice->deviceData->cardNb,
				 "cardId", requestedProbedDevice->deviceData->cardId,
				 "cardShortName", requestedProbedDevice->deviceData->cardShortName,
				 "cardLongName", requestedProbedDevice->deviceData->cardLongName,
				 "cardDriver", requestedProbedDevice->deviceData->cardDriver,
				 "cardMixerName", requestedProbedDevice->deviceData->cardMixerName,
				 "cardComponents", requestedProbedDevice->deviceData->cardComponents,
				 "extendedCardNb", requestedProbedDevice->deviceData->extendedCardNb);
	if(wrapRet) {
		AFB_API_ERROR(apiHandle,
			      "Didn't succeed to allocate second part of requested (uid: '%s') dependency full json object",
			      requestedProbedDevice->uid);
		json_object_put(requestedProbedDeviceJ);
		return NULL;
	}

	wrap_json_object_add(requestedProbedDeviceJ, additionalInfoJ);

	if(requestedProbedDevice->deviceData->playbackDeviceNb == HAL_UNKNOWN_DEVICE)
		return requestedProbedDeviceJ;

	wrapRet = wrap_json_pack(&additionalInfoJ,
				 "{s:i s:s s:s}",
				 "playbackDeviceNb", requestedProbedDevice->deviceData->playbackDeviceNb,
				 "playbackDeviceId", requestedProbedDevice->deviceData->playbackDeviceId,
				 "playbackDeviceName", requestedProbedDevice->deviceData->playbackDeviceName);
	if(wrapRet) {
		AFB_API_ERROR(apiHandle,
			      "Didn't succeed to allocate third part of requested (uid: '%s') dependency full json object",
			      requestedProbedDevice->uid);
		json_object_put(requestedProbedDeviceJ);
		return NULL;
	}

	wrap_json_object_add(requestedProbedDeviceJ, additionalInfoJ);

	return requestedProbedDeviceJ;
}

json_object *HalUtlGetJsonForSpecificDependencies(afb_api_t apiHandle,
						  struct InternalHalProbedDevice *requestedProbedDevice,
						  enum DependencyInfoJsonFormat jsonFormat)
{
	json_object *requestedProbedDeviceJ;

	if(! apiHandle) {
		AFB_API_ERROR(apiHandle, "Api handle is not valid");
		return NULL;
	}

	if(! requestedProbedDevice) {
		AFB_API_ERROR(apiHandle, "Requested dependency is not valid");
		return NULL;
	}

	switch(jsonFormat) {
		case DEPENDENCY_COMPACT_JSON:
			requestedProbedDeviceJ = HalUtlGetCompactJsonForSpecificDependencies(apiHandle,requestedProbedDevice);
			if(! requestedProbedDeviceJ) {
				AFB_API_ERROR(apiHandle,
					      "An error happened when generating compact json for selected dependency (uid: '%s')",
					      requestedProbedDevice->uid);
				return NULL;
			}
			return requestedProbedDeviceJ;

		case DEPENDENCY_FULL_JSON:
			requestedProbedDeviceJ = HalUtlGetFullJsonForSpecificDependencies(apiHandle,requestedProbedDevice);
			if(! requestedProbedDeviceJ) {
				AFB_API_ERROR(apiHandle,
					      "An error happened when generating full json for selected dependency (uid: '%s')",
					      requestedProbedDevice->uid);
				return NULL;
			}
			return requestedProbedDeviceJ;

		default:
			AFB_API_ERROR(apiHandle, "Unrecognized requested json format");
			return NULL;
	}
}

json_object *HalUtlGetJsonForSpecificDependenciesUsingUid(afb_api_t apiHandle,
							  struct cds_list_head *probedDevicesListHead,
							  char *uid,
							  enum DependencyInfoJsonFormat jsonFormat)
{
	json_object *requestedProbedDeviceJ;

	struct InternalHalProbedDevice *requestedProbedDevice;

	if(! apiHandle) {
		AFB_API_ERROR(apiHandle, "Api handle is not valid");
		return NULL;
	}

	if(! probedDevicesListHead) {
		AFB_API_ERROR(apiHandle, "Probed device list is not valid");
		return NULL;
	}

	if(cds_list_empty(probedDevicesListHead)) {
		AFB_API_INFO(apiHandle, "Probed device list is empty");
		return NULL;
	}

	if(! uid) {
		AFB_API_ERROR(apiHandle, "Requested dependency uid is not valid");
		return NULL;
	}

	requestedProbedDevice = HalUtlSearchProbedDeviceDataById(probedDevicesListHead, uid);
	if(! requestedProbedDevice) {
		AFB_API_ERROR(apiHandle,
			      "Requested dependency uid ('%s) was not found in probed device list",
			      uid);
		return NULL;
	}

	requestedProbedDeviceJ = HalUtlGetJsonForSpecificDependencies(apiHandle, requestedProbedDevice, jsonFormat);
	if(! requestedProbedDeviceJ) {
		AFB_API_ERROR(apiHandle,
			      "An error happened when generating json for selected dependency (uid: '%s')",
			      requestedProbedDevice->uid);
		return NULL;
	}

	return requestedProbedDeviceJ;
}

json_object *HalUtlGetJsonArrayForSelectedDependencies(afb_api_t apiHandle,
						       struct cds_list_head *probedDevicesListHead,
						       enum DependencyInfoJsonFormat jsonFormat,
						       enum DependencyStatus requestedStatus)
{
	json_object *requestedProbedDeviceJ, *requestedDependenciesInfoJ;

	struct InternalHalProbedDevice *currentProbedDevice;

	if(! apiHandle) {
		AFB_API_ERROR(apiHandle, "Api handle is not valid");
		return NULL;
	}

	if(! probedDevicesListHead || cds_list_empty(probedDevicesListHead)) {
		AFB_API_ERROR(apiHandle, "Probed device list is not valid or empty");
		return NULL;
	}

	requestedDependenciesInfoJ = json_object_new_array();
	if(! requestedDependenciesInfoJ) {
		AFB_API_ERROR(apiHandle, "Didn't succeed to allocate probed devices (dependencies) json array");
		return NULL;
	}

	cds_list_for_each_entry(currentProbedDevice, probedDevicesListHead, node) {
		requestedProbedDeviceJ = NULL;

		if(HalUtlIsDependencySelected(currentProbedDevice, requestedStatus)) {
			requestedProbedDeviceJ = HalUtlGetJsonForSpecificDependencies(apiHandle,
										      currentProbedDevice,
										      jsonFormat);
			if(! requestedProbedDeviceJ) {
				AFB_API_ERROR(apiHandle,
					      "Didn't succeed to get json for probed devices (dependencies) with uid : '%s'",
					      currentProbedDevice->uid);
				json_object_put(requestedDependenciesInfoJ);
				return NULL;
			}
		}

		if(requestedProbedDeviceJ)
			json_object_array_add(requestedDependenciesInfoJ, requestedProbedDeviceJ);
	}

	return requestedDependenciesInfoJ;
}

json_object *HalUtlGetJsonArrayForAvailableDependencies(afb_api_t apiHandle,
							struct cds_list_head *probedDevicesListHead,
							enum DependencyInfoJsonFormat jsonFormat)
{
	json_object *requestedDependenciesInfoJ;

	if(! apiHandle) {
		AFB_API_ERROR(apiHandle, "Api handle is not valid");
		return NULL;
	}

	if(! probedDevicesListHead) {
		AFB_API_ERROR(apiHandle, "Probed device list is not valid");
		return NULL;
	}

	if(cds_list_empty(probedDevicesListHead)) {
		AFB_API_INFO(apiHandle, "Probed device list is empty");
		return NULL;
	}

	requestedDependenciesInfoJ = HalUtlGetJsonArrayForSelectedDependencies(apiHandle,
									       probedDevicesListHead,
									       jsonFormat,
									       AVAILABLE_DEPENDENCY);
	if(! requestedDependenciesInfoJ) {
		AFB_API_ERROR(apiHandle, "Didn't succeed get available devices (dependencies) info");
		return NULL;
	}

	if(! json_object_array_length(requestedDependenciesInfoJ)) {
		AFB_API_INFO(apiHandle, "No available devices (dependencies) in list");
		json_object_put(requestedDependenciesInfoJ);
		return NULL;
	}

	return requestedDependenciesInfoJ;
}

json_object *HalUtlGetJsonArrayForAllDependencies(afb_api_t apiHandle,
						  struct cds_list_head *probedDevicesListHead,
						  enum DependencyInfoJsonFormat jsonFormat)
{
	json_object *requestedDependenciesInfoJ;

	if(! apiHandle) {
		AFB_API_ERROR(apiHandle, "Api handle is not valid");
		return NULL;
	}

	if(! probedDevicesListHead) {
		AFB_API_ERROR(apiHandle, "Probed device list is not valid");
		return NULL;
	}

	if(cds_list_empty(probedDevicesListHead)) {
		AFB_API_INFO(apiHandle, "Probed device list is empty");
		return NULL;
	}

	requestedDependenciesInfoJ = HalUtlGetJsonArrayForSelectedDependencies(apiHandle,
									       probedDevicesListHead,
									       jsonFormat,
									       ALL_DEPENDENCY);
	if(! requestedDependenciesInfoJ) {
		AFB_API_ERROR(apiHandle, "Didn't succeed get all devices (dependencies) info");
		return NULL;
	}

	if(! json_object_array_length(requestedDependenciesInfoJ)) {
		AFB_API_ERROR(apiHandle, "No devices (dependencies) info returned but list is not empty");
		json_object_put(requestedDependenciesInfoJ);
		return NULL;
	}

	return requestedDependenciesInfoJ;
}

/*******************************************************************************
 *	Internal Hal - Streams data handling functions			       *
 ******************************************************************************/

void HalUtlFreeSelectedMixerDataAllocation(struct InternalHalMixerData *mixerDataToFree)
{
	if(! mixerDataToFree)
		return;

	free(mixerDataToFree->verb);
	free(mixerDataToFree->verbToCall);
	free(mixerDataToFree->streamCardId);

	free(mixerDataToFree);
}

struct InternalHalMixerData *HalUtlAddMixerDataToMixerDataList(struct cds_list_head *mixerDataListHead)
{
	return (struct InternalHalMixerData *) HalUtlAddNodeInList(mixerDataListHead, LINKED_LIST_FOR_MIXER_DATA);
}

int HalUtlRemoveSelectedMixerData(struct cds_list_head *mixerDataListHead,
				  struct InternalHalMixerData *mixerDataToRemove)
{
	return HalUtlRemoveNodeFromList(mixerDataListHead, (void *) mixerDataToRemove, LINKED_LIST_FOR_MIXER_DATA);
}

int HalUtlRemoveAllMixerData(struct cds_list_head *mixerDataListHead)
{
	return HalUtlRemoveAllNodeFromList(mixerDataListHead, LINKED_LIST_FOR_MIXER_DATA);
}

int HalUtlGetNumberOfMixerDataInList(struct cds_list_head *mixerDataListHead)
{
	return HalUtlGetNumberOfNodesInList(mixerDataListHead);
}

struct InternalHalMixerData *HalUtlSearchMixerDataByProperties(struct cds_list_head *mixerDataListHead,
							       char *verb,
							       char *verbToCall,
							       char *streamCardId)
{
	struct InternalHalMixerData *currentMixerData;

	if(! mixerDataListHead || cds_list_empty(mixerDataListHead) || ! verb || ! verbToCall)
		return NULL;

	cds_list_for_each_entry(currentMixerData, mixerDataListHead, node) {
		if(! strcmp(verb, currentMixerData->verb) &&
		   ! strcmp(verbToCall, currentMixerData->verbToCall) &&
		   ((! streamCardId && ! currentMixerData->streamCardId) ||
		    (streamCardId && currentMixerData->streamCardId && ! strcmp(streamCardId, currentMixerData->streamCardId))))
			return currentMixerData;
	}

	return NULL;
}

json_object *HalUtlGetJsonArrayForSpecificMixerData(afb_api_t apiHandle, struct InternalHalMixerData *mixerData)
{
	int wrapRet;

	json_object *currentMixerDataJ;

	if(! apiHandle) {
		AFB_API_ERROR(apiHandle, "Api handle is not valid");
		return NULL;
	}

	if(! mixerData) {
		AFB_API_ERROR(apiHandle, "Mixer data to use to generate json object are empty");
		return NULL;
	}

	wrapRet = wrap_json_pack(&currentMixerDataJ,
				 "{s:s s:s}",
				 "name", mixerData->verb,
				 "cardId", mixerData->streamCardId);
	if(wrapRet) {
		AFB_API_ERROR(apiHandle, "Didn't succeed to allocate current mixer json object");
		return NULL;
	}

	return currentMixerDataJ;
}

json_object *HalUtlGetJsonArrayForAllMixersData(afb_api_t apiHandle, struct cds_list_head *mixerDataListHead)
{
	json_object *mixerDataArrayJ, *currentMixerDataJ;

	struct InternalHalMixerData *currentMixerData;

	if(! apiHandle) {
		AFB_API_ERROR(apiHandle, "Api handle is not valid");
		return NULL;
	}

	if(cds_list_empty(mixerDataListHead)) {
		AFB_API_INFO(apiHandle, "Mixer data list is empty");
		return NULL;
	}

	mixerDataArrayJ = json_object_new_array();
	if(! mixerDataArrayJ) {
		AFB_API_ERROR(apiHandle, "Didn't succeed to allocate requested mixer data json array");
		return NULL;
	}

	cds_list_for_each_entry(currentMixerData, mixerDataListHead, node) {
		currentMixerDataJ = HalUtlGetJsonArrayForSpecificMixerData(apiHandle, currentMixerData);
		if(! currentMixerDataJ) {
			AFB_API_ERROR(apiHandle, "Didn't succeed to generate current mixer data json object");
			json_object_put(mixerDataArrayJ);
			return NULL;
		}

		json_object_array_add(mixerDataArrayJ, currentMixerDataJ);
	}

	if(! json_object_array_length(mixerDataArrayJ)) {
		AFB_API_ERROR(apiHandle, "No mixer info returned but list is not empty");
		json_object_put(mixerDataArrayJ);
		return NULL;
	}

	return mixerDataArrayJ;
}

/*******************************************************************************
 *	Internal Hal - Alsa Map data handling functions			       *
 ******************************************************************************/

void HalUtlFreeSelectedHalMapDataAllocation(struct InternalHalAlsaMap *halMapDataToFree)
{
	free(halMapDataToFree->targetUid);
	free(halMapDataToFree->targetInfo);

	free(halMapDataToFree->targetedDependency);

	free(halMapDataToFree->uid);
	free(halMapDataToFree->info);

	free(halMapDataToFree->action);

	if(halMapDataToFree->ctl.name != halMapDataToFree->uid)
		free(halMapDataToFree->ctl.name);

	if(halMapDataToFree->ctl.alsaCtlCreation) {
		free(halMapDataToFree->ctl.alsaCtlCreation->enums);
		free(halMapDataToFree->ctl.alsaCtlCreation->dbscale);
		free(halMapDataToFree->ctl.alsaCtlCreation);
	}
	else if(halMapDataToFree->ctl.alsaCtlProperties) {
		free(halMapDataToFree->ctl.alsaCtlProperties->enums);
		free(halMapDataToFree->ctl.alsaCtlProperties->dbscale);
		free(halMapDataToFree->ctl.alsaCtlProperties);
	}

	free(halMapDataToFree);
}

struct InternalHalAlsaMap *HalUtlAddHalMapDataToHalMapDataList(struct cds_list_head *halMapListHead)
{
	return (struct InternalHalAlsaMap *) HalUtlAddNodeInList(halMapListHead, LINKED_LIST_FOR_HALMAP_DATA);
}

int HalUtlRemoveSelectedHalMapData(struct cds_list_head *halMapListHead,
				   struct InternalHalAlsaMap *halMapDataToRemove)
{
	return HalUtlRemoveNodeFromList(halMapListHead, (void *) halMapDataToRemove, LINKED_LIST_FOR_HALMAP_DATA);
}

int HalUtlRemoveAllHalMapData(struct cds_list_head *halMapListHead)
{
	return HalUtlRemoveAllNodeFromList(halMapListHead, LINKED_LIST_FOR_HALMAP_DATA);
}

int HalUtlGetNumberOfHalMapDataInList(struct cds_list_head *halMapListHead)
{
	return HalUtlGetNumberOfNodesInList(halMapListHead);
}

json_object *HalUtGetJsonArrayForSpecificHalMapControl(afb_api_t apiHandle,
						       struct InternalHalAlsaMap *currentHalMapData)
{
	int wrapRet;

	json_object *currentAlsaMapInfoJ, *additionalInfoJ;

	if(! apiHandle) {
		AFB_API_ERROR(apiHandle, "Can't get current internal hal api handle");
		return NULL;
	}

	if(! currentHalMapData) {
		AFB_API_ERROR(apiHandle, "ALSA control data to use to generate json object are empty");
		return NULL;
	}

	wrapRet = wrap_json_pack(&currentAlsaMapInfoJ,
				 "{s:s s:s s:s}",
				 "name", currentHalMapData->uid,
				 "info", currentHalMapData->info ? currentHalMapData->info : "none",
				 "target", currentHalMapData->targetedDependency);
	if(wrapRet) {
		AFB_API_ERROR(apiHandle, "Didn't succeed to allocate current halmap control first part of info json object");
		return NULL;
	}

	if(currentHalMapData->cardNb == HAL_UNKNOWN_DEVICE ||
	   ! currentHalMapData->ctl.alsaCtlProperties) {
		json_object_object_add(currentAlsaMapInfoJ, "available", json_object_new_boolean(0));
	}
	else {
		wrapRet = wrap_json_pack(&additionalInfoJ,
					 "{s:b s:i s:s}",
					 "available", 1,
					 "cardNb", currentHalMapData->cardNb,
					 "cardControlName", currentHalMapData->ctl.name ? currentHalMapData->ctl.name : "'N/A'");
		if(wrapRet) {
			AFB_API_ERROR(apiHandle, "Didn't succeed to allocate current halmap control second part of info json object");
			json_object_put(currentAlsaMapInfoJ);
			return NULL;
		}

		wrap_json_object_add(currentAlsaMapInfoJ, additionalInfoJ);
	}

	wrapRet = wrap_json_pack(&additionalInfoJ,
				"{s:s s:s}",
				"halmap-uid", currentHalMapData->targetUid,
				"halmap-info", currentHalMapData->targetInfo ? currentHalMapData->targetInfo : "none");
	if(wrapRet) {
		AFB_API_ERROR(apiHandle, "Didn't succeed to allocate current halmap control third part of info json object");
		json_object_put(currentAlsaMapInfoJ);
		return NULL;
	}

	wrap_json_object_add(currentAlsaMapInfoJ, additionalInfoJ);

	return currentAlsaMapInfoJ;
}

json_object *HalUtGetJsonArrayForAllHalMapControls(afb_api_t apiHandle, struct cds_list_head *halMapListHead)
{
	struct InternalHalAlsaMap *currentHalMapData;

	json_object *halMapDataArrayJ, *currentHalMapDataJ;

	if(! apiHandle) {
		AFB_API_ERROR(apiHandle, "Can't get current internal hal api handle");
		return NULL;
	}

	if(! halMapListHead) {
		AFB_API_ERROR(apiHandle, "HalMap data list is not valid");
		return NULL;
	}

	if(cds_list_empty(halMapListHead)) {
		AFB_API_INFO(apiHandle, "HalMap data list is empty");
		return NULL;
	}

	halMapDataArrayJ = json_object_new_array();
	if(! halMapDataArrayJ) {
		AFB_API_ERROR(apiHandle, "Didn't succeed to allocate ALSA controls data json array");
		return NULL;
	}

	cds_list_for_each_entry(currentHalMapData, halMapListHead, node) {
		currentHalMapDataJ = HalUtGetJsonArrayForSpecificHalMapControl(apiHandle, currentHalMapData);
		if(! currentHalMapDataJ) {
			AFB_API_ERROR(apiHandle, "Didn't succeed to generate current HalMap data json object");
			json_object_put(halMapDataArrayJ);
			return NULL;
		}

		json_object_array_add(halMapDataArrayJ, currentHalMapDataJ);
	}

	if(! json_object_array_length(halMapDataArrayJ)) {
		AFB_API_ERROR(apiHandle, "No HalMap info returned but list is not empty");
		json_object_put(halMapDataArrayJ);
		return NULL;
	}

	return halMapDataArrayJ;
}

/*******************************************************************************
 *	Hal data handling functions					       *
 ******************************************************************************/

void HalUtlFreeSelectedHalAllocation(struct HalData *halDataToFree)
{
	if(! halDataToFree)
		return;

	if(halDataToFree->internal) {
		free(halDataToFree->internalHalData->mixerApiName);
		free(halDataToFree->internalHalData->prefix);

		HalUtlRemoveAllProbedDevicesFromList(&halDataToFree->internalHalData->probedDevicesListHead);

		HalUtlRemoveAllMixerData(&halDataToFree->internalHalData->streamsDataListHead);

		HalUtlRemoveAllHalMapData(&halDataToFree->internalHalData->halMapListHead);

		free(halDataToFree->internalHalData);
	}

	free(halDataToFree);
}

struct HalData *HalUtlAddHalToHalList(struct cds_list_head *halDataListHead)
{
	return (struct HalData *) HalUtlAddNodeInList(halDataListHead, LINKED_LIST_FOR_HAL_DATA);
}

int HalUtlRemoveSelectedHalFromList(struct cds_list_head *halDataListHead, struct HalData *halToRemove)
{
	return HalUtlRemoveNodeFromList(halDataListHead, (void *) halToRemove, LINKED_LIST_FOR_HAL_DATA);
}

int HalUtlRemoveAllHalFromList(struct cds_list_head *halDataListHead)
{
	return HalUtlRemoveAllNodeFromList(halDataListHead, LINKED_LIST_FOR_HAL_DATA);
}

int HalUtlGetNumberOfHalInList(struct cds_list_head *halDataListHead)
{
	return HalUtlGetNumberOfNodesInList(halDataListHead);
}

struct HalData *HalUtlSearchHalDataByApiName(struct cds_list_head *halDataListHead, char *apiName)
{
	struct HalData *currentHalData;

	if(! halDataListHead || cds_list_empty(halDataListHead) || ! apiName)
		return NULL;

	cds_list_for_each_entry(currentHalData, halDataListHead, node) {
		if(! strcmp(apiName, currentHalData->apiName))
			return currentHalData;
	}

	return NULL;
}

/*******************************************************************************
 *	Hal Manager data handling functions				       *
 ******************************************************************************/

int HalUtlInitializeHalMgrData(afb_api_t apiHandle, struct HalMgrData *halMgrData, char *apiName, char *info)
{
	if(! apiHandle || ! halMgrData || ! apiName || ! info)
		return -1;

	// Allocate and fill apiName and info strings
	halMgrData->apiName = strdup(apiName);
	if(! halMgrData->apiName)
		return -2;

	halMgrData->info = strdup(info);
	if(! halMgrData->info)
		return -3;

	halMgrData->apiHandle = apiHandle;

	return 0;
}

void HalUtlRemoveHalMgrData(struct HalMgrData *halMgrData)
{
	if(! halMgrData)
		return;

	HalUtlRemoveAllHalFromList(&halMgrData->halDataListHead);

	free(halMgrData->apiName);
	free(halMgrData->info);

	free(halMgrData);
}