Skip to content

Commit

Permalink
Always send message using strongly typed message type (#6648)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca authored May 12, 2021
1 parent 442d255 commit 0f776d1
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/app/util/chip-message-send.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <messaging/ExchangeContext.h>
#include <messaging/ExchangeMgr.h>
#include <protocols/Protocols.h>
#include <protocols/temp_zcl/TempZCL.h>
#include <support/logging/CHIPLogging.h>
#include <transport/raw/MessageHeader.h>

Expand Down Expand Up @@ -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)
{
Expand Down
3 changes: 2 additions & 1 deletion src/controller/CHIPCluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

#include <controller/CHIPCluster.h>
#include <protocols/temp_zcl/TempZCL.h>

namespace chip {
namespace Controller {
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion src/controller/CHIPDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <core/CHIPEncoding.h>
#include <core/CHIPSafeCasts.h>
#include <protocols/Protocols.h>
#include <protocols/service_provisioning/ServiceProvisioning.h>
#include <support/Base64.h>
#include <support/CHIPMem.h>
#include <support/CodeUtils.h>
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions src/controller/CHIPDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename MessageType, typename = std::enable_if_t<std::is_enum<MessageType>::value>>
CHIP_ERROR SendMessage(MessageType msgType, System::PacketBufferHandle && message)
{
static_assert(std::is_same<std::underlying_type_t<MessageType>, uint8_t>::value, "Enum is wrong size; cast is not safe");
return SendMessage(Protocols::MessageTypeTraits<MessageType>::ProtocolId(), static_cast<uint8_t>(msgType),
std::move(message));
}

/**
* @brief
* Send the command in internal command sender.
Expand Down
3 changes: 2 additions & 1 deletion src/controller/java/CHIPDeviceController-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <jni.h>
#include <platform/KeyValueStoreManager.h>
#include <protocols/Protocols.h>
#include <protocols/temp_zcl/TempZCL.h>
#include <pthread.h>
#include <support/CHIPMem.h>
#include <support/CodeUtils.h>
Expand Down Expand Up @@ -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));
}
}

Expand Down
57 changes: 57 additions & 0 deletions src/protocols/service_provisioning/ServiceProvisioning.h
Original file line number Diff line number Diff line change
@@ -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 <core/CHIPCore.h>
#include <messaging/ExchangeContext.h>
#include <messaging/ExchangeMgr.h>
#include <messaging/Flags.h>
#include <protocols/Protocols.h>
#include <support/CodeUtils.h>
#include <support/DLLUtil.h>
#include <support/logging/CHIPLogging.h>

namespace chip {
namespace Protocols {
namespace ServiceProvisioning {

/**
* ServiceProvisioning Protocol Message Types
*/
enum class MsgType : uint8_t
{
ServiceProvisioningRequest = 0x01,
};

} // namespace ServiceProvisioning

template <>
struct MessageTypeTraits<ServiceProvisioning::MsgType>
{
static constexpr const Protocols::Id & ProtocolId() { return ServiceProvisioning::Id; }
};

} // namespace Protocols
} // namespace chip
58 changes: 58 additions & 0 deletions src/protocols/temp_zcl/TempZCL.h
Original file line number Diff line number Diff line change
@@ -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 <core/CHIPCore.h>
#include <messaging/ExchangeContext.h>
#include <messaging/ExchangeMgr.h>
#include <messaging/Flags.h>
#include <protocols/Protocols.h>
#include <support/CodeUtils.h>
#include <support/DLLUtil.h>
#include <support/logging/CHIPLogging.h>

namespace chip {
namespace Protocols {
namespace TempZCL {

/**
* TempZCL Protocol Message Types
*/
enum class MsgType : uint8_t
{
TempZCLRequest = 0x01,
TempZCLResponse = 0x02
};

} // namespace TempZCL

template <>
struct MessageTypeTraits<TempZCL::MsgType>
{
static constexpr const Protocols::Id & ProtocolId() { return TempZCL::Id; }
};

} // namespace Protocols
} // namespace chip

0 comments on commit 0f776d1

Please sign in to comment.