bus.cpp
4.37 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
/*
* Copyright (c) 2015-2016, Arkadiusz Materek (arekmat@poczta.fm)
*
* Licensed under GNU General Public License 3.0 or later.
*
* This program 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.
*/
#include "bus.hpp"
#include <dali/config.hpp>
namespace dali {
namespace controller {
namespace {
const Time kCommandRepeatTimeout = 100;
}
Bus::Bus(IBusDriver* bus, Client* client) :
mBus(bus),
mClient(client),
mState(IBusDriver::IBusState::UNKNOWN),
mLastCommand(Command::INVALID),
mCommandRepeatCount(0),
mLastCommandTime(0) {
mBus->registerClient(this);
}
Bus::~Bus() {
mBus->unregisterClient(this);
}
void Bus::onDataReceived(Time time, uint16_t data) {
uint8_t param;
Command command = extractCommand(data, ¶m);
if (command == Command::INVALID) {
return;
}
if (mLastCommand == Command::INVALID) {
mCommandRepeatCount = 0;
mLastCommandTime = time;
Status status = mClient->handleCommand(mCommandRepeatCount, command, param);
if (status == Status::REPEAT_REQUIRED) {
mLastCommand = command;
return;
} else {
mLastCommand = Command::INVALID;
return;
}
} else if (mLastCommand == command) {
if (time - mLastCommandTime < kCommandRepeatTimeout) {
mCommandRepeatCount++;
} else {
mLastCommand = Command::INVALID;
mCommandRepeatCount = 0;
}
mLastCommandTime = time;
Status status = mClient->handleCommand(mCommandRepeatCount, command, param);
if (status == Status::REPEAT_REQUIRED) {
mLastCommand = command;
return;
} else {
mLastCommand = Command::INVALID;
return;
}
} else { // (mLastCommand != Command::INVALID) || (mLastCommand != cmd)
mLastCommand = Command::INVALID;
mCommandRepeatCount = 0;
mLastCommandTime = time;
if (time - mLastCommandTime < kCommandRepeatTimeout) {
mClient->handleIgnoredCommand(command, param);
} else {
Status status = mClient->handleCommand(mCommandRepeatCount, command, param);
if (status == Status::REPEAT_REQUIRED) {
mLastCommand = command;
return;
} else {
return;
}
}
}
}
void Bus::onBusStateChanged(IBusDriver::IBusState state) {
if (mState != state) {
mState = state;
if (state == IBusDriver::IBusState::DISCONNECTED) {
mClient->onBusDisconnected();
}
}
}
Command Bus::extractCommand(uint16_t data, uint8_t* commandParam) {
Command command = Command::INVALID;
uint8_t param = (uint8_t) (data & 0xff);
uint8_t addr = (uint8_t) (data >> 8);
bool cmdBitSet = ((addr & 0x01) != 0);
if ((addr & 0xfe) == 0xfe) {
// Broadcast
if (cmdBitSet) {
command = (Command) param;
param = 0xff;
} else {
command = Command::DIRECT_POWER_CONTROL;
}
} else if ((addr & 0x80) == 0) {
// Short address
addr >>= 1;
uint8_t myAddr = mClient->getShortAddr() >> 1;
if (cmdBitSet) {
command = (Command) param;
param = 0xff;
} else {
command = Command::DIRECT_POWER_CONTROL;
}
if (addr != myAddr) {
return Command::INVALID;
}
} else if ((addr & 0x60) == 0) {
// Group address
uint8_t group = (addr >> 1) & 0x0f;
uint16_t groups = mClient->getGroups();
if (cmdBitSet) {
command = (Command) param;
param = 0xff;
} else {
command = Command::DIRECT_POWER_CONTROL;
}
if ((groups & (1 << group)) == 0) {
return Command::INVALID;
}
} else {
command = (Command) ((uint16_t) Command::_SPECIAL_COMMAND + addr);
if (command == Command::INITIALISE) {
uint8_t myAddr = mClient->getShortAddr() >> 1;
switch (param) {
case 0x00: // All control gear shall react
break;
case 0xff: // Control gear without short address shall react
if (myAddr <= DALI_ADDR_MAX) {
return Command::INVALID;
}
break;
default: // Control gear with the address AAAAAAb shall react
if (myAddr != (param >> 1)) {
return Command::INVALID;
}
break;
}
}
}
*commandParam = param;
return command;
}
} // namespace controller
}// namespace dali