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/>.
20 #include "local-def.h"
22 /* -------------- RADIO DEFINITIONS ------------------ */
28 #define pthread_signal(n, m) pthread_mutex_lock(m); pthread_cond_signal(n); pthread_mutex_unlock(m)
29 #define pthread_wait(n, m) pthread_mutex_lock(m); pthread_cond_wait(n, m); pthread_mutex_unlock(m)
30 #define BUF_LEN 16*16384
32 typedef enum { FM, AM } Mode;
33 typedef struct dongle_ctx dongle_ctx;
34 typedef struct demod_ctx demod_ctx;
35 typedef struct output_ctx output_ctx;
39 unsigned char thr_finished;
40 uint16_t buf[BUF_LEN];
46 unsigned char thr_finished;
50 int pre_r, pre_j, now_r, now_j, index;
51 int pre_index, now_index;
60 unsigned char thr_finished;
73 unsigned char should_run;
81 void* _dongle_thread_fn (void *);
82 void* _demod_thread_fn (void *);
83 void* _output_thread_fn (void *);
84 unsigned int _radio_dev_count (void);
85 const char* _radio_dev_name (unsigned int);
86 unsigned char _radio_dev_init (struct dev_ctx *, unsigned int);
87 unsigned char _radio_dev_free (struct dev_ctx *);
88 void _radio_apply_params (struct dev_ctx *);
89 void _radio_start_threads (struct dev_ctx *);
90 void _radio_stop_threads (struct dev_ctx *);
92 static unsigned int init_dev_count;
93 static struct dev_ctx **dev_ctx;
95 /* ------------- RADIO IMPLEMENTATION ----------------- */
97 /* ---- PUBLIC FUNCTIONS --- */
100 init_dev_count = _radio_dev_count();
103 dev_ctx = (struct dev_ctx**) malloc(init_dev_count * sizeof(struct dev_ctx));
105 for (i = 0; i < init_dev_count; i++) {
106 dev_ctx[i] = (struct dev_ctx*) malloc(sizeof(struct dev_ctx));
107 dev_ctx[i]->dev = NULL;
108 dev_ctx[i]->mode = FM;
109 dev_ctx[i]->freq = 100.0;
110 dev_ctx[i]->mute = 0;
111 dev_ctx[i]->should_run = 0;
112 dev_ctx[i]->dongle = NULL;
113 dev_ctx[i]->demod = NULL;
114 dev_ctx[i]->output = NULL;
115 _radio_dev_init(dev_ctx[i], i);
122 for (i = 0; i < init_dev_count; i++) {
123 _radio_dev_free(dev_ctx[i]);
129 void radio_set_mode (struct dev_ctx *dev_ctx, Mode mode) {
130 dev_ctx->mode = mode;
131 _radio_apply_params(dev_ctx);
134 void radio_set_freq (struct dev_ctx *dev_ctx, float freq) {
135 dev_ctx->freq = freq;
136 _radio_apply_params(dev_ctx);
139 void radio_set_mute (struct dev_ctx *dev_ctx, unsigned char mute) {
140 dev_ctx->mute = mute;
141 _radio_apply_params(dev_ctx);
144 void radio_play (struct dev_ctx *dev_ctx) {
145 _radio_start_threads(dev_ctx);
148 void radio_stop (struct dev_ctx *dev_ctx) {
149 _radio_stop_threads(dev_ctx);
152 /* --- HELPER FUNCTIONS --- */
154 unsigned int _radio_dev_count () {
155 return rtlsdr_get_device_count();
158 const char* _radio_dev_name (unsigned int num) {
159 return rtlsdr_get_device_name(num);
162 unsigned char _radio_dev_init (struct dev_ctx *dev_ctx, unsigned int num) {
163 rtlsdr_dev_t *dev = dev_ctx->dev;
165 if (rtlsdr_open(&dev, num) < 0)
168 rtlsdr_set_tuner_gain_mode(dev, 0);
170 if (rtlsdr_reset_buffer(dev) < 0)
173 // dev_ctx->dev = dev; REQUIRED IN C TOO ? TEST !
175 _radio_apply_params(dev_ctx);
180 unsigned char _radio_dev_free (struct dev_ctx *dev_ctx) {
181 rtlsdr_dev_t *dev = dev_ctx->dev;
183 if (rtlsdr_close(dev) < 0)
190 void _radio_apply_params (struct dev_ctx *dev_ctx) {
191 rtlsdr_dev_t *dev = dev_ctx->dev;
192 Mode mode = dev_ctx->mode;
193 float freq = dev_ctx->freq;
197 rate = ((1000000 / 200000) + 1) * 200000;
203 rtlsdr_set_center_freq(dev, freq);
204 rtlsdr_set_sample_rate(dev, rate);
206 // dev_ctx->dev = dev; REQUIRED IN C TOO ? TEST !
209 void _radio_start_threads (struct dev_ctx *dev_ctx) {
210 rtlsdr_dev_t *dev = dev_ctx->dev;
211 dev_ctx->dongle = (dongle_ctx*) malloc(sizeof(dongle_ctx));
212 dev_ctx->demod = (demod_ctx*) malloc(sizeof(demod_ctx));
213 dev_ctx->output = (output_ctx*) malloc(sizeof(output_ctx));
215 dongle_ctx *dongle = dev_ctx->dongle;
216 demod_ctx *demod = dev_ctx->demod;
217 output_ctx *output = dev_ctx->output;
219 pthread_rwlock_init(&demod->lck, NULL);
220 pthread_cond_init(&demod->ok, NULL);
221 pthread_mutex_init(&demod->ok_m, NULL);
222 pthread_rwlock_init(&output->lck, NULL);
223 pthread_cond_init(&output->ok, NULL);
224 pthread_mutex_init(&output->ok_m, NULL);
226 dev_ctx->should_run = 1;
229 dongle->thr_finished = 0;
230 pthread_create(&dongle->thr, NULL, _dongle_thread_fn, (void*)dev_ctx);
233 demod->pre_r = demod->pre_j = 0;
234 demod->now_r = demod->now_j = 0;
235 demod->index = demod->pre_index = demod->now_index = 0;
236 demod->thr_finished = 0;
237 pthread_create(&demod->thr, NULL, _demod_thread_fn, (void*)dev_ctx);
240 output->thr_finished = 0;
241 pthread_create(&output->thr, NULL, _output_thread_fn, (void*)dev_ctx);
244 void _radio_stop_threads (struct dev_ctx *dev_ctx) {
245 rtlsdr_dev_t *dev = dev_ctx->dev;
246 dongle_ctx *dongle = dev_ctx->dongle;
247 demod_ctx *demod = dev_ctx->demod;
248 output_ctx *output = dev_ctx->output;
250 if (!dongle || !demod || !output)
253 /* stop each "while" loop in threads */
254 dev_ctx->should_run = 0;
256 rtlsdr_cancel_async(dev);
257 pthread_signal(&demod->ok, &demod->ok_m);
258 pthread_signal(&output->ok, &output->ok_m);
260 while (!dongle->thr_finished ||
261 !demod->thr_finished ||
262 !output->thr_finished)
265 pthread_join(dongle->thr, NULL);
266 pthread_join(demod->thr, NULL);
267 pthread_join(output->thr, NULL);
268 pthread_rwlock_destroy(&demod->lck);
269 pthread_cond_destroy(&demod->ok);
270 pthread_mutex_destroy(&demod->ok_m);
271 pthread_rwlock_destroy(&output->lck);
272 pthread_cond_destroy(&output->ok);
273 pthread_mutex_destroy(&output->ok_m);
275 free(dongle); dev_ctx->dongle = NULL;
276 free(demod); dev_ctx->demod = NULL;
277 free(output); dev_ctx->output = NULL;
280 /* ---- LOCAL THREADED FUNCTIONS ---- */
282 static void _rtlsdr_callback (unsigned char *buf, uint32_t len, void *ctx) {
283 struct dev_ctx *dev_ctx = (struct dev_ctx *)ctx;
284 dongle_ctx *dongle = dev_ctx->dongle;
285 demod_ctx *demod = dev_ctx->demod;
289 if (!dev_ctx->should_run)
293 for (i = 0; i < (int)len; i += 8) {
294 tmp = 255 - buf[i+3];
298 buf[i+4] = 255 - buf[i+4];
299 buf[i+5] = 255 - buf[i+5];
301 tmp = 255 - buf[i+6];
307 for (i = 0; i < (int)len; i++)
308 dongle->buf[i] = (int16_t)buf[i] - 127;
310 /* lock demod thread, write to it, unlock */
311 pthread_rwlock_wrlock(&demod->lck);
312 memcpy(demod->buf, dongle->buf, 2 * len);
313 demod->buf_len = len;
314 pthread_rwlock_unlock(&demod->lck);
315 pthread_signal(&demod->ok, &demod->ok_m);
318 void* _dongle_thread_fn (void *ctx) {
319 struct dev_ctx *dev_ctx = (struct dev_ctx *)ctx;
320 struct dongle_ctx *dongle = dev_ctx->dongle;
322 rtlsdr_read_async(dev_ctx->dev, _rtlsdr_callback, dev_ctx, 0, 0);
324 dongle->thr_finished = 1;
328 void _lowpass_demod (void *ctx) {
329 demod_ctx *demod = (demod_ctx *)ctx;
332 while (i < demod->buf_len) {
333 demod->now_r += demod->buf[i];
334 demod->now_j += demod->buf[i+1];
337 if (demod->index < ((1000000 / 200000) + 1))
339 demod->buf[i2] = demod->now_r;
340 demod->buf[i2+1] = demod->now_j;
342 demod->now_r = demod->now_j = 0;
348 void _lowpassreal_demod (void *ctx) {
349 demod_ctx *demod = (demod_ctx *)ctx;
354 while (i < demod->res_len) {
355 demod->now_index += demod->res[i];
357 demod->pre_index += slow;
358 if (demod->pre_index < fast)
360 demod->res[i2] = (int16_t)(demod->now_index / (fast/slow));
361 demod->pre_index -= fast;
362 demod->now_index = 0;
368 void _multiply (int ar, int aj, int br, int bj, int *cr, int *cj) {
373 int _polar_discriminant (int ar, int aj, int br, int bj) {
376 _multiply(ar, aj, br, -bj, &cr, &cj);
377 angle = atan2((double)cj, (double)cr);
378 return (int)(angle / 3.14159 * (1<<14));
381 void _fm_demod (void *ctx) {
382 demod_ctx *demod = (demod_ctx *)ctx;
383 int16_t *buf = demod->buf;
384 int buf_len = demod->buf_len;
387 pcm = _polar_discriminant(buf[0], buf[1], demod->pre_r, demod->pre_j);
388 demod->res[0] = (int16_t)pcm;
390 for (i = 2; i < (buf_len-1); i += 2) {
391 pcm = _polar_discriminant(buf[i], buf[i+1], buf[i-2], buf[i-1]);
392 demod->res[i/2] = (int16_t)pcm;
394 demod->pre_r = buf[buf_len - 2];
395 demod->pre_j = buf[buf_len - 1];
396 demod->res_len = buf_len/2;
399 void _am_demod (void *ctx) {
400 demod_ctx *demod = (demod_ctx *)ctx;
401 int16_t *buf = demod->buf;
402 int buf_len = demod->buf_len;
405 for (i = 0; i < buf_len; i += 2) {
406 pcm = buf[i] * buf[i];
407 pcm += buf[i+1] * buf[i+1];
408 demod->res[i/2] = (int16_t)sqrt(pcm);
410 demod->res_len = buf_len/2;
413 void* _demod_thread_fn (void *ctx) {
414 struct dev_ctx *dev_ctx = (struct dev_ctx *)ctx;
415 demod_ctx *demod = dev_ctx->demod;
416 output_ctx *output = dev_ctx->output;
418 while(dev_ctx->should_run) {
419 pthread_wait(&demod->ok, &demod->ok_m);
420 pthread_rwlock_wrlock(&demod->lck);
421 _lowpass_demod(demod);
422 if (dev_ctx->mode == FM)
426 _lowpassreal_demod(demod);
427 pthread_rwlock_unlock(&demod->lck);
429 /* lock demod thread, write to it, unlock */
430 pthread_rwlock_wrlock(&output->lck);
431 memcpy(output->buf, demod->res, 2 * demod->res_len);
432 output->buf_len = demod->res_len;
433 pthread_rwlock_unlock(&output->lck);
434 pthread_signal(&output->ok, &output->ok_m);
437 demod->thr_finished = 1;
441 void* _output_thread_fn (void *ctx) {
442 struct dev_ctx *dev_ctx = (struct dev_ctx *)ctx;
443 output_ctx *output = dev_ctx->output;
445 while (dev_ctx->should_run) {
446 pthread_wait(&output->ok, &output->ok_m);
447 pthread_rwlock_rdlock(&output->lck);
448 //if (!dev_ctx->mute)
449 // mRadio->PlayAlsa((void*)&output->buf, output->buf_len);
450 pthread_rwlock_unlock(&output->lck);
453 output->thr_finished = 1;
457 /* -------------- PLUGIN BINDING ------------------- */
459 STATIC json_object* start (AFB_session *session, AFB_request *request, void* handle) {
460 json_object *response;
463 // request all query key/value
464 getQueryAll (request, query, sizeof(query));
466 // check if we have some post data
467 if (request->post == NULL) request->post="NoData";
469 // return response to caller
470 response = jsonNewMessage(AFB_SUCCESS, "Start Radio plugin query={%s} PostData: \'%s\' ", query, request->post);
472 //if (verbose) fprintf(stderr, "%d: \n", pingcount);
476 STATIC json_object* stop (AFB_session *session, AFB_request *request, void* handle) {
477 json_object *response;
480 getQueryAll (request, query, sizeof(query));
482 if (request->post == NULL) request->post="NoData";
484 response = jsonNewMessage(AFB_SUCCESS, "Stop Radio plugin query={%s} PostData: \'%s\' ", query, request->post);
495 STATIC AFB_restapi pluginApis[]= {
496 {"start" , (AFB_apiCB)start , "Ping Application Framework"},
497 {"stop" , (AFB_apiCB)stop , "Ping Application Framework"},
501 PUBLIC AFB_plugin *radioRegister (AFB_session *session) {
502 AFB_plugin *plugin = malloc (sizeof (AFB_plugin));
503 plugin->type = AFB_PLUGIN_JSON;
504 plugin->info = "Application Framework Binder - Radio plugin";
505 plugin->prefix = "radio";
506 plugin->apis = pluginApis;