File: fu-logitech-bulkcontroller-common.c

package info (click to toggle)
fwupd 2.0.20-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 32,504 kB
  • sloc: ansic: 277,388; python: 11,485; xml: 9,493; sh: 1,625; makefile: 167; cpp: 19; asm: 11; javascript: 9
file content (255 lines) | stat: -rw-r--r-- 8,827 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright 1999-2021 Logitech, Inc.
 * All Rights Reserved
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include <fwupdplugin.h>

#include "fu-logitech-bulkcontroller-common.h"
#include "usb_msg.pb-c.h"

static void
fu_logitech_bulkcontroller_proto_manager_set_header(FuLogitechBulkcontrollerDevice *self,
						    Logi__Device__Proto__Header *header_msg)
{
	gint64 timestamp_tv;

	g_return_if_fail(header_msg != NULL);

	/* make predictable */
	if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) ||
	    fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATION_TAG)) {
		header_msg->id = 0;
		header_msg->timestamp = g_strdup("0");
		return;
	}

	timestamp_tv = g_get_real_time();
	header_msg->id = g_uuid_string_random();
	header_msg->timestamp = g_strdup_printf("%" G_GINT64_FORMAT, timestamp_tv / 1000);
}

GByteArray *
fu_logitech_bulkcontroller_proto_manager_generate_get_device_info_request(
    FuLogitechBulkcontrollerDevice *self)
{
	GByteArray *buf = g_byte_array_new();
	Logi__Device__Proto__Header header_msg = LOGI__DEVICE__PROTO__HEADER__INIT;
	Logi__Device__Proto__GetDeviceInfoRequest get_deviceinfo_msg =
	    LOGI__DEVICE__PROTO__GET_DEVICE_INFO_REQUEST__INIT;
	Logi__Device__Proto__UsbMsg usb_msg = LOGI__DEVICE__PROTO__USB_MSG__INIT;
	Logi__Device__Proto__Request request_msg = LOGI__DEVICE__PROTO__REQUEST__INIT;
	request_msg.payload_case = LOGI__DEVICE__PROTO__REQUEST__PAYLOAD_GET_DEVICE_INFO_REQUEST;
	request_msg.get_device_info_request = &get_deviceinfo_msg;

	fu_logitech_bulkcontroller_proto_manager_set_header(self, &header_msg);
	usb_msg.header = &header_msg;
	usb_msg.message_case = LOGI__DEVICE__PROTO__USB_MSG__MESSAGE_REQUEST;
	usb_msg.request = &request_msg;

	fu_byte_array_set_size(buf, logi__device__proto__usb_msg__get_packed_size(&usb_msg), 0x00);
	logi__device__proto__usb_msg__pack(&usb_msg, (unsigned char *)buf->data);
	g_free(header_msg.id);
	g_free(header_msg.timestamp);
	return buf;
}

GByteArray *
fu_logitech_bulkcontroller_proto_manager_generate_transition_to_device_mode_request(
    FuLogitechBulkcontrollerDevice *self)
{
	GByteArray *buf = g_byte_array_new();
	Logi__Device__Proto__Header header_msg = LOGI__DEVICE__PROTO__HEADER__INIT;
	Logi__Device__Proto__TransitionToDeviceModeRequest transition_to_device_mode_msg =
	    LOGI__DEVICE__PROTO__TRANSITION_TO_DEVICE_MODE_REQUEST__INIT;
	Logi__Device__Proto__UsbMsg usb_msg = LOGI__DEVICE__PROTO__USB_MSG__INIT;
	Logi__Device__Proto__Request request_msg = LOGI__DEVICE__PROTO__REQUEST__INIT;
	request_msg.payload_case =
	    LOGI__DEVICE__PROTO__REQUEST__PAYLOAD_TRANSITION_TO_DEVICEMODE_REQUEST;
	request_msg.transition_to_devicemode_request = &transition_to_device_mode_msg;

	fu_logitech_bulkcontroller_proto_manager_set_header(self, &header_msg);
	usb_msg.header = &header_msg;
	usb_msg.message_case = LOGI__DEVICE__PROTO__USB_MSG__MESSAGE_REQUEST;
	usb_msg.request = &request_msg;

	fu_byte_array_set_size(buf, logi__device__proto__usb_msg__get_packed_size(&usb_msg), 0x00);
	logi__device__proto__usb_msg__pack(&usb_msg, (unsigned char *)buf->data);
	g_free(header_msg.id);
	g_free(header_msg.timestamp);
	return buf;
}

