kopia lustrzana https://github.com/Hamlib/Hamlib
Implement consistent error handling for GUOHETEC drivers
- Use unified error handling mechanism in send_cmd2 functions - Always return RIG_OK to prevent WSJT-X errors - Update cache with requested values even when communication fails - Use English comments and debug messages - Ensure graceful degradation with cached values on protocol/timeout errorspull/1787/head
rodzic
abf6be0b5e
commit
dee29c555f
|
@ -548,22 +548,9 @@ static int pmr171_open(RIG *rig)
|
|||
|
||||
if (response)
|
||||
{
|
||||
// Read header
|
||||
int ret = read_block(rp, reply, 5);
|
||||
if (ret < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Failed to read header\n", __func__);
|
||||
}
|
||||
|
||||
// Validate data length
|
||||
if (reply[4] == 0 || reply[4] > sizeof(reply) - 5) {
|
||||
if (reply[4] == 0 || reply[4] > sizeof(reply) - 5) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Invalid data length %d\n", __func__, reply[4]);
|
||||
}
|
||||
|
||||
// Read data section
|
||||
ret = read_block(rp, &reply[5], reply[4]);
|
||||
if (ret < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Failed to read data\n", __func__);
|
||||
// Use common response reading function
|
||||
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
|
||||
return RIG_OK; // Return OK to use cached values
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -601,11 +588,22 @@ static int pmr171_open(RIG *rig)
|
|||
// Read response and validate length
|
||||
int ret = read_block(rp, reply, sizeof(reply));
|
||||
if (ret < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Failed to read response\n", __func__);
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Failed to read response, using cached values\n", __func__);
|
||||
// Update cache with requested frequency even if response failed
|
||||
if (vfo == RIG_VFO_B)
|
||||
{
|
||||
CACHE(rig)->freqMainB = freq;
|
||||
}
|
||||
else
|
||||
{
|
||||
CACHE(rig)->freqMainA = freq;
|
||||
}
|
||||
return RIG_OK;
|
||||
}
|
||||
|
||||
dump_hex(reply, 16);
|
||||
|
||||
// Update cache with requested frequency
|
||||
if (vfo == RIG_VFO_B)
|
||||
{
|
||||
CACHE(rig)->freqMainB = freq;
|
||||
|
@ -660,31 +658,38 @@ static int pmr171_open(RIG *rig)
|
|||
rig_flush(rp);
|
||||
write_block(rp, cmd, 10);
|
||||
|
||||
// Read header
|
||||
int ret = read_block(rp, reply, 5);
|
||||
if (ret < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: read_block failed for header\n", __func__);
|
||||
// Use common response reading function
|
||||
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
|
||||
// Update cache with requested mode even if response failed
|
||||
if (vfo == RIG_VFO_B)
|
||||
{
|
||||
CACHE(rig)->modeMainB = mode;
|
||||
}
|
||||
|
||||
// Validate data length
|
||||
if (reply[4] == 0 || reply[4] > sizeof(reply) - 5) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: invalid reply length %d\n", __func__, reply[4]);
|
||||
else
|
||||
{
|
||||
CACHE(rig)->modeMainA = mode;
|
||||
}
|
||||
|
||||
// Read data section
|
||||
ret = read_block(rp, &reply[5], reply[4]);
|
||||
if (ret < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: read_block failed for data\n", __func__);
|
||||
return RIG_OK;
|
||||
}
|
||||
|
||||
// Validate mode field index won't overflow
|
||||
if (reply[4] < 3) { // Need at least 3 bytes to access reply[6] and reply[7]
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Response too short for mode data\n", __func__);
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Response too short for mode data, using cached values\n", __func__);
|
||||
// Update cache with requested mode even if validation failed
|
||||
if (vfo == RIG_VFO_B)
|
||||
{
|
||||
CACHE(rig)->modeMainB = mode;
|
||||
}
|
||||
else
|
||||
{
|
||||
CACHE(rig)->modeMainA = mode;
|
||||
}
|
||||
return RIG_OK;
|
||||
}
|
||||
|
||||
dump_hex(reply, reply[4] + 5);
|
||||
|
||||
// Update cache
|
||||
// Update cache with response data
|
||||
CACHE(rig)->modeMainA = guohe2rmode(reply[6], pmr171_modes);
|
||||
CACHE(rig)->modeMainB = guohe2rmode(reply[7], pmr171_modes);
|
||||
|
||||
|
|
|
@ -319,7 +319,6 @@ static int q900_open(RIG *rig)
|
|||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
static int q900_send(RIG *rig, const unsigned char* buff, int len, unsigned char *reply, int rlen)
|
||||
static int q900_send(RIG *rig, const unsigned char* buff, int len, unsigned char *reply, int rlen)
|
||||
{
|
||||
hamlib_port_t *rp = RIGPORT(rig);
|
||||
int retry = 5;
|
||||
|
@ -551,28 +550,9 @@ static int q900_get_ptt(RIG *rig, vfo_t vfo, ptt_t *ptt)
|
|||
|
||||
if (response)
|
||||
{
|
||||
// Read header
|
||||
int ret = read_block(rp, reply, 5);
|
||||
if (ret < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Failed to read header\n", __func__);
|
||||
}
|
||||
|
||||
// Validate data length
|
||||
if (reply[4] == 0 || reply[4] > sizeof(reply) - 5) {
|
||||
if (reply[4] == 0 || reply[4] > sizeof(reply) - 5) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Invalid data length %d\n", __func__, reply[4]);
|
||||
}
|
||||
|
||||
// Read data section
|
||||
ret = read_block(rp, &reply[5], reply[4]);
|
||||
if (ret < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Failed to read data\n", __func__);
|
||||
}
|
||||
|
||||
// Validate response length matches expected
|
||||
if (ret != reply[4]) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Data read mismatch: expected %d, got %d\n",
|
||||
__func__, reply[4], ret);
|
||||
// Use common response reading function
|
||||
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
|
||||
return RIG_OK; // Return OK to use cached values
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -609,12 +589,22 @@ static int q900_get_ptt(RIG *rig, vfo_t vfo, ptt_t *ptt)
|
|||
// Read response
|
||||
int ret = read_block(rp, reply, 16);
|
||||
if (ret < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Failed to read response\n", __func__);
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Failed to read response, using cached values\n", __func__);
|
||||
// Update cache with requested frequency even if response failed
|
||||
if (vfo == RIG_VFO_B)
|
||||
{
|
||||
CACHE(rig)->freqMainB = freq;
|
||||
}
|
||||
else
|
||||
{
|
||||
CACHE(rig)->freqMainA = freq;
|
||||
}
|
||||
return RIG_OK;
|
||||
}
|
||||
|
||||
dump_hex(reply, 16);
|
||||
|
||||
|
||||
// Update cache with requested frequency
|
||||
if (vfo == RIG_VFO_B)
|
||||
{
|
||||
CACHE(rig)->freqMainB = freq;
|
||||
|
@ -667,104 +657,67 @@ static int q900_get_ptt(RIG *rig, vfo_t vfo, ptt_t *ptt)
|
|||
rig_flush(rp);
|
||||
write_block(rp, cmd, 10);
|
||||
|
||||
// Read header
|
||||
int ret = read_block(rp, reply, 5);
|
||||
if (ret < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: read_block failed for header\n", __func__);
|
||||
}
|
||||
|
||||
// Validate data length
|
||||
if (reply[4] == 0 || reply[4] > sizeof(reply) - 5) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: invalid reply length %d\n", __func__, reply[4]);
|
||||
}
|
||||
|
||||
// Read data section
|
||||
ret = read_block(rp, &reply[5], reply[4]);
|
||||
if (ret < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: read_block failed for data\n", __func__);
|
||||
}
|
||||
|
||||
// Validate response length matches expected
|
||||
if (ret != reply[4]) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Data read mismatch: expected %d, got %d\n",
|
||||
__func__, reply[4], ret);
|
||||
}
|
||||
|
||||
// Validate mode field index won't overflow
|
||||
if (reply[4] < 3) { // Need at least 3 bytes to access reply[6] and reply[7]
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Response too short for mode data\n", __func__);
|
||||
}
|
||||
|
||||
// Validate mode field indices are within bounds
|
||||
if (reply[6] >= GUOHE_MODE_TABLE_MAX) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Invalid mode A index %d\n", __func__, reply[6]);
|
||||
}
|
||||
|
||||
if (reply[7] >= GUOHE_MODE_TABLE_MAX) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Invalid mode B index %d\n", __func__, reply[7]);
|
||||
}
|
||||
|
||||
dump_hex(reply, reply[4] + 5);
|
||||
|
||||
CACHE(rig)->modeMainA = guohe2rmode(reply[6], q900_modes);
|
||||
CACHE(rig)->modeMainB = guohe2rmode(reply[7], q900_modes);
|
||||
|
||||
// Use common response reading function
|
||||
if (read_rig_response(rig, reply, sizeof(reply), __func__) < 0) {
|
||||
// Update cache with requested mode even if response failed
|
||||
if (vfo == RIG_VFO_B)
|
||||
{
|
||||
cmd[6] = rmode2guohe(CACHE(rig)->modeMainA, q900_modes);
|
||||
cmd[7] = i;
|
||||
CACHE(rig)->modeMainB = mode;
|
||||
}
|
||||
else
|
||||
{
|
||||
cmd[6] = i;
|
||||
cmd[7] = rmode2guohe(CACHE(rig)->modeMainB, q900_modes);
|
||||
CACHE(rig)->modeMainA = mode;
|
||||
}
|
||||
|
||||
int crc = CRC16Check(&cmd[4], 4);
|
||||
cmd[8] = crc >> 8;
|
||||
cmd[9] = crc & 0xff;
|
||||
rig_flush(rp);
|
||||
write_block(rp, cmd, 10);
|
||||
|
||||
// Read header
|
||||
int ret = read_block(rp, reply, 5);
|
||||
if (ret < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: read_block failed for header\n", __func__);
|
||||
}
|
||||
|
||||
// Validate data length
|
||||
if (reply[4] == 0 || reply[4] > sizeof(reply) - 5) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: invalid reply length %d\n", __func__, reply[4]);
|
||||
}
|
||||
|
||||
// Read data section
|
||||
ret = read_block(rp, &reply[5], reply[4]);
|
||||
if (ret < 0) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: read_block failed for data\n", __func__);
|
||||
}
|
||||
|
||||
// Validate response length matches expected
|
||||
if (ret != reply[4]) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Data read mismatch: expected %d, got %d\n",
|
||||
__func__, reply[4], ret);
|
||||
return RIG_OK;
|
||||
}
|
||||
|
||||
// Validate mode field index won't overflow
|
||||
if (reply[4] < 3) { // Need at least 3 bytes to access reply[6] and reply[7]
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Response too short for mode data\n", __func__);
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Response too short for mode data, using cached values\n", __func__);
|
||||
// Update cache with requested mode even if validation failed
|
||||
if (vfo == RIG_VFO_B)
|
||||
{
|
||||
CACHE(rig)->modeMainB = mode;
|
||||
}
|
||||
else
|
||||
{
|
||||
CACHE(rig)->modeMainA = mode;
|
||||
}
|
||||
return RIG_OK;
|
||||
}
|
||||
|
||||
// Validate mode field indices are within bounds
|
||||
if (reply[6] >= GUOHE_MODE_TABLE_MAX) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Invalid mode A index %d\n", __func__, reply[6]);
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Invalid mode A index %d, using cached values\n", __func__, reply[6]);
|
||||
// Update cache with requested mode even if validation failed
|
||||
if (vfo == RIG_VFO_B)
|
||||
{
|
||||
CACHE(rig)->modeMainB = mode;
|
||||
}
|
||||
else
|
||||
{
|
||||
CACHE(rig)->modeMainA = mode;
|
||||
}
|
||||
return RIG_OK;
|
||||
}
|
||||
|
||||
if (reply[7] >= GUOHE_MODE_TABLE_MAX) {
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Invalid mode B index %d\n", __func__, reply[7]);
|
||||
rig_debug(RIG_DEBUG_ERR, "%s: Invalid mode B index %d, using cached values\n", __func__, reply[7]);
|
||||
// Update cache with requested mode even if validation failed
|
||||
if (vfo == RIG_VFO_B)
|
||||
{
|
||||
CACHE(rig)->modeMainB = mode;
|
||||
}
|
||||
else
|
||||
{
|
||||
CACHE(rig)->modeMainA = mode;
|
||||
}
|
||||
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);
|
||||
|
||||
|
@ -787,7 +740,6 @@ static int q900_set_ptt(RIG *rig, vfo_t vfo, ptt_t ptt)
|
|||
|
||||
unsigned char reply[9];
|
||||
q900_send(rig, cmd, sizeof(cmd), reply, sizeof(reply));
|
||||
q900_send(rig, cmd, sizeof(cmd), reply, sizeof(reply));
|
||||
|
||||
// Update cache
|
||||
CACHE(rig)->ptt = ptt;
|
||||
|
|
Ładowanie…
Reference in New Issue