Hello

I am trying to capture GSM downlink and uplink signals using NI USRP 2954R and 
uhd-c api then writing it to a file, which is then read by gnuradio to 
demodulate, decode and get the actual data. But using the receiver  code, 
somehow I was able to get only Downlink data but not uplink. I tried the same 
grc file with uhd-source in gnuradio and it was working perfectly for both. 
Below is my code, please check it once, and let me know where I am going wrong.

#include <uhd/types/tune_request.hpp>
#include <uhd/utils/thread_priority.hpp>
#include <uhd/utils/safe_main.hpp>
#include <uhd/usrp/multi_usrp.hpp>
#include <uhd/exception.hpp>
#include <boost/program_options.hpp>
#include <boost/format.hpp>
#include <boost/thread.hpp>
#include <uhd/stream.hpp>
#include <iostream>
#include <fstream>
#include <csignal>
#include <complex>
#include <pthread.h>

#include "../include/conf.hpp"


static bool stop_signal_called = false;

void sig_int_handler(int){
    stop_signal_called = true;
}

typedef boost::function<uhd::sensor_value_t (const std::string&)> 
get_sensor_fn_t;

//template<typename samp_type>
void recv_to_file(struct data2hosts data){
    uhd::usrp::multi_usrp::sptr usrp = data.usrp;
        const std::string file = data.file;
        size_t samps_per_buff = data.spb;
        unsigned long long num_requested_samples = data.total_num_samps;
        bool null = NULL;
        bool enable_size_map = data.enable_size_map;
        bool continue_on_bad_packet = data.continue_on_bad_packet;

    unsigned long long num_total_samps = 0;
    //create a receive streamer
    uhd::stream_args_t stream_args("fc32","sc16");//cpu_format,wire_format);
    stream_args.channels.push_back(data.chan_no);
    uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args);
    uhd::rx_metadata_t md;
    std::vector<std::complex<float>> buff(samps_per_buff);///////
    std::ofstream outfile;
    if (not null)
        outfile.open(file.c_str(), std::ofstream::binary);
    bool overflow_message = true;

    //setup streaming
    uhd::stream_cmd_t stream_cmd((num_requested_samples == 0)?
        uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS:
        uhd::stream_cmd_t::STREAM_MODE_NUM_SAMPS_AND_DONE
    );
    stream_cmd.num_samps = size_t(num_requested_samples);
    stream_cmd.stream_now = true;
    stream_cmd.time_spec = uhd::time_spec_t();
    rx_stream->issue_stream_cmd(stream_cmd);

    boost::system_time start = boost::get_system_time();
    //unsigned long long ticks_requested = (long)(time_requested * 
(double)boost::posix_time::time_duration::ticks_per_second());
    boost::posix_time::time_duration ticks_diff;
    boost::system_time last_update = start;
    unsigned long long last_update_samps = 0;

    typedef std::map<size_t,size_t> SizeMap;
    SizeMap mapSizes;

    while(not stop_signal_called and (num_requested_samples != num_total_samps 
or num_requested_samples == 0)) {
        boost::system_time now = boost::get_system_time();

        size_t num_rx_samps = rx_stream->recv(&buff.front(), buff.size(), md, 
3.0, enable_size_map);
        //std::cout << boost::format("No of bytes received %u")% num_rx_samps 
<< std::endl;
        if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) {
            std::cout << boost::format("Timeout while streaming") << std::endl;
            break;
        }
        if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_OVERFLOW){
            if (overflow_message) {
                overflow_message = false;
                std::cerr << boost::format(
                    "Got an overflow indication. Please consider the 
following:\n"
                    "  Your write medium must sustain a rate of %fMB/s.\n"
                    "  Dropped samples will not be written to the file.\n"
                    "  Please modify this example for your purposes.\n"
                    "  This message will not appear again.\n"
                ) % 
(usrp->get_rx_rate()*sizeof(std::complex<float>)/1e6);////////////////
            }
            continue;
        }
        if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE){
            std::string error = str(boost::format("Receiver error: %s") % 
md.strerror());
            if (continue_on_bad_packet){
                std::cerr << error << std::endl;
                continue;
            }
            else
                throw std::runtime_error(error);
        }

        if (enable_size_map) {
            SizeMap::iterator it = mapSizes.find(num_rx_samps);
            if (it == mapSizes.end())
                mapSizes[num_rx_samps] = 0;
            mapSizes[num_rx_samps] += 1;
        }

        num_total_samps += num_rx_samps;

        if (outfile.is_open())
            outfile.write((const char*)&buff.front(), 
num_rx_samps*sizeof(std::complex<float>));//////////////

        ticks_diff = now - start;
        /*if (ticks_requested > 0){
            if ((unsigned long long)ticks_diff.ticks() > ticks_requested)
                break;
        }*/
    }

    stream_cmd.stream_mode = uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS;
    rx_stream->issue_stream_cmd(stream_cmd);

    if (outfile.is_open())
     outfile.close();
}

