Recommended Usage Pattern
7) Recommended usage pattern
A clean pattern is:
- define handler callbacks
- define one config object per packet type using the macros
- place all config objects in an array
- pass that array to
PacketDispatcherInit()
Example
//Imports
#include "packet_dispatcher.h"
#include "packet_dispatcher_macros.h"
/*Define handler callbacks*/
//(such that: typedef result_t (*packet_handler_t)(void* buffer) )
static result_t handle_drive_msg(void* buffer) {
PBDriveMsg* msg = (PBDriveMsg*)buffer;
return process_drive_msg(msg);
}
static result_t handle_sensor_diag(void* buffer) {
PBSensorDiag* msg = (PBSensorDiag*)buffer;
return process_sensor_diag(msg);
}
PACKET_HANDLER_CONFIG_STATIC(drive_handler_cfg,
PBEnvelope_drive_msg_tag,
drive_msg,
handle_drive_msg);
PACKET_HANDLER_CONFIG_STATIC_QUEUE(sensor_diag_handler_cfg,
PBEnvelope_sensor_diag_tag,
sensor_diag,
handle_sensor_diag,
10U);
static packet_handler_config_t* handlers[] = {
drive_handler_cfg,
sensor_diag_handler_cfg,
};
Important note about array type
The current PacketDispatcherInit() API expects:
packet_handler_config_t* handlers
meaning a contiguous array of structs, not an array of pointers.
So with the current implementation, the final array should actually be:
static packet_handler_config_t handlers[] = {
drive_handler_cfg,
sensor_diag_handler_cfg,
};
not an array of pointers.
That distinction matters. The macros define actual config objects, not pointers.