Debian Codegraph @

olad/plugin_api/UniverseTest.cpp

raw
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 *
16 * UniverseTest.cpp
17 * Test fixture for the Universe and UniverseStore classes
18 * Copyright (C) 2005 Simon Newton
19 */
20
21 #include <cppunit/extensions/HelperMacros.h>
22 #include <iostream>
23 #include <string>
24 #include <vector>
25
26 #include "ola/Callback.h"
27 #include "ola/Constants.h"
28 #include "ola/Clock.h"
29 #include "ola/DmxBuffer.h"
30 #include "ola/rdm/RDMCommand.h"
31 #include "ola/rdm/RDMReply.h"
32 #include "ola/rdm/RDMResponseCodes.h"
33 #include "ola/rdm/UID.h"
34 #include "olad/DmxSource.h"
35 #include "olad/PluginAdaptor.h"
36 #include "olad/Port.h"
37 #include "olad/PortBroker.h"
38 #include "olad/Preferences.h"
39 #include "olad/Universe.h"
40 #include "olad/plugin_api/Client.h"
41 #include "olad/plugin_api/PortManager.h"
42 #include "olad/plugin_api/TestCommon.h"
43 #include "olad/plugin_api/UniverseStore.h"
44 #include "ola/testing/TestUtils.h"
45
46
47 using ola::AbstractDevice;
48 using ola::Clock;
49 using ola::DmxBuffer;
50 using ola::NewCallback;
51 using ola::NewSingleCallback;
52 using ola::TimeStamp;
53 using ola::Universe;
54 using ola::rdm::NewDiscoveryUniqueBranchRequest;
55 using ola::rdm::RDMCallback;
56 using ola::rdm::RDMReply;
57 using ola::rdm::RDMRequest;
58 using ola::rdm::RDMResponse;
59 using ola::rdm::UID;
60 using ola::rdm::UIDSet;
61 using ola::rdm::RDMStatusCode;
62 using std::string;
63 using std::vector;
64
65 static unsigned int TEST_UNIVERSE = 1;
66 static const char TEST_DATA[] = "this is some test data";
67
68
69 class UniverseTest: public CppUnit::TestFixture {
70 CPPUNIT_TEST_SUITE(UniverseTest);
71 CPPUNIT_TEST(testLifecycle);
72 CPPUNIT_TEST(testSetGetDmx);
73 CPPUNIT_TEST(testSendDmx);
74 CPPUNIT_TEST(testReceiveDmx);
75 CPPUNIT_TEST(testSourceClients);
76 CPPUNIT_TEST(testSinkClients);
77 CPPUNIT_TEST(testLtpMerging);
78 CPPUNIT_TEST(testHtpMerging);
79 CPPUNIT_TEST(testRDMDiscovery);
80 CPPUNIT_TEST(testRDMSend);
81 CPPUNIT_TEST_SUITE_END();
82
83 public:
84 void setUp();
85 void tearDown();
86 void testLifecycle();
87 void testSetGetDmx();
88 void testSendDmx();
89 void testReceiveDmx();
90 void testSourceClients();
91 void testSinkClients();
92 void testLtpMerging();
93 void testHtpMerging();
94 void testRDMDiscovery();
95 void testRDMSend();
96
97 private:
98 ola::MemoryPreferences *m_preferences;
99 ola::UniverseStore *m_store;
100 DmxBuffer m_buffer;
101 ola::Clock m_clock;
102
103 void ConfirmUIDs(UIDSet *expected, const UIDSet &uids);
104
105 void ConfirmRDM(int line,
106 RDMStatusCode expected_status_code,
107 const RDMResponse *expected_response,
108 RDMReply *reply);
109
110 void ReturnRDMCode(RDMStatusCode status_code,
111 const RDMRequest *request,
112 RDMCallback *callback) {
113 delete request;
114 RunRDMCallback(callback, status_code);
115 }
116 };
117
118
119 class MockClient: public ola::Client {
120 public:
121 MockClient()
122 : ola::Client(NULL, UID(ola::OPEN_LIGHTING_ESTA_CODE, 0)),
123 m_dmx_set(false) {
124 }
125
126 bool SendDMX(unsigned int universe_id, uint8_t priority,
127 const DmxBuffer &buffer) {
128 OLA_ASSERT_EQ(TEST_UNIVERSE, universe_id);
129 OLA_ASSERT_EQ(ola::dmx::SOURCE_PRIORITY_MIN, priority);
130 OLA_ASSERT_EQ(string(TEST_DATA), buffer.Get());
131 m_dmx_set = true;
132 return true;
133 }
134 bool m_dmx_set;
135 };
136
137
138 CPPUNIT_TEST_SUITE_REGISTRATION(UniverseTest);
139
140
141 void UniverseTest::setUp() {
142 ola::InitLogging(ola::OLA_LOG_INFO, ola::OLA_LOG_STDERR);
143 m_preferences = new ola::MemoryPreferences("foo");
144 m_store = new ola::UniverseStore(m_preferences, NULL);
145 m_buffer.Set(TEST_DATA);
146 }
147
148 void UniverseTest::tearDown() {
149 delete m_store;
150 delete m_preferences;
151 }
152
153 /*
154 * Test that we can create universes and save their settings
155 */
156 void UniverseTest::testLifecycle() {
157 Universe *universe = m_store->GetUniverse(TEST_UNIVERSE);
158 OLA_ASSERT_FALSE(universe);
159
160 universe = m_store->GetUniverseOrCreate(TEST_UNIVERSE);
161 OLA_ASSERT(universe);
162 OLA_ASSERT_EQ(TEST_UNIVERSE, universe->UniverseId());
163 OLA_ASSERT_EQ((unsigned int) 1, m_store->UniverseCount());
164 OLA_ASSERT_EQ(Universe::MERGE_LTP, universe->MergeMode());
165 OLA_ASSERT_FALSE(universe->IsActive());
166
167 string universe_name = "New Name";
168 universe->SetName(universe_name);
169 universe->SetMergeMode(Universe::MERGE_HTP);
170
171 OLA_ASSERT_EQ(universe_name, universe->Name());
172 OLA_ASSERT_EQ(Universe::MERGE_HTP, universe->MergeMode());
173
174 // delete it
175 m_store->AddUniverseGarbageCollection(universe);
176 m_store->GarbageCollectUniverses();
177 OLA_ASSERT_EQ((unsigned int) 0, m_store->UniverseCount());
178 universe = m_store->GetUniverse(TEST_UNIVERSE);
179 OLA_ASSERT_FALSE(universe);
180
181 // now re-create it
182 universe = m_store->GetUniverseOrCreate(TEST_UNIVERSE);
183 OLA_ASSERT(universe);
184 OLA_ASSERT_EQ((unsigned int) 1, m_store->UniverseCount());
185 OLA_ASSERT_EQ(TEST_UNIVERSE, universe->UniverseId());
186 OLA_ASSERT_EQ(universe_name, universe->Name());
187 OLA_ASSERT_EQ(Universe::MERGE_HTP, universe->MergeMode());
188
189 m_store->DeleteAll();
190 OLA_ASSERT_EQ((unsigned int) 0, m_store->UniverseCount());
191 }
192
193
194 /*
195 * Check that SetDMX/GetDMX works
196 */
197 void UniverseTest::testSetGetDmx() {
198 Universe *universe = m_store->GetUniverseOrCreate(TEST_UNIVERSE);
199 OLA_ASSERT(universe);
200
201 // a new universe should be all 0s
202 DmxBuffer empty_buffer;
203 OLA_ASSERT(empty_buffer == universe->GetDMX());
204
205 // check that SetDMX works
206 OLA_ASSERT(universe->SetDMX(m_buffer));
207 OLA_ASSERT(m_buffer == universe->GetDMX());
208 }
209
210
211 /*
212 * Check that SendDmx updates all ports
213 */
214 void UniverseTest::testSendDmx() {
215 Universe *universe = m_store->GetUniverseOrCreate(TEST_UNIVERSE);
216 OLA_ASSERT(universe);
217
218 TestMockOutputPort port(NULL, 1); // output port
219 universe->AddPort(&port);
220 OLA_ASSERT_EQ((unsigned int) 0, universe->InputPortCount());
221 OLA_ASSERT_EQ((unsigned int) 1, universe->OutputPortCount());
222 OLA_ASSERT(universe->IsActive());
223
224 // send some data to the universe and check the port gets it
225 OLA_ASSERT(universe->SetDMX(m_buffer));
226 OLA_ASSERT(m_buffer == port.ReadDMX());
227
228 // remove the port from the universe
229 universe->RemovePort(&port);
230 OLA_ASSERT_EQ((unsigned int) 0, universe->InputPortCount());
231 OLA_ASSERT_EQ((unsigned int) 0, universe->OutputPortCount());
232 OLA_ASSERT_FALSE(universe->IsActive());
233 }
234
235
236 /*
237 * Check that we update when ports have new data
238 */
239 void UniverseTest::testReceiveDmx() {
240 ola::PortBroker broker;
241 ola::PortManager port_manager(m_store, &broker);
242 TimeStamp time_stamp;
243 MockSelectServer ss(&time_stamp);
244 ola::PluginAdaptor plugin_adaptor(NULL, &ss, NULL, NULL, NULL, NULL);
245
246 MockDevice device(NULL, "foo");
247 TestMockInputPort port(&device, 1, &plugin_adaptor); // input port
248 port_manager.PatchPort(&port, TEST_UNIVERSE);
249
250 Universe *universe = m_store->GetUniverseOrCreate(TEST_UNIVERSE);
251 OLA_ASSERT(universe);
252
253 OLA_ASSERT_EQ((unsigned int) 1, universe->InputPortCount());
254 OLA_ASSERT_EQ((unsigned int) 0, universe->OutputPortCount());
255 OLA_ASSERT(universe->IsActive());
256
257 // Setup the port with some data, and check that signalling the universe
258 // works.
259 m_clock.CurrentMonotonicTime(&time_stamp);
260 port.WriteDMX(m_buffer);
261 port.DmxChanged();
262 OLA_ASSERT_EQ(ola::dmx::SOURCE_PRIORITY_DEFAULT, universe->ActivePriority());
263 OLA_ASSERT_EQ(m_buffer.Size(), universe->GetDMX().Size());
264 OLA_ASSERT(m_buffer == universe->GetDMX());
265
266 // Remove the port from the universe
267 universe->RemovePort(&port);
268 OLA_ASSERT_FALSE(universe->IsActive());
269 OLA_ASSERT_EQ((unsigned int) 0, universe->InputPortCount());
270 OLA_ASSERT_EQ((unsigned int) 0, universe->OutputPortCount());
271 }
272
273
274 /*
275 * Check that we can add/remove source clients from this universes
276 */
277 void UniverseTest::testSourceClients() {
278 Universe *universe = m_store->GetUniverseOrCreate(TEST_UNIVERSE);
279 OLA_ASSERT(universe);
280 OLA_ASSERT_EQ((unsigned int) 0, universe->SourceClientCount());
281 OLA_ASSERT_EQ((unsigned int) 0, universe->SinkClientCount());
282
283 // test that we can add a source client
284 MockClient client;
285 universe->AddSourceClient(&client);
286 OLA_ASSERT_EQ((unsigned int) 1, universe->SourceClientCount());
287 OLA_ASSERT_EQ((unsigned int) 0, universe->SinkClientCount());
288 OLA_ASSERT(universe->ContainsSourceClient(&client));
289 OLA_ASSERT_FALSE(universe->ContainsSinkClient(&client));
290 OLA_ASSERT(universe->IsActive());
291
292 // Setting DMX now does nothing
293 OLA_ASSERT_FALSE(client.m_dmx_set);
294 universe->SetDMX(m_buffer);
295 OLA_ASSERT_FALSE(client.m_dmx_set);
296
297 // now remove it
298 universe->RemoveSourceClient(&client);
299 OLA_ASSERT_EQ((unsigned int) 0, universe->SourceClientCount());
300 OLA_ASSERT_EQ((unsigned int) 0, universe->SinkClientCount());
301 OLA_ASSERT_FALSE(universe->ContainsSourceClient(&client));
302 OLA_ASSERT_FALSE(universe->ContainsSinkClient(&client));
303 OLA_ASSERT_FALSE(universe->IsActive());
304
305 // try to remove it again
306 OLA_ASSERT_FALSE(universe->RemoveSourceClient(&client));
307 OLA_ASSERT_EQ((unsigned int) 0, universe->SourceClientCount());
308 OLA_ASSERT_EQ((unsigned int) 0, universe->SinkClientCount());
309 OLA_ASSERT_FALSE(universe->ContainsSourceClient(&client));
310 OLA_ASSERT_FALSE(universe->ContainsSinkClient(&client));
311 OLA_ASSERT_FALSE(universe->IsActive());
312 }
313
314
315 /*
316 * Check that we can add/remove sink clients from this universes
317 */
318 void UniverseTest::testSinkClients() {
319 Universe *universe = m_store->GetUniverseOrCreate(TEST_UNIVERSE);
320 OLA_ASSERT(universe);
321 OLA_ASSERT_EQ((unsigned int) 0, universe->SourceClientCount());
322 OLA_ASSERT_EQ((unsigned int) 0, universe->SinkClientCount());
323
324 // test that we can add a source client
325 MockClient client;
326 universe->AddSinkClient(&client);
327 OLA_ASSERT_EQ((unsigned int) 1, universe->SinkClientCount());
328 OLA_ASSERT_EQ((unsigned int) 0, universe->SourceClientCount());
329 OLA_ASSERT(universe->ContainsSinkClient(&client));
330 OLA_ASSERT_FALSE(universe->ContainsSourceClient(&client));
331 OLA_ASSERT(universe->IsActive());
332
333 // Setting DMX now should update the client
334 OLA_ASSERT_FALSE(client.m_dmx_set);
335 universe->SetDMX(m_buffer);
336 OLA_ASSERT(client.m_dmx_set);
337
338 // now remove it
339 universe->RemoveSinkClient(&client);
340 OLA_ASSERT_EQ((unsigned int) 0, universe->SinkClientCount());
341 OLA_ASSERT_EQ((unsigned int) 0, universe->SourceClientCount());
342 OLA_ASSERT_FALSE(universe->ContainsSinkClient(&client));
343 OLA_ASSERT_FALSE(universe->ContainsSourceClient(&client));
344 OLA_ASSERT_FALSE(universe->IsActive());
345
346 // try to remove it again
347 OLA_ASSERT_FALSE(universe->RemoveSinkClient(&client));
348 OLA_ASSERT_EQ((unsigned int) 0, universe->SinkClientCount());
349 OLA_ASSERT_EQ((unsigned int) 0, universe->SourceClientCount());
350 OLA_ASSERT_FALSE(universe->ContainsSinkClient(&client));
351 OLA_ASSERT_FALSE(universe->ContainsSourceClient(&client));
352 OLA_ASSERT_FALSE(universe->IsActive());
353 }
354
355
356 /*
357 * Check that LTP merging works correctly
358 */
359 void UniverseTest::testLtpMerging() {
360 DmxBuffer buffer1, buffer2, htp_buffer;
361 buffer1.SetFromString("1,0,0,10");
362 buffer2.SetFromString("0,255,0,5,6,7");
363
364 ola::PortBroker broker;
365 ola::PortManager port_manager(m_store, &broker);
366
367 TimeStamp time_stamp;
368 MockSelectServer ss(&time_stamp);
369 ola::PluginAdaptor plugin_adaptor(NULL, &ss, NULL, NULL, NULL, NULL);
370 MockDevice device(NULL, "foo");
371 MockDevice device2(NULL, "bar");
372 TestMockInputPort port(&device, 1, &plugin_adaptor); // input port
373 TestMockInputPort port2(&device2, 1, &plugin_adaptor); // input port
374 port_manager.PatchPort(&port, TEST_UNIVERSE);
375 port_manager.PatchPort(&port2, TEST_UNIVERSE);
376
377 Universe *universe = m_store->GetUniverseOrCreate(TEST_UNIVERSE);
378 OLA_ASSERT(universe);
379 universe->SetMergeMode(Universe::MERGE_LTP);
380
381 OLA_ASSERT_EQ((unsigned int) 2, universe->InputPortCount());
382 OLA_ASSERT_EQ((unsigned int) 0, universe->OutputPortCount());
383 OLA_ASSERT(universe->IsActive());
384 OLA_ASSERT_EQ((unsigned int) 0, universe->GetDMX().Size());
385
386 // Setup the ports with some data, and check that signalling the universe
387 // works.
388 m_clock.CurrentMonotonicTime(&time_stamp);
389 port.WriteDMX(buffer1);
390 port.DmxChanged();
391 OLA_ASSERT_EQ(ola::dmx::SOURCE_PRIORITY_DEFAULT, universe->ActivePriority());
392 OLA_ASSERT_EQ(buffer1.Size(), universe->GetDMX().Size());
393 OLA_ASSERT(buffer1 == universe->GetDMX());
394
395 // Now the second port gets data
396 m_clock.CurrentMonotonicTime(&time_stamp);
397 port2.WriteDMX(buffer2);
398 port2.DmxChanged();
399 OLA_ASSERT_EQ(ola::dmx::SOURCE_PRIORITY_DEFAULT, universe->ActivePriority());
400 OLA_ASSERT_EQ(buffer2.Size(), universe->GetDMX().Size());
401 OLA_ASSERT(buffer2 == universe->GetDMX());
402
403 // now resend the first port
404 m_clock.CurrentMonotonicTime(&time_stamp);
405 port.WriteDMX(buffer1);
406 port.DmxChanged();
407 OLA_ASSERT_EQ(ola::dmx::SOURCE_PRIORITY_DEFAULT, universe->ActivePriority());
408 OLA_ASSERT_EQ(buffer1.Size(), universe->GetDMX().Size());
409 OLA_ASSERT(buffer1 == universe->GetDMX());
410
411 // now check a client
412 DmxBuffer client_buffer;
413 client_buffer.SetFromString("255,0,0,255,10");
414 m_clock.CurrentMonotonicTime(&time_stamp);
415 ola::DmxSource source(client_buffer, time_stamp,
416 ola::dmx::SOURCE_PRIORITY_DEFAULT);
417 MockClient input_client;
418 input_client.DMXReceived(TEST_UNIVERSE, source);
419 universe->SourceClientDataChanged(&input_client);
420
421 DmxBuffer client_htp_merge_result;
422 client_htp_merge_result.SetFromString("255,255,0,255,10,7");
423 OLA_ASSERT_EQ(ola::dmx::SOURCE_PRIORITY_DEFAULT, universe->ActivePriority());
424 OLA_ASSERT_EQ(client_buffer.Size(), universe->GetDMX().Size());
425 OLA_ASSERT(client_buffer == universe->GetDMX());
426
427 // clean up
428 universe->RemoveSourceClient(&input_client);
429 universe->RemovePort(&port);
430 universe->RemovePort(&port2);
431 OLA_ASSERT_FALSE(universe->IsActive());
432 }
433
434
435 /*
436 * Check that HTP merging works correctly
437 */
438 void UniverseTest::testHtpMerging() {
439 DmxBuffer buffer1, buffer2, htp_buffer;
440 buffer1.SetFromString("1,0,0,10");
441 buffer2.SetFromString("0,255,0,5,6,7");
442 htp_buffer.SetFromString("1,255,0,10,6,7");
443
444 ola::PortBroker broker;
445 ola::PortManager port_manager(m_store, &broker);
446
447 TimeStamp time_stamp;
448 MockSelectServer ss(&time_stamp);
449 ola::PluginAdaptor plugin_adaptor(NULL, &ss, NULL, NULL, NULL, NULL);
450 MockDevice device(NULL, "foo");
451 MockDevice device2(NULL, "bar");
452 TestMockInputPort port(&device, 1, &plugin_adaptor); // input port
453 TestMockInputPort port2(&device2, 1, &plugin_adaptor); // input port
454 port_manager.PatchPort(&port, TEST_UNIVERSE);
455 port_manager.PatchPort(&port2, TEST_UNIVERSE);
456
457 Universe *universe = m_store->GetUniverseOrCreate(TEST_UNIVERSE);
458 OLA_ASSERT(universe);
459 universe->SetMergeMode(Universe::MERGE_HTP);
460
461 OLA_ASSERT_EQ(universe->OutputPortCount(), (unsigned int) 0);
462 OLA_ASSERT_EQ(universe->OutputPortCount(), (unsigned int) 0);
463 OLA_ASSERT(universe->IsActive());
464 OLA_ASSERT_EQ((unsigned int) 0, universe->GetDMX().Size());
465
466 // Setup the ports with some data, and check that signalling the universe
467 // works.
468 m_clock.CurrentMonotonicTime(&time_stamp);
469 port.WriteDMX(buffer1);
470 port.DmxChanged();
471 OLA_ASSERT_EQ(ola::dmx::SOURCE_PRIORITY_DEFAULT, universe->ActivePriority());
472 OLA_ASSERT_EQ(buffer1.Size(), universe->GetDMX().Size());
473 OLA_ASSERT(buffer1 == universe->GetDMX());
474
475 // Now the second port gets data
476 m_clock.CurrentMonotonicTime(&time_stamp);
477 port2.WriteDMX(buffer2);
478 port2.DmxChanged();
479 OLA_ASSERT_EQ(ola::dmx::SOURCE_PRIORITY_DEFAULT, universe->ActivePriority());
480 OLA_ASSERT_EQ(htp_buffer.Size(), universe->GetDMX().Size());
481 OLA_ASSERT(htp_buffer == universe->GetDMX());
482
483 // now raise the priority of the second port
484 uint8_t new_priority = 120;
485 port2.SetPriority(new_priority);
486 m_clock.CurrentMonotonicTime(&time_stamp);
487 port2.DmxChanged();
488 OLA_ASSERT_EQ(new_priority, universe->ActivePriority());
489 OLA_ASSERT_EQ(buffer2.Size(), universe->GetDMX().Size());
490 OLA_ASSERT(buffer2 == universe->GetDMX());
491
492 // raise the priority of the first port
493 port.SetPriority(new_priority);
494 m_clock.CurrentMonotonicTime(&time_stamp);
495 port.DmxChanged();
496 OLA_ASSERT_EQ(new_priority, universe->ActivePriority());
497 OLA_ASSERT_EQ(htp_buffer.Size(), universe->GetDMX().Size());
498 OLA_ASSERT(htp_buffer == universe->GetDMX());
499
500 // now check a client
501 DmxBuffer client_buffer;
502 client_buffer.SetFromString("255,0,0,255,10");
503 m_clock.CurrentMonotonicTime(&time_stamp);
504 ola::DmxSource source(client_buffer, time_stamp, new_priority);
505 MockClient input_client;
506 input_client.DMXReceived(TEST_UNIVERSE, source);
507 universe->SourceClientDataChanged(&input_client);
508
509 DmxBuffer client_htp_merge_result;
510 client_htp_merge_result.SetFromString("255,255,0,255,10,7");
511 OLA_ASSERT_EQ(new_priority, universe->ActivePriority());
512 OLA_ASSERT_EQ(client_htp_merge_result.Size(),
513 universe->GetDMX().Size());
514 OLA_ASSERT(client_htp_merge_result == universe->GetDMX());
515
516 // clean up
517 universe->RemoveSourceClient(&input_client);
518 universe->RemovePort(&port);
519 universe->RemovePort(&port2);
520 OLA_ASSERT_FALSE(universe->IsActive());
521 }
522
523
524 /**
525 * Test RDM discovery for a universe/
526 */
527 void UniverseTest::testRDMDiscovery() {
528 Universe *universe = m_store->GetUniverseOrCreate(TEST_UNIVERSE);
529 OLA_ASSERT(universe);
530
531 // check the uid set is initially empty
532 UIDSet universe_uids;
533 universe->GetUIDs(&universe_uids);
534 OLA_ASSERT_EQ(0u, universe_uids.Size());
535
536 UID uid1(0x7a70, 1);
537 UID uid2(0x7a70, 2);
538 UID uid3(0x7a70, 3);
539 UIDSet port1_uids, port2_uids;
540 port1_uids.AddUID(uid1);
541 port2_uids.AddUID(uid2);
542 TestMockRDMOutputPort port1(NULL, 1, &port1_uids);
543 // this port is configured to update the uids on patch
544 TestMockRDMOutputPort port2(NULL, 2, &port2_uids, true);
545 universe->AddPort(&port1);
546 port1.SetUniverse(universe);
547 universe->AddPort(&port2);
548 port2.SetUniverse(universe);
549
550 OLA_ASSERT_EQ((unsigned int) 0, universe->InputPortCount());
551 OLA_ASSERT_EQ((unsigned int) 2, universe->OutputPortCount());
552 universe->GetUIDs(&universe_uids);
553 OLA_ASSERT_EQ(1u, universe_uids.Size());
554 OLA_ASSERT(universe_uids.Contains(uid2));
555 OLA_ASSERT(universe->IsActive());
556
557 // now trigger discovery
558 UIDSet expected_uids;
559 expected_uids.AddUID(uid1);
560 expected_uids.AddUID(uid2);
561
562 universe->RunRDMDiscovery(
563 NewSingleCallback(this, &UniverseTest::ConfirmUIDs, &expected_uids),
564 true);
565
566 // now add a uid to one port, and remove a uid from another
567 port1_uids.AddUID(uid3);
568 port2_uids.RemoveUID(uid2);
569
570 expected_uids.AddUID(uid3);
571 expected_uids.RemoveUID(uid2);
572
573 universe->RunRDMDiscovery(
574 NewSingleCallback(this, &UniverseTest::ConfirmUIDs, &expected_uids),
575 true);
576
577 // remove the first port from the universe and confirm there are no more UIDs
578 universe->RemovePort(&port1);
579 expected_uids.Clear();
580
581 universe->RunRDMDiscovery(
582 NewSingleCallback(this, &UniverseTest::ConfirmUIDs, &expected_uids),
583 true);
584
585 universe_uids.Clear();
586 universe->GetUIDs(&universe_uids);
587 OLA_ASSERT_EQ(0u, universe_uids.Size());
588
589 universe->RemovePort(&port2);
590 OLA_ASSERT_EQ((unsigned int) 0, universe->InputPortCount());
591 OLA_ASSERT_EQ((unsigned int) 0, universe->OutputPortCount());
592 OLA_ASSERT_FALSE(universe->IsActive());
593 }
594
595
596 /**
597 * test Sending an RDM request
598 */
599 void UniverseTest::testRDMSend() {
600 Universe *universe = m_store->GetUniverseOrCreate(TEST_UNIVERSE);
601 OLA_ASSERT(universe);
602
603 // setup the ports with a UID on each
604 UID uid1(0x7a70, 1);
605 UID uid2(0x7a70, 2);
606 UID uid3(0x7a70, 3);
607 UIDSet port1_uids, port2_uids;
608 port1_uids.AddUID(uid1);
609 port2_uids.AddUID(uid2);
610 TestMockRDMOutputPort port1(NULL, 1, &port1_uids, true);
611 TestMockRDMOutputPort port2(NULL, 2, &port2_uids, true);
612 universe->AddPort(&port1);
613 port1.SetUniverse(universe);
614 universe->AddPort(&port2);
615 port2.SetUniverse(universe);
616
617 UID source_uid(0x7a70, 100);
618 // first try a command to a uid we don't know about
619 RDMRequest *request = new ola::rdm::RDMGetRequest(
620 source_uid,
621 uid3,
622 0, // transaction #
623 1, // port id
624 10, // sub device
625 296, // param id
626 NULL,
627 0);
628
629 universe->SendRDMRequest(
630 request,
631 NewSingleCallback(this,
632 &UniverseTest::ConfirmRDM,
633 __LINE__,
634 ola::rdm::RDM_UNKNOWN_UID,
635 #ifdef __FreeBSD__
636 reinterpret_cast<const RDMResponse*>(0)));
637 #else
638 reinterpret_cast<const RDMResponse*>(NULL)));
639 #endif // __FreeBSD__
640
641 // ok, now try something that returns a response from the port
642 request = new ola::rdm::RDMGetRequest(
643 source_uid,
644 uid1,
645 0, // transaction #
646 1, // port id
647 10, // sub device
648 296, // param id
649 NULL,
650 0);
651
652 port1.SetRDMHandler(
653 NewCallback(this, &UniverseTest::ReturnRDMCode, ola::rdm::RDM_TIMEOUT));
654
655 universe->SendRDMRequest(
656 request,
657 NewSingleCallback(this,
658 &UniverseTest::ConfirmRDM,
659 __LINE__,
660 ola::rdm::RDM_TIMEOUT,
661 #ifdef __FreeBSD__
662 reinterpret_cast<const RDMResponse*>(0)));
663 #else
664 reinterpret_cast<const RDMResponse*>(NULL)));
665 #endif // __FreeBSD__
666
667 // now try a broadcast fan out
668 UID vendorcast_uid = UID::VendorcastAddress(0x7a70);
669 request = new ola::rdm::RDMGetRequest(
670 source_uid,
671 vendorcast_uid,
672 0, // transaction #
673 1, // port id
674 10, // sub device
675 296, // param id
676 NULL,
677 0);
678
679 port1.SetRDMHandler(
680 NewCallback(this,
681 &UniverseTest::ReturnRDMCode,
682 ola::rdm::RDM_WAS_BROADCAST));
683 port2.SetRDMHandler(
684 NewCallback(this,
685 &UniverseTest::ReturnRDMCode,
686 ola::rdm::RDM_WAS_BROADCAST));
687
688 universe->SendRDMRequest(
689 request,
690 NewSingleCallback(this,
691 &UniverseTest::ConfirmRDM,
692 __LINE__,
693 ola::rdm::RDM_WAS_BROADCAST,
694 #ifdef __FreeBSD__
695 reinterpret_cast<const RDMResponse*>(0)));
696 #else
697 reinterpret_cast<const RDMResponse*>(NULL)));
698 #endif // __FreeBSD__
699
700 // now confirm that if one of the ports fails to send, we see this response
701 request = new ola::rdm::RDMGetRequest(
702 source_uid,
703 vendorcast_uid,
704 0, // transaction #
705 1, // port id
706 10, // sub device
707 296, // param id
708 NULL,
709 0);
710
711 port2.SetRDMHandler(
712 NewCallback(this,
713 &UniverseTest::ReturnRDMCode,
714 ola::rdm::RDM_FAILED_TO_SEND));
715
716 universe->SendRDMRequest(
717 request,
718 NewSingleCallback(this,
719 &UniverseTest::ConfirmRDM,
720 __LINE__,
721 ola::rdm::RDM_FAILED_TO_SEND,
722 #ifdef __FreeBSD__
723 reinterpret_cast<const RDMResponse*>(0)));
724 #else
725 reinterpret_cast<const RDMResponse*>(NULL)));
726 #endif // __FreeBSD__
727
728 // DUB responses are slightly different
729 request = NewDiscoveryUniqueBranchRequest(source_uid, uid1, uid2, 0);
730
731 port1.SetRDMHandler(
732 NewCallback(this,
733 &UniverseTest::ReturnRDMCode,
734 ola::rdm::RDM_DUB_RESPONSE));
735 port2.SetRDMHandler(
736 NewCallback(this,
737 &UniverseTest::ReturnRDMCode,
738 ola::rdm::RDM_DUB_RESPONSE));
739
740 universe->SendRDMRequest(
741 request,
742 NewSingleCallback(this,
743 &UniverseTest::ConfirmRDM,
744 __LINE__,
745 ola::rdm::RDM_DUB_RESPONSE,
746 #ifdef __FreeBSD__
747 reinterpret_cast<const RDMResponse*>(0)));
748 #else
749 reinterpret_cast<const RDMResponse*>(NULL)));
750 #endif // __FreeBSD__
751
752 // now check that we still get a RDM_DUB_RESPONSE even if one port returns an
753 // RDM_TIMEOUT
754 request = NewDiscoveryUniqueBranchRequest(source_uid, uid1, uid2, 0);
755
756 port2.SetRDMHandler(
757 NewCallback(this,
758 &UniverseTest::ReturnRDMCode,
759 ola::rdm::RDM_TIMEOUT));
760
761 universe->SendRDMRequest(
762 request,
763 NewSingleCallback(this,
764 &UniverseTest::ConfirmRDM,
765 __LINE__,
766 ola::rdm::RDM_DUB_RESPONSE,
767 #ifdef __FreeBSD__
768 reinterpret_cast<const RDMResponse*>(0)));
769 #else
770 reinterpret_cast<const RDMResponse*>(NULL)));
771 #endif // __FreeBSD__
772
773 // and the same again but the second port returns
774 // RDM_PLUGIN_DISCOVERY_NOT_SUPPORTED
775 request = NewDiscoveryUniqueBranchRequest(source_uid, uid1, uid2, 0);
776
777 port2.SetRDMHandler(
778 NewCallback(this,
779 &UniverseTest::ReturnRDMCode,
780 ola::rdm::RDM_PLUGIN_DISCOVERY_NOT_SUPPORTED));
781
782 universe->SendRDMRequest(
783 request,
784 NewSingleCallback(this,
785 &UniverseTest::ConfirmRDM,
786 __LINE__,
787 ola::rdm::RDM_DUB_RESPONSE,
788 #ifdef __FreeBSD__
789 reinterpret_cast<const RDMResponse*>(0)));
790 #else
791 reinterpret_cast<const RDMResponse*>(NULL)));
792 #endif // __FreeBSD__
793
794 // now the first port returns a RDM_TIMEOUT
795 request = NewDiscoveryUniqueBranchRequest(source_uid, uid1, uid2, 0);
796
797 port1.SetRDMHandler(
798 NewCallback(this,
799 &UniverseTest::ReturnRDMCode,
800 ola::rdm::RDM_TIMEOUT));
801
802 universe->SendRDMRequest(
803 request,
804 NewSingleCallback(this,
805 &UniverseTest::ConfirmRDM,
806 __LINE__,
807 ola::rdm::RDM_TIMEOUT,
808 #ifdef __FreeBSD__
809 reinterpret_cast<const RDMResponse*>(0)));
810 #else
811 reinterpret_cast<const RDMResponse*>(NULL)));
812 #endif // __FreeBSD__
813
814 // finally if neither ports support the DUB, we should return that
815 request = NewDiscoveryUniqueBranchRequest(source_uid, uid1, uid2, 0);
816
817 port1.SetRDMHandler(
818 NewCallback(this,
819 &UniverseTest::ReturnRDMCode,
820 ola::rdm::RDM_PLUGIN_DISCOVERY_NOT_SUPPORTED));
821
822 universe->SendRDMRequest(
823 request,
824 NewSingleCallback(this,
825 &UniverseTest::ConfirmRDM,
826 __LINE__,
827 ola::rdm::RDM_PLUGIN_DISCOVERY_NOT_SUPPORTED,
828 #ifdef __FreeBSD__
829 reinterpret_cast<const RDMResponse*>(0)));
830 #else
831 reinterpret_cast<const RDMResponse*>(NULL)));
832 #endif // __FreeBSD__
833
834 universe->RemovePort(&port1);
835 universe->RemovePort(&port2);
836 }
837
838
839 /**
840 * Check we got the uids we expect
841 */
842 void UniverseTest::ConfirmUIDs(UIDSet *expected, const UIDSet &uids) {
843 OLA_ASSERT_EQ(*expected, uids);
844 }
845
846
847 /**
848 * Confirm an RDM response
849 */
850 void UniverseTest::ConfirmRDM(int line,
851 RDMStatusCode expected_status_code,
852 const RDMResponse *expected_response,
853 RDMReply *reply) {
854 std::ostringstream str;
855 str << "Line " << line;
856 OLA_ASSERT_EQ_MSG(expected_status_code,
857 reply->StatusCode(),
858 str.str());
859 OLA_ASSERT_EQ_MSG(expected_response, reply->Response(), str.str());
860 }