Added support for null terminated strings as VISIBLE_STRING.
authoretisserant
Thu, 20 Mar 2008 11:41:06 +0100
changeset 421 6221b4db8c42
parent 420 a42335b39bf4
child 422 43a4d6deb007
Added support for null terminated strings as VISIBLE_STRING.
Also fixed size declaration problem with visible_strings.
examples/DS401_Slave_Gui/ObjDict.c
objdictgen/gen_cfile.py
src/objacces.c
--- a/examples/DS401_Slave_Gui/ObjDict.c	Thu Mar 20 10:45:17 2008 +0100
+++ b/examples/DS401_Slave_Gui/ObjDict.c	Thu Mar 20 11:41:06 2008 +0100
@@ -167,21 +167,21 @@
                     UNS8 ObjDict_obj1008[10] = "";
                     subindex ObjDict_Index1008[] = 
                      {
-                       { RO, visible_string, 0, (void*)&ObjDict_obj1008 }
+                       { RO, visible_string, 10, (void*)&ObjDict_obj1008 }
                      };
 
 /* index 0x1009 :   Manufacturer Hardware Version. */
                     UNS8 ObjDict_obj1009[10] = "";
                     subindex ObjDict_Index1009[] = 
                      {
-                       { RO, visible_string, 0, (void*)&ObjDict_obj1009 }
+                       { RO, visible_string, 10, (void*)&ObjDict_obj1009 }
                      };
 
 /* index 0x100A :   Manufacturer Software Version. */
                     UNS8 ObjDict_obj100A[10] = "";
                     subindex ObjDict_Index100A[] = 
                      {
-                       { RO, visible_string, 0, (void*)&ObjDict_obj100A }
+                       { RO, visible_string, 10, (void*)&ObjDict_obj100A }
                      };
 
 /* index 0x100C :   Guard Time. */
--- a/objdictgen/gen_cfile.py	Thu Mar 20 10:45:17 2008 +0100
+++ b/objdictgen/gen_cfile.py	Thu Mar 20 11:41:06 2008 +0100
@@ -231,7 +231,7 @@
                             if subIndex == len(values)-1:
                                 sep = ""
                             if typeinfos[2] == "visible_string":
-                                value = "\"%s\""%value
+                                value = "\"%s%s\""%(value, "\\0" * (default_string_size - len(value)))
                             elif typeinfos[2] == "domain":
                                 value = "\"%s\""%''.join(["\\x%2.2x"%ord(char) for char in value])
                             else:
@@ -248,7 +248,7 @@
                             if subIndex == len(values)-1:
                                 sep = ""
                             if typeinfos[2] == "visible_string":
-                                value = "\"%s\""%value
+                                value = "\"%s%s\""%(value, "\\0" * (default_string_size - len(value)))
                             elif typeinfos[2] == "domain":
                                 value = "\"%s\""%''.join(["\\x%2.2x"%ord(char) for char in value])
                             else:
@@ -272,7 +272,7 @@
                         else:
                             texts["suffixe"] = ""
                         if typeinfos[2] == "visible_string":
-                            texts["value"] = "\"%s\""%value
+                            texts["value"] = "\"%s%s\""%(value, "\\0" * (default_string_size - len(value)))
                             texts["comment"] = ""
                         elif typeinfos[2] == "domain":
                             texts["value"] = "\"%s\""%''.join(["\\x%2.2x"%ord(char) for char in value])
@@ -333,8 +333,10 @@
                     name = FormatName("%s_%s"%(entry_infos["name"],subentry_infos["name"]))
                 else:
                     name = "%s_obj%04X_%s"%(texts["NodeName"], texts["index"], FormatName(subentry_infos["name"]))
-            if typeinfos[2] in ["visible_string", "domain"]:
-                sizeof = typeinfos[1]
+            if typeinfos[2] == "visible_string":
+                sizeof = str(max(len(values[subIndex]), default_string_size))
+            elif typeinfos[2] == "domain":
+                sizeof = str(len(values[subIndex]))
             else:
                 sizeof = "sizeof (%s)"%typeinfos[0]
             params = Node.GetParamsEntry(index, subIndex)
--- a/src/objacces.c	Thu Mar 20 10:45:17 2008 +0100
+++ b/src/objacces.c	Thu Mar 20 11:41:06 2008 +0100
@@ -138,12 +138,13 @@
 
   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 && dataType > boolean && !(
+            dataType >= visible_string && 
+            domain <= dataType)) {
       /* data must be transmited with low byte first */
       UNS8 i, j = 0;
       MSG_WAR(boolean, "data type ", *pDataType);
@@ -156,7 +157,7 @@
     }
     else /* no endianisation change */
 #  endif
-    if(*pDataType < visible_string) {
+    if(*pDataType != visible_string) {
         memcpy(pDestData, ptrTable->pSubindex[bSubindex].pObject,szData);
         *pExpectedSize = szData;
     }else{
@@ -171,7 +172,11 @@
         while( *ptr && ptr < ptr_end){
             *((UNS8*)pDestData++) = *(ptr++);
         } 
-        *pExpectedSize = ptr - ptr_start; 
+         
+        *pExpectedSize = ptr - ptr_start;
+        /* terminate string if not maximum length */
+        if (*pExpectedSize < szData) 
+            *(ptr) = 0; 
     }
 
     return OD_SUCCESSFUL;
@@ -294,11 +299,14 @@
 
   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 && 
+            domain <= dataType))
         {
           /* we invert the data source directly. This let us do range
             testing without */
@@ -318,6 +326,14 @@
         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 */