This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
edacs-lcn.c
162 lines (142 loc) · 4.98 KB
/
edacs-lcn.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
/*-------------------------------------------------------------------------------
* EDACS-LCN
* A program for decoding edacs dotting sequence detection
*
* ASCII art generated by:
* https://fsymbols.com/generators/carty/
*
* Portions of this software originally from:
* https://github.com/sp5wwp/ledacs
* XTAL Labs
* 30 IV 2016
* Many thanks to SP5WWP for permission to use and modify this software
*
* LWVMOBILE
* 2022-02 Version EDACS-LCN Florida Man Edition
*-----------------------------------------------------------------------------*/
#define _GNU_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
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
unsigned long long vr=0;
int handle; //for UDP
unsigned short port = UDP_PORT; //
char data[UDP_BUFLEN]={0}; //
struct sockaddr_in address; //
short int debug = 0;
char * FM_banner[7] = {
" ",
" ███████╗██████╗ █████╗ ██████╗███████╗ ██╗ ██████╗███╗ ██╗",
" ██╔════╝██╔══██╗██╔══██╗██╔════╝██╔════╝ ██║ ██╔════╝████╗ ██║",
" █████╗ ██║ ██║███████║██║ ███████╗ ██ ██║ ██║ ██╔██╗ ██║",
" ██╔══╝ ██║ ██║██╔══██║██║ ╚════██║ ██║ ██║ ██║╚██╗██║",
" ███████╗██████╔╝██║ ██║╚██████╗███████║ ███████╗╚██████╗██║ ╚████║",
" ╚══════╝╚═════╝ ╚═╝ ╚═╝ ╚═════╝╚══════╝ ╚══════╝ ╚═════╝╚═╝ ╚═══╝"
};
//--------------------------------------------
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
init_udp();
sleep(2);
for(short int i=0; i<7; i++)
{
printf("%s \n", FM_banner[i]);
}
for(int i=0; i<SAMP_NUM/2/3-1; i++) //zero array
{
avg_arr[i]=0;
}
squelchSet(5000);
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;
}
//--------------------------------------
//vr=sr;
sr=sr<<1;
//vr=vr<
if (avg<AFC)
sr|=1;
if (sr > 0 && debug > 0){
printf("SR = %16llX\n", sr);
}
if ( (sr&0xFFFFFFFFFFFFFFFF)==0xAAAAAAAAAAAAAAAA ) //dotting sequence
{
//squelchSet(5000);
sr = 0; //zero out sr so it doesn't keep tripping over itself
raw_stream[0] = 0; //zero out everything else too
raw_stream[1] = 0;
raw_stream[2] = 0;
//usleep(5*1000); //wait so it doesn't keep squelching, disabling due to delay causing new voice grant to hang up immediately.
}
else /* */
{
}
}
return 0;
}