3-phase PV router
Loading...
Searching...
No Matches
test_main.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
11
12#include <unity.h>
13
14#include <stdint.h>
15#include <string.h>
16
17#include "onewire_search.h"
18
19namespace
20{
21constexpr uint8_t MAX_DEVICES{ 8 };
22
24class SimulatedBus
25{
26public:
27 void add(const uint8_t (&rom)[8])
28 {
29 if (count != MAX_DEVICES)
30 {
31 memcpy(roms[count++], rom, 8);
32 }
33 }
34
36 void replugAll(uint8_t devices)
37 {
38 count = devices;
39 }
40
41 bool reset()
42 {
43 ++resets;
44 for (uint8_t i = 0; i != count; ++i)
45 {
46 inSearch[i] = true;
47 }
48 bit = 0;
49 readComplement = false;
50 return count != 0; // presence pulse
51 }
52
53 void write(uint8_t command)
54 {
55 lastCommand = command;
56 }
57
58 uint8_t read_bit()
59 {
60 uint8_t line{ 1 };
61 for (uint8_t i = 0; i != count; ++i)
62 {
63 if (inSearch[i] && (bitOf(i) == readComplement)) // sends 0: bit 0, or complement of 1
64 {
65 line = 0;
66 }
67 }
68 readComplement = !readComplement;
69 return line;
70 }
71
72 void write_bit(uint8_t value)
73 {
74 for (uint8_t i = 0; i != count; ++i)
75 {
76 if (bitOf(i) != (value != 0))
77 {
78 inSearch[i] = false;
79 }
80 }
81 ++bit;
82 }
83
85 void unplugAll()
86 {
87 plugged = count;
88 count = 0;
89 }
90
91 uint8_t plugged{ 0 };
92
93 uint8_t lastCommand{ 0 };
94 uint16_t resets{ 0 };
95
96private:
97 bool bitOf(uint8_t device) const
98 {
99 return (roms[device][bit / 8] >> (bit % 8)) & 1U;
100 }
101
102 uint8_t roms[MAX_DEVICES][8]{};
103 bool inSearch[MAX_DEVICES]{};
104 uint8_t count{ 0 };
105 uint8_t bit{ 0 };
106 bool readComplement{ false };
107};
108
110uint8_t searchAll(SimulatedBus& bus, uint8_t (&found)[MAX_DEVICES + 1][8])
111{
112 RomSearch< SimulatedBus > search{ bus };
113 uint8_t rom[8]{};
114 uint8_t n{ 0 };
115 while ((n != MAX_DEVICES + 1) && search.next(rom))
116 {
117 memcpy(found[n++], rom, 8);
118 }
119 return n;
120}
121
122bool contains(const uint8_t (&found)[MAX_DEVICES + 1][8], uint8_t n, const uint8_t* rom)
123{
124 for (uint8_t i = 0; i != n; ++i)
125 {
126 if (0 == memcmp(found[i], rom, 8))
127 {
128 return true;
129 }
130 }
131 return false;
132}
133
134// DS18B20 codes: family 0x28, serial number, CRC (the search does not check it)
135constexpr uint8_t SENSOR_A[8]{ 0x28, 0xBE, 0x41, 0x6B, 0x09, 0x00, 0x00, 0xA4 };
136constexpr uint8_t SENSOR_B[8]{ 0x28, 0x1B, 0xD7, 0x6A, 0x09, 0x00, 0x00, 0xB7 };
137constexpr uint8_t SENSOR_C[8]{ 0x28, 0x1B, 0xD7, 0x6A, 0x09, 0x00, 0x00, 0x37 }; // B but bit 63, the last one sent
138constexpr uint8_t SENSOR_D[8]{ 0x28, 0xFF, 0x00, 0x12, 0x34, 0x56, 0x78, 0x9A };
139} // namespace
140
141void setUp()
142{
143}
144
146{
147}
148
150{
151 SimulatedBus bus;
152 uint8_t found[MAX_DEVICES + 1][8]{};
153
154 TEST_ASSERT_EQUAL_UINT8(0, searchAll(bus, found));
155}
156
158{
159 SimulatedBus bus;
160 bus.add(SENSOR_A);
161 uint8_t found[MAX_DEVICES + 1][8]{};
162
163 TEST_ASSERT_EQUAL_UINT8(1, searchAll(bus, found));
164 TEST_ASSERT_EQUAL_UINT8_ARRAY(SENSOR_A, found[0], 8);
165 TEST_ASSERT_EQUAL_HEX8(0xF0, bus.lastCommand); // SEARCH ROM
166}
167
169{
170 SimulatedBus bus;
171 bus.add(SENSOR_A);
172 bus.add(SENSOR_B);
173 uint8_t found[MAX_DEVICES + 1][8]{};
174
175 TEST_ASSERT_EQUAL_UINT8(2, searchAll(bus, found));
176 TEST_ASSERT_TRUE(contains(found, 2, SENSOR_A));
177 TEST_ASSERT_TRUE(contains(found, 2, SENSOR_B));
178}
179
181{
182 SimulatedBus bus;
183 bus.add(SENSOR_B);
184 bus.add(SENSOR_C);
185 uint8_t found[MAX_DEVICES + 1][8]{};
186
187 TEST_ASSERT_EQUAL_UINT8(2, searchAll(bus, found));
188 TEST_ASSERT_TRUE(contains(found, 2, SENSOR_B));
189 TEST_ASSERT_TRUE(contains(found, 2, SENSOR_C));
190}
191
193{
194 SimulatedBus bus;
195 bus.add(SENSOR_A);
196 bus.add(SENSOR_B);
197 bus.add(SENSOR_C);
198 bus.add(SENSOR_D);
199 uint8_t found[MAX_DEVICES + 1][8]{};
200
201 TEST_ASSERT_EQUAL_UINT8(4, searchAll(bus, found));
202 TEST_ASSERT_TRUE(contains(found, 4, SENSOR_A));
203 TEST_ASSERT_TRUE(contains(found, 4, SENSOR_B));
204 TEST_ASSERT_TRUE(contains(found, 4, SENSOR_C));
205 TEST_ASSERT_TRUE(contains(found, 4, SENSOR_D));
206}
207
209{
210 SimulatedBus bus;
211 uint8_t roms[MAX_DEVICES][8]{};
212 for (uint8_t i = 0; i != MAX_DEVICES; ++i)
213 {
214 // codes that share long prefixes: conflicts deep in the 64 bits
215 const uint8_t rom[8]{ 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, static_cast< uint8_t >(i * 37), static_cast< uint8_t >(i) };
216 memcpy(roms[i], rom, 8);
217 bus.add(rom);
218 }
219 uint8_t found[MAX_DEVICES + 1][8]{};
220
221 TEST_ASSERT_EQUAL_UINT8(MAX_DEVICES, searchAll(bus, found));
222 for (uint8_t i = 0; i != MAX_DEVICES; ++i)
223 {
224 TEST_ASSERT_TRUE(contains(found, MAX_DEVICES, roms[i]));
225 }
226}
227
229{
230 SimulatedBus bus;
231 bus.add(SENSOR_A);
232 RomSearch< SimulatedBus > search{ bus };
233 uint8_t rom[8]{};
234
235 TEST_ASSERT_TRUE(search.next(rom));
236 const uint16_t resets{ bus.resets };
237 TEST_ASSERT_FALSE(search.next(rom));
238 TEST_ASSERT_EQUAL_UINT16(resets, bus.resets); // no bus traffic once the last one is found
239}
240
242{
243 SimulatedBus bus;
244 bus.add(SENSOR_A);
245 bus.add(SENSOR_D);
246 RomSearch< SimulatedBus > search{ bus };
247 uint8_t rom[8]{};
248
249 TEST_ASSERT_TRUE(search.next(rom));
250 bus.unplugAll();
251 TEST_ASSERT_FALSE(search.next(rom)); // no presence pulse
252}
253
255{
256 SimulatedBus bus;
257 bus.add(SENSOR_A);
258 bus.add(SENSOR_D);
259 RomSearch< SimulatedBus > search{ bus };
260 uint8_t first[8]{};
261 uint8_t rom[8]{};
262
263 TEST_ASSERT_TRUE(search.next(first));
264 bus.unplugAll();
265 TEST_ASSERT_FALSE(search.next(rom));
266 bus.replugAll(bus.plugged);
267 TEST_ASSERT_TRUE(search.next(rom));
268 TEST_ASSERT_EQUAL_UINT8_ARRAY(first, rom, 8); // the first device again, not the second
269}
270
271int main()
272{
273 UNITY_BEGIN();
274 RUN_TEST(test_no_device);
275 RUN_TEST(test_one_device);
276 RUN_TEST(test_two_devices);
279 RUN_TEST(test_eight_devices);
283 return UNITY_END();
284}
The successive searches of a 1-Wire bus, one device each.
void setUp(void)
Definition test_main.cpp:27
void tearDown(void)
Definition test_main.cpp:28
uint8_t i
constexpr uint8_t bitOf(const uint8_t(&map)[N], uint8_t loadNum)
Rank of a load among the loads of its own unit, in ascending map order.
Definition load_map.h:195
char * line(char *dst, const char *tag, int16_t value, uint8_t index=0)
Writes the line of a signed value.
Definition teleinfo.h:142
int main(void)
void test_two_devices()
void test_one_device()
void test_no_search_after_the_last_device()
void test_search_starts_over_after_a_reset_without_presence()
void test_devices_unplugged_between_two_searches()
void test_four_devices_each_found_once()
void test_eight_devices()
void test_no_device()
void test_codes_differing_in_their_last_bit()
The ROM search of a 1-Wire bus, on the bit primitives of the bus.