|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 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 | +#include "RemoteDataModelLogger.h" |
| 20 | + |
| 21 | +#import "MTRError_Utils.h" |
| 22 | +#import <objc/runtime.h> |
| 23 | + |
| 24 | +#include <json/json.h> |
| 25 | +#include <lib/support/SafeInt.h> |
| 26 | +#include <protocols/interaction_model/StatusCode.h> |
| 27 | + |
| 28 | +#include <string> |
| 29 | + |
| 30 | +constexpr const char * kClusterIdKey = "clusterId"; |
| 31 | +constexpr const char * kEndpointIdKey = "endpointId"; |
| 32 | +constexpr const char * kAttributeIdKey = "attributeId"; |
| 33 | +constexpr const char * kCommandIdKey = "commandId"; |
| 34 | +constexpr const char * kErrorIdKey = "error"; |
| 35 | +constexpr const char * kClusterErrorIdKey = "clusterError"; |
| 36 | +constexpr const char * kValueKey = "value"; |
| 37 | + |
| 38 | +constexpr const char kBase64Header[] = "base64:"; |
| 39 | + |
| 40 | +namespace { |
| 41 | +RemoteDataModelLoggerDelegate * gDelegate; |
| 42 | + |
| 43 | +std::string JsonToString(Json::Value & json) |
| 44 | +{ |
| 45 | + Json::FastWriter writer; |
| 46 | + writer.omitEndingLineFeed(); |
| 47 | + return writer.write(json); |
| 48 | +} |
| 49 | + |
| 50 | +CHIP_ERROR LogError(Json::Value & value, const chip::app::StatusIB & status) |
| 51 | +{ |
| 52 | + if (status.mClusterStatus.HasValue()) { |
| 53 | + auto statusValue = status.mClusterStatus.Value(); |
| 54 | + value[kClusterErrorIdKey] = statusValue; |
| 55 | + } |
| 56 | + |
| 57 | +#if CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT |
| 58 | + auto statusName = chip::Protocols::InteractionModel::StatusName(status.mStatus); |
| 59 | + value[kErrorIdKey] = statusName; |
| 60 | +#else |
| 61 | + auto statusName = status.mStatus; |
| 62 | + value[kErrorIdKey] = chip::to_underlying(statusName); |
| 63 | +#endif // CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT |
| 64 | + |
| 65 | + auto valueStr = JsonToString(value); |
| 66 | + return gDelegate->LogJSON(valueStr.c_str()); |
| 67 | +} |
| 68 | + |
| 69 | +CHIP_ERROR AsJsonValue(id value, Json::Value & jsonValue) |
| 70 | +{ |
| 71 | + if (value == nil) { |
| 72 | + jsonValue = Json::nullValue; |
| 73 | + } else if ([value isKindOfClass:[NSNumber class]]) { |
| 74 | + if (CFNumberIsFloatType((CFNumberRef) value)) { |
| 75 | + jsonValue = [value doubleValue]; |
| 76 | + } else if ([[value stringValue] hasPrefix:@"-"]) { |
| 77 | + jsonValue = [value longLongValue]; |
| 78 | + } else { |
| 79 | + jsonValue = [value unsignedLongLongValue]; |
| 80 | + } |
| 81 | + } else if ([value isKindOfClass:[NSArray class]]) { |
| 82 | + jsonValue = Json::arrayValue; |
| 83 | + |
| 84 | + NSArray * array = value; |
| 85 | + for (id element in array) { |
| 86 | + Json::Value jsonElement; |
| 87 | + VerifyOrDie(CHIP_NO_ERROR == AsJsonValue(element, jsonElement)); |
| 88 | + jsonValue.append(jsonElement); |
| 89 | + } |
| 90 | + } else if ([value isKindOfClass:[NSDictionary class]]) { |
| 91 | + jsonValue = Json::ValueType::objectValue; |
| 92 | + |
| 93 | + NSDictionary * dict = value; |
| 94 | + for (id key in dict) { |
| 95 | + Json::Value jsonElement; |
| 96 | + VerifyOrDie(CHIP_NO_ERROR == AsJsonValue([dict objectForKey:key], jsonElement)); |
| 97 | + jsonValue[[key UTF8String]] = jsonElement; |
| 98 | + } |
| 99 | + } else if ([value isKindOfClass:[NSData class]]) { |
| 100 | + NSData * data = value; |
| 101 | + data = [data base64EncodedDataWithOptions:0]; |
| 102 | + auto base64Str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; |
| 103 | + auto prefix = [NSString stringWithUTF8String:kBase64Header]; |
| 104 | + auto base64PrefixedStr = [prefix stringByAppendingString:base64Str]; |
| 105 | + jsonValue = [base64PrefixedStr UTF8String]; |
| 106 | + } else if ([value isKindOfClass:[NSString class]]) { |
| 107 | + jsonValue = [value UTF8String]; |
| 108 | + } else if ([value isKindOfClass:[NSObject class]]) { |
| 109 | + jsonValue = Json::ValueType::objectValue; |
| 110 | + |
| 111 | + unsigned int numberOfProperties; |
| 112 | + objc_property_t * properties = class_copyPropertyList([value class], &numberOfProperties); |
| 113 | + for (NSUInteger i = 0; i < numberOfProperties; i++) { |
| 114 | + objc_property_t property = properties[i]; |
| 115 | + NSString * key = [[NSString alloc] initWithUTF8String:property_getName(property)]; |
| 116 | + |
| 117 | + Json::Value jsonElement; |
| 118 | + VerifyOrDie(CHIP_NO_ERROR == AsJsonValue([value valueForKey:key], jsonElement)); |
| 119 | + jsonValue[[key UTF8String]] = jsonElement; |
| 120 | + } |
| 121 | + free(properties); |
| 122 | + } else { |
| 123 | + return CHIP_ERROR_NOT_IMPLEMENTED; |
| 124 | + } |
| 125 | + |
| 126 | + return CHIP_NO_ERROR; |
| 127 | +} |
| 128 | + |
| 129 | +} // namespace |
| 130 | + |
| 131 | +namespace RemoteDataModelLogger { |
| 132 | +CHIP_ERROR LogAttributeAsJSON(NSNumber * endpointId, NSNumber * clusterId, NSNumber * attributeId, id result) |
| 133 | +{ |
| 134 | + VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR); |
| 135 | + |
| 136 | + Json::Value value; |
| 137 | + value[kEndpointIdKey] = [endpointId unsignedLongLongValue]; |
| 138 | + value[kClusterIdKey] = [clusterId unsignedLongLongValue]; |
| 139 | + value[kAttributeIdKey] = [attributeId unsignedLongLongValue]; |
| 140 | + |
| 141 | + Json::Value jsonValue; |
| 142 | + VerifyOrDie(CHIP_NO_ERROR == AsJsonValue(result, jsonValue)); |
| 143 | + value[kValueKey] = jsonValue; |
| 144 | + |
| 145 | + auto valueStr = JsonToString(value); |
| 146 | + return gDelegate->LogJSON(valueStr.c_str()); |
| 147 | +} |
| 148 | + |
| 149 | +CHIP_ERROR LogCommandAsJSON(NSNumber * endpointId, NSNumber * clusterId, NSNumber * commandId, id result) |
| 150 | +{ |
| 151 | + VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR); |
| 152 | + |
| 153 | + Json::Value value; |
| 154 | + value[kEndpointIdKey] = [endpointId unsignedLongLongValue]; |
| 155 | + value[kClusterIdKey] = [clusterId unsignedLongLongValue]; |
| 156 | + value[kCommandIdKey] = [commandId unsignedLongLongValue]; |
| 157 | + |
| 158 | + Json::Value jsonValue; |
| 159 | + VerifyOrDie(CHIP_NO_ERROR == AsJsonValue(result, jsonValue)); |
| 160 | + value[kValueKey] = jsonValue; |
| 161 | + |
| 162 | + auto valueStr = JsonToString(value); |
| 163 | + return gDelegate->LogJSON(valueStr.c_str()); |
| 164 | +} |
| 165 | + |
| 166 | +CHIP_ERROR LogAttributeErrorAsJSON(NSNumber * endpointId, NSNumber * clusterId, NSNumber * attributeId, NSError * error) |
| 167 | +{ |
| 168 | + VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR); |
| 169 | + |
| 170 | + Json::Value value; |
| 171 | + value[kEndpointIdKey] = [endpointId unsignedLongLongValue]; |
| 172 | + value[kClusterIdKey] = [clusterId unsignedLongLongValue]; |
| 173 | + value[kAttributeIdKey] = [attributeId unsignedLongLongValue]; |
| 174 | + |
| 175 | + auto err = MTRErrorToCHIPErrorCode(error); |
| 176 | + auto status = chip::app::StatusIB(err); |
| 177 | + return LogError(value, status); |
| 178 | +} |
| 179 | + |
| 180 | +CHIP_ERROR LogCommandErrorAsJSON(NSNumber * endpointId, NSNumber * clusterId, NSNumber * commandId, NSError * error) |
| 181 | +{ |
| 182 | + VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR); |
| 183 | + |
| 184 | + Json::Value value; |
| 185 | + value[kEndpointIdKey] = [endpointId unsignedLongLongValue]; |
| 186 | + value[kClusterIdKey] = [clusterId unsignedLongLongValue]; |
| 187 | + value[kCommandIdKey] = [commandId unsignedLongLongValue]; |
| 188 | + |
| 189 | + auto err = MTRErrorToCHIPErrorCode(error); |
| 190 | + auto status = chip::app::StatusIB(err); |
| 191 | + return LogError(value, status); |
| 192 | +} |
| 193 | + |
| 194 | +void SetDelegate(RemoteDataModelLoggerDelegate * delegate) { gDelegate = delegate; } |
| 195 | +}; // namespace RemoteDataModelLogger |
0 commit comments