prepares asynchronous i2c_write result
authorTobias Jahnke <tjahnk@users.noreply.github.com>
Mon, 31 Jul 2017 15:40:01 +0000 (17:40 +0200)
committerFulup Ar Foll <fulup@iot.bzh>
Tue, 1 Aug 2017 09:47:14 +0000 (11:47 +0200)
ucs2-afb/ucs_binding.c
ucs2-interface/ucs_config.h
ucs2-interface/ucs_interface.h
ucs2-interface/ucs_lib_interf.c

index 133a946..b42c3ff 100644 (file)
@@ -65,6 +65,8 @@ typedef struct {
 
 static ucsContextT *ucsContextS;
 
+STATIC void ucs2_write_i2c_response(void *result_ptr, void *request_ptr);
+
 PUBLIC void UcsXml_CB_OnError(const char format[], uint16_t vargsCnt, ...) {
     /*AFB_DEBUG (afbIface, format, args); */
     va_list args;
@@ -612,7 +614,9 @@ PUBLIC void ucs2_write_i2c (struct afb_req request) {
                   0x2Au,                /* i2c slave address */
                   0x03E8u,              /* timeout 1000 milliseconds */
                   tx_payload_sz,        /* uint8_t dataLen */
-                  &tx_payload[0]        /* uint8_t *pData */
+                  &tx_payload[0],       /* uint8_t *pData */
+                  ucs2_write_i2c_response,
+                  (void*)&request
                 );  
     
     afb_req_success(request,NULL,"done!!!");
@@ -620,3 +624,24 @@ PUBLIC void ucs2_write_i2c (struct afb_req request) {
  OnErrorExit:
     return;
 }
+
+STATIC void ucs2_write_i2c_response(void *result_ptr, void *request_ptr) {
+    
+    if (request_ptr){
+        afb_req *req = (afb_req *)request_ptr;
+        Ucs_I2c_ResultCode_t *res = (Ucs_I2c_ResultCode_t *)result_ptr;
+        
+        if (!res) {
+            afb_req_fail(*req, NULL,"failure, result code not provided");
+        }
+        else if (*res != UCS_I2C_RES_SUCCESS){
+            afb_req_fail_f(*req, NULL, "failure, result code: %d", *res);
+        }
+        else {
+            afb_req_success(*req, NULL, "success");
+        }
+    } 
+    else {
+        AFB_NOTICE("write_i2c: ambiguous response data");
+    }
+}
index 16210af..93bba34 100644 (file)
@@ -64,6 +64,14 @@ typedef enum
     UniCmdResult_ERROR_ProcessFinished
 } UnicensCmdResult_t;
 
+/**
+ * \brief Asynchronous callback notifiying a command result
+ * \param result_ptr    The asynchronous result of the command
+ * \param request_ptr   User reference, typically points to the afb_req
+ *                      object.
+ */
+typedef void (*Ucsi_ResultCb_t)(void *result_ptr, void *request_ptr);
+
 /**
  * \brief Internal enum for Unicens Integration
  */
@@ -135,6 +143,10 @@ typedef struct
     uint16_t timeout;
     uint8_t dataLen;
     uint8_t data[I2C_WRITE_MAX_LEN];
+    
+    Ucsi_ResultCb_t result_fptr;
+    void *request_ptr;
+    
 } UnicensCmdI2CWrite_t;
 
 /**
index c18d440..0ec603a 100644 (file)
@@ -179,11 +179,14 @@ bool UCSI_SetRouteActive(UCSI_Data_t *pPriv, uint16_t routeId, bool isActive);
  * \param timeout - Timeout in milliseconds.
  * \param dataLen - Amount of bytes to send via I2C
  * \param pData - The payload to be send.
+ * \param result_fptr - Callback function notifying the asynchronous result.
+ * \param request_ptr - User reference which is provided for the asynchronous result.
  *
  * \return true, if route command was enqueued to Unicens.
  */
 bool UCSI_I2CWrite(UCSI_Data_t *pPriv, uint16_t targetAddress, bool isBurst, uint8_t blockCount,
-    uint8_t slaveAddr, uint16_t timeout, uint8_t dataLen, uint8_t *pData);
+    uint8_t slaveAddr, uint16_t timeout, uint8_t dataLen, uint8_t *pData,
+    Ucsi_ResultCb_t result_fptr, void *request_ptr);
 
 /**
  * \brief Enables or disables a route by the given routeId
index fd60496..508b723 100644 (file)
@@ -308,7 +308,8 @@ bool UCSI_SetRouteActive(UCSI_Data_t *my, uint16_t routeId, bool isActive)
 }
 
 bool UCSI_I2CWrite(UCSI_Data_t *my, uint16_t targetAddress, bool isBurst, uint8_t blockCount,
-    uint8_t slaveAddr, uint16_t timeout, uint8_t dataLen, uint8_t *pData)
+    uint8_t slaveAddr, uint16_t timeout, uint8_t dataLen, uint8_t *pData, 
+    Ucsi_ResultCb_t result_fptr, void *request_ptr)
 {
     UnicensCmdEntry_t entry;
     assert(MAGIC == my->magic);
@@ -321,6 +322,8 @@ bool UCSI_I2CWrite(UCSI_Data_t *my, uint16_t targetAddress, bool isBurst, uint8_
     entry.val.I2CWrite.slaveAddr = slaveAddr;
     entry.val.I2CWrite.timeout = timeout;
     entry.val.I2CWrite.dataLen = dataLen;
+    entry.val.I2CWrite.result_fptr = result_fptr;
+    entry.val.I2CWrite.request_ptr = request_ptr;
     memcpy(entry.val.I2CWrite.data, pData, dataLen);
     return EnqueueCommand(my, &entry);
 }