summaryrefslogtreecommitdiff
path: root/Radio/HW/BladeRF/src/streaming/async.h
blob: b0fd3b8ca15edd5994b1f8ac85fbe233ac9f8ec9 (plain) (blame)
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
/*
 * This file is part of the bladeRF project:
 *   http://www.github.com/nuand/bladeRF
 *
 * Copyright (C) 2014 Nuand LLC
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef STREAMING_ASYNC_H_
#define STREAMING_ASYNC_H_

#include <pthread.h>

#include <libbladeRF.h>

#include "thread.h"

#include "format.h"

typedef enum {
    STREAM_IDLE,          /* Idle and initialized */
    STREAM_RUNNING,       /* Currently running */
    STREAM_SHUTTING_DOWN, /* Currently tearing down.
                           * See bladerf_stream->error_code to determine
                           * whether or not the shutdown was a clean exit
                           * or due to an error. */
    STREAM_DONE           /* Done and deallocated */
} bladerf_stream_state;

struct bladerf_stream {
    /* These items are configured in async_init_stream() and should only be
     * read (NOT MODIFIED) during the execution of the stream */
    struct bladerf *dev;
    bladerf_channel_layout layout;
    bladerf_format format;
    unsigned int transfer_timeout;
    bladerf_stream_cb cb;
    void *user_data;
    size_t samples_per_buffer;
    size_t num_buffers;
    void **buffers;

    MUTEX lock;

    /* The following items must be accessed atomically */
    int error_code;
    bladerf_stream_state state;
    pthread_cond_t can_submit_buffer;
    pthread_cond_t stream_started;
    void *backend_data;
};

/* Get the number of bytes per stream buffer */
static inline size_t async_stream_buf_bytes(struct bladerf_stream *s)
{
    if (s->format == BLADERF_FORMAT_PACKET_META)
       return s->samples_per_buffer;
    return samples_to_bytes(s->format, s->samples_per_buffer);
}

int async_init_stream(struct bladerf_stream **stream,
                      struct bladerf *dev,
                      bladerf_stream_cb callback,
                      void ***buffers,
                      size_t num_buffers,
                      bladerf_format format,
                      size_t buffer_size,
                      size_t num_transfers,
                      void *user_data);

/* Set the transfer timeout. This acquires stream->lock. */
int async_set_transfer_timeout(struct bladerf_stream *stream,
                               unsigned int transfer_timeout_ms);

/* Get the transfer timeout. This acquires stream->lock. */
int async_get_transfer_timeout(struct bladerf_stream *stream,
                               unsigned int *transfer_timeout_ms);

/* Backend code is responsible for acquiring stream->lock in their callbacks */
int async_run_stream(struct bladerf_stream *stream,
                     bladerf_channel_layout layout);


/* This function WILL acquire stream->lock before calling backend code.
 *
 * If nonblock=true and no transfers are available, this function shall return
 * BLADERF_ERR_WOULD_BLOCK.
 */
int async_submit_stream_buffer(struct bladerf_stream *stream,
                               void *buffer,
                               size_t *length,
                               unsigned int timeout_ms,
                               bool nonblock);


void async_deinit_stream(struct bladerf_stream *stream);

#endif