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
|
/*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (C) 2014 John Crispin <blogic@openwrt.org>
* Copyright (C) 2021 Nuno Goncalves <nunojpg@gmail.com>
*/
#include <fcntl.h>
#include <termios.h>
#include <libubox/ulog.h>
#include <libubox/ustream.h>
#include <libubox/utils.h>
#include <libubox/uloop.h>
#include <libubus.h>
#define GL_TARGET_XE300 1
#define GL_TARGET_XE3000 2
#if GL_TARGET == GL_TARGET_XE300
#define MCU_PORT "/dev/ttyUSB0"
#elif GL_TARGET == GL_TARGET_XE3000
#define MCU_PORT "/dev/ttyS1"
#else
#error Please define GL_TARGET!
#endif /* GL_TARGET */
static struct ustream_fd stream;
static struct ubus_auto_conn conn;
static struct blob_buf b;
struct Battery
{
float temperature;
uint16_t cycles;
uint8_t soc;
bool charging;
bool set;
} battery;
#if GL_TARGET == GL_TARGET_XE300
// MCU status returns something like:
// {OK},100,275,1,0
static bool
process(char *read)
{
if (read[0] != '{' ||
read[1] != 'O' ||
read[2] != 'K' ||
read[3] != '}' ||
read[4] != ',')
return false;
const char *from = read + 5;
char *to;
battery.soc = strtoul(from, &to, 10);
if (from == to)
return false;
from = to + 1;
battery.temperature = strtoul(from, &to, 10) / 10.0f;
if (from == to)
return false;
if (to[0] != ',' || (to[1] != '0' && to[1] != '1') || to[2] != ',')
return false;
battery.charging = to[1] == '1';
from = to + 3;
battery.cycles = strtoul(from, &to, 10);
if (from == to)
return false;
return true;
}
#elif GL_TARGET == GL_TARGET_XE3000
static bool
get_int_value(const char *read, const char *key, int *int_value, char **new_end)
{
char *from = NULL;
from = strstr(read, key);
if ((!from) || (from != read))
{
return false;
}
from = (char *)read + strlen(key);
*int_value = strtol(from, new_end, 10);
if (from == *new_end)
{
return false;
}
return true;
}
// MCU status returns something like:
// {"code":0,"capacity":100,"temp":28,"chg_state":1,"charge_cycle":0}
static bool
process(char *read)
{
int int_value = 0;
char *to = NULL;
if ((read[0] != '{') ||
(!get_int_value(&read[1], "\"code\":", &int_value, &to)) ||
(int_value != 0))
{
return false;
}
if (!get_int_value(to + 1, "\"capacity\":", &int_value, &to))
{
return false;
}
battery.soc = int_value;
if (!get_int_value(to + 1, "\"temp\":", &int_value, &to))
{
return false;
}
battery.temperature = (float) int_value;
if (!get_int_value(to + 1, "\"chg_state\":", &int_value, &to))
{
return false;
}
battery.charging = (bool) int_value;
if (!get_int_value(to + 1, "\"charge_cycle\":", &int_value, &to))
{
return false;
}
battery.cycles = (uint16_t) int_value;
return true;
}
#endif /* GL_TARGET */
static int
consume(struct ustream *s, char **a)
{
char *eol = strstr(*a, "\n");
if (!eol)
return -1;
*eol++ = '\0';
battery.set = process(*a);
if (!battery.set)
ULOG_ERR("failed to parse message from serial: %s", *a);
ustream_consume(s, eol - *a);
*a = eol;
return 0;
}
static void
msg_cb(struct ustream *s, int bytes)
{
int len;
char *a = ustream_get_read_buf(s, &len);
while (!consume(s, &a))
;
}
static void
notify_cb(struct ustream *s)
{
if (!s->eof)
return;
ULOG_ERR("tty error, shutting down\n");
exit(-1);
}
static int
serial_open(char *dev)
{
const int tty = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (tty < 0)
{
ULOG_ERR("%s: device open failed: %s\n", dev, strerror(errno));
return -1;
}
struct termios config;
tcgetattr(tty, &config);
cfmakeraw(&config);
cfsetispeed(&config, B9600);
cfsetospeed(&config, B9600);
tcsetattr(tty, TCSANOW, &config);
stream.stream.string_data = true;
stream.stream.notify_read = msg_cb;
stream.stream.notify_state = notify_cb;
ustream_fd_init(&stream, tty);
tcflush(tty, TCIFLUSH);
return 0;
}
static struct uloop_timeout serial_query_timer;
static void
serial_query_handler(struct uloop_timeout *timeout)
{
const char cmd[] = "{ \"mcu_status\": \"1\" }\n";
const unsigned cmd_len = sizeof(cmd) - 1;
ustream_write(&stream.stream, cmd, cmd_len, false);
uloop_timeout_set(&serial_query_timer, 3000); // timeout in 3 sec
uloop_timeout_add(timeout);
}
static int
battery_info(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
blob_buf_init(&b, 0);
if (!battery.set)
{
blobmsg_add_u8(&b, "error", 1);
}
else
{
blobmsg_add_u16(&b, "soc", battery.soc);
blobmsg_add_u8(&b, "charging", battery.charging);
blobmsg_add_double(&b, "temperature", battery.temperature);
blobmsg_add_u16(&b, "cycles", battery.cycles);
}
ubus_send_reply(ctx, req, b.head);
return UBUS_STATUS_OK;
}
static const struct ubus_method battery_methods[] = {
UBUS_METHOD_NOARG("info", battery_info),
};
static struct ubus_object_type battery_object_type =
UBUS_OBJECT_TYPE("battery", battery_methods);
static struct ubus_object battery_object = {
.name = "battery",
.type = &battery_object_type,
.methods = battery_methods,
.n_methods = ARRAY_SIZE(battery_methods),
};
static void
ubus_connect_handler(struct ubus_context *ctx)
{
int ret;
ret = ubus_add_object(ctx, &battery_object);
if (ret)
fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
}
int
main(int argc, char **argv)
{
uloop_init();
conn.path = NULL;
conn.cb = ubus_connect_handler;
ubus_auto_connect(&conn);
if (serial_open(MCU_PORT) < 0)
return -1;
serial_query_timer.cb = serial_query_handler;
serial_query_handler(&serial_query_timer);
uloop_run();
uloop_done();
return 0;
}
|