void Manager::handle_pair_request(const RXPacket& packet)
{
    const TxType tx_type = packet.get_tx_type();
    const id_t id = packet.get_id();

    log(DEBUG, PSTR("Pair req from %lu"), id);
    if (tx_type==TX && cc_txs.find(id)) {
        // ignore pair request from CC_TX we're already paired with
    } else if (tx_type==TRX && cc_trxs.find(id)) {
        // pair request from CC_TRX we previously attempted to pair with
        // this means our ACK response failed so try again
        pair_with = id;
        pair(packet);
    } else if (auto_pair) {
        // Auto pair mode. Go ahead and pair.
        pair_with = id;
        pair(packet);
    } else if (pair_with == id) {
        // Manual pair mode and pair_with has already been set so pair.
        pair(packet);
    } else {
        // Manual pair mode. Tell user about pair request.
        Serial.print(F("{\"pr\": "));
        packet.print_id_and_type(true);
        Serial.println(F("}"));
    }
}
bool Manager::process_rx_pack_buf_and_find_id(const id_t& target_id)
{
    bool success = false;
    TxType tx_type;
    id_t id;
    RXPacket* packet = NULL; // just using this pointer to make code more readable

    /* Loop through every packet in packet buffer. If it's done then post-process it
     * and then check if it's valid.  If so then handle the different types of
     * packet.  Finally reset the packet and return.
     */
    for (index_t packet_i=0; packet_i<PACKET_BUF_LENGTH; packet_i++) {

        packet = &rfm.rx_packet_buffer.packets[packet_i];
        if (packet->done()) {
            tx_type = packet->get_tx_type();
            if (packet->is_ok()) {
                id = packet->get_id();
                success |= (id == target_id); // Was this the packet we were looking for?

                //******** PAIRING REQUEST **********************
                if (packet->is_pairing_request()) {
                    packet->reset();
                    handle_pair_request(*packet);
                    break;
                }

                //********* CC TX (transmit-only sensor) ********
                switch (tx_type) {
                case TX:
                    bool found;
                    index_t cc_tx_i;
                    found = cc_txs.find(id, cc_tx_i);
                    if (found) { // received ID is a CC_TX id we know about
                        packet->print_id_and_watts(); // send data over serial
                        cc_txs[cc_tx_i].update(*packet);
                        cc_txs.next();
                    } else {
                        log(INFO, PSTR("Rx'd CC_TX packet with unknown ID %lu"), id);
                        if (print_packets >= ALL_VALID) {
                            packet->print_id_and_watts(); // send data over serial
                        }
                    }
                    break;
                case TRX:
                    //****** CC TRX (transceiver; e.g. EDF IAM) ******
                    if (cc_trxs.find(id)) {
                        // Received ID is a CC_TRX id we know about
                        packet->print_id_and_watts(); // send data over serial
                    }
                    //********* UNKNOWN TRX ID *************************
                    else {
                        log(INFO, PSTR("Rx'd CC_TRX packet with unknown ID %lu"), id);
                        if (print_packets >= ALL_VALID) {
                            packet->print_id_and_watts(); // send data over serial
                        }
                    }
                    break;
                }

            } else { // packet is not OK
                log(INFO, PSTR("Rx'd broken %s packet"), tx_type==TX ? "TX" : "TRX");
                if (print_packets == ALL) {
                    packet->print_bytes();
                }
            }
            packet->reset();
        }
    }

    return success;
}