bool check_locked_sensor(std::vector<std::string> sensor_names, const char* 
sensor_name, get_sensor_fn_t get_sensor_fn, double setup_time){
    if (std::find(sensor_names.begin(), sensor_names.end(), sensor_name) == 
sensor_names.end())
        return false;

    boost::system_time start = boost::get_system_time();
    boost::system_time first_lock_time;

    std::cout << boost::format("Waiting for \"%s\": ") % sensor_name;
    std::cout.flush();

    while (true) {
        if ((not first_lock_time.is_not_a_date_time()) and
                (boost::get_system_time() > (first_lock_time + 
boost::posix_time::seconds(setup_time))))
        {
            std::cout << " locked." << std::endl;
            break;
        }
        if (get_sensor_fn(sensor_name).to_bool()){
            if (first_lock_time.is_not_a_date_time())
                first_lock_time = boost::get_system_time();
            std::cout << "+";
            std::cout.flush();
        }
        else {
            first_lock_time = boost::system_time();    //reset to 'not a date 
time'

            if (boost::get_system_time() > (start + 
boost::posix_time::seconds(setup_time))){
                std::cout << std::endl;
                throw std::runtime_error(str(boost::format("timed out waiting 
for consecutive locks on sensor \"%s\"") % sensor_name));
            }
            std::cout << "_";
            std::cout.flush();
        }
        boost::this_thread::sleep(boost::posix_time::milliseconds(100));
    }
    std::cout << std::endl;
    return true;
}

