This repository has been archived by the owner on Jan 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdot-detector.c
166 lines (135 loc) · 4.16 KB
/
dot-detector.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
/*-------------------------------------------------------------------------------
* Dotting sequence detector
*
*
*
*
*
* XTAL Labs
* 30 IV 2016
* LWVMOBILE - Tweaks
* 2020-10 Version 0.3b ledacs-esk
*-----------------------------------------------------------------------------*/
#define _DEFAULT_SOURCE //_BSD_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <errno.h>
#define UDP_BUFLEN 5 //maximum UDP buffer length
#define SRV_IP "127.0.0.1" //IP
#define UDP_PORT 6020 //UDP port
#define SAMP_NUM 1000*3*2
#define DOTTING 0xAAAAAA //C71C71
#define D_MASK 0xFFFFFF
unsigned char samples[SAMP_NUM]; //8-bit samples from rtl_fm (or rtl_udp)
signed short int raw_stream[SAMP_NUM/2]; //16-bit signed int samples
signed int AFC=0; //Auto Frequency Control -> DC offset
signed int min=SHRT_MAX, max=SHRT_MIN; //min and max sample values
unsigned int avg_cnt=0; //avg array index variable
signed short int avg_arr[SAMP_NUM/2/3];
unsigned long long sr=0; //shift register for pushing decoded binary data
int handle; //for UDP
unsigned short port = UDP_PORT; //
char data[UDP_BUFLEN]={0}; //
struct sockaddr_in address; //
//--------------------------------------------
int init_udp() //UDP init
{
handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (handle <= 0)
{
printf("Failed to create socket\n");
return 1;
}
printf("Sockets successfully initialized\n");
memset((char *) &address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(SRV_IP); //address of host
address.sin_port = htons(port);
return 0;
}
//--------------------------------
void squelchSet(unsigned long long int sq) //squelch
{
data[0]=2;
data[1]=sq&0xFF;
data[2]=(sq>>8)&0xFF;
data[3]=(sq>>16)&0xFF;
data[4]=(sq>>24)&0xFF;
sendto(handle, data, UDP_BUFLEN, 0, (const struct sockaddr*) &address, sizeof(struct sockaddr_in));
}
int main(void)
{
signed int avg=0; //sample average
FILE *fp; int fread;
init_udp();
sleep(2);
fp = fopen("/tmp/squelch", "w"); //squelch init
fputc('1', fp);
fclose(fp);
//AFC=getAFC(SAMP_NUM);
for(int i=0; i<SAMP_NUM/2/3-1; i++) //zero array
{
avg_arr[i]=0;
}
while(1)
{
read(0, samples, 3*2); //read samples
raw_stream[0]=(signed short int)((samples[0+1]<<8)|(samples[0]&0xFF));
raw_stream[1]=(signed short int)((samples[2+1]<<8)|(samples[2]&0xFF));
raw_stream[2]=(signed short int)((samples[4+1]<<8)|(samples[4]&0xFF));
avg=(raw_stream[0]+raw_stream[1]+raw_stream[2])/3;
//AFC recomputing using averaged samples
avg_arr[avg_cnt]=avg;
avg_cnt++;
if (avg_cnt>=SAMP_NUM/2/3-1) //reset after filling avg_array
{
avg_cnt=0;
min=SHRT_MAX;
max=SHRT_MIN;
for(int i=0; i<SAMP_NUM/2/3-1; i++) //simple min/max detector
{
if (avg_arr[i]>max)
max=avg_arr[i];
if (avg_arr[i]<min)
min=avg_arr[i];
}
AFC=(min+max)/2;
//if(AFC!=0)printf("AFC=%d\n",AFC);
}
//--------------------------------------
sr=sr<<1;
if (avg<AFC)
sr|=1;
if ((sr&0xFFFFFF)==0xC71C71 || (sr&0xFFFFFFFFFFFFFFFF)==0xAAAAAAAAAAAAAAAA || (sr&0xFFFFFFFFFFFFFFFF)==0x5555555555555555)
{
fp = fopen("/tmp/squelch", "r+");
fread=fgetc(fp);
if(fread=='0')
{
squelchSet(5000);
usleep(200*1000);
fp = freopen("/tmp/squelch", "w", fp);
fputc('1', fp);
printf("OFF=%d\n", AFC);
}
fclose(fp);
}
else /* ((sr&0xFFFFFF)==0xC71C71 || (sr&0xFFFFFF)==0xAAAAAA || (sr&0xFFFFFF)==0x555555){ */
{
//if (sr != 0xFFFFFFFFFFFFFFFF && sr != 0xAAAAAAAAAAAAAAAA && sr != 0x5555555555555555 && (sr&0xFFFFFF) != 0xC71C71 && sr&0xF000000000000000 != 0)
if (sr != 0xFFFFFFFFFFFFFFFF && sr != 0xAAAAAAAAAAAAAAAA && sr != 0x5555555555555555 && (sr&0xFFFFFF) != 0xC71C71 && sr&0xF000000000000000 != 0)
{
//printf("%016llX\n", sr); //print voice bits only after factoring out dotting sequence and register shifting
}
}
}
return 0;
}