2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Manuel Bachmann"
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.
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.
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/>.
19 #include "radio-api.h"
20 #include "radio-rtlsdr.h"
22 /* ------------- RADIO RTLSDR IMPLEMENTATION ---------------- */
24 /* --- PUBLIC FUNCTIONS --- */
26 /* Radio initialization should be done only when user start the radio and not at plugin initialization
27 Making this call too early would impose to restart the binder to detect a radio */
28 PUBLIC unsigned char _radio_on (unsigned int num, radioCtxHandleT *ctx) {
30 if (num >= _radio_dev_count())
33 if (init_dev_count < _radio_dev_count()) {
34 init_dev_count = _radio_dev_count();
35 dev_ctx = (dev_ctx_T**) realloc (dev_ctx, init_dev_count * sizeof(dev_ctx_T));
38 dev_ctx[num] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T));
39 dev_ctx[num]->dev = NULL;
40 dev_ctx[num]->mode = ctx->mode;
41 dev_ctx[num]->freq = ctx->freq;
42 dev_ctx[num]->mute = ctx->mute;
43 dev_ctx[num]->should_run = 0;
44 dev_ctx[num]->dongle = NULL;
45 dev_ctx[num]->demod = NULL;
46 dev_ctx[num]->output = NULL;
47 _radio_dev_init(dev_ctx[num], num);
52 PUBLIC void _radio_off (unsigned int num) {
54 if (num >= _radio_dev_count())
58 _radio_dev_free(dev_ctx[num]);
65 PUBLIC void _radio_set_mode (unsigned int num, Mode mode) {
66 if (!dev_ctx || !dev_ctx[num])
69 dev_ctx[num]->mode = mode;
70 _radio_apply_params(dev_ctx[num]);
73 PUBLIC void _radio_set_freq (unsigned int num, double freq) {
74 if (!dev_ctx || !dev_ctx[num])
77 dev_ctx[num]->freq = (float)freq;
78 _radio_apply_params(dev_ctx[num]);
81 PUBLIC void _radio_set_mute (unsigned int num, unsigned char mute) {
82 if (!dev_ctx || !dev_ctx[num])
85 dev_ctx[num]->mute = mute;
86 _radio_apply_params(dev_ctx[num]);
89 PUBLIC void _radio_play (unsigned int num) {
90 if (!dev_ctx || !dev_ctx[num])
93 _radio_start_threads(dev_ctx[num]);
96 PUBLIC void _radio_stop (unsigned int num) {
97 if (!dev_ctx || !dev_ctx[num])
100 _radio_stop_threads(dev_ctx[num]);
103 PUBLIC unsigned int _radio_dev_count () {
104 return rtlsdr_get_device_count();
107 PUBLIC const char* _radio_dev_name (unsigned int num) {
108 return rtlsdr_get_device_name(num);
112 /* --- LOCAL HELPER FUNCTIONS --- */
114 STATIC unsigned char _radio_dev_init (dev_ctx_T *dev_ctx, unsigned int num) {
115 rtlsdr_dev_t *dev = dev_ctx->dev;
117 if (rtlsdr_open(&dev, num) < 0)
120 rtlsdr_set_tuner_gain_mode(dev, 0);
122 if (rtlsdr_reset_buffer(dev) < 0)
127 _radio_apply_params(dev_ctx);
132 STATIC unsigned char _radio_dev_free (dev_ctx_T *dev_ctx) {
133 rtlsdr_dev_t *dev = dev_ctx->dev;
135 if (rtlsdr_close(dev) < 0)
144 STATIC void _radio_apply_params (dev_ctx_T *dev_ctx) {
145 rtlsdr_dev_t *dev = dev_ctx->dev;
146 Mode mode = dev_ctx->mode;
147 float freq = dev_ctx->freq;
151 rate = ((1000000 / 200000) + 1) * 200000;
157 rtlsdr_set_center_freq(dev, freq);
158 rtlsdr_set_sample_rate(dev, rate);
163 STATIC void _radio_start_threads (dev_ctx_T *dev_ctx) {
164 rtlsdr_dev_t *dev = dev_ctx->dev;
165 dev_ctx->dongle = (dongle_ctx*) malloc(sizeof(dongle_ctx));
166 dev_ctx->demod = (demod_ctx*) malloc(sizeof(demod_ctx));
167 dev_ctx->output = (output_ctx*) malloc(sizeof(output_ctx));
169 dongle_ctx *dongle = dev_ctx->dongle;
170 demod_ctx *demod = dev_ctx->demod;
171 output_ctx *output = dev_ctx->output;
173 pthread_rwlock_init(&demod->lck, NULL);
174 pthread_cond_init(&demod->ok, NULL);
175 pthread_mutex_init(&demod->ok_m, NULL);
176 pthread_rwlock_init(&output->lck, NULL);
177 pthread_cond_init(&output->ok, NULL);
178 pthread_mutex_init(&output->ok_m, NULL);
180 dev_ctx->should_run = 1;
183 dongle->thr_finished = 0;
184 pthread_create(&dongle->thr, NULL, _dongle_thread_fn, (void*)dev_ctx);
187 demod->pre_r = demod->pre_j = 0;
188 demod->now_r = demod->now_j = 0;
189 demod->index = demod->pre_index = demod->now_index = 0;
190 demod->thr_finished = 0;
191 pthread_create(&demod->thr, NULL, _demod_thread_fn, (void*)dev_ctx);
194 output->thr_finished = 0;
195 pthread_create(&output->thr, NULL, _output_thread_fn, (void*)dev_ctx);
198 STATIC void _radio_stop_threads (dev_ctx_T *dev_ctx) {
199 rtlsdr_dev_t *dev = dev_ctx->dev;
200 dongle_ctx *dongle = dev_ctx->dongle;
201 demod_ctx *demod = dev_ctx->demod;
202 output_ctx *output = dev_ctx->output;
204 if (!dongle || !demod || !output)
207 /* stop each "while" loop in threads */
208 dev_ctx->should_run = 0;
210 rtlsdr_cancel_async(dev);
211 pthread_signal(&demod->ok, &demod->ok_m);
212 pthread_signal(&output->ok, &output->ok_m);
214 while (!dongle->thr_finished ||
215 !demod->thr_finished ||
216 !output->thr_finished)
219 pthread_join(dongle->thr, NULL);
220 pthread_join(demod->thr, NULL);
221 pthread_join(output->thr, NULL);
222 pthread_rwlock_destroy(&demod->lck);
223 pthread_cond_destroy(&demod->ok);
224 pthread_mutex_destroy(&demod->ok_m);
225 pthread_rwlock_destroy(&output->lck);
226 pthread_cond_destroy(&output->ok);
227 pthread_mutex_destroy(&output->ok_m);
229 free(dongle); dev_ctx->dongle = NULL;
230 free(demod); dev_ctx->demod = NULL;
231 free(output); dev_ctx->output = NULL;
234 /* ---- LOCAL THREADED FUNCTIONS ---- */
236 STATIC void _rtlsdr_callback (unsigned char *buf, uint32_t len, void *ctx) {
237 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
238 dongle_ctx *dongle = dev_ctx->dongle;
239 demod_ctx *demod = dev_ctx->demod;
243 if (!dev_ctx->should_run)
247 for (i = 0; i < (int)len; i += 8) {
248 tmp = 255 - buf[i+3];
252 buf[i+4] = 255 - buf[i+4];
253 buf[i+5] = 255 - buf[i+5];
255 tmp = 255 - buf[i+6];
261 for (i = 0; i < (int)len; i++)
262 dongle->buf[i] = (int16_t)buf[i] - 127;
264 /* lock demod thread, write to it, unlock */
265 pthread_rwlock_wrlock(&demod->lck);
266 memcpy(demod->buf, dongle->buf, 2 * len);
267 demod->buf_len = len;
268 pthread_rwlock_unlock(&demod->lck);
269 pthread_signal(&demod->ok, &demod->ok_m);
272 STATIC void* _dongle_thread_fn (void *ctx) {
273 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
274 dongle_ctx *dongle = dev_ctx->dongle;
276 rtlsdr_read_async(dev_ctx->dev, _rtlsdr_callback, dev_ctx, 0, 0);
278 dongle->thr_finished = 1;
282 STATIC void _lowpass_demod (void *ctx) {
283 demod_ctx *demod = (demod_ctx *)ctx;
286 while (i < demod->buf_len) {
287 demod->now_r += demod->buf[i];
288 demod->now_j += demod->buf[i+1];
291 if (demod->index < ((1000000 / 200000) + 1))
293 demod->buf[i2] = demod->now_r;
294 demod->buf[i2+1] = demod->now_j;
296 demod->now_r = demod->now_j = 0;
302 STATIC void _lowpassreal_demod (void *ctx) {
303 demod_ctx *demod = (demod_ctx *)ctx;
308 while (i < demod->res_len) {
309 demod->now_index += demod->res[i];
311 demod->pre_index += slow;
312 if (demod->pre_index < fast)
314 demod->res[i2] = (int16_t)(demod->now_index / (fast/slow));
315 demod->pre_index -= fast;
316 demod->now_index = 0;
322 STATIC void _multiply (int ar, int aj, int br, int bj, int *cr, int *cj) {
327 STATIC int _polar_discriminant (int ar, int aj, int br, int bj) {
330 _multiply(ar, aj, br, -bj, &cr, &cj);
331 angle = atan2((double)cj, (double)cr);
332 return (int)(angle / 3.14159 * (1<<14));
335 STATIC void _fm_demod (void *ctx) {
336 demod_ctx *demod = (demod_ctx *)ctx;
337 int16_t *buf = demod->buf;
338 int buf_len = demod->buf_len;
341 pcm = _polar_discriminant(buf[0], buf[1], demod->pre_r, demod->pre_j);
342 demod->res[0] = (int16_t)pcm;
344 for (i = 2; i < (buf_len-1); i += 2) {
345 pcm = _polar_discriminant(buf[i], buf[i+1], buf[i-2], buf[i-1]);
346 demod->res[i/2] = (int16_t)pcm;
348 demod->pre_r = buf[buf_len - 2];
349 demod->pre_j = buf[buf_len - 1];
350 demod->res_len = buf_len/2;
353 STATIC void _am_demod (void *ctx) {
354 demod_ctx *demod = (demod_ctx *)ctx;
355 int16_t *buf = demod->buf;
356 int buf_len = demod->buf_len;
359 for (i = 0; i < buf_len; i += 2) {
360 pcm = buf[i] * buf[i];
361 pcm += buf[i+1] * buf[i+1];
362 demod->res[i/2] = (int16_t)sqrt(pcm);
364 demod->res_len = buf_len/2;
367 STATIC void* _demod_thread_fn (void *ctx) {
368 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
369 demod_ctx *demod = dev_ctx->demod;
370 output_ctx *output = dev_ctx->output;
372 while(dev_ctx->should_run) {
373 pthread_wait(&demod->ok, &demod->ok_m);
374 pthread_rwlock_wrlock(&demod->lck);
375 _lowpass_demod(demod);
376 if (dev_ctx->mode == FM)
380 _lowpassreal_demod(demod);
381 pthread_rwlock_unlock(&demod->lck);
383 /* lock demod thread, write to it, unlock */
384 pthread_rwlock_wrlock(&output->lck);
385 memcpy(output->buf, demod->res, 2 * demod->res_len);
386 output->buf_len = demod->res_len;
387 pthread_rwlock_unlock(&output->lck);
388 pthread_signal(&output->ok, &output->ok_m);
391 demod->thr_finished = 1;
395 STATIC void* _output_thread_fn (void *ctx) {
396 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
397 output_ctx *output = dev_ctx->output;
400 file = fopen (AUDIO_BUFFER, "wb");
402 while (dev_ctx->should_run) {
403 pthread_wait(&output->ok, &output->ok_m);
404 pthread_rwlock_rdlock(&output->lck);
405 if (!dev_ctx->mute && file) {
406 fwrite (output->buf, 2, output->buf_len, file);
408 fseek (file, 0, SEEK_SET);
410 pthread_rwlock_unlock(&output->lck);
412 if (file) fclose(file);
413 unlink (AUDIO_BUFFER);
415 output->thr_finished = 1;