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