GByteArray *
fu_logitech_bulkcontroller_proto_manager_generate_set_device_time_request(
    FuLogitechBulkcontrollerDevice *self,
    GError **error)
{
	g_autofree gchar *olson_location = NULL;
	g_autoptr(GByteArray) buf = g_byte_array_new();
	Logi__Device__Proto__Header header_msg = LOGI__DEVICE__PROTO__HEADER__INIT;
	Logi__Device__Proto__SetDeviceTimeRequest set_devicetime_msg =
	    LOGI__DEVICE__PROTO__SET_DEVICE_TIME_REQUEST__INIT;
	Logi__Device__Proto__UsbMsg usb_msg = LOGI__DEVICE__PROTO__USB_MSG__INIT;
	Logi__Device__Proto__Request request_msg = LOGI__DEVICE__PROTO__REQUEST__INIT;

	/* the device expects an olson_location, not a timezone */
	if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) ||
	    fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATION_TAG)) {
		olson_location = g_strdup("Europe/London");
	} else {
		olson_location = fu_common_get_olson_timezone_id(error);
		if (olson_location == NULL)
			return NULL;
	}

	request_msg.payload_case = LOGI__DEVICE__PROTO__REQUEST__PAYLOAD_SET_DEVICE_TIME_REQUEST;
	request_msg.set_device_time_request = &set_devicetime_msg;

	/* make predictable */
	if (fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATED) ||
	    fu_device_has_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_EMULATION_TAG)) {
		set_devicetime_msg.ts = 0;
	} else {
		set_devicetime_msg.ts = (g_get_real_time() / 1000) + SET_TIME_DELAY_MS;
	}
	set_devicetime_msg.time_zone = olson_location;
	fu_logitech_bulkcontroller_proto_manager_set_header(self, &header_msg);
	usb_msg.header = &header_msg;
	usb_msg.message_case = LOGI__DEVICE__PROTO__USB_MSG__MESSAGE_REQUEST;
	usb_msg.request = &request_msg;

	fu_byte_array_set_size(buf, logi__device__proto__usb_msg__get_packed_size(&usb_msg), 0x00);
	logi__device__proto__usb_msg__pack(&usb_msg, (unsigned char *)buf->data);
	g_free(header_msg.id);
	g_free(header_msg.timestamp);
	return g_steal_pointer(&buf);
}

static GByteArray *
fu_logitech_bulkcontroller_usb_msg_parse_msg_response(Logi__Device__Proto__UsbMsg *usb_msg,
						      FuLogitechBulkcontrollerProtoId *proto_id,
						      GError **error)
{
	g_autoptr(GByteArray) buf = g_byte_array_new();

	if (usb_msg->response == NULL) {
		g_set_error_literal(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_INVALID_DATA,
				    "no USB response");
		return NULL;
	}
	switch (usb_msg->response->payload_case) {
	case LOGI__DEVICE__PROTO__RESPONSE__PAYLOAD_GET_DEVICE_INFO_RESPONSE:
		if (usb_msg->response->get_device_info_response) {
			const gchar *tmp = usb_msg->response->get_device_info_response->payload;
			*proto_id = kProtoId_GetDeviceInfoResponse;
			if (tmp != NULL)
				g_byte_array_append(buf, (const guint8 *)tmp, strlen(tmp));
		}
		break;
	case LOGI__DEVICE__PROTO__RESPONSE__PAYLOAD_TRANSITION_TO_DEVICEMODE_RESPONSE:
		if (usb_msg->response->transition_to_devicemode_response) {
			*proto_id = kProtoId_TransitionToDeviceModeResponse;
			if (!usb_msg->response->transition_to_devicemode_response->success) {
				g_set_error(error,
					    FWUPD_ERROR,
					    FWUPD_ERROR_NOT_SUPPORTED,
					    "transition mode request failed. error: %u",
					    (guint)usb_msg->response
						->transition_to_devicemode_response->error);
				return NULL;
			}
		}
		break;
	default:
		break;
	};

	/* success */
	return g_steal_pointer(&buf);
}

