Skip to content

Commit

Permalink
Merged testing
Browse files Browse the repository at this point in the history
  • Loading branch information
hfedcba committed Jul 30, 2019
2 parents 3ab4e3d + 12634af commit acf590a
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 70 deletions.
3 changes: 2 additions & 1 deletion getVersion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
dir=`mktemp -d`
cat > "$dir/libhomegear-base-version.cpp" <<-'EOF'
#include "homegear-base/BaseLib.h"
#include <iostream>
int main(int argc, char** argv)
{
std::cout << BaseLib::SharedObjects::version() << std::endl;
return 0;
}
EOF
g++ -std=c++11 -o $dir/libhomegear-base-version $dir/libhomegear-base-version.cpp -lhomegear-base -lgcrypt -lgnutls -lpthread
g++ -std=c++11 -o $dir/libhomegear-base-version $dir/libhomegear-base-version.cpp -lhomegear-base -lgcrypt -lgnutls -lpthread -latomic
chmod 755 $dir/libhomegear-base-version
$dir/libhomegear-base-version
rm -Rf $dir
2 changes: 2 additions & 0 deletions src/Insteon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "InsteonCentral.h"
#include "GD.h"

#include <iomanip>

namespace Insteon
{

Expand Down
26 changes: 14 additions & 12 deletions src/InsteonCentral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "InsteonCentral.h"
#include "GD.h"

#include <iomanip>

namespace Insteon {

InsteonCentral::InsteonCentral(ICentralEventSink* eventHandler) : BaseLib::Systems::ICentral(INSTEON_FAMILY_ID, GD::bl, eventHandler)
Expand Down Expand Up @@ -504,14 +506,14 @@ bool InsteonCentral::onPacketReceived(std::string& senderID, std::shared_ptr<Bas
if(_disposing) return false;
std::shared_ptr<InsteonPacket> insteonPacket(std::dynamic_pointer_cast<InsteonPacket>(packet));
if(!insteonPacket) return false;
if(GD::bl->debugLevel >= 4) std::cout << BaseLib::HelperFunctions::getTimeString(insteonPacket->timeReceived()) << " Insteon packet received: " + insteonPacket->hexString() << std::endl;
if(GD::bl->debugLevel >= 4) std::cout << BaseLib::HelperFunctions::getTimeString(insteonPacket->getTimeReceived()) << " Insteon packet received: " + insteonPacket->hexString() << std::endl;
if(insteonPacket->senderAddress() == _address) //Packet spoofed
{
std::shared_ptr<InsteonPeer> peer(getPeer(insteonPacket->destinationAddress()));
if(peer)
{
if(senderID != peer->getPhysicalInterfaceID()) return true; //Packet we sent was received by another interface
GD::out.printWarning("Warning: Central address of packet to peer " + std::to_string(peer->getID()) + " was spoofed. Packet was: " + packet->hexString());
GD::out.printWarning("Warning: Central address of packet to peer " + std::to_string(peer->getID()) + " was spoofed. Packet was: " + insteonPacket->hexString());
peer->serviceMessages->set("CENTRAL_ADDRESS_SPOOFED", 1, 0);
std::shared_ptr<std::vector<std::string>> valueKeys(new std::vector<std::string> { "CENTRAL_ADDRESS_SPOOFED" });
std::shared_ptr<std::vector<PVariable>> values(new std::vector<PVariable> { PVariable(new Variable((int32_t)1)) });
Expand All @@ -527,7 +529,7 @@ bool InsteonCentral::onPacketReceived(std::string& senderID, std::shared_ptr<Bas
if(!(_pairing && insteonPacket->messageType() == 0x01) && physicalInterface->getID() != senderID) return true;

bool handled = false;
if(_receivedPackets.set(insteonPacket->senderAddress(), insteonPacket, insteonPacket->timeReceived())) handled = true;
if(_receivedPackets.set(insteonPacket->senderAddress(), insteonPacket, insteonPacket->getTimeReceived())) handled = true;
if(insteonPacket->flags() == InsteonPacketFlags::DirectNak || insteonPacket->flags() == InsteonPacketFlags::GroupCleanupDirectNak)
{
handleNak(insteonPacket);
Expand Down Expand Up @@ -1554,9 +1556,9 @@ void InsteonCentral::handleNak(std::shared_ptr<InsteonPacket> packet)
else GD::out.printDebug("Debug: NACK received from 0x" + BaseLib::HelperFunctions::getHexString(packet->senderAddress(), 6));
}

if(sentPacket && sentPacket->messageType() == 0x2F && sentPacket->payload()->size() == 14)
if(sentPacket && sentPacket->messageType() == 0x2F && sentPacket->payload().size() == 14)
{
if(sentPacket->payload()->at(0) == 0x01 && sentPacket->payload()->at(1) == 0x00)
if(sentPacket->payload().at(0) == 0x01 && sentPacket->payload().at(1) == 0x00)
{
//First "read"
enablePairingMode(packet->interfaceID());
Expand Down Expand Up @@ -1699,12 +1701,12 @@ void InsteonCentral::handleDatabaseOpResponse(std::shared_ptr<InsteonPacket> pac
if(!queue) return;
std::shared_ptr<InsteonPacket> sentPacket(_sentPackets.get(packet->senderAddress()));

if(queue->getQueueType() == PacketQueueType::PAIRING && sentPacket && sentPacket->messageType() == 0x2F && sentPacket->payload()->size() == 14)
if(queue->getQueueType() == PacketQueueType::PAIRING && sentPacket && sentPacket->messageType() == 0x2F && sentPacket->payload().size() == 14)
{
if(queue->peer && sentPacket->payload()->at(0) == 1 && packet->payload()->size() == 14 && (packet->payload()->at(5) & 0x80)) //Read and "record is in use"
if(queue->peer && sentPacket->payload().at(0) == 1 && packet->payload().size() == 14 && (packet->payload().at(5) & 0x80)) //Read and "record is in use"
{
int32_t address = (packet->payload()->at(7) << 16) + (packet->payload()->at(8) << 8) + packet->payload()->at(9);
if(packet->payload()->at(5) & 0x40) //Controller bit?
int32_t address = (packet->payload().at(7) << 16) + (packet->payload().at(8) << 8) + packet->payload().at(9);
if(packet->payload().at(5) & 0x40) //Controller bit?
{
if(address == queue->peer->getPhysicalInterface()->address()) //Peer already knows me
{
Expand All @@ -1722,15 +1724,15 @@ void InsteonCentral::handleDatabaseOpResponse(std::shared_ptr<InsteonPacket> pac
GD::out.printWarning("Warning: Peer \"0x" + BaseLib::HelperFunctions::getHexString(packet->senderAddress(), 6) + "\" is already paired to another device with address \"0x" + BaseLib::HelperFunctions::getHexString(address, 6) + "\".");
}
}
else if(sentPacket->payload()->at(5) == 0xE2)
else if(sentPacket->payload().at(5) == 0xE2)
{
if(!peerExists(packet->senderAddress()))
{
addPeer(queue->peer);
}
}
}
else if(queue->getQueueType() == PacketQueueType::UNPAIRING && sentPacket && sentPacket->payload()->size() > 3 && sentPacket->payload()->at(3) == 0xFF)
else if(queue->getQueueType() == PacketQueueType::UNPAIRING && sentPacket && sentPacket->payload().size() > 3 && sentPacket->payload().at(3) == 0xFF)
{
std::shared_ptr<InsteonPeer> peer = getPeer(packet->senderAddress());
if(peer)
Expand All @@ -1748,7 +1750,7 @@ void InsteonCentral::handleDatabaseOpResponse(std::shared_ptr<InsteonPacket> pac
//Remove packets needed for a NAK as first response
while(!queue->isEmpty())
{
if(queue->front()->getType() == QueueEntryType::PACKET && queue->front()->getPacket()->payload()->size() == 14 && queue->front()->getPacket()->payload()->at(1) == 2 && queue->front()->getPacket()->messageType() == 0x2F)
if(queue->front()->getType() == QueueEntryType::PACKET && queue->front()->getPacket()->payload().size() == 14 && queue->front()->getPacket()->payload().at(1) == 2 && queue->front()->getPacket()->messageType() == 0x2F)
{
queue->processCurrentQueueEntry(false); //Necessary for the packet to be sent after silent popping
break;
Expand Down
12 changes: 6 additions & 6 deletions src/InsteonMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ bool InsteonMessage::typeIsEqual(std::shared_ptr<InsteonPacket> packet)
try
{
if(_messageType != packet->messageType() || (_messageSubtype > -1 && packet->messageSubtype() > -1 && _messageSubtype != packet->messageSubtype()) || _messageFlags != packet->flags()) return false;
std::vector<uint8_t>* payload = packet->payload();
std::vector<uint8_t>& payload = packet->payload();
if(_subtypes.empty()) return true;
for(std::vector<std::pair<uint32_t, int32_t>>::const_iterator i = _subtypes.begin(); i != _subtypes.end(); ++i)
{
if(i->first >= payload->size()) return false;
if(payload->at(i->first) != i->second) return false;
if(i->first >= payload.size()) return false;
if(payload.at(i->first) != i->second) return false;
}
return true;
}
Expand Down Expand Up @@ -165,12 +165,12 @@ bool InsteonMessage::typeIsEqual(std::shared_ptr<InsteonMessage> message, std::s
if(message->getMessageType() != packet->messageType() || message->getMessageFlags() != packet->flags()) return false;
if(message->getMessageSubtype() > -1 && packet->messageSubtype() > -1 && message->getMessageSubtype() != packet->messageSubtype()) return false;
std::vector<std::pair<uint32_t, int32_t>>* subtypes = message->getSubtypes();
std::vector<uint8_t>* payload = packet->payload();
std::vector<uint8_t>& payload = packet->payload();
if(subtypes == nullptr || subtypes->size() == 0) return true;
for(std::vector<std::pair<uint32_t, int32_t> >::const_iterator i = subtypes->begin(); i != subtypes->end(); ++i)
{
if(i->first >= payload->size()) return false;
if(payload->at(i->first) != i->second) return false;
if(i->first >= payload.size()) return false;
if(payload.at(i->first) != i->second) return false;
}
return true;
}
Expand Down
9 changes: 7 additions & 2 deletions src/InsteonPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@
#include "InsteonPacket.h"
#include "GD.h"

#include <iomanip>

namespace Insteon
{

const std::array<uint8_t, 9> InsteonPacket::_bitmask{0xFF, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF};

InsteonPacket::InsteonPacket()
{
}
Expand Down Expand Up @@ -519,13 +524,13 @@ bool InsteonPacket::equals(std::shared_ptr<InsteonPacket>& rhs)
{
if(_messageType != rhs->messageType()) return false;
if(_messageSubtype != rhs->messageSubtype()) return false;
if(_payload.size() != rhs->payload()->size()) return false;
if(_payload.size() != rhs->payload().size()) return false;
if(_senderAddress != rhs->senderAddress()) return false;
if(_destinationAddress != rhs->destinationAddress()) return false;
if(_flags != rhs->flags()) return false;
if(_extended != rhs->extended()) return false;
if(_hopsMax != rhs->hopsMax()) return false;
if(_payload == (*rhs->payload())) return true;
if(_payload == rhs->payload()) return true;
return false;
}
}
92 changes: 52 additions & 40 deletions src/InsteonPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,51 +52,63 @@ enum class InsteonPacketFlags : uint32_t

class InsteonPacket : public BaseLib::Systems::Packet
{
public:
//Properties
InsteonPacket();
InsteonPacket(std::string packet, std::string interfaceID = "", int64_t timeReceived = 0);
InsteonPacket(std::vector<char>& packet, std::string interfaceID = "", int64_t timeReceived = 0);
InsteonPacket(std::vector<uint8_t>& packet, std::string interfaceID = "", int64_t timeReceived = 0);
InsteonPacket(uint8_t messageType, uint8_t messageSubtype, int32_t destinationAddress, uint8_t hopsLeft, uint8_t hopsMax, InsteonPacketFlags flags, std::vector<uint8_t> payload);
virtual ~InsteonPacket();
public:
//Properties
InsteonPacket();
InsteonPacket(std::string packet, std::string interfaceID = "", int64_t timeReceived = 0);
InsteonPacket(std::vector<char>& packet, std::string interfaceID = "", int64_t timeReceived = 0);
InsteonPacket(std::vector<uint8_t>& packet, std::string interfaceID = "", int64_t timeReceived = 0);
InsteonPacket(uint8_t messageType, uint8_t messageSubtype, int32_t destinationAddress, uint8_t hopsLeft, uint8_t hopsMax, InsteonPacketFlags flags, std::vector<uint8_t> payload);
virtual ~InsteonPacket();

void import(std::vector<char>& packet);
void import(std::vector<uint8_t>& packet);
void import(std::string packetHex);
void import(std::vector<char>& packet);
void import(std::vector<uint8_t>& packet);
void import(std::string packetHex);

std::string interfaceID() { return _interfaceID; }
void setInterfaceID(std::string value) { _interfaceID = value; }
void setDestinationAddress(int32_t value) { _destinationAddress = value; }
bool extended() { return _extended; }
InsteonPacketFlags flags() { return _flags; }
void setFlags(InsteonPacketFlags value) { _flags = value; }
uint8_t hopsLeft() { return _hopsLeft; }
void setHopsLeft(uint8_t value) { _hopsLeft = value; }
uint8_t hopsMax() { return _hopsMax; }
void setHopsMax(uint8_t value) { _hopsMax = value; }
uint8_t messageType() { return _messageType; }
void setMessageType(uint8_t type) { _messageType = type; }
uint8_t messageSubtype() { return _messageSubtype; }
virtual std::string hexString();
virtual std::vector<char> byteArray();
uint8_t length() { return _length; }
int32_t senderAddress() { return _senderAddress; }
int32_t destinationAddress() { return _destinationAddress; }
uint8_t controlByte() { return _controlByte; }
std::string interfaceID() { return _interfaceID; }
void setInterfaceID(std::string value) { _interfaceID = value; }
void setDestinationAddress(int32_t value) { _destinationAddress = value; }
bool extended() { return _extended; }
InsteonPacketFlags flags() { return _flags; }
void setFlags(InsteonPacketFlags value) { _flags = value; }
uint8_t hopsLeft() { return _hopsLeft; }
void setHopsLeft(uint8_t value) { _hopsLeft = value; }
uint8_t hopsMax() { return _hopsMax; }
void setHopsMax(uint8_t value) { _hopsMax = value; }
uint8_t messageType() { return _messageType; }
void setMessageType(uint8_t type) { _messageType = type; }
uint8_t messageSubtype() { return _messageSubtype; }
std::vector<uint8_t>& payload() { return _payload; }
std::string hexString();
std::vector<char> byteArray();

virtual std::vector<uint8_t> getPosition(double index, double size, int32_t mask);
virtual void setPosition(double index, double size, std::vector<uint8_t>& value);
std::vector<uint8_t> getPosition(double index, double size, int32_t mask);
void setPosition(double index, double size, std::vector<uint8_t>& value);

bool equals(std::shared_ptr<InsteonPacket>& rhs);
protected:
std::string _interfaceID;
bool _extended = false;
InsteonPacketFlags _flags = InsteonPacketFlags::Direct;
uint8_t _hopsLeft = 0;
uint8_t _hopsMax = 0;
uint8_t _messageType = 0;
uint8_t _messageSubtype = 0;
bool equals(std::shared_ptr<InsteonPacket>& rhs);
private:
static const std::array<uint8_t, 9> _bitmask;

virtual uint8_t getByte(std::string);
int32_t getInt(std::string);
void calculateChecksum();
uint8_t _length = 0;
int32_t _senderAddress = 0;
int32_t _destinationAddress = 0;
uint8_t _controlByte = 0;
std::vector<uint8_t> _payload;
std::string _interfaceID;
bool _extended = false;
InsteonPacketFlags _flags = InsteonPacketFlags::Direct;
uint8_t _hopsLeft = 0;
uint8_t _hopsMax = 0;
uint8_t _messageType = 0;
uint8_t _messageSubtype = 0;

virtual uint8_t getByte(std::string);
int32_t getInt(std::string);
void calculateChecksum();
};

}
Expand Down
4 changes: 3 additions & 1 deletion src/InsteonPeer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "InsteonCentral.h"
#include "GD.h"

#include <iomanip>

namespace Insteon
{
std::shared_ptr<BaseLib::Systems::ICentral> InsteonPeer::getCentral()
Expand Down Expand Up @@ -596,7 +598,7 @@ void InsteonPeer::getValuesFromPacket(std::shared_ptr<InsteonPacket> packet, std
if(frame->subtype > -1 && packet->messageSubtype() != frame->subtype) continue;
int32_t channelIndex = frame->channelIndex;
int32_t channel = -1;
if(channelIndex >= 9 && (signed)packet->payload()->size() > (channelIndex - 9)) channel = packet->payload()->at(channelIndex - 9) - frame->channelIndexOffset;
if(channelIndex >= 9 && (signed)packet->payload().size() > (channelIndex - 9)) channel = packet->payload().at(channelIndex - 9) - frame->channelIndexOffset;
if(channel > -1 && frame->channelSize < 1.0) channel &= (0xFF >> (8 - std::lround(frame->channelSize * 10) % 10));
if(frame->channel > -1) channel = frame->channel;
if(frame->length > 0 && packet->length() != frame->length) continue;
Expand Down
16 changes: 8 additions & 8 deletions src/PhysicalInterfaces/Insteon_Hub_X10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,20 +386,20 @@ void InsteonHubX10::sendPacket(std::shared_ptr<BaseLib::Systems::Packet> packet)
return;
}

std::shared_ptr<InsteonPacket> insteonPacket(std::dynamic_pointer_cast<InsteonPacket>(packet));
if(!insteonPacket) return;

if(!_initComplete)
{
_out.printWarning("Warning: !!!Not!!! sending (Port " + _settings->port + "), because the init sequence is not completed: " + packet->hexString());
_out.printWarning("Warning: !!!Not!!! sending (Port " + _settings->port + "), because the init sequence is not completed: " + insteonPacket->hexString());
_sendMutex.unlock();
return;
}

_lastAction = BaseLib::HelperFunctions::getTime();

std::shared_ptr<InsteonPacket> insteonPacket(std::dynamic_pointer_cast<InsteonPacket>(packet));
if(!insteonPacket) return;

//Don't move this, because packet->hexString() also calculates the checksum!
_out.printInfo("Info: Sending (" + _settings->id + "): " + packet->hexString());
_out.printInfo("Info: Sending (" + _settings->id + "): " + insteonPacket->hexString());

std::vector<char> requestPacket { 0x02, 0x62 };
requestPacket.push_back(insteonPacket->destinationAddress() >> 16);
Expand All @@ -408,7 +408,7 @@ void InsteonHubX10::sendPacket(std::shared_ptr<BaseLib::Systems::Packet> packet)
requestPacket.push_back(((uint8_t)insteonPacket->flags() << 5) + ((uint8_t)insteonPacket->extended() << 4) + (insteonPacket->hopsLeft() << 2) + insteonPacket->hopsMax());
requestPacket.push_back(insteonPacket->messageType());
requestPacket.push_back(insteonPacket->messageSubtype());
requestPacket.insert(requestPacket.end(), insteonPacket->payload()->begin(), insteonPacket->payload()->end());
requestPacket.insert(requestPacket.end(), insteonPacket->payload().begin(), insteonPacket->payload().end());

std::vector<uint8_t> responsePacket;
for(int32_t i = 0; i < 20; i++)
Expand Down Expand Up @@ -805,14 +805,14 @@ void InsteonHubX10::listen()
catch(const BaseLib::SocketClosedException& ex)
{
_stopped = true;
_out.printWarning("Warning: " + ex.what());
_out.printWarning("Warning: " + std::string(ex.what()));
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
continue;
}
catch(const BaseLib::SocketOperationException& ex)
{
_stopped = true;
_out.printError("Error: " + ex.what());
_out.printError("Error: " + std::string(ex.what()));
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
continue;
}
Expand Down

0 comments on commit acf590a

Please sign in to comment.