]> rtime.felk.cvut.cz Git - CanFestival-3.git/blobdiff - src/dcf.c
Implemented retry on SDO timeout in dcf.c.
[CanFestival-3.git] / src / dcf.c
index b0b52cc5b3be1581b920e592dcad425d64e9ee7d..90a8c19f035837925066d26076a59ac50cd64fcd 100644 (file)
--- a/src/dcf.c
+++ b/src/dcf.c
 **
 **
 */
-#include "objacces.h"
-#include "sdo.h"
-#include "dcf.h"
-#include "sysdep.h"
 
 
-/**
-**
-**
-** @param d fuck fuck fuck
-** @param nodeId
-** @param index
-** @param subIndex
-** @param count
-** @param dataType
-** @param data
-** @param Callback
-** @param endianize
-*/
+#include "data.h"
+#include "sysdep.h"
+
 extern UNS8 _writeNetworkDict (CO_Data* d, UNS8 nodeId, UNS16 index,
                                UNS8 subIndex, UNS8 count, UNS8 dataType, void *data, SDOCallback_t Callback, UNS8 endianize);
 
-const indextable *ptrTable;
+
+static void send_consise_dcf_loop(CO_Data* d,UNS8 nodeId);
+
+/* Seek to next NodeID's DCF */
+#define SEEK_NEXT_DCF() \
+       nodeId=(nodeId+1) % d->dcf_odentry->bSubCount; \
+       if(nodeId==0) nodeId=1; \
+       d->dcf_cursor = NULL;
 
 /**
 **
@@ -64,7 +57,7 @@ const indextable *ptrTable;
 */
 static void CheckSDOAndContinue(CO_Data* d, UNS8 nodeId)
 {
-  UNS32 abortCode;
+  UNS32 abortCode = 0;
 
   if(getWriteResultNetworkDict (d, nodeId, &abortCode) != SDO_FINISHED)
     {
@@ -73,9 +66,17 @@ static void CheckSDOAndContinue(CO_Data* d, UNS8 nodeId)
     }
 
   closeSDOtransfer(d, nodeId, SDO_CLIENT);
-  decompo_dcf(d,nodeId);
+  /* Timedout ? */
+  if(abortCode == SDOABT_TIMED_OUT){
+    /* Node may not be ready, try another one */
+    /* Warning, this might leed to endless attempts */
+    /* if node does never answer */
+       SEEK_NEXT_DCF()
+  }
+  send_consise_dcf_loop(d,nodeId);
 }
 
+
 /**
 **
 **
@@ -84,69 +85,117 @@ static void CheckSDOAndContinue(CO_Data* d, UNS8 nodeId)
 **
 ** @return
 */
