|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Siddharth B Purohit, CubePilot Pty Ltd |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +#include <gtest/gtest.h> |
| 24 | +#include <canard.h> |
| 25 | +#include <canard_internals.h> |
| 26 | +#include <math.h> |
| 27 | + |
| 28 | +#if CANARD_MAX_MTU > 8 |
| 29 | + |
| 30 | +static bool g_should_accept = true; |
| 31 | +/** |
| 32 | + * This callback is invoked by the library when a new message or request or response is received. |
| 33 | + */ |
| 34 | +static void onTransferReceived(CanardInstance* ins, |
| 35 | + CanardRxTransfer* transfer) |
| 36 | +{ |
| 37 | + (void)ins; |
| 38 | + (void)transfer; |
| 39 | +} |
| 40 | + |
| 41 | +static bool shouldAcceptTransfer(const CanardInstance* ins, |
| 42 | + uint64_t* out_data_type_signature, |
| 43 | + uint16_t data_type_id, |
| 44 | + CanardTransferType transfer_type, |
| 45 | + uint8_t source_node_id) |
| 46 | +{ |
| 47 | + (void)ins; |
| 48 | + (void)out_data_type_signature; |
| 49 | + (void)data_type_id; |
| 50 | + (void)transfer_type; |
| 51 | + (void)source_node_id; |
| 52 | + return g_should_accept; |
| 53 | +} |
| 54 | + |
| 55 | +TEST(CanardBufferedCANFrame, TestPushBytes) |
| 56 | +{ |
| 57 | + ASSERT_LE(sizeof(CanardTxQueueItem), CANARD_MEM_BLOCK_SIZE); |
| 58 | + ASSERT_EQ((sizeof(CanardCANFrame)-CANARD_MAX_MTU), sizeof(CanardTxQueueCANFrame)); |
| 59 | + |
| 60 | + uint8_t canard_memory_pool[1024]; |
| 61 | + CanardInstance canard; |
| 62 | + CanardTxQueueItem *queue_item = NULL; |
| 63 | + CanardCANFrame frame; |
| 64 | + double total_allocated_bytes = 0.0; |
| 65 | + |
| 66 | + canardInit(&canard, canard_memory_pool, sizeof(canard_memory_pool), |
| 67 | + onTransferReceived, shouldAcceptTransfer, NULL); |
| 68 | + |
| 69 | + // Test 1: Only one byte of payload |
| 70 | + queue_item = createTxItem(&canard.allocator); |
| 71 | + frame.data[0] = 0x11; |
| 72 | + frame.data_len = 1; |
| 73 | + ASSERT_EQ(canardBufferedCANFramePushBytes(&canard.allocator, queue_item, frame.data, frame.data_len), CANARD_OK); |
| 74 | + |
| 75 | + // ensure no space was allocated, except for the Tx Item |
| 76 | + ASSERT_EQ(canard.allocator.statistics.current_usage_blocks, 1); |
| 77 | + |
| 78 | + // check the frame |
| 79 | + canardBufferedCANFrameToCANFrame(&canard.allocator, &frame, queue_item); |
| 80 | + ASSERT_EQ(frame.data_len, 1); |
| 81 | + ASSERT_EQ(frame.data[0], 0x11); |
| 82 | + |
| 83 | + // push 7 more bytes |
| 84 | + memset(frame.data, 0x22, 7); |
| 85 | + frame.data_len = 7; |
| 86 | + ASSERT_EQ(canardBufferedCANFramePushBytes(&canard.allocator, queue_item, frame.data, frame.data_len), CANARD_OK); |
| 87 | + |
| 88 | + // depending on the size of payload head, a block may or may not be allocated |
| 89 | + if (CANARD_TX_QUEUE_PAYLOAD_HEAD_SIZE >= 8) { |
| 90 | + ASSERT_EQ(canard.allocator.statistics.current_usage_blocks, 1); |
| 91 | + } else { |
| 92 | + ASSERT_EQ(canard.allocator.statistics.current_usage_blocks, 2); |
| 93 | + } |
| 94 | + total_allocated_bytes = 8.0 - CANARD_TX_QUEUE_PAYLOAD_HEAD_SIZE; |
| 95 | + |
| 96 | + // check the frame |
| 97 | + canardBufferedCANFrameToCANFrame(&canard.allocator, &frame, queue_item); |
| 98 | + ASSERT_EQ(frame.data_len, 8); |
| 99 | + ASSERT_EQ(frame.data[0], 0x11); |
| 100 | + for (int i = 1; i < 8; i++) { |
| 101 | + ASSERT_EQ(frame.data[i], 0x22); |
| 102 | + } |
| 103 | + |
| 104 | + // push 48 more byte |
| 105 | + memset(frame.data, 0x33, 48); |
| 106 | + frame.data_len = 48; |
| 107 | + ASSERT_EQ(canardBufferedCANFramePushBytes(&canard.allocator, queue_item, frame.data, frame.data_len), CANARD_OK); |
| 108 | + total_allocated_bytes += 48.0; |
| 109 | + |
| 110 | + // (48 / (Block size)) should be allocated |
| 111 | + ASSERT_EQ(canard.allocator.statistics.current_usage_blocks, ceil(total_allocated_bytes / CANARD_BUFFER_BLOCK_DATA_SIZE) + 1); |
| 112 | + |
| 113 | + // check the frame |
| 114 | + canardBufferedCANFrameToCANFrame(&canard.allocator, &frame, queue_item); |
| 115 | + ASSERT_EQ(frame.data_len, 56); |
| 116 | + ASSERT_EQ(frame.data[0], 0x11); |
| 117 | + for (int i = 1; i < 8; i++) { |
| 118 | + ASSERT_EQ(frame.data[i], 0x22); |
| 119 | + } |
| 120 | + for (int i = 8; i < 56; i++) { |
| 121 | + ASSERT_EQ(frame.data[i], 0x33); |
| 122 | + } |
| 123 | + |
| 124 | + // push 4 more bytes |
| 125 | + memset(frame.data, 0x55, 4); |
| 126 | + frame.data_len = 4; |
| 127 | + ASSERT_EQ(canardBufferedCANFramePushBytes(&canard.allocator, queue_item, frame.data, frame.data_len), CANARD_OK); |
| 128 | + total_allocated_bytes += 4.0; |
| 129 | + ASSERT_EQ(canard.allocator.statistics.current_usage_blocks, ceil(total_allocated_bytes / CANARD_BUFFER_BLOCK_DATA_SIZE) + 1); |
| 130 | + |
| 131 | + // check the frame |
| 132 | + canardBufferedCANFrameToCANFrame(&canard.allocator, &frame, queue_item); |
| 133 | + ASSERT_EQ(frame.data_len, 60); |
| 134 | + ASSERT_EQ(frame.data[0], 0x11); |
| 135 | + for (int i = 1; i < 8; i++) { |
| 136 | + ASSERT_EQ(frame.data[i], 0x22); |
| 137 | + } |
| 138 | + for (int i = 8; i < 56; i++) { |
| 139 | + ASSERT_EQ(frame.data[i], 0x33); |
| 140 | + } |
| 141 | + for (int i = 56; i < 60; i++) { |
| 142 | + ASSERT_EQ(frame.data[i], 0x55); |
| 143 | + } |
| 144 | + |
| 145 | + // push 4 more bytes |
| 146 | + memset(frame.data, 0x66, 4); |
| 147 | + frame.data_len = 4; |
| 148 | + ASSERT_EQ(canardBufferedCANFramePushBytes(&canard.allocator, queue_item, frame.data, frame.data_len), CANARD_OK); |
| 149 | + total_allocated_bytes += 4.0; |
| 150 | + ASSERT_EQ(canard.allocator.statistics.current_usage_blocks, ceil(total_allocated_bytes / CANARD_BUFFER_BLOCK_DATA_SIZE) + 1); |
| 151 | + |
| 152 | + // check the frame |
| 153 | + canardBufferedCANFrameToCANFrame(&canard.allocator, &frame, queue_item); |
| 154 | + ASSERT_EQ(frame.data_len, 64); |
| 155 | + ASSERT_EQ(frame.data[0], 0x11); |
| 156 | + for (int i = 1; i < 8; i++) { |
| 157 | + ASSERT_EQ(frame.data[i], 0x22); |
| 158 | + } |
| 159 | + for (int i = 8; i < 56; i++) { |
| 160 | + ASSERT_EQ(frame.data[i], 0x33); |
| 161 | + } |
| 162 | + for (int i = 56; i < 60; i++) { |
| 163 | + ASSERT_EQ(frame.data[i], 0x55); |
| 164 | + } |
| 165 | + for (int i = 60; i < 64; i++) { |
| 166 | + ASSERT_EQ(frame.data[i], 0x66); |
| 167 | + } |
| 168 | +} |
| 169 | +#endif |
0 commit comments