1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions * Copyright (c) 2012-2013 Apple Computer, Inc. All Rights Reserved.
3*bbb1b6f9SApple OSS Distributions *
4*bbb1b6f9SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions *
6*bbb1b6f9SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions *
15*bbb1b6f9SApple OSS Distributions * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions *
18*bbb1b6f9SApple OSS Distributions * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions * limitations under the License.
25*bbb1b6f9SApple OSS Distributions *
26*bbb1b6f9SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions */
28*bbb1b6f9SApple OSS Distributions
29*bbb1b6f9SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
30*bbb1b6f9SApple OSS Distributions
31*bbb1b6f9SApple OSS Distributions #include <IOKit/IOKernelReportStructs.h>
32*bbb1b6f9SApple OSS Distributions #include <IOKit/IOKernelReporters.h>
33*bbb1b6f9SApple OSS Distributions
34*bbb1b6f9SApple OSS Distributions
35*bbb1b6f9SApple OSS Distributions //#define IORDEBUG_LEGEND 1
36*bbb1b6f9SApple OSS Distributions
37*bbb1b6f9SApple OSS Distributions #ifdef IORDEBUG_LEGEND
38*bbb1b6f9SApple OSS Distributions #define IORLEGENDLOG(fmt, args...) \
39*bbb1b6f9SApple OSS Distributions do { \
40*bbb1b6f9SApple OSS Distributions IOLog("IOReportLegend | "); \
41*bbb1b6f9SApple OSS Distributions IOLog(fmt, ##args); \
42*bbb1b6f9SApple OSS Distributions IOLog("\n"); \
43*bbb1b6f9SApple OSS Distributions } while(0)
44*bbb1b6f9SApple OSS Distributions #else
45*bbb1b6f9SApple OSS Distributions #define IORLEGENDLOG(fmt, args...)
46*bbb1b6f9SApple OSS Distributions #endif
47*bbb1b6f9SApple OSS Distributions
48*bbb1b6f9SApple OSS Distributions
49*bbb1b6f9SApple OSS Distributions #define super OSObject
50*bbb1b6f9SApple OSS Distributions OSDefineMetaClassAndStructors(IOReportLegend, OSObject);
51*bbb1b6f9SApple OSS Distributions
52*bbb1b6f9SApple OSS Distributions OSSharedPtr<IOReportLegend>
with(OSArray * legend)53*bbb1b6f9SApple OSS Distributions IOReportLegend::with(OSArray *legend)
54*bbb1b6f9SApple OSS Distributions {
55*bbb1b6f9SApple OSS Distributions OSSharedPtr<IOReportLegend> iorLegend = OSMakeShared<IOReportLegend>();
56*bbb1b6f9SApple OSS Distributions
57*bbb1b6f9SApple OSS Distributions if (iorLegend) {
58*bbb1b6f9SApple OSS Distributions if (legend != NULL) {
59*bbb1b6f9SApple OSS Distributions if (iorLegend->initWith(legend) != kIOReturnSuccess) {
60*bbb1b6f9SApple OSS Distributions return nullptr;
61*bbb1b6f9SApple OSS Distributions }
62*bbb1b6f9SApple OSS Distributions }
63*bbb1b6f9SApple OSS Distributions
64*bbb1b6f9SApple OSS Distributions return iorLegend;
65*bbb1b6f9SApple OSS Distributions } else {
66*bbb1b6f9SApple OSS Distributions return nullptr;
67*bbb1b6f9SApple OSS Distributions }
68*bbb1b6f9SApple OSS Distributions }
69*bbb1b6f9SApple OSS Distributions
70*bbb1b6f9SApple OSS Distributions /* must clean up everything if it fails */
71*bbb1b6f9SApple OSS Distributions IOReturn
initWith(OSArray * legend)72*bbb1b6f9SApple OSS Distributions IOReportLegend::initWith(OSArray *legend)
73*bbb1b6f9SApple OSS Distributions {
74*bbb1b6f9SApple OSS Distributions if (legend) {
75*bbb1b6f9SApple OSS Distributions _reportLegend = OSArray::withArray(legend);
76*bbb1b6f9SApple OSS Distributions }
77*bbb1b6f9SApple OSS Distributions
78*bbb1b6f9SApple OSS Distributions if (_reportLegend == NULL) {
79*bbb1b6f9SApple OSS Distributions return kIOReturnError;
80*bbb1b6f9SApple OSS Distributions } else {
81*bbb1b6f9SApple OSS Distributions return kIOReturnSuccess;
82*bbb1b6f9SApple OSS Distributions }
83*bbb1b6f9SApple OSS Distributions }
84*bbb1b6f9SApple OSS Distributions
85*bbb1b6f9SApple OSS Distributions
86*bbb1b6f9SApple OSS Distributions void
free(void)87*bbb1b6f9SApple OSS Distributions IOReportLegend::free(void)
88*bbb1b6f9SApple OSS Distributions {
89*bbb1b6f9SApple OSS Distributions super::free();
90*bbb1b6f9SApple OSS Distributions }
91*bbb1b6f9SApple OSS Distributions
92*bbb1b6f9SApple OSS Distributions
93*bbb1b6f9SApple OSS Distributions OSArray*
getLegend(void)94*bbb1b6f9SApple OSS Distributions IOReportLegend::getLegend(void)
95*bbb1b6f9SApple OSS Distributions {
96*bbb1b6f9SApple OSS Distributions return _reportLegend.get();
97*bbb1b6f9SApple OSS Distributions }
98*bbb1b6f9SApple OSS Distributions
99*bbb1b6f9SApple OSS Distributions IOReturn
addReporterLegend(IOService * reportingService,IOReporter * reporter,const char * groupName,const char * subGroupName)100*bbb1b6f9SApple OSS Distributions IOReportLegend::addReporterLegend(IOService *reportingService,
101*bbb1b6f9SApple OSS Distributions IOReporter *reporter,
102*bbb1b6f9SApple OSS Distributions const char *groupName,
103*bbb1b6f9SApple OSS Distributions const char *subGroupName)
104*bbb1b6f9SApple OSS Distributions {
105*bbb1b6f9SApple OSS Distributions IOReturn res = kIOReturnError;
106*bbb1b6f9SApple OSS Distributions OSSharedPtr<IOReportLegend> legend;
107*bbb1b6f9SApple OSS Distributions OSSharedPtr<OSObject> curLegend;
108*bbb1b6f9SApple OSS Distributions
109*bbb1b6f9SApple OSS Distributions // No need to check groupName and subGroupName because optional params
110*bbb1b6f9SApple OSS Distributions if (!reportingService || !reporter) {
111*bbb1b6f9SApple OSS Distributions goto finish;
112*bbb1b6f9SApple OSS Distributions }
113*bbb1b6f9SApple OSS Distributions
114*bbb1b6f9SApple OSS Distributions // It's fine if the legend doesn't exist (IOReportLegend::with(NULL)
115*bbb1b6f9SApple OSS Distributions // is how you make an empty legend). If it's not an array, then
116*bbb1b6f9SApple OSS Distributions // we're just going to replace it.
117*bbb1b6f9SApple OSS Distributions curLegend = reportingService->copyProperty(kIOReportLegendKey);
118*bbb1b6f9SApple OSS Distributions legend = IOReportLegend::with(OSDynamicCast(OSArray, curLegend.get()));
119*bbb1b6f9SApple OSS Distributions if (!legend) {
120*bbb1b6f9SApple OSS Distributions goto finish;
121*bbb1b6f9SApple OSS Distributions }
122*bbb1b6f9SApple OSS Distributions
123*bbb1b6f9SApple OSS Distributions // Add the reporter's entries and update the service property.
124*bbb1b6f9SApple OSS Distributions // The overwrite triggers a release of the old legend array.
125*bbb1b6f9SApple OSS Distributions legend->addReporterLegend(reporter, groupName, subGroupName);
126*bbb1b6f9SApple OSS Distributions reportingService->setProperty(kIOReportLegendKey, legend->getLegend());
127*bbb1b6f9SApple OSS Distributions reportingService->setProperty(kIOReportLegendPublicKey, true);
128*bbb1b6f9SApple OSS Distributions
129*bbb1b6f9SApple OSS Distributions res = kIOReturnSuccess;
130*bbb1b6f9SApple OSS Distributions
131*bbb1b6f9SApple OSS Distributions finish:
132*bbb1b6f9SApple OSS Distributions return res;
133*bbb1b6f9SApple OSS Distributions }
134*bbb1b6f9SApple OSS Distributions
135*bbb1b6f9SApple OSS Distributions
136*bbb1b6f9SApple OSS Distributions IOReturn
addLegendEntry(IOReportLegendEntry * legendEntry,const char * groupName,const char * subGroupName)137*bbb1b6f9SApple OSS Distributions IOReportLegend::addLegendEntry(IOReportLegendEntry *legendEntry,
138*bbb1b6f9SApple OSS Distributions const char *groupName,
139*bbb1b6f9SApple OSS Distributions const char *subGroupName)
140*bbb1b6f9SApple OSS Distributions {
141*bbb1b6f9SApple OSS Distributions kern_return_t res = kIOReturnError;
142*bbb1b6f9SApple OSS Distributions OSSharedPtr<const OSSymbol> tmpGroupName;
143*bbb1b6f9SApple OSS Distributions OSSharedPtr<const OSSymbol> tmpSubGroupName;
144*bbb1b6f9SApple OSS Distributions
145*bbb1b6f9SApple OSS Distributions if (!legendEntry) {
146*bbb1b6f9SApple OSS Distributions return res;
147*bbb1b6f9SApple OSS Distributions }
148*bbb1b6f9SApple OSS Distributions
149*bbb1b6f9SApple OSS Distributions if (groupName) {
150*bbb1b6f9SApple OSS Distributions tmpGroupName = OSSymbol::withCString(groupName);
151*bbb1b6f9SApple OSS Distributions }
152*bbb1b6f9SApple OSS Distributions
153*bbb1b6f9SApple OSS Distributions if (subGroupName) {
154*bbb1b6f9SApple OSS Distributions tmpSubGroupName = OSSymbol::withCString(subGroupName);
155*bbb1b6f9SApple OSS Distributions }
156*bbb1b6f9SApple OSS Distributions
157*bbb1b6f9SApple OSS Distributions // It is ok to call appendLegendWith() if tmpGroups are NULL
158*bbb1b6f9SApple OSS Distributions res = organizeLegend(legendEntry, tmpGroupName.get(), tmpSubGroupName.get());
159*bbb1b6f9SApple OSS Distributions
160*bbb1b6f9SApple OSS Distributions return res;
161*bbb1b6f9SApple OSS Distributions }
162*bbb1b6f9SApple OSS Distributions
163*bbb1b6f9SApple OSS Distributions
164*bbb1b6f9SApple OSS Distributions IOReturn
addReporterLegend(IOReporter * reporter,const char * groupName,const char * subGroupName)165*bbb1b6f9SApple OSS Distributions IOReportLegend::addReporterLegend(IOReporter *reporter,
166*bbb1b6f9SApple OSS Distributions const char *groupName,
167*bbb1b6f9SApple OSS Distributions const char *subGroupName)
168*bbb1b6f9SApple OSS Distributions {
169*bbb1b6f9SApple OSS Distributions IOReturn res = kIOReturnError;
170*bbb1b6f9SApple OSS Distributions OSSharedPtr<IOReportLegendEntry> legendEntry;
171*bbb1b6f9SApple OSS Distributions
172*bbb1b6f9SApple OSS Distributions if (reporter) {
173*bbb1b6f9SApple OSS Distributions legendEntry = reporter->createLegend();
174*bbb1b6f9SApple OSS Distributions
175*bbb1b6f9SApple OSS Distributions if (legendEntry) {
176*bbb1b6f9SApple OSS Distributions res = addLegendEntry(legendEntry.get(), groupName, subGroupName);
177*bbb1b6f9SApple OSS Distributions }
178*bbb1b6f9SApple OSS Distributions }
179*bbb1b6f9SApple OSS Distributions
180*bbb1b6f9SApple OSS Distributions return res;
181*bbb1b6f9SApple OSS Distributions }
182*bbb1b6f9SApple OSS Distributions
183*bbb1b6f9SApple OSS Distributions
184*bbb1b6f9SApple OSS Distributions IOReturn
organizeLegend(IOReportLegendEntry * legendEntry,const OSSymbol * groupName,const OSSymbol * subGroupName)185*bbb1b6f9SApple OSS Distributions IOReportLegend::organizeLegend(IOReportLegendEntry *legendEntry,
186*bbb1b6f9SApple OSS Distributions const OSSymbol *groupName,
187*bbb1b6f9SApple OSS Distributions const OSSymbol *subGroupName)
188*bbb1b6f9SApple OSS Distributions {
189*bbb1b6f9SApple OSS Distributions if (!legendEntry) {
190*bbb1b6f9SApple OSS Distributions return kIOReturnBadArgument;
191*bbb1b6f9SApple OSS Distributions }
192*bbb1b6f9SApple OSS Distributions
193*bbb1b6f9SApple OSS Distributions if (!groupName && subGroupName) {
194*bbb1b6f9SApple OSS Distributions return kIOReturnBadArgument;
195*bbb1b6f9SApple OSS Distributions }
196*bbb1b6f9SApple OSS Distributions
197*bbb1b6f9SApple OSS Distributions IORLEGENDLOG("IOReportLegend::organizeLegend");
198*bbb1b6f9SApple OSS Distributions // Legend is empty, enter first node
199*bbb1b6f9SApple OSS Distributions if (_reportLegend == NULL) {
200*bbb1b6f9SApple OSS Distributions IORLEGENDLOG("IOReportLegend::new legend creation");
201*bbb1b6f9SApple OSS Distributions _reportLegend = OSArray::withCapacity(1);
202*bbb1b6f9SApple OSS Distributions
203*bbb1b6f9SApple OSS Distributions if (!_reportLegend) {
204*bbb1b6f9SApple OSS Distributions return kIOReturnNoMemory;
205*bbb1b6f9SApple OSS Distributions }
206*bbb1b6f9SApple OSS Distributions }
207*bbb1b6f9SApple OSS Distributions
208*bbb1b6f9SApple OSS Distributions if (groupName) {
209*bbb1b6f9SApple OSS Distributions legendEntry->setObject(kIOReportLegendGroupNameKey, groupName);
210*bbb1b6f9SApple OSS Distributions }
211*bbb1b6f9SApple OSS Distributions
212*bbb1b6f9SApple OSS Distributions if (subGroupName) {
213*bbb1b6f9SApple OSS Distributions legendEntry->setObject(kIOReportLegendSubGroupNameKey, subGroupName);
214*bbb1b6f9SApple OSS Distributions }
215*bbb1b6f9SApple OSS Distributions
216*bbb1b6f9SApple OSS Distributions _reportLegend->setObject(legendEntry);
217*bbb1b6f9SApple OSS Distributions
218*bbb1b6f9SApple OSS Distributions // callers can now safely release legendEntry (it is part of _reportLegend)
219*bbb1b6f9SApple OSS Distributions
220*bbb1b6f9SApple OSS Distributions return kIOReturnSuccess;
221*bbb1b6f9SApple OSS Distributions }
222