Class VescPacketFactory

Class Documentation

class VescPacketFactory

Class for creating VESC packets from raw data.

Public Types

typedef std::function<VescPacketPtr(std::shared_ptr<VescFrame>)> CreateFn

Public Functions

VescPacketFactory(const VescPacketFactory&) = delete

Delete copy constructor and equals operator.

VescPacketFactory &operator=(const VescPacketFactory&) = delete

Public Static Functions

static VescPacketFactory *getFactory()

Return the global factory object

static VescPacketPtr createPacket(const Buffer::const_iterator &begin, const Buffer::const_iterator &end, int *num_bytes_needed, std::string *what)

Create a VescPacket from a buffer (factory function). Packet must start (start of frame character) at begin and complete (end of frame character) before *p end. The buffer element at end is not examined, i.e. it can be the past-the-end element. Only returns a packet if the packet is valid, i.e. valid size, matching checksum, complete etc. An empty pointer is returned if a packet cannot be found or if it is invalid. If a valid packet is not found, optional output parameter @what is set to a string providing a reason why a packet was not found. If a packet was not found because additional bytes are needed on the buffer, optional output parameter num_bytes_needed will contain the number of bytes needed to either determine the size of the packet or complete the packet. Output parameters num_bytes_needed and what will be set to 0 and empty if a valid packet is found.

Parameters:
  • begin[in] – Iterator to a buffer at the start-of-frame character

  • end[in] – Iterator to the buffer past-the-end element.

  • num_bytes_needed[out] – Number of bytes needed to determine the packet size or complete the frame.

  • what[out] – Human readable string giving a reason why the packet was not found.

Returns:

Pointer to a valid VescPacket if successful. Otherwise, an empty pointer.

static void registerPacketType(int payload_id, CreateFn fn)

Register a packet type with the factory.