diff --git a/rigs/guohetec/guohetec.c b/rigs/guohetec/guohetec.c index 794425122..d5019c39a 100644 --- a/rigs/guohetec/guohetec.c +++ b/rigs/guohetec/guohetec.c @@ -1,11 +1,29 @@ -#include #include #include -#include "serial.h" +#include "iofunc.h" #include "register.h" +#include "riglist.h" #include "guohetec.h" -#include "misc.h" +// Common response validation function implementations +int validate_packet_header(const unsigned char *reply, const char *func_name) +{ + if (reply[0] != 0xA5 || reply[1] != 0xA5 || + reply[2] != 0xA5 || reply[3] != 0xA5) { + rig_debug(RIG_DEBUG_ERR, "%s: Invalid packet header, using cached values\n", func_name); + return -1; + } + return 0; +} + +int validate_data_length(const unsigned char *reply, int reply_size, const char *func_name) +{ + if (reply[4] == 0 || reply[4] > reply_size - 5) { + rig_debug(RIG_DEBUG_ERR, "%s: Invalid data length %d, using cached values\n", func_name, reply[4]); + return -1; + } + return 0; +} // CRC16/CCITT-FALSE uint16_t CRC16Check(const unsigned char *buf, int len) @@ -175,10 +193,14 @@ int validate_rig_response(RIG *rig, unsigned char *reply, int reply_size, const char *func_name) { // Validate packet header - VALIDATE_PACKET_HEADER(reply, func_name); + if (validate_packet_header(reply, func_name) < 0) { + return -1; + } // Validate data length - VALIDATE_DATA_LENGTH(reply, reply_size, func_name); + if (validate_data_length(reply, reply_size, func_name) < 0) { + return -1; + } return 0; } diff --git a/rigs/guohetec/guohetec.h b/rigs/guohetec/guohetec.h index 90965a129..589e2e3f7 100644 --- a/rigs/guohetec/guohetec.h +++ b/rigs/guohetec/guohetec.h @@ -31,21 +31,13 @@ return RIG_OK; \ } while(0) -// Common response validation macros -#define VALIDATE_PACKET_HEADER(reply, func_name) do { \ - if (reply[0] != 0xA5 || reply[1] != 0xA5 || \ - reply[2] != 0xA5 || reply[3] != 0xA5) { \ - rig_debug(RIG_DEBUG_ERR, "%s: Invalid packet header, using cached values\n", func_name); \ - return -1; \ - } \ -} while(0) +// Common response validation function declarations +int validate_packet_header(const unsigned char *reply, const char *func_name); +int validate_data_length(const unsigned char *reply, int reply_size, const char *func_name); -#define VALIDATE_DATA_LENGTH(reply, reply_size, func_name) do { \ - if (reply[4] == 0 || reply[4] > (reply_size) - 5) { \ - rig_debug(RIG_DEBUG_ERR, "%s: Invalid data length %d, using cached values\n", func_name, reply[4]); \ - return -1; \ - } \ -} while(0) +// Keep the macro for backward compatibility +#define VALIDATE_PACKET_HEADER(reply, func_name) validate_packet_header(reply, func_name) +#define VALIDATE_DATA_LENGTH(reply, reply_size, func_name) validate_data_length(reply, reply_size, func_name) #define VALIDATE_READ_RESULT(ret, expected, func_name) do { \ if (ret < 0) { \ diff --git a/rigs/guohetec/pmr171.c b/rigs/guohetec/pmr171.c index e1ede9adf..51183af98 100644 --- a/rigs/guohetec/pmr171.c +++ b/rigs/guohetec/pmr171.c @@ -7,14 +7,12 @@ #endif #include "hamlib/rig.h" -#include "serial.h" +#include "iofunc.h" #include "guohetec.h" #include "cache.h" -#include "misc.h" #include "tones.h" #include "bandplan.h" #include "cal.h" -#include typedef struct pmr171_data_s @@ -39,8 +37,6 @@ typedef struct pmr171_data_s char SWR; } pmr171_data_t; - -#define GUOHE_MODE_TABLE_MAX 8 static rmode_t pmr171_modes[GUOHE_MODE_TABLE_MAX] = { RIG_MODE_USB, @@ -598,17 +594,15 @@ static int pmr171_open(RIG *rig) return RIG_OK; } - dump_hex(reply, 16); - - // Update cache with requested frequency - if (vfo == RIG_VFO_B) - { - CACHE(rig)->freqMainB = freq; - } - else - { - CACHE(rig)->freqMainA = freq; - } + // Update cache with requested frequency + if (vfo == RIG_VFO_B) + { + CACHE(rig)->freqMainB = freq; + } + else + { + CACHE(rig)->freqMainA = freq; + } return RIG_OK; } @@ -684,8 +678,6 @@ static int pmr171_open(RIG *rig) return RIG_OK; } - dump_hex(reply, reply[4] + 5); - // Update cache with response data CACHE(rig)->modeMainA = guohe2rmode(reply[6], pmr171_modes); CACHE(rig)->modeMainB = guohe2rmode(reply[7], pmr171_modes); diff --git a/rigs/guohetec/q900.c b/rigs/guohetec/q900.c index 7bdcf8392..059106f67 100644 --- a/rigs/guohetec/q900.c +++ b/rigs/guohetec/q900.c @@ -7,14 +7,12 @@ #endif #include "hamlib/rig.h" -#include "serial.h" +#include "iofunc.h" #include "guohetec.h" #include "cache.h" -#include "misc.h" #include "tones.h" #include "bandplan.h" #include "cal.h" -#include typedef struct q900_data_s { @@ -38,8 +36,6 @@ typedef struct q900_data_s char SWR; } q900_data_t; - -#define GUOHE_MODE_TABLE_MAX 8 static rmode_t q900_modes[GUOHE_MODE_TABLE_MAX] = { RIG_MODE_USB, @@ -599,17 +595,15 @@ static int q900_get_ptt(RIG *rig, vfo_t vfo, ptt_t *ptt) return RIG_OK; } - dump_hex(reply, 16); - - // Update cache with requested frequency - if (vfo == RIG_VFO_B) - { - CACHE(rig)->freqMainB = freq; - } - else - { - CACHE(rig)->freqMainA = freq; - } + // Update cache with requested frequency + if (vfo == RIG_VFO_B) + { + CACHE(rig)->freqMainB = freq; + } + else + { + CACHE(rig)->freqMainA = freq; + } return RIG_OK; } @@ -712,8 +706,6 @@ static int q900_get_ptt(RIG *rig, vfo_t vfo, ptt_t *ptt) return RIG_OK; } - dump_hex(reply, reply[4] + 5); - // Update cache with response data CACHE(rig)->modeMainA = guohe2rmode(reply[6], q900_modes); CACHE(rig)->modeMainB = guohe2rmode(reply[7], q900_modes); @@ -799,3 +791,4 @@ static int q900_set_ptt(RIG *rig, vfo_t vfo, ptt_t ptt) } /* ---------------------------------------------------------------------- */ +