2 * Copyright (c) 2012-2013 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <IOKit/IOKernelReportStructs.h>
30 #include <IOKit/IOKernelReporters.h>
33 //#define IORDEBUG_LEGEND 1
35 #ifdef IORDEBUG_LEGEND
36 #define IORLEGENDLOG(fmt, args...) \
38 IOLog("IOReportLegend | "); \
43 #define IORLEGENDLOG(fmt, args...)
47 #define super OSObject
48 OSDefineMetaClassAndStructors(IOReportLegend
, OSObject
);
51 IOReportLegend::with(OSArray
*legend
)
53 IOReportLegend
*iorLegend
= new IOReportLegend
;
57 if (iorLegend
->initWith(legend
) != kIOReturnSuccess
) {
58 OSSafeReleaseNULL(iorLegend
);
69 /* must clean up everything if it fails */
71 IOReportLegend::initWith(OSArray
*legend
)
74 _reportLegend
= OSArray::withArray(legend
);
77 if (_reportLegend
== NULL
) {
78 return kIOReturnError
;
80 return kIOReturnSuccess
;
86 IOReportLegend::free(void)
89 _reportLegend
->release();
96 IOReportLegend::getLegend(void)
102 IOReportLegend::addReporterLegend(IOService
*reportingService
,
103 IOReporter
*reporter
,
104 const char *groupName
,
105 const char *subGroupName
)
107 IOReturn res
= kIOReturnError
;
108 IOReportLegend
*legend
= NULL
;
109 OSObject
*curLegend
= NULL
;
111 // No need to check groupName and subGroupName because optional params
112 if (!reportingService
|| !reporter
) {
116 // It's fine if the legend doesn't exist (IOReportLegend::with(NULL)
117 // is how you make an empty legend). If it's not an array, then
118 // we're just going to replace it.
119 curLegend
= reportingService
->copyProperty(kIOReportLegendKey
);
120 legend
= IOReportLegend::with(OSDynamicCast(OSArray
, curLegend
));
125 // Add the reporter's entries and update the service property.
126 // The overwrite triggers a release of the old legend array.
127 legend
->addReporterLegend(reporter
, groupName
, subGroupName
);
128 reportingService
->setProperty(kIOReportLegendKey
, legend
->getLegend());
129 reportingService
->setProperty(kIOReportLegendPublicKey
, true);
131 res
= kIOReturnSuccess
;
138 curLegend
->release();
146 IOReportLegend::addLegendEntry(IOReportLegendEntry
*legendEntry
,
147 const char *groupName
,
148 const char *subGroupName
)
150 kern_return_t res
= kIOReturnError
;
151 const OSSymbol
*tmpGroupName
= NULL
;
152 const OSSymbol
*tmpSubGroupName
= NULL
;
159 tmpGroupName
= OSSymbol::withCString(groupName
);
163 tmpSubGroupName
= OSSymbol::withCString(subGroupName
);
166 // It is ok to call appendLegendWith() if tmpGroups are NULL
168 res
= organizeLegend(legendEntry
, tmpGroupName
, tmpSubGroupName
);
171 tmpGroupName
->release();
173 if (tmpSubGroupName
) {
174 tmpSubGroupName
->release();
184 IOReportLegend::addReporterLegend(IOReporter
*reporter
,
185 const char *groupName
,
186 const char *subGroupName
)
188 IOReturn res
= kIOReturnError
;
189 IOReportLegendEntry
*legendEntry
= NULL
;
192 legendEntry
= reporter
->createLegend();
195 res
= addLegendEntry(legendEntry
, groupName
, subGroupName
);
196 legendEntry
->release();
205 IOReportLegend::organizeLegend(IOReportLegendEntry
*legendEntry
,
206 const OSSymbol
*groupName
,
207 const OSSymbol
*subGroupName
)
209 IOReturn res
= kIOReturnError
;
212 return res
= kIOReturnBadArgument
;
215 if (!groupName
&& subGroupName
) {
216 return res
= kIOReturnBadArgument
;
219 IORLEGENDLOG("IOReportLegend::organizeLegend");
220 // Legend is empty, enter first node
221 if (_reportLegend
== NULL
) {
222 IORLEGENDLOG("IOReportLegend::new legend creation");
223 _reportLegend
= OSArray::withCapacity(1);
225 if (!_reportLegend
) {
226 return kIOReturnNoMemory
;
231 legendEntry
->setObject(kIOReportLegendGroupNameKey
, groupName
);
235 legendEntry
->setObject(kIOReportLegendSubGroupNameKey
, subGroupName
);
238 _reportLegend
->setObject(legendEntry
);
240 // callers can now safely release legendEntry (it is part of _reportLegend)
242 return res
= kIOReturnSuccess
;