aboutsummaryrefslogtreecommitdiffstats
path: root/src/4a-internals-hal/4a-internals-hal-mixer-link.c
blob: 19fb3a0e5cfc816a7dd55d82530dab8c43e11ef8 (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
/*
 * 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-hal-streams-handler.h"

#include "4a-internals-hal-mixer-link.h"
#include "4a-internals-hal-cb.h"

/*******************************************************************************
 *		Internals HAL handle mixer calls functions		       *
 ******************************************************************************/

int InternalHalHandleMixerData(afb_api_t apiHandle,
			       json_object *currentDataJ)
{
	int idx, mixerDataNb, verbStart, size;

	char *currentDataVerbName, *currentStreamCardId;

	json_type currentDataType;
	json_object *currentJ;

	currentDataType = json_object_get_type(currentDataJ);
	switch(currentDataType) {
		case json_type_object:
			mixerDataNb = 1;
			break;

		case json_type_array:
			mixerDataNb = (unsigned int) json_object_array_length(currentDataJ);
			break;

		default:
			mixerDataNb = 0;
			AFB_API_ERROR(apiHandle, "No data returned");
			return -1;
	}

	for(idx = 0; idx < mixerDataNb; idx++) {
		if(currentDataType == json_type_array)
			currentJ = json_object_array_get_idx(currentDataJ, (int) idx);
		else
			currentJ = currentDataJ;

		if(wrap_json_unpack(currentJ, "{s:s}", "verb", &currentDataVerbName)) {
			AFB_API_ERROR(apiHandle, "Can't find verb in current data object");
			return -2;
		}
		else if(wrap_json_unpack(currentJ, "{s:s}", "alsa", &currentStreamCardId)) {
			AFB_API_ERROR(apiHandle, "Can't find card id in current data object");
			return -3;
		}
		else {
			size = (int) strlen(currentDataVerbName);
			for(verbStart = 0; verbStart < size; verbStart++) {
				if(currentDataVerbName[verbStart] == '#') {
					verbStart++;
					break;
				}
			}

			if(verbStart == size)
				verbStart = 0;

			if(! HalUtlAddStreamDataAndCreateStreamVerb(apiHandle,
								    &currentDataVerbName[verbStart],
								    currentDataVerbName,
								    currentStreamCardId)) {
				AFB_API_ERROR(apiHandle,
					      "Error while adding stream '%s'",
					      currentDataVerbName);
				HalUtlRemoveAllStreamsDataAndDeleteAllStreamsVerb(apiHandle);
				return -4;
			}
		}
	}

	return 0;
}

int InternalHalHandleMixerAttachResponse(afb_api_t apiHandle, struct InternalHalData *currentHalSpecificData, json_object *mixerResponseJ)
{
	int err;

	json_object *mixerStreamsJ = NULL;

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

	if(wrap_json_unpack(mixerResponseJ,
			    "{s?:o}",
			    "streams", &mixerStreamsJ)) {
		AFB_API_ERROR(apiHandle,
			      "An error happened when tried to decode mixer response ('%s')",
			      json_object_get_string(mixerResponseJ));
		return -2;
	}

	if(mixerStreamsJ) {
		err = InternalHalHandleMixerData(apiHandle, mixerStreamsJ);
		if(err) {
			AFB_API_ERROR(apiHandle,
				      "Error %i during handling response mixer streams data '%s'",
				      err,
				      json_object_get_string(mixerStreamsJ));
			HalUtlRemoveAllMixerData(&currentHalSpecificData->streamsDataListHead);
			return -3;
		}
	}
	else {
		AFB_API_WARNING(apiHandle, "No stream was created by mixer ('%s')", json_object_get_string(mixerResponseJ));
		return 1;
	}

	return 0;
}

int InternalHalAttachDependencyToMixer(afb_api_t apiHandle, struct InternalHalProbedDevice *probedDeviceToAttach)
{
	int err = 0, mixerError;

	char *apiToCall, *returnedError = NULL, *returnedInfo = NULL;

	CtlConfigT *ctrlConfig;

	struct HalData *currentHalData;

	json_object *requestJ, *dependencyJ, *responseJ = NULL;

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

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

	currentHalData = (struct HalData *) getExternalData(ctrlConfig);
	if(! currentHalData) {
		AFB_API_ERROR(apiHandle, "Can't get current internal hal controller data");
		return -3;
	}

	switch(currentHalData->status) {
		case HAL_STATUS_INIT_FAILED:
			AFB_API_ERROR(apiHandle, "Seems that the hal initialization failed, will not be able to attach this hal");
			return -4;

		case HAL_STATUS_UNAVAILABLE:
			AFB_API_ERROR(apiHandle, "Seems that the hal corresponding card was not found by alsacore at startup");
			return -5;

		case HAL_STATUS_READY:
			AFB_API_NOTICE(apiHandle, "Seems that the hal mixer is already initialized");
			return 1;

		case HAL_STATUS_AVAILABLE:
			break;
	}

	apiToCall = currentHalData->internalHalData->mixerApiName;
	if(! apiToCall) {
		AFB_API_ERROR(apiHandle, "Can't get mixer api");
		return -6;
	}

	dependencyJ = HalUtlGetJsonArrayForAllDependenciesInfoWithHandledDependency(apiHandle,
										    &currentHalData->internalHalData->probedDevicesListHead,
										    DEPENDENCY_COMPACT_JSON,
										    probedDeviceToAttach->uid);
	if(! dependencyJ) {
		AFB_API_ERROR(apiHandle, "Didn't succeed to generate available dependencies compact json array");
		return -7;
	}

	requestJ = wrap_json_clone(currentHalData->internalHalData->halMixerJ);
	json_object_object_add(requestJ, "dependencies", dependencyJ);

	if(afb_api_call_sync(apiHandle,
			     apiToCall,
			     MIXER_ATTACH_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'",
			      MIXER_ATTACH_VERB,
			      apiToCall,
			      returnedError ? returnedError : "not returned",
			      returnedInfo ? returnedInfo : "not returned");
		err = -8;
	}
	else if(! responseJ) {
		AFB_API_WARNING(apiHandle,
				"Seems that %s call to api %s succeed but no response was returned",
				MIXER_ATTACH_VERB,
				apiToCall);
	}
	else {
		mixerError = InternalHalHandleMixerAttachResponse(apiHandle, currentHalData->internalHalData, responseJ);
		if(mixerError < 0) {
			AFB_API_ERROR(apiHandle,
				      "Seems that %s call to api %s succeed but this error was send by response decoder : %i '%s'",
				      MIXER_ATTACH_VERB,
				      apiToCall,
				      mixerError,
				      json_object_get_string(responseJ));
			err = -9;
		}
		else if(mixerError > 0) {
			AFB_API_WARNING(apiHandle,
					"Seems that %s call to api %s succeed but warning %i was rised by response decoder : '%s'",
					MIXER_ATTACH_VERB,
					apiToCall,
					mixerError,
					json_object_get_string(responseJ));
		}
		else {
			AFB_API_NOTICE(apiHandle,
				       "Seems that %s call to api %s succeed with no warning raised : '%s'",
				       MIXER_ATTACH_VERB,
				       apiToCall,
				       json_object_get_string(responseJ));
		}
	}

	if(err)
		probedDeviceToAttach->mixerLinkStatus = DEPENDENCY_MIXER_ATTACH_FAILED;
	else
		probedDeviceToAttach->mixerLinkStatus = DEPENDENCY_MIXER_ATTACH_SUCCEED;

	if(responseJ)
		json_object_put(responseJ);

	free(returnedError);
	free(returnedInfo);

	return err;
}

int InternalHalDetachDependencyToMixer(afb_api_t apiHandle, struct InternalHalProbedDevice *probedDeviceToDetach)
{
	int err, toReturn = 0;

	char *apiToCall, *returnedError = NULL, *returnedInfo = NULL;

	CtlConfigT *ctrlConfig;

	struct HalData *currentHalData;

	json_object *requestJ, *dependencyJ, *responseJ = NULL;

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

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

	currentHalData = (struct HalData *) getExternalData(ctrlConfig);
	if(! currentHalData) {
		AFB_API_ERROR(apiHandle, "Can't get current internal hal controller data");
		return -3;
	}

	apiToCall = currentHalData->internalHalData->mixerApiName;
	if(! apiToCall) {
		AFB_API_ERROR(apiHandle, "Can't get mixer api");
		return -4;
	}

	dependencyJ = HalUtlGetJsonForSpecificDependenciesUsingUid(apiHandle,
								   &currentHalData->internalHalData->probedDevicesListHead,
								   probedDeviceToDetach->uid,
								   DEPENDENCY_COMPACT_JSON);
	if(! dependencyJ) {
		AFB_API_ERROR(apiHandle, "Didn't succeed to get dependency %s compact json array", probedDeviceToDetach->uid);
		return -5;
	}

	err = wrap_json_pack(&requestJ, "{s:o}", "dependencies", dependencyJ);
	if(err) {
		AFB_API_ERROR(apiHandle,
			      "Didn't succeed to allocate detach dependency %s request json", probedDeviceToDetach->uid);
		return -6;
	}

	if(afb_api_call_sync(apiHandle,
			     apiToCall,
			     MIXER_DETACH_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'",
			      MIXER_DETACH_VERB,
			      apiToCall,
			      returnedError ? returnedError : "not returned",
			      returnedInfo ? returnedInfo : "not returned");
		toReturn = -7;
	}

	if(toReturn)
		probedDeviceToDetach->mixerLinkStatus = DEPENDENCY_MIXER_DETACH_FAILED;
	else
		probedDeviceToDetach->mixerLinkStatus = DEPENDENCY_MIXER_DETACH_SUCCEED;

	if(responseJ)
		json_object_put(responseJ);

	free(returnedError);
	free(returnedInfo);

	return toReturn;
}

int InternalHalGetInfoFromMixer(afb_api_t apiHandle,
				char *apiToCall,
				json_object *requestJson,
				json_object **toReturnJ,
				char **returnedError,
				char **returnedInfo)
{
	json_object *responseJ = NULL;

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

	if(! apiToCall) {
		AFB_API_ERROR(apiHandle, "Can't get mixer api");
		return -2;
	}

	if(! requestJson) {
		AFB_API_ERROR(apiHandle, "Can't get request json");
		return -3;
	}

	if(*returnedError || *returnedInfo) {
		AFB_API_ERROR(apiHandle, "'returnedError' and 'returnedInfo' strings should be empty and set to 'NULL'");
		return -4;
	}

	if(*toReturnJ) {
		AFB_API_ERROR(apiHandle, "'toReturnJ' should be empty and set to 'NULL'");
		return -5;
	}

	if(afb_api_call_sync(apiHandle,
			     apiToCall,
			     MIXER_INFO_VERB,
			     json_object_get(requestJson),
			     &responseJ,
			     returnedError,
			     returnedInfo)) {
		AFB_API_ERROR(apiHandle,
			      "Something went wrong during call to verb '%s' of api '%s' with error '%s' and info '%s'",
			      apiToCall,
			      MIXER_INFO_VERB,
			      *returnedError ? *returnedError : "not returned",
			      *returnedInfo ? *returnedInfo : "not returned");
		return -6;
	}
	else if(! responseJ) {
		AFB_API_ERROR(apiHandle,
			      "Seems that %s call to api %s succeed but no response was returned",
			      MIXER_INFO_VERB,
			      apiToCall);
		json_object_put(responseJ);
		return -7;
	}
	else {
		AFB_API_NOTICE(apiHandle,
			       "Seems that %s call to api %s succeed with no warning raised : '%s'",
			       MIXER_INFO_VERB,
			       apiToCall,
			       json_object_get_string(responseJ));
		*toReturnJ = responseJ;
	}

	return 0;
}