From e94c8800c515dc64a7d7ae275e607e23591b8498 Mon Sep 17 00:00:00 2001
From: Tobias Jahnke <tjahnk@users.noreply.github.com>
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 @@
         </df>
         <df name="hal-most-unicens">
           <in>hal_most_unicens.c</in>
+          <in>wrap-json.c</in>
         </df>
         <df name="HAL-plugin">
           <in>HalPlugPcm.c</in>
@@ -396,6 +397,29 @@
             tool="0"
             flavor2="3">
         <cTool flags="3">
+          <incDir>
+            <pElem>/opt/AGL/include/afb</pElem>
+            <pElem>HAL-afb/hal-most-unicens</pElem>
+            <pElem>/usr/include/json-c</pElem>
+            <pElem>Shared-Interface</pElem>
+            <pElem>HAL-afb/HAL-interface</pElem>
+            <pElem>/usr/lib64/gcc/x86_64-suse-linux/5/include</pElem>
+            <pElem>/opt/AGL/include</pElem>
+            <pElem>build/HAL-afb/hal-most-unicens</pElem>
+          </incDir>
+        </cTool>
+      </item>
+      <item path="HAL-afb/hal-most-unicens/wrap-json.c"
+            ex="false"
+            tool="0"
+            flavor2="3">
+        <cTool flags="3">
+          <incDir>
+            <pElem>HAL-afb/hal-most-unicens</pElem>
+            <pElem>/usr/lib64/gcc/x86_64-suse-linux/5/include</pElem>
+            <pElem>/usr/include/json-c</pElem>
+            <pElem>build/HAL-afb/hal-most-unicens</pElem>
+          </incDir>
         </cTool>
       </item>
       <item path="HighLevel-afb/HighLevelApiConf.c" ex="false" tool="0" flavor2="3">
@@ -617,18 +641,6 @@
           </preprocessorList>
         </cTool>
       </folder>
-      <folder path="0/HAL-afb/hal-most-unicens">
-        <cTool>
-          <incDir>
-            <pElem>HAL-afb/Scarlett-Focusrite</pElem>
-            <pElem>/usr/include/json-c</pElem>
-            <pElem>Shared-Interface</pElem>
-            <pElem>HAL-afb/HAL-interface</pElem>
-            <pElem>../../../opt/include</pElem>
-            <pElem>build/HAL-afb/Scarlett-Focusrite</pElem>
-          </incDir>
-        </cTool>
-      </folder>
       <folder path="0/HighLevel-afb">
         <cTool>
           <incDir>
-- 
cgit