aboutsummaryrefslogtreecommitdiffstats
path: root/HAL-afb/hal-most-unicens/wrap_unicens.c
diff options
context:
space:
mode:
Diffstat (limited to 'HAL-afb/hal-most-unicens/wrap_unicens.c')
-rw-r--r--HAL-afb/hal-most-unicens/wrap_unicens.c45
1 files changed, 37 insertions, 8 deletions
diff --git a/HAL-afb/hal-most-unicens/wrap_unicens.c b/HAL-afb/hal-most-unicens/wrap_unicens.c
index 15f94a1..2bd1e91 100644
--- a/HAL-afb/hal-most-unicens/wrap_unicens.c
+++ b/HAL-afb/hal-most-unicens/wrap_unicens.c
@@ -25,6 +25,11 @@
#include "wrap_unicens.h"
#include "wrap-json.h"
+typedef struct async_job_ {
+ wrap_ucs_result_cb_t result_fptr;
+ void *result_user_ptr;
+} async_job_t;
+
/* Subscribes to unicens2-binding events.
* \return Returns 0 if successful, otherwise != 0".
*/
@@ -134,12 +139,6 @@ OnErrorExit:
return err;
}
-
-static void wrap_ucs_i2cwrite_cb(void *closure __attribute__((unused)), int status, struct json_object *j_result) {
-
- AFB_NOTICE("wrap_ucs_i2cwrite_cb: status=%d, res=%s", status, json_object_to_json_string(j_result));
-}
-
/* Write I2C command to a network node.
* \param node Node address
* \param data_ptr Reference to command data
@@ -188,15 +187,34 @@ OnErrorExit:
return err;
}
+/* ------------------ ASYNCHRONOUS API ---------------------------------------*/
+
+static void wrap_ucs_i2cwrite_cb(void *closure, int status, struct json_object *j_result) {
+
+ AFB_NOTICE("wrap_ucs_i2cwrite_cb: closure=%p status=%d, res=%s", closure, status, json_object_to_json_string(j_result));
+
+ if (closure) {
+ async_job_t *job_ptr = (async_job_t *)closure;
+
+ if (job_ptr->result_fptr)
+ job_ptr->result_fptr(0U, job_ptr->result_user_ptr);
+
+ free(closure);
+ }
+}
+
/* Write I2C command to a network node.
* \param node Node address
* \param data_ptr Reference to command data
* \param data_sz Size of the command data. Valid values: 1..32.
* \return Returns 0 if successful, otherwise != 0".
*/
-extern int wrap_ucs_i2cwrite(uint16_t node, uint8_t *data_ptr, uint8_t data_sz) {
+extern int wrap_ucs_i2cwrite(uint16_t node, uint8_t *data_ptr, uint8_t data_sz,
+ wrap_ucs_result_cb_t result_fptr,
+ void *result_user_ptr) {
json_object *j_query, *j_array = NULL;
+ async_job_t *job_ptr = NULL;
int err;
uint8_t cnt;
@@ -215,8 +233,19 @@ extern int wrap_ucs_i2cwrite(uint16_t node, uint8_t *data_ptr, uint8_t data_sz)
json_object_object_add(j_query, "node", json_object_new_int(node));
json_object_object_add(j_query, "data", j_array);
+
+ job_ptr = malloc(sizeof(async_job_t));
+
+ if (!job_ptr) {
+ err = -1;
+ AFB_ERROR("Failed to create async job object");
+ goto OnErrorExit;
+ }
+
+ job_ptr->result_fptr = result_fptr;
+ job_ptr->result_user_ptr = result_user_ptr;
- afb_service_call("UNICENS", "writei2c", j_query, wrap_ucs_i2cwrite_cb, NULL);
+ afb_service_call("UNICENS", "writei2c", j_query, wrap_ucs_i2cwrite_cb, job_ptr);
err = 0;
j_query = NULL;