1010
1111LOG_MODULE_REGISTER (sensor_module , CONFIG_SENSOR_MODULE_LOG_LEVEL );
1212
13- /* Macro to validate sensor type enum values */
14- #define IS_VALID_SENSOR_TYPE (type ) ((int)(type) >= 0 && (type) < SENSOR_TYPE_COUNT)
15-
1613/* Macro for type-safe sensor state object initialization */
1714#define SENSOR_STATE_OBJECT_INIT () \
18- (struct sensor_state_object) \
1915 { \
2016 .ctx = {0}, .current_state = SENSOR_MODULE_STATE_INIT, .current_data = {0}, \
21- .error_count = 0, .max_retries = 0, .last_read_time = 0, .read_timeout_ms = 0, \
22- IF_ENABLED(CONFIG_SENSOR_MODULE_WARMUP_ENABLE, \
23- (.sensor_init_time = 0, \
24- .sensor_warmup_complete = {false})) \
17+ .error_count = 0, .max_retries = 0, .last_read_time = 0, .read_timeout_ms = 0 \
2518 }
2619
2720/* ZBUS subscriber for sensor requests */
@@ -63,12 +56,6 @@ struct sensor_state_object {
6356 /* Timing */
6457 int64_t last_read_time ;
6558 int64_t read_timeout_ms ;
66-
67- #ifdef CONFIG_SENSOR_MODULE_WARMUP_ENABLE
68- /* Sensor warmup tracking */
69- int64_t sensor_init_time ;
70- bool sensor_warmup_complete [SENSOR_TYPE_COUNT ];
71- #endif
7259};
7360
7461/* Forward declarations for state functions */
@@ -128,11 +115,6 @@ static void update_sensor_health(struct sensor_health *health, bool success);
128115static const char * get_sensor_name (enum sensor_type type );
129116static void sensor_set_state (struct sensor_state_object * ctx , enum sensor_module_state new_state );
130117
131- #ifdef CONFIG_SENSOR_MODULE_WARMUP_ENABLE
132- static bool is_sensor_warmup_complete (enum sensor_type type );
133- static int64_t get_sensor_warmup_time (enum sensor_type type );
134- #endif
135-
136118#ifdef CONFIG_CCS811_ENV_COMPENSATION
137119static int update_ccs811_env_data (const struct sensor_value * temp , const struct sensor_value * hum );
138120static void handle_ccs811_env_compensation (const struct sensor_value * temp ,
@@ -145,7 +127,7 @@ int sensor_module_init(void)
145127 int ret ;
146128
147129 /* Initialize state machine context */
148- sensor_state_obj = SENSOR_STATE_OBJECT_INIT ();
130+ sensor_state_obj = ( struct sensor_state_object ) SENSOR_STATE_OBJECT_INIT ();
149131
150132 sensor_state_obj .max_retries = CONFIG_SENSOR_MODULE_MAX_RETRIES ;
151133 sensor_state_obj .read_timeout_ms = CONFIG_SENSOR_MODULE_READ_TIMEOUT_MS ;
@@ -264,14 +246,6 @@ static void sensor_state_init_run(void *obj)
264246 return ;
265247 }
266248
267- #ifdef CONFIG_SENSOR_MODULE_WARMUP_ENABLE
268- /* Initialize warmup timing */
269- ctx -> sensor_init_time = k_uptime_get ();
270- for (int i = 0 ; i < SENSOR_TYPE_COUNT ; i ++ ) {
271- ctx -> sensor_warmup_complete [i ] = false;
272- }
273- #endif
274-
275249 /* Initialize context with configured values */
276250 ctx -> error_count = 0 ;
277251 ctx -> max_retries = sensor_state_obj .max_retries ;
@@ -318,15 +292,6 @@ static void sensor_state_reading_run(void *obj)
318292 continue ;
319293 }
320294
321- #ifdef CONFIG_SENSOR_MODULE_WARMUP_ENABLE
322- /* Check if sensor warmup is complete */
323- if (!is_sensor_warmup_complete (i )) {
324- LOG_DBG ("Sensor SM: %s warmup not complete, skipping read" ,
325- get_sensor_name (i ));
326- continue ;
327- }
328- #endif
329-
330295 int ret = read_sensor_data (i , & ctx -> current_data );
331296 if (ret == 0 ) {
332297 successful_reads ++ ;
@@ -354,43 +319,18 @@ static void sensor_state_reading_run(void *obj)
354319 }
355320 }
356321
357- /* Count enabled sensors that have completed warmup */
322+ /* Count enabled sensors */
358323 int enabled_sensor_count = 0 ;
359324 for (int i = 0 ; i < SENSOR_TYPE_COUNT ; i ++ ) {
360325 if (sensors [i ].enabled ) {
361- #ifdef CONFIG_SENSOR_MODULE_WARMUP_ENABLE
362- if (is_sensor_warmup_complete (i )) {
363- enabled_sensor_count ++ ;
364- }
365- #else
366326 enabled_sensor_count ++ ;
367- #endif
368327 }
369328 }
370329
371330 /* Only publish data if at least one sensor read successfully */
372331 if (successful_reads == 0 ) {
373- #ifdef CONFIG_SENSOR_MODULE_WARMUP_ENABLE
374- /* Check if any sensors are still warming up */
375- bool any_warming = false;
376- for (int i = 0 ; i < SENSOR_TYPE_COUNT ; i ++ ) {
377- if (sensors [i ].enabled && !is_sensor_warmup_complete (i )) {
378- any_warming = true;
379- break ;
380- }
381- }
382-
383- if (any_warming ) {
384- LOG_DBG ("Sensor SM: No readings yet - sensors still warming up" );
385- sensor_set_state (ctx , SENSOR_MODULE_STATE_IDLE );
386- } else {
387- LOG_ERR ("Sensor SM: All sensors failed, entering error state" );
388- sensor_set_state (ctx , SENSOR_MODULE_STATE_ERROR );
389- }
390- #else
391332 LOG_ERR ("Sensor SM: All sensors failed, entering error state" );
392333 sensor_set_state (ctx , SENSOR_MODULE_STATE_ERROR );
393- #endif
394334 } else if (successful_reads < enabled_sensor_count ) {
395335 LOG_WRN ("Sensor SM: Partial sensor failure (%d/%d successful), but publishing "
396336 "available data" ,
@@ -501,8 +441,8 @@ static int read_sensor_data(enum sensor_type type, struct sensor_msg *data)
501441 return - EINVAL ;
502442 }
503443
504- if (! IS_VALID_SENSOR_TYPE ( type ) || !sensors [type ].enabled ) {
505- LOG_ERR ("Invalid or disabled sensor type: %d" , ( int ) type );
444+ if (type >= SENSOR_TYPE_COUNT || !sensors [type ].enabled ) {
445+ LOG_ERR ("Invalid or disabled sensor type: %d" , type );
506446 return - EINVAL ;
507447 }
508448
@@ -670,7 +610,7 @@ static void handle_ccs811_fallback_compensation(void)
670610
671611int sensor_module_get_sensor_info (enum sensor_type sensor_type , struct sensor_info * sensor_info )
672612{
673- if (! IS_VALID_SENSOR_TYPE ( sensor_type ) || !sensor_info ) {
613+ if (sensor_type >= SENSOR_TYPE_COUNT || !sensor_info ) {
674614 LOG_ERR ("Invalid parameters for sensor info request" );
675615 return - EINVAL ;
676616 }
@@ -763,48 +703,3 @@ static void sensor_set_state(struct sensor_state_object *ctx, enum sensor_module
763703 ctx -> current_state = new_state ;
764704 smf_set_state (SMF_CTX (ctx ), & sensor_states [new_state ]);
765705}
766-
767- #ifdef CONFIG_SENSOR_MODULE_WARMUP_ENABLE
768- /* Helper function to get warmup time for a sensor type */
769- static int64_t get_sensor_warmup_time (enum sensor_type type )
770- {
771- switch (type ) {
772- case SENSOR_TYPE_BME280 :
773- return CONFIG_SENSOR_MODULE_BME280_WARMUP_MS ;
774- case SENSOR_TYPE_CCS811 :
775- return CONFIG_SENSOR_MODULE_CCS811_WARMUP_MS ;
776- case SENSOR_TYPE_HM3301 :
777- return CONFIG_SENSOR_MODULE_HM3301_WARMUP_MS ;
778- default :
779- return 0 ;
780- }
781- }
782-
783- /* Helper function to check if sensor warmup is complete */
784- static bool is_sensor_warmup_complete (enum sensor_type type )
785- {
786- if (!IS_VALID_SENSOR_TYPE (type )) {
787- LOG_ERR ("Invalid sensor type: %d" , (int )type );
788- return false;
789- }
790-
791- /* Check if already marked as complete */
792- if (sensor_state_obj .sensor_warmup_complete [type ]) {
793- return true;
794- }
795-
796- /* Check if warmup time has elapsed */
797- int64_t elapsed_time = k_uptime_get () - sensor_state_obj .sensor_init_time ;
798- int64_t warmup_time = get_sensor_warmup_time (type );
799-
800- if (elapsed_time >= warmup_time ) {
801- /* Mark as complete and log */
802- sensor_state_obj .sensor_warmup_complete [type ] = true;
803- LOG_INF ("Sensor SM: %s warmup complete after %lld ms" , get_sensor_name (type ),
804- elapsed_time );
805- return true;
806- }
807-
808- return false;
809- }
810- #endif /* CONFIG_SENSOR_MODULE_WARMUP_ENABLE */
0 commit comments