Skip to content

Commit 35b3f63

Browse files
committed
method=td is OK
1 parent 6952973 commit 35b3f63

File tree

3 files changed

+88
-20
lines changed

3 files changed

+88
-20
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ libcsdr.so: fft_fftw.c fft_rpi.c libcsdr_wrapper.c libcsdr.c libcsdr_gpl.c fastd
5555
csdr: csdr.c libcsdr.so
5656
gcc -std=gnu99 $(PARAMS_LOOPVECT) $(PARAMS_SIMD) csdr.c $(PARAMS_LIBS) -L. -lcsdr $(PARAMS_MISC) -o csdr
5757
ddcd: ddcd.cpp libcsdr.so ddcd.h
58-
g++ $(PARAMS_LOOPVECT) $(PARAMS_SIMD) ddcd.cpp $(PARAMS_LIBS) -L. -lcsdr $(PARAMS_MISC) -o ddcd
58+
g++ $(PARAMS_LOOPVECT) $(PARAMS_SIMD) ddcd.cpp $(PARAMS_LIBS) -L. -lcsdr -lpthread $(PARAMS_MISC) -o ddcd
5959
arm-cross: clean-vect
6060
#note: this doesn't work since having added FFTW
6161
arm-linux-gnueabihf-gcc -std=gnu99 -O3 -fshort-double -ffast-math -dumpbase dumpvect-arm -fdump-tree-vect-details -mfloat-abi=softfp -march=armv7-a -mtune=cortex-a9 -mfpu=neon -mvectorize-with-neon-quad -Wno-unused-result -Wformat=0 $(SOURCES) -lm -o ./csdr

ddcd.cpp

+83-19
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ int main(int argc, char* argv[])
333333
close(clients[i]->socket);
334334
delete clients[i];
335335
clients.erase(clients.begin()+i);
336-
fprintf(stderr, MSG_START "done closing client from main process.");
336+
fprintf(stderr, MSG_START "done closing client from main process.\n");
337337
}
338338
}
339339
else { if(clients[i]->error) print_client(clients[i], "pipe okay again."); clients[i]->error=0; }
@@ -347,16 +347,24 @@ int main(int argc, char* argv[])
347347

348348
pid_t run_subprocess(char* cmd, int* pipe_in, int* pipe_out, pid_t* pgrp)
349349
{
350+
/*char sem_name[101];
351+
snprintf(sem_name,100,"ddcd_sem_%d",getpid());
352+
sem_t mysem;
353+
if(sem_init(&mysem, 1, 1)==-1) error_exit("failed to sem_init() in run_subprocess()");
354+
fprintf(stderr, "sem_waiting\n");
355+
if(sem_wait(&mysem)==-1) error_exit("the first sem_wait() failed in run_subprocess()");
356+
fprintf(stderr, "sem_waited\n");
357+
*/
358+
int syncpipe[2];
359+
if(pipe(syncpipe)==-1) error_exit("failed to create pipe()");
350360
pid_t pid = fork();
351-
if(*pgrp>=0)
352-
{
353-
setpgrp();
354-
*pgrp = getpgrp();
355-
}
356-
//fprintf(stderr, "run_subprocess :: fork-ed %d\n", pid);
361+
357362
if(pid < 0) return 0; //fork failed
358363
if(pid == 0)
359364
{
365+
setpgrp();
366+
write(syncpipe[1], " ", 1);
367+
//if(sem_post(&mysem)==-1) error_exit("failed to sem_post() in run_subprocess()");
360368
//We're the subprocess
361369
//fprintf(stderr, "run_subprocess :: execl\n");
362370
//if(fcntl(pipe_in[1], F_SETPIPE_SZ, pipe_max_size) == -1) perror("Failed to F_SETPIPE_SZ in run_subprocess()");
@@ -373,19 +381,22 @@ pid_t run_subprocess(char* cmd, int* pipe_in, int* pipe_out, pid_t* pgrp)
373381
execl("/bin/bash","bash","-c",cmd, (char*)0);
374382
error_exit(MSG_START "run_subprocess failed to execute command");
375383
}
376-
else return pid;
384+
else
385+
{
386+
//if(sem_wait(&mysem)==-1) error_exit("the second sem_wait() failed in run_subprocess()");
387+
int synctemp;
388+
read(syncpipe[0], &synctemp, 1);
389+
*pgrp = getpgid(pid);
390+
fprintf(stderr, MSG_START "run_subprocess pgid returned = %d\n", *pgrp);
391+
return pid;
392+
}
377393
}
378394

379395
void print_client(client_t* client, const char* what)
380396
{
381397
fprintf(stderr,MSG_START "(client %s:%d) %s\n", inet_ntoa(client->addr.sin_addr), client->addr.sin_port, what);
382398
}
383399

384-
void client_cleanup()
385-
{
386-
close(this_client->pipefd[0]);
387-
}
388-
389400
#define CTL_BUFSIZE 1024
390401

391402
int read_socket_ctl(int fd, char* output, int max_size)
@@ -397,7 +408,7 @@ int read_socket_ctl(int fd, char* output, int max_size)
397408
if(buffer_index==CTL_BUFSIZE) buffer_index=0;
398409
int bytes_read=recv(fd,buffer+buffer_index,(CTL_BUFSIZE-buffer_index)*sizeof(char), MSG_DONTWAIT);
399410
if(bytes_read<=0) return 0;
400-
fprintf(stderr, "recv %d\n", bytes_read);
411+
//fprintf(stderr, "recv %d\n", bytes_read);
401412

