Skip to content

Commit

Permalink
separate the gqrx protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
neural75 committed Aug 1, 2017
1 parent 844b19a commit 1c520df
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 168 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -72,4 +72,5 @@ CTestTestfile.cmake

bin
build
gqrx-scan

13 changes: 12 additions & 1 deletion .vscode/tasks.json
Expand Up @@ -2,15 +2,26 @@
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"options": {
"cwd": "${workspaceRoot}/bin/Debug"
},

"tasks": [
{
"taskName": "Build",
"command": "gcc -g -o gqrx-scan gqrx-scan.c -lm",
"command": "make",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"taskName": "Clean",
"command": "make clean",
"type": "shell",
"group": "build",
"problemMatcher": []
}
]
}
2 changes: 1 addition & 1 deletion CMakeLists.txt
Expand Up @@ -8,6 +8,6 @@ set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE} CACHE PATH "Libr

# The following folder will be included
include_directories("${PROJECT_SOURCE_DIR}")
add_executable(gqrx-scan ${PROJECT_SOURCE_DIR}/gqrx-scan.c)
add_executable(gqrx-scan ${PROJECT_SOURCE_DIR}/gqrx-scan.c ${PROJECT_SOURCE_DIR}/gqrx-prot.c)
target_link_libraries(gqrx-scan m)
install (TARGETS gqrx-scan DESTINATION bin)
177 changes: 177 additions & 0 deletions gqrx-prot.c
@@ -0,0 +1,177 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pwd.h>
#include <stdbool.h>
#include <linux/limits.h>
#include <math.h>
#include "gqrx-prot.h"

//
// error - wrapper for perror
//
void error(char *msg) {
perror(msg);
exit(0);
}

//
// Connect
//
int Connect (char *hostname, int portno)
{
int sockfd, n;
struct sockaddr_in serveraddr;
struct hostent *server;


/* socket: create the socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");

/* gethostbyname: get the server's DNS entry */
server = gethostbyname(hostname);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host as %s\n", hostname);
exit(0);
}

/* build the server's Internet address */
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serveraddr.sin_addr.s_addr, server->h_length);
serveraddr.sin_port = htons(portno);

/* connect: create a connection with the server */
if (connect(sockfd, (const struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
error("ERROR connecting");

return sockfd;
}
//
// Send
//
bool Send(int sockfd, char *buf)
{
int n;

n = write(sockfd, buf, strlen(buf));
if (n < 0)
error("ERROR writing to socket");
return true;
}

//
// Recv
//
bool Recv(int sockfd, char *buf)
{
int n;

n = read(sockfd, buf, BUFSIZE);
if (n < 0)
error("ERROR reading from socket");
buf[n]= '\0';
return true;
}


//
// GQRX Protocol
//
bool GetCurrentFreq(int sockfd, long *freq)
{
char buf[BUFSIZE];

Send(sockfd, "f\n");
Recv(sockfd, buf);

if (strcmp(buf, "RPRT 1") == 0 )
return false;

sscanf(buf, "%ld", freq);
return true;
}
bool SetFreq(int sockfd, long freq)
{
char buf[BUFSIZE];

sprintf (buf, "F %ld\n", freq);
Send(sockfd, buf);
Recv(sockfd, buf);

if (strcmp(buf, "RPRT 1") == 0 )
return false;

long freq_current = 0;
do
{
GetCurrentFreq(sockfd, &freq_current);
} while (freq_current != freq);

return true;
}

bool GetSignalLevel(int sockfd, double *dBFS)
{
char buf[BUFSIZE];

Send(sockfd, "l\n");
Recv(sockfd, buf);

if (strcmp(buf, "RPRT 1") == 0 )
return false;

sscanf(buf, "%lf", dBFS);
*dBFS = round((*dBFS) * 10)/10;

if (*dBFS == 0.0)
return false;
return true;
}

bool GetSquelchLevel(int sockfd, double *dBFS)
{
char buf[BUFSIZE];

Send(sockfd, "l SQL\n");
Recv(sockfd, buf);

if (strcmp(buf, "RPRT 1") == 0 )
return false;

sscanf(buf, "%lf", dBFS);
*dBFS = round((*dBFS) * 10)/10;

return true;
}
//
// GetSignalLevelEx
// Get a bunch of sample with some delay and calculate the mean value
//
bool GetSignalLevelEx(int sockfd, double *dBFS, int n_samp)
{
double temp_level;
*dBFS = 0;
int errors = 0;
for (int i = 0; i < n_samp; i++)
{
if ( GetSignalLevel(sockfd, &temp_level) )
*dBFS = *dBFS + temp_level;
else
errors++;
usleep(1000);
}
*dBFS = *dBFS / (n_samp - errors);
return true;
}
43 changes: 43 additions & 0 deletions gqrx-prot.h
@@ -0,0 +1,43 @@
#ifndef _GQRX_PROT_H_
#define _GQRX_PROT_H_

#define BUFSIZE 1024
#define FREQ_MAX 4096
#define SAVED_FREQ_MAX 1000
#define TAG_MAX 100




//
// error - wrapper for perror
//
void error(char *msg);

//
// Connect
//
int Connect (char *hostname, int portno);

//
// Send
//
bool Send(int sockfd, char *buf);

//
// Recv
//
bool Recv(int sockfd, char *buf);

//
// GQRX Protocol
//
bool GetCurrentFreq(int sockfd, long *freq);
bool SetFreq(int sockfd, long freq);
bool GetSignalLevel(int sockfd, double *dBFS);
bool GetSquelchLevel(int sockfd, double *dBFS);
bool GetSignalLevelEx(int sockfd, double *dBFS, int n_samp);



#endif /* _GQRX_PROT_H_ */

0 comments on commit 1c520df

Please sign in to comment.