|
10 | 10 |
|
11 | 11 | #include "de_web_plugin.h"
|
12 | 12 | #include "de_web_plugin_private.h"
|
| 13 | +#include "utils/utils.h" |
13 | 14 | #include "zdp_handlers.h"
|
14 | 15 |
|
15 | 16 | /*! Handle the case that a node (re)joins the network.
|
@@ -250,6 +251,126 @@ void DeRestPluginPrivate::handleDeviceAnnceIndication(const deCONZ::ApsDataIndic
|
250 | 251 | }
|
251 | 252 | }
|
252 | 253 |
|
| 254 | +struct MapMfCode |
| 255 | +{ |
| 256 | + quint64 macPrefix; |
| 257 | + quint16 mfcode; |
| 258 | + /* Bits 9-15. These bits indicate the revision of the ZigBee Pro Core specification that the running stack is implemented to. Prior |
| 259 | + to revision 21 of the specification these bits were reserved and thus set to 0. A stack that is compliant to revision 22 |
| 260 | + would set these bits to 22 (0010110b). A stack shall indicate the revision of the specification it is compliant to by |
| 261 | + setting these bits. |
| 262 | +
|
| 263 | + 0x0000 Reserved prior Rev. 21 |
| 264 | + 0x2A00 (21 << 9) Rev. 21 |
| 265 | + 0x2C00 (22 << 9) Rev. 22 |
| 266 | + */ |
| 267 | + quint16 serverMask; |
| 268 | +}; |
| 269 | + |
| 270 | +static const std::array<MapMfCode, 3> mapMfCode = { |
| 271 | + { |
| 272 | + { 0x04cf8c0000000000ULL, 0x115F, 0x0040}, // Xiaomi |
| 273 | + { 0x54ef440000000000ULL, 0x115F, 0x0040} // Xiaomi |
| 274 | + } |
| 275 | +}; |
| 276 | + |
| 277 | +/*! Sends a Node Descriptor response. |
| 278 | +
|
| 279 | + Sends modified Manufacturer Code and Server Mask for some devices. |
| 280 | +
|
| 281 | + \param ind - a ZDP NodeDescriptor_req |
| 282 | + \param apsCtrl - APS controller instance |
| 283 | + */ |
| 284 | +void ZDP_HandleNodeDescriptorRequest(const deCONZ::ApsDataIndication &ind, deCONZ::ApsController *apsCtrl) |
| 285 | +{ |
| 286 | + if (!apsCtrl) |
| 287 | + { |
| 288 | + return; |
| 289 | + } |
| 290 | + |
| 291 | + const deCONZ::Node *self = getCoreNode(apsCtrl->getParameter(deCONZ::ParamMacAddress), apsCtrl); |
| 292 | + |
| 293 | + if (!self) |
| 294 | + { |
| 295 | + return; |
| 296 | + } |
| 297 | + |
| 298 | + quint8 seq; |
| 299 | + quint16 nwkAddr; |
| 300 | + |
| 301 | + { |
| 302 | + QDataStream stream(ind.asdu()); |
| 303 | + stream.setByteOrder(QDataStream::LittleEndian); |
| 304 | + |
| 305 | + stream >> seq; |
| 306 | + stream >> nwkAddr; |
| 307 | + |
| 308 | + if (stream.status() != QDataStream::Ok) |
| 309 | + { |
| 310 | + return; |
| 311 | + } |
| 312 | + } |
| 313 | + |
| 314 | + if (nwkAddr != self->address().nwk()) |
| 315 | + { |
| 316 | + return; |
| 317 | + } |
| 318 | + |
| 319 | + quint16 mfCode = VENDOR_DDEL; |
| 320 | + quint16 serverMask = 0x0040; // compatible with stack revisions below version 21 |
| 321 | + QByteArray ndRaw; |
| 322 | + |
| 323 | + if (!self->nodeDescriptor().isNull()) |
| 324 | + { |
| 325 | + ndRaw = self->nodeDescriptor().toByteArray(); |
| 326 | + serverMask = static_cast<quint16>(self->nodeDescriptor().serverMask()) & 0xFFFF; |
| 327 | + } |
| 328 | + else // fallback if not known |
| 329 | + { |
| 330 | + ndRaw = QByteArray("\x10\x40\x0f\x35\x11\x47\x2b\x00\x40\x00\x2b\x00\x00", 13); |
| 331 | + } |
| 332 | + |
| 333 | + auto i = std::find_if(mapMfCode.cbegin(), mapMfCode.cend(), [&ind](const auto &entry) { |
| 334 | + return (ind.srcAddress().ext() & entry.macPrefix) == entry.macPrefix; |
| 335 | + }); |
| 336 | + |
| 337 | + if (i != mapMfCode.cend()) |
| 338 | + { |
| 339 | + mfCode = i->mfcode; |
| 340 | + serverMask = i->serverMask; |
| 341 | + } |
| 342 | + |
| 343 | + { // change manufacturer code and server mask if needed |
| 344 | + QDataStream stream(&ndRaw, QIODevice::WriteOnly); |
| 345 | + stream.setByteOrder(QDataStream::LittleEndian); |
| 346 | + |
| 347 | + stream.device()->seek(3); |
| 348 | + stream << mfCode; |
| 349 | + stream.device()->seek(8); |
| 350 | + stream << serverMask; |
| 351 | + } |
| 352 | + |
| 353 | + deCONZ::ApsDataRequest req; |
| 354 | + |
| 355 | + req.setProfileId(ZDP_PROFILE_ID); |
| 356 | + req.setSrcEndpoint(ZDO_ENDPOINT); |
| 357 | + req.setDstEndpoint(ZDO_ENDPOINT); |
| 358 | + req.setClusterId(ZDP_NODE_DESCRIPTOR_RSP_CLID); |
| 359 | + req.setDstAddressMode(deCONZ::ApsNwkAddress); |
| 360 | + req.setTxOptions(deCONZ::ApsTxAcknowledgedTransmission); |
| 361 | + req.dstAddress() = ind.srcAddress(); |
| 362 | + |
| 363 | + QDataStream stream(&req.asdu(), QIODevice::WriteOnly); |
| 364 | + stream.setByteOrder(QDataStream::LittleEndian); |
| 365 | + |
| 366 | + stream << seq; |
| 367 | + stream << quint8(ZDP_SUCCESS); |
| 368 | + stream << nwkAddr; |
| 369 | + stream.writeRawData(ndRaw.constData(), ndRaw.size()); |
| 370 | + |
| 371 | + if (apsCtrl->apsdeDataRequest(req) == deCONZ::Success) { } |
| 372 | +} |
| 373 | + |
253 | 374 | /*! Handle node descriptor response.
|
254 | 375 | \param ind a ZDP NodeDescriptor_rsp
|
255 | 376 | */
|
|
0 commit comments