Skip to content

Commit

Permalink
Merge pull request #168 from dl1ycf/PR
Browse files Browse the repository at this point in the history
My next bunch of updates as a pull request.
  • Loading branch information
g0orx committed Jan 25, 2022
2 parents 360f511 + 464135d commit 7ad6218
Show file tree
Hide file tree
Showing 15 changed files with 330 additions and 597 deletions.
26 changes: 17 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ STEMLAB_OBJS=stemlab_discovery.o
endif

ifeq ($(SERVER_INCLUDE), SERVER)
SERVER_OPTIONS=-D SERVER
SERVER_OPTIONS=-D CLIENT_SERVER
SERVER_SOURCES= \
hpsdr_server.c
client_server.c server_menu.c
SERVER_HEADERS= \
hpsdr_server.h
client_server.h server_menu.h
SERVER_OBJS= \
hpsdr_server.o
client_server.o server_menu.o
endif

GTKINCLUDES=`pkg-config --cflags gtk+-3.0`
Expand All @@ -184,8 +184,8 @@ AUDIO_SOURCES=pulseaudio.c
AUDIO_OBJS=pulseaudio.o
endif
ifeq ($(UNAME_S), Darwin)
AUDIO_OPTIONS=-DPORTAUDIO
AUDIO_LIBS=-lportaudio
AUDIO_OPTIONS=-DPORTAUDIO `pkg-config --cflags portaudio-2.0`
AUDIO_LIBS=`pkg-config --libs portaudio-2.0`
AUDIO_SOURCES=portaudio.c
AUDIO_OBJS=portaudio.o
endif
Expand Down Expand Up @@ -479,6 +479,9 @@ prebuild:
# in the variable CPPOPTIONS
#
CPPOPTIONS= --enable=all --suppress=shadowVariable --suppress=variableScope
ifeq ($(UNAME_S), Darwin)
CPPOPTIONS += -D__APPLE__
endif
CPPINCLUDES:=$(shell echo $(INCLUDES) | sed -e "s/-pthread / /" )

.PHONY: cppcheck
Expand All @@ -493,9 +496,14 @@ clean:
-rm -f *.o
-rm -f $(PROGRAM) $(PROGRAM).app hpsdrsim

#
# If $DESTDIR is set, copy to that directory, otherwise use /usr/local/bin
#
DESTDIR?= /usr/local/bin

.PHONY: install
install: $(PROGRAM)
cp $(PROGRAM) /usr/local/bin
cp $(PROGRAM) $(DESTDIR)

.PHONY: release
release: $(PROGRAM)
Expand Down Expand Up @@ -534,13 +542,13 @@ controller2v2: clean $(PROGRAM)
#############################################################################

hpsdrsim.o: hpsdrsim.c hpsdrsim.h
$(CC) -c -O -DALSASOUND hpsdrsim.c
$(CC) -c -O hpsdrsim.c

newhpsdrsim.o: newhpsdrsim.c hpsdrsim.h
$(CC) -c -O newhpsdrsim.c

hpsdrsim: hpsdrsim.o newhpsdrsim.o
$(LINK) -o hpsdrsim hpsdrsim.o newhpsdrsim.o -lasound -lm -lpthread
$(LINK) -o hpsdrsim hpsdrsim.o newhpsdrsim.o -lm -lpthread


