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