adds node-availibility event and subscribe function
authorTobias Jahnke <tjahnk@users.noreply.github.com>
Thu, 3 Aug 2017 14:48:18 +0000 (16:48 +0200)
committerTobias Jahnke <tjahnk@users.noreply.github.com>
Thu, 3 Aug 2017 14:48:18 +0000 (16:48 +0200)
htdocs/UNICENS.html
ucs2-afb/ucs_apidef.h
ucs2-afb/ucs_apidef.json
ucs2-afb/ucs_binding.c
ucs2-afb/ucs_binding.h
ucs2-interface/ucs_interface.h
ucs2-interface/ucs_lib_interf.c

index 176e11d..d3fc285 100644 (file)
@@ -16,6 +16,7 @@
     <br>
     
     <ol>
+    <li><button onclick="callbinder('UNICENS','subscribe', {})">Subscribe to events</button></li>
     <li><button onclick="callbinder('UNICENS','initialise', {filename:ucs2_config})">Parse XML and Start UNICENS</button></li>
     <li><button onclick="callbinder('UNICENS','volume', {channel:'master', volume: 255})">Set Master Volume to 255</button></li>
     <li><button onclick="callbinder('unicens','volume', {channel:'master', volume: 230})">Set Master Volume to 230</button></li>
index d32a6dc..1c27ab7 100644 (file)
@@ -33,21 +33,21 @@ static const char _afb_description_v2_UNICENS[] =
     "et\":{\"x-permissions\":{\"$ref\":\"#/components/x-permissions/config\"}"
     ",\"parameters\":[{\"in\":\"query\",\"name\":\"filename\",\"required\":tr"
     "ue,\"schema\":{\"type\":\"string\"}}],\"responses\":{\"200\":{\"$ref\":\""
