Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions projects/freertos/cmd_channel_freertos.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,50 @@ int cmd_channel_freertos_send_packet (QueueHandle_t tx_queue, struct cmd_packet

return 0;
}

/**
* Receive a command packet from a communication channel.
* This is an implementation of the receive_packet() function for
* the struct cmd_channel and uses the global I2CRequestQueue.
*
* @param channel The channel to receive a packet from.
* @param packet Output for the packet data being received.
* @param ms_timeout The amount of time to wait for a received packet, in milliseconds. A
* negative value will wait forever, and a value of 0 will return immediately.
*
* @return 0 if a packet was successfully received or an error code.
*/
static int cmd_channel_receive_packet (struct cmd_channel *channel,
struct cmd_packet *packet, int ms_timeout)
{
return cmd_channel_freertos_receive_packet(I2CRequestQueue, packet,
ms_timeout);
}

/**
* Send a command packet over a communication channel.
* This is an implementation of the send_packet() function for
* the struct cmd_channel and uses the global I2CResponseQueue.
*
* @param channel The channel to send a packet on.
* @param packet The packet to send.
*
* @return 0 if the the packet was successfully sent or an error code.
*/
static int cmd_channel_send_packet (struct cmd_channel *channel,
struct cmd_packet *packet)
{
return cmd_channel_freertos_send_packet(I2CResponseQueue, packet, 0);
}

/**
* Set the default receive_packet and send_packet function hooks.
* These use the global I2CRequestQueue and I2CResponseQueue.
*
* @param channel The channel to initialise.
*/
void cmd_channel_packet_default_init (struct cmd_channel *channel)
{
channel->receive_packet = cmd_channel_receive_packet;
channel->send_packet = cmd_channel_send_packet;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're looking to turn this into a proper implementation of cmd_channel and do it in way that is consistent with the overall architecture and existing coding style, we would do the following:

  1. Define a new 'struct cmd_channel_freertos' that derives from cmd_channel. This new structure would contain the Rx and Tx queues necessary for receive_packet and send_packet, removing the dependencies on any global context.
  2. Create cmd_channel_freertos_init and cmd_channel_freertos_release functions to initialize and free the instance. The init function returns int (returning error for null parameters), while release returns void (probably with an empty implementation since nothing would be internally created).
  3. The init function would take the cmd_channel_freertos instance to initialize and the Rx and Tx queue instances to use with the channel. It would save these internally and set the function pointers.
  4. The functions you have here as cmd_channel_receive_packet/send_packet would be named cmd_channel_freertos_receive_packet/send_packet, which would probably require a name change of the existing functions. No comment block is necessary on these functions since they are implementations of a base API.

}
3 changes: 3 additions & 0 deletions projects/freertos/cmd_channel_freertos.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
#include "queue.h"
#include "cmd_interface/cmd_channel.h"

extern QueueHandle_t I2CRequestQueue;
extern QueueHandle_t I2CResponseQueue;

int cmd_channel_freertos_receive_packet (QueueHandle_t rx_queue, struct cmd_packet *packet,
int ms_timeout);
int cmd_channel_freertos_send_packet (QueueHandle_t tx_queue, struct cmd_packet *packet,
int ms_timeout);

void cmd_channel_packet_default_init (struct cmd_channel *channel);

#endif /* CMD_CHANNEL_FREERTOS_H_ */