static GByteArray *
fu_logitech_bulkcontroller_usb_msg_parse_msg_event(Logi__Device__Proto__UsbMsg *usb_msg,
						   FuLogitechBulkcontrollerProtoId *proto_id,
						   GError **error)
{
	g_autoptr(GByteArray) buf = g_byte_array_new();

	if (usb_msg->response == NULL) {
		g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_INVALID_DATA, "no USB event");
		return NULL;
	}
	switch (usb_msg->event->payload_case) {
	case LOGI__DEVICE__PROTO__EVENT__PAYLOAD_KONG_EVENT:
		if (usb_msg->event->kong_event) {
			const gchar *tmp = usb_msg->event->kong_event->mqtt_event;
			*proto_id = kProtoId_KongEvent;
			if (tmp != NULL)
				g_byte_array_append(buf, (const guint8 *)tmp, strlen(tmp));
		}
		break;
	case LOGI__DEVICE__PROTO__EVENT__PAYLOAD_HANDSHAKE_EVENT:
		if (usb_msg->event->handshake_event)
			*proto_id = kProtoId_HandshakeEvent;
		break;
	case LOGI__DEVICE__PROTO__EVENT__PAYLOAD_CRASH_DUMP_AVAILABLE_EVENT:
		*proto_id = kProtoId_CrashDumpAvailableEvent;
		break;
	default:
		break;
	};

	/* success */
	return g_steal_pointer(&buf);
}

GByteArray *
fu_logitech_bulkcontroller_proto_manager_decode_message(const guint8 *data,
							guint32 len,
							FuLogitechBulkcontrollerProtoId *proto_id,
							GError **error)
{
	g_autoptr(GByteArray) buf = NULL;
	Logi__Device__Proto__UsbMsg *usb_msg =
	    logi__device__proto__usb_msg__unpack(NULL, len, (const unsigned char *)data);

	/* sanity check */
	if (usb_msg == NULL) {
		g_set_error_literal(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_INVALID_DATA,
				    "unable to unpack data");
		return NULL;
	}

	switch (usb_msg->message_case) {
	case LOGI__DEVICE__PROTO__USB_MSG__MESSAGE_ACK:
		buf = g_byte_array_new();
		*proto_id = kProtoId_Ack;
		break;
	case LOGI__DEVICE__PROTO__USB_MSG__MESSAGE_RESPONSE:
		buf =
		    fu_logitech_bulkcontroller_usb_msg_parse_msg_response(usb_msg, proto_id, error);
		if (buf == NULL)
			return NULL;
		break;
	case LOGI__DEVICE__PROTO__USB_MSG__MESSAGE_EVENT:
		buf = fu_logitech_bulkcontroller_usb_msg_parse_msg_event(usb_msg, proto_id, error);
		if (buf == NULL)
			return NULL;
		break;
	default:
		buf = g_byte_array_new();
		g_debug("ignoring invalid message case 0x%x", usb_msg->message_case);
		break;
	};
	logi__device__proto__usb_msg__free_unpacked(usb_msg, NULL);
	return g_steal_pointer(&buf);
}