]> rtime.felk.cvut.cz Git - CanFestival-3.git/blob - objdictgen/gen_cfile.py
Bug on gen_cfile.py valueRangeTest generation fixed
[CanFestival-3.git] / objdictgen / gen_cfile.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 #This file is part of CanFestival, a library implementing CanOpen Stack. 
5 #
6 #Copyright (C): Edouard TISSERANT and Francis DUPIN
7 #
8 #See COPYING file for copyrights details.
9 #
10 #This library is free software; you can redistribute it and/or
11 #modify it under the terms of the GNU Lesser General Public
12 #License as published by the Free Software Foundation; either
13 #version 2.1 of the License, or (at your option) any later version.
14 #
15 #This library is distributed in the hope that it will be useful,
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 #Lesser General Public License for more details.
19 #
20 #You should have received a copy of the GNU Lesser General Public
21 #License along with this library; if not, write to the Free Software
22 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
24 from node import *
25 from types import *
26
27 import re, os
28
29 word_model = re.compile('([a-zA-Z_0-9]*)')
30 type_model = re.compile('([\_A-Z]*)([0-9]*)')
31 range_model = re.compile('([\_A-Z]*)([0-9]*)\[([\-0-9]*)-([\-0-9]*)\]')
32
33 categories = [("SDO_SVR", 0x1200, 0x127F), ("SDO_CLT", 0x1280, 0x12FF),
34               ("PDO_RCV", 0x1400, 0x15FF), ("PDO_RCV_MAP", 0x1600, 0x17FF),
35               ("PDO_TRS", 0x1800, 0x19FF), ("PDO_TRS_MAP", 0x1A00, 0x1BFF)]
36 index_categories = ["firstIndex", "lastIndex"]
37
38 generated_tag = """\n/* File generated by gen_cfile.py. Should not be modified. */\n"""
39
40 internal_types = {}
41
42 # Format a string for making a C++ variable
43 def FormatName(name):
44     wordlist = [word for word in word_model.findall(name) if word != '']
45     return "_".join(wordlist)
46
47 # Extract the informations from a given type name
48 def GetValidTypeInfos(typename):
49     if typename in internal_types:
50         return internal_types[typename]
51     else:
52         result = type_model.match(typename)
53         if result:
54             values = result.groups()
55             if values[0] == "UNSIGNED" and int(values[1]) in [i * 8 for i in xrange(1, 9)]:
56                 typeinfos = ("UNS%s"%values[1], "", "uint%s"%values[1])
57             elif values[0] == "INTEGER" and int(values[1]) in [i * 8 for i in xrange(1, 9)]:
58                 typeinfos = ("INTEGER%s"%values[1], "", "int%s"%values[1])
59             elif values[0] == "REAL" and int(values[1]) in (32, 64):
60                 typeinfos = ("%s%s"%(values[0], values[1]), "", "real%s"%values[1])
61             elif values[0] == "VISIBLE_STRING":
62                 if values[1] == "":
63                     typeinfos = ("UNS8", "[10]", "visible_string")
64                 else:
65                     typeinfos = ("UNS8", "[%s]"%values[1], "visible_string")
66             elif values[0] == "DOMAIN":
67                 typeinfos = ("UNS8*", "", "domain")
68             elif values[0] == "BOOLEAN":
69                 typeinfos = ("UNS8", "", "boolean")
70             else:
71                 raise ValueError, """!!! %s isn't a valid type for CanFestival."""%typename
72             internal_types[typename] = typeinfos
73         else:
74             raise ValueError, """!!! %s isn't a valid type for CanFestival."""%typename
75     return typeinfos
76
77 def WriteFile(filepath, content):
78     cfile = open(filepath,"w")
79     cfile.write(content)
80     cfile.close()
81
82 def GenerateFileContent(Node, headerfilepath):
83     global type
84     global internal_types
85     texts = {}
86     texts["maxPDOtransmit"] = 0
87     texts["NodeName"] = Node.GetNodeName()
88     texts["NodeID"] = Node.GetNodeID()
89     texts["NodeType"] = Node.GetNodeType()
90     texts["Description"] = Node.GetNodeDescription()
91     texts["iam_a_slave"] = 0
92     if (texts["NodeType"] == "slave"):
93         texts["iam_a_slave"] = 1
94     
95     # Compiling lists of indexes
96     rangelist = [idx for idx in Node.GetIndexes() if 0 <= idx <= 0x260]
97     listIndex = [idx for idx in Node.GetIndexes() if 0x1000 <= idx <= 0xFFFF]
98     communicationlist = [idx for idx in Node.GetIndexes() if 0x1000 <= idx <= 0x11FF]
99     sdolist = [idx for idx in Node.GetIndexes() if 0x1200 <= idx <= 0x12FF]
100     pdolist = [idx for idx in Node.GetIndexes() if 0x1400 <= idx <= 0x1BFF]
101     variablelist = [idx for idx in Node.GetIndexes() if 0x2000 <= idx <= 0xBFFF]
102
103 #-------------------------------------------------------------------------------
104 #                       Declaration of the value range types
105 #-------------------------------------------------------------------------------    
106     
107     valueRangeContent = ""
108     strDefine = "\n#define valueRange_EMC 0x9F /* Type for index 0x1003 subindex 0x00 (only set of value 0 is possible) */"
109     strSwitch = """    case valueRange_EMC:
110       if (*(UNS8*)value < (UNS8)0) return OD_VALUE_TOO_LOW;
111       if (*(UNS8*)value > (UNS8)0) return OD_VALUE_TOO_HIGH;
112       break;\n"""
113     internal_types["valueRange_EMC"] = ("UNS8", "", "valueRange_EMC")
114     num = 0
115     for index in rangelist:
116         rangename = Node.GetEntryName(index)
117         result = range_model.match(rangename)
118         if result:
119             num += 1
120             typeindex = Node.GetEntry(index, 1)
121             typename = Node.GetTypeName(typeindex)
122             typeinfos = GetValidTypeInfos(typename)
123             internal_types[rangename] = (typeinfos[0], typeinfos[1], "valueRange_%d"%num)
124             minvalue = str(Node.GetEntry(index, 2))
125             maxvalue = str(Node.GetEntry(index, 3))
126             strDefine += "\n#define valueRange_%d 0x%02X /* Type %s, %s < value < %s */"%(num,index,typeinfos[0],minvalue,maxvalue)
127             strSwitch += """    case valueRange_%d:
128       if (*(%s*)value < (%s)%s) return OD_VALUE_TOO_LOW;
129       if (*(%s*)value > (%s)%s) return OD_VALUE_TOO_HIGH;
130       break;\n"""%(num,typeinfos[0],typeinfos[0],minvalue,typeinfos[0],typeinfos[0],maxvalue)
131
132     valueRangeContent += strDefine
133     valueRangeContent += "\nUNS32 %(NodeName)s_valueRangeTest (UNS8 typeValue, void * value)\n{"%texts
134     valueRangeContent += "\n  switch (typeValue) {\n"
135     valueRangeContent += strSwitch
136     valueRangeContent += "  }\n  return 0;\n}\n"
137
138 #-------------------------------------------------------------------------------
139 #            Creation of the mapped variables and object dictionary
140 #-------------------------------------------------------------------------------
141
142     mappedVariableContent = ""
143     strDeclareHeader = ""
144     strDeclareCallback = ""
145     indexContents = {}
146     indexCallbacks = {}
147     for index in listIndex:
148         texts["index"] = index
149         strIndex = ""
150         entry_infos = Node.GetEntryInfos(index)
151         texts["EntryName"] = entry_infos["name"].encode('ascii','replace')
152         values = Node.GetEntry(index)
153         callbacks = Node.HasEntryCallbacks(index)
154         if index in variablelist:
155             strIndex += "\n/* index 0x%(index)04X :   Mapped variable %(EntryName)s */\n"%texts
156         else:
157             strIndex += "\n/* index 0x%(index)04X :   %(EntryName)s. */\n"%texts
158         
159         # Entry type is VAR
160         if type(values) != ListType:
161             subentry_infos = Node.GetSubentryInfos(index, 0)
162             typename = Node.GetTypeName(subentry_infos["type"])
163             typeinfos = GetValidTypeInfos(typename)
164             texts["subIndexType"] = typeinfos[0]
165             texts["suffixe"] = typeinfos[1]
166             if typeinfos[2] == "visible_string":
167                 texts["value"] = "\"%s\""%values
168                 texts["comment"] = ""
169             elif typeinfos[2] == "domain":
170                 texts["value"] = "\"%s\""%''.join(["\\x%2.2x"%ord(char) for char in value])
171                 texts["comment"] = ""
172             else:
173                 texts["value"] = "0x%X"%values
174                 texts["comment"] = "\t/* %s */"%str(values)
175             if index in variablelist:
176                 texts["name"] = FormatName(subentry_infos["name"])
177                 strDeclareHeader += "extern %(subIndexType)s %(name)s%(suffixe)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x00*/\n"%texts
178                 if callbacks:
179                     strDeclareHeader += "extern ODCallback_t %(name)s_callbacks[];\t\t/* Callbacks of index0x%(index)04X */\n"%texts
180                 mappedVariableContent += "%(subIndexType)s %(name)s%(suffixe)s = %(value)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x00 */\n"%texts
181             else:
182                 strIndex += "                    %(subIndexType)s %(NodeName)s_obj%(index)04X%(suffixe)s = %(value)s;%(comment)s\n"%texts
183             values = [values]
184         else:
185             subentry_infos = Node.GetSubentryInfos(index, 0)
186             typename = Node.GetTypeName(subentry_infos["type"])
187             typeinfos = GetValidTypeInfos(typename)
188             if index == 0x1003:
189                 texts["value"] = 0
190             else:
191                 texts["value"] = values[0]
192             texts["subIndexType"] = typeinfos[0]
193             strIndex += "                    %(subIndexType)s %(NodeName)s_highestSubIndex_obj%(index)04X = %(value)d; /* number of subindex - 1*/\n"%texts
194             
195             # Entry type is RECORD
196             if entry_infos["struct"] & OD_IdenticalSubindexes:
197                 subentry_infos = Node.GetSubentryInfos(index, 1)
198                 typename = Node.GetTypeName(subentry_infos["type"])
199                 typeinfos = GetValidTypeInfos(typename)
200                 texts["subIndexType"] = typeinfos[0]
201                 texts["suffixe"] = typeinfos[1]
202                 texts["length"] = values[0]
203                 if index in variablelist:
204                     texts["name"] = FormatName(entry_infos["name"])
205                     strDeclareHeader += "extern %(subIndexType)s %(name)s[%(length)d]%(suffixe)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x01 - 0x%(length)02X */\n"%texts
206                     if callbacks:
207                         strDeclareHeader += "extern ODCallback_t %(name)s_callbacks[];\t\t/* Callbacks of index0x%(index)04X */\n"%texts
208                     mappedVariableContent += "%(subIndexType)s %(name)s[] =\t\t/* Mapped at index 0x%(index)04X, subindex 0x01 - 0x%(length)02X */\n  {\n"%texts
209                     for subIndex, value in enumerate(values):
210                         sep = ","
211                         comment = ""
212                         if subIndex > 0:
213                             if subIndex == len(values)-1:
214                                 sep = ""
215                             if typeinfos[2] == "visible_string":
216                                 value = "\"%s\""%value
217                             elif typeinfos[2] == "domain":
218                                 value = "\"%s\""%''.join(["\\x%2.2x"%ord(char) for char in value])
219                             else:
220                                 comment = "\t/* %s */"%str(value)
221                                 value = "0x%X"%value
222                             mappedVariableContent += "    %s%s%s\n"%(value, sep, comment)
223                     mappedVariableContent += "  };\n"
224                 else:
225                     strIndex += "                    %(subIndexType)s %(NodeName)s_obj%(index)04X[] = \n                    {\n"%texts
226                     for subIndex, value in enumerate(values):
227                         sep = ","
228                         comment = ""
229                         if subIndex > 0:
230                             if subIndex == len(values)-1:
231                                 sep = ""
232                             if typeinfos[2] == "visible_string":
233                                 value = "\"%s\""%value
234                             elif typeinfos[2] == "domain":
235                                 value = "\"%s\""%''.join(["\\x%2.2x"%ord(char) for char in value])
236                             else:
237                                 comment = "\t/* %s */"%str(value)
238                                 value = "0x%X"%value
239                             strIndex += "                      %s%s%s\n"%(value, sep, comment)
240                     strIndex += "                    };\n"
241             else:
242                 
243                 texts["parent"] = FormatName(entry_infos["name"])
244                 # Entry type is ARRAY
245                 for subIndex, value in enumerate(values):
246                     texts["subIndex"] = subIndex
247                     if subIndex > 0:
248                         subentry_infos = Node.GetSubentryInfos(index, subIndex)
249                         typename = Node.GetTypeName(subentry_infos["type"])
250                         typeinfos = GetValidTypeInfos(typename)
251                         texts["subIndexType"] = typeinfos[0]
252                         texts["suffixe"] = typeinfos[1]
253                         if typeinfos[2] == "visible_string":
254                             texts["value"] = "\"%s\""%value
255                             texts["comment"] = ""
256                         elif typeinfos[2] == "domain":
257                             texts["value"] = "\"%s\""%''.join(["\\x%2.2x"%ord(char) for char in value])
258                             texts["comment"] = ""                       
259                         else:
260                             texts["value"] = "0x%X"%value
261                             texts["comment"] = "\t/* %s */"%str(value)
262                         texts["name"] = FormatName(subentry_infos["name"])
263                         if index in variablelist:
264                             strDeclareHeader += "extern %(subIndexType)s %(parent)s_%(name)s%(suffixe)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x%(subIndex)02X */\n"%texts
265                             mappedVariableContent += "%(subIndexType)s %(parent)s_%(name)s%(suffixe)s = %(value)s;\t\t/* Mapped at index 0x%(index)04X, subindex 0x%(subIndex)02X */\n"%texts
266                         else:
267                             strIndex += "                    %(subIndexType)s %(NodeName)s_obj%(index)04X_%(name)s%(suffixe)s = %(value)s;%(comment)s\n"%texts
268                 if callbacks:
269                     strDeclareHeader += "extern ODCallback_t %(parent)s_callbacks[];\t\t/* Callbacks of index0x%(index)04X */\n"%texts
270         
271         # Generating Dictionary C++ entry
272         if callbacks:
273             if index in variablelist:
274                 name = FormatName(entry_infos["name"])
275             else:
276                 name = "%(NodeName)s_Index%(index)04X"%texts
277             strIndex += "                    ODCallback_t %s_callbacks[] = \n                     {\n"%name
278             for subIndex in xrange(len(values)):
279                 strIndex += "                       NULL,\n"
280             strIndex += "                     };\n"
281             indexCallbacks[index] = "*callbacks = %s_callbacks; "%name
282         else:
283             indexCallbacks[index] = ""
284         strIndex += "                    subindex %(NodeName)s_Index%(index)04X[] = \n                     {\n"%texts
285         for subIndex in xrange(len(values)):
286             subentry_infos = Node.GetSubentryInfos(index, subIndex)
287             if subIndex < len(values) - 1:
288                 sep = ","
289             else:
290                 sep = ""
291             typename = Node.GetTypeName(subentry_infos["type"])
292             typeinfos = GetValidTypeInfos(typename)
293             if subIndex == 0:
294                 if index == 0x1003:
295                     typeinfos = GetValidTypeInfos("valueRange_EMC")
296                 if entry_infos["struct"] & OD_MultipleSubindexes:
297                     name = "%(NodeName)s_highestSubIndex_obj%(index)04X"%texts
298                 elif index in variablelist:
299                     name = FormatName(subentry_infos["name"])
300                 else:
301                     name = FormatName("%s_obj%04X"%(texts["NodeName"], texts["index"]))
302             elif entry_infos["struct"] & OD_IdenticalSubindexes:
303                 if index in variablelist:
304                     name = "%s[%d]"%(FormatName(entry_infos["name"]), subIndex - 1)
305                 else:
306                     name = "%s_obj%04X[%d]"%(texts["NodeName"], texts["index"], subIndex - 1)
307             else:
308                 if index in variablelist:
309                     name = FormatName("%s_%s"%(entry_infos["name"],subentry_infos["name"]))
310                 else:
311                     name = "%s_obj%04X_%s"%(texts["NodeName"], texts["index"], FormatName(subentry_infos["name"]))
312             if typeinfos[2] in ["visible_string", "domain"]:
313                 sizeof = str(len(values[subIndex]))
314             else:
315                 sizeof = "sizeof (%s)"%typeinfos[0]
316             params = Node.GetParamsEntry(index, subIndex)
317             if params["save"]:
318                 save = "|TO_BE_SAVE"
319             else:
320                 save = ""
321             strIndex += "                       { %s%s, %s, %s, (void*)&%s }%s\n"%(subentry_infos["access"].upper(),save,typeinfos[2],sizeof,name,sep)
322         strIndex += "                     };\n"
323         indexContents[index] = strIndex
324
325 #-------------------------------------------------------------------------------
326 #                     Declaration of Particular Parameters
327 #-------------------------------------------------------------------------------
328
329     if 0x1003 not in communicationlist:
330         entry_infos = Node.GetEntryInfos(0x1003)
331         texts["EntryName"] = entry_infos["name"]
332         indexContents[0x1003] = """\n/* index 0x1003 :   %(EntryName)s */
333                     UNS8 %(NodeName)s_highestSubIndex_obj1003 = 0; /* number of subindex - 1*/
334                     UNS32 %(NodeName)s_obj1003[] = 
335                     {
336                       0x0       /* 0 */
337                     };
338                     ODCallback_t %(NodeName)s_Index1003_callbacks[] = 
339                      {
340                        NULL,
341                        NULL,
342                      };
343                     subindex %(NodeName)s_Index1003[] = 
344                      {
345                        { RW, valueRange_EMC, sizeof (UNS8), (void*)&%(NodeName)s_highestSubIndex_obj1003 },
346                        { RO, uint32, sizeof (UNS32), (void*)&%(NodeName)s_obj1003[0] }
347                      };
348 """%texts
349
350     if 0x1005 not in communicationlist:
351         entry_infos = Node.GetEntryInfos(0x1005)
352         texts["EntryName"] = entry_infos["name"]
353         indexContents[0x1005] = """\n/* index 0x1005 :   %(EntryName)s */
354                     UNS32 %(NodeName)s_obj1005 = 0x0;   /* 0 */
355 """%texts
356
357     if 0x1006 not in communicationlist:
358         entry_infos = Node.GetEntryInfos(0x1006)
359         texts["EntryName"] = entry_infos["name"]
360         indexContents[0x1006] = """\n/* index 0x1006 :   %(EntryName)s */
361                     UNS32 %(NodeName)s_obj1006 = 0x0;   /* 0 */
362 """%texts
363
364     if 0x1016 in communicationlist:
365         texts["nombre"] = Node.GetEntry(0x1016, 0)
366     else:
367         texts["nombre"] = 0
368         entry_infos = Node.GetEntryInfos(0x1016)
369         texts["EntryName"] = entry_infos["name"]
370         indexContents[0x1016] = """\n/* index 0x1016 :   %(EntryName)s */
371                     UNS8 %(NodeName)s_highestSubIndex_obj1016 = 0;
372                     UNS32 %(NodeName)s_obj1016[]={0};
373 """%texts
374     if texts["nombre"] > 0:
375         strTimers = "TIMER_HANDLE %(NodeName)s_heartBeatTimers[%(nombre)d] = {TIMER_NONE,};\n"%texts
376     else:
377         strTimers = "TIMER_HANDLE %(NodeName)s_heartBeatTimers[1];\n"%texts
378
379     if 0x1017 not in communicationlist:
380         entry_infos = Node.GetEntryInfos(0x1017)
381         texts["EntryName"] = entry_infos["name"]
382         indexContents[0x1017] = """\n/* index 0x1017 :   %(EntryName)s */ 
383                     UNS16 %(NodeName)s_obj1017 = 0x0;   /* 0 */
384 """%texts
385
386 #-------------------------------------------------------------------------------
387 #               Declaration of navigation in the Object Dictionary
388 #-------------------------------------------------------------------------------
389
390     strDeclareIndex = ""
391     strDeclareSwitch = ""
392     strQuickIndex = ""
393     quick_index = {}
394     for index_cat in index_categories:
395         quick_index[index_cat] = {}
396         for cat, idx_min, idx_max in categories:
397             quick_index[index_cat][cat] = 0
398     maxPDOtransmit = 0
399     for i, index in enumerate(listIndex):
400         texts["index"] = index
401         strDeclareIndex += "  { (subindex*)%(NodeName)s_Index%(index)04X,sizeof(%(NodeName)s_Index%(index)04X)/sizeof(%(NodeName)s_Index%(index)04X[0]), 0x%(index)04X},\n"%texts
402         strDeclareSwitch += "           case 0x%04X: i = %d;%sbreak;\n"%(index, i, indexCallbacks[index])
403         for cat, idx_min, idx_max in categories:
404             if idx_min <= index <= idx_max:
405                 quick_index["lastIndex"][cat] = i
406                 if quick_index["firstIndex"][cat] == 0:
407                     quick_index["firstIndex"][cat] = i
408                 if cat == "PDO_TRS":
409                     maxPDOtransmit += 1
410     texts["maxPDOtransmit"] = max(1, maxPDOtransmit)
411     for index_cat in index_categories:
412         strQuickIndex += "\nquick_index %s_%s = {\n"%(texts["NodeName"], index_cat)
413         sep = ","
414         for i, (cat, idx_min, idx_max) in enumerate(categories):
415             if i == len(categories) - 1:
416                 sep = ""
417             strQuickIndex += "  %d%s /* %s */\n"%(quick_index[index_cat][cat],sep,cat)
418         strQuickIndex += "};\n"
419
420 #-------------------------------------------------------------------------------
421 #                            Write File Content
422 #-------------------------------------------------------------------------------
423
424     fileContent = generated_tag + """
425 #include "%s"
426 """%(headerfilepath)
427
428     fileContent += """
429 /**************************************************************************/
430 /* Declaration of the mapped variables                                    */
431 /**************************************************************************/
432 """ + mappedVariableContent
433
434     fileContent += """
435 /**************************************************************************/
436 /* Declaration of the value range types                                   */
437 /**************************************************************************/
438 """ + valueRangeContent
439
440     fileContent += """
441 /**************************************************************************/
442 /* The node id                                                            */
443 /**************************************************************************/
444 /* node_id default value.*/
445 UNS8 %(NodeName)s_bDeviceNodeId = 0x%(NodeID)02X;
446
447 /**************************************************************************/
448 /* Array of message processing information */
449
450 const UNS8 %(NodeName)s_iam_a_slave = %(iam_a_slave)d;
451
452 """%texts
453     fileContent += strTimers
454     
455     fileContent += """
456 /*
457 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
458
459                                OBJECT DICTIONARY
460
461 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
462 */
463 """%texts
464     contentlist = indexContents.keys()
465     contentlist.sort()
466     for index in contentlist:
467         fileContent += indexContents[index]
468
469     fileContent += """
470 const indextable %(NodeName)s_objdict[] = 
471 {
472 """%texts
473     fileContent += strDeclareIndex
474     fileContent += """};
475
476 const indextable * %(NodeName)s_scanIndexOD (UNS16 wIndex, UNS32 * errorCode, ODCallback_t **callbacks)
477 {
478         int i;
479         *callbacks = NULL;
480         switch(wIndex){
481 """%texts
482     fileContent += strDeclareSwitch
483     fileContent += """          default:
484                         *errorCode = OD_NO_SUCH_OBJECT;
485                         return NULL;
486         }
487         *errorCode = OD_SUCCESSFUL;
488         return &%(NodeName)s_objdict[i];
489 }
490
491 /* 
492  * To count at which received SYNC a PDO must be sent.
493  * Even if no pdoTransmit are defined, at least one entry is computed
494  * for compilations issues.
495  */
496 s_PDO_status %(NodeName)s_PDO_status[%(maxPDOtransmit)d] = {"""%texts
497
498     fileContent += ",".join(["s_PDO_staus_Initializer"]*texts["maxPDOtransmit"]) + """};
499 """
500
501     fileContent += strQuickIndex
502     fileContent += """
503 UNS16 %(NodeName)s_ObjdictSize = sizeof(%(NodeName)s_objdict)/sizeof(%(NodeName)s_objdict[0]); 
504
505 CO_Data %(NodeName)s_Data = CANOPEN_NODE_DATA_INITIALIZER(%(NodeName)s);
506
507 """%texts
508
509 #-------------------------------------------------------------------------------
510 #                          Write Header File Content
511 #-------------------------------------------------------------------------------
512
513     texts["file_include_name"] = headerfilepath.replace(".", "_").upper()
514     HeaderFileContent = generated_tag + """
515 #ifndef %(file_include_name)s
516 #define %(file_include_name)s
517
518 #include "data.h"
519
520 /* Prototypes of function provided by object dictionnary */
521 UNS32 %(NodeName)s_valueRangeTest (UNS8 typeValue, void * value);
522 const indextable * %(NodeName)s_scanIndexOD (UNS16 wIndex, UNS32 * errorCode, ODCallback_t **callbacks);
523
524 /* Master node data struct */
525 extern CO_Data %(NodeName)s_Data;
526 """%texts
527     HeaderFileContent += strDeclareHeader
528     
529     HeaderFileContent += "\n#endif // %(file_include_name)s\n"%texts
530     
531     return fileContent,HeaderFileContent
532
533 #-------------------------------------------------------------------------------
534 #                             Main Function
535 #-------------------------------------------------------------------------------
536
537 def GenerateFile(filepath, node):
538     try:
539         headerfilepath = os.path.splitext(filepath)[0]+".h"
540         content, header = GenerateFileContent(node, os.path.split(headerfilepath)[1])
541         WriteFile(filepath, content)
542         WriteFile(headerfilepath, header)
543         return None
544     except ValueError, message:
545         return "Unable to Generate C File\n%s"%message
546