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