-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MatterYamlTests] Add basic support for darwin-framework-tool (#28969)
* Add darwin-framework-tool python yaml parser connection support. For the moment darwin-framework-tool does not send back anything nor supports special test commands that have been added to chip-tool to fully support yaml * Add sleep command to darwin-framework-tool * Add wait-for-commissionee command to darwin-framework-tool * Add darwin-framework-tool python yaml parser output connection support * Hack examples/chip-tool/py_matter_chip_tool_adapter/matter_chip_tool_adapter/encoder.py so it does use the right argument names for darwin-framework-tool --------- Co-authored-by: Vivien Nicolas <[email protected]>
- Loading branch information
Showing
22 changed files
with
17,961 additions
and
2,737 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
examples/darwin-framework-tool/commands/common/RemoteDataModelLogger.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2023 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#import <Matter/Matter.h> | ||
|
||
#include <lib/core/CHIPError.h> | ||
|
||
class RemoteDataModelLoggerDelegate { | ||
public: | ||
CHIP_ERROR virtual LogJSON(const char *) = 0; | ||
virtual ~RemoteDataModelLoggerDelegate() {}; | ||
}; | ||
|
||
namespace RemoteDataModelLogger { | ||
CHIP_ERROR LogAttributeAsJSON(NSNumber * endpointId, NSNumber * clusterId, NSNumber * attributeId, id result); | ||
CHIP_ERROR LogCommandAsJSON(NSNumber * endpointId, NSNumber * clusterId, NSNumber * commandId, id result); | ||
CHIP_ERROR LogAttributeErrorAsJSON(NSNumber * endpointId, NSNumber * clusterId, NSNumber * attributeId, NSError * error); | ||
CHIP_ERROR LogCommandErrorAsJSON(NSNumber * endpointId, NSNumber * clusterId, NSNumber * commandId, NSError * error); | ||
void SetDelegate(RemoteDataModelLoggerDelegate * delegate); | ||
}; // namespace RemoteDataModelLogger |
195 changes: 195 additions & 0 deletions
195
examples/darwin-framework-tool/commands/common/RemoteDataModelLogger.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
/* | ||
* Copyright (c) 2023 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#include "RemoteDataModelLogger.h" | ||
|
||
#import "MTRError_Utils.h" | ||
#import <objc/runtime.h> | ||
|
||
#include <json/json.h> | ||
#include <lib/support/SafeInt.h> | ||
#include <protocols/interaction_model/StatusCode.h> | ||
|
||
#include <string> | ||
|
||
constexpr const char * kClusterIdKey = "clusterId"; | ||
constexpr const char * kEndpointIdKey = "endpointId"; | ||
constexpr const char * kAttributeIdKey = "attributeId"; | ||
constexpr const char * kCommandIdKey = "commandId"; | ||
constexpr const char * kErrorIdKey = "error"; | ||
constexpr const char * kClusterErrorIdKey = "clusterError"; | ||
constexpr const char * kValueKey = "value"; | ||
|
||
constexpr const char kBase64Header[] = "base64:"; | ||
|
||
namespace { | ||
RemoteDataModelLoggerDelegate * gDelegate; | ||
|
||
std::string JsonToString(Json::Value & json) | ||
{ | ||
Json::FastWriter writer; | ||
writer.omitEndingLineFeed(); | ||
return writer.write(json); | ||
} | ||
|
||
CHIP_ERROR LogError(Json::Value & value, const chip::app::StatusIB & status) | ||
{ | ||
if (status.mClusterStatus.HasValue()) { | ||
auto statusValue = status.mClusterStatus.Value(); | ||
value[kClusterErrorIdKey] = statusValue; | ||
} | ||
|
||
#if CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT | ||
auto statusName = chip::Protocols::InteractionModel::StatusName(status.mStatus); | ||
value[kErrorIdKey] = statusName; | ||
#else | ||
auto statusName = status.mStatus; | ||
value[kErrorIdKey] = chip::to_underlying(statusName); | ||
#endif // CHIP_CONFIG_IM_STATUS_CODE_VERBOSE_FORMAT | ||
|
||
auto valueStr = JsonToString(value); | ||
return gDelegate->LogJSON(valueStr.c_str()); | ||
} | ||
|
||
CHIP_ERROR AsJsonValue(id value, Json::Value & jsonValue) | ||
{ | ||
if (value == nil) { | ||
jsonValue = Json::nullValue; | ||
} else if ([value isKindOfClass:[NSNumber class]]) { | ||
if (CFNumberIsFloatType((CFNumberRef) value)) { | ||
jsonValue = [value doubleValue]; | ||
} else if ([[value stringValue] hasPrefix:@"-"]) { | ||
jsonValue = [value longLongValue]; | ||
} else { | ||
jsonValue = [value unsignedLongLongValue]; | ||
} | ||
} else if ([value isKindOfClass:[NSArray class]]) { | ||
jsonValue = Json::arrayValue; | ||
|
||
NSArray * array = value; | ||
for (id element in array) { | ||
Json::Value jsonElement; | ||
VerifyOrDie(CHIP_NO_ERROR == AsJsonValue(element, jsonElement)); | ||
jsonValue.append(jsonElement); | ||
} | ||
} else if ([value isKindOfClass:[NSDictionary class]]) { | ||
jsonValue = Json::ValueType::objectValue; | ||
|
||
NSDictionary * dict = value; | ||
for (id key in dict) { | ||
Json::Value jsonElement; | ||
VerifyOrDie(CHIP_NO_ERROR == AsJsonValue([dict objectForKey:key], jsonElement)); | ||
jsonValue[[key UTF8String]] = jsonElement; | ||
} | ||
} else if ([value isKindOfClass:[NSData class]]) { | ||
NSData * data = value; | ||
data = [data base64EncodedDataWithOptions:0]; | ||
auto base64Str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; | ||
auto prefix = [NSString stringWithUTF8String:kBase64Header]; | ||
auto base64PrefixedStr = [prefix stringByAppendingString:base64Str]; | ||
jsonValue = [base64PrefixedStr UTF8String]; | ||
} else if ([value isKindOfClass:[NSString class]]) { | ||
jsonValue = [value UTF8String]; | ||
} else if ([value isKindOfClass:[NSObject class]]) { | ||
jsonValue = Json::ValueType::objectValue; | ||
|
||
unsigned int numberOfProperties; | ||
objc_property_t * properties = class_copyPropertyList([value class], &numberOfProperties); | ||
for (NSUInteger i = 0; i < numberOfProperties; i++) { | ||
objc_property_t property = properties[i]; | ||
NSString * key = [[NSString alloc] initWithUTF8String:property_getName(property)]; | ||
|
||
Json::Value jsonElement; | ||
VerifyOrDie(CHIP_NO_ERROR == AsJsonValue([value valueForKey:key], jsonElement)); | ||
jsonValue[[key UTF8String]] = jsonElement; | ||
} | ||
free(properties); | ||
} else { | ||
return CHIP_ERROR_NOT_IMPLEMENTED; | ||
} | ||
|
||
return CHIP_NO_ERROR; | ||
} | ||
|
||
} // namespace | ||
|
||
namespace RemoteDataModelLogger { | ||
CHIP_ERROR LogAttributeAsJSON(NSNumber * endpointId, NSNumber * clusterId, NSNumber * attributeId, id result) | ||
{ | ||
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR); | ||
|
||
Json::Value value; | ||
value[kEndpointIdKey] = [endpointId unsignedLongLongValue]; | ||
value[kClusterIdKey] = [clusterId unsignedLongLongValue]; | ||
value[kAttributeIdKey] = [attributeId unsignedLongLongValue]; | ||
|
||
Json::Value jsonValue; | ||
VerifyOrDie(CHIP_NO_ERROR == AsJsonValue(result, jsonValue)); | ||
value[kValueKey] = jsonValue; | ||
|
||
auto valueStr = JsonToString(value); | ||
return gDelegate->LogJSON(valueStr.c_str()); | ||
} | ||
|
||
CHIP_ERROR LogCommandAsJSON(NSNumber * endpointId, NSNumber * clusterId, NSNumber * commandId, id result) | ||
{ | ||
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR); | ||
|
||
Json::Value value; | ||
value[kEndpointIdKey] = [endpointId unsignedLongLongValue]; | ||
value[kClusterIdKey] = [clusterId unsignedLongLongValue]; | ||
value[kCommandIdKey] = [commandId unsignedLongLongValue]; | ||
|
||
Json::Value jsonValue; | ||
VerifyOrDie(CHIP_NO_ERROR == AsJsonValue(result, jsonValue)); | ||
value[kValueKey] = jsonValue; | ||
|
||
auto valueStr = JsonToString(value); | ||
return gDelegate->LogJSON(valueStr.c_str()); | ||
} | ||
|
||
CHIP_ERROR LogAttributeErrorAsJSON(NSNumber * endpointId, NSNumber * clusterId, NSNumber * attributeId, NSError * error) | ||
{ | ||
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR); | ||
|
||
Json::Value value; | ||
value[kEndpointIdKey] = [endpointId unsignedLongLongValue]; | ||
value[kClusterIdKey] = [clusterId unsignedLongLongValue]; | ||
value[kAttributeIdKey] = [attributeId unsignedLongLongValue]; | ||
|
||
auto err = MTRErrorToCHIPErrorCode(error); | ||
auto status = chip::app::StatusIB(err); | ||
return LogError(value, status); | ||
} | ||
|
||
CHIP_ERROR LogCommandErrorAsJSON(NSNumber * endpointId, NSNumber * clusterId, NSNumber * commandId, NSError * error) | ||
{ | ||
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR); | ||
|
||
Json::Value value; | ||
value[kEndpointIdKey] = [endpointId unsignedLongLongValue]; | ||
value[kClusterIdKey] = [clusterId unsignedLongLongValue]; | ||
value[kCommandIdKey] = [commandId unsignedLongLongValue]; | ||
|
||
auto err = MTRErrorToCHIPErrorCode(error); | ||
auto status = chip::app::StatusIB(err); | ||
return LogError(value, status); | ||
} | ||
|
||
void SetDelegate(RemoteDataModelLoggerDelegate * delegate) { gDelegate = delegate; } | ||
}; // namespace RemoteDataModelLogger |
Oops, something went wrong.