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
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Dell AIO Serial Backlight board emulator for testing
* the Linux dell-uart-backlight driver.
*
* Copyright (C) 2024 Hans de Goede <hansg@kernel.org>
*/
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <termios.h>
#include <unistd.h>
int serial_fd;
int brightness = 50;
static unsigned char dell_uart_checksum(unsigned char *buf, int len)
{
unsigned char val = 0;
while (len-- > 0)
val += buf[len];
return val ^ 0xff;
}
/* read() will return -1 on SIGINT / SIGTERM causing the mainloop to cleanly exit */
void signalhdlr(int signum)
{
}
int main(int argc, char *argv[])
{
struct sigaction sigact = { .sa_handler = signalhdlr };
unsigned char buf[4], csum, response[32];
const char *version_str = "PHI23-V321";
struct termios tty, saved_tty;
int ret, idx, len = 0;
if (argc != 2) {
fprintf(stderr, "Invalid or missing arguments\n");
fprintf(stderr, "Usage: %s <serial-port>\n", argv[0]);
return 1;
}
serial_fd = open(argv[1], O_RDWR | O_NOCTTY);
if (serial_fd == -1) {
fprintf(stderr, "Error opening %s: %s\n", argv[1], strerror(errno));
return 1;
}
ret = tcgetattr(serial_fd, &tty);
if (ret == -1) {
fprintf(stderr, "Error getting tcattr: %s\n", strerror(errno));
goto out_close;
}
saved_tty = tty;
cfsetspeed(&tty, 9600);
cfmakeraw(&tty);
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
tty.c_cflag |= CLOCAL | CREAD;
ret = tcsetattr(serial_fd, TCSANOW, &tty);
if (ret == -1) {
fprintf(stderr, "Error setting tcattr: %s\n", strerror(errno));
goto out_restore;
}
sigaction(SIGINT, &sigact, 0);
sigaction(SIGTERM, &sigact, 0);
idx = 0;
while (read(serial_fd, &buf[idx], 1) == 1) {
if (idx == 0) {
switch (buf[0]) {
/* 3 MSB bits: cmd-len + 01010 SOF marker */
case 0x6a: len = 3; break;
case 0x8a: len = 4; break;
default:
fprintf(stderr, "Error unexpected first byte: 0x%02x\n", buf[0]);
continue; /* Try to sync up with sender */
}
}
/* Process msg when len bytes have been received */
if (idx != (len - 1)) {
idx++;
continue;
}
/* Reset idx for next command */
idx = 0;
csum = dell_uart_checksum(buf, len - 1);
if (buf[len - 1] != csum) {
fprintf(stderr, "Error checksum mismatch got 0x%02x expected 0x%02x\n",
buf[len - 1], csum);
continue;
}
switch ((buf[0] << 8) | buf[1]) {
case 0x6a06: /* cmd = 0x06, get version */
len = strlen(version_str);
strcpy((char *)&response[2], version_str);
printf("Get version, reply: %s\n", version_str);
break;
case 0x8a0b: /* cmd = 0x0b, set brightness */
if (buf[2] > 100) {
fprintf(stderr, "Error invalid brightness param: %d\n", buf[2]);
continue;
}
len = 0;
brightness = buf[2];
printf("Set brightness %d\n", brightness);
break;
case 0x6a0c: /* cmd = 0x0c, get brightness */
len = 1;
response[2] = brightness;
printf("Get brightness, reply: %d\n", brightness);
break;
case 0x8a0e: /* cmd = 0x0e, set backlight power */
if (buf[2] != 0 && buf[2] != 1) {
fprintf(stderr, "Error invalid set power param: %d\n", buf[2]);
continue;
}
len = 0;
printf("Set power %d\n", buf[2]);
break;
default:
fprintf(stderr, "Error unknown cmd 0x%04x\n",
(buf[0] << 8) | buf[1]);
continue;
}
/* Respond with <total-len> <cmd> <data...> <csum> */
response[0] = len + 3; /* response length in bytes */
response[1] = buf[1]; /* ack cmd */
csum = dell_uart_checksum(response, len + 2);
response[len + 2] = csum;
ret = write(serial_fd, response, response[0]);
if (ret != (response[0]))
fprintf(stderr, "Error writing %d bytes: %d\n",
response[0], ret);
}
ret = 0;
out_restore:
tcsetattr(serial_fd, TCSANOW, &saved_tty);
out_close:
close(serial_fd);
return ret;
}
|