UcsXml component use libxml2 instead of libmxml
[apps/agl-service-unicens.git] / ucs2-afb / ucs_binding.c
index e7a940b..b8a1ba8 100644 (file)
@@ -61,6 +61,7 @@ typedef struct {
   CdevData_t rx;
   CdevData_t tx;
   UCSI_Data_t ucsiData;
+  UcsXmlVal_t* ucsConfig;
 } ucsContextT;
 
 typedef struct {
@@ -68,7 +69,7 @@ typedef struct {
 
 } EventData_t;
 
-static ucsContextT *ucsContextS;
+static ucsContextT *ucsContextS = NULL;
 static EventData_t *eventData = NULL;
 
 PUBLIC void UcsXml_CB_OnError(const char format[], uint16_t vargsCnt, ...) {
@@ -111,6 +112,7 @@ STATIC int onTimerCB (sd_event_source* source,uint64_t timer, void* pTag) {
 
 void UCSI_CB_OnNetworkState(void *pTag, bool isAvailable, uint16_t packetBandwidth, uint8_t amountOfNodes)
 {
+    AFB_NOTICE ("Network is available=%d, bw=%d, nodeCnt=%d", isAvailable, packetBandwidth, amountOfNodes);
 }
 
 /* UCS2 Interface Timer Callback */
@@ -138,8 +140,7 @@ void UCSI_CB_OnUserMessage(void *pTag, bool isError, const char format[],
     va_start(argptr, vargsCnt);
     vsprintf(outbuf, format, argptr);
     va_end(argptr);
-    if (isError)
-        AFB_NOTICE ("%s",outbuf);
+    AFB_NOTICE ("%s",outbuf);
 }
 
 /** UCSI_Service cannot be called directly within UNICENS context, need to service stack through mainloop */
@@ -183,6 +184,14 @@ PUBLIC void UCSI_CB_OnTxRequest(void *pTag, const uint8_t *pData, uint32_t len)
     }
 }
 
+/** UcsXml_FreeVal be called directly within UNICENS context, need to service stack through mainloop */
+STATIC int OnStopCB (sd_event_source *source, uint64_t usec, void *pTag) {
+    if (NULL != ucsContextS && NULL != ucsContextS->ucsConfig) {
+        UcsXml_FreeVal(ucsContextS->ucsConfig);
+        ucsContextS->ucsConfig = NULL;
+    }
+}
+
 /**
  * \brief Callback when UNICENS instance has been stopped.
  * \note This event can be used to free memory holding the resources
@@ -191,8 +200,9 @@ PUBLIC void UCSI_CB_OnTxRequest(void *pTag, const uint8_t *pData, uint32_t len)
  * \param pTag - Pointer given by the integrator by UCSI_Init
  */
 void UCSI_CB_OnStop(void *pTag) {
-    AFB_NOTICE ("UNICENS stopped");
-
+   AFB_NOTICE ("UNICENS stopped");
+   /* push an asynchronous request for loopback to call UcsXml_FreeVal */
+   sd_event_add_time(afb_daemon_get_event_loop(), NULL, CLOCK_MONOTONIC, 0, 0, OnStopCB, pTag);
 }
 
 /** This callback will be raised, when ever an applicative message on the control channel arrived */
@@ -205,10 +215,12 @@ void UCSI_CB_OnAmsMessageReceived(void *pTag)
 
 void UCSI_CB_OnRouteResult(void *pTag, uint16_t routeId, bool isActive, uint16_t connectionLabel)
 {
+    AFB_NOTICE ("Route 0x%X is active=%d, connection label=0x%X", routeId, isActive, connectionLabel);
 }
 
 void UCSI_CB_OnGpioStateChange(void *pTag, uint16_t nodeAddress, uint8_t gpioPinId, bool isHighState)
 {
+    AFB_NOTICE ("GPIO state of node 0x%X changed, pin=%d isHigh=%d", nodeAddress, gpioPinId, isHighState);
 }
 
 PUBLIC void UCSI_CB_OnMgrReport(void *pTag, Ucs_MgrReport_t code, uint16_t nodeAddress, Ucs_Rm_Node_t *pNode){
@@ -320,7 +332,7 @@ STATIC UcsXmlVal_t* ParseFile(const char *filename) {
     ssize_t readSize;
     int fdHandle ;
     struct stat fdStat;
-    UcsXmlVal_t* ucsConfig;
+    UcsXmlVal_t *ucsConfig = NULL;
 
     fdHandle = open(filename, O_RDONLY);
     if (fdHandle <= 0) {
@@ -345,6 +357,7 @@ STATIC UcsXmlVal_t* ParseFile(const char *filename) {
         AFB_ERROR("File XML invalid: '%s'", filename);
         goto OnErrorExit;
     }
+    AFB_NOTICE ("Parsing result: %d Nodes, %d Scripts, Ethernet Bandwith %d bytes = %.2f MBit/s", ucsConfig->nodSize, ucsConfig->routesSize, ucsConfig->packetBw, (48 * 8 * ucsConfig->packetBw / 1000.0));
 
     return (ucsConfig);
 
@@ -353,15 +366,14 @@ STATIC UcsXmlVal_t* ParseFile(const char *filename) {
 }
 
 PUBLIC int StartConfiguration(const char *filename) {
-    static UcsXmlVal_t *ucsConfig;
-    static ucsContextT ucsContext;
+    static ucsContextT ucsContext = { 0 };
 
     sd_event_source *evtSource;
     int err;
 
     /* Read and parse XML file */
-    ucsConfig = ParseFile(filename);
-    if (NULL == ucsConfig) {
+    ucsContext.ucsConfig = ParseFile(filename);
+    if (NULL == ucsContext.ucsConfig) {
         AFB_ERROR ("Cannot access or load file: '%s'", filename);
         goto OnErrorExit;
     }
@@ -388,7 +400,7 @@ PUBLIC int StartConfiguration(const char *filename) {
         ucsContextS = &ucsContext;
     }
     /* Initialise UNICENS with parsed config */
-    if (!UCSI_NewConfig(&ucsContext.ucsiData, ucsConfig))   {
+    if (!UCSI_NewConfig(&ucsContext.ucsiData, ucsContext.ucsConfig))   {
         AFB_ERROR ("Fail to initialize UNICENS");
         goto OnErrorExit;
     }