StateOS
Loading...
Searching...
No Matches
osrawbuffer.h
1/******************************************************************************
2
3 @file StateOS: osrawbuffer.h
4 @author Rajmund Szymanski
5 @date 17.11.2025
6 @brief This file contains definitions for StateOS.
7
8 ******************************************************************************
9
10 Copyright (c) 2018-2022 Rajmund Szymanski. All rights reserved.
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to
14 deal in the Software without restriction, including without limitation the
15 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 sell copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included
20 in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 IN THE SOFTWARE.
29
30 ******************************************************************************/
31
32#ifndef __STATEOS_RAW_H
33#define __STATEOS_RAW_H
34
35#include "oskernel.h"
36#include "osclock.h"
37
43
47
48typedef struct __raw raw_t;
49
50struct __raw
51{
52 obj_t obj;
53
54 size_t count;
55 size_t limit;
56
57 size_t head;
58 size_t tail;
59 char * data;
60};
61
62typedef struct __raw raw_id [];
63
64/******************************************************************************
65 *
66 * Name : _RAW_INIT
67 *
68 * Description : create and initialize a raw buffer object
69 *
70 * Parameters
71 * limit : size of a buffer (max number of stored bytes)
72 * data : raw buffer data
73 *
74 * Return : raw buffer object
75 *
76 * Note : for internal use
77 *
78 ******************************************************************************/
79
80#define _RAW_INIT( _limit, _data ) { _OBJ_INIT(), 0, _limit, 0, 0, _data }
81
82/******************************************************************************
83 *
84 * Name : _RAW_DATA
85 *
86 * Description : create a raw buffer data
87 *
88 * Parameters
89 * limit : size of a buffer (max number of stored bytes)
90 *
91 * Return : raw buffer data
92 *
93 * Note : for internal use
94 *
95 ******************************************************************************/
96
97#ifndef __cplusplus
98#define _RAW_DATA( _limit ) (char[_limit]){ 0 }
99#endif
100
101/******************************************************************************
102 *
103 * Name : _VA_RAW
104 *
105 * Description : calculate buffer size from optional parameter
106 *
107 * Note : for internal use
108 *
109 ******************************************************************************/
110
111#define _VA_RAW( _limit, _size ) ( (_size + 0) ? ((_limit) * (_size + 0)) : (_limit) )
112
121#define OS_RAW_BUFFER( buf, limit, ... ) \
122 char buf[_VA_RAW(limit, __VA_ARGS__)]
123
132#define OS_RAW( raw, limit, ... ) \
133 static char raw##__buf[_VA_RAW(limit, __VA_ARGS__)]; \
134 raw_t raw[] = { _RAW_INIT( _VA_RAW(limit, __VA_ARGS__), raw##__buf ) }
135
136/** static alias of OS_RAW() */
137#define static_RAW( raw, limit, ... ) \
138 static char raw##__buf[_VA_RAW(limit, __VA_ARGS__)]; \
139 static raw_t raw[] = { _RAW_INIT( _VA_RAW(limit, __VA_ARGS__), raw##__buf ) }
140
151
152#ifndef __cplusplus
153#define RAW_INIT( limit, ... ) \
154 _RAW_INIT( _VA_RAW(limit, __VA_ARGS__), _RAW_DATA( _VA_RAW(limit, __VA_ARGS__) ) )
155#endif
156
167
168#ifndef __cplusplus
169#define RAW_CREATE( limit, ... ) \
170 { RAW_INIT ( _VA_RAW(limit, __VA_ARGS__) ) }
171/** alias of RAW_CREATE() */
172#define RAW_NEW \
173 RAW_CREATE
174#endif
175
176#ifdef __cplusplus
177extern "C" {
178#endif
179
190void raw_init( raw_t *raw, void *data, size_t bufsize );
191
203raw_t *raw_create( size_t limit );
204
206__STATIC_INLINE
207raw_t *raw_new( size_t limit ) { return raw_create(limit); }
208
217void raw_reset( raw_t *raw );
218
220__STATIC_INLINE
221void raw_kill( raw_t *raw ) { raw_reset(raw); }
222
231void raw_destroy( raw_t *raw );
232
234__STATIC_INLINE
235void raw_delete( raw_t *raw ) { raw_destroy(raw); }
236
253int raw_take( raw_t *raw, void *data, size_t size, size_t *read );
254
256__STATIC_INLINE
257int raw_tryWait( raw_t *raw, void *data, size_t size, size_t *read ) { return raw_take(raw, data, size, read); }
258
260__STATIC_INLINE
261int raw_takeISR( raw_t *raw, void *data, size_t size, size_t *read ) { return raw_take(raw, data, size, read); }
262
283int raw_waitFor( raw_t *raw, void *data, size_t size, size_t *read, cnt_t delay );
284
303int raw_waitUntil( raw_t *raw, void *data, size_t size, size_t *read, cnt_t time );
304
320
321__STATIC_INLINE
322int raw_wait( raw_t *raw, void *data, size_t size, size_t *read ) { return raw_waitFor(raw, data, size, read, INFINITE); }
323
340int raw_give( raw_t *raw, const void *data, size_t size );
341
343__STATIC_INLINE
344int raw_giveISR( raw_t *raw, const void *data, size_t size ) { return raw_give(raw, data, size); }
345
366int raw_sendFor( raw_t *raw, const void *data, size_t size, cnt_t delay );
367
386int raw_sendUntil( raw_t *raw, const void *data, size_t size, cnt_t time );
387
403
404__STATIC_INLINE
405int raw_send( raw_t *raw, const void *data, size_t size ) { return raw_sendFor(raw, data, size, INFINITE); }
406
422int raw_push( raw_t *raw, const void *data, size_t size );
423
425__STATIC_INLINE
426int raw_pushISR( raw_t *raw, const void *data, size_t size ) { return raw_push(raw, data, size); }
427
439size_t raw_count( raw_t *raw );
440
442__STATIC_INLINE
443size_t raw_countISR( raw_t *raw ) { return raw_count(raw); }
444
456size_t raw_space( raw_t *raw );
457
459__STATIC_INLINE
460size_t raw_spaceISR( raw_t *raw ) { return raw_space(raw); }
461
473size_t raw_limit( raw_t *raw );
474
476__STATIC_INLINE
477size_t raw_limitISR( raw_t *raw ) { return raw_limit(raw); }
478
479#ifdef __cplusplus
480}
481#endif
482
483/* -------------------------------------------------------------------------- */
484
485#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
486namespace stateos {
487
494
495template<size_t limit_>
496struct RawBufferT : public __raw
497{
499 constexpr
500 RawBufferT(): __raw _RAW_INIT(limit_, data_) {}
501
502 ~RawBufferT() { assert(__raw::obj.queue == nullptr); }
503
504 /// move constructor
505 RawBufferT( RawBufferT&& _src ): __raw _RAW_INIT(limit_, data_)
506 {
507 assert(_src.__raw::count == 0 && _src.obj.queue == nullptr);
508 (void) _src;
509 }
510
511 RawBufferT( const RawBufferT& ) = delete;
512 RawBufferT& operator=( RawBufferT&& ) = delete;
513 RawBufferT& operator=( const RawBufferT& ) = delete;
515 void reset () { raw_reset (this); }
516 void kill () { raw_kill (this); }
517 void destroy () { raw_destroy (this); }
518 int take ( void *_data, size_t _size, size_t *_read = nullptr ) { return raw_take (this, _data, _size, _read); }
519 int tryWait ( void *_data, size_t _size, size_t *_read = nullptr ) { return raw_tryWait (this, _data, _size, _read); }
520 int takeISR ( void *_data, size_t _size, size_t *_read = nullptr ) { return raw_takeISR (this, _data, _size, _read); }
521 template<typename T>
522 int waitFor ( void *_data, size_t _size, size_t *_read, const T& _delay ) { return raw_waitFor (this, _data, _size, _read, Clock::count(_delay)); }
523 template<typename T>
524 int waitUntil( void *_data, size_t _size, size_t *_read, const T& _time ) { return raw_waitUntil(this, _data, _size, _read, Clock::until(_time)); }
525 int wait ( void *_data, size_t _size, size_t *_read = nullptr ) { return raw_wait (this, _data, _size, _read); }
526 int give ( const void *_data, size_t _size ) { return raw_give (this, _data, _size); }
527 int giveISR ( const void *_data, size_t _size ) { return raw_giveISR (this, _data, _size); }
528 template<typename T>
529 int sendFor ( const void *_data, size_t _size, const T& _delay ) { return raw_sendFor (this, _data, _size, Clock::count(_delay)); }
530 template<typename T>
531 int sendUntil( const void *_data, size_t _size, const T& _time ) { return raw_sendUntil(this, _data, _size, Clock::until(_time)); }
532 int send ( const void *_data, size_t _size ) { return raw_send (this, _data, _size); }
533 int push ( const void *_data, size_t _size ) { return raw_push (this, _data, _size); }
534 int pushISR ( const void *_data, size_t _size ) { return raw_pushISR (this, _data, _size); }
535 size_t count () { return raw_count (this); }
536 size_t countISR () { return raw_countISR (this); }
537 size_t space () { return raw_space (this); }
538 size_t spaceISR () { return raw_spaceISR (this); }
539 size_t limit () { return raw_limit (this); }
540 size_t limitISR () { return raw_limitISR (this); }
541
542#if __cplusplus >= 201402L
543 using Ptr = std::unique_ptr<RawBufferT<limit_>>;
544#else
545 using Ptr = RawBufferT<limit_> *;
546#endif
547
558
559 static
560 Ptr Create()
561 {
562 auto raw = new (std::nothrow) RawBufferT<limit_>();
563 if (raw != nullptr)
564 raw->__raw::obj.res = raw;
565 return Ptr(raw);
566 }
567
568 private:
569 char data_[limit_];
570};
571
579
580template<unsigned limit_, class C>
581struct RawBufferTT : public RawBufferT<limit_*sizeof(C)>
582{
584 constexpr
585 RawBufferTT(): RawBufferT<limit_*sizeof(C)>() {}
587 int take ( C *_data ) { return raw_take (this, _data, sizeof(C), nullptr); }
588 int tryWait ( C *_data ) { return raw_tryWait (this, _data, sizeof(C), nullptr); }
589 int takeISR ( C *_data ) { return raw_takeISR (this, _data, sizeof(C), nullptr); }
590 template<typename T>
591 int waitFor ( C *_data, const T& _delay ) { return raw_waitFor (this, _data, sizeof(C), nullptr, Clock::count(_delay)); }
592 template<typename T>
593 int waitUntil( C *_data, const T& _time ) { return raw_waitUntil(this, _data, sizeof(C), nullptr, Clock::until(_time)); }
594 int wait ( C *_data ) { return raw_wait (this, _data, sizeof(C), nullptr); }
595 int give ( const C *_data ) { return raw_give (this, _data, sizeof(C)); }
596 int giveISR ( const C *_data ) { return raw_giveISR (this, _data, sizeof(C)); }
597 template<typename T>
598 int sendFor ( const C *_data, const T& _delay ) { return raw_sendFor (this, _data, sizeof(C), Clock::count(_delay)); }
599 template<typename T>
600 int sendUntil( const C *_data, const T& _time ) { return raw_sendUntil(this, _data, sizeof(C), Clock::until(_time)); }
601 int send ( const C *_data ) { return raw_send (this, _data, sizeof(C)); }
602 int push ( const C *_data ) { return raw_push (this, _data, sizeof(C)); }
603 int pushISR ( const C *_data ) { return raw_pushISR (this, _data, sizeof(C)); }
604
605#if __cplusplus >= 201402L
606 using Ptr = std::unique_ptr<RawBufferTT<limit_, C>>;
607#else
608 using Ptr = RawBufferTT<limit_, C> *;
609#endif
610
622
623 static
624 Ptr Create()
625 {
626 auto raw = new (std::nothrow) RawBufferTT<limit_, C>();
627 if (raw != nullptr)
628 raw->__raw::obj.res = raw;
629 return Ptr(raw);
630 }
631
632};
633
634} // namespace
635#endif//__cplusplus
636
637/* -------------------------------------------------------------------------- */
638
640
641#endif//__STATEOS_RAW_H
#define INFINITE
infinite waiting
Definition osbase.h:261
char data[]
message data
Definition osmessagequeue.h:54
size_t raw_countISR(raw_t *raw)
ISR alias of raw_count().
Definition osrawbuffer.h:442
int raw_take(raw_t *raw, void *data, size_t size, size_t *read)
try to transfer data from the raw buffer object, don't wait if the raw buffer object is empty
int raw_sendFor(raw_t *raw, const void *data, size_t size, cnt_t delay)
try to transfer data to the raw buffer object, wait for given duration of time while the raw buffer o...
int raw_wait(raw_t *raw, void *data, size_t size, size_t *read)
try to transfer data from the raw buffer object, wait indefinitely while the raw buffer object is emp...
Definition osrawbuffer.h:321
size_t raw_limit(raw_t *raw)
return the size of the raw buffer
raw_t * raw_create(size_t limit)
create and initialize a new raw buffer object
void raw_reset(raw_t *raw)
reset the raw buffer object and wake up all waiting tasks with 'E_STOPPED' event value
int raw_tryWait(raw_t *raw, void *data, size_t size, size_t *read)
alias of raw_take()
Definition osrawbuffer.h:256
int raw_takeISR(raw_t *raw, void *data, size_t size, size_t *read)
ISR alias of raw_take().
Definition osrawbuffer.h:260
int raw_giveISR(raw_t *raw, const void *data, size_t size)
ISR alias of raw_give().
Definition osrawbuffer.h:343
int raw_waitUntil(raw_t *raw, void *data, size_t size, size_t *read, cnt_t time)
try to transfer data from the raw buffer object, wait until given timepoint while the raw buffer obje...
size_t raw_spaceISR(raw_t *raw)
ISR alias of raw_space().
Definition osrawbuffer.h:459
int raw_give(raw_t *raw, const void *data, size_t size)
try to transfer data to the raw buffer object, don't wait if the raw buffer object is full
size_t raw_space(raw_t *raw)
return the amount of free space in the raw buffer
void raw_delete(raw_t *raw)
alias of raw_destroy()
Definition osrawbuffer.h:234
int raw_push(raw_t *raw, const void *data, size_t size)
try to transfer data to the raw buffer object, remove the oldest data if the raw buffer object is ful...
void raw_destroy(raw_t *raw)
reset the raw buffer object, wake up all waiting tasks with 'E_DELETED' event value and free allocate...
int raw_sendUntil(raw_t *raw, const void *data, size_t size, cnt_t time)
try to transfer data to the raw buffer object, wait until given timepoint while the raw buffer object...
size_t raw_count(raw_t *raw)
return the amount of data contained in the raw buffer
size_t raw_limitISR(raw_t *raw)
ISR alias of raw_limit().
Definition osrawbuffer.h:476
int raw_waitFor(raw_t *raw, void *data, size_t size, size_t *read, cnt_t delay)
try to transfer data from the raw buffer object, wait for given duration of time while the raw buffer...
int raw_pushISR(raw_t *raw, const void *data, size_t size)
ISR alias of raw_push().
Definition osrawbuffer.h:425
void raw_init(raw_t *raw, void *data, size_t bufsize)
initialize a raw buffer object
void raw_kill(raw_t *raw)
alias of raw_reset()
Definition osrawbuffer.h:220
int raw_send(raw_t *raw, const void *data, size_t size)
try to transfer data to the raw buffer object, wait indefinitely while the raw buffer object is full
Definition osrawbuffer.h:404
raw_t * raw_new(size_t limit)
alias of raw_create()
Definition osrawbuffer.h:206
static constexpr rep count(const std::chrono::duration< _Rep, _Period > &_delay)
delay in ticks, rounded up to whole ticks; negative delay: IMMEDIATE, delay out of range: INFINITE
Definition osclock.h:109
static constexpr rep until(const std::chrono::time_point< Clock, _Duration > &_time)
time point in ticks, rounded up to whole ticks (only a time point of this clock)
Definition osclock.h:116
int waitUntil(C *_data, const T &_time)
raw_waitUntil()
Definition osrawbuffer.h:592
static Ptr Create()
create dynamic object with manageable resources
Definition osrawbuffer.h:623
int wait(C *_data)
raw_wait()
Definition osrawbuffer.h:593
constexpr RawBufferTT()
constructor, see the constructor parameters of the class
Definition osrawbuffer.h:584
int send(const C *_data)
raw_send()
Definition osrawbuffer.h:600
int push(const C *_data)
raw_push()
Definition osrawbuffer.h:601
int takeISR(C *_data)
raw_takeISR()
Definition osrawbuffer.h:588
int sendFor(const C *_data, const T &_delay)
raw_sendFor()
Definition osrawbuffer.h:597
int waitFor(C *_data, const T &_delay)
raw_waitFor()
Definition osrawbuffer.h:590
int pushISR(const C *_data)
raw_pushISR()
Definition osrawbuffer.h:602
std::unique_ptr< RawBufferTT< limit_, C > > Ptr
result type of Create()
Definition osrawbuffer.h:605
int sendUntil(const C *_data, const T &_time)
raw_sendUntil()
Definition osrawbuffer.h:599
int tryWait(C *_data)
raw_tryWait()
Definition osrawbuffer.h:587
int take(C *_data)
raw_take()
Definition osrawbuffer.h:586
int giveISR(const C *_data)
raw_giveISR()
Definition osrawbuffer.h:595
int give(const C *_data)
raw_give()
Definition osrawbuffer.h:594
create and initialize a raw buffer object
Definition osrawbuffer.h:496
int waitFor(void *_data, size_t _size, size_t *_read, const T &_delay)
raw_waitFor()
Definition osrawbuffer.h:521
size_t limit()
raw_limit()
Definition osrawbuffer.h:538
size_t spaceISR()
raw_spaceISR()
Definition osrawbuffer.h:537
int takeISR(void *_data, size_t _size, size_t *_read=nullptr)
raw_takeISR()
Definition osrawbuffer.h:519
int giveISR(const void *_data, size_t _size)
raw_giveISR()
Definition osrawbuffer.h:526
int send(const void *_data, size_t _size)
raw_send()
Definition osrawbuffer.h:531
void kill()
raw_kill()
Definition osrawbuffer.h:515
void reset()
raw_reset()
Definition osrawbuffer.h:514
int tryWait(void *_data, size_t _size, size_t *_read=nullptr)
raw_tryWait()
Definition osrawbuffer.h:518
size_t space()
raw_space()
Definition osrawbuffer.h:536
size_t limitISR()
raw_limitISR()
Definition osrawbuffer.h:539
int sendFor(const void *_data, size_t _size, const T &_delay)
raw_sendFor()
Definition osrawbuffer.h:528
int give(const void *_data, size_t _size)
raw_give()
Definition osrawbuffer.h:525
int sendUntil(const void *_data, size_t _size, const T &_time)
raw_sendUntil()
Definition osrawbuffer.h:530
int waitUntil(void *_data, size_t _size, size_t *_read, const T &_time)
raw_waitUntil()
Definition osrawbuffer.h:523
static Ptr Create()
create dynamic object with manageable resources
Definition osrawbuffer.h:559
size_t countISR()
raw_countISR()
Definition osrawbuffer.h:535
void destroy()
raw_destroy()
Definition osrawbuffer.h:516
int pushISR(const void *_data, size_t _size)
raw_pushISR()
Definition osrawbuffer.h:533
int take(void *_data, size_t _size, size_t *_read=nullptr)
raw_take()
Definition osrawbuffer.h:517
int push(const void *_data, size_t _size)
raw_push()
Definition osrawbuffer.h:532
constexpr RawBufferT()
constructor, see the constructor parameters of the class
Definition osrawbuffer.h:499
std::unique_ptr< RawBufferT< limit_ > > Ptr
result type of Create()
Definition osrawbuffer.h:542
size_t count()
raw_count()
Definition osrawbuffer.h:534
int wait(void *_data, size_t _size, size_t *_read=nullptr)
raw_wait()
Definition osrawbuffer.h:524