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