Esempio n. 1
0
 def _initDefaultType(self):
     self.defaultTypes['Boolean'] = C.sequence().extend(
         [C.statement(C.typedef('boolean', 'Boolean'))])
     self.defaultTypes['UInt8'] = C.sequence().extend([
         C.statement(C.typedef('uint8', 'UInt8')),
         C.define('UInt8_LowerLimit', '((UInt8)0u)'),
         C.define('UInt8_UpperLimit', '((UInt8)255u)')
     ])
     self.defaultTypes['UInt16'] = C.sequence().extend([
         C.statement(C.typedef('uint16', 'UInt16')),
         C.define('UInt16_LowerLimit', '((UInt16)0u)'),
         C.define('UInt16_UpperLimit', '((UInt16)65535u)')
     ])
     self.defaultTypes['UInt32'] = C.sequence().extend([
         C.statement(C.typedef('uint32', 'UInt32')),
         C.define('UInt32_LowerLimit', '((UInt32)0u)'),
         C.define('UInt32_UpperLimit', '((UInt32)4294967295u)')
     ])
     self.defaultTypes['SInt8'] = C.sequence().extend([
         C.statement(C.typedef('sint8', 'SInt8')),
         C.define('SInt8_LowerLimit', '((SInt8)-128)'),
         C.define('SInt8_UpperLimit', '((SInt8)127)')
     ])
     self.defaultTypes['SInt16'] = C.sequence().extend([
         C.statement(C.typedef('sint16', 'SInt16')),
         C.define('SInt16_LowerLimit', '((SInt16)-32768)'),
         C.define('SInt16_UpperLimit', '((SInt16)32767)')
     ])
     self.defaultTypes['SInt32'] = C.sequence().extend([
         C.statement(C.typedef('sint32', 'SInt32')),
         C.define('SInt32_LowerLimit', '((SInt32)-2147483648)'),
         C.define('SInt32_UpperLimit', '((SInt32)2147483647)')
     ])
