Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
1.Come up with the Advisory and Required MISRA C Standards for pointers, structure and functions.
ANS : MISRA (Motor Industry Software Reliability Association) C is a set of guidelines for the C programming language. Here's a suggested set of advisory and required MISRA C standards for pointers, structures, and functions:
Advisory Standards:
Pointer Usage:
Avoid using pointers where direct variable access is sufficient.
Document pointer ownership and lifetime to avoid memory leaks and dangling pointers.
Prefer const pointers to non-const pointers where applicable to enhance code readability and immutability.
Structure Usage:
Keep structure definitions simple and well-commented to improve code understanding.
Limit the depth of nested structures to avoid complex data dependencies.
Use typedefs for complex structures to improve code readability and maintainability.
Function Design:
Follow a consistent naming convention for functions to enhance code clarity and maintainability.
Keep functions short and focused to improve readability and ease of debugging.
Document function interfaces, parameters, and return values comprehensively to facilitate code reuse and understanding.
Required Standards:
Pointer Safety:
Ensure that pointers are always initialized before use to prevent undefined behavior.
Prohibit pointer arithmetic unless absolutely necessary to avoid memory corruption and segmentation faults.
Enforce proper bounds checking when dereferencing pointers to prevent buffer overflows and security vulnerabilities.
Structure Integrity:
Require complete initialization of structure members to prevent uninitialized data access and undefined behavior.
Prohibit direct modification of structure members outside of designated setter functions to enforce encapsulation and maintain data integrity.
Validate structure size and alignment to ensure portability and compatibility across different platforms and compilers.
Function Prototyping:
Mandate function prototypes for all functions to ensure type safety and avoid implicit function declarations.
Enforce strict parameter type checking to prevent mismatched function calls and potential runtime errors.
Require consistent function declarations and definitions to enhance code maintainability and portability.
These standards aim to promote safe and reliable C programming practices, particularly concerning pointers, structures, and functions, which are crucial aspects of C code organization and memory management.
2.Write a code in compliance with AUTOSAR V1.0.5 R3.1 Rev 0001 Rule 7.6.2 and 7.6.3 and 7.7.2
ANS :
#include <stdint.h>
/* Rule 7.6.2: Identifiers (internal and external) shall not rely on the significance of more than 31 characters. */
typedef struct {
uint32_t speed; // shorter variable name "speed"
uint32_t rpm; // shorter variable name "rpm"
uint32_t fuelLevel; // kept the same as it's compliant
} VehicleParameters;
/* Rule 7.6.3: The typedef specifier shall be used in the declaration and definition of all types that indicate size and signedness. */
typedef enum {
DOOR_OPEN, // compliant enum definition
DOOR_CLOSED // compliant enum definition
} DoorState;
/* Rule 7.7.2: The types int8, int16, int32, uint8, uint16, uint32, sint8, sint16, and sint32 shall be used. */
typedef int32_t Speed_t; // compliant typedef using sint32 type
typedef uint8_t Status_t; // compliant typedef using uint8 type
void accelerateVehicle(VehicleParameters *vehicle) {
// Simplified acceleration model: increase speed based on RPM
vehicle->speed += (vehicle->rpm / 1000); // Increase speed by RPM/1000 units per second
}
int main() {
VehicleParameters vehicle;
vehicle.speed = 100;
vehicle.rpm = 2000;
vehicle.fuelLevel = 50;
DoorState driverDoor = DOOR_OPEN;
DoorState passengerDoor = DOOR_CLOSED;
Speed_t currentSpeed = 60;
Status_t systemStatus = 0;
for (int i = 0; i < 10; i++) {
accelerateVehicle(&vehicle);
printf("Speed: %d\n", vehicle.speed);
}
return 0;
}