+ return j_obj;
+}
+
+STATIC void ucs2_writei2c_CB (void *result_ptr, void *request_ptr) {
+
+ if (request_ptr){
+ afb_req_t req = request_ptr;
+ Ucs_I2c_ResultCode_t *res = (Ucs_I2c_ResultCode_t *)result_ptr;
+
+ if (!res) {
+ afb_req_reply(req, NULL, "processing","busy or lost initialization");
+ }
+ else if (*res != UCS_I2C_RES_SUCCESS){
+ afb_req_reply_f(req, NULL, "error-result", "result code: %d", *res);
+ }
+ else {
+ afb_req_reply(req, NULL, NULL, "success");
+ }
+
+ afb_req_unref(req);
+ }
+ else {
+ AFB_API_NOTICE(afbBindingRoot, "write_i2c: ambiguous response data");
+ }
+}
+
+/* write a single i2c command */
+STATIC void ucs2_writei2c_cmd(afb_req_t 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;
+ 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_API_NOTICE(afbBindingRoot, "node_address: 0x%02X", node_addr);
+ if (node_addr == 0) {
+ afb_req_reply(request, NULL, "query-params","param node invalid type");
+ goto OnErrorExit;
+ }
+ }
+ else {
+ afb_req_reply(request, NULL, "query-params","param node missing");
+ goto OnErrorExit;
+ }
+
+ key_found = json_object_object_get_ex(j_obj, "data", &j_tmp);
+ if (key_found && (json_object_get_type(j_tmp)==json_type_array)) {
+ int size = json_object_array_length(j_tmp);
+ if ((size > 0) && (size <= I2C_MAX_DATA_SZ)) {
+
+ int32_t i;
+ int32_t val;
+ struct json_object *j_elem;
+
+ for (i = 0; i < size; i++) {
+
+ j_elem = json_object_array_get_idx(j_tmp, i);
+ val = json_object_get_int(j_elem);
+ if ((val < 0) && (val > 0xFF)){
+ i = 0;
+ break;
+ }
+ i2c_data[i] = (uint8_t)json_object_get_int(j_elem);
+ }
+
+ i2c_data_sz = (uint8_t)i;
+ }
+ }
+
+ if (i2c_data_sz == 0) {
+ AFB_API_NOTICE(afbBindingRoot, "data: invalid or not found");
+ afb_req_reply(request, NULL, "query-params","params wrong or missing");
+ goto OnErrorExit;
+ }
+
+ if (UCSI_I2CWrite( &ucsContextS->ucsiData, /* UCSI_Data_t *pPriv*/
+ node_addr, /* uint16_t targetAddress*/
+ false, /* bool isBurst*/
+ 0u, /* block count */
+ 0x2Au, /* i2c slave address */
+ 0x03E8u, /* timeout 1000 milliseconds */
+ i2c_data_sz, /* uint8_t dataLen */
+ &i2c_data[0], /* uint8_t *pData */
+ &ucs2_writei2c_CB, /* callback*/
+ (void*)request /* callback argument */
+ )) {
+ /* asynchronous command is running */
+ afb_req_addref(request);
+ }
+ else {
+ AFB_API_NOTICE(afbBindingRoot, "i2c write: scheduling command failed");
+ afb_req_reply(request, NULL, "query-command-queue","command queue overload");
+ goto OnErrorExit;
+ }
+
+OnErrorExit:
+ return;
+}
+
+/* parse array or single command */
+PUBLIC void ucs2_writei2c (afb_req_t request) {
+
+ struct json_object *j_obj;
+
+ j_obj = ucs2_validate_command(request, "writei2c");
+
+ if (j_obj) {
+ ucs2_writei2c_cmd(request, j_obj);
+ }
+}
+
+PUBLIC void ucs2_sendmessage(afb_req_t req) {
+ uint8_t *data_ptr = NULL;
+ size_t data_sz = 0;
+ int ret, node_addr, msg_id = 0;
+ struct json_object *j_obj;
+
+ j_obj = ucs2_validate_command(req, "sendmessageb64");
+
+ if (!j_obj) {
+ AFB_API_NOTICE(afbBindingRoot, "validation of command failed");
+ goto OnErrorExit;
+ }
+
+ ret = wrap_json_unpack(j_obj, "{s:i, s:i, s?Y}", "node", &node_addr, "msgid", &msg_id, "data", &data_ptr, &data_sz);
+
+ if ((ret==0) &&
+ UCSI_SendAmsMessage(&ucsContextS->ucsiData, msg_id, node_addr, &data_ptr[0], data_sz)
+ ) {
+ afb_req_reply(req, NULL, NULL, "sendmessageb64 started successful");
+ }
+ else {
+ AFB_API_ERROR(afbBindingRoot, "sendmessageb64: scheduling command failed. ret: %d", ret);
+ afb_req_reply(req, NULL, "query-command-queue","ambiguous command or queue overload");
+ goto OnErrorExit;
+ }
+
+OnErrorExit:
+ if (data_ptr) {
+ free(data_ptr);
+ }