• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OpenLightingProject / ola / 6370547479

01 Oct 2023 12:26PM UTC coverage: 45.025% (-0.02%) from 45.048%
6370547479

push

github

web-flow
Merge pull request #1898 from peternewman/mac-type-tests

Add the ability to set a universe to blackout instead via ola_set_dmx

7603 of 17646 branches covered (0.0%)

13 of 13 new or added lines in 1 file covered. (100.0%)

21415 of 47562 relevant lines covered (45.03%)

55.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/examples/ola-client.cpp
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
 * ola-client.cpp
17
 * The multi purpose ola client.
18
 * Copyright (C) 2005 Simon Newton
19
 */
20

21
#include <errno.h>
22
#include <getopt.h>
23
#include <ola/DmxBuffer.h>
24
#include <ola/Logging.h>
25
#include <ola/base/Init.h>
26
#include <ola/base/SysExits.h>
27
#include <ola/client/ClientWrapper.h>
28
#include <ola/client/OlaClient.h>
29
#include <ola/file/Util.h>
30
#include <ola/io/SelectServer.h>
31
#include <olad/PortConstants.h>
32

33

34
#include <iostream>
35
#include <iomanip>
36
#include <string>
37
#include <vector>
38

39
using ola::NewSingleCallback;
40
using ola::client::OlaClient;
41
using ola::client::OlaClientWrapper;
42
using ola::client::OlaDevice;
43
using ola::client::OlaInputPort;
44
using ola::client::OlaOutputPort;
45
using ola::client::OlaPlugin;
46
using ola::client::OlaUniverse;
47
using ola::client::Result;
48
using ola::io::SelectServer;
49
using std::cerr;
50
using std::cout;
51
using std::endl;
52
using std::setw;
53
using std::string;
54
using std::vector;
55

56
static const int INVALID_VALUE = -1;
57

58
/*
59
 * The mode is determined by the name in which we were called
60
 */
61
typedef enum {
62
  DEVICE_INFO,
63
  DEVICE_PATCH,
64
  PLUGIN_INFO,
65
  PLUGIN_STATE,
66
  UNIVERSE_INFO,
67
  UNIVERSE_NAME,
68
  UNI_MERGE,
69
  SET_DMX,
70
  SET_PORT_PRIORITY,
71
} mode;
72

73

74
typedef struct {
75
  mode m;          // mode
76
  int uni;         // universe id
77
  unsigned int plugin_id;   // plugin id
78
  bool help;       // show the help
79
  int device_id;   // device id
80
  int port_id;     // port id
81
  ola::client::PortDirection port_direction;  // input or output
82
  ola::client::PatchAction patch_action;      // patch or unpatch
83
  OlaUniverse::merge_mode merge_mode;  // the merge mode
84
  string cmd;      // argv[0]
85
  string uni_name;  // universe name
86
  string dmx;      // DMX string
87
  bool blackout;
88
  ola::port_priority_mode priority_mode;  // port priority mode
89
  uint8_t priority_value;  // port priority value
90
  bool list_plugin_ids;
91
  bool list_universe_ids;
92
  string state;      // plugin enable/disable state
93
} options;
×
94

95

96
/**
97
 * A Helper function to display a list of ports
98
 */
99
template<class PortClass>
100
void ListPorts(const vector<PortClass> &ports, bool input) {
×
101
  typename vector<PortClass>::const_iterator port_iter;
×
102
  for (port_iter = ports.begin(); port_iter != ports.end(); ++port_iter) {
×
103
    cout << "  port " << port_iter->Id() << ", ";
×
104

105
    if (input) {
×
106
      cout << "IN";
×
107
    } else {
108
      cout << "OUT";
×
109
    }
110

111
    if (!port_iter->Description().empty()) {
×
112
      cout << " " << port_iter->Description();
×
113
    }
114

115
    switch (port_iter->PriorityCapability()) {
×
116
      case ola::CAPABILITY_STATIC:
×
117
        cout << ", priority " << static_cast<int>(port_iter->Priority());
×
118
        break;
119
      case ola::CAPABILITY_FULL:
×
120
        cout << ", priority ";
×
121
        if (port_iter->PriorityMode() == ola::PRIORITY_MODE_INHERIT) {
×
122
          cout << "inherited";
×
123
        } else {
124
          cout << "override " << static_cast<int>(port_iter->Priority());
×
125
        }
126
        break;
127
      default:
128
        break;
129
    }
130

131
    if (port_iter->IsActive()) {
×
132
      cout << ", patched to universe " << port_iter->Universe();
×
133
    }
134

135
    if (port_iter->SupportsRDM()) {
×
136
      cout << ", RDM supported";
×
137
    }
138
    cout << endl;
×
139
  }
140
}
×
141

