-
Notifications
You must be signed in to change notification settings - Fork 513
/
spark_wiring_diagnostics.h
409 lines (328 loc) · 14 KB
/
spark_wiring_diagnostics.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
/*
* Copyright (c) 2017 Particle Industries, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "spark_wiring_thread.h"
#include "spark_wiring_interrupts.h"
#include "spark_wiring_error.h"
#include "spark_wiring_global.h"
#include "diagnostics.h"
#include "debug.h"
#include <mutex>
#include <atomic>
namespace particle {
// Base abstract class for a diagnostic data source
class AbstractDiagnosticData {
public:
static int get(uint16_t id, void* data, size_t& size);
static int get(const diag_source* src, void* data, size_t& size);
// This class is non-copyable
AbstractDiagnosticData(const AbstractDiagnosticData&) = delete;
AbstractDiagnosticData& operator=(const AbstractDiagnosticData&) = delete;
protected:
AbstractDiagnosticData(uint16_t id, diag_type type);
AbstractDiagnosticData(uint16_t id, const char* name, diag_type type);
virtual int get(void* data, size_t& size) = 0;
private:
diag_source d_;
static int callback(const diag_source* src, int cmd, void* data);
};
// Base abstract class for a data source containing an integer value
class AbstractIntegerDiagnosticData: public AbstractDiagnosticData {
public:
typedef int32_t IntType; // Underlying integer type
static int get(uint16_t id, IntType& val);
static int get(const diag_source* src, IntType& val);
protected:
explicit AbstractIntegerDiagnosticData(uint16_t id, const char* name = nullptr);
virtual int get(IntType& val) = 0;
private:
virtual int get(void* data, size_t& size) override; // AbstractDiagnosticData
};
template<typename LockingPolicyT>
class IntegerDiagnosticData: public AbstractIntegerDiagnosticData {
public:
explicit IntegerDiagnosticData(uint16_t id, IntType val = 0);
IntegerDiagnosticData(uint16_t id, const char* name, IntType val = 0);
IntType operator++();
IntType operator++(int);
IntType operator--();
IntType operator--(int);
IntType operator+=(IntType val);
IntType operator-=(IntType val);
IntegerDiagnosticData<LockingPolicyT>& operator=(IntType val);
operator IntType() const;
private:
mutable LockingPolicyT lock_;
IntType val_;
virtual int get(IntType& val) override; // AbstractIntegerDiagnosticData
};
template<>
class IntegerDiagnosticData<AtomicLockingPolicy>: public AbstractIntegerDiagnosticData {
public:
explicit IntegerDiagnosticData(uint16_t id, IntType val = 0);
IntegerDiagnosticData(uint16_t id, const char* name, IntType val = 0);
IntType operator++();
IntType operator++(int);
IntType operator--();
IntType operator--(int);
IntType operator+=(IntType val);
IntType operator-=(IntType val);
IntegerDiagnosticData<AtomicLockingPolicy>& operator=(IntType val);
operator IntType() const;
private:
std::atomic<IntType> val_;
virtual int get(IntType& val) override; // AbstractIntegerDiagnosticData
};
template<typename EnumT, typename LockingPolicyT>
class EnumDiagnosticData: public AbstractIntegerDiagnosticData {
public:
typedef EnumT EnumType;
EnumDiagnosticData(uint16_t id, EnumT val);
EnumDiagnosticData(uint16_t id, const char* name, EnumT val);
EnumDiagnosticData<EnumT, LockingPolicyT>& operator=(EnumT val);
operator EnumT() const;
private:
mutable LockingPolicyT lock_;
IntType val_;
virtual int get(IntType& val) override; // AbstractIntegerDiagnosticData
};
template<typename EnumT>
class EnumDiagnosticData<EnumT, AtomicLockingPolicy>: public AbstractIntegerDiagnosticData {
public:
typedef EnumT EnumType;
EnumDiagnosticData(uint16_t id, EnumT val);
EnumDiagnosticData(uint16_t id, const char* name, EnumT val);
EnumDiagnosticData<EnumT, AtomicLockingPolicy>& operator=(EnumT val);
operator EnumT() const;
private:
std::atomic<IntType> val_;
virtual int get(IntType& val) override; // AbstractIntegerDiagnosticData
};
// Convenience typedefs
typedef IntegerDiagnosticData<NoLockingPolicy> SimpleIntegerDiagnosticData;
typedef IntegerDiagnosticData<AtomicLockingPolicy> AtomicIntegerDiagnosticData;
template<typename EnumT>
using SimpleEnumDiagnosticData = EnumDiagnosticData<EnumT, NoLockingPolicy>;
template<typename EnumT>
using AtomicEnumDiagnosticData = EnumDiagnosticData<EnumT, AtomicLockingPolicy>;
} // namespace particle
inline particle::AbstractDiagnosticData::AbstractDiagnosticData(uint16_t id, diag_type type) :
AbstractDiagnosticData(id, nullptr, type) {
}
inline particle::AbstractDiagnosticData::AbstractDiagnosticData(uint16_t id, const char* name, diag_type type) :
d_{ sizeof(diag_source), 0 /* flags */, id, (uint16_t)type, name, this /* data */, callback } {
diag_register_source(&d_, nullptr);
}
inline int particle::AbstractDiagnosticData::get(uint16_t id, void* data, size_t& size) {
const diag_source* src = nullptr;
const int ret = diag_get_source(id, &src, nullptr);
if (ret != SYSTEM_ERROR_NONE) {
return ret;
}
return get(src, data, size);
}
inline int particle::AbstractDiagnosticData::get(const diag_source* src, void* data, size_t& size) {
SPARK_ASSERT(src && src->callback);
diag_source_get_cmd_data d = { sizeof(diag_source_get_cmd_data), 0 /* reserved */, data, size };
const int ret = src->callback(src, DIAG_SOURCE_CMD_GET, &d);
if (ret == SYSTEM_ERROR_NONE) {
size = d.data_size;
}
return ret;
}
inline int particle::AbstractDiagnosticData::callback(const diag_source* src, int cmd, void* data) {
const auto d = static_cast<AbstractDiagnosticData*>(src->data);
switch (cmd) {
case DIAG_SOURCE_CMD_GET: {
const auto cmdData = static_cast<diag_source_get_cmd_data*>(data);
return d->get(cmdData->data, cmdData->data_size);
}
default:
return SYSTEM_ERROR_NOT_SUPPORTED;
}
}
inline particle::AbstractIntegerDiagnosticData::AbstractIntegerDiagnosticData(uint16_t id, const char* name) :
AbstractDiagnosticData(id, name, DIAG_TYPE_INT) {
}
inline int particle::AbstractIntegerDiagnosticData::get(uint16_t id, IntType& val) {
const diag_source* src = nullptr;
const int ret = diag_get_source(id, &src, nullptr);
if (ret != SYSTEM_ERROR_NONE) {
return ret;
}
return get(src, val);
}
inline int particle::AbstractIntegerDiagnosticData::get(const diag_source* src, IntType& val) {
SPARK_ASSERT(src->type == DIAG_TYPE_INT);
size_t size = sizeof(IntType);
return AbstractDiagnosticData::get(src, &val, size);
}
inline int particle::AbstractIntegerDiagnosticData::get(void* data, size_t& size) {
if (!data) {
size = sizeof(IntType);
return SYSTEM_ERROR_NONE;
}
if (size < sizeof(IntType)) {
return SYSTEM_ERROR_TOO_LARGE;
}
const int ret = get(*(IntType*)data);
if (ret == SYSTEM_ERROR_NONE) {
size = sizeof(IntType);
}
return ret;
}
template<typename LockingPolicyT>
inline particle::IntegerDiagnosticData<LockingPolicyT>::IntegerDiagnosticData(uint16_t id, IntType val) :
IntegerDiagnosticData(id, nullptr, val) {
}
template<typename LockingPolicyT>
inline particle::IntegerDiagnosticData<LockingPolicyT>::IntegerDiagnosticData(uint16_t id, const char* name, IntType val) :
AbstractIntegerDiagnosticData(id, name),
val_(val) {
}
template<typename LockingPolicyT>
inline particle::AbstractIntegerDiagnosticData::IntType particle::IntegerDiagnosticData<LockingPolicyT>::operator++() {
const std::lock_guard<LockingPolicyT> lock(lock_);
return ++val_;
}
template<typename LockingPolicyT>
inline particle::AbstractIntegerDiagnosticData::IntType particle::IntegerDiagnosticData<LockingPolicyT>::operator++(int) {
const std::lock_guard<LockingPolicyT> lock(lock_);
return val_++;
}
template<typename LockingPolicyT>
inline particle::AbstractIntegerDiagnosticData::IntType particle::IntegerDiagnosticData<LockingPolicyT>::operator--() {
const std::lock_guard<LockingPolicyT> lock(lock_);
return --val_;
}
template<typename LockingPolicyT>
inline particle::AbstractIntegerDiagnosticData::IntType particle::IntegerDiagnosticData<LockingPolicyT>::operator--(int) {
const std::lock_guard<LockingPolicyT> lock(lock_);
return val_--;
}
template<typename LockingPolicyT>
inline particle::AbstractIntegerDiagnosticData::IntType particle::IntegerDiagnosticData<LockingPolicyT>::operator+=(IntType val) {
const std::lock_guard<LockingPolicyT> lock(lock_);
return (val_ += val);
}
template<typename LockingPolicyT>
inline particle::AbstractIntegerDiagnosticData::IntType particle::IntegerDiagnosticData<LockingPolicyT>::operator-=(IntType val) {
const std::lock_guard<LockingPolicyT> lock(lock_);
return (val_ -= val);
}
template<typename LockingPolicyT>
inline particle::IntegerDiagnosticData<LockingPolicyT>& particle::IntegerDiagnosticData<LockingPolicyT>::operator=(IntType val) {
const std::lock_guard<LockingPolicyT> lock(lock_);
val_ = val;
return *this;
}
template<typename LockingPolicyT>
inline particle::IntegerDiagnosticData<LockingPolicyT>::operator IntType() const {
const std::lock_guard<LockingPolicyT> lock(lock_);
return val_;
}
template<typename LockingPolicyT>
inline int particle::IntegerDiagnosticData<LockingPolicyT>::get(IntType& val) {
const std::lock_guard<LockingPolicyT> lock(lock_);
val = val_;
return SYSTEM_ERROR_NONE;
}
inline particle::IntegerDiagnosticData<AtomicLockingPolicy>::IntegerDiagnosticData(uint16_t id, IntType val) :
IntegerDiagnosticData(id, nullptr, val) {
}
inline particle::IntegerDiagnosticData<AtomicLockingPolicy>::IntegerDiagnosticData(uint16_t id, const char* name, IntType val) :
AbstractIntegerDiagnosticData(id, name),
val_(val) {
}
inline particle::AbstractIntegerDiagnosticData::IntType particle::IntegerDiagnosticData<AtomicLockingPolicy>::operator++() {
return (val_.fetch_add(1, std::memory_order_relaxed) + 1);
}
inline particle::AbstractIntegerDiagnosticData::IntType particle::IntegerDiagnosticData<AtomicLockingPolicy>::operator++(int) {
return val_.fetch_add(1, std::memory_order_relaxed);
}
inline particle::AbstractIntegerDiagnosticData::IntType particle::IntegerDiagnosticData<AtomicLockingPolicy>::operator--() {
return (val_.fetch_sub(1, std::memory_order_relaxed) - 1);
}
inline particle::AbstractIntegerDiagnosticData::IntType particle::IntegerDiagnosticData<AtomicLockingPolicy>::operator--(int) {
return val_.fetch_sub(1, std::memory_order_relaxed);
}
inline particle::AbstractIntegerDiagnosticData::IntType particle::IntegerDiagnosticData<AtomicLockingPolicy>::operator+=(IntType val) {
return (val_.fetch_add(val, std::memory_order_relaxed) + val);
}
inline particle::AbstractIntegerDiagnosticData::IntType particle::IntegerDiagnosticData<AtomicLockingPolicy>::operator-=(IntType val) {
return (val_.fetch_sub(val, std::memory_order_relaxed) - val);
}
inline particle::IntegerDiagnosticData<AtomicLockingPolicy>& particle::IntegerDiagnosticData<AtomicLockingPolicy>::operator=(IntType val) {
val_.store(val, std::memory_order_relaxed);
return *this;
}
inline particle::IntegerDiagnosticData<AtomicLockingPolicy>::operator IntType() const {
return val_.load(std::memory_order_relaxed);
}
inline int particle::IntegerDiagnosticData<AtomicLockingPolicy>::get(IntType& val) {
val = val_.load(std::memory_order_relaxed);
return SYSTEM_ERROR_NONE;
}
template<typename EnumT, typename LockingPolicyT>
inline particle::EnumDiagnosticData<EnumT, LockingPolicyT>::EnumDiagnosticData(uint16_t id, EnumT val) :
EnumDiagnosticData(id, nullptr, val) {
}
template<typename EnumT, typename LockingPolicyT>
inline particle::EnumDiagnosticData<EnumT, LockingPolicyT>::EnumDiagnosticData(uint16_t id, const char* name, EnumT val) :
AbstractIntegerDiagnosticData(id, name),
val_((IntType)val) {
}
template<typename EnumT, typename LockingPolicyT>
inline particle::EnumDiagnosticData<EnumT, LockingPolicyT>& particle::EnumDiagnosticData<EnumT, LockingPolicyT>::operator=(EnumT val) {
const std::lock_guard<LockingPolicyT> lock(lock_);
val_ = (IntType)val;
return *this;
}
template<typename EnumT, typename LockingPolicyT>
inline particle::EnumDiagnosticData<EnumT, LockingPolicyT>::operator EnumT() const {
const std::lock_guard<LockingPolicyT> lock(lock_);
return (EnumT)val_;
}
template<typename EnumT, typename LockingPolicyT>
inline int particle::EnumDiagnosticData<EnumT, LockingPolicyT>::get(IntType& val) {
const std::lock_guard<LockingPolicyT> lock(lock_);
val = val_;
return SYSTEM_ERROR_NONE;
}
template<typename EnumT>
inline particle::EnumDiagnosticData<EnumT, AtomicLockingPolicy>::EnumDiagnosticData(uint16_t id, EnumT val) :
EnumDiagnosticData(id, nullptr, val) {
}
template<typename EnumT>
inline particle::EnumDiagnosticData<EnumT, AtomicLockingPolicy>::EnumDiagnosticData(uint16_t id, const char* name, EnumT val) :
AbstractIntegerDiagnosticData(id, name),
val_((IntType)val) {
}
template<typename EnumT>
inline particle::EnumDiagnosticData<EnumT, AtomicLockingPolicy>& particle::EnumDiagnosticData<EnumT, AtomicLockingPolicy>::operator=(EnumT val) {
val_.store((IntType)val, std::memory_order_relaxed);
return *this;
}
template<typename EnumT>
inline particle::EnumDiagnosticData<EnumT, AtomicLockingPolicy>::operator EnumT() const {
return (EnumT)val_.load(std::memory_order_relaxed);
}
template<typename EnumT>
inline int particle::EnumDiagnosticData<EnumT, AtomicLockingPolicy>::get(IntType& val) {
val = val_.load(std::memory_order_relaxed);
return SYSTEM_ERROR_NONE;
}