Skip to content

Commit

Permalink
Merge pull request #421 from Luos-io/feat/secure_OD
Browse files Browse the repository at this point in the history
secure OD struct to not compile in case of access violation. fix #295
  • Loading branch information
JeromeGalan authored Mar 28, 2023
2 parents 4f70a66 + 9762166 commit de61fb9
Show file tree
Hide file tree
Showing 80 changed files with 2,245 additions and 457 deletions.
64 changes: 46 additions & 18 deletions engine/OD/od_angular.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
/*******************************************************************************
* Definitions
******************************************************************************/
typedef float angular_position_t;
typedef struct
{
float _private;
} angular_position_t;

typedef struct
{
float _private;
} angular_speed_t;

/*******************************************************************************
* Variables
Expand All @@ -26,108 +34,128 @@ typedef float angular_position_t;
// deg
static inline float AngularOD_PositionTo_deg(angular_position_t self)
{
return self;
return self._private;
}

static inline angular_position_t AngularOD_PositionFrom_deg(float deg)
{
return deg;
angular_position_t self;
self._private = deg;
return self;
}

// rev
static inline float AngularOD_PositionTo_rev(angular_position_t self)
{
return self / 360.0f;
return self._private / 360.0f;
}

static inline angular_position_t AngularOD_PositionFrom_rev(float rev)
{
return rev * 360.0f;
angular_position_t self;
self._private = rev * 360.0f;
return self;
}

// rad
static inline float AngularOD_PositionTo_rad(angular_position_t self)
{
return (self * 2.0f * 3.141592653589793f) / 360.0f;
return (self._private * 2.0f * 3.141592653589793f) / 360.0f;
}

static inline angular_position_t AngularOD_PositionFrom_rad(float rad)
{
return (rad * 360.0f) / (2.0f * 3.141592653589793f);
angular_position_t self;
self._private = (rad * 360.0f) / (2.0f * 3.141592653589793f);
return self;
}

//******** Messages management ***********
static inline void AngularOD_PositionToMsg(const angular_position_t *const self, msg_t *const msg)
{
LUOS_ASSERT(self);
LUOS_ASSERT(msg);
msg->header.cmd = ANGULAR_POSITION;
memcpy(msg->data, self, sizeof(angular_position_t));
msg->header.size = sizeof(angular_position_t);
}

static inline void AngularOD_PositionFromMsg(angular_position_t *const self, const msg_t *const msg)
{
LUOS_ASSERT(self);
LUOS_ASSERT(msg);
memcpy(self, msg->data, msg->header.size);
}

typedef float angular_speed_t;

// angular_speed are stored in degree/s (deg/s)
//******** Conversions ***********
//******************************** Conversions *******************************

// deg_s
static inline float AngularOD_SpeedTo_deg_s(angular_speed_t self)
{
return self;
return self._private;
}

static inline angular_speed_t AngularOD_SpeedFrom_deg_s(float deg)
{
return deg;
angular_speed_t self;
self._private = deg;
return self;
}

// rev_s
static inline float AngularOD_SpeedTo_rev_s(angular_speed_t self)
{
return self / 360.0f;
return self._private / 360.0f;
}

static inline angular_speed_t AngularOD_SpeedFrom_rev_s(float rev_s)
{
return rev_s * 360.0f;
angular_speed_t self;
self._private = rev_s * 360.0f;
return self;
}

// rev_min
static inline float AngularOD_SpeedTo_rev_min(angular_speed_t self)
{
return self * 60.0f / 360.0f;
return self._private * 60.0f / 360.0f;
}

static inline angular_speed_t AngularOD_SpeedFrom_rev_min(float rev_min)
{
return rev_min * 360.0f / 60.0f;
angular_speed_t self;
self._private = rev_min * 360.0f / 60.0f;
return self;
}

// rad_s
static inline float AngularOD_SpeedTo_rad_s(angular_speed_t self)
{
return (self * 2.0f * 3.141592653589793f) / 360.0f;
return (self._private * 2.0f * 3.141592653589793f) / 360.0f;
}

static inline angular_speed_t AngularOD_SpeedFrom_rad_s(float rad_s)
{
return (rad_s * 360.0f) / (2.0f * 3.141592653589793f);
angular_speed_t self;
self._private = (rad_s * 360.0f) / (2.0f * 3.141592653589793f);
return self;
}

//******** Messages management ***********
static inline void AngularOD_SpeedToMsg(const angular_speed_t *const self, msg_t *const msg)
{
LUOS_ASSERT(self);
LUOS_ASSERT(msg);
msg->header.cmd = ANGULAR_SPEED;
memcpy(msg->data, self, sizeof(angular_speed_t));
msg->header.size = sizeof(angular_speed_t);
}

static inline void AngularOD_SpeedFromMsg(angular_speed_t *const self, const msg_t *const msg)
{
LUOS_ASSERT(self);
LUOS_ASSERT(msg);
memcpy(self, msg->data, msg->header.size);
}

Expand Down
4 changes: 4 additions & 0 deletions engine/OD/od_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ typedef struct __attribute__((__packed__))
//******** Messages management ***********
static inline void ControlOD_ControlToMsg(const control_t *const self, msg_t *const msg)
{
LUOS_ASSERT(self);
LUOS_ASSERT(msg);
msg->header.cmd = CONTROL;
memcpy(msg->data, self, sizeof(control_t));
msg->header.size = sizeof(control_t);
}

static inline void ControlOD_ControlFromMsg(control_t *const self, const msg_t *const msg)
{
LUOS_ASSERT(self);
LUOS_ASSERT(msg);
LUOS_ASSERT(msg->header.size == sizeof(control_t));
memcpy(self, msg->data, sizeof(control_t));
// check data validity
Expand Down
73 changes: 53 additions & 20 deletions engine/OD/od_electric.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@
/*******************************************************************************
* Definitions
******************************************************************************/
typedef float voltage_t;
typedef struct
{
float _private;
} voltage_t;

