-
Notifications
You must be signed in to change notification settings - Fork 4
/
libskycharge-dummy.c
558 lines (465 loc) · 13 KB
/
libskycharge-dummy.c
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
* Copyright (C) 2021-2022 Skycharge GmbH
* Author: Roman Penyaev <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <gps.h>
#include "libskycharge-pri.h"
#include "libskybms.h"
#include "types.h"
struct skydum_dev {
struct sky_dev dev;
struct sky_charging_state state;
struct sky_dev_params params;
struct sky_dev_desc devdesc;
struct gps_data_t gpsdata;
struct bms_lib bms;
bool gps_nodev;
struct sky_droneport_state dp_state;
unsigned capacity_mAh;
unsigned cur, vol;
unsigned long long ts_ms;
};
static int skydum_peerinfo(const struct sky_dev_ops *ops,
const struct sky_conf *conf,
struct sky_peerinfo *peerinfo)
{
return -EOPNOTSUPP;
}
static int skydum_devslist(const struct sky_dev_ops *ops,
const struct sky_conf *conf,
struct sky_dev_desc **head)
{
struct sky_dev_desc *dev;
dev = calloc(1, sizeof(*dev));
if (!dev)
return -ENOMEM;
dev->dev_type = SKY_MUX_HW2;
dev->proto_version = 0;
dev->hw_info = (struct sky_hw_info) {
.fw_version = 0x00010203,
.hw_version = 0x00040302,
.plc_proto_version = 0x12345678,
.uid.part1 = rand(),
.uid.part2 = rand(),
.uid.part3 = rand(),
};
dev->conf = *conf;
dev->dev_ops = ops;
strcpy(dev->portname, "dummy0");
dev->next = *head;
*head = dev;
return 0;
}
static unsigned rand_between(unsigned M, unsigned N)
{
return M + rand() / (RAND_MAX / (N - M + 1) + 1);
}
static int skydum_devopen(const struct sky_dev_desc *devdesc,
struct sky_dev **dev_)
{
struct skydum_dev *dev;
struct timespec ts;
int rc;
clock_gettime(CLOCK_REALTIME, &ts);
srand(ts.tv_nsec * ts.tv_sec);
dev = calloc(1, sizeof(*dev));
if (!dev)
return -ENOMEM;
rc = gps_open(GPSD_SHARED_MEMORY, NULL, &dev->gpsdata);
if (rc)
/* Do not make much noise if GPS does not exist */
dev->gps_nodev = true;
dev->dp_state.status |= (SKY_DP_IS_READY | SKY_DP_IS_CLOSED);
bms_init(&dev->bms);
dev->capacity_mAh = 500;
dev->cur = rand_between(5000, 15000);
dev->vol = rand_between(16000, 51000);
dev->ts_ms = msecs_epoch();
*dev_ = &dev->dev;
return 0;
}
static void skydum_devclose(struct sky_dev *dev_)
{
struct skydum_dev *dev;
dev = container_of(dev_, struct skydum_dev, dev);
bms_deinit(&dev->bms);
if (!dev->gps_nodev)
gps_close(&dev->gpsdata);
free(dev);
}
static int skydum_paramsget(struct sky_dev *dev_, struct sky_dev_params *params)
{
struct skydum_dev *dev;
int i;
if (params->dev_params_bits == 0)
return -EINVAL;
dev = container_of(dev_, struct skydum_dev, dev);
for (i = 0; i < SKY_HW2_NUM_DEVPARAM; i++) {
if (!(params->dev_params_bits & (1<<i)))
continue;
params->dev_params[i] = dev->params.dev_params[i];
}
return 0;
}
static int skydum_paramsset(struct sky_dev *dev_,
const struct sky_dev_params *params)
{
struct skydum_dev *dev;
int i;
if (params->dev_params_bits == 0)
return 0;
dev = container_of(dev_, struct skydum_dev, dev);
for (i = 0; i < SKY_HW2_NUM_DEVPARAM; i++) {
if (!(params->dev_params_bits & (1<<i)))
continue;
dev->params.dev_params[i] = params->dev_params[i];
}
return 0;
}
static int skydum_chargingstate(struct sky_dev *dev_,
struct sky_charging_state *state)
{
struct bms_data bms_data;
struct skydum_dev *dev;
float dt;
int rc;
dev = container_of(dev_, struct skydum_dev, dev);
dt = (msecs_epoch() - dev->ts_ms) / 1000.0;
dev->state.mux_temperature_C = rand_between(43, 56);
dev->state.sink_temperature_C = rand_between(55, 64);
dev->state.current_mA = dev->cur;
dev->state.voltage_mV = dev->vol;
dev->state.charging_secs = dt;
dev->state.energy_mWh = dev->vol * dev->cur / 1000 * (dt / 3600.0);
dev->state.charge_mAh = dev->cur * (dt / 3600.0);
dev->state.dev_hw_state = SKY_HW2_SCANNING;
*state = dev->state;
rc = bms_request_data(&dev->bms, &bms_data);
if (!rc) {
state->until_full_secs = bms_data.charge_time;
state->state_of_charge = bms_data.charge_perc;
} else {
unsigned int until_full_mAh;
state->state_of_charge =
(float)dev->state.charge_mAh / dev->capacity_mAh * 100;
if (state->state_of_charge >= 100) {
/* Start new charging */
dev->ts_ms = msecs_epoch();
dev->state.current_mA = dev->cur =
rand_between(5000, 15000);
dev->state.voltage_mV = dev->vol =
rand_between(16000, 51000);
state->state_of_charge = 0;
dev->state.charging_secs = 0;
dev->state.energy_mWh = 0;
dev->state.charge_mAh = 0;
}
until_full_mAh = dev->capacity_mAh - dev->state.charge_mAh;
state->until_full_secs =
(float)until_full_mAh / dev->cur * 3600.0;
}
return 0;
}
static int
skydum_subscribe(struct sky_dev *dev_, struct sky_subscription_token *token)
{
/* Nop for now */
return 0;
}
static void skydum_unsubscribe(struct sky_dev *dev_)
{
/* Nop for now */
}
static int skydum_subscription_work(struct sky_dev *dev_,
struct sky_charging_state *state)
{
return skydum_chargingstate(dev_, state);
}
static int skydum_reset(struct sky_dev *dev_)
{
return -EINVAL;
}
static int skydum_scanresume(struct sky_dev *dev_)
{
return 0;
}
static int skydum_scanstop(struct sky_dev *dev_)
{
return 0;
}
static int skydum_droneport_open(struct sky_dev *dev_)
{
struct skydum_dev *dev;
dev = container_of(dev_, struct skydum_dev, dev);
if (dev->dp_state.status & SKY_DP_IS_OPENED)
return -EALREADY;
dev->dp_state.status |= SKY_DP_IS_OPENED;
dev->dp_state.status &= ~SKY_DP_IS_CLOSED;
return 0;
}
static int skydum_droneport_close(struct sky_dev *dev_)
{
struct skydum_dev *dev;
dev = container_of(dev_, struct skydum_dev, dev);
if (dev->dp_state.status & SKY_DP_IS_CLOSED)
return -EALREADY;
dev->dp_state.status |= SKY_DP_IS_CLOSED;
dev->dp_state.status &= ~SKY_DP_IS_OPENED;
return 0;
}
static int
skydum_droneport_state(struct sky_dev *dev_, struct sky_droneport_state *state)
{
struct skydum_dev *dev;
dev = container_of(dev_, struct skydum_dev, dev);
state->status = dev->dp_state.status;
return 0;
}
static int skydum_gpsdata(struct sky_dev *dev_, struct sky_gpsdata *gpsdata)
{
struct skydum_dev *dev;
struct timespec ts;
double msecs;
int rc;
dev = container_of(dev_, struct skydum_dev, dev);
if (!dev->gps_nodev) {
#if GPSD_API_MAJOR_VERSION <= 6
rc = gps_read(&dev->gpsdata);
#else
rc = gps_read(&dev->gpsdata, NULL, 0);
#endif
if (rc < 0)
return -EOPNOTSUPP;
memset(gpsdata, 0, sizeof(*gpsdata));
#if GPSD_API_MAJOR_VERSION < 10
gpsdata->status = dev->gpsdata.status;
#else
gpsdata->status = dev->gpsdata.fix.status;
#endif
gpsdata->satellites_used = dev->gpsdata.satellites_used;
BUILD_BUG_ON(sizeof(gpsdata->dop) != sizeof(dev->gpsdata.dop));
memcpy(&gpsdata->dop, &dev->gpsdata.dop, sizeof(gpsdata->dop));
gpsdata->fix.mode = dev->gpsdata.fix.mode;
#if GPSD_API_MAJOR_VERSION < 9
gpsdata->fix.time = dev->gpsdata.fix.time;
#else
{
double tstamp = dev->gpsdata.fix.time.tv_sec;
tstamp += dev->gpsdata.fix.time.tv_nsec / 1000000000.0;
gpsdata->fix.time = tstamp;
}
#endif
gpsdata->fix.latitude = dev->gpsdata.fix.latitude;
gpsdata->fix.longitude = dev->gpsdata.fix.longitude;
gpsdata->fix.altitude = dev->gpsdata.fix.altitude;
} else {
/*
* No real GPS, output dummy stuff
*/
memset(gpsdata, 0, sizeof(*gpsdata));
clock_gettime(CLOCK_REALTIME, &ts);
msecs = ts.tv_nsec / 1000000ull;
/*
* integer to fractional, e.g. 123.0 -> 0.123,
* the equation is: x / 10 ^ ceil(log10(msecs))
*/
while (msecs > 1.0)
msecs /= 10;
#if GPSD_API_MAJOR_VERSION < 9
gpsdata->fix.time = (double)ts.tv_sec + msecs;
#else
{
double tstamp = ts.tv_sec;
tstamp += ts.tv_nsec / 1000000000.0;
gpsdata->fix.time = tstamp;
}
#endif
gpsdata->fix.latitude = 12345.54321;
gpsdata->fix.longitude = 54321.12345;
gpsdata->fix.altitude = NAN;
gpsdata->status = SKY_GPS_STATUS_DGPS_FIX;
gpsdata->fix.mode = SKY_GPS_MODE_3D;
gpsdata->satellites_used = 42;
gpsdata->dop.pdop = 12.21;
gpsdata->dop.hdop = 21.12;
gpsdata->dop.vdop = 23.32;
gpsdata->dop.tdop = 34.43;
gpsdata->dop.gdop = 54.43;
}
return 0;
}
static int skydum_dronedetect(struct sky_dev *dev_,
enum sky_drone_status *status)
{
*status = SKY_DRONE_NOT_DETECTED;
return 0;
}
static bool skydum_asyncreq_cancel(struct sky_async *async,
struct sky_async_req *req)
{
bool res = false;
if (req->next != req) {
/* Request still in the submit queue */
assert(!req->tag);
sky_asyncreq_del(req);
res = true;
}
return res;
}
static int skydum_asyncopen(const struct sky_conf *conf,
const struct sky_dev_ops *ops,
struct sky_async **async_)
{
struct sky_async *async;
async = calloc(1, sizeof(*async));
if (!async)
return -ENOMEM;
sky_async_init(conf, ops, async);
*async_ = async;
return 0;
}
static void skydum_asyncclose(struct sky_async *async)
{
while (!sky_async_empty(async)) {
struct sky_async_req *req;
req = sky_asyncreq_pop(async);
sky_asyncreq_complete(async, req, -EIO);
}
free(async);
}
static int skydum_asyncfd(struct sky_async *async)
{
/* Probably in future we support that */
return -EOPNOTSUPP;
}
static int skydum_asyncreq_execute(struct sky_async *async,
struct sky_async_req *req)
{
int rc;
switch (req->type) {
case SKY_GET_DEV_PARAMS_REQ:
rc = skydum_paramsget(req->dev, req->out.ptr);
break;
case SKY_SET_DEV_PARAMS_REQ:
rc = skydum_paramsset(req->dev, req->in.ptr);
break;
case SKY_RESUME_SCAN_REQ:
rc = skydum_scanresume(req->dev);
break;
case SKY_STOP_SCAN_REQ:
rc = skydum_scanstop(req->dev);
break;
case SKY_OPEN_DRONEPORT_REQ:
rc = skydum_droneport_open(req->dev);
break;
case SKY_CLOSE_DRONEPORT_REQ:
rc = skydum_droneport_close(req->dev);
break;
case SKY_DRONEPORT_STATE_REQ:
rc = skydum_droneport_state(req->dev, req->out.ptr);
break;
case SKY_RESET_DEV_REQ:
rc = skydum_reset(req->dev);
break;
case SKY_CHARGING_STATE_REQ:
rc = skydum_chargingstate(req->dev, req->out.ptr);
break;
case SKY_DEVS_LIST_REQ:
rc = skydum_devslist(async->ops, async->conf, req->out.ptr);
break;
case SKY_PEERINFO_REQ:
rc = skydum_peerinfo(async->ops, async->conf, req->out.ptr);
break;
case SKY_GPSDATA_REQ:
rc = skydum_gpsdata(req->dev, req->out.ptr);
break;
case SKY_DRONEDETECT_REQ:
rc = skydum_dronedetect(req->dev, req->out.ptr);
break;
case SKY_SINK_GET_DEV_PARAMS_REQ:
rc = -EOPNOTSUPP;
break;
case SKY_SINK_SET_DEV_PARAMS_REQ:
rc = -EOPNOTSUPP;
break;
case SKY_SINK_GET_INFO_REQ:
rc = -EOPNOTSUPP;
break;
case SKY_SINK_START_CHARGE_REQ:
rc = -EOPNOTSUPP;
break;
case SKY_SINK_STOP_CHARGE_REQ:
rc = -EOPNOTSUPP;
break;
case SKY_GET_SUBSCRIPTION_TOKEN_REQ:
rc = -EOPNOTSUPP;
break;
default:
/* Consider fatal */
sky_err("Unknown request: %d\n", req->type);
rc = -EINVAL;
break;
}
return rc;
}
static int skydum_asyncexecute(struct sky_async *async, bool wait)
{
int completed = 0;
while (!sky_async_empty(async)) {
struct sky_async_req *req;
int rc;
req = sky_asyncreq_pop(async);
rc = skydum_asyncreq_execute(async, req);
sky_asyncreq_complete(async, req, rc);
completed++;
}
return completed;
}
static struct sky_dev_ops sky_dummy_devops = {
.contype = SKY_DUMMY,
.asyncopen = skydum_asyncopen,
.asyncclose = skydum_asyncclose,
.asyncexecute = skydum_asyncexecute,
.asyncfd = skydum_asyncfd,
.asyncreq_cancel = skydum_asyncreq_cancel,
.devopen = skydum_devopen,
.devclose = skydum_devclose,
.subscribe = skydum_subscribe,
.unsubscribe = skydum_unsubscribe,
.subscription_work = skydum_subscription_work,
};
sky_register_devops(&sky_dummy_devops);