diff --git a/src/Makefile.am b/src/Makefile.am index c3ac077ce..74f639f38 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,15 +1,16 @@ INCLUDES = @INCLUDES@ @INCLTDL@ RIGSRC = rig.c serial.c misc.c register.c event.c cal.c conf.c tones.c \ rotator.c locator.c rot_reg.c rot_conf.c iofunc.c ext.c \ - mem.c settings.c parallel.c usb_port.c debug.c + mem.c settings.c parallel.c usb_port.c debug.c network.c lib_LTLIBRARIES = libhamlib.la libhamlib_la_SOURCES = $(RIGSRC) libhamlib_la_LDFLAGS = $(WINLDFLAGS) -no-undefined -version-info @ABI_VERSION@:8:0 libhamlib_la_CFLAGS = -DIN_HAMLIB $(AM_CFLAGS) -DHAMLIB_MODULE_DIR=\"$(libdir)\" -libhamlib_la_LIBADD = @LIBLTDL@ $(top_builddir)/lib/libmisc.la @MATH_LIBS@ $(LIBUSB_LIBS) +libhamlib_la_LIBADD = @LIBLTDL@ $(top_builddir)/lib/libmisc.la \ + @NET_LIBS@ @MATH_LIBS@ $(LIBUSB_LIBS) noinst_HEADERS = event.h misc.h serial.h iofunc.h cal.h tones.h \ rot_conf.h token.h idx_builtin.h register.h par_nt.h \ - parallel.h usb_port.h + parallel.h usb_port.h network.h diff --git a/src/network.c b/src/network.c new file mode 100644 index 000000000..114db916a --- /dev/null +++ b/src/network.c @@ -0,0 +1,120 @@ +/* + * Hamlib Interface - network communication low-level support + * Copyright (c) 2000-2008 by Stephane Fillod + * + * $Id: network.c,v 1.1 2008-09-21 19:30:34 fillods Exp $ + * + * This library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +/** + * \addtogroup rig_internal + * @{ + */ + +/** + * \brief Network port IO + * \file network.c + */ + +#define WINVER 0x0501 + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include /* Standard input/output definitions */ +#include /* String function definitions */ +#include /* UNIX standard function definitions */ +#include /* File control definitions */ +#include /* Error number definitions */ +#include +#include +#include +#include + + +#ifdef HAVE_NETINET_IN_H +#include +#include /* TODO */ +#endif +#ifdef HAVE_ARPA_INET_H +#include +#endif +#ifdef HAVE_SYS_SOCKET_H +#include +#elif HAVE_WS2TCPIP_H +#include +#endif + + +#include "hamlib/rig.h" +#include "network.h" +#include "misc.h" + +/** + * \brief Open network port using rig.state data + * \param rp port data structure (must spec port id eg hostname:port) + * \return RIG_OK or < 0 if error + */ +int network_open(hamlib_port_t *rp) { + + int fd; /* File descriptor for the port */ + int status; + struct addrinfo hints, *res; + char *portstr; + char hostname[FILPATHLEN]; + + if (!rp) + return -RIG_EINVAL; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + strcpy(hostname, rp->pathname); + portstr = strchr(hostname, ':'); + if (!portstr) + return -RIG_ECONF; + *portstr++ = '\0'; + + status=getaddrinfo(hostname, portstr, &hints, &res); + if (status != 0) { + rig_debug(RIG_DEBUG_ERR, "Cannot get host \"%s\": %s\n", + rp->pathname, gai_strerror(errno)); + return -RIG_ECONF; + } + + fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + if (fd < 0) + return -RIG_EIO; + + status = connect(fd, res->ai_addr, res->ai_addrlen); + freeaddrinfo(res); + if (status < 0) { + rig_debug(RIG_DEBUG_ERR, "Cannot open NET device \"%s\": %s\n", + rp->pathname, strerror(errno)); + close(fd); + return -RIG_EIO; + } + + rp->fd = fd; + + return RIG_OK; +} + +/** @} */ diff --git a/src/network.h b/src/network.h new file mode 100644 index 000000000..1a4af19b9 --- /dev/null +++ b/src/network.h @@ -0,0 +1,36 @@ +/* + * Hamlib Interface - network communication header + * Copyright (c) 2000-2008 by Stephane Fillod + * + * $Id: network.h,v 1.1 2008-09-21 19:30:35 fillods Exp $ + * + * This library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#ifndef _NETWORK_H +#define _NETWORK_H 1 + +#include +#include "iofunc.h" + +__BEGIN_DECLS + +/* Hamlib internal use, see rig.c */ +int network_open(hamlib_port_t *p); + +__END_DECLS + +#endif /* _NETWORK_H */ diff --git a/src/rig.c b/src/rig.c index 3c722ad4f..32ad9773b 100644 --- a/src/rig.c +++ b/src/rig.c @@ -2,7 +2,7 @@ * Hamlib Interface - main file * Copyright (c) 2000-2008 by Stephane Fillod and Frank Singleton * - * $Id: rig.c,v 1.97 2008-09-12 11:49:32 fillods Exp $ + * $Id: rig.c,v 1.98 2008-09-21 19:30:35 fillods Exp $ * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as @@ -64,6 +64,7 @@ #include "serial.h" #include "parallel.h" #include "usb_port.h" +#include "network.h" #include "event.h" /** @@ -295,6 +296,10 @@ RIG * HAMLIB_API rig_init(rig_model_t rig_model) strncpy(rs->rigport.pathname, DEFAULT_PARALLEL_PORT, FILPATHLEN); break; + case RIG_PORT_NETWORK: + strncpy(rs->rigport.pathname, "localhost:4532", FILPATHLEN); + break; + default: strncpy(rs->rigport.pathname, "", FILPATHLEN); } @@ -480,8 +485,12 @@ int HAMLIB_API rig_open(RIG *rig) case RIG_PORT_RPC: break; /* ez :) */ - case RIG_PORT_NETWORK: /* not implemented yet! */ - return -RIG_ENIMPL; + case RIG_PORT_NETWORK: + status = network_open(&rs->rigport); + if (status < 0) + return status; + break; + default: return -RIG_EINVAL; } diff --git a/src/rotator.c b/src/rotator.c index 07a770d95..2361ba609 100644 --- a/src/rotator.c +++ b/src/rotator.c @@ -2,7 +2,7 @@ * Hamlib Interface - main file * Copyright (c) 2000-2008 by Stephane Fillod and Frank Singleton * - * $Id: rotator.c,v 1.22 2008-05-04 15:36:23 fillods Exp $ + * $Id: rotator.c,v 1.23 2008-09-21 19:30:35 fillods Exp $ * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as @@ -57,6 +57,8 @@ #include "hamlib/rotator.h" #include "serial.h" #include "parallel.h" +#include "usb_port.h" +#include "network.h" #include "rot_conf.h" #include "token.h" @@ -231,6 +233,10 @@ ROT * HAMLIB_API rot_init(rot_model_t rot_model) strncpy(rs->rotport.pathname, DEFAULT_PARALLEL_PORT, FILPATHLEN); break; + case RIG_PORT_NETWORK: + strncpy(rs->rotport.pathname, "localhost:4533", FILPATHLEN); + break; + default: strncpy(rs->rotport.pathname, "", FILPATHLEN); } @@ -316,12 +322,22 @@ int HAMLIB_API rot_open(ROT *rot) rs->rotport.fd = status; break; + case RIG_PORT_USB: + status = usb_port_open(&rs->rotport); + if (status < 0) + return status; + break; + case RIG_PORT_NONE: case RIG_PORT_RPC: break; /* ez :) */ - case RIG_PORT_NETWORK: /* not implemented yet! */ - return -RIG_ENIMPL; + case RIG_PORT_NETWORK: + status = network_open(&rs->rotport); + if (status < 0) + return status; + break; + default: return -RIG_EINVAL; } @@ -391,6 +407,9 @@ int HAMLIB_API rot_close(ROT *rot) case RIG_PORT_PARALLEL: par_close(&rs->rotport); break; + case RIG_PORT_USB: + usb_port_close(&rs->rotport); + break; default: close(rs->rotport.fd); }