Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Darwin] Implement multiple read and subscribe #25840

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
23dc3a6
Implement darwin multiple attribute, event read / subscribe
joonhaengHeo Mar 17, 2023
fce7525
restyle
joonhaengHeo Mar 17, 2023
46ffc58
Merge branch 'master' of github.com:joonhaengHeo/connectedhomeip into…
joonhaengHeo Mar 24, 2023
4b84448
Merge branch 'implement_darwin_multiple_read_subscribe' of github.com…
joonhaengHeo Mar 24, 2023
bc5eeb3
Merge branch 'master' of github.com:joonhaengHeo/connectedhomeip into…
joonhaengHeo Mar 25, 2023
a36a2e9
Add Exception check
joonhaengHeo Mar 27, 2023
5be3414
Remove unused code
joonhaengHeo Mar 27, 2023
2e7495e
Restyle
joonhaengHeo Mar 27, 2023
4488b7e
Merge branch 'master' into implement_darwin_multiple_read_subscribe
joonhaengHeo Mar 27, 2023
69548b2
Modify from comment
joonhaengHeo Mar 28, 2023
51c8670
Restyle
joonhaengHeo Mar 28, 2023
f8d8c69
Add MTRAttributeRequestPath, EventRequestPath
joonhaengHeo Apr 7, 2023
09d9521
restyle
joonhaengHeo Apr 7, 2023
d9c09b0
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
4b65716
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
5c4302f
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
bfc594a
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
23bdf38
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
e5d6acb
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
b084dc9
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
392e276
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
3d01392
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
00831ca
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
d4a25dc
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
9a11f72
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
4131e35
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
fc27a7c
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
d6ff44c
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
dd9dfda
Update src/darwin/Framework/CHIP/MTRBaseDevice.mm
joonhaengHeo Apr 8, 2023
b05f10e
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
465963a
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
287d75d
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 8, 2023
574120f
Update src/darwin/Framework/CHIP/MTRBaseDevice.mm
joonhaengHeo Apr 8, 2023
b8cb57d
Update src/darwin/Framework/CHIP/MTRBaseDevice.mm
joonhaengHeo Apr 8, 2023
39978a5
Update src/darwin/Framework/CHIP/MTRBaseDevice.mm
joonhaengHeo Apr 8, 2023
95ec85b
Update src/darwin/Framework/CHIP/MTRBaseDevice.mm
joonhaengHeo Apr 8, 2023
cae5d81
Update src/darwin/Framework/CHIP/MTRBaseDevice.mm
joonhaengHeo Apr 8, 2023
1160cf8
Update src/darwin/Framework/CHIP/MTRBaseDevice.mm
joonhaengHeo Apr 8, 2023
374845b
Update src/darwin/Framework/CHIP/MTRBaseDevice.mm
joonhaengHeo Apr 8, 2023
ec0434a
Update src/darwin/Framework/CHIP/MTRBaseDevice.mm
joonhaengHeo Apr 8, 2023
5981cbd
Update src/darwin/Framework/CHIP/MTRBaseDevice.mm
joonhaengHeo Apr 8, 2023
ebfbfce
fix build error
joonhaengHeo Apr 9, 2023
a26987a
restyle
joonhaengHeo Apr 9, 2023
dca26f6
Add set null eventPath
joonhaengHeo Apr 9, 2023
d6027d0
Merge branch 'implement_darwin_multiple_read_subscribe' of github.com…
joonhaengHeo Apr 9, 2023
6267b37
Modify memort allocation
joonhaengHeo Apr 11, 2023
e77bd17
restyle
joonhaengHeo Apr 11, 2023
cc4f374
Modify delete -> memoryfree
joonhaengHeo Apr 11, 2023
7144454
Update code
joonhaengHeo Apr 12, 2023
32cc8ce
remove unused code
joonhaengHeo Apr 12, 2023
7eb0bbb
restyle
joonhaengHeo Apr 12, 2023
5c9d323
Merge branch 'master' into implement_darwin_multiple_read_subscribe
joonhaengHeo Apr 12, 2023
12373e9
Fix crash in Test
joonhaengHeo Apr 12, 2023
0d0de78
Add TestCases, modify some issues
joonhaengHeo Apr 12, 2023
3adafc0
restyle
joonhaengHeo Apr 12, 2023
069c9ee
Add comment, modify testcase
joonhaengHeo Apr 12, 2023
a1cd0cc
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 12, 2023
debd9aa
Update src/darwin/Framework/CHIP/MTRBaseDevice.mm
joonhaengHeo Apr 12, 2023
ac6a7ab
Update src/darwin/Framework/CHIP/MTRBaseDevice.mm
joonhaengHeo Apr 12, 2023
580746f
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 12, 2023
bd0ff2a
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 12, 2023
3212054
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 12, 2023
48e88fd
Update src/darwin/Framework/CHIP/MTRBaseDevice.h
joonhaengHeo Apr 12, 2023
85aac30
modify code location, add some patch
joonhaengHeo Apr 12, 2023
3c7b624
modify typo
joonhaengHeo Apr 12, 2023
477a88e
Add assert code
joonhaengHeo Apr 12, 2023
c83dfd5
restyle
joonhaengHeo Apr 12, 2023
e442c8c
Update src/darwin/Framework/CHIP/MTRBaseDevice.mm
joonhaengHeo Apr 13, 2023
dd40779
Update src/darwin/Framework/CHIP/MTRBaseDevice.mm
joonhaengHeo Apr 13, 2023
994c73e
Add subscribe null check
joonhaengHeo Apr 13, 2023
3619342
Merge branch 'master' into implement_darwin_multiple_read_subscribe
joonhaengHeo Apr 13, 2023
c97da79
modify
joonhaengHeo Apr 14, 2023
40eca60
Modify using uniqur_ptr
joonhaengHeo Apr 17, 2023
f855ea5
restyle
joonhaengHeo Apr 17, 2023
fb12a69
Revert "restyle"
joonhaengHeo Apr 18, 2023
5428b0a
Revert "Modify using uniqur_ptr"
joonhaengHeo Apr 18, 2023
b8cdaf1
modify using scopeBuffer
joonhaengHeo Apr 18, 2023
d493f31
restyle
joonhaengHeo Apr 18, 2023
995d77f
Dispatch resubscription callback to the right queue.
bzbarsky-apple Apr 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 52 additions & 37 deletions src/darwin/Framework/CHIP/MTRBaseDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@
NSString * const MTRArrayValueType = @"Array";

