Dawn Framework 1.0
Universal data acquisition framework for embedded systems
battery.cxx
1// dawn/src/io/battery.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/io/battery.hxx"
7
8#include <cstdint>
9#include <cstdio>
10#include <errno.h>
11#include <fcntl.h>
12#include <sys/ioctl.h>
13#include <unistd.h>
14
15#include <nuttx/power/battery_ioctl.h>
16
17using namespace dawn;
18
19CIOBatteryBase::~CIOBatteryBase()
20{
21 deinit();
22}
23
25{
27 {
28 DAWNERR("Battery IO requires DTYPE_UINT32\n");
29 return -EINVAL;
30 }
31
32 if (getCmnDevno() == -1)
33 {
34 DAWNERR("Battery device number not configured\n");
35 return -EINVAL;
36 }
37
38 snprintf(path, sizeof(path), "/dev/batt%d", getCmnDevno());
39
40 fd = open(path, O_RDONLY);
41 if (fd < 0)
42 {
43 DAWNERR("failed to open %s (%d)\n", path, -errno);
44 return -errno;
45 }
46
47 return OK;
48}
49
51{
52 if (fd >= 0)
53 {
54 close(fd);
55 fd = -1;
56 }
57
58 return OK;
59}
60
61int CIOBatteryBase::batRead(int cmd, void *arg) const
62{
63 int ret = ioctl(fd, cmd, (unsigned long)(uintptr_t)arg);
64 if (ret < 0)
65 {
66 return -errno;
67 }
68
69 return OK;
70}
71
72int CIOBattVolt::getDataImpl(IODataCmn &data, size_t len)
73{
74 int32_t raw;
75 int ret;
76
77 if (len != 1)
78 {
79 return -ENOTSUP;
80 }
81
82 ret = batRead(BATIOC_VOLTAGE, &raw);
83 if (ret < 0)
84 {
85 return ret;
86 }
87
88 *reinterpret_cast<uint32_t *>(data.getDataPtr()) = static_cast<uint32_t>(raw);
89
90 return OK;
91}
92
93int CIOBattSoc::getDataImpl(IODataCmn &data, size_t len)
94{
95 int32_t raw;
96 int ret;
97
98 if (len != 1)
99 {
100 return -ENOTSUP;
101 }
102
103 ret = batRead(BATIOC_CAPACITY, &raw);
104 if (ret < 0)
105 {
106 return ret;
107 }
108
109 *reinterpret_cast<uint32_t *>(data.getDataPtr()) = static_cast<uint32_t>(raw);
110
111 return OK;
112}
113
115{
116 int raw;
117 int ret;
118
119 if (len != 1)
120 {
121 return -ENOTSUP;
122 }
123
124 ret = batRead(BATIOC_STATE, &raw);
125 if (ret < 0)
126 {
127 return ret;
128 }
129
130 *reinterpret_cast<uint32_t *>(data.getDataPtr()) = static_cast<uint32_t>(raw);
131
132 return OK;
133}
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition battery.cxx:93
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition battery.cxx:114
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition battery.cxx:72
int configure()
Configure object from descriptor data.
Definition battery.cxx:24
int batRead(int cmd, void *arg) const
ioctl read of a single battery value into *arg.
Definition battery.cxx:61
int deinit()
De-initialize object.
Definition battery.cxx:50
int getCmnDevno() const
Get device number for this I/O.
Definition common.hxx:817
uint8_t getDtype() const
Get data type field.
Definition object.cxx:175
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
Base interface for I/O data buffers (static and dynamic).
Definition idata.hxx:21
virtual void * getDataPtr(size_t batch=0)=0
Get pointer to data only (skips timestamp if present).
@ DTYPE_UINT32
Unsigned 32-bit integer (0 to 4294967295).
Definition objectid.hxx:96