Skip to content
This repository was archived by the owner on Jun 28, 2025. It is now read-only.

Commit 56825cc

Browse files
Added protection against dereferencing nullptr in TMStateData obj
1 parent 5903200 commit 56825cc

File tree

3 files changed

+157
-97
lines changed

3 files changed

+157
-97
lines changed

SystemManager/Src/SystemManager.cpp

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,26 @@
55

66
#include "SystemManager.hpp"
77

8+
#include <iostream>
9+
10+
#include "FreeRTOS.h"
11+
#include "GroundStationCommunication.hpp"
12+
#include "TelemetryManager.hpp"
13+
#include "cmsis_os.h"
814
#include "drivers_config.hpp"
915
#include "independent_watchdog.h"
1016
#include "rcreceiver_datatypes.h"
1117
#include "sbus_defines.h"
1218
#include "sbus_receiver.hpp"
1319
#include "tim.h"
14-
#include "GroundStationCommunication.hpp"
15-
#include "TelemetryManager.hpp"
16-
#include "GroundStationCommunication.hpp"
17-
#include "TelemetryManager.hpp"
18-
#include "cmsis_os.h"
19-
#include "FreeRTOS.h"
20-
#include <iostream>
2120

2221
extern "C" {
23-
#include "app_fatfs.h"
24-
#include "log_util.h"
22+
#include "app_fatfs.h"
23+
#include "log_util.h"
2524
}
2625

27-
#define TIMEOUT_CYCLES 250000 // 25k = 1 sec fro testing 10/14/2023 => 250k = 10 sec
28-
#define TIMOUT_MS 10000 // 10 sec
26+
#define TIMEOUT_CYCLES 250000 // 25k = 1 sec fro testing 10/14/2023 => 250k = 10 sec
27+
#define TIMOUT_MS 10000 // 10 sec
2928

3029
// 0 - AM, 1 - TM, 2 - PM
3130
static TaskHandle_t taskHandles[3];
@@ -52,6 +51,7 @@ SystemManager::SystemManager()
5251
TMStateData stateData;
5352

5453
// values to be assigned to stateData
54+
// THIS IS JUST A PLACEHOLDER, THE MEMORY ADDRESSES NEED TO LIVE LONGER THAN THE SM CONSTRUCTOR
5555
int32_t alt = 0;
5656
int32_t lat = 0;
5757
int32_t lon = 0;
@@ -69,41 +69,41 @@ SystemManager::SystemManager()
6969
float yawspeed = 0;
7070

7171
// use the memory address of the above, since stateData uses pointers
72-
stateData.alt = &alt;
73-
stateData.lat = &lat;
74-
stateData.lon = &lon;
75-
stateData.relative_alt = &relative_alt;
76-
stateData.vx = &vx;
77-
stateData.vy = &vy;
78-
stateData.vz = &vz;
79-
stateData.hdg = &hdg;
80-
stateData.time_boot_ms = &time_boot_ms;
81-
stateData.roll = &roll;
82-
stateData.pitch = &pitch;
83-
stateData.yaw = &yaw;
84-
stateData.rollspeed = &rollspeed;
85-
stateData.pitchspeed = &pitchspeed;
86-
stateData.yawspeed = &yawspeed;
72+
stateData.set_alt(&alt);
73+
stateData.set_lat(&lat);
74+
stateData.set_lon(&lon);
75+
stateData.set_relative_alt(&relative_alt);
76+
stateData.set_vx(&vx);
77+
stateData.set_vy(&vy);
78+
stateData.set_vz(&vz);
79+
stateData.set_hdg(&hdg);
80+
stateData.set_time_boot_ms(&time_boot_ms);
81+
stateData.set_roll(&roll);
82+
stateData.set_pitch(&pitch);
83+
stateData.set_yaw(&yaw);
84+
stateData.set_rollspeed(&rollspeed);
85+
stateData.set_pitchspeed(&pitchspeed);
86+
stateData.set_yawspeed(&yawspeed);
8787

