-
Notifications
You must be signed in to change notification settings - Fork 2
/
sink_gaia.c
4055 lines (3185 loc) · 122 KB
/
sink_gaia.c
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
Copyright Cambridge Silicon Radio Limited 2010 - 2015.
Part of ADK 4.0
FILE NAME
sink_gaia.c
DESCRIPTION
Interface with the Generic Application Interface Architecture library
NOTES
*/
#ifdef ENABLE_GAIA
#include "sink_gaia.h"
#include <vm.h>
#include <memory.h>
#include <led.h>
#include <csr_voice_prompts_plugin.h>
#include <boot.h>
#include "sink_config.h"
#include "sink_configmanager.h"
#include "sink_gatt_client_ias.h"
#include "sink_leds.h"
#include "sink_led_manager.h"
#include "sink_tones.h"
#include "sink_audio_prompts.h"
#include "sink_buttons.h"
#include "sink_volume.h"
#include "sink_speech_recognition.h"
#include "sink_device_id.h"
#include "sink_statemanager.h"
#include "sink_link_policy.h"
#include <kalimba.h>
#define DSP_GAIA_MSG_SET_USER_PARAM (0x121a)
#define DSP_GAIA_MSG_GET_USER_PARAM (0x129a)
#define DSP_GAIA_MSG_SET_USER_GROUP_PARAM (0x121b)
#define DSP_GAIA_MSG_GET_USER_GROUP_PARAM (0x129b)
/*
#define DSP_GAIA_MSG_SET_USER_PARAM_RESP (0x321a)
#define DSP_GAIA_MSG_GET_USER_PARAM_RESP (0x329a)
#define DSP_GAIA_MSG_SET_USER_GROUP_PARAM_RESP (0x321b)
#define DSP_GAIA_MSG_GET_USER_GROUP_PARAM_RESP (0x329b)
*/
#define GAIA_AUDIO_GAIN_PAYLOAD_SHORT (6)
#define GAIA_AUDIO_GAIN_PAYLOAD_LONG (80)
#define GAIA_AUDIO_GAIN_MAX_TONE (161) /* WAV_RING_TONE_TOP - 1, not quite right */
#define GAIA_AUDIO_GAIN_MAX_GAIN (22)
#define GAIA_VOL_MAP_ELEMENTS (5)
/* Digital Volume Control limits imposed by Sink Configuration Tool */
#define GAIA_VOL_MAX_STEPS (500)
#define GAIA_VOL_MAX_TRIM (720) /* in 1/60 dB units */
#define GAIA_VOL_MAX_DAC_VOLUME (15)
/* Gaia-global data stored in app-allocated structure */
#define gaia_data theSink.rundata->gaia_data
#ifdef CVC_PRODTEST
#define CVC_PRODTEST_PASS 0x0001
#define CVC_PRODTEST_FAIL 0x0002
#endif
/* Valid values of Volume Parameter Number (see dsp_volume_user_config
structure in codec.h)
*/
typedef enum
{
gaia_vol_no_of_steps_main, /* number of steps of volume change permitted */
gaia_vol_volume_knee_value_1_main, /* volume point at which curve of dB conversion changes */
gaia_vol_volume_knee_value_2_main, /* volume point at which curve of dB conversion changes */
gaia_vol_dB_knee_value_1_main, /* dB value for point at which curve of dB conversion changes */
gaia_vol_dB_knee_value_2_main, /* dB value for point at which curve of dB conversion changes */
gaia_vol_dB_max_main, /* dB value at maximum volume level */
gaia_vol_dB_min_main, /* dB value at minimum volume level */
gaia_vol_no_of_steps_aux, /* number of steps of volume change permitted */
gaia_vol_volume_knee_value_1_aux, /* volume point at which curve of dB conversion changes */
gaia_vol_volume_knee_value_2_aux, /* volume point at which curve of dB conversion changes */
gaia_vol_dB_knee_value_1_aux, /* dB value for point at which curve of dB conversion changes */
gaia_vol_dB_knee_value_2_aux, /* dB value for point at which curve of dB conversion changes */
gaia_vol_dB_max_aux, /* dB value at maximum volume level */
gaia_vol_dB_min_aux, /* dB value at minimum volume level */
gaia_vol_device_trim_master, /* TWS master device trim gain */
gaia_vol_device_trim_slave, /* TWS slave device trim gain */
gaia_vol_device_trim_change, /* TWS device trim step change */
gaia_vol_device_trim_min, /* TWS device trim minimum gain */
gaia_vol_device_trim_max, /* TWS device trim maximum gain */
gaia_vol_param_top /* one step beyond greatest valid value */
} gaia_vol_param_t;
#define GAIA_VOL_NUM_PARAMETERS (gaia_vol_param_top)
STATIC_ASSERT(GAIA_VOL_NUM_PARAMETERS == sizeof (dsp_volume_user_config), gaia_vol);
/*************************************************************************
NAME
gaia_send_packet
DESCRIPTION
Build and Send a Gaia protocol packet
*/
static void gaia_send_packet(uint16 vendor_id, uint16 command_id, uint16 status,
uint16 payload_length, uint8 *payload)
{
uint16 packet_length;
uint8 *packet;
uint8 flags = GaiaTransportGetFlags(gaia_data.gaia_transport);
packet_length = GAIA_HEADER_SIZE + payload_length + 2;
packet = mallocPanic(packet_length);
if (packet)
{
packet_length = GaiaBuildResponse(packet, flags,
vendor_id, command_id,
status, payload_length, payload);
GaiaSendPacket(gaia_data.gaia_transport, packet_length, packet);
}
}
/*************************************************************************
NAME
gaia_send_response
DESCRIPTION
Build and Send a Gaia acknowledgement packet
*/
void gaia_send_response(uint16 vendor_id, uint16 command_id, uint16 status,
uint16 payload_length, uint8 *payload)
{
gaia_send_packet(vendor_id, command_id | GAIA_ACK_MASK, status,
payload_length, payload);
}
/*************************************************************************
NAME
gaia_send_response_16
DESCRIPTION
Build and Send a Gaia acknowledgement packet from a uint16[] payload
*/
void gaia_send_response_16(uint16 command_id, uint16 status,
uint16 payload_length, uint16 *payload)
{
uint16 packet_length;
uint8 *packet;
uint8 flags = GaiaTransportGetFlags(gaia_data.gaia_transport);
packet_length = GAIA_HEADER_SIZE + 2 * payload_length + 2;
packet = mallocPanic(packet_length);
if (packet)
{
packet_length = GaiaBuildResponse16(packet, flags,
GAIA_VENDOR_CSR, command_id | GAIA_ACK_MASK,
status, payload_length, payload);
GaiaSendPacket(gaia_data.gaia_transport, packet_length, packet);
}
}
/*************************************************************************
NAME
gaia_send_simple_response
gaia_send_success
gaia_send_success_payload
gaia_send_invalid_parameter
gaia_send_insufficient_resources
gaia_send_incorrect_state
DESCRIPTION
Convenience macros for common responses
*/
#define gaia_send_simple_response(command_id, status) \
gaia_send_response(GAIA_VENDOR_CSR, command_id, status, 0, NULL)
#define gaia_send_success(command_id) \
gaia_send_simple_response(command_id, GAIA_STATUS_SUCCESS)
#define gaia_send_success_payload(command_id, payload_len, payload) \
gaia_send_response(GAIA_VENDOR_CSR, command_id, GAIA_STATUS_SUCCESS, payload_len, (uint8 *) payload)
#define gaia_send_invalid_parameter(command_id) \
gaia_send_simple_response(command_id, GAIA_STATUS_INVALID_PARAMETER)
#define gaia_send_insufficient_resources(command_id) \
gaia_send_simple_response(command_id, GAIA_STATUS_INSUFFICIENT_RESOURCES)
#define gaia_send_incorrect_state(command_id) \
gaia_send_simple_response(command_id, GAIA_STATUS_INCORRECT_STATE)
#ifdef DEBUG_GAIA
static void dump(char *caption, uint16 *data, uint16 data_len)
{
GAIA_DEBUG(("%s: %d:", caption, data_len));
while (data_len--)
GAIA_DEBUG((" %04x", *data++));
GAIA_DEBUG(("\n"));
}
#endif
/*************************************************************************
NAME
gaia_send_notification
DESCRIPTION
Send a Gaia notification packet
*/
static void gaia_send_notification(uint8 event, uint16 payload_length, uint8 *payload)
{
gaia_send_packet(GAIA_VENDOR_CSR, GAIA_EVENT_NOTIFICATION, event, payload_length, payload);
}
/*************************************************************************
NAME
wpack
DESCRIPTION
Pack an array of 2n uint8s into an array of n uint16s
*/
static void wpack(uint16 *dest, uint8 *src, uint16 n)
{
while (n--)
{
*dest = *src++ << 8;
*dest++ |= *src++;
}
}
/*************************************************************************
NAME
dwunpack
DESCRIPTION
Unpack a uint32 into an array of 4 uint8s
*/
static void dwunpack(uint8 *dest, uint32 src)
{
*dest++ = src >> 24;
*dest++ = (src >> 16) & 0xFF;
*dest++ = (src >> 8) & 0xFF;
*dest = src & 0xFF;
}
/*************************************************************************
NAME
app_event_from_opid
DESCRIPTION
Translate an AVC operation identifier to an application event code
*/
#ifdef ENABLE_AVRCP
static sinkEvents_t app_event_from_opid(avc_operation_id opid)
{
switch (opid)
{
case opid_power:
return stateManagerGetState() == deviceLimbo ? EventUsrPowerOn : EventUsrPowerOff;
case opid_volume_up:
return EventUsrVolumeUp;
case opid_volume_down:
return EventUsrVolumeDown;
case opid_mute:
return EventUsrMainOutVolMuteToggle;
case opid_play:
return EventUsrAvrcpPlay;
case opid_stop:
return EventUsrAvrcpStop;
case opid_pause:
return EventUsrAvrcpPause;
case opid_forward:
return EventUsrAvrcpSkipForward;
case opid_backward:
return EventUsrAvrcpSkipBackward;
default:
return EventInvalid;
}
}
#endif
/*************************************************************************
NAME
pitch_length_tone
DESCRIPTION
Pack a pitch + length pair into a ringtone note
*/
static ringtone_note pitch_length_tone(uint8 pitch, uint8 length)
{
return (ringtone_note)
((pitch << RINGTONE_SEQ_NOTE_PITCH_POS)
| (length << RINGTONE_SEQ_NOTE_LENGTH_POS));
}
/*************************************************************************
NAME
get_config_lengths
DESCRIPTION
Load the lengths structure
*/
static void get_config_lengths(lengths_config_type *lengths)
{
ConfigRetrieve(CONFIG_LENGTHS, lengths, sizeof (lengths_config_type));
}
/*************************************************************************
NAME
send_app_message
DESCRIPTION
Send a message to the main application
*/
static void send_app_message(uint16 message)
{
MessageSend(&theSink.task, message, NULL);
}
/*************************************************************************
NAME
set_abs_eq_bank
DESCRIPTION
Select a Music Manager equaliser bank by absolute index
*/
static void set_abs_eq_bank(uint16 bank)
{
bank += A2DP_MUSIC_PROCESSING_FULL_SET_EQ_BANK0;
GAIA_DEBUG(("G: EQ %u -> %u\n",
theSink.a2dp_link_data->a2dp_audio_mode_params.music_mode_processing,
bank));
if(bank != theSink.a2dp_link_data->a2dp_audio_mode_params.music_mode_processing)
{
AUDIO_MODE_T mode = AUDIO_MODE_CONNECTED;
usbAudioGetMode(&mode);
theSink.a2dp_link_data->a2dp_audio_mode_params.music_mode_processing = bank;
configManagerWriteSessionData() ;
AudioSetMode(mode, &theSink.a2dp_link_data->a2dp_audio_mode_params);
#ifdef ENABLE_PEER
peerSendAudioEnhancements();
#endif
}
}
/*************************************************************************
NAME
gaia_device_reset
DESCRIPTION
Action a GAIA_COMMAND_DEVICE_RESET command by queueing an immediate
power off followed by a reboot
*/
static void gaia_device_reset(void)
{
theSink.features.GaiaRemainConnected = FALSE;
theSink.PowerOffIsEnabled = TRUE;
send_app_message(EventUsrPowerOff);
MessageSendLater(&theSink.task, EventSysReboot, NULL, D_SEC(2));
gaia_send_success(GAIA_COMMAND_DEVICE_RESET);
}
/*************************************************************************
NAME
gaia_change_volume
DESCRIPTION
Respond to GAIA_COMMAND_CHANGE_VOLUME request by sending the
appropriate event message to the sink device task. The device inverts
the message meanings if it thinks the buttons are inverted so we have
to double bluff it.
direction 0 means volume up, 1 means down.
*/
static void gaia_change_volume(uint8 direction)
{
uint16 message = 0;
uint8 status;
if (theSink.gVolButtonsInverted)
direction ^= 1;
if (direction == 0)
{
message = EventUsrVolumeUp;
status = GAIA_STATUS_SUCCESS;
}
else if (direction == 1)
{
message = EventUsrVolumeDown;
status = GAIA_STATUS_SUCCESS;
}
else
status = GAIA_STATUS_INVALID_PARAMETER;
if (status == GAIA_STATUS_SUCCESS)
send_app_message(message);
gaia_send_simple_response(GAIA_COMMAND_CHANGE_VOLUME, status);
}
/*************************************************************************
NAME
gaia_alert_leds
DESCRIPTION
Act on a GAIA_COMMAND_ALERT_LEDS request
*/
static void gaia_alert_leds(uint8 *payload)
{
uint16 time;
LEDPattern_t *pattern;
uint16 idx = 0;
while ((idx < theSink.theLEDTask->gEventPatternsAllocated)
&& ((theSink.theLEDTask->pEventPatterns[idx]).StateOrEvent != EventSysGaiaAlertLEDs))
++idx;
GAIA_DEBUG(("G: al: %d/%d\n", idx, theSink.theLEDTask->gEventPatternsAllocated));
if (idx == theSink.theLEDTask->gEventPatternsAllocated)
{
gaia_send_invalid_parameter(GAIA_COMMAND_ALERT_LEDS);
return;
}
pattern = &theSink.theLEDTask->pEventPatterns[idx];
time = (payload[0] << 8) | payload[1];
pattern->pattern.OnTime = (time >> theSink.features.LedTimeMultiplier) / 10;
time = (payload[2] << 8) | payload[3];
pattern->pattern.OffTime = (time >> theSink.features.LedTimeMultiplier) / 10;
time = (payload[4] << 8) | payload[5];
pattern->pattern.RepeatTime = (time >> theSink.features.LedTimeMultiplier) / 50;
pattern->pattern.DimTime = payload[7];
pattern->pattern.NumFlashes = payload[8];
pattern->pattern.TimeOut = payload[9];
pattern->pattern.LED_A = payload[10];
pattern->pattern.LED_B = payload[11];
pattern->pattern.OverideDisable = FALSE;
pattern->pattern.Colour = payload[12];
LEDManagerIndicateEvent(EventSysGaiaAlertLEDs);
gaia_send_success(GAIA_COMMAND_ALERT_LEDS);
}
/*************************************************************************
NAME
gaia_alert_tone
DESCRIPTION
Act on a GAIA_COMMAND_ALERT_TONE request
The command payload holds tempo, volume, timbre and decay values for
the whole sequence followed by pitch and duration values for each note.
NOTE
Since we can't be sure when the tone will start playing, let alone
when it will finish, we can't free the storage.
*/
static void gaia_alert_tone(uint8 length, uint8 *tones)
{
/* Must be at least 4 octets and an even number in total */
if ((length < 4) || (length & 1))
gaia_send_invalid_parameter(GAIA_COMMAND_ALERT_TONE);
/* If there are no notes then we're done */
else if (length == 4)
gaia_send_success(GAIA_COMMAND_ALERT_TONE);
else
{
if (gaia_data.alert_tone == NULL)
gaia_data.alert_tone = mallocPanic(GAIA_TONE_BUFFER_SIZE);
if ((gaia_data.alert_tone == NULL) || (length > GAIA_TONE_MAX_LENGTH))
gaia_send_insufficient_resources(GAIA_COMMAND_ALERT_TONE);
else
{
uint16 idx;
uint16 volume;
ringtone_note *t = gaia_data.alert_tone;
GAIA_DEBUG(("G: at: %d %d %d %d\n", tones[0], tones[1], tones[2], tones[3]));
AudioStopTone();
/* Set up tempo, volume, timbre and decay */
*t++ = RINGTONE_TEMPO(tones[0] * 4);
volume = tones[1];
*t++ = (ringtone_note) (RINGTONE_SEQ_TIMBRE | tones[2]);
*t++ = RINGTONE_DECAY(tones[3]);
/* Set up note-duration pairs */
for (idx = 4; idx < length; idx += 2)
{
GAIA_DEBUG((" - %d %d\n", tones[idx], tones[idx + 1]));
*t++ = pitch_length_tone(tones[idx], tones[idx + 1]);
}
/* Mark the end */
*t = RINGTONE_END;
AudioPlayTone(gaia_data.alert_tone, FALSE, theSink.codec_task, volume, theSink.conf2->audio_routing_data.PluginFeatures);
gaia_send_success(GAIA_COMMAND_ALERT_TONE);
}
}
}
/*************************************************************************
NAME
gaia_alert_event
DESCRIPTION
Act on a GAIA_COMMAND_ALERT_EVENT request by indicating the associated
event
*/
static void gaia_alert_event(uint16 event)
{
GAIA_DEBUG(("G: ae: %04X\n", event));
if (is_valid_event_id(event))
{
if (event != EventSysLEDEventComplete)
LEDManagerIndicateEvent(event);
TonesPlayEvent(event);
gaia_send_success(GAIA_COMMAND_ALERT_EVENT);
}
else
gaia_send_invalid_parameter(GAIA_COMMAND_ALERT_EVENT);
}
/*************************************************************************
NAME
gaia_alert_voice
DESCRIPTION
Act on a GAIA_COMMAND_ALERT_VOICE request
Play the indicated voice prompt
We have already established that theSink.num_audio_prompt_languages != 0
*/
static void gaia_alert_voice(uint8 payload_length, uint8 *payload)
{
lengths_config_type lengths;
TaskData *plugin = NULL;
uint16 nr_prompts;
uint16 ap_id = (payload[0] << 8) | payload[1];
get_config_lengths(&lengths);
nr_prompts = lengths.num_audio_prompts / theSink.num_audio_prompt_languages;
GAIA_DEBUG(("G: ti: %d/%d + %d\n", ap_id, nr_prompts, payload_length - 2));
if (ap_id < nr_prompts)
{
plugin = (TaskData *) &csr_voice_prompts_plugin;
AudioPromptPlay(plugin, ap_id, TRUE, FALSE);
gaia_send_success(GAIA_COMMAND_ALERT_VOICE);
}
else
gaia_send_invalid_parameter(GAIA_COMMAND_ALERT_VOICE);
}
/*************************************************************************
NAME
gaia_set_led_config
DESCRIPTION
Respond to GAIA_COMMAND_SET_LED_CONFIGURATION request
State and Event config requests are 17 octets long and look like
+--------+-----------------+-----------------+-----------------+-----------------+
| TYPE | STATE/EVENT | LED ON TIME | OFF TIME | REPEAT TIME |
+--------+--------+--------+--------+--------+--------+--------+--------+--------+ ...
0 1 2 3 4 5 6 7 8
+-----------------+--------+--------+--------+--------+--------+--------+
| DIM TIME |FLASHES |TIMEOUT | LED_A | LED_B |OVERRIDE| COLOUR |
... +--------+--------+--------+--------+--------+--------+DISABLE-+--------+
9 10 11 12 13 14 15 16
Filter config requests are 6 to 8 octets depending on the filter action and look like
+--------+--------+-----------------+--------+--------+--------+--------+
| TYPE | INDEX | EVENT | ACTION |OVERRIDE| PARAM1 |[PARAM2]|
+--------+--------+--------+--------+--------+DISABLE-+--------+--------+
0 1 2 3 4 5 6 [7]
*/
static void gaia_set_led_config(uint16 request_length, uint8 *request)
{
lengths_config_type lengths;
uint16 no_entries;
uint16 max_entries;
uint16 config_key;
uint16 config_length;
uint16 index;
uint16 event;
uint8 expect_length;
uint8 type = request[0];
get_config_lengths(&lengths);
switch (type)
{
case GAIA_LED_CONFIGURATION_FILTER:
no_entries = lengths.no_led_filter;
max_entries = MAX_LED_FILTERS;
config_key = CONFIG_LED_FILTERS;
switch (request[3])
{
case GAIA_LED_FILTER_OVERRIDE:
expect_length = 6;
break;
case GAIA_LED_FILTER_CANCEL:
case GAIA_LED_FILTER_COLOUR:
case GAIA_LED_FILTER_FOLLOW:
expect_length = 7;
break;
case GAIA_LED_FILTER_SPEED:
expect_length = 8;
break;
default:
expect_length = 0;
break;
}
break;
case GAIA_LED_CONFIGURATION_STATE:
no_entries = lengths.no_led_states;
max_entries = MAX_LED_STATES;
config_key = CONFIG_LED_STATES;
expect_length = 17;
break;
case GAIA_LED_CONFIGURATION_EVENT:
no_entries = lengths.no_led_events;
max_entries = MAX_LED_EVENTS;
config_key = CONFIG_LED_EVENTS;
expect_length = 17;
break;
default:
no_entries = 0;
max_entries = 0;
config_key = 0;
expect_length = 0;
break;
}
GAIA_DEBUG(("G: lc: t=%d id=%d n=%d k=%d r=%d x=%d\n",
type, request[2], no_entries, config_key, request_length, expect_length));
if (request_length != expect_length)
{
gaia_send_invalid_parameter(GAIA_COMMAND_SET_LED_CONFIGURATION);
return;
}
if (type == GAIA_LED_CONFIGURATION_FILTER)
{
LEDFilter_t *config = NULL;
bool changed = FALSE;
index = request[1] - 1;
event = (request[2] << 8) | request[3];
if ((index > no_entries) || !is_valid_event_id(event))
{
gaia_send_invalid_parameter(GAIA_COMMAND_SET_LED_CONFIGURATION);
return;
}
if (index == no_entries)
config_length = (no_entries + 1) * sizeof (LEDFilter_t);
else
config_length = no_entries * sizeof (LEDFilter_t);
config = mallocPanic(config_length);
if (config == NULL)
{
gaia_send_insufficient_resources(GAIA_COMMAND_SET_LED_CONFIGURATION);
return;
}
ConfigRetrieve(config_key, config, config_length);
changed = (index == no_entries) || (config[index].Event != event);
switch (request[4])
{
case GAIA_LED_FILTER_CANCEL:
if (changed || (config[index].FilterType != CANCEL) || (config[index].FilterToCancel != request[6]))
{
memset(&config[index], 0, sizeof (LEDFilter_t));
config[index].FilterType = CANCEL;
config[index].FilterToCancel = request[6];
changed = TRUE;
}
break;
case GAIA_LED_FILTER_SPEED:
if (changed
||(config[index].FilterType != SPEED)
|| (config[index].SpeedAction != request[6])
|| (config[index].Speed != request[7]))
{
memset(&config[index], 0, sizeof (LEDFilter_t));
config[index].FilterType = SPEED;
config[index].SpeedAction = request[6];
config[index].Speed = request[7];
changed = TRUE;
}
break;
case GAIA_LED_FILTER_OVERRIDE:
if (changed || (config[index].FilterType != OVERRIDE))
{
memset(&config[index], 0, sizeof (LEDFilter_t));
config[index].FilterType = OVERRIDE;
changed = TRUE;
}
break;
case GAIA_LED_FILTER_COLOUR:
if (changed ||(config[index].FilterType != COLOUR) || (config[index].Colour != request[6]))
{
memset(&config[index], 0, sizeof (LEDFilter_t));
config[index].FilterType = COLOUR;
config[index].Colour = request[6];
changed = TRUE;
}
break;
case GAIA_LED_FILTER_FOLLOW:
if (changed
|| (config[index].FilterType != FOLLOW)
|| (config[index].FollowerLEDDelay != request[6]))
{
memset(&config[index], 0, sizeof (LEDFilter_t));
config[index].FilterType = FOLLOW;
config[index].FollowerLEDDelay = request[6];
changed = TRUE;
}
break;
default:
expect_length = 0;
break;
}
if (changed)
{
config[index].Event = event;
config_length = ConfigStore(config_key, config, config_length);
if ((config_length > 0) && (no_entries != lengths.no_led_filter))
{
lengths.no_led_filter = no_entries;
config_length = ConfigStore(CONFIG_LENGTHS, &lengths, sizeof lengths);
if (config_length == 0)
{
GAIA_DEBUG(("GAIA: ConfigStore lengths failed\n"));
Panic();
}
}
}
freePanic(config);
}
else
{
LEDPattern_t *config = NULL;
bool changed = FALSE;
uint16 on_time = 0;
uint16 off_time = 0;
uint16 repeat_time = 0;
event = (request[1] << 8) | request[2];
if (type == GAIA_LED_CONFIGURATION_EVENT && !is_valid_event_id(event))
{
gaia_send_invalid_parameter(GAIA_COMMAND_SET_LED_CONFIGURATION);
return;
}
config_length = no_entries * sizeof (LEDPattern_t);
config = mallocPanic(config_length + sizeof (LEDPattern_t));
if (config == NULL)
{
gaia_send_insufficient_resources(GAIA_COMMAND_SET_LED_CONFIGURATION);
return;
}
ConfigRetrieve(config_key, config, config_length);
index = 0;
while ((index < no_entries) && (config[index].StateOrEvent != event))
++index;
GAIA_DEBUG(("G: idx %d/%d/%d\n", index, no_entries, max_entries));
if (index == max_entries)
{
freePanic(config);
gaia_send_insufficient_resources(GAIA_COMMAND_SET_LED_CONFIGURATION);
return;
}
if (index == no_entries)
{
GAIA_DEBUG(("G: + %d %d at %d\n", type, event, index));
changed = TRUE;
++no_entries;
config_length += sizeof (LEDPattern_t);
}
else
GAIA_DEBUG(("G: = %d %d at %d\n", type, event, index));
on_time = ((uint16) (request[3] << 8) | request[4]) / 10;
off_time = ((uint16) (request[5] << 8) | request[6]) / 10;
repeat_time = ((uint16) (request[7] << 8) | request[8]) / 50;
if (changed
|| config[index].pattern.OnTime != on_time
|| config[index].pattern.OffTime != off_time
|| config[index].pattern.RepeatTime != repeat_time
|| config[index].pattern.DimTime != request[10]
|| config[index].pattern.NumFlashes != request[11]
|| config[index].pattern.TimeOut != request[12]
|| config[index].pattern.LED_A != request[13]
|| config[index].pattern.LED_B != request[14]
|| config[index].pattern.OverideDisable != request[15]
|| config[index].pattern.Colour != request[16])
{
memset(&config[index], 0, sizeof (LEDPattern_t));
config[index].StateOrEvent = event;
config[index].pattern.OnTime = on_time;
config[index].pattern.OffTime = off_time;
config[index].pattern.RepeatTime = repeat_time;
config[index].pattern.DimTime = request[10];
config[index].pattern.NumFlashes = request[11];
config[index].pattern.TimeOut = request[12];
config[index].pattern.LED_A = request[13];
config[index].pattern.LED_B = request[14];
config[index].pattern.OverideDisable = request[15];
config[index].pattern.Colour = request[16];
config_length = ConfigStore(config_key, config, config_length);
if (config_length > 0)
{
if (type == GAIA_LED_CONFIGURATION_STATE)
{
changed = no_entries != lengths.no_led_states;
lengths.no_led_states = no_entries;
}
else
{
changed = no_entries != lengths.no_led_events;
lengths.no_led_events = no_entries;
}
if (changed)
{
config_length = ConfigStore(CONFIG_LENGTHS, &lengths, sizeof lengths);
if (config_length == 0)
{
GAIA_DEBUG(("GAIA: ConfigStore lengths failed\n"));
Panic();
}
}
}
}
freePanic(config);
}
if (config_length == 0)
gaia_send_insufficient_resources(GAIA_COMMAND_SET_LED_CONFIGURATION);
else if (type == GAIA_LED_CONFIGURATION_FILTER)
{
uint8 payload[4];
payload[0] = type; /* type */
payload[1] = request[1]; /* index */
payload[2] = event >> 8; /* event */
payload[3] = event & 0xFF;
gaia_send_success_payload(GAIA_COMMAND_SET_LED_CONFIGURATION, 4, payload);
}
else
{
uint8 payload[3];
payload[0] = type; /* type */
payload[1] = event >> 8; /* event */
payload[2] = event & 0xFF;
gaia_send_success_payload(GAIA_COMMAND_SET_LED_CONFIGURATION, 3, payload);
}
}
/*************************************************************************
NAME
gaia_send_led_config
DESCRIPTION
Respond to GAIA_COMMAND_GET_LED_CONFIGURATION request for the
configuration of a given state, event or filter
*/
#define filter_config ((LEDFilter_t *)config)
#define pattern_config ((LEDPattern_t *)config)
static void gaia_send_led_config(uint8 type, uint16 index)
{
lengths_config_type lengths;
void *config;
uint16 config_length;
uint16 key;
uint16 no_entries;
uint16 time;
uint16 payload_len;
uint8 payload[17];
get_config_lengths(&lengths);
switch (type)
{
case GAIA_LED_CONFIGURATION_STATE: