aboutsummaryrefslogtreecommitdiffstats
path: root/src/4a-internals-hal/4a-internals-hal-api-loader.c
blob: a8b62c68e016ab06b0f1eaa0a37b536e68c96add (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
/*
 * 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 <filescan-utils.h>
#include <wrap-json.h>

#include <afb/afb-binding.h>

#include <ctl-config.h>

#include "4a-hal-utilities-hal-api-handler.h"
#include "4a-hal-utilities-hal-streams-handler.h"
#include "4a-hal-utilities-ping.h"

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

/*******************************************************************************
 *		Json parsing functions using controller			       *
 ******************************************************************************/

// Config Section definition
static CtlSectionT ctrlSectionsDefault[] =
{
	{ .key = "haldependencies",	.loadCB = InternalHalHalDependenciesConfig },
	{ .key = "resources",		.loadCB = PluginConfig },
	{ .key = "halmixer",		.loadCB = InternalHalHalMixerConfig },
	{ .key = "onload",		.loadCB = OnloadConfig },
	{ .key = "controls",		.loadCB = ControlConfig },
	{ .key = "events",		.loadCB = EventConfig },
	{ .key = "halmap",		.loadCB = InternalHalHalMapConfig },
	{ .key = NULL }
};

/*******************************************************************************
 *		Dynamic HAL verbs' functions				       *
 ******************************************************************************/

// Every HAL export the same API & Interface Mapping from SndCard to AudioLogic is done through alsaHalSndCardT
static afb_verb_t InternalHalApiStaticVerbs[] =
{
	/* VERB'S NAME			FUNCTION TO CALL			SHORT DESCRIPTION */
	{ .verb = "ping",		.callback = HalUtlPing,			.info = "Ping for test"},
	{ .verb = "info",		.callback = InternalHalInfoVerb,	.info = "List available dependencies/streams/controls for this api" },
	{ .verb = "subscribe",		.callback = InternalHalSubscribe,	.info = "Subscribe to event(s) for values changes (streams/controls) for this api" },
	{ .verb = "unsubscribe",	.callback = InternalHalUnsubscribe,	.info = "Unsubscribe to event(s) for values changes (streams/controls) for this api" },
	{ .verb = HAL_ALL_STREAMS_VERB,	.callback = HalUtlActionOnAllStream,	.info = "Send a stream action on all streams" },
	{ .verb = NULL }		// Marker for end of the array
};

/*******************************************************************************
 *		Dynamic API functions for internals hal			       *
 ******************************************************************************/

static int InternalHalInitOneApi(afb_api_t apiHandle)
{
	int err;

	CtlConfigT *ctrlConfig;
	struct HalData *currentHalData;

	if(! apiHandle)
		return -1;

	// Retrieve section config from api handle
	ctrlConfig = (CtlConfigT *) afb_api_get_userdata(apiHandle);
	if(! ctrlConfig)
		return -2;

	currentHalData = (struct HalData *) getExternalData(ctrlConfig);
	if(! currentHalData)
		return -3;

	// Fill HalDatadata structure
	currentHalData->internal = 1;

	currentHalData->uid = (char *) ctrlConfig->uid;
	currentHalData->info = (char *) ctrlConfig->info;

	currentHalData->author = (char *) ctrlConfig->author;
	currentHalData->version = (char *) ctrlConfig->version;
	currentHalData->date = (char *) ctrlConfig->date;

	currentHalData->internalHalData->apiHandle = apiHandle;
	currentHalData->internalHalData->ctrlConfig = ctrlConfig;

	currentHalData->internalHalData->streamUpdates = afb_api_make_event(apiHandle, HAL_STREAM_UPDATES_EVENT_NAME);
	if(! currentHalData->internalHalData->streamUpdates)
		return -4;

	// TBD JAI: handle refresh of hal status for dynamic card (/dev/by-id)

	err = CtlConfigExec(apiHandle, ctrlConfig);
	if(err < 0) {
		AFB_API_ERROR(apiHandle, "Error %i caught when trying to apply current internal hal controller sections", err);
		currentHalData->status = HAL_STATUS_INIT_FAILED;
	}

	if(err > 0)
		AFB_API_WARNING(apiHandle, "Warning %i raised when trying to apply current internal hal controller sections", err);

	return 0;
}

static int InternalHalLoadOneApi(void *cbdata, afb_api_t apiHandle)
{
	int err;
	CtlConfigT *ctrlConfig;
	CtlSectionT *ctrlCurrentSections;

	if(! cbdata || ! apiHandle)
		return -1;

	ctrlConfig = (CtlConfigT*) cbdata;

	// Save closure as api's data context
	afb_api_set_userdata(apiHandle, ctrlConfig);

	// Add static internal hal verbs
	if(afb_api_set_verbs_v3(apiHandle, InternalHalApiStaticVerbs)) {
		AFB_API_ERROR(apiHandle, "Load Section : fail to register static V2 verbs");
		return -2;
	}

	ctrlCurrentSections = malloc(sizeof(ctrlSectionsDefault));
	if(! ctrlCurrentSections) {
		AFB_API_ERROR(apiHandle, "Didn't succeed to allocate current internal hal section data structure for controller");
		return -3;
	}

	memcpy(ctrlCurrentSections, ctrlSectionsDefault, sizeof(ctrlSectionsDefault));

	// Load section for corresponding Api
	err = CtlLoadSections(apiHandle, ctrlConfig, ctrlCurrentSections);
	if(err < 0) {
		AFB_API_ERROR(apiHandle, "Error %i caught when trying to load current internal hal controller sections", err);
		return -4;
	}

	if(err > 0)
		AFB_API_WARNING(apiHandle, "Warning %i raised when trying to load current internal hal controller sections", err);

	// Declare an event manager for this Api
	afb_api_on_event(apiHandle, InternalHalDispatchApiEvent);

	// Init Api function (does not receive user closure ???)
	afb_api_on_init(apiHandle, InternalHalInitOneApi);

	return 0;
}

int InternalHalCreateApi(afb_api_t apiHandle, char *path, struct HalMgrData *halMgrData)
{
	int err;
	CtlConfigT *ctrlConfig;

	if(! apiHandle || ! path || ! halMgrData)
		return -1;

	// Create one Api per file
	ctrlConfig = CtlLoadMetaData(apiHandle, path);
	if(! ctrlConfig) {
		AFB_API_ERROR(apiHandle, "No valid internal hal config file in : '%s'", path);
		return -2;
	}

	if(! ctrlConfig->api) {
		AFB_API_ERROR(apiHandle, "API Missing from metadata in : '%s'", path);
		return -3;
	}

	err = HalUtlAddHalDataAndCreateHalApi(apiHandle, halMgrData, ctrlConfig, InternalHalLoadOneApi);
	if(err) {
		AFB_API_ERROR(apiHandle, "Error %i happened while trying to add hal api data and create new api", err);
		return -4;
	}

	return 0;
}

int InternalHalDeleteApi(afb_api_t apiHandle, struct cds_list_head *halDataListHead, struct HalData *toDeleteHalData)
{
	int err;

	char *toDeleteApiName;

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

	err = InternalHalUnmanageAllHalMap(toDeleteHalData->internalHalData->apiHandle,
					   &toDeleteHalData->internalHalData->halMapListHead,
					   &toDeleteHalData->internalHalData->probedDevicesListHead);
	if(err) {
		AFB_API_ERROR(apiHandle,
			      "Error %i happened while trying to unmanage all halmap of '%s' hal api",
			      err,
			      toDeleteHalData->apiName);
		return -2;
	}

	toDeleteApiName = strdup(toDeleteHalData->apiName);
	if(! toDeleteApiName) {
		AFB_API_ERROR(apiHandle, "Didn't succeed to store (allocate) 'apiName' string");
		return -3;
	}

	err = HalUtlRemoveHalDataAndDeleteHalApi(toDeleteHalData->internalHalData->apiHandle,
						 toDeleteHalData,
						 halDataListHead);
	if(err) {
		AFB_API_ERROR(apiHandle,
			      "Error %i happened while trying to delete '%s' api and to remove all its data",
			      err,
			      toDeleteApiName);
		free(toDeleteApiName);
		return -4;
	}

	AFB_API_NOTICE(apiHandle,
		      "Api '%s' and all its data has been deleted",
		      toDeleteApiName);

	free(toDeleteApiName);

	return 0;
}

int InternalHalCreateAllApi(afb_api_t apiHandle, struct HalMgrData *halMgrData)
{
	int index;
	char *dirList, *fileName, *fullPath;
	char filePath[CONTROL_MAXPATH_LEN];

	filePath[CONTROL_MAXPATH_LEN - 1] = '\0';

	json_object *configJ, *entryJ;

	if(! apiHandle || ! halMgrData)
		return -1;

	AFB_API_NOTICE(apiHandle, "Begining to create all APIs");

	dirList = getenv("CONTROL_CONFIG_PATH");
	if(! dirList)
		dirList = CONTROL_CONFIG_PATH;

	configJ = CtlConfigScan(dirList, "hal");
	if(! configJ) {
		AFB_API_WARNING(apiHandle, "No hal-(binder-middle-name)*.json config file(s) found in %s, 4a-hal-manager will only works with external hal", dirList);
		return 0;
	}

	// We load 1st file others are just warnings
	for(index = 0; index < (int) json_object_array_length(configJ); index++) {
		entryJ = json_object_array_get_idx(configJ, index);

		if(wrap_json_unpack(entryJ, "{s:s, s:s !}", "fullpath", &fullPath, "filename", &fileName)) {
			AFB_API_ERROR(apiHandle, "HOOPs invalid JSON entry = %s", json_object_get_string(entryJ));
			return -2;
		}

		strncpy(filePath, fullPath, sizeof(filePath) - 1);
		strncat(filePath, "/", sizeof(filePath) - 1);
		strncat(filePath, fileName, sizeof(filePath) - 1);

		if(InternalHalCreateApi(apiHandle, filePath, halMgrData) < 0)
			return -3;
	}

	return 0;
}