Ejemplo n.º 1
0
uint8_t BayRF24::sendPayload(void) {
	if (_powerdown)
		powerUp();
	else
		stopListening();
	openWritingPipe (_pipe);
	uint8_t res;

	res = RF24::write(getPayload(), getPacketLength());
	uint8_t curr_pa = 0;
	while (!res && curr_pa < 4) {
		setPALevel((rf24_pa_dbm_e) curr_pa);
		delayMicroseconds(random(2000));
		res = RF24::write(getPayload(), getPacketLength());
		curr_pa++;
	}

	if (_powerdown)
		powerDown();
	else {
		txStandBy();
		startListening();
	}

	return !res;
}
Ejemplo n.º 2
0
void DS2Packet::printPacket(FILE* file) const
{
	if(type == DS2_8BIT)
	{
		fprintf(file, "0x%02x 0x%02x ", getAddress(), getPacketLength());
	}
	else if(type == DS2_16BIT)
	{
		fprintf(file, "0x%02x 0x%02x 0x%02x ", getAddress(), getPacketLength() >> 8, getPacketLength() & 0xff);
	}
Ejemplo n.º 3
0
    void Client::parseTransmit()
    {
        qDebug() << "parseTransmit():";
        if ( !(this->status_ == ST_ADMIN ||
               this->status_ == ST_CLIENT))
        {
            qDebug() << "cmd not allowed";
            this->disconnectFromHost();
            return;
        }

        if (buf_.size()<9) // TODO: remove magic number
            return;     // not all data avaliable

        qDebug() << "buf size" << buf_.size();
        qDebug() << "packet len" << getPacketLength(buf_);

        if (buf_.size()<getPacketLength(buf_)+9) // TODO: remove magic number
            return; // not all data avaliable

        qint16 clientId=getClientId(buf_);

        QByteArray cmd=getPacketData(buf_);

        QByteArray response;
        response.append(CSPYP1_PROTOCOL_ID);
        response.append(CSPYP1_PROTOCOL_VERSION);
        response.append(CMD1_TRANSMIT);
        response.append(toBytes(getId()));
        response.append(toBytes(cmd.size()));
        response.append(cmd);

        auto destClient = getServer()->searchClient(clientId);
        qDebug() << "Client to ID:" << clientId;

        if (destClient != getServer()->clientEnd())
        {
            qDebug() << "transmiting data..";
            destClient.value()->send(response);
        }

        buf_=buf_.right(buf_.size() - (getPacketLength(buf_) + 9));

        if (buf_.size() > 0)
            onDataReceived();   // If something in buffer - parse again
    }
Ejemplo n.º 4
0
bool OctreeQueryNode::packetIsDuplicate() const {
    // if shutting down, return immediately
    if (_isShuttingDown) {
        return false;
    }
    // since our packets now include header information, like sequence number, and createTime, we can't just do a memcmp
    // of the entire packet, we need to compare only the packet content...
    int numBytesPacketHeader = numBytesForPacketHeaderGivenPacketType(_myPacketType);
    
    if (_lastOctreePacketLength == getPacketLength()) {
        if (memcmp(_lastOctreePacket + (numBytesPacketHeader + OCTREE_PACKET_EXTRA_HEADERS_SIZE),
                _octreePacket + (numBytesPacketHeader + OCTREE_PACKET_EXTRA_HEADERS_SIZE),
                   getPacketLength() - (numBytesPacketHeader + OCTREE_PACKET_EXTRA_HEADERS_SIZE)) == 0) {
            return true;
        }
    }
    return false;
}
Ejemplo n.º 5
0
uint8_t checksum(char * packet)
{
	uint8_t checksum, i;
//	packet_len = getTypeLength(mypacket->data_type) + 3;
	for (i = 0; i < getPacketLength(packet) - 1; i++)
		checksum += packet[i];
	checksum = !checksum + 1;
	return checksum;
}
Ejemplo n.º 6
0
void OctreeQueryNode::resetOctreePacket() {
    // if shutting down, return immediately
    if (_isShuttingDown) {
        return;
    }

    // Whenever we call this, we will keep a copy of the last packet, so we can determine if the last packet has
    // changed since we last reset it. Since we know that no two packets can ever be identical without being the same
    // scene information, (e.g. the root node packet of a static scene), we can use this as a strategy for reducing
    // packet send rate.
    _lastOctreePacketLength = getPacketLength();
    memcpy(_lastOctreePacket, _octreePacket, _lastOctreePacketLength);

    // If we're moving, and the client asked for low res, then we force monochrome, otherwise, use
    // the clients requested color state.
    _currentPacketIsColor = getWantColor();
    _currentPacketIsCompressed = getWantCompression();
    OCTREE_PACKET_FLAGS flags = 0;
    if (_currentPacketIsColor) {
        setAtBit(flags,PACKET_IS_COLOR_BIT);
    }
    if (_currentPacketIsCompressed) {
        setAtBit(flags,PACKET_IS_COMPRESSED_BIT);
    }

    _octreePacketAvailableBytes = MAX_PACKET_SIZE;
    int numBytesPacketHeader = populatePacketHeader(reinterpret_cast<char*>(_octreePacket), _myPacketType);
    _octreePacketAt = _octreePacket + numBytesPacketHeader;
    _octreePacketAvailableBytes -= numBytesPacketHeader;

    // pack in flags
    OCTREE_PACKET_FLAGS* flagsAt = (OCTREE_PACKET_FLAGS*)_octreePacketAt;
    *flagsAt = flags;
    _octreePacketAt += sizeof(OCTREE_PACKET_FLAGS);
    _octreePacketAvailableBytes -= sizeof(OCTREE_PACKET_FLAGS);
    
    // pack in sequence number
    OCTREE_PACKET_SEQUENCE* sequenceAt = (OCTREE_PACKET_SEQUENCE*)_octreePacketAt;
    *sequenceAt = _sequenceNumber;
    _octreePacketAt += sizeof(OCTREE_PACKET_SEQUENCE);
    _octreePacketAvailableBytes -= sizeof(OCTREE_PACKET_SEQUENCE);

    // pack in timestamp
    OCTREE_PACKET_SENT_TIME now = usecTimestampNow();
    OCTREE_PACKET_SENT_TIME* timeAt = (OCTREE_PACKET_SENT_TIME*)_octreePacketAt;
    *timeAt = now;
    _octreePacketAt += sizeof(OCTREE_PACKET_SENT_TIME);
    _octreePacketAvailableBytes -= sizeof(OCTREE_PACKET_SENT_TIME);

    _octreePacketWaiting = false;
}
Ejemplo n.º 7
0
void VoxelNodeData::resetVoxelPacket() {
    // Whenever we call this, we will keep a copy of the last packet, so we can determine if the last packet has
    // changed since we last reset it. Since we know that no two packets can ever be identical without being the same
    // scene information, (e.g. the root node packet of a static scene), we can use this as a strategy for reducing
    // packet send rate.
    _lastVoxelPacketLength = getPacketLength();
    memcpy(_lastVoxelPacket, _voxelPacket, _lastVoxelPacketLength);

    // If we're moving, and the client asked for low res, then we force monochrome, otherwise, use 
    // the clients requested color state.    
    _currentPacketIsColor = (LOW_RES_MONO && getWantLowResMoving() && _viewFrustumChanging) ? false : getWantColor();
    PACKET_TYPE voxelPacketType = _currentPacketIsColor ? PACKET_TYPE_VOXEL_DATA : PACKET_TYPE_VOXEL_DATA_MONOCHROME;
    int numBytesPacketHeader = populateTypeAndVersion(_voxelPacket, voxelPacketType);
    _voxelPacketAt = _voxelPacket + numBytesPacketHeader;
    _voxelPacketAvailableBytes = MAX_VOXEL_PACKET_SIZE - numBytesPacketHeader;
    _voxelPacketWaiting = false;
}
Ejemplo n.º 8
0
void Header::prependSerializedHeader(vpr::BufferObjectWriter* packetWriter)
{
   // Set Packet length
   setPacketLength( packetWriter->getData()->size() + RIM_PACKET_HEAD_SIZE );

   std::vector<vpr::Uint8> header_data;
   vpr::BufferObjectWriter writer( &header_data );

   // Serialize all header data.
   writer.writeUint16( mRIMCode );
   writer.writeUint16( mPacketType );
   writer.writeUint32( mFrame );
   writer.writeUint32( mPacketLength );

   // Prepend header onto packets data
   packetWriter->getData()->insert( packetWriter->getData()->begin(),
                                   header_data.begin(),
                                   header_data.end() );

   packetWriter->setCurPos( getPacketLength() );

}
Ejemplo n.º 9
0
void OctreeQueryNode::octreePacketSent() {
    packetSent(_octreePacket, getPacketLength());
}
Ejemplo n.º 10
0
uint8_t ZBRxResponse::getDataLength() {
	return getPacketLength() - getDataOffset() - 1;
}
Ejemplo n.º 11
0
/**	Checks for availability of a valid response packet.
 *
 *	This function should always be called and return true prior to using results
 *	of a command.
 *
 *	@returns	true if a valid response packet is available
 */
boolean SM130::available()
{
	// If in SEEK mode and using DREADY pin, check the status
	if (cmd == CMD_SEEK_TAG && pinDREADY != 0xff)
	{
		if (!digitalRead(pinDREADY))
			return false;
	}

	// Set the maximum length of the expected response packet
	byte len;
	switch(cmd)
	{
	case CMD_ANTENNA_POWER:
	case CMD_AUTHENTICATE:
	case CMD_DEC_VALUE:
	case CMD_INC_VALUE:
	case CMD_WRITE_KEY:
	case CMD_HALT_TAG:
	case CMD_SLEEP:
		len = 4;
		break;
	case CMD_WRITE4:
	case CMD_WRITE_VALUE:
	case CMD_READ_VALUE:
		len = 8;
	case CMD_SEEK_TAG:
	case CMD_SELECT_TAG:
		len = 11;
		break;
	default:
		len = SIZE_PACKET;
	}

	// If valid data received, process the response packet
	if (receiveData(len) > 0)
	{
		// Init response variables
		tagType = tagLength = *tagString = 0;

		// If packet length is 2, the command failed. Set error code.
		errorCode = getPacketLength() < 3 ? data[2] : 0;

		// Process command response
		switch (getCommand())
		{
		case CMD_RESET:
		case CMD_VERSION:
			// RESET and VERSION commands produce the firmware version
			len = min(getPacketLength(), sizeof(versionString)) - 1;
			memcpy(versionString, data + 2, len);
			versionString[len] = 0;
			break;

		case CMD_SEEK_TAG:
		case CMD_SELECT_TAG:
			// If no error, get tag number
			if(errorCode == 0 && getPacketLength() >= 6)
			{
				tagLength = getPacketLength() - 2;
				tagType = data[2];
				memcpy(tagNumber, data + 3, tagLength);
				arrayToHex(tagString, tagNumber, tagLength);
			}
			break;

		case CMD_AUTHENTICATE:
			break;

		case CMD_READ16:
			break;

		case CMD_WRITE16:
		case CMD_WRITE4:
			break;

		case CMD_ANTENNA_POWER:
			errorCode = 0;
			antennaPower = data[2];
			break;

		case CMD_SLEEP:
			// If in SLEEP mode, no data is available
			return false;
		}

		// Data available
		return true;
	}
	// No data available
	return false;
}
Ejemplo n.º 12
0
/*
** Return a pointer to the error block.
*/
PTR_ret findPacketV1ErrorStart (PACKET_ptr packet_ptr)
{
  return (packet_ptr + getPacketLength(packet_ptr) - 
	  getPacketV1ErrorLength(packet_ptr));
}
Ejemplo n.º 13
0
/**	Checks for availability of a valid response packet.
 *
 *	This function should always be called and return true prior to using results
 *	of a command.
 *
 *	@returns	true if a valid response packet is available
 */
boolean SL018::available()
{
	// Set the maximum length of the expected response packet
	byte len;
	switch(cmd)
	{
	case CMD_IDLE:
	case CMD_RESET:
		len = 0;
		break;
	case CMD_LOGIN:
	case CMD_SET_LED:
	case CMD_SLEEP:
		len = 3;
		break;
	case CMD_READ4:
	case CMD_WRITE4:
	case CMD_READ_VALUE:
	case CMD_WRITE_VALUE:
	case CMD_DEC_VALUE:
	case CMD_INC_VALUE:
	case CMD_COPY_VALUE:
		len = 7;
		break;
	case CMD_WRITE_KEY:
		len = 9;
		break;
	case CMD_SEEK:
	case CMD_SELECT:
		len = 11;
		break;
	default:
		len = SIZE_PACKET;
	}

	// If valid data received, process the response packet
	if (len && receiveData(len) > 0)
	{
		// Init response variables
		tagType = tagLength = *tagString = 0;
		errorCode = data[2];

		// Process command response
		switch (getCommand())
		{
		case CMD_SEEK:
		case CMD_SELECT:
			// If no error, get tag number
			if(errorCode == 0 && getPacketLength() >= 7)
			{
				tagLength = getPacketLength() - 3;
				tagType = data[getPacketLength()];
				memcpy(tagNumber, data + 3, tagLength);
				arrayToHex(tagString, tagNumber, tagLength);
			}
			else if(cmd == CMD_SEEK)
			{
				// Continue seek
				seekTag();
				return false;
			}
		}
		// Data is available
		return true;
	}
	// No data available
	return false;
}
Ejemplo n.º 14
0
bool VoxelNodeData::packetIsDuplicate() const {
    if (_lastVoxelPacketLength == getPacketLength()) {
        return memcmp(_lastVoxelPacket, _voxelPacket, getPacketLength()) == 0;
    }
    return false;
}
Ejemplo n.º 15
0
QByteArray getPacketData(const QByteArray& buffer)
{
    return buffer.mid(9,getPacketLength(buffer));
}
Ejemplo n.º 16
0
void readPacket() {
        // reset previous response
        if ((_response_Available == true) || (_response_ERROR > 0)) {
                // discard previous packet and start over
                resetResponse();
        }


    while (uart_available()) {


        b = uart_getc();

        if (_pos > 0 && b == START_BYTE && ATAP == 2) {
                // new packet start before previous packeted completed -- discard previous packet and start over
                _response_ERROR = UNEXPECTED_START_BYTE;
				 uart_putc(_response_ERROR);
                return;
        }

                if (_pos > 0 && b == ESCAPE) {
                        if (uart_available()) {
                                b = uart_getc();
                                b = 0x20 ^ b;
                        } else {
                                // escape byte.  next byte will be
                                _escape = true;
                                continue;
                        }
                }


                if (_escape == true) {
                        b = 0x20 ^ b;
                        _escape = false;
                }


                // checksum includes all bytes starting with api id
                if (_pos >= API_ID_INDEX) {
                        _checksumTotal+= b;
                }


        switch(_pos) {
                        case 0:
                        if (b == START_BYTE) {
                                _pos++;
                        }
                        break;
                  
				        case 1:
                                // length msb
                                _response_MsbLength = b;
                                _pos++;
                                break;
 
                        case 2:
                                // length lsb
                                _response_LsbLength = b;
                                _pos++;
                                break;

                        case 3:
                                _response_ApiId = b;
                                _pos++;
                                break;

                        default:
                                // starts at fifth byte

                                if (_pos > MAX_FRAME_DATA_SIZE) {
                                        // exceed max size.  should never occur
                                        _response_ERROR = PACKET_EXCEEDS_BYTE_ARRAY_LENGTH;
										 uart_putc(_response_ERROR);
                                        return;
                                }

                                // check if we're at the end of the packet
                                // packet length does not include start, length, or checksum bytes, so add 3
                                if (_pos == (getPacketLength() + 3)) {
                                        // verify checksum
                                        

                                        if ((_checksumTotal & 0xff) == 0xff) {
                                                _response_Checksum = b;
                                                _response_Available = true;
												//no check
												//uart_puts_P("OK\n");///////////////////////////
												//DDRA = 0x02;////////////////////////////////////
												//PORTA = 0x02;//////////////////////////

                                                _response_ERROR = NO_ERROR;
                                        } else {
                                                // checksum failed
                                                _response_ERROR = CHECKSUM_FAILURE;
												  uart_putc(_response_ERROR);
	
                                        }

                                        // minus 4 because we start after start,msb,lsb,api and up to but not including checksum
                                        // e.g. if frame was one byte, _pos=4 would be the byte, pos=5 is the checksum, where end stop reading
                                        _response_FrameLength = (_pos - 4);

                                        // reset state vars
                                        _pos = 0;

                                        _checksumTotal = 0;

                                        return;
                                } else {
                                        // add to packet array, starting with the fourth byte of the apiFrame
                                        _response_FrameData[_pos - 4] = b;
                                        _pos++;
                                }
        }
    }
}