Clears potential garbage data in read buffer prior to command being sent

astyle-formatting
Jae Stutzman 2017-01-23 22:21:49 -06:00 zatwierdzone przez Nate Bargmann
rodzic 0277efdac9
commit 230430c4e9
3 zmienionych plików z 32 dodań i 1 usunięć

Wyświetl plik

@ -33,6 +33,7 @@
#include <ctype.h>
#include "hamlib/rig.h"
#include "network.h"
#include "serial.h"
#include "misc.h"
#include "register.h"
@ -242,7 +243,13 @@ int kenwood_transaction(RIG *rig, const char *cmdstr, char *data, size_t datasiz
len++;
}
serial_flush(&rs->rigport);
/* flush anything in the read buffer before command is sent */
if (rs->rigport.type.rig == RIG_PORT_NETWORK || rs->rigport.type.rig == RIG_PORT_UDP_NETWORK) {
network_flush(&rs->rigport);
} else {
serial_flush(&rs->rigport);
}
retval = write_block(&rs->rigport, cmd, len);
free(cmd);

Wyświetl plik

@ -44,6 +44,7 @@
#include <sys/time.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/ioctl.h>
#ifdef HAVE_NETINET_IN_H
@ -73,6 +74,8 @@
static int wsstarted;
#endif
#define NET_BUFFER_SIZE 64
static void handle_error (enum rig_debug_level_e lvl, const char *msg)
{
int e;
@ -103,6 +106,26 @@ static void handle_error (enum rig_debug_level_e lvl, const char *msg)
#endif
}
/**
* \brief Clears any data in the read buffer of the socket
*
* \param rp Port data structure
*/
void network_flush(hamlib_port_t* rp)
{
int len = 0;
char buffer[NET_BUFFER_SIZE] = { 0 };
for (;;) {
ioctl(rp->fd, FIONREAD, &len);
if (len > 0) {
len = read(rp->fd, &buffer, len < NET_BUFFER_SIZE ? len : NET_BUFFER_SIZE);
rig_debug(RIG_DEBUG_WARN, "Network data cleared: %s\n", buffer);
} else {
break;
}
}
}
/**
* \brief Open network port using rig.state data
*

Wyświetl plik

@ -30,6 +30,7 @@ __BEGIN_DECLS
/* Hamlib internal use, see rig.c */
int network_open(hamlib_port_t *p, int default_port);
int network_close(hamlib_port_t *rp);
void network_flush(hamlib_port_t *rp);
__END_DECLS