+ 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;
+}
+
+PUBLIC void ucs2_subscriberx (struct afb_req request) {
+
+ if (!eventDataRx) {
+
+ eventDataRx = malloc(sizeof(EventDataRx_t));
+ if (eventDataRx) {
+ eventDataRx->rx_event = afb_daemon_make_event("rx-message");
+ }
+
+ if (!eventDataRx || !afb_event_is_valid(eventDataRx->rx_event)) {
+ afb_req_fail_f(request, "create-event", "Cannot create or register event");
+ goto OnExitError;
+ }
+ }
+
+ if (afb_req_subscribe(request, eventDataRx->rx_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 json_object * ucs2_validate_command (struct afb_req request,
+ const char* func_name) {
+
+ struct json_object *j_obj = NULL;
+
+ if (!ucsContextS) { /* check UNICENS is initialized */
+ afb_req_fail_f(request, "unicens-init",
+ "Load a configuration before calling %s.",
+ func_name);
+ goto OnErrorExit;
+ }
+
+ j_obj = afb_req_json(request);
+ if (!j_obj) {
+ afb_req_fail_f(request,
+ "query-notjson","query=%s not a valid json entry",
+ afb_req_value(request,""));
+ goto OnErrorExit;
+ }
+
+ AFB_DEBUG("request: %s", json_object_to_json_string(j_obj));
+
+ if (json_object_get_type(j_obj)==json_type_array) {
+ int len = json_object_array_length(j_obj);
+
+ if (len == 1) { /* only support 1 command in array */
+ j_obj = json_object_array_get_idx(j_obj, 0);
+ }
+ else {
+ afb_req_fail_f(request,
+ "query-array",
+ "query of multiple %s commands is not supported",
+ func_name);
+ j_obj = NULL;
+ goto OnErrorExit;
+ }
+ }
+
+ OnErrorExit:
+ return j_obj;
+}
+
+STATIC void ucs2_writei2c_CB (void *result_ptr, void *request_ptr) {
+
+ if (request_ptr){
+ afb_req *req = (afb_req *)request_ptr;
+ Ucs_I2c_ResultCode_t *res = (Ucs_I2c_ResultCode_t *)result_ptr;
+
+ if (!res) {
+ afb_req_fail(*req, "processing","busy or lost initialization");
+ }
+ else if (*res != UCS_I2C_RES_SUCCESS){
+ afb_req_fail_f(*req, "error-result", "result code: %d", *res);
+ }
+ else {
+ afb_req_success(*req, NULL, "success");
+ }
+
+ afb_req_unref(*req);
+ free(request_ptr);
+ }
+ else {
+ AFB_NOTICE("write_i2c: ambiguous response data");
+ }
+}
+
+/* write a single i2c command */
+STATIC void ucs2_writei2c_cmd(struct afb_req request, json_object *j_obj) {
+
+ static uint8_t i2c_data[I2C_MAX_DATA_SZ];
+ uint8_t i2c_data_sz = 0;
+ uint16_t node_addr = 0;
+ struct afb_req *async_req_ptr = NULL;
+ json_object *j_tmp;
+ json_bool key_found;
+
+ if (json_object_object_get_ex(j_obj, "node", &j_tmp)) {
+ node_addr = (uint16_t)json_object_get_int(j_tmp);
+ AFB_NOTICE("node_address: 0x%02X", node_addr);
+ if (node_addr == 0) {
+ afb_req_fail_f(request, "query-params","param node invalid type");
+ goto OnErrorExit;
+ }
+ }
+ else {
+ afb_req_fail_f(request, "query-params","param node missing");