示例#1
0
文件: COC.cpp 项目: marerh/Homegear
void COC::lineReceived(const std::string& data)
{
    try
    {
    	std::string packetHex;
		if(stackPrefix.empty())
		{
			if(data.size() > 0 && data.at(0) == '*') return;
			packetHex = data;
		}
		else
		{
			if(data.size() + 1 <= stackPrefix.size()) return;
			if(data.substr(0, stackPrefix.size()) != stackPrefix || data.at(stackPrefix.size()) == '*') return;
			else packetHex = data.substr(stackPrefix.size());
		}
		if(packetHex.size() > 21) //21 is minimal packet length (=10 Byte + COC "Z")
		{
			std::shared_ptr<MAXPacket> packet(new MAXPacket(packetHex, BaseLib::HelperFunctions::getTime()));
			raisePacketReceived(packet);
		}
		else if(!packetHex.empty())
		{
			if(packetHex == "LOVF") _out.printWarning("Warning: COC with id " + _settings->id + " reached 1% limit. You need to wait, before sending is allowed again.");
			else if(packetHex == "Z") return;
			else _out.printWarning("Warning: Too short packet received: " + packetHex);
		}
    }
    catch(const std::exception& ex)
    {
        _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
    }
    catch(BaseLib::Exception& ex)
    {
        _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
    }
    catch(...)
    {
        _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__);
    }
}
示例#2
0
文件: CUNO.cpp 项目: marerh/Homegear
void CUNO::processData(std::vector<uint8_t>& data)
{
	try
	{
		if(data.empty()) return;
		std::string packets;
		packets.insert(packets.end(), data.begin(), data.end());

		std::istringstream stringStream(packets);
		std::string packetHex;
		while(std::getline(stringStream, packetHex))
		{
			if(packetHex.size() > 21) //21 is minimal packet length (=10 Byte + CUNO "A")
        	{
				std::shared_ptr<BidCoSPacket> packet(new BidCoSPacket(packetHex, BaseLib::HelperFunctions::getTime()));
				raisePacketReceived(packet);
        	}
        	else if(!packetHex.empty())
        	{
        		if(packetHex == "LOVF") _out.printWarning("Warning: CUNO with id " + _settings->id + " reached 1% limit. You need to wait, before sending is allowed again.");
        		else if(packetHex == "A") continue;
        		else _out.printWarning("Warning: Too short packet received: " + packetHex);
        	}
		}
	}
    catch(const std::exception& ex)
    {
        _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
    }
    catch(BaseLib::Exception& ex)
    {
        _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
    }
    catch(...)
    {
        _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__);
    }
}
示例#3
0
void TICC1100::mainThread()
{
    try
    {
		int32_t pollResult;
		int32_t bytesRead;
		std::vector<char> readBuffer({'0'});

        while(!_stopCallbackThread && _fileDescriptor->descriptor > -1 && _gpioDescriptors[1]->descriptor > -1)
        {
        	if(_stopped)
        	{
        		std::this_thread::sleep_for(std::chrono::milliseconds(200));
        		continue;
        	}
        	pollfd pollstruct {
				(int)_gpioDescriptors[1]->descriptor,
				(short)(POLLPRI | POLLERR),
				(short)0
			};

			pollResult = poll(&pollstruct, 1, 100);
			/*if(pollstruct.revents & POLLERR)
			{
				_out.printWarning("Warning: Error polling GPIO. Reopening...");
				closeGPIO();
				std::this_thread::sleep_for(std::chrono::milliseconds(1000));
				openGPIO(_settings->gpio1);
			}*/
			if(pollResult > 0)
			{
				if(lseek(_gpioDescriptors[1]->descriptor, 0, SEEK_SET) == -1) throw BaseLib::Exception("Could not poll gpio: " + std::string(strerror(errno)));
				bytesRead = read(_gpioDescriptors[1]->descriptor, &readBuffer[0], 1);
				if(!bytesRead) continue;
				if(readBuffer.at(0) == 0x30)
				{
					if(!_sending) _txMutex.try_lock(); //We are receiving, don't send now
					continue; //Packet is being received. Wait for GDO high
				}
				if(_sending) endSending();
				else
				{
					//sendCommandStrobe(CommandStrobes::Enum::SIDLE);
					std::shared_ptr<MAXPacket> packet;
					if(crcOK())
					{
						uint8_t firstByte = readRegister(Registers::Enum::FIFO);
						std::vector<uint8_t> packetBytes = readRegisters(Registers::Enum::FIFO, firstByte + 1); //Read packet + RSSI
						packetBytes[0] = firstByte;
						if(packetBytes.size() >= 9) packet.reset(new MAXPacket(packetBytes, true, BaseLib::HelperFunctions::getTime()));
						else _out.printWarning("Warning: Too small packet received: " + BaseLib::HelperFunctions::getHexString(packetBytes));
					}
					else _out.printDebug("Debug: MAX! packet received, but CRC failed.");
					if(!_sendingPending)
					{
						sendCommandStrobe(CommandStrobes::Enum::SFRX);
						sendCommandStrobe(CommandStrobes::Enum::SRX);
					}
					if(packet)
					{
						if(_firstPacket) _firstPacket = false;
						else raisePacketReceived(packet);
					}
				}
				_txMutex.unlock(); //Packet sent or received, now we can send again
			}
			else if(pollResult < 0)
			{
				_txMutex.unlock();
				_out.printError("Error: Could not poll gpio: " + std::string(strerror(errno)) + ". Reopening...");
				closeGPIO(1);
				std::this_thread::sleep_for(std::chrono::milliseconds(1000));
				openGPIO(1, true);
			}
			//pollResult == 0 is timeout
        }
    }
    catch(const std::exception& ex)
    {
    	_txMutex.unlock();
        _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
    }
    catch(BaseLib::Exception& ex)
    {
    	_txMutex.unlock();
        _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
    }
    catch(...)
    {
    	_txMutex.unlock();
        _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__);
    }
    try
    {
		if(!_stopCallbackThread && (_fileDescriptor->descriptor == -1 || _gpioDescriptors[1]->descriptor == -1))
		{
			_out.printError("Connection to TI CC1101 closed inexpectedly... Trying to reconnect...");
			_stopCallbackThread = true; //Set to true, so that sendPacket aborts
			_txMutex.unlock(); //Make sure _txMutex is unlocked
			std::thread thread(&TICC1100::startListening, this);
			thread.detach();
		}
		else _txMutex.unlock(); //Make sure _txMutex is unlocked
	}
    catch(const std::exception& ex)
    {
        _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
    }
    catch(BaseLib::Exception& ex)
    {
        _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what());
    }
    catch(...)
    {
        _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__);
    }
}