summaryrefslogtreecommitdiff
path: root/src/sensors/cd-parse-beagle.c
blob: de83dccf770168563b48fe3158e1d8033595fb4a (plain)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2010 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <glib.h>
#include <huey/huey.h>
#include <munki/munki.h>

#include "cd-sensor.h"

typedef enum {
	CD_PARSE_SECTION_LEVEL,
	CD_PARSE_SECTION_SP,
	CD_PARSE_SECTION_MS_US,
	CD_PARSE_SECTION_DUR,
	CD_PARSE_SECTION_LEN,
	CD_PARSE_SECTION_ERR,
	CD_PARSE_SECTION_DEV,
	CD_PARSE_SECTION_EP,
	CD_PARSE_SECTION_RECORD,
	CD_PARSE_SECTION_SUMMARY
} CdParseSection;

typedef enum {
	CD_PARSE_ENTRY_DIRECTION_UNKNOWN,
	CD_PARSE_ENTRY_DIRECTION_REQUEST,
	CD_PARSE_ENTRY_DIRECTION_REPLY
} CdParseEntryDirection;

typedef struct {
	const gchar		*record;
	const gchar		*summary;
	const gchar		*summary_pretty;
	gint			 dev;
	gint			 ep;
	const gchar		*ep_description;
	CdParseEntryDirection	 direction;
} CdParseEntry;

/**
 * cd_parse_beagle_process_entry_huey:
 **/
static void
cd_parse_beagle_process_entry_huey (CdParseEntry *entry)
{
	g_auto(GStrv) tok = NULL;
	guint j;
	guint8 cmd;
	guint8 instruction = 0;
	const gchar *command_as_text;
	GString *output = NULL;

	entry->ep_description = "default";

	/* only know how to parse 8 bytes */
	tok = g_strsplit (entry->summary, " ", -1);
	if (g_strv_length (tok) != 8) {
		g_print ("not 8 tokens: %s\n", entry->summary);
		goto out;
	}

	output = g_string_new ("");
	for (j = 0; j < 8; j++) {
		command_as_text = NULL;
		cmd = g_ascii_strtoll (tok[j], NULL, 16);
		if (j == 0 && entry->direction == CD_PARSE_ENTRY_DIRECTION_REPLY) {
			command_as_text = huey_rc_to_string (cmd);
			if (command_as_text == NULL)
				g_warning ("return code 0x%02x not known in %s", cmd, entry->summary);
		}
		if ((j == 0 && entry->direction == CD_PARSE_ENTRY_DIRECTION_REQUEST) ||
		    (j == 1 && entry->direction == CD_PARSE_ENTRY_DIRECTION_REPLY)) {
			instruction = cmd;
			command_as_text = huey_cmd_code_to_string (instruction);
			if (command_as_text == NULL)
				g_warning ("command code 0x%02x not known", cmd);
		}

		/* some requests are filled with junk data */
		if (entry->direction == CD_PARSE_ENTRY_DIRECTION_REQUEST &&
		    instruction == HUEY_CMD_REGISTER_READ && j > 1)
			g_string_append_printf (output, "xx ");
		else if (entry->direction == CD_PARSE_ENTRY_DIRECTION_REQUEST &&
			 instruction == HUEY_CMD_SET_LEDS && j > 4)
			g_string_append_printf (output, "xx ");
		else if (entry->direction == CD_PARSE_ENTRY_DIRECTION_REQUEST &&
			 instruction == HUEY_CMD_GET_AMBIENT && j > 3)
			g_string_append_printf (output, "xx ");
		else if (command_as_text != NULL)
			g_string_append_printf (output, "%02x(%s) ", cmd, command_as_text);
		else
			g_string_append_printf (output, "%02x ", cmd);
	}

	/* remove trailing space */
	if (output->len > 1)
		g_string_set_size (output, output->len - 1);
out:
	if (output != NULL)
		entry->summary_pretty = g_string_free (output, FALSE);
}

/**
 * cd_parse_beagle_process_entry_colormunki:
 **/
static void
cd_parse_beagle_process_entry_colormunki (CdParseEntry *entry)
{
	g_auto(GStrv) tok = NULL;
	guint j;
	guint8 cmd;
	guint tok_len;
	GString *output;

	/* set ep description */
	entry->ep_description = munki_endpoint_to_string (entry->ep);

	output = g_string_new ("");

	/* only know how to parse 8 bytes */
	tok = g_strsplit (entry->summary, " ", -1);
	tok_len = g_strv_length (tok);

	/* status */
	if (entry->ep == MUNKI_EP_CONTROL &&
	    entry->direction == CD_PARSE_ENTRY_DIRECTION_REPLY &&
	    tok_len == 2) {

		/* dial position */
		cmd = g_ascii_strtoll (tok[0], NULL, 16);
		g_string_append_printf (output, "%s(dial-position-%s) ",
					tok[0],
					munki_dial_position_to_string (cmd));

		/* button value */
		cmd = g_ascii_strtoll (tok[1], NULL, 16);
		g_string_append_printf (output, "%s(button-state-%s)",
					tok[1],
					munki_button_state_to_string (cmd));
		goto out;
	}

	/* event */
	if (entry->ep == MUNKI_EP_EVENT &&
	    entry->direction == CD_PARSE_ENTRY_DIRECTION_REPLY &&
	    tok_len == 8) {
		g_print ("process 8: %s\n", entry->summary);

		/* cmd */
		cmd = g_ascii_strtoll (tok[0], NULL, 16);
		g_string_append_printf (output, "%s(%s) ",
					tok[0],
					munki_command_value_to_string (cmd));

		for (j=1; j<8; j++) {
			cmd = g_ascii_strtoll (tok[j], NULL, 16);
			g_string_append_printf (output, "%02x ", cmd);
		}
		if (output->len > 1)
			g_string_set_size (output, output->len - 1);
		goto out;
	}

	/* unknown command */
	for (j = 0; j < tok_len; j++) {
		cmd = g_ascii_strtoll (tok[j], NULL, 16);
		g_string_append_printf (output, "%02x ", cmd);
	}
	if (output->len > 1)
		g_string_set_size (output, output->len - 1);
out:
	if (output != NULL)
		entry->summary_pretty = g_string_free (output, FALSE);
}