8888
MAV_STATE mavState = MAV_STATE::MAV_STATE_STANDBY;
8989
MAV_MODE_FLAG mavMode = MAV_MODE_FLAG::MAV_MODE_FLAG_MANUAL_INPUT_ENABLED;
9090

91-
// Creating parameters for the GroundStationCommunication that will be passed to telemetryManager
91+
// Creating parameters for the GroundStationCommunication that will be passed to
92+
// telemetryManager
9293
TMCircularBuffer DMAReceiveBuffer = *(new TMCircularBuffer(rfd900_circular_buffer));
9394

94-
9595
// the buffer that stores non_routine/low_priority bytes (ex. Battery Voltage) to be sent to the
9696
// ground station.
97-
uint8_t* lowPriorityTransmitBuffer = new uint8_t[RFD900_BUF_SIZE];
97+
uint8_t *lowPriorityTransmitBuffer = new uint8_t[RFD900_BUF_SIZE];
9898

9999
// the buffer that stores routine/high_priority bytes (ex. heading, general state data) to be
100100
// sent to the ground station.
101-
uint8_t* highPriorityTransmitBuffer = new uint8_t[RFD900_BUF_SIZE];
101+
uint8_t *highPriorityTransmitBuffer = new uint8_t[RFD900_BUF_SIZE];
102102

103-
GroundStationCommunication GSC = *(new GroundStationCommunication(DMAReceiveBuffer, lowPriorityTransmitBuffer,
104-
highPriorityTransmitBuffer, RFD900_BUF_SIZE));
103+
GroundStationCommunication GSC = *(new GroundStationCommunication(
104+
DMAReceiveBuffer, lowPriorityTransmitBuffer, highPriorityTransmitBuffer, RFD900_BUF_SIZE));
105105

106-
// the buffer that stores the bytes received from the ground station.
106+
// the buffer that stores the bytes received from the ground station.
107107
MavlinkTranslator MT;
108108

109109
this->telemetryManager = new TelemetryManager(stateData, mavState, mavMode, GSC, MT);
@@ -112,14 +112,14 @@ SystemManager::SystemManager()
112112
// REGULAR INTERVAL AS IT DEALS WITH MESSAGE DECODING AND LOW PRIORITY DATA TRANSMISSION
113113
}
114114

