@@ -179,8 +179,9 @@ static lgw_context_t lgw_context = {
179
179
/* File handle to write debug logs */
180
180
FILE * log_file = NULL ;
181
181
182
- /* File descriptor to I2C linux device */
183
- int lgw_i2c_target = -1 ;
182
+ /* I2C temperature sensor handles */
183
+ static int ts_fd = -1 ;
184
+ static uint8_t ts_addr = 0xFF ;
184
185
185
186
/* -------------------------------------------------------------------------- */
186
187
/* --- PRIVATE FUNCTIONS DECLARATION ---------------------------------------- */
@@ -233,6 +234,7 @@ int lgw_board_setconf(struct lgw_conf_board_s * conf) {
233
234
CONTEXT_BOARD .clksrc = conf -> clksrc ;
234
235
CONTEXT_BOARD .full_duplex = conf -> full_duplex ;
235
236
strncpy (CONTEXT_SPI , conf -> spidev_path , sizeof CONTEXT_SPI );
237
+ CONTEXT_SPI [sizeof CONTEXT_SPI - 1 ] = '\0' ; /* ensure string termination */
236
238
237
239
DEBUG_PRINTF ("Note: board configuration: spidev_path: %s, lorawan_public:%d, clksrc:%d, full_duplex:%d\n" , CONTEXT_SPI ,
238
240
CONTEXT_LWAN_PUBLIC ,
@@ -555,7 +557,8 @@ int lgw_debug_setconf(struct lgw_conf_debug_s * conf) {
555
557
}
556
558
557
559
if (conf -> log_file_name != NULL ) {
558
- strncpy (CONTEXT_DEBUG .log_file_name , conf -> log_file_name , strlen (conf -> log_file_name ));
560
+ strncpy (CONTEXT_DEBUG .log_file_name , conf -> log_file_name , sizeof CONTEXT_DEBUG .log_file_name );
561
+ CONTEXT_DEBUG .log_file_name [sizeof CONTEXT_DEBUG .log_file_name - 1 ] = '\0' ; /* ensure string termination */
559
562
}
560
563
561
564
return LGW_HAL_SUCCESS ;
@@ -564,7 +567,7 @@ int lgw_debug_setconf(struct lgw_conf_debug_s * conf) {
564
567
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
565
568
566
569
int lgw_start (void ) {
567
- int i , err , err_id_1 , err_id_2 ;
570
+ int i , err ;
568
571
int reg_stat ;
569
572
570
573
if (CONTEXT_STARTED == true) {
@@ -713,18 +716,21 @@ int lgw_start(void) {
713
716
dbg_init_gpio ();
714
717
#endif
715
718
716
- /* Open I2C */
717
- err_id_1 = i2c_linuxdev_open (I2C_DEVICE , I2C_PORT_TEMP_SENSOR_1 , & lgw_i2c_target );
718
- err_id_2 = i2c_linuxdev_open (I2C_DEVICE , I2C_PORT_TEMP_SENSOR_2 , & lgw_i2c_target );
719
- if (((err_id_1 != 0 ) || (lgw_i2c_target <= 0 )) && ((err_id_2 != 0 ) || (lgw_i2c_target <= 0 ))) {
720
- printf ("ERROR: failed to open I2C device %s (err=%i)\n" , I2C_DEVICE , err );
721
- return LGW_HAL_ERROR ;
722
- }
723
-
724
- /* Configure the CoreCell temperature sensor */
725
- if (lgw_stts751_configure () != LGW_I2C_SUCCESS ) {
726
- printf ("ERROR: failed to configure temperature sensor\n" );
727
- return LGW_HAL_ERROR ;
719
+ /* Try to configure temperature sensor STTS751-0DP3F */
720
+ ts_addr = I2C_PORT_TEMP_SENSOR_0 ;
721
+ i2c_linuxdev_open (I2C_DEVICE , ts_addr , & ts_fd );
722
+ err = stts751_configure (ts_fd , ts_addr );
723
+ if (err != LGW_I2C_SUCCESS ) {
724
+ i2c_linuxdev_close (ts_fd );
725
+ ts_fd = -1 ;
726
+ /* Not found, try to configure temperature sensor STTS751-1DP3F */
727
+ ts_addr = I2C_PORT_TEMP_SENSOR_1 ;
728
+ i2c_linuxdev_open (I2C_DEVICE , ts_addr , & ts_fd );
729
+ err = stts751_configure (ts_fd , ts_addr );
730
+ if (err != LGW_I2C_SUCCESS ) {
731
+ printf ("ERROR: failed to configure the temperature sensor\n" );
732
+ return LGW_HAL_ERROR ;
733
+ }
728
734
}
729
735
730
736
/* set hal state */
@@ -753,10 +759,9 @@ int lgw_stop(void) {
753
759
lgw_disconnect ();
754
760
755
761
DEBUG_MSG ("INFO: Closing I2C\n" );
756
- err = i2c_linuxdev_close (lgw_i2c_target );
762
+ err = i2c_linuxdev_close (ts_fd );
757
763
if (err != 0 ) {
758
764
printf ("ERROR: failed to close I2C device (err=%i)\n" , err );
759
- /* TODO: return error or not ? */
760
765
}
761
766
762
767
CONTEXT_STARTED = false;
@@ -790,7 +795,7 @@ int lgw_receive(uint8_t max_pkt, struct lgw_pkt_rx_s *pkt_data) {
790
795
}
791
796
792
797
/* Get the current temperature for further RSSI compensation : TODO */
793
- res = lgw_stts751_get_temperature ( & current_temperature );
798
+ res = stts751_get_temperature ( ts_fd , ts_addr , & current_temperature );
794
799
if (res != LGW_I2C_SUCCESS ) {
795
800
printf ("ERROR: failed to get current temperature\n" );
796
801
return LGW_HAL_ERROR ;
@@ -944,6 +949,8 @@ int lgw_abort_tx(uint8_t rf_chain) {
944
949
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
945
950
946
951
int lgw_get_trigcnt (uint32_t * trig_cnt_us ) {
952
+ CHECK_NULL (trig_cnt_us );
953
+
947
954
* trig_cnt_us = sx1302_timestamp_counter (true);
948
955
949
956
return LGW_HAL_SUCCESS ;
@@ -952,6 +959,8 @@ int lgw_get_trigcnt(uint32_t* trig_cnt_us) {
952
959
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
953
960
954
961
int lgw_get_instcnt (uint32_t * inst_cnt_us ) {
962
+ CHECK_NULL (inst_cnt_us );
963
+
955
964
* inst_cnt_us = sx1302_timestamp_counter (false);
956
965
957
966
return LGW_HAL_SUCCESS ;
@@ -960,6 +969,8 @@ int lgw_get_instcnt(uint32_t* inst_cnt_us) {
960
969
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
961
970
962
971
int lgw_get_eui (uint64_t * eui ) {
972
+ CHECK_NULL (eui );
973
+
963
974
if (sx1302_get_eui (eui ) != LGW_REG_SUCCESS ) {
964
975
return LGW_HAL_ERROR ;
965
976
}
@@ -968,6 +979,18 @@ int lgw_get_eui(uint64_t* eui) {
968
979
969
980
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
970
981
982
+ int lgw_get_temperature (float * temperature ) {
983
+ CHECK_NULL (temperature );
984
+
985
+ if (stts751_get_temperature (ts_fd , ts_addr , temperature ) != LGW_I2C_SUCCESS ) {
986
+ return LGW_HAL_ERROR ;
987
+ }
988
+
989
+ return LGW_HAL_SUCCESS ;
990
+ }
991
+
992
+ /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
993
+
971
994
const char * lgw_version_info () {
972
995
return lgw_version_string ;
973
996
}
0 commit comments