142

143
/*
144
 * This is called when we receive universe results from the client
145
 * @param list_ids_only show ids only
146
 * @param universes a vector of OlaUniverses
147
 */
148
void DisplayUniverses(SelectServer *ss,
×
149
                      bool list_ids_only,
150
                      const Result &result,
151
                      const vector <OlaUniverse> &universes) {
152
  vector<OlaUniverse>::const_iterator iter;
×
153

154
  if (!result.Success()) {
×
155
    cerr << result.Error() << endl;
×
156
    ss->Terminate();
×
157
    return;
×
158
  }
159

160
  if (list_ids_only) {
×
161
    for (iter = universes.begin(); iter != universes.end(); ++iter) {
×
162
      cout << iter->Id() << endl;
×
163
    }
164
  } else {
165
    cout << setw(5) << "Id" << "\t" << setw(30) << "Name" << "\t\tMerge Mode"
×
166
         << endl;
×
167
    cout << "----------------------------------------------------------"
×
168
         << endl;
×
169

170
    for (iter = universes.begin(); iter != universes.end(); ++iter) {
×
171
      cout << setw(5) << iter->Id() << "\t" << setw(30) << iter->Name()
×
172
           << "\t\t"
173
           << (iter->MergeMode() == OlaUniverse::MERGE_HTP ? "HTP" : "LTP")
×
174
           << endl;
×
175
    }
176

177
    cout << "----------------------------------------------------------" <<
×
178
      endl;
×
179
  }
180

181
  ss->Terminate();
×
182
}
183

184

185
/*
186
 * @param list_ids_only show ids only
187
 * @params plugins a vector of OlaPlugins
188
 */
189
void DisplayPlugins(SelectServer *ss,
×
190
                    bool list_ids_only,
191
                    const Result &result,
192
                    const vector <OlaPlugin> &plugins) {
193
  vector<OlaPlugin>::const_iterator iter;
×
194

195
  if (!result.Success()) {
×
196
    cerr << result.Error() << endl;
×
197
    ss->Terminate();
×
198
    return;
×
199
  }
200

201
  if (list_ids_only) {
×
202
    for (iter = plugins.begin(); iter != plugins.end(); ++iter) {
×
203
      cout << iter->Id() << endl;
×
204
    }
205
  } else {
206
    cout << setw(5) << "Id" << "\tPlugin Name" << endl;
×
207
    cout << "--------------------------------------" << endl;
×
208

209
    for (iter = plugins.begin(); iter != plugins.end(); ++iter) {
×
210
      cout << setw(5) << iter->Id() << "\t" << iter->Name() << endl;
×
211
    }
212

213
    cout << "--------------------------------------" << endl;
×
214
  }
215

216
  ss->Terminate();
×
217
}
218

219

220
/*
221
 * Print a plugin description
222
 */
223
void DisplayPluginDescription(SelectServer *ss,
×
224
                              const Result &result,
225
                              const string &description) {
226
  if (!result.Success()) {
×
227
    cerr << result.Error() << endl;
×
228
  } else {
229
    cout << description << endl;
×
230
  }
231
  ss->Terminate();
×
232
  return;
×
233
}
234

235

236
/*
237
 * Print a plugin state
238
 */
239
void DisplayPluginState(SelectServer *ss,
×
240
                        const Result &result,
241
                        const ola::client::PluginState &state) {
242
  if (!result.Success()) {
×
243
    cerr << result.Error() << endl;
×
244
  } else {
245
    cout << state.name << endl;
×
246
    cout << "Enabled: " << (state.enabled ? "True" : "False") << endl;
×
247
    cout << "Active: " << (state.active ? "True" : "False") << endl;
×
248
    vector<OlaPlugin>::const_iterator iter = state.conflicting_plugins.begin();
×
249
    cout << "Conflicts with:" << endl;
×
250
    for (; iter != state.conflicting_plugins.end(); ++iter) {
×
251
      cout << "  " << iter->Name() << "(" << iter->Id() << ")" << endl;
×
252
    }
253
  }
254
  ss->Terminate();
×
255
  return;
×
256
}
257

258

259
/*
260
 * @param devices a vector of OlaDevices
261
 */
