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
|
/*
* 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 <stdbool.h>
#include <wrap-json.h>
#include <ctl-config.h>
#include "hal-bt.h"
#include "hal-bt-cb.h"
#include "hal-bt-data.h"
#include "hal-bt-mixer-link.h"
// Local (static) Hal manager data structure
static struct HalBtPluginData localHalBtPluginData;
CTLP_CAPI_REGISTER(HAL_BT_PLUGIN_NAME)
// Call at initialisation time
CTLP_ONLOAD(plugin, callbacks)
{
AFB_ApiNotice(plugin->api, "%s Plugin Registered correctly: uid='%s' 'info='%s'", HAL_BT_PLUGIN_NAME, plugin->uid, plugin->info);
return 0;
}
CTLP_INIT(plugin, callbacks)
{
int btChannelsNumber;
unsigned int idx;
char *btStreamZone, *returnedInfo;
CtlConfigT *ctrlConfig;
struct SpecificHalData *currentHalData;
json_object *actionsToAdd, *returnedJ, *halMixerJ, *halOrigCaptureJ, *halNewCaptureJ, *halOrigStreamJ, *halNewStreamJ, *btCaptureJ, *btCaptureParamsJ, *btStreamJ;
AFB_ApiInfo(plugin->api, "Plugin initialization of %s plugin", HAL_BT_PLUGIN_NAME);
if(AFB_RequireApi(plugin->api, BT_MANAGER_API, 1)) {
AFB_ApiWarning(plugin->api, "Didn't succeed to require %s api, bluetooth is disable because not reachable", BT_MANAGER_API);
return 0;
}
if(! (ctrlConfig = (CtlConfigT *) afb_dynapi_get_userdata(plugin->api))) {
AFB_ApiError(plugin->api, "Can't get current hal controller config");
return -1;
}
if(! (currentHalData = (struct SpecificHalData *) ctrlConfig->external)) {
AFB_ApiError(plugin->api, "Can't get current hal controller data");
return -2;
}
if((! currentHalData->ctlHalSpecificData) ||
(! (halMixerJ = currentHalData->ctlHalSpecificData->halMixerJ))) {
AFB_ApiError(plugin->api, "Can't get current hal mixer json section");
return -3;
}
if(AFB_ServiceSync(plugin->api, BT_MANAGER_API, BT_MANAGER_GET_POWER_INFO, NULL, &returnedJ)) {
if((! wrap_json_unpack(returnedJ, "{s:{s:s}}", "request", "info", &returnedInfo)) &&
(! strncmp(returnedInfo, "Unable to get power status", strlen(returnedInfo)))) {
AFB_ApiWarning(plugin->api,
"No bluetooth receiver detected when calling verb '%s' of '%s' api, bluetooth is disable because not reachable",
BT_MANAGER_GET_POWER_INFO,
BT_MANAGER_API);
return 0;
}
else {
AFB_ApiError(plugin->api,
"Error during call to verb '%s' of '%s' api (%s)",
BT_MANAGER_GET_POWER_INFO,
BT_MANAGER_API,
json_object_get_string(returnedJ));
return -4;
}
}
if((! json_object_is_type(plugin->paramsJ, json_type_object)) ||
(wrap_json_unpack(plugin->paramsJ, "{s:i, s:s}", "channels", &btChannelsNumber, "zone", &btStreamZone))) {
AFB_ApiError(plugin->api, "Can't get %s plugin parameters from json ('%s')", HAL_BT_PLUGIN_NAME, json_object_get_string(plugin->paramsJ));
return -5;
}
wrap_json_pack(&actionsToAdd, "{s:s s:s}",
"uid", "Bluetooth-Manager/device_updated",
"action", "plugin://hal-bt#events");
if(currentHalData->status != HAL_STATUS_AVAILABLE) {
AFB_ApiWarning(plugin->api, "Controller initialization of %s plugin cannot be done because hal is not ready to be used", HAL_BT_PLUGIN_NAME);
return 0;
}
memset(&localHalBtPluginData, '\0', sizeof(localHalBtPluginData));
localHalBtPluginData.currentHalApiHandle = plugin->api;
localHalBtPluginData.currentHalData = currentHalData;
idx = 0;
while(ctrlConfig->sections[idx].key && strcasecmp(ctrlConfig->sections[idx].key, "events"))
idx++;
if(! ctrlConfig->sections[idx].key) {
AFB_ApiError(plugin->api, "Wasn't able to add '%s' as a new event, 'events' section not found", json_object_get_string(actionsToAdd));
json_object_put(actionsToAdd);
return -6;
}
if(AddActionsToSectionFromPlugin(plugin->api, *ctrlConfig->ctlPlugins, &ctrlConfig->sections[idx], actionsToAdd, 0)) {
AFB_ApiError(plugin->api, "Wasn't able to add '%s' as a new event to %s", json_object_get_string(actionsToAdd), ctrlConfig->sections[idx].key);
json_object_put(actionsToAdd);
return -7;
}
wrap_json_pack(&actionsToAdd, "{s:s s:s s:s}",
"uid", "init-bt-plugin",
"info", "Init Bluetooth hal plugin",
"action", "plugin://hal-bt#init");
idx = 0;
while(ctrlConfig->sections[idx].key && strcasecmp(ctrlConfig->sections[idx].key, "onload"))
idx++;
if(! ctrlConfig->sections[idx].key) {
AFB_ApiError(plugin->api, "Wasn't able to add '%s' as a new onload, 'onload' section not found", json_object_get_string(actionsToAdd));
json_object_put(actionsToAdd);
return -8;
}
if(AddActionsToSectionFromPlugin(plugin->api, *ctrlConfig->ctlPlugins, &ctrlConfig->sections[idx], actionsToAdd, 0)) {
AFB_ApiError(plugin->api, "Wasn't able to add '%s' as a new onload to %s", json_object_get_string(actionsToAdd), ctrlConfig->sections[idx].uid);
json_object_put(actionsToAdd);
return -9;
}
btCaptureJ = json_tokener_parse(MIXER_BT_CAPTURE_JSON_SECTION);
wrap_json_pack(&btCaptureParamsJ, "{s:i}", "channels", btChannelsNumber);
json_object_object_add(btCaptureJ, "params", btCaptureParamsJ);
if(! json_object_object_get_ex(halMixerJ, "captures", &halOrigCaptureJ)) {
halNewCaptureJ = json_object_new_array();
json_object_array_add(halNewCaptureJ, btCaptureJ);
json_object_object_add(halMixerJ, "captures", halNewCaptureJ);
}
else if(json_object_is_type(halOrigCaptureJ, json_type_array)) {
halNewCaptureJ = halOrigCaptureJ;
json_object_array_add(halNewCaptureJ, btCaptureJ);
}
else if(json_object_is_type(halOrigCaptureJ, json_type_object)) {
json_object_get(halOrigCaptureJ);
json_object_object_del(halMixerJ, "captures");
halNewCaptureJ = json_object_new_array();
json_object_array_add(halNewCaptureJ, halOrigCaptureJ);
json_object_array_add(halNewCaptureJ, btCaptureJ);
json_object_object_add(halMixerJ, "captures", halNewCaptureJ);
}
else {
AFB_ApiError(plugin->api, "Unrecognized 'halmixer' captures format");
return -10;
}
btStreamJ = json_tokener_parse(MIXER_BT_STREAM_JSON_SECTION);
json_object_object_add(btStreamJ, "zone", json_object_new_string(btStreamZone));
if(! json_object_object_get_ex(halMixerJ, "streams", &halOrigStreamJ)) {
halNewStreamJ = json_object_new_array();
json_object_array_add(halNewStreamJ, btStreamJ);
json_object_object_add(halMixerJ, "streams", halNewStreamJ);
}
else if(json_object_is_type(halOrigStreamJ, json_type_array)) {
halNewStreamJ = halOrigStreamJ;
json_object_array_add(halNewStreamJ, btStreamJ);
}
else if(json_object_is_type(halOrigStreamJ, json_type_object)) {
json_object_get(halOrigStreamJ);
json_object_object_del(halMixerJ, "streams");
halNewStreamJ = json_object_new_array();
json_object_array_add(halNewStreamJ, halOrigStreamJ);
json_object_array_add(halNewStreamJ, btStreamJ);
json_object_object_add(halMixerJ, "streams", halNewStreamJ);
}
else {
AFB_ApiError(plugin->api, "Unrecognized 'halmixer' streams format");
return -11;
}
localHalBtPluginData.halBtPluginEnabled = 1;
AFB_ApiNotice(plugin->api, "Plugin initialization of %s plugin correctly done", HAL_BT_PLUGIN_NAME);
return 0;
}
// Call at contoller onload time
CTLP_CAPI(init, source, argsJ, queryJ)
{
int err;
char *returnedInfo;
json_object *toSendJ, *returnedJ, *returnedBtList = NULL;
if(! localHalBtPluginData.halBtPluginEnabled) {
AFB_ApiWarning(source->api, "Controller onload initialization of %s plugin cannot be done because bluetooth is not reachable", HAL_BT_PLUGIN_NAME);
return 0;
}
AFB_ApiInfo(source->api, "Controller onload initialization of %s plugin", HAL_BT_PLUGIN_NAME);
// Loading hal BT plugin specific verbs
if(afb_dynapi_add_verb(source->api,
HAL_BT_GET_STREAMING_STATUS_VERB,
"Get Bluetooth streaming status",
HalBtGetStreamingStatus,
(void *) &localHalBtPluginData,
NULL,
0)) {
AFB_ApiError(source->api, "Error while creating verb for bluetooth plugin : '%s'", HAL_BT_GET_STREAMING_STATUS_VERB);
return -1;
}
if(afb_dynapi_add_verb(source->api,
HAL_BT_SET_STREAMING_STATUS_VERB,
"Set Bluetooth streaming status",
HalBtSetStreamingStatus,
(void *) &localHalBtPluginData,
NULL,
0)) {
AFB_ApiError(source->api, "Error while creating verb for bluetooth plugin : '%s'", HAL_BT_SET_STREAMING_STATUS_VERB);
return -2;
}
if(afb_dynapi_add_verb(source->api,
HAL_BT_GET_CONNECTED_A2DP_DEVICES_VERB,
"Get connected Bluetooth A2DP devices list",
HalBtGetA2DPBluetoothDevices,
(void *) &localHalBtPluginData,
NULL,
0)) {
AFB_ApiError(source->api, "Error while creating verb for bluetooth plugin : '%s'", HAL_BT_GET_CONNECTED_A2DP_DEVICES_VERB);
return -3;
}
if(afb_dynapi_add_verb(source->api,
HAL_BT_GET_SELECTED_A2DP_DEVICE_VERB,
"Get selected Bluetooth A2DP device",
HalBtGetSelectedA2DPBluetoothDevice,
(void *) &localHalBtPluginData,
NULL,
0)) {
AFB_ApiError(source->api, "Error while creating verb for bluetooth plugin : '%s'", HAL_BT_GET_SELECTED_A2DP_DEVICE_VERB);
return -4;
}
if(afb_dynapi_add_verb(source->api,
HAL_BT_SET_SELECTED_A2DP_DEVICE_VERB,
"Set selected Bluetooth A2DP device",
HalBtSetSelectedA2DPBluetoothDevice,
(void *) &localHalBtPluginData,
NULL,
0)) {
AFB_ApiError(source->api, "Error while creating verb for bluetooth plugin : '%s'", HAL_BT_SET_SELECTED_A2DP_DEVICE_VERB);
return -5;
}
// Register to Bluetooth manager
wrap_json_pack(&toSendJ, "{s:s}", "value", BT_MANAGER_DEVICE_UPDATE_EVENT);
if(AFB_ServiceSync(source->api, BT_MANAGER_API, BT_MANAGER_SUBSCRIBE_VERB, toSendJ, &returnedJ)) {
AFB_ApiError(source->api,
"Error during call to verb '%s' of '%s' api (%s)",
BT_MANAGER_SUBSCRIBE_VERB,
BT_MANAGER_API,
json_object_get_string(returnedJ));
return -6;
}
else if(! wrap_json_unpack(returnedJ, "{s:{s:s}}", "request", "info", &returnedInfo)) {
AFB_ApiError(source->api,
"Couldn't subscribe to event '%s' during call to verb '%s' of api '%s' (error '%s')",
BT_MANAGER_DEVICE_UPDATE_EVENT,
BT_MANAGER_SUBSCRIBE_VERB,
BT_MANAGER_API,
returnedInfo);
json_object_put(returnedJ);
return -6;
}
json_object_put(returnedJ);
if(AFB_ServiceSync(source->api, BT_MANAGER_API, BT_MANAGER_GET_DEVICES_VERB, NULL, &returnedJ)) {
if((! wrap_json_unpack(returnedJ, "{s:{s:s}}", "request", "info", &returnedInfo)) &&
(! strncmp(returnedInfo, "No find devices", strlen(returnedInfo)))) {
AFB_ApiInfo(source->api,
"No bluetooth devices returned by call to verb '%s' of '%s' api",
BT_MANAGER_GET_DEVICES_VERB,
BT_MANAGER_API);
}
else {
AFB_ApiError(source->api,
"Error during call to verb '%s' of '%s' api (%s)",
BT_MANAGER_GET_DEVICES_VERB,
BT_MANAGER_API,
json_object_get_string(returnedJ));
return -7;
}
}
else if(wrap_json_unpack(returnedJ, "{s:{s:o}}", "response", "list", &returnedBtList)) {
AFB_ApiError(source->api,
"Couldn't get bluetooth device list during call to verb '%s' of api '%s'",
BT_MANAGER_GET_DEVICES_VERB,
BT_MANAGER_API);
json_object_put(returnedJ);
return -7;
}
if((returnedBtList) && (err = HalBtDataHandleReceivedMutlipleBtDeviceData(&localHalBtPluginData, returnedBtList)))
return (10 * err);
json_object_put(returnedJ);
if(localHalBtPluginData.selectedBtDevice) {
localHalBtPluginData.btStreamEnabled = 1;
AFB_ApiInfo(source->api, "Useable bluetooth device detected at initialization, will try to use it");
if(HalBtMixerLinkSetBtStreamingSettings(source->api,
localHalBtPluginData.currentHalData->ctlHalSpecificData->mixerApiName,
localHalBtPluginData.btStreamEnabled,
localHalBtPluginData.selectedBtDevice->hci,
localHalBtPluginData.selectedBtDevice->address)) {
AFB_ApiError(source->api,
"Couldn't set bluetooth streaming settings during call to verb '%s' of api '%s'",
MIXER_SET_STREAMED_BT_DEVICE_VERB,
localHalBtPluginData.currentHalData->ctlHalSpecificData->mixerApiName);
return -8;
}
}
AFB_ApiNotice(source->api, "Controller onload initialization of %s plugin correctly done", HAL_BT_PLUGIN_NAME);
return 0;
}
// This receive Hal bluetooth plugin events
CTLP_CAPI(events, source, argsJ, queryJ)
{
struct HalBtDeviceData *previouslySelectedBtDevice = localHalBtPluginData.selectedBtDevice;
if(! localHalBtPluginData.halBtPluginEnabled) {
AFB_ApiWarning(source->api, "Bluetooth event received but cannot be handled because bluetooth is not reachable");
return 0;
}
if(HalBtDataHandleReceivedSingleBtDeviceData(&localHalBtPluginData, queryJ)) {
AFB_ApiError(source->api, "Error while decoding bluetooth event received json (%s)", json_object_get_string(queryJ));
return -1;
}
if(localHalBtPluginData.selectedBtDevice == previouslySelectedBtDevice) {
AFB_ApiDebug(source->api, "Bluetooth event received but device didn't change");
return 0;
}
if(localHalBtPluginData.selectedBtDevice)
localHalBtPluginData.btStreamEnabled = 1;
else
localHalBtPluginData.btStreamEnabled = 0;
if(HalBtMixerLinkSetBtStreamingSettings(source->api,
localHalBtPluginData.currentHalData->ctlHalSpecificData->mixerApiName,
localHalBtPluginData.btStreamEnabled,
localHalBtPluginData.selectedBtDevice ? localHalBtPluginData.selectedBtDevice->hci : NULL,
localHalBtPluginData.selectedBtDevice ? localHalBtPluginData.selectedBtDevice->address : NULL)) {
AFB_ApiError(source->api,
"Couldn't set bluetooth streaming settings during call to verb '%s' of api '%s'",
MIXER_SET_STREAMED_BT_DEVICE_VERB,
localHalBtPluginData.currentHalData->ctlHalSpecificData->mixerApiName);
return -2;
}
return 0;
}
|