Skip to content

Commit

Permalink
Add APIs of Channel in message layer (#4019)
Browse files Browse the repository at this point in the history
* Add APIs of Channel in message layer

* fix comments

* Resolve comments

* Fix comments

* Restyled by clang-format

* Restyled by clang-format

* Fix comments

* Fix conflict

* Restyled by clang-format

* Resolve conflict

* Restyled by clang-format

* Remove PASE pairing from Channel

* Split test-cases into another file

* Restyled by clang-format

* Resolve comments

* Fix CI

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed Mar 9, 2021
1 parent ce7389f commit 2319985
Show file tree
Hide file tree
Showing 23 changed files with 1,273 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/app/tests/integration/chip_im_initiator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ int main(int argc, char * argv[])
err = gSessionManager.Init(chip::kTestControllerNodeId, &chip::DeviceLayer::SystemLayer, &gTransportManager, &admins);
SuccessOrExit(err);

err = gExchangeManager.Init(&gSessionManager);
err = gExchangeManager.Init(chip::kTestControllerNodeId, &gTransportManager, &gSessionManager);
SuccessOrExit(err);

err = chip::app::InteractionModelEngine::GetInstance()->Init(&gExchangeManager);
Expand Down
2 changes: 1 addition & 1 deletion src/app/tests/integration/chip_im_responder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ int main(int argc, char * argv[])
err = gSessionManager.Init(chip::kTestDeviceNodeId, &chip::DeviceLayer::SystemLayer, &gTransportManager, &admins);
SuccessOrExit(err);

err = gExchangeManager.Init(&gSessionManager);
err = gExchangeManager.Init(chip::kTestDeviceNodeId, &gTransportManager, &gSessionManager);
SuccessOrExit(err);

err = chip::app::InteractionModelEngine::GetInstance()->Init(&gExchangeManager);
Expand Down
31 changes: 31 additions & 0 deletions src/lib/core/CHIPConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,37 @@
#define CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS 16
#endif // CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS

/**
* @def CHIP_CONFIG_MAX_ACTIVE_CHANNELS
*
* @brief
* Maximum number of simultaneously active channels
*/
#ifndef CHIP_CONFIG_MAX_ACTIVE_CHANNELS
#define CHIP_CONFIG_MAX_ACTIVE_CHANNELS 16
#endif // CHIP_CONFIG_MAX_ACTIVE_CHANNELS

/**
* @def CHIP_CONFIG_MAX_CHANNEL_HANDLES
*
* @brief
* Maximum number of channel handles
*/
#ifndef CHIP_CONFIG_MAX_CHANNEL_HANDLES
#define CHIP_CONFIG_MAX_CHANNEL_HANDLES 64
#endif // CHIP_CONFIG_MAX_CHANNEL_HANDLES

/**
* @def CHIP_CONFIG_NODE_ADDRESS_RESOLVE_TIMEOUT_MSECS
*
* @brief
* This is the default timeout for node addres resolve over mDNS
*
*/
#ifndef CHIP_CONFIG_NODE_ADDRESS_RESOLVE_TIMEOUT_MSECS
#define CHIP_CONFIG_NODE_ADDRESS_RESOLVE_TIMEOUT_MSECS (5000)
#endif // CHIP_CONFIG_NODE_ADDRESS_RESOLVE_TIMEOUT_MSECS

/**
* @def CHIP_CONFIG_CONNECT_IP_ADDRS
*
Expand Down
3 changes: 3 additions & 0 deletions src/lib/core/CHIPError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,9 @@ bool FormatCHIPError(char * buf, uint16_t bufSize, int32_t err)
case CHIP_ERROR_IM_MALFORMED_STATUS_CODE:
desc = "Malformed Interacton Model Status Code";
break;
case CHIP_ERROR_PEER_NODE_NOT_FOUND:
desc = "Unable to find the peer node";
break;
}
#endif // !CHIP_CONFIG_SHORT_ERROR_STR

Expand Down
8 changes: 8 additions & 0 deletions src/lib/core/CHIPError.h
Original file line number Diff line number Diff line change
Expand Up @@ -1761,6 +1761,14 @@ typedef CHIP_CONFIG_ERROR_TYPE CHIP_ERROR;
*/
#define CHIP_ERROR_IM_MALFORMED_STATUS_CODE _CHIP_ERROR(187)

/**
* @def CHIP_ERROR_PEER_NODE_NOT_FOUND
*
* @brief
* Unable to find the peer node
*/
#define CHIP_ERROR_PEER_NODE_NOT_FOUND _CHIP_ERROR(188)

/**
* @}
*/
Expand Down
1 change: 1 addition & 0 deletions src/lib/core/tests/TestCHIPErrorStr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ static int32_t sContext[] =
CHIP_ERROR_IM_MALFORMED_COMMAND_DATA_ELEMENT,
CHIP_ERROR_IM_MALFORMED_EVENT_DATA_ELEMENT,
CHIP_ERROR_IM_MALFORMED_STATUS_CODE,
CHIP_ERROR_PEER_NODE_NOT_FOUND,
};
// clang-format on

