aboutsummaryrefslogtreecommitdiffstats
path: root/Dummy_Streaming.cpp
blob: a85986bf2e087a636136252f59de865f245fb9a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2018 Matthias P. Braendli
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "SoapyDummy.hpp"
#include <SoapySDR/Logger.hpp>
#include <SoapySDR/Formats.hpp>
#include <chrono>
#include <thread>
#include <algorithm>

std::vector<std::string> SoapyDummy::getStreamFormats(const int direction, const size_t channel) const
{
    std::vector<std::string> formats;

    formats.push_back(SOAPY_SDR_CF32);

    return formats;
}

std::string SoapyDummy::getNativeStreamFormat(const int direction, const size_t channel, double &fullScale) const
{
    fullScale = 1.0;
    return SOAPY_SDR_CF32;
}

SoapySDR::ArgInfoList SoapyDummy::getStreamArgsInfo(const int direction, const size_t channel) const
{
    SoapySDR::ArgInfoList streamArgs;

    SoapySDR::ArgInfo buffersArg;
    buffersArg.key="buffers";
    buffersArg.value = std::to_string(1);
    buffersArg.name = "Buffer Count";
    buffersArg.description = "Number of buffers per read.";
    buffersArg.units = "buffers";
    buffersArg.type = SoapySDR::ArgInfo::INT;
    streamArgs.push_back(buffersArg);

    return streamArgs;
}

SoapySDR::Stream* SoapyDummy::setupStream(
        const int direction,
        const std::string &format,
        const std::vector<size_t> &channels,
        const SoapySDR::Kwargs &args )
{
    if (channels.size() > 1 or (channels.size() > 0 and channels.at(0) != 0 )) {
        throw std::runtime_error( "setupStream invalid channel selection" );
    }

    if (direction==SOAPY_SDR_RX) {

        if (format == SOAPY_SDR_CF32) {
            SoapySDR_log( SOAPY_SDR_DEBUG, "Using format CF32." );
        }
        else
            throw std::runtime_error( "setupStream invalid format " + format );

        return RX_STREAM;
    }
    else if (direction==SOAPY_SDR_TX) {

        if (format == SOAPY_SDR_CF32) {
            SoapySDR_log( SOAPY_SDR_DEBUG, "Using format CF32." );
        }
        else
            throw std::runtime_error( "setupStream invalid format " + format );

        return TX_STREAM;
    }
    else {
        throw std::runtime_error("Invalid direction");
    }
}

void SoapyDummy::closeStream( SoapySDR::Stream *stream )
{
    if (stream == RX_STREAM) {
    }
    else if (stream == TX_STREAM) {
    }
}


size_t SoapyDummy::getStreamMTU( SoapySDR::Stream *stream ) const
{
    if (stream == RX_STREAM) {
        return m_rxstream.mtu;
    }
    else if (stream == TX_STREAM) {
        return m_txstream.mtu;
    }
    else {
        throw std::runtime_error("Invalid stream");
    }
}

int SoapyDummy::activateStream(
        SoapySDR::Stream *stream,
        const int flags,
        const long long timeNs,
        const size_t numElems )
{
    if (stream == RX_STREAM) {
        SoapySDR_logf(SOAPY_SDR_DEBUG, "Start RX");
        m_rxstream.active = true;
    }
    else if (stream == TX_STREAM) {
        SoapySDR_logf(SOAPY_SDR_DEBUG, "Start TX");
        m_txstream.active = true;
    }

    return 0;
}


int SoapyDummy::deactivateStream(
        SoapySDR::Stream *stream,
        const int flags,
        const long long timeNs )
{
    if (stream == RX_STREAM) {
        m_rxstream.active = false;
    }
    else if (stream == TX_STREAM) {
        m_rxstream.active = false;
    }
    return 0;
}


int SoapyDummy::readStream(
        SoapySDR::Stream *stream,
        void * const *buffs,
        const size_t numElems,
        int &flags,
        long long &timeNs,
        const long timeoutUs )
{
    if (stream != RX_STREAM){
        return SOAPY_SDR_NOT_SUPPORTED;
    }

    if (not m_rxstream.active) {
        using namespace std;
        this_thread::sleep_for(chrono::microseconds(timeoutUs));
        return SOAPY_SDR_TIMEOUT;
    }

    std::complex<float> * const buf = static_cast<std::complex<float> * const>(buffs[0]);

    size_t numToRead = std::min(numElems, this->getStreamMTU(stream));
    const size_t numRead = m_circ_buffer.read(buf, numToRead);
    return numRead;
}

int SoapyDummy::writeStream(
        SoapySDR::Stream *stream,
        const void * const *buffs,
        const size_t numElems,
        int &flags,
        const long long timeNs,
        const long timeoutUs )
{
    if (stream != TX_STREAM){
        return SOAPY_SDR_NOT_SUPPORTED;
    }

    if (not m_txstream.active) {
        using namespace std;
        this_thread::sleep_for(chrono::microseconds(timeoutUs));
        return SOAPY_SDR_TIMEOUT;
    }

    const std::complex<float> * const buf = static_cast<const std::complex<float> * const>(buffs[0]);

    size_t numWritten = std::min(numElems, this->getStreamMTU(stream));
    m_circ_buffer.write(buf, numWritten, timeNs);
    return numWritten;
}


int SoapyDummy::readStreamStatus(
        SoapySDR::Stream *stream,
        size_t &chanMask,
        int &flags,
        long long &timeNs,
        const long timeoutUs
        ){

    if(stream != TX_STREAM){
        return SOAPY_SDR_NOT_SUPPORTED;
    }

    return SOAPY_SDR_TIMEOUT;
}

size_t SoapyDummy::getNumDirectAccessBuffers(
        SoapySDR::Stream *stream)
{
    return 0;
}