-
Notifications
You must be signed in to change notification settings - Fork 0
/
lge_status.cc
308 lines (262 loc) · 7.95 KB
/
lge_status.cc
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
/*
* Copyright (c) 2011 Pelle Johansson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "status.hh"
#include "status_private.hh"
#include "lge_status.hh"
#include "backend.h"
#include <stdlib.h>
#include <string.h>
#include <err.h>
class lge_status : public status
{
public:
lge_status(backend_ptr &ptr, std::string name, std::string line, std::string client, int throttle);
virtual const char *packet_separators() const;
virtual void update_status(const std::string &packet, const struct backend_output *inptr);
virtual int send_status_request(const std::string &code);
virtual int query_command(const std::string &code) const;
virtual int query_status(const std::string &code) const;
virtual int query(const std::string &code, std::string &out_buf);
virtual void send_command(const std::string &cmd, const std::vector<int32_t> &args);
#define NOTIFY(name, code, cmd, type) void update_ ## name(const struct lge_notify *lgenot, int ok, const std::string &arg);
#include "lge_notify.h"
#undef NOTIFY
};
lge_status::lge_status(backend_ptr &ptr, std::string name, std::string line, std::string client, int throttle)
: status(ptr, std::move(name), std::move(line), std::move(client), throttle)
{
}
const char *
lge_status::packet_separators()
const
{
return "x";
}
struct lge_notify
{
const char *code;
const char *cmd;
void (lge_status::*func)(const struct lge_notify *lgenot, int ok, const std::string &arg);
};
#define UPDATE_FUNC_BOOL(field) \
void \
lge_status::update_ ## field(const struct lge_notify *lgenot, int ok, const std::string &arg) { \
if (ok) \
notify(lgenot->code, atoi(arg.c_str())); \
else \
notify(lgenot->code, -1); \
}
#define UPDATE_FUNC_INT(field) \
void \
lge_status::update_ ## field(const struct lge_notify *lgenot, int ok, const std::string &arg) { \
if (ok) \
notify(lgenot->code, strtol(arg.c_str(), NULL, 16)); \
else \
notify(lgenot->code, INT_MIN); \
}
UPDATE_FUNC_BOOL(power);
UPDATE_FUNC_INT (aspect_ratio)
UPDATE_FUNC_INT (video_mute)
UPDATE_FUNC_BOOL(audio_mute);
UPDATE_FUNC_INT (volume)
UPDATE_FUNC_INT (contrast)
UPDATE_FUNC_INT (brightness)
UPDATE_FUNC_INT (colour)
UPDATE_FUNC_INT (tint)
UPDATE_FUNC_INT (sharpness)
UPDATE_FUNC_BOOL (osd)
UPDATE_FUNC_BOOL (remote_control_lock)
UPDATE_FUNC_INT (treble)
UPDATE_FUNC_INT (bass)
UPDATE_FUNC_INT (balance)
UPDATE_FUNC_INT (colour_temperature)
UPDATE_FUNC_INT (energy_saving)
void
lge_status::update_tv_band(const struct lge_notify *lgenot, int ok, const std::string &arg) {
if (ok)
notify(lgenot->code, arg[4] - '0');
else
notify(lgenot->code, -1);
}
void
lge_status::update_tv_channel(const struct lge_notify *lgenot, int ok, const std::string &arg) {
if (ok) {
int val = 0;
int i;
for (i = 0 ; i < 4 ; i++) {
val <<= 4;
if (arg[i] >= '0' && arg[i] <= '9')
val += arg[i] - '0';
else if (arg[i] >= 'A' && arg[i] <= 'F')
val += arg[i] - 'A';
else if (arg[i] >= 'a' && arg[i] <= 'f')
val += arg[i] - 'a';
}
notify(lgenot->code, val);
} else
notify(lgenot->code, -1);
}
UPDATE_FUNC_BOOL (programme_add)
UPDATE_FUNC_INT (back_light)
void
lge_status::update_source(const struct lge_notify *lgenot, int ok, const std::string &arg) {
if (ok) {
int val = 0x100 + strtol(arg.c_str(), NULL, 16);
if (val >= 0x1A0 && val <= 0x1AF) {
/* For some reason this shifts. */
val -= 0x10;
}
notify(lgenot->code, val);
} else
notify(lgenot->code, -1);
}
const struct lge_notify lge_notifies[] = {
#define NOTIFY(name, code, cmd, type) { code, cmd, &lge_status::update_ ## name },
#include "lge_notify.h"
#undef NOTIFY
{ NULL }
};
void
lge_status::update_status(const std::string &line, const struct backend_output *inptr)
{
const struct lge_notify *lgenot;
char cmd[3];
int ok;
while (inptr && (inptr->len < 2 || inptr->data[1] != line[0]))
remove_output(&inptr);
if (!inptr) {
warnx("No output match for %s", line.c_str());
return;
}
cmd[0] = inptr->data[0];
remove_output(&inptr);
//warnx("Read packet %c%s", cmd[0], line.c_str());
cmd[1] = line[0];
cmd[2] = '\0';
if (line.length() < sizeof ("x 01 ")) {
warnx("Invalid packet");
return;
}
std::string ack = line.substr(sizeof("x 01 ") - 1, 2);
if (ack == "OK")
ok = 1;
else if (ack == "NG")
ok = 0;
else {
warnx("Invalid OK/NG");
return;
}
std::string arg = line.substr(sizeof("x 01 ") + 1);
for (lgenot = lge_notifies ; lgenot->code ; lgenot++) {
if (strncmp(cmd, lgenot->cmd, 2) == 0) {
(this->*lgenot->func)(lgenot, ok, arg);
}
}
}
int
lge_status::send_status_request(const std::string &code)
{
const struct lge_notify *lgenot;
for (lgenot = lge_notifies ; lgenot->code ; lgenot++) {
if (code == lgenot->code) {
send("%s 00 FF\r", lgenot->cmd);
return 0;
}
}
warnx("send_status_request(%s)", code.c_str());
return -1;
}
int
lge_status::query(const std::string &code, std::string &out_buf)
{
/* Not really supported (but could use a cache) */
return STATUS_UNKNOWN;
}
const struct lge_command {
const char *cmd;
const char *fmt;
size_t narg;
int split;
struct timeval throttle;
} lge_commands[] = {
#define THROTTLED_COMMAND(name, cmd, code, arg, s, ms) { cmd, code " 00 " arg "\r", 0, 0, { s, ms * 1000 } },
#define SIMPLE_COMMAND(name, cmd, code, arg) { cmd, code " 00 " arg "\r", 0, 0 },
#define UINT_COMMAND(name, cmd, code) { cmd, code " 00 %02X\r", 1, 0 },
#define UINT2_SUFF_COMMAND(name, cmd, code, suff) { cmd, code " 00 %02X %02X " suff "\r", 1, 1 },
#include "lge_command.h"
{ NULL }
};
int
lge_status::query_command(const std::string &code)
const
{
const struct lge_command *lgecmd;
for (lgecmd = lge_commands ; lgecmd->cmd ; lgecmd++) {
if (code == lgecmd->cmd) {
return 0;
}
}
return -1;
}
int
lge_status::query_status(const std::string &code)
const
{
const struct lge_notify *lgenot;
for (lgenot = lge_notifies ; lgenot->code ; lgenot++) {
if (code == lgenot->code) {
return 0;
}
}
return -1;
}
void
lge_status::send_command(const std::string &cmd, const std::vector<int32_t> &args)
{
const struct lge_command *lgecmd;
for (lgecmd = lge_commands ; lgecmd->cmd ; lgecmd++) {
if (cmd == lgecmd->cmd) {
break;
}
}
if (!lgecmd->cmd) {
warnx("No such command: %s", cmd.c_str());
return;
}
if (args.size() != lgecmd->narg) {
warnx("Mismatch number of arguments %zd <> %zd", args.size(), lgecmd->narg);
return;
}
if (args.size() == 0)
send_throttle(&lgecmd->throttle, lgecmd->fmt, "" /* Suppress warning */);
else if (lgecmd->split)
send_throttle(&lgecmd->throttle, lgecmd->fmt, (int)args[0] / 256, (int)args[0] & 255);
else
send_throttle(&lgecmd->throttle, lgecmd->fmt, (int)args[0]);
}
class status *lge_creator(backend_ptr &ptr, std::string name, std::string line, std::string client, int throttle)
{
return new lge_status(ptr, std::move(name), std::move(line), std::move(client), throttle);
}