+ AFB_NOTICE ("UNICENS stopped");
+ /* push an asynchronous request for loopback to call UcsXml_FreeVal */
+ sd_event_add_time(afb_daemon_get_event_loop(), NULL, CLOCK_MONOTONIC, 0, 0, OnStopCB, pTag);
+}
+
+/* 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_event_info = json_object_new_object();
+ json_object *j_array = json_object_new_array();
+
+ 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);
+
+ afb_event_push(eventDataRx->rx_event, j_event_info);
+}
+
+/** 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);
+ }