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 unsigned int init_dev_count = 0;
27 static struct dev_ctx **dev_ctx = NULL;
29 /* ------------- RADIO RTLSDR IMPLEMENTATION ---------------- */
31 /* --- PUBLIC FUNCTIONS --- */
33 /* Radio initialization should be done only when user start the radio and not at plugin initialization
34 Making this call too early would impose to restart the binder to detect a radio */
35 unsigned char _radio_on (unsigned int num, radioCtxHandleT *ctx) {
37 if (num >= _radio_dev_count())
40 if (init_dev_count < _radio_dev_count()) {
41 init_dev_count = _radio_dev_count();
42 dev_ctx = (dev_ctx_T**) realloc (dev_ctx, init_dev_count * sizeof(dev_ctx_T*));
45 dev_ctx[num] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T));
46 dev_ctx[num]->dev = NULL;
47 dev_ctx[num]->mode = ctx->mode;
48 dev_ctx[num]->freq = ctx->freq;
49 dev_ctx[num]->mute = ctx->mute;
50 dev_ctx[num]->should_run = 0;
51 dev_ctx[num]->dongle = NULL;
52 dev_ctx[num]->demod = NULL;
53 dev_ctx[num]->output = NULL;
54 _radio_dev_init (dev_ctx[num], num);
59 void _radio_off (unsigned int num) {
61 if (num >= _radio_dev_count())
65 _radio_dev_free (dev_ctx[num]);
72 void _radio_set_mode (unsigned int num, Mode mode) {
73 if (!dev_ctx || !dev_ctx[num])
76 dev_ctx[num]->mode = mode;
77 _radio_apply_params (dev_ctx[num]);
80 void _radio_set_freq (unsigned int num, double freq) {
81 if (!dev_ctx || !dev_ctx[num])
84 dev_ctx[num]->freq = (float)freq;
85 _radio_apply_params (dev_ctx[num]);
88 void _radio_set_mute (unsigned int num, unsigned char mute) {
89 if (!dev_ctx || !dev_ctx[num])
92 dev_ctx[num]->mute = mute;
93 _radio_apply_params (dev_ctx[num]);
96 void _radio_play (unsigned int num) {
97 if (!dev_ctx || !dev_ctx[num])
100 _radio_start_threads (dev_ctx[num]);
103 void _radio_stop (unsigned int num) {
104 if (!dev_ctx || !dev_ctx[num])
107 _radio_stop_threads (dev_ctx[num]);
110 unsigned int _radio_dev_count () {
111 return rtlsdr_get_device_count();
114 const char* _radio_dev_name (unsigned int num) {
115 return rtlsdr_get_device_name (num);
119 /* --- LOCAL HELPER FUNCTIONS --- */
121 unsigned char _radio_dev_init (dev_ctx_T *dev_ctx, unsigned int num) {
122 rtlsdr_dev_t *dev = dev_ctx->dev;
124 if (rtlsdr_open (&dev, num) < 0)
127 rtlsdr_set_tuner_gain_mode (dev, 0);
129 if (rtlsdr_reset_buffer (dev) < 0)
134 _radio_apply_params (dev_ctx);
139 unsigned char _radio_dev_free (dev_ctx_T *dev_ctx) {
140 rtlsdr_dev_t *dev = dev_ctx->dev;
142 if (rtlsdr_close (dev) < 0)
151 void _radio_apply_params (dev_ctx_T *dev_ctx) {
152 rtlsdr_dev_t *dev = dev_ctx->dev;
153 Mode mode = dev_ctx->mode;
154 float freq = dev_ctx->freq;
158 rate = ((1000000 / 200000) + 1) * 200000;
164 rtlsdr_set_center_freq (dev, freq);
165 rtlsdr_set_sample_rate (dev, rate);
170 void _radio_start_threads (dev_ctx_T *dev_ctx) {
171 dev_ctx->dongle = (dongle_ctx*) malloc (sizeof(dongle_ctx));
172 dev_ctx->demod = (demod_ctx*) malloc (sizeof(demod_ctx));
173 dev_ctx->output = (output_ctx*) malloc (sizeof(output_ctx));
175 dongle_ctx *dongle = dev_ctx->dongle;
176 demod_ctx *demod = dev_ctx->demod;
177 output_ctx *output = dev_ctx->output;
179 pthread_rwlock_init (&demod->lck, NULL);
180 pthread_cond_init (&demod->ok, NULL);
181 pthread_mutex_init (&demod->ok_m, NULL);
182 pthread_rwlock_init (&output->lck, NULL);
183 pthread_cond_init (&output->ok, NULL);
184 pthread_mutex_init (&output->ok_m, NULL);
186 dev_ctx->should_run = 1;
189 dongle->thr_finished = 0;
190 pthread_create (&dongle->thr, NULL, _dongle_thread_fn, (void*)dev_ctx);
193 demod->pre_r = demod->pre_j = 0;
194 demod->now_r = demod->now_j = 0;
195 demod->index = demod->pre_index = demod->now_index = 0;
196 demod->thr_finished = 0;
197 pthread_create (&demod->thr, NULL, _demod_thread_fn, (void*)dev_ctx);
200 output->thr_finished = 0;
201 pthread_create (&output->thr, NULL, _output_thread_fn, (void*)dev_ctx);
204 void _radio_stop_threads (dev_ctx_T *dev_ctx) {
205 rtlsdr_dev_t *dev = dev_ctx->dev;
206 dongle_ctx *dongle = dev_ctx->dongle;
207 demod_ctx *demod = dev_ctx->demod;
208 output_ctx *output = dev_ctx->output;
210 if (!dongle || !demod || !output)
213 /* stop each "while" loop in threads */
214 dev_ctx->should_run = 0;
216 rtlsdr_cancel_async (dev);
217 pthread_signal (&demod->ok, &demod->ok_m);
218 pthread_signal (&output->ok, &output->ok_m);
220 while (!dongle->thr_finished ||
221 !demod->thr_finished ||
222 !output->thr_finished)
225 pthread_join (dongle->thr, NULL);
226 pthread_join (demod->thr, NULL);
227 pthread_join (output->thr, NULL);
228 pthread_rwlock_destroy (&demod->lck);
229 pthread_cond_destroy (&demod->ok);
230 pthread_mutex_destroy (&demod->ok_m);
231 pthread_rwlock_destroy (&output->lck);
232 pthread_cond_destroy (&output->ok);
233 pthread_mutex_destroy (&output->ok_m);
235 free (dongle); dev_ctx->dongle = NULL;
236 free (demod); dev_ctx->demod = NULL;
237 free (output); dev_ctx->output = NULL;
240 /* ---- LOCAL THREADED FUNCTIONS ---- */
242 static void _rtlsdr_callback (unsigned char *buf, uint32_t len, void *ctx) {
243 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
244 dongle_ctx *dongle = dev_ctx->dongle;
245 demod_ctx *demod = dev_ctx->demod;
249 if (!dev_ctx->should_run)
253 for (i = 0; i < (int)len; i += 8) {
254 tmp = 255 - buf[i+3];
258 buf[i+4] = 255 - buf[i+4];
259 buf[i+5] = 255 - buf[i+5];
261 tmp = 255 - buf[i+6];
267 for (i = 0; i < (int)len; i++)
268 dongle->buf[i] = (int16_t)buf[i] - 127;
270 /* lock demod thread, write to it, unlock */
271 pthread_rwlock_wrlock (&demod->lck);
272 memcpy (demod->buf, dongle->buf, 2 * len);
273 demod->buf_len = len;
274 pthread_rwlock_unlock (&demod->lck);
275 pthread_signal (&demod->ok, &demod->ok_m);
278 static void* _dongle_thread_fn (void *ctx) {
279 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
280 dongle_ctx *dongle = dev_ctx->dongle;
282 rtlsdr_read_async (dev_ctx->dev, _rtlsdr_callback, dev_ctx, 0, 0);
284 dongle->thr_finished = 1;
288 static void _lowpass_demod (void *ctx) {
289 demod_ctx *demod = (demod_ctx *)ctx;
292 while (i < demod->buf_len) {
293 demod->now_r += demod->buf[i];
294 demod->now_j += demod->buf[i+1];
297 if (demod->index < ((1000000 / 200000) + 1))
299 demod->buf[i2] = demod->now_r;
300 demod->buf[i2+1] = demod->now_j;
302 demod->now_r = demod->now_j = 0;
308 static void _lowpassreal_demod (void *ctx) {
309 demod_ctx *demod = (demod_ctx *)ctx;
314 while (i < demod->res_len) {
315 demod->now_index += demod->res[i];
317 demod->pre_index += slow;
318 if (demod->pre_index < fast)
320 demod->res[i2] = (int16_t)(demod->now_index / (fast/slow));
321 demod->pre_index -= fast;
322 demod->now_index = 0;
328 static void _multiply (int ar, int aj, int br, int bj, int *cr, int *cj) {
333 static int _polar_discriminant (int ar, int aj, int br, int bj) {
336 _multiply (ar, aj, br, -bj, &cr, &cj);
337 angle = atan2 ((double)cj, (double)cr);
338 return (int)(angle / 3.14159 * (1<<14));
341 static void _fm_demod (void *ctx) {
342 demod_ctx *demod = (demod_ctx *)ctx;
343 int16_t *buf = demod->buf;
344 int buf_len = demod->buf_len;
347 pcm = _polar_discriminant (buf[0], buf[1], demod->pre_r, demod->pre_j);
348 demod->res[0] = (int16_t)pcm;
350 for (i = 2; i < (buf_len-1); i += 2) {
351 pcm = _polar_discriminant (buf[i], buf[i+1], buf[i-2], buf[i-1]);
352 demod->res[i/2] = (int16_t)pcm;
354 demod->pre_r = buf[buf_len - 2];
355 demod->pre_j = buf[buf_len - 1];
356 demod->res_len = buf_len/2;
359 static void _am_demod (void *ctx) {
360 demod_ctx *demod = (demod_ctx *)ctx;
361 int16_t *buf = demod->buf;
362 int buf_len = demod->buf_len;
365 for (i = 0; i < buf_len; i += 2) {
366 pcm = buf[i] * buf[i];
367 pcm += buf[i+1] * buf[i+1];
368 demod->res[i/2] = (int16_t)sqrt(pcm);
370 demod->res_len = buf_len/2;
373 static void* _demod_thread_fn (void *ctx) {
374 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
375 demod_ctx *demod = dev_ctx->demod;
376 output_ctx *output = dev_ctx->output;
378 while(dev_ctx->should_run) {
379 pthread_wait (&demod->ok, &demod->ok_m);
380 pthread_rwlock_wrlock (&demod->lck);
381 _lowpass_demod (demod);
382 if (dev_ctx->mode == FM)
386 _lowpassreal_demod (demod);
387 pthread_rwlock_unlock (&demod->lck);
389 /* lock demod thread, write to it, unlock */
390 pthread_rwlock_wrlock (&output->lck);
391 memcpy (output->buf, demod->res, 2 * demod->res_len);
392 output->buf_len = demod->res_len;
393 pthread_rwlock_unlock (&output->lck);
394 pthread_signal (&output->ok, &output->ok_m);
397 demod->thr_finished = 1;
401 static void* _output_thread_fn (void *ctx) {
402 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
403 output_ctx *output = dev_ctx->output;
406 file = fopen (AUDIO_BUFFER, "wb");
408 while (dev_ctx->should_run) {
409 pthread_wait (&output->ok, &output->ok_m);
410 pthread_rwlock_rdlock (&output->lck);
411 if (!dev_ctx->mute && file) {
412 fwrite (output->buf, 2, output->buf_len, file);
414 fseek (file, 0, SEEK_SET);
416 pthread_rwlock_unlock (&output->lck);
418 if (file) fclose(file);
419 unlink (AUDIO_BUFFER);
421 output->thr_finished = 1;