Lora-net / sx1302_hal

SX1302/SX1303 Hardware Abstraction Layer and Tools (packet forwarder...)
Other
219 stars 272 forks source link

i2c_linuxdev_close called twice if no temperature sensor found? #76

Closed brocaar closed 2 years ago

brocaar commented 2 years ago

I have a shield which does not have a temperature sensor:

Opening SPI communication interface
Note: chip version is 0x10 (v1.0)
INFO: Configuring SX1250_0 in single input mode
INFO: using legacy timestamp
INFO: LoRa Service modem: configuring preamble size to 8 symbols
ARB: dual demodulation disabled for all SF
INFO: no temperature sensor found on port 0x39
INFO: no temperature sensor found on port 0x3B
INFO: no temperature sensor found on port 0x38
ERROR: no temperature sensor found.

When calling lgw_stop, an error is returned (both in the logs as by the function):

Closing SPI communication interface
ERROR: failed to close I2C temperature sensor device (err=-1)

I believe this is because in case no temperature sensor was found, i2c_linuxdev_close is called and ts_fd is set to -1:

            if (err != LGW_I2C_SUCCESS) {
                printf("INFO: no temperature sensor found on port 0x%02X\n", ts_addr);
                i2c_linuxdev_close(ts_fd);
                ts_fd = -1;
            } else {
                printf("INFO: found temperature sensor on port 0x%02X\n", ts_addr);
                break;
            }

In the lgw_stop function, i2c_linuxdev_close is called again without checking if ts_fd was set to -1:

    if (CONTEXT_COM_TYPE == LGW_COM_SPI) {
        DEBUG_MSG("INFO: Closing I2C for temperature sensor\n");
        x = i2c_linuxdev_close(ts_fd);
        if (x != 0) {
            printf("ERROR: failed to close I2C temperature sensor device (err=%i)\n", x);
            err = LGW_HAL_ERROR;
        }

        if (CONTEXT_BOARD.full_duplex == true) {
            DEBUG_MSG("INFO: Closing I2C for AD5338R\n");
            x = i2c_linuxdev_close(ad_fd);
            if (x != 0) {
                printf("ERROR: failed to close I2C AD5338R device (err=%i)\n", x);
                err = LGW_HAL_ERROR;
            }
        }
    }

Should this be wrapped in a if (ts_fd != -1) { ... }?

brocaar commented 2 years ago

Nevermind, I believe in case no temp. sensor was found, the HAL immediately returns an error. The board I was using had already a patch to work around the missing temp. sensor.