ChibiOS/HAL 9.0.0
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
47static 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
57static 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
67static 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
77static 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
88static msg_t _unget(void* ip, uint8_t b)
89{
90 MemoryStream* msp = ip;
91
92 if (msp->offset <= 0)
93 return MSG_RESET;
94 msp->offset -= 1;
95 *(msp->buffer + msp->offset) = b;
96
97 return MSG_OK;
98}
99
100static const struct MemStreamVMT vmt = {(size_t)0, _writes, _reads, _put, _get, _unget};
101
102/*===========================================================================*/
103/* Driver exported functions. */
104/*===========================================================================*/
105
106/**
107 * @brief Memory stream object initialization.
108 *
109 * @param[out] msp pointer to the @p MemoryStream object to be initialized
110 * @param[in] buffer pointer to the memory buffer for the memory stream
111 * @param[in] size total size of the memory stream buffer
112 * @param[in] eos initial End Of Stream offset. Normally you need to
113 * put this to zero for RAM buffers or equal to @p size
114 * for ROM streams.
115 */
116void msObjectInit(MemoryStream *msp, uint8_t *buffer,
117 size_t size, size_t eos) {
118
119 msp->vmt = &vmt;
120 msp->buffer = buffer;
121 msp->size = size;
122 msp->eos = eos;
123 msp->offset = 0;
124}
125
126/** @} */
static msg_t _unget(void *ip, uint8_t b)
Definition bufstreams.c:87
static size_t _writes(void *ip, const uint8_t *bp, size_t n)
Definition bufstreams.c:51
static size_t _reads(void *ip, uint8_t *bp, size_t n)
Definition bufstreams.c:56
static const struct EFlashDriverVMT vmt
Definition hal_efl.c:71
static msg_t _get(void *ip)
Definition memstreams.c:77
static msg_t _put(void *ip, uint8_t b)
Definition memstreams.c:67
static msg_t _unget(void *ip, uint8_t b)
Definition memstreams.c:88
static size_t _writes(void *ip, const uint8_t *bp, size_t n)
Definition memstreams.c:47
static size_t _reads(void *ip, uint8_t *bp, size_t n)
Definition memstreams.c:57
void msObjectInit(MemoryStream *msp, uint8_t *buffer, size_t size, size_t eos)
Memory stream object initialization.
Definition memstreams.c:116
int32_t msg_t
Type of a message.
Definition osal.h:159
#define MSG_OK
Definition osal.h:56
#define MSG_RESET
Definition osal.h:58
static msg_t _get(void *ip)
Definition hal_serial.c:67
static msg_t _put(void *ip, uint8_t b)
Definition hal_serial.c:62
HAL subsystem header.
Memory streams structures and macros.
MemStream virtual methods table.
Definition memstreams.h:61
Memory stream object.
Definition memstreams.h:70
const struct MemStreamVMT * vmt
Virtual Methods Table.
Definition memstreams.h:72