+/* helper function: wraps Rx message in json and triggers notification */
+STATIC void NotifyEventRxMsg(uint16_t src_addr, uint16_t msg_id, uint8_t *data_ptr, uint32_t data_sz) {
+
+ if (!eventDataRx)
+ return;
+
+ if (data_sz > CTRL_MAX_DATA_SZ) {
+ AFB_NOTICE("RX-MSG: discarded, payload exceeds %d bytes", CTRL_MAX_DATA_SZ);
+ return;
+ }
+
+ json_object *j_query = NULL;
+ int node = (int)src_addr;
+ int msgid = (int)msg_id;
+ size_t data_size = (size_t)data_sz;
+
+ /* 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_query);
+}
+
+/** Asynchronous processing of Rx messages in mainloop is recommended */
+STATIC int OnAmsMessageReceivedCB (sd_event_source *source, void *pTag) {
+ ucsContextT *ucsContext = (ucsContextT*) pTag;
+ uint32_t data_sz = 0U;
+ uint8_t *data_ptr = NULL;
+ uint16_t msg_id = 0U;
+ uint16_t src_addr = 0U;
+
+ while (UCSI_GetAmsMessage(&ucsContext->ucsiData, &msg_id, &src_addr, &data_ptr, &data_sz)) {
+ NotifyEventRxMsg(src_addr, msg_id, data_ptr, data_sz);
+ AFB_DEBUG("RX-MSG: src=0x%04X, msg_id=0x%04X, size=%d", src_addr, msg_id, data_sz);
+ UCSI_ReleaseAmsMessage(&ucsContext->ucsiData);
+ }
+
+ return 0;