From 0f776d18a66dc2c7cc81741d295f437352ca4822 Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Wed, 12 May 2021 12:23:42 -0700 Subject: [PATCH] Always send message using strongly typed message type (#6648) --- src/app/util/chip-message-send.cpp | 3 +- src/controller/CHIPCluster.cpp | 3 +- src/controller/CHIPDevice.cpp | 3 +- src/controller/CHIPDevice.h | 11 ++++ .../java/CHIPDeviceController-JNI.cpp | 3 +- .../ServiceProvisioning.h | 57 ++++++++++++++++++ src/protocols/temp_zcl/TempZCL.h | 58 +++++++++++++++++++ 7 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 src/protocols/service_provisioning/ServiceProvisioning.h create mode 100644 src/protocols/temp_zcl/TempZCL.h diff --git a/src/app/util/chip-message-send.cpp b/src/app/util/chip-message-send.cpp index f9b02ce6306f43..c5fab58b4e6068 100644 --- a/src/app/util/chip-message-send.cpp +++ b/src/app/util/chip-message-send.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -87,7 +88,7 @@ EmberStatus chipSendUnicast(Messaging::ExchangeContext * exchange, EmberApsFrame memcpy(buffer->Start() + frameSize, message, messageLength); buffer->SetDataLength(dataLength); - CHIP_ERROR err = exchange->SendMessage(Protocols::TempZCL::Id, 0, std::move(buffer), sendFlags); + CHIP_ERROR err = exchange->SendMessage(Protocols::TempZCL::MsgType::TempZCLResponse, std::move(buffer), sendFlags); if (err != CHIP_NO_ERROR) { diff --git a/src/controller/CHIPCluster.cpp b/src/controller/CHIPCluster.cpp index 09ce93bf3ffa8f..51bf0a5e6391cd 100644 --- a/src/controller/CHIPCluster.cpp +++ b/src/controller/CHIPCluster.cpp @@ -25,6 +25,7 @@ */ #include +#include namespace chip { namespace Controller { @@ -57,7 +58,7 @@ CHIP_ERROR ClusterBase::SendCommand(uint8_t seqNum, chip::System::PacketBufferHa mDevice->AddResponseHandler(seqNum, onSuccessCallback, onFailureCallback); } - err = mDevice->SendMessage(Protocols::TempZCL::Id, 0, std::move(payload)); + err = mDevice->SendMessage(Protocols::TempZCL::MsgType::TempZCLRequest, std::move(payload)); SuccessOrExit(err); exit: diff --git a/src/controller/CHIPDevice.cpp b/src/controller/CHIPDevice.cpp index 56997b956e89fe..dbe17799d4adc0 100644 --- a/src/controller/CHIPDevice.cpp +++ b/src/controller/CHIPDevice.cpp @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -313,7 +314,7 @@ CHIP_ERROR Device::OpenPairingWindow(uint32_t timeout, PairingWindowOption optio System::PacketBufferHandle outBuffer; ReturnErrorOnFailure(writer.Finalize(&outBuffer)); - ReturnErrorOnFailure(SendMessage(Protocols::ServiceProvisioning::Id, 0, std::move(outBuffer))); + ReturnErrorOnFailure(SendMessage(Protocols::ServiceProvisioning::MsgType::ServiceProvisioningRequest, std::move(outBuffer))); setupPayload.version = 0; setupPayload.rendezvousInformation = RendezvousInformationFlags(RendezvousInformationFlag::kBLE); diff --git a/src/controller/CHIPDevice.h b/src/controller/CHIPDevice.h index 52a29c8349d6af..95e1a2da453c4a 100644 --- a/src/controller/CHIPDevice.h +++ b/src/controller/CHIPDevice.h @@ -130,6 +130,17 @@ class DLL_EXPORT Device : public Messaging::ExchangeDelegate */ CHIP_ERROR SendMessage(Protocols::Id protocolId, uint8_t msgType, System::PacketBufferHandle message); + /** + * A strongly-message-typed version of SendMessage. + */ + template ::value>> + CHIP_ERROR SendMessage(MessageType msgType, System::PacketBufferHandle && message) + { + static_assert(std::is_same, uint8_t>::value, "Enum is wrong size; cast is not safe"); + return SendMessage(Protocols::MessageTypeTraits::ProtocolId(), static_cast(msgType), + std::move(message)); + } + /** * @brief * Send the command in internal command sender. diff --git a/src/controller/java/CHIPDeviceController-JNI.cpp b/src/controller/java/CHIPDeviceController-JNI.cpp index 626d6d435e05d6..0d7931af4049ad 100644 --- a/src/controller/java/CHIPDeviceController-JNI.cpp +++ b/src/controller/java/CHIPDeviceController-JNI.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -439,7 +440,7 @@ JNI_METHOD(void, sendMessage)(JNIEnv * env, jobject self, jlong handle, jlong de } else { - err = chipDevice->SendMessage(Protocols::TempZCL::Id, 0, std::move(buffer)); + err = chipDevice->SendMessage(Protocols::TempZCL::MsgType::TempZCLRequest, std::move(buffer)); } } diff --git a/src/protocols/service_provisioning/ServiceProvisioning.h b/src/protocols/service_provisioning/ServiceProvisioning.h new file mode 100644 index 00000000000000..d059e721da98e2 --- /dev/null +++ b/src/protocols/service_provisioning/ServiceProvisioning.h @@ -0,0 +1,57 @@ +/* + * + * Copyright (c) 2021 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. + */ + +/** + * @file + * This file defines message types for ServiceProvisioning protocol. + * + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace chip { +namespace Protocols { +namespace ServiceProvisioning { + +/** + * ServiceProvisioning Protocol Message Types + */ +enum class MsgType : uint8_t +{ + ServiceProvisioningRequest = 0x01, +}; + +} // namespace ServiceProvisioning + +template <> +struct MessageTypeTraits +{ + static constexpr const Protocols::Id & ProtocolId() { return ServiceProvisioning::Id; } +}; + +} // namespace Protocols +} // namespace chip diff --git a/src/protocols/temp_zcl/TempZCL.h b/src/protocols/temp_zcl/TempZCL.h new file mode 100644 index 00000000000000..793339c7415eaa --- /dev/null +++ b/src/protocols/temp_zcl/TempZCL.h @@ -0,0 +1,58 @@ +/* + * + * Copyright (c) 2021 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. + */ + +/** + * @file + * This file defines message types for TempZCL protocol. + * + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace chip { +namespace Protocols { +namespace TempZCL { + +/** + * TempZCL Protocol Message Types + */ +enum class MsgType : uint8_t +{ + TempZCLRequest = 0x01, + TempZCLResponse = 0x02 +}; + +} // namespace TempZCL + +template <> +struct MessageTypeTraits +{ + static constexpr const Protocols::Id & ProtocolId() { return TempZCL::Id; } +}; + +} // namespace Protocols +} // namespace chip