canard_internals.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. CANARD_INTERNAL CanardRxState* traverseRxStates(CanardInstance* ins,
  41. uint32_t transfer_descriptor);
  42. CANARD_INTERNAL CanardRxState* createRxState(CanardPoolAllocator* allocator,
  43. uint32_t transfer_descriptor);
  44. CANARD_INTERNAL CanardRxState* prependRxState(CanardInstance* ins,
  45. uint32_t transfer_descriptor);
  46. CANARD_INTERNAL CanardRxState* findRxState(CanardRxState* state,
  47. uint32_t transfer_descriptor);
  48. CANARD_INTERNAL int16_t bufferBlockPushBytes(CanardPoolAllocator* allocator,
  49. CanardRxState* state,
  50. const uint8_t* data,
  51. uint8_t data_len);
  52. CANARD_INTERNAL CanardBufferBlock* createBufferBlock(CanardPoolAllocator* allocator);
  53. CANARD_INTERNAL CanardTransferType extractTransferType(uint32_t id);
  54. CANARD_INTERNAL uint16_t extractDataType(uint32_t id);
  55. CANARD_INTERNAL void pushTxQueue(CanardInstance* ins,
  56. CanardTxQueueItem* item);
  57. CANARD_INTERNAL bool isPriorityHigher(uint32_t id,
  58. uint32_t rhs);
  59. CANARD_INTERNAL CanardTxQueueItem* createTxItem(CanardPoolAllocator* allocator);
  60. CANARD_INTERNAL void prepareForNextTransfer(CanardRxState* state);
  61. CANARD_INTERNAL int16_t computeTransferIDForwardDistance(uint8_t a,
  62. uint8_t b);
  63. CANARD_INTERNAL void incrementTransferID(uint8_t* transfer_id);
  64. CANARD_INTERNAL uint64_t releaseStatePayload(CanardInstance* ins,
  65. CanardRxState* rxstate);
  66. /// Returns the number of frames enqueued
  67. CANARD_INTERNAL int16_t enqueueTxFrames(CanardInstance* ins,
  68. uint32_t can_id,
  69. uint8_t* transfer_id,
  70. uint16_t crc,
  71. const uint8_t* payload,
  72. uint16_t payload_len);
  73. CANARD_INTERNAL void copyBitArray(const uint8_t* src,
  74. uint32_t src_offset,
  75. uint32_t src_len,
  76. uint8_t* dst,
  77. uint32_t dst_offset);
  78. /**
  79. * Moves specified bits from the scattered transfer storage to a specified contiguous buffer.
  80. * Returns the number of bits copied, or negated error code.
  81. */
  82. CANARD_INTERNAL int16_t descatterTransferPayload(const CanardRxTransfer* transfer,
  83. uint32_t bit_offset,
  84. uint8_t bit_length,
  85. void* output);
  86. CANARD_INTERNAL bool isBigEndian(void);
  87. CANARD_INTERNAL void swapByteOrder(void* data, unsigned size);
  88. /*
  89. * Transfer CRC
  90. */
  91. CANARD_INTERNAL uint16_t crcAddByte(uint16_t crc_val,
  92. uint8_t byte);
  93. CANARD_INTERNAL uint16_t crcAddSignature(uint16_t crc_val,
  94. uint64_t data_type_signature);
  95. CANARD_INTERNAL uint16_t crcAdd(uint16_t crc_val,
  96. const uint8_t* bytes,
  97. size_t len);
  98. /**
  99. * Inits a memory allocator.
  100. *
  101. * @param [in] allocator The memory allocator to initialize.
  102. * @param [in] buf The buffer used by the memory allocator.
  103. * @param [in] buf_len The number of blocks in buf.
  104. */
  105. CANARD_INTERNAL void initPoolAllocator(CanardPoolAllocator* allocator,
  106. CanardPoolAllocatorBlock* buf,
  107. uint16_t buf_len);
  108. /**
  109. * Allocates a block from the given pool allocator.
  110. */
  111. CANARD_INTERNAL void* allocateBlock(CanardPoolAllocator* allocator);
  112. /**
  113. * Frees a memory block previously returned by canardAllocateBlock.
  114. */
  115. CANARD_INTERNAL void freeBlock(CanardPoolAllocator* allocator,
  116. void* p);
  117. #ifdef __cplusplus
  118. }
  119. #endif
  120. #endif