-    "#/components/responses/200\"}}}},\"/volume\":{\"description\":\"Set Mast"
-    "er Volume.\",\"get\":{\"x-permissions\":{\"$ref\":\"#/components/x-permi"
-    "ssions/monitor\"},\"parameters\":[{\"in\":\"query\",\"name\":\"value\",\""
-    "required\":true,\"schema\":{\"type\":\"integer\"}}],\"responses\":{\"200"
-    "\":{\"$ref\":\"#/components/responses/200\"}}}},\"/monitor\":{\"descript"
-    "ion\":\"Subscribe to Unicens Event.\",\"get\":{\"x-permissions\":{\"$ref"
-    "\":\"#/components/x-permissions/monitor\"},\"responses\":{\"200\":{\"$re"
-    "f\":\"#/components/responses/200\"}}}},\"/writei2c\":{\"description\":\""
-    "Writes I2C command to remote node.\",\"get\":{\"x-permissions\":{\"$ref\""
-    ":\"#/components/x-permissions/monitor\"},\"parameters\":[{\"in\":\"query"
-    "\",\"name\":\"node\",\"required\":true,\"schema\":{\"type\":\"integer\","
-    "\"format\":\"int32\"}},{\"in\":\"query\",\"name\":\"data\",\"required\":"
-    "true,\"schema\":{\"type\":\"array\",\"format\":\"int32\"},\"style\":\"si"
-    "mple\"}],\"responses\":{\"200\":{\"$ref\":\"#/components/responses/200\""
-    "}}}}}}"
+    "#/components/responses/200\"}}}},\"/subscribe\":{\"description\":\"Subsc"
+    "ribe to UNICENS Events.\",\"get\":{\"x-permissions\":{\"$ref\":\"#/compo"
+    "nents/x-permissions/monitor\"},\"responses\":{\"200\":{\"$ref\":\"#/comp"
+    "onents/responses/200\"}}}},\"/writei2c\":{\"description\":\"Writes I2C c"
+    "ommand to remote node.\",\"get\":{\"x-permissions\":{\"$ref\":\"#/compon"
+    "ents/x-permissions/monitor\"},\"parameters\":[{\"in\":\"query\",\"name\""
+    ":\"node\",\"required\":true,\"schema\":{\"type\":\"integer\",\"format\":"
+    "\"int32\"}},{\"in\":\"query\",\"name\":\"data\",\"required\":true,\"sche"
+    "ma\":{\"type\":\"array\",\"format\":\"int32\"},\"style\":\"simple\"}],\""
+    "responses\":{\"200\":{\"$ref\":\"#/components/responses/200\"}}}},\"/vol"
+    "ume\":{\"description\":\"Set Master Volume (DEPRECATED).\",\"get\":{\"x-"
+    "permissions\":{\"$ref\":\"#/components/x-permissions/monitor\"},\"parame"
+    "ters\":[{\"in\":\"query\",\"name\":\"value\",\"required\":true,\"schema\""
+    ":{\"type\":\"integer\"}}],\"responses\":{\"200\":{\"$ref\":\"#/component"
+    "s/responses/200\"}}}}}}"
 ;
 
 static const struct afb_auth _afb_auths_v2_UNICENS[] = {
@@ -57,9 +57,9 @@ static const struct afb_auth _afb_auths_v2_UNICENS[] = {
 
  void ucs2_listconfig(struct afb_req req);
  void ucs2_initialise(struct afb_req req);
- void ucs2_volume(struct afb_req req);
- void ucs2_monitor(struct afb_req req);
+ void ucs2_subscribe(struct afb_req req);
  void ucs2_writei2c(struct afb_req req);
+ void ucs2_volume(struct afb_req req);
 
 static const struct afb_verb_v2 _afb_verbs_v2_UNICENS[] = {
     {
@@ -77,22 +77,22 @@ static const struct afb_verb_v2 _afb_verbs_v2_UNICENS[] = {
         .session = AFB_SESSION_NONE_V2
     },
     {
-        .verb = "volume",
-        .callback = ucs2_volume,
+        .verb = "subscribe",
+        .callback = ucs2_subscribe,
         .auth = &_afb_auths_v2_UNICENS[1],
         .info = NULL,
         .session = AFB_SESSION_NONE_V2
     },
     {
-        .verb = "monitor",
-        .callback = ucs2_monitor,
+        .verb = "writei2c",
+        .callback = ucs2_writei2c,
         .auth = &_afb_auths_v2_UNICENS[1],
         .info = NULL,
         .session = AFB_SESSION_NONE_V2
     },
     {
-        .verb = "writei2c",
-        .callback = ucs2_writei2c,
+        .verb = "volume",
+        .callback = ucs2_volume,
         .auth = &_afb_auths_v2_UNICENS[1],
         .info = NULL,
         .session = AFB_SESSION_NONE_V2
index 24474e6..febd4c8 100644 (file)
         }
       }
     },
-    "/volume": {
-      "description": "Set Master Volume.",
-      "get": {
-        "x-permissions": {
-          "$ref": "#/components/x-permissions/monitor"
-        },
-        "parameters": [
-          {
-            "in": "query",
-            "name": "value",
-            "required": true,
-            "schema": { "type": "integer" }
-          }
-        ],
-        "responses": {
-          "200": {"$ref": "#/components/responses/200"}
-        }
-      }
-    },
-    "/monitor": {
-      "description": "Subscribe to Unicens Event.",
+    "/subscribe": {
+      "description": "Subscribe to UNICENS Events.",
       "get": {
         "x-permissions": {
           "$ref": "#/components/x-permissions/monitor"
           "200": {"$ref": "#/components/responses/200"}
         }
       }
+    },
+    "/volume": {
+      "description": "Set Master Volume (DEPRECATED).",
+      "get": {
+        "x-permissions": {
+          "$ref": "#/components/x-permissions/monitor"
+        },
+        "parameters": [
+          {
+            "in": "query",
+            "name": "value",
+            "required": true,
+            "schema": { "type": "integer" }
+          }
+        ],
+        "responses": {
+          "200": {"$ref": "#/components/responses/200"}
+        }
+      }
     }
   }
 }
index fe6e033..8a1174c 100644 (file)
@@ -62,7 +62,13 @@ typedef struct {
   UCSI_channelsT *channels;
 } ucsContextT;
 
+typedef struct {
+    struct afb_event node_event;
+    
+} EventData_t;
+
 static ucsContextT *ucsContextS;
+static EventData_t *eventData = NULL;
 
 PUBLIC void UcsXml_CB_OnError(const char format[], uint16_t vargsCnt, ...) {
     /*AFB_DEBUG (afbIface, format, args); */
@@ -200,6 +206,31 @@ void UCSI_CB_OnGpioStateChange(void *pTag, uint16_t nodeAddress, uint8_t gpioPin
 {
 }
 
+PUBLIC void UCSI_CB_OnMgrReport(void *pTag, Ucs_MgrReport_t code, uint16_t nodeAddress, Ucs_Rm_Node_t *pNode){
+
+    bool available;
+    
+    if (code == UCS_MGR_REP_AVAILABLE) {
+        available = true;
+    }
+    else if (code == UCS_MGR_REP_NOT_AVAILABLE) {
+        available = false;
+    }
+    else {
+        /*untracked event - just exit*/
+        return;
+    }
+    
+    if (eventData) {
+        
+        json_object *j_event_info = json_object_new_object();
+        json_object_object_add(j_event_info, "node", json_object_new_int(nodeAddress));
+        json_object_object_add(j_event_info, "available", json_object_new_boolean(available));
+        
+        afb_event_push(eventData->node_event, j_event_info);
+    }     
+}
+
 bool Cdev_Init(CdevData_t *d, const char *fileName, bool read, bool write)
 {
     if (NULL == d || NULL == fileName)  goto OnErrorExit;
@@ -531,12 +562,34 @@ PUBLIC void ucs2_listconfig (struct afb_req request) {
     return;
 }
 
-PUBLIC void ucs2_monitor (struct afb_req request) {
+PUBLIC void ucs2_subscribe (struct afb_req request) {
     
-   afb_req_success(request,NULL,"UNICENS-to_be_done"); 
+    if (!eventData) {
+        
+        eventData = malloc(sizeof(EventData_t));
+        if (eventData) {
+            eventData->node_event = afb_daemon_make_event ("node-availibility");
+        }
+        
+        if (!eventData || !afb_event_is_valid(eventData->node_event)) {
+            afb_req_fail_f (request, "create-event", "Cannot create or register event");
+            goto OnExitError;
+        }
+    }
+    
+    if (afb_req_subscribe(request, eventData->node_event) != 0) {
+        
+        afb_req_fail_f (request, "subscribe-event", "Cannot subscribe to event");
+        goto OnExitError;
+    }
+    
+    afb_req_success(request,NULL,"event subscription successful"); 
+    
+OnExitError:
+    return;
 }
 
-STATIC void ucs2_writei2c_CB(void *result_ptr, void *request_ptr) {
+STATIC void ucs2_writei2c_CB (void *result_ptr, void *request_ptr) {
     
     if (request_ptr){
         afb_req *req = (afb_req *)request_ptr;
@@ -560,7 +613,7 @@ STATIC void ucs2_writei2c_CB(void *result_ptr, void *request_ptr) {
     }
 }
 
-PUBLIC void ucs2_writei2c(struct afb_req request) {
+PUBLIC void ucs2_writei2c (struct afb_req request) {
     
     struct json_object *j_obj;
     static uint8_t i2c_data[I2C_MAX_DATA_SZ];
index d77a2f8..24c0b4e 100644 (file)
@@ -44,9 +44,9 @@ extern struct afb_service afbSrv;
 
 // API verbs prototype
 PUBLIC void ucs2_configure (struct afb_req request);
-PUBLIC void ucs2_volume    (struct afb_req request);
-PUBLIC void ucs2_monitor   (struct afb_req request);
+PUBLIC void ucs2_subscribe (struct afb_req request);
 PUBLIC void ucs2_writei2c  (struct afb_req request);
+PUBLIC void ucs2_volume    (struct afb_req request);
 
 #endif /* UCS2BINDING_H */
 
index 0ec603a..82bba55 100644 (file)
@@ -292,6 +292,16 @@ extern void UCSI_CB_OnRouteResult(void *pTag, uint16_t routeId, bool isActive);
  */
 extern void UCSI_CB_OnGpioStateChange(void *pTag, uint16_t nodeAddress, uint8_t gpioPinId, bool isHighState);
 
+/**
+ * \brief Callback when nodes are discovered or disappear
+ * \note This function must be implemented by the integrator
+ * \param pTag - Pointer given by the integrator by UCSI_Init
+ * \param code - Report code
+ * \param nodeAddress - Node Address of the INIC sending the update.
+ * \param pNode - Reference to the node structure or NULL.
+ */
+extern void UCSI_CB_OnMgrReport(void *pTag, Ucs_MgrReport_t code, uint16_t nodeAddress, Ucs_Rm_Node_t *pNode);
+
 #ifdef __cplusplus
 }
 #endif
index 52840ba..9e7167d 100644 (file)
@@ -784,6 +784,8 @@ static void OnUcsMgrReport(Ucs_MgrReport_t code, uint16_t node_address, Ucs_Rm_N
         UCSI_CB_OnUserMessage(my->tag, true, "Node=%X: unknown code", 1, node_address);
         break;
     }
+    
+    UCSI_CB_OnMgrReport(my->tag, code, node_address, node_ptr);
 }
 
 static void OnUcsNsRun(Ucs_Rm_Node_t * node_ptr, Ucs_Ns_ResultCode_t result, void *ucs_user_ptr)