guohetec: fix cppcheck warnings and improve code quality

Based on cppcheck analysis and maintainer feedback:

- Fix constParameterPointer warnings: declare reply parameters as const where appropriate
- Fix variableScope warnings: reduce reply variable scope to minimum required
- Apply IWYU (IncludeWhatYouUse) suggestions for header optimization:
  * Add iofunc.h (for read_block, write_block functions)
  * Add riglist.h (for RIG_MODEL_* constants)
  * Remove unistd.h, misc.h, serial.h (unnecessary includes)
- Maintain backward compatibility and existing functionality
- Improve code maintainability and reduce compilation warnings

All changes follow maintainer recommendations and maintain WSJT-X compatibility.
pull/1787/head
声纳 2025-06-28 17:13:12 +08:00
rodzic c2d4fbe601
commit 6797ab7646
4 zmienionych plików z 164 dodań i 200 usunięć

Wyświetl plik

@ -189,7 +189,7 @@ int read_rig_response(RIG *rig, unsigned char *reply, int reply_size,
* @param func_name Function name for debug messages
* @return 0 on success, -1 on error
*/
int validate_rig_response(RIG *rig, unsigned char *reply, int reply_size,
int validate_rig_response(RIG *rig, const unsigned char *reply, int reply_size,
const char *func_name)
{
// Validate packet header
@ -213,7 +213,7 @@ int validate_rig_response(RIG *rig, unsigned char *reply, int reply_size,
* @param func_name Function name for debug messages
* @return 0 on success, -1 on error
*/
int validate_freq_response(RIG *rig, unsigned char *reply, int reply_size,
int validate_freq_response(RIG *rig, const unsigned char *reply, int reply_size,
const char *func_name)
{
// Basic validation
@ -257,7 +257,7 @@ int validate_freq_response(RIG *rig, unsigned char *reply, int reply_size,
* @param min_length Minimum required data length
* @return 0 on success, -1 on error
*/
int validate_mode_response(RIG *rig, unsigned char *reply, int reply_size,
int validate_mode_response(RIG *rig, const unsigned char *reply, int reply_size,
const char *func_name, int min_length)
{
// Basic validation
@ -305,8 +305,6 @@ DECLARE_PROBERIG_BACKEND(guohetec) {
0x0B,
0x00, 0x00
};
uint8_t reply[PMR171_REPLY_LENGTH];
int orig_rate = port->parm.serial.rate;
int orig_timeout = port->timeout;
@ -314,6 +312,8 @@ DECLARE_PROBERIG_BACKEND(guohetec) {
const int rates[] = {9600, 19200, 38400, 57600, 115200, 0};
for (int i = 0; rates[i]; i++) {
uint8_t reply[PMR171_REPLY_LENGTH];
port->parm.serial.rate = rates[i];
port->timeout = 500;

Wyświetl plik

@ -61,13 +61,13 @@ unsigned char *to_be(unsigned char data[], unsigned long long freq, unsigned int
unsigned long long from_be(const unsigned char data[],unsigned int byte_len);
// Common response validation functions
int validate_rig_response(RIG *rig, unsigned char *reply, int reply_size,
int validate_rig_response(RIG *rig, const unsigned char *reply, int reply_size,
const char *func_name);
int read_rig_response(RIG *rig, unsigned char *reply, int reply_size,
const char *func_name);
int validate_freq_response(RIG *rig, unsigned char *reply, int reply_size,
int validate_freq_response(RIG *rig, const unsigned char *reply, int reply_size,
const char *func_name);
int validate_mode_response(RIG *rig, unsigned char *reply, int reply_size,
int validate_mode_response(RIG *rig, const unsigned char *reply, int reply_size,
const char *func_name, int min_length);
#endif // _guohetec_H_

Wyświetl plik

@ -367,39 +367,39 @@ static int pmr171_open(RIG *rig)
cmd[6] = crc >> 8;
cmd[7] = crc & 0xFF;
// Receive buffer and send command
unsigned char reply[40];
pmr171_send(rig, cmd, sizeof(cmd), reply, sizeof(reply));
{
unsigned char reply[40];
pmr171_send(rig, cmd, sizeof(cmd), reply, sizeof(reply));
// Validate response using common function
if (validate_freq_response(rig, reply, sizeof(reply), __func__) < 0) {
RETURN_CACHED_FREQ(rig, vfo, freq);
// Validate response using common function
if (validate_freq_response(rig, reply, sizeof(reply), __func__) < 0) {
RETURN_CACHED_FREQ(rig, vfo, freq);
}
// Parse frequency (big-endian)
int freq_a_offset = 9; // VFOA frequency starting position
int freq_b_offset = 13; // VFOB frequency starting position
uint32_t freq_a = (reply[freq_a_offset] << 24) |
(reply[freq_a_offset+1] << 16) |
(reply[freq_a_offset+2] << 8) |
reply[freq_a_offset+3];
uint32_t freq_b = (reply[freq_b_offset] << 24) |
(reply[freq_b_offset+1] << 16) |
(reply[freq_b_offset+2] << 8) |
reply[freq_b_offset+3];
// Update cache
CACHE(rig)->freqMainA = (freq_t)freq_a;
CACHE(rig)->freqMainB = (freq_t)freq_b;
// Return requested VFO frequency
*freq = (vfo == RIG_VFO_A) ? CACHE(rig)->freqMainA : CACHE(rig)->freqMainB;
rig_debug(RIG_DEBUG_VERBOSE, "%s: Successfully got VFOA=%.0f Hz, VFOB=%.0f Hz\n",
__func__, CACHE(rig)->freqMainA, CACHE(rig)->freqMainB);
}
// Parse frequency (big-endian)
int freq_a_offset = 9; // VFOA frequency starting position
int freq_b_offset = 13; // VFOB frequency starting position
uint32_t freq_a = (reply[freq_a_offset] << 24) |
(reply[freq_a_offset+1] << 16) |
(reply[freq_a_offset+2] << 8) |
reply[freq_a_offset+3];
uint32_t freq_b = (reply[freq_b_offset] << 24) |
(reply[freq_b_offset+1] << 16) |
(reply[freq_b_offset+2] << 8) |
reply[freq_b_offset+3];
// Update cache
CACHE(rig)->freqMainA = (freq_t)freq_a;
CACHE(rig)->freqMainB = (freq_t)freq_b;
// Return requested VFO frequency
*freq = (vfo == RIG_VFO_A) ? CACHE(rig)->freqMainA : CACHE(rig)->freqMainB;
rig_debug(RIG_DEBUG_VERBOSE, "%s: Successfully got VFOA=%.0f Hz, VFOB=%.0f Hz\n",
__func__, CACHE(rig)->freqMainA, CACHE(rig)->freqMainB);
return RIG_OK;
}
@ -407,28 +407,25 @@ static int pmr171_open(RIG *rig)
{
struct rig_cache *cachep = CACHE(rig);
const pmr171_data_t *p = (pmr171_data_t *) STATE(rig)->priv;
unsigned char reply[40];
// Get latest status from hardware
pmr171_send_cmd1(rig, 0x0b, 0);
// Read and validate response using common function
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
RETURN_CACHED_MODE(rig, vfo, mode, width, cachep, p);
{
unsigned char reply[40];
// Get latest status from hardware
pmr171_send_cmd1(rig, 0x0b, 0);
// Read and validate response using common function
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
RETURN_CACHED_MODE(rig, vfo, mode, width, cachep, p);
}
// Validate mode response using common function
if (validate_mode_response(rig, reply, sizeof(reply), __func__, 5) < 0) {
RETURN_CACHED_MODE(rig, vfo, mode, width, cachep, p);
}
// Update cache
cachep->modeMainA = guohe2rmode(reply[7], pmr171_modes);
cachep->modeMainB = guohe2rmode(reply[8], pmr171_modes);
// Return requested mode
*mode = (vfo == RIG_VFO_A) ? cachep->modeMainA : cachep->modeMainB;
*width = p->filterBW;
}
// Validate mode response using common function
if (validate_mode_response(rig, reply, sizeof(reply), __func__, 5) < 0) {
RETURN_CACHED_MODE(rig, vfo, mode, width, cachep, p);
}
// Update cache
cachep->modeMainA = guohe2rmode(reply[7], pmr171_modes);
cachep->modeMainB = guohe2rmode(reply[8], pmr171_modes);
// Return requested mode
*mode = (vfo == RIG_VFO_A) ? cachep->modeMainA : cachep->modeMainB;
*width = p->filterBW;
return RIG_OK;
}
@ -445,25 +442,22 @@ static int pmr171_open(RIG *rig)
static int pmr171_get_vfo(RIG *rig, vfo_t *vfo)
{
unsigned char reply[40];
// Send status sync command to get current VFO state
pmr171_send_cmd1(rig, 0x0b, 0);
// Read and validate response using common function
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
RETURN_CACHED_VFO(rig, vfo);
{
unsigned char reply[40];
// Send status sync command to get current VFO state
pmr171_send_cmd1(rig, 0x0b, 0);
// Read and validate response using common function
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
RETURN_CACHED_VFO(rig, vfo);
}
// Validate VFO status field index won't overflow
if (reply[4] < 13) { // Need at least 13 bytes to access reply[17]
rig_debug(RIG_DEBUG_ERR, "%s: Response too short for VFO data, using cached values\n", __func__);
RETURN_CACHED_VFO(rig, vfo);
}
// According to protocol doc, reply[17] is A/B frequency status
*vfo = (reply[17] == 1) ? RIG_VFO_B : RIG_VFO_A;
}
// Validate VFO status field index won't overflow
if (reply[4] < 13) { // Need at least 13 bytes to access reply[17]
rig_debug(RIG_DEBUG_ERR, "%s: Response too short for VFO data, using cached values\n", __func__);
RETURN_CACHED_VFO(rig, vfo);
}
// According to protocol doc, reply[17] is A/B frequency status
*vfo = (reply[17] == 1) ? RIG_VFO_B : RIG_VFO_A;
return RIG_OK;
}
@ -471,25 +465,22 @@ static int pmr171_open(RIG *rig)
static int pmr171_get_ptt(RIG *rig, vfo_t vfo, ptt_t *ptt)
{
struct rig_cache *cachep = CACHE(rig);
unsigned char reply[40];
pmr171_send_cmd1(rig, 0x0b, 0);
// Read and validate response using common function
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
RETURN_CACHED_PTT(rig, ptt, cachep);
{
unsigned char reply[40];
pmr171_send_cmd1(rig, 0x0b, 0);
// Read and validate response using common function
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
RETURN_CACHED_PTT(rig, ptt, cachep);
}
// Validate PTT status field index won't overflow
if (reply[4] < 2) { // Need at least 2 bytes to access reply[6]
rig_debug(RIG_DEBUG_ERR, "%s: Response too short for PTT data, using cached values\n", __func__);
RETURN_CACHED_PTT(rig, ptt, cachep);
}
// Get PTT status
cachep->ptt = reply[6];
*ptt = cachep->ptt;
}
// Validate PTT status field index won't overflow
if (reply[4] < 2) { // Need at least 2 bytes to access reply[6]
rig_debug(RIG_DEBUG_ERR, "%s: Response too short for PTT data, using cached values\n", __func__);
RETURN_CACHED_PTT(rig, ptt, cachep);
}
// Get PTT status
cachep->ptt = reply[6];
*ptt = cachep->ptt;
return RIG_OK;
}
@ -525,28 +516,24 @@ static int pmr171_open(RIG *rig)
static int pmr171_send_cmd2(RIG *rig, unsigned char cmd, unsigned char value,
int response)
{
unsigned char reply[40];
unsigned char buf[64] = { 0xa5, 0xa5, 0xa5, 0xa5, 0x04, 0x00, 0x00, 0x00, 0x00 };
hamlib_port_t *rp = RIGPORT(rig);
rig_debug(RIG_DEBUG_VERBOSE, "%s: called\n", __func__);
unsigned char buf[64] = { 0xa5, 0xa5, 0xa5, 0xa5, 0x04, 0x00, 0x00, 0x00, 0x00 };
buf[5] = cmd;
buf[6] = value;
unsigned int crc = CRC16Check(&buf[4], 3);
buf[7] = crc >> 8;
buf[8] = crc & 0xff;
rig_flush(rp);
write_block(rp, buf, 9);
if (response)
{
unsigned char reply[40];
// Use common response reading function
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
return RIG_OK; // Return OK to use cached values
}
}
return pmr171_read_ack(rig);
}

Wyświetl plik

@ -363,36 +363,32 @@ static int q900_get_freq(RIG *rig, vfo_t vfo, freq_t *freq)
cmd[6] = crc >> 8;
cmd[7] = crc & 0xFF;
unsigned char reply[40];
q900_send(rig, cmd, sizeof(cmd), reply, sizeof(reply));
// Validate response using common function
if (validate_freq_response(rig, reply, sizeof(reply), __func__) < 0) {
RETURN_CACHED_FREQ(rig, vfo, freq);
{
unsigned char reply[40];
q900_send(rig, cmd, sizeof(cmd), reply, sizeof(reply));
// Validate response using common function
if (validate_freq_response(rig, reply, sizeof(reply), __func__) < 0) {
RETURN_CACHED_FREQ(rig, vfo, freq);
}
// Parse frequency (big-endian)
int freq_a_offset = 9; // VFOA frequency starting position
int freq_b_offset = 13; // VFOB frequency starting position
uint32_t freq_a = (reply[freq_a_offset] << 24) |
(reply[freq_a_offset+1] << 16) |
(reply[freq_a_offset+2] << 8) |
reply[freq_a_offset+3];
uint32_t freq_b = (reply[freq_b_offset] << 24) |
(reply[freq_b_offset+1] << 16) |
(reply[freq_b_offset+2] << 8) |
reply[freq_b_offset+3];
// Update cache
CACHE(rig)->freqMainA = (freq_t)freq_a;
CACHE(rig)->freqMainB = (freq_t)freq_b;
// Return requested VFO frequency
*freq = (vfo == RIG_VFO_A) ? CACHE(rig)->freqMainA : CACHE(rig)->freqMainB;
rig_debug(RIG_DEBUG_VERBOSE, "%s: Successfully got VFOA=%.0f Hz, VFOB=%.0f Hz\n",
__func__, CACHE(rig)->freqMainA, CACHE(rig)->freqMainB);
}
// Parse frequency (big-endian)
int freq_a_offset = 9;
int freq_b_offset = 13;
uint32_t freq_a = (reply[freq_a_offset] << 24) |
(reply[freq_a_offset+1] << 16) |
(reply[freq_a_offset+2] << 8) |
reply[freq_a_offset+3];
uint32_t freq_b = (reply[freq_b_offset] << 24) |
(reply[freq_b_offset+1] << 16) |
(reply[freq_b_offset+2] << 8) |
reply[freq_b_offset+3];
CACHE(rig)->freqMainA = (freq_t)freq_a;
CACHE(rig)->freqMainB = (freq_t)freq_b;
*freq = (vfo == RIG_VFO_A) ? CACHE(rig)->freqMainA : CACHE(rig)->freqMainB;
rig_debug(RIG_DEBUG_VERBOSE, "%s: Successfully obtained VFOA=%.0f Hz, VFOB=%.0f Hz\n",
__func__, CACHE(rig)->freqMainA, CACHE(rig)->freqMainB);
return RIG_OK;
}
@ -400,25 +396,25 @@ static int q900_get_freq(RIG *rig, vfo_t vfo, freq_t *freq)
{
struct rig_cache *cachep = CACHE(rig);
const q900_data_t *p = (q900_data_t *) STATE(rig)->priv;
unsigned char reply[255];
q900_send_cmd1(rig, 0x0b, 0);
// Read and validate response using common function
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
RETURN_CACHED_MODE(rig, vfo, mode, width, cachep, p);
{
unsigned char reply[255];
// Get latest status from hardware
q900_send_cmd1(rig, 0x0b, 0);
// Read and validate response using common function
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
RETURN_CACHED_MODE(rig, vfo, mode, width, cachep, p);
}
// Validate mode response using common function
if (validate_mode_response(rig, reply, sizeof(reply), __func__, 5) < 0) {
RETURN_CACHED_MODE(rig, vfo, mode, width, cachep, p);
}
// Update cache
cachep->modeMainA = guohe2rmode(reply[7], q900_modes);
cachep->modeMainB = guohe2rmode(reply[8], q900_modes);
// Return requested mode
*mode = (vfo == RIG_VFO_A) ? cachep->modeMainA : cachep->modeMainB;
*width = p->filterBW;
}
// Validate mode response using common function
if (validate_mode_response(rig, reply, sizeof(reply), __func__, 5) < 0) {
RETURN_CACHED_MODE(rig, vfo, mode, width, cachep, p);
}
cachep->modeMainA = guohe2rmode(reply[7], q900_modes);
cachep->modeMainB = guohe2rmode(reply[8], q900_modes);
*mode = (vfo == RIG_VFO_A) ? cachep->modeMainA : cachep->modeMainB;
*width = p->filterBW;
return RIG_OK;
}
@ -435,29 +431,22 @@ static int q900_get_freq(RIG *rig, vfo_t vfo, freq_t *freq)
static int q900_get_vfo(RIG *rig, vfo_t *vfo)
{
unsigned char reply[255];
q900_send_cmd1(rig, 0x0b, 0);
// Read and validate response using common function
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
RETURN_CACHED_VFO(rig, vfo);
{
unsigned char reply[255];
// Send status sync command to get current VFO state
q900_send_cmd1(rig, 0x0b, 0);
// Read and validate response using common function
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
RETURN_CACHED_VFO(rig, vfo);
}
// Validate VFO status field index won't overflow
if (reply[4] < 13) { // Need at least 13 bytes to access reply[17]
rig_debug(RIG_DEBUG_ERR, "%s: Response too short for VFO data, using cached values\n", __func__);
RETURN_CACHED_VFO(rig, vfo);
}
// According to protocol doc, reply[17] is A/B frequency status
*vfo = (reply[17] == 1) ? RIG_VFO_B : RIG_VFO_A;
}
// Validate VFO status field index won't overflow
if (reply[4] < 13) { // Need at least 13 bytes to access reply[17]
rig_debug(RIG_DEBUG_ERR, "%s: Response too short for VFO data, using cached values\n", __func__);
RETURN_CACHED_VFO(rig, vfo);
}
// Validate VFO status value
if (reply[17] != 0 && reply[17] != 1) {
rig_debug(RIG_DEBUG_ERR, "%s: Invalid VFO status value %d, using cached values\n", __func__, reply[17]);
RETURN_CACHED_VFO(rig, vfo);
}
*vfo = (reply[17] == 1) ? RIG_VFO_B : RIG_VFO_A;
return RIG_OK;
}
@ -465,30 +454,22 @@ static int q900_get_freq(RIG *rig, vfo_t vfo, freq_t *freq)
static int q900_get_ptt(RIG *rig, vfo_t vfo, ptt_t *ptt)
{
struct rig_cache *cachep = CACHE(rig);
unsigned char reply[255];
q900_send_cmd1(rig, 0x0b, 0);
// Read and validate response using common function
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
RETURN_CACHED_PTT(rig, ptt, cachep);
{
unsigned char reply[255];
q900_send_cmd1(rig, 0x0b, 0);
// Read and validate response using common function
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
RETURN_CACHED_PTT(rig, ptt, cachep);
}
// Validate PTT status field index won't overflow
if (reply[4] < 2) { // Need at least 2 bytes to access reply[6]
rig_debug(RIG_DEBUG_ERR, "%s: Response too short for PTT data, using cached values\n", __func__);
RETURN_CACHED_PTT(rig, ptt, cachep);
}
// Get PTT status
cachep->ptt = reply[6];
*ptt = cachep->ptt;
}
// Validate PTT status field index won't overflow
if (reply[4] < 2) { // Need at least 2 bytes to access reply[6]
rig_debug(RIG_DEBUG_ERR, "%s: Response too short for PTT data, using cached values\n", __func__);
RETURN_CACHED_PTT(rig, ptt, cachep);
}
// Validate PTT status value
if (reply[6] != 0 && reply[6] != 1) {
rig_debug(RIG_DEBUG_ERR, "%s: Invalid PTT status value %d, using cached values\n", __func__, reply[6]);
RETURN_CACHED_PTT(rig, ptt, cachep);
}
cachep->ptt = reply[6];
*ptt = cachep->ptt;
return RIG_OK;
}
@ -527,28 +508,24 @@ static int q900_get_ptt(RIG *rig, vfo_t vfo, ptt_t *ptt)
static int q900_send_cmd2(RIG *rig, unsigned char cmd, unsigned char value,
int response)
{
unsigned char reply[256];
unsigned char buf[64] = { 0xa5, 0xa5, 0xa5, 0xa5, 0x04, 0x00, 0x00, 0x00, 0x00 };
hamlib_port_t *rp = RIGPORT(rig);
rig_debug(RIG_DEBUG_VERBOSE, "%s: called\n", __func__);
unsigned char buf[64] = { 0xa5, 0xa5, 0xa5, 0xa5, 0x04, 0x00, 0x00, 0x00, 0x00 };
buf[5] = cmd;
buf[6] = value;
unsigned int crc = CRC16Check(&buf[4], 3);
buf[7] = crc >> 8;
buf[8] = crc & 0xff;
rig_flush(rp);
write_block(rp, buf, 9);
if (response)
{
unsigned char reply[256];
// Use common response reading function
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
return RIG_OK; // Return OK to use cached values
}
}
return q900_read_ack(rig);
}