Skip to content

Commit 18ae53f

Browse files
committed
fix: make startup splash survive framebuffer update race
1 parent 8b2222f commit 18ae53f

11 files changed

Lines changed: 687 additions & 46 deletions

‎Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ check:
2222
sh tests/test-package-deb.sh
2323
sh tests/test-splash-preview.sh
2424
sh tests/test-daemon-splash.sh
25+
sh tests/test-actions-session.sh
2526

2627
clean:
2728
$(MAKE) -C userspace BUILD_DIR=$(BUILD_DIR) clean

‎backends/actions-micro/actions_micro_backend.c‎

Lines changed: 220 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353
#define USB_RESET_MIN_INTERVAL_NS 60000000000ULL
5454
#define USB_SYSFS_DEVICES "/sys/bus/usb/devices"
5555
#define USB_DEVICE_NODE_LENGTH 64U
56+
#define SESSION_UNKNOWN UINT16_MAX
57+
#define SESSION_SYNC_TIMEOUT_NS 5000000000ULL
58+
#define SESSION_SYNC_RETRY_NS 500000000ULL
59+
#define MAX_SESSION_SYNC_POLLS 100U
60+
#define MAX_SESSION_SYNC_SENDS 12U
5661

5762
#define VIDEO_CONFIG 0x800U
5863
#define VIDEO_IDR 0x200U
@@ -119,6 +124,12 @@ struct actions_context {
119124
uint32_t video_sequence;
120125
uint64_t video_report_index;
121126
uint8_t next_command_endpoint;
127+
uint16_t local_session;
128+
uint16_t remote_session;
129+
uint16_t acked_remote_session;
130+
bool peer_confirmed;
131+
bool session_confirmed;
132+
bool sync_reply_pending;
122133
uint64_t frames;
123134
uint64_t reports;
124135
/* 输入与复位状态把无声会话掉线纳入可观测、可恢复的路径。 */
@@ -191,6 +202,99 @@ static void write_le32(unsigned char *data, uint32_t value)
191202
data[3] = (unsigned char)(value >> 24);
192203
}
193204

205+
/* SGUP v3 carries two negotiated session IDs, not a fixed protocol magic. */
206+
static bool rewrite_report_session(unsigned char report[HID_REPORT_LENGTH],
207+
uint16_t local_session, uint16_t remote_session)
208+
{
209+
unsigned char *payload = report + HID_HEADER_LENGTH;
210+
uint32_t fragments = read_le32(report + 8);
211+
uint16_t bytes = read_le16(report + 12);
212+
213+
if ((report[1] != 1 && report[1] != 2) ||
214+
(fragments & 0xffffU) == 0 || (fragments >> 16) != 0 ||
215+
bytes < 16U || bytes > HID_PAYLOAD_LENGTH ||
216+
(memcmp(payload + 4, "CNYS", 4) != 0 &&
217+
memcmp(payload + 4, "_PPA", 4) != 0 &&
218+
memcmp(payload + 4, "RRIM", 4) != 0 &&
219+
memcmp(payload + 4, "GNIP", 4) != 0)) {
220+
return false;
221+
}
222+
write_le16(payload + 8, local_session);
223+
write_le16(payload + 10, remote_session);
224+
return true;
225+
}
226+
227+
/* A matching video request can acknowledge SYNC without a separate SYNC echo. */
228+
static int consume_sync_report(struct actions_context *state,
229+
const unsigned char *report, size_t report_bytes)
230+
{
231+
const unsigned char *payload;
232+
uint16_t payload_bytes;
233+
uint16_t remote_session;
234+
uint16_t echoed_session;
235+
236+
if (report_bytes < HID_HEADER_LENGTH + 16U || report[1] != 1 ||
237+
read_le32(report + 8) != 1U) {
238+
return 0;
239+
}
240+
payload_bytes = read_le16(report + 12);
241+
payload = report + HID_HEADER_LENGTH;
242+
if (payload_bytes < 16U || payload_bytes > report_bytes - HID_HEADER_LENGTH ||
243+
read_le32(payload) != payload_bytes ||
244+
read_le32(payload + 12) != 3U) {
245+
return 0;
246+
}
247+
remote_session = read_le16(payload + 8);
248+
echoed_session = read_le16(payload + 10);
249+
if (memcmp(payload + 4, "CNYS", 4) != 0) {
250+
if (!state->session_confirmed && remote_session != SESSION_UNKNOWN &&
251+
remote_session == state->remote_session &&
252+
state->acked_remote_session == remote_session &&
253+
echoed_session == state->local_session &&
254+
(memcmp(payload + 4, "RRIM", 4) == 0 ||
255+
memcmp(payload + 4, "_PPA", 4) == 0 ||
256+
memcmp(payload + 4, "GNIP", 4) == 0)) {
257+
state->peer_confirmed = true;
258+
state->session_confirmed = true;
259+
fprintf(stderr,
260+
"actions-micro: session peer acknowledged type=%.4s "
261+
"local=%04x remote=%04x\n", payload + 4,
262+
state->local_session, state->remote_session);
263+
}
264+
return 0;
265+
}
266+
if (payload_bytes < 24U) {
267+
return 0;
268+
}
269+
if (remote_session == SESSION_UNKNOWN) {
270+
return 0;
271+
}
272+
if (state->session_confirmed) {
273+
if (remote_session != state->remote_session ||
274+
echoed_session != state->local_session) {
275+
fprintf(stderr,
276+
"actions-micro: firmware session changed local=%04x "
277+
"remote=%04x received=%04x:%04x; renegotiating\n",
278+
state->local_session, state->remote_session,
279+
remote_session, echoed_session);
280+
return -ESTALE;
281+
}
282+
return 0;
283+
}
284+
if (state->remote_session != remote_session) {
285+
state->remote_session = remote_session;
286+
state->acked_remote_session = SESSION_UNKNOWN;
287+
state->sync_reply_pending = true;
288+
}
289+
state->peer_confirmed = echoed_session == state->local_session;
290+
if (!state->peer_confirmed) {
291+
state->sync_reply_pending = true;
292+
}
293+
state->session_confirmed = state->peer_confirmed &&
294+
state->acked_remote_session == remote_session;
295+
return 0;
296+
}
297+
194298
static int find_actions_micro_usb_node(
195299
char device_path[USB_DEVICE_NODE_LENGTH]);
196300

