]> rtime.felk.cvut.cz Git - CanFestival-3.git/blobdiff - src/objacces.c
fixed : deprecation warning with the module "sets" when using python 2.6
[CanFestival-3.git] / src / objacces.c
index 2d127978ef83b07db70588bdcdaef1577000bb36..8e6a897c4c68b09e08c2cd1c2e0c432798f38c10 100644 (file)
 **
 ** @return
 **/
+#ifdef DEBUG_WAR_CONSOLE_ON
 UNS8 accessDictionaryError(UNS16 index, UNS8 subIndex,
-                           UNS8 sizeDataDict, UNS8 sizeDataGiven, UNS32 code)
+                           UNS32 sizeDataDict, UNS32 sizeDataGiven, UNS32 code)
 {
-#ifdef DEBUG_WAR_CONSOLE_ON
   MSG_WAR(0x2B09,"Dictionary index : ", index);
   MSG_WAR(0X2B10,"           subindex : ", subIndex);
   switch (code) {
@@ -84,9 +84,11 @@ UNS8 accessDictionaryError(UNS16 index, UNS8 subIndex,
   default :
     MSG_WAR(0x2B20, "Unknown error code : ", code);
   }
-#endif
   return 0;
 }
+#else
+#define accessDictionaryError(index, subIndex, sizeDataDict, sizeDataGiven, code)
+#endif
 
 /*!
 **
@@ -106,14 +108,14 @@ UNS32 _getODentry( CO_Data* d,
                    UNS16 wIndex,
                    UNS8 bSubindex,
                    void * pDestData,
-                   UNS8 * pExpectedSize,
+                   UNS32 * pExpectedSize,
                    UNS8 * pDataType,
                    UNS8 checkAccess,
                    UNS8 endianize)
 { /* DO NOT USE MSG_ERR because the macro may send a PDO -> infinite
     loop if it fails. */
   UNS32 errorCode;
-  UNS8 szData;
+  UNS32 szData;
   const indextable *ptrTable;
   ODCallback_t *Callback;
 
@@ -129,7 +131,7 @@ UNS32 _getODentry( CO_Data* d,
 
   if (checkAccess && (ptrTable->pSubindex[bSubindex].bAccessType & WO)) {
     MSG_WAR(0x2B30, "Access Type : ", ptrTable->pSubindex[bSubindex].bAccessType);
-    accessDictionaryError(wIndex, bSubindex, 0, 0, OD_WRITE_NOT_ALLOWED);
+    accessDictionaryError(wIndex, bSubindex, 0, 0, OD_READ_NOT_ALLOWED);
     return OD_READ_NOT_ALLOWED;
   }
 
@@ -138,12 +140,13 @@ UNS32 _getODentry( CO_Data* d,
 
   if(*pExpectedSize == 0 ||
      *pExpectedSize == szData ||
-     (*pDataType == visible_string && *pExpectedSize < szData)) {
-    /* We
-      allow to fetch a shorter string than expected */
+     /* allow to fetch a shorter string than expected */
+     (*pDataType >= visible_string && *pExpectedSize < szData)) { 
 
 #  ifdef CANOPEN_BIG_ENDIAN
-    if(endianize && *pDataType > boolean && *pDataType < visible_string) {
+     if(endianize && *pDataType > boolean && !(
+            *pDataType >= visible_string && 
+            *pDataType <= domain)) {
       /* data must be transmited with low byte first */
       UNS8 i, j = 0;
       MSG_WAR(boolean, "data type ", *pDataType);
@@ -153,14 +156,33 @@ UNS32 _getODentry( CO_Data* d,
         ((UNS8*)pDestData)[j++] =
           ((UNS8*)ptrTable->pSubindex[bSubindex].pObject)[i-1];
       }
+      *pExpectedSize = szData;
     }
-    else /* It it is a visible string no endianisation to perform */
-      memcpy(pDestData, ptrTable->pSubindex[bSubindex].pObject,szData);
-#  else
-    memcpy(pDestData, ptrTable->pSubindex[bSubindex].pObject,szData);
+    else /* no endianisation change */
 #  endif
-
-    *pExpectedSize = szData;
+    if(*pDataType != visible_string) {
+        memcpy(pDestData, ptrTable->pSubindex[bSubindex].pObject,szData);
+        *pExpectedSize = szData;
+    }else{
+        /* TODO : CONFORM TO DS-301 : 
+         *  - stop using NULL terminated strings
+         *  - store string size in td_subindex 
+         * */
+        /* Copy null terminated string to user, and return discovered size */
+        UNS8 *ptr = (UNS8*)ptrTable->pSubindex[bSubindex].pObject;
+        UNS8 *ptr_start = ptr;
+        /* *pExpectedSize IS < szData . if null, use szData */
+        UNS8 *ptr_end = ptr + (*pExpectedSize ? *pExpectedSize : szData) ; 
+        UNS8 *ptr_dest = (UNS8*)pDestData;
+        while( *ptr && ptr < ptr_end){
+            *(ptr_dest++) = *(ptr++);
+        } 
+         
+        *pExpectedSize = ptr - ptr_start;
+        /* terminate string if not maximum length */
+        if (*pExpectedSize < szData) 
+            *(ptr) = 0; 
+    }
 
     return OD_SUCCESSFUL;
   }
@@ -172,68 +194,6 @@ UNS32 _getODentry( CO_Data* d,
   }
 }
 
-/*!
-**
-**
-** @param d
-** @param wIndex
-** @param bSubindex
-** @param pDestData
-** @param pExpectedSize
-** @param pDataType
-** @param checkAccess
-**
-** @return
-**/
-UNS32 getODentry( CO_Data* d,
-                  UNS16 wIndex,
-                  UNS8 bSubindex,
-                  void * pDestData,
-                  UNS8 * pExpectedSize,
-                  UNS8 * pDataType,
-                  UNS8 checkAccess)
-{
-  return _getODentry( d,
-                      wIndex,
-                      bSubindex,
-                      pDestData,
-                      pExpectedSize,
-                      pDataType,
-                      checkAccess,
-                      1);//endianize
-}
-
-/*!
-**
-**
-** @param d
-** @param wIndex
-** @param bSubindex
-** @param pDestData
-** @param pExpectedSize
-** @param pDataType
-** @param checkAccess
-**
-** @return
-**/
-UNS32 readLocalDict( CO_Data* d,
-                     UNS16 wIndex,
-                     UNS8 bSubindex,
-                     void * pDestData,
-                     UNS8 * pExpectedSize,
-                     UNS8 * pDataType,
-                     UNS8 checkAccess)
-{
-  return _getODentry( d,
-                      wIndex,
-                      bSubindex,
-                      pDestData,
-                      pExpectedSize,
-                      pDataType,
-                      checkAccess,
-                      0);//do not endianize
-}
-
 /*!
 **
 **
@@ -251,11 +211,11 @@ UNS32 _setODentry( CO_Data* d,
                    UNS16 wIndex,
                    UNS8 bSubindex,
                    void * pSourceData,
-                   UNS8 * pExpectedSize,
+                   UNS32 * pExpectedSize,
                    UNS8 checkAccess,
                    UNS8 endianize)
 {
-  UNS8 szData;
+  UNS32 szData;
   UNS8 dataType;
   UNS32 errorCode;
   const indextable *ptrTable;
@@ -282,11 +242,14 @@ UNS32 _setODentry( CO_Data* d,
 
   if( *pExpectedSize == 0 ||
       *pExpectedSize == szData ||
-      (dataType == visible_string && *pExpectedSize < szData)) /* We
-                                                                  allow to store a shorter string than entry size */
+      /* allow to store a shorter string than entry size */
+      (dataType == visible_string && *pExpectedSize < szData))
     {
 #ifdef CANOPEN_BIG_ENDIAN
-      if(endianize && dataType > boolean && dataType < visible_string)
+      /* re-endianize do not occur for bool, strings time and domains */
+      if(endianize && dataType > boolean && !(
+            dataType >= visible_string && 
+            dataType <= domain))
         {
           /* we invert the data source directly. This let us do range
             testing without */
@@ -306,12 +269,24 @@ UNS32 _setODentry( CO_Data* d,
         return errorCode;
       }
       memcpy(ptrTable->pSubindex[bSubindex].pObject,pSourceData, *pExpectedSize);
+     /* TODO : CONFORM TO DS-301 : 
+      *  - stop using NULL terminated strings
+      *  - store string size in td_subindex 
+      * */
+      /* terminate visible_string with '\0' */
+      if(dataType == visible_string && *pExpectedSize < szData)
+        ((UNS8*)ptrTable->pSubindex[bSubindex].pObject)[*pExpectedSize] = 0;
+      
       *pExpectedSize = szData;
 
       /* Callbacks */
       if(Callback && Callback[bSubindex]){
-        (*Callback[bSubindex])(d, ptrTable, bSubindex);
-      }
+        errorCode = (Callback[bSubindex])(d, ptrTable, bSubindex);
+        if(errorCode != OD_SUCCESSFUL)
+        {
+            return errorCode;
+        }
+       }
 
       /* TODO : Store dans NVRAM */
       if (ptrTable->pSubindex[bSubindex].bAccessType & TO_BE_SAVE){
@@ -325,62 +300,6 @@ UNS32 _setODentry( CO_Data* d,
     }
 }
 
-/*!
-**
-**
-** @param d
-** @param wIndex
-** @param bSubindex
-** @param pSourceData
-** @param pExpectedSize
-** @param checkAccess
-**
-** @return
-**/
-UNS32 setODentry( CO_Data* d,
-                  UNS16 wIndex,
-                  UNS8 bSubindex,
-                  void * pSourceData,
-                  UNS8 * pExpectedSize,
-                  UNS8 checkAccess)
-{
-  return _setODentry( d,
-                      wIndex,
-                      bSubindex,
-                      pSourceData,
-                      pExpectedSize,
-                      checkAccess,
-                      1);//endianize
-}
-
-/*!
-**
-**
-** @param d
-** @param wIndex
-** @param bSubindex
-** @param pSourceData
-** @param pExpectedSize
-** @param checkAccess
-**
-** @return
-**/
-UNS32 writeLocalDict( CO_Data* d,
-                      UNS16 wIndex,
-                      UNS8 bSubindex,
-                      void * pSourceData,
-                      UNS8 * pExpectedSize,
-                      UNS8 checkAccess)
-{
-  return _setODentry( d,
-                      wIndex,
-                      bSubindex,
-                      pSourceData,
-                      pExpectedSize,
-                      checkAccess,
-                      0);//do not endianize
-}
-
 /*!
 **
 **
@@ -408,11 +327,12 @@ const indextable * scanIndexOD (CO_Data* d, UNS16 wIndex, UNS32 *errorCode, ODCa
 **/
 UNS32 RegisterSetODentryCallBack(CO_Data* d, UNS16 wIndex, UNS8 bSubindex, ODCallback_t Callback)
 {
-  UNS32 errorCode;
-  ODCallback_t *CallbackList;
+UNS32 errorCode;
+ODCallback_t *CallbackList;
+const indextable *odentry;
 
-  scanIndexOD (d, wIndex, &errorCode, &CallbackList);
-  if(errorCode == OD_SUCCESSFUL && CallbackList)
+  odentry = scanIndexOD (d, wIndex, &errorCode, &CallbackList);
+  if(errorCode == OD_SUCCESSFUL  &&  CallbackList  &&  bSubindex < odentry->bSubCount) 
     CallbackList[bSubindex] = Callback;
   return errorCode;
 }