void GroundStation::processPendingDatagrams()
{

    while(recvUdpSocket.hasPendingDatagrams())
    {
        QTextStream(stdout) << "Processing started" << endl;
        QByteArray datagram;
        datagram.resize(recvUdpSocket.pendingDatagramSize());
        
        // Read from the udpSocket while there is a datagram and store into datagram.
        recvUdpSocket.readDatagram(datagram.data(), datagram.size());


        //do {
            //datagram.resize(udpSocket.pendingDatagramSize());
            //udpSocket.readDatagram(datagram.data(), datagram.size());
        //} while (udpSocket.hasPendingDatagrams());

        //QDataStream in(&datagram, QIODevice::ReadOnly);
        //in.setVersion(QDataStream::Qt_4_3);
        
        // Validates check sum first and then convert Datagram into proper packet.
        Protocol::Packet* packet = Protocol::Packet::Parse((uint8_t*)datagram.data(), datagram.size());
        Protocol::PacketType packet_type = packet->get_type();

        // Depending on the type call the proper method to extract packet's information and print
        if(packet != nullptr)
        {
            QTextStream(stdout) << "Packet number " << GroundStation::NUM_RECV_PACKETS + 1 << endl;
            switch(packet_type)
            {
                case Protocol::PacketType::Action:
                    print_action_packet(*dynamic_cast<Protocol::ActionPacket*>(packet));
                    break;
                case Protocol::PacketType::Ack:
                    print_ack_packet(*dynamic_cast<Protocol::AckPacket*>(packet));
                    break;
                case Protocol::PacketType::Info:
                    print_info_packet(*dynamic_cast<Protocol::InfoPacket*>(packet));
                    break;
                case Protocol::PacketType::Telem:
                    print_telemetry_packet(*dynamic_cast<Protocol::TelemetryPacket*>(packet));
                    break;
                default:
                    break;
            }

            QTextStream(stdout) << ""<< endl;
            ++GroundStation::NUM_RECV_PACKETS;        }
        else
        {
            QTextStream(stdout) << "ERROR: Packet is invalid" << endl;
        }
    }
}
Example #2
0
void UAV::processPendingDatagrams()
{
    QTextStream(stdout) << "data recieved!" << endl;
    while(recvUdpSocket.hasPendingDatagrams())
    {
        QTextStream(stdout) << "Processing started" << endl;
        QByteArray datagram;
        datagram.resize(recvUdpSocket.pendingDatagramSize());

        // Read from the udpSocket while there is a datagram and store into datagram.
        recvUdpSocket.readDatagram(datagram.data(), datagram.size());

        // Validates check sum first and then convert Datagram into proper packet.
        Protocol::Packet* packet = Protocol::Packet::Parse((uint8_t*)datagram.data(), datagram.size());
        Protocol::PacketType packet_type = packet->get_type();

            

        // Depending on the type call the proper method to extract packet's information and print
        if(packet != nullptr)
        {
            QTextStream(stdout) << "Packet number " << UAV::NUM_RECV_PACKETS + 1 << endl;
            switch(packet_type)
            {
                case Protocol::PacketType::Action:
                    print_action_packet(*dynamic_cast<Protocol::ActionPacket*>(packet));
                    respond_to_action_packet(*dynamic_cast<Protocol::ActionPacket*>(packet));
                    sendAckPacket(packet, "Action Packet");
                    break;
                case Protocol::PacketType::Ack:
                    print_ack_packet(*dynamic_cast<Protocol::AckPacket*>(packet));
                    break;
                case Protocol::PacketType::Info:
                    print_info_packet(*dynamic_cast<Protocol::InfoPacket*>(packet));
                    sendAckPacket(packet, "Info Packet");
                    break;
                case Protocol::PacketType::Telem:
                    print_telemetry_packet(*dynamic_cast<Protocol::TelemetryPacket*>(packet));
                    sendAckPacket(packet, "Telemetry Packet");
                    break;
                default:
                    break;
            }

            QTextStream(stdout) << ""<< endl;
            ++UAV::NUM_RECV_PACKETS;
        }
        else
        {
            QTextStream(stdout) << "ERROR: Packet is invalid" << endl;
        }
    }
}