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