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"
21 /* ------------- RADIO RTLSDR IMPLEMENTATION ---------------- */
23 /* --- PUBLIC FUNCTIONS --- */
25 /* Radio initialization should be done only when user start the radio and not at plugin initialization
26 Making this call too early would impose to restart the binder to detect a radio */
27 PUBLIC unsigned char _radio_on (unsigned int num, radioCtxHandleT *ctx) {
29 if (num >= _radio_dev_count())
32 if (init_dev_count < _radio_dev_count()) {
33 init_dev_count = _radio_dev_count();
34 dev_ctx = (dev_ctx_T**) realloc (dev_ctx, init_dev_count * sizeof(dev_ctx_T));
37 dev_ctx[num] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T));
38 dev_ctx[num]->dev = NULL;
39 dev_ctx[num]->mode = ctx->mode;
40 dev_ctx[num]->freq = ctx->freq;
41 dev_ctx[num]->mute = ctx->mute;
42 dev_ctx[num]->should_run = 0;
43 dev_ctx[num]->dongle = NULL;
44 dev_ctx[num]->demod = NULL;
45 dev_ctx[num]->output = NULL;
46 _radio_dev_init(dev_ctx[num], num);
51 PUBLIC void _radio_off (unsigned int num) {
53 if (num >= _radio_dev_count())
57 _radio_dev_free(dev_ctx[num]);
64 PUBLIC void _radio_set_mode (unsigned int num, Mode mode) {
65 if (!dev_ctx || !dev_ctx[num])
68 dev_ctx[num]->mode = mode;
69 _radio_apply_params(dev_ctx[num]);
72 PUBLIC void _radio_set_freq (unsigned int num, double freq) {
73 if (!dev_ctx || !dev_ctx[num])
76 dev_ctx[num]->freq = (float)freq;
77 _radio_apply_params(dev_ctx[num]);
80 PUBLIC void _radio_set_mute (unsigned int num, unsigned char mute) {
81 if (!dev_ctx || !dev_ctx[num])
84 dev_ctx[num]->mute = mute;
85 _radio_apply_params(dev_ctx[num]);
88 PUBLIC void _radio_play (unsigned int num) {
89 if (!dev_ctx || !dev_ctx[num])
92 _radio_start_threads(dev_ctx[num]);
95 PUBLIC void _radio_stop (unsigned int num) {
96 if (!dev_ctx || !dev_ctx[num])
99 _radio_stop_threads(dev_ctx[num]);
102 PUBLIC unsigned int _radio_dev_count () {
103 return rtlsdr_get_device_count();
106 PUBLIC const char* _radio_dev_name (unsigned int num) {
107 return rtlsdr_get_device_name(num);
111 /* --- LOCAL HELPER FUNCTIONS --- */
113 STATIC unsigned char _radio_dev_init (dev_ctx_T *dev_ctx, unsigned int num) {
114 rtlsdr_dev_t *dev = dev_ctx->dev;
116 if (rtlsdr_open(&dev, num) < 0)
119 rtlsdr_set_tuner_gain_mode(dev, 0);
121 if (rtlsdr_reset_buffer(dev) < 0)
126 _radio_apply_params(dev_ctx);
131 STATIC unsigned char _radio_dev_free (dev_ctx_T *dev_ctx) {
132 rtlsdr_dev_t *dev = dev_ctx->dev;
134 if (rtlsdr_close(dev) < 0)
143 STATIC void _radio_apply_params (dev_ctx_T *dev_ctx) {
144 rtlsdr_dev_t *dev = dev_ctx->dev;
145 Mode mode = dev_ctx->mode;
146 float freq = dev_ctx->freq;
150 rate = ((1000000 / 200000) + 1) * 200000;
156 rtlsdr_set_center_freq(dev, freq);
157 rtlsdr_set_sample_rate(dev, rate);
162 STATIC void _radio_start_threads (dev_ctx_T *dev_ctx) {
163 rtlsdr_dev_t *dev = dev_ctx->dev;
164 dev_ctx->dongle = (dongle_ctx*) malloc(sizeof(dongle_ctx));
165 dev_ctx->demod = (demod_ctx*) malloc(sizeof(demod_ctx));
166 dev_ctx->output = (output_ctx*) malloc(sizeof(output_ctx));
168 dongle_ctx *dongle = dev_ctx->dongle;
169 demod_ctx *demod = dev_ctx->demod;
170 output_ctx *output = dev_ctx->output;
172 pthread_rwlock_init(&demod->lck, NULL);
173 pthread_cond_init(&demod->ok, NULL);
174 pthread_mutex_init(&demod->ok_m, NULL);
175 pthread_rwlock_init(&output->lck, NULL);
176 pthread_cond_init(&output->ok, NULL);
177 pthread_mutex_init(&output->ok_m, NULL);
179 dev_ctx->should_run = 1;
182 dongle->thr_finished = 0;
183 pthread_create(&dongle->thr, NULL, _dongle_thread_fn, (void*)dev_ctx);
186 demod->pre_r = demod->pre_j = 0;
187 demod->now_r = demod->now_j = 0;
188 demod->index = demod->pre_index = demod->now_index = 0;
189 demod->thr_finished = 0;
190 pthread_create(&demod->thr, NULL, _demod_thread_fn, (void*)dev_ctx);
193 output->thr_finished = 0;
194 pthread_create(&output->thr, NULL, _output_thread_fn, (void*)dev_ctx);
197 STATIC void _radio_stop_threads (dev_ctx_T *dev_ctx) {
198 rtlsdr_dev_t *dev = dev_ctx->dev;
199 dongle_ctx *dongle = dev_ctx->dongle;
200 demod_ctx *demod = dev_ctx->demod;
201 output_ctx *output = dev_ctx->output;
203 if (!dongle || !demod || !output)
206 /* stop each "while" loop in threads */
207 dev_ctx->should_run = 0;
209 rtlsdr_cancel_async(dev);
210 pthread_signal(&demod->ok, &demod->ok_m);
211 pthread_signal(&output->ok, &output->ok_m);
213 while (!dongle->thr_finished ||
214 !demod->thr_finished ||
215 !output->thr_finished)
218 pthread_join(dongle->thr, NULL);
219 pthread_join(demod->thr, NULL);
220 pthread_join(output->thr, NULL);
221 pthread_rwlock_destroy(&demod->lck);
222 pthread_cond_destroy(&demod->ok);
223 pthread_mutex_destroy(&demod->ok_m);
224 pthread_rwlock_destroy(&output->lck);
225 pthread_cond_destroy(&output->ok);
226 pthread_mutex_destroy(&output->ok_m);
228 free(dongle); dev_ctx->dongle = NULL;
229 free(demod); dev_ctx->demod = NULL;
230 free(output); dev_ctx->output = NULL;
233 /* ---- LOCAL THREADED FUNCTIONS ---- */
235 STATIC void _rtlsdr_callback (unsigned char *buf, uint32_t len, void *ctx) {
236 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
237 dongle_ctx *dongle = dev_ctx->dongle;
238 demod_ctx *demod = dev_ctx->demod;
242 if (!dev_ctx->should_run)
246 for (i = 0; i < (int)len; i += 8) {
247 tmp = 255 - buf[i+3];
251 buf[i+4] = 255 - buf[i+4];
252 buf[i+5] = 255 - buf[i+5];
254 tmp = 255 - buf[i+6];
260 for (i = 0; i < (int)len; i++)
261 dongle->buf[i] = (int16_t)buf[i] - 127;
263 /* lock demod thread, write to it, unlock */
264 pthread_rwlock_wrlock(&demod->lck);
265 memcpy(demod->buf, dongle->buf, 2 * len);
266 demod->buf_len = len;
267 pthread_rwlock_unlock(&demod->lck);
268 pthread_signal(&demod->ok, &demod->ok_m);
271 STATIC void* _dongle_thread_fn (void *ctx) {
272 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
273 dongle_ctx *dongle = dev_ctx->dongle;
275 rtlsdr_read_async(dev_ctx->dev, _rtlsdr_callback, dev_ctx, 0, 0);
277 dongle->thr_finished = 1;
281 STATIC void _lowpass_demod (void *ctx) {
282 demod_ctx *demod = (demod_ctx *)ctx;
285 while (i < demod->buf_len) {
286 demod->now_r += demod->buf[i];
287 demod->now_j += demod->buf[i+1];
290 if (demod->index < ((1000000 / 200000) + 1))
292 demod->buf[i2] = demod->now_r;
293 demod->buf[i2+1] = demod->now_j;
295 demod->now_r = demod->now_j = 0;
301 STATIC void _lowpassreal_demod (void *ctx) {
302 demod_ctx *demod = (demod_ctx *)ctx;
307 while (i < demod->res_len) {
308 demod->now_index += demod->res[i];
310 demod->pre_index += slow;
311 if (demod->pre_index < fast)
313 demod->res[i2] = (int16_t)(demod->now_index / (fast/slow));
314 demod->pre_index -= fast;
315 demod->now_index = 0;
321 STATIC void _multiply (int ar, int aj, int br, int bj, int *cr, int *cj) {
326 STATIC int _polar_discriminant (int ar, int aj, int br, int bj) {
329 _multiply(ar, aj, br, -bj, &cr, &cj);
330 angle = atan2((double)cj, (double)cr);
331 return (int)(angle / 3.14159 * (1<<14));
334 STATIC void _fm_demod (void *ctx) {
335 demod_ctx *demod = (demod_ctx *)ctx;
336 int16_t *buf = demod->buf;
337 int buf_len = demod->buf_len;
340 pcm = _polar_discriminant(buf[0], buf[1], demod->pre_r, demod->pre_j);
341 demod->res[0] = (int16_t)pcm;
343 for (i = 2; i < (buf_len-1); i += 2) {
344 pcm = _polar_discriminant(buf[i], buf[i+1], buf[i-2], buf[i-1]);
345 demod->res[i/2] = (int16_t)pcm;
347 demod->pre_r = buf[buf_len - 2];
348 demod->pre_j = buf[buf_len - 1];
349 demod->res_len = buf_len/2;
352 STATIC void _am_demod (void *ctx) {
353 demod_ctx *demod = (demod_ctx *)ctx;
354 int16_t *buf = demod->buf;
355 int buf_len = demod->buf_len;
358 for (i = 0; i < buf_len; i += 2) {
359 pcm = buf[i] * buf[i];
360 pcm += buf[i+1] * buf[i+1];
361 demod->res[i/2] = (int16_t)sqrt(pcm);
363 demod->res_len = buf_len/2;
366 STATIC void* _demod_thread_fn (void *ctx) {
367 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
368 demod_ctx *demod = dev_ctx->demod;
369 output_ctx *output = dev_ctx->output;
371 while(dev_ctx->should_run) {
372 pthread_wait(&demod->ok, &demod->ok_m);
373 pthread_rwlock_wrlock(&demod->lck);
374 _lowpass_demod(demod);
375 if (dev_ctx->mode == FM)
379 _lowpassreal_demod(demod);
380 pthread_rwlock_unlock(&demod->lck);
382 /* lock demod thread, write to it, unlock */
383 pthread_rwlock_wrlock(&output->lck);
384 memcpy(output->buf, demod->res, 2 * demod->res_len);
385 output->buf_len = demod->res_len;
386 pthread_rwlock_unlock(&output->lck);
387 pthread_signal(&output->ok, &output->ok_m);
390 demod->thr_finished = 1;
394 STATIC void* _output_thread_fn (void *ctx) {
395 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
396 output_ctx *output = dev_ctx->output;
399 file = fopen (AUDIO_BUFFER, "wb");
401 while (dev_ctx->should_run) {
402 pthread_wait(&output->ok, &output->ok_m);
403 pthread_rwlock_rdlock(&output->lck);
404 if (!dev_ctx->mute && file) {
405 fwrite (output->buf, 2, output->buf_len, file);
407 fseek (file, 0, SEEK_SET);
409 pthread_rwlock_unlock(&output->lck);
411 if (file) fclose(file);
412 unlink (AUDIO_BUFFER);
414 output->thr_finished = 1;