262
void DisplayDevices(SelectServer *ss,
×
263
                    const Result &result,
264
                    const vector <OlaDevice> &devices) {
265
  vector<OlaDevice>::const_iterator iter;
×
266

267
  if (!result.Success()) {
×
268
    cerr << result.Error() << endl;
×
269
    ss->Terminate();
×
270
    return;
×
271
  }
272

273
  for (iter = devices.begin(); iter != devices.end(); ++iter) {
×
274
    cout << "Device " << iter->Alias() << ": " << iter->Name() << endl;
×
275
    vector<OlaInputPort> input_ports = iter->InputPorts();
×
276
    ListPorts(input_ports, true);
×
277
    vector<OlaOutputPort> output_ports = iter->OutputPorts();
×
278
    ListPorts(output_ports, false);
×
279
  }
×
280
  ss->Terminate();
×
281
}
282

283
/*
284
 * Called when a generic set command completes
285
 */
286
void HandleAck(SelectServer *ss, const Result &result) {
×
287
  if (!result.Success()) {
×
288
    cerr << result.Error() << endl;
×
289
  }
290
  ss->Terminate();
×
291
}
×
292

293
/*
294
 * Init options
295
 */
296
void InitOptions(options *opts) {
×
297
  opts->m = DEVICE_INFO;
×
298
  opts->uni = INVALID_VALUE;
×
299
  opts->plugin_id = ola::OLA_PLUGIN_ALL;
×
300
  opts->help = false;
×
301
  opts->list_plugin_ids = false;
×
302
  opts->list_universe_ids = false;
×
303
  opts->patch_action = ola::client::PATCH;
×
304
  opts->port_id = INVALID_VALUE;
×
305
  opts->port_direction = ola::client::OUTPUT_PORT;
×
306
  opts->device_id = INVALID_VALUE;
×
307
  opts->merge_mode = OlaUniverse::MERGE_HTP;
×
308
  opts->blackout = false;
×
309
  opts->priority_mode = ola::PRIORITY_MODE_INHERIT;
×
310
  opts->priority_value = 0;
×
311
}
×
312

313

314
/*
315
 * Decide what mode we're running in
316
 */
317
void SetMode(options *opts) {
×
318
  string cmd_name = ola::file::FilenameFromPathOrPath(opts->cmd);
×
319
  // To skip the lt prefix during development
320
  ola::StripPrefix(&cmd_name, "lt-");
×
321
#ifdef _WIN32
322
  // Strip the extension
323
  size_t extension = cmd_name.find(".");
324
  if (extension != string::npos) {
325
    cmd_name = cmd_name.substr(0, extension);
326
  }
327
#endif  // _WIN32
328

329
  if (cmd_name == "ola_plugin_info") {
×
330
    opts->m = PLUGIN_INFO;
×
331
  } else if (cmd_name == "ola_plugin_state") {
×
332
    opts->m = PLUGIN_STATE;
×
333
  } else if (cmd_name == "ola_patch") {
×
334
    opts->m = DEVICE_PATCH;
×
335
  } else if (cmd_name == "ola_ptch") {
×
336
    // Working around Windows UAC
337
    opts->m = DEVICE_PATCH;
×
338
  } else if (cmd_name == "ola_uni_info") {
×
339
    opts->m = UNIVERSE_INFO;
×
340
  } else if (cmd_name == "ola_uni_name") {
×
341
    opts->m = UNIVERSE_NAME;
×
342
  } else if (cmd_name == "ola_uni_merge") {
×
343
    opts->m = UNI_MERGE;
×
344
  } else if (cmd_name == "ola_set_dmx") {
×
345
    opts->m = SET_DMX;
×
346
  } else if (cmd_name == "ola_set_priority") {
×
347
    opts->m = SET_PORT_PRIORITY;
×
348
  }
349
}
×
350

351

352
/*
353
 * parse our cmd line options
354
 */