int UHD_SAFE_MAIN(int argc, char *argv[]){
    uhd::set_thread_priority_safe();
    int i=0;
    std::string args, type ;
    struct rfSettings rfParam[3];
    struct data2hosts data[2];
    double rate, freq, gain, bw, setupTime, totalTime = 0;;
    pthread_t tid[2]={0x00};
    std::string ant("RX2");
    std::string file1("/media/nameme/usrp_data.dat");
    std::string file2("/media/nameme/usrp_data1.dat");
    std::string wirefmt("sc16");
    bool enable_size_map = true;
    bool continue_on_bad_packet = true;
    size_t total_num_samps = 0, spb = 1000000;
    pthread_t tid1,tid2;
    memset(rfParam,0x00,sizeof(rfParam));

    /*****************************Koyel to check***************************/

    rfParam[0].freq = 935800000;
    strcpy(rfParam[0].band,"P-GSM");
    rfParam[0].bandwidth = 25000000;
    rfParam[0].samplerate = 50000000;
    rfParam[0].total_time = 0x00;
    rfParam[0].setup_time = 1.0;
    rfParam[0].gain = 30.0;

    rfParam[1].freq = 925000000;
    strcpy(rfParam[1].band,"E-GSM");
    rfParam[1].bandwidth = 35000000;
    rfParam[1].samplerate = 70000000;
    rfParam[1].setup_time = 1.0;
    rfParam[1].gain = 30.0;

   /* rfParam[2].freq = 1805200000;
    strcpy(rfParam[2].band,"DCS");
    rfParam[2].bandwidth =  100000000; //75000000;
    rfParam[2].samplerate = 200000000;//200000000; //149200000;
    rfParam[2].setup_time = 1.0;
    rfParam[2].gain = 30.0;*/
    rfParam[2].freq = 959600000;//935800000;
    strcpy(rfParam[2].band,"DCS");
    rfParam[2].bandwidth =  250000; //75000000;
    rfParam[2].samplerate = 1000000;//200000000; //149200000;
    rfParam[2].setup_time = 1.0;
    rfParam[2].gain = 30.0;


    /*************************************************************************/

    std::string subdev("A:0 B:0");
    std::string sync("internal");
    for(i=0;i<3;i++){
        if(strcmp(rfParam[i].band,"DCS")==0x00){
            rate = rfParam[i].samplerate;
            freq = rfParam[i].freq;
            gain = rfParam[i].gain;
            bw = rfParam[i].bandwidth;
            setupTime = rfParam[i].setup_time;
            totalTime = rfParam[i].total_time;
            break;
        }
    }
    //create a usrp device
    std::cout << boost::format("\nCreating the USRP device with args: 
\"%s\"...\n") % args;
    static uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(args);

    //set subdevice
    usrp->set_rx_subdev_spec(subdev);

    //set antenna
    std::cout << boost::format("Setting antenna to:     %s\n") % ant;
    usrp->set_rx_antenna(ant);
    usrp->set_rx_antenna(ant);

    //tune to a particular frequency
    std::cout << boost::format("Setting RX Freq: %f MHz...") % (freq/1e6) << 
std::endl;
    uhd::tune_request_t tune_request(freq,0);
    tune_request.args = uhd::device_addr_t("mode_n=integer");
    usrp->set_rx_freq(tune_request,0);
    uhd::tune_request_t tune_requestUplink(freq-45000000);
    usrp->set_rx_freq(tune_requestUplink,1);
    std::cout << boost::format("Actual RX Freq: %f MHz...") % 
(usrp->get_rx_freq(0)/1e6) << std::endl << std::endl;
    std::cout << boost::format("Actual RX Freq: %f MHz...") % 
(usrp->get_rx_freq(1)/1e6) << std::endl << std::endl;

    //set sample rate
    std::cout << boost::format("Setting sample rate to: %d\n") % rate;
    usrp->set_rx_rate(rate);
    usrp->set_rx_rate(rate);
    std::cout << boost::format("Actual sample rate:     %d \n") % 
usrp->get_rx_rate(0);
    std::cout << boost::format("Actual sample rate:     %d \n") % 
usrp->get_rx_rate(1);

    //set gain
    std::cout << boost::format("Setting gain to: %d\n") % gain;
    usrp->set_rx_gain(gain,0);
    usrp->set_rx_gain(gain,1);
    std::cout << boost::format("Actual gain:     %d\n") % usrp->get_rx_gain(0);
    std::cout << boost::format("Actual gain:     %d\n") % usrp->get_rx_gain(1);

    //set the bandwidth
    std::cout << boost::format("Setting bandwidth to %f MHz")%(bw/1e6) << 
std::endl;
    usrp->set_rx_bandwidth(bw,0);
    usrp->set_rx_bandwidth(bw,1);
    std::cout << boost::format("Actual RX Bandwidth: %f MHz...") % 
(usrp->get_rx_bandwidth(0)/1e6) << std::endl << std::endl;
    std::cout << boost::format("Actual RX Bandwidth: %f MHz...") % 
(usrp->get_rx_bandwidth(1)/1e6) << std::endl << std::endl;


    
boost::this_thread::sleep(boost::posix_time::seconds(rfParam[0].setup_time)); 
//allow for some setup time
    check_locked_sensor(usrp->get_rx_sensor_names(0), "lo_locked", 
boost::bind(&uhd::usrp::multi_usrp::get_rx_sensor, usrp, _1, 0), 
rfParam[0].setup_time);
    // Get an rx_streamer from the device
    //check_locked_sensor(usrp->get_rx_sensor_names(0), "lo_locked", 
boost::bind(&uhd::usrp::multi_usrp::get_rx_sensor, usrp, _1, 0), 
rfParam[0].setup_time);
    std::signal(SIGINT, &sig_int_handler);
    std::cout << "Press Ctrl + C to stop streaming..." << std::endl;
    data[0].usrp = usrp;
    data[0].file = file1;
    data[0].total_num_samps = total_num_samps;
    data[0].spb = spb;
    data[0].enable_size_map = enable_size_map;
    data[0].continue_on_bad_packet = continue_on_bad_packet;
    data[0].chan_no = 0;

    data[1].usrp = usrp;
    data[1].file = file2;
    data[1].total_num_samps = total_num_samps;
    data[1].spb = spb;
    data[1].enable_size_map = enable_size_map;
    data[1].continue_on_bad_packet = continue_on_bad_packet;
    data[1].chan_no = 1;

/*#define recv_to_file_args(format) \
    (usrp, file1, spb, total_num_samps, NULL, enable_size_map, 
continue_on_bad_packet)
    recv_to_file<std::complex<float> >recv_to_file_args("fc32");*/
    boost::thread thread2(&recv_to_file,data[1]);
    boost::thread thread1(&recv_to_file,data[0]);
    thread2.join();
    thread1.join();
    //finished
     std::cout << std::endl << "Done!" << std::endl << std::endl;
}

BR
Snehasish


_______________________________________________
USRP-users mailing list
USRP-users@lists.ettus.com
http://lists.ettus.com/mailman/listinfo/usrp-users_lists.ettus.com

Reply via email to