]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOSimpleReporter.cpp
xnu-6153.141.1.tar.gz
[apple/xnu.git] / iokit / Kernel / IOSimpleReporter.cpp
1 /*
2 * Copyright (c) 2012-2013 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <IOKit/IOKernelReportStructs.h>
30 #include <IOKit/IOKernelReporters.h>
31 #include "IOReporterDefs.h"
32
33 #define super IOReporter
34 OSDefineMetaClassAndStructors(IOSimpleReporter, IOReporter);
35
36 /* static */
37 IOSimpleReporter*
38 IOSimpleReporter::with(IOService *reportingService,
39 IOReportCategories categories,
40 IOReportUnit unit)
41 {
42 IOSimpleReporter *reporter, *rval = NULL;
43
44 // kprintf("%s\n", __func__); // can't IORLOG() from static
45
46 reporter = new IOSimpleReporter;
47 if (!reporter) {
48 goto finish;
49 }
50
51
52 if (!reporter->initWith(reportingService, categories, unit)) {
53 goto finish;
54 }
55
56 // success
57 rval = reporter;
58
59 finish:
60 if (!rval) {
61 OSSafeReleaseNULL(reporter);
62 }
63
64 return rval;
65 }
66
67 bool
68 IOSimpleReporter::initWith(IOService *reportingService,
69 IOReportCategories categories,
70 IOReportUnit unit)
71 {
72 // fully specify the channel type for the superclass
73 IOReportChannelType channelType = {
74 .categories = categories,
75 .report_format = kIOReportFormatSimple,
76 .nelements = 1,
77 .element_idx = 0
78 };
79
80 return super::init(reportingService, channelType, unit);
81 }
82
83
84 IOReturn
85 IOSimpleReporter::setValue(uint64_t channel_id,
86 int64_t value)
87 {
88 IOReturn res = kIOReturnError;
89 IOSimpleReportValues simple_values;
90 int element_index = 0;
91
92 lockReporter();
93
94 if (getFirstElementIndex(channel_id, &element_index) != kIOReturnSuccess) {
95 res = kIOReturnBadArgument;
96 goto finish;
97 }
98
99
100 if (copyElementValues(element_index, (IOReportElementValues *)&simple_values) != kIOReturnSuccess) {
101 res = kIOReturnBadArgument;
102 goto finish;
103 }
104
105 simple_values.simple_value = value;
106 res = setElementValues(element_index, (IOReportElementValues *)&simple_values);
107
108 finish:
109 unlockReporter();
110 return res;
111 }
112
113
114 IOReturn
115 IOSimpleReporter::incrementValue(uint64_t channel_id,
116 int64_t increment)
117 {
118 IOReturn res = kIOReturnError;
119 IOSimpleReportValues simple_values;
120 int element_index = 0;
121
122 lockReporter();
123
124 if (getFirstElementIndex(channel_id, &element_index) != kIOReturnSuccess) {
125 res = kIOReturnBadArgument;
126 goto finish;
127 }
128
129 if (copyElementValues(element_index, (IOReportElementValues *)&simple_values) != kIOReturnSuccess) {
130 res = kIOReturnBadArgument;
131 goto finish;
132 }
133
134 simple_values.simple_value += increment;
135
136 res = setElementValues(element_index, (IOReportElementValues *)&simple_values);
137
138 finish:
139 unlockReporter();
140 return res;
141 }
142
143 int64_t
144 IOSimpleReporter::getValue(uint64_t channel_id)
145 {
146 IOSimpleReportValues *values = NULL;
147 int64_t simple_value = (int64_t)kIOReportInvalidValue;
148 int index = 0;
149
150 lockReporter();
151
152 if (getFirstElementIndex(channel_id, &index) == kIOReturnSuccess) {
153 values = (IOSimpleReportValues *)getElementValues(index);
154
155 if (values != NULL) {
156 simple_value = values->simple_value;
157 }
158 }
159
160 unlockReporter();
161 return simple_value;
162 }