355
void ParseOptions(int argc, char *argv[], options *opts) {
×
356
  enum {
×
357
    LIST_PLUGIN_IDS_OPTION = 256,
358
    LIST_UNIVERSE_IDS_OPTION,
359
  };
360

361
  static struct option long_options[] = {
×
362
      {"dmx", required_argument, 0, 'd'},
363
      {"blackout", no_argument, 0, 'b'},
364
      {"help", no_argument, 0, 'h'},
365
      {"ltp", no_argument, 0, 'l'},
366
      {"name", required_argument, 0, 'n'},
367
      {"plugin-id", required_argument, 0, 'p'},
368
      {"state", required_argument, 0, 's'},
369
      {"list-plugin-ids", no_argument, 0, LIST_PLUGIN_IDS_OPTION},
370
      {"list-universe-ids", no_argument, 0, LIST_UNIVERSE_IDS_OPTION},
371
      {"universe", required_argument, 0, 'u'},
372
      {0, 0, 0, 0}
373
    };
374

375
  int c;
×
376
  int option_index = 0;
×
377

378
  while (1) {
×
379
    c = getopt_long(argc, argv, "ld:bn:u:p:s:hv", long_options, &option_index);
×
380

381
    if (c == -1)
×
382
      break;
383

384
    switch (c) {
×
385
      case 0:
386
        break;
387
      case 'd':
×
388
        opts->dmx = optarg;
×
389
        break;
390
      case 'b':
×
391
        opts->blackout = true;
×
392
        break;
×
393
      case 'h':
×
394
        opts->help = true;
×
395
        break;
×
396
      case 'l':
×
397
        opts->merge_mode = OlaUniverse::MERGE_LTP;
×
398
        break;
×
399
      case 'n':
×
400
        opts->uni_name = optarg;
×
401
        break;
402
      case 'p':
×
403
        opts->plugin_id = atoi(optarg);
×
404
        break;
×
405
      case 's':
×
406
        opts->state = optarg;
×
407
        break;
408
      case 'u':
×
409
        opts->uni = atoi(optarg);
×
410
        break;
×
411
      case LIST_PLUGIN_IDS_OPTION:
×
412
        opts->list_plugin_ids = true;
×
413
        break;
×
414
      case LIST_UNIVERSE_IDS_OPTION:
×
415
        opts->list_universe_ids = true;
×
416
        break;
×
417
      case '?':
418
        break;
419
      default:
420
        break;
421
    }
422
  }
423
}
×
424

425

426
/*
427
 * parse our cmd line options for the patch command
428
 */
429
int ParsePatchOptions(int argc, char *argv[], options *opts) {
×
430
  static struct option long_options[] = {
×
431
      {"device", required_argument, 0, 'd'},
432
      {"help", no_argument, 0, 'h'},
433
      {"input", no_argument, 0, 'i'},
434
      {"patch", no_argument, 0, 'a'},
435
      {"port", required_argument, 0, 'p'},
436
      {"universe", required_argument, 0, 'u'},
437
      {"unpatch", no_argument, 0, 'r'},
438
      {0, 0, 0, 0}
439
    };
440

441
  int c;
×
442
  int option_index = 0;
×
443

444
  while (1) {
×
445
    c = getopt_long(argc, argv, "ard:p:u:hi", long_options, &option_index);
×
446

447
    if (c == -1) {
×
448
      break;
449
    }
450

451
    switch (c) {
×
452
      case 0:
453
        break;
454
      case 'a':
×
455
        opts->patch_action = ola::client::PATCH;
×
456
        break;
×
457
      case 'd':
×
458
        opts->device_id = atoi(optarg);
×
459
        break;
×
460
      case 'p':
×
461
        opts->port_id = atoi(optarg);
×
462
        break;
×
463
      case 'r':
×
464
        opts->patch_action = ola::client::UNPATCH;
×
465
        break;
×
466
      case 'u':
×
467
        opts->uni = atoi(optarg);
×
468
        break;
×
469
      case 'h':
×
470
        opts->help = true;
×
471
        break;
×
472
      case 'i':
×
473
        opts->port_direction = ola::client::INPUT_PORT;
×
474
        break;
×
475
      case '?':
476
        break;
477
      default:
478
        break;
479
    }
480
  }
481
  return 0;
×
482
}
483

484

485
/*
486
 * parse our cmd line options for the set priority command
487
 */
488
int ParseSetPriorityOptions(int argc, char *argv[], options *opts) {
×
489
  static struct option long_options[] = {
×
490
      {"device", required_argument, 0, 'd'},
491
      {"help", no_argument, 0, 'h'},
492
      {"input", no_argument, 0, 'i'},
493
      {"port", required_argument, 0, 'p'},
494
      {"override", required_argument, 0, 'o'},
495
      {0, 0, 0, 0}
496
    };
497

498
  int c;
×
499
  int option_index = 0;
×
500

501
  while (1) {
×
502
    c = getopt_long(argc, argv, "d:p:o:hi", long_options, &option_index);
×
503

504
    if (c == -1) {
×
505
      break;
506
    }
507

508
    switch (c) {
×
509
      case 0:
510
        break;
511
      case 'd':
×
512
        opts->device_id = atoi(optarg);
×
513
        break;
×
514
      case 'h':
×
515
        opts->help = true;
×
516
        break;
×
517
      case 'i':
×
518
        opts->port_direction = ola::client::INPUT_PORT;
×
519
        break;
×
520
      case 'o':
×
521
        opts->priority_mode = ola::PRIORITY_MODE_STATIC;
×
522
        opts->priority_value = atoi(optarg);
×
523
        break;
×
524
      case 'p':
×
525
        opts->port_id = atoi(optarg);
×
526
        break;
×
527
      case '?':
528
        break;
529
      default:
530
        break;
531
    }
532
  }
533
  return 0;
×
534
}
535

