@@ -142,29 +142,60 @@ struct veml6031_data {
142
142
uint32_t int_flags ;
143
143
};
144
144
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
+
145
165
static void veml6031_sleep_by_integration_time (const struct veml6031_data * data )
146
166
{
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 );
148
179
}
149
180
150
181
static int veml6031_check_gain (const struct sensor_value * val )
151
182
{
152
- return val -> val1 >= VEML6031_GAIN_1 && val -> val1 <= VEML6031_GAIN_0_5 ;
183
+ return veml6031_gain_in_range ( val -> val1 ) ;
153
184
}
154
185
155
186
static int veml6031_check_it (const struct sensor_value * val )
156
187
{
157
- return val -> val1 >= VEML6031_IT_3_125 && val -> val1 <= VEML6031_IT_400 ;
188
+ return veml6031_itim_in_range ( val -> val1 ) ;
158
189
}
159
190
160
191
static int veml6031_check_div4 (const struct sensor_value * val )
161
192
{
162
- return val -> val1 >= VEML6031_SIZE_4_4 && val -> val1 <= VEML6031_SIZE_1_4 ;
193
+ return veml6031_div4_in_range ( val -> val1 ) ;
163
194
}
164
195
165
196
static int veml6031_check_pers (const struct sensor_value * val )
166
197
{
167
- return val -> val1 >= VEML6031_PERS_1 && val -> val1 <= VEML6031_PERS_8 ;
198
+ return veml6031_pers_in_range ( val -> val1 ) ;
168
199
}
169
200
170
201
static 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)
305
336
}
306
337
data -> ir_data = sys_le16_to_cpu (data -> ir_data );
307
338
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
+ }
309
348
310
349
LOG_DBG ("Read ALS measurement: counts=%d, lux=%d ir=%d" , data -> als_data , data -> als_lux ,
311
350
data -> ir_data );
@@ -357,10 +396,22 @@ static int veml6031_attr_set(const struct device *dev, enum sensor_channel chan,
357
396
}
358
397
break ;
359
398
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
+ }
360
405
data -> thresh_low =
361
406
val -> val1 / veml6031_resolution [data -> div4 ][data -> gain ][data -> itim ];
362
407
return veml6031_write_thresh_low (dev );
363
408
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
+ }
364
415
data -> thresh_high =
365
416
val -> val1 / veml6031_resolution [data -> div4 ][data -> gain ][data -> itim ];
366
417
return veml6031_write_thresh_high (dev );
0 commit comments