/**
 * cd_parse_beagle_process_entry:
 **/
static gchar *
cd_parse_beagle_process_entry (CdSensorKind kind, CdParseEntry *entry)
{
	gchar *retval = NULL;
	const gchar *direction = "??";

	/* timeout */
	if (g_str_has_suffix (entry->record, "IN-NAK]"))
		goto out;

	/* device closed */
	if (g_strcmp0 (entry->record, "[1 ORPHANED]") == 0)
		goto out;

	/* usb error */
	if (g_strcmp0 (entry->record, "[53 SYNC ERRORS]") == 0)
		goto out;

	/* other event to ignore */
	if (g_strcmp0 (entry->record, "Bus event") == 0)
		goto out;
	if (g_strcmp0 (entry->record, "Get Configuration Descriptor") == 0)
		goto out;
	if (g_strcmp0 (entry->record, "Set Configuration") == 0)
		goto out;

	/* not sure what these are */
	if (g_str_has_suffix (entry->record, " SOF]"))
		goto out;
	if (g_strcmp0 (entry->record, "Clear Endpoint Feature") == 0)
		goto out;

	/* start or end of file */
	if (g_str_has_prefix (entry->record, "Capture started"))
		goto out;
	if (g_strcmp0 (entry->record, "Capture stopped") == 0)
		goto out;

	/* get direction */
	if (g_str_has_prefix (entry->record, "IN txn"))
		entry->direction = CD_PARSE_ENTRY_DIRECTION_REPLY;
	else if (g_strcmp0 (entry->record, "Control Transfer") == 0)
		entry->direction = CD_PARSE_ENTRY_DIRECTION_REQUEST;

	/* get correct string */
	if (entry->direction == CD_PARSE_ENTRY_DIRECTION_REQUEST)
		direction = ">>";
	else if (entry->direction == CD_PARSE_ENTRY_DIRECTION_REPLY)
		direction = "<<";

	/* sexify the output */
	if (kind == CD_SENSOR_KIND_HUEY)
		cd_parse_beagle_process_entry_huey (entry);
	else if (kind == CD_SENSOR_KIND_COLOR_MUNKI_PHOTO)
		cd_parse_beagle_process_entry_colormunki (entry);
	retval = g_strdup_printf ("dev%02i ep%02i(%s)\t%s\t%s\n",
				  entry->dev, entry->ep,
				  entry->ep_description,
				  direction,
				  entry->summary_pretty != NULL ? entry->summary_pretty : entry->summary);
out:
	return retval;
}

/**
 * main:
 **/
gint
main (gint argc, gchar *argv[])
{
	gboolean ret;
	gchar **sections = NULL;
	guint i;
	CdParseEntry entry;
	gchar *part;
	gint retval = 1;
	CdSensorKind kind;
	g_autofree gchar *data = NULL;
	g_auto(GStrv) split = NULL;
	g_autoptr(GError) error = NULL;
	g_autoptr(GString) output = NULL;

	if (argc != 4) {
		g_print ("need to specify [huey|colormunki] input output\n");
		goto out;
	}
	kind = cd_sensor_kind_from_string (argv[1]);
	if (kind != CD_SENSOR_KIND_HUEY &&
	    kind != CD_SENSOR_KIND_DTP94 &&
	    kind != CD_SENSOR_KIND_COLOR_MUNKI_PHOTO) {
		g_print ("only huey and colormunki device kinds supported\n");
		goto out;
	}

	/* read file */
	ret = g_file_get_contents (argv[2], &data, NULL, &error);
	if (!ret) {
		g_print ("failed to read: %s\n", error->message);
		goto out;
	}

	/* parse string */
	output = g_string_new ("// automatically generated, do not edit\n");

	/* parse string */
	split = g_strsplit (data, "\n", -1);
	for (i = 0; split[i] != NULL; i++) {

		/* comment or blank line */
		if (split[i][0] == '#' ||
		    split[i][0] == '\0')
			continue;

		g_print ("@@%u:%s\n", i, split[i]);

		/* populate a CdParseEntry */
		sections = g_strsplit (split[i], ",", -1);
		entry.record = sections[CD_PARSE_SECTION_RECORD];
		entry.summary = sections[CD_PARSE_SECTION_SUMMARY];
		entry.dev = atoi (sections[CD_PARSE_SECTION_DEV]);
		entry.ep = atoi (sections[CD_PARSE_SECTION_EP]);
		entry.direction = CD_PARSE_ENTRY_DIRECTION_UNKNOWN;
		entry.summary_pretty = NULL;
		entry.ep_description = NULL;
		part = cd_parse_beagle_process_entry (kind, &entry);
		if (part != NULL) {
			g_string_append (output, part);
//			g_print ("%s\n", part);
		}
		g_free (part);
		g_strfreev (sections);
	}

	/* write file */
	ret = g_file_set_contents (argv[3], output->str, -1, &error);
	if (!ret) {
		g_print ("failed to read: %s\n", error->message);
		goto out;
	}

	g_print ("done!\n");
	retval = 0;
out:
	return retval;
}