From 5d4ac3aebd5f269ced0cd6f572090ad1c927e5be Mon Sep 17 00:00:00 2001 From: Tobias Jahnke Date: Fri, 4 Aug 2017 14:51:38 +0200 Subject: removed legacy most dependencies in nbproject --- conf.d/app-templates | 2 +- nbproject/configurations.xml | 129 ++----------------------------------------- 2 files changed, 7 insertions(+), 124 deletions(-) diff --git a/conf.d/app-templates b/conf.d/app-templates index 8967162..dee5836 160000 --- a/conf.d/app-templates +++ b/conf.d/app-templates @@ -1 +1 @@ -Subproject commit 8967162dd12bce89f9ae27f5c9bce7b78624e3fd +Subproject commit dee58363ddb98f8e63239035f1a8f1ab151c5e96 diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 8d7491c..59122f3 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -78,23 +78,8 @@ HighLevelApiConf.c HighLevelBinding.c - - - ConnectionInfo.cpp - Console.c - MacAddr.cpp - MostIpc.cpp - MostMsg.cpp - MostMsgTx.cpp - MsgAddr.cpp - MsgFilter.cpp - Shadow_NetworkMaster.cpp - Thread.cpp - - DeviceContainer.cpp - DeviceValue.cpp - Mediator.cpp - libmostvolume.cpp + + audio-interface.c - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ../../../opt/include - ../../../opt/include/alsa - /usr/include/p11-kit-1 - /usr/include/json-c - /usr/include/lua5.3 - build/Alsa-Plugin/Alsa-Policy-Hook - - - CONTROL_CONFIG_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/conf.d/project/config.d:/usr/local/controler/config.d" - CONTROL_CONFIG_POST="control" - CONTROL_CONFIG_PRE="onload" - CONTROL_DOSCRIPT_PRE="doscript" - CONTROL_LUA_EVENT="luaevt" - CONTROL_LUA_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/conf.d/project/lua.d:/usr/local/controler/ctl-lua.d" - CONTROL_MAXPATH_LEN=255 - CONTROL_ONLOAD_DEFAULT="onload-default" - CONTROL_PLUGIN_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/build:/home/fulup/opt/audio-bindings/ctlplug:/usr/lib/afb/ctlplug" - CTL_PLUGIN_MAGIC=2468013579 - MAX_LINEAR_DB_SCALE=24 - MAX_SND_CARD=16 - NATIVE_LINUX - PIC - TLV_BYTE_SIZE=256 - policy_hook_cb_EXPORTS - + + @@ -933,50 +858,8 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ../../../opt/include/afb - Alsa-Plugin/Alsa-Hook-Callback - ../../../opt/include/alsa - /usr/include/json-c - ../../../opt/include - build/Alsa-Plugin/Alsa-Hook-Callback - + + -- cgit 1.2.3-korg From b046f9ef9071c2a22f62b8eed34ccb25266c54ea Mon Sep 17 00:00:00 2001 From: Tobias Jahnke Date: Fri, 4 Aug 2017 19:06:08 +0200 Subject: adds HAL for MOST/UNICENS --- HAL-afb/hal-most-unicens/CMakeLists.txt | 42 +++++++++++++++++++++++ HAL-afb/hal-most-unicens/hal_most_unicens.c | 53 +++++++++++++++++++++++++++++ nbproject/configurations.xml | 41 ++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 HAL-afb/hal-most-unicens/CMakeLists.txt create mode 100644 HAL-afb/hal-most-unicens/hal_most_unicens.c diff --git a/HAL-afb/hal-most-unicens/CMakeLists.txt b/HAL-afb/hal-most-unicens/CMakeLists.txt new file mode 100644 index 0000000..ed49f1b --- /dev/null +++ b/HAL-afb/hal-most-unicens/CMakeLists.txt @@ -0,0 +1,42 @@ +########################################################################### +# Copyright 2015, 2016, 2017 IoT.bzh +# +# author: Fulup Ar Foll +# +# 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. +########################################################################### + + +# Add target to project dependency list +PROJECT_TARGET_ADD(hal-most-unicens) + + # Define project Targets + ADD_LIBRARY(hal-most-unicens MODULE hal_most_unicens.c) + + # Binder exposes a unique public entry point + SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES + PREFIX "afb-" + LABELS "BINDING" + LINK_FLAGS ${BINDINGS_LINK_FLAG} + OUTPUT_NAME ${TARGET_NAME} + ) + + # Library dependencies (include updates automatically) + TARGET_LINK_LIBRARIES(${TARGET_NAME} + hal-interface + audio-interface + ) + + # installation directory + INSTALL(TARGETS ${TARGET_NAME} + LIBRARY DESTINATION ${BINDINGS_INSTALL_DIR}) \ No newline at end of file diff --git a/HAL-afb/hal-most-unicens/hal_most_unicens.c b/HAL-afb/hal-most-unicens/hal_most_unicens.c new file mode 100644 index 0000000..a98906c --- /dev/null +++ b/HAL-afb/hal-most-unicens/hal_most_unicens.c @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2017, Microchip Technology Inc. and its subsidiaries. + * Author Tobias Jahnke + * + * 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 "hal-interface.h" +#include "audio-interface.h" + +/* declare ALSA mixer controls */ +STATIC alsaHalMapT alsaHalMap[]= { + { .tag=Master_Playback_Volume, .ctl={.name="Master Playback Volume" } }, + { .tag=Master_OnOff_Switch , .ctl={.name="Master Playback Switch" } }, + { .tag=PCM_Playback_Volume , .ctl={.name="PCM Playback Volume" } }, + { .tag=EndHalCrlTag} /* marker for end of the array */ +} ; + +/* HAL sound card mapping info */ +STATIC alsaHalSndCardT alsaHalSndCard = { + .name = "Microchip MOST:1", /* WARNING: name MUST match with 'aplay -l' */ + .info = "HAL for MICROCHIP MOST sound card controlled by UNICENS binding", + .ctls = alsaHalMap, + .volumeCB = NULL, /* use default volume normalization function */ +}; + + +STATIC int sndServiceInit () { + int err; + AFB_DEBUG ("Initializing HAL-MOST-UNICENS-BINDING"); + + err = halServiceInit (afbBindingV2.api, &alsaHalSndCard); + return err; +} + +// API prefix should be unique for each snd card +PUBLIC const struct afb_binding_v2 afbBindingV2 = { + .api = "hal-most-unicens", + .init = sndServiceInit, + .verbs = halServiceApi, + .onevent = halServiceEvent, +}; diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 59122f3..3f333ec 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -57,6 +57,9 @@ hal-volramp.c hal-volume.c + + hal_most_unicens.c + HalPlugPcm.c @@ -388,6 +391,13 @@ + + + + @@ -607,6 +617,18 @@ + + + + HAL-afb/Scarlett-Focusrite + /usr/include/json-c + Shared-Interface + HAL-afb/HAL-interface + ../../../opt/include + build/HAL-afb/Scarlett-Focusrite + + + @@ -839,6 +861,13 @@ + + + + @@ -974,6 +1003,18 @@ + + + + HAL-afb/Scarlett-Focusrite + /usr/include/json-c + Shared-Interface + HAL-afb/HAL-interface + ../../../opt/include + build/HAL-afb/Scarlett-Focusrite + + + -- cgit 1.2.3-korg From c364f1084e171862b5d3393f2f180b3419cef68a Mon Sep 17 00:00:00 2001 From: Tobias Jahnke Date: Mon, 7 Aug 2017 17:46:43 +0200 Subject: added custom master and playback mixer controls for hal-most-unicens --- HAL-afb/hal-most-unicens/hal_most_unicens.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/HAL-afb/hal-most-unicens/hal_most_unicens.c b/HAL-afb/hal-most-unicens/hal_most_unicens.c index a98906c..388b840 100644 --- a/HAL-afb/hal-most-unicens/hal_most_unicens.c +++ b/HAL-afb/hal-most-unicens/hal_most_unicens.c @@ -19,11 +19,27 @@ #include "hal-interface.h" #include "audio-interface.h" +static int master_volume; +static int master_switch; +static int pcm_volume; + +void unicens_volume_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, json_object *valuesJ) { + + AFB_NOTICE("UNICENS Volume tag=%d, vol=%d, min=%d, max=%d", tag, control->value, control->minval, control->maxval); + +} + /* declare ALSA mixer controls */ STATIC alsaHalMapT alsaHalMap[]= { - { .tag=Master_Playback_Volume, .ctl={.name="Master Playback Volume" } }, - { .tag=Master_OnOff_Switch , .ctl={.name="Master Playback Switch" } }, - { .tag=PCM_Playback_Volume , .ctl={.name="PCM Playback Volume" } }, + { .tag=Master_Playback_Volume, .cb={.callback=unicens_volume_cb, .handle=&master_volume}, .info="Sets master playback volume", + .ctl={.numid=0, .type=SND_CTL_ELEM_TYPE_INTEGER, .count=1, .minval=0, .maxval=100, .step=1, .value=50, .name="Master Playback Volume"} + }, + { .tag=Master_OnOff_Switch, .cb={.callback=unicens_volume_cb, .handle=&master_switch}, .info="Sets master playback switch", + .ctl={.numid=0, .type=SND_CTL_ELEM_TYPE_BOOLEAN, .count=1, .minval=0, .maxval=1, .step=1, .value=1, .name="Master Playback Switch"} + }, + { .tag=PCM_Playback_Volume, .cb={.callback=unicens_volume_cb, .handle=&pcm_volume}, .info="Sets PCM playback volume", + .ctl={.numid=0, .type=SND_CTL_ELEM_TYPE_INTEGER, .count=6, .minval=0, .maxval=100, .step=1, .value=100, .name="PCM Playback Volume"} + }, { .tag=EndHalCrlTag} /* marker for end of the array */ } ; @@ -35,7 +51,6 @@ STATIC alsaHalSndCardT alsaHalSndCard = { .volumeCB = NULL, /* use default volume normalization function */ }; - STATIC int sndServiceInit () { int err; AFB_DEBUG ("Initializing HAL-MOST-UNICENS-BINDING"); @@ -44,7 +59,7 @@ STATIC int sndServiceInit () { return err; } -// API prefix should be unique for each snd card +/* API prefix should be unique for each snd card */ PUBLIC const struct afb_binding_v2 afbBindingV2 = { .api = "hal-most-unicens", .init = sndServiceInit, -- cgit 1.2.3-korg From 1d36fcd6461a106711725642e7dc6d89c3473b3e Mon Sep 17 00:00:00 2001 From: Tobias Jahnke Date: Tue, 8 Aug 2017 16:55:49 +0200 Subject: added code to parse mixer control updates --- HAL-afb/hal-most-unicens/CMakeLists.txt | 2 +- HAL-afb/hal-most-unicens/hal_most_unicens.c | 48 +- HAL-afb/hal-most-unicens/wrap-json.c | 940 ++++++++++++++++++++++++++++ HAL-afb/hal-most-unicens/wrap-json.h | 47 ++ HAL-afb/hal-most-unicens/wrap-json.md | 306 +++++++++ 5 files changed, 1334 insertions(+), 9 deletions(-) create mode 100644 HAL-afb/hal-most-unicens/wrap-json.c create mode 100644 HAL-afb/hal-most-unicens/wrap-json.h create mode 100644 HAL-afb/hal-most-unicens/wrap-json.md diff --git a/HAL-afb/hal-most-unicens/CMakeLists.txt b/HAL-afb/hal-most-unicens/CMakeLists.txt index ed49f1b..c87ca4d 100644 --- a/HAL-afb/hal-most-unicens/CMakeLists.txt +++ b/HAL-afb/hal-most-unicens/CMakeLists.txt @@ -21,7 +21,7 @@ PROJECT_TARGET_ADD(hal-most-unicens) # Define project Targets - ADD_LIBRARY(hal-most-unicens MODULE hal_most_unicens.c) + ADD_LIBRARY(hal-most-unicens MODULE hal_most_unicens.c wrap-json.c) # Binder exposes a unique public entry point SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES diff --git a/HAL-afb/hal-most-unicens/hal_most_unicens.c b/HAL-afb/hal-most-unicens/hal_most_unicens.c index 388b840..3ab7ace 100644 --- a/HAL-afb/hal-most-unicens/hal_most_unicens.c +++ b/HAL-afb/hal-most-unicens/hal_most_unicens.c @@ -17,27 +17,59 @@ */ #define _GNU_SOURCE #include "hal-interface.h" -#include "audio-interface.h" +#include "audio-interface.h" +#include "wrap-json.h" static int master_volume; -static int master_switch; -static int pcm_volume; +static json_bool master_switch; +static int pcm_volume[6]; -void unicens_volume_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, json_object *valuesJ) { +void unicens_master_vol_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { + + const char *j_str = json_object_to_json_string(j_obj); + + if (wrap_json_unpack(j_obj, "[i!]", &master_volume) == 0) { + AFB_NOTICE("master_volume: %s, value=%d", j_str, master_volume); + } + else { + AFB_NOTICE("master_volume: INVALID STRING %s", j_str); + } +} + +void unicens_master_switch_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { + + const char *j_str = json_object_to_json_string(j_obj); - AFB_NOTICE("UNICENS Volume tag=%d, vol=%d, min=%d, max=%d", tag, control->value, control->minval, control->maxval); + if (wrap_json_unpack(j_obj, "[b!]", &master_switch) == 0) { + AFB_NOTICE("master_switch: %s, value=%d", j_str, master_switch); + } + else { + AFB_NOTICE("master_switch: INVALID STRING %s", j_str); + } +} + +void unicens_pcm_vol_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { + + const char *j_str = json_object_to_json_string(j_obj); + if (wrap_json_unpack(j_obj, "[iiiiii!]", &pcm_volume[0], &pcm_volume[1], &pcm_volume[2], &pcm_volume[3], + &pcm_volume[4], &pcm_volume[5]) == 0) { + AFB_NOTICE("pcm_vol: %s", j_str); + } + else { + AFB_NOTICE("pcm_vol: INVALID STRING %s", j_str); + } } /* declare ALSA mixer controls */ STATIC alsaHalMapT alsaHalMap[]= { - { .tag=Master_Playback_Volume, .cb={.callback=unicens_volume_cb, .handle=&master_volume}, .info="Sets master playback volume", + { .tag=Master_Playback_Volume, .cb={.callback=unicens_master_vol_cb, .handle=&master_volume}, .info="Sets master playback volume", .ctl={.numid=0, .type=SND_CTL_ELEM_TYPE_INTEGER, .count=1, .minval=0, .maxval=100, .step=1, .value=50, .name="Master Playback Volume"} }, - { .tag=Master_OnOff_Switch, .cb={.callback=unicens_volume_cb, .handle=&master_switch}, .info="Sets master playback switch", + { .tag=Master_OnOff_Switch, .cb={.callback=unicens_master_switch_cb, .handle=&master_switch}, .info="Sets master playback switch", .ctl={.numid=0, .type=SND_CTL_ELEM_TYPE_BOOLEAN, .count=1, .minval=0, .maxval=1, .step=1, .value=1, .name="Master Playback Switch"} }, - { .tag=PCM_Playback_Volume, .cb={.callback=unicens_volume_cb, .handle=&pcm_volume}, .info="Sets PCM playback volume", + { .tag=PCM_Playback_Volume, .cb={.callback=unicens_pcm_vol_cb, .handle=&pcm_volume}, .info="Sets PCM playback volume", .ctl={.numid=0, .type=SND_CTL_ELEM_TYPE_INTEGER, .count=6, .minval=0, .maxval=100, .step=1, .value=100, .name="PCM Playback Volume"} }, { .tag=EndHalCrlTag} /* marker for end of the array */ diff --git a/HAL-afb/hal-most-unicens/wrap-json.c b/HAL-afb/hal-most-unicens/wrap-json.c new file mode 100644 index 0000000..987a008 --- /dev/null +++ b/HAL-afb/hal-most-unicens/wrap-json.c @@ -0,0 +1,940 @@ +/* + Copyright (C) 2016, 2017 "IoT.bzh" + + author: José Bollo + + 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. +*/ + +#include + +#include "wrap-json.h" + +#define STACKCOUNT 32 +#define STRCOUNT 8 + +enum { + wrap_json_error_none, + wrap_json_error_null_object, + wrap_json_error_truncated, + wrap_json_error_internal_error, + wrap_json_error_out_of_memory, + wrap_json_error_invalid_character, + wrap_json_error_too_long, + wrap_json_error_too_deep, + wrap_json_error_null_spec, + wrap_json_error_null_key, + wrap_json_error_null_string, + wrap_json_error_out_of_range, + wrap_json_error_incomplete, + wrap_json_error_missfit_type, + wrap_json_error_key_not_found, + _wrap_json_error_count_ +}; + +static const char ignore_all[] = " \t\n\r,:"; +static const char pack_accept_arr[] = "][{snbiIfoO"; +static const char pack_accept_key[] = "s}"; +#define pack_accept_any (&pack_accept_arr[1]) + +static const char unpack_accept_arr[] = "*!][{snbiIfFoO"; +static const char unpack_accept_key[] = "*!s}"; +#define unpack_accept_any (&unpack_accept_arr[3]) + +static const char *pack_errors[_wrap_json_error_count_] = +{ + [wrap_json_error_none] = "unknown error", + [wrap_json_error_null_object] = "null object", + [wrap_json_error_truncated] = "truncated", + [wrap_json_error_internal_error] = "internal error", + [wrap_json_error_out_of_memory] = "out of memory", + [wrap_json_error_invalid_character] = "invalid character", + [wrap_json_error_too_long] = "too long", + [wrap_json_error_too_deep] = "too deep", + [wrap_json_error_null_spec] = "spec is NULL", + [wrap_json_error_null_key] = "key is NULL", + [wrap_json_error_null_string] = "string is NULL", + [wrap_json_error_out_of_range] = "array too small", + [wrap_json_error_incomplete] = "incomplete container", + [wrap_json_error_missfit_type] = "missfit of type", + [wrap_json_error_key_not_found] = "key not found" +}; + +int wrap_json_get_error_position(int rc) +{ + if (rc < 0) + rc = -rc; + return (rc >> 4) + 1; +} + +int wrap_json_get_error_code(int rc) +{ + if (rc < 0) + rc = -rc; + return rc & 15; +} + +const char *wrap_json_get_error_string(int rc) +{ + rc = wrap_json_get_error_code(rc); + if (rc >= sizeof pack_errors / sizeof *pack_errors) + rc = 0; + return pack_errors[rc]; +} + + + +static inline const char *skip(const char *d) +{ + while (*d && strchr(ignore_all, *d)) + d++; + return d; +} + +int wrap_json_vpack(struct json_object **result, const char *desc, va_list args) +{ + /* TODO: the case of structs with key being single char should be optimized */ + int nstr, notnull, nullable, rc; + size_t sz, dsz, ssz; + char *s; + char c; + const char *d; + char buffer[256]; + struct { const char *str; size_t sz; } strs[STRCOUNT]; + struct { struct json_object *cont, *key; const char *acc; char type; } stack[STACKCOUNT], *top; + struct json_object *obj; + + ssz = sizeof buffer; + s = buffer; + top = stack; + top->key = NULL; + top->cont = NULL; + top->acc = pack_accept_any; + top->type = 0; + d = desc; + if (!d) + goto null_spec; + d = skip(d); + for(;;) { + c = *d; + if (!c) + goto truncated; + if (!strchr(top->acc, c)) + goto invalid_character; + d = skip(++d); + switch(c) { + case 's': + nullable = 0; + notnull = 0; + nstr = 0; + sz = 0; + for (;;) { + strs[nstr].str = va_arg(args, const char*); + if (strs[nstr].str) + notnull = 1; + if (*d == '?') { + d = skip(++d); + nullable = 1; + } + switch(*d) { + case '%': strs[nstr].sz = va_arg(args, size_t); d = skip(++d); break; + case '#': strs[nstr].sz = (size_t)va_arg(args, int); d = skip(++d); break; + default: strs[nstr].sz = strs[nstr].str ? strlen(strs[nstr].str) : 0; break; + } + sz += strs[nstr++].sz; + if (*d == '?') { + d = skip(++d); + nullable = 1; + } + if (*d != '+') + break; + if (nstr >= STRCOUNT) + goto too_long; + d = skip(++d); + } + if (*d == '*') + nullable = 1; + if (notnull) { + if (sz > ssz) { + ssz += ssz; + if (ssz < sz) + ssz = sz; + s = alloca(sz); + } + dsz = sz; + while (nstr) { + nstr--; + dsz -= strs[nstr].sz; + memcpy(&s[dsz], strs[nstr].str, strs[nstr].sz); + } + obj = json_object_new_string_len(s, (int)sz); + if (!obj) + goto out_of_memory; + } else if (nullable) + obj = NULL; + else + goto null_string; + break; + case 'n': + obj = NULL; + break; + case 'b': + obj = json_object_new_boolean(va_arg(args, int)); + if (!obj) + goto out_of_memory; + break; + case 'i': + obj = json_object_new_int(va_arg(args, int)); + if (!obj) + goto out_of_memory; + break; + case 'I': + obj = json_object_new_int64(va_arg(args, int64_t)); + if (!obj) + goto out_of_memory; + break; + case 'f': + obj = json_object_new_double(va_arg(args, double)); + if (!obj) + goto out_of_memory; + break; + case 'o': + case 'O': + obj = va_arg(args, struct json_object*); + if (*d == '?') + d = skip(++d); + else if (*d != '*' && !obj) + goto null_object; + if (c == 'O') + json_object_get(obj); + break; + case '[': + case '{': + if (++top >= &stack[STACKCOUNT]) + goto too_deep; + top->key = NULL; + if (c == '[') { + top->type = ']'; + top->acc = pack_accept_arr; + top->cont = json_object_new_array(); + } else { + top->type = '}'; + top->acc = pack_accept_key; + top->cont = json_object_new_object(); + } + if (!top->cont) + goto out_of_memory; + continue; + case '}': + case ']': + if (c != top->type || top <= stack) + goto internal_error; + obj = (top--)->cont; + if (*d == '*' && !(c == '}' ? json_object_object_length(obj) : json_object_array_length(obj))) { + json_object_put(obj); + obj = NULL; + } + break; + default: + goto internal_error; + } + switch (top->type) { + case 0: + if (top != stack) + goto internal_error; + if (*d) + goto invalid_character; + *result = obj; + return 0; + case ']': + if (obj || *d != '*') + json_object_array_add(top->cont, obj); + if (*d == '*') + d = skip(++d); + break; + case '}': + if (!obj) + goto null_key; + top->key = obj; + top->acc = pack_accept_any; + top->type = ':'; + break; + case ':': + if (obj || *d != '*') + json_object_object_add(top->cont, json_object_get_string(top->key), obj); + if (*d == '*') + d = skip(++d); + json_object_put(top->key); + top->key = NULL; + top->acc = pack_accept_key; + top->type = '}'; + break; + default: + goto internal_error; + } + } + +null_object: + rc = wrap_json_error_null_object; + goto error; +truncated: + rc = wrap_json_error_truncated; + goto error; +internal_error: + rc = wrap_json_error_internal_error; + goto error; +out_of_memory: + rc = wrap_json_error_out_of_memory; + goto error; +invalid_character: + rc = wrap_json_error_invalid_character; + goto error; +too_long: + rc = wrap_json_error_too_long; + goto error; +too_deep: + rc = wrap_json_error_too_deep; + goto error; +null_spec: + rc = wrap_json_error_null_spec; + goto error; +null_key: + rc = wrap_json_error_null_key; + goto error; +null_string: + rc = wrap_json_error_null_string; + goto error; +error: + do { + json_object_put(top->key); + json_object_put(top->cont); + } while (--top >= stack); + *result = NULL; + rc = rc | (int)((d - desc) << 4); + return -rc; +} + +int wrap_json_pack(struct json_object **result, const char *desc, ...) +{ + int rc; + va_list args; + + va_start(args, desc); + rc = wrap_json_vpack(result, desc, args); + va_end(args); + return rc; +} + +static int vunpack(struct json_object *object, const char *desc, va_list args, int store) +{ + int rc = 0, optionnal, ignore; + char c, xacc[2] = { 0, 0 }; + const char *acc; + const char *d, *fit = NULL; + const char *key = NULL; + const char **ps = NULL; + double *pf = NULL; + int *pi = NULL; + int64_t *pI = NULL; + size_t *pz = NULL; + struct { struct json_object *parent; const char *acc; int index, count; char type; } stack[STACKCOUNT], *top; + struct json_object *obj; + struct json_object **po; + + xacc[0] = 0; + ignore = 0; + top = NULL; + acc = unpack_accept_any; + d = desc; + if (!d) + goto null_spec; + d = skip(d); + obj = object; + for(;;) { + fit = d; + c = *d; + if (!c) + goto truncated; + if (!strchr(acc, c)) + goto invalid_character; + d = skip(++d); + switch(c) { + case 's': + if (xacc[0] == '}') { + /* expects a key */ + key = va_arg(args, const char *); + if (!key) + goto null_key; + if (*d != '?') + optionnal = 0; + else { + optionnal = 1; + d = skip(++d); + } + if (ignore) + ignore++; + else { + if (json_object_object_get_ex(top->parent, key, &obj)) { + /* found */ + top->index++; + } else { + /* not found */ + if (!optionnal) + goto key_not_found; + ignore = 1; + obj = NULL; + } + } + xacc[0] = ':'; + acc = unpack_accept_any; + continue; + } + /* get a string */ + if (store) + ps = va_arg(args, const char **); + if (!ignore) { + if (!json_object_is_type(obj, json_type_string)) + goto missfit; + if (store && ps) + *ps = json_object_get_string(obj); + } + if (*d == '%') { + d = skip(++d); + if (store) { + pz = va_arg(args, size_t *); + if (!ignore && pz) + *pz = (size_t)json_object_get_string_len(obj); + } + } + break; + case 'n': + if (!ignore && !json_object_is_type(obj, json_type_null)) + goto missfit; + break; + case 'b': + if (store) + pi = va_arg(args, int *); + + if (!ignore) { + if (!json_object_is_type(obj, json_type_boolean)) + goto missfit; + if (store && pi) + *pi = json_object_get_boolean(obj); + } + break; + case 'i': + if (store) + pi = va_arg(args, int *); + + if (!ignore) { + if (!json_object_is_type(obj, json_type_int)) + goto missfit; + if (store && pi) + *pi = json_object_get_int(obj); + } + break; + case 'I': + if (store) + pI = va_arg(args, int64_t *); + + if (!ignore) { + if (!json_object_is_type(obj, json_type_int)) + goto missfit; + if (store && pI) + *pI = json_object_get_int64(obj); + } + break; + case 'f': + case 'F': + if (store) + pf = va_arg(args, double *); + + if (!ignore) { + if (!(json_object_is_type(obj, json_type_double) || (c == 'F' && json_object_is_type(obj, json_type_int)))) + goto missfit; + if (store && pf) + *pf = json_object_get_double(obj); + } + break; + case 'o': + case 'O': + if (store) { + po = va_arg(args, struct json_object **); + if (!ignore && po) { + if (c == 'O') + obj = json_object_get(obj); + *po = obj; + } + } + break; + + case '[': + case '{': + if (!top) + top = stack; + else if (++top >= &stack[STACKCOUNT]) + goto too_deep; + + top->acc = acc; + top->type = xacc[0]; + top->index = 0; + top->parent = obj; + if (ignore) + ignore++; + if (c == '[') { + if (!ignore) { + if (!json_object_is_type(obj, json_type_array)) + goto missfit; + top->count = json_object_array_length(obj); + } + xacc[0] = ']'; + acc = unpack_accept_arr; + } else { + if (!ignore) { + if (!json_object_is_type(obj, json_type_object)) + goto missfit; + top->count = json_object_object_length(obj); + } + xacc[0] = '}'; + acc = unpack_accept_key; + continue; + } + break; + case '}': + case ']': + if (!top || c != xacc[0]) + goto internal_error; + acc = top->acc; + xacc[0] = top->type; + top = top == stack ? NULL : top - 1; + if (ignore) + ignore--; + break; + case '!': + if (*d != xacc[0]) + goto invalid_character; + if (!ignore && top->index != top->count) + goto incomplete; + /*@fallthrough@*/ + case '*': + acc = xacc; + continue; + default: + goto internal_error; + } + switch (xacc[0]) { + case 0: + if (top) + goto internal_error; + if (*d) + goto invalid_character; + return 0; + case ']': + if (!ignore) { + key = strchr(unpack_accept_arr, *d); + if (key && key >= unpack_accept_any) { + if (top->index >= top->count) + goto out_of_range; + obj = json_object_array_get_idx(top->parent, top->index++); + } + } + break; + case ':': + acc = unpack_accept_key; + xacc[0] = '}'; + if (ignore) + ignore--; + break; + default: + goto internal_error; + } + } +truncated: + rc = wrap_json_error_truncated; + goto error; +internal_error: + rc = wrap_json_error_internal_error; + goto error; +invalid_character: + rc = wrap_json_error_invalid_character; + goto error; +too_deep: + rc = wrap_json_error_too_deep; + goto error; +null_spec: + rc = wrap_json_error_null_spec; + goto error; +null_key: + rc = wrap_json_error_null_key; + goto error; +out_of_range: + rc = wrap_json_error_out_of_range; + goto error; +incomplete: + rc = wrap_json_error_incomplete; + goto error; +missfit: + rc = wrap_json_error_missfit_type; + goto errorfit; +key_not_found: + rc = wrap_json_error_key_not_found; + goto error; +errorfit: + d = fit; +error: + rc = rc | (int)((d - desc) << 4); + return -rc; +} + +int wrap_json_vcheck(struct json_object *object, const char *desc, va_list args) +{ + return vunpack(object, desc, args, 0); +} + +int wrap_json_check(struct json_object *object, const char *desc, ...) +{ + int rc; + va_list args; + + va_start(args, desc); + rc = vunpack(object, desc, args, 0); + va_end(args); + return rc; +} + +int wrap_json_vmatch(struct json_object *object, const char *desc, va_list args) +{ + return !vunpack(object, desc, args, 0); +} + +int wrap_json_match(struct json_object *object, const char *desc, ...) +{ + int rc; + va_list args; + + va_start(args, desc); + rc = vunpack(object, desc, args, 0); + va_end(args); + return !rc; +} + +int wrap_json_vunpack(struct json_object *object, const char *desc, va_list args) +{ + return vunpack(object, desc, args, 1); +} + +int wrap_json_unpack(struct json_object *object, const char *desc, ...) +{ + int rc; + va_list args; + + va_start(args, desc); + rc = vunpack(object, desc, args, 1); + va_end(args); + return rc; +} + +static void object_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure) +{ + struct json_object_iterator it = json_object_iter_begin(object); + struct json_object_iterator end = json_object_iter_end(object); + while (!json_object_iter_equal(&it, &end)) { + callback(closure, json_object_iter_peek_value(&it), json_object_iter_peek_name(&it)); + json_object_iter_next(&it); + } +} + +static void array_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure) +{ + int n = json_object_array_length(object); + int i = 0; + while(i < n) + callback(closure, json_object_array_get_idx(object, i++)); +} + +void wrap_json_optarray_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure) +{ + if (json_object_is_type(object, json_type_array)) + array_for_all(object, callback, closure); + else + callback(closure, object); +} + +void wrap_json_array_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure) +{ + if (json_object_is_type(object, json_type_array)) + array_for_all(object, callback, closure); +} + +void wrap_json_object_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure) +{ + if (json_object_is_type(object, json_type_object)) + object_for_all(object, callback, closure); +} + +void wrap_json_optobject_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure) +{ + if (json_object_is_type(object, json_type_object)) + object_for_all(object, callback, closure); + else + callback(closure, object, NULL); +} + +void wrap_json_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure) +{ + if (!object) + /* do nothing */; + else if (json_object_is_type(object, json_type_object)) + object_for_all(object, callback, closure); + else if (!json_object_is_type(object, json_type_array)) + callback(closure, object, NULL); + else { + int n = json_object_array_length(object); + int i = 0; + while(i < n) + callback(closure, json_object_array_get_idx(object, i++), NULL); + } +} + +#if defined(WRAP_JSON_TEST) +#include + +void p(const char *desc, ...) +{ + int rc; + va_list args; + struct json_object *result; + + va_start(args, desc); + rc = wrap_json_vpack(&result, desc, args); + va_end(args); + if (!rc) + printf(" SUCCESS %s\n\n", json_object_to_json_string(result)); + else + printf(" ERROR[char %d err %d] %s\n\n", wrap_json_get_error_position(rc), wrap_json_get_error_code(rc), wrap_json_get_error_string(rc)); + json_object_put(result); +} + +const char *xs[10]; +int *xi[10]; +int64_t *xI[10]; +double *xf[10]; +struct json_object *xo[10]; +size_t xz[10]; + +void u(const char *value, const char *desc, ...) +{ + unsigned m, k; + int rc; + va_list args; + struct json_object *obj, *o; + + memset(xs, 0, sizeof xs); + memset(xi, 0, sizeof xi); + memset(xI, 0, sizeof xI); + memset(xf, 0, sizeof xf); + memset(xo, 0, sizeof xo); + memset(xz, 0, sizeof xz); + obj = json_tokener_parse(value); + va_start(args, desc); + rc = wrap_json_vunpack(obj, desc, args); + va_end(args); + if (rc) + printf(" ERROR[char %d err %d] %s\n\n", wrap_json_get_error_position(rc), wrap_json_get_error_code(rc), wrap_json_get_error_string(rc)); + else { + value = NULL; + printf(" SUCCESS"); + va_start(args, desc); + k = m = 0; + while(*desc) { + switch(*desc) { + case '{': m = (m << 1) | 1; k = 1; break; + case '}': m = m >> 1; k = m&1; break; + case '[': m = m << 1; k = 0; break; + case ']': m = m >> 1; k = m&1; break; + case 's': printf(" s:%s", k ? va_arg(args, const char*) : *(va_arg(args, const char**)?:&value)); k ^= m&1; break; + case '%': printf(" %%:%zu", *va_arg(args, size_t*)); k = m&1; break; + case 'n': printf(" n"); k = m&1; break; + case 'b': printf(" b:%d", *va_arg(args, int*)); k = m&1; break; + case 'i': printf(" i:%d", *va_arg(args, int*)); k = m&1; break; + case 'I': printf(" I:%lld", *va_arg(args, int64_t*)); k = m&1; break; + case 'f': printf(" f:%f", *va_arg(args, double*)); k = m&1; break; + case 'F': printf(" F:%f", *va_arg(args, double*)); k = m&1; break; + case 'o': printf(" o:%s", json_object_to_json_string(*va_arg(args, struct json_object**))); k = m&1; break; + case 'O': o = *va_arg(args, struct json_object**); printf(" O:%s", json_object_to_json_string(o)); json_object_put(o); k = m&1; break; + default: break; + } + desc++; + } + va_end(args); + printf("\n\n"); + } + json_object_put(obj); +} + +#define P(...) do{ printf("pack(%s)\n",#__VA_ARGS__); p(__VA_ARGS__); } while(0) +#define U(...) do{ printf("unpack(%s)\n",#__VA_ARGS__); u(__VA_ARGS__); } while(0) + +int main() +{ + char buffer[4] = {'t', 'e', 's', 't'}; + + P("n"); + P("b", 1); + P("b", 0); + P("i", 1); + P("I", (uint64_t)0x123456789abcdef); + P("f", 3.14); + P("s", "test"); + P("s?", "test"); + P("s?", NULL); + P("s#", "test asdf", 4); + P("s%", "test asdf", (size_t)4); + P("s#", buffer, 4); + P("s%", buffer, (size_t)4); + P("s++", "te", "st", "ing"); + P("s#+#+", "test", 1, "test", 2, "test"); + P("s%+%+", "test", (size_t)1, "test", (size_t)2, "test"); + P("{}", 1.0); + P("[]", 1.0); + P("o", json_object_new_int(1)); + P("o?", json_object_new_int(1)); + P("o?", NULL); + P("O", json_object_new_int(1)); + P("O?", json_object_new_int(1)); + P("O?", NULL); + P("{s:[]}", "foo"); + P("{s+#+: []}", "foo", "barbar", 3, "baz"); + P("{s:s,s:o,s:O}", "a", NULL, "b", NULL, "c", NULL); + P("{s:**}", "a", NULL); + P("{s:s*,s:o*,s:O*}", "a", NULL, "b", NULL, "c", NULL); + P("[i,i,i]", 0, 1, 2); + P("[s,o,O]", NULL, NULL, NULL); + P("[**]", NULL); + P("[s*,o*,O*]", NULL, NULL, NULL); + P(" s ", "test"); + P("[ ]"); + P("[ i , i, i ] ", 1, 2, 3); + P("{\n\n1"); + P("[}"); + P("{]"); + P("["); + P("{"); + P("[i]a", 42); + P("ia", 42); + P("s", NULL); + P("+", NULL); + P(NULL); + P("{s:i}", NULL, 1); + P("{ {}: s }", "foo"); + P("{ s: {}, s:[ii{} }", "foo", "bar", 12, 13); + P("[[[[[ [[[[[ [[[[ }]]]] ]]]] ]]]]]"); + + U("true", "b", &xi[0]); + U("false", "b", &xi[0]); + U("null", "n"); + U("42", "i", &xi[0]); + U("123456789", "I", &xI[0]); + U("3.14", "f", &xf[0]); + U("12345", "F", &xf[0]); + U("3.14", "F", &xf[0]); + U("\"foo\"", "s", &xs[0]); + U("\"foo\"", "s%", &xs[0], &xz[0]); + U("{}", "{}"); + U("[]", "[]"); + U("{}", "o", &xo[0]); + U("{}", "O", &xo[0]); + U("{\"foo\":42}", "{si}", "foo", &xi[0]); + U("[1,2,3]", "[i,i,i]", &xi[0], &xi[1], &xi[2]); + U("{\"a\":1,\"b\":2,\"c\":3}", "{s:i, s:i, s:i}", "a", &xi[0], "b", &xi[1], "c", &xi[2]); + U("42", "z"); + U("null", "[i]"); + U("[]", "[}"); + U("{}", "{]"); + U("[]", "["); + U("{}", "{"); + U("[42]", "[i]a", &xi[0]); + U("42", "ia", &xi[0]); + U("[]", NULL); + U("\"foo\"", "s", NULL); + U("42", "s", NULL); + U("42", "n"); + U("42", "b", NULL); + U("42", "f", NULL); + U("42", "[i]", NULL); + U("42", "{si}", "foo", NULL); + U("\"foo\"", "n"); + U("\"foo\"", "b", NULL); + U("\"foo\"", "i", NULL); + U("\"foo\"", "I", NULL); + U("\"foo\"", "f", NULL); + U("\"foo\"", "F", NULL); + U("true", "s", NULL); + U("true", "n"); + U("true", "i", NULL); + U("true", "I", NULL); + U("true", "f", NULL); + U("true", "F", NULL); + U("[42]", "[ii]", &xi[0], &xi[1]); + U("{\"foo\":42}", "{si}", NULL, &xi[0]); + U("{\"foo\":42}", "{si}", "baz", &xi[0]); + U("[1,2,3]", "[iii!]", &xi[0], &xi[1], &xi[2]); + U("[1,2,3]", "[ii!]", &xi[0], &xi[1]); + U("[1,2,3]", "[ii]", &xi[0], &xi[1]); + U("[1,2,3]", "[ii*]", &xi[0], &xi[1]); + U("{\"foo\":42,\"baz\":45}", "{sisi}", "baz", &xi[0], "foo", &xi[1]); + U("{\"foo\":42,\"baz\":45}", "{sisi*}", "baz", &xi[0], "foo", &xi[1]); + U("{\"foo\":42,\"baz\":45}", "{sisi!}", "baz", &xi[0], "foo", &xi[1]); + U("{\"foo\":42,\"baz\":45}", "{si}", "baz", &xi[0], "foo", &xi[1]); + U("{\"foo\":42,\"baz\":45}", "{si*}", "baz", &xi[0], "foo", &xi[1]); + U("{\"foo\":42,\"baz\":45}", "{si!}", "baz", &xi[0], "foo", &xi[1]); + U("[1,{\"foo\":2,\"bar\":null},[3,4]]", "[i{sisn}[ii]]", &xi[0], "foo", &xi[1], "bar", &xi[2], &xi[3]); + U("[1,2,3]", "[ii!i]", &xi[0], &xi[1], &xi[2]); + U("[1,2,3]", "[ii*i]", &xi[0], &xi[1], &xi[2]); + U("{\"foo\":1,\"bar\":2}", "{si!si}", "foo", &xi[1], "bar", &xi[2]); + U("{\"foo\":1,\"bar\":2}", "{si*si}", "foo", &xi[1], "bar", &xi[2]); + U("{\"foo\":{\"baz\":null,\"bar\":null}}", "{s{sn!}}", "foo", "bar"); + U("[[1,2,3]]", "[[ii!]]", &xi[0], &xi[1]); + U("{}", "{s?i}", "foo", &xi[0]); + U("{\"foo\":1}", "{s?i}", "foo", &xi[0]); + U("{}", "{s?[ii]s?{s{si!}}}", "foo", &xi[0], &xi[1], "bar", "baz", "quux", &xi[2]); + U("{\"foo\":[1,2]}", "{s?[ii]s?{s{si!}}}", "foo", &xi[0], &xi[1], "bar", "baz", "quux", &xi[2]); + U("{\"bar\":{\"baz\":{\"quux\":15}}}", "{s?[ii]s?{s{si!}}}", "foo", &xi[0], &xi[1], "bar", "baz", "quux", &xi[2]); + U("{\"foo\":{\"bar\":4}}", "{s?{s?i}}", "foo", "bar", &xi[0]); + U("{\"foo\":{}}", "{s?{s?i}}", "foo", "bar", &xi[0]); + U("{}", "{s?{s?i}}", "foo", "bar", &xi[0]); + U("{\"foo\":42,\"baz\":45}", "{s?isi!}", "baz", &xi[0], "foo", &xi[1]); + U("{\"foo\":42}", "{s?isi!}", "baz", &xi[0], "foo", &xi[1]); + return 0; +} + +#endif + +#if 0 + + + /* Unpack the same item twice */ + j = json_pack("{s:s, s:i, s:b}", "foo", "bar", "baz", 42, "quux", 1); + if(!json_unpack_ex(j, &error, 0, "{s:s,s:s!}", "foo", &s, "foo", &s)) + fail("json_unpack object with strict validation failed"); + { + const char *possible_errors[] = { + "2 object item(s) left unpacked: baz, quux", + "2 object item(s) left unpacked: quux, baz" + }; + check_errors(possible_errors, 2, "", 1, 10, 10); + } + json_decref(j); + +#endif + \ No newline at end of file diff --git a/HAL-afb/hal-most-unicens/wrap-json.h b/HAL-afb/hal-most-unicens/wrap-json.h new file mode 100644 index 0000000..b5eadf5 --- /dev/null +++ b/HAL-afb/hal-most-unicens/wrap-json.h @@ -0,0 +1,47 @@ +/* + Copyright (C) 2016, 2017 "IoT.bzh" + + author: José Bollo + + 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. +*/ + +#pragma once + +#include +#include + +extern int wrap_json_get_error_position(int rc); +extern int wrap_json_get_error_code(int rc); +extern const char *wrap_json_get_error_string(int rc); + +extern int wrap_json_vpack(struct json_object **result, const char *desc, va_list args); +extern int wrap_json_pack(struct json_object **result, const char *desc, ...); + +extern int wrap_json_vunpack(struct json_object *object, const char *desc, va_list args); +extern int wrap_json_unpack(struct json_object *object, const char *desc, ...); +extern int wrap_json_vcheck(struct json_object *object, const char *desc, va_list args); +extern int wrap_json_check(struct json_object *object, const char *desc, ...); +extern int wrap_json_vmatch(struct json_object *object, const char *desc, va_list args); +extern int wrap_json_match(struct json_object *object, const char *desc, ...); + +extern void wrap_json_optarray_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure); +extern void wrap_json_array_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure); + +extern void wrap_json_optarray_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure); +extern void wrap_json_array_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure); +extern void wrap_json_object_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure); +extern void wrap_json_optobject_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure); +extern void wrap_json_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure); + + \ No newline at end of file diff --git a/HAL-afb/hal-most-unicens/wrap-json.md b/HAL-afb/hal-most-unicens/wrap-json.md new file mode 100644 index 0000000..3718b66 --- /dev/null +++ b/HAL-afb/hal-most-unicens/wrap-json.md @@ -0,0 +1,306 @@ +WRAP-JSON facility +================== + +The facility wrap-json is based on the pack/unpack API on the +libray jansson. The two chapters below are copied from the +documentation of jansson library copyrighted by Petri Lehtinen +(see at end). + +Building Values +--------------- + +This section describes functions that help to create, or *pack*, complex +JSON values, especially nested objects and arrays. Value building is +based on a *format string* that is used to tell the functions about the +expected arguments. + +For example, the format string `"i"` specifies a single integer value, +while the format string `"[ssb]"` or the equivalent `"[s, s, b]"` +specifies an array value with two strings and a boolean as its items: + + /* Create the JSON integer 42 */ + wrap_json_pack(&result, "i", 42); + + /* Create the JSON array ["foo", "bar", true] */ + wrap_json_pack(&result, "[ssb]", "foo", "bar", 1); + +Here's the full list of format specifiers. The type in parentheses +denotes the resulting JSON type, and the type in brackets (if any) +denotes the C type that is expected as the corresponding argument or +arguments. + +`s` (string) \[const char \*\] + +: Convert a null terminated UTF-8 string to a JSON string. + +`s?` (string) \[const char \*\] + +: Like `s`, but if the argument is *NULL*, output a JSON null value. + +`s*` (string) \[const char \*\] + +: Like `s`, but if the argument is *NULL*, do not output any value. + This format can only be used inside an object or an array. If used + inside an object, the corresponding key is additionally suppressed + when the value is omitted. See below for an example. + +`s#` (string) \[const char \*, int\] + +: Convert a UTF-8 buffer of a given length to a JSON string. + +`s%` (string) \[const char \*, size\_t\] + +: Like `s#` but the length argument is of type size\_t. + +`+` \[const char \*\] + +: Like `s`, but concatenate to the previous string. Only valid after + `s`, `s#`, `+` or `+#`. + +`+#` \[const char \*, int\] + +: Like `s#`, but concatenate to the previous string. Only valid after + `s`, `s#`, `+` or `+#`. + +`+%` (string) \[const char \*, size\_t\] + +: Like `+#` but the length argument is of type size\_t. + +`n` (null) + +: Output a JSON null value. No argument is consumed. + +`b` (boolean) \[int\] + +: Convert a C int to JSON boolean value. Zero is converted to `false` + and non-zero to `true`. + +`i` (integer) \[int\] + +: Convert a C int to JSON integer. + +`I` (integer) \[json\_int\_t\] + +: Convert a C json\_int\_t to JSON integer. + +`f` (real) \[double\] + +: Convert a C double to JSON real. + +`o` (any value) \[json\_t \*\] + +: Output any given JSON value as-is. If the value is added to an array + or object, the reference to the value passed to `o` is stolen by the + container. + +`O` (any value) \[json\_t \*\] + +: Like `o`, but the argument's reference count is incremented. This is + useful if you pack into an array or object and want to keep the + reference for the JSON value consumed by `O` to yourself. + +`o?`, `O?` (any value) \[json\_t \*\] + +: Like `o` and `O`, respectively, but if the argument is *NULL*, + output a JSON null value. + +`o*`, `O*` (any value) \[json\_t \*\] + +: Like `o` and `O`, respectively, but if the argument is *NULL*, do + not output any value. This format can only be used inside an object + or an array. If used inside an object, the corresponding key is + additionally suppressed. See below for an example. + +`[fmt]` (array) + +: Build an array with contents from the inner format string. `fmt` may + contain objects and arrays, i.e. recursive value building is + supported. + +`{fmt}` (object) + +: Build an object with contents from the inner format string `fmt`. + The first, third, etc. format specifier represent a key, and must be + a string (see `s`, `s#`, `+` and `+#` above), as object keys are + always strings. The second, fourth, etc. format specifier represent + a value. Any value may be an object or array, i.e. recursive value + building is supported. + +Whitespace, `:` and `,` are ignored. + +More examples: + + /* Build an empty JSON object */ + wrap_json_pack(&result, "{}"); + + /* Build the JSON object {"foo": 42, "bar": 7} */ + wrap_json_pack(&result, "{sisi}", "foo", 42, "bar", 7); + + /* Like above, ':', ',' and whitespace are ignored */ + wrap_json_pack(&result, "{s:i, s:i}", "foo", 42, "bar", 7); + + /* Build the JSON array [[1, 2], {"cool": true}] */ + wrap_json_pack(&result, "[[i,i],{s:b}]", 1, 2, "cool", 1); + + /* Build a string from a non-null terminated buffer */ + char buffer[4] = {'t', 'e', 's', 't'}; + wrap_json_pack(&result, "s#", buffer, 4); + + /* Concatenate strings together to build the JSON string "foobarbaz" */ + wrap_json_pack(&result, "s++", "foo", "bar", "baz"); + + /* Create an empty object or array when optional members are missing */ + wrap_json_pack(&result, "{s:s*,s:o*,s:O*}", "foo", NULL, "bar", NULL, "baz", NULL); + wrap_json_pack(&result, "[s*,o*,O*]", NULL, NULL, NULL); + +Parsing and Validating Values +----------------------------- + +This section describes functions that help to validate complex values +and extract, or *unpack*, data from them. Like building values +<apiref-pack>, this is also based on format strings. + +While a JSON value is unpacked, the type specified in the format string +is checked to match that of the JSON value. This is the validation part +of the process. In addition to this, the unpacking functions can also +check that all items of arrays and objects are unpacked. This check be +enabled with the format specifier `!` or by using the flag +`JSON_STRICT`. See below for details. + +Here's the full list of format specifiers. The type in parentheses +denotes the JSON type, and the type in brackets (if any) denotes the C +type whose address should be passed. + +`s` (string) \[const char \*\] + +: Convert a JSON string to a pointer to a null terminated UTF-8 + string. The resulting string is extracted by using + json\_string\_value() internally, so it exists as long as there are + still references to the corresponding JSON string. + +`s%` (string) \[const char \*, size\_t \*\] + +: Convert a JSON string to a pointer to a null terminated UTF-8 string + and its length. + +`n` (null) + +: Expect a JSON null value. Nothing is extracted. + +`b` (boolean) \[int\] + +: Convert a JSON boolean value to a C int, so that `true` is converted + to 1 and `false` to 0. + +`i` (integer) \[int\] + +: Convert a JSON integer to C int. + +`I` (integer) \[json\_int\_t\] + +: Convert a JSON integer to C json\_int\_t. + +`f` (real) \[double\] + +: Convert a JSON real to C double. + +`F` (integer or real) \[double\] + +: Convert a JSON number (integer or real) to C double. + +`o` (any value) \[json\_t \*\] + +: Store a JSON value with no conversion to a json\_t pointer. + +`O` (any value) \[json\_t \*\] + +: Like `O`, but the JSON value's reference count is incremented. + +`[fmt]` (array) + +: Convert each item in the JSON array according to the inner format + string. `fmt` may contain objects and arrays, i.e. recursive value + extraction is supported. + +`{fmt}` (object) + +: Convert each item in the JSON object according to the inner format + string `fmt`. The first, third, etc. format specifier represent a + key, and must be `s`. The corresponding argument to unpack functions + is read as the object key. The second fourth, etc. format specifier + represent a value and is written to the address given as the + corresponding argument. **Note** that every other argument is read + from and every other is written to. + + `fmt` may contain objects and arrays as values, i.e. recursive value + extraction is supported. + +`!` + +: This special format specifier is used to enable the check that all + object and array items are accessed, on a per-value basis. It must + appear inside an array or object as the last format specifier before + the closing bracket or brace. + +`*` + +: This special format specifier is the opposite of `!`. This is the default. + It must appear inside an array or object as the last format specifier + before the closing bracket or brace. + +Whitespace, `:` and `,` are ignored. + +Examples: + + /* root is the JSON integer 42 */ + int myint; + wrap_json_unpack(root, "i", &myint); + assert(myint == 42); + + /* root is the JSON object {"foo": "bar", "quux": true} */ + const char *str; + int boolean; + wrap_json_unpack(root, "{s:s, s:b}", "foo", &str, "quux", &boolean); + assert(strcmp(str, "bar") == 0 && boolean == 1); + + /* root is the JSON array [[1, 2], {"baz": null} */ + wrap_json_check(root, "[[i,i], {s:n}]", "baz"); + /* returns 0 for validation success, nothing is extracted */ + + /* root is the JSON array [1, 2, 3, 4, 5] */ + int myint1, myint2; + wrap_json_unpack(root, "[ii!]", &myint1, &myint2); + /* returns -1 for failed validation */ + + /* root is an empty JSON object */ + int myint = 0, myint2 = 0, myint3 = 0; + wrap_json_unpack(root, "{s?i, s?[ii]}", + "foo", &myint1, + "bar", &myint2, &myint3); + /* myint1, myint2 or myint3 is no touched as "foo" and "bar" don't exist */ + + +Copyright +--------- + +Copyright (c) 2009-2016 Petri Lehtinen + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + + \ No newline at end of file -- cgit 1.2.3-korg From e94c8800c515dc64a7d7ae275e607e23591b8498 Mon Sep 17 00:00:00 2001 From: Tobias Jahnke Date: Wed, 9 Aug 2017 14:14:50 +0200 Subject: introduced automatic connection/start of unicens2-binding --- HAL-afb/HDA-intel/CMakeLists.txt | 4 +- HAL-afb/hal-most-unicens/hal_most_unicens.c | 120 +++++++++++++++++++++++++--- nbproject/configurations.xml | 36 ++++++--- 3 files changed, 137 insertions(+), 23 deletions(-) diff --git a/HAL-afb/HDA-intel/CMakeLists.txt b/HAL-afb/HDA-intel/CMakeLists.txt index 37e277b..94234b4 100644 --- a/HAL-afb/HDA-intel/CMakeLists.txt +++ b/HAL-afb/HDA-intel/CMakeLists.txt @@ -16,6 +16,7 @@ # limitations under the License. ########################################################################### +#[[ # Add target to project dependency list PROJECT_TARGET_ADD(hal-intel-hda) @@ -39,4 +40,5 @@ PROJECT_TARGET_ADD(hal-intel-hda) # installation directory INSTALL(TARGETS ${TARGET_NAME} - LIBRARY DESTINATION ${BINDINGS_INSTALL_DIR}) \ No newline at end of file + LIBRARY DESTINATION ${BINDINGS_INSTALL_DIR}) + ]] \ No newline at end of file diff --git a/HAL-afb/hal-most-unicens/hal_most_unicens.c b/HAL-afb/hal-most-unicens/hal_most_unicens.c index 3ab7ace..a1a1e66 100644 --- a/HAL-afb/hal-most-unicens/hal_most_unicens.c +++ b/HAL-afb/hal-most-unicens/hal_most_unicens.c @@ -19,6 +19,7 @@ #include "hal-interface.h" #include "audio-interface.h" #include "wrap-json.h" +#include "string.h" static int master_volume; static json_bool master_switch; @@ -64,13 +65,13 @@ void unicens_pcm_vol_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, /* declare ALSA mixer controls */ STATIC alsaHalMapT alsaHalMap[]= { { .tag=Master_Playback_Volume, .cb={.callback=unicens_master_vol_cb, .handle=&master_volume}, .info="Sets master playback volume", - .ctl={.numid=0, .type=SND_CTL_ELEM_TYPE_INTEGER, .count=1, .minval=0, .maxval=100, .step=1, .value=50, .name="Master Playback Volume"} + .ctl={.numid=1, .type=SND_CTL_ELEM_TYPE_INTEGER, .count=1, .minval=0, .maxval=100, .step=1, .value=50, .name="Master Playback Volume"} }, - { .tag=Master_OnOff_Switch, .cb={.callback=unicens_master_switch_cb, .handle=&master_switch}, .info="Sets master playback switch", - .ctl={.numid=0, .type=SND_CTL_ELEM_TYPE_BOOLEAN, .count=1, .minval=0, .maxval=1, .step=1, .value=1, .name="Master Playback Switch"} - }, + /*{ .tag=Master_OnOff_Switch, .cb={.callback=unicens_master_switch_cb, .handle=&master_switch}, .info="Sets master playback switch", + .ctl={.numid=2, .type=SND_CTL_ELEM_TYPE_BOOLEAN, .count=1, .minval=0, .maxval=1, .step=1, .value=1, .name="Master Playback Switch"} + },*/ { .tag=PCM_Playback_Volume, .cb={.callback=unicens_pcm_vol_cb, .handle=&pcm_volume}, .info="Sets PCM playback volume", - .ctl={.numid=0, .type=SND_CTL_ELEM_TYPE_INTEGER, .count=6, .minval=0, .maxval=100, .step=1, .value=100, .name="PCM Playback Volume"} + .ctl={.numid=3, .type=SND_CTL_ELEM_TYPE_INTEGER, .count=6, .minval=0, .maxval=100, .step=1, .value=100, .name="PCM Playback Volume"} }, { .tag=EndHalCrlTag} /* marker for end of the array */ } ; @@ -83,18 +84,117 @@ STATIC alsaHalSndCardT alsaHalSndCard = { .volumeCB = NULL, /* use default volume normalization function */ }; -STATIC int sndServiceInit () { +STATIC int unicens_start_binding() { + + json_object *j_response, *j_query = NULL; int err; - AFB_DEBUG ("Initializing HAL-MOST-UNICENS-BINDING"); - err = halServiceInit (afbBindingV2.api, &alsaHalSndCard); + /* Build an empty JSON object */ + err = wrap_json_pack(&j_query, "{}"); + if (err) { + AFB_ERROR("Failed to create subscribe json object"); + goto OnErrorExit; + } + + err = afb_service_call_sync("UNICENS", "subscribe", j_query, &j_response); + if (err) { + AFB_ERROR("Fail subscribing to UNICENS events"); + goto OnErrorExit; + } + else { + AFB_NOTICE("Subscribed to UNICENS events, res=%s", json_object_to_json_string(j_response)); + json_object_put(j_response); + } + json_object_put(j_query); +#if 0 + /* Build JSON object to retrieve UNICENS configuration */ + err = wrap_json_pack(&j_query, "{}"); + if (err) { + AFB_ERROR("Failed to create listconfig json object"); + goto OnErrorExit; + } + + err = afb_service_call_sync("UNICENS", "listconfig", j_query, &j_response); + if (err) { + AFB_ERROR("Failed to call listconfig"); + goto OnErrorExit; + } + else { + AFB_NOTICE("UNICENS listconfig result, res=%s", json_object_to_json_string(j_response)); + json_object_put(j_response); + } + json_object_put(j_query); +#endif + + /* Build JSON object to initialize UNICENS */ + err = wrap_json_pack(&j_query, "{s:s}", "filename", "/home/agluser/DEVELOPMENT/AGL/BINDING/unicens2-binding/data/config_multichannel_audio_kit.xml"); + if (err) { + AFB_ERROR("Failed to create initialize json object"); + goto OnErrorExit; + } + err = afb_service_call_sync("UNICENS", "initialise", j_query, &j_response); + if (err) { + AFB_ERROR("Failed to initialize UNICENS"); + goto OnErrorExit; + } + else { + AFB_NOTICE("Initialized UNICENS, res=%s", json_object_to_json_string(j_response)); + json_object_put(j_response); + } + json_object_put(j_query); + + + j_query = NULL; + OnErrorExit: + if (j_query) + json_object_put(j_query); + return NULL; +} + +STATIC int unicens_service_init() { + int err; + AFB_NOTICE("Initializing HAL-MOST-UNICENS-BINDING"); + + err = halServiceInit(afbBindingV2.api, &alsaHalSndCard); + if (err) { + AFB_ERROR("Cannot initialize hal-most-unicens binding."); + goto OnErrorExit; + } + + err= afb_daemon_require_api("UNICENS", 1); + if (err) { + AFB_ERROR("UNICENS is missing or not initialized"); + goto OnErrorExit; + } + + unicens_start_binding(); + + +OnErrorExit: + AFB_NOTICE("Initializing HAL-MOST-UNICENS-BINDING done.."); return err; } +// This receive all event this binding subscribe to +PUBLIC void unicens_event_cb(const char *evtname, json_object *j_event) { + + if (strncmp(evtname, "alsacore/", 9) == 0) { + halServiceEvent(evtname, j_event); + return; + } + + if (strncmp(evtname, "UNICENS/", 8) == 0) { + AFB_NOTICE("unicens_event_cb: evtname=%s [msg=%s]", evtname, json_object_get_string(j_event)); + return; + } + + AFB_NOTICE("unicens_event_cb: UNHANDLED EVENT, evtname=%s [msg=%s]", evtname, json_object_get_string(j_event)); +} + /* API prefix should be unique for each snd card */ PUBLIC const struct afb_binding_v2 afbBindingV2 = { .api = "hal-most-unicens", - .init = sndServiceInit, + .init = unicens_service_init, .verbs = halServiceApi, - .onevent = halServiceEvent, + .onevent = unicens_event_cb, }; diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 3f333ec..d22e757 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -59,6 +59,7 @@ hal_most_unicens.c + wrap-json.c HalPlugPcm.c @@ -396,6 +397,29 @@ tool="0" flavor2="3"> + + /opt/AGL/include/afb + HAL-afb/hal-most-unicens + /usr/include/json-c + Shared-Interface + HAL-afb/HAL-interface + /usr/lib64/gcc/x86_64-suse-linux/5/include + /opt/AGL/include + build/HAL-afb/hal-most-unicens + + + + + + + HAL-afb/hal-most-unicens + /usr/lib64/gcc/x86_64-suse-linux/5/include + /usr/include/json-c + build/HAL-afb/hal-most-unicens + @@ -617,18 +641,6 @@ - - - - HAL-afb/Scarlett-Focusrite - /usr/include/json-c - Shared-Interface - HAL-afb/HAL-interface - ../../../opt/include - build/HAL-afb/Scarlett-Focusrite - - - -- cgit 1.2.3-korg From cf590ad67211ee634d4cb24ca97c9e65891240b9 Mon Sep 17 00:00:00 2001 From: Tobias Jahnke Date: Thu, 10 Aug 2017 10:35:39 +0200 Subject: implements wrapper for unicens functions --- HAL-afb/hal-most-unicens/CMakeLists.txt | 2 +- HAL-afb/hal-most-unicens/hal_most_unicens.c | 91 +++-------- HAL-afb/hal-most-unicens/wrap_unicens.c | 227 ++++++++++++++++++++++++++++ HAL-afb/hal-most-unicens/wrap_unicens.h | 27 ++++ nbproject/configurations.xml | 22 +++ 5 files changed, 297 insertions(+), 72 deletions(-) create mode 100644 HAL-afb/hal-most-unicens/wrap_unicens.c create mode 100644 HAL-afb/hal-most-unicens/wrap_unicens.h diff --git a/HAL-afb/hal-most-unicens/CMakeLists.txt b/HAL-afb/hal-most-unicens/CMakeLists.txt index c87ca4d..828614f 100644 --- a/HAL-afb/hal-most-unicens/CMakeLists.txt +++ b/HAL-afb/hal-most-unicens/CMakeLists.txt @@ -21,7 +21,7 @@ PROJECT_TARGET_ADD(hal-most-unicens) # Define project Targets - ADD_LIBRARY(hal-most-unicens MODULE hal_most_unicens.c wrap-json.c) + ADD_LIBRARY(hal-most-unicens MODULE hal_most_unicens.c wrap-json.c wrap_unicens.c) # Binder exposes a unique public entry point SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES diff --git a/HAL-afb/hal-most-unicens/hal_most_unicens.c b/HAL-afb/hal-most-unicens/hal_most_unicens.c index a1a1e66..394e502 100644 --- a/HAL-afb/hal-most-unicens/hal_most_unicens.c +++ b/HAL-afb/hal-most-unicens/hal_most_unicens.c @@ -20,17 +20,23 @@ #include "audio-interface.h" #include "wrap-json.h" #include "string.h" +#include "wrap_unicens.h" static int master_volume; static json_bool master_switch; static int pcm_volume[6]; +static uint8_t test[3] = {0x07,0x03,0xFF}; + void unicens_master_vol_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { const char *j_str = json_object_to_json_string(j_obj); if (wrap_json_unpack(j_obj, "[i!]", &master_volume) == 0) { AFB_NOTICE("master_volume: %s, value=%d", j_str, master_volume); + /*wrap_ucs_i2cwrite(0x270, test, 3); + wrap_ucs_i2cwrite(0x271, test, 3); + wrap_ucs_i2cwrite(0x272, test, 3);*/ } else { AFB_NOTICE("master_volume: INVALID STRING %s", j_str); @@ -84,92 +90,35 @@ STATIC alsaHalSndCardT alsaHalSndCard = { .volumeCB = NULL, /* use default volume normalization function */ }; -STATIC int unicens_start_binding() { - - json_object *j_response, *j_query = NULL; - int err; - - /* Build an empty JSON object */ - err = wrap_json_pack(&j_query, "{}"); - if (err) { - AFB_ERROR("Failed to create subscribe json object"); - goto OnErrorExit; - } +/* initializes ALSA sound card, UNICENS API */ +STATIC int unicens_service_init() { + int err = 0; + AFB_NOTICE("Initializing HAL-MOST-UNICENS-BINDING"); - err = afb_service_call_sync("UNICENS", "subscribe", j_query, &j_response); + err = halServiceInit(afbBindingV2.api, &alsaHalSndCard); if (err) { - AFB_ERROR("Fail subscribing to UNICENS events"); + AFB_ERROR("Cannot initialize ALSA soundcard."); goto OnErrorExit; - } - else { - AFB_NOTICE("Subscribed to UNICENS events, res=%s", json_object_to_json_string(j_response)); - json_object_put(j_response); - } - json_object_put(j_query); -#if 0 - /* Build JSON object to retrieve UNICENS configuration */ - err = wrap_json_pack(&j_query, "{}"); - if (err) { - AFB_ERROR("Failed to create listconfig json object"); - goto OnErrorExit; - } + } - err = afb_service_call_sync("UNICENS", "listconfig", j_query, &j_response); + err= afb_daemon_require_api("UNICENS", 1); if (err) { - AFB_ERROR("Failed to call listconfig"); + AFB_ERROR("Failed to access UNICENS API"); goto OnErrorExit; } - else { - AFB_NOTICE("UNICENS listconfig result, res=%s", json_object_to_json_string(j_response)); - json_object_put(j_response); - } - json_object_put(j_query); -#endif - /* Build JSON object to initialize UNICENS */ - err = wrap_json_pack(&j_query, "{s:s}", "filename", "/home/agluser/DEVELOPMENT/AGL/BINDING/unicens2-binding/data/config_multichannel_audio_kit.xml"); + err = wrap_ucs_subscribe_sync(); if (err) { - AFB_ERROR("Failed to create initialize json object"); - goto OnErrorExit; - } - err = afb_service_call_sync("UNICENS", "initialise", j_query, &j_response); - if (err) { - AFB_ERROR("Failed to initialize UNICENS"); + AFB_ERROR("Failed to subscribe to unicensv2-binding"); goto OnErrorExit; } - else { - AFB_NOTICE("Initialized UNICENS, res=%s", json_object_to_json_string(j_response)); - json_object_put(j_response); - } - json_object_put(j_query); - - - j_query = NULL; - OnErrorExit: - if (j_query) - json_object_put(j_query); - return NULL; -} - -STATIC int unicens_service_init() { - int err; - AFB_NOTICE("Initializing HAL-MOST-UNICENS-BINDING"); - err = halServiceInit(afbBindingV2.api, &alsaHalSndCard); - if (err) { - AFB_ERROR("Cannot initialize hal-most-unicens binding."); - goto OnErrorExit; - } - - err= afb_daemon_require_api("UNICENS", 1); + err = wrap_ucs_initialize_sync("/home/agluser/DEVELOPMENT/AGL/BINDING/unicens2-binding/data/config_multichannel_audio_kit.xml"); if (err) { - AFB_ERROR("UNICENS is missing or not initialized"); - goto OnErrorExit; + AFB_ERROR("Failed to initialize unicens-v2 binding"); + goto OnErrorExit; } - unicens_start_binding(); - - OnErrorExit: AFB_NOTICE("Initializing HAL-MOST-UNICENS-BINDING done.."); return err; diff --git a/HAL-afb/hal-most-unicens/wrap_unicens.c b/HAL-afb/hal-most-unicens/wrap_unicens.c new file mode 100644 index 0000000..15f94a1 --- /dev/null +++ b/HAL-afb/hal-most-unicens/wrap_unicens.c @@ -0,0 +1,227 @@ +/* + * Copyright (C) 2017, Microchip Technology Inc. and its subsidiaries. + * Author Tobias Jahnke + * + * 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 +#define AFB_BINDING_VERSION 2 + +#include +#include +#include + +#include "wrap_unicens.h" +#include "wrap-json.h" + +/* Subscribes to unicens2-binding events. + * \return Returns 0 if successful, otherwise != 0". + */ +extern int wrap_ucs_subscribe_sync() { + + json_object *j_response, *j_query = NULL; + int err; + + /* Build an empty JSON object */ + err = wrap_json_pack(&j_query, "{}"); + if (err) { + AFB_ERROR("Failed to create subscribe json object"); + goto OnErrorExit; + } + + err = afb_service_call_sync("UNICENS", "subscribe", j_query, &j_response); + if (err) { + AFB_ERROR("Fail subscribing to UNICENS events"); + goto OnErrorExit; + } + else { + AFB_NOTICE("Subscribed to UNICENS events, res=%s", json_object_to_json_string(j_response)); + json_object_put(j_response); + } + json_object_put(j_query); + j_query = NULL; + +OnErrorExit: + if (j_query) + json_object_put(j_query); + return err; +} + +/* Retrieves XML configuration path to initialize unicens2-binding. + * \param config_path Directory containing XML configuration file(s). + * \return Returns 0 if successful, otherwise != 0". + */ +extern int wrap_ucs_getconfig_sync(const char *config_path) { + json_object *j_response, *j_query = NULL; + int err; + + /* Build JSON object to retrieve UNICENS configuration */ + if (config_path != NULL) + err = wrap_json_pack(&j_query, "{s:s}", "cfgpath", config_path); + else + err = wrap_json_pack(&j_query, "{}"); + + if (err) { + AFB_ERROR("Failed to create listconfig json object"); + goto OnErrorExit; + } + + err = afb_service_call_sync("UNICENS", "listconfig", j_query, &j_response); + if (err) { + AFB_ERROR("Failed to call listconfig"); + goto OnErrorExit; + } + else { + AFB_NOTICE("UNICENS listconfig result, res=%s", json_object_to_json_string(j_response)); + json_object_put(j_response); + } + json_object_put(j_query); + j_query = NULL; + +OnErrorExit: + if (j_query) + json_object_put(j_query); + return err; +} + + +/* Initializes the unicens2-binding. + * \param file_name Path to XML configuration file or \c NULL for + * first found file in default path. + * \return Returns 0 if successful, otherwise != 0". + */ +extern int wrap_ucs_initialize_sync(const char *file_name) { + json_object *j_response, *j_query = NULL; + int err; + + /* Build JSON object to initialize UNICENS */ + if (file_name != NULL) + err = wrap_json_pack(&j_query, "{s:s}", "filename", file_name); + else + err = wrap_json_pack(&j_query, "{}"); + + if (err) { + AFB_ERROR("Failed to create initialize json object"); + goto OnErrorExit; + } + err = afb_service_call_sync("UNICENS", "initialise", j_query, &j_response); + if (err) { + AFB_ERROR("Failed to initialize UNICENS"); + goto OnErrorExit; + } + else { + AFB_NOTICE("Initialized UNICENS, res=%s", json_object_to_json_string(j_response)); + json_object_put(j_response); + } + + json_object_put(j_query); + j_query = NULL; + +OnErrorExit: + if (j_query) + json_object_put(j_query); + return err; +} + + +static void wrap_ucs_i2cwrite_cb(void *closure __attribute__((unused)), int status, struct json_object *j_result) { + + AFB_NOTICE("wrap_ucs_i2cwrite_cb: status=%d, res=%s", status, json_object_to_json_string(j_result)); +} + +/* Write I2C command to a network node. + * \param node Node address + * \param data_ptr Reference to command data + * \param data_sz Size of the command data. Valid values: 1..32. + * \return Returns 0 if successful, otherwise != 0". + */ +extern int wrap_ucs_i2cwrite_sync(uint16_t node, uint8_t *data_ptr, uint8_t data_sz) { + + json_object *j_response, *j_query, *j_array = NULL; + int err; + uint8_t cnt; + + j_query = json_object_new_object(); + j_array = json_object_new_array(); + + if (!j_query || !j_array) { + err = -1; + AFB_ERROR("Failed to create writei2c json objects"); + goto OnErrorExit; + } + + for (cnt = 0U; cnt < data_sz; cnt++) { + json_object_array_add(j_array, json_object_new_int(data_ptr[cnt])); + } + + json_object_object_add(j_query, "node", json_object_new_int(node)); + json_object_object_add(j_query, "data", j_array); + + err = afb_service_call_sync("UNICENS", "writei2c", j_query, &j_response); + + if (err) { + AFB_ERROR("Failed to call writei2c"); + goto OnErrorExit; + } + else { + AFB_NOTICE("Called writei2c, res=%s", "async"/*json_object_to_json_string(j_response)*/); + json_object_put(j_response); + } + + json_object_put(j_query); + j_query = NULL; + +OnErrorExit: + if (j_query) + json_object_put(j_query); + return err; +} + +/* Write I2C command to a network node. + * \param node Node address + * \param data_ptr Reference to command data + * \param data_sz Size of the command data. Valid values: 1..32. + * \return Returns 0 if successful, otherwise != 0". + */ +extern int wrap_ucs_i2cwrite(uint16_t node, uint8_t *data_ptr, uint8_t data_sz) { + + json_object *j_query, *j_array = NULL; + int err; + uint8_t cnt; + + j_query = json_object_new_object(); + j_array = json_object_new_array(); + + if (!j_query || !j_array) { + err = -1; + AFB_ERROR("Failed to create writei2c json objects"); + goto OnErrorExit; + } + + for (cnt = 0U; cnt < data_sz; cnt++) { + json_object_array_add(j_array, json_object_new_int(data_ptr[cnt])); + } + + json_object_object_add(j_query, "node", json_object_new_int(node)); + json_object_object_add(j_query, "data", j_array); + + afb_service_call("UNICENS", "writei2c", j_query, wrap_ucs_i2cwrite_cb, NULL); + err = 0; + j_query = NULL; + +OnErrorExit: + if (j_query) + json_object_put(j_query); + return err; +} diff --git a/HAL-afb/hal-most-unicens/wrap_unicens.h b/HAL-afb/hal-most-unicens/wrap_unicens.h new file mode 100644 index 0000000..27071c7 --- /dev/null +++ b/HAL-afb/hal-most-unicens/wrap_unicens.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2017, Microchip Technology Inc. and its subsidiaries. + * Author Tobias Jahnke + * + * 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. + * + */ + +#pragma once + +#include + +extern int wrap_ucs_subscribe_sync(); +extern int wrap_ucs_getconfig_sync(const char *config_path); +extern int wrap_ucs_initialize_sync(const char* file_name); +extern int wrap_ucs_i2cwrite_sync(uint16_t node, uint8_t *data_ptr, uint8_t data_sz); +extern int wrap_ucs_i2cwrite(uint16_t node, uint8_t *data_ptr, uint8_t data_sz); diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index d22e757..f7ef12b 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -60,6 +60,7 @@ hal_most_unicens.c wrap-json.c + wrap_unicens.c HalPlugPcm.c @@ -422,6 +423,20 @@ + + + + /opt/AGL/include/afb + HAL-afb/hal-most-unicens + /usr/include/json-c + /usr/lib64/gcc/x86_64-suse-linux/5/include + build/HAL-afb/hal-most-unicens + + + @@ -880,6 +895,13 @@ + + + + -- cgit 1.2.3-korg From 9a57e535616ce2fd84344911b02a6fe471258c93 Mon Sep 17 00:00:00 2001 From: Tobias Jahnke Date: Thu, 10 Aug 2017 16:34:54 +0200 Subject: includes lib-most-volume in HAL UNICENS --- HAL-afb/hal-most-unicens/CMakeLists.txt | 8 +- HAL-afb/hal-most-unicens/hal_most_unicens.c | 13 +- HAL-afb/hal-most-unicens/ucs2-vol/CMakeLists.txt | 26 ++++ .../ucs2-vol/inc/device_container.h | 59 ++++++++ .../hal-most-unicens/ucs2-vol/inc/device_value.h | 91 +++++++++++ .../hal-most-unicens/ucs2-vol/inc/libmostvolume.h | 104 +++++++++++++ HAL-afb/hal-most-unicens/ucs2-vol/inc/setup.h | 70 +++++++++ .../hal-most-unicens/ucs2-vol/src/CMakeLists.txt | 39 +++++ .../ucs2-vol/src/device_container.cpp | 166 +++++++++++++++++++++ .../hal-most-unicens/ucs2-vol/src/device_value.cpp | 130 ++++++++++++++++ .../ucs2-vol/src/libmostvolume.cpp | 86 +++++++++++ HAL-afb/hal-most-unicens/ucs2-vol/src/setup.cpp | 89 +++++++++++ HAL-afb/hal-most-unicens/wrap_unicens.c | 45 +++++- HAL-afb/hal-most-unicens/wrap_unicens.h | 11 +- HAL-afb/hal-most-unicens/wrap_volume.c | 36 +++++ HAL-afb/hal-most-unicens/wrap_volume.h | 22 +++ nbproject/configurations.xml | 81 +++++++++- 17 files changed, 1058 insertions(+), 18 deletions(-) create mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/CMakeLists.txt create mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/inc/device_container.h create mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/inc/device_value.h create mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/inc/libmostvolume.h create mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/inc/setup.h create mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/src/CMakeLists.txt create mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/src/device_container.cpp create mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/src/device_value.cpp create mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/src/libmostvolume.cpp create mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/src/setup.cpp create mode 100644 HAL-afb/hal-most-unicens/wrap_volume.c create mode 100644 HAL-afb/hal-most-unicens/wrap_volume.h diff --git a/HAL-afb/hal-most-unicens/CMakeLists.txt b/HAL-afb/hal-most-unicens/CMakeLists.txt index 828614f..784024a 100644 --- a/HAL-afb/hal-most-unicens/CMakeLists.txt +++ b/HAL-afb/hal-most-unicens/CMakeLists.txt @@ -16,6 +16,7 @@ # limitations under the License. ########################################################################### +ADD_SUBDIRECTORY(ucs2-vol) # Add target to project dependency list PROJECT_TARGET_ADD(hal-most-unicens) @@ -39,4 +40,9 @@ PROJECT_TARGET_ADD(hal-most-unicens) # installation directory INSTALL(TARGETS ${TARGET_NAME} - LIBRARY DESTINATION ${BINDINGS_INSTALL_DIR}) \ No newline at end of file + LIBRARY DESTINATION ${BINDINGS_INSTALL_DIR}) + + # Define properties to expose when others use this target + TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME} + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/ucs2-vol/inc + ) diff --git a/HAL-afb/hal-most-unicens/hal_most_unicens.c b/HAL-afb/hal-most-unicens/hal_most_unicens.c index 394e502..ace9399 100644 --- a/HAL-afb/hal-most-unicens/hal_most_unicens.c +++ b/HAL-afb/hal-most-unicens/hal_most_unicens.c @@ -21,12 +21,13 @@ #include "wrap-json.h" #include "string.h" #include "wrap_unicens.h" +#include "wrap_volume.h" static int master_volume; static json_bool master_switch; static int pcm_volume[6]; -static uint8_t test[3] = {0x07,0x03,0xFF}; +/*static uint8_t test[3] = {0x07,0x03,0xFF};*/ void unicens_master_vol_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { @@ -109,13 +110,19 @@ STATIC int unicens_service_init() { err = wrap_ucs_subscribe_sync(); if (err) { - AFB_ERROR("Failed to subscribe to unicensv2-binding"); + AFB_ERROR("Failed to subscribe to UNICENS binding"); goto OnErrorExit; } err = wrap_ucs_initialize_sync("/home/agluser/DEVELOPMENT/AGL/BINDING/unicens2-binding/data/config_multichannel_audio_kit.xml"); if (err) { - AFB_ERROR("Failed to initialize unicens-v2 binding"); + AFB_ERROR("Failed to initialize UNICENS binding"); + goto OnErrorExit; + } + + err = wrap_volume_init(); + if (err) { + AFB_ERROR("Failed to initialize wrapper for volume library"); goto OnErrorExit; } diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/CMakeLists.txt b/HAL-afb/hal-most-unicens/ucs2-vol/CMakeLists.txt new file mode 100644 index 0000000..8735677 --- /dev/null +++ b/HAL-afb/hal-most-unicens/ucs2-vol/CMakeLists.txt @@ -0,0 +1,26 @@ +########################################################################### +# Copyright 2015, 2016, 2017 IoT.bzh +# +# author: Fulup Ar Foll +# +# 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 subproject targets +ADD_SUBDIRECTORY(src) + + + + diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/inc/device_container.h b/HAL-afb/hal-most-unicens/ucs2-vol/inc/device_container.h new file mode 100644 index 0000000..5e3b753 --- /dev/null +++ b/HAL-afb/hal-most-unicens/ucs2-vol/inc/device_container.h @@ -0,0 +1,59 @@ +/* + * libmostvolume example + * + * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +#ifndef DEVICECONTAINER_H +#define DEVICECONTAINER_H + +#include "device_value.h" +#include "libmostvolume.h" + +class CDeviceContainer { + +public: + CDeviceContainer(); + virtual ~CDeviceContainer(); + + void AssignService(lib_most_volume_init_t *init_ptr) {_init_ptr = init_ptr;} + + void RegisterValues(CDeviceValue** list_pptr, uint16_t list_sz); + void SetValue(uint16_t key, uint8_t value); + void ClearValues(); + void Update(); + +private: + void RequestService(uint16_t timeout); + void IncrementProcIndex(void); + void HandleI2cResult(uint8_t result); + + static void OnI2cResult(uint8_t result, void *obj_ptr); + + uint16_t _idx_processing; + uint16_t _values_sz; + CDeviceValue **_values_pptr; + bool _tx_busy; + bool _service_requested; + lib_most_volume_init_t *_init_ptr; +}; + +#endif /* DEVICECONTAINER_H */ + diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/inc/device_value.h b/HAL-afb/hal-most-unicens/ucs2-vol/inc/device_value.h new file mode 100644 index 0000000..21fdbdc --- /dev/null +++ b/HAL-afb/hal-most-unicens/ucs2-vol/inc/device_value.h @@ -0,0 +1,91 @@ +/* + * libmostvolume example + * + * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +#ifndef DEVICEVALUE_H +#define DEVICEVALUE_H + +#include +#include "libmostvolume.h" + +struct SRxMessage { + uint32_t devInst; /*instance if multiple devices are using the same group id*/ + uint32_t sourceAddr; + uint32_t targetAddr; + uint32_t nFBlock; + uint32_t nInst; + uint32_t nFunc; + uint32_t nOpType; + const uint8_t *pPayload; + uint32_t payloadLen; +}; + +struct STxMessage { + uint32_t devInst; /*instance if multiple devices are using the same group id*/ + uint32_t targetAddr; + uint32_t nFBlock; + uint32_t nInst; + uint32_t nFunc; + uint32_t nOpType; + const uint8_t *pPayload; + uint32_t payloadLen; +}; + +enum DeviceValueType { + DEVICE_VAL_MASTER = 0, + DEVICE_VAL_LEFT = 1, + DEVICE_VAL_RIGHT = 2 + +}; + +class CDeviceValue { +public: + CDeviceValue(uint16_t address, DeviceValueType type, uint16_t key); + virtual ~CDeviceValue(); + + uint16_t GetKey(){return _key;} + DeviceValueType GetType(){return _type;} // returns the assigned type + void SetValue(uint8_t value){_target_value = value;} // sets desired value + + bool RequiresUpdate(); // returns true if target is not actual value + // returns true if success, false if failed + // -> stop transmission + bool FireUpdateMessage(lib_most_volume_writei2c_cb_t writei2c_fptr, + lib_most_volume_writei2c_result_cb_t result_fptr, + void *result_user_ptr);// fires message & updates actual value + +private: + void HandleI2cResult(uint8_t result); + void ApplyMostValue(uint8_t value, DeviceValueType type, uint8_t tx_payload[]); + + bool _is_initial; // ensure first update + DeviceValueType _type; // determines the remote i2c command + uint16_t _key; // lookup key + uint16_t _address; // target node/group address + uint8_t _target_value; // desired value + uint8_t _actual_value; // value set and confirmed via network + uint8_t _tx_payload[20]; + uint8_t _tx_payload_sz; +}; + +#endif /* DEVICEPROPERTY_H */ + diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/inc/libmostvolume.h b/HAL-afb/hal-most-unicens/ucs2-vol/inc/libmostvolume.h new file mode 100644 index 0000000..1d7249d --- /dev/null +++ b/HAL-afb/hal-most-unicens/ucs2-vol/inc/libmostvolume.h @@ -0,0 +1,104 @@ +/* + * libmostvolume example + * + * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +#ifndef LIB_MOST_VOLUME_H +#define LIB_MOST_VOLUME_H + +#include + + +#ifdef __cplusplus +extern "C" { +#endif + +/** Describes the volume control */ +enum lib_most_volume_channel_t{ + LIB_MOST_VOLUME_CH_FRONT_LEFT = 0, + LIB_MOST_VOLUME_CH_FRONT_RIGHT = 1, + LIB_MOST_VOLUME_CH_REAR_LEFT = 2, + LIB_MOST_VOLUME_CH_REAR_RIGHT = 3, + LIB_MOST_VOLUME_CH_CENTER = 4, + LIB_MOST_VOLUME_CH_SUB = 5, + LIB_MOST_VOLUME_MASTER = 6 +}; + +/** Is fired when the application shall call "lib_most_volume_service()" after a certain time + * \param timeout Time in ms after which the application shall call lib_most_volume_service(). + * Valid values: + * 0x0000: as soon as possible, + * 0x0001..0xFFFE: timeout in ms, + * 0xFFFF: never + */ +typedef void (*lib_most_volume_service_cb_t)(uint16_t timeout); + +typedef void (*lib_most_volume_writei2c_result_cb_t)(uint8_t result, void *user_ptr); + +/** Callback function required to write volume changes to network devices + * \param node The node address + * \param data_ptr I2C command data + * \param data_sz Size of the I2C command data + * \return Returns \c 0 for success, other value on failure. + */ +typedef int (*lib_most_volume_writei2c_cb_t)(uint16_t node, uint8_t *data_ptr, uint8_t data_sz, + lib_most_volume_writei2c_result_cb_t result_fptr, + void *result_user_ptr); + +typedef struct lib_most_volume_init_ { + lib_most_volume_service_cb_t service_cb; + lib_most_volume_writei2c_cb_t writei2c_cb; + +} lib_most_volume_init_t; + + +/** Initializes the library + * \param UNICENS_inst Reference to the UNICENS instance, created by the application. + * \param req_service_fptr Callback function which is fired if the application shall call + * lib_most_volume_service. + * \return '0' on success, otherwise value >'0'. + */ +extern uint8_t lib_most_volume_init(lib_most_volume_init_t *init_ptr); + +/** Terminates the library + * \return '0' on success, otherwise value >'0'. + */ +extern uint8_t lib_most_volume_exit(void); + +/** Sets a single volume value. + * \param channel The volume control to be set. + * \param volume The volume value to be set. Valid values: 0..255. + * \return '0' on success, otherwise value >'0'. + */ +extern uint8_t lib_most_volume_set(enum lib_most_volume_channel_t channel, uint8_t volume); + +/** Shall be called either cyclically (e.g. 50ms -> polling) or after "timeout" + * when "service_fptr" is fired (-> event triggered). + * \return '0' on success, otherwise value >'0'. + */ +extern uint8_t lib_most_volume_service(void); + +#ifdef __cplusplus +} +#endif + +#endif /* LIB_MOST_VOLUME_H */ + diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/inc/setup.h b/HAL-afb/hal-most-unicens/ucs2-vol/inc/setup.h new file mode 100644 index 0000000..63bc0e2 --- /dev/null +++ b/HAL-afb/hal-most-unicens/ucs2-vol/inc/setup.h @@ -0,0 +1,70 @@ +/* + * libmostvolume example + * + * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +#ifndef SETUP_H +#define SETUP_H + +#include "libmostvolume.h" +#include "device_value.h" +#include "device_container.h" + +#define MAX_CHANNELS (LIB_MOST_VOLUME_MASTER + 1u) +#define MAX_MASTERS 3u + +class CSetup { + +public: + void Configure(lib_most_volume_init_t *init_ptr); + void SetVolume(enum lib_most_volume_channel_t channel, uint8_t volume); + void Update(); + +private: + CDeviceValue _volume_amp_270_m; + CDeviceValue _volume_amp_270_l; + CDeviceValue _volume_amp_270_r; + + CDeviceValue _volume_amp_271_m; + CDeviceValue _volume_amp_271_l; + CDeviceValue _volume_amp_271_r; + + CDeviceValue _volume_amp_272_m; + CDeviceValue _volume_amp_272_l; + CDeviceValue _volume_amp_272_r; + + CDeviceContainer _value_container; + lib_most_volume_init_t init_data; + +public: + static CSetup* GetInstance(); // singleton + static void Release(); // singleton + +protected: + CSetup(); // singleton + virtual ~CSetup(); // singleton + +private: + static CSetup* _instance; // singleton +}; + +#endif /* SETUP_H */ + diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/src/CMakeLists.txt b/HAL-afb/hal-most-unicens/ucs2-vol/src/CMakeLists.txt new file mode 100644 index 0000000..1be7184 --- /dev/null +++ b/HAL-afb/hal-most-unicens/ucs2-vol/src/CMakeLists.txt @@ -0,0 +1,39 @@ +########################################################################### +# Copyright 2015, 2016, 2017 IoT.bzh +# +# author: Fulup Ar Foll +# +# 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. +########################################################################### + +# Add target to project dependency list +PROJECT_TARGET_ADD(ucs2-volume) + +# Define targets source files +ADD_LIBRARY(${TARGET_NAME} STATIC device_container.cpp device_value.cpp libmostvolume.cpp setup.cpp) + + # Library properties + SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES + OUTPUT_NAME ${TARGET_NAME} + ) + + # Library dependencies from PKG_REQUIRED_LIST + #TARGET_LINK_LIBRARIES(ucs2-volume # Library dependencies (include updates automatically) + # ${link_libraries} + #) + + # Define properties to expose when others use this target + TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME} + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../inc + ) + diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/src/device_container.cpp b/HAL-afb/hal-most-unicens/ucs2-vol/src/device_container.cpp new file mode 100644 index 0000000..cef25b1 --- /dev/null +++ b/HAL-afb/hal-most-unicens/ucs2-vol/src/device_container.cpp @@ -0,0 +1,166 @@ +/* + * libmostvolume example + * + * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +#include +#include "device_container.h" + +#define DEVCONT_TIME_RETRIGGER (uint16_t)100U +#define DEVCONT_TIME_NOW (uint16_t)0U +#define DEVCONT_TIME_STOP (uint16_t)0xFFFFU + +#define DEVCONT_UNUSED(a) (a = a) + +CDeviceContainer::CDeviceContainer() +{ + this->_idx_processing = 0U; + this->_values_pptr = NULL; + this->_values_sz = 0U; + this->_tx_busy = false; + this->_service_requested = false; + this->_init_ptr = NULL; +} + +CDeviceContainer::~CDeviceContainer() +{ + /*Clb_RegisterI2CResultCB(NULL, NULL);*/ /* avoid that the result callback is fired after object is destroyed */ +} + +void CDeviceContainer::RegisterValues(CDeviceValue** list_pptr, uint16_t list_sz) +{ + this->_idx_processing = 0U; + this->_values_pptr = list_pptr; + this->_values_sz = list_sz; + this->_tx_busy = false; + + if ((list_pptr != NULL) && (list_sz > 0U)) + { + this->_idx_processing = (uint16_t)(list_sz - 1U); + } +} + +void CDeviceContainer::ClearValues() +{ + this->_idx_processing = 0U; + this->_values_pptr = NULL; + this->_values_sz = 0U; + this->_tx_busy = false; +} + +void CDeviceContainer::SetValue(uint16_t key, uint8_t value) +{ + uint16_t idx; + bool req_update = false; + + for (idx = 0U; idx < this->_values_sz; idx++) + { + if (this->_values_pptr[idx]->GetKey() == key) + { + this->_values_pptr[idx]->SetValue(value); + if (this->_values_pptr[idx]->RequiresUpdate()) + { + req_update = true; + } + } + } + + if (req_update && (!this->_tx_busy)) + { + RequestService(DEVCONT_TIME_NOW); //fire callback + } +} + +void CDeviceContainer::IncrementProcIndex(void) +{ + if ((_idx_processing + 1U) >= this->_values_sz) + { + _idx_processing = 0U; + } + else + { + _idx_processing++; + } +} + +// starts at latest position, searches next value to update, waits until response +void CDeviceContainer::Update() +{ + uint16_t cnt; + bool error = false; + _service_requested = false; + + if (this->_tx_busy) + { + return; + } + + for (cnt = 0u; cnt < this->_values_sz; cnt++) /* just run one cycle */ + { + IncrementProcIndex(); + + if (_values_pptr[_idx_processing]->RequiresUpdate()) + { + if (_values_pptr[_idx_processing]->FireUpdateMessage(this->_init_ptr->writei2c_cb, + &OnI2cResult, + this)) + { + /*Clb_RegisterI2CResultCB(&OnI2cResult, this);*/ + this->_tx_busy = true; + break; + } + else + { + error = true; + } + } + } + + if (error) + { + RequestService(DEVCONT_TIME_RETRIGGER); + } +} + +void CDeviceContainer::HandleI2cResult(uint8_t result) +{ + DEVCONT_UNUSED(result); + this->_tx_busy = false; + this->RequestService(DEVCONT_TIME_NOW); +} + +void CDeviceContainer::OnI2cResult(uint8_t result, void *obj_ptr) +{ + ((CDeviceContainer*)obj_ptr)->HandleI2cResult(result); +} + +void CDeviceContainer::RequestService(uint16_t timeout) +{ + if (!_service_requested) + { + _service_requested = true; + + if (this->_init_ptr && this->_init_ptr->service_cb) + { + this->_init_ptr->service_cb(timeout); + } + } +} diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/src/device_value.cpp b/HAL-afb/hal-most-unicens/ucs2-vol/src/device_value.cpp new file mode 100644 index 0000000..b8c681e --- /dev/null +++ b/HAL-afb/hal-most-unicens/ucs2-vol/src/device_value.cpp @@ -0,0 +1,130 @@ +/* + * libmostvolume example + * + * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +#include "device_value.h" +#include "setup.h" + +#define MUTE_VALUE 0x03FFU +#define MUTE_VALUE_HB 0x03U +#define MUTE_VALUE_LB 0xFFU + +#define CONTROL_MASTER 0x07U +#define CONTROL_CH_1 0x08U +#define CONTROL_CH_2 0x09U + +CDeviceValue::CDeviceValue(uint16_t address, DeviceValueType type, uint16_t key) +{ + this->_is_initial = true; + this->_address = address; + this->_target_value = 0x01u; + this->_actual_value = 0x01u; + + this->_type = type; + this->_key = key; + + _tx_payload[0] = CONTROL_MASTER;// 7: master, 8: channel 1, 9: Channel 2 + _tx_payload[1] = MUTE_VALUE_HB; //HB:Volume + _tx_payload[2] = MUTE_VALUE_LB; //LB:Volume + _tx_payload_sz = 3u; +} + +CDeviceValue::~CDeviceValue() +{ +} + +void CDeviceValue::ApplyMostValue(uint8_t value, DeviceValueType type, uint8_t tx_payload[]) +{ + uint16_t tmp = MUTE_VALUE; + + switch (type) + { + case DEVICE_VAL_LEFT: + tmp = (uint16_t)(0x80U + 0x37FU - (0x37FU * ((int32_t)value) / (0xFFU))); + //tmp = 0x3FF - (0x3FF * ((int32_t)value) / (0xFF)); + //tmp = 0x100 + 0x2FF - (0x2FF * ((int32_t)value) / (0xFF)); + tx_payload[0] = CONTROL_CH_1; + break; + case DEVICE_VAL_RIGHT: + tmp = (uint16_t)(0x80U + 0x37FU - (0x37FU * ((int32_t)value) / (0xFFU))); + //tmp = 0x3FF - (0x3FF * ((int32_t)value) / (0xFF)); + //tmp = 0x100 + 0x2FF - (0x2FF * ((int32_t)value) / (0xFF)); + tx_payload[0] = CONTROL_CH_2; + break; + default: + /*std::cerr << "CDeviceValue::ApplyMostValue() error matching incorrect" << std::endl;*/ + case DEVICE_VAL_MASTER: + tmp = (uint16_t)(0x100U + 0x2FFU - (0x2FFU * ((int32_t)value) / (0xFFU))); + tx_payload[0] = CONTROL_MASTER; + break; + } + + tx_payload[1] = (uint8_t)((tmp >> 8U) & (uint16_t)0xFFU); //HB:Volume + tx_payload[2] = (uint8_t)(tmp & (uint16_t)0xFFU); //LB:Volume +} + +// returns true if target is not actual value +bool CDeviceValue::RequiresUpdate() +{ + if (this->_target_value != this->_actual_value) + { + return true; + } + + return false; +} + +bool CDeviceValue::FireUpdateMessage(lib_most_volume_writei2c_cb_t writei2c_fptr, + lib_most_volume_writei2c_result_cb_t result_fptr, + void *result_user_ptr) +{ + int ret; + ApplyMostValue(this->_target_value, _type, _tx_payload); + + ret = writei2c_fptr(this->_address, &_tx_payload[0], _tx_payload_sz, + result_fptr, + result_user_ptr); + + if (ret == 0) + { + // Clb_RegisterI2CResultCB(OnI2cResult, this); + // mark value as set! + this->_actual_value = this->_target_value; + return true; + } + + return false; +} + +#if 0 + ret = Ucs_I2c_WritePort( CSetup::GetInstance()->RetrieveUnicensInst(), + this->_address, + 0x0F00u, /* i2c port handle */ + UCS_I2C_DEFAULT_MODE, /* 0 */ + 0u, /* block count */ + 0x2Au, /* i2c slave address */ + 0x03E8u, /* timeout 1000 milliseconds */ + _tx_payload_sz, /* data length */ + &_tx_payload[0], /* data pointer */ + &Clb_OnWriteI2CPortResult + ); +#endif diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/src/libmostvolume.cpp b/HAL-afb/hal-most-unicens/ucs2-vol/src/libmostvolume.cpp new file mode 100644 index 0000000..a90b5b1 --- /dev/null +++ b/HAL-afb/hal-most-unicens/ucs2-vol/src/libmostvolume.cpp @@ -0,0 +1,86 @@ +/* + * libmostvolume example + * + * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +#include "libmostvolume.h" +#include "setup.h" +/*#include */ + +static bool _running = false; + +extern "C" uint8_t lib_most_volume_init(lib_most_volume_init_t *init_ptr) +{ + uint8_t success = 1U; + /*std::cerr << "lib_most_volume_init(): called" << std::endl;*/ + + if (!_running && init_ptr) + { + CSetup::GetInstance()->Configure(init_ptr); + success = 0U; + _running = true; + } + + return success; +} + +extern "C" uint8_t lib_most_volume_exit(void) +{ + uint8_t success = 1U; + /*std::cerr << "lib_most_volume_exit(): called" << std::endl;*/ + + if (_running) + { + CSetup::Release(); + success = 0U; + _running = false; + } + + return success; +} + +extern "C" uint8_t lib_most_volume_set(enum lib_most_volume_channel_t channel, uint8_t volume) +{ + uint8_t success = 1U; + /*std::cerr << "lib_most_volume_set(): channel=" << channel << ", volume=" << (int)volume << std::endl;*/ + + if (_running) + { + CSetup::GetInstance()->SetVolume(channel, volume); + success = 0U; + } + + return success; +} + +extern "C" uint8_t lib_most_volume_service(void) +{ + uint8_t success = 1U; + /*std::cerr << "lib_most_volume_service(): called" << std::endl;*/ + + if (_running) + { + CSetup::GetInstance()->Update(); + success = 0U; + } + + return success; +} diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/src/setup.cpp b/HAL-afb/hal-most-unicens/ucs2-vol/src/setup.cpp new file mode 100644 index 0000000..61b8a68 --- /dev/null +++ b/HAL-afb/hal-most-unicens/ucs2-vol/src/setup.cpp @@ -0,0 +1,89 @@ +/* + * libmostvolume example + * + * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ +#include +#include "setup.h" + +CSetup* CSetup::_instance = NULL; + +// singleton +CSetup* CSetup::GetInstance() { + if (_instance == NULL) { + _instance = new CSetup(); + } + + return _instance; +} + +// singleton +void CSetup::Release() { + if (_instance != NULL) { + delete _instance; + } + _instance = NULL; +} + +CSetup::CSetup() + : _volume_amp_270_m(0x270U, DEVICE_VAL_MASTER, LIB_MOST_VOLUME_MASTER), + _volume_amp_270_l(0x270U, DEVICE_VAL_LEFT, LIB_MOST_VOLUME_CH_FRONT_LEFT), + _volume_amp_270_r(0x270U, DEVICE_VAL_RIGHT, LIB_MOST_VOLUME_CH_FRONT_RIGHT), + _volume_amp_271_m(0x271U, DEVICE_VAL_MASTER, LIB_MOST_VOLUME_MASTER), + _volume_amp_271_l(0x271U, DEVICE_VAL_LEFT, LIB_MOST_VOLUME_CH_REAR_LEFT), + _volume_amp_271_r(0x271U, DEVICE_VAL_RIGHT, LIB_MOST_VOLUME_CH_REAR_RIGHT), + _volume_amp_272_m(0x272U, DEVICE_VAL_MASTER, LIB_MOST_VOLUME_MASTER), + _volume_amp_272_l(0x272U, DEVICE_VAL_LEFT, LIB_MOST_VOLUME_CH_CENTER), + _volume_amp_272_r(0x272U, DEVICE_VAL_RIGHT, LIB_MOST_VOLUME_CH_SUB), + _value_container() +{ + static CDeviceValue* value_list[9] = { &_volume_amp_270_m, + &_volume_amp_270_l, + &_volume_amp_270_r, + &_volume_amp_271_m, + &_volume_amp_271_l, + &_volume_amp_271_r, + &_volume_amp_272_m, + &_volume_amp_272_l, + &_volume_amp_272_r}; + + _value_container.RegisterValues(value_list, 9U); +} + +CSetup::~CSetup() +{ + +} + +void CSetup::Configure(lib_most_volume_init_t *init_ptr) +{ + this->init_data = *init_ptr; + _value_container.AssignService(&this->init_data); +} + +void CSetup::SetVolume(enum lib_most_volume_channel_t channel, uint8_t volume) +{ + _value_container.SetValue((uint16_t)channel, volume); +} + +void CSetup::Update() +{ + _value_container.Update(); +} diff --git a/HAL-afb/hal-most-unicens/wrap_unicens.c b/HAL-afb/hal-most-unicens/wrap_unicens.c index 15f94a1..2bd1e91 100644 --- a/HAL-afb/hal-most-unicens/wrap_unicens.c +++ b/HAL-afb/hal-most-unicens/wrap_unicens.c @@ -25,6 +25,11 @@ #include "wrap_unicens.h" #include "wrap-json.h" +typedef struct async_job_ { + wrap_ucs_result_cb_t result_fptr; + void *result_user_ptr; +} async_job_t; + /* Subscribes to unicens2-binding events. * \return Returns 0 if successful, otherwise != 0". */ @@ -134,12 +139,6 @@ OnErrorExit: return err; } - -static void wrap_ucs_i2cwrite_cb(void *closure __attribute__((unused)), int status, struct json_object *j_result) { - - AFB_NOTICE("wrap_ucs_i2cwrite_cb: status=%d, res=%s", status, json_object_to_json_string(j_result)); -} - /* Write I2C command to a network node. * \param node Node address * \param data_ptr Reference to command data @@ -188,15 +187,34 @@ OnErrorExit: return err; } +/* ------------------ ASYNCHRONOUS API ---------------------------------------*/ + +static void wrap_ucs_i2cwrite_cb(void *closure, int status, struct json_object *j_result) { + + AFB_NOTICE("wrap_ucs_i2cwrite_cb: closure=%p status=%d, res=%s", closure, status, json_object_to_json_string(j_result)); + + if (closure) { + async_job_t *job_ptr = (async_job_t *)closure; + + if (job_ptr->result_fptr) + job_ptr->result_fptr(0U, job_ptr->result_user_ptr); + + free(closure); + } +} + /* Write I2C command to a network node. * \param node Node address * \param data_ptr Reference to command data * \param data_sz Size of the command data. Valid values: 1..32. * \return Returns 0 if successful, otherwise != 0". */ -extern int wrap_ucs_i2cwrite(uint16_t node, uint8_t *data_ptr, uint8_t data_sz) { +extern int wrap_ucs_i2cwrite(uint16_t node, uint8_t *data_ptr, uint8_t data_sz, + wrap_ucs_result_cb_t result_fptr, + void *result_user_ptr) { json_object *j_query, *j_array = NULL; + async_job_t *job_ptr = NULL; int err; uint8_t cnt; @@ -215,8 +233,19 @@ extern int wrap_ucs_i2cwrite(uint16_t node, uint8_t *data_ptr, uint8_t data_sz) json_object_object_add(j_query, "node", json_object_new_int(node)); json_object_object_add(j_query, "data", j_array); + + job_ptr = malloc(sizeof(async_job_t)); + + if (!job_ptr) { + err = -1; + AFB_ERROR("Failed to create async job object"); + goto OnErrorExit; + } + + job_ptr->result_fptr = result_fptr; + job_ptr->result_user_ptr = result_user_ptr; - afb_service_call("UNICENS", "writei2c", j_query, wrap_ucs_i2cwrite_cb, NULL); + afb_service_call("UNICENS", "writei2c", j_query, wrap_ucs_i2cwrite_cb, job_ptr); err = 0; j_query = NULL; diff --git a/HAL-afb/hal-most-unicens/wrap_unicens.h b/HAL-afb/hal-most-unicens/wrap_unicens.h index 27071c7..268fb74 100644 --- a/HAL-afb/hal-most-unicens/wrap_unicens.h +++ b/HAL-afb/hal-most-unicens/wrap_unicens.h @@ -17,11 +17,18 @@ */ #pragma once - #include +/* Asynchronous API: result callback */ +typedef void (*wrap_ucs_result_cb_t)(uint8_t result, void *user_ptr); + +/* Asynchronous API: functions */ +extern int wrap_ucs_i2cwrite(uint16_t node, uint8_t *data_ptr, uint8_t data_sz, + wrap_ucs_result_cb_t result_fptr, + void *result_user_ptr); + +/* Synchronous API: functions */ extern int wrap_ucs_subscribe_sync(); extern int wrap_ucs_getconfig_sync(const char *config_path); extern int wrap_ucs_initialize_sync(const char* file_name); extern int wrap_ucs_i2cwrite_sync(uint16_t node, uint8_t *data_ptr, uint8_t data_sz); -extern int wrap_ucs_i2cwrite(uint16_t node, uint8_t *data_ptr, uint8_t data_sz); diff --git a/HAL-afb/hal-most-unicens/wrap_volume.c b/HAL-afb/hal-most-unicens/wrap_volume.c new file mode 100644 index 0000000..811879f --- /dev/null +++ b/HAL-afb/hal-most-unicens/wrap_volume.c @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2017, Microchip Technology Inc. and its subsidiaries. + * Author Tobias Jahnke + * + * 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 +#define AFB_BINDING_VERSION 2 + +#include +#include +#include + +#include "wrap_volume.h" +#include "wrap_unicens.h" + +extern int wrap_volume_init(void) { + + /*lib_most_volume_init_t mv_init; + mv_init.writei2c_cb = &wrap_ucs_i2cwrite; + mv_init.service_cb = NULL; + lib_most_volume_init(NULL);*/ + + return -1; +} \ No newline at end of file diff --git a/HAL-afb/hal-most-unicens/wrap_volume.h b/HAL-afb/hal-most-unicens/wrap_volume.h new file mode 100644 index 0000000..92fae48 --- /dev/null +++ b/HAL-afb/hal-most-unicens/wrap_volume.h @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2017, Microchip Technology Inc. and its subsidiaries. + * Author Tobias Jahnke + * + * 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. + * + */ + +#pragma once +#include + +extern int wrap_volume_init(void); diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index f7ef12b..07b5f8e 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -58,9 +58,18 @@ hal-volume.c + + + device_container.cpp + device_value.cpp + libmostvolume.cpp + setup.cpp + + hal_most_unicens.c wrap-json.c wrap_unicens.c + wrap_volume.c HalPlugPcm.c @@ -397,7 +406,6 @@ ex="false" tool="0" flavor2="3"> - /opt/AGL/include/afb HAL-afb/hal-most-unicens @@ -410,11 +418,55 @@ + + + + HAL-afb/hal-most-unicens/ucs2-vol/inc + HAL-afb/hal-most-unicens/ucs2-vol/src + + + + + + + HAL-afb/hal-most-unicens/ucs2-vol/src + HAL-afb/hal-most-unicens/ucs2-vol/inc + + + + + + + HAL-afb/hal-most-unicens/ucs2-vol/src + HAL-afb/hal-most-unicens/ucs2-vol/inc + + + + + + + HAL-afb/hal-most-unicens/ucs2-vol/inc + HAL-afb/hal-most-unicens/ucs2-vol/src + + + - + HAL-afb/hal-most-unicens /usr/lib64/gcc/x86_64-suse-linux/5/include @@ -427,7 +479,21 @@ ex="false" tool="0" flavor2="3"> - + + + /opt/AGL/include/afb + HAL-afb/hal-most-unicens + /usr/include/json-c + /usr/lib64/gcc/x86_64-suse-linux/5/include + build/HAL-afb/hal-most-unicens + + + + + /opt/AGL/include/afb HAL-afb/hal-most-unicens @@ -446,7 +512,7 @@ - + @@ -902,6 +968,13 @@ + + + + -- cgit 1.2.3-korg From eb6b00b52432941cf081a3b511c70e758f6a7213 Mon Sep 17 00:00:00 2001 From: Tobias Jahnke Date: Thu, 10 Aug 2017 17:35:55 +0200 Subject: fixes linker issue and implements volume service --- HAL-afb/hal-most-unicens/CMakeLists.txt | 3 ++- HAL-afb/hal-most-unicens/wrap_volume.c | 39 +++++++++++++++++++++++++++++---- HAL-afb/hal-most-unicens/wrap_volume.h | 1 + nbproject/configurations.xml | 6 +++++ 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/HAL-afb/hal-most-unicens/CMakeLists.txt b/HAL-afb/hal-most-unicens/CMakeLists.txt index 784024a..e532191 100644 --- a/HAL-afb/hal-most-unicens/CMakeLists.txt +++ b/HAL-afb/hal-most-unicens/CMakeLists.txt @@ -22,7 +22,7 @@ ADD_SUBDIRECTORY(ucs2-vol) PROJECT_TARGET_ADD(hal-most-unicens) # Define project Targets - ADD_LIBRARY(hal-most-unicens MODULE hal_most_unicens.c wrap-json.c wrap_unicens.c) + ADD_LIBRARY(hal-most-unicens MODULE hal_most_unicens.c wrap-json.c wrap_unicens.c wrap_volume.c) # Binder exposes a unique public entry point SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES @@ -36,6 +36,7 @@ PROJECT_TARGET_ADD(hal-most-unicens) TARGET_LINK_LIBRARIES(${TARGET_NAME} hal-interface audio-interface + ucs2-volume ) # installation directory diff --git a/HAL-afb/hal-most-unicens/wrap_volume.c b/HAL-afb/hal-most-unicens/wrap_volume.c index 811879f..438704f 100644 --- a/HAL-afb/hal-most-unicens/wrap_volume.c +++ b/HAL-afb/hal-most-unicens/wrap_volume.c @@ -19,18 +19,49 @@ #define AFB_BINDING_VERSION 2 #include +#include #include #include #include "wrap_volume.h" #include "wrap_unicens.h" +#include "libmostvolume.h" + +static int wrap_volume_service_timeout_cb(sd_event_source* source, + uint64_t timer __attribute__((__unused__)), + void *userdata __attribute__((__unused__))) { + + uint8_t ret; + + sd_event_source_unref(source); + ret = lib_most_volume_service(); + + if (ret != 0U) { + AFB_ERROR("lib_most_volume_service returns %d", ret); + } + + return 0; +} + +static void wrap_volume_service_cb(uint16_t timeout) { + uint64_t usec; + sd_event_now(afb_daemon_get_event_loop(), CLOCK_BOOTTIME, &usec); + sd_event_add_time( afb_daemon_get_event_loop(), NULL, CLOCK_MONOTONIC, + usec + (timeout*1000), + 250, + wrap_volume_service_timeout_cb, + NULL); +} extern int wrap_volume_init(void) { - /*lib_most_volume_init_t mv_init; + uint8_t ret = 0U; + lib_most_volume_init_t mv_init; + mv_init.writei2c_cb = &wrap_ucs_i2cwrite; - mv_init.service_cb = NULL; - lib_most_volume_init(NULL);*/ + mv_init.service_cb = wrap_volume_service_cb; + + ret = lib_most_volume_init(&mv_init); - return -1; + return ret * (-1); } \ No newline at end of file diff --git a/HAL-afb/hal-most-unicens/wrap_volume.h b/HAL-afb/hal-most-unicens/wrap_volume.h index 92fae48..b17bc26 100644 --- a/HAL-afb/hal-most-unicens/wrap_volume.h +++ b/HAL-afb/hal-most-unicens/wrap_volume.h @@ -18,5 +18,6 @@ #pragma once #include +#include extern int wrap_volume_init(void); diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 07b5f8e..70d59f9 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -495,12 +495,18 @@ flavor2="3"> + ../../../opt/include/afb /opt/AGL/include/afb HAL-afb/hal-most-unicens /usr/include/json-c /usr/lib64/gcc/x86_64-suse-linux/5/include build/HAL-afb/hal-most-unicens + + CONTROL_CDEV_RX="/dev/inic-usb-crx" + CONTROL_CDEV_TX="/dev/inic-usb-ctx" + MAX_SND_CARD=16 + -- cgit 1.2.3-korg From 3d4ecfd96e63d4fbc719e4be06e022a6811b0c1e Mon Sep 17 00:00:00 2001 From: Tobias Jahnke Date: Fri, 11 Aug 2017 09:22:38 +0200 Subject: MOST ALSA mixer controls now mapped to UNCENS I2C commands --- HAL-afb/hal-most-unicens/hal_most_unicens.c | 11 +++--- HAL-afb/hal-most-unicens/wrap_unicens.c | 2 +- HAL-afb/hal-most-unicens/wrap_volume.c | 53 ++++++++++++++++++++++++++++- HAL-afb/hal-most-unicens/wrap_volume.h | 2 ++ nbproject/configurations.xml | 8 +---- 5 files changed, 61 insertions(+), 15 deletions(-) diff --git a/HAL-afb/hal-most-unicens/hal_most_unicens.c b/HAL-afb/hal-most-unicens/hal_most_unicens.c index ace9399..a5971fb 100644 --- a/HAL-afb/hal-most-unicens/hal_most_unicens.c +++ b/HAL-afb/hal-most-unicens/hal_most_unicens.c @@ -23,11 +23,11 @@ #include "wrap_unicens.h" #include "wrap_volume.h" +#define PCM_MAX_CHANNELS 6 + static int master_volume; static json_bool master_switch; -static int pcm_volume[6]; - -/*static uint8_t test[3] = {0x07,0x03,0xFF};*/ +static int pcm_volume[PCM_MAX_CHANNELS]; void unicens_master_vol_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { @@ -35,9 +35,7 @@ void unicens_master_vol_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* hand if (wrap_json_unpack(j_obj, "[i!]", &master_volume) == 0) { AFB_NOTICE("master_volume: %s, value=%d", j_str, master_volume); - /*wrap_ucs_i2cwrite(0x270, test, 3); - wrap_ucs_i2cwrite(0x271, test, 3); - wrap_ucs_i2cwrite(0x272, test, 3);*/ + wrap_volume_master(master_volume); } else { AFB_NOTICE("master_volume: INVALID STRING %s", j_str); @@ -63,6 +61,7 @@ void unicens_pcm_vol_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, if (wrap_json_unpack(j_obj, "[iiiiii!]", &pcm_volume[0], &pcm_volume[1], &pcm_volume[2], &pcm_volume[3], &pcm_volume[4], &pcm_volume[5]) == 0) { AFB_NOTICE("pcm_vol: %s", j_str); + wrap_volume_pcm(pcm_volume, PCM_MAX_CHANNELS/*array size*/); } else { AFB_NOTICE("pcm_vol: INVALID STRING %s", j_str); diff --git a/HAL-afb/hal-most-unicens/wrap_unicens.c b/HAL-afb/hal-most-unicens/wrap_unicens.c index 2bd1e91..0cac86b 100644 --- a/HAL-afb/hal-most-unicens/wrap_unicens.c +++ b/HAL-afb/hal-most-unicens/wrap_unicens.c @@ -191,7 +191,7 @@ OnErrorExit: static void wrap_ucs_i2cwrite_cb(void *closure, int status, struct json_object *j_result) { - AFB_NOTICE("wrap_ucs_i2cwrite_cb: closure=%p status=%d, res=%s", closure, status, json_object_to_json_string(j_result)); + AFB_DEBUG("wrap_ucs_i2cwrite_cb: closure=%p status=%d, res=%s", closure, status, json_object_to_json_string(j_result)); if (closure) { async_job_t *job_ptr = (async_job_t *)closure; diff --git a/HAL-afb/hal-most-unicens/wrap_volume.c b/HAL-afb/hal-most-unicens/wrap_volume.c index 438704f..cf70647 100644 --- a/HAL-afb/hal-most-unicens/wrap_volume.c +++ b/HAL-afb/hal-most-unicens/wrap_volume.c @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -53,6 +54,19 @@ static void wrap_volume_service_cb(uint16_t timeout) { NULL); } +/* Retrieves a new value adapted to a new maximum value. Minimum value is + * always zero. */ +static int wrap_volume_calculate(int value, int max_old, int max_new) { + + if (value > max_old) + value = max_old; + + value = (value * max_new) / max_old; /* calculate range: 0..255 */ + assert(value <= max_new); + + return value; +} + extern int wrap_volume_init(void) { uint8_t ret = 0U; @@ -64,4 +78,41 @@ extern int wrap_volume_init(void) { ret = lib_most_volume_init(&mv_init); return ret * (-1); -} \ No newline at end of file +} + +extern int wrap_volume_master(int volume) { + + int new_value, ret; + + new_value = wrap_volume_calculate(volume, 100, 255); + ret = lib_most_volume_set(LIB_MOST_VOLUME_MASTER, (uint8_t)new_value); + + if (ret != 0) { + AFB_ERROR("wrap_volume_master: volume library not ready."); + ret = ret * (-1); /* make return value negative */ + } + + return ret; +} + +extern int wrap_volume_pcm(int *volume_ptr, int volume_sz) { + + const int MAX_PCM_CHANNELS = 6; + int cnt, ret; + assert(volume_ptr != NULL); + assert(volume_sz <= MAX_PCM_CHANNELS); + + for (cnt = 0; cnt < volume_sz; cnt ++) { + + int new_value = wrap_volume_calculate(volume_ptr[cnt], 100, 255); + ret = lib_most_volume_set((enum lib_most_volume_channel_t)cnt, (uint8_t)new_value); + + if (ret != 0) { + AFB_ERROR("wrap_volume_pcm: volume library not ready."); + ret = ret * (-1); /* make return value negative */ + break; + } + } + + return ret; +} diff --git a/HAL-afb/hal-most-unicens/wrap_volume.h b/HAL-afb/hal-most-unicens/wrap_volume.h index b17bc26..3378540 100644 --- a/HAL-afb/hal-most-unicens/wrap_volume.h +++ b/HAL-afb/hal-most-unicens/wrap_volume.h @@ -21,3 +21,5 @@ #include extern int wrap_volume_init(void); +extern int wrap_volume_master(int volume); +extern int wrap_volume_pcm(int *volume_ptr, int volume_sz); diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 70d59f9..f0f58e2 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -495,18 +495,12 @@ flavor2="3"> - ../../../opt/include/afb /opt/AGL/include/afb HAL-afb/hal-most-unicens - /usr/include/json-c /usr/lib64/gcc/x86_64-suse-linux/5/include + HAL-afb/hal-most-unicens/ucs2-vol/inc build/HAL-afb/hal-most-unicens - - CONTROL_CDEV_RX="/dev/inic-usb-crx" - CONTROL_CDEV_TX="/dev/inic-usb-ctx" - MAX_SND_CARD=16 - -- cgit 1.2.3-korg From abe141f1d4dd46fe2bda6cc56db0ceaa964d87b1 Mon Sep 17 00:00:00 2001 From: Tobias Jahnke Date: Fri, 11 Aug 2017 09:28:55 +0200 Subject: project name HAL_MOST_UNICENS is now uppercase --- HAL-afb/HAL_MOST_UNICENS/CMakeLists.txt | 49 ++ HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c | 155 ++++ HAL-afb/HAL_MOST_UNICENS/ucs2-vol/CMakeLists.txt | 26 + .../ucs2-vol/inc/device_container.h | 59 ++ .../HAL_MOST_UNICENS/ucs2-vol/inc/device_value.h | 91 ++ .../HAL_MOST_UNICENS/ucs2-vol/inc/libmostvolume.h | 104 +++ HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc/setup.h | 70 ++ .../HAL_MOST_UNICENS/ucs2-vol/src/CMakeLists.txt | 39 + .../ucs2-vol/src/device_container.cpp | 166 ++++ .../HAL_MOST_UNICENS/ucs2-vol/src/device_value.cpp | 130 +++ .../ucs2-vol/src/libmostvolume.cpp | 86 ++ HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/setup.cpp | 89 ++ HAL-afb/HAL_MOST_UNICENS/wrap-json.c | 940 +++++++++++++++++++++ HAL-afb/HAL_MOST_UNICENS/wrap-json.h | 47 ++ HAL-afb/HAL_MOST_UNICENS/wrap-json.md | 306 +++++++ HAL-afb/HAL_MOST_UNICENS/wrap_unicens.c | 256 ++++++ HAL-afb/HAL_MOST_UNICENS/wrap_unicens.h | 34 + HAL-afb/HAL_MOST_UNICENS/wrap_volume.c | 118 +++ HAL-afb/HAL_MOST_UNICENS/wrap_volume.h | 25 + HAL-afb/hal-most-unicens/CMakeLists.txt | 49 -- HAL-afb/hal-most-unicens/hal_most_unicens.c | 155 ---- HAL-afb/hal-most-unicens/ucs2-vol/CMakeLists.txt | 26 - .../ucs2-vol/inc/device_container.h | 59 -- .../hal-most-unicens/ucs2-vol/inc/device_value.h | 91 -- .../hal-most-unicens/ucs2-vol/inc/libmostvolume.h | 104 --- HAL-afb/hal-most-unicens/ucs2-vol/inc/setup.h | 70 -- .../hal-most-unicens/ucs2-vol/src/CMakeLists.txt | 39 - .../ucs2-vol/src/device_container.cpp | 166 ---- .../hal-most-unicens/ucs2-vol/src/device_value.cpp | 130 --- .../ucs2-vol/src/libmostvolume.cpp | 86 -- HAL-afb/hal-most-unicens/ucs2-vol/src/setup.cpp | 89 -- HAL-afb/hal-most-unicens/wrap-json.c | 940 --------------------- HAL-afb/hal-most-unicens/wrap-json.h | 47 -- HAL-afb/hal-most-unicens/wrap-json.md | 306 ------- HAL-afb/hal-most-unicens/wrap_unicens.c | 256 ------ HAL-afb/hal-most-unicens/wrap_unicens.h | 34 - HAL-afb/hal-most-unicens/wrap_volume.c | 118 --- HAL-afb/hal-most-unicens/wrap_volume.h | 25 - nbproject/configurations.xml | 217 ++--- 39 files changed, 2902 insertions(+), 2895 deletions(-) create mode 100644 HAL-afb/HAL_MOST_UNICENS/CMakeLists.txt create mode 100644 HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c create mode 100644 HAL-afb/HAL_MOST_UNICENS/ucs2-vol/CMakeLists.txt create mode 100644 HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc/device_container.h create mode 100644 HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc/device_value.h create mode 100644 HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc/libmostvolume.h create mode 100644 HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc/setup.h create mode 100644 HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/CMakeLists.txt create mode 100644 HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/device_container.cpp create mode 100644 HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/device_value.cpp create mode 100644 HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/libmostvolume.cpp create mode 100644 HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/setup.cpp create mode 100644 HAL-afb/HAL_MOST_UNICENS/wrap-json.c create mode 100644 HAL-afb/HAL_MOST_UNICENS/wrap-json.h create mode 100644 HAL-afb/HAL_MOST_UNICENS/wrap-json.md create mode 100644 HAL-afb/HAL_MOST_UNICENS/wrap_unicens.c create mode 100644 HAL-afb/HAL_MOST_UNICENS/wrap_unicens.h create mode 100644 HAL-afb/HAL_MOST_UNICENS/wrap_volume.c create mode 100644 HAL-afb/HAL_MOST_UNICENS/wrap_volume.h delete mode 100644 HAL-afb/hal-most-unicens/CMakeLists.txt delete mode 100644 HAL-afb/hal-most-unicens/hal_most_unicens.c delete mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/CMakeLists.txt delete mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/inc/device_container.h delete mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/inc/device_value.h delete mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/inc/libmostvolume.h delete mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/inc/setup.h delete mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/src/CMakeLists.txt delete mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/src/device_container.cpp delete mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/src/device_value.cpp delete mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/src/libmostvolume.cpp delete mode 100644 HAL-afb/hal-most-unicens/ucs2-vol/src/setup.cpp delete mode 100644 HAL-afb/hal-most-unicens/wrap-json.c delete mode 100644 HAL-afb/hal-most-unicens/wrap-json.h delete mode 100644 HAL-afb/hal-most-unicens/wrap-json.md delete mode 100644 HAL-afb/hal-most-unicens/wrap_unicens.c delete mode 100644 HAL-afb/hal-most-unicens/wrap_unicens.h delete mode 100644 HAL-afb/hal-most-unicens/wrap_volume.c delete mode 100644 HAL-afb/hal-most-unicens/wrap_volume.h diff --git a/HAL-afb/HAL_MOST_UNICENS/CMakeLists.txt b/HAL-afb/HAL_MOST_UNICENS/CMakeLists.txt new file mode 100644 index 0000000..e532191 --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/CMakeLists.txt @@ -0,0 +1,49 @@ +########################################################################### +# Copyright 2015, 2016, 2017 IoT.bzh +# +# author: Fulup Ar Foll +# +# 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. +########################################################################### + +ADD_SUBDIRECTORY(ucs2-vol) + +# Add target to project dependency list +PROJECT_TARGET_ADD(hal-most-unicens) + + # Define project Targets + ADD_LIBRARY(hal-most-unicens MODULE hal_most_unicens.c wrap-json.c wrap_unicens.c wrap_volume.c) + + # Binder exposes a unique public entry point + SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES + PREFIX "afb-" + LABELS "BINDING" + LINK_FLAGS ${BINDINGS_LINK_FLAG} + OUTPUT_NAME ${TARGET_NAME} + ) + + # Library dependencies (include updates automatically) + TARGET_LINK_LIBRARIES(${TARGET_NAME} + hal-interface + audio-interface + ucs2-volume + ) + + # installation directory + INSTALL(TARGETS ${TARGET_NAME} + LIBRARY DESTINATION ${BINDINGS_INSTALL_DIR}) + + # Define properties to expose when others use this target + TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME} + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/ucs2-vol/inc + ) diff --git a/HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c b/HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c new file mode 100644 index 0000000..a5971fb --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c @@ -0,0 +1,155 @@ +/* + * Copyright (C) 2017, Microchip Technology Inc. and its subsidiaries. + * Author Tobias Jahnke + * + * 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 "hal-interface.h" +#include "audio-interface.h" +#include "wrap-json.h" +#include "string.h" +#include "wrap_unicens.h" +#include "wrap_volume.h" + +#define PCM_MAX_CHANNELS 6 + +static int master_volume; +static json_bool master_switch; +static int pcm_volume[PCM_MAX_CHANNELS]; + +void unicens_master_vol_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { + + const char *j_str = json_object_to_json_string(j_obj); + + if (wrap_json_unpack(j_obj, "[i!]", &master_volume) == 0) { + AFB_NOTICE("master_volume: %s, value=%d", j_str, master_volume); + wrap_volume_master(master_volume); + } + else { + AFB_NOTICE("master_volume: INVALID STRING %s", j_str); + } +} + +void unicens_master_switch_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { + + const char *j_str = json_object_to_json_string(j_obj); + + if (wrap_json_unpack(j_obj, "[b!]", &master_switch) == 0) { + AFB_NOTICE("master_switch: %s, value=%d", j_str, master_switch); + } + else { + AFB_NOTICE("master_switch: INVALID STRING %s", j_str); + } +} + +void unicens_pcm_vol_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { + + const char *j_str = json_object_to_json_string(j_obj); + + if (wrap_json_unpack(j_obj, "[iiiiii!]", &pcm_volume[0], &pcm_volume[1], &pcm_volume[2], &pcm_volume[3], + &pcm_volume[4], &pcm_volume[5]) == 0) { + AFB_NOTICE("pcm_vol: %s", j_str); + wrap_volume_pcm(pcm_volume, PCM_MAX_CHANNELS/*array size*/); + } + else { + AFB_NOTICE("pcm_vol: INVALID STRING %s", j_str); + } +} + +/* declare ALSA mixer controls */ +STATIC alsaHalMapT alsaHalMap[]= { + { .tag=Master_Playback_Volume, .cb={.callback=unicens_master_vol_cb, .handle=&master_volume}, .info="Sets master playback volume", + .ctl={.numid=1, .type=SND_CTL_ELEM_TYPE_INTEGER, .count=1, .minval=0, .maxval=100, .step=1, .value=50, .name="Master Playback Volume"} + }, + /*{ .tag=Master_OnOff_Switch, .cb={.callback=unicens_master_switch_cb, .handle=&master_switch}, .info="Sets master playback switch", + .ctl={.numid=2, .type=SND_CTL_ELEM_TYPE_BOOLEAN, .count=1, .minval=0, .maxval=1, .step=1, .value=1, .name="Master Playback Switch"} + },*/ + { .tag=PCM_Playback_Volume, .cb={.callback=unicens_pcm_vol_cb, .handle=&pcm_volume}, .info="Sets PCM playback volume", + .ctl={.numid=3, .type=SND_CTL_ELEM_TYPE_INTEGER, .count=6, .minval=0, .maxval=100, .step=1, .value=100, .name="PCM Playback Volume"} + }, + { .tag=EndHalCrlTag} /* marker for end of the array */ +} ; + +/* HAL sound card mapping info */ +STATIC alsaHalSndCardT alsaHalSndCard = { + .name = "Microchip MOST:1", /* WARNING: name MUST match with 'aplay -l' */ + .info = "HAL for MICROCHIP MOST sound card controlled by UNICENS binding", + .ctls = alsaHalMap, + .volumeCB = NULL, /* use default volume normalization function */ +}; + +/* initializes ALSA sound card, UNICENS API */ +STATIC int unicens_service_init() { + int err = 0; + AFB_NOTICE("Initializing HAL-MOST-UNICENS-BINDING"); + + err = halServiceInit(afbBindingV2.api, &alsaHalSndCard); + if (err) { + AFB_ERROR("Cannot initialize ALSA soundcard."); + goto OnErrorExit; + } + + err= afb_daemon_require_api("UNICENS", 1); + if (err) { + AFB_ERROR("Failed to access UNICENS API"); + goto OnErrorExit; + } + + err = wrap_ucs_subscribe_sync(); + if (err) { + AFB_ERROR("Failed to subscribe to UNICENS binding"); + goto OnErrorExit; + } + + err = wrap_ucs_initialize_sync("/home/agluser/DEVELOPMENT/AGL/BINDING/unicens2-binding/data/config_multichannel_audio_kit.xml"); + if (err) { + AFB_ERROR("Failed to initialize UNICENS binding"); + goto OnErrorExit; + } + + err = wrap_volume_init(); + if (err) { + AFB_ERROR("Failed to initialize wrapper for volume library"); + goto OnErrorExit; + } + +OnErrorExit: + AFB_NOTICE("Initializing HAL-MOST-UNICENS-BINDING done.."); + return err; +} + +// This receive all event this binding subscribe to +PUBLIC void unicens_event_cb(const char *evtname, json_object *j_event) { + + if (strncmp(evtname, "alsacore/", 9) == 0) { + halServiceEvent(evtname, j_event); + return; + } + + if (strncmp(evtname, "UNICENS/", 8) == 0) { + AFB_NOTICE("unicens_event_cb: evtname=%s [msg=%s]", evtname, json_object_get_string(j_event)); + return; + } + + AFB_NOTICE("unicens_event_cb: UNHANDLED EVENT, evtname=%s [msg=%s]", evtname, json_object_get_string(j_event)); +} + +/* API prefix should be unique for each snd card */ +PUBLIC const struct afb_binding_v2 afbBindingV2 = { + .api = "hal-most-unicens", + .init = unicens_service_init, + .verbs = halServiceApi, + .onevent = unicens_event_cb, +}; diff --git a/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/CMakeLists.txt b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/CMakeLists.txt new file mode 100644 index 0000000..8735677 --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/CMakeLists.txt @@ -0,0 +1,26 @@ +########################################################################### +# Copyright 2015, 2016, 2017 IoT.bzh +# +# author: Fulup Ar Foll +# +# 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 subproject targets +ADD_SUBDIRECTORY(src) + + + + diff --git a/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc/device_container.h b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc/device_container.h new file mode 100644 index 0000000..5e3b753 --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc/device_container.h @@ -0,0 +1,59 @@ +/* + * libmostvolume example + * + * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +#ifndef DEVICECONTAINER_H +#define DEVICECONTAINER_H + +#include "device_value.h" +#include "libmostvolume.h" + +class CDeviceContainer { + +public: + CDeviceContainer(); + virtual ~CDeviceContainer(); + + void AssignService(lib_most_volume_init_t *init_ptr) {_init_ptr = init_ptr;} + + void RegisterValues(CDeviceValue** list_pptr, uint16_t list_sz); + void SetValue(uint16_t key, uint8_t value); + void ClearValues(); + void Update(); + +private: + void RequestService(uint16_t timeout); + void IncrementProcIndex(void); + void HandleI2cResult(uint8_t result); + + static void OnI2cResult(uint8_t result, void *obj_ptr); + + uint16_t _idx_processing; + uint16_t _values_sz; + CDeviceValue **_values_pptr; + bool _tx_busy; + bool _service_requested; + lib_most_volume_init_t *_init_ptr; +}; + +#endif /* DEVICECONTAINER_H */ + diff --git a/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc/device_value.h b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc/device_value.h new file mode 100644 index 0000000..21fdbdc --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc/device_value.h @@ -0,0 +1,91 @@ +/* + * libmostvolume example + * + * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +#ifndef DEVICEVALUE_H +#define DEVICEVALUE_H + +#include +#include "libmostvolume.h" + +struct SRxMessage { + uint32_t devInst; /*instance if multiple devices are using the same group id*/ + uint32_t sourceAddr; + uint32_t targetAddr; + uint32_t nFBlock; + uint32_t nInst; + uint32_t nFunc; + uint32_t nOpType; + const uint8_t *pPayload; + uint32_t payloadLen; +}; + +struct STxMessage { + uint32_t devInst; /*instance if multiple devices are using the same group id*/ + uint32_t targetAddr; + uint32_t nFBlock; + uint32_t nInst; + uint32_t nFunc; + uint32_t nOpType; + const uint8_t *pPayload; + uint32_t payloadLen; +}; + +enum DeviceValueType { + DEVICE_VAL_MASTER = 0, + DEVICE_VAL_LEFT = 1, + DEVICE_VAL_RIGHT = 2 + +}; + +class CDeviceValue { +public: + CDeviceValue(uint16_t address, DeviceValueType type, uint16_t key); + virtual ~CDeviceValue(); + + uint16_t GetKey(){return _key;} + DeviceValueType GetType(){return _type;} // returns the assigned type + void SetValue(uint8_t value){_target_value = value;} // sets desired value + + bool RequiresUpdate(); // returns true if target is not actual value + // returns true if success, false if failed + // -> stop transmission + bool FireUpdateMessage(lib_most_volume_writei2c_cb_t writei2c_fptr, + lib_most_volume_writei2c_result_cb_t result_fptr, + void *result_user_ptr);// fires message & updates actual value + +private: + void HandleI2cResult(uint8_t result); + void ApplyMostValue(uint8_t value, DeviceValueType type, uint8_t tx_payload[]); + + bool _is_initial; // ensure first update + DeviceValueType _type; // determines the remote i2c command + uint16_t _key; // lookup key + uint16_t _address; // target node/group address + uint8_t _target_value; // desired value + uint8_t _actual_value; // value set and confirmed via network + uint8_t _tx_payload[20]; + uint8_t _tx_payload_sz; +}; + +#endif /* DEVICEPROPERTY_H */ + diff --git a/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc/libmostvolume.h b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc/libmostvolume.h new file mode 100644 index 0000000..1d7249d --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc/libmostvolume.h @@ -0,0 +1,104 @@ +/* + * libmostvolume example + * + * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +#ifndef LIB_MOST_VOLUME_H +#define LIB_MOST_VOLUME_H + +#include + + +#ifdef __cplusplus +extern "C" { +#endif + +/** Describes the volume control */ +enum lib_most_volume_channel_t{ + LIB_MOST_VOLUME_CH_FRONT_LEFT = 0, + LIB_MOST_VOLUME_CH_FRONT_RIGHT = 1, + LIB_MOST_VOLUME_CH_REAR_LEFT = 2, + LIB_MOST_VOLUME_CH_REAR_RIGHT = 3, + LIB_MOST_VOLUME_CH_CENTER = 4, + LIB_MOST_VOLUME_CH_SUB = 5, + LIB_MOST_VOLUME_MASTER = 6 +}; + +/** Is fired when the application shall call "lib_most_volume_service()" after a certain time + * \param timeout Time in ms after which the application shall call lib_most_volume_service(). + * Valid values: + * 0x0000: as soon as possible, + * 0x0001..0xFFFE: timeout in ms, + * 0xFFFF: never + */ +typedef void (*lib_most_volume_service_cb_t)(uint16_t timeout); + +typedef void (*lib_most_volume_writei2c_result_cb_t)(uint8_t result, void *user_ptr); + +/** Callback function required to write volume changes to network devices + * \param node The node address + * \param data_ptr I2C command data + * \param data_sz Size of the I2C command data + * \return Returns \c 0 for success, other value on failure. + */ +typedef int (*lib_most_volume_writei2c_cb_t)(uint16_t node, uint8_t *data_ptr, uint8_t data_sz, + lib_most_volume_writei2c_result_cb_t result_fptr, + void *result_user_ptr); + +typedef struct lib_most_volume_init_ { + lib_most_volume_service_cb_t service_cb; + lib_most_volume_writei2c_cb_t writei2c_cb; + +} lib_most_volume_init_t; + + +/** Initializes the library + * \param UNICENS_inst Reference to the UNICENS instance, created by the application. + * \param req_service_fptr Callback function which is fired if the application shall call + * lib_most_volume_service. + * \return '0' on success, otherwise value >'0'. + */ +extern uint8_t lib_most_volume_init(lib_most_volume_init_t *init_ptr); + +/** Terminates the library + * \return '0' on success, otherwise value >'0'. + */ +extern uint8_t lib_most_volume_exit(void); + +/** Sets a single volume value. + * \param channel The volume control to be set. + * \param volume The volume value to be set. Valid values: 0..255. + * \return '0' on success, otherwise value >'0'. + */ +extern uint8_t lib_most_volume_set(enum lib_most_volume_channel_t channel, uint8_t volume); + +/** Shall be called either cyclically (e.g. 50ms -> polling) or after "timeout" + * when "service_fptr" is fired (-> event triggered). + * \return '0' on success, otherwise value >'0'. + */ +extern uint8_t lib_most_volume_service(void); + +#ifdef __cplusplus +} +#endif + +#endif /* LIB_MOST_VOLUME_H */ + diff --git a/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc/setup.h b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc/setup.h new file mode 100644 index 0000000..63bc0e2 --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc/setup.h @@ -0,0 +1,70 @@ +/* + * libmostvolume example + * + * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +#ifndef SETUP_H +#define SETUP_H + +#include "libmostvolume.h" +#include "device_value.h" +#include "device_container.h" + +#define MAX_CHANNELS (LIB_MOST_VOLUME_MASTER + 1u) +#define MAX_MASTERS 3u + +class CSetup { + +public: + void Configure(lib_most_volume_init_t *init_ptr); + void SetVolume(enum lib_most_volume_channel_t channel, uint8_t volume); + void Update(); + +private: + CDeviceValue _volume_amp_270_m; + CDeviceValue _volume_amp_270_l; + CDeviceValue _volume_amp_270_r; + + CDeviceValue _volume_amp_271_m; + CDeviceValue _volume_amp_271_l; + CDeviceValue _volume_amp_271_r; + + CDeviceValue _volume_amp_272_m; + CDeviceValue _volume_amp_272_l; + CDeviceValue _volume_amp_272_r; + + CDeviceContainer _value_container; + lib_most_volume_init_t init_data; + +public: + static CSetup* GetInstance(); // singleton + static void Release(); // singleton + +protected: + CSetup(); // singleton + virtual ~CSetup(); // singleton + +private: + static CSetup* _instance; // singleton +}; + +#endif /* SETUP_H */ + diff --git a/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/CMakeLists.txt b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/CMakeLists.txt new file mode 100644 index 0000000..1be7184 --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/CMakeLists.txt @@ -0,0 +1,39 @@ +########################################################################### +# Copyright 2015, 2016, 2017 IoT.bzh +# +# author: Fulup Ar Foll +# +# 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. +########################################################################### + +# Add target to project dependency list +PROJECT_TARGET_ADD(ucs2-volume) + +# Define targets source files +ADD_LIBRARY(${TARGET_NAME} STATIC device_container.cpp device_value.cpp libmostvolume.cpp setup.cpp) + + # Library properties + SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES + OUTPUT_NAME ${TARGET_NAME} + ) + + # Library dependencies from PKG_REQUIRED_LIST + #TARGET_LINK_LIBRARIES(ucs2-volume # Library dependencies (include updates automatically) + # ${link_libraries} + #) + + # Define properties to expose when others use this target + TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME} + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../inc + ) + diff --git a/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/device_container.cpp b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/device_container.cpp new file mode 100644 index 0000000..cef25b1 --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/device_container.cpp @@ -0,0 +1,166 @@ +/* + * libmostvolume example + * + * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +#include +#include "device_container.h" + +#define DEVCONT_TIME_RETRIGGER (uint16_t)100U +#define DEVCONT_TIME_NOW (uint16_t)0U +#define DEVCONT_TIME_STOP (uint16_t)0xFFFFU + +#define DEVCONT_UNUSED(a) (a = a) + +CDeviceContainer::CDeviceContainer() +{ + this->_idx_processing = 0U; + this->_values_pptr = NULL; + this->_values_sz = 0U; + this->_tx_busy = false; + this->_service_requested = false; + this->_init_ptr = NULL; +} + +CDeviceContainer::~CDeviceContainer() +{ + /*Clb_RegisterI2CResultCB(NULL, NULL);*/ /* avoid that the result callback is fired after object is destroyed */ +} + +void CDeviceContainer::RegisterValues(CDeviceValue** list_pptr, uint16_t list_sz) +{ + this->_idx_processing = 0U; + this->_values_pptr = list_pptr; + this->_values_sz = list_sz; + this->_tx_busy = false; + + if ((list_pptr != NULL) && (list_sz > 0U)) + { + this->_idx_processing = (uint16_t)(list_sz - 1U); + } +} + +void CDeviceContainer::ClearValues() +{ + this->_idx_processing = 0U; + this->_values_pptr = NULL; + this->_values_sz = 0U; + this->_tx_busy = false; +} + +void CDeviceContainer::SetValue(uint16_t key, uint8_t value) +{ + uint16_t idx; + bool req_update = false; + + for (idx = 0U; idx < this->_values_sz; idx++) + { + if (this->_values_pptr[idx]->GetKey() == key) + { + this->_values_pptr[idx]->SetValue(value); + if (this->_values_pptr[idx]->RequiresUpdate()) + { + req_update = true; + } + } + } + + if (req_update && (!this->_tx_busy)) + { + RequestService(DEVCONT_TIME_NOW); //fire callback + } +} + +void CDeviceContainer::IncrementProcIndex(void) +{ + if ((_idx_processing + 1U) >= this->_values_sz) + { + _idx_processing = 0U; + } + else + { + _idx_processing++; + } +} + +// starts at latest position, searches next value to update, waits until response +void CDeviceContainer::Update() +{ + uint16_t cnt; + bool error = false; + _service_requested = false; + + if (this->_tx_busy) + { + return; + } + + for (cnt = 0u; cnt < this->_values_sz; cnt++) /* just run one cycle */ + { + IncrementProcIndex(); + + if (_values_pptr[_idx_processing]->RequiresUpdate()) + { + if (_values_pptr[_idx_processing]->FireUpdateMessage(this->_init_ptr->writei2c_cb, + &OnI2cResult, + this)) + { + /*Clb_RegisterI2CResultCB(&OnI2cResult, this);*/ + this->_tx_busy = true; + break; + } + else + { + error = true; + } + } + } + + if (error) + { + RequestService(DEVCONT_TIME_RETRIGGER); + } +} + +void CDeviceContainer::HandleI2cResult(uint8_t result) +{ + DEVCONT_UNUSED(result); + this->_tx_busy = false; + this->RequestService(DEVCONT_TIME_NOW); +} + +void CDeviceContainer::OnI2cResult(uint8_t result, void *obj_ptr) +{ + ((CDeviceContainer*)obj_ptr)->HandleI2cResult(result); +} + +void CDeviceContainer::RequestService(uint16_t timeout) +{ + if (!_service_requested) + { + _service_requested = true; + + if (this->_init_ptr && this->_init_ptr->service_cb) + { + this->_init_ptr->service_cb(timeout); + } + } +} diff --git a/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/device_value.cpp b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/device_value.cpp new file mode 100644 index 0000000..b8c681e --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/device_value.cpp @@ -0,0 +1,130 @@ +/* + * libmostvolume example + * + * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +#include "device_value.h" +#include "setup.h" + +#define MUTE_VALUE 0x03FFU +#define MUTE_VALUE_HB 0x03U +#define MUTE_VALUE_LB 0xFFU + +#define CONTROL_MASTER 0x07U +#define CONTROL_CH_1 0x08U +#define CONTROL_CH_2 0x09U + +CDeviceValue::CDeviceValue(uint16_t address, DeviceValueType type, uint16_t key) +{ + this->_is_initial = true; + this->_address = address; + this->_target_value = 0x01u; + this->_actual_value = 0x01u; + + this->_type = type; + this->_key = key; + + _tx_payload[0] = CONTROL_MASTER;// 7: master, 8: channel 1, 9: Channel 2 + _tx_payload[1] = MUTE_VALUE_HB; //HB:Volume + _tx_payload[2] = MUTE_VALUE_LB; //LB:Volume + _tx_payload_sz = 3u; +} + +CDeviceValue::~CDeviceValue() +{ +} + +void CDeviceValue::ApplyMostValue(uint8_t value, DeviceValueType type, uint8_t tx_payload[]) +{ + uint16_t tmp = MUTE_VALUE; + + switch (type) + { + case DEVICE_VAL_LEFT: + tmp = (uint16_t)(0x80U + 0x37FU - (0x37FU * ((int32_t)value) / (0xFFU))); + //tmp = 0x3FF - (0x3FF * ((int32_t)value) / (0xFF)); + //tmp = 0x100 + 0x2FF - (0x2FF * ((int32_t)value) / (0xFF)); + tx_payload[0] = CONTROL_CH_1; + break; + case DEVICE_VAL_RIGHT: + tmp = (uint16_t)(0x80U + 0x37FU - (0x37FU * ((int32_t)value) / (0xFFU))); + //tmp = 0x3FF - (0x3FF * ((int32_t)value) / (0xFF)); + //tmp = 0x100 + 0x2FF - (0x2FF * ((int32_t)value) / (0xFF)); + tx_payload[0] = CONTROL_CH_2; + break; + default: + /*std::cerr << "CDeviceValue::ApplyMostValue() error matching incorrect" << std::endl;*/ + case DEVICE_VAL_MASTER: + tmp = (uint16_t)(0x100U + 0x2FFU - (0x2FFU * ((int32_t)value) / (0xFFU))); + tx_payload[0] = CONTROL_MASTER; + break; + } + + tx_payload[1] = (uint8_t)((tmp >> 8U) & (uint16_t)0xFFU); //HB:Volume + tx_payload[2] = (uint8_t)(tmp & (uint16_t)0xFFU); //LB:Volume +} + +// returns true if target is not actual value +bool CDeviceValue::RequiresUpdate() +{ + if (this->_target_value != this->_actual_value) + { + return true; + } + + return false; +} + +bool CDeviceValue::FireUpdateMessage(lib_most_volume_writei2c_cb_t writei2c_fptr, + lib_most_volume_writei2c_result_cb_t result_fptr, + void *result_user_ptr) +{ + int ret; + ApplyMostValue(this->_target_value, _type, _tx_payload); + + ret = writei2c_fptr(this->_address, &_tx_payload[0], _tx_payload_sz, + result_fptr, + result_user_ptr); + + if (ret == 0) + { + // Clb_RegisterI2CResultCB(OnI2cResult, this); + // mark value as set! + this->_actual_value = this->_target_value; + return true; + } + + return false; +} + +#if 0 + ret = Ucs_I2c_WritePort( CSetup::GetInstance()->RetrieveUnicensInst(), + this->_address, + 0x0F00u, /* i2c port handle */ + UCS_I2C_DEFAULT_MODE, /* 0 */ + 0u, /* block count */ + 0x2Au, /* i2c slave address */ + 0x03E8u, /* timeout 1000 milliseconds */ + _tx_payload_sz, /* data length */ + &_tx_payload[0], /* data pointer */ + &Clb_OnWriteI2CPortResult + ); +#endif diff --git a/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/libmostvolume.cpp b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/libmostvolume.cpp new file mode 100644 index 0000000..a90b5b1 --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/libmostvolume.cpp @@ -0,0 +1,86 @@ +/* + * libmostvolume example + * + * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ + +#include "libmostvolume.h" +#include "setup.h" +/*#include */ + +static bool _running = false; + +extern "C" uint8_t lib_most_volume_init(lib_most_volume_init_t *init_ptr) +{ + uint8_t success = 1U; + /*std::cerr << "lib_most_volume_init(): called" << std::endl;*/ + + if (!_running && init_ptr) + { + CSetup::GetInstance()->Configure(init_ptr); + success = 0U; + _running = true; + } + + return success; +} + +extern "C" uint8_t lib_most_volume_exit(void) +{ + uint8_t success = 1U; + /*std::cerr << "lib_most_volume_exit(): called" << std::endl;*/ + + if (_running) + { + CSetup::Release(); + success = 0U; + _running = false; + } + + return success; +} + +extern "C" uint8_t lib_most_volume_set(enum lib_most_volume_channel_t channel, uint8_t volume) +{ + uint8_t success = 1U; + /*std::cerr << "lib_most_volume_set(): channel=" << channel << ", volume=" << (int)volume << std::endl;*/ + + if (_running) + { + CSetup::GetInstance()->SetVolume(channel, volume); + success = 0U; + } + + return success; +} + +extern "C" uint8_t lib_most_volume_service(void) +{ + uint8_t success = 1U; + /*std::cerr << "lib_most_volume_service(): called" << std::endl;*/ + + if (_running) + { + CSetup::GetInstance()->Update(); + success = 0U; + } + + return success; +} diff --git a/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/setup.cpp b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/setup.cpp new file mode 100644 index 0000000..61b8a68 --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src/setup.cpp @@ -0,0 +1,89 @@ +/* + * libmostvolume example + * + * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * You may also obtain this software under a propriety license from Microchip. + * Please contact Microchip for further information. + * + */ +#include +#include "setup.h" + +CSetup* CSetup::_instance = NULL; + +// singleton +CSetup* CSetup::GetInstance() { + if (_instance == NULL) { + _instance = new CSetup(); + } + + return _instance; +} + +// singleton +void CSetup::Release() { + if (_instance != NULL) { + delete _instance; + } + _instance = NULL; +} + +CSetup::CSetup() + : _volume_amp_270_m(0x270U, DEVICE_VAL_MASTER, LIB_MOST_VOLUME_MASTER), + _volume_amp_270_l(0x270U, DEVICE_VAL_LEFT, LIB_MOST_VOLUME_CH_FRONT_LEFT), + _volume_amp_270_r(0x270U, DEVICE_VAL_RIGHT, LIB_MOST_VOLUME_CH_FRONT_RIGHT), + _volume_amp_271_m(0x271U, DEVICE_VAL_MASTER, LIB_MOST_VOLUME_MASTER), + _volume_amp_271_l(0x271U, DEVICE_VAL_LEFT, LIB_MOST_VOLUME_CH_REAR_LEFT), + _volume_amp_271_r(0x271U, DEVICE_VAL_RIGHT, LIB_MOST_VOLUME_CH_REAR_RIGHT), + _volume_amp_272_m(0x272U, DEVICE_VAL_MASTER, LIB_MOST_VOLUME_MASTER), + _volume_amp_272_l(0x272U, DEVICE_VAL_LEFT, LIB_MOST_VOLUME_CH_CENTER), + _volume_amp_272_r(0x272U, DEVICE_VAL_RIGHT, LIB_MOST_VOLUME_CH_SUB), + _value_container() +{ + static CDeviceValue* value_list[9] = { &_volume_amp_270_m, + &_volume_amp_270_l, + &_volume_amp_270_r, + &_volume_amp_271_m, + &_volume_amp_271_l, + &_volume_amp_271_r, + &_volume_amp_272_m, + &_volume_amp_272_l, + &_volume_amp_272_r}; + + _value_container.RegisterValues(value_list, 9U); +} + +CSetup::~CSetup() +{ + +} + +void CSetup::Configure(lib_most_volume_init_t *init_ptr) +{ + this->init_data = *init_ptr; + _value_container.AssignService(&this->init_data); +} + +void CSetup::SetVolume(enum lib_most_volume_channel_t channel, uint8_t volume) +{ + _value_container.SetValue((uint16_t)channel, volume); +} + +void CSetup::Update() +{ + _value_container.Update(); +} diff --git a/HAL-afb/HAL_MOST_UNICENS/wrap-json.c b/HAL-afb/HAL_MOST_UNICENS/wrap-json.c new file mode 100644 index 0000000..987a008 --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/wrap-json.c @@ -0,0 +1,940 @@ +/* + Copyright (C) 2016, 2017 "IoT.bzh" + + author: José Bollo + + 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. +*/ + +#include + +#include "wrap-json.h" + +#define STACKCOUNT 32 +#define STRCOUNT 8 + +enum { + wrap_json_error_none, + wrap_json_error_null_object, + wrap_json_error_truncated, + wrap_json_error_internal_error, + wrap_json_error_out_of_memory, + wrap_json_error_invalid_character, + wrap_json_error_too_long, + wrap_json_error_too_deep, + wrap_json_error_null_spec, + wrap_json_error_null_key, + wrap_json_error_null_string, + wrap_json_error_out_of_range, + wrap_json_error_incomplete, + wrap_json_error_missfit_type, + wrap_json_error_key_not_found, + _wrap_json_error_count_ +}; + +static const char ignore_all[] = " \t\n\r,:"; +static const char pack_accept_arr[] = "][{snbiIfoO"; +static const char pack_accept_key[] = "s}"; +#define pack_accept_any (&pack_accept_arr[1]) + +static const char unpack_accept_arr[] = "*!][{snbiIfFoO"; +static const char unpack_accept_key[] = "*!s}"; +#define unpack_accept_any (&unpack_accept_arr[3]) + +static const char *pack_errors[_wrap_json_error_count_] = +{ + [wrap_json_error_none] = "unknown error", + [wrap_json_error_null_object] = "null object", + [wrap_json_error_truncated] = "truncated", + [wrap_json_error_internal_error] = "internal error", + [wrap_json_error_out_of_memory] = "out of memory", + [wrap_json_error_invalid_character] = "invalid character", + [wrap_json_error_too_long] = "too long", + [wrap_json_error_too_deep] = "too deep", + [wrap_json_error_null_spec] = "spec is NULL", + [wrap_json_error_null_key] = "key is NULL", + [wrap_json_error_null_string] = "string is NULL", + [wrap_json_error_out_of_range] = "array too small", + [wrap_json_error_incomplete] = "incomplete container", + [wrap_json_error_missfit_type] = "missfit of type", + [wrap_json_error_key_not_found] = "key not found" +}; + +int wrap_json_get_error_position(int rc) +{ + if (rc < 0) + rc = -rc; + return (rc >> 4) + 1; +} + +int wrap_json_get_error_code(int rc) +{ + if (rc < 0) + rc = -rc; + return rc & 15; +} + +const char *wrap_json_get_error_string(int rc) +{ + rc = wrap_json_get_error_code(rc); + if (rc >= sizeof pack_errors / sizeof *pack_errors) + rc = 0; + return pack_errors[rc]; +} + + + +static inline const char *skip(const char *d) +{ + while (*d && strchr(ignore_all, *d)) + d++; + return d; +} + +int wrap_json_vpack(struct json_object **result, const char *desc, va_list args) +{ + /* TODO: the case of structs with key being single char should be optimized */ + int nstr, notnull, nullable, rc; + size_t sz, dsz, ssz; + char *s; + char c; + const char *d; + char buffer[256]; + struct { const char *str; size_t sz; } strs[STRCOUNT]; + struct { struct json_object *cont, *key; const char *acc; char type; } stack[STACKCOUNT], *top; + struct json_object *obj; + + ssz = sizeof buffer; + s = buffer; + top = stack; + top->key = NULL; + top->cont = NULL; + top->acc = pack_accept_any; + top->type = 0; + d = desc; + if (!d) + goto null_spec; + d = skip(d); + for(;;) { + c = *d; + if (!c) + goto truncated; + if (!strchr(top->acc, c)) + goto invalid_character; + d = skip(++d); + switch(c) { + case 's': + nullable = 0; + notnull = 0; + nstr = 0; + sz = 0; + for (;;) { + strs[nstr].str = va_arg(args, const char*); + if (strs[nstr].str) + notnull = 1; + if (*d == '?') { + d = skip(++d); + nullable = 1; + } + switch(*d) { + case '%': strs[nstr].sz = va_arg(args, size_t); d = skip(++d); break; + case '#': strs[nstr].sz = (size_t)va_arg(args, int); d = skip(++d); break; + default: strs[nstr].sz = strs[nstr].str ? strlen(strs[nstr].str) : 0; break; + } + sz += strs[nstr++].sz; + if (*d == '?') { + d = skip(++d); + nullable = 1; + } + if (*d != '+') + break; + if (nstr >= STRCOUNT) + goto too_long; + d = skip(++d); + } + if (*d == '*') + nullable = 1; + if (notnull) { + if (sz > ssz) { + ssz += ssz; + if (ssz < sz) + ssz = sz; + s = alloca(sz); + } + dsz = sz; + while (nstr) { + nstr--; + dsz -= strs[nstr].sz; + memcpy(&s[dsz], strs[nstr].str, strs[nstr].sz); + } + obj = json_object_new_string_len(s, (int)sz); + if (!obj) + goto out_of_memory; + } else if (nullable) + obj = NULL; + else + goto null_string; + break; + case 'n': + obj = NULL; + break; + case 'b': + obj = json_object_new_boolean(va_arg(args, int)); + if (!obj) + goto out_of_memory; + break; + case 'i': + obj = json_object_new_int(va_arg(args, int)); + if (!obj) + goto out_of_memory; + break; + case 'I': + obj = json_object_new_int64(va_arg(args, int64_t)); + if (!obj) + goto out_of_memory; + break; + case 'f': + obj = json_object_new_double(va_arg(args, double)); + if (!obj) + goto out_of_memory; + break; + case 'o': + case 'O': + obj = va_arg(args, struct json_object*); + if (*d == '?') + d = skip(++d); + else if (*d != '*' && !obj) + goto null_object; + if (c == 'O') + json_object_get(obj); + break; + case '[': + case '{': + if (++top >= &stack[STACKCOUNT]) + goto too_deep; + top->key = NULL; + if (c == '[') { + top->type = ']'; + top->acc = pack_accept_arr; + top->cont = json_object_new_array(); + } else { + top->type = '}'; + top->acc = pack_accept_key; + top->cont = json_object_new_object(); + } + if (!top->cont) + goto out_of_memory; + continue; + case '}': + case ']': + if (c != top->type || top <= stack) + goto internal_error; + obj = (top--)->cont; + if (*d == '*' && !(c == '}' ? json_object_object_length(obj) : json_object_array_length(obj))) { + json_object_put(obj); + obj = NULL; + } + break; + default: + goto internal_error; + } + switch (top->type) { + case 0: + if (top != stack) + goto internal_error; + if (*d) + goto invalid_character; + *result = obj; + return 0; + case ']': + if (obj || *d != '*') + json_object_array_add(top->cont, obj); + if (*d == '*') + d = skip(++d); + break; + case '}': + if (!obj) + goto null_key; + top->key = obj; + top->acc = pack_accept_any; + top->type = ':'; + break; + case ':': + if (obj || *d != '*') + json_object_object_add(top->cont, json_object_get_string(top->key), obj); + if (*d == '*') + d = skip(++d); + json_object_put(top->key); + top->key = NULL; + top->acc = pack_accept_key; + top->type = '}'; + break; + default: + goto internal_error; + } + } + +null_object: + rc = wrap_json_error_null_object; + goto error; +truncated: + rc = wrap_json_error_truncated; + goto error; +internal_error: + rc = wrap_json_error_internal_error; + goto error; +out_of_memory: + rc = wrap_json_error_out_of_memory; + goto error; +invalid_character: + rc = wrap_json_error_invalid_character; + goto error; +too_long: + rc = wrap_json_error_too_long; + goto error; +too_deep: + rc = wrap_json_error_too_deep; + goto error; +null_spec: + rc = wrap_json_error_null_spec; + goto error; +null_key: + rc = wrap_json_error_null_key; + goto error; +null_string: + rc = wrap_json_error_null_string; + goto error; +error: + do { + json_object_put(top->key); + json_object_put(top->cont); + } while (--top >= stack); + *result = NULL; + rc = rc | (int)((d - desc) << 4); + return -rc; +} + +int wrap_json_pack(struct json_object **result, const char *desc, ...) +{ + int rc; + va_list args; + + va_start(args, desc); + rc = wrap_json_vpack(result, desc, args); + va_end(args); + return rc; +} + +static int vunpack(struct json_object *object, const char *desc, va_list args, int store) +{ + int rc = 0, optionnal, ignore; + char c, xacc[2] = { 0, 0 }; + const char *acc; + const char *d, *fit = NULL; + const char *key = NULL; + const char **ps = NULL; + double *pf = NULL; + int *pi = NULL; + int64_t *pI = NULL; + size_t *pz = NULL; + struct { struct json_object *parent; const char *acc; int index, count; char type; } stack[STACKCOUNT], *top; + struct json_object *obj; + struct json_object **po; + + xacc[0] = 0; + ignore = 0; + top = NULL; + acc = unpack_accept_any; + d = desc; + if (!d) + goto null_spec; + d = skip(d); + obj = object; + for(;;) { + fit = d; + c = *d; + if (!c) + goto truncated; + if (!strchr(acc, c)) + goto invalid_character; + d = skip(++d); + switch(c) { + case 's': + if (xacc[0] == '}') { + /* expects a key */ + key = va_arg(args, const char *); + if (!key) + goto null_key; + if (*d != '?') + optionnal = 0; + else { + optionnal = 1; + d = skip(++d); + } + if (ignore) + ignore++; + else { + if (json_object_object_get_ex(top->parent, key, &obj)) { + /* found */ + top->index++; + } else { + /* not found */ + if (!optionnal) + goto key_not_found; + ignore = 1; + obj = NULL; + } + } + xacc[0] = ':'; + acc = unpack_accept_any; + continue; + } + /* get a string */ + if (store) + ps = va_arg(args, const char **); + if (!ignore) { + if (!json_object_is_type(obj, json_type_string)) + goto missfit; + if (store && ps) + *ps = json_object_get_string(obj); + } + if (*d == '%') { + d = skip(++d); + if (store) { + pz = va_arg(args, size_t *); + if (!ignore && pz) + *pz = (size_t)json_object_get_string_len(obj); + } + } + break; + case 'n': + if (!ignore && !json_object_is_type(obj, json_type_null)) + goto missfit; + break; + case 'b': + if (store) + pi = va_arg(args, int *); + + if (!ignore) { + if (!json_object_is_type(obj, json_type_boolean)) + goto missfit; + if (store && pi) + *pi = json_object_get_boolean(obj); + } + break; + case 'i': + if (store) + pi = va_arg(args, int *); + + if (!ignore) { + if (!json_object_is_type(obj, json_type_int)) + goto missfit; + if (store && pi) + *pi = json_object_get_int(obj); + } + break; + case 'I': + if (store) + pI = va_arg(args, int64_t *); + + if (!ignore) { + if (!json_object_is_type(obj, json_type_int)) + goto missfit; + if (store && pI) + *pI = json_object_get_int64(obj); + } + break; + case 'f': + case 'F': + if (store) + pf = va_arg(args, double *); + + if (!ignore) { + if (!(json_object_is_type(obj, json_type_double) || (c == 'F' && json_object_is_type(obj, json_type_int)))) + goto missfit; + if (store && pf) + *pf = json_object_get_double(obj); + } + break; + case 'o': + case 'O': + if (store) { + po = va_arg(args, struct json_object **); + if (!ignore && po) { + if (c == 'O') + obj = json_object_get(obj); + *po = obj; + } + } + break; + + case '[': + case '{': + if (!top) + top = stack; + else if (++top >= &stack[STACKCOUNT]) + goto too_deep; + + top->acc = acc; + top->type = xacc[0]; + top->index = 0; + top->parent = obj; + if (ignore) + ignore++; + if (c == '[') { + if (!ignore) { + if (!json_object_is_type(obj, json_type_array)) + goto missfit; + top->count = json_object_array_length(obj); + } + xacc[0] = ']'; + acc = unpack_accept_arr; + } else { + if (!ignore) { + if (!json_object_is_type(obj, json_type_object)) + goto missfit; + top->count = json_object_object_length(obj); + } + xacc[0] = '}'; + acc = unpack_accept_key; + continue; + } + break; + case '}': + case ']': + if (!top || c != xacc[0]) + goto internal_error; + acc = top->acc; + xacc[0] = top->type; + top = top == stack ? NULL : top - 1; + if (ignore) + ignore--; + break; + case '!': + if (*d != xacc[0]) + goto invalid_character; + if (!ignore && top->index != top->count) + goto incomplete; + /*@fallthrough@*/ + case '*': + acc = xacc; + continue; + default: + goto internal_error; + } + switch (xacc[0]) { + case 0: + if (top) + goto internal_error; + if (*d) + goto invalid_character; + return 0; + case ']': + if (!ignore) { + key = strchr(unpack_accept_arr, *d); + if (key && key >= unpack_accept_any) { + if (top->index >= top->count) + goto out_of_range; + obj = json_object_array_get_idx(top->parent, top->index++); + } + } + break; + case ':': + acc = unpack_accept_key; + xacc[0] = '}'; + if (ignore) + ignore--; + break; + default: + goto internal_error; + } + } +truncated: + rc = wrap_json_error_truncated; + goto error; +internal_error: + rc = wrap_json_error_internal_error; + goto error; +invalid_character: + rc = wrap_json_error_invalid_character; + goto error; +too_deep: + rc = wrap_json_error_too_deep; + goto error; +null_spec: + rc = wrap_json_error_null_spec; + goto error; +null_key: + rc = wrap_json_error_null_key; + goto error; +out_of_range: + rc = wrap_json_error_out_of_range; + goto error; +incomplete: + rc = wrap_json_error_incomplete; + goto error; +missfit: + rc = wrap_json_error_missfit_type; + goto errorfit; +key_not_found: + rc = wrap_json_error_key_not_found; + goto error; +errorfit: + d = fit; +error: + rc = rc | (int)((d - desc) << 4); + return -rc; +} + +int wrap_json_vcheck(struct json_object *object, const char *desc, va_list args) +{ + return vunpack(object, desc, args, 0); +} + +int wrap_json_check(struct json_object *object, const char *desc, ...) +{ + int rc; + va_list args; + + va_start(args, desc); + rc = vunpack(object, desc, args, 0); + va_end(args); + return rc; +} + +int wrap_json_vmatch(struct json_object *object, const char *desc, va_list args) +{ + return !vunpack(object, desc, args, 0); +} + +int wrap_json_match(struct json_object *object, const char *desc, ...) +{ + int rc; + va_list args; + + va_start(args, desc); + rc = vunpack(object, desc, args, 0); + va_end(args); + return !rc; +} + +int wrap_json_vunpack(struct json_object *object, const char *desc, va_list args) +{ + return vunpack(object, desc, args, 1); +} + +int wrap_json_unpack(struct json_object *object, const char *desc, ...) +{ + int rc; + va_list args; + + va_start(args, desc); + rc = vunpack(object, desc, args, 1); + va_end(args); + return rc; +} + +static void object_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure) +{ + struct json_object_iterator it = json_object_iter_begin(object); + struct json_object_iterator end = json_object_iter_end(object); + while (!json_object_iter_equal(&it, &end)) { + callback(closure, json_object_iter_peek_value(&it), json_object_iter_peek_name(&it)); + json_object_iter_next(&it); + } +} + +static void array_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure) +{ + int n = json_object_array_length(object); + int i = 0; + while(i < n) + callback(closure, json_object_array_get_idx(object, i++)); +} + +void wrap_json_optarray_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure) +{ + if (json_object_is_type(object, json_type_array)) + array_for_all(object, callback, closure); + else + callback(closure, object); +} + +void wrap_json_array_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure) +{ + if (json_object_is_type(object, json_type_array)) + array_for_all(object, callback, closure); +} + +void wrap_json_object_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure) +{ + if (json_object_is_type(object, json_type_object)) + object_for_all(object, callback, closure); +} + +void wrap_json_optobject_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure) +{ + if (json_object_is_type(object, json_type_object)) + object_for_all(object, callback, closure); + else + callback(closure, object, NULL); +} + +void wrap_json_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure) +{ + if (!object) + /* do nothing */; + else if (json_object_is_type(object, json_type_object)) + object_for_all(object, callback, closure); + else if (!json_object_is_type(object, json_type_array)) + callback(closure, object, NULL); + else { + int n = json_object_array_length(object); + int i = 0; + while(i < n) + callback(closure, json_object_array_get_idx(object, i++), NULL); + } +} + +#if defined(WRAP_JSON_TEST) +#include + +void p(const char *desc, ...) +{ + int rc; + va_list args; + struct json_object *result; + + va_start(args, desc); + rc = wrap_json_vpack(&result, desc, args); + va_end(args); + if (!rc) + printf(" SUCCESS %s\n\n", json_object_to_json_string(result)); + else + printf(" ERROR[char %d err %d] %s\n\n", wrap_json_get_error_position(rc), wrap_json_get_error_code(rc), wrap_json_get_error_string(rc)); + json_object_put(result); +} + +const char *xs[10]; +int *xi[10]; +int64_t *xI[10]; +double *xf[10]; +struct json_object *xo[10]; +size_t xz[10]; + +void u(const char *value, const char *desc, ...) +{ + unsigned m, k; + int rc; + va_list args; + struct json_object *obj, *o; + + memset(xs, 0, sizeof xs); + memset(xi, 0, sizeof xi); + memset(xI, 0, sizeof xI); + memset(xf, 0, sizeof xf); + memset(xo, 0, sizeof xo); + memset(xz, 0, sizeof xz); + obj = json_tokener_parse(value); + va_start(args, desc); + rc = wrap_json_vunpack(obj, desc, args); + va_end(args); + if (rc) + printf(" ERROR[char %d err %d] %s\n\n", wrap_json_get_error_position(rc), wrap_json_get_error_code(rc), wrap_json_get_error_string(rc)); + else { + value = NULL; + printf(" SUCCESS"); + va_start(args, desc); + k = m = 0; + while(*desc) { + switch(*desc) { + case '{': m = (m << 1) | 1; k = 1; break; + case '}': m = m >> 1; k = m&1; break; + case '[': m = m << 1; k = 0; break; + case ']': m = m >> 1; k = m&1; break; + case 's': printf(" s:%s", k ? va_arg(args, const char*) : *(va_arg(args, const char**)?:&value)); k ^= m&1; break; + case '%': printf(" %%:%zu", *va_arg(args, size_t*)); k = m&1; break; + case 'n': printf(" n"); k = m&1; break; + case 'b': printf(" b:%d", *va_arg(args, int*)); k = m&1; break; + case 'i': printf(" i:%d", *va_arg(args, int*)); k = m&1; break; + case 'I': printf(" I:%lld", *va_arg(args, int64_t*)); k = m&1; break; + case 'f': printf(" f:%f", *va_arg(args, double*)); k = m&1; break; + case 'F': printf(" F:%f", *va_arg(args, double*)); k = m&1; break; + case 'o': printf(" o:%s", json_object_to_json_string(*va_arg(args, struct json_object**))); k = m&1; break; + case 'O': o = *va_arg(args, struct json_object**); printf(" O:%s", json_object_to_json_string(o)); json_object_put(o); k = m&1; break; + default: break; + } + desc++; + } + va_end(args); + printf("\n\n"); + } + json_object_put(obj); +} + +#define P(...) do{ printf("pack(%s)\n",#__VA_ARGS__); p(__VA_ARGS__); } while(0) +#define U(...) do{ printf("unpack(%s)\n",#__VA_ARGS__); u(__VA_ARGS__); } while(0) + +int main() +{ + char buffer[4] = {'t', 'e', 's', 't'}; + + P("n"); + P("b", 1); + P("b", 0); + P("i", 1); + P("I", (uint64_t)0x123456789abcdef); + P("f", 3.14); + P("s", "test"); + P("s?", "test"); + P("s?", NULL); + P("s#", "test asdf", 4); + P("s%", "test asdf", (size_t)4); + P("s#", buffer, 4); + P("s%", buffer, (size_t)4); + P("s++", "te", "st", "ing"); + P("s#+#+", "test", 1, "test", 2, "test"); + P("s%+%+", "test", (size_t)1, "test", (size_t)2, "test"); + P("{}", 1.0); + P("[]", 1.0); + P("o", json_object_new_int(1)); + P("o?", json_object_new_int(1)); + P("o?", NULL); + P("O", json_object_new_int(1)); + P("O?", json_object_new_int(1)); + P("O?", NULL); + P("{s:[]}", "foo"); + P("{s+#+: []}", "foo", "barbar", 3, "baz"); + P("{s:s,s:o,s:O}", "a", NULL, "b", NULL, "c", NULL); + P("{s:**}", "a", NULL); + P("{s:s*,s:o*,s:O*}", "a", NULL, "b", NULL, "c", NULL); + P("[i,i,i]", 0, 1, 2); + P("[s,o,O]", NULL, NULL, NULL); + P("[**]", NULL); + P("[s*,o*,O*]", NULL, NULL, NULL); + P(" s ", "test"); + P("[ ]"); + P("[ i , i, i ] ", 1, 2, 3); + P("{\n\n1"); + P("[}"); + P("{]"); + P("["); + P("{"); + P("[i]a", 42); + P("ia", 42); + P("s", NULL); + P("+", NULL); + P(NULL); + P("{s:i}", NULL, 1); + P("{ {}: s }", "foo"); + P("{ s: {}, s:[ii{} }", "foo", "bar", 12, 13); + P("[[[[[ [[[[[ [[[[ }]]]] ]]]] ]]]]]"); + + U("true", "b", &xi[0]); + U("false", "b", &xi[0]); + U("null", "n"); + U("42", "i", &xi[0]); + U("123456789", "I", &xI[0]); + U("3.14", "f", &xf[0]); + U("12345", "F", &xf[0]); + U("3.14", "F", &xf[0]); + U("\"foo\"", "s", &xs[0]); + U("\"foo\"", "s%", &xs[0], &xz[0]); + U("{}", "{}"); + U("[]", "[]"); + U("{}", "o", &xo[0]); + U("{}", "O", &xo[0]); + U("{\"foo\":42}", "{si}", "foo", &xi[0]); + U("[1,2,3]", "[i,i,i]", &xi[0], &xi[1], &xi[2]); + U("{\"a\":1,\"b\":2,\"c\":3}", "{s:i, s:i, s:i}", "a", &xi[0], "b", &xi[1], "c", &xi[2]); + U("42", "z"); + U("null", "[i]"); + U("[]", "[}"); + U("{}", "{]"); + U("[]", "["); + U("{}", "{"); + U("[42]", "[i]a", &xi[0]); + U("42", "ia", &xi[0]); + U("[]", NULL); + U("\"foo\"", "s", NULL); + U("42", "s", NULL); + U("42", "n"); + U("42", "b", NULL); + U("42", "f", NULL); + U("42", "[i]", NULL); + U("42", "{si}", "foo", NULL); + U("\"foo\"", "n"); + U("\"foo\"", "b", NULL); + U("\"foo\"", "i", NULL); + U("\"foo\"", "I", NULL); + U("\"foo\"", "f", NULL); + U("\"foo\"", "F", NULL); + U("true", "s", NULL); + U("true", "n"); + U("true", "i", NULL); + U("true", "I", NULL); + U("true", "f", NULL); + U("true", "F", NULL); + U("[42]", "[ii]", &xi[0], &xi[1]); + U("{\"foo\":42}", "{si}", NULL, &xi[0]); + U("{\"foo\":42}", "{si}", "baz", &xi[0]); + U("[1,2,3]", "[iii!]", &xi[0], &xi[1], &xi[2]); + U("[1,2,3]", "[ii!]", &xi[0], &xi[1]); + U("[1,2,3]", "[ii]", &xi[0], &xi[1]); + U("[1,2,3]", "[ii*]", &xi[0], &xi[1]); + U("{\"foo\":42,\"baz\":45}", "{sisi}", "baz", &xi[0], "foo", &xi[1]); + U("{\"foo\":42,\"baz\":45}", "{sisi*}", "baz", &xi[0], "foo", &xi[1]); + U("{\"foo\":42,\"baz\":45}", "{sisi!}", "baz", &xi[0], "foo", &xi[1]); + U("{\"foo\":42,\"baz\":45}", "{si}", "baz", &xi[0], "foo", &xi[1]); + U("{\"foo\":42,\"baz\":45}", "{si*}", "baz", &xi[0], "foo", &xi[1]); + U("{\"foo\":42,\"baz\":45}", "{si!}", "baz", &xi[0], "foo", &xi[1]); + U("[1,{\"foo\":2,\"bar\":null},[3,4]]", "[i{sisn}[ii]]", &xi[0], "foo", &xi[1], "bar", &xi[2], &xi[3]); + U("[1,2,3]", "[ii!i]", &xi[0], &xi[1], &xi[2]); + U("[1,2,3]", "[ii*i]", &xi[0], &xi[1], &xi[2]); + U("{\"foo\":1,\"bar\":2}", "{si!si}", "foo", &xi[1], "bar", &xi[2]); + U("{\"foo\":1,\"bar\":2}", "{si*si}", "foo", &xi[1], "bar", &xi[2]); + U("{\"foo\":{\"baz\":null,\"bar\":null}}", "{s{sn!}}", "foo", "bar"); + U("[[1,2,3]]", "[[ii!]]", &xi[0], &xi[1]); + U("{}", "{s?i}", "foo", &xi[0]); + U("{\"foo\":1}", "{s?i}", "foo", &xi[0]); + U("{}", "{s?[ii]s?{s{si!}}}", "foo", &xi[0], &xi[1], "bar", "baz", "quux", &xi[2]); + U("{\"foo\":[1,2]}", "{s?[ii]s?{s{si!}}}", "foo", &xi[0], &xi[1], "bar", "baz", "quux", &xi[2]); + U("{\"bar\":{\"baz\":{\"quux\":15}}}", "{s?[ii]s?{s{si!}}}", "foo", &xi[0], &xi[1], "bar", "baz", "quux", &xi[2]); + U("{\"foo\":{\"bar\":4}}", "{s?{s?i}}", "foo", "bar", &xi[0]); + U("{\"foo\":{}}", "{s?{s?i}}", "foo", "bar", &xi[0]); + U("{}", "{s?{s?i}}", "foo", "bar", &xi[0]); + U("{\"foo\":42,\"baz\":45}", "{s?isi!}", "baz", &xi[0], "foo", &xi[1]); + U("{\"foo\":42}", "{s?isi!}", "baz", &xi[0], "foo", &xi[1]); + return 0; +} + +#endif + +#if 0 + + + /* Unpack the same item twice */ + j = json_pack("{s:s, s:i, s:b}", "foo", "bar", "baz", 42, "quux", 1); + if(!json_unpack_ex(j, &error, 0, "{s:s,s:s!}", "foo", &s, "foo", &s)) + fail("json_unpack object with strict validation failed"); + { + const char *possible_errors[] = { + "2 object item(s) left unpacked: baz, quux", + "2 object item(s) left unpacked: quux, baz" + }; + check_errors(possible_errors, 2, "", 1, 10, 10); + } + json_decref(j); + +#endif + \ No newline at end of file diff --git a/HAL-afb/HAL_MOST_UNICENS/wrap-json.h b/HAL-afb/HAL_MOST_UNICENS/wrap-json.h new file mode 100644 index 0000000..b5eadf5 --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/wrap-json.h @@ -0,0 +1,47 @@ +/* + Copyright (C) 2016, 2017 "IoT.bzh" + + author: José Bollo + + 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. +*/ + +#pragma once + +#include +#include + +extern int wrap_json_get_error_position(int rc); +extern int wrap_json_get_error_code(int rc); +extern const char *wrap_json_get_error_string(int rc); + +extern int wrap_json_vpack(struct json_object **result, const char *desc, va_list args); +extern int wrap_json_pack(struct json_object **result, const char *desc, ...); + +extern int wrap_json_vunpack(struct json_object *object, const char *desc, va_list args); +extern int wrap_json_unpack(struct json_object *object, const char *desc, ...); +extern int wrap_json_vcheck(struct json_object *object, const char *desc, va_list args); +extern int wrap_json_check(struct json_object *object, const char *desc, ...); +extern int wrap_json_vmatch(struct json_object *object, const char *desc, va_list args); +extern int wrap_json_match(struct json_object *object, const char *desc, ...); + +extern void wrap_json_optarray_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure); +extern void wrap_json_array_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure); + +extern void wrap_json_optarray_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure); +extern void wrap_json_array_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure); +extern void wrap_json_object_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure); +extern void wrap_json_optobject_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure); +extern void wrap_json_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure); + + \ No newline at end of file diff --git a/HAL-afb/HAL_MOST_UNICENS/wrap-json.md b/HAL-afb/HAL_MOST_UNICENS/wrap-json.md new file mode 100644 index 0000000..3718b66 --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/wrap-json.md @@ -0,0 +1,306 @@ +WRAP-JSON facility +================== + +The facility wrap-json is based on the pack/unpack API on the +libray jansson. The two chapters below are copied from the +documentation of jansson library copyrighted by Petri Lehtinen +(see at end). + +Building Values +--------------- + +This section describes functions that help to create, or *pack*, complex +JSON values, especially nested objects and arrays. Value building is +based on a *format string* that is used to tell the functions about the +expected arguments. + +For example, the format string `"i"` specifies a single integer value, +while the format string `"[ssb]"` or the equivalent `"[s, s, b]"` +specifies an array value with two strings and a boolean as its items: + + /* Create the JSON integer 42 */ + wrap_json_pack(&result, "i", 42); + + /* Create the JSON array ["foo", "bar", true] */ + wrap_json_pack(&result, "[ssb]", "foo", "bar", 1); + +Here's the full list of format specifiers. The type in parentheses +denotes the resulting JSON type, and the type in brackets (if any) +denotes the C type that is expected as the corresponding argument or +arguments. + +`s` (string) \[const char \*\] + +: Convert a null terminated UTF-8 string to a JSON string. + +`s?` (string) \[const char \*\] + +: Like `s`, but if the argument is *NULL*, output a JSON null value. + +`s*` (string) \[const char \*\] + +: Like `s`, but if the argument is *NULL*, do not output any value. + This format can only be used inside an object or an array. If used + inside an object, the corresponding key is additionally suppressed + when the value is omitted. See below for an example. + +`s#` (string) \[const char \*, int\] + +: Convert a UTF-8 buffer of a given length to a JSON string. + +`s%` (string) \[const char \*, size\_t\] + +: Like `s#` but the length argument is of type size\_t. + +`+` \[const char \*\] + +: Like `s`, but concatenate to the previous string. Only valid after + `s`, `s#`, `+` or `+#`. + +`+#` \[const char \*, int\] + +: Like `s#`, but concatenate to the previous string. Only valid after + `s`, `s#`, `+` or `+#`. + +`+%` (string) \[const char \*, size\_t\] + +: Like `+#` but the length argument is of type size\_t. + +`n` (null) + +: Output a JSON null value. No argument is consumed. + +`b` (boolean) \[int\] + +: Convert a C int to JSON boolean value. Zero is converted to `false` + and non-zero to `true`. + +`i` (integer) \[int\] + +: Convert a C int to JSON integer. + +`I` (integer) \[json\_int\_t\] + +: Convert a C json\_int\_t to JSON integer. + +`f` (real) \[double\] + +: Convert a C double to JSON real. + +`o` (any value) \[json\_t \*\] + +: Output any given JSON value as-is. If the value is added to an array + or object, the reference to the value passed to `o` is stolen by the + container. + +`O` (any value) \[json\_t \*\] + +: Like `o`, but the argument's reference count is incremented. This is + useful if you pack into an array or object and want to keep the + reference for the JSON value consumed by `O` to yourself. + +`o?`, `O?` (any value) \[json\_t \*\] + +: Like `o` and `O`, respectively, but if the argument is *NULL*, + output a JSON null value. + +`o*`, `O*` (any value) \[json\_t \*\] + +: Like `o` and `O`, respectively, but if the argument is *NULL*, do + not output any value. This format can only be used inside an object + or an array. If used inside an object, the corresponding key is + additionally suppressed. See below for an example. + +`[fmt]` (array) + +: Build an array with contents from the inner format string. `fmt` may + contain objects and arrays, i.e. recursive value building is + supported. + +`{fmt}` (object) + +: Build an object with contents from the inner format string `fmt`. + The first, third, etc. format specifier represent a key, and must be + a string (see `s`, `s#`, `+` and `+#` above), as object keys are + always strings. The second, fourth, etc. format specifier represent + a value. Any value may be an object or array, i.e. recursive value + building is supported. + +Whitespace, `:` and `,` are ignored. + +More examples: + + /* Build an empty JSON object */ + wrap_json_pack(&result, "{}"); + + /* Build the JSON object {"foo": 42, "bar": 7} */ + wrap_json_pack(&result, "{sisi}", "foo", 42, "bar", 7); + + /* Like above, ':', ',' and whitespace are ignored */ + wrap_json_pack(&result, "{s:i, s:i}", "foo", 42, "bar", 7); + + /* Build the JSON array [[1, 2], {"cool": true}] */ + wrap_json_pack(&result, "[[i,i],{s:b}]", 1, 2, "cool", 1); + + /* Build a string from a non-null terminated buffer */ + char buffer[4] = {'t', 'e', 's', 't'}; + wrap_json_pack(&result, "s#", buffer, 4); + + /* Concatenate strings together to build the JSON string "foobarbaz" */ + wrap_json_pack(&result, "s++", "foo", "bar", "baz"); + + /* Create an empty object or array when optional members are missing */ + wrap_json_pack(&result, "{s:s*,s:o*,s:O*}", "foo", NULL, "bar", NULL, "baz", NULL); + wrap_json_pack(&result, "[s*,o*,O*]", NULL, NULL, NULL); + +Parsing and Validating Values +----------------------------- + +This section describes functions that help to validate complex values +and extract, or *unpack*, data from them. Like building values +<apiref-pack>, this is also based on format strings. + +While a JSON value is unpacked, the type specified in the format string +is checked to match that of the JSON value. This is the validation part +of the process. In addition to this, the unpacking functions can also +check that all items of arrays and objects are unpacked. This check be +enabled with the format specifier `!` or by using the flag +`JSON_STRICT`. See below for details. + +Here's the full list of format specifiers. The type in parentheses +denotes the JSON type, and the type in brackets (if any) denotes the C +type whose address should be passed. + +`s` (string) \[const char \*\] + +: Convert a JSON string to a pointer to a null terminated UTF-8 + string. The resulting string is extracted by using + json\_string\_value() internally, so it exists as long as there are + still references to the corresponding JSON string. + +`s%` (string) \[const char \*, size\_t \*\] + +: Convert a JSON string to a pointer to a null terminated UTF-8 string + and its length. + +`n` (null) + +: Expect a JSON null value. Nothing is extracted. + +`b` (boolean) \[int\] + +: Convert a JSON boolean value to a C int, so that `true` is converted + to 1 and `false` to 0. + +`i` (integer) \[int\] + +: Convert a JSON integer to C int. + +`I` (integer) \[json\_int\_t\] + +: Convert a JSON integer to C json\_int\_t. + +`f` (real) \[double\] + +: Convert a JSON real to C double. + +`F` (integer or real) \[double\] + +: Convert a JSON number (integer or real) to C double. + +`o` (any value) \[json\_t \*\] + +: Store a JSON value with no conversion to a json\_t pointer. + +`O` (any value) \[json\_t \*\] + +: Like `O`, but the JSON value's reference count is incremented. + +`[fmt]` (array) + +: Convert each item in the JSON array according to the inner format + string. `fmt` may contain objects and arrays, i.e. recursive value + extraction is supported. + +`{fmt}` (object) + +: Convert each item in the JSON object according to the inner format + string `fmt`. The first, third, etc. format specifier represent a + key, and must be `s`. The corresponding argument to unpack functions + is read as the object key. The second fourth, etc. format specifier + represent a value and is written to the address given as the + corresponding argument. **Note** that every other argument is read + from and every other is written to. + + `fmt` may contain objects and arrays as values, i.e. recursive value + extraction is supported. + +`!` + +: This special format specifier is used to enable the check that all + object and array items are accessed, on a per-value basis. It must + appear inside an array or object as the last format specifier before + the closing bracket or brace. + +`*` + +: This special format specifier is the opposite of `!`. This is the default. + It must appear inside an array or object as the last format specifier + before the closing bracket or brace. + +Whitespace, `:` and `,` are ignored. + +Examples: + + /* root is the JSON integer 42 */ + int myint; + wrap_json_unpack(root, "i", &myint); + assert(myint == 42); + + /* root is the JSON object {"foo": "bar", "quux": true} */ + const char *str; + int boolean; + wrap_json_unpack(root, "{s:s, s:b}", "foo", &str, "quux", &boolean); + assert(strcmp(str, "bar") == 0 && boolean == 1); + + /* root is the JSON array [[1, 2], {"baz": null} */ + wrap_json_check(root, "[[i,i], {s:n}]", "baz"); + /* returns 0 for validation success, nothing is extracted */ + + /* root is the JSON array [1, 2, 3, 4, 5] */ + int myint1, myint2; + wrap_json_unpack(root, "[ii!]", &myint1, &myint2); + /* returns -1 for failed validation */ + + /* root is an empty JSON object */ + int myint = 0, myint2 = 0, myint3 = 0; + wrap_json_unpack(root, "{s?i, s?[ii]}", + "foo", &myint1, + "bar", &myint2, &myint3); + /* myint1, myint2 or myint3 is no touched as "foo" and "bar" don't exist */ + + +Copyright +--------- + +Copyright (c) 2009-2016 Petri Lehtinen + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + + \ No newline at end of file diff --git a/HAL-afb/HAL_MOST_UNICENS/wrap_unicens.c b/HAL-afb/HAL_MOST_UNICENS/wrap_unicens.c new file mode 100644 index 0000000..0cac86b --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/wrap_unicens.c @@ -0,0 +1,256 @@ +/* + * Copyright (C) 2017, Microchip Technology Inc. and its subsidiaries. + * Author Tobias Jahnke + * + * 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 +#define AFB_BINDING_VERSION 2 + +#include +#include +#include + +#include "wrap_unicens.h" +#include "wrap-json.h" + +typedef struct async_job_ { + wrap_ucs_result_cb_t result_fptr; + void *result_user_ptr; +} async_job_t; + +/* Subscribes to unicens2-binding events. + * \return Returns 0 if successful, otherwise != 0". + */ +extern int wrap_ucs_subscribe_sync() { + + json_object *j_response, *j_query = NULL; + int err; + + /* Build an empty JSON object */ + err = wrap_json_pack(&j_query, "{}"); + if (err) { + AFB_ERROR("Failed to create subscribe json object"); + goto OnErrorExit; + } + + err = afb_service_call_sync("UNICENS", "subscribe", j_query, &j_response); + if (err) { + AFB_ERROR("Fail subscribing to UNICENS events"); + goto OnErrorExit; + } + else { + AFB_NOTICE("Subscribed to UNICENS events, res=%s", json_object_to_json_string(j_response)); + json_object_put(j_response); + } + json_object_put(j_query); + j_query = NULL; + +OnErrorExit: + if (j_query) + json_object_put(j_query); + return err; +} + +/* Retrieves XML configuration path to initialize unicens2-binding. + * \param config_path Directory containing XML configuration file(s). + * \return Returns 0 if successful, otherwise != 0". + */ +extern int wrap_ucs_getconfig_sync(const char *config_path) { + json_object *j_response, *j_query = NULL; + int err; + + /* Build JSON object to retrieve UNICENS configuration */ + if (config_path != NULL) + err = wrap_json_pack(&j_query, "{s:s}", "cfgpath", config_path); + else + err = wrap_json_pack(&j_query, "{}"); + + if (err) { + AFB_ERROR("Failed to create listconfig json object"); + goto OnErrorExit; + } + + err = afb_service_call_sync("UNICENS", "listconfig", j_query, &j_response); + if (err) { + AFB_ERROR("Failed to call listconfig"); + goto OnErrorExit; + } + else { + AFB_NOTICE("UNICENS listconfig result, res=%s", json_object_to_json_string(j_response)); + json_object_put(j_response); + } + json_object_put(j_query); + j_query = NULL; + +OnErrorExit: + if (j_query) + json_object_put(j_query); + return err; +} + + +/* Initializes the unicens2-binding. + * \param file_name Path to XML configuration file or \c NULL for + * first found file in default path. + * \return Returns 0 if successful, otherwise != 0". + */ +extern int wrap_ucs_initialize_sync(const char *file_name) { + json_object *j_response, *j_query = NULL; + int err; + + /* Build JSON object to initialize UNICENS */ + if (file_name != NULL) + err = wrap_json_pack(&j_query, "{s:s}", "filename", file_name); + else + err = wrap_json_pack(&j_query, "{}"); + + if (err) { + AFB_ERROR("Failed to create initialize json object"); + goto OnErrorExit; + } + err = afb_service_call_sync("UNICENS", "initialise", j_query, &j_response); + if (err) { + AFB_ERROR("Failed to initialize UNICENS"); + goto OnErrorExit; + } + else { + AFB_NOTICE("Initialized UNICENS, res=%s", json_object_to_json_string(j_response)); + json_object_put(j_response); + } + + json_object_put(j_query); + j_query = NULL; + +OnErrorExit: + if (j_query) + json_object_put(j_query); + return err; +} + +/* Write I2C command to a network node. + * \param node Node address + * \param data_ptr Reference to command data + * \param data_sz Size of the command data. Valid values: 1..32. + * \return Returns 0 if successful, otherwise != 0". + */ +extern int wrap_ucs_i2cwrite_sync(uint16_t node, uint8_t *data_ptr, uint8_t data_sz) { + + json_object *j_response, *j_query, *j_array = NULL; + int err; + uint8_t cnt; + + j_query = json_object_new_object(); + j_array = json_object_new_array(); + + if (!j_query || !j_array) { + err = -1; + AFB_ERROR("Failed to create writei2c json objects"); + goto OnErrorExit; + } + + for (cnt = 0U; cnt < data_sz; cnt++) { + json_object_array_add(j_array, json_object_new_int(data_ptr[cnt])); + } + + json_object_object_add(j_query, "node", json_object_new_int(node)); + json_object_object_add(j_query, "data", j_array); + + err = afb_service_call_sync("UNICENS", "writei2c", j_query, &j_response); + + if (err) { + AFB_ERROR("Failed to call writei2c"); + goto OnErrorExit; + } + else { + AFB_NOTICE("Called writei2c, res=%s", "async"/*json_object_to_json_string(j_response)*/); + json_object_put(j_response); + } + + json_object_put(j_query); + j_query = NULL; + +OnErrorExit: + if (j_query) + json_object_put(j_query); + return err; +} + +/* ------------------ ASYNCHRONOUS API ---------------------------------------*/ + +static void wrap_ucs_i2cwrite_cb(void *closure, int status, struct json_object *j_result) { + + AFB_DEBUG("wrap_ucs_i2cwrite_cb: closure=%p status=%d, res=%s", closure, status, json_object_to_json_string(j_result)); + + if (closure) { + async_job_t *job_ptr = (async_job_t *)closure; + + if (job_ptr->result_fptr) + job_ptr->result_fptr(0U, job_ptr->result_user_ptr); + + free(closure); + } +} + +/* Write I2C command to a network node. + * \param node Node address + * \param data_ptr Reference to command data + * \param data_sz Size of the command data. Valid values: 1..32. + * \return Returns 0 if successful, otherwise != 0". + */ +extern int wrap_ucs_i2cwrite(uint16_t node, uint8_t *data_ptr, uint8_t data_sz, + wrap_ucs_result_cb_t result_fptr, + void *result_user_ptr) { + + json_object *j_query, *j_array = NULL; + async_job_t *job_ptr = NULL; + int err; + uint8_t cnt; + + j_query = json_object_new_object(); + j_array = json_object_new_array(); + + if (!j_query || !j_array) { + err = -1; + AFB_ERROR("Failed to create writei2c json objects"); + goto OnErrorExit; + } + + for (cnt = 0U; cnt < data_sz; cnt++) { + json_object_array_add(j_array, json_object_new_int(data_ptr[cnt])); + } + + json_object_object_add(j_query, "node", json_object_new_int(node)); + json_object_object_add(j_query, "data", j_array); + + job_ptr = malloc(sizeof(async_job_t)); + + if (!job_ptr) { + err = -1; + AFB_ERROR("Failed to create async job object"); + goto OnErrorExit; + } + + job_ptr->result_fptr = result_fptr; + job_ptr->result_user_ptr = result_user_ptr; + + afb_service_call("UNICENS", "writei2c", j_query, wrap_ucs_i2cwrite_cb, job_ptr); + err = 0; + j_query = NULL; + +OnErrorExit: + if (j_query) + json_object_put(j_query); + return err; +} diff --git a/HAL-afb/HAL_MOST_UNICENS/wrap_unicens.h b/HAL-afb/HAL_MOST_UNICENS/wrap_unicens.h new file mode 100644 index 0000000..268fb74 --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/wrap_unicens.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2017, Microchip Technology Inc. and its subsidiaries. + * Author Tobias Jahnke + * + * 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. + * + */ + +#pragma once +#include + +/* Asynchronous API: result callback */ +typedef void (*wrap_ucs_result_cb_t)(uint8_t result, void *user_ptr); + +/* Asynchronous API: functions */ +extern int wrap_ucs_i2cwrite(uint16_t node, uint8_t *data_ptr, uint8_t data_sz, + wrap_ucs_result_cb_t result_fptr, + void *result_user_ptr); + +/* Synchronous API: functions */ +extern int wrap_ucs_subscribe_sync(); +extern int wrap_ucs_getconfig_sync(const char *config_path); +extern int wrap_ucs_initialize_sync(const char* file_name); +extern int wrap_ucs_i2cwrite_sync(uint16_t node, uint8_t *data_ptr, uint8_t data_sz); diff --git a/HAL-afb/HAL_MOST_UNICENS/wrap_volume.c b/HAL-afb/HAL_MOST_UNICENS/wrap_volume.c new file mode 100644 index 0000000..cf70647 --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/wrap_volume.c @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2017, Microchip Technology Inc. and its subsidiaries. + * Author Tobias Jahnke + * + * 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 +#define AFB_BINDING_VERSION 2 + +#include +#include +#include +#include +#include + +#include "wrap_volume.h" +#include "wrap_unicens.h" +#include "libmostvolume.h" + +static int wrap_volume_service_timeout_cb(sd_event_source* source, + uint64_t timer __attribute__((__unused__)), + void *userdata __attribute__((__unused__))) { + + uint8_t ret; + + sd_event_source_unref(source); + ret = lib_most_volume_service(); + + if (ret != 0U) { + AFB_ERROR("lib_most_volume_service returns %d", ret); + } + + return 0; +} + +static void wrap_volume_service_cb(uint16_t timeout) { + uint64_t usec; + sd_event_now(afb_daemon_get_event_loop(), CLOCK_BOOTTIME, &usec); + sd_event_add_time( afb_daemon_get_event_loop(), NULL, CLOCK_MONOTONIC, + usec + (timeout*1000), + 250, + wrap_volume_service_timeout_cb, + NULL); +} + +/* Retrieves a new value adapted to a new maximum value. Minimum value is + * always zero. */ +static int wrap_volume_calculate(int value, int max_old, int max_new) { + + if (value > max_old) + value = max_old; + + value = (value * max_new) / max_old; /* calculate range: 0..255 */ + assert(value <= max_new); + + return value; +} + +extern int wrap_volume_init(void) { + + uint8_t ret = 0U; + lib_most_volume_init_t mv_init; + + mv_init.writei2c_cb = &wrap_ucs_i2cwrite; + mv_init.service_cb = wrap_volume_service_cb; + + ret = lib_most_volume_init(&mv_init); + + return ret * (-1); +} + +extern int wrap_volume_master(int volume) { + + int new_value, ret; + + new_value = wrap_volume_calculate(volume, 100, 255); + ret = lib_most_volume_set(LIB_MOST_VOLUME_MASTER, (uint8_t)new_value); + + if (ret != 0) { + AFB_ERROR("wrap_volume_master: volume library not ready."); + ret = ret * (-1); /* make return value negative */ + } + + return ret; +} + +extern int wrap_volume_pcm(int *volume_ptr, int volume_sz) { + + const int MAX_PCM_CHANNELS = 6; + int cnt, ret; + assert(volume_ptr != NULL); + assert(volume_sz <= MAX_PCM_CHANNELS); + + for (cnt = 0; cnt < volume_sz; cnt ++) { + + int new_value = wrap_volume_calculate(volume_ptr[cnt], 100, 255); + ret = lib_most_volume_set((enum lib_most_volume_channel_t)cnt, (uint8_t)new_value); + + if (ret != 0) { + AFB_ERROR("wrap_volume_pcm: volume library not ready."); + ret = ret * (-1); /* make return value negative */ + break; + } + } + + return ret; +} diff --git a/HAL-afb/HAL_MOST_UNICENS/wrap_volume.h b/HAL-afb/HAL_MOST_UNICENS/wrap_volume.h new file mode 100644 index 0000000..3378540 --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/wrap_volume.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2017, Microchip Technology Inc. and its subsidiaries. + * Author Tobias Jahnke + * + * 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. + * + */ + +#pragma once +#include +#include + +extern int wrap_volume_init(void); +extern int wrap_volume_master(int volume); +extern int wrap_volume_pcm(int *volume_ptr, int volume_sz); diff --git a/HAL-afb/hal-most-unicens/CMakeLists.txt b/HAL-afb/hal-most-unicens/CMakeLists.txt deleted file mode 100644 index e532191..0000000 --- a/HAL-afb/hal-most-unicens/CMakeLists.txt +++ /dev/null @@ -1,49 +0,0 @@ -########################################################################### -# Copyright 2015, 2016, 2017 IoT.bzh -# -# author: Fulup Ar Foll -# -# 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. -########################################################################### - -ADD_SUBDIRECTORY(ucs2-vol) - -# Add target to project dependency list -PROJECT_TARGET_ADD(hal-most-unicens) - - # Define project Targets - ADD_LIBRARY(hal-most-unicens MODULE hal_most_unicens.c wrap-json.c wrap_unicens.c wrap_volume.c) - - # Binder exposes a unique public entry point - SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES - PREFIX "afb-" - LABELS "BINDING" - LINK_FLAGS ${BINDINGS_LINK_FLAG} - OUTPUT_NAME ${TARGET_NAME} - ) - - # Library dependencies (include updates automatically) - TARGET_LINK_LIBRARIES(${TARGET_NAME} - hal-interface - audio-interface - ucs2-volume - ) - - # installation directory - INSTALL(TARGETS ${TARGET_NAME} - LIBRARY DESTINATION ${BINDINGS_INSTALL_DIR}) - - # Define properties to expose when others use this target - TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME} - PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/ucs2-vol/inc - ) diff --git a/HAL-afb/hal-most-unicens/hal_most_unicens.c b/HAL-afb/hal-most-unicens/hal_most_unicens.c deleted file mode 100644 index a5971fb..0000000 --- a/HAL-afb/hal-most-unicens/hal_most_unicens.c +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright (C) 2017, Microchip Technology Inc. and its subsidiaries. - * Author Tobias Jahnke - * - * 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 "hal-interface.h" -#include "audio-interface.h" -#include "wrap-json.h" -#include "string.h" -#include "wrap_unicens.h" -#include "wrap_volume.h" - -#define PCM_MAX_CHANNELS 6 - -static int master_volume; -static json_bool master_switch; -static int pcm_volume[PCM_MAX_CHANNELS]; - -void unicens_master_vol_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { - - const char *j_str = json_object_to_json_string(j_obj); - - if (wrap_json_unpack(j_obj, "[i!]", &master_volume) == 0) { - AFB_NOTICE("master_volume: %s, value=%d", j_str, master_volume); - wrap_volume_master(master_volume); - } - else { - AFB_NOTICE("master_volume: INVALID STRING %s", j_str); - } -} - -void unicens_master_switch_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { - - const char *j_str = json_object_to_json_string(j_obj); - - if (wrap_json_unpack(j_obj, "[b!]", &master_switch) == 0) { - AFB_NOTICE("master_switch: %s, value=%d", j_str, master_switch); - } - else { - AFB_NOTICE("master_switch: INVALID STRING %s", j_str); - } -} - -void unicens_pcm_vol_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { - - const char *j_str = json_object_to_json_string(j_obj); - - if (wrap_json_unpack(j_obj, "[iiiiii!]", &pcm_volume[0], &pcm_volume[1], &pcm_volume[2], &pcm_volume[3], - &pcm_volume[4], &pcm_volume[5]) == 0) { - AFB_NOTICE("pcm_vol: %s", j_str); - wrap_volume_pcm(pcm_volume, PCM_MAX_CHANNELS/*array size*/); - } - else { - AFB_NOTICE("pcm_vol: INVALID STRING %s", j_str); - } -} - -/* declare ALSA mixer controls */ -STATIC alsaHalMapT alsaHalMap[]= { - { .tag=Master_Playback_Volume, .cb={.callback=unicens_master_vol_cb, .handle=&master_volume}, .info="Sets master playback volume", - .ctl={.numid=1, .type=SND_CTL_ELEM_TYPE_INTEGER, .count=1, .minval=0, .maxval=100, .step=1, .value=50, .name="Master Playback Volume"} - }, - /*{ .tag=Master_OnOff_Switch, .cb={.callback=unicens_master_switch_cb, .handle=&master_switch}, .info="Sets master playback switch", - .ctl={.numid=2, .type=SND_CTL_ELEM_TYPE_BOOLEAN, .count=1, .minval=0, .maxval=1, .step=1, .value=1, .name="Master Playback Switch"} - },*/ - { .tag=PCM_Playback_Volume, .cb={.callback=unicens_pcm_vol_cb, .handle=&pcm_volume}, .info="Sets PCM playback volume", - .ctl={.numid=3, .type=SND_CTL_ELEM_TYPE_INTEGER, .count=6, .minval=0, .maxval=100, .step=1, .value=100, .name="PCM Playback Volume"} - }, - { .tag=EndHalCrlTag} /* marker for end of the array */ -} ; - -/* HAL sound card mapping info */ -STATIC alsaHalSndCardT alsaHalSndCard = { - .name = "Microchip MOST:1", /* WARNING: name MUST match with 'aplay -l' */ - .info = "HAL for MICROCHIP MOST sound card controlled by UNICENS binding", - .ctls = alsaHalMap, - .volumeCB = NULL, /* use default volume normalization function */ -}; - -/* initializes ALSA sound card, UNICENS API */ -STATIC int unicens_service_init() { - int err = 0; - AFB_NOTICE("Initializing HAL-MOST-UNICENS-BINDING"); - - err = halServiceInit(afbBindingV2.api, &alsaHalSndCard); - if (err) { - AFB_ERROR("Cannot initialize ALSA soundcard."); - goto OnErrorExit; - } - - err= afb_daemon_require_api("UNICENS", 1); - if (err) { - AFB_ERROR("Failed to access UNICENS API"); - goto OnErrorExit; - } - - err = wrap_ucs_subscribe_sync(); - if (err) { - AFB_ERROR("Failed to subscribe to UNICENS binding"); - goto OnErrorExit; - } - - err = wrap_ucs_initialize_sync("/home/agluser/DEVELOPMENT/AGL/BINDING/unicens2-binding/data/config_multichannel_audio_kit.xml"); - if (err) { - AFB_ERROR("Failed to initialize UNICENS binding"); - goto OnErrorExit; - } - - err = wrap_volume_init(); - if (err) { - AFB_ERROR("Failed to initialize wrapper for volume library"); - goto OnErrorExit; - } - -OnErrorExit: - AFB_NOTICE("Initializing HAL-MOST-UNICENS-BINDING done.."); - return err; -} - -// This receive all event this binding subscribe to -PUBLIC void unicens_event_cb(const char *evtname, json_object *j_event) { - - if (strncmp(evtname, "alsacore/", 9) == 0) { - halServiceEvent(evtname, j_event); - return; - } - - if (strncmp(evtname, "UNICENS/", 8) == 0) { - AFB_NOTICE("unicens_event_cb: evtname=%s [msg=%s]", evtname, json_object_get_string(j_event)); - return; - } - - AFB_NOTICE("unicens_event_cb: UNHANDLED EVENT, evtname=%s [msg=%s]", evtname, json_object_get_string(j_event)); -} - -/* API prefix should be unique for each snd card */ -PUBLIC const struct afb_binding_v2 afbBindingV2 = { - .api = "hal-most-unicens", - .init = unicens_service_init, - .verbs = halServiceApi, - .onevent = unicens_event_cb, -}; diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/CMakeLists.txt b/HAL-afb/hal-most-unicens/ucs2-vol/CMakeLists.txt deleted file mode 100644 index 8735677..0000000 --- a/HAL-afb/hal-most-unicens/ucs2-vol/CMakeLists.txt +++ /dev/null @@ -1,26 +0,0 @@ -########################################################################### -# Copyright 2015, 2016, 2017 IoT.bzh -# -# author: Fulup Ar Foll -# -# 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 subproject targets -ADD_SUBDIRECTORY(src) - - - - diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/inc/device_container.h b/HAL-afb/hal-most-unicens/ucs2-vol/inc/device_container.h deleted file mode 100644 index 5e3b753..0000000 --- a/HAL-afb/hal-most-unicens/ucs2-vol/inc/device_container.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * libmostvolume example - * - * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * You may also obtain this software under a propriety license from Microchip. - * Please contact Microchip for further information. - * - */ - -#ifndef DEVICECONTAINER_H -#define DEVICECONTAINER_H - -#include "device_value.h" -#include "libmostvolume.h" - -class CDeviceContainer { - -public: - CDeviceContainer(); - virtual ~CDeviceContainer(); - - void AssignService(lib_most_volume_init_t *init_ptr) {_init_ptr = init_ptr;} - - void RegisterValues(CDeviceValue** list_pptr, uint16_t list_sz); - void SetValue(uint16_t key, uint8_t value); - void ClearValues(); - void Update(); - -private: - void RequestService(uint16_t timeout); - void IncrementProcIndex(void); - void HandleI2cResult(uint8_t result); - - static void OnI2cResult(uint8_t result, void *obj_ptr); - - uint16_t _idx_processing; - uint16_t _values_sz; - CDeviceValue **_values_pptr; - bool _tx_busy; - bool _service_requested; - lib_most_volume_init_t *_init_ptr; -}; - -#endif /* DEVICECONTAINER_H */ - diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/inc/device_value.h b/HAL-afb/hal-most-unicens/ucs2-vol/inc/device_value.h deleted file mode 100644 index 21fdbdc..0000000 --- a/HAL-afb/hal-most-unicens/ucs2-vol/inc/device_value.h +++ /dev/null @@ -1,91 +0,0 @@ -/* - * libmostvolume example - * - * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * You may also obtain this software under a propriety license from Microchip. - * Please contact Microchip for further information. - * - */ - -#ifndef DEVICEVALUE_H -#define DEVICEVALUE_H - -#include -#include "libmostvolume.h" - -struct SRxMessage { - uint32_t devInst; /*instance if multiple devices are using the same group id*/ - uint32_t sourceAddr; - uint32_t targetAddr; - uint32_t nFBlock; - uint32_t nInst; - uint32_t nFunc; - uint32_t nOpType; - const uint8_t *pPayload; - uint32_t payloadLen; -}; - -struct STxMessage { - uint32_t devInst; /*instance if multiple devices are using the same group id*/ - uint32_t targetAddr; - uint32_t nFBlock; - uint32_t nInst; - uint32_t nFunc; - uint32_t nOpType; - const uint8_t *pPayload; - uint32_t payloadLen; -}; - -enum DeviceValueType { - DEVICE_VAL_MASTER = 0, - DEVICE_VAL_LEFT = 1, - DEVICE_VAL_RIGHT = 2 - -}; - -class CDeviceValue { -public: - CDeviceValue(uint16_t address, DeviceValueType type, uint16_t key); - virtual ~CDeviceValue(); - - uint16_t GetKey(){return _key;} - DeviceValueType GetType(){return _type;} // returns the assigned type - void SetValue(uint8_t value){_target_value = value;} // sets desired value - - bool RequiresUpdate(); // returns true if target is not actual value - // returns true if success, false if failed - // -> stop transmission - bool FireUpdateMessage(lib_most_volume_writei2c_cb_t writei2c_fptr, - lib_most_volume_writei2c_result_cb_t result_fptr, - void *result_user_ptr);// fires message & updates actual value - -private: - void HandleI2cResult(uint8_t result); - void ApplyMostValue(uint8_t value, DeviceValueType type, uint8_t tx_payload[]); - - bool _is_initial; // ensure first update - DeviceValueType _type; // determines the remote i2c command - uint16_t _key; // lookup key - uint16_t _address; // target node/group address - uint8_t _target_value; // desired value - uint8_t _actual_value; // value set and confirmed via network - uint8_t _tx_payload[20]; - uint8_t _tx_payload_sz; -}; - -#endif /* DEVICEPROPERTY_H */ - diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/inc/libmostvolume.h b/HAL-afb/hal-most-unicens/ucs2-vol/inc/libmostvolume.h deleted file mode 100644 index 1d7249d..0000000 --- a/HAL-afb/hal-most-unicens/ucs2-vol/inc/libmostvolume.h +++ /dev/null @@ -1,104 +0,0 @@ -/* - * libmostvolume example - * - * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * You may also obtain this software under a propriety license from Microchip. - * Please contact Microchip for further information. - * - */ - -#ifndef LIB_MOST_VOLUME_H -#define LIB_MOST_VOLUME_H - -#include - - -#ifdef __cplusplus -extern "C" { -#endif - -/** Describes the volume control */ -enum lib_most_volume_channel_t{ - LIB_MOST_VOLUME_CH_FRONT_LEFT = 0, - LIB_MOST_VOLUME_CH_FRONT_RIGHT = 1, - LIB_MOST_VOLUME_CH_REAR_LEFT = 2, - LIB_MOST_VOLUME_CH_REAR_RIGHT = 3, - LIB_MOST_VOLUME_CH_CENTER = 4, - LIB_MOST_VOLUME_CH_SUB = 5, - LIB_MOST_VOLUME_MASTER = 6 -}; - -/** Is fired when the application shall call "lib_most_volume_service()" after a certain time - * \param timeout Time in ms after which the application shall call lib_most_volume_service(). - * Valid values: - * 0x0000: as soon as possible, - * 0x0001..0xFFFE: timeout in ms, - * 0xFFFF: never - */ -typedef void (*lib_most_volume_service_cb_t)(uint16_t timeout); - -typedef void (*lib_most_volume_writei2c_result_cb_t)(uint8_t result, void *user_ptr); - -/** Callback function required to write volume changes to network devices - * \param node The node address - * \param data_ptr I2C command data - * \param data_sz Size of the I2C command data - * \return Returns \c 0 for success, other value on failure. - */ -typedef int (*lib_most_volume_writei2c_cb_t)(uint16_t node, uint8_t *data_ptr, uint8_t data_sz, - lib_most_volume_writei2c_result_cb_t result_fptr, - void *result_user_ptr); - -typedef struct lib_most_volume_init_ { - lib_most_volume_service_cb_t service_cb; - lib_most_volume_writei2c_cb_t writei2c_cb; - -} lib_most_volume_init_t; - - -/** Initializes the library - * \param UNICENS_inst Reference to the UNICENS instance, created by the application. - * \param req_service_fptr Callback function which is fired if the application shall call - * lib_most_volume_service. - * \return '0' on success, otherwise value >'0'. - */ -extern uint8_t lib_most_volume_init(lib_most_volume_init_t *init_ptr); - -/** Terminates the library - * \return '0' on success, otherwise value >'0'. - */ -extern uint8_t lib_most_volume_exit(void); - -/** Sets a single volume value. - * \param channel The volume control to be set. - * \param volume The volume value to be set. Valid values: 0..255. - * \return '0' on success, otherwise value >'0'. - */ -extern uint8_t lib_most_volume_set(enum lib_most_volume_channel_t channel, uint8_t volume); - -/** Shall be called either cyclically (e.g. 50ms -> polling) or after "timeout" - * when "service_fptr" is fired (-> event triggered). - * \return '0' on success, otherwise value >'0'. - */ -extern uint8_t lib_most_volume_service(void); - -#ifdef __cplusplus -} -#endif - -#endif /* LIB_MOST_VOLUME_H */ - diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/inc/setup.h b/HAL-afb/hal-most-unicens/ucs2-vol/inc/setup.h deleted file mode 100644 index 63bc0e2..0000000 --- a/HAL-afb/hal-most-unicens/ucs2-vol/inc/setup.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - * libmostvolume example - * - * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * You may also obtain this software under a propriety license from Microchip. - * Please contact Microchip for further information. - * - */ - -#ifndef SETUP_H -#define SETUP_H - -#include "libmostvolume.h" -#include "device_value.h" -#include "device_container.h" - -#define MAX_CHANNELS (LIB_MOST_VOLUME_MASTER + 1u) -#define MAX_MASTERS 3u - -class CSetup { - -public: - void Configure(lib_most_volume_init_t *init_ptr); - void SetVolume(enum lib_most_volume_channel_t channel, uint8_t volume); - void Update(); - -private: - CDeviceValue _volume_amp_270_m; - CDeviceValue _volume_amp_270_l; - CDeviceValue _volume_amp_270_r; - - CDeviceValue _volume_amp_271_m; - CDeviceValue _volume_amp_271_l; - CDeviceValue _volume_amp_271_r; - - CDeviceValue _volume_amp_272_m; - CDeviceValue _volume_amp_272_l; - CDeviceValue _volume_amp_272_r; - - CDeviceContainer _value_container; - lib_most_volume_init_t init_data; - -public: - static CSetup* GetInstance(); // singleton - static void Release(); // singleton - -protected: - CSetup(); // singleton - virtual ~CSetup(); // singleton - -private: - static CSetup* _instance; // singleton -}; - -#endif /* SETUP_H */ - diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/src/CMakeLists.txt b/HAL-afb/hal-most-unicens/ucs2-vol/src/CMakeLists.txt deleted file mode 100644 index 1be7184..0000000 --- a/HAL-afb/hal-most-unicens/ucs2-vol/src/CMakeLists.txt +++ /dev/null @@ -1,39 +0,0 @@ -########################################################################### -# Copyright 2015, 2016, 2017 IoT.bzh -# -# author: Fulup Ar Foll -# -# 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. -########################################################################### - -# Add target to project dependency list -PROJECT_TARGET_ADD(ucs2-volume) - -# Define targets source files -ADD_LIBRARY(${TARGET_NAME} STATIC device_container.cpp device_value.cpp libmostvolume.cpp setup.cpp) - - # Library properties - SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES - OUTPUT_NAME ${TARGET_NAME} - ) - - # Library dependencies from PKG_REQUIRED_LIST - #TARGET_LINK_LIBRARIES(ucs2-volume # Library dependencies (include updates automatically) - # ${link_libraries} - #) - - # Define properties to expose when others use this target - TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME} - PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../inc - ) - diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/src/device_container.cpp b/HAL-afb/hal-most-unicens/ucs2-vol/src/device_container.cpp deleted file mode 100644 index cef25b1..0000000 --- a/HAL-afb/hal-most-unicens/ucs2-vol/src/device_container.cpp +++ /dev/null @@ -1,166 +0,0 @@ -/* - * libmostvolume example - * - * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * You may also obtain this software under a propriety license from Microchip. - * Please contact Microchip for further information. - * - */ - -#include -#include "device_container.h" - -#define DEVCONT_TIME_RETRIGGER (uint16_t)100U -#define DEVCONT_TIME_NOW (uint16_t)0U -#define DEVCONT_TIME_STOP (uint16_t)0xFFFFU - -#define DEVCONT_UNUSED(a) (a = a) - -CDeviceContainer::CDeviceContainer() -{ - this->_idx_processing = 0U; - this->_values_pptr = NULL; - this->_values_sz = 0U; - this->_tx_busy = false; - this->_service_requested = false; - this->_init_ptr = NULL; -} - -CDeviceContainer::~CDeviceContainer() -{ - /*Clb_RegisterI2CResultCB(NULL, NULL);*/ /* avoid that the result callback is fired after object is destroyed */ -} - -void CDeviceContainer::RegisterValues(CDeviceValue** list_pptr, uint16_t list_sz) -{ - this->_idx_processing = 0U; - this->_values_pptr = list_pptr; - this->_values_sz = list_sz; - this->_tx_busy = false; - - if ((list_pptr != NULL) && (list_sz > 0U)) - { - this->_idx_processing = (uint16_t)(list_sz - 1U); - } -} - -void CDeviceContainer::ClearValues() -{ - this->_idx_processing = 0U; - this->_values_pptr = NULL; - this->_values_sz = 0U; - this->_tx_busy = false; -} - -void CDeviceContainer::SetValue(uint16_t key, uint8_t value) -{ - uint16_t idx; - bool req_update = false; - - for (idx = 0U; idx < this->_values_sz; idx++) - { - if (this->_values_pptr[idx]->GetKey() == key) - { - this->_values_pptr[idx]->SetValue(value); - if (this->_values_pptr[idx]->RequiresUpdate()) - { - req_update = true; - } - } - } - - if (req_update && (!this->_tx_busy)) - { - RequestService(DEVCONT_TIME_NOW); //fire callback - } -} - -void CDeviceContainer::IncrementProcIndex(void) -{ - if ((_idx_processing + 1U) >= this->_values_sz) - { - _idx_processing = 0U; - } - else - { - _idx_processing++; - } -} - -// starts at latest position, searches next value to update, waits until response -void CDeviceContainer::Update() -{ - uint16_t cnt; - bool error = false; - _service_requested = false; - - if (this->_tx_busy) - { - return; - } - - for (cnt = 0u; cnt < this->_values_sz; cnt++) /* just run one cycle */ - { - IncrementProcIndex(); - - if (_values_pptr[_idx_processing]->RequiresUpdate()) - { - if (_values_pptr[_idx_processing]->FireUpdateMessage(this->_init_ptr->writei2c_cb, - &OnI2cResult, - this)) - { - /*Clb_RegisterI2CResultCB(&OnI2cResult, this);*/ - this->_tx_busy = true; - break; - } - else - { - error = true; - } - } - } - - if (error) - { - RequestService(DEVCONT_TIME_RETRIGGER); - } -} - -void CDeviceContainer::HandleI2cResult(uint8_t result) -{ - DEVCONT_UNUSED(result); - this->_tx_busy = false; - this->RequestService(DEVCONT_TIME_NOW); -} - -void CDeviceContainer::OnI2cResult(uint8_t result, void *obj_ptr) -{ - ((CDeviceContainer*)obj_ptr)->HandleI2cResult(result); -} - -void CDeviceContainer::RequestService(uint16_t timeout) -{ - if (!_service_requested) - { - _service_requested = true; - - if (this->_init_ptr && this->_init_ptr->service_cb) - { - this->_init_ptr->service_cb(timeout); - } - } -} diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/src/device_value.cpp b/HAL-afb/hal-most-unicens/ucs2-vol/src/device_value.cpp deleted file mode 100644 index b8c681e..0000000 --- a/HAL-afb/hal-most-unicens/ucs2-vol/src/device_value.cpp +++ /dev/null @@ -1,130 +0,0 @@ -/* - * libmostvolume example - * - * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * You may also obtain this software under a propriety license from Microchip. - * Please contact Microchip for further information. - * - */ - -#include "device_value.h" -#include "setup.h" - -#define MUTE_VALUE 0x03FFU -#define MUTE_VALUE_HB 0x03U -#define MUTE_VALUE_LB 0xFFU - -#define CONTROL_MASTER 0x07U -#define CONTROL_CH_1 0x08U -#define CONTROL_CH_2 0x09U - -CDeviceValue::CDeviceValue(uint16_t address, DeviceValueType type, uint16_t key) -{ - this->_is_initial = true; - this->_address = address; - this->_target_value = 0x01u; - this->_actual_value = 0x01u; - - this->_type = type; - this->_key = key; - - _tx_payload[0] = CONTROL_MASTER;// 7: master, 8: channel 1, 9: Channel 2 - _tx_payload[1] = MUTE_VALUE_HB; //HB:Volume - _tx_payload[2] = MUTE_VALUE_LB; //LB:Volume - _tx_payload_sz = 3u; -} - -CDeviceValue::~CDeviceValue() -{ -} - -void CDeviceValue::ApplyMostValue(uint8_t value, DeviceValueType type, uint8_t tx_payload[]) -{ - uint16_t tmp = MUTE_VALUE; - - switch (type) - { - case DEVICE_VAL_LEFT: - tmp = (uint16_t)(0x80U + 0x37FU - (0x37FU * ((int32_t)value) / (0xFFU))); - //tmp = 0x3FF - (0x3FF * ((int32_t)value) / (0xFF)); - //tmp = 0x100 + 0x2FF - (0x2FF * ((int32_t)value) / (0xFF)); - tx_payload[0] = CONTROL_CH_1; - break; - case DEVICE_VAL_RIGHT: - tmp = (uint16_t)(0x80U + 0x37FU - (0x37FU * ((int32_t)value) / (0xFFU))); - //tmp = 0x3FF - (0x3FF * ((int32_t)value) / (0xFF)); - //tmp = 0x100 + 0x2FF - (0x2FF * ((int32_t)value) / (0xFF)); - tx_payload[0] = CONTROL_CH_2; - break; - default: - /*std::cerr << "CDeviceValue::ApplyMostValue() error matching incorrect" << std::endl;*/ - case DEVICE_VAL_MASTER: - tmp = (uint16_t)(0x100U + 0x2FFU - (0x2FFU * ((int32_t)value) / (0xFFU))); - tx_payload[0] = CONTROL_MASTER; - break; - } - - tx_payload[1] = (uint8_t)((tmp >> 8U) & (uint16_t)0xFFU); //HB:Volume - tx_payload[2] = (uint8_t)(tmp & (uint16_t)0xFFU); //LB:Volume -} - -// returns true if target is not actual value -bool CDeviceValue::RequiresUpdate() -{ - if (this->_target_value != this->_actual_value) - { - return true; - } - - return false; -} - -bool CDeviceValue::FireUpdateMessage(lib_most_volume_writei2c_cb_t writei2c_fptr, - lib_most_volume_writei2c_result_cb_t result_fptr, - void *result_user_ptr) -{ - int ret; - ApplyMostValue(this->_target_value, _type, _tx_payload); - - ret = writei2c_fptr(this->_address, &_tx_payload[0], _tx_payload_sz, - result_fptr, - result_user_ptr); - - if (ret == 0) - { - // Clb_RegisterI2CResultCB(OnI2cResult, this); - // mark value as set! - this->_actual_value = this->_target_value; - return true; - } - - return false; -} - -#if 0 - ret = Ucs_I2c_WritePort( CSetup::GetInstance()->RetrieveUnicensInst(), - this->_address, - 0x0F00u, /* i2c port handle */ - UCS_I2C_DEFAULT_MODE, /* 0 */ - 0u, /* block count */ - 0x2Au, /* i2c slave address */ - 0x03E8u, /* timeout 1000 milliseconds */ - _tx_payload_sz, /* data length */ - &_tx_payload[0], /* data pointer */ - &Clb_OnWriteI2CPortResult - ); -#endif diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/src/libmostvolume.cpp b/HAL-afb/hal-most-unicens/ucs2-vol/src/libmostvolume.cpp deleted file mode 100644 index a90b5b1..0000000 --- a/HAL-afb/hal-most-unicens/ucs2-vol/src/libmostvolume.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/* - * libmostvolume example - * - * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * You may also obtain this software under a propriety license from Microchip. - * Please contact Microchip for further information. - * - */ - -#include "libmostvolume.h" -#include "setup.h" -/*#include */ - -static bool _running = false; - -extern "C" uint8_t lib_most_volume_init(lib_most_volume_init_t *init_ptr) -{ - uint8_t success = 1U; - /*std::cerr << "lib_most_volume_init(): called" << std::endl;*/ - - if (!_running && init_ptr) - { - CSetup::GetInstance()->Configure(init_ptr); - success = 0U; - _running = true; - } - - return success; -} - -extern "C" uint8_t lib_most_volume_exit(void) -{ - uint8_t success = 1U; - /*std::cerr << "lib_most_volume_exit(): called" << std::endl;*/ - - if (_running) - { - CSetup::Release(); - success = 0U; - _running = false; - } - - return success; -} - -extern "C" uint8_t lib_most_volume_set(enum lib_most_volume_channel_t channel, uint8_t volume) -{ - uint8_t success = 1U; - /*std::cerr << "lib_most_volume_set(): channel=" << channel << ", volume=" << (int)volume << std::endl;*/ - - if (_running) - { - CSetup::GetInstance()->SetVolume(channel, volume); - success = 0U; - } - - return success; -} - -extern "C" uint8_t lib_most_volume_service(void) -{ - uint8_t success = 1U; - /*std::cerr << "lib_most_volume_service(): called" << std::endl;*/ - - if (_running) - { - CSetup::GetInstance()->Update(); - success = 0U; - } - - return success; -} diff --git a/HAL-afb/hal-most-unicens/ucs2-vol/src/setup.cpp b/HAL-afb/hal-most-unicens/ucs2-vol/src/setup.cpp deleted file mode 100644 index 61b8a68..0000000 --- a/HAL-afb/hal-most-unicens/ucs2-vol/src/setup.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/* - * libmostvolume example - * - * Copyright (C) 2017 Microchip Technology Germany II GmbH & Co. KG - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * You may also obtain this software under a propriety license from Microchip. - * Please contact Microchip for further information. - * - */ -#include -#include "setup.h" - -CSetup* CSetup::_instance = NULL; - -// singleton -CSetup* CSetup::GetInstance() { - if (_instance == NULL) { - _instance = new CSetup(); - } - - return _instance; -} - -// singleton -void CSetup::Release() { - if (_instance != NULL) { - delete _instance; - } - _instance = NULL; -} - -CSetup::CSetup() - : _volume_amp_270_m(0x270U, DEVICE_VAL_MASTER, LIB_MOST_VOLUME_MASTER), - _volume_amp_270_l(0x270U, DEVICE_VAL_LEFT, LIB_MOST_VOLUME_CH_FRONT_LEFT), - _volume_amp_270_r(0x270U, DEVICE_VAL_RIGHT, LIB_MOST_VOLUME_CH_FRONT_RIGHT), - _volume_amp_271_m(0x271U, DEVICE_VAL_MASTER, LIB_MOST_VOLUME_MASTER), - _volume_amp_271_l(0x271U, DEVICE_VAL_LEFT, LIB_MOST_VOLUME_CH_REAR_LEFT), - _volume_amp_271_r(0x271U, DEVICE_VAL_RIGHT, LIB_MOST_VOLUME_CH_REAR_RIGHT), - _volume_amp_272_m(0x272U, DEVICE_VAL_MASTER, LIB_MOST_VOLUME_MASTER), - _volume_amp_272_l(0x272U, DEVICE_VAL_LEFT, LIB_MOST_VOLUME_CH_CENTER), - _volume_amp_272_r(0x272U, DEVICE_VAL_RIGHT, LIB_MOST_VOLUME_CH_SUB), - _value_container() -{ - static CDeviceValue* value_list[9] = { &_volume_amp_270_m, - &_volume_amp_270_l, - &_volume_amp_270_r, - &_volume_amp_271_m, - &_volume_amp_271_l, - &_volume_amp_271_r, - &_volume_amp_272_m, - &_volume_amp_272_l, - &_volume_amp_272_r}; - - _value_container.RegisterValues(value_list, 9U); -} - -CSetup::~CSetup() -{ - -} - -void CSetup::Configure(lib_most_volume_init_t *init_ptr) -{ - this->init_data = *init_ptr; - _value_container.AssignService(&this->init_data); -} - -void CSetup::SetVolume(enum lib_most_volume_channel_t channel, uint8_t volume) -{ - _value_container.SetValue((uint16_t)channel, volume); -} - -void CSetup::Update() -{ - _value_container.Update(); -} diff --git a/HAL-afb/hal-most-unicens/wrap-json.c b/HAL-afb/hal-most-unicens/wrap-json.c deleted file mode 100644 index 987a008..0000000 --- a/HAL-afb/hal-most-unicens/wrap-json.c +++ /dev/null @@ -1,940 +0,0 @@ -/* - Copyright (C) 2016, 2017 "IoT.bzh" - - author: José Bollo - - 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. -*/ - -#include - -#include "wrap-json.h" - -#define STACKCOUNT 32 -#define STRCOUNT 8 - -enum { - wrap_json_error_none, - wrap_json_error_null_object, - wrap_json_error_truncated, - wrap_json_error_internal_error, - wrap_json_error_out_of_memory, - wrap_json_error_invalid_character, - wrap_json_error_too_long, - wrap_json_error_too_deep, - wrap_json_error_null_spec, - wrap_json_error_null_key, - wrap_json_error_null_string, - wrap_json_error_out_of_range, - wrap_json_error_incomplete, - wrap_json_error_missfit_type, - wrap_json_error_key_not_found, - _wrap_json_error_count_ -}; - -static const char ignore_all[] = " \t\n\r,:"; -static const char pack_accept_arr[] = "][{snbiIfoO"; -static const char pack_accept_key[] = "s}"; -#define pack_accept_any (&pack_accept_arr[1]) - -static const char unpack_accept_arr[] = "*!][{snbiIfFoO"; -static const char unpack_accept_key[] = "*!s}"; -#define unpack_accept_any (&unpack_accept_arr[3]) - -static const char *pack_errors[_wrap_json_error_count_] = -{ - [wrap_json_error_none] = "unknown error", - [wrap_json_error_null_object] = "null object", - [wrap_json_error_truncated] = "truncated", - [wrap_json_error_internal_error] = "internal error", - [wrap_json_error_out_of_memory] = "out of memory", - [wrap_json_error_invalid_character] = "invalid character", - [wrap_json_error_too_long] = "too long", - [wrap_json_error_too_deep] = "too deep", - [wrap_json_error_null_spec] = "spec is NULL", - [wrap_json_error_null_key] = "key is NULL", - [wrap_json_error_null_string] = "string is NULL", - [wrap_json_error_out_of_range] = "array too small", - [wrap_json_error_incomplete] = "incomplete container", - [wrap_json_error_missfit_type] = "missfit of type", - [wrap_json_error_key_not_found] = "key not found" -}; - -int wrap_json_get_error_position(int rc) -{ - if (rc < 0) - rc = -rc; - return (rc >> 4) + 1; -} - -int wrap_json_get_error_code(int rc) -{ - if (rc < 0) - rc = -rc; - return rc & 15; -} - -const char *wrap_json_get_error_string(int rc) -{ - rc = wrap_json_get_error_code(rc); - if (rc >= sizeof pack_errors / sizeof *pack_errors) - rc = 0; - return pack_errors[rc]; -} - - - -static inline const char *skip(const char *d) -{ - while (*d && strchr(ignore_all, *d)) - d++; - return d; -} - -int wrap_json_vpack(struct json_object **result, const char *desc, va_list args) -{ - /* TODO: the case of structs with key being single char should be optimized */ - int nstr, notnull, nullable, rc; - size_t sz, dsz, ssz; - char *s; - char c; - const char *d; - char buffer[256]; - struct { const char *str; size_t sz; } strs[STRCOUNT]; - struct { struct json_object *cont, *key; const char *acc; char type; } stack[STACKCOUNT], *top; - struct json_object *obj; - - ssz = sizeof buffer; - s = buffer; - top = stack; - top->key = NULL; - top->cont = NULL; - top->acc = pack_accept_any; - top->type = 0; - d = desc; - if (!d) - goto null_spec; - d = skip(d); - for(;;) { - c = *d; - if (!c) - goto truncated; - if (!strchr(top->acc, c)) - goto invalid_character; - d = skip(++d); - switch(c) { - case 's': - nullable = 0; - notnull = 0; - nstr = 0; - sz = 0; - for (;;) { - strs[nstr].str = va_arg(args, const char*); - if (strs[nstr].str) - notnull = 1; - if (*d == '?') { - d = skip(++d); - nullable = 1; - } - switch(*d) { - case '%': strs[nstr].sz = va_arg(args, size_t); d = skip(++d); break; - case '#': strs[nstr].sz = (size_t)va_arg(args, int); d = skip(++d); break; - default: strs[nstr].sz = strs[nstr].str ? strlen(strs[nstr].str) : 0; break; - } - sz += strs[nstr++].sz; - if (*d == '?') { - d = skip(++d); - nullable = 1; - } - if (*d != '+') - break; - if (nstr >= STRCOUNT) - goto too_long; - d = skip(++d); - } - if (*d == '*') - nullable = 1; - if (notnull) { - if (sz > ssz) { - ssz += ssz; - if (ssz < sz) - ssz = sz; - s = alloca(sz); - } - dsz = sz; - while (nstr) { - nstr--; - dsz -= strs[nstr].sz; - memcpy(&s[dsz], strs[nstr].str, strs[nstr].sz); - } - obj = json_object_new_string_len(s, (int)sz); - if (!obj) - goto out_of_memory; - } else if (nullable) - obj = NULL; - else - goto null_string; - break; - case 'n': - obj = NULL; - break; - case 'b': - obj = json_object_new_boolean(va_arg(args, int)); - if (!obj) - goto out_of_memory; - break; - case 'i': - obj = json_object_new_int(va_arg(args, int)); - if (!obj) - goto out_of_memory; - break; - case 'I': - obj = json_object_new_int64(va_arg(args, int64_t)); - if (!obj) - goto out_of_memory; - break; - case 'f': - obj = json_object_new_double(va_arg(args, double)); - if (!obj) - goto out_of_memory; - break; - case 'o': - case 'O': - obj = va_arg(args, struct json_object*); - if (*d == '?') - d = skip(++d); - else if (*d != '*' && !obj) - goto null_object; - if (c == 'O') - json_object_get(obj); - break; - case '[': - case '{': - if (++top >= &stack[STACKCOUNT]) - goto too_deep; - top->key = NULL; - if (c == '[') { - top->type = ']'; - top->acc = pack_accept_arr; - top->cont = json_object_new_array(); - } else { - top->type = '}'; - top->acc = pack_accept_key; - top->cont = json_object_new_object(); - } - if (!top->cont) - goto out_of_memory; - continue; - case '}': - case ']': - if (c != top->type || top <= stack) - goto internal_error; - obj = (top--)->cont; - if (*d == '*' && !(c == '}' ? json_object_object_length(obj) : json_object_array_length(obj))) { - json_object_put(obj); - obj = NULL; - } - break; - default: - goto internal_error; - } - switch (top->type) { - case 0: - if (top != stack) - goto internal_error; - if (*d) - goto invalid_character; - *result = obj; - return 0; - case ']': - if (obj || *d != '*') - json_object_array_add(top->cont, obj); - if (*d == '*') - d = skip(++d); - break; - case '}': - if (!obj) - goto null_key; - top->key = obj; - top->acc = pack_accept_any; - top->type = ':'; - break; - case ':': - if (obj || *d != '*') - json_object_object_add(top->cont, json_object_get_string(top->key), obj); - if (*d == '*') - d = skip(++d); - json_object_put(top->key); - top->key = NULL; - top->acc = pack_accept_key; - top->type = '}'; - break; - default: - goto internal_error; - } - } - -null_object: - rc = wrap_json_error_null_object; - goto error; -truncated: - rc = wrap_json_error_truncated; - goto error; -internal_error: - rc = wrap_json_error_internal_error; - goto error; -out_of_memory: - rc = wrap_json_error_out_of_memory; - goto error; -invalid_character: - rc = wrap_json_error_invalid_character; - goto error; -too_long: - rc = wrap_json_error_too_long; - goto error; -too_deep: - rc = wrap_json_error_too_deep; - goto error; -null_spec: - rc = wrap_json_error_null_spec; - goto error; -null_key: - rc = wrap_json_error_null_key; - goto error; -null_string: - rc = wrap_json_error_null_string; - goto error; -error: - do { - json_object_put(top->key); - json_object_put(top->cont); - } while (--top >= stack); - *result = NULL; - rc = rc | (int)((d - desc) << 4); - return -rc; -} - -int wrap_json_pack(struct json_object **result, const char *desc, ...) -{ - int rc; - va_list args; - - va_start(args, desc); - rc = wrap_json_vpack(result, desc, args); - va_end(args); - return rc; -} - -static int vunpack(struct json_object *object, const char *desc, va_list args, int store) -{ - int rc = 0, optionnal, ignore; - char c, xacc[2] = { 0, 0 }; - const char *acc; - const char *d, *fit = NULL; - const char *key = NULL; - const char **ps = NULL; - double *pf = NULL; - int *pi = NULL; - int64_t *pI = NULL; - size_t *pz = NULL; - struct { struct json_object *parent; const char *acc; int index, count; char type; } stack[STACKCOUNT], *top; - struct json_object *obj; - struct json_object **po; - - xacc[0] = 0; - ignore = 0; - top = NULL; - acc = unpack_accept_any; - d = desc; - if (!d) - goto null_spec; - d = skip(d); - obj = object; - for(;;) { - fit = d; - c = *d; - if (!c) - goto truncated; - if (!strchr(acc, c)) - goto invalid_character; - d = skip(++d); - switch(c) { - case 's': - if (xacc[0] == '}') { - /* expects a key */ - key = va_arg(args, const char *); - if (!key) - goto null_key; - if (*d != '?') - optionnal = 0; - else { - optionnal = 1; - d = skip(++d); - } - if (ignore) - ignore++; - else { - if (json_object_object_get_ex(top->parent, key, &obj)) { - /* found */ - top->index++; - } else { - /* not found */ - if (!optionnal) - goto key_not_found; - ignore = 1; - obj = NULL; - } - } - xacc[0] = ':'; - acc = unpack_accept_any; - continue; - } - /* get a string */ - if (store) - ps = va_arg(args, const char **); - if (!ignore) { - if (!json_object_is_type(obj, json_type_string)) - goto missfit; - if (store && ps) - *ps = json_object_get_string(obj); - } - if (*d == '%') { - d = skip(++d); - if (store) { - pz = va_arg(args, size_t *); - if (!ignore && pz) - *pz = (size_t)json_object_get_string_len(obj); - } - } - break; - case 'n': - if (!ignore && !json_object_is_type(obj, json_type_null)) - goto missfit; - break; - case 'b': - if (store) - pi = va_arg(args, int *); - - if (!ignore) { - if (!json_object_is_type(obj, json_type_boolean)) - goto missfit; - if (store && pi) - *pi = json_object_get_boolean(obj); - } - break; - case 'i': - if (store) - pi = va_arg(args, int *); - - if (!ignore) { - if (!json_object_is_type(obj, json_type_int)) - goto missfit; - if (store && pi) - *pi = json_object_get_int(obj); - } - break; - case 'I': - if (store) - pI = va_arg(args, int64_t *); - - if (!ignore) { - if (!json_object_is_type(obj, json_type_int)) - goto missfit; - if (store && pI) - *pI = json_object_get_int64(obj); - } - break; - case 'f': - case 'F': - if (store) - pf = va_arg(args, double *); - - if (!ignore) { - if (!(json_object_is_type(obj, json_type_double) || (c == 'F' && json_object_is_type(obj, json_type_int)))) - goto missfit; - if (store && pf) - *pf = json_object_get_double(obj); - } - break; - case 'o': - case 'O': - if (store) { - po = va_arg(args, struct json_object **); - if (!ignore && po) { - if (c == 'O') - obj = json_object_get(obj); - *po = obj; - } - } - break; - - case '[': - case '{': - if (!top) - top = stack; - else if (++top >= &stack[STACKCOUNT]) - goto too_deep; - - top->acc = acc; - top->type = xacc[0]; - top->index = 0; - top->parent = obj; - if (ignore) - ignore++; - if (c == '[') { - if (!ignore) { - if (!json_object_is_type(obj, json_type_array)) - goto missfit; - top->count = json_object_array_length(obj); - } - xacc[0] = ']'; - acc = unpack_accept_arr; - } else { - if (!ignore) { - if (!json_object_is_type(obj, json_type_object)) - goto missfit; - top->count = json_object_object_length(obj); - } - xacc[0] = '}'; - acc = unpack_accept_key; - continue; - } - break; - case '}': - case ']': - if (!top || c != xacc[0]) - goto internal_error; - acc = top->acc; - xacc[0] = top->type; - top = top == stack ? NULL : top - 1; - if (ignore) - ignore--; - break; - case '!': - if (*d != xacc[0]) - goto invalid_character; - if (!ignore && top->index != top->count) - goto incomplete; - /*@fallthrough@*/ - case '*': - acc = xacc; - continue; - default: - goto internal_error; - } - switch (xacc[0]) { - case 0: - if (top) - goto internal_error; - if (*d) - goto invalid_character; - return 0; - case ']': - if (!ignore) { - key = strchr(unpack_accept_arr, *d); - if (key && key >= unpack_accept_any) { - if (top->index >= top->count) - goto out_of_range; - obj = json_object_array_get_idx(top->parent, top->index++); - } - } - break; - case ':': - acc = unpack_accept_key; - xacc[0] = '}'; - if (ignore) - ignore--; - break; - default: - goto internal_error; - } - } -truncated: - rc = wrap_json_error_truncated; - goto error; -internal_error: - rc = wrap_json_error_internal_error; - goto error; -invalid_character: - rc = wrap_json_error_invalid_character; - goto error; -too_deep: - rc = wrap_json_error_too_deep; - goto error; -null_spec: - rc = wrap_json_error_null_spec; - goto error; -null_key: - rc = wrap_json_error_null_key; - goto error; -out_of_range: - rc = wrap_json_error_out_of_range; - goto error; -incomplete: - rc = wrap_json_error_incomplete; - goto error; -missfit: - rc = wrap_json_error_missfit_type; - goto errorfit; -key_not_found: - rc = wrap_json_error_key_not_found; - goto error; -errorfit: - d = fit; -error: - rc = rc | (int)((d - desc) << 4); - return -rc; -} - -int wrap_json_vcheck(struct json_object *object, const char *desc, va_list args) -{ - return vunpack(object, desc, args, 0); -} - -int wrap_json_check(struct json_object *object, const char *desc, ...) -{ - int rc; - va_list args; - - va_start(args, desc); - rc = vunpack(object, desc, args, 0); - va_end(args); - return rc; -} - -int wrap_json_vmatch(struct json_object *object, const char *desc, va_list args) -{ - return !vunpack(object, desc, args, 0); -} - -int wrap_json_match(struct json_object *object, const char *desc, ...) -{ - int rc; - va_list args; - - va_start(args, desc); - rc = vunpack(object, desc, args, 0); - va_end(args); - return !rc; -} - -int wrap_json_vunpack(struct json_object *object, const char *desc, va_list args) -{ - return vunpack(object, desc, args, 1); -} - -int wrap_json_unpack(struct json_object *object, const char *desc, ...) -{ - int rc; - va_list args; - - va_start(args, desc); - rc = vunpack(object, desc, args, 1); - va_end(args); - return rc; -} - -static void object_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure) -{ - struct json_object_iterator it = json_object_iter_begin(object); - struct json_object_iterator end = json_object_iter_end(object); - while (!json_object_iter_equal(&it, &end)) { - callback(closure, json_object_iter_peek_value(&it), json_object_iter_peek_name(&it)); - json_object_iter_next(&it); - } -} - -static void array_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure) -{ - int n = json_object_array_length(object); - int i = 0; - while(i < n) - callback(closure, json_object_array_get_idx(object, i++)); -} - -void wrap_json_optarray_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure) -{ - if (json_object_is_type(object, json_type_array)) - array_for_all(object, callback, closure); - else - callback(closure, object); -} - -void wrap_json_array_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure) -{ - if (json_object_is_type(object, json_type_array)) - array_for_all(object, callback, closure); -} - -void wrap_json_object_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure) -{ - if (json_object_is_type(object, json_type_object)) - object_for_all(object, callback, closure); -} - -void wrap_json_optobject_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure) -{ - if (json_object_is_type(object, json_type_object)) - object_for_all(object, callback, closure); - else - callback(closure, object, NULL); -} - -void wrap_json_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure) -{ - if (!object) - /* do nothing */; - else if (json_object_is_type(object, json_type_object)) - object_for_all(object, callback, closure); - else if (!json_object_is_type(object, json_type_array)) - callback(closure, object, NULL); - else { - int n = json_object_array_length(object); - int i = 0; - while(i < n) - callback(closure, json_object_array_get_idx(object, i++), NULL); - } -} - -#if defined(WRAP_JSON_TEST) -#include - -void p(const char *desc, ...) -{ - int rc; - va_list args; - struct json_object *result; - - va_start(args, desc); - rc = wrap_json_vpack(&result, desc, args); - va_end(args); - if (!rc) - printf(" SUCCESS %s\n\n", json_object_to_json_string(result)); - else - printf(" ERROR[char %d err %d] %s\n\n", wrap_json_get_error_position(rc), wrap_json_get_error_code(rc), wrap_json_get_error_string(rc)); - json_object_put(result); -} - -const char *xs[10]; -int *xi[10]; -int64_t *xI[10]; -double *xf[10]; -struct json_object *xo[10]; -size_t xz[10]; - -void u(const char *value, const char *desc, ...) -{ - unsigned m, k; - int rc; - va_list args; - struct json_object *obj, *o; - - memset(xs, 0, sizeof xs); - memset(xi, 0, sizeof xi); - memset(xI, 0, sizeof xI); - memset(xf, 0, sizeof xf); - memset(xo, 0, sizeof xo); - memset(xz, 0, sizeof xz); - obj = json_tokener_parse(value); - va_start(args, desc); - rc = wrap_json_vunpack(obj, desc, args); - va_end(args); - if (rc) - printf(" ERROR[char %d err %d] %s\n\n", wrap_json_get_error_position(rc), wrap_json_get_error_code(rc), wrap_json_get_error_string(rc)); - else { - value = NULL; - printf(" SUCCESS"); - va_start(args, desc); - k = m = 0; - while(*desc) { - switch(*desc) { - case '{': m = (m << 1) | 1; k = 1; break; - case '}': m = m >> 1; k = m&1; break; - case '[': m = m << 1; k = 0; break; - case ']': m = m >> 1; k = m&1; break; - case 's': printf(" s:%s", k ? va_arg(args, const char*) : *(va_arg(args, const char**)?:&value)); k ^= m&1; break; - case '%': printf(" %%:%zu", *va_arg(args, size_t*)); k = m&1; break; - case 'n': printf(" n"); k = m&1; break; - case 'b': printf(" b:%d", *va_arg(args, int*)); k = m&1; break; - case 'i': printf(" i:%d", *va_arg(args, int*)); k = m&1; break; - case 'I': printf(" I:%lld", *va_arg(args, int64_t*)); k = m&1; break; - case 'f': printf(" f:%f", *va_arg(args, double*)); k = m&1; break; - case 'F': printf(" F:%f", *va_arg(args, double*)); k = m&1; break; - case 'o': printf(" o:%s", json_object_to_json_string(*va_arg(args, struct json_object**))); k = m&1; break; - case 'O': o = *va_arg(args, struct json_object**); printf(" O:%s", json_object_to_json_string(o)); json_object_put(o); k = m&1; break; - default: break; - } - desc++; - } - va_end(args); - printf("\n\n"); - } - json_object_put(obj); -} - -#define P(...) do{ printf("pack(%s)\n",#__VA_ARGS__); p(__VA_ARGS__); } while(0) -#define U(...) do{ printf("unpack(%s)\n",#__VA_ARGS__); u(__VA_ARGS__); } while(0) - -int main() -{ - char buffer[4] = {'t', 'e', 's', 't'}; - - P("n"); - P("b", 1); - P("b", 0); - P("i", 1); - P("I", (uint64_t)0x123456789abcdef); - P("f", 3.14); - P("s", "test"); - P("s?", "test"); - P("s?", NULL); - P("s#", "test asdf", 4); - P("s%", "test asdf", (size_t)4); - P("s#", buffer, 4); - P("s%", buffer, (size_t)4); - P("s++", "te", "st", "ing"); - P("s#+#+", "test", 1, "test", 2, "test"); - P("s%+%+", "test", (size_t)1, "test", (size_t)2, "test"); - P("{}", 1.0); - P("[]", 1.0); - P("o", json_object_new_int(1)); - P("o?", json_object_new_int(1)); - P("o?", NULL); - P("O", json_object_new_int(1)); - P("O?", json_object_new_int(1)); - P("O?", NULL); - P("{s:[]}", "foo"); - P("{s+#+: []}", "foo", "barbar", 3, "baz"); - P("{s:s,s:o,s:O}", "a", NULL, "b", NULL, "c", NULL); - P("{s:**}", "a", NULL); - P("{s:s*,s:o*,s:O*}", "a", NULL, "b", NULL, "c", NULL); - P("[i,i,i]", 0, 1, 2); - P("[s,o,O]", NULL, NULL, NULL); - P("[**]", NULL); - P("[s*,o*,O*]", NULL, NULL, NULL); - P(" s ", "test"); - P("[ ]"); - P("[ i , i, i ] ", 1, 2, 3); - P("{\n\n1"); - P("[}"); - P("{]"); - P("["); - P("{"); - P("[i]a", 42); - P("ia", 42); - P("s", NULL); - P("+", NULL); - P(NULL); - P("{s:i}", NULL, 1); - P("{ {}: s }", "foo"); - P("{ s: {}, s:[ii{} }", "foo", "bar", 12, 13); - P("[[[[[ [[[[[ [[[[ }]]]] ]]]] ]]]]]"); - - U("true", "b", &xi[0]); - U("false", "b", &xi[0]); - U("null", "n"); - U("42", "i", &xi[0]); - U("123456789", "I", &xI[0]); - U("3.14", "f", &xf[0]); - U("12345", "F", &xf[0]); - U("3.14", "F", &xf[0]); - U("\"foo\"", "s", &xs[0]); - U("\"foo\"", "s%", &xs[0], &xz[0]); - U("{}", "{}"); - U("[]", "[]"); - U("{}", "o", &xo[0]); - U("{}", "O", &xo[0]); - U("{\"foo\":42}", "{si}", "foo", &xi[0]); - U("[1,2,3]", "[i,i,i]", &xi[0], &xi[1], &xi[2]); - U("{\"a\":1,\"b\":2,\"c\":3}", "{s:i, s:i, s:i}", "a", &xi[0], "b", &xi[1], "c", &xi[2]); - U("42", "z"); - U("null", "[i]"); - U("[]", "[}"); - U("{}", "{]"); - U("[]", "["); - U("{}", "{"); - U("[42]", "[i]a", &xi[0]); - U("42", "ia", &xi[0]); - U("[]", NULL); - U("\"foo\"", "s", NULL); - U("42", "s", NULL); - U("42", "n"); - U("42", "b", NULL); - U("42", "f", NULL); - U("42", "[i]", NULL); - U("42", "{si}", "foo", NULL); - U("\"foo\"", "n"); - U("\"foo\"", "b", NULL); - U("\"foo\"", "i", NULL); - U("\"foo\"", "I", NULL); - U("\"foo\"", "f", NULL); - U("\"foo\"", "F", NULL); - U("true", "s", NULL); - U("true", "n"); - U("true", "i", NULL); - U("true", "I", NULL); - U("true", "f", NULL); - U("true", "F", NULL); - U("[42]", "[ii]", &xi[0], &xi[1]); - U("{\"foo\":42}", "{si}", NULL, &xi[0]); - U("{\"foo\":42}", "{si}", "baz", &xi[0]); - U("[1,2,3]", "[iii!]", &xi[0], &xi[1], &xi[2]); - U("[1,2,3]", "[ii!]", &xi[0], &xi[1]); - U("[1,2,3]", "[ii]", &xi[0], &xi[1]); - U("[1,2,3]", "[ii*]", &xi[0], &xi[1]); - U("{\"foo\":42,\"baz\":45}", "{sisi}", "baz", &xi[0], "foo", &xi[1]); - U("{\"foo\":42,\"baz\":45}", "{sisi*}", "baz", &xi[0], "foo", &xi[1]); - U("{\"foo\":42,\"baz\":45}", "{sisi!}", "baz", &xi[0], "foo", &xi[1]); - U("{\"foo\":42,\"baz\":45}", "{si}", "baz", &xi[0], "foo", &xi[1]); - U("{\"foo\":42,\"baz\":45}", "{si*}", "baz", &xi[0], "foo", &xi[1]); - U("{\"foo\":42,\"baz\":45}", "{si!}", "baz", &xi[0], "foo", &xi[1]); - U("[1,{\"foo\":2,\"bar\":null},[3,4]]", "[i{sisn}[ii]]", &xi[0], "foo", &xi[1], "bar", &xi[2], &xi[3]); - U("[1,2,3]", "[ii!i]", &xi[0], &xi[1], &xi[2]); - U("[1,2,3]", "[ii*i]", &xi[0], &xi[1], &xi[2]); - U("{\"foo\":1,\"bar\":2}", "{si!si}", "foo", &xi[1], "bar", &xi[2]); - U("{\"foo\":1,\"bar\":2}", "{si*si}", "foo", &xi[1], "bar", &xi[2]); - U("{\"foo\":{\"baz\":null,\"bar\":null}}", "{s{sn!}}", "foo", "bar"); - U("[[1,2,3]]", "[[ii!]]", &xi[0], &xi[1]); - U("{}", "{s?i}", "foo", &xi[0]); - U("{\"foo\":1}", "{s?i}", "foo", &xi[0]); - U("{}", "{s?[ii]s?{s{si!}}}", "foo", &xi[0], &xi[1], "bar", "baz", "quux", &xi[2]); - U("{\"foo\":[1,2]}", "{s?[ii]s?{s{si!}}}", "foo", &xi[0], &xi[1], "bar", "baz", "quux", &xi[2]); - U("{\"bar\":{\"baz\":{\"quux\":15}}}", "{s?[ii]s?{s{si!}}}", "foo", &xi[0], &xi[1], "bar", "baz", "quux", &xi[2]); - U("{\"foo\":{\"bar\":4}}", "{s?{s?i}}", "foo", "bar", &xi[0]); - U("{\"foo\":{}}", "{s?{s?i}}", "foo", "bar", &xi[0]); - U("{}", "{s?{s?i}}", "foo", "bar", &xi[0]); - U("{\"foo\":42,\"baz\":45}", "{s?isi!}", "baz", &xi[0], "foo", &xi[1]); - U("{\"foo\":42}", "{s?isi!}", "baz", &xi[0], "foo", &xi[1]); - return 0; -} - -#endif - -#if 0 - - - /* Unpack the same item twice */ - j = json_pack("{s:s, s:i, s:b}", "foo", "bar", "baz", 42, "quux", 1); - if(!json_unpack_ex(j, &error, 0, "{s:s,s:s!}", "foo", &s, "foo", &s)) - fail("json_unpack object with strict validation failed"); - { - const char *possible_errors[] = { - "2 object item(s) left unpacked: baz, quux", - "2 object item(s) left unpacked: quux, baz" - }; - check_errors(possible_errors, 2, "", 1, 10, 10); - } - json_decref(j); - -#endif - \ No newline at end of file diff --git a/HAL-afb/hal-most-unicens/wrap-json.h b/HAL-afb/hal-most-unicens/wrap-json.h deleted file mode 100644 index b5eadf5..0000000 --- a/HAL-afb/hal-most-unicens/wrap-json.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - Copyright (C) 2016, 2017 "IoT.bzh" - - author: José Bollo - - 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. -*/ - -#pragma once - -#include -#include - -extern int wrap_json_get_error_position(int rc); -extern int wrap_json_get_error_code(int rc); -extern const char *wrap_json_get_error_string(int rc); - -extern int wrap_json_vpack(struct json_object **result, const char *desc, va_list args); -extern int wrap_json_pack(struct json_object **result, const char *desc, ...); - -extern int wrap_json_vunpack(struct json_object *object, const char *desc, va_list args); -extern int wrap_json_unpack(struct json_object *object, const char *desc, ...); -extern int wrap_json_vcheck(struct json_object *object, const char *desc, va_list args); -extern int wrap_json_check(struct json_object *object, const char *desc, ...); -extern int wrap_json_vmatch(struct json_object *object, const char *desc, va_list args); -extern int wrap_json_match(struct json_object *object, const char *desc, ...); - -extern void wrap_json_optarray_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure); -extern void wrap_json_array_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure); - -extern void wrap_json_optarray_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure); -extern void wrap_json_array_for_all(struct json_object *object, void (*callback)(void*,struct json_object*), void *closure); -extern void wrap_json_object_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure); -extern void wrap_json_optobject_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure); -extern void wrap_json_for_all(struct json_object *object, void (*callback)(void*,struct json_object*,const char*), void *closure); - - \ No newline at end of file diff --git a/HAL-afb/hal-most-unicens/wrap-json.md b/HAL-afb/hal-most-unicens/wrap-json.md deleted file mode 100644 index 3718b66..0000000 --- a/HAL-afb/hal-most-unicens/wrap-json.md +++ /dev/null @@ -1,306 +0,0 @@ -WRAP-JSON facility -================== - -The facility wrap-json is based on the pack/unpack API on the -libray jansson. The two chapters below are copied from the -documentation of jansson library copyrighted by Petri Lehtinen -(see at end). - -Building Values ---------------- - -This section describes functions that help to create, or *pack*, complex -JSON values, especially nested objects and arrays. Value building is -based on a *format string* that is used to tell the functions about the -expected arguments. - -For example, the format string `"i"` specifies a single integer value, -while the format string `"[ssb]"` or the equivalent `"[s, s, b]"` -specifies an array value with two strings and a boolean as its items: - - /* Create the JSON integer 42 */ - wrap_json_pack(&result, "i", 42); - - /* Create the JSON array ["foo", "bar", true] */ - wrap_json_pack(&result, "[ssb]", "foo", "bar", 1); - -Here's the full list of format specifiers. The type in parentheses -denotes the resulting JSON type, and the type in brackets (if any) -denotes the C type that is expected as the corresponding argument or -arguments. - -`s` (string) \[const char \*\] - -: Convert a null terminated UTF-8 string to a JSON string. - -`s?` (string) \[const char \*\] - -: Like `s`, but if the argument is *NULL*, output a JSON null value. - -`s*` (string) \[const char \*\] - -: Like `s`, but if the argument is *NULL*, do not output any value. - This format can only be used inside an object or an array. If used - inside an object, the corresponding key is additionally suppressed - when the value is omitted. See below for an example. - -`s#` (string) \[const char \*, int\] - -: Convert a UTF-8 buffer of a given length to a JSON string. - -`s%` (string) \[const char \*, size\_t\] - -: Like `s#` but the length argument is of type size\_t. - -`+` \[const char \*\] - -: Like `s`, but concatenate to the previous string. Only valid after - `s`, `s#`, `+` or `+#`. - -`+#` \[const char \*, int\] - -: Like `s#`, but concatenate to the previous string. Only valid after - `s`, `s#`, `+` or `+#`. - -`+%` (string) \[const char \*, size\_t\] - -: Like `+#` but the length argument is of type size\_t. - -`n` (null) - -: Output a JSON null value. No argument is consumed. - -`b` (boolean) \[int\] - -: Convert a C int to JSON boolean value. Zero is converted to `false` - and non-zero to `true`. - -`i` (integer) \[int\] - -: Convert a C int to JSON integer. - -`I` (integer) \[json\_int\_t\] - -: Convert a C json\_int\_t to JSON integer. - -`f` (real) \[double\] - -: Convert a C double to JSON real. - -`o` (any value) \[json\_t \*\] - -: Output any given JSON value as-is. If the value is added to an array - or object, the reference to the value passed to `o` is stolen by the - container. - -`O` (any value) \[json\_t \*\] - -: Like `o`, but the argument's reference count is incremented. This is - useful if you pack into an array or object and want to keep the - reference for the JSON value consumed by `O` to yourself. - -`o?`, `O?` (any value) \[json\_t \*\] - -: Like `o` and `O`, respectively, but if the argument is *NULL*, - output a JSON null value. - -`o*`, `O*` (any value) \[json\_t \*\] - -: Like `o` and `O`, respectively, but if the argument is *NULL*, do - not output any value. This format can only be used inside an object - or an array. If used inside an object, the corresponding key is - additionally suppressed. See below for an example. - -`[fmt]` (array) - -: Build an array with contents from the inner format string. `fmt` may - contain objects and arrays, i.e. recursive value building is - supported. - -`{fmt}` (object) - -: Build an object with contents from the inner format string `fmt`. - The first, third, etc. format specifier represent a key, and must be - a string (see `s`, `s#`, `+` and `+#` above), as object keys are - always strings. The second, fourth, etc. format specifier represent - a value. Any value may be an object or array, i.e. recursive value - building is supported. - -Whitespace, `:` and `,` are ignored. - -More examples: - - /* Build an empty JSON object */ - wrap_json_pack(&result, "{}"); - - /* Build the JSON object {"foo": 42, "bar": 7} */ - wrap_json_pack(&result, "{sisi}", "foo", 42, "bar", 7); - - /* Like above, ':', ',' and whitespace are ignored */ - wrap_json_pack(&result, "{s:i, s:i}", "foo", 42, "bar", 7); - - /* Build the JSON array [[1, 2], {"cool": true}] */ - wrap_json_pack(&result, "[[i,i],{s:b}]", 1, 2, "cool", 1); - - /* Build a string from a non-null terminated buffer */ - char buffer[4] = {'t', 'e', 's', 't'}; - wrap_json_pack(&result, "s#", buffer, 4); - - /* Concatenate strings together to build the JSON string "foobarbaz" */ - wrap_json_pack(&result, "s++", "foo", "bar", "baz"); - - /* Create an empty object or array when optional members are missing */ - wrap_json_pack(&result, "{s:s*,s:o*,s:O*}", "foo", NULL, "bar", NULL, "baz", NULL); - wrap_json_pack(&result, "[s*,o*,O*]", NULL, NULL, NULL); - -Parsing and Validating Values ------------------------------ - -This section describes functions that help to validate complex values -and extract, or *unpack*, data from them. Like building values -<apiref-pack>, this is also based on format strings. - -While a JSON value is unpacked, the type specified in the format string -is checked to match that of the JSON value. This is the validation part -of the process. In addition to this, the unpacking functions can also -check that all items of arrays and objects are unpacked. This check be -enabled with the format specifier `!` or by using the flag -`JSON_STRICT`. See below for details. - -Here's the full list of format specifiers. The type in parentheses -denotes the JSON type, and the type in brackets (if any) denotes the C -type whose address should be passed. - -`s` (string) \[const char \*\] - -: Convert a JSON string to a pointer to a null terminated UTF-8 - string. The resulting string is extracted by using - json\_string\_value() internally, so it exists as long as there are - still references to the corresponding JSON string. - -`s%` (string) \[const char \*, size\_t \*\] - -: Convert a JSON string to a pointer to a null terminated UTF-8 string - and its length. - -`n` (null) - -: Expect a JSON null value. Nothing is extracted. - -`b` (boolean) \[int\] - -: Convert a JSON boolean value to a C int, so that `true` is converted - to 1 and `false` to 0. - -`i` (integer) \[int\] - -: Convert a JSON integer to C int. - -`I` (integer) \[json\_int\_t\] - -: Convert a JSON integer to C json\_int\_t. - -`f` (real) \[double\] - -: Convert a JSON real to C double. - -`F` (integer or real) \[double\] - -: Convert a JSON number (integer or real) to C double. - -`o` (any value) \[json\_t \*\] - -: Store a JSON value with no conversion to a json\_t pointer. - -`O` (any value) \[json\_t \*\] - -: Like `O`, but the JSON value's reference count is incremented. - -`[fmt]` (array) - -: Convert each item in the JSON array according to the inner format - string. `fmt` may contain objects and arrays, i.e. recursive value - extraction is supported. - -`{fmt}` (object) - -: Convert each item in the JSON object according to the inner format - string `fmt`. The first, third, etc. format specifier represent a - key, and must be `s`. The corresponding argument to unpack functions - is read as the object key. The second fourth, etc. format specifier - represent a value and is written to the address given as the - corresponding argument. **Note** that every other argument is read - from and every other is written to. - - `fmt` may contain objects and arrays as values, i.e. recursive value - extraction is supported. - -`!` - -: This special format specifier is used to enable the check that all - object and array items are accessed, on a per-value basis. It must - appear inside an array or object as the last format specifier before - the closing bracket or brace. - -`*` - -: This special format specifier is the opposite of `!`. This is the default. - It must appear inside an array or object as the last format specifier - before the closing bracket or brace. - -Whitespace, `:` and `,` are ignored. - -Examples: - - /* root is the JSON integer 42 */ - int myint; - wrap_json_unpack(root, "i", &myint); - assert(myint == 42); - - /* root is the JSON object {"foo": "bar", "quux": true} */ - const char *str; - int boolean; - wrap_json_unpack(root, "{s:s, s:b}", "foo", &str, "quux", &boolean); - assert(strcmp(str, "bar") == 0 && boolean == 1); - - /* root is the JSON array [[1, 2], {"baz": null} */ - wrap_json_check(root, "[[i,i], {s:n}]", "baz"); - /* returns 0 for validation success, nothing is extracted */ - - /* root is the JSON array [1, 2, 3, 4, 5] */ - int myint1, myint2; - wrap_json_unpack(root, "[ii!]", &myint1, &myint2); - /* returns -1 for failed validation */ - - /* root is an empty JSON object */ - int myint = 0, myint2 = 0, myint3 = 0; - wrap_json_unpack(root, "{s?i, s?[ii]}", - "foo", &myint1, - "bar", &myint2, &myint3); - /* myint1, myint2 or myint3 is no touched as "foo" and "bar" don't exist */ - - -Copyright ---------- - -Copyright (c) 2009-2016 Petri Lehtinen - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - - \ No newline at end of file diff --git a/HAL-afb/hal-most-unicens/wrap_unicens.c b/HAL-afb/hal-most-unicens/wrap_unicens.c deleted file mode 100644 index 0cac86b..0000000 --- a/HAL-afb/hal-most-unicens/wrap_unicens.c +++ /dev/null @@ -1,256 +0,0 @@ -/* - * Copyright (C) 2017, Microchip Technology Inc. and its subsidiaries. - * Author Tobias Jahnke - * - * 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 -#define AFB_BINDING_VERSION 2 - -#include -#include -#include - -#include "wrap_unicens.h" -#include "wrap-json.h" - -typedef struct async_job_ { - wrap_ucs_result_cb_t result_fptr; - void *result_user_ptr; -} async_job_t; - -/* Subscribes to unicens2-binding events. - * \return Returns 0 if successful, otherwise != 0". - */ -extern int wrap_ucs_subscribe_sync() { - - json_object *j_response, *j_query = NULL; - int err; - - /* Build an empty JSON object */ - err = wrap_json_pack(&j_query, "{}"); - if (err) { - AFB_ERROR("Failed to create subscribe json object"); - goto OnErrorExit; - } - - err = afb_service_call_sync("UNICENS", "subscribe", j_query, &j_response); - if (err) { - AFB_ERROR("Fail subscribing to UNICENS events"); - goto OnErrorExit; - } - else { - AFB_NOTICE("Subscribed to UNICENS events, res=%s", json_object_to_json_string(j_response)); - json_object_put(j_response); - } - json_object_put(j_query); - j_query = NULL; - -OnErrorExit: - if (j_query) - json_object_put(j_query); - return err; -} - -/* Retrieves XML configuration path to initialize unicens2-binding. - * \param config_path Directory containing XML configuration file(s). - * \return Returns 0 if successful, otherwise != 0". - */ -extern int wrap_ucs_getconfig_sync(const char *config_path) { - json_object *j_response, *j_query = NULL; - int err; - - /* Build JSON object to retrieve UNICENS configuration */ - if (config_path != NULL) - err = wrap_json_pack(&j_query, "{s:s}", "cfgpath", config_path); - else - err = wrap_json_pack(&j_query, "{}"); - - if (err) { - AFB_ERROR("Failed to create listconfig json object"); - goto OnErrorExit; - } - - err = afb_service_call_sync("UNICENS", "listconfig", j_query, &j_response); - if (err) { - AFB_ERROR("Failed to call listconfig"); - goto OnErrorExit; - } - else { - AFB_NOTICE("UNICENS listconfig result, res=%s", json_object_to_json_string(j_response)); - json_object_put(j_response); - } - json_object_put(j_query); - j_query = NULL; - -OnErrorExit: - if (j_query) - json_object_put(j_query); - return err; -} - - -/* Initializes the unicens2-binding. - * \param file_name Path to XML configuration file or \c NULL for - * first found file in default path. - * \return Returns 0 if successful, otherwise != 0". - */ -extern int wrap_ucs_initialize_sync(const char *file_name) { - json_object *j_response, *j_query = NULL; - int err; - - /* Build JSON object to initialize UNICENS */ - if (file_name != NULL) - err = wrap_json_pack(&j_query, "{s:s}", "filename", file_name); - else - err = wrap_json_pack(&j_query, "{}"); - - if (err) { - AFB_ERROR("Failed to create initialize json object"); - goto OnErrorExit; - } - err = afb_service_call_sync("UNICENS", "initialise", j_query, &j_response); - if (err) { - AFB_ERROR("Failed to initialize UNICENS"); - goto OnErrorExit; - } - else { - AFB_NOTICE("Initialized UNICENS, res=%s", json_object_to_json_string(j_response)); - json_object_put(j_response); - } - - json_object_put(j_query); - j_query = NULL; - -OnErrorExit: - if (j_query) - json_object_put(j_query); - return err; -} - -/* Write I2C command to a network node. - * \param node Node address - * \param data_ptr Reference to command data - * \param data_sz Size of the command data. Valid values: 1..32. - * \return Returns 0 if successful, otherwise != 0". - */ -extern int wrap_ucs_i2cwrite_sync(uint16_t node, uint8_t *data_ptr, uint8_t data_sz) { - - json_object *j_response, *j_query, *j_array = NULL; - int err; - uint8_t cnt; - - j_query = json_object_new_object(); - j_array = json_object_new_array(); - - if (!j_query || !j_array) { - err = -1; - AFB_ERROR("Failed to create writei2c json objects"); - goto OnErrorExit; - } - - for (cnt = 0U; cnt < data_sz; cnt++) { - json_object_array_add(j_array, json_object_new_int(data_ptr[cnt])); - } - - json_object_object_add(j_query, "node", json_object_new_int(node)); - json_object_object_add(j_query, "data", j_array); - - err = afb_service_call_sync("UNICENS", "writei2c", j_query, &j_response); - - if (err) { - AFB_ERROR("Failed to call writei2c"); - goto OnErrorExit; - } - else { - AFB_NOTICE("Called writei2c, res=%s", "async"/*json_object_to_json_string(j_response)*/); - json_object_put(j_response); - } - - json_object_put(j_query); - j_query = NULL; - -OnErrorExit: - if (j_query) - json_object_put(j_query); - return err; -} - -/* ------------------ ASYNCHRONOUS API ---------------------------------------*/ - -static void wrap_ucs_i2cwrite_cb(void *closure, int status, struct json_object *j_result) { - - AFB_DEBUG("wrap_ucs_i2cwrite_cb: closure=%p status=%d, res=%s", closure, status, json_object_to_json_string(j_result)); - - if (closure) { - async_job_t *job_ptr = (async_job_t *)closure; - - if (job_ptr->result_fptr) - job_ptr->result_fptr(0U, job_ptr->result_user_ptr); - - free(closure); - } -} - -/* Write I2C command to a network node. - * \param node Node address - * \param data_ptr Reference to command data - * \param data_sz Size of the command data. Valid values: 1..32. - * \return Returns 0 if successful, otherwise != 0". - */ -extern int wrap_ucs_i2cwrite(uint16_t node, uint8_t *data_ptr, uint8_t data_sz, - wrap_ucs_result_cb_t result_fptr, - void *result_user_ptr) { - - json_object *j_query, *j_array = NULL; - async_job_t *job_ptr = NULL; - int err; - uint8_t cnt; - - j_query = json_object_new_object(); - j_array = json_object_new_array(); - - if (!j_query || !j_array) { - err = -1; - AFB_ERROR("Failed to create writei2c json objects"); - goto OnErrorExit; - } - - for (cnt = 0U; cnt < data_sz; cnt++) { - json_object_array_add(j_array, json_object_new_int(data_ptr[cnt])); - } - - json_object_object_add(j_query, "node", json_object_new_int(node)); - json_object_object_add(j_query, "data", j_array); - - job_ptr = malloc(sizeof(async_job_t)); - - if (!job_ptr) { - err = -1; - AFB_ERROR("Failed to create async job object"); - goto OnErrorExit; - } - - job_ptr->result_fptr = result_fptr; - job_ptr->result_user_ptr = result_user_ptr; - - afb_service_call("UNICENS", "writei2c", j_query, wrap_ucs_i2cwrite_cb, job_ptr); - err = 0; - j_query = NULL; - -OnErrorExit: - if (j_query) - json_object_put(j_query); - return err; -} diff --git a/HAL-afb/hal-most-unicens/wrap_unicens.h b/HAL-afb/hal-most-unicens/wrap_unicens.h deleted file mode 100644 index 268fb74..0000000 --- a/HAL-afb/hal-most-unicens/wrap_unicens.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) 2017, Microchip Technology Inc. and its subsidiaries. - * Author Tobias Jahnke - * - * 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. - * - */ - -#pragma once -#include - -/* Asynchronous API: result callback */ -typedef void (*wrap_ucs_result_cb_t)(uint8_t result, void *user_ptr); - -/* Asynchronous API: functions */ -extern int wrap_ucs_i2cwrite(uint16_t node, uint8_t *data_ptr, uint8_t data_sz, - wrap_ucs_result_cb_t result_fptr, - void *result_user_ptr); - -/* Synchronous API: functions */ -extern int wrap_ucs_subscribe_sync(); -extern int wrap_ucs_getconfig_sync(const char *config_path); -extern int wrap_ucs_initialize_sync(const char* file_name); -extern int wrap_ucs_i2cwrite_sync(uint16_t node, uint8_t *data_ptr, uint8_t data_sz); diff --git a/HAL-afb/hal-most-unicens/wrap_volume.c b/HAL-afb/hal-most-unicens/wrap_volume.c deleted file mode 100644 index cf70647..0000000 --- a/HAL-afb/hal-most-unicens/wrap_volume.c +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright (C) 2017, Microchip Technology Inc. and its subsidiaries. - * Author Tobias Jahnke - * - * 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 -#define AFB_BINDING_VERSION 2 - -#include -#include -#include -#include -#include - -#include "wrap_volume.h" -#include "wrap_unicens.h" -#include "libmostvolume.h" - -static int wrap_volume_service_timeout_cb(sd_event_source* source, - uint64_t timer __attribute__((__unused__)), - void *userdata __attribute__((__unused__))) { - - uint8_t ret; - - sd_event_source_unref(source); - ret = lib_most_volume_service(); - - if (ret != 0U) { - AFB_ERROR("lib_most_volume_service returns %d", ret); - } - - return 0; -} - -static void wrap_volume_service_cb(uint16_t timeout) { - uint64_t usec; - sd_event_now(afb_daemon_get_event_loop(), CLOCK_BOOTTIME, &usec); - sd_event_add_time( afb_daemon_get_event_loop(), NULL, CLOCK_MONOTONIC, - usec + (timeout*1000), - 250, - wrap_volume_service_timeout_cb, - NULL); -} - -/* Retrieves a new value adapted to a new maximum value. Minimum value is - * always zero. */ -static int wrap_volume_calculate(int value, int max_old, int max_new) { - - if (value > max_old) - value = max_old; - - value = (value * max_new) / max_old; /* calculate range: 0..255 */ - assert(value <= max_new); - - return value; -} - -extern int wrap_volume_init(void) { - - uint8_t ret = 0U; - lib_most_volume_init_t mv_init; - - mv_init.writei2c_cb = &wrap_ucs_i2cwrite; - mv_init.service_cb = wrap_volume_service_cb; - - ret = lib_most_volume_init(&mv_init); - - return ret * (-1); -} - -extern int wrap_volume_master(int volume) { - - int new_value, ret; - - new_value = wrap_volume_calculate(volume, 100, 255); - ret = lib_most_volume_set(LIB_MOST_VOLUME_MASTER, (uint8_t)new_value); - - if (ret != 0) { - AFB_ERROR("wrap_volume_master: volume library not ready."); - ret = ret * (-1); /* make return value negative */ - } - - return ret; -} - -extern int wrap_volume_pcm(int *volume_ptr, int volume_sz) { - - const int MAX_PCM_CHANNELS = 6; - int cnt, ret; - assert(volume_ptr != NULL); - assert(volume_sz <= MAX_PCM_CHANNELS); - - for (cnt = 0; cnt < volume_sz; cnt ++) { - - int new_value = wrap_volume_calculate(volume_ptr[cnt], 100, 255); - ret = lib_most_volume_set((enum lib_most_volume_channel_t)cnt, (uint8_t)new_value); - - if (ret != 0) { - AFB_ERROR("wrap_volume_pcm: volume library not ready."); - ret = ret * (-1); /* make return value negative */ - break; - } - } - - return ret; -} diff --git a/HAL-afb/hal-most-unicens/wrap_volume.h b/HAL-afb/hal-most-unicens/wrap_volume.h deleted file mode 100644 index 3378540..0000000 --- a/HAL-afb/hal-most-unicens/wrap_volume.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2017, Microchip Technology Inc. and its subsidiaries. - * Author Tobias Jahnke - * - * 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. - * - */ - -#pragma once -#include -#include - -extern int wrap_volume_init(void); -extern int wrap_volume_master(int volume); -extern int wrap_volume_pcm(int *volume_ptr, int volume_sz); diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index f0f58e2..75a5a25 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -57,23 +57,15 @@ hal-volramp.c hal-volume.c - - - - device_container.cpp - device_value.cpp - libmostvolume.cpp - setup.cpp - - + + HalPlugPcm.c + + hal_most_unicens.c wrap-json.c wrap_unicens.c wrap_volume.c - - HalPlugPcm.c - IntelHdaHAL.c @@ -389,117 +381,132 @@ Shared-Interface - HAL-afb/HAL-interface - build/HAL-afb/Unicens-USB + /usr/lib64/gcc/x86_64-suse-linux/5/include + build/HAL-afb/HAL-interface - - + + - HAL-afb/HAL-interface - build/HAL-afb/Unicens-USB + /usr/lib64/gcc/x86_64-suse-linux/5/include + build/HAL-afb/HAL-interface - + + - /opt/AGL/include/afb - HAL-afb/hal-most-unicens - /usr/include/json-c - Shared-Interface - HAL-afb/HAL-interface + HAL-afb/HAL-plugin /usr/lib64/gcc/x86_64-suse-linux/5/include - /opt/AGL/include - build/HAL-afb/hal-most-unicens + build/HAL-afb/HAL-plugin - - + + - HAL-afb/hal-most-unicens/ucs2-vol/inc - HAL-afb/hal-most-unicens/ucs2-vol/src + HAL-afb/HAL-plugin + /usr/lib64/gcc/x86_64-suse-linux/5/include + build/HAL-afb/HAL-plugin - + - - + + - HAL-afb/hal-most-unicens/ucs2-vol/src - HAL-afb/hal-most-unicens/ucs2-vol/inc + ../../../opt/include/afb + HAL-afb/HAL-plugin + ../../../opt/include/alsa + build/HAL-afb/HAL-plugin - + + CONTROL_CDEV_RX="/dev/inic-usb-crx" + CONTROL_CDEV_TX="/dev/inic-usb-ctx" + MAX_SND_CARD=16 + + - - + tool="0" + flavor2="3"> - HAL-afb/hal-most-unicens/ucs2-vol/src - HAL-afb/hal-most-unicens/ucs2-vol/inc + /opt/AGL/include/afb + HAL-afb/hal-most-unicens + /usr/include/json-c + Shared-Interface + HAL-afb/HAL-interface + build/HAL-afb/Unicens-USB - + - - + tool="0" + flavor2="3"> + - HAL-afb/hal-most-unicens/ucs2-vol/inc - HAL-afb/hal-most-unicens/ucs2-vol/src + HAL-afb/HAL-interface + build/HAL-afb/Unicens-USB - + - - + /opt/AGL/include/afb HAL-afb/hal-most-unicens - /usr/lib64/gcc/x86_64-suse-linux/5/include /usr/include/json-c + /usr/lib64/gcc/x86_64-suse-linux/5/include build/HAL-afb/hal-most-unicens - - + /opt/AGL/include/afb HAL-afb/hal-most-unicens - /usr/include/json-c /usr/lib64/gcc/x86_64-suse-linux/5/include + HAL-afb/hal-most-unicens/ucs2-vol/inc build/HAL-afb/hal-most-unicens - + + + + - + + + + + - /opt/AGL/include/afb - HAL-afb/hal-most-unicens + Shared-Interface + HAL-afb/HAL-interface /usr/lib64/gcc/x86_64-suse-linux/5/include - HAL-afb/hal-most-unicens/ucs2-vol/inc - build/HAL-afb/hal-most-unicens + /opt/AGL/include + build/HAL-afb/Unicens-USB + + + + + + + HAL-afb/HAL-interface + /usr/lib64/gcc/x86_64-suse-linux/5/include + build/HAL-afb/Unicens-USB @@ -512,7 +519,7 @@ - + @@ -918,6 +925,27 @@ + + + + + + + + + + + + @@ -954,27 +982,6 @@ - - - - - - - - - - - - @@ -1062,6 +1069,18 @@ + + + + HAL-afb/Scarlett-Focusrite + /usr/include/json-c + Shared-Interface + HAL-afb/HAL-interface + ../../../opt/include + build/HAL-afb/Scarlett-Focusrite + + + @@ -1110,18 +1129,6 @@ - - - - HAL-afb/Scarlett-Focusrite - /usr/include/json-c - Shared-Interface - HAL-afb/HAL-interface - ../../../opt/include - build/HAL-afb/Scarlett-Focusrite - - - -- cgit 1.2.3-korg From 4aec17e1d42c1b0f3f628342bf24120299a0c819 Mon Sep 17 00:00:00 2001 From: Tobias Jahnke Date: Fri, 11 Aug 2017 13:52:51 +0200 Subject: Adds readme for HAL MOST UNICENS --- HAL-afb/HAL_MOST_UNICENS/README.md | 51 +++++++++++++++++++++++++++++ HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c | 7 ++-- 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 HAL-afb/HAL_MOST_UNICENS/README.md diff --git a/HAL-afb/HAL_MOST_UNICENS/README.md b/HAL-afb/HAL_MOST_UNICENS/README.md new file mode 100644 index 0000000..a0cc190 --- /dev/null +++ b/HAL-afb/HAL_MOST_UNICENS/README.md @@ -0,0 +1,51 @@ + +# HAL for MOST Sound Card + +## Introduction +This Hardware Abstraction Layer (HAL) is intended to control MOST audio via +standard ALSA mixer controls. Therefore the HAL add's controls to the MOST sound +card. Modification of the mixer controls is mapped to control commands which are +send to MOST nodes. + +The HAL requires to access the UNICENS V2 binding which is setting up the MOST +network and the MOST node. + +Please check the following required components: +* MOST Linux Driver +* User must be member of "audio" group +* UNICENS V2 binding +* [K2L MOST150 Audio 5.1 Kit](https://www.k2l.de/products/74/MOST150%20Audio%205.1%20Kit/) + +## Possible Modifications +Check if you need to adapt the default path for UNICENS configuration, e.g. +``` +#define XML_CONFIG_PATH "/opt/AGL/unicens2-binding/config_multichannel_audio_kit.xml" +``` + +Check if you need to adapt the name of the MOST sound card. +``` +#define ALSA_CARD_NAME "Microchip MOST:1" +``` + +You can check your MOST sound card name by calling ```aplay -l```, e.g. +``` +aplay -l +**** List of PLAYBACK Hardware Devices **** +card 0: PCH [HDA Intel PCH], device 0: 92HD90BXX Analog [92HD90BXX Analog] + Subdevices: 1/1 + Subdevice #0: subdevice #0 +card 1: ep016ch [Microchip MOST:1], device 0: ep01-6ch [] + Subdevices: 1/1 + Subdevice #0: subdevice #0 +card 2: ep022ch [Microchip MOST:2], device 0: ep02-2ch [] + Subdevices: 1/1 + Subdevice #0: subdevice #0 + +``` +Choose the first sound card with 6 channels, e.g. if you see ```ep01-6ch``` just +take ```Microchip MOST:1```. + +If you get messed up with card enumeration the following action may help: +- Unplug your MOST USB hardware from target +- Call ```sudo rm /var/lib/alsa/asound.state``` +- Connect MOST USB hardware after reboot or restart of ALSA diff --git a/HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c b/HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c index a5971fb..2cffe8d 100644 --- a/HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c +++ b/HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c @@ -23,6 +23,9 @@ #include "wrap_unicens.h" #include "wrap_volume.h" +#define XML_CONFIG_PATH "/home/agluser/DEVELOPMENT/AGL/BINDING/unicens2-binding/data/config_multichannel_audio_kit.xml" +#define ALSA_CARD_NAME "Microchip MOST:1" + #define PCM_MAX_CHANNELS 6 static int master_volume; @@ -84,7 +87,7 @@ STATIC alsaHalMapT alsaHalMap[]= { /* HAL sound card mapping info */ STATIC alsaHalSndCardT alsaHalSndCard = { - .name = "Microchip MOST:1", /* WARNING: name MUST match with 'aplay -l' */ + .name = ALSA_CARD_NAME, /* WARNING: name MUST match with 'aplay -l' */ .info = "HAL for MICROCHIP MOST sound card controlled by UNICENS binding", .ctls = alsaHalMap, .volumeCB = NULL, /* use default volume normalization function */ @@ -113,7 +116,7 @@ STATIC int unicens_service_init() { goto OnErrorExit; } - err = wrap_ucs_initialize_sync("/home/agluser/DEVELOPMENT/AGL/BINDING/unicens2-binding/data/config_multichannel_audio_kit.xml"); + err = wrap_ucs_initialize_sync(XML_CONFIG_PATH); if (err) { AFB_ERROR("Failed to initialize UNICENS binding"); goto OnErrorExit; -- cgit 1.2.3-korg From 999340f641477f94e3730e53d8a8bf39fe93b0c4 Mon Sep 17 00:00:00 2001 From: Tobias Jahnke Date: Tue, 15 Aug 2017 13:34:17 +0200 Subject: implemented listconfig in UNICENS wrapper --- HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c | 23 +++++- HAL-afb/HAL_MOST_UNICENS/wrap_unicens.c | 110 ++++++++++++++++++++++++---- HAL-afb/HAL_MOST_UNICENS/wrap_unicens.h | 2 +- conf.d/cmake/config.cmake | 2 + nbproject/configurations.xml | 88 +++++++++++++++++----- 5 files changed, 187 insertions(+), 38 deletions(-) diff --git a/HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c b/HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c index 2cffe8d..7240fe0 100644 --- a/HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c +++ b/HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c @@ -23,10 +23,12 @@ #include "wrap_unicens.h" #include "wrap_volume.h" -#define XML_CONFIG_PATH "/home/agluser/DEVELOPMENT/AGL/BINDING/unicens2-binding/data/config_multichannel_audio_kit.xml" -#define ALSA_CARD_NAME "Microchip MOST:1" +#ifndef UCS2_CFG_PATH +# define UCS2_CFG_PATH "/home/agluser/DEVELOPMENT/AGL/BINDING/unicens2-binding/data" +#endif -#define PCM_MAX_CHANNELS 6 +#define ALSA_CARD_NAME "Microchip MOST:1" +#define PCM_MAX_CHANNELS 6 static int master_volume; static json_bool master_switch; @@ -96,6 +98,7 @@ STATIC alsaHalSndCardT alsaHalSndCard = { /* initializes ALSA sound card, UNICENS API */ STATIC int unicens_service_init() { int err = 0; + char *config_file = NULL; AFB_NOTICE("Initializing HAL-MOST-UNICENS-BINDING"); err = halServiceInit(afbBindingV2.api, &alsaHalSndCard); @@ -110,13 +113,25 @@ STATIC int unicens_service_init() { goto OnErrorExit; } + err = wrap_ucs_getconfig_sync(UCS2_CFG_PATH, &config_file); + if (err || (config_file == NULL)) { + AFB_ERROR("Failed to retrieve configuration"); + goto OnErrorExit; + } + else { + AFB_NOTICE("Found configuration: %s", config_file); + } + err = wrap_ucs_subscribe_sync(); if (err) { AFB_ERROR("Failed to subscribe to UNICENS binding"); goto OnErrorExit; } - err = wrap_ucs_initialize_sync(XML_CONFIG_PATH); + err = wrap_ucs_initialize_sync(config_file); + free(config_file); + config_file = NULL; + if (err) { AFB_ERROR("Failed to initialize UNICENS binding"); goto OnErrorExit; diff --git a/HAL-afb/HAL_MOST_UNICENS/wrap_unicens.c b/HAL-afb/HAL_MOST_UNICENS/wrap_unicens.c index 0cac86b..dc82568 100644 --- a/HAL-afb/HAL_MOST_UNICENS/wrap_unicens.c +++ b/HAL-afb/HAL_MOST_UNICENS/wrap_unicens.c @@ -21,14 +21,18 @@ #include #include #include - #include "wrap_unicens.h" #include "wrap-json.h" typedef struct async_job_ { - wrap_ucs_result_cb_t result_fptr; + wrap_ucs_result_cb_t result_fptr; void *result_user_ptr; -} async_job_t; +} async_job_t; + +typedef struct parse_result_ { + int done; + char *str_result; +} parse_result_t; /* Subscribes to unicens2-binding events. * \return Returns 0 if successful, otherwise != 0". @@ -63,19 +67,78 @@ OnErrorExit: return err; } +/* Checks if name ends with a given letter. */ +static int wrap_ucs_string_ends_with(const char *name, char letter) { + + int result = 0; + size_t len = strlen(name); + + if (len > 0) { + + if (name[len] == letter) { + result = 1; + } + } + + return result; +} + +/* Callback for iteration through found files. Marks search as "done" as + * soon as the search pattern "kit.xml" matches. + * \param closure User reference. Points to parse_result_t. + * \param j_obj Points to json object within array. + */ +static void wrap_ucs_find_xml_cb(void *closure, struct json_object *j_obj) { + + const char *dir, *name; + parse_result_t *result; + + if (!closure) + return; + + result = (parse_result_t *)closure; + if (result->done) + return; + + wrap_json_unpack(j_obj, "{s:s, s:s}", "dirpath", &dir, "basename", &name); + AFB_DEBUG("Found file: %s", name); + + if(strstr(name, "kit.xml") != NULL) { + size_t sz = strlen(dir)+strlen(name)+10; + char * full_path = malloc(sz); + + strncpy(full_path, dir, sz); + + if (!wrap_ucs_string_ends_with(dir, '/')) + strncat(full_path, "/", sz); + + strncat(full_path, name, sz); + AFB_DEBUG("Found XML file: %s", full_path); + + result->done = 1; + result->str_result = full_path; + } +} + /* Retrieves XML configuration path to initialize unicens2-binding. - * \param config_path Directory containing XML configuration file(s). + * \param config_path Directory containing XML configuration file(s). + * \param file_found Points to found file_path or NULL if not found. + * Memory must be released by calling free(). * \return Returns 0 if successful, otherwise != 0". */ -extern int wrap_ucs_getconfig_sync(const char *config_path) { - json_object *j_response, *j_query = NULL; - int err; +extern int wrap_ucs_getconfig_sync(const char *config_path, char **file_found) { + + int err = 0; + json_object *j_response, *j_query, *j_paths = NULL; + parse_result_t result = {.done = 0, .str_result = NULL}; + + *file_found = NULL; /* Build JSON object to retrieve UNICENS configuration */ - if (config_path != NULL) - err = wrap_json_pack(&j_query, "{s:s}", "cfgpath", config_path); - else + if ((config_path == NULL) || (strcmp(config_path, "") == 0)) err = wrap_json_pack(&j_query, "{}"); + else + err = wrap_json_pack(&j_query, "{s:s}", "cfgpath", config_path); if (err) { AFB_ERROR("Failed to create listconfig json object"); @@ -88,7 +151,25 @@ extern int wrap_ucs_getconfig_sync(const char *config_path) { goto OnErrorExit; } else { - AFB_NOTICE("UNICENS listconfig result, res=%s", json_object_to_json_string(j_response)); + AFB_DEBUG("UNICENS listconfig result, res=%s", json_object_to_json_string(j_response)); + + if (json_object_object_get_ex(j_response, "response", &j_paths)) { + + AFB_DEBUG("UNICENS listconfig result, paths=%s", json_object_to_json_string(j_paths)); + + wrap_json_optarray_for_all(j_paths, &wrap_ucs_find_xml_cb, &result); + + if (result.done) { + *file_found = strdup(result.str_result); + free(result.str_result); + result.str_result = NULL; + } + else { + AFB_NOTICE("wrap_ucs_getconfig_sync, search pattern does not match for paths=%s", json_object_to_json_string(j_paths)); + err = -1; + } + } + json_object_put(j_response); } json_object_put(j_query); @@ -100,7 +181,6 @@ OnErrorExit: return err; } - /* Initializes the unicens2-binding. * \param file_name Path to XML configuration file or \c NULL for * first found file in default path. @@ -170,11 +250,11 @@ extern int wrap_ucs_i2cwrite_sync(uint16_t node, uint8_t *data_ptr, uint8_t data err = afb_service_call_sync("UNICENS", "writei2c", j_query, &j_response); if (err) { - AFB_ERROR("Failed to call writei2c"); + AFB_ERROR("Failed to call writei2c_sync"); goto OnErrorExit; } else { - AFB_NOTICE("Called writei2c, res=%s", "async"/*json_object_to_json_string(j_response)*/); + AFB_INFO("Called writei2c_sync, res=%s", json_object_to_json_string(j_response)); json_object_put(j_response); } @@ -191,7 +271,7 @@ OnErrorExit: static void wrap_ucs_i2cwrite_cb(void *closure, int status, struct json_object *j_result) { - AFB_DEBUG("wrap_ucs_i2cwrite_cb: closure=%p status=%d, res=%s", closure, status, json_object_to_json_string(j_result)); + AFB_INFO("wrap_ucs_i2cwrite_cb: closure=%p status=%d, res=%s", closure, status, json_object_to_json_string(j_result)); if (closure) { async_job_t *job_ptr = (async_job_t *)closure; diff --git a/HAL-afb/HAL_MOST_UNICENS/wrap_unicens.h b/HAL-afb/HAL_MOST_UNICENS/wrap_unicens.h index 268fb74..c28d13a 100644 --- a/HAL-afb/HAL_MOST_UNICENS/wrap_unicens.h +++ b/HAL-afb/HAL_MOST_UNICENS/wrap_unicens.h @@ -29,6 +29,6 @@ extern int wrap_ucs_i2cwrite(uint16_t node, uint8_t *data_ptr, uint8_t data_sz, /* Synchronous API: functions */ extern int wrap_ucs_subscribe_sync(); -extern int wrap_ucs_getconfig_sync(const char *config_path); +extern int wrap_ucs_getconfig_sync(const char *config_path, char **file_found); extern int wrap_ucs_initialize_sync(const char* file_name); extern int wrap_ucs_i2cwrite_sync(uint16_t node, uint8_t *data_ptr, uint8_t data_sz); diff --git a/conf.d/cmake/config.cmake b/conf.d/cmake/config.cmake index 73a00f6..9719711 100644 --- a/conf.d/cmake/config.cmake +++ b/conf.d/cmake/config.cmake @@ -83,6 +83,8 @@ set (PKG_REQUIRED_LIST add_compile_options(-DCTL_PLUGIN_MAGIC=2468013579) add_compile_options(-DCONTROL_PLUGIN_PATH="${CMAKE_BINARY_DIR}:${BINDINGS_INSTALL_DIR}/ctlplug:/usr/lib/afb/ctlplug") + add_compile_options(-DUCS2_CFG_PATH="../../unicens2-binding/data:/etc/default/ucs:../data:./data") + # Print a helper message when every thing is finished # ---------------------------------------------------- set(CLOSING_MESSAGE "Debug in ./buid: afb-daemon --port=1234 --ldpaths=. --workdir=. --roothttp=../htdocs --tracereq=common --token='' --verbose") diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 75a5a25..f7241d3 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -61,6 +61,14 @@ HalPlugPcm.c + + + device_container.cpp + device_value.cpp + libmostvolume.cpp + setup.cpp + + hal_most_unicens.c wrap-json.c wrap_unicens.c @@ -387,7 +395,7 @@ - + /usr/lib64/gcc/x86_64-suse-linux/5/include build/HAL-afb/HAL-interface @@ -395,7 +403,7 @@ - + HAL-afb/HAL-plugin /usr/lib64/gcc/x86_64-suse-linux/5/include @@ -404,7 +412,7 @@ - + HAL-afb/HAL-plugin /usr/lib64/gcc/x86_64-suse-linux/5/include @@ -413,7 +421,7 @@ - + ../../../opt/include/afb HAL-afb/HAL-plugin @@ -433,7 +441,7 @@ flavor2="3"> /opt/AGL/include/afb - HAL-afb/hal-most-unicens + HAL-afb/HAL_MOST_UNICENS /usr/include/json-c Shared-Interface HAL-afb/HAL-interface @@ -441,11 +449,55 @@ + + + + HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc + HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src + + + + + + + HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src + HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc + + + + + + + HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src + HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc + + + + + + + HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc + HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src + + + - + HAL-afb/HAL-interface build/HAL-afb/Unicens-USB @@ -458,10 +510,10 @@ flavor2="3"> /opt/AGL/include/afb - HAL-afb/hal-most-unicens - /usr/include/json-c + HAL-afb/HAL_MOST_UNICENS /usr/lib64/gcc/x86_64-suse-linux/5/include - build/HAL-afb/hal-most-unicens + /usr/include/json-c + build/HAL-afb/HAL_MOST_UNICENS @@ -469,29 +521,29 @@ ex="false" tool="0" flavor2="3"> - + /opt/AGL/include/afb - HAL-afb/hal-most-unicens + HAL-afb/HAL_MOST_UNICENS /usr/lib64/gcc/x86_64-suse-linux/5/include - HAL-afb/hal-most-unicens/ucs2-vol/inc - build/HAL-afb/hal-most-unicens + HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc + build/HAL-afb/HAL_MOST_UNICENS - + - + - + Shared-Interface HAL-afb/HAL-interface @@ -502,7 +554,7 @@ - + HAL-afb/HAL-interface /usr/lib64/gcc/x86_64-suse-linux/5/include @@ -519,7 +571,7 @@ - + -- cgit 1.2.3-korg From c01c0a73884fc39791862a0a3b5d1759a52c04bf Mon Sep 17 00:00:00 2001 From: Tobias Jahnke Date: Wed, 16 Aug 2017 15:54:22 +0200 Subject: merge changes for new audio interface --- HAL-afb/HAL_MOST_UNICENS/CMakeLists.txt | 2 +- HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c | 10 +- nbproject/configurations.xml | 303 +++++++++------------------- 3 files changed, 104 insertions(+), 211 deletions(-) diff --git a/HAL-afb/HAL_MOST_UNICENS/CMakeLists.txt b/HAL-afb/HAL_MOST_UNICENS/CMakeLists.txt index e532191..b7740f0 100644 --- a/HAL-afb/HAL_MOST_UNICENS/CMakeLists.txt +++ b/HAL-afb/HAL_MOST_UNICENS/CMakeLists.txt @@ -35,7 +35,7 @@ PROJECT_TARGET_ADD(hal-most-unicens) # Library dependencies (include updates automatically) TARGET_LINK_LIBRARIES(${TARGET_NAME} hal-interface - audio-interface + audio-common ucs2-volume ) diff --git a/HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c b/HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c index 7240fe0..4405e61 100644 --- a/HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c +++ b/HAL-afb/HAL_MOST_UNICENS/hal_most_unicens.c @@ -16,10 +16,10 @@ * */ #define _GNU_SOURCE +#include #include "hal-interface.h" -#include "audio-interface.h" +#include "audio-common.h" #include "wrap-json.h" -#include "string.h" #include "wrap_unicens.h" #include "wrap_volume.h" @@ -34,7 +34,7 @@ static int master_volume; static json_bool master_switch; static int pcm_volume[PCM_MAX_CHANNELS]; -void unicens_master_vol_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { +void unicens_master_vol_cb(halCtlsTagT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { const char *j_str = json_object_to_json_string(j_obj); @@ -47,7 +47,7 @@ void unicens_master_vol_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* hand } } -void unicens_master_switch_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { +void unicens_master_switch_cb(halCtlsTagT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { const char *j_str = json_object_to_json_string(j_obj); @@ -59,7 +59,7 @@ void unicens_master_switch_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* h } } -void unicens_pcm_vol_cb(halCtlsEnumT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { +void unicens_pcm_vol_cb(halCtlsTagT tag, alsaHalCtlMapT *control, void* handle, json_object *j_obj) { const char *j_str = json_object_to_json_string(j_obj); diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index f7241d3..f045173 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -60,20 +60,6 @@ HalPlugPcm.c - - - - device_container.cpp - device_value.cpp - libmostvolume.cpp - setup.cpp - - - hal_most_unicens.c - wrap-json.c - wrap_unicens.c - wrap_volume.c - IntelHdaHAL.c @@ -92,8 +78,23 @@ HighLevelApiConf.c HighLevelBinding.c - - audio-interface.c + + + ConnectionInfo.cpp + Console.c + MacAddr.cpp + MostIpc.cpp + MostMsg.cpp + MostMsgTx.cpp + MsgAddr.cpp + MsgFilter.cpp + Shadow_NetworkMaster.cpp + Thread.cpp + + DeviceContainer.cpp + DeviceValue.cpp + Mediator.cpp + libmostvolume.cpp Shared-Interface - /usr/lib64/gcc/x86_64-suse-linux/5/include - build/HAL-afb/HAL-interface + HAL-afb/HAL-interface + build/HAL-afb/Unicens-USB - + - /usr/lib64/gcc/x86_64-suse-linux/5/include - build/HAL-afb/HAL-interface + HAL-afb/HAL-interface + build/HAL-afb/Unicens-USB - - - - HAL-afb/HAL-plugin - /usr/lib64/gcc/x86_64-suse-linux/5/include - build/HAL-afb/HAL-plugin - + + - - - - HAL-afb/HAL-plugin - /usr/lib64/gcc/x86_64-suse-linux/5/include - build/HAL-afb/HAL-plugin - + + - - - - ../../../opt/include/afb - HAL-afb/HAL-plugin - ../../../opt/include/alsa - build/HAL-afb/HAL-plugin - - - CONTROL_CDEV_RX="/dev/inic-usb-crx" - CONTROL_CDEV_TX="/dev/inic-usb-ctx" - MAX_SND_CARD=16 - - + - - - /opt/AGL/include/afb - HAL-afb/HAL_MOST_UNICENS - /usr/include/json-c - Shared-Interface - HAL-afb/HAL-interface - build/HAL-afb/Unicens-USB - - + - - - - HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc - HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src - - + - - - - HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src - HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc - - + flavor2="4"> - - - - HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src - HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc - - + - - - - HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc - HAL-afb/HAL_MOST_UNICENS/ucs2-vol/src - - + - - - - HAL-afb/HAL-interface - build/HAL-afb/Unicens-USB - - + - - - /opt/AGL/include/afb - HAL-afb/HAL_MOST_UNICENS - /usr/lib64/gcc/x86_64-suse-linux/5/include - /usr/include/json-c - build/HAL-afb/HAL_MOST_UNICENS - - + - - - - /opt/AGL/include/afb - HAL-afb/HAL_MOST_UNICENS - /usr/lib64/gcc/x86_64-suse-linux/5/include - HAL-afb/HAL_MOST_UNICENS/ucs2-vol/inc - build/HAL-afb/HAL_MOST_UNICENS - - + - - - + - + + - - + tool="1" + flavor2="4"> - - - - Shared-Interface - HAL-afb/HAL-interface - /usr/lib64/gcc/x86_64-suse-linux/5/include - /opt/AGL/include - build/HAL-afb/Unicens-USB - - + - - + + + + - HAL-afb/HAL-interface - /usr/lib64/gcc/x86_64-suse-linux/5/include - build/HAL-afb/Unicens-USB + build/Alsa-Plugin/Alsa-Policy-Hook - - - - - - - - - - - - + + CONTROL_DOSCRIPT_PRE="doscript" + NATIVE_LINUX + PIC + policy_hook_cb_EXPORTS + @@ -977,27 +861,6 @@ - - - - - - - - - - - - @@ -1053,8 +916,50 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ../../../opt/include/afb + Alsa-Plugin/Alsa-Hook-Callback + ../../../opt/include/alsa + /usr/include/json-c + ../../../opt/include + build/Alsa-Plugin/Alsa-Hook-Callback + @@ -1121,18 +1026,6 @@ - - - - HAL-afb/Scarlett-Focusrite - /usr/include/json-c - Shared-Interface - HAL-afb/HAL-interface - ../../../opt/include - build/HAL-afb/Scarlett-Focusrite - - - -- cgit 1.2.3-korg From b5e786d60f3fda609a7c66bcbf258137d08e040b Mon Sep 17 00:00:00 2001 From: Tobias Jahnke Date: Wed, 16 Aug 2017 16:12:04 +0200 Subject: updated submodules --- conf.d/app-templates | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf.d/app-templates b/conf.d/app-templates index dee5836..8967162 160000 --- a/conf.d/app-templates +++ b/conf.d/app-templates @@ -1 +1 @@ -Subproject commit dee58363ddb98f8e63239035f1a8f1ab151c5e96 +Subproject commit 8967162dd12bce89f9ae27f5c9bce7b78624e3fd -- cgit 1.2.3-korg