diff --git a/misc/Config Directory/homematicbidcos.conf b/misc/Config Directory/homematicbidcos.conf index 4673782..b3d844a 100644 --- a/misc/Config Directory/homematicbidcos.conf +++ b/misc/Config Directory/homematicbidcos.conf @@ -82,21 +82,21 @@ processBroadcastWithAesEnabled = false #port = 2017 ## The CA certificate -#caFile = +#caFile = /etc/homegear/ca/cacert.pem ## The client certificate -#certFile = +#certFile = /etc/homegear/ca/certs/gateway-client.crt ## The client key file -#keyFile = +#keyFile = /etc/homegear/ca/private/gateway-client.key ## Use the ID defined above to verify the common name of the certificate ## Default: false #useIdForHostnameVerification = true ## Default: responseDelay = 95 -## Should be "95" for CUL or COC, "100" for TI CC1101, "90" for Homegear Gateway and "60" for HM-CFG-LAN or HM-LGW -#responseDelay = 95 +## Should be "95" for CUL or COC, "100" for TI CC1101, "98" for Homegear Gateway and "60" for HM-CFG-LAN or HM-LGW +#responseDelay = 98 ####################################### ### HomeMatic Wireless LAN Gateway ### diff --git a/src/HomeMaticCentral.cpp b/src/HomeMaticCentral.cpp index e1e48d7..449dffe 100644 --- a/src/HomeMaticCentral.cpp +++ b/src/HomeMaticCentral.cpp @@ -3952,7 +3952,12 @@ void HomeMaticCentral::handleAck(int32_t messageCounter, std::shared_ptrmessageType() == 0x01 && sentPacket->payload()->at(0) == 0x00 && sentPacket->payload()->at(1) == 0x06) || (sentPacket && sentPacket->messageType() == 0x11 && sentPacket->payload()->at(0) == 0x04 && sentPacket->payload()->at(1) == 0x00)) { std::shared_ptr peer = getPeer(packet->senderAddress()); - if(peer) deletePeer(peer->getID()); + if(peer) + { + uint64_t peerId = peer->getID(); + peer.reset(); + deletePeer(peerId); + } } } else if(queue->getQueueType() == BidCoSQueueType::SETAESKEY) @@ -4555,7 +4560,12 @@ PVariable HomeMaticCentral::deleteDevice(BaseLib::PRpcClientInfo clientInfo, uin _bl->threadManager.start(_resetThread, false, &HomeMaticCentral::unpair, this, id, defer); } //Force delete - if(force) deletePeer(peer->getID()); + if(force) + { + uint64_t peerId = peer->getID(); + peer.reset(); + deletePeer(peerId); + } else { int32_t waitIndex = 0; diff --git a/src/PhysicalInterfaces/AesHandshake.cpp b/src/PhysicalInterfaces/AesHandshake.cpp index 16e4bd8..ed1f126 100644 --- a/src/PhysicalInterfaces/AesHandshake.cpp +++ b/src/PhysicalInterfaces/AesHandshake.cpp @@ -136,6 +136,8 @@ std::shared_ptr AesHandshake::getCFrame(std::shared_ptr cFrame; try { + if(_myAddress == -1) _out.printWarning("Warning: address is unset in AesHandshake."); + std::vector cPayload; cPayload.reserve(8); cPayload.push_back(0x04); @@ -203,6 +205,8 @@ std::shared_ptr AesHandshake::getAFrame(std::shared_ptr decryptGuard(_decryptMutex); if(_bl->debugLevel >= 4)_out.printInfo("Info: r-Frame is: " + rFrame->hexString()); std::vector rfKey; @@ -348,6 +352,7 @@ void AesHandshake::setMFrame(std::shared_ptr mFrame) std::shared_ptr AesHandshake::getRFrame(std::shared_ptr cFrame, std::shared_ptr& mFrame, uint32_t keyIndex) { + if(_myAddress == -1) _out.printWarning("Warning: address is unset in AesHandshake."); if(_bl->debugLevel >= 4) _out.printInfo("Info: c-Frame is: " + cFrame->hexString()); std::shared_ptr rFrame; diff --git a/src/PhysicalInterfaces/AesHandshake.h b/src/PhysicalInterfaces/AesHandshake.h index ce6528c..caf22e9 100644 --- a/src/PhysicalInterfaces/AesHandshake.h +++ b/src/PhysicalInterfaces/AesHandshake.h @@ -71,7 +71,7 @@ class AesHandshake BaseLib::SharedObjects* _bl = nullptr; BaseLib::Output _out; - int32_t _myAddress = 0x1C6940; + int32_t _myAddress = -1; std::vector _rfKey; std::vector _oldRfKey; uint32_t _currentRfKeyIndex = 0; diff --git a/src/PhysicalInterfaces/Cul.cpp b/src/PhysicalInterfaces/Cul.cpp index 5189b39..750d532 100644 --- a/src/PhysicalInterfaces/Cul.cpp +++ b/src/PhysicalInterfaces/Cul.cpp @@ -48,7 +48,6 @@ Cul::Cul(std::shared_ptr settings) settings->listenThreadPolicy = SCHED_FIFO; } - _aesHandshake.reset(new AesHandshake(_bl, _out, _myAddress, _rfKey, _oldRfKey, _currentRfKeyIndex)); memset(&_termios, 0, sizeof(termios)); } diff --git a/src/PhysicalInterfaces/Cunx.cpp b/src/PhysicalInterfaces/Cunx.cpp index 8974653..d15a839 100644 --- a/src/PhysicalInterfaces/Cunx.cpp +++ b/src/PhysicalInterfaces/Cunx.cpp @@ -47,8 +47,6 @@ Cunx::Cunx(std::shared_ptr settings settings->listenThreadPriority = 45; settings->listenThreadPolicy = SCHED_FIFO; } - - _aesHandshake.reset(new AesHandshake(_bl, _out, _myAddress, _rfKey, _oldRfKey, _currentRfKeyIndex)); } Cunx::~Cunx() diff --git a/src/PhysicalInterfaces/HomegearGateway.cpp b/src/PhysicalInterfaces/HomegearGateway.cpp index 3fedc24..583c2ca 100644 --- a/src/PhysicalInterfaces/HomegearGateway.cpp +++ b/src/PhysicalInterfaces/HomegearGateway.cpp @@ -61,12 +61,25 @@ void HomegearGateway::startListening() { stopListening(); + if(!_aesHandshake) return; //AES is not initialized + + if(!GD::family->getCentral()) + { + _stopCallbackThread = true; + _out.printError("Error: Could not get central address. Stopping listening."); + return; + } + _myAddress = GD::family->getCentral()->getAddress(); + _aesHandshake->setMyAddress(_myAddress); + if(_settings->host.empty() || _settings->port.empty() || _settings->caFile.empty() || _settings->certFile.empty() || _settings->keyFile.empty()) { _out.printError("Error: Configuration of Homegear Gateway is incomplete. Please correct it in \"homematicbidcos.conf\"."); return; } + IBidCoSInterface::startListening(); + _tcpSocket.reset(new BaseLib::TcpSocket(_bl, _settings->host, _settings->port, true, _settings->caFile, true, _settings->certFile, _settings->keyFile)); _tcpSocket->setConnectionRetries(1); _tcpSocket->setReadTimeout(5000000); @@ -75,7 +88,6 @@ void HomegearGateway::startListening() _stopCallbackThread = false; if(_settings->listenThreadPriority > -1) _bl->threadManager.start(_listenThread, true, _settings->listenThreadPriority, _settings->listenThreadPolicy, &HomegearGateway::listen, this); else _bl->threadManager.start(_listenThread, true, &HomegearGateway::listen, this); - IPhysicalInterface::startListening(); } catch(const std::exception& ex) { @@ -95,12 +107,12 @@ void HomegearGateway::stopListening() { try { + IBidCoSInterface::stopListening(); _stopCallbackThread = true; if(_tcpSocket) _tcpSocket->close(); _bl->threadManager.join(_listenThread); _stopped = true; _tcpSocket.reset(); - IPhysicalInterface::stopListening(); } catch(const std::exception& ex) { @@ -410,4 +422,4 @@ void HomegearGateway::processPacket(std::string& data) } } -} \ No newline at end of file +} diff --git a/src/PhysicalInterfaces/IBidCoSInterface.cpp b/src/PhysicalInterfaces/IBidCoSInterface.cpp index dc0ac63..b6d1fd1 100644 --- a/src/PhysicalInterfaces/IBidCoSInterface.cpp +++ b/src/PhysicalInterfaces/IBidCoSInterface.cpp @@ -414,7 +414,7 @@ void IBidCoSInterface::processReceivedPacket(std::shared_ptr packe queuePacket(mFrame, mFrame->timeSending() + 1200); } // }}} - + queuePacket(rFrame); return; }