eRPC API Reference  Rev. 1.12.0
NXP Semiconductors
erpc_message_buffer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2016, Freescale Semiconductor, Inc.
3  * Copyright 2016 NXP
4  * All rights reserved.
5  *
6  *
7  * SPDX-License-Identifier: BSD-3-Clause
8  */
9 
10 #ifndef _EMBEDDED_RPC__MESSAGE_BUFFER_H_
11 #define _EMBEDDED_RPC__MESSAGE_BUFFER_H_
12 
13 #include "erpc_common.h"
14 
15 #include <cstddef>
16 #include <stdint.h>
17 
24 // Classes
27 
28 namespace erpc {
38 {
39 public:
45  MessageBuffer(void) : m_buf(NULL), m_len(0), m_used(0) {}
46 
55  MessageBuffer(uint8_t *buffer, uint16_t length) : m_buf(buffer), m_len(length), m_used(0) {}
56 
57  MessageBuffer(const MessageBuffer &buffer);
58 
67  void set(uint8_t *buffer, uint16_t length)
68  {
69  m_buf = buffer;
70  m_len = length;
71  m_used = 0;
72  }
73 
79  uint8_t *get(void) { return m_buf; }
80 
86  const uint8_t *get(void) const { return m_buf; }
87 
93  uint16_t getLength(void) const { return m_len; }
94 
100  uint16_t getUsed(void) const { return m_used; }
101 
107  uint16_t getFree(void) const { return m_len - m_used; }
108 
114  void setUsed(uint16_t used);
115 
125  erpc_status_t read(uint16_t offset, void *data, uint32_t length);
126 
136  erpc_status_t write(uint16_t offset, const void *data, uint32_t length);
137 
145  erpc_status_t copy(const MessageBuffer *other);
146 
152  void swap(MessageBuffer *other);
153 
157  operator uint8_t *(void) { return m_buf; }
158 
162  operator const uint8_t *(void) const { return m_buf; }
163 
169  uint8_t &operator[](int index) { return m_buf[index]; }
170 
176  const uint8_t &operator[](int index) const { return m_buf[index]; }
177 
178 private:
179  uint8_t *volatile m_buf;
180  uint16_t volatile m_len;
181  uint16_t volatile m_used;
182 };
183 
187 class Cursor
188 {
189 public:
195  Cursor(void) : m_buffer(), m_pos(NULL) {}
196 
204  explicit Cursor(MessageBuffer &buffer) : m_buffer(buffer), m_pos(buffer.get()) {}
205 
212  void setBuffer(MessageBuffer &buffer, uint8_t reserved = 0);
213 
217  MessageBuffer getBuffer(void);
218 
222  MessageBuffer &getBufferRef(void);
223 
231  uint8_t *get(void) { return m_pos; }
232 
240  const uint8_t *get(void) const { return m_pos; }
241 
247  uint16_t getRemaining(void) const { return m_buffer.getLength() - (uint16_t)(m_pos - m_buffer.get()); }
248 
254  uint16_t getRemainingUsed(void) const { return m_buffer.getUsed() - (uint16_t)(m_pos - m_buffer.get()); }
255 
265  erpc_status_t read(void *data, uint32_t length);
266 
276  erpc_status_t write(const void *data, uint32_t length);
277 
281  operator uint8_t *(void) { return m_pos; }
282 
286  operator const uint8_t *(void) const { return m_pos; }
287 
293  uint8_t &operator[](int index);
294 
300  const uint8_t &operator[](int index) const;
301 
309  Cursor &operator+=(uint16_t n);
310 
318  Cursor &operator-=(uint16_t n);
319 
325  Cursor &operator++(void);
326 
332  Cursor &operator--(void);
333 
334 private:
335  MessageBuffer m_buffer;
336  uint8_t *m_pos;
337 };
338 
345 {
346 public:
352  MessageBufferFactory(void);
353 
357  virtual ~MessageBufferFactory(void);
358 
364  virtual MessageBuffer create(void) = 0;
365 
375  MessageBuffer create(uint8_t reserveHeaderSize);
376 
382  virtual bool createServerBuffer(void);
383 
393  virtual erpc_status_t prepareServerBufferForSend(MessageBuffer &message, uint8_t reserveHeaderSize = 0);
394 
400  virtual void dispose(MessageBuffer *buf) = 0;
401 };
402 
403 } // namespace erpc
404 
407 #endif // _EMBEDDED_RPC__MESSAGE_BUFFER_H_
erpc_status_t read(uint16_t offset, void *data, uint32_t length)
This function read data from local buffer.
Definition: erpc_message_buffer.cpp:38
erpc_status_t copy(const MessageBuffer *other)
This function copy given message buffer to local instance.
Definition: erpc_message_buffer.cpp:84
Cursor(MessageBuffer &buffer)
Constructor.
Definition: erpc_message_buffer.hpp:204
enum _erpc_status erpc_status_t
Type used for all status and error return values.
Definition: erpc_common.h:85
MessageBuffer(void)
Constructor.
Definition: erpc_message_buffer.hpp:45
uint8_t & operator[](int index)
Array operator return value of buffer at given index.
Definition: erpc_message_buffer.hpp:169
uint16_t getUsed(void) const
This function returns length of used space of buffer.
Definition: erpc_message_buffer.hpp:100
Cursor within a MessageBuffer.
Definition: erpc_message_buffer.hpp:187
uint16_t getRemainingUsed(void) const
Return remaining space from used of current buffer.
Definition: erpc_message_buffer.hpp:254
uint16_t getRemaining(void) const
Return remaining free space in current buffer.
Definition: erpc_message_buffer.hpp:247
uint16_t getLength(void) const
This function returns length of buffer.
Definition: erpc_message_buffer.hpp:93
uint16_t getFree(void) const
This function returns length of free space of buffer.
Definition: erpc_message_buffer.hpp:107
Represents a memory buffer containing a message.
Definition: erpc_message_buffer.hpp:37
Definition: erpc_arbitrated_client_manager.hpp:25
void swap(MessageBuffer *other)
This function swap message buffer attributes between given instance and local instance.
Definition: erpc_message_buffer.cpp:97
uint8_t * get(void)
This function returns pointer to buffer to read/write.
Definition: erpc_message_buffer.hpp:79
void setUsed(uint16_t used)
This function sets length of used space of buffer.
Definition: erpc_message_buffer.cpp:31
MessageBuffer(uint8_t *buffer, uint16_t length)
Constructor.
Definition: erpc_message_buffer.hpp:55
Abstract interface for message buffer factory.
Definition: erpc_message_buffer.hpp:344
erpc_status_t write(uint16_t offset, const void *data, uint32_t length)
This function write data to local buffer.
Definition: erpc_message_buffer.cpp:61
Cursor(void)
Constructor.
Definition: erpc_message_buffer.hpp:195
const uint8_t & operator[](int index) const
Array operator return value of buffer at given index.
Definition: erpc_message_buffer.hpp:176