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.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="José Bollo">
<title>AGL framework, overview of the proposal of IoT.bzh</title>
<style type="text/css">code{white-space: pre;}</style>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="stylesheet" href="doc.css">
</head>
<body>
<header>
<h1 class="title">AGL framework, overview of the proposal of IoT.bzh</h1>
<h2 class="author">José Bollo</h2>
<h3 class="date">29 mai 2016</h3>
</header>
<nav id="TOC">
<ul>
<li><a href="#agl-/*
* Copyright (C) 2016 "IoT.bzh"
* Author Fulup Ar Foll <fulup@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.
*
* reference:
* amixer contents; amixer controls;
* http://www.tldp.org/HOWTO/Alsa-sound-6.html
*/
#define _GNU_SOURCE // needed for vasprintf
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <fcntl.h>
#include <math.h>
#include <sys/time.h>
#include <sys/types.h>
#include "hal-interface.h"
static alsaHalMapT *halCtls;
static const char *halDevid;
STATIC const char *halCtlsLabels[] = {
[StartHalCrlTag] = "NOT_USED",
[Master_Playback_Volume] = "Master_Playback_Volume",
[Master_OnOff_Switch] = "Master_OnOff_Switch",
[Master_Playback_Ramp] = "Master_Playback_Ramp",
[PCM_Playback_Volume] = "PCM_Playback_Volume",
[PCM_Playback_Switch] = "PCM_Playback_Switch",
[Capture_Volume] = "Capture_Volume",
[EndHalCrlTag] = "NOT_USED"
};
// Force specific HAL to depend on ShareHalLib
PUBLIC char* SharedHalLibVersion="1.0";
// Subscribe to AudioBinding events
STATIC void halSubscribe(afb_req request) {
const char *devidJ = afb_req_value(request, "devid");
if (devidJ == NULL) {
afb_req_fail_f(request, "devidJ-missing", "devidJ=hw:xxx missing");
}
}
// HAL normalise volume values to 0-100%
STATIC struct json_object *NormaliseValue(const alsaHalCtlMapT *halCtls, struct json_object *valuesJ) {
int length;
// assert response as the right length
length = json_object_array_length(valuesJ);
if (length != halCtls->count) {
AFB_WARNING ("NormaliseValue invalid ctl=%s values count=%d len=%d", halCtls->name, halCtls->count, length);
return NULL;
}
json_object *normalisedJ= json_object_new_array();
for (int idx=0; idx <length; idx++) {
json_object *valueJ = json_object_array_get_idx (valuesJ, idx);
int value = json_object_get_int(valueJ);
// cleanup and normalise value
if (value > halCtls->maxval) value= halCtls->maxval;
if (value < halCtls->minval) value= halCtls->minval;
// If Integer scale to 0/100
if (halCtls->type == SND_CTL_ELEM_TYPE_INTEGER) {
value = (value * 100) / (halCtls->maxval - halCtls->minval);
}
json_object_array_add(normalisedJ, json_object_new_int(value));
}
return (normalisedJ);
}
// Remap low level controls into HAL hight level ones
STATIC json_object *CtlGetPrepareResponse(afb_req request, struct json_object *ctlsJ) {
struct json_object *halResponseJ;
// make sure return controls have a valid type
if (json_object_get_type(ctlsJ) != json_type_array) {
afb_req_fail_f(request, "ctls-notarray", "Invalid Controls return from alsa/getcontrol ctlsJ=%s", json_object_get_string(ctlsJ));
goto OnErrorExit;
}
// responseJ is a JSON array
halResponseJ = json_object_new_array();
// loop on array and store values into client context
for (int idx = 0; idx < json_object_array_length(ctlsJ); idx++) {
struct json_object *sndCtlJ, *valJ, *numidJ;
int numid;
sndCtlJ = json_object_array_get_idx(ctlsJ, idx);
if (!json_object_object_get_ex(sndCtlJ, "numid", &numidJ) || !json_object_object_get_ex(sndCtlJ, "val", &valJ)) {
afb_req_fail_f(request, "ctl-invalid", "Invalid Control return from alsa/getcontrol ctl=%s", json_object_get_string(sndCtlJ));
goto OnErrorExit;
}
// HAL and Business logic use the same AlsaMixerHal.h direct conversion
numid= (halCtlsEnumT) json_object_get_int(numidJ);
for (int idx = 0; halCtls[idx].ctl.numid; idx++) {
if (halCtls[idx].ctl.numid == numid) {
// translate low level numid to HAL one and normalise values
struct json_object *halCtlJ = json_object_new_object();
json_object_object_add(halCtlJ, "label", json_object_new_string(halCtls[idx].label)); // idx+1 == HAL/NUMID
json_object_object_add(halCtlJ, "tag" , json_object_new_int(halCtls[idx].tag)); // idx+1 == HAL/NUMID
json_object_object_add(halCtlJ, "val" , NormaliseValue(&halCtls[idx].ctl, valJ));
json_object_array_add(halResponseJ, halCtlJ);
break;
}
}
if ( halCtls[idx].ctl.numid == 0) {
afb_req_fail_f(request, "ctl-invalid", "Invalid Control numid=%d from alsa/getcontrol ctlJ=%s", numid, json_object_get_string(sndCtlJ));
goto OnErrorExit;
}
}
return halResponseJ;
OnErrorExit:
return NULL;
}
// Return ALL HAL snd controls
PUBLIC void halListCtls(afb_req request) {
struct json_object *ctlsHalJ = json_object_new_array();
for (int idx = 0; halCtls[idx].ctl.numid; idx++) {
struct json_object *ctlHalJ = json_object_new_object();
json_object_object_add(ctlHalJ, "label", json_object_new_string(halCtls[idx].label));
json_object_object_add(ctlHalJ, "tag" , json_object_new_int(halCtls[idx].tag));
json_object_object_add(ctlHalJ, "count", json_object_new_int(halCtls[idx].ctl.count));
json_object_array_add (ctlsHalJ, ctlHalJ);
}
afb_req_success (request, ctlsHalJ, NULL);
}
// Map HAL ctlName to ctlLabel
STATIC int halCtlStringToIndex (const char* label) {
for (int idx = 0; halCtls[idx].ctl.numid; idx++) {
if (!strcmp (halCtls[idx].label, label)) return idx;
}
// not found
return -1;
}
STATIC int halCtlTagToIndex (halCtlsEnumT tag) {
for (int idx = 0; halCtls[idx].ctl.numid; idx++) {
if (halCtls[idx].tag == tag) return idx;
}
// not found
return -1;
}
STATIC int halGetOneCtls (afb_req request, struct json_object*ctlInJ) {
struct json_object *tmpJ;
int tag, index;
// check 1st short command mode [tag1, tag2, ...]
tag = json_object_get_type (ctlInJ);
if (!tag) {
json_object_object_get_ex (ctlInJ, "tag" , &tmpJ);
tag = json_object_get_int(tmpJ);
}
if (tag) {
index = halCtlTagToIndex((halCtlsEnumT) tag);
} else {
// tag was not provided let's try label
const char *label;
json_object_object_get_ex (ctlInJ, "label" , &tmpJ);
label = json_object_get_string(tmpJ);
index = halCtlStringToIndex(label);
}
if (index < 0) {
afb_req_fail_f(request, "ctl-invalid", "No Label/Tag given ctl=[%s]", json_object_get_string(ctlInJ));
goto OnErrorExit;
}
// return corresponding lowlevel numid to querylist
return halCtls[index].ctl.numid;
OnErrorExit:
return -1;
}
// Translate high level control to low level and call lower layer
PUBLIC void halGetCtls(afb_req request) {
int err, numid;
struct json_object *ctlsInJ, *ctlsOutJ, *queryJ, *responseJ;
// get query from request
ctlsInJ = afb_req_json(request);
ctlsOutJ = json_object_new_array();
switch (json_object_get_type(ctlsInJ)) {
case json_type_object: {
numid = halGetOneCtls (request, ctlsInJ);
if (numid <=0) goto OnErrorExit;
json_object_array_add (ctlsOutJ, json_object_new_int(numid));
break;
}
case json_type_array: {
for (int idx= 0; idx < json_object_array_length (ctlsInJ); idx++) {
struct json_object *ctlInJ = json_object_array_get_idx (ctlsInJ, idx);
numid= halGetOneCtls (request, ctlInJ);
if (numid<=0) goto OnErrorExit;
json_object_array_add (ctlsOutJ, json_object_new_int(numid));
}
break;
}
default:
afb_req_fail_f(request, "ctl-invalid", "Not a valid JSON ctl=[%s]", json_object_get_string(ctlsInJ));
goto OnErrorExit;
}
// Call now level CTL
queryJ = json_object_new_object();
json_object_object_add(queryJ, "devid", json_object_new_string (halDevid));
json_object_object_add(queryJ, "numids", ctlsOutJ);
err= afb_service_call_sync("alsacore", "getctls", queryJ, &responseJ);
if (err) {
afb_req_fail_f(request, "subcall:alsacore/getctl", "%s", json_object_get_string(responseJ));
goto OnErrorExit;
}
// Let ignore info data if any and keep on response
json_object_object_get_ex (responseJ, "response", &responseJ);
// map back low level response to HAL ctl with normalised values
struct json_object *halResponse = CtlGetPrepareResponse(request, responseJ);
if (!halResponse) goto OnErrorExit;
afb_req_success (request, halResponse, NULL);
return;
OnErrorExit:
return;
};
// This receive all event this binding subscribe to
PUBLIC void halServiceEvent(const char *evtname, struct json_object *eventJ) {
int numid;
struct json_object *numidJ, *valuesJ, *valJ;
AFB_NOTICE("halServiceEvent evtname=%s [msg=%s]", evtname, json_object_get_string(eventJ));
json_object_object_get_ex (eventJ, "values" , &valuesJ);
if (!valuesJ) {
AFB_ERROR("halServiceEvent novalues: evtname=%s [msg=%s]", evtname, json_object_get_string(eventJ));
return;
}
json_object_object_get_ex (valuesJ, "numid" , &numidJ);
numid = json_object_get_int (numidJ);
// search it corresponding numid in halCtls attach a callback
if (numid) {
for (int idx= 0; halCtls[idx].ctl.numid; idx++) {
if (halCtls[idx].ctl.numid == numid && halCtls[idx].cb.callback != NULL) {
json_object_object_get_ex (valuesJ, "val" , &valJ);
halCtls[idx].cb.callback (&halCtls[idx].ctl, halCtls[idx].cb.handle, valJ);
}
}
}
}
STATIC int UpdateOneSndCtl (alsaHalCtlMapT *ctl, struct json_object *sndCtlJ) {
struct json_object *tmpJ, *ctlJ;
AFB_NOTICE ("*** %s, ", json_object_get_string(sndCtlJ));
// make sure we face a valid Alsa Low level ctl
if (!json_object_object_get_ex (sndCtlJ, "ctrl" , &ctlJ)) return -1;
json_object_object_get_ex (sndCtlJ, "name" , &tmpJ);
ctl->name = (char*)json_object_get_string(tmpJ);
json_object_object_get_ex (sndCtlJ, "numid" , &tmpJ);
ctl->numid = json_object_get_int(tmpJ);
json_object_object_get_ex (ctlJ, "min" , &tmpJ);
ctl->minval = json_object_get_int(tmpJ);
json_object_object_get_ex (ctlJ, "max" , &tmpJ);
ctl->maxval = json_object_get_int(tmpJ);
json_object_object_get_ex (ctlJ, "step" , &tmpJ);
ctl->step = json_object_get_int(tmpJ);
json_object_object_get_ex (ctlJ, "count" , &tmpJ);
ctl->count = json_object_get_int(tmpJ);
json_object_object_get_ex (ctlJ, "type" , &tmpJ);
ctl->type = (snd_ctl_elem_type_t)json_object_get_int(tmpJ);
return 0;
}
// this is call when after all bindings are loaded
PUBLIC int halServiceInit (const char *apiPrefix, alsaHalSndCardT *alsaHalSndCard) {
int err;
struct json_object *queryurl, *responseJ, *devidJ, *ctlsJ, *tmpJ;
halCtls = alsaHalSndCard->ctls; // Get sndcard specific HAL control mapping
err= afb_daemon_require_api("alsacore", 1);
if (err) {
AFB_ERROR ("AlsaCore missing cannot use AlsaHAL");
goto OnErrorExit;
}
// register HAL with Alsa Low Level Binder
queryurl = json_object_new_object();
json_object_object_add(queryurl, "prefix", json_object_new_string(apiPrefix));
json_object_object_add(queryurl, "sndname", json_object_new_string(alsaHalSndCard->name));
err= afb_service_call_sync ("alsacore", "halregister", queryurl, &responseJ);
json_object_put(queryurl);
if (err) {
AFB_NOTICE ("Fail to register HAL to ALSA lowlevel binding Response=[%s]", json_object_get_string(responseJ));
goto OnErrorExit;
}
// extract sound devidJ from HAL registration
if (!json_object_object_get_ex(responseJ, "response", &tmpJ) || !json_object_object_get_ex(tmpJ, "devid", &devidJ)) {
AFB_ERROR ("Ooops: Internal error no devidJ return from HAL registration Response=[%s]", json_object_get_string(responseJ));
goto OnErrorExit;
}
// save devid for future use
halDevid = strdup (json_object_get_string(devidJ));
// for each Non Alsa Control callback create a custom control
ctlsJ= json_object_new_array();
for (int idx= 0; (halCtls[idx].ctl.name||halCtls[idx].ctl.numid); idx++) {
struct json_object *ctlJ;
// map HAL ctlTad with halCrlLabel to simplify set/get ctl operations using Labels
halCtls[idx].label = halCtlsLabels[halCtls[idx].tag];
if (halCtls[idx].cb.callback != NULL) {
ctlJ = json_object_new_object();
if (halCtls[idx].ctl.name) json_object_object_add(ctlJ, "name" , json_object_new_string(halCtls[idx].ctl.name));
if (halCtls[idx].ctl.minval) json_object_object_add(ctlJ, "min" , json_object_new_int(halCtls[idx].ctl.minval));
if (halCtls[idx].ctl.maxval) json_object_object_add(ctlJ, "max" , json_object_new_int(halCtls[idx].ctl.maxval));
if (halCtls[idx].ctl.step) json_object_object_add(ctlJ, "step" , json_object_new_int(halCtls[idx].ctl.step));
if (halCtls[idx].ctl.type) json_object_object_add(ctlJ, "type" , json_object_new_int(halCtls[idx].ctl.type));
json_object_array_add(ctlsJ, ctlJ);
} else {
ctlJ = json_object_new_object();
if (halCtls[idx].ctl.numid) json_object_object_add(ctlJ, "numid" , json_object_new_int(halCtls[idx].ctl.numid));
if (halCtls[idx].ctl.name) json_object_object_add(ctlJ, "name" , json_object_new_string(halCtls[idx].ctl.name));
json_object_array_add(ctlsJ, ctlJ);
}
}
// Build new queryJ to add HAL custom control if any
if (json_object_array_length (ctlsJ) > 0) {
queryurl = json_object_new_object();
json_object_get(devidJ); // make sure devidJ does not get free by 1st call.
json_object_object_add(queryurl, "devid",devidJ);
json_object_object_add(queryurl, "ctls",ctlsJ);
err= afb_service_call_sync ("alsacore", "addcustomctl", queryurl, &responseJ);
if (err) {
AFB_ERROR ("Fail creating HAL Custom ALSA ctls Response=[%s]", json_object_get_string(responseJ));
goto OnErrorExit;
}
}
// Make sure response is valid
json_object_object_get_ex (responseJ, "response" , &ctlsJ);
if (json_object_get_type(ctlsJ) != json_type_array) {
AFB_ERROR ("Response Invalid JSON array ctls Response=[%s]", json_object_get_string(responseJ));
goto OnErrorExit;
}
// update HAL data from JSON response
for (int idx= 0; idx < json_object_array_length (ctlsJ); idx++) {
json_object *ctlJ = json_object_array_get_idx (ctlsJ, idx);
err= UpdateOneSndCtl(&halCtls[idx].ctl, ctlJ) ;
if (err) {
AFB_ERROR ("Fail found MAP Alsa Low level=%s", json_object_get_string(ctlJ));
goto OnErrorExit;
}
}
// finally register for alsa lowlevel event
queryurl = json_object_new_object();
json_object_object_add(queryurl, "devid",devidJ);
err= afb_service_call_sync ("alsacore", "subscribe", queryurl, &responseJ);
if (err) {
AFB_ERROR ("Fail subscribing to ALSA lowlevel events");
goto OnErrorExit;
}
return (0);
OnErrorExit:
return (1);
};
// Every HAL export the same API & Interface Mapping from SndCard to AudioLogic is done through alsaHalSndCardT
PUBLIC afb_verb_v2 halServiceApi[] = {
/* VERB'S NAME FUNCTION TO CALL SHORT DESCRIPTION */
{ .verb = "ping", .callback = pingtest},
{ .verb = "listctls", .callback = halListCtls},
{ .verb = "getctls", .callback = halGetCtls},
{ .verb = "subscribe", .callback = halSubscribe},
{ .verb = NULL} /* marker for end of the array */
};
|