forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.h
147 lines (125 loc) · 3.92 KB
/
event.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
#ifndef EVENT_H
#define EVENT_H
#include <QString>
#include "device.h"
class Resource;
class ResourceItem;
struct EventData;
class Event
{
public:
Event();
Event(const char *resource, const char *what, const QString &id, ResourceItem *item, DeviceKey deviceKey = 0);
Event(const char *resource, const char *what, const QString &id, DeviceKey deviceKey);
Event(const char *resource, const char *what, const QString &id, int num = 0, DeviceKey deviceKey = 0);
Event(const char *resource, const char *what, int num, DeviceKey deviceKey = 0);
//! Don't call following ctor directly use EventWithData() factory function.
Event(const char *resource, const char *what, const void *data, size_t size, DeviceKey deviceKey = 0);
const char *resource() const { return m_resource; }
const char *what() const { return m_what; }
const QString &id() const { return m_id; }
int num() const { return m_num; }
int numPrevious() const { return m_numPrev; }
DeviceKey deviceKey() const { return m_deviceKey; }
void setDeviceKey(DeviceKey key) { m_deviceKey = key; }
bool hasData() const;
quint16 dataSize() const { return m_dataSize; }
bool getData(void *dst, size_t size) const;
private:
const char *m_resource = nullptr;
const char *m_what = nullptr;
QString m_id;
union
{
struct
{
int m_num;
int m_numPrev;
};
struct
{
quint16 m_dataIndex;
quint16 m_dataId;
quint16 m_dataSize;
};
};
DeviceKey m_deviceKey = 0;
struct
{
unsigned char m_hasData : 1;
unsigned char _pad : 7;
};
};
template <typename D>
Event EventWithData(const char *resource, const char *what, const D &data, DeviceKey deviceKey)
{
static_assert (std::is_trivially_copyable<D>::value, "data needs to be trivially copyable");
return Event(resource, what, &data, sizeof(data), deviceKey);
}
inline Event EventWithData(const char *resource, const char *what, const void *data, size_t size, DeviceKey deviceKey)
{
return Event(resource, what, data, size, deviceKey);
}
//! Unpacks APS confirm id.
inline quint8 EventApsConfirmId(const Event &event)
{
return event.num() >> 8 & 0xFF;
}
//! Unpacks APS confirm status.
inline quint8 EventApsConfirmStatus(const Event &event)
{
return event.num() & 0xFF;
}
//! Packs APS id and confirm status into an \c int used as `num` parameter for REventApsConfirm.
inline int EventApsConfirmPack(quint8 id, quint8 status)
{
return id << 8 | status;
}
//! Unpacks ZDP sequence number.
inline quint8 EventZdpResponseSequenceNumber(const Event &event)
{
return event.num() >> 8 & 0xFF;
}
//! Unpacks ZDP sequence number.
inline quint8 EventZdpResponseStatus(const Event &event)
{
return event.num() & 0xFF;
}
//! Packs APS id and confirm status into an \c int used as `num` parameter for REventApsConfirm.
inline int EventZdpResponsePack(quint8 seq, quint8 status)
{
return seq << 8 | status;
}
//! Unpacks Zcl sequence number.
inline quint8 EventZclSequenceNumber(const Event &event)
{
return event.num() >> 8 & 0xFF;
}
//! Unpacks ZCL command status.
inline quint8 EventZclStatus(const Event &event)
{
return event.num() & 0xFF;
}
//! Packs ZCL sequence number and command status into an \c int used as `num` parameter for REventZclResponse.
inline int EventZclResponsePack(quint8 seq, quint8 status)
{
return seq << 8 | status;
}
//! Packs timer into an \c int used as `num` parameter for REventStartTimer and REventStopTimer.
inline int EventTimerPack(int timerId, int timeoutMs)
{
Q_ASSERT(timerId <= 0xFF);
Q_ASSERT(timeoutMs <= 0xFFFFFF);
return timerId << 24 | timeoutMs;
}
//! Unpacks timer id.
inline int EventTimerId(const Event &event)
{
return (event.num() >> 24) & 0xFF;
}
//! Unpacks timer timout.
inline int EventTimerTimout(const Event &event)
{
return event.num() & 0xFFFFFF;
}
#endif // EVENT_H