-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathRDMHandlers.cpp
792 lines (672 loc) · 25 KB
/
RDMHandlers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* RDMHandlers.cpp
* Copyright (C) 2011 Simon Newton
*/
#include "Common.h"
#include "RDMEnums.h"
#include "RDMHandlers.h"
#include "RDMSender.h"
#include "WidgetSettings.h"
// The list of all pids that we support
const RDMHandler::pid_definition RDMHandler::PID_DEFINITIONS[] = {
{PID_QUEUED_MESSAGE, &RDMHandler::HandleGetQueuedMessage, NULL, 1, true},
{PID_SUPPORTED_PARAMETERS, &RDMHandler::HandleGetSupportedParameters, NULL,
0, false},
{PID_PARAMETER_DESCRIPTION, &RDMHandler::HandleGetParameterDescription, NULL,
2, false},
{PID_DEVICE_INFO, &RDMHandler::HandleGetDeviceInfo, NULL, 0, false},
{PID_PRODUCT_DETAIL_ID_LIST, &RDMHandler::HandleGetProductDetailId, NULL, 0,
true},
{PID_DEVICE_MODEL_DESCRIPTION, &RDMHandler::HandleGetDeviceModelDescription,
NULL, 0, true},
{PID_MANUFACTURER_LABEL, &RDMHandler::HandleGetManufacturerLabel, NULL, 0,
true},
{PID_DEVICE_LABEL, &RDMHandler::HandleGetDeviceLabel,
&RDMHandler::HandleSetDeviceLabel, 0, true},
{PID_LANGUAGE_CAPABILITIES, &RDMHandler::HandleGetLanguage, NULL, 0, true},
{PID_LANGUAGE, &RDMHandler::HandleGetLanguage,
&RDMHandler::HandleSetLanguage, 0, true},
{PID_SOFTWARE_VERSION_LABEL, &RDMHandler::HandleGetSoftwareVersion, NULL, 0,
false},
{PID_DMX_PERSONALITY, &RDMHandler::HandleGetPersonality,
&RDMHandler::HandleSetPersonality, 0, true},
{PID_DMX_PERSONALITY_DESCRIPTION,
&RDMHandler::HandleGetPersonalityDescription, NULL, 1, true},
{PID_DMX_START_ADDRESS, &RDMHandler::HandleGetStartAddress,
&RDMHandler::HandleSetStartAddress, 0, false},
{PID_SENSOR_DEFINITION, &RDMHandler::HandleGetSensorDefinition, NULL, 1,
true},
{PID_SENSOR_VALUE, &RDMHandler::HandleGetSensorValue,
&RDMHandler::HandleSetSensorValue, 1, true},
{PID_RECORD_SENSORS, NULL, &RDMHandler::HandleRecordSensor, 0, true},
{PID_DEVICE_POWER_CYCLES, &RDMHandler::HandleGetDevicePowerCycles,
&RDMHandler::HandleSetDevicePowerCycles, 0, true},
{PID_IDENTIFY_DEVICE, &RDMHandler::HandleGetIdentifyDevice,
&RDMHandler::HandleSetIdentifyDevice, 0, false},
{PID_MANUFACTURER_SET_SERIAL, NULL, &RDMHandler::HandleSetSerial, 4, true},
};
const RDMHandler::rdm_personality RDMHandler::rdm_personalities[] = {
{1, 6, "6x PWM"},
{2, 6, "3x inverted PWM, 3x PWM"},
{3, 6, "6x inverted PWM"},
};
// Various constants used in RDM messages
const char RDMHandler::SUPPORTED_LANGUAGE[] = "en";
const char RDMHandler::SOFTWARE_VERSION_STRING[] = "1.0";
const char RDMHandler::SET_SERIAL_PID_DESCRIPTION[] = "Set Serial Number";
const char RDMHandler::TEMPERATURE_SENSOR_DESCRIPTION[] = "Case Temperature";
/**
* Verify a RDM checksum
* @param message a pointer to an RDM message starting with the SUB_START_CODE
* @param size the size of the message data
* @return true if the checksum is ok, false otherwise
*/
bool RDMHandler::VerifyChecksum(const byte *message, int size) {
// don't checksum the checksum itself (last two bytes)
unsigned int checksum = 0;
for (int i = 0; i < size - 2; i++)
checksum += message[i];
byte checksum_offset = message[2];
return (checksum >> 8 == message[checksum_offset] &&
(checksum & 0xff) == message[checksum_offset + 1]);
}
/**
* Read the value of the temperature sensor.
* @return the temp in degrees C * 10
*/
int RDMHandler::ReadTemperatureSensor() {
// v = input / 1024 * 5 V
// t = 100 * v
// we multiple the result by 10
return 10 * 5.0 * analogRead(TEMP_SENSOR_PIN) * 100.0 / 1024.0;
}
/**
* Send a sensor response, this is used for both PID_SENSOR_VALUE &
* PID_RECORD_SENSORS.
*/
void RDMHandler::SendSensorResponse(const byte *received_message) {
rdm_sender.StartRDMAckResponse(received_message, 9);
rdm_sender.SendByteAndChecksum(received_message[24]);
rdm_sender.SendIntAndChecksum(ReadTemperatureSensor()); // current
rdm_sender.SendIntAndChecksum(0); // lowest
rdm_sender.SendIntAndChecksum(0); // highest
rdm_sender.SendIntAndChecksum(WidgetSettings.SensorValue()); // recorded
rdm_sender.EndRDMResponse();
}
/**
* Send a RDM message with a string as param data. Used for DEVICE_LABEL,
* MANUFACTURER_LABEL, etc.
*/
void RDMHandler::HandleStringRequest(const byte *received_message,
const char *label,
byte label_size) {
rdm_sender.StartRDMResponse(received_message, RDM_RESPONSE_ACK, label_size);
for (unsigned int i = 0; i < label_size; ++i)
rdm_sender.SendByteAndChecksum(label[i]);
rdm_sender.EndRDMResponse();
}
/**
* Handle a GET QUEUED_MESSAGE request
*/
void RDMHandler::HandleGetQueuedMessage(const byte *received_message) {
if (m_device_label_pending) {
rdm_sender.DecrementMessageCount();
m_device_label_pending = false;
m_sent_device_label = true;
rdm_sender.StartCustomResponse(received_message, RDM_RESPONSE_ACK,
0, SET_COMMAND_RESPONSE, PID_DEVICE_LABEL);
} else if (m_sent_device_label && received_message[24] ==
STATUS_GET_LAST_MESSAGE) {
rdm_sender.StartCustomResponse(received_message, RDM_RESPONSE_ACK,
0, SET_COMMAND_RESPONSE, PID_DEVICE_LABEL);
} else {
m_sent_device_label = false;
rdm_sender.StartCustomResponse(received_message, RDM_RESPONSE_ACK,
0, GET_COMMAND_RESPONSE, PID_STATUS_MESSAGES);
}
rdm_sender.EndRDMResponse();
}
/**
* Handle a GET SUPPORTED_PARAMETERS request
*/
void RDMHandler::HandleGetSupportedParameters(const byte *received_message) {
byte supported_params = 0;
for (byte i = 0; i < sizeof(PID_DEFINITIONS) / sizeof(pid_definition); ++i) {
if (PID_DEFINITIONS[i].include_in_supported_params)
supported_params++;
}
rdm_sender.StartRDMAckResponse(received_message, supported_params * 2);
for (byte i = 0; i < sizeof(PID_DEFINITIONS) / sizeof(pid_definition); ++i) {
if (PID_DEFINITIONS[i].include_in_supported_params)
rdm_sender.SendIntAndChecksum(PID_DEFINITIONS[i].pid);
}
rdm_sender.EndRDMResponse();
}
/**
* Handle a GET PARAMETER_DESCRIPTION request
*/
void RDMHandler::HandleGetParameterDescription(const byte *received_message) {
unsigned int param_id = (((unsigned int) received_message[24] << 8) +
received_message[25]);
if (param_id != 0x8000) {
rdm_sender.SendNack(received_message, NR_DATA_OUT_OF_RANGE);
return;
}
rdm_sender.StartRDMAckResponse(received_message,
20 + sizeof(SET_SERIAL_PID_DESCRIPTION) - 1);
rdm_sender.SendIntAndChecksum(0x8000);
rdm_sender.SendByteAndChecksum(4); // pdl size
rdm_sender.SendByteAndChecksum(0x03); // data type, uint8
rdm_sender.SendByteAndChecksum(0x02); // command class, set only
rdm_sender.SendByteAndChecksum(0); // type
rdm_sender.SendByteAndChecksum(0); // unit, none
rdm_sender.SendByteAndChecksum(0); // prefix, none
rdm_sender.SendLongAndChecksum(0); // min
rdm_sender.SendLongAndChecksum(0xfffffffe); // max
rdm_sender.SendLongAndChecksum(1); // default
for (unsigned int i = 0; i < sizeof(SET_SERIAL_PID_DESCRIPTION) - 1; ++i)
rdm_sender.SendByteAndChecksum(SET_SERIAL_PID_DESCRIPTION[i]);
rdm_sender.EndRDMResponse();
}
/**
* Handle a GET DEVICE_INFO request
*/
void RDMHandler::HandleGetDeviceInfo(const byte *received_message) {
rdm_sender.StartRDMAckResponse(received_message, 19);
rdm_sender.SendIntAndChecksum(256); // protocol version
rdm_sender.SendIntAndChecksum(2); // device model
rdm_sender.SendIntAndChecksum(0x0508); // product category
rdm_sender.SendLongAndChecksum(SOFTWARE_VERSION); // software version
byte personality = WidgetSettings.Personality();
rdm_sender.SendIntAndChecksum(rdm_personalities[personality - 1].slots);
// current personality
rdm_sender.SendByteAndChecksum(personality);
rdm_sender.SendByteAndChecksum(sizeof(rdm_personalities) /
sizeof(rdm_personality));
// DMX Start Address
rdm_sender.SendIntAndChecksum(WidgetSettings.StartAddress());
rdm_sender.SendIntAndChecksum(0); // Sub device count
rdm_sender.SendByteAndChecksum(1); // Sensor Count
rdm_sender.EndRDMResponse();
}
/**
* Handle a GET PRODUCT_DETAIL_ID request
*/
void RDMHandler::HandleGetProductDetailId(const byte *received_message) {
rdm_sender.StartRDMAckResponse(received_message, 2);
rdm_sender.SendIntAndChecksum(0x0403); // PWM dimmer
rdm_sender.EndRDMResponse();
}
/**
* Handle a GET PID_DEVICE_MODEL_DESCRIPTION request
*/
void RDMHandler::HandleGetDeviceModelDescription(
const byte *received_message) {
HandleStringRequest(received_message, DEVICE_NAME, DEVICE_NAME_SIZE);
}
/**
* Handle a GET MANUFACTURER_NAME request
*/
void RDMHandler::HandleGetManufacturerLabel(const byte *received_message) {
HandleStringRequest(received_message, MANUFACTURER_NAME,
MANUFACTURER_NAME_SIZE);
}
/**
* Handle a GET DEVICE_LABEL request
*/
void RDMHandler::HandleGetDeviceLabel(const byte *received_message) {
char device_label[MAX_LABEL_SIZE];
byte size = WidgetSettings.DeviceLabel(device_label, sizeof(device_label));
HandleStringRequest(received_message, device_label, size);
}
/**
* Handle a GET LANGUAGE / LANGUAGE_CAPABILITIES request
*/
void RDMHandler::HandleGetLanguage(const byte *received_message) {
HandleStringRequest(received_message,
SUPPORTED_LANGUAGE,
sizeof(SUPPORTED_LANGUAGE) - 1);
}
/**
* Handle a GET SOFTWARE_VERSION_LABEL request
*/
void RDMHandler::HandleGetSoftwareVersion(const byte *received_message) {
rdm_sender.StartRDMAckResponse(received_message,
sizeof(SOFTWARE_VERSION_STRING));
for (unsigned int i = 0; i < sizeof(SOFTWARE_VERSION_STRING); ++i)
rdm_sender.SendByteAndChecksum(SOFTWARE_VERSION_STRING[i]);
rdm_sender.EndRDMResponse();
}
/**
* Handle a GET DMX_PERSONALITY request
*/
void RDMHandler::HandleGetPersonality(const byte *received_message) {
rdm_sender.StartRDMAckResponse(received_message, 2);
rdm_sender.SendByteAndChecksum(WidgetSettings.Personality());
rdm_sender.SendByteAndChecksum(sizeof(rdm_personalities) /
sizeof(rdm_personality));
rdm_sender.EndRDMResponse();
}
/**
* Handle a GET DMX_PERSONALITY_DESCRIPTION request
*/
void RDMHandler::HandleGetPersonalityDescription(
const byte *received_message) {
byte max_personalities = sizeof(rdm_personalities) / sizeof(rdm_personality);
byte personality_number = received_message[24];
if (personality_number == 0 || personality_number > max_personalities) {
rdm_sender.SendNack(received_message, NR_DATA_OUT_OF_RANGE);
return;
}
rdm_personality const *personality =
&rdm_personalities[personality_number - 1];
unsigned int description_length = strlen(personality->description);
rdm_sender.StartRDMAckResponse(received_message, 3 + description_length);
rdm_sender.SendByteAndChecksum(personality_number);
rdm_sender.SendIntAndChecksum(personality->slots);
for (unsigned int i = 0; i < description_length; ++i)
rdm_sender.SendByteAndChecksum(personality->description[i]);
rdm_sender.EndRDMResponse();
}
/**
* Handle a GET DMX_START_ADDRESS request
*/
void RDMHandler::HandleGetStartAddress(const byte *received_message) {
int start_address = WidgetSettings.StartAddress();
rdm_sender.StartRDMAckResponse(received_message, sizeof(start_address));
rdm_sender.SendIntAndChecksum(start_address);
rdm_sender.EndRDMResponse();
}
/**
* Handle a GET SENSOR_DEFINITION request
*/
void RDMHandler::HandleGetSensorDefinition(const byte *received_message) {
if (received_message[24]) {
rdm_sender.SendNack(received_message, NR_DATA_OUT_OF_RANGE);
return;
}
rdm_sender.StartRDMAckResponse(
received_message,
13 + sizeof(TEMPERATURE_SENSOR_DESCRIPTION) - 1);
rdm_sender.SendByteAndChecksum(received_message[24]);
rdm_sender.SendByteAndChecksum(0x00); // type: temperature
rdm_sender.SendByteAndChecksum(1); // unit: C
rdm_sender.SendByteAndChecksum(1); // prefix: deci
rdm_sender.SendIntAndChecksum(0); // range min
rdm_sender.SendIntAndChecksum(1500); // range max
rdm_sender.SendIntAndChecksum(100); // normal min
rdm_sender.SendIntAndChecksum(400); // normal max
rdm_sender.SendByteAndChecksum(1); // recorded value support
for (unsigned int i = 0; i < sizeof(TEMPERATURE_SENSOR_DESCRIPTION) - 1; ++i)
rdm_sender.SendByteAndChecksum(TEMPERATURE_SENSOR_DESCRIPTION[i]);
rdm_sender.EndRDMResponse();
}
/**
* Handle a GET SENSOR_VALUE request
*/
void RDMHandler::HandleGetSensorValue(const byte *received_message) {
if (received_message[24]) {
rdm_sender.SendNack(received_message, NR_DATA_OUT_OF_RANGE);
return;
}
SendSensorResponse(received_message);
}
/**
* Handle a GET DEVICE_POWER_CYCLES request
*/
void RDMHandler::HandleGetDevicePowerCycles(const byte *received_message) {
unsigned long power_cycles = WidgetSettings.DevicePowerCycles();
rdm_sender.StartRDMAckResponse(received_message, sizeof(power_cycles));
rdm_sender.SendLongAndChecksum(power_cycles);
rdm_sender.EndRDMResponse();
}
/**
* Handle a GET IDENTIFY_DEVICE request
*/
void RDMHandler::HandleGetIdentifyDevice(const byte *received_message) {
rdm_sender.StartRDMAckResponse(received_message, 1);
rdm_sender.SendByteAndChecksum(m_identify_mode_enabled);
rdm_sender.EndRDMResponse();
}
/**
* Handle a SET DMX_START_ADDRESS request
*/
void RDMHandler::HandleSetLanguage(bool was_broadcast,
int sub_device,
const byte *received_message) {
// check for invalid size or value
if (received_message[23] != 2) {
rdm_sender.NackOrBroadcast(was_broadcast,
received_message,
NR_FORMAT_ERROR);
return;
}
bool ok = true;
for (byte i = 0; i < sizeof(SUPPORTED_LANGUAGE) - 1; ++i) {
ok &= SUPPORTED_LANGUAGE[i] == received_message[24 + i];
}
if (!ok) {
rdm_sender.NackOrBroadcast(was_broadcast,
received_message,
NR_DATA_OUT_OF_RANGE);
return;
}
if (was_broadcast) {
rdm_sender.ReturnRDMErrorResponse(RDM_STATUS_BROADCAST);
} else {
rdm_sender.SendEmptyAck(received_message);
}
}
/**
* Handle a SET DMX_START_ADDRESS request
*/
void RDMHandler::HandleSetDeviceLabel(bool was_broadcast,
int sub_device,
const byte *received_message) {
// check for invalid size or value
if (received_message[23] > MAX_LABEL_SIZE) {
rdm_sender.NackOrBroadcast(was_broadcast,
received_message,
NR_FORMAT_ERROR);
return;
}
WidgetSettings.SetDeviceLabel((char*) received_message + 24,
received_message[23]);
if (was_broadcast) {
rdm_sender.ReturnRDMErrorResponse(RDM_STATUS_BROADCAST);
} else {
// 400ms should be more than enough time
rdm_sender.SendAckTimer(received_message, 4);
m_device_label_pending = true;
rdm_sender.IncrementMessageCount();
}
}
/**
* Handle a SET DMX_PERSONALITY request
*/
void RDMHandler::HandleSetPersonality(bool was_broadcast,
int sub_device,
const byte *received_message) {
// check for invalid size or value
if (received_message[23] != 1) {
rdm_sender.NackOrBroadcast(was_broadcast,
received_message,
NR_FORMAT_ERROR);
return;
}
if (received_message[24] == 0 ||
received_message[24] >
sizeof(rdm_personalities) / sizeof(rdm_personality)) {
rdm_sender.NackOrBroadcast(was_broadcast,
received_message,
NR_DATA_OUT_OF_RANGE);
return;
}
WidgetSettings.SetPersonality(received_message[24]);
if (was_broadcast) {
rdm_sender.ReturnRDMErrorResponse(RDM_STATUS_BROADCAST);
} else {
rdm_sender.SendEmptyAck(received_message);
}
}
/**
* Handle a SET DMX_START_ADDRESS request
*/
void RDMHandler::HandleSetStartAddress(bool was_broadcast,
int sub_device,
const byte *received_message) {
// check for invalid size or value
if (received_message[23] != 2) {
rdm_sender.NackOrBroadcast(was_broadcast,
received_message,
NR_FORMAT_ERROR);
return;
}
int new_start_address = (((int) received_message[24] << 8) +
received_message[25]);
if (new_start_address == 0 || new_start_address > MAX_DMX_ADDRESS) {
rdm_sender.NackOrBroadcast(was_broadcast,
received_message,
NR_DATA_OUT_OF_RANGE);
return;
}
WidgetSettings.SetStartAddress(new_start_address);
if (was_broadcast) {
rdm_sender.ReturnRDMErrorResponse(RDM_STATUS_BROADCAST);
} else {
rdm_sender.SendEmptyAck(received_message);
}
}
/**
* Handle a SET SENSOR_VALUE request
*/
void RDMHandler::HandleSetSensorValue(bool was_broadcast,
int sub_device,
const byte *received_message) {
// check for invalid size or value
if (received_message[23] != 1) {
rdm_sender.NackOrBroadcast(was_broadcast,
received_message,
NR_FORMAT_ERROR);
return;
}
if (received_message[24] && received_message[24] != 0xff) {
rdm_sender.SendNack(received_message, NR_DATA_OUT_OF_RANGE);
return;
}
WidgetSettings.SaveSensorValue(0);
SendSensorResponse(received_message);
}
/*
* Handle a SET RECORD_SENSORS request
*/
void RDMHandler::HandleRecordSensor(bool was_broadcast,
int sub_device,
const byte *received_message) {
// check for invalid size or value
if (received_message[23] != 1) {
rdm_sender.NackOrBroadcast(was_broadcast,
received_message,
NR_FORMAT_ERROR);
return;
}
if (received_message[24] && received_message[24] != 0xff) {
rdm_sender.NackOrBroadcast(was_broadcast, received_message,
NR_DATA_OUT_OF_RANGE);
return;
}
WidgetSettings.SaveSensorValue(ReadTemperatureSensor());
if (was_broadcast) {
rdm_sender.ReturnRDMErrorResponse(RDM_STATUS_BROADCAST);
} else {
rdm_sender.SendEmptyAck(received_message);
}
}
/**
* Handle a SET DEVICE_POWER_CYCLES request
*/
void RDMHandler::HandleSetDevicePowerCycles(bool was_broadcast,
int sub_device,
const byte *received_message) {
// check for invalid size or value
if (received_message[23] != 4) {
rdm_sender.NackOrBroadcast(was_broadcast,
received_message,
NR_FORMAT_ERROR);
return;
}
unsigned long power_cycles = 0;
for (byte i = 0; i < 4; ++i) {
power_cycles = power_cycles << 8;
power_cycles += received_message[24 + i];
}
WidgetSettings.SetDevicePowerCycles(power_cycles);
if (was_broadcast) {
rdm_sender.ReturnRDMErrorResponse(RDM_STATUS_BROADCAST);
} else {
rdm_sender.SendEmptyAck(received_message);
}
}
/**
* Handle a SET IDENTIFY_DEVICE request
*/
void RDMHandler::HandleSetIdentifyDevice(bool was_broadcast,
int sub_device,
const byte *received_message) {
// check for invalid size or value
if (received_message[23] != 1) {
rdm_sender.NackOrBroadcast(was_broadcast,
received_message,
NR_FORMAT_ERROR);
return;
}
if (received_message[24] != 0 && received_message[24] != 1) {
rdm_sender.NackOrBroadcast(was_broadcast,
received_message,
NR_DATA_OUT_OF_RANGE);
return;
}
m_identify_mode_enabled = received_message[24];
digitalWrite(IDENTIFY_LED_PIN, m_identify_mode_enabled);
if (was_broadcast) {
rdm_sender.ReturnRDMErrorResponse(RDM_STATUS_BROADCAST);
} else {
rdm_sender.SendEmptyAck(received_message);
}
}
/**
* Handle a SET SERIAL_NUMBER request
*/
void RDMHandler::HandleSetSerial(bool was_broadcast,
int sub_device,
const byte *received_message) {
if (received_message[23] != 4) {
rdm_sender.NackOrBroadcast(was_broadcast,
received_message,
NR_FORMAT_ERROR);
return;
}
unsigned long new_serial_number = 0;
for (byte i = 0; i < 4; ++i) {
new_serial_number = new_serial_number << 8;
new_serial_number += received_message[24 + i];
}
if (new_serial_number == 0xffffffff) {
rdm_sender.NackOrBroadcast(was_broadcast,
received_message,
NR_DATA_OUT_OF_RANGE);
return;
}
WidgetSettings.SetSerialNumber(new_serial_number);
if (was_broadcast) {
rdm_sender.ReturnRDMErrorResponse(RDM_STATUS_BROADCAST);
} else {
rdm_sender.SendEmptyAck(received_message);
}
}
/*
* Handle an RDM message
* @param message pointer to a RDM message where the first byte is the sub star
* code.
* @param size the size of the message data.
*/
void RDMHandler::HandleRDMMessage(const byte *message, int size) {
// check for a packet that is too small, an invalid start / sub start code
// or a mismatched message length.
if (size < MINIMUM_RDM_PACKET_SIZE || message[0] != START_CODE ||
message[1] != SUB_START_CODE || message[2] != size - 2) {
rdm_sender.ReturnRDMErrorResponse(RDM_STATUS_FAILED);
return;
}
if (!VerifyChecksum(message, size)) {
rdm_sender.ReturnRDMErrorResponse(RDM_STATUS_FAILED_CHECKSUM);
return;
}
// true if this is broadcast or vendorcast, in which case we don't return a
// RDM message
bool is_broadcast = true;
for (int i = 5; i <= 8; ++i) {
is_broadcast &= (message[i] == 0xff);
}
int expected_esta_id = message[3];
expected_esta_id = expected_esta_id << 8;
expected_esta_id += message[4];
bool to_us = (
(expected_esta_id == WidgetSettings.EstaId() &&
(WidgetSettings.MatchesSerialNumber(message + 5) ||
is_broadcast)) ||
(expected_esta_id == 0xffff && is_broadcast));
if (!to_us) {
if (is_broadcast) {
rdm_sender.ReturnRDMErrorResponse(RDM_STATUS_BROADCAST);
} else {
rdm_sender.ReturnRDMErrorResponse(RDM_STATUS_INVALID_DESTINATION);
}
return;
}
// check the command class
byte command_class = message[20];
if (command_class != GET_COMMAND && command_class != SET_COMMAND) {
rdm_sender.ReturnRDMErrorResponse(RDM_STATUS_INVALID_COMMAND);
}
// check sub devices
unsigned int sub_device = (message[18] << 8) + message[19];
if (sub_device != 0 && sub_device != 0xffff) {
// respond with nack
rdm_sender.NackOrBroadcast(is_broadcast,
message,
NR_SUB_DEVICE_OUT_OF_RANGE);
return;
}
unsigned int param_id = (message[21] << 8) + message[22];
pid_definition const *pid_handler = NULL;
for (byte i = 0; i < sizeof(PID_DEFINITIONS) / sizeof(pid_definition); ++i) {
if (PID_DEFINITIONS[i].pid == param_id)
pid_handler = &PID_DEFINITIONS[i];
}
if (!pid_handler) {
rdm_sender.NackOrBroadcast(is_broadcast, message, NR_UNKNOWN_PID);
return;
}
if (command_class == GET_COMMAND) {
if (!pid_handler->get_handler) {
rdm_sender.NackOrBroadcast(is_broadcast, message,
NR_UNSUPPORTED_COMMAND_CLASS);
return;
}
if (is_broadcast) {
rdm_sender.ReturnRDMErrorResponse(RDM_STATUS_BROADCAST);
return;
}
if (sub_device) {
rdm_sender.SendNack(message, NR_SUB_DEVICE_OUT_OF_RANGE);
return;
}
if (message[23] != pid_handler->get_argument_size) {
rdm_sender.SendNack(message, NR_FORMAT_ERROR);
return;
}
(this->*(pid_handler->get_handler))(message);
} else {
if (!pid_handler->set_handler) {
rdm_sender.NackOrBroadcast(is_broadcast, message,
NR_UNSUPPORTED_COMMAND_CLASS);
return;
}
(this->*(pid_handler->set_handler))(is_broadcast, sub_device, message);
}
}