summaryrefslogtreecommitdiff
path: root/src/linux/up-device-wup.c
blob: 73bb8e58d4229eabedd91fb9337e358089146120 (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2006-2008 Richard Hughes <richard@hughsie.com>
 *
 * Data values taken from wattsup.c: Copyright (C) 2005 Patrick Mochel
 *
 * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <string.h>
#include <math.h>

#include <glib.h>
#include <glib-object.h>
#include <gudev/gudev.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <ctype.h>
#include <getopt.h>
#include <errno.h>

#include "sysfs-utils.h"
#include "egg-debug.h"

#include "up-types.h"
#include "up-device-wup.h"

#define UP_DEVICE_WUP_REFRESH_TIMEOUT			10 /* seconds */
#define UP_DEVICE_WUP_RESPONSE_OFFSET_WATTS		0x0
#define UP_DEVICE_WUP_RESPONSE_OFFSET_VOLTS		0x1
#define UP_DEVICE_WUP_RESPONSE_OFFSET_AMPS		0x2
#define UP_DEVICE_WUP_RESPONSE_OFFSET_KWH		0x3
#define UP_DEVICE_WUP_RESPONSE_OFFSET_COST		0x4
#define UP_DEVICE_WUP_RESPONSE_OFFSET_MONTHLY_KWH	0x5
#define UP_DEVICE_WUP_RESPONSE_OFFSET_MONTHLY_COST	0x6
#define UP_DEVICE_WUP_RESPONSE_OFFSET_MAX_WATTS	0x7
#define UP_DEVICE_WUP_RESPONSE_OFFSET_MAX_VOLTS	0x8
#define UP_DEVICE_WUP_RESPONSE_OFFSET_MAX_AMPS	0x9
#define UP_DEVICE_WUP_RESPONSE_OFFSET_MIN_WATTS	0xa
#define UP_DEVICE_WUP_RESPONSE_OFFSET_MIN_VOLTS	0xb
#define UP_DEVICE_WUP_RESPONSE_OFFSET_MIN_AMPS	0xc
#define UP_DEVICE_WUP_RESPONSE_OFFSET_POWER_FACTOR	0xd
#define UP_DEVICE_WUP_RESPONSE_OFFSET_DUTY_CYCLE	0xe
#define UP_DEVICE_WUP_RESPONSE_OFFSET_POWER_CYCLE	0xf

/* commands can never be bigger then this */
#define UP_DEVICE_WUP_COMMAND_LEN			256

struct UpDeviceWupPrivate
{
	guint			 poll_timer_id;
	int			 fd;
};

G_DEFINE_TYPE (UpDeviceWup, up_device_wup, UP_TYPE_DEVICE)
#define UP_DEVICE_WUP_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_DEVICE_WUP, UpDeviceWupPrivate))

static gboolean		 up_device_wup_refresh	 	(UpDevice *device);

/**
 * up_device_wup_poll_cb:
 **/
static gboolean
up_device_wup_poll_cb (UpDeviceWup *wup)
{
	UpDevice *device = UP_DEVICE (wup);

	egg_debug ("Polling: %s", up_device_get_object_path (device));
	up_device_wup_refresh (device);

	/* always continue polling */
	return TRUE;
}

/**
 * up_device_wup_set_speed:
 **/
static gboolean
up_device_wup_set_speed (UpDeviceWup *wup)
{
	struct termios t;
	int retval;

	retval = tcgetattr (wup->priv->fd, &t);
	if (retval != 0) {
		egg_debug ("failed to get speed");
		return FALSE;
	}

	cfmakeraw (&t);
	cfsetispeed (&t, B115200);
	cfsetospeed (&t, B115200);
	tcflush (wup->priv->fd, TCIFLUSH);

	t.c_iflag |= IGNPAR;
	t.c_cflag &= ~CSTOPB;
	retval = tcsetattr (wup->priv->fd, TCSANOW, &t);
	if (retval != 0) {
		egg_debug ("failed to set speed");
		return FALSE;
	}

	return TRUE;
}

/**
 * up_device_wup_write_command:
 *
 * data: a command string in the form "#command,subcommand,datalen,data[n]", e.g. "#R,W,0"
 **/
static gboolean
up_device_wup_write_command (UpDeviceWup *wup, const gchar *data)
{
	guint ret = TRUE;
	gint retval;
	gint length;

	length = strlen (data);
	egg_debug ("writing [%s]", data);
	retval = write (wup->priv->fd, data, length);
	if (retval != length) {
		egg_debug ("Writing [%s] to device failed", data);
		ret = FALSE;
	}
	return ret;
}

/**
 * up_device_wup_read_command:
 *
 * Return value: Some data to parse
 **/
static gchar *
up_device_wup_read_command (UpDeviceWup *wup)
{
	int retval;
	gchar buffer[UP_DEVICE_WUP_COMMAND_LEN];
	retval = read (wup->priv->fd, &buffer, UP_DEVICE_WUP_COMMAND_LEN);
	if (retval < 0) {
		egg_debug ("failed to read from fd: %s", strerror (errno));
		return NULL;
	}
	return g_strdup (buffer);
}

/**
 * up_device_wup_parse_command:
 *
 * Return value: Som data to parse
 **/
static gboolean
up_device_wup_parse_command (UpDeviceWup *wup, const gchar *data)
{
	gboolean ret = FALSE;
	gchar command;
	gchar subcommand;
	gchar **tokens = NULL;
	gchar *packet = NULL;
	guint i;
	guint size;
	guint length;
	guint number_tokens;
	UpDevice *device = UP_DEVICE (wup);
	const guint offset = 3;

	/* invalid */
	if (data == NULL)
		goto out;

	/* Try to find a valid packet in the data stream
	 * Data may be sdfsd#P,-,0;sdfs and we only want this bit:
	 *                  \-----/
	 * so try to find the start and the end */

	/* ensure we have a long enough response */
	length = strlen (data);
	if (length < 3) {
		egg_debug ("not enough data '%s'", data);
		goto out;
	}

	/* strip to the first '#' char */
	for (i=0; i<length-1; i++)
		if (data[i] == '#')
			packet = g_strdup (data+i);

	/* does packet exist? */
	if (packet == NULL) {
		egg_debug ("no start char in %s", data);
		goto out;
	}

	/* replace the first ';' char with a NULL if it exists */
	length = strlen (packet);
	for (i=0; i<length; i++) {
		if (packet[i] < 0x20 && packet[i] > 0x7e)
			packet[i] = '?';
		if (packet[i] == ';') {
			packet[i] = '\0';
			break;
		}
	}

	/* check we have enough data inthe packet */
	tokens = g_strsplit (packet, ",", -1);
	number_tokens = g_strv_length (tokens);
	if (number_tokens < 3) {
		egg_debug ("not enough tokens '%s'", packet);
		goto out;
	}

	/* remove leading or trailing whitespace in tokens */
	for (i=0; i<number_tokens; i++)
		g_strstrip (tokens[i]);

	/* check the first token */
	length = strlen (tokens[0]);
	if (length != 2) {
		egg_debug ("expected command '#?' but got '%s'", tokens[0]);
		goto out;
	}
	if (tokens[0][0] != '#') {
		egg_debug ("expected command '#?' but got '%s'", tokens[0]);
		goto out;
	}
	command = tokens[0][1];

	/* check the second token */
	length = strlen (tokens[1]);
	if (length != 1) {
		egg_debug ("expected command '?' but got '%s'", tokens[1]);
		goto out;
	}
	subcommand = tokens[1][0]; /* expect to be '-' */

	/* check the length is present */
	length = strlen (tokens[2]);
	if (length == 0) {
		egg_debug ("length value not present");
		goto out;
	}

	/* check the length matches what data we've got*/
	size = atoi (tokens[2]);
	if (size != number_tokens - offset) {
		egg_debug ("size expected to be '%i' but got '%i'", number_tokens - offset, size);
		goto out;
	}

	/* update the command fields */
	if (command == 'd' && subcommand == '-' && number_tokens - offset == 18) {
		g_object_set (device,
			      "energy-rate", strtod (tokens[offset+UP_DEVICE_WUP_RESPONSE_OFFSET_WATTS], NULL) / 10.0f,
			      "voltage", strtod (tokens[offset+UP_DEVICE_WUP_RESPONSE_OFFSET_VOLTS], NULL) / 10.0f,
			      NULL);
		ret = TRUE;
	} else {
		egg_debug ("ignoring command '%c'", command);
	}

out:
	g_free (packet);
	g_strfreev (tokens);
	return ret;
}

/**
 * up_device_wup_coldplug:
 *
 * Return %TRUE on success, %FALSE if we failed to get data and should be removed
 **/
static gboolean
up_device_wup_coldplug (UpDevice *device)
{
	UpDeviceWup *wup = UP_DEVICE_WUP (device);
	GUdevDevice *native;
	gboolean ret = FALSE;
	const gchar *device_file;
	const gchar *type;
	const gchar *native_path;
	gchar *data;
	const gchar *vendor;
	const gchar *product;

	/* detect what kind of device we are */
	native = G_UDEV_DEVICE (up_device_get_native (device));
	type = g_udev_device_get_property (native, "UP_MONITOR_TYPE");
	if (type == NULL || g_strcmp0 (type, "wup") != 0)
		goto out;

	/* get the device file */
	device_file = g_udev_device_get_device_file (native);
	if (device_file == NULL) {
		egg_debug ("could not get device file for WUP device");
		goto out;
	}

	/* connect to the device */
	wup->priv->fd = open (device_file, O_RDWR | O_NONBLOCK);
	if (wup->priv->fd < 0) {
		egg_debug ("cannot open device file %s", device_file);
		goto out;
	}
	egg_debug ("opened %s", device_file);

	/* set speed */
	ret = up_device_wup_set_speed (wup);
	if (!ret) {
		egg_debug ("not a WUP device (cannot set speed): %s", device_file);
		goto out;
	}

	/* attempt to clear */
	ret = up_device_wup_write_command (wup, "#R,W,0;");
	if (!ret)
		egg_debug ("failed to clear, nonfatal");

	/* setup logging interval */
	data = g_strdup_printf ("#L,W,3,E,1,%i;", UP_DEVICE_WUP_REFRESH_TIMEOUT);
	ret = up_device_wup_write_command (wup, data);
	if (!ret)
		egg_debug ("failed to setup logging interval, nonfatal");
	g_free (data);

	/* dummy read */
	data = up_device_wup_read_command (wup);
	egg_debug ("data after clear %s", data);

	/* shouldn't do anything */
	up_device_wup_parse_command (wup, data);
	g_free (data);

	/* prefer UPOWER names */
	vendor = g_udev_device_get_property (native, "UPOWER_VENDOR");
	if (vendor == NULL)
		vendor = g_udev_device_get_property (native, "ID_VENDOR");
	product = g_udev_device_get_property (native, "UPOWER_PRODUCT");
	if (product == NULL)
		product = g_udev_device_get_property (native, "ID_PRODUCT");

	/* hardcode some values */
	native_path = g_udev_device_get_sysfs_path (native);
	g_object_set (device,
		      "type", UP_DEVICE_KIND_MONITOR,
		      "is-rechargeable", FALSE,
		      "power-supply", FALSE,
		      "is-present", FALSE,
		      "vendor", vendor,
		      "model", product,
		      "serial", g_strstrip (sysfs_get_string (native_path, "serial")),
		      "has-history", TRUE,
		      "state", UP_DEVICE_STATE_DISCHARGING,
		      NULL);

	/* coldplug */
	egg_debug ("coldplug");
	up_device_wup_refresh (device);

	/* hardcode true, as we'll retry later if busy */
	ret = TRUE;

out:
	return ret;
}

/**
 * up_device_wup_refresh:
 *
 * Return %TRUE on success, %FALSE if we failed to refresh or no data
 **/
static gboolean
up_device_wup_refresh (UpDevice *device)
{
	gboolean ret = FALSE;
	GTimeVal timeval;
	gchar *data = NULL;
	UpDeviceWup *wup = UP_DEVICE_WUP (device);

	/* get data */
	data = up_device_wup_read_command (wup);
	if (data == NULL) {
		egg_debug ("no data");
		goto out;
	}

	/* parse */
	ret = up_device_wup_parse_command (wup, data);
	if (!ret) {
		egg_debug ("failed to parse %s", data);
		goto out;
	}

	/* reset time */
	g_get_current_time (&timeval);
	g_object_set (device, "update-time", (guint64) timeval.tv_sec, NULL);

out:
	g_free (data);
	/* FIXME: always true? */
	return TRUE;
}

/**
 * up_device_wup_init:
 **/
static void
up_device_wup_init (UpDeviceWup *wup)
{
	wup->priv = UP_DEVICE_WUP_GET_PRIVATE (wup);
	wup->priv->fd = -1;
	wup->priv->poll_timer_id = g_timeout_add_seconds (UP_DEVICE_WUP_REFRESH_TIMEOUT,
							  (GSourceFunc) up_device_wup_poll_cb, wup);
#if GLIB_CHECK_VERSION(2,25,8)
	g_source_set_name_by_id (wup->priv->poll_timer_id, "[UpDeviceWup] poll");
#endif
}

/**
 * up_device_wup_finalize:
 **/
static void
up_device_wup_finalize (GObject *object)
{
	UpDeviceWup *wup;

	g_return_if_fail (object != NULL);
	g_return_if_fail (UP_IS_DEVICE_WUP (object));

	wup = UP_DEVICE_WUP (object);
	g_return_if_fail (wup->priv != NULL);

	if (wup->priv->fd > 0)
		close (wup->priv->fd);
	if (wup->priv->poll_timer_id > 0)
		g_source_remove (wup->priv->poll_timer_id);

	G_OBJECT_CLASS (up_device_wup_parent_class)->finalize (object);
}

/**
 * up_device_wup_class_init:
 **/
static void
up_device_wup_class_init (UpDeviceWupClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	UpDeviceClass *device_class = UP_DEVICE_CLASS (klass);

	object_class->finalize = up_device_wup_finalize;
	device_class->coldplug = up_device_wup_coldplug;
	device_class->refresh = up_device_wup_refresh;

	g_type_class_add_private (klass, sizeof (UpDeviceWupPrivate));
}

/**
 * up_device_wup_new:
 **/
UpDeviceWup *
up_device_wup_new (void)
{
	return g_object_new (UP_TYPE_DEVICE_WUP, NULL);
}