adds async. result to i2c_write
authorTobias Jahnke <tjahnk@users.noreply.github.com>
Tue, 1 Aug 2017 09:03:06 +0000 (11:03 +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_lib_interf.c

index b42c3ff..dffacd3 100644 (file)
@@ -555,6 +555,7 @@ PUBLIC void ucs2_write_i2c (struct afb_req request) {
     static uint8_t tx_payload[UCSB_I2C_MAX_PAYLOAD];
     uint8_t tx_payload_sz = 0;
     uint16_t node_addr = 0;
+    struct afb_req *async_req_ptr = NULL;
     
     /* check UNICENS is initialised */
     if (!ucsContextS) {
@@ -606,20 +607,31 @@ PUBLIC void ucs2_write_i2c (struct afb_req request) {
         afb_req_fail_f(request, "query-params","params wrong or missing");
         goto OnErrorExit;
     }
-        
-    UCSI_I2CWrite(&ucsContextS->ucsiData,/*UCSI_Data_t *pPriv*/
-                  node_addr,            /*uint16_t targetAddress*/
-                  false,                /*bool isBurst*/
-                  0u,                   /* block count */
-                  0x2Au,                /* i2c slave address */
-                  0x03E8u,              /* timeout 1000 milliseconds */
-                  tx_payload_sz,        /* uint8_t dataLen */
-                  &tx_payload[0],       /* uint8_t *pData */
-                  ucs2_write_i2c_response,
-                  (void*)&request
-                );  
     
-    afb_req_success(request,NULL,"done!!!");
+    async_req_ptr = malloc(sizeof(afb_req));
+    *async_req_ptr = request;
+    
+    if (UCSI_I2CWrite(  &ucsContextS->ucsiData,   /*UCSI_Data_t *pPriv*/
+                        node_addr,                /*uint16_t targetAddress*/
+                        false,                    /*bool isBurst*/
+                        0u,                       /* block count */
+                        0x2Au,                    /* i2c slave address */
+                        0x03E8u,                  /* timeout 1000 milliseconds */
+                        tx_payload_sz,            /* uint8_t dataLen */
+                        &tx_payload[0],           /* uint8_t *pData */
+                        &ucs2_write_i2c_response, /* callback*/
+                        (void*)async_req_ptr      /* callback argument */
+                  )) {
+        /* asynchronous command is running */
+        afb_req_addref(request);
+    }
+    else {
+        AFB_NOTICE("i2c_data: scheduling command failed");
+        afb_req_fail_f(request, "query-command-queue","command queue overload");
+        free(async_req_ptr);
+        async_req_ptr = NULL;
+        goto OnErrorExit;
+    }
     
  OnErrorExit:
     return;
@@ -632,14 +644,17 @@ STATIC void ucs2_write_i2c_response(void *result_ptr, void *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");
+            afb_req_fail(*req, "processing","busy or lost initialization");
         }
         else if (*res != UCS_I2C_RES_SUCCESS){
-            afb_req_fail_f(*req, NULL, "failure, result code: %d", *res);
+            afb_req_fail_f(*req, "error-result", "result code: %d", *res);
         }
         else {
             afb_req_success(*req, NULL, "success");
         }
+        
+        afb_req_unref(*req);
+        free(request_ptr);
     } 
     else {
         AFB_NOTICE("write_i2c: ambiguous response data");
index 508b723..52840ba 100644 (file)
@@ -222,8 +222,11 @@ void UCSI_Service(UCSI_Data_t *my)
                 (e->val.I2CWrite.isBurst ? UCS_I2C_BURST_MODE : UCS_I2C_DEFAULT_MODE), e->val.I2CWrite.blockCount,
                 e->val.I2CWrite.slaveAddr, e->val.I2CWrite.timeout, e->val.I2CWrite.dataLen, e->val.I2CWrite.data, OnUcsI2CWrite))
                 popEntry = false;
-            else
+            else {
                 UCSI_CB_OnUserMessage(my->tag, true, "Ucs_I2c_WritePort failed", 0);
+                assert(e->val.I2CWrite.result_fptr != NULL);
+                e->val.I2CWrite.result_fptr(NULL /*processing error*/, e->val.I2CWrite.request_ptr);
+            }
             break;
         default:
             assert(false);
@@ -826,6 +829,16 @@ static void OnUcsI2CWrite(uint16_t node_address, uint16_t i2c_port_handle,
 {
     UCSI_Data_t *my = (UCSI_Data_t *)user_ptr;
     assert(MAGIC == my->magic);
+    
+    if ((my->currentCmd->cmd == UnicensCmd_I2CWrite) 
+        && (my->currentCmd->val.I2CWrite.result_fptr)) {
+        
+        my->currentCmd->val.I2CWrite.result_fptr(&result.code, my->currentCmd->val.I2CWrite.request_ptr);
+    }
+    else {
+        assert(false);
+    }
+    
     OnCommandExecuted(my, UnicensCmd_I2CWrite);
     if (UCS_I2C_RES_SUCCESS != result.code)
         UCSI_CB_OnUserMessage(my->tag, true, "Remote I2C Write to node=0x%X failed", 1, node_address);