canard_internals.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2016-2017 UAVCAN Team
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. * Contributors: https://github.com/UAVCAN/libcanard/contributors
  25. */
  26. /*
  27. * This file holds function declarations that expose the library's internal definitions for unit testing.
  28. * It is NOT part of the library's API and should not even be looked at by the user.
  29. */
  30. #ifndef CANARD_INTERNALS_H
  31. #define CANARD_INTERNALS_H
  32. #include "canard.h"
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /// This macro is needed only for testing and development. Do not redefine this in production.
  37. #ifndef CANARD_INTERNAL
  38. # define CANARD_INTERNAL static
  39. #endif
  40. /*
  41. * Some MCUs like TMS320 have 16-bits addressing, so
  42. * 1. (uint8_t) same (uint16_t)
  43. * 2. sizeof(float) is 2
  44. * 3. union not same like STM32, because type uint8_t does not exist in hardware
  45. *
  46. * union
  47. * {
  48. * uint64_t u8;
  49. * uint64_t u16;
  50. * uint64_t u32;
  51. * uint64_t u64;
  52. * uint8_t bytes[8];
  53. * } storage;
  54. *
  55. * address:| bytes: | u64: | u32: | u16: | u8:
  56. * 0x00 | bytes[0] | (u64 )&0xFF | (u32 )&0xFF | u16 | u8
  57. * 0x01 | bytes[1] | (u64>>16)&0xFF | (u32>>16)&0xFF |
  58. * 0x02 | bytes[2] | (u64>>32)&0xFF |
  59. * 0x03 | bytes[3] | (u64>>48)&0xFF |
  60. * 0x04 | bytes[4] |
  61. * 0x05 | bytes[5] |
  62. * 0x06 | bytes[6] |
  63. * 0x07 | bytes[7] |
  64. *
  65. */
  66. #ifndef WORD_ADDRESSING_IS_16BITS
  67. #if defined(__TI_COMPILER_VERSION__) || defined(__TMS320C2000__)
  68. #define WORD_ADDRESSING_IS_16BITS 1
  69. #else
  70. #define WORD_ADDRESSING_IS_16BITS 0
  71. #endif
  72. #endif
  73. #if WORD_ADDRESSING_IS_16BITS
  74. # define uint8_t uint16_t
  75. # define int8_t int16_t
  76. # define CANARD_SIZEOF_FLOAT 2
  77. #else
  78. # define CANARD_SIZEOF_FLOAT 4
  79. #endif
  80. CANARD_INTERNAL CanardRxState* traverseRxStates(CanardInstance* ins,
  81. uint32_t transfer_descriptor);
  82. CANARD_INTERNAL CanardRxState* createRxState(CanardPoolAllocator* allocator,
  83. uint32_t transfer_descriptor);
  84. CANARD_INTERNAL CanardRxState* prependRxState(CanardInstance* ins,
  85. uint32_t transfer_descriptor);
  86. CANARD_INTERNAL CanardRxState* findRxState(CanardInstance *ins,
  87. uint32_t transfer_descriptor);
  88. CANARD_INTERNAL int16_t bufferBlockPushBytes(CanardPoolAllocator* allocator,
  89. CanardRxState* state,
  90. const uint8_t* data,
  91. uint8_t data_len);
  92. CANARD_INTERNAL CanardBufferBlock* createBufferBlock(CanardPoolAllocator* allocator);
  93. CANARD_INTERNAL void pushTxQueue(CanardInstance* ins,
  94. CanardTxQueueItem* item);
  95. CANARD_INTERNAL bool isPriorityHigher(uint32_t id,
  96. uint32_t rhs);
  97. CANARD_INTERNAL CanardTxQueueItem* createTxItem(CanardPoolAllocator* allocator);
  98. CANARD_INTERNAL void prepareForNextTransfer(CanardRxState* state);
  99. CANARD_INTERNAL int16_t computeTransferIDForwardDistance(uint8_t a,
  100. uint8_t b);
  101. CANARD_INTERNAL void incrementTransferID(uint8_t* transfer_id);
  102. CANARD_INTERNAL uint64_t releaseStatePayload(CanardInstance* ins,
  103. CanardRxState* rxstate);
  104. CANARD_INTERNAL uint16_t dlcToDataLength(uint16_t dlc);
  105. CANARD_INTERNAL uint16_t dataLengthToDlc(uint16_t data_length);
  106. /// Returns the number of frames enqueued
  107. CANARD_INTERNAL int16_t enqueueTxFrames(CanardInstance* ins,
  108. uint32_t can_id,
  109. uint16_t crc,
  110. CanardTxTransfer* transfer);
  111. CANARD_INTERNAL void copyBitArray(const uint8_t* src,
  112. uint32_t src_offset,
  113. uint32_t src_len,
  114. uint8_t* dst,
  115. uint32_t dst_offset);
  116. /**
  117. * Moves specified bits from the scattered transfer storage to a specified contiguous buffer.
  118. * Returns the number of bits copied, or negated error code.
  119. */
  120. CANARD_INTERNAL int16_t descatterTransferPayload(const CanardRxTransfer* transfer,
  121. uint32_t bit_offset,
  122. uint8_t bit_length,
  123. void* output);
  124. CANARD_INTERNAL bool isBigEndian(void);
  125. CANARD_INTERNAL void swapByteOrder(void* data, unsigned size);
  126. /*
  127. * Transfer CRC
  128. */
  129. CANARD_INTERNAL uint16_t crcAddByte(uint16_t crc_val,
  130. uint8_t byte);
  131. CANARD_INTERNAL uint16_t crcAddSignature(uint16_t crc_val,
  132. uint64_t data_type_signature);
  133. CANARD_INTERNAL uint16_t crcAdd(uint16_t crc_val,
  134. const uint8_t* bytes,
  135. size_t len);
  136. /**
  137. * Inits a memory allocator.
  138. *
  139. * @param [in] allocator The memory allocator to initialize.
  140. * @param [in] buf The buffer used by the memory allocator.
  141. * @param [in] buf_len The number of blocks in buf.
  142. */
  143. CANARD_INTERNAL void initPoolAllocator(CanardPoolAllocator* allocator,
  144. void *buf,
  145. uint16_t buf_len);
  146. /**
  147. * Allocates a block from the given pool allocator.
  148. */
  149. CANARD_INTERNAL void* allocateBlock(CanardPoolAllocator* allocator);
  150. /**
  151. * Frees a memory block previously returned by canardAllocateBlock.
  152. */
  153. CANARD_INTERNAL void freeBlock(CanardPoolAllocator* allocator,
  154. void* p);
  155. CANARD_INTERNAL uint16_t calculateCRC(const CanardTxTransfer* transfer_object);
  156. CANARD_INTERNAL CanardBufferBlock *canardBufferFromIdx(CanardPoolAllocator* allocator, canard_buffer_idx_t idx);
  157. CANARD_INTERNAL canard_buffer_idx_t canardBufferToIdx(CanardPoolAllocator* allocator, const CanardBufferBlock *buf);
  158. CANARD_INTERNAL CanardRxState *canardRxFromIdx(CanardPoolAllocator* allocator, canard_buffer_idx_t idx);
  159. CANARD_INTERNAL canard_buffer_idx_t canardRxToIdx(CanardPoolAllocator* allocator, const CanardRxState *rx);
  160. #ifdef __cplusplus
  161. }
  162. #endif
  163. #endif