def _genComponentHeader(self, fp, component): ws = component.swc.rootWS() assert(ws is not None) hfile=C.hfile(None, guard='RTE_%s_H'%(component.swc.name.upper())) hfile.code.append(C.include('Rte.h')) hfile.code.append(C.include('Rte_Type.h')) lines = self._genInitValues(ws, component.swc.requirePorts+component.swc.providePorts) if len(lines)>0: hfile.code.extend([C.line(x) for x in _genCommentHeader('Init Values')]) hfile.code.extend(lines) #Write API hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('API Prototypes')]) for proto in component.clientAPI.get_all(): hfile.code.append(C.statement(proto.func)) if len(component.clientAPI.final['read'])>0: hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Read_<p>_<d>')]) hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['read']]) if len(component.clientAPI.final['write'])>0: hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Write_<p>_<d>')]) hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['write']]) if len(component.clientAPI.final['receive'])>0: hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Receive_<p>_<d>')]) hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['receive']]) if len(component.clientAPI.final['send'])>0: hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Send_<p>_<d>')]) hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['send']]) if len(component.clientAPI.final['mode'])>0: hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Mode_<p>_<d>')]) hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['mode']]) if len(component.clientAPI.final['mode'])>0: hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Mode_<mode>')]) hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['mode']]) if len(component.clientAPI.final['calprm'])>0: hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Calprm_<name>')]) hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['calprm']]) if len(component.clientAPI.final['call'])>0: hfile.code.append(C.blank()) hfile.code.extend([C.line(x) for x in _genCommentHeader('Rte_Call_<p>_<o> ')]) hfile.code.extend([C.define(proto.shortname, proto.func.name) for proto in component.clientAPI.final['call']]) if len(component.rte_runnables)>0: for name in sorted(component.rte_runnables): runnable = component.rte_runnables[name] tmp = self._writeRunnableProto(runnable) hfile.code.extend(tmp) fp.write('\n'.join(hfile.lines())) fp.write('\n')
def __init__(self, tasks): self.decl = C.variable('os_task_cfg', 'os_task_elem_t', static=True, const=True, array='OS_NUM_TASKS') self.body = C.block(innerIndent=innerIndentDefault) for task in tasks: fmt='{0: >25},{1: >15},{2: >30},{3: >30}' if len(task.timer_events)>0: self.body.append(C.line('{'+fmt.format( '&m_os_task_'+task.name, task.name, '&os_alarm_cfg_%s[0]'%task.name, 'OS_NUM_ALARMS_%s'%task.name)+'},')) else: self.body.append(C.line('{'+fmt.format( '&m_os_task_'+task.name, task.name, '(os_alarm_cfg_t*) 0', '0')+'},'))
def _genCommentHeader(comment): code = C.sequence() code.append( C.line( '/*********************************************************************************************************************' )) code.append(C.line('* %s' % comment)) code.append( C.line( '*********************************************************************************************************************/' )) return code
def _genCommentHeader2(comment): """ Same as _genCommentHeader but returns a C sequence instead of raw strings """ code = C.sequence() code.append( C.line( '/*********************************************************************************************************************' )) code.append(C.line('* %s' % comment)) code.append( C.line( '*********************************************************************************************************************/' )) return code
def __init__(self, task): init_delay=0 #FIXME: allow users to select this at a later time self.decl = C.variable('os_alarm_cfg_%s'%task.name, 'os_alarm_cfg_t', static=True, const=True, array='OS_NUM_ALARMS_%s'%task.name) self.body = C.block(innerIndent=innerIndentDefault) self.body.append(C.linecomment('OS Task, Event ID, Init Delay (ms), Period (ms)')) for event in task.timer_events: self.body.append(C.line('{'+'{0: >10},{1: >50},{2: >5},{3: >5}'.format( '&m_os_task_'+task.name, 'EVENT_MASK_%s_%s'%(task.name,event.name), init_delay, event.inner.period)+'},'))
def _writeRunnableProto(self, runnable): lines = [] lines.extend([ C.line(x) for x in _genCommentHeader('Runnable %s' % runnable.name) ]) lines.append(C.statement(runnable.prototype)) lines.append(C.blank()) return lines
def _generate_task_cfg_source(self, dest_dir, header_file, file_name = 'os_task_cfg.c'): source = C.cfile(os.path.join(dest_dir, file_name)) code = source.code code.extend(_genCommentHeader('INCLUDES')) code.append(C.include(header_file)) code.append(C.include('os_event_cfg.h')) code.append('') code.extend(_genCommentHeader('PRIVATE VARIABLES')) for static_var in sorted(self.static_vars.values(), key=lambda x: x.name): code.append(C.statement(static_var)) code.append('') for alarm_var in self.alarm_vars: code.append(C.line(str(alarm_var.decl)+' =')) code.append(C.statement(alarm_var.body)) code.append(C.line(str(self.os_task_var.decl)+' =')) code.append(C.statement(self.os_task_var.body)) code.append('') code.extend(_genCommentHeader('PUBLIC VARIABLES')) code.append(C.line('os_cfg_t g_os_cfg =')) body = C.block(innerIndent = innerIndentDefault) body.append(C.line('&os_task_cfg[0],')) body.append(C.line('OS_NUM_TASKS,')) body.append(C.line('0,')) body.append(C.line('0')) code.append(C.statement(body)) code.append('') code.extend(_genCommentHeader('PUBLIC FUNCTIONS')) for elem in self.cfg.partition.mode_switch_functions.values(): for callback_name in sorted(elem.calls.keys()): code.extend(self._generate_mode_switch_func(callback_name, elem.calls[callback_name])) code.append('') with io.open(source.path, 'w', newline='\n') as fp: for line in source.lines(): fp.write(line+'\n')
def _write_source_global_funcs(self): code = C.sequence() code.extend(_genCommentHeader2('GLOBAL FUNCTIONS')) code.append(C.blank()) for task in sorted(self.os_cfg.tasks, key=lambda x: x.name): code.append(C.line('OS_TASK_HANDLER({0.name}, arg)'.format(task))) code.append(self._generate_task_body(task)) code.append(C.blank(2)) return code
def generate_on_exit_code(self, event, callback_name): code = C.sequence() else_str = 'else ' if len(self.calls) > 0 else '' code.append(C.line(else_str+'if ( (previousMode == RTE_MODE_{0}) && (newMode != RTE_MODE_{0}) )'.format(event.mode+'_'+event.modeDeclaration))) block = C.block(innerIndent = innerIndentDefault) block.append(C.statement(C.fcall(callback_name))) code.append(block) self.add_event_to_call(event, callback_name) self.body.extend(code)
def __init__(self, tasks): self.decl = C.variable('os_task_cfg', 'os_task_elem_t', static=True, const=True, array='OS_NUM_TASKS') self.body = C.block(innerIndent=innerIndentDefault) for task in tasks: fmt = '{0: >25},{1: >15},{2: >30},{3: >30}' if len(task.timer_events) > 0: self.body.append( C.line('{' + fmt.format('&m_os_task_' + task.name, task.name, '&os_alarm_cfg_%s[0]' % task.name, 'OS_NUM_ALARMS_%s' % task.name) + '},')) else: self.body.append( C.line('{' + fmt.format('&m_os_task_' + task.name, task.name, '(os_alarm_cfg_t*) 0', '0') + '},'))
def _generate_runnable_calls(self, code, matching_runnables): events = matching_runnables[0].event_triggers if len(events) == 1: event = events[0] if not isinstance(event, autosar.rte.base.OperationInvokedEvent): code.append(C.line('if (eventMask & %s)' % event.symbol)) block = C.block(innerIndent=innerIndentDefault) for runnable in matching_runnables: block.append(C.statement(C.fcall(runnable.symbol))) code.append(block) elif len(events) > 1: raise NotImplementedError('multiple events')
def _createHeaderLines(self, filepath): hfile = C.hfile(filepath) code = hfile.code code.extend([C.line(x) for x in _genCommentHeader('Includes')]) code.append(C.include("Std_Types.h")) code.append(C.include("Rte_Type.h")) code.append(C.include("Rte.h")) code.append(C.blank()) code.extend(_genCommentHeader('Constants and Types')) for key in sorted(self.typedefs.keys()): code.append(C.statement(self.typedefs[key])) code.append(C.blank()) code.extend([ C.line(x) for x in _genCommentHeader('Public Function Declarations') ]) code.append(C.blank()) code.append( C.statement(C.function('%s_Start' % self.api_prefix, 'void'))) for func in sorted(self.partition.upperLayerAPI.final['get'], key=lambda x: x.shortname): assert func.proto is not None hfile.code.append(C.statement(func.proto)) for func in sorted(self.partition.upperLayerAPI.final['setReadData'], key=lambda x: x.shortname): assert func.proto is not None hfile.code.append(C.statement(func.proto)) for func in sorted(self.partition.upperLayerAPI.final['setReadResult'], key=lambda x: x.shortname): assert func.proto is not None hfile.code.append(C.statement(func.proto)) for func in sorted(self.extra_public_functions.values(), key=lambda x: x.shortname): assert func.proto is not None hfile.code.append(C.statement(func.proto)) hfile.code.append(C.blank()) return hfile.lines()
def _generate_task_body(self, task): code = C.block(innerIndent=innerIndentDefault) isRunning = C.variable('isRunning', 'boolean') code.append(C.statement('{0} = TRUE'.format(str(isRunning)))) code.append(C.statement('os_task_t *self = (os_task_t*)arg')) code.append('') code.append(C.line('if (self == 0)')) body = C.block(innerIndent=innerIndentDefault) body.append(C.statement('THREAD_RETURN(1)')) code.append(body) code.append('') code.append(C.line('while (isRunning == TRUE)')) while_block = C.block(innerIndent=innerIndentDefault) while_block.append(C.statement('uint32 eventMask')) while_block.append( C.statement('int8_t result = os_task_waitEvent(self, &eventMask)')) while_block.append(C.line('if (result == 0)')) if_block = C.block(innerIndent=innerIndentDefault) if_block.extend(self._generate_event_mask_triggers(task)) while_block.append(if_block) while_block.append(C.line('else if(result > 0)')) if_block = C.block(innerIndent=innerIndentDefault) if_block.append(C.statement('printf("%s_QuitEvent\\n")' % task.name)) if_block.append(C.statement('isRunning = false')) while_block.append(if_block) while_block.append(C.line('else')) if_block = C.block(innerIndent=innerIndentDefault) if_block.append( C.statement(r'fprintf(stderr, "os_task_waitEvent failed\n")')) while_block.append(if_block) code.append(while_block) code.append('') code.append(C.statement('THREAD_RETURN(0)')) return code
def _createMockServerCallFunction(self, proto, var_name): body = C.block(innerIndent=innerIndentDefault) body.append(C.line('if (%s != 0)' % (var_name))) inner = C.block(innerIndent=innerIndentDefault) fcall = C.fcall(var_name) for arg in proto.args: fcall.add_param(arg.name) if proto.typename != 'void': inner.append(C.statement('return %s' % str(fcall))) else: inner.append(C.statement(fcall)) body.append(inner) if proto.typename != 'void': body.append(C.statement('return RTE_E_OK')) return body
def __init__(self, task): init_delay = 0 #FIXME: allow users to select this at a later time self.decl = C.variable('os_alarm_cfg_%s' % task.name, 'os_alarm_cfg_t', static=True, const=True, array='OS_NUM_ALARMS_%s' % task.name) self.body = C.block(innerIndent=innerIndentDefault) self.body.append( C.linecomment( 'OS Task, Event ID, Init Delay (ms), Period (ms)' )) for event in task.timer_events: self.body.append( C.line('{' + '{0: >10},{1: >50},{2: >5},{3: >5}'.format( '&m_os_task_' + task.name, 'EVENT_MASK_%s_%s' % (task.name, event.name), init_delay, event.inner.period) + '},'))
def _generate_task_cfg_source(self, dest_dir, header_file, file_name='os_task_cfg.c'): source = C.cfile(os.path.join(dest_dir, file_name)) code = source.code code.extend(_genCommentHeader('INCLUDES')) code.append(C.include(header_file)) code.append(C.include('os_event_cfg.h')) code.append('') code.extend(_genCommentHeader('PRIVATE VARIABLES')) for static_var in sorted(self.static_vars.values(), key=lambda x: x.name): code.append(C.statement(static_var)) code.append('') for alarm_var in self.alarm_vars: code.append(C.line(str(alarm_var.decl) + ' =')) code.append(C.statement(alarm_var.body)) code.append(C.line(str(self.os_task_var.decl) + ' =')) code.append(C.statement(self.os_task_var.body)) code.append('') code.extend(_genCommentHeader('PUBLIC VARIABLES')) code.append(C.line('os_cfg_t g_os_cfg =')) body = C.block(innerIndent=innerIndentDefault) body.append(C.line('&os_task_cfg[0],')) body.append(C.line('OS_NUM_TASKS,')) body.append(C.line('0,')) body.append(C.line('0')) code.append(C.statement(body)) code.append('') code.extend(_genCommentHeader('PUBLIC FUNCTIONS')) for elem in self.cfg.partition.mode_switch_functions.values(): for callback_name in sorted(elem.calls.keys()): code.extend( self._generate_mode_switch_func(callback_name, elem.calls[callback_name])) code.append('') with io.open(source.path, 'w', newline='\n') as fp: for line in source.lines(): fp.write(line + '\n')
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')
def _genCommentHeader(comment): code = C.sequence() code.append(C.line('/*********************************************************************************************************************')) code.append(C.line('* %s'%comment)) code.append(C.line('*********************************************************************************************************************/')) return code