forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.cpp
132 lines (113 loc) · 2.64 KB
/
event.cpp
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
#include <array>
#include "event.h"
#include "resource.h"
constexpr const int MaxEventDataBuffers = 64;
constexpr const int MaxEventDataSize = 256;
struct EventData
{
quint16 id;
quint8 data[MaxEventDataSize];
};
static size_t _eventDataIter = 0;
static EventData _eventData[MaxEventDataBuffers];
quint16 allocDataBuffer()
{
_eventDataIter = (_eventDataIter + 1) % MaxEventDataBuffers;
_eventData[_eventDataIter].id++;
return int(_eventDataIter);
}
/*! Constructor.
*/
Event::Event()
{
m_num = 0;
m_numPrev = 0;
m_hasData = 0;
}
Event::Event(const char *resource, const char *what, const QString &id, ResourceItem *item, DeviceKey deviceKey) :
m_resource(resource),
m_what(what),
m_id(id),
m_num(0),
m_numPrev(0),
m_deviceKey(deviceKey),
m_hasData(0)
{
DBG_Assert(item != 0);
if (item)
{
m_num = item->toNumber();
m_numPrev = item->toNumberPrevious();
}
}
/*! Constructor.
*/
Event::Event(const char *resource, const char *what, const QString &id, DeviceKey deviceKey) :
m_resource(resource),
m_what(what),
m_id(id),
m_num(0),
m_numPrev(0),
m_deviceKey(deviceKey),
m_hasData(0)
{
}
/*! Constructor.
*/
Event::Event(const char *resource, const char *what, const QString &id, int num, DeviceKey deviceKey) :
m_resource(resource),
m_what(what),
m_id(id),
m_num(num),
m_numPrev(0),
m_deviceKey(deviceKey),
m_hasData(0)
{
}
/*! Constructor.
*/
Event::Event(const char *resource, const char *what, int num, DeviceKey deviceKey) :
m_resource(resource),
m_what(what),
m_num(num),
m_numPrev(0),
m_deviceKey(deviceKey),
m_hasData(0)
{
if (resource == RGroups)
{
m_id = QString::number(num);
}
}
Event::Event(const char *resource, const char *what, const void *data, size_t size, DeviceKey deviceKey) :
m_resource(resource),
m_what(what),
m_deviceKey(deviceKey),
m_hasData(1)
{
Q_ASSERT(data);
Q_ASSERT(size > 0 && size <= MaxEventDataSize);
m_num = 0;
m_numPrev = 0;
m_dataIndex = allocDataBuffer();
m_dataId = _eventData[m_dataIndex].id;
m_dataSize = size;
memcpy(_eventData[m_dataIndex].data, data, size);
}
bool Event::hasData() const
{
if (m_hasData != 1) { return false; }
if (m_dataIndex >= MaxEventDataSize) { return false; }
if (m_dataId != _eventData[m_dataIndex].id) { return false; }
return true;
}
bool Event::getData(void *dst, size_t size) const
{
if (size == m_dataSize && hasData())
{
memcpy(dst, _eventData[m_dataIndex].data, size);
return true;
}
Q_ASSERT(0);
return false;
}