536

537
/*
538
 * help message for device info
539
 */
540
void DisplayDeviceInfoHelp(const options &opts) {
×
541
  cout << "Usage: " << opts.cmd << " [--plugin-id <plugin_id>]\n"
×
542
          "\n"
543
          "Show information on the devices loaded by olad.\n"
544
          "\n"
545
          "  -h, --help                  Display this help message and exit.\n"
546
          "  -p, --plugin-id <plugin-id> Show only devices owned by this "
547
          "plugin.\n"
×
548
       << endl;
×
549
}
×
550

551

552
/*
553
 * Display the Patch help
554
 */
555
void DisplayPatchHelp(const options &opts) {
×
556
  cout << "Usage: " << opts.cmd
×
557
       << " [--patch | --unpatch] --device <dev> --port <port> "
558
          "[--universe <uni>]\n"
559
          "\n"
560
          "Control ola port <-> universe mappings.\n"
561
          "\n"
562
          "  -a, --patch              Patch this port (default).\n"
563
          "  -d, --device <device>    Id of device to patch.\n"
564
          "  -h, --help               Display this help message and exit.\n"
565
          "  -p, --port <port>        Id of the port to patch.\n"
566
          "  -r, --unpatch            Unpatch this port.\n"
567
          "  -i, --input              Patch the input port (default is "
568
          "output).\n"
569
          "  -u, --universe <uni>     Id of the universe to patch to (default "
570
          "0).\n"
×
571
       << endl;
×
572
}
×
573

574

575
/*
576
 * help message for plugin info
577
 */
578
void DisplayPluginInfoHelp(const options &opts) {
×
579
  cout << "Usage: " << opts.cmd << " [--plugin-id <plugin-id>]\n"
×
580
          "\n"
581
          "Get info on the plugins loaded by olad. Called without arguments"
582
          " this will\n"
583
          "display the plugins loaded by olad. When used with --plugin-id this"
584
          " will\n"
585
          "display the specified plugin's description.\n"
586
          "\n"
587
          "  -h, --help                  Display this help message and exit.\n"
588
          "  -p, --plugin-id <plugin_id> Id of the plugin to fetch the "
589
          "description of\n"
590
          "  --list-plugin-ids           List plugin Ids only.\n"
×
591
       << endl;
×
592
}
×
593

594

595
/*
596
 * help message for plugin state
597
 */
598
void DisplayPluginStateHelp(const options &opts) {
×
599
  cout << "Usage: " << opts.cmd
×
600
       << " --plugin-id <plugin-id> [--state <enable|disable>]\n"
601
          "\n"
602
          "Displays the enabled/disabled state for a plugin and the list of "
603
          "plugins\n"
604
          "this plugin will conflict with.\n"
605
          "\n"
606
          "  -h, --help                  Display this help message and exit.\n"
607
          "  -p, --plugin-id <plugin-id> Id of the plugin to fetch the state "
608
          "of\n"
609
          "  -s, --state <enable|disable> State to set a plugin to\n"
×
610
      << endl;
×
611
}
×
612

613

614
/*
615
 * help message for uni info
616
 */
617
void DisplayUniverseInfoHelp(const options &opts) {
×
618
  cout << "Usage: " << opts.cmd
×
619
       << "\n"
620
          "Shows info on the active universes in use.\n"
621
          "\n"
622
          "  -h, --help          Display this help message and exit.\n"
623
          "  --list-universe-ids List universe Ids only.\n"
×
624
       << endl;
×
625
}
×
626

627

628
/*
629
 * Help message for set uni name
630
 */
631
void DisplayUniverseNameHelp(const options &opts) {
×
632
  cout << "Usage: " << opts.cmd << " --name <name> --universe <uni>\n"
×
633
          "\n"
634
          "Set a name for the specified universe\n"
635
          "\n"
636
          "  -h, --help                Display this help message and exit.\n"
637
          "  -n, --name <name>         Name for the universe.\n"
638
          "  -u, --universe <universe> Id of the universe to name.\n"
×
639
       << endl;
×
640
}
×
641

