2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Manuel Bachmann"
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include "radio-api.h"
24 #include "radio-rtlsdr.h"
26 static void* _dongle_thread_fn (void *);
27 static void* _demod_thread_fn (void *);
28 static void* _output_thread_fn (void *);
30 static unsigned int init_dev_count = 0;
31 static struct dev_ctx **dev_ctx = NULL;
33 /* ------------- RADIO RTLSDR IMPLEMENTATION ---------------- */
35 /* --- PUBLIC FUNCTIONS --- */
37 /* Radio initialization should be done only when user start the radio and not at plugin initialization
38 Making this call too early would impose to restart the binder to detect a radio */
39 unsigned char _radio_on (unsigned int num, radioCtxHandleT *ctx) {
41 if (num >= _radio_dev_count())
44 if (init_dev_count < _radio_dev_count()) {
45 init_dev_count = _radio_dev_count();
46 dev_ctx = (dev_ctx_T**) realloc (dev_ctx, init_dev_count * sizeof(dev_ctx_T*));
49 dev_ctx[num] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T));
50 dev_ctx[num]->dev = NULL;
51 dev_ctx[num]->mode = ctx->mode;
52 dev_ctx[num]->freq = ctx->freq;
53 dev_ctx[num]->mute = ctx->mute;
54 dev_ctx[num]->should_run = 0;
55 dev_ctx[num]->dongle = NULL;
56 dev_ctx[num]->demod = NULL;
57 dev_ctx[num]->output = NULL;
58 _radio_dev_init (dev_ctx[num], num);
63 void _radio_off (unsigned int num) {
65 if (num >= _radio_dev_count())
69 _radio_dev_free (dev_ctx[num]);
76 void _radio_set_mode (unsigned int num, Mode mode) {
77 if (!dev_ctx || !dev_ctx[num])
80 dev_ctx[num]->mode = mode;
81 _radio_apply_params (dev_ctx[num]);
84 void _radio_set_freq (unsigned int num, double freq) {
85 if (!dev_ctx || !dev_ctx[num])
88 dev_ctx[num]->freq = (float)freq;
89 _radio_apply_params (dev_ctx[num]);
92 void _radio_set_mute (unsigned int num, unsigned char mute) {
93 if (!dev_ctx || !dev_ctx[num])
96 dev_ctx[num]->mute = mute;
97 _radio_apply_params (dev_ctx[num]);
100 void _radio_play (unsigned int num) {
101 if (!dev_ctx || !dev_ctx[num])
104 _radio_start_threads (dev_ctx[num]);
107 void _radio_stop (unsigned int num) {
108 if (!dev_ctx || !dev_ctx[num])
111 _radio_stop_threads (dev_ctx[num]);
114 unsigned int _radio_dev_count () {
115 return rtlsdr_get_device_count();
118 const char* _radio_dev_name (unsigned int num) {
119 return rtlsdr_get_device_name (num);
123 /* --- LOCAL HELPER FUNCTIONS --- */
125 unsigned char _radio_dev_init (dev_ctx_T *dev_ctx, unsigned int num) {
126 rtlsdr_dev_t *dev = dev_ctx->dev;
128 if (rtlsdr_open (&dev, num) < 0)
131 rtlsdr_set_tuner_gain_mode (dev, 0);
133 if (rtlsdr_reset_buffer (dev) < 0)
138 _radio_apply_params (dev_ctx);
143 unsigned char _radio_dev_free (dev_ctx_T *dev_ctx) {
144 rtlsdr_dev_t *dev = dev_ctx->dev;
146 if (rtlsdr_close (dev) < 0)
155 void _radio_apply_params (dev_ctx_T *dev_ctx) {
156 rtlsdr_dev_t *dev = dev_ctx->dev;
157 Mode mode = dev_ctx->mode;
158 float freq = dev_ctx->freq;
162 rate = ((1000000 / 200000) + 1) * 200000;
168 rtlsdr_set_center_freq (dev, freq);
169 rtlsdr_set_sample_rate (dev, rate);
174 void _radio_start_threads (dev_ctx_T *dev_ctx) {
175 dev_ctx->dongle = (dongle_ctx*) malloc (sizeof(dongle_ctx));
176 dev_ctx->demod = (demod_ctx*) malloc (sizeof(demod_ctx));
177 dev_ctx->output = (output_ctx*) malloc (sizeof(output_ctx));
179 dongle_ctx *dongle = dev_ctx->dongle;
180 demod_ctx *demod = dev_ctx->demod;
181 output_ctx *output = dev_ctx->output;
183 pthread_rwlock_init (&demod->lck, NULL);
184 pthread_cond_init (&demod->ok, NULL);
185 pthread_mutex_init (&demod->ok_m, NULL);
186 pthread_rwlock_init (&output->lck, NULL);
187 pthread_cond_init (&output->ok, NULL);
188 pthread_mutex_init (&output->ok_m, NULL);
190 dev_ctx->should_run = 1;
193 dongle->thr_finished = 0;
194 pthread_create (&dongle->thr, NULL, _dongle_thread_fn, (void*)dev_ctx);
197 demod->pre_r = demod->pre_j = 0;
198 demod->now_r = demod->now_j = 0;
199 demod->index = demod->pre_index = demod->now_index = 0;
200 demod->thr_finished = 0;
201 pthread_create (&demod->thr, NULL, _demod_thread_fn, (void*)dev_ctx);
204 output->thr_finished = 0;
205 pthread_create (&output->thr, NULL, _output_thread_fn, (void*)dev_ctx);
208 void _radio_stop_threads (dev_ctx_T *dev_ctx) {
209 rtlsdr_dev_t *dev = dev_ctx->dev;
210 dongle_ctx *dongle = dev_ctx->dongle;
211 demod_ctx *demod = dev_ctx->demod;
212 output_ctx *output = dev_ctx->output;
214 if (!dongle || !demod || !output)
217 /* stop each "while" loop in threads */
218 dev_ctx->should_run = 0;
220 rtlsdr_cancel_async (dev);
221 pthread_signal (&demod->ok, &demod->ok_m);
222 pthread_signal (&output->ok, &output->ok_m);
224 while (!dongle->thr_finished ||
225 !demod->thr_finished ||
226 !output->thr_finished)
229 pthread_join (dongle->thr, NULL);
230 pthread_join (demod->thr, NULL);
231 pthread_join (output->thr, NULL);
232 pthread_rwlock_destroy (&demod->lck);
233 pthread_cond_destroy (&demod->ok);
234 pthread_mutex_destroy (&demod->ok_m);
235 pthread_rwlock_destroy (&output->lck);
236 pthread_cond_destroy (&output->ok);
237 pthread_mutex_destroy (&output->ok_m);
239 free (dongle); dev_ctx->dongle = NULL;
240 free (demod); dev_ctx->demod = NULL;
241 free (output); dev_ctx->output = NULL;
244 /* ---- LOCAL THREADED FUNCTIONS ---- */
246 static void _rtlsdr_callback (unsigned char *buf, uint32_t len, void *ctx) {
247 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
248 dongle_ctx *dongle = dev_ctx->dongle;
249 demod_ctx *demod = dev_ctx->demod;
253 if (!dev_ctx->should_run)
257 for (i = 0; i < (int)len; i += 8) {
258 tmp = 255 - buf[i+3];
262 buf[i+4] = 255 - buf[i+4];
263 buf[i+5] = 255 - buf[i+5];
265 tmp = 255 - buf[i+6];
271 for (i = 0; i < (int)len; i++)
272 dongle->buf[i] = (int16_t)buf[i] - 127;
274 /* lock demod thread, write to it, unlock */
275 pthread_rwlock_wrlock (&demod->lck);
276 memcpy (demod->buf, dongle->buf, 2 * len);
277 demod->buf_len = len;
278 pthread_rwlock_unlock (&demod->lck);
279 pthread_signal (&demod->ok, &demod->ok_m);
282 static void* _dongle_thread_fn (void *ctx) {
283 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
284 dongle_ctx *dongle = dev_ctx->dongle;
286 rtlsdr_read_async (dev_ctx->dev, _rtlsdr_callback, dev_ctx, 0, 0);
288 dongle->thr_finished = 1;
292 static void _lowpass_demod (void *ctx) {
293 demod_ctx *demod = (demod_ctx *)ctx;
296 while (i < demod->buf_len) {
297 demod->now_r += demod->buf[i];
298 demod->now_j += demod->buf[i+1];
301 if (demod->index < ((1000000 / 200000) + 1))
303 demod->buf[i2] = demod->now_r;
304 demod->buf[i2+1] = demod->now_j;
306 demod->now_r = demod->now_j = 0;
312 static void _lowpassreal_demod (void *ctx) {
313 demod_ctx *demod = (demod_ctx *)ctx;
318 while (i < demod->res_len) {
319 demod->now_index += demod->res[i];
321 demod->pre_index += slow;
322 if (demod->pre_index < fast)
324 demod->res[i2] = (int16_t)(demod->now_index / (fast/slow));
325 demod->pre_index -= fast;
326 demod->now_index = 0;
332 static void _multiply (int ar, int aj, int br, int bj, int *cr, int *cj) {
337 static int _polar_discriminant (int ar, int aj, int br, int bj) {
340 _multiply (ar, aj, br, -bj, &cr, &cj);
341 angle = atan2 ((double)cj, (double)cr);
342 return (int)(angle / 3.14159 * (1<<14));
345 static void _fm_demod (void *ctx) {
346 demod_ctx *demod = (demod_ctx *)ctx;
347 int16_t *buf = demod->buf;
348 int buf_len = demod->buf_len;
351 pcm = _polar_discriminant (buf[0], buf[1], demod->pre_r, demod->pre_j);
352 demod->res[0] = (int16_t)pcm;
354 for (i = 2; i < (buf_len-1); i += 2) {
355 pcm = _polar_discriminant (buf[i], buf[i+1], buf[i-2], buf[i-1]);
356 demod->res[i/2] = (int16_t)pcm;
358 demod->pre_r = buf[buf_len - 2];
359 demod->pre_j = buf[buf_len - 1];
360 demod->res_len = buf_len/2;
363 static void _am_demod (void *ctx) {
364 demod_ctx *demod = (demod_ctx *)ctx;
365 int16_t *buf = demod->buf;
366 int buf_len = demod->buf_len;
369 for (i = 0; i < buf_len; i += 2) {
370 pcm = buf[i] * buf[i];
371 pcm += buf[i+1] * buf[i+1];
372 demod->res[i/2] = (int16_t)sqrt(pcm);
374 demod->res_len = buf_len/2;
377 static void* _demod_thread_fn (void *ctx) {
378 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
379 demod_ctx *demod = dev_ctx->demod;
380 output_ctx *output = dev_ctx->output;
382 while(dev_ctx->should_run) {
383 pthread_wait (&demod->ok, &demod->ok_m);
384 pthread_rwlock_wrlock (&demod->lck);
385 _lowpass_demod (demod);
386 if (dev_ctx->mode == FM)
390 _lowpassreal_demod (demod);
391 pthread_rwlock_unlock (&demod->lck);
393 /* lock demod thread, write to it, unlock */
394 pthread_rwlock_wrlock (&output->lck);
395 memcpy (output->buf, demod->res, 2 * demod->res_len);
396 output->buf_len = demod->res_len;
397 pthread_rwlock_unlock (&output->lck);
398 pthread_signal (&output->ok, &output->ok_m);
401 demod->thr_finished = 1;
405 static void* _output_thread_fn (void *ctx) {
406 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
407 output_ctx *output = dev_ctx->output;
410 file = fopen (AUDIO_BUFFER, "wb");
412 while (dev_ctx->should_run) {
413 pthread_wait (&output->ok, &output->ok_m);
414 pthread_rwlock_rdlock (&output->lck);
415 if (!dev_ctx->mute && file) {
416 fwrite (output->buf, 2, output->buf_len, file);
418 fseek (file, 0, SEEK_SET);
420 pthread_rwlock_unlock (&output->lck);
422 if (file) fclose(file);
423 unlink (AUDIO_BUFFER);
425 output->thr_finished = 1;