class MTRDataValueDictionaryCallbackBridge;
template <typename DecodableValueType> class BufferedReadClientCallback;
class MTRDataValueDictionaryDecodableType;

@interface MTRReadClientContainer : NSObject
@property (nonatomic, readwrite) app::ReadClient * readClientPtr;
@property (nonatomic, readwrite) app::AttributePathParams * pathParams;
@property (nonatomic, readwrite) app::EventPathParams * eventPathParams;
@property (nonatomic, readwrite) uint64_t deviceID;
@property (nonatomic, readwrite) BufferedReadClientCallback<MTRDataValueDictionaryDecodableType> * callbackPtr;
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
- (void)onDone;
@end

Expand Down Expand Up @@ -158,6 +161,10 @@ static void PurgeReadClientContainers(
Platform::MemoryFree(container.eventPathParams);
joonhaengHeo marked this conversation as resolved.
Show resolved Hide resolved
container.eventPathParams = nullptr;
}
if (container.callbackPtr != nullptr) {
Platform::Delete(container.callbackPtr);
container.callbackPtr = nullptr;
}
}
[listToDelete removeAllObjects];
if (completion) {
Expand Down Expand Up @@ -240,6 +247,10 @@ - (void)onDone
Platform::MemoryFree(_eventPathParams);
_eventPathParams = nullptr;
}
if (_callbackPtr) {
Platform::Delete(_callbackPtr);
_callbackPtr = nullptr;
}
PurgeCompletedReadClientContainers(_deviceID);
}

