eRPC API Reference  Rev. 1.11.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:
46  : m_buf(NULL)
47  , m_len(0)
48  , m_used(0)
49  {
50  }
51 
60  MessageBuffer(uint8_t *buffer, uint16_t length)
61  : m_buf(buffer)
62  , m_len(length)
63  , m_used(0)
64  {
65  }
66 
75  void set(uint8_t *buffer, uint16_t length)
76  {
77  m_buf = buffer;
78  m_len = length;
79  m_used = 0;
80  }
81 
87  uint8_t *get(void) { return m_buf; }
88 
94  const uint8_t *get(void) const { return m_buf; }
95 
101  uint16_t getLength(void) const { return m_len; }
102 
108  uint16_t getUsed(void) const { return m_used; }
109 
115  uint16_t getFree(void) const { return m_len - m_used; }
116 
122  void setUsed(uint16_t used);
123 
133  erpc_status_t read(uint16_t offset, void *data, uint32_t length);
134 
144  erpc_status_t write(uint16_t offset, const void *data, uint32_t length);
145 
153  erpc_status_t copy(const MessageBuffer *other);
154 
160  void swap(MessageBuffer *other);
161 
165  operator uint8_t *(void) { return m_buf; }
166 
170  operator const uint8_t *(void) const { return m_buf; }
171 
177  uint8_t &operator[](int index) { return m_buf[index]; }
178 
184  const uint8_t &operator[](int index) const { return m_buf[index]; }
185 
189  class Cursor
190  {
191  public:
197  Cursor(void)
198  : m_buffer(NULL)
199  , m_pos(NULL)
200  {
201  }
202 
210  explicit Cursor(MessageBuffer *buffer)
211  : m_buffer(buffer)
212  , m_pos(buffer->get())
213  {
214  }
215 
221  void set(MessageBuffer *buffer);
222 
230  uint8_t *get(void) { return m_pos; }
231 
239  const uint8_t *get(void) const { return m_pos; }
240 
246  uint16_t getRemaining(void) const { return m_buffer->getLength() - (uint16_t)(m_pos - m_buffer->get()); }
247 
253  uint16_t getRemainingUsed(void) const { return m_buffer->getUsed() - (uint16_t)(m_pos - m_buffer->get()); }
254 
264  erpc_status_t read(void *data, uint32_t length);
265 
275  erpc_status_t write(const void *data, uint32_t length);
276 
280  operator uint8_t *(void) { return m_pos; }
281 
285  operator const uint8_t *(void) const { return m_pos; }
286 
292  uint8_t &operator[](int index);
293 
299  const uint8_t &operator[](int index) const;
300 
308  Cursor &operator+=(uint16_t n);
309 
317  Cursor &operator-=(uint16_t n);
318 
324  Cursor &operator++(void);
325 
331  Cursor &operator--(void);
332 
333  private:
334  MessageBuffer *m_buffer;
335  uint8_t *m_pos;
336  };
337 
338 private:
339  uint8_t *volatile m_buf;
340  uint16_t volatile m_len;
341  uint16_t volatile m_used;
342 };
343 
350 {
351 public:
358 
362  virtual ~MessageBufferFactory(void) {}
363 
369  virtual MessageBuffer create(void) = 0;
370 
376  virtual bool createServerBuffer(void) { return true; }
377 
386  virtual erpc_status_t prepareServerBufferForSend(MessageBuffer *message);
387 
393  virtual void dispose(MessageBuffer *buf) = 0;
394 };
395 
396 } // namespace erpc
397 
400 #endif // _EMBEDDED_RPC__MESSAGE_BUFFER_H_
uint16_t getRemaining(void) const
Return remaining free space in current buffer.
Definition: erpc_message_buffer.hpp:246
erpc_status_t read(uint16_t offset, void *data, uint32_t length)
This function read data from local buffer.
Definition: erpc_message_buffer.cpp:30
Cursor(MessageBuffer *buffer)
Constructor.
Definition: erpc_message_buffer.hpp:210
erpc_status_t copy(const MessageBuffer *other)
This function copy given message buffer to local instance.
Definition: erpc_message_buffer.cpp:76
uint8_t & operator[](int index)
Array operator return value of buffer at given index.
Definition: erpc_message_buffer.cpp:114
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
Cursor & operator+=(uint16_t n)
Sum operator return local buffer.
Definition: erpc_message_buffer.cpp:130
virtual ~MessageBufferFactory(void)
MessageBufferFactory destructor.
Definition: erpc_message_buffer.hpp:362
Cursor & operator--(void)
Substract -1 operator.
Definition: erpc_message_buffer.cpp:157
Cursor within a MessageBuffer.
Definition: erpc_message_buffer.hpp:189
uint8_t & operator[](int index)
Array operator return value of buffer at given index.
Definition: erpc_message_buffer.hpp:177
uint16_t getUsed(void) const
This function returns length of used space of buffer.
Definition: erpc_message_buffer.hpp:108
erpc_status_t read(void *data, uint32_t length)
Read data from current buffer.
Definition: erpc_message_buffer.cpp:166
uint8_t * get(void)
Return position in buffer.
Definition: erpc_message_buffer.hpp:230
MessageBufferFactory(void)
Constructor.
Definition: erpc_message_buffer.hpp:357
uint16_t getLength(void) const
This function returns length of buffer.
Definition: erpc_message_buffer.hpp:101
Cursor & operator++(void)
Sum +1 operator.
Definition: erpc_message_buffer.cpp:148
uint16_t getFree(void) const
This function returns length of free space of buffer.
Definition: erpc_message_buffer.hpp:115
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:89
uint8_t * get(void)
This function returns pointer to buffer to read/write.
Definition: erpc_message_buffer.hpp:87
uint16_t getRemainingUsed(void) const
Return remaining space from used of current buffer.
Definition: erpc_message_buffer.hpp:253
Cursor & operator-=(uint16_t n)
Substract operator return local buffer.
Definition: erpc_message_buffer.cpp:139
Cursor(void)
Constructor.
Definition: erpc_message_buffer.hpp:197
void setUsed(uint16_t used)
This function sets length of used space of buffer.
Definition: erpc_message_buffer.cpp:23
virtual bool createServerBuffer(void)
This function informs server if it has to create buffer for received message.
Definition: erpc_message_buffer.hpp:376
MessageBuffer(uint8_t *buffer, uint16_t length)
Constructor.
Definition: erpc_message_buffer.hpp:60
erpc_status_t write(const void *data, uint32_t length)
Read data from current buffer.
Definition: erpc_message_buffer.cpp:196
Abstract interface for message buffer factory.
Definition: erpc_message_buffer.hpp:349
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:53
const uint8_t & operator[](int index) const
Array operator return value of buffer at given index.
Definition: erpc_message_buffer.hpp:184