1 /* $NetBSD: sysmon_envsys.c,v 1.89 2009/07/08 17:54:27 pgoyette Exp $ */
4 * Copyright (c) 2007, 2008 Juan Romero Pardines.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 * Copyright (c) 2000 Zembu Labs, Inc.
30 * All rights reserved.
32 * Author: Jason R. Thorpe <thorpej@zembu.com>
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
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.
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.
62 * Environmental sensor framework for sysmon, exported to userland
66 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.89 2009/07/08 17:54:27 pgoyette Exp $");
69 #include <sys/param.h>
70 #include <sys/types.h>
72 #include <sys/errno.h>
73 #include <sys/fcntl.h>
74 #include <sys/kernel.h>
75 #include <sys/systm.h>
77 #include <sys/mutex.h>
80 /* #define ENVSYS_DEBUG */
81 #include <dev/sysmon/sysmonvar.h>
82 #include <dev/sysmon/sysmon_envsysvar.h>
83 #include <dev/sysmon/sysmon_taskq.h>
85 kmutex_t sme_global_mtx
;
87 static prop_dictionary_t sme_propd
;
88 static uint32_t sysmon_envsys_next_sensor_index
;
89 static struct sysmon_envsys
*sysmon_envsys_find_40(u_int
);
91 static void sysmon_envsys_destroy_plist(prop_array_t
);
92 static void sme_remove_userprops(void);
93 static int sme_add_property_dictionary(struct sysmon_envsys
*, prop_array_t
,
95 static void sme_initial_refresh(void *);
100 * + Initialize global mutex, dictionary and the linked list.
103 sysmon_envsys_init(void)
105 LIST_INIT(&sysmon_envsys_list
);
106 mutex_init(&sme_global_mtx
, MUTEX_DEFAULT
, IPL_NONE
);
107 sme_propd
= prop_dictionary_create();
113 * + Open the system monitor device.
116 sysmonopen_envsys(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
122 * sysmonclose_envsys:
124 * + Close the system monitor device.
127 sysmonclose_envsys(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
133 * sysmonioctl_envsys:
135 * + Perform a sysmon envsys control request.
138 sysmonioctl_envsys(dev_t dev
, u_long cmd
, void *data
, int flag
, struct lwp
*l
)
140 struct sysmon_envsys
*sme
= NULL
;
146 * To update the global dictionary with latest data from devices.
148 case ENVSYS_GETDICTIONARY
:
150 struct plistref
*plist
= (struct plistref
*)data
;
153 * Update dictionaries on all sysmon envsys devices
156 mutex_enter(&sme_global_mtx
);
157 LIST_FOREACH(sme
, &sysmon_envsys_list
, sme_list
) {
158 sysmon_envsys_acquire(sme
, false);
159 error
= sme_update_dictionary(sme
);
161 DPRINTF(("%s: sme_update_dictionary, "
162 "error=%d\n", __func__
, error
));
163 sysmon_envsys_release(sme
, false);
164 mutex_exit(&sme_global_mtx
);
167 sysmon_envsys_release(sme
, false);
169 mutex_exit(&sme_global_mtx
);
171 * Copy global dictionary to userland.
173 error
= prop_dictionary_copyout_ioctl(plist
, cmd
, sme_propd
);
177 * To set properties on multiple devices.
179 case ENVSYS_SETDICTIONARY
:
181 const struct plistref
*plist
= (const struct plistref
*)data
;
182 prop_dictionary_t udict
;
183 prop_object_iterator_t iter
, iter2
;
184 prop_object_t obj
, obj2
;
185 prop_array_t array_u
, array_k
;
186 const char *devname
= NULL
;
188 if ((flag
& FWRITE
) == 0)
192 * Get dictionary from userland.
194 error
= prop_dictionary_copyin_ioctl(plist
, cmd
, &udict
);
196 DPRINTF(("%s: copyin_ioctl error=%d\n",
201 iter
= prop_dictionary_iterator(udict
);
203 prop_object_release(udict
);
208 * Iterate over the userland dictionary and process
209 * the list of devices.
211 while ((obj
= prop_object_iterator_next(iter
))) {
212 array_u
= prop_dictionary_get_keysym(udict
, obj
);
213 if (prop_object_type(array_u
) != PROP_TYPE_ARRAY
) {
214 prop_object_iterator_release(iter
);
215 prop_object_release(udict
);
219 devname
= prop_dictionary_keysym_cstring_nocopy(obj
);
220 DPRINTF(("%s: processing the '%s' array requests\n",
224 * find the correct sme device.
226 sme
= sysmon_envsys_find(devname
);
228 DPRINTF(("%s: NULL sme\n", __func__
));
229 prop_object_iterator_release(iter
);
230 prop_object_release(udict
);
235 * Find the correct array object with the string
236 * supplied by the userland dictionary.
238 array_k
= prop_dictionary_get(sme_propd
, devname
);
239 if (prop_object_type(array_k
) != PROP_TYPE_ARRAY
) {
240 DPRINTF(("%s: array device failed\n",
242 sysmon_envsys_release(sme
, false);
243 prop_object_iterator_release(iter
);
244 prop_object_release(udict
);
248 iter2
= prop_array_iterator(array_u
);
250 sysmon_envsys_release(sme
, false);
251 prop_object_iterator_release(iter
);
252 prop_object_release(udict
);
257 * Iterate over the array of dictionaries to
258 * process the list of sensors and properties.
260 while ((obj2
= prop_object_iterator_next(iter2
))) {
262 * do the real work now.
264 error
= sme_userset_dictionary(sme
,
268 sysmon_envsys_release(sme
, false);
269 prop_object_iterator_release(iter2
);
270 prop_object_iterator_release(iter
);
271 prop_object_release(udict
);
276 sysmon_envsys_release(sme
, false);
277 prop_object_iterator_release(iter2
);
280 prop_object_iterator_release(iter
);
281 prop_object_release(udict
);
285 * To remove all properties from all devices registered.
287 case ENVSYS_REMOVEPROPS
:
289 const struct plistref
*plist
= (const struct plistref
*)data
;
290 prop_dictionary_t udict
;
293 if ((flag
& FWRITE
) == 0)
296 error
= prop_dictionary_copyin_ioctl(plist
, cmd
, &udict
);
298 DPRINTF(("%s: copyin_ioctl error=%d\n",
303 obj
= prop_dictionary_get(udict
, "envsys-remove-props");
304 if (!obj
|| !prop_bool_true(obj
)) {
305 DPRINTF(("%s: invalid 'envsys-remove-props'\n",
310 prop_object_release(udict
);
311 sme_remove_userprops();
316 * Compatibility ioctls with the old interface, only implemented
317 * ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough to make old
320 case ENVSYS_GTREDATA
:
322 struct envsys_tre_data
*tred
= (void *)data
;
323 envsys_data_t
*edata
= NULL
;
326 tred
->validflags
= 0;
328 sme
= sysmon_envsys_find_40(tred
->sensor
);
333 tred
->sensor
= SME_SENSOR_IDX(sme
, tred
->sensor
);
335 DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
336 __func__
, tred
->sensor
, oidx
, sme
->sme_name
,
339 TAILQ_FOREACH(edata
, &sme
->sme_sensors_list
, sensors_head
) {
340 if (edata
->sensor
== tred
->sensor
) {
347 sysmon_envsys_release(sme
, false);
352 if (tred
->sensor
< sme
->sme_nsensors
) {
353 if ((sme
->sme_flags
& SME_DISABLE_REFRESH
) == 0 &&
354 (sme
->sme_flags
& SME_POLL_ONLY
) == 0) {
355 mutex_enter(&sme
->sme_mtx
);
356 (*sme
->sme_refresh
)(sme
, edata
);
357 mutex_exit(&sme
->sme_mtx
);
361 * copy required values to the old interface.
363 tred
->sensor
= edata
->sensor
;
364 tred
->cur
.data_us
= edata
->value_cur
;
365 tred
->cur
.data_s
= edata
->value_cur
;
366 tred
->max
.data_us
= edata
->value_max
;
367 tred
->max
.data_s
= edata
->value_max
;
368 tred
->min
.data_us
= edata
->value_min
;
369 tred
->min
.data_s
= edata
->value_min
;
370 tred
->avg
.data_us
= edata
->value_avg
;
371 tred
->avg
.data_s
= edata
->value_avg
;
372 if (edata
->units
== ENVSYS_BATTERY_CHARGE
)
373 tred
->units
= ENVSYS_INDICATOR
;
375 tred
->units
= edata
->units
;
377 tred
->validflags
|= ENVSYS_FVALID
;
378 tred
->validflags
|= ENVSYS_FCURVALID
;
380 if (edata
->flags
& ENVSYS_FPERCENT
) {
381 tred
->validflags
|= ENVSYS_FMAXVALID
;
382 tred
->validflags
|= ENVSYS_FFRACVALID
;
385 if (edata
->state
== ENVSYS_SINVALID
) {
386 tred
->validflags
&= ~ENVSYS_FCURVALID
;
387 tred
->cur
.data_us
= tred
->cur
.data_s
= 0;
390 DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
391 __func__
, edata
->desc
, tred
->cur
.data_s
));
392 DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
393 " tred->sensor=%d\n", __func__
, tred
->validflags
,
394 tred
->units
, tred
->sensor
));
397 sysmon_envsys_release(sme
, false);
401 case ENVSYS_GTREINFO
:
403 struct envsys_basic_info
*binfo
= (void *)data
;
404 envsys_data_t
*edata
= NULL
;
407 binfo
->validflags
= 0;
409 sme
= sysmon_envsys_find_40(binfo
->sensor
);
413 oidx
= binfo
->sensor
;
414 binfo
->sensor
= SME_SENSOR_IDX(sme
, binfo
->sensor
);
416 TAILQ_FOREACH(edata
, &sme
->sme_sensors_list
, sensors_head
) {
417 if (edata
->sensor
== binfo
->sensor
) {
424 sysmon_envsys_release(sme
, false);
429 binfo
->validflags
|= ENVSYS_FVALID
;
431 if (binfo
->sensor
< sme
->sme_nsensors
) {
432 if (edata
->units
== ENVSYS_BATTERY_CHARGE
)
433 binfo
->units
= ENVSYS_INDICATOR
;
435 binfo
->units
= edata
->units
;
438 * previously, the ACPI sensor names included the
439 * device name. Include that in compatibility code.
441 if (strncmp(sme
->sme_name
, "acpi", 4) == 0)
442 (void)snprintf(binfo
->desc
, sizeof(binfo
->desc
),
443 "%s %s", sme
->sme_name
, edata
->desc
);
445 (void)strlcpy(binfo
->desc
, edata
->desc
,
446 sizeof(binfo
->desc
));
449 DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
450 __func__
, binfo
->units
, binfo
->validflags
));
451 DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
452 __func__
, binfo
->desc
, binfo
->sensor
));
454 binfo
->sensor
= oidx
;
455 sysmon_envsys_release(sme
, false);
468 * sysmon_envsys_create:
470 * + Allocates a new sysmon_envsys object and initializes the
471 * stuff for sensors and events.
473 struct sysmon_envsys
*
474 sysmon_envsys_create(void)
476 struct sysmon_envsys
*sme
;
478 sme
= kmem_zalloc(sizeof(*sme
), KM_SLEEP
);
479 TAILQ_INIT(&sme
->sme_sensors_list
);
480 LIST_INIT(&sme
->sme_events_list
);
481 mutex_init(&sme
->sme_mtx
, MUTEX_DEFAULT
, IPL_NONE
);
482 cv_init(&sme
->sme_condvar
, "sme_wait");
488 * sysmon_envsys_destroy:
490 * + Removes all sensors from the tail queue, destroys the callout
491 * and frees the sysmon_envsys object.
494 sysmon_envsys_destroy(struct sysmon_envsys
*sme
)
496 envsys_data_t
*edata
;
498 KASSERT(sme
!= NULL
);
500 while (!TAILQ_EMPTY(&sme
->sme_sensors_list
)) {
501 edata
= TAILQ_FIRST(&sme
->sme_sensors_list
);
502 TAILQ_REMOVE(&sme
->sme_sensors_list
, edata
, sensors_head
);
504 mutex_destroy(&sme
->sme_mtx
);
505 cv_destroy(&sme
->sme_condvar
);
506 kmem_free(sme
, sizeof(*sme
));
510 * sysmon_envsys_sensor_attach:
512 * + Attachs a sensor into a sysmon_envsys device checking that units
513 * is set to a valid type and description is unique and not empty.
516 sysmon_envsys_sensor_attach(struct sysmon_envsys
*sme
, envsys_data_t
*edata
)
518 const struct sme_description_table
*sdt_units
;
519 envsys_data_t
*oedata
;
522 KASSERT(sme
!= NULL
|| edata
!= NULL
);
525 * Find the correct units for this sensor.
527 sdt_units
= sme_get_description_table(SME_DESC_UNITS
);
528 for (i
= 0; sdt_units
[i
].type
!= -1; i
++)
529 if (sdt_units
[i
].type
== edata
->units
)
532 if (strcmp(sdt_units
[i
].desc
, "unknown") == 0)
536 * Check that description is not empty or duplicate.
538 if (strlen(edata
->desc
) == 0)
541 mutex_enter(&sme
->sme_mtx
);
542 sysmon_envsys_acquire(sme
, true);
543 TAILQ_FOREACH(oedata
, &sme
->sme_sensors_list
, sensors_head
) {
544 if (strcmp(oedata
->desc
, edata
->desc
) == 0) {
545 sysmon_envsys_release(sme
, true);
546 mutex_exit(&sme
->sme_mtx
);
551 * Ok, the sensor has been added into the device queue.
553 TAILQ_INSERT_TAIL(&sme
->sme_sensors_list
, edata
, sensors_head
);
556 * Give the sensor a index position.
558 edata
->sensor
= sme
->sme_nsensors
;
560 sysmon_envsys_release(sme
, true);
561 mutex_exit(&sme
->sme_mtx
);
563 DPRINTF(("%s: attached #%d (%s), units=%d (%s)\n",
564 __func__
, edata
->sensor
, edata
->desc
,
565 sdt_units
[i
].type
, sdt_units
[i
].desc
));
571 * sysmon_envsys_sensor_detach:
573 * + Detachs a sensor from a sysmon_envsys device and decrements the
574 * sensors count on success.
577 sysmon_envsys_sensor_detach(struct sysmon_envsys
*sme
, envsys_data_t
*edata
)
579 envsys_data_t
*oedata
;
582 KASSERT(sme
!= NULL
|| edata
!= NULL
);
585 * Check the sensor is already on the list.
587 mutex_enter(&sme
->sme_mtx
);
588 sysmon_envsys_acquire(sme
, true);
589 TAILQ_FOREACH(oedata
, &sme
->sme_sensors_list
, sensors_head
) {
590 if (oedata
->sensor
== edata
->sensor
) {
597 sysmon_envsys_release(sme
, true);
598 mutex_exit(&sme
->sme_mtx
);
603 * remove it and decrement the sensors count.
605 TAILQ_REMOVE(&sme
->sme_sensors_list
, edata
, sensors_head
);
607 sysmon_envsys_release(sme
, true);
608 mutex_exit(&sme
->sme_mtx
);
615 * sysmon_envsys_register:
617 * + Register a sysmon envsys device.
618 * + Create array of dictionaries for a device.
621 sysmon_envsys_register(struct sysmon_envsys
*sme
)
624 SLIST_ENTRY(sme_evdrv
) evdrv_head
;
625 sme_event_drv_t
*evdrv
;
627 SLIST_HEAD(, sme_evdrv
) sme_evdrv_list
;
628 struct sme_evdrv
*evdv
= NULL
;
629 struct sysmon_envsys
*lsme
;
630 prop_array_t array
= NULL
;
631 prop_dictionary_t dict
, dict2
;
632 envsys_data_t
*edata
= NULL
;
633 sme_event_drv_t
*this_evdrv
;
636 KASSERT(sme
!= NULL
);
637 KASSERT(sme
->sme_name
!= NULL
);
640 * Check if requested sysmon_envsys device is valid
641 * and does not exist already in the list.
643 mutex_enter(&sme_global_mtx
);
644 LIST_FOREACH(lsme
, &sysmon_envsys_list
, sme_list
) {
645 if (strcmp(lsme
->sme_name
, sme
->sme_name
) == 0) {
646 mutex_exit(&sme_global_mtx
);
650 mutex_exit(&sme_global_mtx
);
653 * sanity check: if SME_DISABLE_REFRESH is not set,
654 * the sme_refresh function callback must be non NULL.
656 if ((sme
->sme_flags
& SME_DISABLE_REFRESH
) == 0)
657 if (!sme
->sme_refresh
)
661 * If the list of sensors is empty, there's no point to continue...
663 if (TAILQ_EMPTY(&sme
->sme_sensors_list
)) {
664 DPRINTF(("%s: sensors list empty for %s\n", __func__
,
670 * Initialize the singly linked list for driver events.
672 SLIST_INIT(&sme_evdrv_list
);
674 array
= prop_array_create();
679 * Iterate over all sensors and create a dictionary per sensor.
680 * We must respect the order in which the sensors were added.
682 TAILQ_FOREACH(edata
, &sme
->sme_sensors_list
, sensors_head
) {
683 dict
= prop_dictionary_create();
690 * Create all objects in sensor's dictionary.
692 this_evdrv
= sme_add_sensor_dictionary(sme
, array
,
695 evdv
= kmem_zalloc(sizeof(*evdv
), KM_SLEEP
);
696 evdv
->evdrv
= this_evdrv
;
697 SLIST_INSERT_HEAD(&sme_evdrv_list
, evdv
, evdrv_head
);
702 * If the array does not contain any object (sensor), there's
703 * no need to attach the driver.
705 if (prop_array_count(array
) == 0) {
707 DPRINTF(("%s: empty array for '%s'\n", __func__
,
713 * Add the dictionary for the global properties of this device.
715 dict2
= prop_dictionary_create();
721 error
= sme_add_property_dictionary(sme
, array
, dict2
);
723 prop_object_release(dict2
);
728 * Add the array into the global dictionary for the driver.
735 mutex_enter(&sme_global_mtx
);
736 if (!prop_dictionary_set(sme_propd
, sme
->sme_name
, array
)) {
738 DPRINTF(("%s: prop_dictionary_set for '%s'\n", __func__
,
744 * Add the device into the list.
746 LIST_INSERT_HEAD(&sysmon_envsys_list
, sme
, sme_list
);
747 sme
->sme_fsensor
= sysmon_envsys_next_sensor_index
;
748 sysmon_envsys_next_sensor_index
+= sme
->sme_nsensors
;
749 mutex_exit(&sme_global_mtx
);
753 * No errors? register the events that were set in the driver
754 * and make an initial data refresh if was requested.
757 sysmon_task_queue_init();
758 SLIST_FOREACH(evdv
, &sme_evdrv_list
, evdrv_head
) {
759 sysmon_task_queue_sched(0,
760 sme_event_drvadd
, evdv
->evdrv
);
762 DPRINTF(("%s: driver '%s' registered (nsens=%d)\n",
763 __func__
, sme
->sme_name
, sme
->sme_nsensors
));
765 if (sme
->sme_flags
& SME_INIT_REFRESH
)
766 sysmon_task_queue_sched(0, sme_initial_refresh
, sme
);
770 while (!SLIST_EMPTY(&sme_evdrv_list
)) {
771 evdv
= SLIST_FIRST(&sme_evdrv_list
);
772 SLIST_REMOVE_HEAD(&sme_evdrv_list
, evdrv_head
);
773 kmem_free(evdv
, sizeof(*evdv
));
779 * Ugh... something wasn't right; unregister all events and sensors
780 * previously assigned and destroy the array with all its objects.
782 DPRINTF(("%s: failed to register '%s' (%d)\n", __func__
,
783 sme
->sme_name
, error
));
785 sme_event_unregister_all(sme
);
786 while (!TAILQ_EMPTY(&sme
->sme_sensors_list
)) {
787 edata
= TAILQ_FIRST(&sme
->sme_sensors_list
);
788 TAILQ_REMOVE(&sme
->sme_sensors_list
, edata
, sensors_head
);
790 sysmon_envsys_destroy_plist(array
);
795 * sysmon_envsys_destroy_plist:
797 * + Remove all objects from the array of dictionaries that is
798 * created in a sysmon envsys device.
801 sysmon_envsys_destroy_plist(prop_array_t array
)
803 prop_object_iterator_t iter
, iter2
;
804 prop_dictionary_t dict
;
807 KASSERT(array
!= NULL
);
808 KASSERT(prop_object_type(array
) == PROP_TYPE_ARRAY
);
810 DPRINTFOBJ(("%s: objects in array=%d\n", __func__
,
811 prop_array_count(array
)));
813 iter
= prop_array_iterator(array
);
817 while ((dict
= prop_object_iterator_next(iter
))) {
818 KASSERT(prop_object_type(dict
) == PROP_TYPE_DICTIONARY
);
819 iter2
= prop_dictionary_iterator(dict
);
822 DPRINTFOBJ(("%s: iterating over dictionary\n", __func__
));
823 while ((obj
= prop_object_iterator_next(iter2
)) != NULL
) {
824 DPRINTFOBJ(("%s: obj=%s\n", __func__
,
825 prop_dictionary_keysym_cstring_nocopy(obj
)));
826 prop_dictionary_remove(dict
,
827 prop_dictionary_keysym_cstring_nocopy(obj
));
828 prop_object_iterator_reset(iter2
);
830 prop_object_iterator_release(iter2
);
831 DPRINTFOBJ(("%s: objects in dictionary:%d\n",
832 __func__
, prop_dictionary_count(dict
)));
833 prop_object_release(dict
);
837 prop_object_iterator_release(iter
);
838 prop_object_release(array
);
842 * sysmon_envsys_unregister:
844 * + Unregister a sysmon envsys device.
847 sysmon_envsys_unregister(struct sysmon_envsys
*sme
)
851 KASSERT(sme
!= NULL
);
854 * Unregister all events associated with device.
856 sme_event_unregister_all(sme
);
858 * Decrement global sensors counter (only used for compatibility
859 * with previous API) and remove the device from the list.
861 mutex_enter(&sme_global_mtx
);
862 sysmon_envsys_next_sensor_index
-= sme
->sme_nsensors
;
863 LIST_REMOVE(sme
, sme_list
);
864 mutex_exit(&sme_global_mtx
);
867 * Remove the device (and all its objects) from the global dictionary.
869 array
= prop_dictionary_get(sme_propd
, sme
->sme_name
);
870 if (array
&& prop_object_type(array
) == PROP_TYPE_ARRAY
) {
871 mutex_enter(&sme_global_mtx
);
872 prop_dictionary_remove(sme_propd
, sme
->sme_name
);
873 mutex_exit(&sme_global_mtx
);
874 sysmon_envsys_destroy_plist(array
);
877 * And finally destroy the sysmon_envsys object.
879 sysmon_envsys_destroy(sme
);
883 * sysmon_envsys_find:
885 * + Find a sysmon envsys device and mark it as busy
886 * once it's available.
888 struct sysmon_envsys
*
889 sysmon_envsys_find(const char *name
)
891 struct sysmon_envsys
*sme
;
893 mutex_enter(&sme_global_mtx
);
894 LIST_FOREACH(sme
, &sysmon_envsys_list
, sme_list
) {
895 if (strcmp(sme
->sme_name
, name
) == 0) {
896 sysmon_envsys_acquire(sme
, false);
900 mutex_exit(&sme_global_mtx
);
906 * Compatibility function with the old API.
908 struct sysmon_envsys
*
909 sysmon_envsys_find_40(u_int idx
)
911 struct sysmon_envsys
*sme
;
913 mutex_enter(&sme_global_mtx
);
914 LIST_FOREACH(sme
, &sysmon_envsys_list
, sme_list
) {
915 if (idx
>= sme
->sme_fsensor
&&
916 idx
< (sme
->sme_fsensor
+ sme
->sme_nsensors
)) {
917 sysmon_envsys_acquire(sme
, false);
921 mutex_exit(&sme_global_mtx
);
927 * sysmon_envsys_acquire:
929 * + Wait until a sysmon envsys device is available and mark
933 sysmon_envsys_acquire(struct sysmon_envsys
*sme
, bool locked
)
935 KASSERT(sme
!= NULL
);
938 while (sme
->sme_flags
& SME_FLAG_BUSY
)
939 cv_wait(&sme
->sme_condvar
, &sme
->sme_mtx
);
940 sme
->sme_flags
|= SME_FLAG_BUSY
;
942 mutex_enter(&sme
->sme_mtx
);
943 while (sme
->sme_flags
& SME_FLAG_BUSY
)
944 cv_wait(&sme
->sme_condvar
, &sme
->sme_mtx
);
945 sme
->sme_flags
|= SME_FLAG_BUSY
;
946 mutex_exit(&sme
->sme_mtx
);
951 * sysmon_envsys_release:
953 * + Unmark a sysmon envsys device as busy, and notify
957 sysmon_envsys_release(struct sysmon_envsys
*sme
, bool locked
)
959 KASSERT(sme
!= NULL
);
962 sme
->sme_flags
&= ~SME_FLAG_BUSY
;
963 cv_broadcast(&sme
->sme_condvar
);
965 mutex_enter(&sme
->sme_mtx
);
966 sme
->sme_flags
&= ~SME_FLAG_BUSY
;
967 cv_broadcast(&sme
->sme_condvar
);
968 mutex_exit(&sme
->sme_mtx
);
973 * sme_initial_refresh:
975 * + Do an initial refresh of the sensors in a device just after
976 * interrupts are enabled in the autoconf(9) process.
980 sme_initial_refresh(void *arg
)
982 struct sysmon_envsys
*sme
= arg
;
983 envsys_data_t
*edata
;
985 mutex_enter(&sme
->sme_mtx
);
986 sysmon_envsys_acquire(sme
, true);
987 TAILQ_FOREACH(edata
, &sme
->sme_sensors_list
, sensors_head
)
988 (*sme
->sme_refresh
)(sme
, edata
);
989 sysmon_envsys_release(sme
, true);
990 mutex_exit(&sme
->sme_mtx
);
994 * sme_sensor_dictionary_get:
996 * + Returns a dictionary of a device specified by its index
1000 sme_sensor_dictionary_get(prop_array_t array
, const char *index
)
1002 prop_object_iterator_t iter
;
1003 prop_dictionary_t dict
;
1006 KASSERT(array
!= NULL
|| index
!= NULL
);
1008 iter
= prop_array_iterator(array
);
1012 while ((dict
= prop_object_iterator_next(iter
))) {
1013 obj
= prop_dictionary_get(dict
, "index");
1014 if (prop_string_equals_cstring(obj
, index
))
1018 prop_object_iterator_release(iter
);
1023 * sme_remove_userprops:
1025 * + Remove all properties from all devices that were set by
1026 * the ENVSYS_SETDICTIONARY ioctl.
1029 sme_remove_userprops(void)
1031 struct sysmon_envsys
*sme
;
1033 prop_dictionary_t sdict
;
1034 envsys_data_t
*edata
= NULL
;
1035 char tmp
[ENVSYS_DESCLEN
];
1038 mutex_enter(&sme_global_mtx
);
1039 LIST_FOREACH(sme
, &sysmon_envsys_list
, sme_list
) {
1040 sysmon_envsys_acquire(sme
, false);
1041 array
= prop_dictionary_get(sme_propd
, sme
->sme_name
);
1043 TAILQ_FOREACH(edata
, &sme
->sme_sensors_list
, sensors_head
) {
1044 (void)snprintf(tmp
, sizeof(tmp
), "sensor%d",
1046 sdict
= sme_sensor_dictionary_get(array
, tmp
);
1047 KASSERT(sdict
!= NULL
);
1050 if (edata
->upropset
& PROP_BATTCAP
) {
1051 prop_dictionary_remove(sdict
,
1052 "critical-capacity");
1053 ptype
= PENVSYS_EVENT_CAPACITY
;
1056 if (edata
->upropset
& PROP_BATTWARN
) {
1057 prop_dictionary_remove(sdict
,
1058 "warning-capacity");
1059 ptype
= PENVSYS_EVENT_CAPACITY
;
1062 sme_event_unregister(sme
, edata
->desc
, ptype
);
1065 if (edata
->upropset
& PROP_WARNMAX
) {
1066 prop_dictionary_remove(sdict
, "warning-max");
1067 ptype
= PENVSYS_EVENT_LIMITS
;
1070 if (edata
->upropset
& PROP_WARNMIN
) {
1071 prop_dictionary_remove(sdict
, "warning-min");
1072 ptype
= PENVSYS_EVENT_LIMITS
;
1075 if (edata
->upropset
& PROP_CRITMAX
) {
1076 prop_dictionary_remove(sdict
, "critical-max");
1077 ptype
= PENVSYS_EVENT_LIMITS
;
1080 if (edata
->upropset
& PROP_CRITMIN
) {
1081 prop_dictionary_remove(sdict
, "critical-min");
1082 ptype
= PENVSYS_EVENT_LIMITS
;
1085 sme_event_unregister(sme
, edata
->desc
, ptype
);
1087 if (edata
->upropset
& PROP_RFACT
) {
1088 (void)sme_sensor_upint32(sdict
, "rfact", 0);
1092 if (edata
->upropset
& PROP_DESC
)
1093 (void)sme_sensor_upstring(sdict
,
1094 "description", edata
->desc
);
1096 if (edata
->upropset
)
1097 edata
->upropset
= 0;
1101 * Restore default timeout value.
1103 sme
->sme_events_timeout
= SME_EVENTS_DEFTIMEOUT
;
1104 sysmon_envsys_release(sme
, false);
1106 mutex_exit(&sme_global_mtx
);
1110 * sme_add_property_dictionary:
1112 * + Add global properties into a device.
1115 sme_add_property_dictionary(struct sysmon_envsys
*sme
, prop_array_t array
,
1116 prop_dictionary_t dict
)
1118 prop_dictionary_t pdict
;
1121 pdict
= prop_dictionary_create();
1126 * Add the 'refresh-timeout' object into the 'device-properties'
1127 * dictionary. We use by default 30 seconds.
1131 * <key>device-properties</key>
1133 * <key>refresh-timeout</key>
1134 * <integer>120</integer<
1140 if (!sme
->sme_events_timeout
)
1141 sme
->sme_events_timeout
= SME_EVENTS_DEFTIMEOUT
;
1143 if (!prop_dictionary_set_uint64(pdict
, "refresh-timeout",
1144 sme
->sme_events_timeout
)) {
1149 if (!prop_dictionary_set(dict
, "device-properties", pdict
)) {
1155 * Add the device dictionary into the sysmon envsys array.
1157 if (!prop_array_add(array
, dict
))
1161 prop_object_release(pdict
);
1166 * sme_add_sensor_dictionary:
1168 * + Adds the sensor objects into the dictionary and returns a pointer
1169 * to a sme_event_drv_t object if a monitoring flag was set
1170 * (or NULL otherwise).
1173 sme_add_sensor_dictionary(struct sysmon_envsys
*sme
, prop_array_t array
,
1174 prop_dictionary_t dict
, envsys_data_t
*edata
)
1176 const struct sme_description_table
*sdt
, *sdt_units
;
1177 sme_event_drv_t
*sme_evdrv_t
= NULL
;
1179 char indexstr
[ENVSYS_DESCLEN
];
1182 * Find the correct units for this sensor.
1184 sdt_units
= sme_get_description_table(SME_DESC_UNITS
);
1185 for (i
= 0; sdt_units
[i
].type
!= -1; i
++)
1186 if (sdt_units
[i
].type
== edata
->units
)
1190 * Add the index sensor string.
1194 * <string>sensor0</string>
1197 (void)snprintf(indexstr
, sizeof(indexstr
), "sensor%d", edata
->sensor
);
1198 if (sme_sensor_upstring(dict
, "index", indexstr
))
1204 * <string>foo</string>
1205 * <key>description</key>
1206 * <string>blah blah</string>
1209 if (sme_sensor_upstring(dict
, "type", sdt_units
[i
].desc
))
1212 if (sme_sensor_upstring(dict
, "description", edata
->desc
))
1216 * Add sensor's state description.
1220 * <string>valid</string>
1223 sdt
= sme_get_description_table(SME_DESC_STATES
);
1224 for (j
= 0; sdt
[j
].type
!= -1; j
++)
1225 if (sdt
[j
].type
== edata
->state
)
1228 DPRINTF(("%s: sensor desc=%s type=%d state=%d\n",
1229 __func__
, edata
->desc
, edata
->units
, edata
->state
));
1231 if (sme_sensor_upstring(dict
, "state", sdt
[j
].desc
))
1235 * Add the monitoring boolean object:
1238 * <key>monitoring-supported</key>
1242 * always false on Battery {capacity,charge}, Drive and Indicator types.
1243 * They cannot be monitored.
1246 if ((edata
->flags
& ENVSYS_FMONNOTSUPP
) ||
1247 (edata
->units
== ENVSYS_INDICATOR
) ||
1248 (edata
->units
== ENVSYS_DRIVE
) ||
1249 (edata
->units
== ENVSYS_BATTERY_CAPACITY
) ||
1250 (edata
->units
== ENVSYS_BATTERY_CHARGE
)) {
1251 if (sme_sensor_upbool(dict
, "monitoring-supported", false))
1254 if (sme_sensor_upbool(dict
, "monitoring-supported", true))
1259 * Add the percentage boolean object, true if ENVSYS_FPERCENT
1260 * is set or false otherwise.
1263 * <key>want-percentage</key>
1267 if (edata
->flags
& ENVSYS_FPERCENT
)
1268 if (sme_sensor_upbool(dict
, "want-percentage", true))
1272 * Add the allow-rfact boolean object, true if
1273 * ENVSYS_FCHANGERFACT if set or false otherwise.
1276 * <key>allow-rfact</key>
1280 if (edata
->units
== ENVSYS_SVOLTS_DC
||
1281 edata
->units
== ENVSYS_SVOLTS_AC
) {
1282 if (edata
->flags
& ENVSYS_FCHANGERFACT
) {
1283 if (sme_sensor_upbool(dict
, "allow-rfact", true))
1286 if (sme_sensor_upbool(dict
, "allow-rfact", false))
1292 * Add the object for battery capacity sensors:
1295 * <key>battery-capacity</key>
1296 * <string>NORMAL</string>
1299 if (edata
->units
== ENVSYS_BATTERY_CAPACITY
) {
1300 sdt
= sme_get_description_table(SME_DESC_BATTERY_CAPACITY
);
1301 for (j
= 0; sdt
[j
].type
!= -1; j
++)
1302 if (sdt
[j
].type
== edata
->value_cur
)
1305 if (sme_sensor_upstring(dict
, "battery-capacity", sdt
[j
].desc
))
1310 * Add the drive-state object for drive sensors:
1313 * <key>drive-state</key>
1314 * <string>drive is online</string>
1317 if (edata
->units
== ENVSYS_DRIVE
) {
1318 sdt
= sme_get_description_table(SME_DESC_DRIVE_STATES
);
1319 for (j
= 0; sdt
[j
].type
!= -1; j
++)
1320 if (sdt
[j
].type
== edata
->value_cur
)
1323 if (sme_sensor_upstring(dict
, "drive-state", sdt
[j
].desc
))
1328 * Add the following objects if sensor is enabled...
1330 if (edata
->state
== ENVSYS_SVALID
) {
1332 * Add the following objects:
1336 * <integer>2500</integer>
1338 * <integer>10000</integer>
1339 * <key>cur-value</key>
1340 * <integer>1250</integer>
1341 * <key>min-value</key>
1342 * <integer>800</integer>
1343 * <key>max-value</integer>
1344 * <integer>3000</integer>
1345 * <key>avg-value</integer>
1346 * <integer>1400</integer>
1349 if (edata
->units
== ENVSYS_SFANRPM
)
1350 if (sme_sensor_upuint32(dict
, "rpms", edata
->rpms
))
1353 if (edata
->units
== ENVSYS_SVOLTS_AC
||
1354 edata
->units
== ENVSYS_SVOLTS_DC
)
1355 if (sme_sensor_upint32(dict
, "rfact", edata
->rfact
))
1358 if (sme_sensor_upint32(dict
, "cur-value", edata
->value_cur
))
1361 if (edata
->flags
& ENVSYS_FVALID_MIN
) {
1362 if (sme_sensor_upint32(dict
,
1368 if (edata
->flags
& ENVSYS_FVALID_MAX
) {
1369 if (sme_sensor_upint32(dict
,
1375 if (edata
->flags
& ENVSYS_FVALID_AVG
) {
1376 if (sme_sensor_upint32(dict
,
1387 * Add the dictionary into the array.
1390 if (!prop_array_add(array
, dict
)) {
1391 DPRINTF(("%s: prop_array_add\n", __func__
));
1396 * Register a new event if a monitoring flag was set.
1398 if (edata
->monitor
) {
1399 sme_evdrv_t
= kmem_zalloc(sizeof(*sme_evdrv_t
), KM_SLEEP
);
1400 sme_evdrv_t
->sed_sdict
= dict
;
1401 sme_evdrv_t
->sed_edata
= edata
;
1402 sme_evdrv_t
->sed_sme
= sme
;
1403 sme_evdrv_t
->sed_powertype
= sdt_units
[i
].crittype
;
1410 prop_object_release(dict
);
1415 * sme_update_dictionary:
1417 * + Update per-sensor dictionaries with new values if there were
1418 * changes, otherwise the object in dictionary is untouched.
1421 sme_update_dictionary(struct sysmon_envsys
*sme
)
1423 const struct sme_description_table
*sdt
;
1424 envsys_data_t
*edata
;
1425 prop_object_t array
, dict
, obj
, obj2
;
1429 * Retrieve the array of dictionaries in device.
1431 array
= prop_dictionary_get(sme_propd
, sme
->sme_name
);
1432 if (prop_object_type(array
) != PROP_TYPE_ARRAY
) {
1433 DPRINTF(("%s: not an array (%s)\n", __func__
, sme
->sme_name
));
1438 * Get the last dictionary on the array, this contains the
1439 * 'device-properties' sub-dictionary.
1441 obj
= prop_array_get(array
, prop_array_count(array
) - 1);
1442 if (!obj
|| prop_object_type(obj
) != PROP_TYPE_DICTIONARY
) {
1443 DPRINTF(("%s: not a device-properties dictionary\n", __func__
));
1447 obj2
= prop_dictionary_get(obj
, "device-properties");
1452 * Update the 'refresh-timeout' property.
1454 if (!prop_dictionary_set_uint64(obj2
, "refresh-timeout",
1455 sme
->sme_events_timeout
))
1459 * - iterate over all sensors.
1461 * - check if data in dictionary is different than new data.
1462 * - update dictionary if there were changes.
1464 DPRINTF(("%s: updating '%s' with nsensors=%d\n", __func__
,
1465 sme
->sme_name
, sme
->sme_nsensors
));
1468 * Don't bother with locking when traversing the queue,
1469 * the device is already marked as busy; if a sensor
1470 * is going to be removed or added it will have to wait.
1472 TAILQ_FOREACH(edata
, &sme
->sme_sensors_list
, sensors_head
) {
1474 * refresh sensor data via sme_refresh only if the
1477 if ((sme
->sme_flags
& SME_DISABLE_REFRESH
) == 0) {
1478 mutex_enter(&sme
->sme_mtx
);
1479 (*sme
->sme_refresh
)(sme
, edata
);
1480 mutex_exit(&sme
->sme_mtx
);
1484 * retrieve sensor's dictionary.
1486 dict
= prop_array_get(array
, edata
->sensor
);
1487 if (prop_object_type(dict
) != PROP_TYPE_DICTIONARY
) {
1488 DPRINTF(("%s: not a dictionary (%d:%s)\n",
1489 __func__
, edata
->sensor
, sme
->sme_name
));
1494 * update sensor's state.
1496 sdt
= sme_get_description_table(SME_DESC_STATES
);
1497 for (j
= 0; sdt
[j
].type
!= -1; j
++)
1498 if (sdt
[j
].type
== edata
->state
)
1501 DPRINTFOBJ(("%s: sensor #%d type=%d (%s) flags=%d\n",
1502 __func__
, edata
->sensor
, sdt
[j
].type
, sdt
[j
].desc
,
1505 error
= sme_sensor_upstring(dict
, "state", sdt
[j
].desc
);
1510 * update sensor's type.
1512 sdt
= sme_get_description_table(SME_DESC_UNITS
);
1513 for (j
= 0; sdt
[j
].type
!= -1; j
++)
1514 if (sdt
[j
].type
== edata
->units
)
1517 DPRINTFOBJ(("%s: sensor #%d units=%d (%s)\n",
1518 __func__
, edata
->sensor
, sdt
[j
].type
, sdt
[j
].desc
));
1520 error
= sme_sensor_upstring(dict
, "type", sdt
[j
].desc
);
1525 * update sensor's current value.
1527 error
= sme_sensor_upint32(dict
,
1534 * Battery charge, Integer and Indicator types do not
1535 * need the following objects, so skip them.
1537 if (edata
->units
== ENVSYS_INTEGER
||
1538 edata
->units
== ENVSYS_INDICATOR
||
1539 edata
->units
== ENVSYS_BATTERY_CHARGE
)
1543 * update sensor flags.
1545 if (edata
->flags
& ENVSYS_FPERCENT
) {
1546 error
= sme_sensor_upbool(dict
,
1554 * update sensor's {avg,max,min}-value.
1556 if (edata
->flags
& ENVSYS_FVALID_MAX
) {
1557 error
= sme_sensor_upint32(dict
,
1564 if (edata
->flags
& ENVSYS_FVALID_MIN
) {
1565 error
= sme_sensor_upint32(dict
,
1572 if (edata
->flags
& ENVSYS_FVALID_AVG
) {
1573 error
= sme_sensor_upint32(dict
,
1581 * update 'rpms' only for ENVSYS_SFANRPM sensors.
1583 if (edata
->units
== ENVSYS_SFANRPM
) {
1584 error
= sme_sensor_upuint32(dict
,
1592 * update 'rfact' only for ENVSYS_SVOLTS_[AD]C sensors.
1594 if (edata
->units
== ENVSYS_SVOLTS_AC
||
1595 edata
->units
== ENVSYS_SVOLTS_DC
) {
1596 error
= sme_sensor_upint32(dict
,
1604 * update 'drive-state' only for ENVSYS_DRIVE sensors.
1606 if (edata
->units
== ENVSYS_DRIVE
) {
1607 sdt
= sme_get_description_table(SME_DESC_DRIVE_STATES
);
1608 for (j
= 0; sdt
[j
].type
!= -1; j
++)
1609 if (sdt
[j
].type
== edata
->value_cur
)
1612 error
= sme_sensor_upstring(dict
,
1620 * update 'battery-capacity' only for ENVSYS_BATTERY_CAPACITY
1623 if (edata
->units
== ENVSYS_BATTERY_CAPACITY
) {
1625 sme_get_description_table(SME_DESC_BATTERY_CAPACITY
);
1626 for (j
= 0; sdt
[j
].type
!= -1; j
++)
1627 if (sdt
[j
].type
== edata
->value_cur
)
1630 error
= sme_sensor_upstring(dict
,
1642 * sme_userset_dictionary:
1644 * + Parse the userland dictionary and run the appropiate tasks
1645 * that were specified.
1648 sme_userset_dictionary(struct sysmon_envsys
*sme
, prop_dictionary_t udict
,
1651 const struct sme_description_table
*sdt
;
1652 envsys_data_t
*edata
;
1653 prop_dictionary_t dict
, tdict
= NULL
;
1654 prop_object_t obj
, obj1
, obj2
, tobj
= NULL
;
1655 uint64_t refresh_timo
= 0;
1656 sysmon_envsys_lim_t lims
;
1659 bool targetfound
= false;
1662 * The user wanted to change the refresh timeout value for this
1665 * Get the 'device-properties' object from the userland dictionary.
1667 obj
= prop_dictionary_get(udict
, "device-properties");
1668 if (obj
&& prop_object_type(obj
) == PROP_TYPE_DICTIONARY
) {
1670 * Get the 'refresh-timeout' property for this device.
1672 obj1
= prop_dictionary_get(obj
, "refresh-timeout");
1673 if (obj1
&& prop_object_type(obj1
) == PROP_TYPE_NUMBER
) {
1676 prop_number_unsigned_integer_value(obj1
);
1677 if (refresh_timo
< 1)
1680 mutex_enter(&sme
->sme_mtx
);
1681 sme
->sme_events_timeout
= refresh_timo
;
1682 mutex_exit(&sme
->sme_mtx
);
1689 * Get sensor's index from userland dictionary.
1691 obj
= prop_dictionary_get(udict
, "index");
1694 if (prop_object_type(obj
) != PROP_TYPE_STRING
) {
1695 DPRINTF(("%s: 'index' not a string\n", __func__
));
1702 * Don't bother with locking when traversing the queue,
1703 * the device is already marked as busy; if a sensor
1704 * is going to be removed or added it will have to wait.
1706 TAILQ_FOREACH(edata
, &sme
->sme_sensors_list
, sensors_head
) {
1708 * Get a dictionary and check if it's our sensor by checking
1709 * at its index position.
1711 dict
= prop_array_get(array
, edata
->sensor
);
1712 obj1
= prop_dictionary_get(dict
, "index");
1717 if (!prop_string_equals(obj1
, obj
))
1723 * Check if a new description operation was
1724 * requested by the user and set new description.
1726 obj2
= prop_dictionary_get(udict
, "description");
1727 if (obj2
&& prop_object_type(obj2
) == PROP_TYPE_STRING
) {
1729 blah
= prop_string_cstring_nocopy(obj2
);
1732 * Check for duplicate description.
1734 for (i
= 0; i
< sme
->sme_nsensors
; i
++) {
1735 if (i
== edata
->sensor
)
1737 tdict
= prop_array_get(array
, i
);
1739 prop_dictionary_get(tdict
, "description");
1740 if (prop_string_equals(obj2
, tobj
)) {
1747 * Update the object in dictionary.
1749 mutex_enter(&sme
->sme_mtx
);
1750 error
= sme_sensor_upstring(dict
,
1754 mutex_exit(&sme
->sme_mtx
);
1758 DPRINTF(("%s: sensor%d changed desc to: %s\n",
1759 __func__
, edata
->sensor
, blah
));
1760 edata
->upropset
|= PROP_DESC
;
1761 mutex_exit(&sme
->sme_mtx
);
1765 * did the user want to change the rfact?
1767 obj2
= prop_dictionary_get(udict
, "rfact");
1768 if (obj2
&& prop_object_type(obj2
) == PROP_TYPE_NUMBER
) {
1770 if (edata
->flags
& ENVSYS_FCHANGERFACT
) {
1771 mutex_enter(&sme
->sme_mtx
);
1772 edata
->rfact
= prop_number_integer_value(obj2
);
1773 edata
->upropset
|= PROP_RFACT
;
1774 mutex_exit(&sme
->sme_mtx
);
1775 DPRINTF(("%s: sensor%d changed rfact to %d\n",
1776 __func__
, edata
->sensor
, edata
->rfact
));
1783 sdt
= sme_get_description_table(SME_DESC_UNITS
);
1784 for (i
= 0; sdt
[i
].type
!= -1; i
++)
1785 if (sdt
[i
].type
== edata
->units
)
1789 * did the user want to set a critical capacity event?
1791 obj2
= prop_dictionary_get(udict
, "critical-capacity");
1792 if (obj2
&& prop_object_type(obj2
) == PROP_TYPE_NUMBER
) {
1794 if ((edata
->flags
& ENVSYS_FMONNOTSUPP
) ||
1795 (edata
->flags
& ENVSYS_FPERCENT
) == 0) {
1800 lims
.sel_critmin
= prop_number_integer_value(obj2
);
1801 lims
.sel_flags
|= PROP_BATTCAP
;
1805 * did the user want to set a warning capacity event?
1807 obj2
= prop_dictionary_get(udict
, "warning-capacity");
1808 if (obj2
&& prop_object_type(obj2
) == PROP_TYPE_NUMBER
) {
1810 if ((edata
->flags
& ENVSYS_FMONNOTSUPP
) ||
1811 (edata
->flags
& ENVSYS_FPERCENT
) == 0) {
1816 lims
.sel_warnmin
= prop_number_integer_value(obj2
);
1817 lims
.sel_flags
|= PROP_BATTWARN
;
1821 * did the user want to set a critical max event?
1823 obj2
= prop_dictionary_get(udict
, "critical-max");
1824 if (obj2
&& prop_object_type(obj2
) == PROP_TYPE_NUMBER
) {
1826 if (edata
->units
== ENVSYS_INDICATOR
||
1828 (ENVSYS_FPERCENT
| ENVSYS_FMONNOTSUPP
)) {
1833 lims
.sel_critmax
= prop_number_integer_value(obj2
);
1834 lims
.sel_flags
|= PROP_CRITMAX
;
1838 * did the user want to set a warning max event?
1840 obj2
= prop_dictionary_get(udict
, "warning-max");
1841 if (obj2
&& prop_object_type(obj2
) == PROP_TYPE_NUMBER
) {
1843 if (edata
->units
== ENVSYS_INDICATOR
||
1845 (ENVSYS_FPERCENT
| ENVSYS_FMONNOTSUPP
)) {
1850 lims
.sel_warnmax
= prop_number_integer_value(obj2
);
1851 lims
.sel_flags
|= PROP_WARNMAX
;
1855 * did the user want to set a critical min event?
1857 obj2
= prop_dictionary_get(udict
, "critical-min");
1858 if (obj2
&& prop_object_type(obj2
) == PROP_TYPE_NUMBER
) {
1860 if (edata
->units
== ENVSYS_INDICATOR
||
1862 (ENVSYS_FPERCENT
| ENVSYS_FMONNOTSUPP
)) {
1867 lims
.sel_critmin
= prop_number_integer_value(obj2
);
1868 lims
.sel_flags
|= PROP_CRITMIN
;
1872 * did the user want to set a warning min event?
1874 obj2
= prop_dictionary_get(udict
, "warning-min");
1875 if (obj2
&& prop_object_type(obj2
) == PROP_TYPE_NUMBER
) {
1877 if (edata
->units
== ENVSYS_INDICATOR
||
1879 (ENVSYS_FPERCENT
| ENVSYS_FMONNOTSUPP
)) {
1884 lims
.sel_warnmin
= prop_number_integer_value(obj2
);
1885 lims
.sel_flags
|= PROP_WARNMIN
;
1888 if (lims
.sel_flags
) {
1889 error
= sme_event_register(dict
, edata
, sme
, &lims
,
1890 (edata
->flags
& ENVSYS_FPERCENT
)?
1891 PENVSYS_EVENT_CAPACITY
:
1892 PENVSYS_EVENT_LIMITS
,
1894 if (error
== EEXIST
)
1899 mutex_enter(&sme
->sme_mtx
);
1900 edata
->upropset
|= lims
.sel_flags
;
1901 mutex_exit(&sme
->sme_mtx
);
1905 * All objects in dictionary were processed.
1912 * invalid target? return the error.