642

643
/*
644
 * Help message for set uni merge mode
645
 */
646
void DisplayUniverseMergeHelp(const options &opts) {
×
647
  cout << "Usage: " << opts.cmd << " --universe <uni> [--ltp]\n"
×
648
          "\n"
649
          "Change the merge mode for the specified universe. Without --ltp "
650
          "it will\n"
651
          "revert to HTP mode.\n"
652
          "\n"
653
          "  -h, --help                Display this help message and exit.\n"
654
          "  -l, --ltp                 Change to LTP mode.\n"
655
          "  -u, --universe <universe> Id of the universe to change.\n"
×
656
       << endl;
×
657
}
×
658

659

660

661
/*
662
 * Help message for set dmx
663
 */
664
void DisplaySetDmxHelp(const options &opts) {
×
665
  cout << "Usage: " << opts.cmd << " --universe <universe> [ --dmx <values> ] "
×
666
          "[ --blackout ]\n"
667
          "\n"
668
          "Sets the DMX values for a universe.\n"
669
          "\n"
670
          "  -h, --help                Display this help message and exit.\n"
671
          "  -u, --universe <universe> Universe number, e.g. 0.\n"
672
          "  -d, --dmx <values>        Comma separated DMX values, e.g. "
673
          "0,255,128 sets first channel to 0, second channel to 255"
674
          " and third channel to 128.\n"
675
          "  -b, --blackout            Send a universe to blackout instead.\n"
×
676
       << endl;
×
677
}
×
678

679
/*
680
 * Display the Patch help
681
 */
682
void DisplaySetPriorityHelp(const options &opts) {
×
683
  cout << "Usage: " << opts.cmd
×
684
       << " --device <dev> --port <port> [--override <value>]\n"
685
          "\n"
686
          "Set a port's priority, without the --override flag this will set "
687
          "the port\n"
688
          " to inherit mode.\n"
689
          "\n"
690
          "  -d, --device <device>    Id of device to set priority for.\n"
691
          "  -h, --help               Display this help message and exit.\n"
692
          "  -i, --input              Set an input port\n"
693
          "  -o, --override <value>   Set the port priority to a static "
694
          "value.\n"
695
          "  -p, --port <port>        Id of the port to set priority for.\n"
×
696
       << endl;
×
697
}
×
698

699

700

701
/*
702
 * Display the help message
703
 */
704
void DisplayHelpAndExit(const options &opts) {
×
705
  switch (opts.m) {
×
706
    case DEVICE_INFO:
×
707
      DisplayDeviceInfoHelp(opts);
×
708
      break;
×
709
    case DEVICE_PATCH:
×
710
      DisplayPatchHelp(opts);
×
711
      break;
×
712
    case PLUGIN_INFO:
×
713
      DisplayPluginInfoHelp(opts);
×
714
      break;
×
715
    case PLUGIN_STATE:
×
716
      DisplayPluginStateHelp(opts);
×
717
      break;
×
718
    case UNIVERSE_INFO:
×
719
      DisplayUniverseInfoHelp(opts);
×
720
      break;
×
721
    case UNIVERSE_NAME:
×
722
      DisplayUniverseNameHelp(opts);
×
723
      break;
×
724
    case UNI_MERGE:
×
725
      DisplayUniverseMergeHelp(opts);
×
726
      break;
×
727
    case SET_DMX:
×
728
      DisplaySetDmxHelp(opts);
×
729
      break;
×
730
    case SET_PORT_PRIORITY:
×
731
      DisplaySetPriorityHelp(opts);
×
732
  }
733
  exit(0);
×
734
}
735

736

737
/*
738
 * Send a fetch device info request
739
 * @param client  the ola client
740
 * @param opts  the const options
741
 */
742
int FetchDeviceInfo(OlaClientWrapper *wrapper, const options &opts) {
×
743
  SelectServer *ss = wrapper->GetSelectServer();
×
744
  OlaClient *client = wrapper->GetClient();
×
745
  client->FetchDeviceInfo((ola::ola_plugin_id) opts.plugin_id,
×
746
                          NewSingleCallback(&DisplayDevices, ss));
747
  return 0;
×
748
}
749

750