Esempio n. 2
0
    def generate(self, dest_dir='.', file_name='Rte_Type.h'):
        """
      Generates Rte_Type.h
      Note: The last argument has been deprecated and is no longer in use
      """
        if self.partition.isFinalized == False:
            self.partition.finalize()
        file_path = os.path.join(dest_dir, file_name)
        with io.open(file_path, 'w', newline='\n') as fp:
            hfile = C.hfile(file_name)
            hfile.code.extend(
                [C.line(x) for x in _genCommentHeader('Includes')])
            hfile.code.append(C.include("Std_Types.h"))
            hfile.code.append(C.blank())
            (basicTypes, complexTypes,
             modeTypes) = self.partition.types.getTypes()
            hfile.code.extend([
                C.line(x) for x in _genCommentHeader('Data Type Definitions')
            ])
            hfile.code.append(C.blank())
            ws = self.partition.ws
            unusedDefaultTypes = self._findUnusedDefaultTypes(ws, basicTypes)

            first = True
            for ref in sorted(basicTypes) + sorted(complexTypes):
                dataType = ws.find(ref)
                if dataType is not None:
                    typedef = None
                    if first:
                        first = False
                    else:
                        hfile.code.append(C.blank())
                    hfile.code.append('#define Rte_TypeDef_%s' % dataType.name)
                    if isinstance(dataType, autosar.datatype.BooleanDataType):
                        typedef = C.typedef('boolean', dataType.name)
                        hfile.code.append(C.statement(typedef))
                    elif isinstance(dataType,
                                    autosar.datatype.IntegerDataType):
                        valrange = dataType.maxVal - dataType.minVal
                        bitcount = valrange.bit_length()
                        typename = dataType.name
                        basetype = self._typename(bitcount, dataType.minVal)
                        typedef = C.typedef(basetype, typename)
                        hfile.code.append(C.statement(typedef))
                        isUnsigned = True if basetype in ('uint8', 'uint16',
                                                          'uint32') else False
                        if isUnsigned:
                            minval = str(dataType.minVal) + 'u'
                            maxval = str(dataType.maxVal) + 'u'
                        else:
                            minval = str(dataType.minVal)
                            maxval = str(dataType.maxVal)
                        hfile.code.append('#define %s_LowerLimit ((%s)%s)' %
                                          (typename, typename, minval))
                        hfile.code.append('#define %s_UpperLimit ((%s)%s)' %
                                          (typename, typename, maxval))
                        if dataType.compuMethodRef is not None:
                            compuMethod = ws.find(dataType.compuMethodRef)
                            if compuMethod is not None:
                                lines1 = []
                                lines2 = []
                                if isinstance(
                                        compuMethod,
                                        autosar.datatype.CompuMethodConst):
                                    for elem in compuMethod.elements:
                                        if isUnsigned:
                                            value = str(elem.upperLimit) + 'u'
                                        else:
                                            value = str(elem.upperLimit)
                                        lines1.append(
                                            '#define RTE_CONST_%s (%s)' %
                                            (elem.textValue, value))
                                        lines2.append(
                                            '#define %s ((%s)%s)' %
                                            (elem.textValue, typename, value))
                                if len(lines2) > 0:
                                    tmp = lines1 + [C.blank()] + lines2
                                else:
                                    tmp = lines1
                                for line in tmp:
                                    hfile.code.append(line)
                            else:
                                raise ValueError(dataType.compuMethodRef)
                    elif isinstance(dataType, autosar.datatype.RecordDataType):
                        body = C.block(innerIndent=innerIndentDefault)
                        for elem in dataType.elements:
                            childType = ws.find(elem.typeRef, role='DataType')
                            body.append(
                                C.statement(
                                    C.variable(elem.name, childType.name)))
                        struct = C.struct(None, body, typedef=dataType.name)
                        hfile.code.append(C.statement(struct))
                    elif isinstance(dataType, autosar.datatype.StringDataType):
                        hfile.code.append('typedef uint8 %s[%d];' %
                                          (dataType.name, dataType.length + 1))
                    elif isinstance(dataType, autosar.datatype.ArrayDataType):
                        childType = ws.find(dataType.typeRef, role='DataType')
                        if childType is None:
                            raise ValueError('invalid type reference: ' +
                                             dataType.typeRef)
                        hfile.code.append(
                            'typedef %s %s[%d];' %
                            (childType.name, dataType.name, dataType.length))
                    elif isinstance(dataType, autosar.datatype.RealDataType):
                        if dataType.encoding == 'DOUBLE':
                            platform_typename = 'float64'
                        else:
                            platform_typename = 'float32'
                        hfile.code.append('typedef %s %s;' %
                                          (platform_typename, dataType.name))
                    else:
                        raise NotImplementedError(type(dataType))
                        #sys.stderr.write('not implemented: %s\n'%str(type(dataType)))
                else:
                    raise ValueError(ref)

            if len(modeTypes) > 0:
                lines = _genCommentHeader('Mode Types')
                tmp = []
                hfile.code.extend(lines)
                first = True
                for ref in modeTypes:
                    if first:
                        first = False
                    else:
                        tmp.append(C.blank())
                    modeType = ws.find(ref)
                    hfile.code.append(
                        C.statement(
                            C.typedef('uint8',
                                      'Rte_ModeType_' + modeType.name)))

                    for i, elem in enumerate(modeType.modeDeclarations):
                        # define RTE_MODE_EcuM_Mode_POST_RUN ((Rte_ModeType_EcuM_Mode)0)
                        tmp.append(
                            C.define(
                                'RTE_MODE_%s_%s' % (modeType.name, elem.name),
                                '((Rte_ModeType_EcuM_Mode)%d)' % i))

                hfile.code.append(C.blank())
                hfile.code.extend(tmp)
            if len(unusedDefaultTypes) > 0:
                hfile.code.append(C.blank(2))
                hfile.code.append(
                    C.line('#ifndef RTE_SUPPRESS_UNUSED_DATATYPES'))
                for name in sorted(unusedDefaultTypes):
                    hfile.code.append(C.blank())
                    hfile.code.extend(self.defaultTypes[name])
                hfile.code.append(C.blank())
                hfile.code.append(C.line('#endif'))
            fp.write('\n'.join(hfile.lines()))
            fp.write('\n')