ChibiOS/HAL  7.0.3
memstreams.c
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 /**
18  * @file memstreams.c
19  * @brief Memory streams code.
20  *
21  * @addtogroup HAL_MEMORY_STREAMS
22  * @details Memory buffers handled as streams.
23  * @{
24  */
25 
26 #include <string.h>
27 
28 #include "hal.h"
29 #include "memstreams.h"
30 
31 /*===========================================================================*/
32 /* Driver local definitions. */
33 /*===========================================================================*/
34 
35 /*===========================================================================*/
36 /* Driver exported variables. */
37 /*===========================================================================*/
38 
39 /*===========================================================================*/
40 /* Driver local variables. */
41 /*===========================================================================*/
42 
43 /*===========================================================================*/
44 /* Driver local functions. */
45 /*===========================================================================*/
46 
47 static size_t _writes(void *ip, const uint8_t *bp, size_t n) {
48  MemoryStream *msp = ip;
49 
50  if (msp->size - msp->eos < n)
51  n = msp->size - msp->eos;
52  memcpy(msp->buffer + msp->eos, bp, n);
53  msp->eos += n;
54  return n;
55 }
56 
57 static size_t _reads(void *ip, uint8_t *bp, size_t n) {
58  MemoryStream *msp = ip;
59 
60  if (msp->eos - msp->offset < n)
61  n = msp->eos - msp->offset;
62  memcpy(bp, msp->buffer + msp->offset, n);
63  msp->offset += n;
64  return n;
65 }
66 
67 static msg_t _put(void *ip, uint8_t b) {
68  MemoryStream *msp = ip;
69 
70  if (msp->size - msp->eos <= 0)
71  return MSG_RESET;
72  *(msp->buffer + msp->eos) = b;
73  msp->eos += 1;
74  return MSG_OK;
75 }
76 
77 static msg_t _get(void *ip) {
78  uint8_t b;
79  MemoryStream *msp = ip;
80 
81  if (msp->eos - msp->offset <= 0)
82  return MSG_RESET;
83  b = *(msp->buffer + msp->offset);
84  msp->offset += 1;
85  return b;
86 }
87 
88 static const struct MemStreamVMT vmt = {(size_t)0, _writes, _reads, _put, _get};
89 
90 /*===========================================================================*/
91 /* Driver exported functions. */
92 /*===========================================================================*/
93 
94 /**
95  * @brief Memory stream object initialization.
96  *
97  * @param[out] msp pointer to the @p MemoryStream object to be initialized
98  * @param[in] buffer pointer to the memory buffer for the memory stream
99  * @param[in] size total size of the memory stream buffer
100  * @param[in] eos initial End Of Stream offset. Normally you need to
101  * put this to zero for RAM buffers or equal to @p size
102  * for ROM streams.
103  */
104 void msObjectInit(MemoryStream *msp, uint8_t *buffer,
105  size_t size, size_t eos) {
106 
107  msp->vmt = &vmt;
108  msp->buffer = buffer;
109  msp->size = size;
110  msp->eos = eos;
111  msp->offset = 0;
112 }
113 
114 /** @} */
HAL subsystem header.
void msObjectInit(MemoryStream *msp, uint8_t *buffer, size_t size, size_t eos)
Memory stream object initialization.
Definition: memstreams.c:104
const struct MemStreamVMT * vmt
Virtual Methods Table.
Definition: memstreams.h:72
int32_t msg_t
Type of a message.
Definition: osal.h:160
Memory stream object.
Definition: memstreams.h:70
Memory streams structures and macros.
MemStream virtual methods table.
Definition: memstreams.h:61