Пример #1
0
bool DDAProtocol :: exec()
{
  int errorCount = ERROR_COUNT;
  while(!m_terminated)
  {
    if(rxPacket())
    {
      errorCount = ERROR_COUNT;
      unsigned txSize = m_txData.size();
      if(!txSize)
      {
        //Send ACK
        unsigned char tx = ACK;
        serial_write(m_serial, &tx, 1);
      }
      else
      {
        txPacket();
        waitAck();
      }
      handleData(m_rxData);
      m_rxData.clear();
    }
    else
    {
      if(--errorCount <= 0)
        return false;
    }
  }
  return true;
}
Пример #2
0
static void serialReceive(uint8_t byte) {
     // serialSendByte(AMB8420_UART_ID ^ 1, '0' + (byte >> 4) );
     // serialSendByte(AMB8420_UART_ID ^ 1, '0' + (byte & 0xf) );
     // serialSendByte(AMB8420_UART_ID ^ 1, '\n');
    // greenLedToggle();

    switch (recvState) {
    case STATE_READ_DELIMITER:
//        PRINT("delim\n");
        if (byte == AMB8420_START_DELIMITER) {
            recvState = STATE_READ_COMMAND;
        }
        break;

    case STATE_READ_COMMAND:
        recvCommand = byte;
//        PRINTF("command = %d\n", command);
//        if ((recvCommand & 0xC0) == 0) {
        // if (recvCommand == 0 || recvCommand == 1) {
        //     // not a reply command; something wrong!
        //     // PRINTF("not a reply command: cmd=0x%02x\n", recvCommand);
        //     // isRadioRxError = true;
        //     PRINTF("!\n");
        //     recvState = STATE_READ_DELIMITER;
        // } else {
            recvState = STATE_READ_LENGTH;
//        }
        break;

    case STATE_READ_LENGTH:
        recvLength = byte;
        // PRINTF("length = %d\n", recvLength);
        if (recvLength) {
//            PRINT("len -> data\n");
            recvState = STATE_READ_DATA;
            rxCursor = 0;
            if (recvLength > sizeof(rxBuffer)) {
                recvLength = sizeof(rxBuffer);
            }
        } else {
//            PRINT("len -> cs\n");
            recvState = STATE_READ_CS;
        }
        break;

    case STATE_READ_DATA:
        rxBuffer[rxCursor++] = byte;
//        PRINTF("rxCursor = %d\n", rxCursor);
        if (rxCursor == recvLength || rxCursor == sizeof(rxBuffer)) {
//            PRINT("data -> cs\n");
            recvState = STATE_READ_CS;
        }
        break;

    case STATE_READ_CS:
        rxPacket(byte);
        break;
    }
}
Пример #3
0
void libHubsan::bind()
{
	Serial.println("Sending beacon packets...");
	byte status_byte = 0x00; // variable to hold W/R register data.

	// Generate 4 byte random session id.
	randomSeed(analogRead(0));
	for (int i=0;i<4;i++){
		_sessionid[i] = random(255);
	}

	for (int i=0;i<16;i++){ // Initialize packet array.
		_txpacket[i] = 0x00;
	}
	_txpacket[0] = 0x01; // Bind level = 01 (Unbound - BEACON lvl 1 Packet)
	_txpacket[1] = _channel; // Selected Channel
	for (int i=0;i<4;i++){
		_txpacket[i+2] = _sessionid[i];
	}
	getChecksum(_txpacket);

	// Transmit ANNOUNCE Packet until a response is heard.
	while (true){
		txPacket(_txpacket);
		a7105.sendStrobe(A7105_RX); // Switch to RX mode.
		bool response = false;
		for (int i=0;i<15;i++){ // Listen to see if there was a response.
			a7105.readRegister(A7105_00_MODE,status_byte);
			if (CHECK_BIT(status_byte,0)==false){
				response = true;
				break;
			}
			delay(1);
		}
		if (response){
			break;
		}
		a7105.sendStrobe(A7105_STANDBY);
	}
	rxPacket(_rxpacket);

	// Escalate handshake.
	_txpacket[0] = 0x03; // Bind Level = 01 (Unbound - BEACON lvl 3 Packet)
	getChecksum(_txpacket);
	while (true){
		txPacket(_txpacket);
		a7105.sendStrobe(A7105_RX); // Switch to RX mode.
		bool response = false;
		for (int i=0;i<15;i++){ // Listen to see if there was a response.
			a7105.readRegister(A7105_00_MODE,status_byte);
			if (CHECK_BIT(status_byte,0)==false){
				response = true;
				break;
			}
			delay(1);
		}
		if (response){
			break;
		}
		a7105.sendStrobe(A7105_STANDBY);
	}
	rxPacket(_rxpacket);

	// Set IDCode to the session value.
	a7105.writeRegister(A7105_06_ID_DATA,4,_sessionid);

	// Commence confirmation handshake.
	_txpacket[0] = 0x01; // Bind Level = 01 (Mid-Bind - Confirmation of IDCODE change packet)
	getChecksum(_txpacket);
	while (true){
		txPacket(_txpacket);
		a7105.sendStrobe(A7105_RX); // Switch to RX mode.
		bool response = false;
		for (int i=0;i<15;i++){ // Listen to see if there was a response.
			a7105.readRegister(A7105_00_MODE,status_byte);
			if (CHECK_BIT(status_byte,0)==false){
				response = true;
				break;
			}
			delay(1);
		}
		if (response){
			break;
		}
		a7105.sendStrobe(A7105_STANDBY);
	}
	rxPacket(_rxpacket);

	// Commence full handshake escalation.
	_txpacket[0] = 0x09;
	for (int i=0;i<10;i++){
		_txpacket[2] = byte(i);
		getChecksum(_txpacket);
		while (true){
			txPacket(_txpacket);
			a7105.sendStrobe(A7105_RX); // Switch to RX mode.
			bool response = false;
			for (int i=0;i<15;i++){ // Listen to see if there was a response.
				a7105.readRegister(A7105_00_MODE,status_byte);
				if (CHECK_BIT(status_byte,0)==false){
					response = true;
					break;
				}
				delay(1);
			}
			if (response){
				break;
			}
			a7105.sendStrobe(A7105_STANDBY);
		}
		rxPacket(_rxpacket);
	}
	a7105.writeRegister(A7105_1F_CODE_I,0x0F); // Enable FEC.
	a7105.sendStrobe(A7105_STANDBY);
}