debian:
Expand Down
97 changes: 67 additions & 30 deletions client_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ void remote_audio(RECEIVER *rx,short left_sample,short right_sample) {
static gint send_spectrum(void *arg) {
REMOTE_CLIENT *client=(REMOTE_CLIENT *)arg;
float *samples;
double sample;
short s;
SPECTRUM_DATA spectrum_data;
gint result;
Expand Down Expand Up @@ -221,8 +220,7 @@ g_print("send_spectrum: no more receivers\n");
spectrum_data.vfo_b_ctun_freq=htonll(vfo[VFO_B].ctun_frequency);
spectrum_data.vfo_a_offset=htonll(vfo[VFO_A].offset);
spectrum_data.vfo_b_offset=htonll(vfo[VFO_B].offset);
s=(short)receiver[r]->meter;
spectrum_data.meter=htons(s);
spectrum_data.meter=htond(receiver[r]->meter);
spectrum_data.samples=htons(rx->width);
samples=rx->pixel_samples;
for(int i=0;i<rx->width;i++) {
Expand Down Expand Up @@ -291,7 +289,9 @@ void send_adc_data(REMOTE_CLIENT *client,int i) {
adc_data.random=adc[i].random;
adc_data.preamp=adc[i].preamp;
adc_data.attenuation=htons(adc[i].attenuation);
adc_data.adc_attenuation=htons(adc_attenuation[i]);
adc_data.gain=htond(adc[i].gain);
adc_data.min_gain=htond(adc[i].min_gain);
adc_data.max_gain=htond(adc[i].max_gain);
int bytes_sent=send_bytes(client->socket,(char *)&adc_data,sizeof(adc_data));
if(bytes_sent<0) {
perror("send_adc_data");
Expand Down Expand Up @@ -339,12 +339,8 @@ void send_receiver_data(REMOTE_CLIENT *client,int rx) {
receiver_data.height=htons(receiver[rx]->height);
receiver_data.x=htons(receiver[rx]->x);
receiver_data.y=htons(receiver[rx]->y);
s=(short)(receiver[rx]->volume*100.0);
receiver_data.volume=htons(s);
s=(short)receiver[rx]->rf_gain;
receiver_data.rf_gain=htons(s);
s=(short)receiver[rx]->agc_gain;
receiver_data.agc_gain=htons(s);
receiver_data.volume=htond(receiver[rx]->volume);
receiver_data.agc_gain=htond(receiver[rx]->agc_gain);

int bytes_sent=send_bytes(client->socket,(char *)&receiver_data,sizeof(receiver_data));
if(bytes_sent<0) {
Expand Down Expand Up @@ -625,6 +621,23 @@ g_print("server_client_thread: CMD_RESP_RX_AGC_GAIN\n");
g_idle_add(ext_remote_command,agc_gain_command);
}
break;
case CMD_RESP_RX_GAIN:
g_print("server_client_thread: CMD_RESP_RX_GAIN\n");
{
RFGAIN_COMMAND *command=g_new(RFGAIN_COMMAND,1);
command->header.data_type=header.data_type;
command->header.version=header.version;
command->header.context.client=client;
bytes_read=recv_bytes(client->socket,(char *)&command->id,sizeof(RFGAIN_COMMAND)-sizeof(header));
if(bytes_read<0) {
g_print("server_client_thread: read %d bytes for RFGAIN_COMMAND\n",bytes_read);
perror("server_client_thread");
// dialog box?
return NULL;
}
g_idle_add(ext_remote_command,command);
}
break;
case CMD_RESP_RX_ATTENUATION:
g_print("server_client_thread: CMD_RESP_RX_ATTENUATION\n");
{
Expand Down Expand Up @@ -1271,6 +1284,22 @@ g_print("send_agc_gain rx=%d gain=%d\n",rx,gain);
}
}

void send_rfgain(int s, int id, double gain) {
RFGAIN_COMMAND command;
g_print("send_rfgain rx=%d gain=%f\n",id,gain);
command.header.sync=REMOTE_SYNC;
command.header.data_type=htons(CMD_RESP_RX_GAIN);
command.header.version=htonl(CLIENT_SERVER_VERSION);
command.id=id;
command.gain=htond(gain);
int bytes_sent=send_bytes(s,(char *)&command,sizeof(command));
if(bytes_sent<0) {
perror("send_command");
} else {
g_print("send_command RFGAIN: %d\n",bytes_sent);
}
}

void send_attenuation(int s,int rx,int attenuation) {
ATTENUATION_COMMAND command;
short a=(short)attenuation;
Expand Down Expand Up @@ -1709,10 +1738,7 @@ g_print("send_mute_rx mute=%d\n",mute);


static void *listen_thread(void *arg) {
int address_length;
struct sockaddr_in address;
REMOTE_CLIENT* client;
int rc;
int on=1;

g_print("hpsdr_server: listening on port %d\n",listen_port);
Expand Down Expand Up @@ -1825,9 +1851,6 @@ g_print("check_vfo_timer_id %d\n",check_vfo_timer_id);

static void *client_thread(void* arg) {
gint bytes_read;
#define MAX_BUFFER 2400
char buffer[MAX_BUFFER];
char *token;
HEADER header;
char *server=(char *)arg;

Expand Down Expand Up @@ -1876,8 +1899,9 @@ g_print("INFO_RADIO: %d\n",bytes_read);
locked=radio_data.locked;
receivers=ntohs(radio_data.receivers);
can_transmit=radio_data.can_transmit;
//TEMP
can_transmit=0;

can_transmit=0; // forced temporarily until Client/Server supports transmitters

step=ntohll(radio_data.step);
split=radio_data.split;
sat_mode=radio_data.sat_mode;
Expand Down Expand Up @@ -1918,7 +1942,9 @@ g_print("INFO_ADC: %d\n",bytes_read);
adc[i].random=adc_data.random;
adc[i].preamp=adc_data.preamp;
adc[i].attenuation=ntohs(adc_data.attenuation);
adc_attenuation[i]=ntohs(adc_data.adc_attenuation);
adc[i].gain=ntohd(adc_data.gain);
adc[i].min_gain=ntohd(adc_data.min_gain);
adc[i].max_gain=ntohd(adc_data.max_gain);
}
break;
case INFO_RECEIVER:
Expand Down Expand Up @@ -1976,18 +2002,14 @@ g_print("INFO_RECEIVER: %d\n",bytes_read);
receiver[rx]->height=ntohs(receiver_data.height);
receiver[rx]->x=ntohs(receiver_data.x);
receiver[rx]->y=ntohs(receiver_data.y);
s=ntohs(receiver_data.volume);
receiver[rx]->volume=(double)s/100.0;
s=ntohs(receiver_data.rf_gain);
receiver[rx]->rf_gain=(double)s;
s=ntohs(receiver_data.agc_gain);
receiver[rx]->agc_gain=(double)s;
receiver[rx]->volume=ntohd(receiver_data.volume);
receiver[rx]->agc_gain=ntohd(receiver_data.agc_gain);
//
receiver[rx]->pixel_samples=NULL;
g_mutex_init(&receiver[rx]->display_mutex);
receiver[rx]->hz_per_pixel=(double)receiver[rx]->sample_rate/(double)receiver[rx]->pixels;

receiver[rx]->playback_handle=NULL;
//receiver[rx]->playback_handle=NULL;
receiver[rx]->local_audio_buffer=NULL;
receiver[rx]->local_audio_buffer_size=2048;
receiver[rx]->local_audio=0;
Expand Down Expand Up @@ -2059,8 +2081,7 @@ g_print("g_idle_add: ext_vfo_update\n");
long long ctun_frequency_b=ntohll(spectrum_data.vfo_b_ctun_freq);
long long offset_a=ntohll(spectrum_data.vfo_a_offset);
long long offset_b=ntohll(spectrum_data.vfo_b_offset);
short meter=ntohs(spectrum_data.meter);
receiver[r]->meter=(double)meter;
receiver[r]->meter=ntohd(spectrum_data.meter);
short samples=ntohs(spectrum_data.samples);
if(receiver[r]->pixel_samples==NULL) {
receiver[r]->pixel_samples=g_new(float,(int)samples);
Expand Down Expand Up @@ -2194,6 +2215,22 @@ g_print("AGC_COMMAND: rx=%d agc=%d\n",rx,a);
receiver[rx]->agc_thresh=(double)thresh;
}
break;
case CMD_RESP_RX_GAIN:
{
RFGAIN_COMMAND command;
bytes_read=recv_bytes(client_socket,(char *)&command.id,sizeof(command)-sizeof(header));
if(bytes_read<0) {
g_print("client_thread: read %d bytes for RFGAIN_CMD\n",bytes_read);
perror("client_thread");
// dialog box?
return NULL;
}
int rx=command.id;
double gain=ntohd(command.gain);
g_print("CMD_RESP_RX_GAIN: new=%f rx=%d old=%f\n",gain,rx,adc[receiver[rx]->adc].gain);
adc[receiver[rx]->adc].gain=gain;
}
break;
case CMD_RESP_RX_ATTENUATION:
{
ATTENUATION_COMMAND attenuation_cmd;
Expand All @@ -2206,8 +2243,8 @@ g_print("AGC_COMMAND: rx=%d agc=%d\n",rx,a);
}
int rx=attenuation_cmd.id;
short attenuation=ntohs(attenuation_cmd.attenuation);
g_print("CMD_RESP_RX_ATTENUATION: attenuation=%d adc_attenuation[rx[%d]->adc]=%d\n",attenuation,rx,adc_attenuation[receiver[rx]->adc]);
adc_attenuation[receiver[rx]->adc]=attenuation;
g_print("CMD_RESP_RX_ATTENUATION: attenuation=%d attenuation[rx[%d]->adc]=%d\n",attenuation,rx,adc[receiver[rx]->adc].attenuation);
adc[receiver[rx]->adc].attenuation=attenuation;
}
break;
case CMD_RESP_RX_NOISE:
Expand Down
23 changes: 20 additions & 3 deletions client_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
#define ntohll be64toh
#endif

//
// Conversion of host(double) to/from network(unsigned int)
// Assume that double data is between -200 and 200,
// convert to uint16 via uint16 = 100.0*(double+200.0) (result in the range 0 to 40000)
//
#define htond(X) htons((uint16_t) ((X+200.0)*100.0) )
#define ntohd(X) 0.01*ntohs(X)-200.0

typedef enum {
RECEIVER_DETACHED, RECEIVER_ATTACHED
} CLIENT_STATE;
Expand Down Expand Up @@ -61,6 +69,7 @@ enum {
CMD_RESP_RX_VOLUME,
CMD_RESP_RX_AGC_GAIN,
CMD_RESP_RX_ATTENUATION,
CMD_RESP_RX_GAIN,
CMD_RESP_RX_SQUELCH,
CMD_RESP_RX_FPS,
CMD_RESP_RX_SELECT,
Expand Down Expand Up @@ -112,7 +121,7 @@ typedef struct _remote_client {
CLIENT_STATE state;
gint receivers;
gint spectrum_update_timer_id;
REMOTE_RX receiver[MAX_RECEIVERS];
REMOTE_RX receiver[8];
void *next;
} REMOTE_CLIENT;

Expand Down Expand Up @@ -159,7 +168,9 @@ typedef struct __attribute__((__packed__)) _adc_data {
uint8_t random;
uint8_t preamp;
uint16_t attenuation;
uint16_t adc_attenuation;
uint16_t gain;
uint16_t min_gain;
uint16_t max_gain;
} ADC_DATA;

typedef struct __attribute__((__packed__)) _receiver_data {
Expand Down Expand Up @@ -196,7 +207,6 @@ typedef struct __attribute__((__packed__)) _receiver_data {
uint16_t x;
uint16_t y;
uint16_t volume;
uint16_t rf_gain;
uint16_t agc_gain;
} RECEIVER_DATA;

Expand Down Expand Up @@ -337,6 +347,12 @@ typedef struct __attribute__((__packed__)) _attenuation_command {
uint16_t attenuation;
} ATTENUATION_COMMAND;

typedef struct __attribute__((__packed__)) _rfgain_command {
HEADER header;
uint8_t id;
uint16_t gain;
} RFGAIN_COMMAND;

typedef struct __attribute__((__packed__)) _squelch_command {
HEADER header;
uint8_t id;
Expand Down Expand Up @@ -486,6 +502,7 @@ extern void send_volume(int s,int rx,int volume);
extern void send_agc(int s,int rx,int agc);
extern void send_agc_gain(int s,int rx,int gain,int hang,int thresh);
extern void send_attenuation(int s,int rx,int attenuation);
extern void send_rfgain(int s,int rx, double gain);
extern void send_squelch(int s,int rx,int enable,int squelch);
extern void send_noise(int s,int rx,int nb,int nb2,int nr,int nr2,int anf,int snb);
extern void send_band(int s,int rx,int band);
Expand Down
2 changes: 1 addition & 1 deletion discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ GtkWidget *host_addr_entry;
static char host_addr_buffer[128]="g0orx.ddns.net";
char *host_addr = &host_addr_buffer[0];
GtkWidget *host_port_spinner;
gint host_port=45000;
gint host_port=50000; // default listening port
#endif

//
Expand Down
7 changes: 7 additions & 0 deletions ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,13 @@ int ext_remote_command(void *data) {
send_agc_gain(client->socket,rx->id,(int)rx->agc_gain,(int)rx->agc_hang,(int)rx->agc_thresh);
}
break;
case CMD_RESP_RX_GAIN:
{
RFGAIN_COMMAND *command=(RFGAIN_COMMAND *) data;
double td=ntohd(command->gain);
set_rf_gain(command->id, td);
}
break;
case CMD_RESP_RX_ATTENUATION:
{
ATTENUATION_COMMAND *attenuation_command=(ATTENUATION_COMMAND *)data;
Expand Down
Loading

0 comments on commit 7ad6218

Please sign in to comment.