751
void Patch(OlaClientWrapper *wrapper, const options &opts) {
×
752
  SelectServer *ss = wrapper->GetSelectServer();
×
753
  OlaClient *client = wrapper->GetClient();
×
754
  if (opts.device_id == INVALID_VALUE || opts.port_id == INVALID_VALUE) {
×
755
    DisplayPatchHelp(opts);
×
756
    exit(1);
×
757
  }
758

759
  if (opts.patch_action == ola::client::PATCH  && opts.uni == INVALID_VALUE) {
×
760
    DisplayPatchHelp(opts);
×
761
    exit(1);
×
762
  }
763
  client->Patch(opts.device_id,
×
764
                opts.port_id,
×
765
                opts.port_direction,
×
766
                opts.patch_action, opts.uni,
×
767
                NewSingleCallback(&HandleAck, ss));
768
}
×
769

770

771
/*
772
 * Fetch information on plugins.
773
 */
774
int FetchPluginInfo(OlaClientWrapper *wrapper, const options &opts) {
×
775
  SelectServer *ss = wrapper->GetSelectServer();
×
776
  OlaClient *client = wrapper->GetClient();
×
777
  if (opts.plugin_id > 0) {
×
778
    client->FetchPluginDescription(
×
779
        (ola::ola_plugin_id) opts.plugin_id,
×
780
        NewSingleCallback(&DisplayPluginDescription, ss));
781
  } else {
782
    client->FetchPluginList(
×
783
        NewSingleCallback(&DisplayPlugins, ss, opts.list_plugin_ids));
×
784
  }
785
  return 0;
×
786
}
787

788

789
/*
790
 * Fetch the state of a plugin.
791
 */
792
int FetchPluginState(OlaClientWrapper *wrapper, const options &opts) {
×
793
  SelectServer *ss = wrapper->GetSelectServer();
×
794
  OlaClient *client = wrapper->GetClient();
×
795
  if (opts.plugin_id == 0) {
×
796
    DisplayPluginStateHelp(opts);
×
797
    exit(1);
×
798
  }
799
  if (!opts.state.empty()) {
×
800
    bool state;
×
801
    if (ola::StringToBoolTolerant(opts.state, &state)) {
×
802
      cout << "Setting state to " << (state ? "enabled" : "disabled") << endl;
×
803
      client->SetPluginState(
×
804
          (ola::ola_plugin_id) opts.plugin_id,
×
805
          state,
806
          NewSingleCallback(&HandleAck, ss));
807
    } else {
808
      cerr << "Invalid state: " << opts.state << endl;
×
809
      DisplayPluginStateHelp(opts);
×
810
      exit(1);
×
811
    }
812
  } else {
813
    client->FetchPluginState((ola::ola_plugin_id) opts.plugin_id,
×
814
                             NewSingleCallback(&DisplayPluginState, ss));
815
  }
816
  return 0;
×
817
}
818

819

820

821
/*
822
 * send a set name request
823
 * @param client the ola client
824
 * @param opts  the const options
825
 */
826
int SetUniverseName(OlaClientWrapper *wrapper, const options &opts) {
×
827
  SelectServer *ss = wrapper->GetSelectServer();
×
828
  OlaClient *client = wrapper->GetClient();
×
829
  if (opts.uni == INVALID_VALUE) {
×
830
    DisplayUniverseNameHelp(opts);
×
831
    exit(1);
×
832
  }
833
  client->SetUniverseName(opts.uni, opts.uni_name,
×
834
                          NewSingleCallback(&HandleAck, ss));
835
  return 0;
×
836
}
837

838

839
/*
840
 * send a set name request
841
 * @param client the ola client
842
 * @param opts  the const options
843
 */
844
int SetUniverseMergeMode(OlaClientWrapper *wrapper,
×
845
                         const options &opts) {
846
  SelectServer *ss = wrapper->GetSelectServer();
×
847
  OlaClient *client = wrapper->GetClient();
×
848
  if (opts.uni == INVALID_VALUE) {
×
849
    DisplayUniverseMergeHelp(opts);
×
850
    exit(1);
×
851
  }
852
  client->SetUniverseMergeMode(
×
853
      opts.uni, opts.merge_mode,
×
854
      NewSingleCallback(&HandleAck, ss));
855
  return 0;
×
856
}
857

858

859
/*
860
 * Send a DMX message
861
 * @param client the ola client
862
 * @param opts the options
863
 */
