2308f27e7874e963e0f385cb4049894035d7b0a5
[src/app-framework-binder.git] / plugins / radio / radio-rtlsdr.h
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Manuel Bachmann"
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
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 "radio-api.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 #define AUDIO_BUFFER "/tmp/audio_buf"
33
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;  /* TODO: 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 unsigned int _radio_dev_count (void);
84 const char* _radio_dev_name (unsigned int);
85
86 unsigned char _radio_on (unsigned int, radioCtxHandleT *);
87 void _radio_off (unsigned int);
88 void _radio_stop (unsigned int);
89 void _radio_play (unsigned int);
90 void _radio_set_mode (unsigned int, Mode);
91 void _radio_set_freq (unsigned int, double);
92 void _radio_set_mute (unsigned int, unsigned char);
93
94 unsigned char _radio_dev_init (struct dev_ctx *, unsigned int);
95 unsigned char _radio_dev_free (struct dev_ctx *);
96 void _radio_apply_params (struct dev_ctx *);
97 void _radio_start_threads (struct dev_ctx *);
98 void _radio_stop_threads (struct dev_ctx *);
99
100 #endif /* RADIO_RTLSDR_H */