eRPC API Reference  Rev. 1.7.0
NXP Semiconductors
erpc_message_buffer.h
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2014-2016, Freescale Semiconductor, Inc.
4  * Copyright 2016 NXP
5  * All rights reserved.
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted (subject to the limitations in the disclaimer below) provided
10  * that the following conditions are met:
11  *
12  * o Redistributions of source code must retain the above copyright notice, this list
13  * of conditions and the following disclaimer.
14  *
15  * o Redistributions in binary form must reproduce the above copyright notice, this
16  * list of conditions and the following disclaimer in the documentation and/or
17  * other materials provided with the distribution.
18  *
19  * o Neither the name of the copyright holder nor the names of its
20  * contributors may be used to endorse or promote products derived from this
21  * software without specific prior written permission.
22  *
23  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef _EMBEDDED_RPC__MESSAGE_BUFFER_H_
37 #define _EMBEDDED_RPC__MESSAGE_BUFFER_H_
38 
39 #include "erpc_common.h"
40 #include <cstddef>
41 #include <stdint.h>
42 
49 // Classes
52 
53 namespace erpc {
63 {
64 public:
71  : m_buf(0)
72  , m_len(0)
73  , m_used(0)
74  {
75  }
76 
85  MessageBuffer(uint8_t *buffer, uint16_t length)
86  : m_buf(buffer)
87  , m_len(length)
88  , m_used(0)
89  {
90  }
91 
100  void set(uint8_t *buffer, uint16_t length)
101  {
102  m_buf = buffer;
103  m_len = length;
104  m_used = 0;
105  }
106 
112  uint8_t *get(void) { return m_buf; }
113 
119  const uint8_t *get(void) const { return m_buf; }
120 
126  uint16_t getLength(void) const { return m_len; }
127 
133  uint16_t getUsed(void) const { return m_used; }
134 
140  uint16_t getFree(void) const { return m_len - m_used; }
141 
147  void setUsed(uint16_t used) { m_used = used; }
148 
158  erpc_status_t read(uint16_t offset, void *data, uint32_t length);
159 
169  erpc_status_t write(uint16_t offset, const void *data, uint32_t length);
170 
178  erpc_status_t copy(const MessageBuffer *other);
179 
185  void swap(MessageBuffer *other);
186 
190  operator uint8_t *(void) { return m_buf; }
191 
195  operator const uint8_t *(void)const { return m_buf; }
196 
202  uint8_t &operator[](int index) { return m_buf[index]; }
203 
209  const uint8_t &operator[](int index) const { return m_buf[index]; }
210 
214  class Cursor
215  {
216  public:
222  Cursor(void)
223  : m_buffer(NULL)
224  , m_pos(NULL)
225  , m_remaining(0)
226  {
227  }
228 
237  : m_buffer(buffer)
238  , m_pos(buffer->get())
239  , m_remaining(buffer->getLength())
240  {
241  }
242 
248  void set(MessageBuffer *buffer);
249 
257  uint8_t *get(void) { return m_pos; }
258 
266  const uint8_t *get(void) const { return m_pos; }
267 
273  uint16_t getRemaining(void) const { return m_remaining; }
274 
284  erpc_status_t read(void *data, uint32_t length);
285 
295  erpc_status_t write(const void *data, uint32_t length);
296 
300  operator uint8_t *(void) { return m_pos; }
301 
305  operator const uint8_t *(void)const { return m_pos; }
306 
312  uint8_t &operator[](int index) { return m_pos[index]; }
313 
319  const uint8_t &operator[](int index) const { return m_pos[index]; }
320 
328  Cursor &operator+=(uint16_t n)
329  {
330  m_pos += n;
331  m_remaining -= n;
332  return *this;
333  }
334 
342  Cursor &operator-=(uint16_t n)
343  {
344  m_pos -= n;
345  m_remaining += n;
346  return *this;
347  }
348 
355  {
356  ++m_pos;
357  --m_remaining;
358  return *this;
359  }
360 
367  {
368  --m_pos;
369  ++m_remaining;
370  return *this;
371  }
372 
373  private:
374  MessageBuffer *m_buffer;
375  uint8_t *m_pos;
376  uint16_t m_remaining;
377  };
378 
379 private:
380  uint8_t *volatile m_buf;
381  uint16_t volatile m_len;
382  uint16_t volatile m_used;
383 };
384 
391 {
392 public:
399 
403  virtual ~MessageBufferFactory(void) {}
404 
410  virtual MessageBuffer create(void) = 0;
411 
417  virtual bool createServerBuffer(void) { return true; }
418 
427  virtual erpc_status_t prepareServerBufferForSend(MessageBuffer *message);
428 
434  virtual void dispose(MessageBuffer *buf) = 0;
435 };
436 
437 } // namespace erpc
438 
441 #endif // _EMBEDDED_RPC__MESSAGE_BUFFER_H_
uint16_t getRemaining(void) const
Return remaining free space in current buffer.
Definition: erpc_message_buffer.h:273
erpc_status_t read(uint16_t offset, void *data, uint32_t length)
This function read data from local buffer.
Definition: erpc_message_buffer.cpp:47
const uint8_t & operator[](int index) const
Array operator return value of buffer at given index.
Definition: erpc_message_buffer.h:319
int32_t erpc_status_t
Type used for all status and error return values.
Definition: erpc_common.h:112
Cursor(MessageBuffer *buffer)
Constructor.
Definition: erpc_message_buffer.h:236
erpc_status_t copy(const MessageBuffer *other)
This function copy given message buffer to local instance.
Definition: erpc_message_buffer.cpp:77
MessageBuffer(void)
Constructor.
Definition: erpc_message_buffer.h:70
void setUsed(uint16_t used)
This function sets length of used space of buffer.
Definition: erpc_message_buffer.h:147
virtual ~MessageBufferFactory(void)
ClientManager destructor.
Definition: erpc_message_buffer.h:403
Cursor within a MessageBuffer.
Definition: erpc_message_buffer.h:214
uint8_t & operator[](int index)
Array operator return value of buffer at given index.
Definition: erpc_message_buffer.h:202
uint16_t getUsed(void) const
This function returns length of used space of buffer.
Definition: erpc_message_buffer.h:133
erpc_status_t read(void *data, uint32_t length)
Read data from current buffer.
Definition: erpc_message_buffer.cpp:108
Cursor & operator--(void)
Substract -1 operator.
Definition: erpc_message_buffer.h:366
uint8_t * get(void)
Return position in buffer.
Definition: erpc_message_buffer.h:257
MessageBufferFactory(void)
Constructor.
Definition: erpc_message_buffer.h:398
uint8_t & operator[](int index)
Array operator return value of buffer at given index.
Definition: erpc_message_buffer.h:312
Cursor & operator++(void)
Sum +1 operator.
Definition: erpc_message_buffer.h:354
uint16_t getLength(void) const
This function returns length of buffer.
Definition: erpc_message_buffer.h:126
uint16_t getFree(void) const
This function returns length of free space of buffer.
Definition: erpc_message_buffer.h:140
Represents a memory buffer containing a message.
Definition: erpc_message_buffer.h:62
Definition: erpc_arbitrated_client_manager.h:51
void swap(MessageBuffer *other)
This function swap message buffer attributes between given instance and local instance.
Definition: erpc_message_buffer.cpp:86
Cursor(void)
Constructor.
Definition: erpc_message_buffer.h:222
virtual bool createServerBuffer(void)
This function inform server if it has to create buffer for received message.
Definition: erpc_message_buffer.h:417
MessageBuffer(uint8_t *buffer, uint16_t length)
Constructor.
Definition: erpc_message_buffer.h:85
erpc_status_t write(const void *data, uint32_t length)
Read data from current buffer.
Definition: erpc_message_buffer.cpp:123
Abstract interface for message buffer factory.
Definition: erpc_message_buffer.h:390
Cursor & operator-=(uint16_t n)
Substract operator return local buffer.
Definition: erpc_message_buffer.h:342
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:62
Cursor & operator+=(uint16_t n)
Sum operator return local buffer.
Definition: erpc_message_buffer.h:328
const uint8_t & operator[](int index) const
Array operator return value of buffer at given index.
Definition: erpc_message_buffer.h:209