115-
116-
//wrapper functions are needed as FreeRTOS xTaskCreate function does not accept functions that have "this" pointers
117-
void SystemManager::attitudeManagerTaskWrapper(void* pvParameters){
118-
SystemManager *systemManagerInstance = static_cast<SystemManager *>(pvParameters);
115+
// wrapper functions are needed as FreeRTOS xTaskCreate function does not accept functions that have
116+
// "this" pointers
117+
void SystemManager::attitudeManagerTaskWrapper(void *pvParameters) {
118+
SystemManager *systemManagerInstance = static_cast<SystemManager *>(pvParameters);
119119
systemManagerInstance->attitudeManagerTask();
120120
}
121121

122-
void SystemManager::telemetryManagerTaskWrapper(void* pvParameters){
122+
void SystemManager::telemetryManagerTaskWrapper(void *pvParameters) {
123123
SystemManager *systemManagerInstance = static_cast<SystemManager *>(pvParameters);
124124
systemManagerInstance->telemetryManagerTask();
125125
}
@@ -129,13 +129,13 @@ void SystemManager::pathManagerTaskWrapper(void *pvParameters) {
129129
systemManagerInstance->pathManagerTask();
130130
}
131131

132-
void SystemManager::systemManagerTask(){
132+
void SystemManager::systemManagerTask() {
133133
TickType_t xNextWakeTime = xTaskGetTickCount();
134134
uint16_t frequency = 5;
135135

136-
for(;;){
136+
for (;;) {
137137
printf("SM called\r\n");
138-
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7); // toggle led light for testing
138+
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7); // toggle led light for testing
139139

140140
this->rcInputs_ = rcController_->GetRCControl();
141141

@@ -177,42 +177,42 @@ void SystemManager::systemManagerTask(){
177177
this->invertedRollMotorChannel_.set(SBUS_MAX / 2);
178178
}
179179

180-
vTaskDelayUntil(&xNextWakeTime, 1000 / frequency);
180+
vTaskDelayUntil(&xNextWakeTime, 1000 / frequency);
181181
}
182182
}
183183

184-
void SystemManager::attitudeManagerTask(){
184+
void SystemManager::attitudeManagerTask() {
185185
TickType_t xNextWakeTime = xTaskGetTickCount();
186186
uint16_t frequency = 5;
187187

188-
for(;;){
189-
//call AM
188+
for (;;) {
189+
// call AM
190190

191191
printf("AM called\r\n");
192-
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_7); // toggle led light for testing
193-
vTaskDelayUntil(&xNextWakeTime, 1000 / frequency);
192+
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_7); // toggle led light for testing
193+
vTaskDelayUntil(&xNextWakeTime, 1000 / frequency);
194194
}
195195
}
196196

197-
void SystemManager::telemetryManagerTask(){
197+
void SystemManager::telemetryManagerTask() {
198198
TickType_t xNextWakeTime = xTaskGetTickCount();
199199
uint16_t frequency = 5;
200200

201-
for(;;){
202-
//call TM
201+
for (;;) {
202+
// call TM
203203

204204
printf("TM called\r\n");
205-
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_9); // toggle led light for testing
206-
vTaskDelayUntil(&xNextWakeTime, 1000 / frequency);
205+
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_9); // toggle led light for testing
206+
vTaskDelayUntil(&xNextWakeTime, 1000 / frequency);
207207
}
208208
}
209209

210-
void SystemManager::pathManagerTask(){
210+
void SystemManager::pathManagerTask() {
211211
TickType_t xNextWakeTime = xTaskGetTickCount();
212212
uint16_t frequency = 5;
213213

214-
for(;;){
215-
//call PM
214+
for (;;) {
215+
// call PM
216216

217217
printf("PM called\r\n");
218218
vTaskDelayUntil(&xNextWakeTime, 1000 / frequency);
@@ -226,14 +226,18 @@ void SystemManager::startSystemManager() {
226226
if (MX_FATFS_Init() != APP_OK) {
227227
Error_Handler();
228228
}
229-
logInit();
230-
231-
//BaseType_t xTaskCreate( TaskFunction_t pvTaskCode, const char * const pcName, configSTACK_DEPTH_TYPE usStackDepth, void * pvParameters, UBaseType_t uxPriority, TaskHandle_t * pxCreatedTask );
232-
// function's name description size of stack to allocate parameters for task priority handler
229+
logInit();
230+
231+
// BaseType_t xTaskCreate( TaskFunction_t pvTaskCode, const char * const pcName,
232+
// configSTACK_DEPTH_TYPE usStackDepth, void * pvParameters, UBaseType_t uxPriority,
233+
// TaskHandle_t * pxCreatedTask );
234+
// function's name description size of
235+
// stack to allocate parameters for task priority
236+
// handler
233237
xTaskCreate(attitudeManagerTaskWrapper, "AM TASK", 800U, this, osPriorityNormal, taskHandles);
234-
xTaskCreate(telemetryManagerTaskWrapper, "TM TASK", 800U, this, osPriorityNormal, taskHandles + 1);
238+
xTaskCreate(telemetryManagerTaskWrapper, "TM TASK", 800U, this, osPriorityNormal,
239+
taskHandles + 1);
235240
// xTaskCreate(pathManagerTaskWrapper, "PM TASK", 800U, this, osPriorityNormal, taskHandles +2);
236241

237242
systemManagerTask();
238-
239243
}

TelemetryManager/Inc/TelemetryManager.hpp

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,79 +16,122 @@
1616
#ifndef TELEMETRYMANAGER_H
1717
#define TELEMETRYMANAGER_H
1818

19+
#define CREATE_GETTER(type, member) \
20+
type get_##member() const { \
21+
return (member != nullptr) ? *member : std::numeric_limits<type>::max(); \
22+
}
23+
24+
#define CREATE_SETTER(type, member) \
25+
void set_##member(type* value) { member = value; }
26+
1927
#include "FreeRTOS.h"
2028
#include "GroundStationCommunication.hpp"
2129
#include "MavlinkTranslator.hpp"
2230
#include "task.h"
31+
#include <limits>
2332

2433
struct TMStateData {
34+
private:
2535
/*References to variables that contain the state of the drone (lat, lng, yaw, pitch, etc..).
2636
* They are updated by System Manager*/
2737
// Altitude (MSL) (unit:
2838
// mm)(https://mavlink.io/en/messages/common.html#GLOBAL_POSITION_INT).
29-
int32_t* alt;
39+
int32_t* alt = nullptr;
3040

3141
// The latitude of the drone (unit:
3242
// degE7)(https://mavlink.io/en/messages/common.html#GLOBAL_POSITION_INT).
33-
int32_t* lat;
43+
int32_t* lat = nullptr;
3444

3545
// The longitude of the drone (unit:
3646
// degE7)(https://mavlink.io/en/messages/common.html#GLOBAL_POSITION_INT).
37-
int32_t* lon;
47+
int32_t* lon = nullptr;
3848

3949
// Altitude above home (unit:
4050
// mm)(https://mavlink.io/en/messages/common.html#GLOBAL_POSITION_INT).
41-
int32_t* relative_alt;
51+
int32_t* relative_alt = nullptr;
4252

4353
// Ground X Speed (Latitude, positive north) (unit:
4454
// cm/s)(https://mavlink.io/en/messages/common.html#GLOBAL_POSITION_INT).
45-
int16_t* vx;
55+
int16_t* vx = nullptr;
4656

4757
// Ground Y Speed (Longitude, positive east) (unit:
4858
// cm/s)(https://mavlink.io/en/messages/common.html#GLOBAL_POSITION_INT).
49-
int16_t* vy;
59+
int16_t* vy = nullptr;
5060

5161
// Ground Z Speed (Altitude, positive down) (unit:
5262
// cm/s)(https://mavlink.io/en/messages/common.html#GLOBAL_POSITION_INT).
53-
int16_t* vz;
63+
int16_t* vz = nullptr;
5464

5565
// Vehicle heading (yaw angle), 0.0..359.99 degrees. If unknown, set to: UINT16_MAX
5666
// (unit: cdeg)(https://mavlink.io/en/messages/common.html#GLOBAL_POSITION_INT).
57-
uint16_t* hdg;
67+
uint16_t* hdg = nullptr;
5868
// Timestamp (time since system boot) (unit: ms)
5969
// (https://mavlink.io/en/messages/common.html#GLOBAL_POSITION_INT).
60-
int32_t* time_boot_ms;
70+
int32_t* time_boot_ms = nullptr;
6171

6272
// Roll angle (-pi..+pi) (unit: rad)
6373
// (https://mavlink.io/en/messages/common.html#ATTITUDE).
64-
float* roll;
74+
float* roll = nullptr;
6575

6676
// Pitch angle (-pi..+pi) (unit: rad)
6777
// (https://mavlink.io/en/messages/common.html#ATTITUDE).
68-
float* pitch;
78+
float* pitch = nullptr;
6979

7080
// Yaw angle (-pi..+pi) (unit: rad)
7181
// (https://mavlink.io/en/messages/common.html#ATTITUDE).
72-
float* yaw;
82+
float* yaw = nullptr;
7383

7484
// Roll angular speed (unit: rad/s)
7585
// (https://mavlink.io/en/messages/common.html#ATTITUDE).
76-
float* rollspeed;
86+
float* rollspeed = nullptr;
7787

7888
// Pitch angular speed (unit: rad/s)
7989
// (https://mavlink.io/en/messages/common.html#ATTITUDE).
80-
float* pitchspeed;
90+
float* pitchspeed = nullptr;
8191

8292
// yawspeed Yaw angular speed (unit: rad/s)
8393
// (https://mavlink.io/en/messages/common.html#ATTITUDE).
84-
float* yawspeed;
94+
float* yawspeed = nullptr;
8595

96+
public:
97+
// Generate getters using the macro
98+
CREATE_GETTER(int32_t, alt)
99+
CREATE_GETTER(int32_t, lat)
100+
CREATE_GETTER(int32_t, lon)
101+
CREATE_GETTER(int32_t, relative_alt)
102+
CREATE_GETTER(int16_t, vx)
103+
CREATE_GETTER(int16_t, vy)
104+
CREATE_GETTER(int16_t, vz)
105+
CREATE_GETTER(uint16_t, hdg)
106+
CREATE_GETTER(int32_t, time_boot_ms)
107+
CREATE_GETTER(float, roll)
108+
CREATE_GETTER(float, pitch)
109+
CREATE_GETTER(float, yaw)
110+
CREATE_GETTER(float, rollspeed)
111+
CREATE_GETTER(float, pitchspeed)
112+
CREATE_GETTER(float, yawspeed)
113+
114+
CREATE_SETTER(int32_t, alt)
115+
CREATE_SETTER(int32_t, lat)
116+
CREATE_SETTER(int32_t, lon)
117+
CREATE_SETTER(int32_t, relative_alt)
118+
CREATE_SETTER(int16_t, vx)
119+
CREATE_SETTER(int16_t, vy)
120+
CREATE_SETTER(int16_t, vz)
121+
CREATE_SETTER(uint16_t, hdg)
122+
CREATE_SETTER(int32_t, time_boot_ms)
123+
CREATE_SETTER(float, roll)
124+
CREATE_SETTER(float, pitch)
125+
CREATE_SETTER(float, yaw)
126+
CREATE_SETTER(float, rollspeed)
127+
CREATE_SETTER(float, pitchspeed)
128+
CREATE_SETTER(float, yawspeed)
86129
};
87130

88131
class TelemetryManager {
89132
public:
90-
91-
// struct containing variables that relate to the state of the drone (lat, lng, yaw, pitch, etc..).
133+
// struct containing variables that relate to the state of the drone (lat, lng, yaw, pitch,
134+
// etc..).
92135
TMStateData stateData;
93136

94137
// the buffer that stores the bytes received from the ground station.
@@ -97,12 +140,10 @@ class TelemetryManager {
97140
// The object that facilitates communication with the ground station
98141
GroundStationCommunication& GSC;
99142

100-
101143
// System status flag (https://mavlink.io/en/messages/minimal.html#MAV_STATE).
102144
MAV_STATE& mavState;
103145
// System mode bitmap (https://mavlink.io/en/messages/minimal.html#MAV_MODE_FLAG).
104146
MAV_MODE_FLAG& mavMode;
105-
106147

107148
/**
108149
* @brief Create and configure FreeRTOS tasks.
@@ -121,11 +162,12 @@ class TelemetryManager {
121162
* To do so call the init() method.
122163
* @param stateData The state of the drone (lat, lng, yaw, pitch, etc..).
123164
* @param mav_state System status flag (https://mavlink.io/en/messages/minimal.html#MAV_STATE).
124-
* @param mav_mode System mode bitmap (https://mavlink.io/en/messages/minimal.html#MAV_MODE_FLAG).
165+
* @param mav_mode System mode bitmap
166+
* (https://mavlink.io/en/messages/minimal.html#MAV_MODE_FLAG).
125167
* @param GSC Object to handle communication with the groundstation
126168
* @param MT Object to translate MAVLink data
127169
*/
128-
TelemetryManager(TMStateData& stateData, MAV_STATE& mavState, MAV_MODE_FLAG& mavMode,
170+
TelemetryManager(TMStateData& stateData, MAV_STATE& mavState, MAV_MODE_FLAG& mavMode,
129171
GroundStationCommunication& GSC, MavlinkTranslator& MT);
130172

131173
/**

0 commit comments

Comments
 (0)