forked from charlie-foxtrot/RTLSDR-Airband
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input-common.cpp
133 lines (122 loc) · 3.34 KB
/
input-common.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* input-common.cpp
* common input handling routines
*
* Copyright (c) 2015-2020 Tomasz Lemiech <szpajder@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef __MINGW32__
#define _GNU_SOURCE 1 // asprintf
#endif
#include <iostream>
#include <assert.h>
#include <dlfcn.h> // dlopen, dlsym
#include <errno.h>
#include <pthread.h>
#include <stdio.h> // asprintf
#include <stdlib.h> // free
#include <string.h>
#include "input-common.h"
using namespace std;
typedef input_t *(*input_new_func_t)(void);
input_t *input_new(char const * const type) {
assert(type != NULL);
void *dlhandle = dlopen(NULL, RTLD_NOW);
assert(dlhandle != NULL);
char *fname = NULL;
assert(asprintf(&fname, "%s_input_new", type) > 0);
input_new_func_t fptr = (input_new_func_t)dlsym(dlhandle, fname);
free(fname);
if(fptr == NULL) {
return NULL;
}
input_t *input = (*fptr)();
assert(input->init != NULL);
assert(input->run_rx_thread != NULL);
assert(input->set_centerfreq != NULL);
return input;
}
int input_init(input_t * const input) {
assert(input != NULL);
input_state_t new_state = INPUT_FAILED; // fail-safe default
errno = 0;
int ret = input->init(input);
if(ret < 0) {
ret = -1;
} else if((ret = pthread_mutex_init(&input->buffer_lock, NULL)) != 0) {
errno = ret;
ret = -1;
} else {
new_state = INPUT_INITIALIZED;
ret = 0;
}
input->state = new_state;
return ret;
}
int input_start(input_t * const input) {
assert(input != NULL);
assert(input->dev_data != NULL);
assert(input->state == INPUT_INITIALIZED);
int err = pthread_create(&input->rx_thread, NULL, input->run_rx_thread, (void *)input);
if(err != 0) {
errno = err;
return -1;
}
return 0;
}
int input_parse_config(input_t * const input, libconfig::Setting &cfg) {
assert(input != NULL);
if(input->parse_config != NULL) {
return input->parse_config(input, cfg);
} else {
// Very simple inputs (like stdin) might not necessarily have any configuration
// variables, so it's legal not to have parse_config defined.
return 0;
}
}
int input_stop(input_t * const input) {
assert(input != NULL);
assert(input->dev_data != NULL);
int err = 0;
errno = 0;
if(input->state == INPUT_RUNNING && input->stop != NULL) {
err = input->stop(input);
if(err != 0) {
input->state = INPUT_FAILED;
return -1;
}
}
input->state = INPUT_STOPPED;
err = pthread_join(input->rx_thread, NULL);
if(err != 0) {
errno = err;
return -1;
}
return 0;
}
int input_set_centerfreq(input_t * const input, int const centerfreq) {
assert(input != NULL);
assert(input->dev_data != NULL);
if(input->state != INPUT_RUNNING) {
return -1;
}
int ret = input->set_centerfreq(input, centerfreq);
if(ret != 0) {
input->state = INPUT_FAILED;
return -1;
}
input->centerfreq = centerfreq;
return 0;
}