typedef struct
{
float _private;
} current_t;

typedef struct
{
float _private;
} power_t;
/*******************************************************************************
* Variables
******************************************************************************/
Expand All @@ -25,115 +38,135 @@ typedef float voltage_t;
// mv
static inline float ElectricOD_VoltageTo_mV(voltage_t self)
{
return self * 1000.0f;
return self._private * 1000.0f;
}

static inline voltage_t ElectricOD_VoltageFrom_mV(float mv)
{
return mv / 1000.0f;
voltage_t self;
self._private = mv / 1000.0f;
return self;
}

// v
static inline float ElectricOD_VoltageTo_V(voltage_t self)
{
return self;
return self._private;
}

static inline voltage_t ElectricOD_VoltageFrom_V(float v)
{
return v;
voltage_t self;
self._private = v;
return self;
}

//******** Messages management ***********
static inline void ElectricOD_VoltageToMsg(const voltage_t *const self, msg_t *const msg)
{
LUOS_ASSERT(self);
LUOS_ASSERT(msg);
msg->header.cmd = VOLTAGE;
memcpy(msg->data, self, sizeof(voltage_t));
msg->header.size = sizeof(voltage_t);
}

static inline void ElectricOD_VoltageFromMsg(voltage_t *const self, const msg_t *const msg)
{
LUOS_ASSERT(self);
LUOS_ASSERT(msg);
memcpy(self, msg->data, msg->header.size);
}

typedef float current_t;

// current are stored in Ampere (A)
//******** Conversions ***********
//******************************** Conversions *******************************

// ma
static inline float ElectricOD_CurrentTo_mA(current_t self)
{
return self * 1000.0f;
return self._private * 1000.0f;
}

static inline current_t ElectricOD_CurrentFrom_mA(float ma)
{
return ma / 1000.0f;
current_t self;
self._private = ma / 1000.0f;
return self;
}

// A
static inline float ElectricOD_CurrentTo_A(current_t self)
{
return self;
return self._private;
}

static inline current_t ElectricOD_CurrentFrom_A(float a)
{
return a;
current_t self;
self._private = a;
return self;
}

//******** Messages management ***********
static inline void ElectricOD_CurrentToMsg(const current_t *const self, msg_t *const msg)
{
LUOS_ASSERT(self);
LUOS_ASSERT(msg);
msg->header.cmd = CURRENT;
memcpy(msg->data, self, sizeof(current_t));
msg->header.size = sizeof(current_t);
}

static inline void ElectricOD_CurrentFromMsg(current_t *const self, const msg_t *const msg)
{
LUOS_ASSERT(self);
LUOS_ASSERT(msg);
memcpy(self, msg->data, msg->header.size);
}

typedef float power_t;

// power are stored in Watt (W)
//******** Conversions ***********
//******************************** Conversions *******************************

// mw
static inline float ElectricOD_PowerTo_mW(power_t self)
{
return self * 1000.0f;
return self._private * 1000.0f;
}

static inline power_t ElectricOD_PowerFrom_mW(float mw)
{
return mw / 1000.0f;
power_t self;
self._private = mw / 1000.0f;
return self;
}

// A
static inline float ElectricOD_PowerTo_W(power_t self)
{
return self;
return self._private;
}

static inline power_t ElectricOD_PowerFrom_W(float w)
{
return w;
power_t self;
self._private = w;
return self;
}

//******** Messages management ***********
static inline void ElectricOD_PowerToMsg(const power_t *const self, msg_t *const msg)
{
LUOS_ASSERT(self);
LUOS_ASSERT(msg);
msg->header.cmd = POWER;
memcpy(msg->data, self, sizeof(power_t));
msg->header.size = sizeof(power_t);
}

static inline void ElectricOD_PowerFromMsg(current_t *const self, const msg_t *const msg)
static inline void ElectricOD_PowerFromMsg(power_t *const self, const msg_t *const msg)
{
LUOS_ASSERT(self);
LUOS_ASSERT(msg);
memcpy(self, msg->data, msg->header.size);
}

Expand Down
Loading

0 comments on commit de61fb9

Please sign in to comment.