sysmon_envsys.c revision 1.70 1 /* $NetBSD: sysmon_envsys.c,v 1.70 2007/11/02 19:21:29 plunky 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.70 2007/11/02 19:21:29 plunky 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
434 /*
435 * previously, the ACPI sensor names included the
436 * device name. Include that in compatibility code.
437 */
438 if (strncmp(sme->sme_name, "acpi", 4) == 0)
439 (void)snprintf(binfo->desc, sizeof(binfo->desc),
440 "%s %s", sme->sme_name, edata->desc);
441 else
442 (void)strlcpy(binfo->desc, edata->desc,
443 sizeof(binfo->desc));
444 }
445
446 DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
447 __func__, binfo->units, binfo->validflags));
448 DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
449 __func__, binfo->desc, binfo->sensor));
450
451 binfo->sensor = oidx;
452 mutex_exit(&sme_mtx);
453
454 break;
455 }
456 default:
457 error = ENOTTY;
458 break;
459 }
460
461 return error;
462 }
463
464 /*
465 * sysmon_envsys_register:
466 *
467 * + Register a sysmon envsys device.
468 * + Create array of dictionaries for a device.
469 */
470 int
471 sysmon_envsys_register(struct sysmon_envsys *sme)
472 {
473 struct sme_evdrv {
474 SLIST_ENTRY(sme_evdrv) evdrv_head;
475 sme_event_drv_t *evdrv;
476 };
477 SLIST_HEAD(, sme_evdrv) sme_evdrv_list;
478 struct sme_evdrv *sme_evdrv = NULL;
479 struct sysmon_envsys *lsme;
480 prop_dictionary_t dict;
481 prop_array_t array;
482 envsys_data_t *edata = NULL;
483 int i, error = 0;
484
485 KASSERT(sme != NULL);
486 KASSERT(sme->sme_name != NULL);
487 KASSERT(sme->sme_sensor_data != NULL);
488
489 /*
490 * sme_nsensors is mandatory...
491 */
492 if (!sme->sme_nsensors)
493 return EINVAL;
494
495 /*
496 * sanity check: if SME_DISABLE_GTREDATA is not set,
497 * the sme_gtredata function callback must be non NULL.
498 */
499 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
500 if (!sme->sme_gtredata)
501 return EINVAL;
502 }
503
504
505 /* create the sysmon envsys device array. */
506 array = prop_array_create();
507 if (!array)
508 return ENOMEM;
509
510 /*
511 * Initialize the singly linked list for sensor descriptions.
512 */
513 SLIST_INIT(&sme->sme_names_list);
514
515 /*
516 * Initialize the singly linked list for driver events.
517 */
518 SLIST_INIT(&sme_evdrv_list);
519 /*
520 * Iterate over all sensors and create a dictionary per sensor,
521 * checking firstly if sensor description is unique.
522 */
523 for (i = 0; i < sme->sme_nsensors; i++) {
524 edata = &sme->sme_sensor_data[i];
525 /*
526 * Check if sensor description is unique.
527 */
528 if (sme_register_sensorname(sme, edata))
529 continue;
530
531 dict = prop_dictionary_create();
532 if (!dict) {
533 error = ENOMEM;
534 goto out2;
535 }
536
537 /*
538 * Create all objects in sensor's dictionary.
539 */
540 sme_evdrv = kmem_zalloc(sizeof(*sme_evdrv), KM_SLEEP);
541 sme_evdrv->evdrv = sme_add_sensor_dictionary(sme,
542 array, dict, edata);
543 if (sme_evdrv->evdrv)
544 SLIST_INSERT_HEAD(&sme_evdrv_list,
545 sme_evdrv, evdrv_head);
546 }
547
548 /*
549 * Check if requested sysmon_envsys device is valid
550 * and does not exist already in the list.
551 */
552 mutex_enter(&sme_mtx);
553 sme->sme_flags |= SME_FLAG_BUSY;
554 LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
555 if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
556 error = EEXIST;
557 goto out;
558 }
559 }
560
561 /*
562 * If the array does not contain any object (sensor), there's
563 * no need to attach the driver.
564 */
565 if (prop_array_count(array) == 0) {
566 error = EINVAL;
567 DPRINTF(("%s: empty array for '%s'\n", __func__,
568 sme->sme_name));
569 goto out;
570 }
571 /*
572 * Add the array into the global dictionary for the driver.
573 *
574 * <dict>
575 * <key>foo0</key>
576 * <array>
577 * ...
578 */
579 if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
580 error = EINVAL;
581 DPRINTF(("%s: prop_dictionary_set for '%s'\n", __func__,
582 sme->sme_name));
583 goto out;
584 }
585 /*
586 * Add the device into the list.
587 */
588 LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
589 sme->sme_fsensor = sysmon_envsys_next_sensor_index;
590 sysmon_envsys_next_sensor_index += sme->sme_nsensors;
591 out:
592 sme->sme_uniqsensors = 0;
593 sme->sme_flags &= ~SME_FLAG_BUSY;
594 mutex_exit(&sme_mtx);
595
596 if (error == 0) {
597 i = 0;
598 SLIST_FOREACH(sme_evdrv, &sme_evdrv_list, evdrv_head) {
599 if (i == 0)
600 sysmon_task_queue_init();
601 sysmon_task_queue_sched(0,
602 sme_event_drvadd, sme_evdrv->evdrv);
603 }
604 DPRINTF(("%s: driver '%s' registered (nsens=%d)\n",
605 __func__, sme->sme_name, sme->sme_nsensors));
606 }
607
608 out2:
609 while (!SLIST_EMPTY(&sme_evdrv_list)) {
610 sme_evdrv = SLIST_FIRST(&sme_evdrv_list);
611 SLIST_REMOVE_HEAD(&sme_evdrv_list, evdrv_head);
612 kmem_free(sme_evdrv, sizeof(*sme_evdrv));
613 }
614 if (!error)
615 return 0;
616
617 DPRINTF(("%s: failed to register '%s' (%d)\n", __func__,
618 sme->sme_name, error));
619 if (error != EEXIST) {
620 mutex_enter(&sme_mtx);
621 sme_event_unregister_all(sme->sme_name);
622 mutex_exit(&sme_mtx);
623 }
624 sysmon_envsys_destroy_plist(array);
625 return error;
626 }
627
628 /*
629 * sysmon_envsys_destroy_plist:
630 *
631 * + Remove all objects from the array of dictionaries that is
632 * created in a sysmon envsys device.
633 */
634 static void
635 sysmon_envsys_destroy_plist(prop_array_t array)
636 {
637 prop_object_iterator_t iter, iter2;
638 prop_dictionary_t dict;
639 prop_object_t obj;
640
641 KASSERT(array != NULL);
642
643 DPRINTFOBJ(("%s: objects in array=%d\n", __func__,
644 prop_array_count(array)));
645
646 iter = prop_array_iterator(array);
647 if (!iter)
648 return;
649
650 while ((dict = prop_object_iterator_next(iter))) {
651 KASSERT(prop_object_type(dict) == PROP_TYPE_DICTIONARY);
652 iter2 = prop_dictionary_iterator(dict);
653 if (!iter2)
654 goto out;
655 DPRINTFOBJ(("%s: iterating over dictionary\n", __func__));
656 while ((obj = prop_object_iterator_next(iter2)) != NULL) {
657 DPRINTFOBJ(("%s: obj=%s\n", __func__,
658 prop_dictionary_keysym_cstring_nocopy(obj)));
659 prop_dictionary_remove(dict,
660 prop_dictionary_keysym_cstring_nocopy(obj));
661 prop_object_iterator_reset(iter2);
662 }
663 prop_object_iterator_release(iter2);
664 DPRINTFOBJ(("%s: objects in dictionary:%d\n",
665 __func__, prop_dictionary_count(dict)));
666 prop_object_release(dict);
667 }
668
669 out:
670 prop_object_iterator_release(iter);
671 prop_object_release(array);
672 }
673
674 /*
675 * sysmon_envsys_unregister:
676 *
677 * + Unregister a sysmon envsys device.
678 */
679 void
680 sysmon_envsys_unregister(struct sysmon_envsys *sme)
681 {
682 struct sme_sensor_names *snames;
683 prop_array_t array;
684
685 KASSERT(sme != NULL);
686
687 mutex_enter(&sme_mtx);
688 while (sme->sme_flags & SME_FLAG_BUSY) {
689 sme->sme_flags |= SME_FLAG_WANTED;
690 cv_wait(&sme_cv, &sme_mtx);
691 }
692 sysmon_envsys_next_sensor_index -= sme->sme_nsensors;
693 /*
694 * Remove all sensor descriptions from the singly linked list.
695 */
696 while (!SLIST_EMPTY(&sme->sme_names_list)) {
697 snames = SLIST_FIRST(&sme->sme_names_list);
698 SLIST_REMOVE_HEAD(&sme->sme_names_list, sme_names);
699 kmem_free(snames, sizeof(*snames));
700 }
701 /*
702 * Unregister all events associated with this device.
703 */
704 sme_event_unregister_all(sme->sme_name);
705 LIST_REMOVE(sme, sme_list);
706 mutex_exit(&sme_mtx);
707 /*
708 * Remove the device (and all its objects) from the global dictionary.
709 */
710 array = prop_dictionary_get(sme_propd, sme->sme_name);
711 if (array && prop_object_type(array) == PROP_TYPE_ARRAY) {
712 prop_dictionary_remove(sme_propd, sme->sme_name);
713 sysmon_envsys_destroy_plist(array);
714 }
715 }
716
717 /*
718 * sysmon_envsys_find:
719 *
720 * + Find a sysmon envsys device.
721 */
722 struct sysmon_envsys *
723 sysmon_envsys_find(const char *name)
724 {
725 struct sysmon_envsys *sme;
726
727 mutex_enter(&sme_mtx);
728 again:
729 for (sme = LIST_FIRST(&sysmon_envsys_list); sme;
730 sme = LIST_NEXT(sme, sme_list)) {
731 if (strcmp(sme->sme_name, name) == 0) {
732 if (sme->sme_flags & SME_FLAG_BUSY) {
733 sme->sme_flags |= SME_FLAG_WANTED;
734 cv_wait(&sme_cv, &sme_mtx);
735 goto again;
736 }
737 sme->sme_flags |= SME_FLAG_BUSY;
738 break;
739 }
740 }
741 mutex_exit(&sme_mtx);
742 return sme;
743 }
744
745 /*
746 * sysmon_envsys_release:
747 *
748 * + Release a sysmon envsys device.
749 */
750 void
751 sysmon_envsys_release(struct sysmon_envsys *sme)
752 {
753 mutex_enter(&sme_mtx);
754 if (sme->sme_flags & SME_FLAG_WANTED)
755 cv_broadcast(&sme_cv);
756 sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
757 mutex_exit(&sme_mtx);
758 }
759
760 /* compatibility function */
761 struct sysmon_envsys *
762 sysmon_envsys_find_40(u_int idx)
763 {
764 struct sysmon_envsys *sme;
765
766 mutex_enter(&sme_mtx);
767 for (sme = LIST_FIRST(&sysmon_envsys_list); sme;
768 sme = LIST_NEXT(sme, sme_list)) {
769 if (idx >= sme->sme_fsensor &&
770 idx < (sme->sme_fsensor + sme->sme_nsensors))
771 break;
772 }
773 mutex_exit(&sme_mtx);
774 return sme;
775 }
776
777 /*
778 * sme_sensor_dictionary_get:
779 *
780 * + Returns a dictionary of a device specified by 'index'.
781 */
782 prop_dictionary_t
783 sme_sensor_dictionary_get(prop_array_t array, const char *index)
784 {
785 prop_object_iterator_t iter;
786 prop_dictionary_t dict;
787 prop_object_t obj;
788
789 KASSERT(array != NULL || index != NULL);
790
791 iter = prop_array_iterator(array);
792 if (!iter)
793 return NULL;
794
795 while ((dict = prop_object_iterator_next(iter))) {
796 obj = prop_dictionary_get(dict, "index");
797 if (prop_string_equals_cstring(obj, index))
798 break;
799 }
800
801 prop_object_iterator_release(iter);
802 return dict;
803 }
804
805 /*
806 * sme_remove_userprops:
807 *
808 * + Remove all properties from all devices that were set by
809 * the userland.
810 */
811 static void
812 sme_remove_userprops(void)
813 {
814 struct sysmon_envsys *sme;
815 prop_array_t array;
816 prop_dictionary_t sdict;
817 envsys_data_t *edata = NULL;
818 char tmp[ENVSYS_DESCLEN];
819 int i, ptype;
820
821 mutex_enter(&sme_mtx);
822 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
823 sme->sme_flags |= SME_FLAG_BUSY;
824 array = prop_dictionary_get(sme_propd, sme->sme_name);
825
826 for (i = 0; i < sme->sme_nsensors; i++) {
827 edata = &sme->sme_sensor_data[i];
828 (void)snprintf(tmp, sizeof(tmp), "sensor%d",
829 edata->sensor);
830 sdict = sme_sensor_dictionary_get(array, tmp);
831
832 if (edata->upropset & USERPROP_BATTCAP) {
833 prop_dictionary_remove(sdict,
834 "critical-capacity");
835 ptype = PENVSYS_EVENT_BATT_USERCAP;
836 sme_event_unregister(edata->desc, ptype);
837 }
838
839 if (edata->upropset & USERPROP_CRITMAX) {
840 prop_dictionary_remove(sdict,
841 "critical-max");
842 ptype = PENVSYS_EVENT_USER_CRITMAX;
843 sme_event_unregister(edata->desc, ptype);
844 }
845
846 if (edata->upropset & USERPROP_CRITMIN) {
847 prop_dictionary_remove(sdict,
848 "critical-min");
849 ptype = PENVSYS_EVENT_USER_CRITMIN;
850 sme_event_unregister(edata->desc, ptype);
851 }
852
853 if (edata->upropset & USERPROP_RFACT) {
854 (void)sme_sensor_upint32(sdict, "rfact", 0);
855 edata->rfact = 0;
856 }
857
858 if (edata->upropset & USERPROP_DESC)
859 (void)sme_sensor_upstring(sdict,
860 "description", edata->desc);
861 }
862
863 if (edata->upropset)
864 edata->upropset = 0;
865
866 sme->sme_flags &= ~SME_FLAG_BUSY;
867 }
868 mutex_exit(&sme_mtx);
869 }
870 /*
871 * sme_register_sensorname:
872 *
873 * + Register a sensor description into the list maintained per device.
874 */
875 static int
876 sme_register_sensorname(struct sysmon_envsys *sme, envsys_data_t *edata)
877 {
878 struct sme_sensor_names *snames, *snames2 = NULL;
879
880 KASSERT(edata != NULL);
881
882 SLIST_FOREACH(snames2, &sme->sme_names_list, sme_names) {
883 /*
884 * Match sensors with empty and duplicate description.
885 */
886 if (strlen(edata->desc) == 0 ||
887 strcmp(snames2->desc, edata->desc) == 0)
888 if (snames2->assigned) {
889 edata->flags |= ENVSYS_FNOTVALID;
890 DPRINTF(("%s: wrong sensor name='%s'\n",
891 sme->sme_name, edata->desc));
892 return EEXIST;
893 }
894 }
895
896 snames = kmem_zalloc(sizeof(*snames), KM_NOSLEEP);
897 if (!snames)
898 return ENOMEM;
899
900 snames->assigned = true;
901 (void)strlcpy(snames->desc, edata->desc, sizeof(snames->desc));
902 DPRINTF(("%s: registering sensor name='%s'\n",
903 sme->sme_name, edata->desc));
904 SLIST_INSERT_HEAD(&sme->sme_names_list, snames, sme_names);
905 sme->sme_uniqsensors++;
906
907 return 0;
908 }
909
910 /*
911 * sme_add_sensor_dictionary:
912 *
913 * + Add the objects into the dictionary.
914 */
915 sme_event_drv_t *
916 sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
917 prop_dictionary_t dict, envsys_data_t *edata)
918 {
919 const struct sme_description_table *sdt, *sdt_units;
920 sme_event_drv_t *sme_evdrv_t = NULL;
921 int i, j;
922 char indexstr[ENVSYS_DESCLEN];
923
924 i = j = 0;
925
926 /* find the correct unit for this sensor. */
927 sdt_units = sme_get_description_table(SME_DESC_UNITS);
928 for (i = 0; sdt_units[i].type != -1; i++)
929 if (sdt_units[i].type == edata->units)
930 break;
931
932 if (strcmp(sdt_units[i].desc, "unknown") == 0) {
933 DPRINTF(("%s: invalid units type for sensor=%d\n",
934 __func__, edata->sensor));
935 goto invalidate_sensor;
936 }
937
938 /*
939 * Add the index sensor string.
940 *
941 * ...
942 * <key>index</key>
943 * <string>sensor0</string>
944 * ...
945 */
946 (void)snprintf(indexstr, sizeof(indexstr), "sensor%d", edata->sensor);
947 if (sme_sensor_upstring(dict, "index", indexstr))
948 goto invalidate_sensor;
949
950 /*
951 * ...
952 * <key>type</key>
953 * <string>foo</string>
954 * <key>description</key>
955 * <string>blah blah</string>
956 * ...
957 */
958 if (sme_sensor_upstring(dict, "type", sdt_units[i].desc))
959 goto invalidate_sensor;
960
961 if (strlen(edata->desc) == 0) {
962 DPRINTF(("%s: invalid description for sensor=%d\n",
963 __func__, edata->sensor));
964 goto invalidate_sensor;
965 }
966
967 if (sme_sensor_upstring(dict, "description", edata->desc))
968 goto invalidate_sensor;
969
970 /*
971 * Add sensor's state description.
972 *
973 * ...
974 * <key>state</key>
975 * <string>valid</string>
976 * ...
977 */
978 sdt = sme_get_description_table(SME_DESC_STATES);
979 for (j = 0; sdt[j].type != -1; j++)
980 if (sdt[j].type == edata->state)
981 break;
982
983 if (strcmp(sdt[j].desc, "unknown") == 0) {
984 DPRINTF(("%s: invalid state for sensor=%d\n",
985 __func__, edata->sensor));
986 goto invalidate_sensor;
987 }
988
989 DPRINTF(("%s: sensor desc=%s type=%d state=%d\n",
990 __func__, edata->desc, edata->units, edata->state));
991
992 if (sme_sensor_upstring(dict, "state", sdt[j].desc))
993 goto invalidate_sensor;
994
995 /*
996 * Add the monitoring boolean object:
997 *
998 * ...
999 * <key>monitoring-supported</key>
1000 * <true/>
1001 * ...
1002 *
1003 * always false on Battery state, Drive and Indicator types.
1004 * They cannot be monitored.
1005 *
1006 */
1007 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1008 (edata->units == ENVSYS_INDICATOR) ||
1009 (edata->units == ENVSYS_DRIVE) ||
1010 (edata->units == ENVSYS_BATTERY_STATE)) {
1011 if (sme_sensor_upbool(dict, "monitoring-supported", false))
1012 goto out;
1013 } else {
1014 if (sme_sensor_upbool(dict, "monitoring-supported", true))
1015 goto out;
1016 }
1017
1018 /*
1019 * Add the percentage boolean object, true if ENVSYS_FPERCENT
1020 * is set or false otherwise.
1021 *
1022 * ...
1023 * <key>want-percentage</key>
1024 * <true/>
1025 * ...
1026 */
1027 if (edata->flags & ENVSYS_FPERCENT)
1028 if (sme_sensor_upbool(dict, "want-percentage", true))
1029 goto out;
1030
1031 /*
1032 * Add the allow-rfact boolean object, true if
1033 * ENVSYS_FCHANGERFACT if set or false otherwise.
1034 *
1035 * ...
1036 * <key>allow-rfact</key>
1037 * <true/>
1038 * ...
1039 */
1040 if (edata->units == ENVSYS_SVOLTS_DC ||
1041 edata->units == ENVSYS_SVOLTS_AC) {
1042 if (edata->flags & ENVSYS_FCHANGERFACT) {
1043 if (sme_sensor_upbool(dict, "allow-rfact", true))
1044 goto out;
1045 } else {
1046 if (sme_sensor_upbool(dict, "allow-rfact", false))
1047 goto out;
1048 }
1049 }
1050
1051
1052 /*
1053 * Add the battery-state object for battery state sensors:
1054 *
1055 * ...
1056 * <key>battery-state</key>
1057 * <string>NORMAL</string>
1058 * ...
1059 */
1060 if (edata->units == ENVSYS_BATTERY_STATE) {
1061 sdt = sme_get_description_table(SME_DESC_BATTERY_STATES);
1062 for (j = 0; sdt[j].type != -1; j++)
1063 if (sdt[j].type == edata->value_cur)
1064 break;
1065
1066 if (sme_sensor_upstring(dict, "battery-state", sdt[j].desc))
1067 goto out;
1068 }
1069
1070 /*
1071 * Add the drive-state object for drive sensors:
1072 *
1073 * ...
1074 * <key>drive-state</key>
1075 * <string>drive is online</string>
1076 * ...
1077 */
1078 if (edata->units == ENVSYS_DRIVE) {
1079 sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
1080 for (j = 0; sdt[j].type != -1; j++)
1081 if (sdt[j].type == edata->value_cur)
1082 break;
1083
1084 if (sme_sensor_upstring(dict, "drive-state", sdt[j].desc))
1085 goto out;
1086 }
1087
1088 /*
1089 * if sensor is enabled, add the following properties...
1090 */
1091 if (edata->state == ENVSYS_SVALID) {
1092 /*
1093 * ...
1094 * <key>rpms</key>
1095 * <integer>2500</integer>
1096 * <key>rfact</key>
1097 * <integer>10000</integer>
1098 * <key>cur-value</key>
1099 * <integer>1250</integer>
1100 * <key>min-value</key>
1101 * <integer>800</integer>
1102 * <key>max-value</integer>
1103 * <integer>3000</integer>
1104 * <key>avg-value</integer>
1105 * <integer>1400</integer>
1106 * </dict>
1107 */
1108 if (edata->units == ENVSYS_SFANRPM)
1109 if (sme_sensor_upuint32(dict, "rpms", edata->rpms))
1110 goto out;
1111
1112 if (edata->units == ENVSYS_SVOLTS_AC ||
1113 edata->units == ENVSYS_SVOLTS_DC)
1114 if (sme_sensor_upint32(dict, "rfact", edata->rfact))
1115 goto out;
1116
1117 if (sme_sensor_upint32(dict, "cur-value", edata->value_cur))
1118 goto out;
1119
1120 if (edata->flags & ENVSYS_FVALID_MIN) {
1121 if (sme_sensor_upint32(dict,
1122 "min-value",
1123 edata->value_min))
1124 goto out;
1125 }
1126
1127 if (edata->flags & ENVSYS_FVALID_MAX) {
1128 if (sme_sensor_upint32(dict,
1129 "max-value",
1130 edata->value_max))
1131 goto out;
1132 }
1133
1134 if (edata->flags & ENVSYS_FVALID_AVG) {
1135 if (sme_sensor_upint32(dict,
1136 "avg-value",
1137 edata->value_avg))
1138 goto out;
1139 }
1140 }
1141
1142 /*
1143 * ...
1144 * </array>
1145 *
1146 * Add the dictionary into the array.
1147 *
1148 */
1149
1150 if (!prop_array_set(array, sme->sme_uniqsensors - 1, dict)) {
1151 DPRINTF(("%s: prop_array_add\n", __func__));
1152 goto invalidate_sensor;
1153 }
1154
1155 /*
1156 * Add a new event if a monitoring flag was set.
1157 */
1158 if (edata->monitor) {
1159 sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
1160 sme_evdrv_t->sdict = dict;
1161 sme_evdrv_t->edata = edata;
1162 sme_evdrv_t->sme = sme;
1163 sme_evdrv_t->powertype = sdt_units[i].crittype;
1164 }
1165
1166 out:
1167 return sme_evdrv_t;
1168
1169 invalidate_sensor:
1170 edata->flags |= ENVSYS_FNOTVALID;
1171 prop_object_release(dict);
1172 return sme_evdrv_t;
1173 }
1174
1175 /*
1176 * sme_update_dictionary:
1177 *
1178 * + Update per-sensor dictionaries with new values if there were
1179 * changes, otherwise the object in dictionary is untouched.
1180 */
1181 int
1182 sme_update_dictionary(struct sysmon_envsys *sme)
1183 {
1184 const struct sme_description_table *sdt;
1185 envsys_data_t *edata;
1186 prop_object_t array, dict;
1187 int i, j, error, invalid;
1188
1189 KASSERT(mutex_owned(&sme_mtx));
1190
1191 error = invalid = 0;
1192 array = dict = NULL;
1193
1194 /* retrieve the array of dictionaries in device. */
1195 array = prop_dictionary_get(sme_propd, sme->sme_name);
1196 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
1197 DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
1198 return EINVAL;
1199 }
1200
1201 /*
1202 * - iterate over all sensors.
1203 * - fetch new data.
1204 * - check if data in dictionary is different than new data.
1205 * - update dictionary if there were changes.
1206 */
1207 DPRINTF(("%s: updating '%s' with nsensors=%d\n", __func__,
1208 sme->sme_name, sme->sme_nsensors));
1209
1210 for (i = 0; i < sme->sme_nsensors; i++) {
1211 edata = &sme->sme_sensor_data[i];
1212 /* skip invalid sensors */
1213 if (edata->flags & ENVSYS_FNOTVALID) {
1214 DPRINTF(("%s: invalid sensor=%s idx=%d\n",
1215 __func__, edata->desc, edata->sensor));
1216 invalid++;
1217 continue;
1218 }
1219
1220 /*
1221 * refresh sensor data via sme_gtredata only if the
1222 * flag is not set.
1223 */
1224 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
1225 error = (*sme->sme_gtredata)(sme, edata);
1226 if (error) {
1227 DPRINTF(("%s: gtredata[%d] failed\n",
1228 __func__, i));
1229 return error;
1230 }
1231 }
1232
1233 /* retrieve sensor's dictionary. */
1234 dict = prop_array_get(array, i - invalid);
1235 if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
1236 DPRINTF(("%s: not a dictionary (%d:%s)\n",
1237 __func__, edata->sensor, sme->sme_name));
1238 return EINVAL;
1239 }
1240
1241 /* update sensor's state */
1242 sdt = sme_get_description_table(SME_DESC_STATES);
1243 for (j = 0; sdt[j].type != -1; j++)
1244 if (sdt[j].type == edata->state)
1245 break;
1246
1247 DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
1248 "units=%d sensor=%d\n", __func__, sdt[j].desc,
1249 sdt[j].type, edata->flags, edata->units, edata->sensor));
1250
1251 /* update sensor state */
1252 error = sme_sensor_upstring(dict, "state", sdt[j].desc);
1253 if (error)
1254 break;
1255
1256 /* update sensor type */
1257 sdt = sme_get_description_table(SME_DESC_UNITS);
1258 for (j = 0; sdt[j].type != -1; j++)
1259 if (sdt[j].type == edata->units)
1260 break;
1261
1262 error = sme_sensor_upstring(dict, "type", sdt[j].desc);
1263 if (error)
1264 break;
1265
1266 /* update sensor current value */
1267 error = sme_sensor_upint32(dict,
1268 "cur-value",
1269 edata->value_cur);
1270 if (error)
1271 break;
1272
1273 /*
1274 * Integer and Indicator types do not the following
1275 * values, so skip them.
1276 */
1277 if (edata->units == ENVSYS_INTEGER ||
1278 edata->units == ENVSYS_INDICATOR)
1279 continue;
1280
1281 /* update sensor flags */
1282 if (edata->flags & ENVSYS_FPERCENT) {
1283 error = sme_sensor_upbool(dict,
1284 "want-percentage",
1285 true);
1286 if (error)
1287 break;
1288 }
1289
1290 if (edata->flags & ENVSYS_FVALID_MAX) {
1291 error = sme_sensor_upint32(dict,
1292 "max-value",
1293 edata->value_max);
1294 if (error)
1295 break;
1296 }
1297
1298 if (edata->flags & ENVSYS_FVALID_MIN) {
1299 error = sme_sensor_upint32(dict,
1300 "min-value",
1301 edata->value_min);
1302 if (error)
1303 break;
1304 }
1305
1306 if (edata->flags & ENVSYS_FVALID_AVG) {
1307 error = sme_sensor_upint32(dict,
1308 "avg-value",
1309 edata->value_avg);
1310 if (error)
1311 break;
1312 }
1313
1314 /* update 'rpms' only in ENVSYS_SFANRPM. */
1315 if (edata->units == ENVSYS_SFANRPM) {
1316 error = sme_sensor_upuint32(dict,
1317 "rpms",
1318 edata->rpms);
1319 if (error)
1320 break;
1321 }
1322
1323 /* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
1324 if (edata->units == ENVSYS_SVOLTS_AC ||
1325 edata->units == ENVSYS_SVOLTS_DC) {
1326 error = sme_sensor_upint32(dict,
1327 "rfact",
1328 edata->rfact);
1329 if (error)
1330 break;
1331 }
1332
1333 /* update 'drive-state' only in ENVSYS_DRIVE. */
1334 if (edata->units == ENVSYS_DRIVE) {
1335 sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
1336 for (j = 0; sdt[j].type != -1; j++)
1337 if (sdt[j].type == edata->value_cur)
1338 break;
1339
1340 error = sme_sensor_upstring(dict,
1341 "drive-state",
1342 sdt[j].desc);
1343 if (error)
1344 break;
1345 }
1346
1347 /* update 'battery-state' only in ENVSYS_BATTERY_STATE. */
1348 if (edata->units == ENVSYS_BATTERY_STATE) {
1349 sdt =
1350 sme_get_description_table(SME_DESC_BATTERY_STATES);
1351 for (j = 0; sdt[j].type != -1; j++)
1352 if (sdt[j].type == edata->value_cur)
1353 break;
1354
1355 error = sme_sensor_upstring(dict,
1356 "battery-state",
1357 sdt[j].desc);
1358 if (error)
1359 break;
1360 }
1361 }
1362
1363 return error;
1364 }
1365
1366 /*
1367 * sme_userset_dictionary:
1368 *
1369 * + Parse the userland sensor's dictionary and run the appropiate
1370 * tasks that was requested.
1371 */
1372 int
1373 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
1374 prop_array_t array)
1375 {
1376 const struct sme_description_table *sdt;
1377 envsys_data_t *edata, *nedata;
1378 prop_dictionary_t dict;
1379 prop_object_t obj, obj1, obj2;
1380 int32_t critval;
1381 int i, invalid, error;
1382 const char *blah, *sname;
1383 bool targetfound = false;
1384
1385 error = invalid = 0;
1386 blah = sname = NULL;
1387
1388 /* get sensor's index from userland dictionary. */
1389 obj = prop_dictionary_get(udict, "index");
1390 if (prop_object_type(obj) != PROP_TYPE_STRING) {
1391 DPRINTF(("%s: sensor-name failed\n", __func__));
1392 return EINVAL;
1393 }
1394
1395 /* iterate over the sensors to find the right one */
1396 for (i = 0; i < sme->sme_nsensors; i++) {
1397 edata = &sme->sme_sensor_data[i];
1398 /*
1399 * skip invalid sensors.
1400 */
1401 if (edata->flags & ENVSYS_FNOTVALID) {
1402 invalid++;
1403 continue;
1404 }
1405
1406 dict = prop_array_get(array, i - invalid);
1407 obj1 = prop_dictionary_get(dict, "index");
1408
1409 /* is it our sensor? */
1410 if (!prop_string_equals(obj1, obj))
1411 continue;
1412
1413 /*
1414 * Check if a new description operation was
1415 * requested by the user and set new description.
1416 */
1417 if ((obj2 = prop_dictionary_get(udict, "description"))) {
1418 targetfound = true;
1419 blah = prop_string_cstring_nocopy(obj2);
1420
1421 for (i = 0; i < sme->sme_nsensors; i++) {
1422 if (i == edata->sensor)
1423 continue;
1424
1425 nedata = &sme->sme_sensor_data[i];
1426 if (strcmp(blah, nedata->desc) == 0) {
1427 error = EEXIST;
1428 break;
1429 }
1430 }
1431
1432 if (error)
1433 break;
1434
1435 error = sme_sensor_upstring(dict,
1436 "description",
1437 blah);
1438 if (error)
1439 break;
1440
1441 edata->upropset |= USERPROP_DESC;
1442 }
1443
1444 /*
1445 * did the user want to change the rfact?
1446 */
1447 obj2 = prop_dictionary_get(udict, "rfact");
1448 if (obj2) {
1449 targetfound = true;
1450 if (edata->flags & ENVSYS_FCHANGERFACT) {
1451 edata->rfact = prop_number_integer_value(obj2);
1452 edata->upropset |= USERPROP_RFACT;
1453 } else {
1454 error = ENOTSUP;
1455 break;
1456 }
1457 }
1458
1459 sdt = sme_get_description_table(SME_DESC_UNITS);
1460 for (i = 0; sdt[i].type != -1; i++)
1461 if (sdt[i].type == edata->units)
1462 break;
1463
1464
1465 /*
1466 * did the user want to set a critical capacity event?
1467 *
1468 * NOTE: if sme_event_register returns EEXIST that means
1469 * the object is already there, but this is not a real
1470 * error, because the object might be updated.
1471 */
1472 obj2 = prop_dictionary_get(udict, "critical-capacity");
1473 if (obj2) {
1474 targetfound = true;
1475 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1476 (edata->flags & ENVSYS_FPERCENT) == 0) {
1477 error = ENOTSUP;
1478 break;
1479 }
1480
1481 critval = prop_number_integer_value(obj2);
1482 error = sme_event_register(dict,
1483 edata,
1484 sme->sme_name,
1485 "critical-capacity",
1486 critval,
1487 PENVSYS_EVENT_BATT_USERCAP,
1488 sdt[i].crittype);
1489 if (error == EEXIST)
1490 error = 0;
1491 if (!error)
1492 edata->upropset |= USERPROP_BATTCAP;
1493 }
1494
1495 /*
1496 * did the user want to set a critical max event?
1497 */
1498 obj2 = prop_dictionary_get(udict, "critical-max");
1499 if (obj2) {
1500 targetfound = true;
1501 if (edata->units == ENVSYS_INDICATOR ||
1502 edata->flags & ENVSYS_FMONNOTSUPP) {
1503 error = ENOTSUP;
1504 break;
1505 }
1506
1507 critval = prop_number_integer_value(obj2);
1508 error = sme_event_register(dict,
1509 edata,
1510 sme->sme_name,
1511 "critical-max",
1512 critval,
1513 PENVSYS_EVENT_USER_CRITMAX,
1514 sdt[i].crittype);
1515 if (error == EEXIST)
1516 error = 0;
1517 if (!error)
1518 edata->upropset |= USERPROP_CRITMAX;
1519 }
1520
1521 /*
1522 * did the user want to set a critical min event?
1523 */
1524 obj2 = prop_dictionary_get(udict, "critical-min");
1525 if (obj2) {
1526 targetfound = true;
1527 if (edata->units == ENVSYS_INDICATOR ||
1528 edata->flags & ENVSYS_FMONNOTSUPP) {
1529 error = ENOTSUP;
1530 break;
1531 }
1532
1533 critval = prop_number_integer_value(obj2);
1534 error = sme_event_register(dict,
1535 edata,
1536 sme->sme_name,
1537 "critical-min",
1538 critval,
1539 PENVSYS_EVENT_USER_CRITMIN,
1540 sdt[i].crittype);
1541 if (error == EEXIST)
1542 error = 0;
1543 if (!error)
1544 edata->upropset |= USERPROP_CRITMIN;
1545 }
1546
1547 /*
1548 * All objects in dictionary were processed.
1549 */
1550 break;
1551 }
1552
1553 /* invalid target? return the error */
1554 if (!targetfound)
1555 error = EINVAL;
1556
1557 return error;
1558 }
1559