74049a9b294950508499205267d8940518315df6
[src/app-framework-binder.git] / plugins / radio / radio-rtlsdr.h
1 /*
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Manuel Bachmann"
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifndef RADIO_RTLSDR_H
20 #define RADIO_RTLSDR_H
21
22 /* -------------- RADIO RTLSDR DEFINITIONS ------------------ */
23
24 #include <math.h>
25 #include <pthread.h>
26 #include <rtl-sdr.h>
27
28 #include "local-def.h"
29
30 #define pthread_signal(n, m) pthread_mutex_lock(m); pthread_cond_signal(n); pthread_mutex_unlock(m)
31 #define pthread_wait(n, m) pthread_mutex_lock(m); pthread_cond_wait(n, m); pthread_mutex_unlock(m)
32 #define BUF_LEN 16*16384
33
34 typedef enum { FM, AM } Mode;
35 typedef struct dongle_ctx dongle_ctx;
36 typedef struct demod_ctx demod_ctx;
37 typedef struct output_ctx output_ctx;
38 typedef struct dev_ctx dev_ctx_T;
39
40 struct dongle_ctx {
41     pthread_t thr;
42     unsigned char thr_finished;
43     uint16_t buf[BUF_LEN];
44     uint32_t buf_len;
45 };
46
47 struct demod_ctx {
48     pthread_t thr;
49     unsigned char thr_finished;
50     pthread_rwlock_t lck;
51     pthread_cond_t ok;
52     pthread_mutex_t ok_m;
53     int pre_r, pre_j, now_r, now_j, index;
54     int pre_index, now_index;
55     int16_t buf[BUF_LEN];
56     int buf_len;
57     int16_t res[BUF_LEN];
58     int res_len;
59 };
60
61 struct output_ctx {
62     pthread_t thr;
63     unsigned char thr_finished;
64     pthread_rwlock_t lck;
65     pthread_cond_t ok;
66     pthread_mutex_t ok_m;
67     int16_t buf[BUF_LEN];
68     int buf_len;
69 };
70
71 struct dev_ctx {
72     int used;  // radio is free ???
73     rtlsdr_dev_t* dev;
74     Mode mode;
75     float freq;
76     unsigned char mute;
77     unsigned char should_run;
78      /* thread contexts */
79     dongle_ctx *dongle;
80     demod_ctx *demod;
81     output_ctx *output;
82 };
83
84 PUBLIC unsigned int _radio_dev_count (void);
85 PUBLIC const char* _radio_dev_name (unsigned int);
86
87 STATIC void* _dongle_thread_fn (void *);
88 STATIC void* _demod_thread_fn (void *);
89 STATIC void* _output_thread_fn (void *);
90 STATIC unsigned char _radio_dev_init (struct dev_ctx *, unsigned int);
91 STATIC unsigned char _radio_dev_free (struct dev_ctx *);
92 STATIC void _radio_apply_params (struct dev_ctx *);
93 STATIC void _radio_start_threads (struct dev_ctx *);
94 STATIC void _radio_stop_threads (struct dev_ctx *);
95
96 static unsigned int init_dev_count = 0;
97 static struct dev_ctx **dev_ctx = NULL;
98
99 #endif /* RADIO_RTLSDR_H */