@@ -142,29 +142,60 @@ struct veml6031_data {
142142 uint32_t int_flags ;
143143};
144144
145+ static bool veml6031_gain_in_range (int32_t gain )
146+ {
147+ return (gain >= VEML6031_GAIN_1 ) && (gain <= VEML6031_GAIN_0_5 );
148+ }
149+
150+ static bool veml6031_itim_in_range (int32_t itim )
151+ {
152+ return (itim >= VEML6031_IT_3_125 ) && (itim <= VEML6031_IT_400 );
153+ }
154+
155+ static bool veml6031_div4_in_range (int32_t div4 )
156+ {
157+ return (div4 >= VEML6031_SIZE_4_4 ) && (div4 <= VEML6031_SIZE_1_4 );
158+ }
159+
160+ static bool veml6031_pers_in_range (int32_t pers )
161+ {
162+ return (pers >= VEML6031_PERS_1 ) && (pers <= VEML6031_PERS_8 );
163+ }
164+
145165static void veml6031_sleep_by_integration_time (const struct veml6031_data * data )
146166{
147- k_sleep (K_USEC (veml6031_it_values [data -> itim ].us ));
167+ if (veml6031_itim_in_range (data -> itim )) {
168+ k_sleep (K_USEC (veml6031_it_values [data -> itim ].us ));
169+ } else {
170+ LOG_WRN_ONCE ("Wrong settings: itim:%d. Most likely an application bug!" ,
171+ data -> itim );
172+ }
173+ }
174+
175+ static int veml6031_check_settings (const struct veml6031_data * data )
176+ {
177+ return veml6031_div4_in_range (data -> div4 ) && veml6031_gain_in_range (data -> gain ) &&
178+ veml6031_itim_in_range (data -> itim );
148179}
149180
150181static int veml6031_check_gain (const struct sensor_value * val )
151182{
152- return val -> val1 >= VEML6031_GAIN_1 && val -> val1 <= VEML6031_GAIN_0_5 ;
183+ return veml6031_gain_in_range ( val -> val1 ) ;
153184}
154185
155186static int veml6031_check_it (const struct sensor_value * val )
156187{
157- return val -> val1 >= VEML6031_IT_3_125 && val -> val1 <= VEML6031_IT_400 ;
188+ return veml6031_itim_in_range ( val -> val1 ) ;
158189}
159190
160191static int veml6031_check_div4 (const struct sensor_value * val )
161192{
162- return val -> val1 >= VEML6031_SIZE_4_4 && val -> val1 <= VEML6031_SIZE_1_4 ;
193+ return veml6031_div4_in_range ( val -> val1 ) ;
163194}
164195
165196static int veml6031_check_pers (const struct sensor_value * val )
166197{
167- return val -> val1 >= VEML6031_PERS_1 && val -> val1 <= VEML6031_PERS_8 ;
198+ return veml6031_pers_in_range ( val -> val1 ) ;
168199}
169200
170201static int veml6031_read (const struct device * dev , uint8_t cmd , uint8_t * data )
@@ -305,7 +336,15 @@ static int veml6031_fetch(const struct device *dev)
305336 }
306337 data -> ir_data = sys_le16_to_cpu (data -> ir_data );
307338
308- data -> als_lux = data -> als_data * veml6031_resolution [data -> div4 ][data -> gain ][data -> itim ];
339+ if (veml6031_check_settings (data )) {
340+ data -> als_lux =
341+ data -> als_data * veml6031_resolution [data -> div4 ][data -> gain ][data -> itim ];
342+ } else {
343+ LOG_WRN_ONCE ("Wrong settings: div4:%d, gain:%d, itim:%d. "
344+ "Most likely an application bug!" ,
345+ data -> div4 , data -> gain , data -> itim );
346+ return - EINVAL ;
347+ }
309348
310349 LOG_DBG ("Read ALS measurement: counts=%d, lux=%d ir=%d" , data -> als_data , data -> als_lux ,
311350 data -> ir_data );
@@ -357,10 +396,22 @@ static int veml6031_attr_set(const struct device *dev, enum sensor_channel chan,
357396 }
358397 break ;
359398 case SENSOR_ATTR_LOWER_THRESH :
399+ if (!veml6031_check_settings (data )) {
400+ LOG_ERR ("Wrong settings: div4:%d, gain:%d, itim:%d. "
401+ "Most likely an application bug!" ,
402+ data -> div4 , data -> gain , data -> itim );
403+ return - EINVAL ;
404+ }
360405 data -> thresh_low =
361406 val -> val1 / veml6031_resolution [data -> div4 ][data -> gain ][data -> itim ];
362407 return veml6031_write_thresh_low (dev );
363408 case SENSOR_ATTR_UPPER_THRESH :
409+ if (!veml6031_check_settings (data )) {
410+ LOG_ERR ("Wrong settings: div4:%d, gain:%d, itim:%d. "
411+ "Most likely an application bug!" ,
412+ data -> div4 , data -> gain , data -> itim );
413+ return - EINVAL ;
414+ }
364415 data -> thresh_high =
365416 val -> val1 / veml6031_resolution [data -> div4 ][data -> gain ][data -> itim ];
366417 return veml6031_write_thresh_high (dev );
0 commit comments