Expand Down
36 changes: 31 additions & 5 deletions src/lib/support/Pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ class StaticAllocatorBitmap : public StaticAllocatorBase
void * Allocate();
void Deallocate(void * element);

// Test-only function declaration
template <typename F>
void ForEachActiveObject(F f);

private:
protected:
void * At(size_t index) { return static_cast<uint8_t *>(mElements) + mElementSize * index; }
size_t IndexOf(void * element)
{
Expand Down Expand Up @@ -119,6 +115,36 @@ class BitMapObjectPool : public StaticAllocatorBitmap
Deallocate(element);
}

/**
* @brief
* Run a functor for each active object in the pool
*
* @param f The functor of type `bool (*)(T*)`, return false to break the iteration
* @return bool Returns false if broke during iteration
*
* caution
* this function is not thread-safe, make sure all usage of the
* pool is protected by a lock, or else avoid using this function
*/
template <typename F>
bool ForEachActiveObject(F f)
{
for (size_t word = 0; word * kBitChunkSize < Capacity(); ++word)
{
auto & usage = mUsage[word];
auto value = usage.load(std::memory_order_relaxed);
for (size_t offset = 0; offset < kBitChunkSize && offset + word * kBitChunkSize < Capacity(); ++offset)
{
if ((value & (kBit1 << offset)) != 0)
{
if (!f(static_cast<T *>(At(word * kBitChunkSize + offset))))
return false;
}
}
}
return true;
}

private:
std::atomic<tBitChunkType> mUsage[(N + kBitChunkSize - 1) / kBitChunkSize];
alignas(alignof(T)) uint8_t mMemory[N * sizeof(T)];
Expand Down
22 changes: 4 additions & 18 deletions src/lib/support/tests/TestPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,14 @@

namespace chip {

template <typename F>
void StaticAllocatorBitmap::ForEachActiveObject(F f)
{
for (size_t word = 0; word * kBitChunkSize < Capacity(); ++word)
{
auto & usage = mUsage[word];
auto value = usage.load(std::memory_order_relaxed);
for (size_t offset = 0; offset < kBitChunkSize && offset + word * kBitChunkSize < Capacity(); ++offset)
{
if ((value & (kBit1 << offset)) != 0)
{
f(At(word * kBitChunkSize + offset));
}
}
}
}

template <class T, size_t N>
size_t GetNumObjectsInUse(BitMapObjectPool<T, N> & pool)
{
size_t count = 0;
pool.ForEachActiveObject([&count](void *) { ++count; });
pool.ForEachActiveObject([&count](void *) {
++count;
return true;
});
return count;
}

Expand Down
4 changes: 4 additions & 0 deletions src/messaging/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ static_library("messaging") {
output_name = "libMessagingLayer"

sources = [
"Channel.cpp",
"Channel.h",
"ChannelContext.cpp",
"ChannelContext.h",
"ErrorCategory.cpp",
"ErrorCategory.h",
"ExchangeACL.h",
Expand Down
48 changes: 48 additions & 0 deletions src/messaging/Channel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* 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 <messaging/Channel.h>
#include <messaging/ChannelContext.h>
#include <messaging/ExchangeMgr.h>

namespace chip {
namespace Messaging {

ChannelState ChannelHandle::GetState() const
{
if (mAssociation == nullptr)
return ChannelState::kNone;
return mAssociation->mChannelContext->GetState();
}

ExchangeContext * ChannelHandle::NewExchange(ExchangeDelegate * delegate)
{
assert(mAssociation != nullptr);
return mAssociation->mChannelContext->NewExchange(delegate);
}

void ChannelHandle::Release()
{
if (mAssociation == nullptr)
return;

mAssociation->mChannelContext->mExchangeManager->ReleaseChannelHandle(mAssociation);
mAssociation = nullptr;
}

} // namespace Messaging
} // namespace chip
Loading

0 comments on commit 2319985

Please sign in to comment.