| 1 | /* |
| 2 | * Copyright (c) 2012-2013 Apple Computer, Inc. All Rights Reserved. |
| 3 | * |
| 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
| 5 | * |
| 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. |
| 14 | * |
| 15 | * Please obtain a copy of the License at |
| 16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
| 17 | * |
| 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. |
| 25 | * |
| 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
| 27 | */ |
| 28 | |
| 29 | #define IOKIT_ENABLE_SHARED_PTR |
| 30 | |
| 31 | #include <libkern/c++/OSSharedPtr.h> |
| 32 | #include <IOKit/IOKernelReportStructs.h> |
| 33 | #include <IOKit/IOKernelReporters.h> |
| 34 | #include "IOReporterDefs.h" |
| 35 | |
| 36 | #define super IOReporter |
| 37 | OSDefineMetaClassAndStructors(IOSimpleReporter, IOReporter); |
| 38 | |
| 39 | /* static */ |
| 40 | OSSharedPtr<IOSimpleReporter> |
| 41 | IOSimpleReporter::with(IOService *reportingService, |
| 42 | IOReportCategories categories, |
| 43 | IOReportUnit unit) |
| 44 | { |
| 45 | OSSharedPtr<IOSimpleReporter> reporter; |
| 46 | |
| 47 | reporter = OSMakeShared<IOSimpleReporter>(); |
| 48 | if (!reporter) { |
| 49 | return nullptr; |
| 50 | } |
| 51 | |
| 52 | if (!reporter->initWith(reportingService, categories, unit)) { |
| 53 | return nullptr; |
| 54 | } |
| 55 | |
| 56 | return reporter; |
| 57 | } |
| 58 | |
| 59 | bool |
| 60 | IOSimpleReporter::initWith(IOService *reportingService, |
| 61 | IOReportCategories categories, |
| 62 | IOReportUnit unit) |
| 63 | { |
| 64 | // fully specify the channel type for the superclass |
| 65 | IOReportChannelType channelType = { |
| 66 | .categories = categories, |
| 67 | .report_format = kIOReportFormatSimple, |
| 68 | .nelements = 1, |
| 69 | .element_idx = 0 |
| 70 | }; |
| 71 | |
| 72 | return super::init(reportingService, channelType, unit); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | IOReturn |
| 77 | IOSimpleReporter::setValue(uint64_t channel_id, |
| 78 | int64_t value) |
| 79 | { |
| 80 | IOReturn res = kIOReturnError; |
| 81 | IOSimpleReportValues simple_values; |
| 82 | int element_index = 0; |
| 83 | |
| 84 | lockReporter(); |
| 85 | |
| 86 | if (getFirstElementIndex(channel_id, element_index: &element_index) != kIOReturnSuccess) { |
| 87 | res = kIOReturnBadArgument; |
| 88 | goto finish; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | if (copyElementValues(element_index, elementValues: (IOReportElementValues *)&simple_values) != kIOReturnSuccess) { |
| 93 | res = kIOReturnBadArgument; |
| 94 | goto finish; |
| 95 | } |
| 96 | |
| 97 | simple_values.simple_value = value; |
| 98 | res = setElementValues(element_index, values: (IOReportElementValues *)&simple_values); |
| 99 | |
| 100 | finish: |
| 101 | unlockReporter(); |
| 102 | return res; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | IOReturn |
| 107 | IOSimpleReporter::incrementValue(uint64_t channel_id, |
| 108 | int64_t increment) |
| 109 | { |
| 110 | IOReturn res = kIOReturnError; |
| 111 | IOSimpleReportValues simple_values; |
| 112 | int element_index = 0; |
| 113 | |
| 114 | lockReporter(); |
| 115 | |
| 116 | if (getFirstElementIndex(channel_id, element_index: &element_index) != kIOReturnSuccess) { |
| 117 | res = kIOReturnBadArgument; |
| 118 | goto finish; |
| 119 | } |
| 120 | |
| 121 | if (copyElementValues(element_index, elementValues: (IOReportElementValues *)&simple_values) != kIOReturnSuccess) { |
| 122 | res = kIOReturnBadArgument; |
| 123 | goto finish; |
| 124 | } |
| 125 | |
| 126 | simple_values.simple_value += increment; |
| 127 | |
| 128 | res = setElementValues(element_index, values: (IOReportElementValues *)&simple_values); |
| 129 | |
| 130 | finish: |
| 131 | unlockReporter(); |
| 132 | return res; |
| 133 | } |
| 134 | |
| 135 | int64_t |
| 136 | IOSimpleReporter::getValue(uint64_t channel_id) |
| 137 | { |
| 138 | IOSimpleReportValues *values = NULL; |
| 139 | int64_t simple_value = (int64_t)kIOReportInvalidValue; |
| 140 | int index = 0; |
| 141 | |
| 142 | lockReporter(); |
| 143 | |
| 144 | if (getFirstElementIndex(channel_id, element_index: &index) == kIOReturnSuccess) { |
| 145 | values = (IOSimpleReportValues *)getElementValues(element_index: index); |
| 146 | |
| 147 | if (values != NULL) { |
| 148 | simple_value = values->simple_value; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | unlockReporter(); |
| 153 | return simple_value; |
| 154 | } |
| 155 | |
| 156 | /* static */ OSPtr<IOReportLegendEntry> |
| 157 | IOSimpleReporter::createLegend(const uint64_t *channelIDs, |
| 158 | const char **channelNames, |
| 159 | int channelCount, |
| 160 | IOReportCategories categories, |
| 161 | IOReportUnit unit) |
| 162 | { |
| 163 | IOReportChannelType channelType = { |
| 164 | .categories = categories, |
| 165 | .report_format = kIOReportFormatSimple, |
| 166 | .nelements = 1, |
| 167 | .element_idx = 0 |
| 168 | }; |
| 169 | |
| 170 | return IOReporter::legendWith(channelIDs, channelNames, channelCount, channelType, unit); |
| 171 | } |
| 172 | |