#!/usr/bin/env python3 """ @file config_generator.py @brief Configuration file generator for Automotive RTOS """ import os import sys import argparse import json from pathlib import Path from typing import Dict, List, Any class ConfigGenerator: """Generate RTOS configuration files""" def __init__(self, project_root): self.project_root = Path(project_root) self.config_dir = self.project_root / 'config' def load_configuration(self, config_file): """Load configuration from JSON file""" with open(config_file, 'r') as f: return json.load(f) def generate_kernel_config(self, config: Dict[str, Any]): """Generate kernel_config.h""" print("Generating kernel_config.h...") kernel = config.get('kernel', {}) content = f"""/** * @file kernel_config.h * @brief Kernel configuration (auto-generated) * @note This file is auto-generated. Do not modify manually. */ #ifndef KERNEL_CONFIG_H #define KERNEL_CONFIG_H /* ============================================================================ * Basic Kernel Configuration * ============================================================================ */ #define MAX_TASKS {kernel.get('max_tasks', 32)} #define MAX_PRIORITY_LEVELS {kernel.get('max_priorities', 16)} #define TICK_RATE_HZ {kernel.get('tick_rate', 1000)} #define MAX_TASK_NAME_LENGTH {kernel.get('max_task_name_length', 16)} /* ============================================================================ * Scheduler Configuration * ============================================================================ */ #define SCHEDULER_TYPE SCHEDULER_PRIORITY_PREEMPTIVE #define ENABLE_ROUND_ROBIN {1 if kernel.get('round_robin', True) else 0} #define ROUND_ROBIN_TIME_SLICE {kernel.get('time_slice', 10)} #define ENABLE_DEADLINE_MONITORING {1 if kernel.get('deadline_monitoring', True) else 0} /* ============================================================================ * Synchronization Configuration * ============================================================================ */ #define MAX_SEMAPHORES {kernel.get('max_semaphores', 32)} #define MAX_MUTEXES {kernel.get('max_mutexes', 16)} #define MAX_QUEUES {kernel.get('max_queues', 16)} #define MAX_TIMERS {kernel.get('max_timers', 16)} /* ============================================================================ * Memory Configuration * ============================================================================ */ #define ENABLE_MEMORY_PROTECTION {1 if kernel.get('memory_protection', True) else 0} #define ENABLE_STACK_CHECK {1 if kernel.get('stack_check', True) else 0} #define DEFAULT_TASK_STACK_SIZE {kernel.get('default_stack_size', 1024)} #define HEAP_SIZE {kernel.get('heap_size', 65536)} #endif /* KERNEL_CONFIG_H */ """ self._write_file(self.config_dir / 'kernel_config.h', content) def generate_task_config(self, config: Dict[str, Any]): """Generate task_config.h""" print("Generating task_config.h...") tasks = config.get('tasks', []) content = f"""/** * @file task_config.h * @brief Task configuration (auto-generated) * @note This file is auto-generated. Do not modify manually. */ #ifndef TASK_CONFIG_H #define TASK_CONFIG_H #include "kernel_config.h" /* ============================================================================ * Task Priority Definitions * ============================================================================ */ """ # Generate priority definitions for task in tasks: name = task['name'].upper().replace(' ', '_') priority = task.get('priority', 0) content += f"#define TASK_PRIORITY_{name:<30s} {priority}\n" content += "\n/* ============================================================================\n" content += " * Task Period Definitions (ms)\n" content += " * ============================================================================ */\n" # Generate period definitions for task in tasks: name = task['name'].upper().replace(' ', '_') period = task.get('period_ms', 10) content += f"#define TASK_PERIOD_{name:<30s} {period}\n" content += "\n/* ============================================================================\n" content += " * Task Stack Sizes (bytes)\n" content += " * ============================================================================ */\n" # Generate stack size definitions for task in tasks: name = task['name'].upper().replace(' ', '_') stack_size = task.get('stack_size', 1024) content += f"#define TASK_STACK_{name:<30s} {stack_size}\n" content += "\n/* ============================================================================\n" content += " * Task Configuration Table\n" content += " * ============================================================================ */\n\n" content += "static const TaskConfigEntry_t task_config_table[] = {\n" content += " /* Name, Priority, Period, Stack, Deadline, Periodic, Critical */\n" for task in tasks: name = task['name'] priority = task.get('priority', 0) period = task.get('period_ms', 10) stack_size = task.get('stack_size', 1024) deadline = task.get('deadline_ms', period) periodic = 'true' if task.get('periodic', True) else 'false' critical = 'true' if task.get('critical', False) else 'false' content += f' {{"{name}", {priority}, {period}, {stack_size}, ' content += f'{deadline}, {periodic}, {critical}}},\n' content += "};\n\n" content += f"#define TASK_CONFIG_COUNT {len(tasks)}\n\n" content += "#endif /* TASK_CONFIG_H */\n" self._write_file(self.config_dir / 'task_config.h', content) def generate_can_config(self, config: Dict[str, Any]): """Generate can_config.h""" print("Generating can_config.h...") can_config = config.get('can', {}) content = f"""/** * @file can_config.h * @brief CAN configuration (auto-generated) */ #ifndef CAN_CONFIG_H #define CAN_CONFIG_H /* CAN Baudrate */ #define CAN_BAUDRATE {can_config.get('baudrate', 500000)} #define CAN_FD_BAUDRATE {can_config.get('fd_baudrate', 2000000)} #define CAN_FD_ENABLED {1 if can_config.get('fd_enabled', False) else 0} /* CAN Message IDs */ """ # Generate message ID definitions messages = can_config.get('messages', []) for msg in messages: name = msg['name'].upper().replace(' ', '_') msg_id = msg.get('id', 0x100) content += f"#define CAN_ID_{name:<30s} 0x{msg_id:03X}\n" content += "\n#endif /* CAN_CONFIG_H */\n" self._write_file(self.config_dir / 'can_config.h', content) def _write_file(self, file_path: Path, content: str): """Write content to file""" file_path.parent.mkdir(parents=True, exist_ok=True) with open(file_path, 'w') as f: f.write(content) print(f" Generated: {file_path}") def generate(self, config_file: str): """Generate all configuration files""" print("=" * 60) print("Configuration Generator") print("=" * 60) config = self.load_configuration(config_file) # Generate configuration files self.generate_kernel_config(config) self.generate_task_config(config) self.generate_can_config(config) print("\nConfiguration generation complete") def main(): parser = argparse.ArgumentParser(description='Configuration generator') parser.add_argument('config_file', help='Configuration JSON file') parser.add_argument('--project', default='.', help='Project root directory') args = parser.parse_args() generator = ConfigGenerator(args.project) generator.generate(args.config_file) if __name__ == '__main__': main()