-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodCommandDynaudio.c
173 lines (141 loc) · 5.02 KB
/
modCommandDynaudio.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
/****************************************************************************
* Copyright (C) 2016 Muffinman
*
* 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 3 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
****************************************************************************/
#include <stdlib.h>
#include <stdarg.h>
#include <syslog.h>
#include <math.h>
#include <stdint.h> // declares uint8_t a.o.
#include "common.h"
#include "processData.h"
#include "verifyConfig.h"
#include "mods.h"
static void help(void);
static int init(int x, ...);
static void produce(const void *message);
static int process(void *command, int command_length);
static void deinit(void);
modCommand_t modCommandDynaudio = {
.name = "dynaudio",
.help = &help,
.init = &init,
.produce = &produce,
.process = &process,
.deinit = &deinit
};
static int inputPos, inputLen, zone, defaultInput;
static void help(void) {
printf("Dynaudio Tail Mod\n"
);
}
static int init(int x, ...) {
if(strcmp(common_data.process->name, "numeric") != 0) {
syslog(LOG_ERR, "[Error] Setting 'data_type' must be 'numeric' for this mod to function: dynaudio");
return EXIT_FAILURE;
}
va_list arguments;
va_start (arguments, x);
int *alwaysMatchTail = va_arg ( arguments, int* );
*alwaysMatchTail = 1;
int *headerLength = va_arg ( arguments, int* );
if(*headerLength != 9) {
syslog(LOG_ERR, "[Error] RX commands are of the wrong size for this mod: %i", *headerLength);
return EXIT_FAILURE;
}
if(*(headerLength+1) != 12) {
syslog(LOG_ERR, "[Error] TX commands are of the wrong size for this mod: %i", *(headerLength+1));
return EXIT_FAILURE;
}
va_end (arguments);
if(validateConfigInt(&config, "command_mod.zone", &zone, -1, 1, 3, -1) == EXIT_FAILURE)
return EXIT_FAILURE;
if(validateConfigInt(&config, "command_mod.default_input", &defaultInput, -1, 1, 7, -1) == EXIT_FAILURE)
return EXIT_FAILURE;
// check for compatibility:min length tail, command; // can only check at runtime!!
inputPos =3;
inputLen = 5;
return EXIT_SUCCESS;
}
static void produce(const void *message) {
/***********************************
* Generating status byte
***********************************/
int statusByte;
#ifdef REDUNDANT
const char *currentInput;
char config_query[CONFIG_QUERY_SIZE];
statusInfo.retrieve("input", ¤tInput);
if(currentInput) {
snprintf(config_query, CONFIG_QUERY_SIZE, "input.%s.[0]", currentInput);
if((config_lookup_int(&config, config_query, &statusByte)) != CONFIG_TRUE) {
statusByte = defaultInput;
}
}
else
statusByte = defaultInput;
statusByte = statusByte*0x10;
#else
statusByte = defaultInput*0x10;
#endif
statusByte += zone;
uint8_t *command = (uint8_t *)message;
command[7] = statusByte;
/***********************************
* Generating checksum byte
***********************************/
int checksum;
int sum = 0;
int count;
for(count=0; count < inputLen; count++) {
sum += *(command+inputPos+count);
}
int q = ceil(sum/255.00);
checksum = q*255-sum-(inputLen-q);
if(checksum < 0)
checksum = checksum & ((1 << 8) - 1);
command[8] = checksum;
//ROUNDUP(SUM(payload)/255)*255-SUM(payload)-([Payload N]-ROUNDUP(SUM(payload)/255))
}
static int process(void *command, int command_length) {
/***********************************
* Verifying status byte (only zone part)
***********************************/
uint8_t *message = (uint8_t *)command;
// Extract 2 LSB
int zonenr = message[7] & ((1 << 2) - 1);
if(zonenr != zone) {
syslog(LOG_DEBUG, "Incoming command not valid, wrong zone: %i", zonenr);
return EXIT_FAILURE;
}
/***********************************
* Update input status if changed
**********************************/
static int inputnr_backup;
int action = message[7] >> 4; // input #
int category = 0x06; // input message
if(inputnr_backup != action) {
inputnr_backup = action;
common_data.process->processCommand(&category, &action);
}
/***********************************
* Correct volume header if necessary
***********************************/
if(message[5] == 0x05)
message[5] = 0x04;
return EXIT_SUCCESS;
}
static void deinit(void) {
}