-UNS32 decompo_dcf(CO_Data* d,UNS8 nodeId)
+UNS8 send_consise_dcf(CO_Data* d,UNS8 nodeId)
 {
-  UNS32 errorCode;
-  UNS16 target_Index;
-  UNS8 target_Subindex;
-  UNS32 target_Size;
-  UNS32 res;
-  ODCallback_t *Callback;
-
-  ptrTable = (*d->scanIndexOD)(0x1F22, &errorCode, &Callback);
-  if (errorCode != OD_SUCCESSFUL)
-    {
-      return errorCode;
-    }
-
-  /** Loop on all Nodes supported in DCF subindexes*/
-  while (nodeId < ptrTable->bSubCount){
-    UNS32 nb_targets;
-
-    UNS8 szData = ptrTable->pSubindex[nodeId].size;
-    UNS8* dcfend;
+  UNS8 szData;
+  /* Fetch DCF OD entry, if not already done */
+  if(!d->dcf_odentry)
+  {
+    UNS32 errorCode;
+    ODCallback_t *Callback;
+    d->dcf_odentry = (*d->scanIndexOD)(0x1F22, &errorCode, &Callback);
+    /* If DCF entry do not exist... Nothing to do.*/
+    if (errorCode != OD_SUCCESSFUL) goto DCF_finish;
+  }
 
-    {
-      UNS8* dcf = *((UNS8**)ptrTable->pSubindex[nodeId].pObject);
-      dcfend = dcf + szData;
-      if (!d->dcf_cursor){
-        d->dcf_cursor = (UNS8*)dcf + 4;
-        d->dcf_count_targets = 0;
-      }
-      nb_targets = UNS32_LE(*((UNS32*)dcf));
-    }
+  szData = d->dcf_odentry->pSubindex[nodeId].size;
+  
+  /* if the entry for the nodeId is not empty. */
+  if(szData!=0){
+       /* if the entry for the nodeId is already been processing, quit.*/
+       if(d->dcf_odentry->pSubindex[nodeId].bAccessType & DCF_TO_SEND) return 1;
+       
+       d->dcf_odentry->pSubindex[nodeId].bAccessType|=DCF_TO_SEND;
+       d->dcf_request++;
+       if(d->dcf_request==1)
+               send_consise_dcf_loop(d,nodeId);
+       return 1;
+  }
+  
+  DCF_finish:
+  return 0;
+}
 
-    /** condition on consise DCF string for NodeID, if big enough */
-    if((UNS8*)d->dcf_cursor + 7 < (UNS8*)dcfend && d->dcf_count_targets < nb_targets)
-      {
-        /** pointer to the DCF string for NodeID */
-        target_Index = UNS16_LE(*((UNS16*)(d->dcf_cursor))); d->dcf_cursor += 2;
-        target_Subindex = *((UNS8*)(((UNS8*)d->dcf_cursor)++));
-        target_Size = UNS32_LE(*((UNS32*)(d->dcf_cursor))); d->dcf_cursor += 4;
-
-        /* printf("Master : ConfigureSlaveNode %2.2x (Concise
-          DCF)\n",nodeId);*/
-        res = _writeNetworkDict(d, /** CO_Data* d*/
-                                nodeId, /** UNS8 nodeId*/
-                                target_Index, /** UNS16 index*/
-                                target_Subindex, /** UNS8 subindex*/
-                                target_Size, /** UNS8 count*/
-                                0, /** UNS8 dataType*/
-                                d->dcf_cursor,/** void *data*/
-                                CheckSDOAndContinue,/** SDOCallback_t
+static void send_consise_dcf_loop(CO_Data* d,UNS8 nodeId)
+{
+  /* Loop on all DCF subindexes, corresponding to node ID until there is no request*/
+  //while (nodeId < d->dcf_odentry->bSubCount){
+  while (d->dcf_request>0){
+       if(d->dcf_odentry->pSubindex[nodeId].bAccessType & DCF_TO_SEND){         
+        UNS8* dcfend;
+               UNS32 nb_entries;
+               UNS8 szData = d->dcf_odentry->pSubindex[nodeId].size;
+        
+               {
+                       UNS8* dcf = *((UNS8**)d->dcf_odentry->pSubindex[nodeId].pObject);
+                       dcfend = dcf + szData;
+                       if (!d->dcf_cursor){
+                       d->dcf_cursor = (UNS8*)dcf + 4;
+                               d->dcf_entries_count = 0;
+                       }
+                       nb_entries = UNS32_LE(*((UNS32*)dcf));
+               }
+
+       /* condition on consise DCF string for NodeID, if big enough */
+       if((UNS8*)d->dcf_cursor + 7 < (UNS8*)dcfend && d->dcf_entries_count < nb_entries){
+       
+               UNS16 target_Index;
+               UNS8 target_Subindex;
+               UNS32 target_Size;
+
+                       /* DCF data may not be 32/16b aligned, 
+                        * we cannot directly dereference d->dcf_cursor 
+                        * as UNS16 or UNS32 
+                        * Do it byte per byte taking care on endianess*/
+#ifdef CANOPEN_BIG_ENDIAN
+               target_Index = *(d->dcf_cursor++) << 8 | 
+                              *(d->dcf_cursor++);
+#else
+               memcpy(&target_Index, d->dcf_cursor,2);
+               d->dcf_cursor+=2;
+#endif
+
+               target_Subindex = *(d->dcf_cursor++);
+
+#ifdef CANOPEN_BIG_ENDIAN
+               target_Size = *(d->dcf_cursor++) << 24 | 
+                             *(d->dcf_cursor++) << 16 | 
+                             *(d->dcf_cursor++) << 8 | 
+                             *(d->dcf_cursor++);
+#else
+               memcpy(&target_Size, d->dcf_cursor,4);
+               d->dcf_cursor+=4;
+#endif
+       
+           _writeNetworkDict(d, /* CO_Data* d*/
+                                nodeId, /* UNS8 nodeId*/
+                                target_Index, /* UNS16 index*/
+                                target_Subindex, /* UNS8 subindex*/
+                                target_Size, /* UNS8 count*/
+                                0, /* UNS8 dataType*/
+                                d->dcf_cursor,/* void *data*/
+                                CheckSDOAndContinue,/* SDOCallback_t
                                                       Callback*/
-                                0); /** no endianize*/
-        /** Push d->dcf_cursor to the end of data*/
-
-        d->dcf_cursor += target_Size;
-        d->dcf_count_targets++;
-
-        return ;
-      }
-    nodeId++;
-    d->dcf_cursor = NULL;
+                                0); /* no endianize*/
+               /* Push d->dcf_cursor to the end of data*/
+
+               d->dcf_cursor += target_Size;
+               d->dcf_entries_count++;
+
+               /* send_consise_dcf_loop will be called by CheckSDOAndContinue for next DCF entry*/
+               return;
+       }
+       else
+       {
+               /* We have finished with the dcf entry. Change the flag, decrement the request
+                *  and execute the bootup callback. */
+               d->dcf_odentry->pSubindex[nodeId].bAccessType&=~DCF_TO_SEND;
+               d->dcf_request--;
+               (*d->post_SlaveBootup)(d, nodeId);
+       }
+       }
+       
+       SEEK_NEXT_DCF()
   }
-  /**  Switch Master to preOperational state */
-  (*d->preOperational)();
 
 }