sbgECom Library  5.2.590-stable
Interface SBG Systems IMU/AHRS/INS
Loading...
Searching...
No Matches
sbgStreamBufferCommon.h
Go to the documentation of this file.
1
32
33#ifndef SBG_STREAM_BUFFER_COMMON_H
34#define SBG_STREAM_BUFFER_COMMON_H
35
36#include <sbgCommon.h>
37
38//----------------------------------------------------------------------//
39//- General definitions -//
40//----------------------------------------------------------------------//
41
45#if SBG_CONFIG_BIG_ENDIAN == 1
49 #define sbgStreamBufferReadUint16 sbgStreamBufferReadUint16BE
50 #define sbgStreamBufferReadInt16 sbgStreamBufferReadInt16BE
51
52 #define sbgStreamBufferReadUint24 sbgStreamBufferReadUint24BE
53 #define sbgStreamBufferReadInt24 sbgStreamBufferReadInt24BE
54
55 #define sbgStreamBufferReadUint32 sbgStreamBufferReadUint32BE
56 #define sbgStreamBufferReadInt32 sbgStreamBufferReadInt32BE
57
58 #define sbgStreamBufferReadUint40 sbgStreamBufferReadUint40BE
59 #define sbgStreamBufferReadInt40 sbgStreamBufferReadInt40BE
60
61 #define sbgStreamBufferReadUint48 sbgStreamBufferReadUint48BE
62 #define sbgStreamBufferReadInt48 sbgStreamBufferReadInt48BE
63
64 #define sbgStreamBufferReadUint56 sbgStreamBufferReadUint56BE
65 #define sbgStreamBufferReadInt56 sbgStreamBufferReadInt56BE
66
67 #define sbgStreamBufferReadUint64 sbgStreamBufferReadUint64BE
68 #define sbgStreamBufferReadInt64 sbgStreamBufferReadInt64BE
69
70 #define sbgStreamBufferReadSizeT32 sbgStreamBufferReadSizeT32BE
71 #define sbgStreamBufferReadSizeT64 sbgStreamBufferReadSizeT64BE
72
73 #define sbgStreamBufferReadFloat sbgStreamBufferReadFloatBE
74 #define sbgStreamBufferReadDouble sbgStreamBufferReadDoubleBE
75
76 #define sbgStreamBufferWriteUint16 sbgStreamBufferWriteUint16BE
77 #define sbgStreamBufferWriteInt16 sbgStreamBufferWriteInt16BE
78
79 #define sbgStreamBufferWriteUint24 sbgStreamBufferWriteUint24BE
80 #define sbgStreamBufferWriteInt24 sbgStreamBufferWriteInt24BE
81
82 #define sbgStreamBufferWriteUint32 sbgStreamBufferWriteUint32BE
83 #define sbgStreamBufferWriteInt32 sbgStreamBufferWriteInt32BE
84
85 #define sbgStreamBufferWriteUint64 sbgStreamBufferWriteUint64BE
86 #define sbgStreamBufferWriteInt64 sbgStreamBufferWriteInt64BE
87
88 #define sbgStreamBufferWriteSizeT32 sbgStreamBufferWriteSizeT32BE
89 #define sbgStreamBufferWriteSizeT64 sbgStreamBufferWriteSizeT64BE
90
91 #define sbgStreamBufferWriteFloat sbgStreamBufferWriteFloatBE
92 #define sbgStreamBufferWriteDouble sbgStreamBufferWriteDoubleBE
93
94 #define sbgStreamBufferReadString sbgStreamBufferReadStringBE
95 #define sbgStreamBufferWriteString sbgStreamBufferWriteStringBE
96#else
100 #define sbgStreamBufferReadUint16 sbgStreamBufferReadUint16LE
101 #define sbgStreamBufferReadInt16 sbgStreamBufferReadInt16LE
102
103 #define sbgStreamBufferReadUint24 sbgStreamBufferReadUint24LE
104 #define sbgStreamBufferReadInt24 sbgStreamBufferReadInt24LE
105
106 #define sbgStreamBufferReadUint32 sbgStreamBufferReadUint32LE
107 #define sbgStreamBufferReadInt32 sbgStreamBufferReadInt32LE
108
109 #define sbgStreamBufferReadUint40 sbgStreamBufferReadUint40LE
110 #define sbgStreamBufferReadInt40 sbgStreamBufferReadInt40LE
111
112 #define sbgStreamBufferReadUint48 sbgStreamBufferReadUint48LE
113 #define sbgStreamBufferReadInt48 sbgStreamBufferReadInt48LE
114
115 #define sbgStreamBufferReadUint56 sbgStreamBufferReadUint56LE
116 #define sbgStreamBufferReadInt56 sbgStreamBufferReadInt56LE
117
118 #define sbgStreamBufferReadUint64 sbgStreamBufferReadUint64LE
119 #define sbgStreamBufferReadInt64 sbgStreamBufferReadInt64LE
120
121 #define sbgStreamBufferReadSizeT32 sbgStreamBufferReadSizeT32LE
122 #define sbgStreamBufferReadSizeT64 sbgStreamBufferReadSizeT64LE
123
124 #define sbgStreamBufferReadFloat sbgStreamBufferReadFloatLE
125 #define sbgStreamBufferReadDouble sbgStreamBufferReadDoubleLE
126
127 #define sbgStreamBufferWriteUint16 sbgStreamBufferWriteUint16LE
128 #define sbgStreamBufferWriteInt16 sbgStreamBufferWriteInt16LE
129
130 #define sbgStreamBufferWriteUint24 sbgStreamBufferWriteUint24LE
131 #define sbgStreamBufferWriteInt24 sbgStreamBufferWriteInt24LE
132
133 #define sbgStreamBufferWriteUint32 sbgStreamBufferWriteUint32LE
134 #define sbgStreamBufferWriteInt32 sbgStreamBufferWriteInt32LE
135
136 #define sbgStreamBufferWriteUint64 sbgStreamBufferWriteUint64LE
137 #define sbgStreamBufferWriteInt64 sbgStreamBufferWriteInt64LE
138
139 #define sbgStreamBufferWriteSizeT32 sbgStreamBufferWriteSizeT32LE
140 #define sbgStreamBufferWriteSizeT64 sbgStreamBufferWriteSizeT64LE
141
142 #define sbgStreamBufferWriteFloat sbgStreamBufferWriteFloatLE
143 #define sbgStreamBufferWriteDouble sbgStreamBufferWriteDoubleLE
144
145 #define sbgStreamBufferReadString sbgStreamBufferReadStringLE
146 #define sbgStreamBufferWriteString sbgStreamBufferWriteStringLE
147#endif
148
153#define sbgStreamBufferReadUint8LE sbgStreamBufferReadUint8
154#define sbgStreamBufferReadInt8LE sbgStreamBufferReadInt8
155#define sbgStreamBufferReadBooleanLE sbgStreamBufferReadBoolean
156#define sbgStreamBufferReadBufferLE sbgStreamBufferReadBuffer
157
158#define sbgStreamBufferWriteUint8LE sbgStreamBufferWriteUint8
159#define sbgStreamBufferWriteInt8LE sbgStreamBufferWriteInt8
160#define sbgStreamBufferWriteBooleanLE sbgStreamBufferWriteBoolean
161#define sbgStreamBufferWriteBufferLE sbgStreamBufferWriteBuffer
162
163#define sbgStreamBufferReadUint8BE sbgStreamBufferReadUint8
164#define sbgStreamBufferReadInt8BE sbgStreamBufferReadInt8
165#define sbgStreamBufferReadBooleanBE sbgStreamBufferReadBoolean
166#define sbgStreamBufferReadBufferBE sbgStreamBufferReadBuffer
167
168#define sbgStreamBufferWriteUint8BE sbgStreamBufferWriteUint8
169#define sbgStreamBufferWriteInt8BE sbgStreamBufferWriteInt8
170#define sbgStreamBufferWriteBooleanBE sbgStreamBufferWriteBoolean
171#define sbgStreamBufferWriteBufferBE sbgStreamBufferWriteBuffer
172
173//----------------------------------------------------------------------//
174//- Structure definitions -//
175//----------------------------------------------------------------------//
176
185
196
208
209//----------------------------------------------------------------------//
210//- Common operations methods -//
211//----------------------------------------------------------------------//
212
221SBG_INLINE SbgErrorCode sbgStreamBufferInitForWrite(SbgStreamBuffer *pHandle, void *pLinkedBuffer, size_t bufferSize)
222{
223 assert(pHandle);
224 assert(pLinkedBuffer);
225
226 //
227 // Initialize stream parameters
228 //
229 pHandle->modes = SB_MODE_WRITE;
230 pHandle->bufferSize = bufferSize;
231 pHandle->errorCode = SBG_NO_ERROR;
232
233 //
234 // Initialize the buffer
235 //
236 pHandle->pBufferPtr = (uint8_t*)pLinkedBuffer;
237 pHandle->pCurrentPtr = (uint8_t*)pLinkedBuffer;
238
239 //
240 // For now, we don't handle any error, maybe we could add checks in debug mode only
241 //
242 return SBG_NO_ERROR;
243}
244
253SBG_INLINE SbgErrorCode sbgStreamBufferInitForRead(SbgStreamBuffer *pHandle, const void *pLinkedBuffer, size_t bufferSize)
254{
255 assert(pHandle);
256 assert(pLinkedBuffer);
257
258 //
259 // Initialize stream parameters
260 //
261 pHandle->modes = SB_MODE_READ;
262 pHandle->bufferSize = bufferSize;
263 pHandle->errorCode = SBG_NO_ERROR;
264
265 //
266 // Initialize the buffer
267 //
268 pHandle->pBufferPtr = (uint8_t*)pLinkedBuffer;
269 pHandle->pCurrentPtr = (uint8_t*)pLinkedBuffer;
270
271 //
272 // For now, we don't handle any error, maybe we could add checks in debug mode only
273 //
274 return SBG_NO_ERROR;
275}
276
284{
285 assert(pHandle);
286
287 //
288 // Return error code
289 //
290 return pHandle->errorCode;
291}
292
299{
300 assert(pHandle);
301
302 //
303 // Return error code
304 //
305 pHandle->errorCode = SBG_NO_ERROR;
306}
307
319{
320 assert(pHandle);
321
322 //
323 // Return the linked buffer size
324 //
325 return pHandle->bufferSize;
326}
327
339{
340 assert(pHandle);
341
342 //
343 // Return the number of bytes between the begin of the stream and the current pointer
344 //
345 return ((size_t)pHandle->pCurrentPtr - (size_t)pHandle->pBufferPtr);
346}
347
358{
359 assert(pHandle);
360
361 //
362 // Return the space left in bytes
363 //
364 return sbgStreamBufferGetSize(pHandle) - sbgStreamBufferGetLength(pHandle);
365}
366
376{
377 assert(pHandle);
378
379 //
380 // Test if we haven't already an error
381 //
382 if (pHandle->errorCode == SBG_NO_ERROR)
383 {
384 //
385 // According to the origin reference point
386 //
387 switch (origin)
388 {
389 case SB_SEEK_SET:
390 pHandle->pCurrentPtr = pHandle->pBufferPtr + offset;
391 break;
392 case SB_SEEK_CUR_INC:
393 pHandle->pCurrentPtr += offset;
394 break;
395 case SB_SEEK_CUR_DEC:
396 pHandle->pCurrentPtr -= offset;
397 break;
398 case SB_SEEK_END:
399 pHandle->pCurrentPtr = pHandle->pBufferPtr + (pHandle->bufferSize - offset);
400 break;
401 default:
403 SBG_LOG_ERROR(pHandle->errorCode, "Invalid origin parameter");
404 }
405
406 //
407 // Make sure that no error has occurred
408 //
409 if (pHandle->errorCode == SBG_NO_ERROR)
410 {
411 //
412 // Test if the current ptr is still within the buffer bounds
413 //
414 if (pHandle->pCurrentPtr < pHandle->pBufferPtr)
415 {
416 //
417 // We are before the buffer so clamp to the begining of the buffer and raise an error
418 //
419 pHandle->pCurrentPtr = pHandle->pBufferPtr;
421 }
422 else if (pHandle->pCurrentPtr > pHandle->pBufferPtr + pHandle->bufferSize)
423 {
424 //
425 // We are after the buffer so clamp to the end of the buffer and raise an error
426 //
427 pHandle->pCurrentPtr = pHandle->pBufferPtr + pHandle->bufferSize;
429 }
430 }
431 }
432
433 return pHandle->errorCode;
434}
435
443{
444 assert(pHandle);
445
446 return (size_t)pHandle->pCurrentPtr - (size_t)pHandle->pBufferPtr;
447}
448
456{
457 assert(pHandle);
458
459 return pHandle->pBufferPtr;
460}
461
469{
470 assert(pHandle);
471
472 return pHandle->pCurrentPtr;
473}
474
475//----------------------------------------------------------------------//
476//- Read operations methods -//
477//----------------------------------------------------------------------//
478
486{
487 assert(pHandle);
488
489 //
490 // Test if we haven't already an error
491 //
492 if (pHandle->errorCode == SBG_NO_ERROR)
493 {
494 //
495 // Test if we can access this item
496 //
497 if (sbgStreamBufferGetSpace(pHandle) >= sizeof(int8_t))
498 {
499 //
500 // Read the byte
501 //
502 return *((int8_t*)(pHandle->pCurrentPtr++));
503 }
504 else
505 {
506 //
507 // We have a buffer overflow
508 //
510 }
511 }
512
513 //
514 // If we are here, it means we have an error so return 0
515 //
516 return 0;
517}
518
526{
527 assert(pHandle);
528
529 //
530 // Test if we haven't already an error
531 //
532 if (pHandle->errorCode == SBG_NO_ERROR)
533 {
534 //
535 // Test if we can access this item
536 //
537 if (sbgStreamBufferGetSpace(pHandle) >= sizeof(uint8_t))
538 {
539 //
540 // Read the byte
541 //
542 return *((uint8_t*)(pHandle->pCurrentPtr++));
543 }
544 else
545 {
546 //
547 // We have a buffer overflow
548 //
550 }
551 }
552
553 //
554 // If we are here, it means we have an error so return 0
555 //
556 return 0;
557}
558
566{
567 assert(pHandle);
568
569 //
570 // Test if we haven't already an error
571 //
572 if (pHandle->errorCode == SBG_NO_ERROR)
573 {
574 //
575 // Test if we can access this item
576 //
577 if (sbgStreamBufferGetSpace(pHandle) >= sizeof(uint8_t))
578 {
579 //
580 // Read the byte and check if the value is different than zero or not
581 //
582 if (*((uint8_t*)(pHandle->pCurrentPtr++)))
583 {
584 return true;
585 }
586 else
587 {
588 return false;
589 }
590 }
591 else
592 {
593 //
594 // We have a buffer overflow
595 //
597 }
598 }
599
600 //
601 // If we are here, it means we have an error so return false
602 //
603 return false;
604}
605
614SBG_INLINE SbgErrorCode sbgStreamBufferReadBuffer(SbgStreamBuffer *pHandle, void *pBuffer, size_t numBytesToRead)
615{
616 assert(pHandle);
617 assert((pBuffer) || (numBytesToRead == 0));
618
619 //
620 // Test if we haven't already an error
621 //
622 if (pHandle->errorCode == SBG_NO_ERROR)
623 {
624 //
625 // Test if enough bytes in stream
626 //
627 if (sbgStreamBufferGetSpace(pHandle) >= numBytesToRead)
628 {
629 //
630 // Copy from the stream buffer to the output buffer
631 //
632 memcpy(pBuffer, pHandle->pCurrentPtr, numBytesToRead);
633
634 //
635 // Update the current pointer
636 //
637 pHandle->pCurrentPtr += numBytesToRead;
638 }
639 else
640 {
641 //
642 // Not enough data in stream
643 //
645 }
646 }
647
648 return pHandle->errorCode;
649}
650
651//----------------------------------------------------------------------//
652//- Write operations methods -//
653//----------------------------------------------------------------------//
654
663{
664 assert(pHandle);
665
666 //
667 // Test if we haven't already an error
668 //
669 if (pHandle->errorCode == SBG_NO_ERROR)
670 {
671 //
672 // Test if we can access this item
673 //
674 if (sbgStreamBufferGetSpace(pHandle) >= sizeof(int8_t))
675 {
676 //
677 // Write each byte
678 //
679 *(pHandle->pCurrentPtr++) = (int8_t)(value);
680 }
681 else
682 {
683 //
684 // We are accessing a data that is outside the stream buffer
685 //
687 }
688 }
689
690 return pHandle->errorCode;
691}
692
701{
702 assert(pHandle);
703
704 //
705 // Test if we haven't already an error
706 //
707 if (pHandle->errorCode == SBG_NO_ERROR)
708 {
709 //
710 // Test if we can access this item
711 //
712 if (sbgStreamBufferGetSpace(pHandle) >= sizeof(uint8_t))
713 {
714 //
715 // Write each byte
716 //
717 *(pHandle->pCurrentPtr++) = (uint8_t)(value);
718 }
719 else
720 {
721 //
722 // We are accessing a data that is outside the stream buffer
723 //
725 }
726 }
727
728 return pHandle->errorCode;
729}
730
739{
740 assert(pHandle);
741
742 //
743 // Test if we haven't already an error
744 //
745 if (pHandle->errorCode == SBG_NO_ERROR)
746 {
747 //
748 // Test if we can access this item
749 //
750 if (sbgStreamBufferGetSpace(pHandle) >= sizeof(uint8_t))
751 {
752 //
753 // Write the boolean as an uint8_t value (1 byte)
754 //
755 if (value)
756 {
757 *(pHandle->pCurrentPtr++) = 1;
758 }
759 else
760 {
761 *(pHandle->pCurrentPtr++) = 0;
762 }
763 }
764 else
765 {
766 //
767 // We are accessing a data that is outside the stream buffer
768 //
770 }
771 }
772
773 return pHandle->errorCode;
774}
775
784SBG_INLINE SbgErrorCode sbgStreamBufferWriteBuffer(SbgStreamBuffer *pHandle, const void *pBuffer, size_t numBytesToWrite)
785{
786 assert(pHandle);
787 assert((pBuffer) || (numBytesToWrite == 0));
788
789 //
790 // Test if we haven't already an error
791 //
792 if (pHandle->errorCode == SBG_NO_ERROR)
793 {
794 //
795 // Test if we can access this item
796 //
797 if (sbgStreamBufferGetSpace(pHandle) >= numBytesToWrite)
798 {
799 //
800 // Copy from the stream buffer to the output buffer
801 //
802 memcpy(pHandle->pCurrentPtr, pBuffer, numBytesToWrite);
803
804 //
805 // Update the current pointer
806 //
807 pHandle->pCurrentPtr += numBytesToWrite;
808 }
809 else
810 {
811 //
812 // We are accessing a data that is outside the stream buffer
813 //
815 }
816 }
817
818 return pHandle->errorCode;
819}
820
821#endif // SBG_STREAM_BUFFER_COMMON_H
Main header for the SBG Systems common C library.
#define SBG_INLINE
Definition sbgDefines.h:227
enum _SbgErrorCode SbgErrorCode
@ SBG_BUFFER_OVERFLOW
Definition sbgErrorCodes.h:63
@ SBG_NO_ERROR
Definition sbgErrorCodes.h:52
@ SBG_INVALID_PARAMETER
Definition sbgErrorCodes.h:64
_SbgSBSeekOrigin
Definition sbgStreamBufferCommon.h:190
@ SB_SEEK_END
Definition sbgStreamBufferCommon.h:194
@ SB_SEEK_SET
Definition sbgStreamBufferCommon.h:191
@ SB_SEEK_CUR_DEC
Definition sbgStreamBufferCommon.h:193
@ SB_SEEK_CUR_INC
Definition sbgStreamBufferCommon.h:192
SBG_INLINE int8_t sbgStreamBufferReadInt8(SbgStreamBuffer *pHandle)
Definition sbgStreamBufferCommon.h:485
enum _SbgSBSeekOrigin SbgSBSeekOrigin
SBG_INLINE SbgErrorCode sbgStreamBufferWriteInt8(SbgStreamBuffer *pHandle, int8_t value)
Definition sbgStreamBufferCommon.h:662
SBG_INLINE void sbgStreamBufferClearLastError(SbgStreamBuffer *pHandle)
Definition sbgStreamBufferCommon.h:298
SBG_INLINE void * sbgStreamBufferGetLinkedBuffer(const SbgStreamBuffer *pHandle)
Definition sbgStreamBufferCommon.h:455
SBG_INLINE SbgErrorCode sbgStreamBufferWriteUint8(SbgStreamBuffer *pHandle, uint8_t value)
Definition sbgStreamBufferCommon.h:700
SBG_INLINE SbgErrorCode sbgStreamBufferSeek(SbgStreamBuffer *pHandle, size_t offset, SbgSBSeekOrigin origin)
Definition sbgStreamBufferCommon.h:375
enum _SbgSBMode SbgSBMode
SBG_INLINE void * sbgStreamBufferGetCursor(const SbgStreamBuffer *pHandle)
Definition sbgStreamBufferCommon.h:468
SBG_INLINE SbgErrorCode sbgStreamBufferInitForWrite(SbgStreamBuffer *pHandle, void *pLinkedBuffer, size_t bufferSize)
Definition sbgStreamBufferCommon.h:221
SBG_INLINE SbgErrorCode sbgStreamBufferReadBuffer(SbgStreamBuffer *pHandle, void *pBuffer, size_t numBytesToRead)
Definition sbgStreamBufferCommon.h:614
SBG_INLINE SbgErrorCode sbgStreamBufferWriteBuffer(SbgStreamBuffer *pHandle, const void *pBuffer, size_t numBytesToWrite)
Definition sbgStreamBufferCommon.h:784
SBG_INLINE SbgErrorCode sbgStreamBufferWriteBoolean(SbgStreamBuffer *pHandle, bool value)
Definition sbgStreamBufferCommon.h:738
SBG_INLINE bool sbgStreamBufferReadBoolean(SbgStreamBuffer *pHandle)
Definition sbgStreamBufferCommon.h:565
SBG_INLINE SbgErrorCode sbgStreamBufferInitForRead(SbgStreamBuffer *pHandle, const void *pLinkedBuffer, size_t bufferSize)
Definition sbgStreamBufferCommon.h:253
SBG_INLINE size_t sbgStreamBufferGetSpace(const SbgStreamBuffer *pHandle)
Definition sbgStreamBufferCommon.h:357
SBG_INLINE SbgErrorCode sbgStreamBufferGetLastError(const SbgStreamBuffer *pHandle)
Definition sbgStreamBufferCommon.h:283
SBG_INLINE size_t sbgStreamBufferTell(const SbgStreamBuffer *pHandle)
Definition sbgStreamBufferCommon.h:442
SBG_INLINE size_t sbgStreamBufferGetLength(const SbgStreamBuffer *pHandle)
Definition sbgStreamBufferCommon.h:338
SBG_INLINE size_t sbgStreamBufferGetSize(const SbgStreamBuffer *pHandle)
Definition sbgStreamBufferCommon.h:318
struct _SbgStreamBuffer SbgStreamBuffer
_SbgSBMode
Definition sbgStreamBufferCommon.h:181
@ SB_MODE_WRITE
Definition sbgStreamBufferCommon.h:183
@ SB_MODE_READ
Definition sbgStreamBufferCommon.h:182
SBG_INLINE uint8_t sbgStreamBufferReadUint8(SbgStreamBuffer *pHandle)
Definition sbgStreamBufferCommon.h:525
Definition sbgStreamBufferCommon.h:201
SbgErrorCode errorCode
Definition sbgStreamBufferCommon.h:206
uint8_t * pCurrentPtr
Definition sbgStreamBufferCommon.h:205
uint8_t * pBufferPtr
Definition sbgStreamBufferCommon.h:204
size_t bufferSize
Definition sbgStreamBufferCommon.h:203
SbgSBMode modes
Definition sbgStreamBufferCommon.h:202