864
int SendDmx(OlaClientWrapper *wrapper, const options &opts) {
×
865
  SelectServer *ss = wrapper->GetSelectServer();
×
866
  OlaClient *client = wrapper->GetClient();
×
867
  ola::DmxBuffer buffer;
×
868
  bool status = false;
×
869
  if (opts.blackout) {
×
870
    status = buffer.Blackout();
×
871
  } else {
872
    status = buffer.SetFromString(opts.dmx);
×
873
  }
874

875
  // A dmx string and blackout are mutually exclusive
876
  if (opts.uni < 0 || !status || (opts.blackout && !opts.dmx.empty()) ||
×
877
      buffer.Size() == 0) {
×
878
    DisplaySetDmxHelp(opts);
×
879
    exit(1);
×
880
  }
881

882
  ola::client::SendDMXArgs args(NewSingleCallback(&HandleAck, ss));
×
883
  client->SendDMX(opts.uni, buffer, args);
×
884
  return 0;
×
885
}
×
886

887

888
/*
889
 * Set the priority of a port
890
 */
891
void SetPortPriority(OlaClientWrapper *wrapper, const options &opts) {
×
892
  SelectServer *ss = wrapper->GetSelectServer();
×
893
  OlaClient *client = wrapper->GetClient();
×
894

895
  if (opts.device_id == INVALID_VALUE || opts.port_id == INVALID_VALUE) {
×
896
    DisplaySetPriorityHelp(opts);
×
897
    exit(1);
×
898
  }
899

900
  if (opts.priority_mode == ola::PRIORITY_MODE_INHERIT) {
×
901
    client->SetPortPriorityInherit(
×
902
        opts.device_id, opts.port_id, opts.port_direction,
×
903
        NewSingleCallback(&HandleAck, ss));
904
  } else if (opts.priority_mode == ola::PRIORITY_MODE_STATIC) {
×
905
    client->SetPortPriorityOverride(
×
906
        opts.device_id, opts.port_id, opts.port_direction, opts.priority_value,
×
907
        NewSingleCallback(&HandleAck, ss));
908
  } else {
909
    DisplaySetPriorityHelp(opts);
×
910
  }
911
}
×
912

913

914
/*
915
 * Main
916
 */
917
int main(int argc, char *argv[]) {
×
918
  ola::InitLogging(ola::OLA_LOG_WARN, ola::OLA_LOG_STDERR);
×
919
  if (!ola::NetworkInit()) {
×
920
    OLA_WARN << "Network initialization failed." << endl;
×
921
    exit(ola::EXIT_UNAVAILABLE);
×
922
  }
923
  OlaClientWrapper ola_client;
×
924
  options opts;
×
925

926
  InitOptions(&opts);
×
927
  opts.cmd = argv[0];
×
928

929
  // decide how we should behave
930
  SetMode(&opts);
×
931

932
  if (opts.m == DEVICE_PATCH) {
×
933
    ParsePatchOptions(argc, argv, &opts);
×
934
  } else if (opts.m == SET_PORT_PRIORITY) {
×
935
    ParseSetPriorityOptions(argc, argv, &opts);
×
936
  } else {
937
    ParseOptions(argc, argv, &opts);
×
938
  }
939

940
  if (opts.help) {
×
941
    DisplayHelpAndExit(opts);
×
942
  }
943

944
  if (!ola_client.Setup()) {
×
945
    OLA_FATAL << "Setup failed";
×
946
    exit(1);
×
947
  }
948

949
  switch (opts.m) {
×
950
    case DEVICE_INFO:
×
951
      FetchDeviceInfo(&ola_client, opts);
×
952
      break;
953
    case DEVICE_PATCH:
×
954
      Patch(&ola_client, opts);
×
955
      break;
956
    case PLUGIN_INFO:
×
957
      FetchPluginInfo(&ola_client, opts);
×
958
      break;
959
    case PLUGIN_STATE:
×
960
      FetchPluginState(&ola_client, opts);
×
961
      break;
962
    case UNIVERSE_INFO:
×
963
      ola_client.GetClient()->FetchUniverseList(
×
964
          NewSingleCallback(&DisplayUniverses,
965
          ola_client.GetSelectServer(), opts.list_universe_ids));
×
966
      break;
967
    case UNIVERSE_NAME:
×
968
      SetUniverseName(&ola_client, opts);
×
969
      break;
970
    case UNI_MERGE:
×
971
      SetUniverseMergeMode(&ola_client, opts);
×
972
      break;
973
    case SET_DMX:
×
974
      SendDmx(&ola_client, opts);
×
975
      break;
976
    case SET_PORT_PRIORITY:
×
977
      SetPortPriority(&ola_client, opts);
×
978
  }
979

980
  ola_client.GetSelectServer()->Run();
×
981
  return 0;
×
982
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc