-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinding.cpp
More file actions
172 lines (157 loc) · 4.51 KB
/
Copy pathbinding.cpp
File metadata and controls
172 lines (157 loc) · 4.51 KB
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
// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
// Copyright (c) 2026 idimus. Free for non-commercial use; commercial use requires a license.
//
// C ABI around hardware_monitor_cpp for loading from Python via ctypes. Exposes
// create/destroy and a poll that returns the current snapshot as a JSON string.
#include <cstdio>
#include <string>
#include "hardware_monitor_cpp/hardware_monitor_cpp.hpp"
using namespace hardware_monitor_cpp;
namespace
{
const char* quantityName(Quantity q)
{
switch (q)
{
case Quantity::Temperature: return "temperature";
case Quantity::Load: return "load";
case Quantity::Power: return "power";
case Quantity::Voltage: return "voltage";
case Quantity::Current: return "current";
case Quantity::Clock: return "clock";
case Quantity::FanSpeed: return "fan";
case Quantity::Energy: return "energy";
case Quantity::Capacity: return "capacity";
case Quantity::DataRate: return "datarate";
case Quantity::DataVolume: return "datavolume";
case Quantity::Level: return "level";
case Quantity::Duration: return "duration";
case Quantity::Count: return "count";
case Quantity::Other: return "other";
}
return "other";
}
void appendEscaped(std::string& out, const std::string& s)
{
out += '"';
for (char c : s)
{
switch (c)
{
case '"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (static_cast<unsigned char>(c) < 0x20)
{
char buf[8];
std::snprintf(buf, sizeof(buf), "\\u%04x", c);
out += buf;
}
else
{
out += c;
}
}
}
out += '"';
}
void appendNumber(std::string& out, double v)
{
char buf[64];
std::snprintf(buf, sizeof(buf), "%.4g", v);
out += buf;
}
struct Handle
{
Monitor monitor;
std::string json;
};
} // namespace
extern "C"
{
#ifdef _WIN32
#define HMC_EXPORT __declspec(dllexport)
#else
#define HMC_EXPORT __attribute__((visibility("default")))
#endif
HMC_EXPORT void* hmc_create()
{
auto* h = new Handle();
h->monitor.addPlatformSources();
h->monitor.open();
h->monitor.poll(); // prime delta metrics
return h;
}
HMC_EXPORT void hmc_destroy(void* p)
{
delete static_cast<Handle*>(p);
}
// Returns a JSON snapshot. The pointer is owned by the handle and valid until the next call.
HMC_EXPORT const char* hmc_poll_json(void* p)
{
auto* h = static_cast<Handle*>(p);
if (!h)
{
return "{}";
}
Snapshot snap = h->monitor.poll();
std::string& o = h->json;
o.clear();
o += "{\"devices\":[";
bool first = true;
for (const DeviceInfo& d : snap.devices())
{
if (!first)
{
o += ',';
}
first = false;
o += "{\"id\":";
appendEscaped(o, toString(d.id));
o += ",\"kind\":";
appendEscaped(o, deviceKindName(d.id.kind));
o += ",\"name\":";
appendEscaped(o, d.name);
o += ",\"attributes\":{";
bool fa = true;
for (const auto& kv : d.attributes)
{
if (!fa)
{
o += ',';
}
fa = false;
appendEscaped(o, kv.first);
o += ':';
appendEscaped(o, kv.second);
}
o += "}}";
}
o += "],\"readings\":[";
first = true;
for (const Reading& r : snap.readings())
{
if (!first)
{
o += ',';
}
first = false;
o += "{\"device\":";
appendEscaped(o, toString(r.device));
o += ",\"quantity\":";
appendEscaped(o, quantityName(r.quantity));
o += ",\"unit\":";
appendEscaped(o, unitSymbol(r.unit));
o += ",\"channel\":";
appendEscaped(o, r.channel);
o += ",\"value\":";
appendNumber(o, r.value);
o += '}';
}
o += "]}";
return o.c_str();
}
} // extern "C"