|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 dresden elektronik ingenieurtechnik gmbh. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * The software in this package is published under the terms of the BSD |
| 6 | + * style license a copy of which has been included with this distribution in |
| 7 | + * the LICENSE.txt file. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +#include <QElapsedTimer> |
| 12 | +#include <QTimer> |
| 13 | +#include <deconz/dbg_trace.h> |
| 14 | +#include "event.h" |
| 15 | +#include "resource.h" |
| 16 | +#include "device_tick.h" |
| 17 | + |
| 18 | +#define DEV_TICK_BOOT_TIME 8000 |
| 19 | +#define TICK_INTERVAL_JOIN 500 |
| 20 | +#define TICK_INTERVAL_IDLE 1000 |
| 21 | + |
| 22 | +struct JoinDevice |
| 23 | +{ |
| 24 | + DeviceKey deviceKey; |
| 25 | + quint8 macCapabilities; |
| 26 | +}; |
| 27 | + |
| 28 | +static const char *RLocal = nullptr; |
| 29 | + |
| 30 | +typedef void (*DT_StateHandler)(DeviceTickPrivate *d, const Event &event); |
| 31 | + |
| 32 | +static void DT_StateInit(DeviceTickPrivate *d, const Event &event); |
| 33 | +static void DT_StateJoin(DeviceTickPrivate *d, const Event &event); |
| 34 | +static void DT_StateIdle(DeviceTickPrivate *d, const Event &event); |
| 35 | + |
| 36 | +class DeviceTickPrivate |
| 37 | +{ |
| 38 | +public: |
| 39 | + DT_StateHandler stateHandler = DT_StateInit; |
| 40 | + std::vector<JoinDevice> joinDevices; |
| 41 | + DeviceTick *q = nullptr; |
| 42 | + QTimer *timer = nullptr; |
| 43 | + size_t devIter = 0; |
| 44 | + const DeviceContainer *devices = nullptr; |
| 45 | +}; |
| 46 | + |
| 47 | +/*! Constructor. |
| 48 | + */ |
| 49 | +DeviceTick::DeviceTick(const DeviceContainer &devices, QObject *parent) : |
| 50 | + QObject(parent), |
| 51 | + d(new DeviceTickPrivate) |
| 52 | +{ |
| 53 | + d->devices = &devices; |
| 54 | + d->q = this; |
| 55 | + d->timer = new QTimer(this); |
| 56 | + d->timer->setSingleShot(true); |
| 57 | + connect(d->timer, &QTimer::timeout, this, &DeviceTick::timoutFired); |
| 58 | + d->timer->start(DEV_TICK_BOOT_TIME); |
| 59 | +} |
| 60 | + |
| 61 | +/*! Destructor. |
| 62 | + */ |
| 63 | +DeviceTick::~DeviceTick() |
| 64 | +{ |
| 65 | + Q_ASSERT(d); |
| 66 | + delete d; |
| 67 | + d = nullptr; |
| 68 | +} |
| 69 | + |
| 70 | +/*! Public event entry. |
| 71 | + */ |
| 72 | +void DeviceTick::handleEvent(const Event &event) |
| 73 | +{ |
| 74 | + d->stateHandler(d, event); |
| 75 | +} |
| 76 | + |
| 77 | +/*! State timer callback. |
| 78 | + */ |
| 79 | +void DeviceTick::timoutFired() |
| 80 | +{ |
| 81 | + d->stateHandler(d, Event(RLocal, REventStateTimeout, 0)); |
| 82 | +} |
| 83 | + |
| 84 | +/*! Sets a new DeviceTick state. |
| 85 | + The events REventStateLeave and REventStateEnter will be triggered accordingly. |
| 86 | + */ |
| 87 | +static void DT_SetState(DeviceTickPrivate *d, DT_StateHandler state) |
| 88 | +{ |
| 89 | + if (d->stateHandler != state) |
| 90 | + { |
| 91 | + d->stateHandler(d, Event(RLocal, REventStateLeave, 0)); |
| 92 | + d->stateHandler = state; |
| 93 | + d->stateHandler(d, Event(RLocal, REventStateEnter, 0)); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/*! Starts the state timer. Free function to be mocked by test code. |
| 98 | + */ |
| 99 | +static void DT_StartTimer(DeviceTickPrivate *d, int timeoutMs) |
| 100 | +{ |
| 101 | + d->timer->start(timeoutMs); |
| 102 | +} |
| 103 | + |
| 104 | +/*! Stops the state timer. Free function to be mocked by test code. |
| 105 | + */ |
| 106 | +static void DT_StopTimer(DeviceTickPrivate *d) |
| 107 | +{ |
| 108 | + d->timer->stop(); |
| 109 | +} |
| 110 | + |
| 111 | +/*! Initial state to wait DEV_TICK_BOOT_TIME seconds before starting normal operation. |
| 112 | + */ |
| 113 | +static void DT_StateInit(DeviceTickPrivate *d, const Event &event) |
| 114 | +{ |
| 115 | + if (event.resource() == RLocal && event.what() == REventStateTimeout) |
| 116 | + { |
| 117 | + DBG_Printf(DBG_INFO, "DEV Tick.Init: booted after %lld seconds\n", DEV_TICK_BOOT_TIME); |
| 118 | + DT_SetState(d, DT_StateIdle); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/*! Emits REventPoll to the next device in DT_StateIdle. |
| 123 | + */ |
| 124 | +static void DT_PollNextIdleDevice(DeviceTickPrivate *d) |
| 125 | +{ |
| 126 | + if (d->devices->empty()) |
| 127 | + { |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + d->devIter %= d->devices->size(); |
| 132 | + Q_ASSERT(d->devIter < d->devices->size()); |
| 133 | + |
| 134 | + const Device *device = d->devices->at(d->devIter); |
| 135 | + emit d->q->eventNotify(Event(device->prefix(), REventPoll, 0, device->key())); |
| 136 | + d->devIter++; |
| 137 | +} |
| 138 | + |
| 139 | +/*! This state is active while Permit Join is disabled for normal idle operation. |
| 140 | +
|
| 141 | + It walks over all Devices in TICK_INTERVAL_IDLE spacing. |
| 142 | + The state transitions to DT_StateJoin when REventPermitjoinEnabled is received. |
| 143 | + */ |
| 144 | +static void DT_StateIdle(DeviceTickPrivate *d, const Event &event) |
| 145 | +{ |
| 146 | + if (event.what() == REventPermitjoinEnabled) |
| 147 | + { |
| 148 | + DT_SetState(d, DT_StateJoin); |
| 149 | + } |
| 150 | + else if (event.resource() == RLocal) |
| 151 | + { |
| 152 | + if (event.what() == REventStateTimeout) |
| 153 | + { |
| 154 | + DT_PollNextIdleDevice(d); |
| 155 | + DT_StartTimer(d, TICK_INTERVAL_IDLE); |
| 156 | + } |
| 157 | + else if (event.what() == REventStateEnter) |
| 158 | + { |
| 159 | + DT_StartTimer(d, TICK_INTERVAL_IDLE); |
| 160 | + } |
| 161 | + else if (event.what() == REventStateLeave) |
| 162 | + { |
| 163 | + DT_StopTimer(d); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +/*! Adds a joining device entry to the queue if not already present. |
| 169 | + */ |
| 170 | +static void DT_RegisterJoiningDevice(DeviceTickPrivate *d, DeviceKey deviceKey, quint8 macCapabilities) |
| 171 | +{ |
| 172 | + Q_ASSERT(deviceKey != 0); // if this triggers we have problems elsewhere |
| 173 | + |
| 174 | + auto i = std::find_if(d->joinDevices.cbegin(), d->joinDevices.cend(), [&deviceKey](const JoinDevice &dev) |
| 175 | + { |
| 176 | + return deviceKey == dev.deviceKey; |
| 177 | + }); |
| 178 | + |
| 179 | + if (i == d->joinDevices.cend()) |
| 180 | + { |
| 181 | + JoinDevice dev; |
| 182 | + dev.deviceKey = deviceKey; |
| 183 | + dev.macCapabilities = macCapabilities; |
| 184 | + d->joinDevices.push_back(dev); |
| 185 | + DBG_Printf(DBG_INFO, "DEV Tick: fast poll 0x%016llX, mac capabilities: 0x%02X\n", deviceKey, macCapabilities); |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +/*! Emits REventPoll to the next device in DT_StateJoin. |
| 190 | + */ |
| 191 | +static void DT_PollNextJoiningDevice(DeviceTickPrivate *d) |
| 192 | +{ |
| 193 | + if (d->joinDevices.empty()) |
| 194 | + { |
| 195 | + return; |
| 196 | + } |
| 197 | + |
| 198 | + d->devIter %= d->joinDevices.size(); |
| 199 | + Q_ASSERT(d->devIter < d->joinDevices.size()); |
| 200 | + |
| 201 | + const JoinDevice &device = d->joinDevices.at(d->devIter); |
| 202 | + emit d->q->eventNotify(Event(RDevices, REventPoll, 0, device.deviceKey)); |
| 203 | + d->devIter++; |
| 204 | +} |
| 205 | + |
| 206 | +/*! This state is active while Permit Join is enabled. |
| 207 | +
|
| 208 | + When a REventDeviceAnnounce event is received, the device is added to a joining |
| 209 | + queue and processed exclusivly and quickly. |
| 210 | +
|
| 211 | + The state transitions to DT_StateIdle when Permit Join is disabled. |
| 212 | + */ |
| 213 | +static void DT_StateJoin(DeviceTickPrivate *d, const Event &event) |
| 214 | +{ |
| 215 | + if (event.what() == REventPermitjoinDisabled) |
| 216 | + { |
| 217 | + DT_SetState(d, DT_StateIdle); |
| 218 | + } |
| 219 | + else if (event.what() == REventDeviceAnnounce) |
| 220 | + { |
| 221 | + DBG_Printf(DBG_INFO, "DEV Tick.Join: %s\n", event.what()); |
| 222 | + DT_RegisterJoiningDevice(d, event.deviceKey(), static_cast<quint8>(event.num())); |
| 223 | + } |
| 224 | + else if (event.resource() == RLocal) |
| 225 | + { |
| 226 | + if (event.what() == REventStateTimeout) |
| 227 | + { |
| 228 | + DT_PollNextJoiningDevice(d); |
| 229 | + DT_StartTimer(d, TICK_INTERVAL_JOIN); |
| 230 | + } |
| 231 | + else if (event.what() == REventStateEnter) |
| 232 | + { |
| 233 | + DT_StartTimer(d, TICK_INTERVAL_JOIN); |
| 234 | + } |
| 235 | + else if (event.what() == REventStateLeave) |
| 236 | + { |
| 237 | + DT_StopTimer(d); |
| 238 | + d->joinDevices.clear(); |
| 239 | + } |
| 240 | + } |
| 241 | +} |
0 commit comments