402413
int prev_newline_at=0;
403414
int last_newline_at=0;
@@ -425,6 +436,26 @@ int read_socket_ctl(int fd, char* output, int max_size)
425436
}
426437
}
427438

439+
int ctl_get_arg(char* input, const char* cmd, const char* format, ...)
440+
{
441+
int retval=0;
442+
int cmdlen=strlen(cmd);
443+
if(input[cmdlen]=='=')
444+
{
445+
//fprintf(stderr, "cga found=\n");
446+
if(input[cmdlen]=0, !strcmp(input,cmd))
447+
{
448+
//fprintf(stderr, "cga foundokay\n");
449+
va_list vl;
450+
va_start(vl,format);
451+
retval=vsscanf(input+cmdlen+1,format,vl);
452+
va_end(vl);
453+
}
454+
input[cmdlen]='=';
455+
}
456+
//fprintf(stderr, "cga retval %d\n", retval);
457+
return retval;
458+
}
428459

429460
void client()
430461
{
@@ -433,12 +464,13 @@ void client()
433464

434465
char client_subprocess_cmd_buf[500];
435466
int input_fd = this_client->pipefd[0];
467+
int pipe_ctl[2], pipe_stdout[2];
436468

437469
prctl(PR_SET_PDEATHSIG, SIGHUP); //get a signal when parent exits
438470

439471
if(decimation!=1)
440472
{
441-
int pipe_ctl[2], pipe_stdout[2];
473+
442474
if(pipe(pipe_ctl)==-1) error_exit(MSG_START "cannot open new pipe() for the client subprocess");
443475
if(pipe(pipe_stdout)==-1) error_exit(MSG_START "cannot open new pipe() for the client subprocess");
444476
switch(ddc_method)
@@ -459,14 +491,45 @@ void client()
459491
write(pipe_ctl[1], "0.0\n", 4);
460492
}
461493
char recv_cmd[CTL_BUFSIZE];
494+
char temps[CTL_BUFSIZE*2];
495+
int tempi;
496+
float tempf;
497+
462498
for(;;)
463499
{
464500
while(read_socket_ctl(this_client->socket, recv_cmd, CTL_BUFSIZE))
465-
fprintf(stderr, "read_socket_ctl: %s\n", recv_cmd);
466-
read(input_fd,buf,bufsizeall);
467-
if(send(this_client->socket,buf,bufsizeall,0)==-1)
468501
{
469-
print_client(this_client, "client process is exiting.");
502+
sprintf(temps, "read_socket_ctl: %s", recv_cmd);
503+
print_client(this_client, temps);
504+
if(ctl_get_arg(recv_cmd, "bypass", "%d", &tempi))
505+
{
506+
if(tempi==1 && client_subprocess_pid)
507+
{
508+
//print_client(this_client, "suspending client_subprocess_pgrp...\n");
509+
//fprintf(stderr, "client_subprocess_pgrp = %d\n", client_subprocess_pgrp);
510+
//killpg(client_subprocess_pgrp, SIGTSTP);
511+
//while(proc_exists(client_subprocess_pid)) usleep(10000);
512+
//print_client(this_client, "done killing client_subprocess_pid.\n");
513+
input_fd=this_client->pipefd[0]; //by doing this, we don't read from pipe_stdout[0] anymore, so that csdr stops doing anything, and also doesn't read anymore from the input: we get the whole I/Q stream!
514+
}
515+
if(tempi==0 && client_subprocess_pid)
516+
{
517+
input_fd=pipe_stdout[0];
518+
}
519+
520+
}
521+
if(ctl_get_arg(recv_cmd, "shift", "%g", &tempf))
522+
{
523+
tempi=sprintf(temps, "%g\n", tempf);
524+
write(pipe_ctl[1], temps, tempi);
525+
fsync(pipe_ctl[1]);
526+
}
527+
}
528+
int nread = read(input_fd,buf,bufsizeall);
529+
if(nread<=0) continue;
530+
if(send(this_client->socket,buf,nread,0)==-1)
531+
{
532+
print_client(this_client, "client process is exiting.\n");
470533
if(client_subprocess_pid) killpg2(client_subprocess_pgrp);
471534
exit(0);
472535
}
@@ -475,6 +538,7 @@ void client()
475538

476539
void killpg2(pid_t pgrp)
477540
{
541+
//fprintf(stderr, MSG_START "killpg2: %d\n", pgrp);
478542
if(pgrp!=1 && pgrp!=0) killpg(pgrp, SIGTERM);
479543
}
480544

ddcd.h

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#include <errno.h>
1717
#include <sys/wait.h>
1818
#include <sys/prctl.h>
19+
#include <stdarg.h>
20+
#include <sys/stat.h>
21+
#include <semaphore.h>
1922

2023
typedef struct client_s
2124
{
@@ -37,6 +40,7 @@ pid_t run_subprocess(char* cmd, int* pipe_in, int* pipe_out, pid_t* pgrp);
3740
void maxfd(int* maxfd, int fd);
3841
void sig_handler(int signo);
3942
void killpg2(pid_t pgrp);
43+
int ctl_get_arg(char* input, const char* cmd, const char* format, ...);
4044

4145
typedef enum ddc_method_e
4246
{

0 commit comments

Comments
 (0)