ChibiOS/HAL 9.0.0
bufstreams.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 This file was contributed by Alex Lewontin.
19 */
20
21/**
22 * @file bufstreams.c
23 * @brief Buffered streams code.
24 *
25 * @addtogroup HAL_BUFFERED_STREAMS
26 * @details Wrapper for BaseSequentialStreams that allows some ungetting.
27 * @{
28 */
29
30#include <string.h>
31
32#include "hal.h"
33#include "bufstreams.h"
34
35/*===========================================================================*/
36/* Driver local definitions. */
37/*===========================================================================*/
38
39/*===========================================================================*/
40/* Driver exported variables. */
41/*===========================================================================*/
42
43/*===========================================================================*/
44/* Driver local variables. */
45/*===========================================================================*/
46
47/*===========================================================================*/
48/* Driver local functions. */
49/*===========================================================================*/
50
51static size_t _writes(void *ip, const uint8_t *bp, size_t n) {
52 BufferedStreamAdapter *bsap = ip;
53 return bsap->bssp->vmt->write(bsap->bssp, bp, n);
54}
55
56static size_t _reads(void *ip, uint8_t *bp, size_t n) {
57 BufferedStreamAdapter* bsap = ip;
58 size_t buffered;
59
60 if (n < (BUFSTREAM_BUFFER_SIZE - bsap->ndx)) {
61 buffered = n;
62 } else {
63 buffered = BUFSTREAM_BUFFER_SIZE - bsap->ndx;
64 }
65
66 memcpy(bp, bsap->buffer + (BUFSTREAM_BUFFER_SIZE - buffered), buffered);
67 bsap->ndx += buffered;
68
69 return buffered + bsap->bssp->vmt->read(bsap->bssp, bp + buffered, n - buffered);
70}
71
72static msg_t _put(void *ip, uint8_t b) {
73 BufferedStreamAdapter* bsap = ip;
74 return bsap->bssp->vmt->put(bsap->bssp, b);
75}
76
77static msg_t _get(void *ip) {
78 BufferedStreamAdapter* bsap = ip;
79
80 if (bsap->ndx == BUFSTREAM_BUFFER_SIZE) {
81 return bsap->bssp->vmt->get(bsap->bssp);
82 }
83
84 return bsap->buffer[bsap->ndx++];
85}
86
87static msg_t _unget(void* ip, uint8_t b) {
88 BufferedStreamAdapter* bsap = ip;
89
90 if (((int8_t)b == STM_RESET) || (bsap->ndx == 0)) {
91 return STM_RESET;
92 }
93
94 bsap->buffer[--(bsap->ndx)] = b;
95
96 return b;
97}
98
99static const struct BufferedStreamAdapterVMT vmt = {
100 (size_t)0, _writes, _reads, _put, _get, _unget
101};
102
103/*===========================================================================*/
104/* Driver exported functions. */
105/*===========================================================================*/
106
107/**
108 * @brief Buffered stream adapter object initialization.
109 *
110 * @param[out] bsap pointer to the @p BufferedStream object to be
111 * initialized
112 * @param[in] bssp pointer to a @p BaseSequentialStream fulfilling
113 * object to be wrapped
114 */
116
117 bsap->vmt = &vmt;
118 bsap->bssp = bssp;
119 memset(bsap->buffer, STM_RESET, BUFSTREAM_BUFFER_SIZE);
120 bsap->ndx = BUFSTREAM_BUFFER_SIZE;
121}
122
123/** @} */
Buffered streams structures and macros.
static msg_t _get(void *ip)
Definition bufstreams.c:77
static msg_t _put(void *ip, uint8_t b)
Definition bufstreams.c:72
#define BUFSTREAM_BUFFER_SIZE
Buffer size for unget.
Definition bufstreams.h:40
void bsaObjectInit(BufferedStreamAdapter *bsap, BaseSequentialStream *bssp)
Buffered stream adapter object initialization.
Definition bufstreams.c:115
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
#define STM_RESET
Definition hal_streams.h:51
int32_t msg_t
Type of a message.
Definition osal.h:159
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.
Base stream class.
Definition hal_streams.h:90
Buffered stream adapter object.
Definition bufstreams.h:79
const struct BufferedStreamAdapterVMT * vmt
Virtual Methods Table.
Definition bufstreams.h:81
BufferedStreamAdapter virtual methods table.
Definition bufstreams.h:70