@@ -942,16 +1046,20 @@ static void log_ffmpeg_pipe_error(const char *operation, int descriptor,
9421046
static int send_report(struct actions_context *state, uint8_t endpoint,
9431047
const unsigned char data[HID_REPORT_LENGTH])
9441048
{
1049+
unsigned char report[HID_REPORT_LENGTH];
9451050
int descriptor = -1;
9461051
int result = -EPROTO;
9471052

1053+
memcpy(report, data, sizeof(report));
1054+
(void)rewrite_report_session(report, state->local_session,
1055+
state->remote_session);
9481056
if (endpoint == 0x03) {
9491057
descriptor = state->hid[0];
9501058
} else if (endpoint == 0x04) {
9511059
descriptor = state->hid[1];
9521060
}
9531061
if (descriptor >= 0) {
954-
result = write_all_logged(descriptor, data, HID_REPORT_LENGTH, "hid");
1062+
result = write_all_logged(descriptor, report, HID_REPORT_LENGTH, "hid");
9551063
if (result == 0) {
9561064
++state->reports;
9571065
} else {
@@ -986,25 +1094,38 @@ static void advance_transport_state(struct actions_context *state,
9861094
}
9871095
}
9881096

1097+
static bool is_sync_report(const unsigned char data[HID_REPORT_LENGTH])
1098+
{
1099+
return data[1] == 1 && read_le32(data + 8) == 1U &&
1100+
read_le16(data + 12) >= 24U &&
1101+
memcmp(data + HID_HEADER_LENGTH + 4, "CNYS", 4) == 0;
1102+
}
1103+
9891104
static int send_initialization(struct actions_context *state)
9901105
{
9911106
uint64_t start_ns = monotonic_nanoseconds();
1107+
unsigned char report[HID_REPORT_LENGTH];
9921108
size_t index;
9931109
int result = 0;
9941110

9951111
for (index = 0; index < state->initialization_count && result == 0;
9961112
++index) {
1113+
if (is_sync_report(state->initialization[index].data)) {
1114+
continue;
1115+
}
1116+
memcpy(report, state->initialization[index].data, sizeof(report));
1117+
write_le32(report + 4, state->command_sequence);
9971118
result = sleep_until_ns(start_ns +
9981119
state->initialization[index].timestamp_us * 1000ULL);
9991120
if (result == 0) {
10001121
result = send_report(state,
10011122
state->initialization[index].endpoint,
1002-
state->initialization[index].data);
1123+
report);
10031124
}
10041125
if (result == 0) {
10051126
advance_transport_state(state,
10061127
state->initialization[index].endpoint,
1007-
state->initialization[index].data);
1128+
report);
10081129
}
10091130
}
10101131

@@ -1014,21 +1135,30 @@ static int send_initialization(struct actions_context *state)
10141135
static int send_full_bootstrap(struct actions_context *state)
10151136
{
10161137
uint64_t start_ns = monotonic_nanoseconds();
1138+
unsigned char report[HID_REPORT_LENGTH];
10171139
size_t index;
10181140
int result = 0;
10191141

10201142
for (index = 0; index < state->bootstrap_count && result == 0;
10211143
++index) {
1144+
/* Captured SYNC belongs to a past session; negotiation replaced it. */
1145+
if (is_sync_report(state->bootstrap[index].data)) {
1146+
continue;
1147+
}
1148+
memcpy(report, state->bootstrap[index].data, sizeof(report));
1149+
if (report[1] == 1) {
1150+
write_le32(report + 4, state->command_sequence);
1151+
}
10221152
result = sleep_until_ns(start_ns +
10231153
state->bootstrap[index].timestamp_us * 1000ULL);
10241154
if (result == 0) {
10251155
result = send_report(state, state->bootstrap[index].endpoint,
1026-
state->bootstrap[index].data);
1156+
report);
10271157
}
10281158
if (result == 0) {
10291159
advance_transport_state(state,
10301160
state->bootstrap[index].endpoint,
1031-
state->bootstrap[index].data);
1161+
report);
10321162
}
10331163
}
10341164

@@ -1205,6 +1335,8 @@ static int poll_hid_inputs(struct actions_context *state, uint64_t now_ns)
12051335
bytes_read = read(descriptors[index].fd, report,
12061336
sizeof(report));
12071337
if (bytes_read > 0) {
1338+
result = consume_sync_report(state, report,
1339+
(size_t)bytes_read);
12081340
state->last_input_ns = now_ns;
12091341
++state->input_reports;
12101342
++state->input_reports_by_endpoint[index];
@@ -1235,6 +1367,74 @@ static int poll_hid_inputs(struct actions_context *state, uint64_t now_ns)
12351367
return result;
12361368
}
12371369

1370+
/* Both peers must advertise each other's ID before captured commands or video. */
1371+
static int send_sync_report(struct actions_context *state)
1372+
{
1373+
unsigned char report[HID_REPORT_LENGTH] = {0};
1374+
unsigned char *payload = report + HID_HEADER_LENGTH;
1375+
uint8_t endpoint = state->next_command_endpoint;
1376+
int result;
1377+
1378+
report[0] = endpoint == 0x04 ? 1 : 2;
1379+
report[1] = 1;
1380+
write_le32(report + 4, state->command_sequence);
1381+
write_le32(report + 8, 1);
1382+
write_le16(report + 12, 24);
1383+
write_le32(payload, 24);
1384+
memcpy(payload + 4, "CNYS", 4);
1385+
write_le32(payload + 12, 3);
1386+
write_le16(payload + 16, 1);
1387+
write_le16(payload + 18, 0x8001);
1388+
result = send_report(state, endpoint, report);
1389+
if (result == 0) {
1390+
advance_transport_state(state, endpoint, report);
1391+
state->acked_remote_session = state->remote_session;
1392+
state->sync_reply_pending = false;
1393+
state->session_confirmed = state->peer_confirmed &&
1394+
state->remote_session != SESSION_UNKNOWN;
1395+
}
1396+
return result;
1397+
}
1398+
1399+
static int negotiate_session(struct actions_context *state)
1400+
{
1401+
uint64_t deadline = monotonic_nanoseconds() + SESSION_SYNC_TIMEOUT_NS;
1402+
uint64_t next_send = 0;
1403+
uint64_t now_ns;
1404+
unsigned int attempt;
1405+
unsigned int sends = 0;
1406+
int result = 0;
1407+
1408+
for (attempt = 0; attempt < MAX_SESSION_SYNC_POLLS && result == 0 &&
1409+
!state->session_confirmed; ++attempt) {
1410+
now_ns = monotonic_nanoseconds();
1411+
if (now_ns >= deadline) {
1412+
break;
1413+
}
1414+
if (sends < MAX_SESSION_SYNC_SENDS &&
1415+
(state->sync_reply_pending || now_ns >= next_send)) {
1416+
result = send_sync_report(state);
1417+
++sends;
1418+
next_send = now_ns + SESSION_SYNC_RETRY_NS;
1419+
}
1420+
if (result == 0) {
1421+
result = poll_hid_inputs(state, now_ns);
1422+
}
1423+
if (result == 0 && !state->session_confirmed) {
1424+
result = sleep_microseconds(50000U);
1425+
}
1426+
}
1427+
if (result == 0 && !state->session_confirmed) {
1428+
result = -ETIMEDOUT;
1429+
}
1430+
fprintf(stderr,
1431+
"actions-micro: session sync %s local=%04x remote=%04x "
1432+
"peer-confirmed=%u sends=%u\n",
1433+
result == 0 ? "confirmed" : "failed", state->local_session,
1434+
state->remote_session, state->peer_confirmed ? 1U : 0U, sends);
1435+
return result;
1436+
}
1437+
12381438
/* 已发送心跳且长时间没有任何输入时,才判定固件会话丢失。 */
12391439
static int check_session_watchdog(struct actions_context *state,
12401440
uint64_t now_ns)
@@ -1682,7 +1882,6 @@ static int send_video_message(struct actions_context *state,
16821882
const unsigned char *video_data,
16831883
size_t video_bytes)
16841884
{
1685-
static const unsigned char protocol_id[4] = {0x29, 0x00, 0x67, 0x45};
16861885
unsigned char report[HID_REPORT_LENGTH];
16871886
unsigned char *message;
16881887
size_t message_bytes;
@@ -1709,7 +1908,8 @@ static int send_video_message(struct actions_context *state,
17091908
}
17101909
write_le32(message, (uint32_t)message_bytes);
17111910
memcpy(message + 4, "RRIM", 4);
1712-
memcpy(message + 8, protocol_id, sizeof(protocol_id));
1911+
write_le16(message + 8, state->local_session);
1912+
write_le16(message + 10, state->remote_session);
17131913
write_le32(message + 12, 3);
17141914
memcpy(message + 16, "TADV", 4);
17151915
write_le32(message + 20, 0);
@@ -1972,6 +2172,14 @@ static int actions_open(const struct usbdisplay_backend_config *config,
19722172
state->encoder.format = USBDISPLAY_FORMAT_INVALID;
19732173
state->width = config->device_width;
19742174
state->height = config->device_height;
2175+
/* A new host session makes daemon restarts independent of old capture IDs. */
2176+
state->local_session = (uint16_t)(monotonic_nanoseconds() ^ (uint64_t)getpid());
2177+
if (state->local_session == SESSION_UNKNOWN) {
2178+
state->local_session = 1;
2179+
}
2180+
state->remote_session = SESSION_UNKNOWN;
2181+
state->acked_remote_session = SESSION_UNKNOWN;
2182+
state->next_command_endpoint = 0x03;
19752183
result = parse_options(config->option, &state->options);
19762184
if (result == 0) {
19772185
result = load_template(state);
@@ -2001,9 +2209,11 @@ static int actions_open(const struct usbdisplay_backend_config *config,
20012209
if (state->options.full_bootstrap) {
20022210
/* usbfs 复位不切断 VBUS,重开时必须避免模板传输序号回退。 */
20032211
prepare_transport_sequence_resume(state);
2004-
result = send_full_bootstrap(state);
2005-
} else {
2006-
result = send_initialization(state);
2212+
}
2213+
result = negotiate_session(state);
2214+
if (result == 0) {
2215+
result = state->options.full_bootstrap ?
2216+
send_full_bootstrap(state) : send_initialization(state);
20072217
}
20082218
}
20092219
if (result == 0) {

0 commit comments

Comments
 (0)