forked from csete/fcdctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
281 lines (248 loc) · 7.42 KB
/
main.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Funcube Dongle command line interface
* Copyright 2011 David Pello EA1IDZ
* Copyright 2011 Pieter-Tjerk de Boer PA3FWM
* Copyright 2012-2014 Alexandru Csete OZ9AEC
*
* This code is licensed under a GNU GPL licensed
* See LICENSE for information
*
*/
#ifdef FCDPP
#define PROGRAM_VERSION "0.4.4-fcdpp"
#else
#define PROGRAM_VERSION "0.4.4"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <stdint.h>
#include "fcd.h"
#include "fcdhidcmd.h"
// based on the #defines for TLGE_P10_0DB etc. from fcdhidcmd.h :
double lnagainvalues[]={-5.0,-2.5,-999,-999,0,2.5,5,7.5,10,12.5,15,17.5,20,25,30};
#include "hidapi.h"
extern const unsigned short _usVID;
extern const unsigned short _usPID;
extern int whichdongle;
void print_list(void)
{
// based on code from fcd.c
struct hid_device_info *phdi=NULL;
//hid_device *phd=NULL;
//char *pszPath=NULL;
// look for all FCDs
phdi=hid_enumerate(_usVID,_usPID);
if (phdi==NULL)
{
puts("No FCD found.\n");
return;
}
puts(" nr USB path firmware frequency LNA gain audio device");
// 0 0004:0006:02 18.09 1234.567890 MHz +20 dB card2
// iterate over all FCDs found
int idx=0;
while (phdi) {
whichdongle=idx;
printf(" %-3i %-12s ",idx, phdi->path);
int stat = fcdGetMode();
if (stat == FCD_MODE_NONE) printf("No FCD Detected.\n");
else if (stat == FCD_MODE_BL) printf("In bootloader mode.\n");
else {
uint8_t lnagain;
uint8_t freq[4];
char version[20];
// read version, frequency, gain
fcdGetFwVerStr(version);
fcdAppGetParam(FCD_CMD_APP_GET_FREQ_HZ,freq,4);
fcdAppGetParam(FCD_CMD_APP_GET_LNA_GAIN,&lnagain,1);
// try to find the corresponding audio device, by comparing the USB path to USB paths found under /proc/asound
char audiopath[16]="(not found)";
int usb1=-1,usb2=-1;
sscanf(phdi->path,"%i:%i",&usb1,&usb2);
int i;
for (i=0;i<16;i++) {
char s[32];
sprintf(s,"/proc/asound/card%i/usbbus",i);
FILE *f;
f=fopen(s,"r");
if (f) {
fgets(s,32,f);
int u1=0,u2=0;
sscanf(s,"%i/%i",&u1,&u2);
fclose(f);
if (u1==usb1 && u2==usb2) { sprintf(audiopath,"card%i",i); break; }
}
}
// print our findings
#ifdef FCDPP
printf(" %-8s %11.6f MHz %s %s\n", version, (*(int *)freq)/1e6, lnagain ? "enabled" : "disabled", audiopath);
#else
printf(" %-8s %11.6f MHz %4g dB %s\n", version, (*(int *)freq)/1e6, lnagainvalues[lnagain], audiopath);
#endif
}
idx++;
phdi = phdi->next;
}
hid_free_enumeration(phdi);
}
const char* program_name;
void print_help()
{
printf("\n");
printf("This is fcdctl version %s\n", PROGRAM_VERSION);
printf("\n");
printf("Usage: %s options [arguments]\n", program_name);
printf(" -l, --list List all FCDs in the system\n");
printf(" -s, --status Gets FCD current status\n");
printf(" -f, --frequency <freq> Sets FCD frequency in MHz\n");
#ifdef FCDPP
printf(" -g, --gain <gain> Enable/disable LNA gain (0 or 1)\n");
#else
printf(" -g, --gain <gain> Sets LNA gain in dB\n");
printf(" -c, --correction <cor> Sets frequency correction in ppm\n");
#endif
printf(" -i, --index <index> Which dongle to show/set (default: 0)\n");
printf(" -h, --help Shows this help\n");
}
void print_status()
{
int stat;
char version[20];
stat = fcdGetMode();
if (stat == FCD_MODE_NONE)
{
printf("No FCD Detected.\n");
return;
}
else if (stat == FCD_MODE_BL)
{
printf("FCD present in bootloader mode.\n");
return;
}
else
{
printf("FCD present in application mode.\n");
stat = fcdGetFwVerStr(version);
printf("FCD firmware version: %s.\n", version);
unsigned char b[8];
stat = fcdAppGetParam(FCD_CMD_APP_GET_FREQ_HZ,b,8);
printf("FCD frequency: %.6f MHz.\n", (*(int *)b)/1e6);
stat = fcdAppGetParam(FCD_CMD_APP_GET_LNA_GAIN,b,1);
#ifdef FCDPP
printf("FCD LNA gain: %s.\n", b[0] == 1 ? "enabled" : "disabled");
#else
printf("FCD LNA gain: %g dB.\n", lnagainvalues[b[0]]);
#endif
return;
}
}
int main(int argc, char* argv[])
{
int stat;
int freq = 0;
double freqf = 0;
int gain = -999;
int corr = 0;
int dolist = 0;
int dostatus = 0;
/* getopt infrastructure */
int next_option;
const char* const short_options = "slg:f:c:i:h";
const struct option long_options[] =
{
{ "status", 0, NULL, 's' },
{ "list", 0, NULL, 'l' },
{ "frequency", 1, NULL, 'f' },
{ "index", 1, NULL, 'i' },
{ "gain", 1, NULL, 'g' },
{ "correction", 1, NULL, 'c' },
{ "help", 0, NULL, 'h' }
};
/* save program name */
program_name = argv[0];
if (argc == 1)
{
print_help();
exit(EXIT_SUCCESS);
}
while(1)
{
/* call getopt */
next_option = getopt_long(argc, argv, short_options, long_options, NULL);
/* end of the options */
if (next_option == -1)
break;
switch (next_option)
{
case 'h' :
print_help();
exit(EXIT_SUCCESS);
case 's' :
dostatus=1;
break;
case 'l' :
dolist=1;
break;
case 'f' :
freqf = atof(optarg);
break;
case 'g' :
gain = atoi(optarg);
break;
case 'i' :
whichdongle = atoi(optarg);
break;
case 'c' :
corr = atoi(optarg);
break;
case '?' :
print_help();
exit(1);
default :
abort();
}
}
if (freqf>0) {
/* MHz -> Hz */
freq = (int)(freqf * 1.0e6f);
/* calculate frequency */
freq *= 1.0 + corr / 1000000.0;
/* set it */
stat = fcdAppSetFreq(freq);
if (stat == FCD_MODE_NONE)
{
printf("No FCD Detected.\n");
return 1;
}
else if (stat == FCD_MODE_BL)
{
printf("FCD in bootloader mode.\n");
return 1;
}
else
{
printf("Freq set to %.6f MHz.\n", freq/1e6);
}
}
if (gain>-999) {
unsigned char b=0;
#ifdef FCDPP
b = gain ? 1 : 0;
#else
while (b<sizeof(lnagainvalues)/sizeof(lnagainvalues[0]) && gain>lnagainvalues[b]+1) b++;
#endif
stat = fcdAppSetParam(FCD_CMD_APP_SET_LNA_GAIN,&b,1);
if (stat == FCD_MODE_NONE) { printf("No FCD Detected.\n"); return 1; }
else if (stat == FCD_MODE_BL) { printf("FCD in bootloader mode.\n"); return 1; }
#ifdef FCDPP
else printf("LNA gain %s.\n", b ? "enabled" : "disabled");
#else
else printf("LNA gain set to %g dB.\n",lnagainvalues[b]);
#endif
}
if (dolist) print_list();
if (dostatus) print_status();
return EXIT_SUCCESS;
}