MIP_SDK  v3.0.0
MicroStrain Communications Library for embedded systems
mip_cmdqueue.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <mip/mip_cmdqueue.h>
4 
5 #include "mip_result.hpp"
6 
7 #include <assert.h>
8 #include <cstring>
9 
10 
11 namespace mip
12 {
16 
20 struct CmdQueue : public C::mip_cmd_queue
21 {
23  ~CmdQueue() { assert(_first_pending_cmd==nullptr); }
24 
25  CmdQueue(const CmdQueue&) = delete;
26  CmdQueue& operator=(const CmdQueue&) = delete;
27 
30 
31  void clear() { C::mip_cmd_queue_clear(this); }
32 
33  void update(Timestamp now) { C::mip_cmd_queue_update(this, now); }
34 
37 
38  void processPacket(const C::mip_packet_view& packet, Timestamp timestamp) { C::mip_cmd_queue_process_packet(this, &packet, timestamp); }
39 };
40 static_assert(sizeof(CmdQueue) == sizeof(C::mip_cmd_queue), "CmdQueue must not have additional data members.");
41 
42 
47 {
50  PendingCmd() { std::memset(static_cast<C::mip_pending_cmd*>(this), 0, sizeof(C::mip_pending_cmd)); }
51 
58  PendingCmd(uint8_t descriptorSet, uint8_t fieldDescriptor, Timeout additionalTime=0) { C::mip_pending_cmd_init_with_timeout(this, descriptorSet, fieldDescriptor, additionalTime); }
59 
69  PendingCmd(uint8_t descriptorSet, uint8_t fieldDescriptor, uint8_t responseDescriptor, uint8_t* responseBuffer, uint8_t responseBufferSize, Timeout additionalTime) { C::mip_pending_cmd_init_full(this, descriptorSet, fieldDescriptor, responseDescriptor, responseBuffer, responseBufferSize, additionalTime); }
70 
76  template<class Cmd>
77  PendingCmd(const Cmd& cmd, Timeout additionalTime=0) : PendingCmd(cmd.descriptorSet, cmd.fieldDescriptor, additionalTime) {}
78 
86  template<class Cmd>
87  PendingCmd(const Cmd& cmd, uint8_t* responseBuffer, uint8_t responseBufferSize, Timeout additionalTime=0) : PendingCmd(Cmd::DESCRIPTOR_SET, Cmd::FIELD_DESCRIPTOR, Cmd::Response::FIELD_DESCRIPTOR, responseBuffer, responseBufferSize, additionalTime) {}
88 
91  PendingCmd(const PendingCmd&) = delete;
92  PendingCmd& operator=(const PendingCmd&) = delete;
93 
96  ~PendingCmd() { CmdResult tmp = status(); assert(tmp.isFinished() || tmp==CmdResult::STATUS_NONE); (void)tmp; }
97 
100  CmdResult status() const { return C::mip_pending_cmd_status(this); }
101 
104 
106  const uint8_t* response() const { return C::mip_pending_cmd_response(this); }
107 
109  uint8_t responseLength() const { return C::mip_pending_cmd_response_length(this); }
110 };
111 
114 } // namespace mip
mip::PendingCmd::responseDescriptor
uint8_t responseDescriptor() const
Returns the response descriptor.
Definition: mip_cmdqueue.hpp:103
mip::C::mip_pending_cmd_response
const uint8_t * mip_pending_cmd_response(const mip_pending_cmd *cmd)
Returns the response payload pointer.
Definition: mip_cmdqueue.c:128
mip::CmdQueue::baseReplyTimeout
Timeout baseReplyTimeout() const
Definition: mip_cmdqueue.hpp:36
mip::C::mip_cmd_queue
struct mip::C::mip_cmd_queue mip_cmd_queue
Holds a list of pending commands.
mip
A collection of C++ classes and functions covering the full mip api.
Definition: commands_3dm.c:11
mip::CmdQueue::clear
void clear()
Definition: mip_cmdqueue.hpp:31
mip::PendingCmd::PendingCmd
PendingCmd(uint8_t descriptorSet, uint8_t fieldDescriptor, Timeout additionalTime=0)
Create a pending command for the given descriptor pair.
Definition: mip_cmdqueue.hpp:58
mip::CmdQueue::enqueue
void enqueue(C::mip_pending_cmd &cmd)
Definition: mip_cmdqueue.hpp:28
mip::C::mip_cmd_queue_process_packet
void mip_cmd_queue_process_packet(mip_cmd_queue *queue, const mip_packet_view *packet, mip_timestamp timestamp)
Process an incoming packet and check for replies to pending commands.
Definition: mip_cmdqueue.c:381
mip::PendingCmd::PendingCmd
PendingCmd(const Cmd &cmd, Timeout additionalTime=0)
Create a pending command given the actual command struct.
Definition: mip_cmdqueue.hpp:77
mip::C::mip_cmd_queue_clear
void mip_cmd_queue_clear(mip_cmd_queue *queue)
Clears the command queue.
Definition: mip_cmdqueue.c:423
mip::CmdQueue::~CmdQueue
~CmdQueue()
Definition: mip_cmdqueue.hpp:23
mip::CmdQueue::dequeue
void dequeue(C::mip_pending_cmd &cmd)
Definition: mip_cmdqueue.hpp:29
mip_cmdqueue.h
mip::C::mip_cmd_queue_base_reply_timeout
mip_timeout mip_cmd_queue_base_reply_timeout(const mip_cmd_queue *queue)
Gets the base reply timeout for all commands.
Definition: mip_cmdqueue.c:493
mip::CmdQueue::setBaseReplyTimeout
void setBaseReplyTimeout(Timeout timeout)
Definition: mip_cmdqueue.hpp:35
mip::C::mip_cmd_queue
Holds a list of pending commands.
Definition: mip_cmdqueue.h:88
mip::C::mip_packet_view
Structure representing a MIP Packet.
Definition: mip_packet.h:50
mip::C::mip_cmd_queue_enqueue
void mip_cmd_queue_enqueue(mip_cmd_queue *queue, mip_pending_cmd *cmd)
Queue a command to wait for replies.
Definition: mip_cmdqueue.c:227
mip::CmdResult::isFinished
bool isFinished() const
Definition: mip_result.hpp:70
mip::CmdQueue::update
void update(Timestamp now)
Definition: mip_cmdqueue.hpp:33
mip::PendingCmd
C++ class representing the state of a MIP command.
Definition: mip_cmdqueue.hpp:46
mip::PendingCmd::~PendingCmd
~PendingCmd()
Sanity check that the PendingCmd is not deallocated while still in the queue.
Definition: mip_cmdqueue.hpp:96
mip::commands_3dm::DESCRIPTOR_SET
@ DESCRIPTOR_SET
Definition: commands_3dm.hpp:31
mip::PendingCmd::status
CmdResult status() const
Gets the status of the pending command.
Definition: mip_cmdqueue.hpp:100
mip::CmdResult
Represents the status of a MIP command.
Definition: mip_result.hpp:24
mip::C::mip_cmd_queue_dequeue
void mip_cmd_queue_dequeue(mip_cmd_queue *queue, mip_pending_cmd *cmd)
Removes a pending command from the queue.
Definition: mip_cmdqueue.c:250
mip::PendingCmd::responseLength
uint8_t responseLength() const
Returns the length of the response data.
Definition: mip_cmdqueue.hpp:109
mip::C::mip_pending_cmd_response_length
uint8_t mip_pending_cmd_response_length(const mip_pending_cmd *cmd)
Returns the length of the response data.
Definition: mip_cmdqueue.c:142
mip::C::mip_pending_cmd_status
enum mip_cmd_result mip_pending_cmd_status(const mip_pending_cmd *cmd)
Returns the status of the pending command.
Definition: mip_cmdqueue.c:110
mip::PendingCmd::PendingCmd
PendingCmd(const Cmd &cmd, uint8_t *responseBuffer, uint8_t responseBufferSize, Timeout additionalTime=0)
Create a pending command given the actual command struct and a response buffer.
Definition: mip_cmdqueue.hpp:87
mip::C::mip_pending_cmd_init_full
void mip_pending_cmd_init_full(mip_pending_cmd *cmd, uint8_t descriptor_set, uint8_t field_descriptor, uint8_t response_descriptor, uint8_t *response_buffer, uint8_t response_buffer_size, mip_timeout additional_time)
Initialize a pending mip commmand with all parameters.
Definition: mip_cmdqueue.c:90
mip::PendingCmd::PendingCmd
PendingCmd(uint8_t descriptorSet, uint8_t fieldDescriptor, uint8_t responseDescriptor, uint8_t *responseBuffer, uint8_t responseBufferSize, Timeout additionalTime)
Create a pending command with expected response.
Definition: mip_cmdqueue.hpp:69
mip::C::mip_cmd_queue_set_base_reply_timeout
void mip_cmd_queue_set_base_reply_timeout(mip_cmd_queue *queue, mip_timeout timeout)
Sets the base reply timeout for all commands.
Definition: mip_cmdqueue.c:483
mip::PendingCmd::operator=
PendingCmd & operator=(const PendingCmd &)=delete
mip::Timestamp
microstrain::EmbeddedTimestamp Timestamp
Definition: mip_types.h:50
mip::C::mip_pending_cmd
Represents a command awaiting a reply from the device.
Definition: mip_cmdqueue.h:37
mip::C::mip_cmd_queue_update
void mip_cmd_queue_update(mip_cmd_queue *queue, mip_timestamp now)
Call periodically to make sure commands time out if no packets are received.
Definition: mip_cmdqueue.c:446
mip::C::mip_pending_cmd_response_descriptor
uint8_t mip_pending_cmd_response_descriptor(const mip_pending_cmd *cmd)
Returns the response descriptor.
Definition: mip_cmdqueue.c:118
mip::CmdQueue::operator=
CmdQueue & operator=(const CmdQueue &)=delete
mip::PendingCmd::PendingCmd
PendingCmd()
Create a null pending command in the CmdResult::NONE state.
Definition: mip_cmdqueue.hpp:50
mip::C::mip_cmd_queue_init
void mip_cmd_queue_init(mip_cmd_queue *queue, mip_timeout base_reply_timeout)
Initializes a command queue.
Definition: mip_cmdqueue.c:206
mip::Timeout
microstrain::EmbeddedTimeout Timeout
Definition: mip_types.h:51
mip::CmdResult::STATUS_NONE
static constexpr C::mip_cmd_result STATUS_NONE
Command has been initialized but not queued yet.
Definition: mip_result.hpp:32
mip::CmdQueue::processPacket
void processPacket(const C::mip_packet_view &packet, Timestamp timestamp)
Definition: mip_cmdqueue.hpp:38
mip::C::mip_pending_cmd_init_with_timeout
void mip_pending_cmd_init_with_timeout(mip_pending_cmd *cmd, uint8_t descriptor_set, uint8_t field_descriptor, mip_timeout additional_time)
Initialize a pending mip commmand with extra timeout time.
Definition: mip_cmdqueue.c:48
mip::PendingCmd::response
const uint8_t * response() const
Returns the response payload pointer.
Definition: mip_cmdqueue.hpp:106
mip::CmdQueue::CmdQueue
CmdQueue(Timeout baseReplyTimeout=1000)
Definition: mip_cmdqueue.hpp:22
mip_result.hpp
mip::CmdQueue
C++ wrapper around a command queue.
Definition: mip_cmdqueue.hpp:20