csdr/ddcd.cpp

328 lines
9.7 KiB
C++
Raw Normal View History

2015-11-04 17:08:33 +08:00
/*
2016-06-05 04:54:17 +08:00
This software is part of libcsdr, a set of simple DSP routines for
2015-11-04 17:08:33 +08:00
Software Defined Radio.
Copyright (c) 2014, Andras Retzler <randras@sdr.hu>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ANDRAS RETZLER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2015-11-04 20:31:47 +08:00
#include "ddcd.h"
2016-06-05 04:54:17 +08:00
int host_port = 0;
char host_address[100] = "127.0.0.1";
2016-06-05 04:54:17 +08:00
int thread_cntr = 0;
//CLI parameters
int decimation = 0;
2015-12-02 21:51:20 +08:00
float transition_bw = 0.05;
2016-06-05 04:54:17 +08:00
int bufsize = 1024; //! currently unused
2016-06-04 23:42:29 +08:00
int bufcnt = 1024;
2015-12-02 21:51:20 +08:00
char ddc_method_str[100] = "td";
ddc_method_t ddc_method;
2015-11-04 17:08:33 +08:00
2016-06-04 23:42:29 +08:00
void sig_handler(int signo)
2016-06-05 04:54:17 +08:00
{
2016-06-04 23:42:29 +08:00
fprintf(stderr, MSG_START "signal %d caught, exiting ddcd...\n", signo);
fflush(stderr);
exit(0);
}
2015-11-04 17:08:33 +08:00
int main(int argc, char* argv[])
{
int c;
for(;;)
{
int option_index = 0;
static struct option long_options[] = {
{"port", required_argument, 0, 'p' },
{"address", required_argument, 0, 'a' },
{"decimation", required_argument, 0, 'd' },
2015-12-02 21:51:20 +08:00
{"bufsize", required_argument, 0, 'b' },
2016-06-05 04:54:17 +08:00
{"bufcnt", required_argument, 0, 'n' },
2015-12-02 22:27:28 +08:00
{"method", required_argument, 0, 'm' },
2015-12-02 21:51:20 +08:00
{"transition", required_argument, 0, 't' }
2015-11-04 17:08:33 +08:00
};
2016-06-05 04:54:17 +08:00
c = getopt_long(argc, argv, "p:a:d:b:n:m:t:", long_options, &option_index);
2015-11-04 17:08:33 +08:00
if(c==-1) break;
2016-06-05 04:54:17 +08:00
switch (c)
2015-11-04 17:08:33 +08:00
{
case 'a':
host_address[100-1]=0;
strncpy(host_address,optarg,100-1);
break;
case 'p':
host_port=atoi(optarg);
break;
case 'd':
decimation=atoi(optarg);
break;
case 'b':
bufsize=atoi(optarg);
break;
2016-06-05 04:54:17 +08:00
case 'n':
bufcnt=atoi(optarg);
break;
2015-12-02 21:51:20 +08:00
case 'm':
2015-12-03 04:58:48 +08:00
ddc_method_str[100-1]=0;
2015-12-02 21:51:20 +08:00
strncpy(ddc_method_str,optarg,100-1);
break;
case 't':
sscanf(optarg,"%g",&transition_bw);
break;
2015-11-04 17:08:33 +08:00
case 0:
case '?':
case ':':
2015-12-02 21:51:20 +08:00
default:;
2015-12-03 04:58:48 +08:00
print_exit(MSG_START "error in getopt_long()\n");
2015-11-04 17:08:33 +08:00
}
}
2016-06-05 04:54:17 +08:00
2015-12-02 21:51:20 +08:00
if(!decimation) print_exit(MSG_START "missing required command line argument, --decimation.\n");
if(!host_port) print_exit(MSG_START "missing required command line argument, --port.\n");
if(decimation<0) print_exit(MSG_START "invalid value for --decimation (should be >0).\n");
if(decimation==1) fprintf(stderr, MSG_START "decimation = 1, just copying raw samples.\n");
if(transition_bw<0||transition_bw>0.5) print_exit(MSG_START "invalid value for --transition (should be between 0 and 0.5).\n");
2016-06-05 04:54:17 +08:00
if(bufsize<0) print_exit(MSG_START "invalid value for --bufsize (should be >0)\n");
if(bufcnt<0) print_exit(MSG_START "invalid value for --bufcnt (should be >0)\n");
if(decimation==1); //don't do anything then //!will have to take care about this later
else if(!strcmp(ddc_method_str,"td"))
2015-12-02 21:51:20 +08:00
{
2016-06-05 04:54:17 +08:00
ddc_method = M_TD;
2015-12-02 21:51:20 +08:00
fprintf(stderr, MSG_START "method is M_TD (default).\n");
}
2016-06-05 04:54:17 +08:00
else if (!strcmp(ddc_method_str,"fastddc"))
2015-12-02 21:51:20 +08:00
{
2016-06-05 04:54:17 +08:00
ddc_method = M_FASTDDC;
2015-12-02 21:51:20 +08:00
fprintf(stderr, MSG_START "method is M_FASTDDC.\n");
}
else print_exit(MSG_START "invalid parameter given to --method.\n");
2016-06-04 23:42:29 +08:00
//set signals
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = sig_handler;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGKILL, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
2016-06-05 04:54:17 +08:00
2016-06-04 23:42:29 +08:00
struct sockaddr_in addr_host;
int listen_socket;
std::vector<client_t*> clients;
clients.reserve(100);
listen_socket=socket(AF_INET,SOCK_STREAM,0);
int sockopt = 1;
if( setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&sockopt, sizeof(sockopt)) == -1 )
error_exit(MSG_START "cannot set SO_REUSEADDR"); //the best description on SO_REUSEADDR ever: http://stackoverflow.com/a/14388707/3182453
2016-06-05 04:54:17 +08:00
2016-06-04 23:42:29 +08:00
memset(&addr_host,'0',sizeof(addr_host));
addr_host.sin_family = AF_INET;
addr_host.sin_port = htons(host_port);
addr_host.sin_addr.s_addr = INADDR_ANY;
2016-06-05 04:54:17 +08:00
if( (addr_host.sin_addr.s_addr=inet_addr(host_address)) == INADDR_NONE )
2016-06-04 23:42:29 +08:00
error_exit(MSG_START "invalid host address");
if( bind(listen_socket, (struct sockaddr*) &addr_host, sizeof(addr_host)) < 0 )
error_exit(MSG_START "cannot bind() address to the socket");
if( listen(listen_socket, 10) == -1 )
error_exit(MSG_START "cannot listen() on socket");
fprintf(stderr,MSG_START "listening on %s:%d\n", inet_ntoa(addr_host.sin_addr), host_port);
struct sockaddr_in addr_cli;
socklen_t addr_cli_len = sizeof(addr_cli);
int new_socket;
2016-06-05 04:54:17 +08:00
int highfd = 0;
2016-06-04 23:42:29 +08:00
FD_ZERO(&select_fds);
FD_SET(listen_socket, &select_fds);
maxfd(&highfd, listen_socket);
FD_SET(input_fd, &select_fds);
maxfd(&highfd, input_fd);
2016-06-05 04:54:17 +08:00
//Set stdin and listen_socket to non-blocking
if(set_nonblocking(input_fd) || set_nonblocking(listen_socket))
2016-06-04 23:42:29 +08:00
error_exit(MSG_START "cannot set_nonblocking()");
2016-06-05 04:54:17 +08:00
//Create tsmpool
tsmpool* pool = new tsmpool(bufsize, bufcnt);
if(!pool->ok) print_exit(MSG_START "tsmpool failed to initialize\n");
2016-06-05 04:54:17 +08:00
unsigned char* current_write_buffer = pool->get_write_buffer();
int index_in_current_write_buffer = 0;
2016-06-04 23:42:29 +08:00
for(;;)
{
//Let's wait until there is any new data to read, or any new connection!
select(highfd, &select_fds, NULL, NULL, NULL);
//Is there a new client connection?
if( (new_socket = accept(listen_socket, (struct sockaddr*)&addr_cli, &addr_cli_len)) != -1)
2016-06-05 04:54:17 +08:00
{
clients_close_all_finished();
2016-06-05 04:56:20 +08:00
if(pthread_create(&new_client->thread, NULL, client_thread , (void*)&new_client)<0)
2016-06-04 23:42:29 +08:00
{
//We're the parent
2016-06-05 04:54:17 +08:00
client_t* new_client = new client_t;
new_client->error = 0;
memcpy(&new_client->addr, &addr_cli, sizeof(new_client->addr));
new_client->socket = new_socket;
new_client->status = CS_CREATED;
clients.push_back(new_client);
2016-06-05 04:56:20 +08:00
fprintf(stderr, MSG_START "pthread_create() done, clients now: %d\n", clients.size());
2016-06-04 23:42:29 +08:00
}
else fprintf(stderr, MSG_START "pthread_create() failed.\n");
2016-06-04 23:42:29 +08:00
}
2016-06-05 04:54:17 +08:00
if(index_in_current_write_buffer >= bufsize)
2016-06-04 23:42:29 +08:00
{
2016-06-05 04:54:17 +08:00
current_write_buffer = pool->get_write_buffer();
index_in_current_write_buffer = 0;error_exiterror_exit
2016-06-04 23:42:29 +08:00
}
2016-06-05 04:54:17 +08:00
int retval = read(input_fd, current_write_buffer + index_in_current_write_buffer, bufsize - index_in_current_write_buffer);
if(retval>0)
2016-06-04 23:42:29 +08:00
{
2016-06-05 04:54:17 +08:00
index_in_current_write_buffer += retval;
}
else if(retval==0)
{
//!end of input stream, close clients and exit
print_exit(MSG_START "end of input, exiting.\n")
2016-06-04 23:42:29 +08:00
}
2016-06-05 04:54:17 +08:00
}
}
2016-06-04 23:42:29 +08:00
2016-06-05 04:54:17 +08:00
#if 0
for (int i=0; i<clients.size(); i++)
{
if(write(clients[i]->pipefd[1], buf, retval)==-1)
{
if(!clients[i]->error)
{
print_client(clients[i], "lost buffer, failed to write pipe.");
clients[i]->error=1;
}
//fprintf(stderr, MSG_START "errno is %d\n", errno); //usually 11
//int wpstatus;
//int wpresult = waitpid(clients[i]->pid, &wpstatus, WNOHANG);
//fprintf(stderr, MSG_START "pid is %d\n",clients[i]->pid);
//perror("somethings wrong");
//if(wpresult == -1) print_client(clients[i], "error while waitpid()!");
//else if(wpresult == 0)
waitpid(clients[i]->pid, NULL, WNOHANG);
if(!proc_exists(clients[i]->pid))
{
//Client exited!
print_client(clients[i], "closing client from main process.");
close(clients[i]->pipefd[1]);
close(clients[i]->socket);
delete clients[i];
clients.erase(clients.begin()+i);
fprintf(stderr, MSG_START "done closing client from main process.\n");
}
2016-06-04 23:42:29 +08:00
}
2016-06-05 04:54:17 +08:00
else { if(clients[i]->error) print_client(clients[i], "pipe okay again."); clients[i]->error=0; }
}
}
//TODO: at the end, server closes pipefd[1] for client
#endif
2016-06-04 23:42:29 +08:00
2016-06-05 04:54:17 +08:00
void clients_close_all_finished()
{
for(int i=0;i<clients.size();i++)
{
if(clients[i]->status == CS_THREAD_FINISHED) clients.erase(i);
}
2016-06-04 23:42:29 +08:00
}
void client_parser_push(char c)
{ //!TODO
command_t cmd;
char* commands_cstr = commands.c_str();
int newline_index = -1;
for(int i=0;commands_cstr[i];i++) if(commands_cstr[i]=='\n') newline_index = i;
if(newline_index == -1)
char param_name[101];
char param_value[101];
for(int i=0;i<100;commands_csdr
}
2016-06-05 04:54:17 +08:00
void* client_thread (void* param) //!TODO
2016-06-04 23:42:29 +08:00
{
2016-06-05 04:54:17 +08:00
client_t* me_the_client = (client_t*)param;
me_the_client->status = CS_THREAD_RUNNING;
char ctl_data_buffer;
int retval;
for(;;)
{
do
{
retval = recv(me_the_client->socket, &ctl_data_buffer, 1, 0);
if(client_parser_push(ctl_data_buffer)) break;
} while (retval);
2016-06-05 04:54:17 +08:00
//read control data from socket
//process control data
//run shift
//run decimation
//have an exit condition (??)
if(ddc_method == M_TD)
{
}
}
me_the_client->status = CS_THREAD_FINISHED;
pthread_exit(NULL);
2016-06-05 04:54:17 +08:00
return NULL;
2016-06-04 23:42:29 +08:00
}
void error_exit(const char* why)
{
perror(why); //do we need a \n at the end of (why)?
2016-06-04 23:42:29 +08:00
exit(1);
2015-11-04 17:08:33 +08:00
}
2015-12-02 21:51:20 +08:00
void print_exit(const char* why)
{
fprintf(stderr, "%s", why);
exit(1);
}
2016-06-04 23:42:29 +08:00
void maxfd(int* maxfd, int fd)
{
2016-06-05 04:54:17 +08:00
if(fd>=*maxfd) *maxfd=fd+1;
2016-06-04 23:42:29 +08:00
}