sysmon_envsys.c revision 1.69 1 /* $NetBSD: sysmon_envsys.c,v 1.69 2007/10/07 04:11:15 xtraeme Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Juan Romero Pardines.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*-
29 * Copyright (c) 2000 Zembu Labs, Inc.
30 * All rights reserved.
31 *
32 * Author: Jason R. Thorpe <thorpej (at) zembu.com>
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by Zembu Labs, Inc.
45 * 4. Neither the name of Zembu Labs nor the names of its employees may
46 * be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
50 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
51 * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
52 * CLAIMED. IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60
61 /*
62 * Environmental sensor framework for sysmon, exported to userland
63 * with proplib(3).
64 */
65
66 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.69 2007/10/07 04:11:15 xtraeme Exp $");
68
69 #include <sys/param.h>
70 #include <sys/types.h>
71 #include <sys/conf.h>
72 #include <sys/errno.h>
73 #include <sys/fcntl.h>
74 #include <sys/kernel.h>
75 #include <sys/systm.h>
76 #include <sys/proc.h>
77 #include <sys/mutex.h>
78 #include <sys/kmem.h>
79
80 /* #define ENVSYS_DEBUG */
81 #include <dev/sysmon/sysmonvar.h>
82 #include <dev/sysmon/sysmon_envsysvar.h>
83 #include <dev/sysmon/sysmon_taskq.h>
84
85 /*
86 * Notes about locking:
87 *
88 * There's a global lock 'sme_mtx' to protect access to 'sysmon_envsys_list'
89 * (devices linked list), 'struct sysmon_envsys' (device), 'sme_events_list'
90 * (events linked list), 'sme_event_t' (event) and the global counter
91 * 'sysmon_envsys_next_sensor_index'.
92 *
93 * Another lock 'sme_init_mtx' is used to protect initialization and
94 * finalization of the events framework (the callout(9) and workqueue(9)
95 * that is used to check for conditions and sending events to powerd(8)).
96 *
97 * The global 'sme_cv' condition variable is used to wait for state changes
98 * on the 'sysmon_envsys_list' and 'sme_events_list' linked lists.
99 *
100 */
101
102 kmutex_t sme_mtx, sme_event_init_mtx;
103 kcondvar_t sme_cv;
104
105 /*
106 * Types of properties that can be set via userland.
107 */
108 enum {
109 USERPROP_DESC = 0x0001,
110 USERPROP_BATTCAP = 0x0002,
111 USERPROP_CRITMAX = 0x0004,
112 USERPROP_CRITMIN = 0x0008,
113 USERPROP_RFACT = 0x0010
114 };
115
116 static prop_dictionary_t sme_propd;
117 static uint32_t sysmon_envsys_next_sensor_index = 0;
118 static struct sysmon_envsys *sysmon_envsys_find_40(u_int);
119
120 static void sysmon_envsys_release(struct sysmon_envsys *);
121 static void sysmon_envsys_destroy_plist(prop_array_t);
122 static int sme_register_sensorname(struct sysmon_envsys *, envsys_data_t *);
123 static void sme_remove_userprops(void);
124
125 /*
126 * sysmon_envsys_init:
127 *
128 * + Initialize global mutexes, dictionary and the linked lists.
129 */
130 void
131 sysmon_envsys_init(void)
132 {
133 LIST_INIT(&sysmon_envsys_list);
134 LIST_INIT(&sme_events_list);
135 mutex_init(&sme_mtx, MUTEX_DRIVER, IPL_NONE);
136 mutex_init(&sme_event_init_mtx, MUTEX_DRIVER, IPL_NONE);
137 cv_init(&sme_cv, "smework");
138 sme_propd = prop_dictionary_create();
139 }
140
141 /*
142 * sysmonopen_envsys:
143 *
144 * + Open the system monitor device.
145 */
146 int
147 sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l)
148 {
149 return 0;
150 }
151
152 /*
153 * sysmonclose_envsys:
154 *
155 * + Close the system monitor device.
156 */
157 int
158 sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l)
159 {
160 /* Nothing to do */
161 return 0;
162 }
163
164 /*
165 * sysmonioctl_envsys:
166 *
167 * + Perform a sysmon envsys control request.
168 */
169 int
170 sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
171 {
172 struct sysmon_envsys *sme = NULL;
173 int error = 0;
174 u_int oidx;
175
176 switch (cmd) {
177 case ENVSYS_GETDICTIONARY:
178 {
179 struct plistref *plist = (struct plistref *)data;
180
181 /*
182 * Update all sysmon envsys devices dictionaries with
183 * new data if it's different than we have currently
184 * in the dictionary.
185 */
186 mutex_enter(&sme_mtx);
187 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
188 sme->sme_flags |= SME_FLAG_BUSY;
189 error = sme_update_dictionary(sme);
190 if (error) {
191 DPRINTF(("%s: sme_update_dictionary, "
192 "error=%d\n", __func__, error));
193 sme->sme_flags &= ~SME_FLAG_BUSY;
194 mutex_exit(&sme_mtx);
195 return error;
196 }
197 sme->sme_flags &= ~SME_FLAG_BUSY;
198 }
199 mutex_exit(&sme_mtx);
200 /*
201 * Copy global dictionary to userland.
202 */
203 error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
204 break;
205 }
206 case ENVSYS_SETDICTIONARY:
207 {
208 const struct plistref *plist = (const struct plistref *)data;
209 prop_dictionary_t udict;
210 prop_object_iterator_t iter, iter2;
211 prop_object_t obj, obj2;
212 prop_array_t array_u, array_k;
213 const char *devname = NULL;
214
215 if ((flag & FWRITE) == 0)
216 return EPERM;
217
218 /*
219 * Get dictionary from userland.
220 */
221 error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
222 if (error) {
223 DPRINTF(("%s: copyin_ioctl error=%d\n",
224 __func__, error));
225 break;
226 }
227
228 iter = prop_dictionary_iterator(udict);
229 if (!iter) {
230 prop_object_release(udict);
231 return ENOMEM;
232 }
233
234 /*
235 * Iterate over the userland dictionary and process
236 * the list of devices.
237 */
238 while ((obj = prop_object_iterator_next(iter))) {
239 array_u = prop_dictionary_get_keysym(udict, obj);
240 if (prop_object_type(array_u) != PROP_TYPE_ARRAY) {
241 prop_object_iterator_release(iter);
242 prop_object_release(udict);
243 return EINVAL;
244 }
245
246 devname = prop_dictionary_keysym_cstring_nocopy(obj);
247 DPRINTF(("%s: processing the '%s' array requests\n",
248 __func__, devname));
249
250 /*
251 * find the correct sme device.
252 */
253 sme = sysmon_envsys_find(devname);
254 if (!sme) {
255 DPRINTF(("%s: NULL sme\n", __func__));
256 prop_object_iterator_release(iter);
257 prop_object_release(udict);
258 return EINVAL;
259 }
260
261 /*
262 * Find the correct array object with the string
263 * supplied by the userland dictionary.
264 */
265 array_k = prop_dictionary_get(sme_propd, devname);
266 if (prop_object_type(array_k) != PROP_TYPE_ARRAY) {
267 DPRINTF(("%s: array device failed\n",
268 __func__));
269 sysmon_envsys_release(sme);
270 prop_object_iterator_release(iter);
271 prop_object_release(udict);
272 return EINVAL;
273 }
274
275 iter2 = prop_array_iterator(array_u);
276 if (!iter2) {
277 sysmon_envsys_release(sme);
278 prop_object_iterator_release(iter);
279 prop_object_release(udict);
280 return ENOMEM;
281 }
282
283 /*
284 * Iterate over the array of dictionaries to
285 * process the list of sensors.
286 */
287 while ((obj2 = prop_object_iterator_next(iter2))) {
288 /* do the real work now */
289 error = sme_userset_dictionary(sme,
290 obj2,
291 array_k);
292 if (error) {
293 sysmon_envsys_release(sme);
294 prop_object_iterator_release(iter2);
295 prop_object_iterator_release(iter);
296 prop_object_release(udict);
297 return EINVAL;
298 }
299 }
300
301 sysmon_envsys_release(sme);
302 prop_object_iterator_release(iter2);
303 }
304
305 prop_object_iterator_release(iter);
306 prop_object_release(udict);
307 break;
308 }
309 case ENVSYS_REMOVEPROPS:
310 {
311 const struct plistref *plist = (const struct plistref *)data;
312 prop_dictionary_t udict;
313 prop_object_t obj;
314
315 if ((flag & FWRITE) == 0)
316 return EPERM;
317
318 error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
319 if (error) {
320 DPRINTF(("%s: copyin_ioctl error=%d\n",
321 __func__, error));
322 break;
323 }
324
325 obj = prop_dictionary_get(udict, "envsys-remove-props");
326 if (!obj || !prop_bool_true(obj)) {
327 DPRINTF(("%s: invalid 'envsys-remove-props'\n",
328 __func__));
329 return EINVAL;
330 }
331
332 sme_remove_userprops();
333
334 prop_object_release(udict);
335 break;
336 }
337
338 /*
339 * Compatibility functions with the old interface, only
340 * implemented ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough
341 * to make old applications work.
342 */
343 case ENVSYS_GTREDATA:
344 {
345 struct envsys_tre_data *tred = (void *)data;
346 envsys_data_t *edata = NULL;
347
348 tred->validflags = 0;
349
350 sme = sysmon_envsys_find_40(tred->sensor);
351 if (!sme)
352 break;
353
354 mutex_enter(&sme_mtx);
355 oidx = tred->sensor;
356 tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
357
358 DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
359 __func__, tred->sensor, oidx, sme->sme_name,
360 sme->sme_nsensors));
361
362 edata = &sme->sme_sensor_data[tred->sensor];
363
364 if (tred->sensor < sme->sme_nsensors) {
365 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
366 error = (*sme->sme_gtredata)(sme, edata);
367 if (error) {
368 DPRINTF(("%s: sme_gtredata failed\n",
369 __func__));
370 mutex_exit(&sme_mtx);
371 return error;
372 }
373 }
374
375 /* copy required values to the old interface */
376 tred->sensor = edata->sensor;
377 tred->cur.data_us = edata->value_cur;
378 tred->cur.data_s = edata->value_cur;
379 tred->max.data_us = edata->value_max;
380 tred->max.data_s = edata->value_max;
381 tred->min.data_us = edata->value_min;
382 tred->min.data_s = edata->value_min;
383 tred->avg.data_us = edata->value_avg;
384 tred->avg.data_s = edata->value_avg;
385 tred->units = edata->units;
386
387 tred->validflags |= ENVSYS_FVALID;
388 tred->validflags |= ENVSYS_FCURVALID;
389
390 if (edata->flags & ENVSYS_FPERCENT) {
391 tred->validflags |= ENVSYS_FMAXVALID;
392 tred->validflags |= ENVSYS_FFRACVALID;
393 }
394
395 if (edata->state == ENVSYS_SINVALID ||
396 edata->flags & ENVSYS_FNOTVALID) {
397 tred->validflags &= ~ENVSYS_FCURVALID;
398 tred->cur.data_us = tred->cur.data_s = 0;
399 }
400
401 DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
402 __func__, edata->desc, tred->cur.data_s));
403 DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
404 " tred->sensor=%d\n", __func__, tred->validflags,
405 tred->units, tred->sensor));
406 }
407 tred->sensor = oidx;
408 mutex_exit(&sme_mtx);
409
410 break;
411 }
412 case ENVSYS_GTREINFO:
413 {
414 struct envsys_basic_info *binfo = (void *)data;
415 envsys_data_t *edata = NULL;
416
417 binfo->validflags = 0;
418
419 sme = sysmon_envsys_find_40(binfo->sensor);
420 if (!sme)
421 break;
422
423 mutex_enter(&sme_mtx);
424 oidx = binfo->sensor;
425 binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
426
427 edata = &sme->sme_sensor_data[binfo->sensor];
428
429 binfo->validflags |= ENVSYS_FVALID;
430
431 if (binfo->sensor < sme->sme_nsensors) {
432 binfo->units = edata->units;
433 (void)strlcpy(binfo->desc, edata->desc,
434 sizeof(binfo->desc));
435 }
436
437 DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
438 __func__, binfo->units, binfo->validflags));
439 DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
440 __func__, binfo->desc, binfo->sensor));
441
442 binfo->sensor = oidx;
443 mutex_exit(&sme_mtx);
444
445 break;
446 }
447 default:
448 error = ENOTTY;
449 break;
450 }
451
452 return error;
453 }
454
455 /*
456 * sysmon_envsys_register:
457 *
458 * + Register a sysmon envsys device.
459 * + Create array of dictionaries for a device.
460 */
461 int
462 sysmon_envsys_register(struct sysmon_envsys *sme)
463 {
464 struct sme_evdrv {
465 SLIST_ENTRY(sme_evdrv) evdrv_head;
466 sme_event_drv_t *evdrv;
467 };
468 SLIST_HEAD(, sme_evdrv) sme_evdrv_list;
469 struct sme_evdrv *sme_evdrv = NULL;
470 struct sysmon_envsys *lsme;
471 prop_dictionary_t dict;
472 prop_array_t array;
473 envsys_data_t *edata = NULL;
474 int i, error = 0;
475
476 KASSERT(sme != NULL);
477 KASSERT(sme->sme_name != NULL);
478 KASSERT(sme->sme_sensor_data != NULL);
479
480 /*
481 * sme_nsensors is mandatory...
482 */
483 if (!sme->sme_nsensors)
484 return EINVAL;
485
486 /*
487 * sanity check: if SME_DISABLE_GTREDATA is not set,
488 * the sme_gtredata function callback must be non NULL.
489 */
490 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
491 if (!sme->sme_gtredata)
492 return EINVAL;
493 }
494
495
496 /* create the sysmon envsys device array. */
497 array = prop_array_create();
498 if (!array)
499 return ENOMEM;
500
501 /*
502 * Initialize the singly linked list for sensor descriptions.
503 */
504 SLIST_INIT(&sme->sme_names_list);
505
506 /*
507 * Initialize the singly linked list for driver events.
508 */
509 SLIST_INIT(&sme_evdrv_list);
510 /*
511 * Iterate over all sensors and create a dictionary per sensor,
512 * checking firstly if sensor description is unique.
513 */
514 for (i = 0; i < sme->sme_nsensors; i++) {
515 edata = &sme->sme_sensor_data[i];
516 /*
517 * Check if sensor description is unique.
518 */
519 if (sme_register_sensorname(sme, edata))
520 continue;
521
522 dict = prop_dictionary_create();
523 if (!dict) {
524 error = ENOMEM;
525 goto out2;
526 }
527
528 /*
529 * Create all objects in sensor's dictionary.
530 */
531 sme_evdrv = kmem_zalloc(sizeof(*sme_evdrv), KM_SLEEP);
532 sme_evdrv->evdrv = sme_add_sensor_dictionary(sme,
533 array, dict, edata);
534 if (sme_evdrv->evdrv)
535 SLIST_INSERT_HEAD(&sme_evdrv_list,
536 sme_evdrv, evdrv_head);
537 }
538
539 /*
540 * Check if requested sysmon_envsys device is valid
541 * and does not exist already in the list.
542 */
543 mutex_enter(&sme_mtx);
544 sme->sme_flags |= SME_FLAG_BUSY;
545 LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
546 if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
547 error = EEXIST;
548 goto out;
549 }
550 }
551
552 /*
553 * If the array does not contain any object (sensor), there's
554 * no need to attach the driver.
555 */
556 if (prop_array_count(array) == 0) {
557 error = EINVAL;
558 DPRINTF(("%s: empty array for '%s'\n", __func__,
559 sme->sme_name));
560 goto out;
561 }
562 /*
563 * Add the array into the global dictionary for the driver.
564 *
565 * <dict>
566 * <key>foo0</key>
567 * <array>
568 * ...
569 */
570 if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
571 error = EINVAL;
572 DPRINTF(("%s: prop_dictionary_set for '%s'\n", __func__,
573 sme->sme_name));
574 goto out;
575 }
576 /*
577 * Add the device into the list.
578 */
579 LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
580 sme->sme_fsensor = sysmon_envsys_next_sensor_index;
581 sysmon_envsys_next_sensor_index += sme->sme_nsensors;
582 out:
583 sme->sme_uniqsensors = 0;
584 sme->sme_flags &= ~SME_FLAG_BUSY;
585 mutex_exit(&sme_mtx);
586
587 if (error == 0) {
588 i = 0;
589 SLIST_FOREACH(sme_evdrv, &sme_evdrv_list, evdrv_head) {
590 if (i == 0)
591 sysmon_task_queue_init();
592 sysmon_task_queue_sched(0,
593 sme_event_drvadd, sme_evdrv->evdrv);
594 }
595 DPRINTF(("%s: driver '%s' registered (nsens=%d)\n",
596 __func__, sme->sme_name, sme->sme_nsensors));
597 }
598
599 out2:
600 while (!SLIST_EMPTY(&sme_evdrv_list)) {
601 sme_evdrv = SLIST_FIRST(&sme_evdrv_list);
602 SLIST_REMOVE_HEAD(&sme_evdrv_list, evdrv_head);
603 kmem_free(sme_evdrv, sizeof(*sme_evdrv));
604 }
605 if (!error)
606 return 0;
607
608 DPRINTF(("%s: failed to register '%s' (%d)\n", __func__,
609 sme->sme_name, error));
610 if (error != EEXIST) {
611 mutex_enter(&sme_mtx);
612 sme_event_unregister_all(sme->sme_name);
613 mutex_exit(&sme_mtx);
614 }
615 sysmon_envsys_destroy_plist(array);
616 return error;
617 }
618
619 /*
620 * sysmon_envsys_destroy_plist:
621 *
622 * + Remove all objects from the array of dictionaries that is
623 * created in a sysmon envsys device.
624 */
625 static void
626 sysmon_envsys_destroy_plist(prop_array_t array)
627 {
628 prop_object_iterator_t iter, iter2;
629 prop_dictionary_t dict;
630 prop_object_t obj;
631
632 KASSERT(array != NULL);
633
634 DPRINTFOBJ(("%s: objects in array=%d\n", __func__,
635 prop_array_count(array)));
636
637 iter = prop_array_iterator(array);
638 if (!iter)
639 return;
640
641 while ((dict = prop_object_iterator_next(iter))) {
642 KASSERT(prop_object_type(dict) == PROP_TYPE_DICTIONARY);
643 iter2 = prop_dictionary_iterator(dict);
644 if (!iter2)
645 goto out;
646 DPRINTFOBJ(("%s: iterating over dictionary\n", __func__));
647 while ((obj = prop_object_iterator_next(iter2)) != NULL) {
648 DPRINTFOBJ(("%s: obj=%s\n", __func__,
649 prop_dictionary_keysym_cstring_nocopy(obj)));
650 prop_dictionary_remove(dict,
651 prop_dictionary_keysym_cstring_nocopy(obj));
652 prop_object_iterator_reset(iter2);
653 }
654 prop_object_iterator_release(iter2);
655 DPRINTFOBJ(("%s: objects in dictionary:%d\n",
656 __func__, prop_dictionary_count(dict)));
657 prop_object_release(dict);
658 }
659
660 out:
661 prop_object_iterator_release(iter);
662 prop_object_release(array);
663 }
664
665 /*
666 * sysmon_envsys_unregister:
667 *
668 * + Unregister a sysmon envsys device.
669 */
670 void
671 sysmon_envsys_unregister(struct sysmon_envsys *sme)
672 {
673 struct sme_sensor_names *snames;
674 prop_array_t array;
675
676 KASSERT(sme != NULL);
677
678 mutex_enter(&sme_mtx);
679 while (sme->sme_flags & SME_FLAG_BUSY) {
680 sme->sme_flags |= SME_FLAG_WANTED;
681 cv_wait(&sme_cv, &sme_mtx);
682 }
683 sysmon_envsys_next_sensor_index -= sme->sme_nsensors;
684 /*
685 * Remove all sensor descriptions from the singly linked list.
686 */
687 while (!SLIST_EMPTY(&sme->sme_names_list)) {
688 snames = SLIST_FIRST(&sme->sme_names_list);
689 SLIST_REMOVE_HEAD(&sme->sme_names_list, sme_names);
690 kmem_free(snames, sizeof(*snames));
691 }
692 /*
693 * Unregister all events associated with this device.
694 */
695 sme_event_unregister_all(sme->sme_name);
696 LIST_REMOVE(sme, sme_list);
697 mutex_exit(&sme_mtx);
698 /*
699 * Remove the device (and all its objects) from the global dictionary.
700 */
701 array = prop_dictionary_get(sme_propd, sme->sme_name);
702 if (array && prop_object_type(array) == PROP_TYPE_ARRAY) {
703 prop_dictionary_remove(sme_propd, sme->sme_name);
704 sysmon_envsys_destroy_plist(array);
705 }
706 }
707
708 /*
709 * sysmon_envsys_find:
710 *
711 * + Find a sysmon envsys device.
712 */
713 struct sysmon_envsys *
714 sysmon_envsys_find(const char *name)
715 {
716 struct sysmon_envsys *sme;
717
718 mutex_enter(&sme_mtx);
719 again:
720 for (sme = LIST_FIRST(&sysmon_envsys_list); sme;
721 sme = LIST_NEXT(sme, sme_list)) {
722 if (strcmp(sme->sme_name, name) == 0) {
723 if (sme->sme_flags & SME_FLAG_BUSY) {
724 sme->sme_flags |= SME_FLAG_WANTED;
725 cv_wait(&sme_cv, &sme_mtx);
726 goto again;
727 }
728 sme->sme_flags |= SME_FLAG_BUSY;
729 break;
730 }
731 }
732 mutex_exit(&sme_mtx);
733 return sme;
734 }
735
736 /*
737 * sysmon_envsys_release:
738 *
739 * + Release a sysmon envsys device.
740 */
741 void
742 sysmon_envsys_release(struct sysmon_envsys *sme)
743 {
744 mutex_enter(&sme_mtx);
745 if (sme->sme_flags & SME_FLAG_WANTED)
746 cv_broadcast(&sme_cv);
747 sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
748 mutex_exit(&sme_mtx);
749 }
750
751 /* compatibility function */
752 struct sysmon_envsys *
753 sysmon_envsys_find_40(u_int idx)
754 {
755 struct sysmon_envsys *sme;
756
757 mutex_enter(&sme_mtx);
758 for (sme = LIST_FIRST(&sysmon_envsys_list); sme;
759 sme = LIST_NEXT(sme, sme_list)) {
760 if (idx >= sme->sme_fsensor &&
761 idx < (sme->sme_fsensor + sme->sme_nsensors))
762 break;
763 }
764 mutex_exit(&sme_mtx);
765 return sme;
766 }
767
768 /*
769 * sme_sensor_dictionary_get:
770 *
771 * + Returns a dictionary of a device specified by 'index'.
772 */
773 prop_dictionary_t
774 sme_sensor_dictionary_get(prop_array_t array, const char *index)
775 {
776 prop_object_iterator_t iter;
777 prop_dictionary_t dict;
778 prop_object_t obj;
779
780 KASSERT(array != NULL || index != NULL);
781
782 iter = prop_array_iterator(array);
783 if (!iter)
784 return NULL;
785
786 while ((dict = prop_object_iterator_next(iter))) {
787 obj = prop_dictionary_get(dict, "index");
788 if (prop_string_equals_cstring(obj, index))
789 break;
790 }
791
792 prop_object_iterator_release(iter);
793 return dict;
794 }
795
796 /*
797 * sme_remove_userprops:
798 *
799 * + Remove all properties from all devices that were set by
800 * the userland.
801 */
802 static void
803 sme_remove_userprops(void)
804 {
805 struct sysmon_envsys *sme;
806 prop_array_t array;
807 prop_dictionary_t sdict;
808 envsys_data_t *edata = NULL;
809 char tmp[ENVSYS_DESCLEN];
810 int i, ptype;
811
812 mutex_enter(&sme_mtx);
813 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
814 sme->sme_flags |= SME_FLAG_BUSY;
815 array = prop_dictionary_get(sme_propd, sme->sme_name);
816
817 for (i = 0; i < sme->sme_nsensors; i++) {
818 edata = &sme->sme_sensor_data[i];
819 (void)snprintf(tmp, sizeof(tmp), "sensor%d",
820 edata->sensor);
821 sdict = sme_sensor_dictionary_get(array, tmp);
822
823 if (edata->upropset & USERPROP_BATTCAP) {
824 prop_dictionary_remove(sdict,
825 "critical-capacity");
826 ptype = PENVSYS_EVENT_BATT_USERCAP;
827 sme_event_unregister(edata->desc, ptype);
828 }
829
830 if (edata->upropset & USERPROP_CRITMAX) {
831 prop_dictionary_remove(sdict,
832 "critical-max");
833 ptype = PENVSYS_EVENT_USER_CRITMAX;
834 sme_event_unregister(edata->desc, ptype);
835 }
836
837 if (edata->upropset & USERPROP_CRITMIN) {
838 prop_dictionary_remove(sdict,
839 "critical-min");
840 ptype = PENVSYS_EVENT_USER_CRITMIN;
841 sme_event_unregister(edata->desc, ptype);
842 }
843
844 if (edata->upropset & USERPROP_RFACT) {
845 (void)sme_sensor_upint32(sdict, "rfact", 0);
846 edata->rfact = 0;
847 }
848
849 if (edata->upropset & USERPROP_DESC)
850 (void)sme_sensor_upstring(sdict,
851 "description", edata->desc);
852 }
853
854 if (edata->upropset)
855 edata->upropset = 0;
856
857 sme->sme_flags &= ~SME_FLAG_BUSY;
858 }
859 mutex_exit(&sme_mtx);
860 }
861 /*
862 * sme_register_sensorname:
863 *
864 * + Register a sensor description into the list maintained per device.
865 */
866 static int
867 sme_register_sensorname(struct sysmon_envsys *sme, envsys_data_t *edata)
868 {
869 struct sme_sensor_names *snames, *snames2 = NULL;
870
871 KASSERT(edata != NULL);
872
873 SLIST_FOREACH(snames2, &sme->sme_names_list, sme_names) {
874 /*
875 * Match sensors with empty and duplicate description.
876 */
877 if (strlen(edata->desc) == 0 ||
878 strcmp(snames2->desc, edata->desc) == 0)
879 if (snames2->assigned) {
880 edata->flags |= ENVSYS_FNOTVALID;
881 DPRINTF(("%s: wrong sensor name='%s'\n",
882 sme->sme_name, edata->desc));
883 return EEXIST;
884 }
885 }
886
887 snames = kmem_zalloc(sizeof(*snames), KM_NOSLEEP);
888 if (!snames)
889 return ENOMEM;
890
891 snames->assigned = true;
892 (void)strlcpy(snames->desc, edata->desc, sizeof(snames->desc));
893 DPRINTF(("%s: registering sensor name='%s'\n",
894 sme->sme_name, edata->desc));
895 SLIST_INSERT_HEAD(&sme->sme_names_list, snames, sme_names);
896 sme->sme_uniqsensors++;
897
898 return 0;
899 }
900
901 /*
902 * sme_add_sensor_dictionary:
903 *
904 * + Add the objects into the dictionary.
905 */
906 sme_event_drv_t *
907 sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
908 prop_dictionary_t dict, envsys_data_t *edata)
909 {
910 const struct sme_description_table *sdt, *sdt_units;
911 sme_event_drv_t *sme_evdrv_t = NULL;
912 int i, j;
913 char indexstr[ENVSYS_DESCLEN];
914
915 i = j = 0;
916
917 /* find the correct unit for this sensor. */
918 sdt_units = sme_get_description_table(SME_DESC_UNITS);
919 for (i = 0; sdt_units[i].type != -1; i++)
920 if (sdt_units[i].type == edata->units)
921 break;
922
923 if (strcmp(sdt_units[i].desc, "unknown") == 0) {
924 DPRINTF(("%s: invalid units type for sensor=%d\n",
925 __func__, edata->sensor));
926 goto invalidate_sensor;
927 }
928
929 /*
930 * Add the index sensor string.
931 *
932 * ...
933 * <key>index</key>
934 * <string>sensor0</string>
935 * ...
936 */
937 (void)snprintf(indexstr, sizeof(indexstr), "sensor%d", edata->sensor);
938 if (sme_sensor_upstring(dict, "index", indexstr))
939 goto invalidate_sensor;
940
941 /*
942 * ...
943 * <key>type</key>
944 * <string>foo</string>
945 * <key>description</key>
946 * <string>blah blah</string>
947 * ...
948 */
949 if (sme_sensor_upstring(dict, "type", sdt_units[i].desc))
950 goto invalidate_sensor;
951
952 if (strlen(edata->desc) == 0) {
953 DPRINTF(("%s: invalid description for sensor=%d\n",
954 __func__, edata->sensor));
955 goto invalidate_sensor;
956 }
957
958 if (sme_sensor_upstring(dict, "description", edata->desc))
959 goto invalidate_sensor;
960
961 /*
962 * Add sensor's state description.
963 *
964 * ...
965 * <key>state</key>
966 * <string>valid</string>
967 * ...
968 */
969 sdt = sme_get_description_table(SME_DESC_STATES);
970 for (j = 0; sdt[j].type != -1; j++)
971 if (sdt[j].type == edata->state)
972 break;
973
974 if (strcmp(sdt[j].desc, "unknown") == 0) {
975 DPRINTF(("%s: invalid state for sensor=%d\n",
976 __func__, edata->sensor));
977 goto invalidate_sensor;
978 }
979
980 DPRINTF(("%s: sensor desc=%s type=%d state=%d\n",
981 __func__, edata->desc, edata->units, edata->state));
982
983 if (sme_sensor_upstring(dict, "state", sdt[j].desc))
984 goto invalidate_sensor;
985
986 /*
987 * Add the monitoring boolean object:
988 *
989 * ...
990 * <key>monitoring-supported</key>
991 * <true/>
992 * ...
993 *
994 * always false on Battery state, Drive and Indicator types.
995 * They cannot be monitored.
996 *
997 */
998 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
999 (edata->units == ENVSYS_INDICATOR) ||
1000 (edata->units == ENVSYS_DRIVE) ||
1001 (edata->units == ENVSYS_BATTERY_STATE)) {
1002 if (sme_sensor_upbool(dict, "monitoring-supported", false))
1003 goto out;
1004 } else {
1005 if (sme_sensor_upbool(dict, "monitoring-supported", true))
1006 goto out;
1007 }
1008
1009 /*
1010 * Add the percentage boolean object, true if ENVSYS_FPERCENT
1011 * is set or false otherwise.
1012 *
1013 * ...
1014 * <key>want-percentage</key>
1015 * <true/>
1016 * ...
1017 */
1018 if (edata->flags & ENVSYS_FPERCENT)
1019 if (sme_sensor_upbool(dict, "want-percentage", true))
1020 goto out;
1021
1022 /*
1023 * Add the allow-rfact boolean object, true if
1024 * ENVSYS_FCHANGERFACT if set or false otherwise.
1025 *
1026 * ...
1027 * <key>allow-rfact</key>
1028 * <true/>
1029 * ...
1030 */
1031 if (edata->units == ENVSYS_SVOLTS_DC ||
1032 edata->units == ENVSYS_SVOLTS_AC) {
1033 if (edata->flags & ENVSYS_FCHANGERFACT) {
1034 if (sme_sensor_upbool(dict, "allow-rfact", true))
1035 goto out;
1036 } else {
1037 if (sme_sensor_upbool(dict, "allow-rfact", false))
1038 goto out;
1039 }
1040 }
1041
1042
1043 /*
1044 * Add the battery-state object for battery state sensors:
1045 *
1046 * ...
1047 * <key>battery-state</key>
1048 * <string>NORMAL</string>
1049 * ...
1050 */
1051 if (edata->units == ENVSYS_BATTERY_STATE) {
1052 sdt = sme_get_description_table(SME_DESC_BATTERY_STATES);
1053 for (j = 0; sdt[j].type != -1; j++)
1054 if (sdt[j].type == edata->value_cur)
1055 break;
1056
1057 if (sme_sensor_upstring(dict, "battery-state", sdt[j].desc))
1058 goto out;
1059 }
1060
1061 /*
1062 * Add the drive-state object for drive sensors:
1063 *
1064 * ...
1065 * <key>drive-state</key>
1066 * <string>drive is online</string>
1067 * ...
1068 */
1069 if (edata->units == ENVSYS_DRIVE) {
1070 sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
1071 for (j = 0; sdt[j].type != -1; j++)
1072 if (sdt[j].type == edata->value_cur)
1073 break;
1074
1075 if (sme_sensor_upstring(dict, "drive-state", sdt[j].desc))
1076 goto out;
1077 }
1078
1079 /*
1080 * if sensor is enabled, add the following properties...
1081 */
1082 if (edata->state == ENVSYS_SVALID) {
1083 /*
1084 * ...
1085 * <key>rpms</key>
1086 * <integer>2500</integer>
1087 * <key>rfact</key>
1088 * <integer>10000</integer>
1089 * <key>cur-value</key>
1090 * <integer>1250</integer>
1091 * <key>min-value</key>
1092 * <integer>800</integer>
1093 * <key>max-value</integer>
1094 * <integer>3000</integer>
1095 * <key>avg-value</integer>
1096 * <integer>1400</integer>
1097 * </dict>
1098 */
1099 if (edata->units == ENVSYS_SFANRPM)
1100 if (sme_sensor_upuint32(dict, "rpms", edata->rpms))
1101 goto out;
1102
1103 if (edata->units == ENVSYS_SVOLTS_AC ||
1104 edata->units == ENVSYS_SVOLTS_DC)
1105 if (sme_sensor_upint32(dict, "rfact", edata->rfact))
1106 goto out;
1107
1108 if (sme_sensor_upint32(dict, "cur-value", edata->value_cur))
1109 goto out;
1110
1111 if (edata->flags & ENVSYS_FVALID_MIN) {
1112 if (sme_sensor_upint32(dict,
1113 "min-value",
1114 edata->value_min))
1115 goto out;
1116 }
1117
1118 if (edata->flags & ENVSYS_FVALID_MAX) {
1119 if (sme_sensor_upint32(dict,
1120 "max-value",
1121 edata->value_max))
1122 goto out;
1123 }
1124
1125 if (edata->flags & ENVSYS_FVALID_AVG) {
1126 if (sme_sensor_upint32(dict,
1127 "avg-value",
1128 edata->value_avg))
1129 goto out;
1130 }
1131 }
1132
1133 /*
1134 * ...
1135 * </array>
1136 *
1137 * Add the dictionary into the array.
1138 *
1139 */
1140
1141 if (!prop_array_set(array, sme->sme_uniqsensors - 1, dict)) {
1142 DPRINTF(("%s: prop_array_add\n", __func__));
1143 goto invalidate_sensor;
1144 }
1145
1146 /*
1147 * Add a new event if a monitoring flag was set.
1148 */
1149 if (edata->monitor) {
1150 sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
1151 sme_evdrv_t->sdict = dict;
1152 sme_evdrv_t->edata = edata;
1153 sme_evdrv_t->sme = sme;
1154 sme_evdrv_t->powertype = sdt_units[i].crittype;
1155 }
1156
1157 out:
1158 return sme_evdrv_t;
1159
1160 invalidate_sensor:
1161 edata->flags |= ENVSYS_FNOTVALID;
1162 prop_object_release(dict);
1163 return sme_evdrv_t;
1164 }
1165
1166 /*
1167 * sme_update_dictionary:
1168 *
1169 * + Update per-sensor dictionaries with new values if there were
1170 * changes, otherwise the object in dictionary is untouched.
1171 */
1172 int
1173 sme_update_dictionary(struct sysmon_envsys *sme)
1174 {
1175 const struct sme_description_table *sdt;
1176 envsys_data_t *edata;
1177 prop_object_t array, dict;
1178 int i, j, error, invalid;
1179
1180 KASSERT(mutex_owned(&sme_mtx));
1181
1182 error = invalid = 0;
1183 array = dict = NULL;
1184
1185 /* retrieve the array of dictionaries in device. */
1186 array = prop_dictionary_get(sme_propd, sme->sme_name);
1187 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
1188 DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
1189 return EINVAL;
1190 }
1191
1192 /*
1193 * - iterate over all sensors.
1194 * - fetch new data.
1195 * - check if data in dictionary is different than new data.
1196 * - update dictionary if there were changes.
1197 */
1198 DPRINTF(("%s: updating '%s' with nsensors=%d\n", __func__,
1199 sme->sme_name, sme->sme_nsensors));
1200
1201 for (i = 0; i < sme->sme_nsensors; i++) {
1202 edata = &sme->sme_sensor_data[i];
1203 /* skip invalid sensors */
1204 if (edata->flags & ENVSYS_FNOTVALID) {
1205 DPRINTF(("%s: invalid sensor=%s idx=%d\n",
1206 __func__, edata->desc, edata->sensor));
1207 invalid++;
1208 continue;
1209 }
1210
1211 /*
1212 * refresh sensor data via sme_gtredata only if the
1213 * flag is not set.
1214 */
1215 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
1216 error = (*sme->sme_gtredata)(sme, edata);
1217 if (error) {
1218 DPRINTF(("%s: gtredata[%d] failed\n",
1219 __func__, i));
1220 return error;
1221 }
1222 }
1223
1224 /* retrieve sensor's dictionary. */
1225 dict = prop_array_get(array, i - invalid);
1226 if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
1227 DPRINTF(("%s: not a dictionary (%d:%s)\n",
1228 __func__, edata->sensor, sme->sme_name));
1229 return EINVAL;
1230 }
1231
1232 /* update sensor's state */
1233 sdt = sme_get_description_table(SME_DESC_STATES);
1234 for (j = 0; sdt[j].type != -1; j++)
1235 if (sdt[j].type == edata->state)
1236 break;
1237
1238 DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
1239 "units=%d sensor=%d\n", __func__, sdt[j].desc,
1240 sdt[j].type, edata->flags, edata->units, edata->sensor));
1241
1242 /* update sensor state */
1243 error = sme_sensor_upstring(dict, "state", sdt[j].desc);
1244 if (error)
1245 break;
1246
1247 /* update sensor type */
1248 sdt = sme_get_description_table(SME_DESC_UNITS);
1249 for (j = 0; sdt[j].type != -1; j++)
1250 if (sdt[j].type == edata->units)
1251 break;
1252
1253 error = sme_sensor_upstring(dict, "type", sdt[j].desc);
1254 if (error)
1255 break;
1256
1257 /* update sensor current value */
1258 error = sme_sensor_upint32(dict,
1259 "cur-value",
1260 edata->value_cur);
1261 if (error)
1262 break;
1263
1264 /*
1265 * Integer and Indicator types do not the following
1266 * values, so skip them.
1267 */
1268 if (edata->units == ENVSYS_INTEGER ||
1269 edata->units == ENVSYS_INDICATOR)
1270 continue;
1271
1272 /* update sensor flags */
1273 if (edata->flags & ENVSYS_FPERCENT) {
1274 error = sme_sensor_upbool(dict,
1275 "want-percentage",
1276 true);
1277 if (error)
1278 break;
1279 }
1280
1281 if (edata->flags & ENVSYS_FVALID_MAX) {
1282 error = sme_sensor_upint32(dict,
1283 "max-value",
1284 edata->value_max);
1285 if (error)
1286 break;
1287 }
1288
1289 if (edata->flags & ENVSYS_FVALID_MIN) {
1290 error = sme_sensor_upint32(dict,
1291 "min-value",
1292 edata->value_min);
1293 if (error)
1294 break;
1295 }
1296
1297 if (edata->flags & ENVSYS_FVALID_AVG) {
1298 error = sme_sensor_upint32(dict,
1299 "avg-value",
1300 edata->value_avg);
1301 if (error)
1302 break;
1303 }
1304
1305 /* update 'rpms' only in ENVSYS_SFANRPM. */
1306 if (edata->units == ENVSYS_SFANRPM) {
1307 error = sme_sensor_upuint32(dict,
1308 "rpms",
1309 edata->rpms);
1310 if (error)
1311 break;
1312 }
1313
1314 /* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
1315 if (edata->units == ENVSYS_SVOLTS_AC ||
1316 edata->units == ENVSYS_SVOLTS_DC) {
1317 error = sme_sensor_upint32(dict,
1318 "rfact",
1319 edata->rfact);
1320 if (error)
1321 break;
1322 }
1323
1324 /* update 'drive-state' only in ENVSYS_DRIVE. */
1325 if (edata->units == ENVSYS_DRIVE) {
1326 sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
1327 for (j = 0; sdt[j].type != -1; j++)
1328 if (sdt[j].type == edata->value_cur)
1329 break;
1330
1331 error = sme_sensor_upstring(dict,
1332 "drive-state",
1333 sdt[j].desc);
1334 if (error)
1335 break;
1336 }
1337
1338 /* update 'battery-state' only in ENVSYS_BATTERY_STATE. */
1339 if (edata->units == ENVSYS_BATTERY_STATE) {
1340 sdt =
1341 sme_get_description_table(SME_DESC_BATTERY_STATES);
1342 for (j = 0; sdt[j].type != -1; j++)
1343 if (sdt[j].type == edata->value_cur)
1344 break;
1345
1346 error = sme_sensor_upstring(dict,
1347 "battery-state",
1348 sdt[j].desc);
1349 if (error)
1350 break;
1351 }
1352 }
1353
1354 return error;
1355 }
1356
1357 /*
1358 * sme_userset_dictionary:
1359 *
1360 * + Parse the userland sensor's dictionary and run the appropiate
1361 * tasks that was requested.
1362 */
1363 int
1364 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
1365 prop_array_t array)
1366 {
1367 const struct sme_description_table *sdt;
1368 envsys_data_t *edata, *nedata;
1369 prop_dictionary_t dict;
1370 prop_object_t obj, obj1, obj2;
1371 int32_t critval;
1372 int i, invalid, error;
1373 const char *blah, *sname;
1374 bool targetfound = false;
1375
1376 error = invalid = 0;
1377 blah = sname = NULL;
1378
1379 /* get sensor's index from userland dictionary. */
1380 obj = prop_dictionary_get(udict, "index");
1381 if (prop_object_type(obj) != PROP_TYPE_STRING) {
1382 DPRINTF(("%s: sensor-name failed\n", __func__));
1383 return EINVAL;
1384 }
1385
1386 /* iterate over the sensors to find the right one */
1387 for (i = 0; i < sme->sme_nsensors; i++) {
1388 edata = &sme->sme_sensor_data[i];
1389 /*
1390 * skip invalid sensors.
1391 */
1392 if (edata->flags & ENVSYS_FNOTVALID) {
1393 invalid++;
1394 continue;
1395 }
1396
1397 dict = prop_array_get(array, i - invalid);
1398 obj1 = prop_dictionary_get(dict, "index");
1399
1400 /* is it our sensor? */
1401 if (!prop_string_equals(obj1, obj))
1402 continue;
1403
1404 /*
1405 * Check if a new description operation was
1406 * requested by the user and set new description.
1407 */
1408 if ((obj2 = prop_dictionary_get(udict, "description"))) {
1409 targetfound = true;
1410 blah = prop_string_cstring_nocopy(obj2);
1411
1412 for (i = 0; i < sme->sme_nsensors; i++) {
1413 if (i == edata->sensor)
1414 continue;
1415
1416 nedata = &sme->sme_sensor_data[i];
1417 if (strcmp(blah, nedata->desc) == 0) {
1418 error = EEXIST;
1419 break;
1420 }
1421 }
1422
1423 if (error)
1424 break;
1425
1426 error = sme_sensor_upstring(dict,
1427 "description",
1428 blah);
1429 if (error)
1430 break;
1431
1432 edata->upropset |= USERPROP_DESC;
1433 }
1434
1435 /*
1436 * did the user want to change the rfact?
1437 */
1438 obj2 = prop_dictionary_get(udict, "rfact");
1439 if (obj2) {
1440 targetfound = true;
1441 if (edata->flags & ENVSYS_FCHANGERFACT) {
1442 edata->rfact = prop_number_integer_value(obj2);
1443 edata->upropset |= USERPROP_RFACT;
1444 } else {
1445 error = ENOTSUP;
1446 break;
1447 }
1448 }
1449
1450 sdt = sme_get_description_table(SME_DESC_UNITS);
1451 for (i = 0; sdt[i].type != -1; i++)
1452 if (sdt[i].type == edata->units)
1453 break;
1454
1455
1456 /*
1457 * did the user want to set a critical capacity event?
1458 *
1459 * NOTE: if sme_event_register returns EEXIST that means
1460 * the object is already there, but this is not a real
1461 * error, because the object might be updated.
1462 */
1463 obj2 = prop_dictionary_get(udict, "critical-capacity");
1464 if (obj2) {
1465 targetfound = true;
1466 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1467 (edata->flags & ENVSYS_FPERCENT) == 0) {
1468 error = ENOTSUP;
1469 break;
1470 }
1471
1472 critval = prop_number_integer_value(obj2);
1473 error = sme_event_register(dict,
1474 edata,
1475 sme->sme_name,
1476 "critical-capacity",
1477 critval,
1478 PENVSYS_EVENT_BATT_USERCAP,
1479 sdt[i].crittype);
1480 if (error == EEXIST)
1481 error = 0;
1482 if (!error)
1483 edata->upropset |= USERPROP_BATTCAP;
1484 }
1485
1486 /*
1487 * did the user want to set a critical max event?
1488 */
1489 obj2 = prop_dictionary_get(udict, "critical-max");
1490 if (obj2) {
1491 targetfound = true;
1492 if (edata->units == ENVSYS_INDICATOR ||
1493 edata->flags & ENVSYS_FMONNOTSUPP) {
1494 error = ENOTSUP;
1495 break;
1496 }
1497
1498 critval = prop_number_integer_value(obj2);
1499 error = sme_event_register(dict,
1500 edata,
1501 sme->sme_name,
1502 "critical-max",
1503 critval,
1504 PENVSYS_EVENT_USER_CRITMAX,
1505 sdt[i].crittype);
1506 if (error == EEXIST)
1507 error = 0;
1508 if (!error)
1509 edata->upropset |= USERPROP_CRITMAX;
1510 }
1511
1512 /*
1513 * did the user want to set a critical min event?
1514 */
1515 obj2 = prop_dictionary_get(udict, "critical-min");
1516 if (obj2) {
1517 targetfound = true;
1518 if (edata->units == ENVSYS_INDICATOR ||
1519 edata->flags & ENVSYS_FMONNOTSUPP) {
1520 error = ENOTSUP;
1521 break;
1522 }
1523
1524 critval = prop_number_integer_value(obj2);
1525 error = sme_event_register(dict,
1526 edata,
1527 sme->sme_name,
1528 "critical-min",
1529 critval,
1530 PENVSYS_EVENT_USER_CRITMIN,
1531 sdt[i].crittype);
1532 if (error == EEXIST)
1533 error = 0;
1534 if (!error)
1535 edata->upropset |= USERPROP_CRITMIN;
1536 }
1537
1538 /*
1539 * All objects in dictionary were processed.
1540 */
1541 break;
1542 }
1543
1544 /* invalid target? return the error */
1545 if (!targetfound)
1546 error = EINVAL;
1547
1548 return error;
1549 }
1550