memstreams.c 3.6 KB

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