Expand All @@ -259,6 +270,10 @@ - (void)dealloc
Platform::MemoryFree(_eventPathParams);
_eventPathParams = nullptr;
}
if (_callbackPtr) {
Platform::Delete(_callbackPtr);
_callbackPtr = nullptr;
}
}
@end

Expand Down Expand Up @@ -748,7 +763,7 @@ CHIP_ERROR Encode(chip::TLV::TLVWriter & writer, chip::TLV::Tag tag) const
static void OnSuccessFn(void * context, id value) { DispatchSuccess(context, value); }
};

template <typename DecodableValueType> class BufferedReadClientCallback final : public app::ReadClient::Callback {
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
template <typename DecodableValueType> class BufferedReadClientCallback : public app::ReadClient::Callback {
public:
using OnSuccessAttributeCallbackType
= std::function<void(const ConcreteAttributePath & aPath, const DecodableValueType & aData)>;
Expand All @@ -765,25 +780,36 @@ CHIP_ERROR Encode(chip::TLV::TLVWriter & writer, chip::TLV::Tag tag) const
OnErrorCallbackType aOnError, OnDoneCallbackType aOnDone,
OnSubscriptionEstablishedCallbackType aOnSubscriptionEstablished = nullptr,
OnDeviceResubscriptionScheduledCallbackType aOnDeviceResubscriptionScheduled = nullptr)
: mAttributePathParamsList(aAttributePathParamsList)
, mAttributePathParamsSize(aAttributePathParamsSize)
, mEventPathParamsList(aEventPathParamsList)
, mEventPathParamsSize(aEventPathParamsSize)
, mOnAttributeSuccess(aOnAttributeSuccess)
: mOnAttributeSuccess(aOnAttributeSuccess)
, mOnEventSuccess(aOnEventSuccess)
, mOnError(aOnError)
, mOnDone(aOnDone)
, mOnSubscriptionEstablished(aOnSubscriptionEstablished)
, mOnDeviceResubscriptionScheduled(aOnDeviceResubscriptionScheduled)
, mBufferedReadAdapter(*this)
{
if (aAttributePathParamsList != nullptr) {
mAttributePathParamsList
= Platform::MakeMemoryUnique<app::AttributePathParams>(aAttributePathParamsSize, sizeof(AttributePathParams));
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
memcpy(
mAttributePathParamsList.get(), aAttributePathParamsList, aAttributePathParamsSize * sizeof(AttributePathParams));
mAttributePathParamsSize = aAttributePathParamsSize;
}

if (aEventPathParamsList != nullptr) {
mEventPathParamsList = Platform::MakeMemoryUnique<app::EventPathParams>(aEventPathParamsSize, sizeof(EventPathParams));
memcpy(mEventPathParamsList.get(), aEventPathParamsList, aEventPathParamsSize * sizeof(EventPathParams));
mEventPathParamsSize = aEventPathParamsSize;
}
}

~BufferedReadClientCallback()
{
// Ensure we release the ReadClient before we tear down anything else,
// so it can call our OnDeallocatePaths properly.
mReadClient = nullptr;
mAttributePathParamsList = nullptr;
mEventPathParamsList = nullptr;
}

app::BufferedReadCallback & GetBufferedCallback() { return mBufferedReadAdapter; }
Expand All @@ -809,9 +835,9 @@ void OnAttributeData(

VerifyOrExit(aStatus.IsSuccess(), err = aStatus.ToChipError());
VerifyOrExit(
std::find_if(mAttributePathParamsList, mAttributePathParamsList + mAttributePathParamsSize,
std::find_if(mAttributePathParamsList.get(), mAttributePathParamsList.get() + mAttributePathParamsSize,
[aPath](app::AttributePathParams & pathParam) -> bool { return pathParam.IsAttributePathSupersetOf(aPath); })
!= mAttributePathParamsList + mAttributePathParamsSize,
!= mAttributePathParamsList.get() + mAttributePathParamsSize,
err = CHIP_ERROR_SCHEMA_MISMATCH);
VerifyOrExit(apData != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT);

Expand All @@ -834,11 +860,11 @@ void OnEventData(const EventHeader & aEventHeader, TLV::TLVReader * apData, cons

VerifyOrExit(mEventPathParamsList != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT);

VerifyOrExit(std::find_if(mEventPathParamsList, mEventPathParamsList + mEventPathParamsSize,
VerifyOrExit(std::find_if(mEventPathParamsList.get(), mEventPathParamsList.get() + mEventPathParamsSize,
[aEventHeader](app::EventPathParams & pathParam) -> bool {
return pathParam.IsEventPathSupersetOf(aEventHeader.mPath);
})
!= mEventPathParamsList + mEventPathParamsSize,
!= mEventPathParamsList.get() + mEventPathParamsSize,
err = CHIP_ERROR_SCHEMA_MISMATCH);
VerifyOrExit(apData != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT);

Expand Down Expand Up @@ -887,8 +913,8 @@ void OnDeallocatePaths(chip::app::ReadPrepareParams && aReadPrepareParams) overr
OnDeviceResubscriptionScheduledCallbackType mOnDeviceResubscriptionScheduled;
app::BufferedReadCallback mBufferedReadAdapter;
Platform::UniquePtr<app::ReadClient> mReadClient;
app::AttributePathParams * mAttributePathParamsList;
app::EventPathParams * mEventPathParamsList;
Platform::MemoryUniquePtr<app::AttributePathParams> mAttributePathParamsList;
Platform::MemoryUniquePtr<app::EventPathParams> mEventPathParamsList;
size_t mAttributePathParamsSize;
size_t mEventPathParamsSize;
};
Expand Down Expand Up @@ -979,32 +1005,25 @@ - (void)readAttributePaths:(NSArray<MTRAttributeRequestPath *> * _Nullable)attri
}
};

AttributePathParams * attributePathParamsList = nullptr;
EventPathParams * eventPathParamsList = nullptr;
Platform::MemoryUniquePtr<AttributePathParams> attributePathParamsList = nullptr;
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
Platform::MemoryUniquePtr<EventPathParams> eventPathParamsList = nullptr;

if (attributes != nil) {
size_t count = 0;
attributePathParamsList
= static_cast<AttributePathParams *>(Platform::MemoryCalloc([attributes count], sizeof(AttributePathParams)));
= Platform::MakeMemoryUnique<AttributePathParams>([attributes count], sizeof(AttributePathParams));
VerifyOrReturnError(attributePathParamsList != nullptr, CHIP_ERROR_NO_MEMORY);
for (MTRAttributeRequestPath * attribute in attributes) {
[attribute convertToAttributePathParams:attributePathParamsList[count++]];
[attribute convertToAttributePathParams:attributePathParamsList.get()[count++]];
}
}

if (events != nil) {
size_t count = 0;
eventPathParamsList
= static_cast<EventPathParams *>(Platform::MemoryCalloc([events count], sizeof(EventPathParams)));
if (eventPathParamsList == nullptr) {
if (attributePathParamsList != nullptr) {
Platform::MemoryFree(attributePathParamsList);
attributePathParamsList = nullptr;
}
return CHIP_ERROR_NO_MEMORY;
}
eventPathParamsList = Platform::MakeMemoryUnique<EventPathParams>([events count], sizeof(EventPathParams));
VerifyOrReturnError(eventPathParamsList != nullptr, CHIP_ERROR_NO_MEMORY);
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
for (MTREventRequestPath * event in events) {
[event convertToEventPathParams:eventPathParamsList[count++]];
[event convertToEventPathParams:eventPathParamsList.get()[count++]];
}
}

Expand All @@ -1013,31 +1032,25 @@ - (void)readAttributePaths:(NSArray<MTRAttributeRequestPath *> * _Nullable)attri

chip::app::ReadPrepareParams readParams(session);
[params toReadPrepareParams:readParams];
readParams.mpAttributePathParamsList = attributePathParamsList;
readParams.mpAttributePathParamsList = attributePathParamsList.get();
readParams.mAttributePathParamsListSize = [attributePaths count];
readParams.mpEventPathParamsList = eventPathParamsList;
readParams.mpEventPathParamsList = eventPathParamsList.get();
readParams.mEventPathParamsListSize = [eventPaths count];

auto onDone = [resultArray, interactionStatus, bridge, successCb, failureCb, attributePathParamsList,
eventPathParamsList](BufferedReadClientCallback<MTRDataValueDictionaryDecodableType> * callback) {
auto onDone = [resultArray, interactionStatus, bridge, successCb, failureCb](
BufferedReadClientCallback<MTRDataValueDictionaryDecodableType> * callback) {
if (*interactionStatus != CHIP_NO_ERROR) {
// Failure
failureCb(bridge, *interactionStatus);
} else {
// Success
successCb(bridge, resultArray);
}
if (attributePathParamsList != nullptr) {
Platform::MemoryFree(attributePathParamsList);
}
if (eventPathParamsList != nullptr) {
Platform::MemoryFree(eventPathParamsList);
}
chip::Platform::Delete(callback);
};

auto callback = chip::Platform::MakeUnique<BufferedReadClientCallback<MTRDataValueDictionaryDecodableType>>(
attributePathParamsList, readParams.mAttributePathParamsListSize, eventPathParamsList,
attributePathParamsList.get(), readParams.mAttributePathParamsListSize, eventPathParamsList.get(),
readParams.mEventPathParamsListSize, onAttributeSuccessCb, onEventSuccessCb, onFailureCb, onDone, nullptr);
VerifyOrReturnError(callback != nullptr, CHIP_ERROR_NO_MEMORY);

Expand Down Expand Up @@ -1437,6 +1450,7 @@ - (void)subscribeToAttributePaths:(NSArray<MTRAttributeRequestPath *> * _Nullabl
readParams.mEventPathParamsListSize = eventPathSize;

auto onDone = [container](BufferedReadClientCallback<MTRDataValueDictionaryDecodableType> * callback) {
container.callbackPtr = nullptr;
[container onDone];
// Make sure we delete callback last, because doing that actually destroys our
// lambda, so we can't access captured values after that.
Expand Down Expand Up @@ -1477,6 +1491,7 @@ - (void)subscribeToAttributePaths:(NSArray<MTRAttributeRequestPath *> * _Nullabl

// Read clients will be purged when deregistered.
container.readClientPtr = readClient;
container.callbackPtr = callback.get();
AddReadClientContainer(container.deviceID, container);
callback.release();
}];
Expand Down
15 changes: 15 additions & 0 deletions src/lib/support/CHIPMem.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,30 @@ struct Deleter
void operator()(T * p) { Delete(p); }
};

template <typename T>
struct MemoryFreer
{
void operator()(T * p) { MemoryFree(p); }
};

template <typename T>
using UniquePtr = std::unique_ptr<T, Deleter<T>>;

template <typename T>
using MemoryUniquePtr = std::unique_ptr<T, MemoryFreer<T>>;

template <typename T, typename... Args>
inline UniquePtr<T> MakeUnique(Args &&... args)
{
return UniquePtr<T>(New<T>(std::forward<Args>(args)...));
}

template <typename T>
inline MemoryUniquePtr<T> MakeMemoryUnique(size_t num, size_t size)
{
return MemoryUniquePtr<T>(static_cast<T *>(MemoryCalloc(num, size)));
}

template <typename T>
using SharedPtr = std::shared_ptr<T>;

Expand Down