-
Notifications
You must be signed in to change notification settings - Fork 4
/
libskycharge.h
1592 lines (1442 loc) · 44.6 KB
/
libskycharge.h
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2021-2022 Skycharge GmbH
* Author: Roman Penyaev <r.peniaev@gmail.com>
*
* 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.
*/
#ifndef LIBSKYCHARGE_H
#define LIBSKYCHARGE_H
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <netinet/in.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* struct sky_dev - Opaque device context.
*/
struct sky_dev;
/**
* struct sky_async - Opaque async context.
*/
struct sky_async;
/**
* enum sky_con_type - Connection type.
*/
enum sky_con_type {
SKY_LOCAL = 0,
SKY_REMOTE = 1,
SKY_DUMMY = 2,
};
/**
* enum sky_dev_type - Device type.
*/
enum sky_dev_type {
SKY_MUX_HW1 = 0,
SKY_MUX_HW2 = 1,
};
/**
* enum sky_batt_type - Battery type.
*/
enum sky_batt_type {
SKY_BATT_LIPO = 0,
SKY_BATT_LION,
};
/**
* enum sky_uart_config_enum - Sink user UART port configuration enum
*/
enum sky_uart_config_enum {
SKY_UART_CFG_WORDSIZE_8B = 0,
SKY_UART_CFG_WORDSIZE_9B = 1,
SKY_UART_CFG_STOPBITS_1 = 0,
SKY_UART_CFG_STOPBITS_2 = 1,
SKY_UART_CFG_PARITY_NONE = 0,
SKY_UART_CFG_PARITY_EVEN = 1,
SKY_UART_CFG_PARITY_ODD = 3,
};
/**
* union sky_uart_config - Sink user UART port configuration
*/
union sky_uart_config {
uint32_t cfg32b;
struct {
uint32_t baud_rate:18;
uint32_t word_size:1;
uint32_t stop_bits:1;
uint32_t parity:2;
};
};
/**
* enum sky_param_value_format - Specifies how parameter value should
* be reprensented.
*/
enum sky_param_value_format {
SKY_PARAM_VALUE_TEXT = 0,
SKY_PARAM_VALUE_TEXT_AND_NUMERIC,
};
/**
* enum sky_detect_mode - Detect mode.
*/
enum sky_detect_mode {
SKY_DETECT_PLC = 0,
SKY_DETECT_RESISTANCE,
SKY_DETECT_CAPACITY,
};
/**
* enum sky_dev_state - Hardware state of the device.
*/
enum sky_dev_state {
/* HW1 */
SKY_HW1_UNKNOWN = 0,
SKY_HW1_SCANNING_INIT = 1,
SKY_HW1_SCANNING_RUN = 2,
SKY_HW1_SCANNING_CHECK_MATRIX = 3,
SKY_HW1_SCANNING_PRINT = 4,
SKY_HW1_SCANNING_CHECK_WATER = 5,
SKY_HW1_SCANNING_WET = 6,
SKY_HW1_SCANNING_DETECTING = 7,
SKY_HW1_PRE_CHARGING_INIT = 8,
SKY_HW1_PRE_CHARGING_RUN = 9,
SKY_HW1_PRE_CHARGING_CHECK_MATRIX = 10,
SKY_HW1_PRE_CHARGING_PRINT = 11,
SKY_HW1_PRE_CHARGING_CHECK_WATER = 12,
SKY_HW1_PRE_CHARGING_WET = 13,
SKY_HW1_PRE_CHARGING_FIND_CHARGERS = 14,
SKY_HW1_CHARGING_INIT = 15,
SKY_HW1_CHARGING_RUN = 16,
SKY_HW1_CHARGING_MONITOR_CURRENT = 17,
SKY_HW1_POST_CHARGING_INIT = 18,
SKY_HW1_POST_CHARGING_RUN = 19,
SKY_HW1_POST_CHARGING_CHECK_MATRIX = 20,
SKY_HW1_POST_CHARGING_PRINT = 21,
SKY_HW1_POST_CHARGING_CHECK_WATER = 22,
SKY_HW1_POST_CHARGING_WET = 23,
SKY_HW1_POST_CHARGING_FIND_CHARGERS = 24,
SKY_HW1_OVERLOAD = 25,
SKY_HW1_AUTOSCAN_DISABLED = 250,
/* HW2 */
SKY_HW2_STOPPED = 0x00,
SKY_HW2_SCANNING = 0x01,
SKY_HW2_LINK_ESTABLISHED = 0x02,
SKY_HW2_PRECHARGE_DELAYED = 0x03,
SKY_HW2_PRECHARGING = 0x04,
SKY_HW2_CHARGING = 0x05,
SKY_HW2_CHARGING_FINISHED = 0x06,
/* HW2 error states */
SKY_HW2_ERR_BAD_LINK = 0x80,
SKY_HW2_ERR_LOW_MUX_VOLTAGE = 0x81,
SKY_HW2_ERR_VOLTAGE_ON_OUTPUT = 0x82,
SKY_HW2_ERR_SHORT_CIRCUIT = 0x83,
};
enum sky_dev_charging_finished_reason {
SKY_HW2_UNKNOWN_REASON = 0x00,
SKY_HW2_REACHED_CURRENT_CUTOFF = 0x01,
SKY_HW2_CHARGING_TIME_ELAPSED = 0x02,
SKY_HW2_CHARGING_FORCIBLY_STOPPED = 0x03,
SKY_HW2_CRITICAL_TEMP_ON_MUX = 0x04,
SKY_HW2_CRITICAL_TEMP_ON_SINK = 0x05,
SKY_HW2_INVAL_CHARGING_SETTINGS = 0x06,
SKY_HW2_LOW_BATT_VOLTAGE = 0x07,
};
/**
* enum sky_dev_param - Configuration MUX device parameter.
*/
enum sky_dev_param {
/* HW1 */
SKY_HW1_EEPROM_INITED = 0,
SKY_HW1_SCANNING_INTERVAL = 1,
SKY_HW1_PRECHARGING_INTERVAL = 2,
SKY_HW1_PRECHARGING_COUNTER = 3,
SKY_HW1_POSTCHARGING_INTERVAL = 4,
SKY_HW1_POSTCHARGING_DELAY = 5,
SKY_HW1_WET_DELAY = 6,
SKY_HW1_SHORTCIRC_DELAY = 7,
SKY_HW1_THRESH_FINISH_CHARGING = 8,
SKY_HW1_THRESH_NOCHARGER_PRESENT = 9,
SKY_HW1_THRESH_SHORTCIRC = 10,
SKY_HW1_CURRENT_MON_INTERVAL = 11,
SKY_HW1_WAIT_START_CHARGING_SEC = 12,
SKY_HW1_NUM_DEVPARAM, /* Should be the last for HW1 */
/* HW2 */
SKY_HW2_PSU_TYPE = 0,
SKY_HW2_DETECT_MODE = 1,
SKY_HW2_PSU_FIXED_VOLTAGE_MV = 2,
SKY_HW2_PSU_FIXED_CURRENT_MA = 3,
SKY_HW2_USE_FIXED_V_I = 4,
SKY_HW2_NR_BAD_HEARTBEATS = 5,
SKY_HW2_IGNORE_INVAL_CHARGING_SETTINGS = 6,
SKY_HW2_IGNORE_LOW_BATT_VOLTAGE = 7,
SKY_HW2_ERROR_INDICATION_TIMEOUT_SECS = 8,
SKY_HW2_KEEP_SILENCE = 9,
SKY_HW2_IGNORE_VOLTAGE_ON_OUTPUT = 10,
SKY_HW2_MIN_SENSE_CURRENT_MA = 11,
SKY_HW2_REPEAT_CHARGE_AFTER_MINS = 12,
SKY_HW2_SENSE_VOLTAGE_CALIB_POINT1_MV = 13,
SKY_HW2_SENSE_VOLTAGE_CALIB_POINT2_MV = 14,
SKY_HW2_SENSE_CURRENT_CALIB_POINT1_MA = 15,
SKY_HW2_SENSE_CURRENT_CALIB_POINT2_MA = 16,
SKY_HW2_PSU_VOLTAGE_CALIB_POINT1_MV = 17,
SKY_HW2_PSU_VOLTAGE_CALIB_POINT2_MV = 18,
SKY_HW2_PSU_CURRENT_CALIB_POINT1_MA = 19,
SKY_HW2_PSU_CURRENT_CALIB_POINT2_MA = 20,
SKY_HW2_NUM_DEVPARAM, /* Should be the last for HW2 */
};
/**
* enum sky_sink_capabilities - Capabilities bits of the sink
*/
enum sky_sink_capabilities {
SKY_CAP_PLC_WHILE_CHARGING = 0,
SKY_CAP_EXTENDED_PASSTHRU = 1,
};
/**
* enum sky_sink_param - Configuration sink device parameter.
*/
enum sky_sink_param {
SKY_SINK_CAPABILITIES = 0,
SKY_SINK_BATT_TYPE = 1,
SKY_SINK_BATT_CAPACITY_MAH = 2,
SKY_SINK_BATT_MIN_VOLTAGE_MV = 3,
SKY_SINK_BATT_MAX_VOLTAGE_MV = 4,
SKY_SINK_CHARGING_MAX_CURRENT_MA = 5,
SKY_SINK_CUTOFF_MIN_CURRENT_MA = 6,
SKY_SINK_CUTOFF_TIMEOUT_MS = 7,
SKY_SINK_PRECHARGE_CURRENT_MA = 8,
SKY_SINK_PRECHARGE_DELAY_SECS = 9,
SKY_SINK_PRECHARGE_SECS = 10,
SKY_SINK_TOTAL_CHARGE_SECS = 11,
SKY_SINK_USER_UART_CONFIG = 12,
SKY_SINK_USER_DATA1 = 13,
SKY_SINK_USER_DATA2 = 14,
SKY_SINK_USER_DATA3 = 15,
SKY_SINK_USER_DATA4 = 16,
SKY_SINK_NUM_DEVPARAM, /* Should be the last for sink */
};
/**
* struct sky_dev_params - Device configuration parameters.
*/
struct sky_dev_params {
uint32_t dev_params_bits;
uint32_t dev_params[32];
};
/**
* enum sky_drone_status - Drone status on charging pad.
*/
enum sky_drone_status {
SKY_DRONE_NOT_DETECTED = 0,
SKY_DRONE_DETECTED = 1,
};
/**
* enum sky_psu_type - Power supply unit type.
*/
enum sky_psu_type {
SKY_PSU_UNKNOWN = 0,
SKY_PSU_RSP_750_48 = 1,
SKY_PSU_RSP_1600_48 = 2,
SKY_PSU_RSP_1600_24 = 3,
SKY_PSU_RSP_750_24 = 4,
};
/**
* enum sky_dp_hw_interface - HW interface between BBone and DP
*/
enum sky_dp_hw_interface {
SKY_DP_UNKNOWN = 0,
SKY_DP_GPIO,
};
/**
* struct sky_conf - Generic configuration options.
*/
struct sky_conf {
uint8_t usruuid[16];
uint8_t devuuid[16];
char devname[16];
char hostname[64];
unsigned srvport; /**< Servers command port */
unsigned subport; /**< Servers subscribe port */
unsigned cliport; /**< Clients command port */
unsigned pubport; /**< Clients publish port */
enum sky_con_type contype;
enum sky_dev_type mux_type;
char mux_dev[32];
struct sky_dev_params mux_hw1_params;
struct sky_dev_params mux_hw2_params;
struct sky_dev_params sink_params;
struct {
enum sky_psu_type type;
float voltage;
float current;
float precharge_current;
float precharge_current_coef;
unsigned precharge_secs;
} psu;
struct {
enum sky_dp_hw_interface hw_interface;
struct {
/* Before changing the size check parse function */
char open_pin[8];
char close_pin[8];
char is_opened_pin[8];
char is_closed_pin[8];
char in_progress_pin[8];
char is_landing_err_pin[8];
char is_ready_pin[8];
char is_drone_detected_pin[8];
} gpio;
} dp;
};
struct sky_dev_ops;
struct sky_hw_ops;
/**
* struct sky_hw_uid - HW unique id
*/
struct sky_hw_uid {
uint32_t part1;
uint32_t part2;
uint32_t part3;
};
/**
* struct sky_hw_info - HW information
*/
struct sky_hw_info {
uint32_t fw_version;
uint32_t hw_version;
uint32_t plc_proto_version;
struct sky_hw_uid uid;
};
/**
* struct sky_dev_desc - Device descriptor.
*/
struct sky_dev_desc {
struct sky_dev_desc *next;
struct sky_conf conf;
enum sky_dev_type dev_type;
const struct sky_dev_ops *dev_ops;
const struct sky_hw_ops *hw_ops;
char dev_name[16];
unsigned char dev_uuid[16];
char portname[32];
uint16_t proto_version;
struct sky_hw_info hw_info;
};
/**
* struct sky_sink_info - Sink device information
*/
struct sky_sink_info {
struct sky_hw_info hw_info;
};
/**
* struct sky_flash_chunk - Flash chunk structure
*/
struct sky_flash_chunk {
uint32_t addr;
uint16_t crc16;
uint16_t size;
uint8_t buf[128];
};
/**
* struct sky_flash_info - Flash info structure
*/
struct sky_flash_info {
uint32_t real_addr;
uint32_t set_addr;
uint16_t page_size;
uint16_t max_chunk_size;
};
/**
* struct sky_passthru_msg - Passthru message
*/
struct sky_passthru_msg {
uint8_t len;
uint8_t buf[128];
};
#define make_version(min, maj, rev) (((maj) & 0xff) << 16 | ((min) & 0xff) << 8 | ((rev) & 0xff))
#define version_major(v) (((v) >> 16) & 0xff)
#define version_minor(v) (((v) >> 8) & 0xff)
#define version_revis(v) ((v) & 0xff)
#define foreach_devdesc(devdesc, head) \
for (devdesc = head; devdesc; devdesc = devdesc->next)
/**
* struct sky_charging_state - Charging device state.
*/
struct sky_charging_state {
uint8_t dev_hw_state; /* enum sky_dev_state */
uint8_t dev_hw_reason; /* enum sky_dev_charging_finished_reason */
uint16_t voltage_mV;
uint16_t current_mA;
uint16_t state_of_charge; /* 0-100% */
uint16_t until_full_secs;
uint16_t charging_secs;
int16_t mux_temperature_C;
int16_t sink_temperature_C;
uint32_t energy_mWh;
uint32_t charge_mAh;
uint8_t mux_humidity_perc; /* 0-100% */
uint8_t link_quality_factor; /* 0-100% */
uint8_t passthru_recv_bytes;
uint8_t padding;
struct {
uint32_t bytes;
uint32_t packets;
uint32_t err_bytes;
uint32_t err_packets;
} tx;
struct {
uint32_t bytes;
uint32_t packets;
uint32_t err_bytes;
uint32_t err_packets;
} rx;
uint32_t charging_session_id; /* Random ID for each charging session */
};
enum sky_droneport_status {
SKY_DP_IS_READY = 1<<0,
SKY_DP_IS_OPENED = 1<<1,
SKY_DP_IS_CLOSED = 1<<2,
SKY_DP_IN_PROGRESS = 1<<3,
SKY_DP_LANDING_ERROR = 1<<4,
};
/**
* struct sky_droneport_state - Droneport state.
*/
struct sky_droneport_state {
unsigned int status;
};
/**
* struct sky_subscription_token - Subscription token
*/
struct sky_subscription_token {
uint8_t buf[32];
size_t len;
};
/**
* struct sky_subscription - Event subscription configuration.
*/
struct sky_subscription {
void *data;
void (*on_state)(void *data, struct sky_charging_state *state);
struct sky_subscription_token *token;
unsigned interval_msecs;
};
/**
* struct sky_peerinfo - Peer information.
*/
struct sky_peerinfo {
uint32_t server_version;
uint16_t proto_version;
};
/**
* struct sky_brokerinfo - Broker information.
*/
struct sky_brokerinfo {
uint16_t af_family;
char addr[INET6_ADDRSTRLEN];
uint16_t proto_version;
uint16_t servers_port;
uint16_t sub_port;
uint16_t clients_port;
uint16_t pub_port;
};
enum sky_gps_status {
SKY_GPS_STATUS_NO_FIX = 0, /* no */
SKY_GPS_STATUS_FIX = 1, /* yes, without DGPS */
SKY_GPS_STATUS_DGPS_FIX = 2, /* yes, with DGPS */
};
enum sky_gps_mode {
SKY_GPS_MODE_NOT_SEEN = 0, /* mode update not seen yet */
SKY_GPS_MODE_NO_FIX = 1, /* none */
SKY_GPS_MODE_2D = 2, /* good for latitude/longitude */
SKY_GPS_MODE_3D = 3 /* good for altitude/climb too */
};
/**
* struct sky_gpsdata - GPS data
*/
struct sky_gpsdata {
enum sky_gps_status status; /* GPS status -- always valid */
unsigned satellites_used; /* Number of satellites used in solution */
/* Dilution of precision factors */
struct {
double xdop, ydop, pdop, hdop, vdop, tdop, gdop;
} dop;
struct {
enum sky_gps_mode mode; /* Mode of fix */
double time; /* Time of update, unix time in sec with fractional part */
double latitude; /* Latitude in degrees (valid if mode >= 2) */
double longitude; /* Longitude in degrees (valid if mode >= 2) */
double altitude; /* Altitude in meters (valid if mode == 3) */
} fix;
};
/**
* union sky_async_storage - Storage for keeping all possible results structures
* of asynchronous commands.
*/
union sky_async_storage {
struct sky_peerinfo peerinfo;
struct sky_dev_desc *devdescs;
struct sky_dev_params params;
struct sky_charging_state charging_state;
struct sky_droneport_state dp_state;
struct sky_gpsdata gpsdata;
enum sky_drone_status drone_status;
struct sky_sink_info sink_info;
struct sky_subscription_token sub_token;
struct sky_flash_chunk flash_chunk;
struct sky_flash_info flash_info;
struct sky_passthru_msg passthru_msg;
};
struct sky_async_req;
/**
* Completion of a response of the asynchronous request.
*/
typedef void (sky_async_completion_t)(struct sky_async_req *req);
/**
* struct sky_async_req - Request for asynchronous commands.
*/
struct sky_async_req {
struct sky_async_req *next;
struct sky_async_req *prev;
sky_async_completion_t *completion;
struct sky_dev *dev;
const uint8_t *usruuid;
unsigned int type;
unsigned int tag; /* response tag */
struct {
const void *ptr;
} in;
struct {
void *ptr;
bool completed;
int rc;
} out;
};
/**
* Returns string representation of the device type.
*/
static inline const char *sky_devtype_to_str(enum sky_dev_type type)
{
return type == SKY_MUX_HW1 ? "MUX-HW1": "MUX-HW2";
}
/**
* Returns string representation of the device state.
*/
const char *sky_devstate_to_str(enum sky_dev_type dev_type,
enum sky_dev_state state);
/**
* Returns string representation of the device charging finished reason.
*/
const char *sky_devreason_to_str(enum sky_dev_charging_finished_reason reason);
/**
* Returns string representation of the device state and charging
* finished reason.
*/
const char *sky_devstate_and_reason_to_str(enum sky_dev_type dev_type,
enum sky_dev_state state,
enum sky_dev_charging_finished_reason reason);
/**
* Returns string representation of the device param.
*/
const char *sky_devparam_to_str(enum sky_dev_type dev_type,
enum sky_dev_param param);
/**
* Returns device param if string is recognized. If string can't
* be parsed number of device params is returned.
*/
enum sky_dev_param sky_devparam_from_str(enum sky_dev_type dev_type,
const char *str);
/**
* Fill in the input buffer with a string representation of the device
* param value and returns length of a string.
*/
int sky_devparam_value_to_str(enum sky_dev_type dev_type,
enum sky_dev_param param,
const struct sky_dev_params *params,
enum sky_param_value_format value_format,
char *buf, size_t size);
/**
* Fill in the param with the value parsed from a string.
*/
int sky_devparam_value_from_str(const char *str,
enum sky_dev_type dev_type,
enum sky_dev_param param,
struct sky_dev_params *params);
/**
* Returns string representation of the sink param.
*/
const char *sky_sinkparam_to_str(enum sky_sink_param param);
/**
* Returns sink param if string is recognized. If string can't
* be parsed number of device params is returned.
*/
enum sky_sink_param sky_sinkparam_from_str(const char *str);
/**
* Fill in the input buffer with a string representation of the sink
* param value and returns length of a string.
*/
int sky_sinkparam_value_to_str(enum sky_sink_param param,
const struct sky_dev_params *params,
enum sky_param_value_format value_format,
char *buf, size_t size);
/**
* Fill in the param with the value parsed from a string.
*/
int sky_sinkparam_value_from_str(const char *str,
enum sky_sink_param param,
struct sky_dev_params *params);
/**
* Returns string representation of the GPS status.
*/
const char *sky_gpsstatus_to_str(enum sky_gps_status status);
/**
* Returns string representation of the GPS mode.
*/
const char *sky_gpsmode_to_str(enum sky_gps_mode mode);
/**
* sky_hw_is_charging() - returns true if hardware is in charge state
*/
static inline bool sky_hw_is_charging(enum sky_dev_type mux_type,
enum sky_dev_state hw_state)
{
if (mux_type == SKY_MUX_HW1)
return hw_state == SKY_HW1_CHARGING_RUN ||
hw_state == SKY_HW1_CHARGING_MONITOR_CURRENT;
return (hw_state == SKY_HW2_PRECHARGING ||
hw_state == SKY_HW2_CHARGING);
}
/**
* sky_hw_is_idle() - returns true if hardware is not in a charge loop
* or does not have any state related to charging
*/
static inline bool sky_hw_is_idle(enum sky_dev_type mux_type,
enum sky_dev_state hw_state)
{
if (mux_type == SKY_MUX_HW1)
return !sky_hw_is_charging(mux_type, hw_state);
return (hw_state == SKY_HW2_STOPPED ||
hw_state == SKY_HW2_SCANNING ||
hw_state == SKY_HW2_ERR_LOW_MUX_VOLTAGE ||
hw_state == SKY_HW2_ERR_VOLTAGE_ON_OUTPUT);
}
/**
* sky_confinit() - Inits generic configuration to zero.
* @cfg: Conf structure to be zeroed out.
*
* Sets all values of the @cfg structure to zero.
*/
void sky_confinit(struct sky_conf *cfg);
/**
* sky_confparse() - Parse generic skycharge configuration.
* @path: Path to config.
* @cfg: Conf structure to be filled in.
*
* Parses configuration and fills in the @cfg structure.
*
* RETURNS:
* Returns 0 on success and <0 otherwise.
*/
int sky_confparse(const char *path, struct sky_conf *cfg);
/**
* sky_asyncopen() - Opens asynchronous context.
* @conf: Generic device configuration.
* @async: Output of asynchronous context.
*
* Opens asynchronous context.
*
* RETURNS:
* Returns 0 on success and <0 otherwise.
*/
int sky_asyncopen(const struct sky_conf *conf,
struct sky_async **async);
/**
* sky_asyncdeinit() - Closes asynchronous context.
* @async: Asynchronous context.
*
* Closes asynchronous context.
*/
void sky_asyncclose(struct sky_async *async);
/**
* sky_asyncfd() - Returns fd of this asynchronous context.
* @async: Asynchronous context.
*
* Returns file descriptor of the asynchronous context.
* Using this file descriptor usual polling on incomming events
* can be done, so POLLIN events should be expected.
* After POLLIN events are received sky_asyncexecute() should be
* invoked with @false as @wait param.
*/
int sky_asyncfd(struct sky_async *async);
/**
* sky_asyncexecute() - Executes all asynchronous requests and completes
* corresponding responses.
* @async: Asynchronous context.
* @wait: Wait for submitted requests to complete
*
* Returns >=0 indicating number of completed requests and received
* responses. Returns <0 in case of error. Error means mostly likely
* unrecoverably error and the whole asynchronous context has to be
* closed and all memory freed.
*/
int sky_asyncexecute(struct sky_async *async, bool wait);
/**
* sky_asyncreq_completionset() - Sets completion handler.
* @req: Asynchronous request.
* @completion: Completion handler to be associated with the request.
*/
void sky_asyncreq_completionset(struct sky_async_req *req,
sky_async_completion_t *completion);
/**
* sky_asyncreq_useruuidset() - Sets user uuid for authentication.
* @req: Asynchronous request.
* @uruuid: User uuid, 16 bytes.
*
* NOTE: @usruuid is not copied, but is used as a pointer, so
* memory should be available during live time of the
* request.
*/
void sky_asyncreq_useruuidset(struct sky_async_req *req,
const uint8_t *usruuid);
/**
* sky_asyncreq_cancel() - Cancels submitted request.
* @async: Asynchronous context.
* @req: Request to be cancelled.
*
* Cancel request which was previously submitted to the asynchronous
* context queue.
*
* Returns true if request was successfully cancelled or false
* in case if request was already completed.
*/
bool sky_asyncreq_cancel(struct sky_async *async,
struct sky_async_req *req);
/**
* sky_subscription_token() - Returns subscription token for the device.
* @token: Pointer to a token structure to be filled in.
*
* Returns subscription token which can be used for further sky_subscribe()
* request.
*
* RETURNS:
* Returns 0 on success and <0 otherwise.
*/
int sky_subscription_token(struct sky_dev *dev,
struct sky_subscription_token *token);
/**
* sky_asyncreq_subscription_token() - Inits and submits an asynchronous request
* to receive the subscription token for the
* device.
*
* See synchronous sky_subscription_token() variant for details.
*/
int sky_asyncreq_subscription_token(struct sky_async *async,
struct sky_dev *dev,
struct sky_subscription_token *token,
struct sky_async_req *req);
/**
* sky_subscribe() - Subscribe on @sky_charging_state update events.
* @dev: Device context.
* @token: Subscription token, see sky_subscription_token().
* For the SKY_LOCAL connection type token is not used
* and NULL can be passed.
* @sub: Subscription configuration.
*
* Subscribes on @sky_charging_state update events.
* Beware:
* @state->on_event() callback is called from another thread.
*
* RETURNS:
* Returns 0 on success and <0 otherwise:
*
* -EINVAL invalid parameters.
* -EEXIST is already subscribed
* -EPERM if operation is not permitted.
* -ECONNRESET connection reset by peer (in case of remote connection)
*/
int sky_subscribe(struct sky_dev *dev, struct sky_subscription_token *token,
struct sky_subscription *sub);
/**
* sky_unsubscribe() - Unsubscribe from @sky_charging_state update events.
* @dev: Device context.
*
* Unsbscribes from @sky_charging_state update events.
* Beware:
* function is synchronous and waits till subscription thread exits.
*
* RETURNS:
* Returns 0 on success and <0 otherwise:
*
* -EPERM if operation is not permitted.
* -ENOENT was not subscribed
* -ECONNRESET connection reset by peer (in case of remote connection)
*/
int sky_unsubscribe(struct sky_dev *dev);
/**
* sky_discoverbroker() - Discover broker on the local network.
* @brokerinfo: Broker infor to be filled in.
* @timeout_ms: Timeout in ms.
*
* Function fills in @brokerinfo and returns 0 if skybroker was
* disovered, otherwise -ENONET is returned.
*
* RETURNS:
* Returns 0 on success and <0 otherwise:
*
* -ENONET skybroker is not on the network
* -EOPNOTSUPP library does not support any remote access
*/
int sky_discoverbroker(struct sky_brokerinfo *brokerinfo,
unsigned int timeout_ms);
/**
* sky_peerinfo() - Fills in peer information.
* @conf: Device configuration.
* @peerinfo: Peerinfo structure.
*
* Function fills in @peerinfo for specified @conf. @conf->contype
* must be @SKY_REMOTE, otherwise -EOPNOTSUPP is returned.
*
* RETURNS:
* Returns 0 on success and <0 otherwise:
*
* -EINVAL invalid parameters.
*/
int sky_peerinfo(const struct sky_conf *conf,
struct sky_peerinfo *peerinfo);
/**
* sky_asyncreq_peerinfo() - Inits and submits an asynchronous request to
* receive the peer information.
*
* See synchronous sky_peerinfo() variant for details.
*/
int sky_asyncreq_peerinfo(struct sky_async *async,
struct sky_peerinfo *peerinfo,
struct sky_async_req *req);
/**
* sky_devslist() - Returns list of found devices.
* @conf: Device configuration.
* @list: Fills list of all found devices.
*
* Functions scans for all Sky devices and puts them to the
* list provided as an argument. Do not forget to call sky_devsfree().
*
* RETURNS:
* Returns 0 on success and <0 otherwise:
*
* -ENODEV if no devices found, see sky_asyncreq_devslist() for API differences.
* -EPERM if operation is not permitted.
* -ENOMEM if memory allocation failed.
*/
int sky_devslist(const struct sky_conf *conf,
struct sky_dev_desc **list);
/**
* sky_asyncreq_devslist() - Inits and submits an asynchronous request to
* fetch a list of devices.
*
* See synchronous sky_devslist() variant for details
*
* BEWARE: @list is set to NULL if no devices are found, but synchronous
* sky_devslist() variant returns -ENODEV.
*/
int sky_asyncreq_devslist(struct sky_async *async,
struct sky_dev_desc **list,
struct sky_async_req *req);
/**
* sky_devsfree() - Frees allocated list.
* @list: Devices list.
*
* Frees a list which was allocated by sky_devslist().
*/
void sky_devsfree(struct sky_dev_desc *list);
/**
* sky_devopen() - Opens a device and returns its context.
* @devdesc: Device descriptor.
* @dev: Output pointer for storing device context.
*
* Function opens a device, described by device descriptor. In case
* of success valid device context will be returned in @dev argument.
* Do not forget to close the device with calling sky_devclose().
*
* RETURNS:
* Returns 0 on success and <0 otherwise:
*
* -EPERM if operation is not permitted.
* -ENOMEM if memory allocation failed.
*/
int sky_devopen(const struct sky_dev_desc *devdesc, struct sky_dev **dev);
/**
* sky_devclose() - Closes a device context.
* @dev: Device context to be closed.
*
* Function disconnects from a device and frees all corresponding memory.
*/
void sky_devclose(struct sky_dev *dev);
/**