2 * Copyright (C) 2015 "IoT.bzh"
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.
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.
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/>.
18 #include "radio-api.h"
19 #include "radio-rtlsdr.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]);
63 PUBLIC void _radio_set_mode (unsigned int num, Mode mode) {
64 if (!dev_ctx || !dev_ctx[num])
67 dev_ctx[num]->mode = mode;
68 _radio_apply_params(dev_ctx[num]);
71 PUBLIC void _radio_set_freq (unsigned int num, float freq) {
72 if (!dev_ctx || !dev_ctx[num])
75 dev_ctx[num]->freq = freq;
76 _radio_apply_params(dev_ctx[num]);
79 PUBLIC void _radio_set_mute (unsigned int num, unsigned char mute) {
80 if (!dev_ctx || !dev_ctx[num])
83 dev_ctx[num]->mute = mute;
84 _radio_apply_params(dev_ctx[num]);
87 PUBLIC void _radio_play (unsigned int num) {
88 if (!dev_ctx || !dev_ctx[num])
91 _radio_start_threads(dev_ctx[num]);
94 PUBLIC void _radio_stop (unsigned int num) {
95 if (!dev_ctx || !dev_ctx[num])
98 _radio_stop_threads(dev_ctx[num]);
101 PUBLIC unsigned int _radio_dev_count () {
102 return rtlsdr_get_device_count();
105 PUBLIC const char* _radio_dev_name (unsigned int num) {
106 return rtlsdr_get_device_name(num);
110 /* --- LOCAL HELPER FUNCTIONS --- */
112 STATIC unsigned char _radio_dev_init (dev_ctx_T *dev_ctx, unsigned int num) {
113 rtlsdr_dev_t *dev = dev_ctx->dev;
115 if (rtlsdr_open(&dev, num) < 0)
118 rtlsdr_set_tuner_gain_mode(dev, 0);
120 if (rtlsdr_reset_buffer(dev) < 0)
125 _radio_apply_params(dev_ctx);
130 STATIC unsigned char _radio_dev_free (dev_ctx_T *dev_ctx) {
131 rtlsdr_dev_t *dev = dev_ctx->dev;
133 if (rtlsdr_close(dev) < 0)
142 STATIC void _radio_apply_params (dev_ctx_T *dev_ctx) {
143 rtlsdr_dev_t *dev = dev_ctx->dev;
144 Mode mode = dev_ctx->mode;
145 float freq = dev_ctx->freq;
149 rate = ((1000000 / 200000) + 1) * 200000;
155 rtlsdr_set_center_freq(dev, freq);
156 rtlsdr_set_sample_rate(dev, rate);
161 STATIC void _radio_start_threads (dev_ctx_T *dev_ctx) {
162 rtlsdr_dev_t *dev = dev_ctx->dev;
163 dev_ctx->dongle = (dongle_ctx*) malloc(sizeof(dongle_ctx));
164 dev_ctx->demod = (demod_ctx*) malloc(sizeof(demod_ctx));
165 dev_ctx->output = (output_ctx*) malloc(sizeof(output_ctx));
167 dongle_ctx *dongle = dev_ctx->dongle;
168 demod_ctx *demod = dev_ctx->demod;
169 output_ctx *output = dev_ctx->output;
171 pthread_rwlock_init(&demod->lck, NULL);
172 pthread_cond_init(&demod->ok, NULL);
173 pthread_mutex_init(&demod->ok_m, NULL);
174 pthread_rwlock_init(&output->lck, NULL);
175 pthread_cond_init(&output->ok, NULL);
176 pthread_mutex_init(&output->ok_m, NULL);
178 dev_ctx->should_run = 1;
181 dongle->thr_finished = 0;
182 pthread_create(&dongle->thr, NULL, _dongle_thread_fn, (void*)dev_ctx);
185 demod->pre_r = demod->pre_j = 0;
186 demod->now_r = demod->now_j = 0;
187 demod->index = demod->pre_index = demod->now_index = 0;
188 demod->thr_finished = 0;
189 pthread_create(&demod->thr, NULL, _demod_thread_fn, (void*)dev_ctx);
192 output->thr_finished = 0;
193 pthread_create(&output->thr, NULL, _output_thread_fn, (void*)dev_ctx);
196 STATIC void _radio_stop_threads (dev_ctx_T *dev_ctx) {
197 rtlsdr_dev_t *dev = dev_ctx->dev;
198 dongle_ctx *dongle = dev_ctx->dongle;
199 demod_ctx *demod = dev_ctx->demod;
200 output_ctx *output = dev_ctx->output;
202 if (!dongle || !demod || !output)
205 /* stop each "while" loop in threads */
206 dev_ctx->should_run = 0;
208 rtlsdr_cancel_async(dev);
209 pthread_signal(&demod->ok, &demod->ok_m);
210 pthread_signal(&output->ok, &output->ok_m);
212 while (!dongle->thr_finished ||
213 !demod->thr_finished ||
214 !output->thr_finished)
217 pthread_join(dongle->thr, NULL);
218 pthread_join(demod->thr, NULL);
219 pthread_join(output->thr, NULL);
220 pthread_rwlock_destroy(&demod->lck);
221 pthread_cond_destroy(&demod->ok);
222 pthread_mutex_destroy(&demod->ok_m);
223 pthread_rwlock_destroy(&output->lck);
224 pthread_cond_destroy(&output->ok);
225 pthread_mutex_destroy(&output->ok_m);
227 free(dongle); dev_ctx->dongle = NULL;
228 free(demod); dev_ctx->demod = NULL;
229 free(output); dev_ctx->output = NULL;
232 /* ---- LOCAL THREADED FUNCTIONS ---- */
234 STATIC void _rtlsdr_callback (unsigned char *buf, uint32_t len, void *ctx) {
235 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
236 dongle_ctx *dongle = dev_ctx->dongle;
237 demod_ctx *demod = dev_ctx->demod;
241 if (!dev_ctx->should_run)
245 for (i = 0; i < (int)len; i += 8) {
246 tmp = 255 - buf[i+3];
250 buf[i+4] = 255 - buf[i+4];
251 buf[i+5] = 255 - buf[i+5];
253 tmp = 255 - buf[i+6];
259 for (i = 0; i < (int)len; i++)
260 dongle->buf[i] = (int16_t)buf[i] - 127;
262 /* lock demod thread, write to it, unlock */
263 pthread_rwlock_wrlock(&demod->lck);
264 memcpy(demod->buf, dongle->buf, 2 * len);
265 demod->buf_len = len;
266 pthread_rwlock_unlock(&demod->lck);
267 pthread_signal(&demod->ok, &demod->ok_m);
270 STATIC void* _dongle_thread_fn (void *ctx) {
271 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
272 dongle_ctx *dongle = dev_ctx->dongle;
274 rtlsdr_read_async(dev_ctx->dev, _rtlsdr_callback, dev_ctx, 0, 0);
276 dongle->thr_finished = 1;
280 STATIC void _lowpass_demod (void *ctx) {
281 demod_ctx *demod = (demod_ctx *)ctx;
284 while (i < demod->buf_len) {
285 demod->now_r += demod->buf[i];
286 demod->now_j += demod->buf[i+1];
289 if (demod->index < ((1000000 / 200000) + 1))
291 demod->buf[i2] = demod->now_r;
292 demod->buf[i2+1] = demod->now_j;
294 demod->now_r = demod->now_j = 0;
300 STATIC void _lowpassreal_demod (void *ctx) {
301 demod_ctx *demod = (demod_ctx *)ctx;
306 while (i < demod->res_len) {
307 demod->now_index += demod->res[i];
309 demod->pre_index += slow;
310 if (demod->pre_index < fast)
312 demod->res[i2] = (int16_t)(demod->now_index / (fast/slow));
313 demod->pre_index -= fast;
314 demod->now_index = 0;
320 STATIC void _multiply (int ar, int aj, int br, int bj, int *cr, int *cj) {
325 STATIC int _polar_discriminant (int ar, int aj, int br, int bj) {
328 _multiply(ar, aj, br, -bj, &cr, &cj);
329 angle = atan2((double)cj, (double)cr);
330 return (int)(angle / 3.14159 * (1<<14));
333 STATIC void _fm_demod (void *ctx) {
334 demod_ctx *demod = (demod_ctx *)ctx;
335 int16_t *buf = demod->buf;
336 int buf_len = demod->buf_len;
339 pcm = _polar_discriminant(buf[0], buf[1], demod->pre_r, demod->pre_j);
340 demod->res[0] = (int16_t)pcm;
342 for (i = 2; i < (buf_len-1); i += 2) {
343 pcm = _polar_discriminant(buf[i], buf[i+1], buf[i-2], buf[i-1]);
344 demod->res[i/2] = (int16_t)pcm;
346 demod->pre_r = buf[buf_len - 2];
347 demod->pre_j = buf[buf_len - 1];
348 demod->res_len = buf_len/2;
351 STATIC void _am_demod (void *ctx) {
352 demod_ctx *demod = (demod_ctx *)ctx;
353 int16_t *buf = demod->buf;
354 int buf_len = demod->buf_len;
357 for (i = 0; i < buf_len; i += 2) {
358 pcm = buf[i] * buf[i];
359 pcm += buf[i+1] * buf[i+1];
360 demod->res[i/2] = (int16_t)sqrt(pcm);
362 demod->res_len = buf_len/2;
365 STATIC void* _demod_thread_fn (void *ctx) {
366 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
367 demod_ctx *demod = dev_ctx->demod;
368 output_ctx *output = dev_ctx->output;
370 while(dev_ctx->should_run) {
371 pthread_wait(&demod->ok, &demod->ok_m);
372 pthread_rwlock_wrlock(&demod->lck);
373 _lowpass_demod(demod);
374 if (dev_ctx->mode == FM)
378 _lowpassreal_demod(demod);
379 pthread_rwlock_unlock(&demod->lck);
381 /* lock demod thread, write to it, unlock */
382 pthread_rwlock_wrlock(&output->lck);
383 memcpy(output->buf, demod->res, 2 * demod->res_len);
384 output->buf_len = demod->res_len;
385 pthread_rwlock_unlock(&output->lck);
386 pthread_signal(&output->ok, &output->ok_m);
389 demod->thr_finished = 1;
393 STATIC void* _output_thread_fn (void *ctx) {
394 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
395 output_ctx *output = dev_ctx->output;
397 while (dev_ctx->should_run) {
398 pthread_wait(&output->ok, &output->ok_m);
399 pthread_rwlock_rdlock(&output->lck);
400 //if (!dev_ctx->mute)
401 // mRadio->PlayAlsa((void*)&output->buf, output->buf_len);
402 pthread_rwlock_unlock(&output->lck);
405 output->thr_finished = 1;