2 * Copyright (C) 2015 "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.
21 #include "radio-api.h"
22 #include "radio-rtlsdr.h"
24 /* ------------- RADIO RTLSDR IMPLEMENTATION ---------------- */
26 /* --- PUBLIC FUNCTIONS --- */
28 /* Radio initialization should be done only when user start the radio and not at plugin initialization
29 Making this call too early would impose to restart the binder to detect a radio */
30 PUBLIC unsigned char _radio_on (unsigned int num, radioCtxHandleT *ctx) {
32 if (num >= _radio_dev_count())
35 if (init_dev_count < _radio_dev_count()) {
36 init_dev_count = _radio_dev_count();
37 dev_ctx = (dev_ctx_T**) realloc (dev_ctx, init_dev_count * sizeof(dev_ctx_T));
40 dev_ctx[num] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T));
41 dev_ctx[num]->dev = NULL;
42 dev_ctx[num]->mode = ctx->mode;
43 dev_ctx[num]->freq = ctx->freq;
44 dev_ctx[num]->mute = ctx->mute;
45 dev_ctx[num]->should_run = 0;
46 dev_ctx[num]->dongle = NULL;
47 dev_ctx[num]->demod = NULL;
48 dev_ctx[num]->output = NULL;
49 _radio_dev_init(dev_ctx[num], num);
54 PUBLIC void _radio_off (unsigned int num) {
56 if (num >= _radio_dev_count())
60 _radio_dev_free(dev_ctx[num]);
67 PUBLIC void _radio_set_mode (unsigned int num, Mode mode) {
68 if (!dev_ctx || !dev_ctx[num])
71 dev_ctx[num]->mode = mode;
72 _radio_apply_params(dev_ctx[num]);
75 PUBLIC void _radio_set_freq (unsigned int num, double freq) {
76 if (!dev_ctx || !dev_ctx[num])
79 dev_ctx[num]->freq = (float)freq;
80 _radio_apply_params(dev_ctx[num]);
83 PUBLIC void _radio_set_mute (unsigned int num, unsigned char mute) {
84 if (!dev_ctx || !dev_ctx[num])
87 dev_ctx[num]->mute = mute;
88 _radio_apply_params(dev_ctx[num]);
91 PUBLIC void _radio_play (unsigned int num) {
92 if (!dev_ctx || !dev_ctx[num])
95 _radio_start_threads(dev_ctx[num]);
98 PUBLIC void _radio_stop (unsigned int num) {
99 if (!dev_ctx || !dev_ctx[num])
102 _radio_stop_threads(dev_ctx[num]);
105 PUBLIC unsigned int _radio_dev_count () {
106 return rtlsdr_get_device_count();
109 PUBLIC const char* _radio_dev_name (unsigned int num) {
110 return rtlsdr_get_device_name(num);
114 /* --- LOCAL HELPER FUNCTIONS --- */
116 STATIC unsigned char _radio_dev_init (dev_ctx_T *dev_ctx, unsigned int num) {
117 rtlsdr_dev_t *dev = dev_ctx->dev;
119 if (rtlsdr_open(&dev, num) < 0)
122 rtlsdr_set_tuner_gain_mode(dev, 0);
124 if (rtlsdr_reset_buffer(dev) < 0)
129 _radio_apply_params(dev_ctx);
134 STATIC unsigned char _radio_dev_free (dev_ctx_T *dev_ctx) {
135 rtlsdr_dev_t *dev = dev_ctx->dev;
137 if (rtlsdr_close(dev) < 0)
146 STATIC void _radio_apply_params (dev_ctx_T *dev_ctx) {
147 rtlsdr_dev_t *dev = dev_ctx->dev;
148 Mode mode = dev_ctx->mode;
149 float freq = dev_ctx->freq;
153 rate = ((1000000 / 200000) + 1) * 200000;
159 rtlsdr_set_center_freq(dev, freq);
160 rtlsdr_set_sample_rate(dev, rate);
165 STATIC void _radio_start_threads (dev_ctx_T *dev_ctx) {
166 rtlsdr_dev_t *dev = dev_ctx->dev;
167 dev_ctx->dongle = (dongle_ctx*) malloc(sizeof(dongle_ctx));
168 dev_ctx->demod = (demod_ctx*) malloc(sizeof(demod_ctx));
169 dev_ctx->output = (output_ctx*) malloc(sizeof(output_ctx));
171 dongle_ctx *dongle = dev_ctx->dongle;
172 demod_ctx *demod = dev_ctx->demod;
173 output_ctx *output = dev_ctx->output;
175 pthread_rwlock_init(&demod->lck, NULL);
176 pthread_cond_init(&demod->ok, NULL);
177 pthread_mutex_init(&demod->ok_m, NULL);
178 pthread_rwlock_init(&output->lck, NULL);
179 pthread_cond_init(&output->ok, NULL);
180 pthread_mutex_init(&output->ok_m, NULL);
182 dev_ctx->should_run = 1;
185 dongle->thr_finished = 0;
186 pthread_create(&dongle->thr, NULL, _dongle_thread_fn, (void*)dev_ctx);
189 demod->pre_r = demod->pre_j = 0;
190 demod->now_r = demod->now_j = 0;
191 demod->index = demod->pre_index = demod->now_index = 0;
192 demod->thr_finished = 0;
193 pthread_create(&demod->thr, NULL, _demod_thread_fn, (void*)dev_ctx);
196 output->thr_finished = 0;
197 pthread_create(&output->thr, NULL, _output_thread_fn, (void*)dev_ctx);
200 STATIC void _radio_stop_threads (dev_ctx_T *dev_ctx) {
201 rtlsdr_dev_t *dev = dev_ctx->dev;
202 dongle_ctx *dongle = dev_ctx->dongle;
203 demod_ctx *demod = dev_ctx->demod;
204 output_ctx *output = dev_ctx->output;
206 if (!dongle || !demod || !output)
209 /* stop each "while" loop in threads */
210 dev_ctx->should_run = 0;
212 rtlsdr_cancel_async(dev);
213 pthread_signal(&demod->ok, &demod->ok_m);
214 pthread_signal(&output->ok, &output->ok_m);
216 while (!dongle->thr_finished ||
217 !demod->thr_finished ||
218 !output->thr_finished)
221 pthread_join(dongle->thr, NULL);
222 pthread_join(demod->thr, NULL);
223 pthread_join(output->thr, NULL);
224 pthread_rwlock_destroy(&demod->lck);
225 pthread_cond_destroy(&demod->ok);
226 pthread_mutex_destroy(&demod->ok_m);
227 pthread_rwlock_destroy(&output->lck);
228 pthread_cond_destroy(&output->ok);
229 pthread_mutex_destroy(&output->ok_m);
231 free(dongle); dev_ctx->dongle = NULL;
232 free(demod); dev_ctx->demod = NULL;
233 free(output); dev_ctx->output = NULL;
236 /* ---- LOCAL THREADED FUNCTIONS ---- */
238 STATIC void _rtlsdr_callback (unsigned char *buf, uint32_t len, void *ctx) {
239 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
240 dongle_ctx *dongle = dev_ctx->dongle;
241 demod_ctx *demod = dev_ctx->demod;
245 if (!dev_ctx->should_run)
249 for (i = 0; i < (int)len; i += 8) {
250 tmp = 255 - buf[i+3];
254 buf[i+4] = 255 - buf[i+4];
255 buf[i+5] = 255 - buf[i+5];
257 tmp = 255 - buf[i+6];
263 for (i = 0; i < (int)len; i++)
264 dongle->buf[i] = (int16_t)buf[i] - 127;
266 /* lock demod thread, write to it, unlock */
267 pthread_rwlock_wrlock(&demod->lck);
268 memcpy(demod->buf, dongle->buf, 2 * len);
269 demod->buf_len = len;
270 pthread_rwlock_unlock(&demod->lck);
271 pthread_signal(&demod->ok, &demod->ok_m);
274 STATIC void* _dongle_thread_fn (void *ctx) {
275 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
276 dongle_ctx *dongle = dev_ctx->dongle;
278 rtlsdr_read_async(dev_ctx->dev, _rtlsdr_callback, dev_ctx, 0, 0);
280 dongle->thr_finished = 1;
284 STATIC void _lowpass_demod (void *ctx) {
285 demod_ctx *demod = (demod_ctx *)ctx;
288 while (i < demod->buf_len) {
289 demod->now_r += demod->buf[i];
290 demod->now_j += demod->buf[i+1];
293 if (demod->index < ((1000000 / 200000) + 1))
295 demod->buf[i2] = demod->now_r;
296 demod->buf[i2+1] = demod->now_j;
298 demod->now_r = demod->now_j = 0;
304 STATIC void _lowpassreal_demod (void *ctx) {
305 demod_ctx *demod = (demod_ctx *)ctx;
310 while (i < demod->res_len) {
311 demod->now_index += demod->res[i];
313 demod->pre_index += slow;
314 if (demod->pre_index < fast)
316 demod->res[i2] = (int16_t)(demod->now_index / (fast/slow));
317 demod->pre_index -= fast;
318 demod->now_index = 0;
324 STATIC void _multiply (int ar, int aj, int br, int bj, int *cr, int *cj) {
329 STATIC int _polar_discriminant (int ar, int aj, int br, int bj) {
332 _multiply(ar, aj, br, -bj, &cr, &cj);
333 angle = atan2((double)cj, (double)cr);
334 return (int)(angle / 3.14159 * (1<<14));
337 STATIC void _fm_demod (void *ctx) {
338 demod_ctx *demod = (demod_ctx *)ctx;
339 int16_t *buf = demod->buf;
340 int buf_len = demod->buf_len;
343 pcm = _polar_discriminant(buf[0], buf[1], demod->pre_r, demod->pre_j);
344 demod->res[0] = (int16_t)pcm;
346 for (i = 2; i < (buf_len-1); i += 2) {
347 pcm = _polar_discriminant(buf[i], buf[i+1], buf[i-2], buf[i-1]);
348 demod->res[i/2] = (int16_t)pcm;
350 demod->pre_r = buf[buf_len - 2];
351 demod->pre_j = buf[buf_len - 1];
352 demod->res_len = buf_len/2;
355 STATIC void _am_demod (void *ctx) {
356 demod_ctx *demod = (demod_ctx *)ctx;
357 int16_t *buf = demod->buf;
358 int buf_len = demod->buf_len;
361 for (i = 0; i < buf_len; i += 2) {
362 pcm = buf[i] * buf[i];
363 pcm += buf[i+1] * buf[i+1];
364 demod->res[i/2] = (int16_t)sqrt(pcm);
366 demod->res_len = buf_len/2;
369 STATIC void* _demod_thread_fn (void *ctx) {
370 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
371 demod_ctx *demod = dev_ctx->demod;
372 output_ctx *output = dev_ctx->output;
374 while(dev_ctx->should_run) {
375 pthread_wait(&demod->ok, &demod->ok_m);
376 pthread_rwlock_wrlock(&demod->lck);
377 _lowpass_demod(demod);
378 if (dev_ctx->mode == FM)
382 _lowpassreal_demod(demod);
383 pthread_rwlock_unlock(&demod->lck);
385 /* lock demod thread, write to it, unlock */
386 pthread_rwlock_wrlock(&output->lck);
387 memcpy(output->buf, demod->res, 2 * demod->res_len);
388 output->buf_len = demod->res_len;
389 pthread_rwlock_unlock(&output->lck);
390 pthread_signal(&output->ok, &output->ok_m);
393 demod->thr_finished = 1;
397 STATIC void* _output_thread_fn (void *ctx) {
398 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
399 output_ctx *output = dev_ctx->output;
402 file = fopen (AUDIO_BUFFER, "wb");
404 while (dev_ctx->should_run) {
405 pthread_wait(&output->ok, &output->ok_m);
406 pthread_rwlock_rdlock(&output->lck);
407 if (!dev_ctx->mute && file) {
408 fwrite (output->buf, 2, output->buf_len, file);
410 fseek (file, 0, SEEK_SET);
412 pthread_rwlock_unlock(&output->lck);
414 if (file) fclose(file);
415 unlink (AUDIO_BUFFER);
417 output->thr_finished = 1;