|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Project CHIP Authors |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +#pragma once |
| 20 | + |
| 21 | +#include <string> |
| 22 | + |
| 23 | +#include <app-common/zap-generated/cluster-objects.h> |
| 24 | +#include <app/ConcreteAttributePath.h> |
| 25 | +#include <app/ConcreteCommandPath.h> |
| 26 | +#include <app/EventHeader.h> |
| 27 | +#include <app/MessageDef/StatusIB.h> |
| 28 | +#include <app/data-model/DecodableList.h> |
| 29 | +#include <commands/common/RemoteDataModelLogger.h> |
| 30 | +#include <lib/support/BytesToHex.h> |
| 31 | + |
| 32 | +class DataModelCustomHandler |
| 33 | +{ |
| 34 | +public: |
| 35 | + static CHIP_ERROR HandleAttribute(const chip::app::ConcreteDataAttributePath & path, chip::TLV::TLVReader * data); |
| 36 | + static CHIP_ERROR HandleCommand(const chip::app::ConcreteCommandPath & path, chip::TLV::TLVReader * data); |
| 37 | + static CHIP_ERROR HandleEvent(const chip::app::EventHeader & header, chip::TLV::TLVReader * data); |
| 38 | + |
| 39 | +private: |
| 40 | + static CHIP_ERROR LogValue(const char * label, size_t indent, bool value) |
| 41 | + { |
| 42 | + DataModelLogger::LogString(label, indent, value ? "TRUE" : "FALSE"); |
| 43 | + return CHIP_NO_ERROR; |
| 44 | + } |
| 45 | + |
| 46 | + static CHIP_ERROR LogValue(const char * label, size_t indent, chip::CharSpan value) |
| 47 | + { |
| 48 | + DataModelLogger::LogString(label, indent, std::string(value.data(), value.size())); |
| 49 | + return CHIP_NO_ERROR; |
| 50 | + } |
| 51 | + |
| 52 | + static CHIP_ERROR LogValue(const char * label, size_t indent, chip::ByteSpan value) |
| 53 | + { |
| 54 | + // CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE includes various prefixes we don't |
| 55 | + // control (timestamps, process ids, etc). Let's assume (hope?) that |
| 56 | + // those prefixes use up no more than half the total available space. |
| 57 | + // Right now it looks like the prefixes are 45 chars out of a 255 char |
| 58 | + // buffer. |
| 59 | + char buffer[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE / 2]; |
| 60 | + size_t prefixSize = ComputePrefixSize(label, indent); |
| 61 | + if (prefixSize > ArraySize(buffer)) |
| 62 | + { |
| 63 | + DataModelLogger::LogString("", 0, "Prefix is too long to fit in buffer"); |
| 64 | + return CHIP_ERROR_INTERNAL; |
| 65 | + } |
| 66 | + |
| 67 | + const size_t availableSize = ArraySize(buffer) - prefixSize; |
| 68 | + // Each byte ends up as two hex characters. |
| 69 | + const size_t bytesPerLogCall = availableSize / 2; |
| 70 | + std::string labelStr(label); |
| 71 | + while (value.size() > bytesPerLogCall) |
| 72 | + { |
| 73 | + ReturnErrorOnFailure( |
| 74 | + chip::Encoding::BytesToUppercaseHexString(value.data(), bytesPerLogCall, &buffer[0], ArraySize(buffer))); |
| 75 | + LogString(labelStr, indent, buffer); |
| 76 | + value = value.SubSpan(bytesPerLogCall); |
| 77 | + // For the second and following lines, make it clear that they are |
| 78 | + // continuation lines by replacing the label with "....". |
| 79 | + labelStr.replace(labelStr.begin(), labelStr.end(), labelStr.size(), '.'); |
| 80 | + } |
| 81 | + ReturnErrorOnFailure(chip::Encoding::BytesToUppercaseHexString(value.data(), value.size(), &buffer[0], ArraySize(buffer))); |
| 82 | + LogString(labelStr, indent, buffer); |
| 83 | + |
| 84 | + return CHIP_NO_ERROR; |
| 85 | + } |
| 86 | + |
| 87 | + template <typename X, |
| 88 | + typename std::enable_if_t< |
| 89 | + std::is_integral<X>::value && !std::is_same<std::remove_cv_t<std::remove_reference_t<X>>, bool>::value, int> = 0> |
| 90 | + static CHIP_ERROR LogValue(const char * label, size_t indent, X value) |
| 91 | + { |
| 92 | + DataModelLogger::LogString(label, indent, std::to_string(value)); |
| 93 | + return CHIP_NO_ERROR; |
| 94 | + } |
| 95 | + |
| 96 | + template <typename X, typename std::enable_if_t<std::is_floating_point<X>::value, int> = 0> |
| 97 | + static CHIP_ERROR LogValue(const char * label, size_t indent, X value) |
| 98 | + { |
| 99 | + DataModelLogger::LogString(label, indent, std::to_string(value)); |
| 100 | + return CHIP_NO_ERROR; |
| 101 | + } |
| 102 | + |
| 103 | + template <typename X, typename std::enable_if_t<std::is_enum<X>::value, int> = 0> |
| 104 | + static CHIP_ERROR LogValue(const char * label, size_t indent, X value) |
| 105 | + { |
| 106 | + return DataModelLogger::LogValue(label, indent, chip::to_underlying(value)); |
| 107 | + } |
| 108 | + |
| 109 | + template <typename X> |
| 110 | + static CHIP_ERROR LogValue(const char * label, size_t indent, chip::BitFlags<X> value) |
| 111 | + { |
| 112 | + return DataModelLogger::LogValue(label, indent, value.Raw()); |
| 113 | + } |
| 114 | + |
| 115 | + template <typename T> |
| 116 | + static CHIP_ERROR LogValue(const char * label, size_t indent, const chip::app::DataModel::DecodableList<T> & value) |
| 117 | + { |
| 118 | + size_t count = 0; |
| 119 | + ReturnErrorOnFailure(value.ComputeSize(&count)); |
| 120 | + DataModelLogger::LogString(label, indent, std::to_string(count) + " entries"); |
| 121 | + |
| 122 | + auto iter = value.begin(); |
| 123 | + size_t i = 0; |
| 124 | + while (iter.Next()) |
| 125 | + { |
| 126 | + ++i; |
| 127 | + std::string itemLabel = std::string("[") + std::to_string(i) + "]"; |
| 128 | + ReturnErrorOnFailure(DataModelLogger::LogValue(itemLabel.c_str(), indent + 1, iter.GetValue())); |
| 129 | + } |
| 130 | + if (iter.GetStatus() != CHIP_NO_ERROR) |
| 131 | + { |
| 132 | + DataModelLogger::LogString(indent + 1, "List truncated due to invalid value"); |
| 133 | + } |
| 134 | + return iter.GetStatus(); |
| 135 | + } |
| 136 | + |
| 137 | + template <typename T> |
| 138 | + static CHIP_ERROR LogValue(const char * label, size_t indent, const chip::app::DataModel::Nullable<T> & value) |
| 139 | + { |
| 140 | + if (value.IsNull()) |
| 141 | + { |
| 142 | + DataModelLogger::LogString(label, indent, "null"); |
| 143 | + return CHIP_NO_ERROR; |
| 144 | + } |
| 145 | + |
| 146 | + return DataModelLogger::LogValue(label, indent, value.Value()); |
| 147 | + } |
| 148 | + |
| 149 | + template <typename T> |
| 150 | + static CHIP_ERROR LogValue(const char * label, size_t indent, const chip::Optional<T> & value) |
| 151 | + { |
| 152 | + if (value.HasValue()) |
| 153 | + { |
| 154 | + return DataModelLogger::LogValue(label, indent, value.Value()); |
| 155 | + } |
| 156 | + |
| 157 | + return CHIP_NO_ERROR; |
| 158 | + } |
| 159 | + |
| 160 | +#include <zap-generated/cluster/logging/DataModelLogger.h> |
| 161 | + |
| 162 | + static void LogString(size_t indent, const std::string string) { LogString("", indent, string); } |
| 163 | + |
| 164 | + static void LogString(const std::string label, size_t indent, const std::string string) |
| 165 | + { |
| 166 | + std::string prefix = ComputePrefix(label, indent); |
| 167 | + |
| 168 | + ChipLogProgress(chipTool, "%s%s", prefix.c_str(), string.c_str()); |
| 169 | + } |
| 170 | + |
| 171 | +private: |
| 172 | + static std::string ComputePrefix(const std::string label, size_t indent) |
| 173 | + { |
| 174 | + std::string prefix; |
| 175 | + for (size_t i = 0; i < indent; ++i) |
| 176 | + { |
| 177 | + prefix.append(" "); |
| 178 | + } |
| 179 | + if (label.size() > 0) |
| 180 | + { |
| 181 | + prefix.append(label); |
| 182 | + prefix.append(":"); |
| 183 | + } |
| 184 | + prefix.append(" "); |
| 185 | + |
| 186 | + return prefix; |
| 187 | + } |
| 188 | + |
| 189 | + static size_t ComputePrefixSize(const std::string label, size_t indent) { return ComputePrefix(label, indent).size(); } |
| 190 | +}; |
0 commit comments