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