#include "ucs_binding.h"
#include "ucs_interface.h"
+#include <wrap-json.h>
#define MAX_FILENAME_LEN (100)
#define RX_BUFFER (64)
return;
}
- json_object *j_event_info = json_object_new_object();
- json_object *j_array = json_object_new_array();
+ json_object *j_query = NULL;
+ int node = (int)src_addr;
+ int msgid = (int)msg_id;
+ size_t data_size = (size_t)data_sz;
- if (!j_event_info || !j_array) {
- if (j_event_info) {
- json_object_put(j_event_info);
- }
- if (j_array) {
- json_object_put(j_array);
- }
- AFB_ERROR("Failed to create json objects");
- return;
- }
-
- uint32_t cnt = 0U;
- for (cnt = 0U; cnt < data_sz; cnt++) {
- json_object_array_add(j_array, json_object_new_int(data_ptr[cnt]));
- }
-
- json_object_object_add(j_event_info, "node", json_object_new_int(src_addr));
- json_object_object_add(j_event_info, "msgid", json_object_new_int(msg_id));
- json_object_object_add(j_event_info, "data", j_array);
+ /* skip data attribute if possible, wrap_json_unpack may fail to deal with
+ * an empty Base64 string */
+ if (data_size > 0)
+ wrap_json_pack(&j_query, "{s:i, s:i, s:Y*}", "node", node, "msgid", msgid, "data", data_ptr, data_size);
+ else
+ wrap_json_pack(&j_query, "{s:i, s:i}", "node", node, "msgid", msgid);
- afb_event_push(eventDataRx->rx_event, j_event_info);
+ afb_event_push(eventDataRx->rx_event, j_query);
}
/** Asynchronous processing of Rx messages in mainloop is recommended */
}
}
-/* write a single control message */
-STATIC void ucs2_sendmessage_cmd(struct afb_req request, json_object *j_obj) {
-
- static uint8_t ctrl_data[CTRL_MAX_DATA_SZ];
- uint8_t ctrl_data_sz = 0;
- uint16_t node_addr = 0;
- uint16_t msg_id = 0;
- json_object *j_tmp;
+PUBLIC void ucs2_sendmessage(struct afb_req req) {
+ uint8_t *data_ptr = NULL;
+ size_t data_sz = 0;
+ int ret, node_addr, msg_id = 0;
+ struct json_object *j_obj;
- 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");
- goto OnErrorExit;
- }
+ j_obj = ucs2_validate_command(req, "sendmessageb64");
- if (json_object_object_get_ex(j_obj, "msgid", &j_tmp)) {
- if (json_object_get_type(j_tmp) == json_type_int) {
- msg_id = (uint16_t)json_object_get_int(j_tmp);
- AFB_NOTICE("msgid: 0x%02X", msg_id);
- }
- else {
- afb_req_fail_f(request, "query-params","param msgid invalid type");
- goto OnErrorExit;
- }
- }
- else {
- afb_req_fail_f(request, "query-params","param msgid missing");
+ if (!j_obj) {
+ AFB_NOTICE("validation of command failed");
goto OnErrorExit;
}
- if (json_object_object_get_ex(j_obj, "data", &j_tmp)) {
- if (json_object_get_type(j_tmp)==json_type_array) {
- int size = json_object_array_length(j_tmp);
- if ((size > 0) && (size <= CTRL_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;
- }
- ctrl_data[i] = (uint8_t)json_object_get_int(j_elem);
- }
-
- if (i != size) { /* check if size matches */
- afb_req_fail_f(request, "query-params",
- "parameter data is ambiguous");
- 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);
- ctrl_data_sz = (uint8_t)i;
- }
- }
- }
-
- if (UCSI_SendAmsMessage(&ucsContextS->ucsiData, /* UCSI_Data_t *pPriv*/
- msg_id,
- node_addr,
- &ctrl_data[0],
- ctrl_data_sz)) {
- afb_req_success(request, NULL, "sendmessage started successful");
+ if ((ret==0) &&
+ UCSI_SendAmsMessage(&ucsContextS->ucsiData, msg_id, node_addr, &data_ptr[0], data_sz)
+ ) {
+ afb_req_success(req, NULL, "sendmessageb64 started successful");
}
else {
- AFB_NOTICE("sendmessage: scheduling command failed");
- afb_req_fail_f(request, "query-command-queue","command queue overload");
+ AFB_ERROR("sendmessageb64: scheduling command failed. ret: %d", ret);
+ afb_req_fail_f(req, "query-command-queue","ambiguous command or queue overload");
goto OnErrorExit;
}
OnErrorExit:
- return;
-}
-
-PUBLIC void ucs2_sendmessage(struct afb_req request) {
- struct json_object *j_obj;
-
- j_obj = ucs2_validate_command(request, "sendmessage");
-
- if (j_obj) {
- ucs2_sendmessage_cmd(request, j_obj);
+ if (data_ptr) {
+ free(data_ptr);
}
-}
-
-PUBLIC void ucs2_sendmessageb64(struct afb_req req) {
- afb_req_fail_f(req, "not-implemented", "Function not yet implemented.");
+ return;
}
PUBLIC int ucs2_initbinding(void) {