summaryrefslogtreecommitdiff
path: root/common/battery_commands.c
blob: c4f282c6990333588593a0a52f1641f017a948a4 (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
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Battery host commands for Chrome EC
 */


#include "host_command.h"
#include "smart_battery.h"
#include "battery.h"

static inline uint8_t hex2asc(uint8_t hex)
{
	return hex + ((hex > 9) ? 'A' : '0');
}

enum lpc_status battery_command_get_info(uint8_t *data)
{
	struct lpc_response_battery_info *r =
			(struct lpc_response_battery_info *)data;
	int val;

	if (battery_design_capacity(&val))
		return EC_LPC_RESULT_ERROR;
	r->design_capacity         = val;
	r->design_capacity_warning = val * BATTERY_LEVEL_WARNING / 100;
	r->design_capacity_low     = val * BATTERY_LEVEL_LOW / 100;

	if (battery_full_charge_capacity(&val))
		return EC_LPC_RESULT_ERROR;
	r->last_full_charge_capacity = val;

	if (battery_design_voltage(&val))
		return EC_LPC_RESULT_ERROR;
	r->design_output_voltage = val;

	if (battery_cycle_count(&val))
		return EC_LPC_RESULT_ERROR;
	r->cycle_count = val;

	return EC_LPC_RESULT_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_LPC_COMMAND_BATTERY_INFO,
		battery_command_get_info);

enum lpc_status battery_command_get_type(uint8_t *data)
{
	struct lpc_response_battery_text *r =
			(struct lpc_response_battery_text *)data;

	if (battery_device_chemistry(r->text, sizeof(r->text)))
		return EC_LPC_RESULT_ERROR;

	return EC_LPC_RESULT_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_LPC_COMMAND_BATTERY_TYPE,
		battery_command_get_type);

enum lpc_status battery_command_get_model_number(uint8_t *data)
{
	struct lpc_response_battery_text *r =
			(struct lpc_response_battery_text *)data;

	if (battery_device_name(r->text, sizeof(r->text)))
		return EC_LPC_RESULT_ERROR;

	return EC_LPC_RESULT_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_LPC_COMMAND_BATTERY_MODEL_NUMBER,
		battery_command_get_model_number);

enum lpc_status battery_command_get_serial_number(uint8_t *data)
{
	struct lpc_response_battery_text *r =
			(struct lpc_response_battery_text *)data;
	int serial;

	if (battery_serial_number(&serial))
		return EC_LPC_RESULT_ERROR;

	/* Smart battery serial number is 16 bits */
	r->text[0] = hex2asc(0xf & (serial >> 12));
	r->text[1] = hex2asc(0xf & (serial >> 8));
	r->text[2] = hex2asc(0xf & (serial >> 4));
	r->text[3] = hex2asc(0xf & serial);
	r->text[4] = 0;

	return EC_LPC_RESULT_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_LPC_COMMAND_BATTERY_SERIAL_NUMBER,
		battery_command_get_serial_number);

enum lpc_status battery_command_get_oem(uint8_t *data)
{
	struct lpc_response_battery_text *r =
			(struct lpc_response_battery_text *)data;

	if (battery_manufacturer_name(r->text, sizeof(r->text)))
		return EC_LPC_RESULT_ERROR;

	return EC_LPC_RESULT_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_LPC_COMMAND_BATTERY_OEM,
		battery_command_get_oem);