Skip to content

Commit

Permalink
Fix chip-tool command line to use the new subscription API.
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple committed Dec 8, 2021
1 parent 398d469 commit 1160743
Show file tree
Hide file tree
Showing 2 changed files with 3,379 additions and 9,220 deletions.
98 changes: 9 additions & 89 deletions examples/chip-tool/templates/commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ static void OnDefaultSuccessResponse(void * context)
command->SetCommandExitStatus(CHIP_NO_ERROR);
}

static void OnDefaultFailureResponse(void * context, uint8_t status)
static void OnDefaultFailure(void * context, EmberAfStatus status)
{
ChipLogProgress(chipTool, "Default Failure Response: 0x%02x", status);
ChipLogProgress(chipTool, "Default Failure Response: 0x%02x", chip::to_underlying(status));

ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_ERROR_INTERNAL);
Expand All @@ -195,81 +195,6 @@ static void OnDefaultSuccess(void * context, const chip::app::DataModel::NullObj
OnDefaultSuccessResponse(context);
};

static void OnDefaultFailure(void * context, EmberAfStatus status)
{
OnDefaultFailureResponse(context, status);
};

static void OnBooleanAttributeReport(void * context, bool value)
{
ChipLogProgress(chipTool, "Boolean attribute Response: %d", value);
}

static void OnInt8uAttributeReport(void * context, uint8_t value)
{
ChipLogProgress(chipTool, "Int8u attribute Response: %" PRIu8, value);
}

static void OnInt16uAttributeReport(void * context, uint16_t value)
{
ChipLogProgress(chipTool, "Int16u attribute Response: %" PRIu16, value);
}

static void OnInt32uAttributeReport(void * context, uint32_t value)
{
ChipLogProgress(chipTool, "Int32u attribute Response: %" PRIu32, value);
}

static void OnInt64uAttributeReport(void * context, uint64_t value)
{
ChipLogProgress(chipTool, "Int64u attribute Response: %" PRIu64, value);
}

static void OnInt8sAttributeReport(void * context, int8_t value)
{
ChipLogProgress(chipTool, "Int8s attribute Response: %" PRId8, value);
}

static void OnInt16sAttributeReport(void * context, int16_t value)
{
ChipLogProgress(chipTool, "Int16s attribute Response: %" PRId16, value);
}

static void OnInt32sAttributeReport(void * context, int32_t value)
{
ChipLogProgress(chipTool, "Int32s attribute Response: %" PRId32, value);
}

static void OnInt64sAttributeReport(void * context, int64_t value)
{
ChipLogProgress(chipTool, "Int64s attribute Response: %" PRId64, value);
}

static void OnFloatAttributeReport(void * context, float value)
{
ChipLogProgress(chipTool, "Float attribute Response: %f", value);
}

static void OnDoubleAttributeReport(void * context, double value)
{
ChipLogProgress(chipTool, "Double attribute Response: %f", value);
}

static void OnOctetStringAttributeReport(void * context, const chip::ByteSpan value)
{
char buffer[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
if (CHIP_NO_ERROR == chip::Encoding::BytesToUppercaseHexString(value.data(), value.size(), &buffer[0], CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE)) {
ChipLogProgress(chipTool, "OctetString attribute Response: %s", buffer);
} else {
ChipLogProgress(chipTool, "OctetString attribute Response len: %zu", value.size());
}
}

static void OnCharStringAttributeReport(void * context, const chip::CharSpan value)
{
ChipLogProgress(chipTool, "CharString attribute Response: %.*s", static_cast<int>(value.size()), value.data());
}

template <typename T>
static void OnGeneralAttributeResponse(void * context, const char * label, T value)
{
Expand Down Expand Up @@ -445,10 +370,6 @@ public:

~Report{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}()
{
delete onSuccessCallback;
delete onSuccessCallbackWithoutExit;
delete onFailureCallback;
delete onReportCallback;
}

CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endpointId) override
Expand All @@ -458,22 +379,21 @@ public:
chip::Controller::{{asUpperCamelCase parent.name}}Cluster cluster;
cluster.Associate(device, endpointId);

ReturnErrorOnFailure(cluster.ReportAttribute{{asUpperCamelCase name}}(onReportCallback->Cancel()));

chip::Callback::Cancelable * successCallback = mWait ? onSuccessCallbackWithoutExit->Cancel() : onSuccessCallback->Cancel();
return cluster.SubscribeAttribute{{asUpperCamelCase name}}(successCallback, onFailureCallback->Cancel(), mMinInterval, mMaxInterval);
auto subscriptionEstablishedCallback = mWait ? OnDefaultSuccessResponseWithoutExit : OnDefaultSuccessResponse;
return cluster.SubscribeAttribute<chip::app::Clusters::{{asUpperCamelCase parent.name}}::Attributes::{{asUpperCamelCase name}}::TypeInfo>(this, OnValueReport, OnDefaultFailure, mMinInterval, mMaxInterval, subscriptionEstablishedCallback);
}

chip::System::Clock::Timeout GetWaitDuration() const override
{
return chip::System::Clock::Seconds16(mWait ? UINT16_MAX : 10);
}

static void OnValueReport(void * context, {{zapTypeToDecodableClusterObjectType type ns=parent.name isArgument=true}} value)
{
LogValue("{{asUpperCamelCase parent.name}}.{{asUpperCamelCase name}} report", 0, value);
}

private:
chip::Callback::Callback<DefaultSuccessCallback> * onSuccessCallback = new chip::Callback::Callback<DefaultSuccessCallback>(OnDefaultSuccessResponse, this);
chip::Callback::Callback<DefaultSuccessCallback> * onSuccessCallbackWithoutExit = new chip::Callback::Callback<DefaultSuccessCallback>(OnDefaultSuccessResponseWithoutExit, this);
chip::Callback::Callback<DefaultFailureCallback> * onFailureCallback = new chip::Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
chip::Callback::Callback<{{chipCallback.name}}AttributeCallback> * onReportCallback = new chip::Callback::Callback<{{chipCallback.name}}AttributeCallback>(On{{chipCallback.name}}AttributeReport, this);
uint16_t mMinInterval;
uint16_t mMaxInterval;
bool mWait;
Expand Down
Loading

0 comments on commit 1160743

Please sign in to comment.