Пример #1
0
void MeshBase::Update()
{
	// Periodic sends
	if (millis() - last_broadcast_time > PEER_DISCOVERY_TIME)
	{
    		if (!IsReady())
			ChooseAddress();
		SendPeerDiscovery();
	}

	// Recieve
	uint8_t pipe_num;
	if (radio.available(&pipe_num))
	{
		bool done = false;
		do {
			uint8_t len = radio.getDynamicPayloadSize();
			uint8_t buff[40];
			done = radio.read(buff, min(len, sizeof(buff)));
			if (pipe_num == 0)
			{
				HandleMessage(0, buff, len);
			} else if (pipe_num == 1) {
				HandlePeerDiscovery(buff, len);
			}
		} while (!done);
	}
	
	// Update peers
	if (millis() - last_peer_check_time > PEER_CHECK_TIME)
	{
		LinkedList<Peer>::Node* current = peers.first;
		while(current != NULL)
		{
			current->item->time += 1;
			if (current->item->time >= PEER_TIMEOUT)
			{
				Serial.print("Lost Peer: ");
				Serial.println(current->item->address, DEC);
				current = peers.Remove(current);
			} else {
				current = current->next;
			}
		}
		last_peer_check_time = millis();
	}
}
Пример #2
0
void MeshBase::HandlePacket(const byte* data, uint8_t len)
{
	if (len < sizeof(Message))
		return;
	const MeshBase::Message* msg = (struct MeshBase::Message*)data;
	uint8_t payload_length = len - sizeof(Message);
	const byte* payload = data + sizeof(Message);
	if (msg->split_more || msg->split_part != 0)
	{
		// Re-assembly needed
		// TODO: Re-assemble packets
	} else {
		switch(msg->type) {
			case type_peer_discovery:
				HandlePeerDiscovery(msg, payload, payload_length);
			break;
			default:
				OnMessage(msg, payload, payload_length);
			break;
		}
		delete data;
	}
}