blob: 8eae6613ddf10e81ffa270b38b656d625858a720 [file] [log] [blame]
Jerry Johns4efc5822021-10-19 14:22:36 -07001/*
2 *
3 * Copyright (c) 2021 Project CHIP Authors
4 * All rights reserved.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#pragma once
20
Michael Spangc6b6fb32023-11-17 02:21:20 -050021#include <app/AppConfig.h>
Jerry Johns4efc5822021-10-19 14:22:36 -070022#include <app/AttributePathParams.h>
23#include <app/InteractionModelEngine.h>
24#include <app/ReadPrepareParams.h>
25#include <controller/TypedReadCallback.h>
26
Wang Qixiang14bdc242023-08-04 13:04:55 +080027#if CHIP_CONFIG_ENABLE_READ_CLIENT
Jerry Johns4efc5822021-10-19 14:22:36 -070028namespace chip {
29namespace Controller {
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -050030namespace detail {
Jerry Johns4efc5822021-10-19 14:22:36 -070031
Jeff Tungad58d802022-10-26 07:37:36 -070032using SubscriptionOnDoneCallback = std::function<void(void)>;
33
Boris Zbarsky9964cdb2021-11-10 17:12:24 -050034template <typename DecodableAttributeType>
Boris Zbarsky5018ff92021-12-10 00:38:45 -050035struct ReportAttributeParams : public app::ReadPrepareParams
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -050036{
Marc Lepage9a595a42021-12-16 03:53:52 -050037 ReportAttributeParams(const SessionHandle & sessionHandle) : app::ReadPrepareParams(sessionHandle)
38 {
39 mKeepSubscriptions = false;
40 }
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -050041 typename TypedReadAttributeCallback<DecodableAttributeType>::OnSuccessCallbackType mOnReportCb;
42 typename TypedReadAttributeCallback<DecodableAttributeType>::OnErrorCallbackType mOnErrorCb;
43 typename TypedReadAttributeCallback<DecodableAttributeType>::OnSubscriptionEstablishedCallbackType
yunhanw-googledce80072022-06-08 13:16:49 -070044 mOnSubscriptionEstablishedCb = nullptr;
45 typename TypedReadAttributeCallback<DecodableAttributeType>::OnResubscriptionAttemptCallbackType mOnResubscriptionAttemptCb =
46 nullptr;
Jeff Tungad58d802022-10-26 07:37:36 -070047 SubscriptionOnDoneCallback mOnDoneCb = nullptr;
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -050048 app::ReadClient::InteractionType mReportType = app::ReadClient::InteractionType::Read;
49};
50
51template <typename DecodableAttributeType>
52CHIP_ERROR ReportAttribute(Messaging::ExchangeManager * exchangeMgr, EndpointId endpointId, ClusterId clusterId,
yunhanw-google0edb9f92022-02-11 00:50:59 -080053 AttributeId attributeId, ReportAttributeParams<DecodableAttributeType> && readParams,
54 const Optional<DataVersion> & aDataVersion = NullOptional)
Jerry Johns4efc5822021-10-19 14:22:36 -070055{
Jerry Johns4efc5822021-10-19 14:22:36 -070056 app::InteractionModelEngine * engine = app::InteractionModelEngine::GetInstance();
57 CHIP_ERROR err = CHIP_NO_ERROR;
58
yunhanw-google2cd1e1e2022-01-29 16:20:00 -080059 auto readPaths = Platform::MakeUnique<app::AttributePathParams>(endpointId, clusterId, attributeId);
60 VerifyOrReturnError(readPaths != nullptr, CHIP_ERROR_NO_MEMORY);
61 readParams.mpAttributePathParamsList = readPaths.get();
Jerry Johns4efc5822021-10-19 14:22:36 -070062 readParams.mAttributePathParamsListSize = 1;
yunhanw-google0edb9f92022-02-11 00:50:59 -080063 chip::Platform::UniquePtr<chip::app::DataVersionFilter> dataVersionFilters;
64 if (aDataVersion.HasValue())
65 {
66 dataVersionFilters = Platform::MakeUnique<app::DataVersionFilter>(endpointId, clusterId, aDataVersion.Value());
67 VerifyOrReturnError(dataVersionFilters != nullptr, CHIP_ERROR_NO_MEMORY);
68 readParams.mpDataVersionFilterList = dataVersionFilters.get();
69 readParams.mDataVersionFilterListSize = 1;
70 }
Jeff Tungad58d802022-10-26 07:37:36 -070071 auto onDoneCb = readParams.mOnDoneCb;
72 auto onDone = [onDoneCb](TypedReadAttributeCallback<DecodableAttributeType> * callback) {
73 if (onDoneCb)
74 {
75 onDoneCb();
76 }
77 chip::Platform::Delete(callback);
78 };
Jerry Johns4efc5822021-10-19 14:22:36 -070079
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -050080 auto callback = chip::Platform::MakeUnique<TypedReadAttributeCallback<DecodableAttributeType>>(
yunhanw-googledce80072022-06-08 13:16:49 -070081 clusterId, attributeId, readParams.mOnReportCb, readParams.mOnErrorCb, onDone, readParams.mOnSubscriptionEstablishedCb,
82 readParams.mOnResubscriptionAttemptCb);
yunhanw-googlef3d6b7c2021-11-17 13:42:38 -080083 VerifyOrReturnError(callback != nullptr, CHIP_ERROR_NO_MEMORY);
84
Jerry Johns118ab9d2021-12-21 18:01:54 -080085 auto readClient =
86 chip::Platform::MakeUnique<app::ReadClient>(engine, exchangeMgr, callback->GetBufferedCallback(), readParams.mReportType);
yunhanw-google2cd1e1e2022-01-29 16:20:00 -080087 VerifyOrReturnError(readClient != nullptr, CHIP_ERROR_NO_MEMORY);
yunhanw-googlef3d6b7c2021-11-17 13:42:38 -080088
yunhanw-google2cd1e1e2022-01-29 16:20:00 -080089 if (readClient->IsSubscriptionType())
90 {
91 readPaths.release();
yunhanw-google0edb9f92022-02-11 00:50:59 -080092 dataVersionFilters.release();
93
yunhanw-google2cd1e1e2022-01-29 16:20:00 -080094 err = readClient->SendAutoResubscribeRequest(std::move(readParams));
95 ReturnErrorOnFailure(err);
96 }
97 else
98 {
99 err = readClient->SendRequest(readParams);
100 ReturnErrorOnFailure(err);
101 }
yunhanw-googlef3d6b7c2021-11-17 13:42:38 -0800102
103 //
104 // At this point, we'll get a callback through the OnDone callback above regardless of success or failure
105 // of the read operation to permit us to free up the callback object. So, release ownership of the callback
106 // object now to prevent it from being reclaimed at the end of this scoped block.
107 //
Michael Spanga2f17432022-01-26 20:52:14 -0500108 callback->AdoptReadClient(std::move(readClient));
yunhanw-googlef3d6b7c2021-11-17 13:42:38 -0800109 callback.release();
Jerry Johns118ab9d2021-12-21 18:01:54 -0800110
yunhanw-googlef3d6b7c2021-11-17 13:42:38 -0800111 return err;
112}
113
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -0500114} // namespace detail
115
116/**
117 * To avoid instantiating all the complicated read code on a per-attribute
118 * basis, we have a helper that's just templated on the type.
119 */
120template <typename DecodableAttributeType>
Marc Lepage9a595a42021-12-16 03:53:52 -0500121CHIP_ERROR ReadAttribute(Messaging::ExchangeManager * exchangeMgr, const SessionHandle & sessionHandle, EndpointId endpointId,
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -0500122 ClusterId clusterId, AttributeId attributeId,
123 typename TypedReadAttributeCallback<DecodableAttributeType>::OnSuccessCallbackType onSuccessCb,
Song GUOe17d7672022-01-22 10:11:27 +0800124 typename TypedReadAttributeCallback<DecodableAttributeType>::OnErrorCallbackType onErrorCb,
yunhanw-googledbc91b72022-05-27 11:08:03 -0700125 bool fabricFiltered = true)
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -0500126{
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500127 detail::ReportAttributeParams<DecodableAttributeType> params(sessionHandle);
Song GUOe17d7672022-01-22 10:11:27 +0800128 params.mOnReportCb = onSuccessCb;
129 params.mOnErrorCb = onErrorCb;
130 params.mIsFabricFiltered = fabricFiltered;
yunhanw-googledbc91b72022-05-27 11:08:03 -0700131 return detail::ReportAttribute(exchangeMgr, endpointId, clusterId, attributeId, std::move(params), NullOptional);
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -0500132}
133
134/*
135 * A typed read attribute function that takes as input a template parameter that encapsulates the type information
136 * for a given attribute as well as callbacks for success and failure and either returns a decoded cluster-object representation
137 * of the requested attribute through the provided success callback or calls the provided failure callback.
138 *
139 * The AttributeTypeInfo is generally expected to be a ClusterName::Attributes::AttributeName::TypeInfo struct, but any
140 * object that contains type information exposed through a 'DecodableType' type declaration as well as GetClusterId() and
141 * GetAttributeId() methods is expected to work.
142 */
143template <typename AttributeTypeInfo>
144CHIP_ERROR
Marc Lepage9a595a42021-12-16 03:53:52 -0500145ReadAttribute(Messaging::ExchangeManager * exchangeMgr, const SessionHandle & sessionHandle, EndpointId endpointId,
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -0500146 typename TypedReadAttributeCallback<typename AttributeTypeInfo::DecodableType>::OnSuccessCallbackType onSuccessCb,
Song GUOe17d7672022-01-22 10:11:27 +0800147 typename TypedReadAttributeCallback<typename AttributeTypeInfo::DecodableType>::OnErrorCallbackType onErrorCb,
yunhanw-googledbc91b72022-05-27 11:08:03 -0700148 bool fabricFiltered = true)
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -0500149{
Song GUOe17d7672022-01-22 10:11:27 +0800150 return ReadAttribute<typename AttributeTypeInfo::DecodableType>(
151 exchangeMgr, sessionHandle, endpointId, AttributeTypeInfo::GetClusterId(), AttributeTypeInfo::GetAttributeId(), onSuccessCb,
yunhanw-googledbc91b72022-05-27 11:08:03 -0700152 onErrorCb, fabricFiltered);
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -0500153}
154
155// Helper for SubscribeAttribute to reduce the amount of code generated.
156template <typename DecodableAttributeType>
yunhanw-google0edb9f92022-02-11 00:50:59 -0800157CHIP_ERROR SubscribeAttribute(
158 Messaging::ExchangeManager * exchangeMgr, const SessionHandle & sessionHandle, EndpointId endpointId, ClusterId clusterId,
159 AttributeId attributeId, typename TypedReadAttributeCallback<DecodableAttributeType>::OnSuccessCallbackType onReportCb,
160 typename TypedReadAttributeCallback<DecodableAttributeType>::OnErrorCallbackType onErrorCb, uint16_t minIntervalFloorSeconds,
161 uint16_t maxIntervalCeilingSeconds,
162 typename TypedReadAttributeCallback<DecodableAttributeType>::OnSubscriptionEstablishedCallbackType onSubscriptionEstablishedCb =
163 nullptr,
yunhanw-googledce80072022-06-08 13:16:49 -0700164 typename TypedReadAttributeCallback<DecodableAttributeType>::OnResubscriptionAttemptCallbackType onResubscriptionAttemptCb =
165 nullptr,
Jeff Tungad58d802022-10-26 07:37:36 -0700166 bool fabricFiltered = true, bool keepPreviousSubscriptions = false, const Optional<DataVersion> & aDataVersion = NullOptional,
167 typename detail::SubscriptionOnDoneCallback onDoneCb = nullptr)
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -0500168{
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500169 detail::ReportAttributeParams<DecodableAttributeType> params(sessionHandle);
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -0500170 params.mOnReportCb = onReportCb;
171 params.mOnErrorCb = onErrorCb;
172 params.mOnSubscriptionEstablishedCb = onSubscriptionEstablishedCb;
yunhanw-googledce80072022-06-08 13:16:49 -0700173 params.mOnResubscriptionAttemptCb = onResubscriptionAttemptCb;
Jeff Tungad58d802022-10-26 07:37:36 -0700174 params.mOnDoneCb = onDoneCb;
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -0500175 params.mMinIntervalFloorSeconds = minIntervalFloorSeconds;
176 params.mMaxIntervalCeilingSeconds = maxIntervalCeilingSeconds;
Jerry Johns12db26d2022-01-26 10:06:04 -0800177 params.mKeepSubscriptions = keepPreviousSubscriptions;
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -0500178 params.mReportType = app::ReadClient::InteractionType::Subscribe;
Song GUOe17d7672022-01-22 10:11:27 +0800179 params.mIsFabricFiltered = fabricFiltered;
yunhanw-google0edb9f92022-02-11 00:50:59 -0800180 return detail::ReportAttribute(exchangeMgr, endpointId, clusterId, attributeId, std::move(params), aDataVersion);
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -0500181}
182
183/*
184 * A typed way to subscribe to the value of a single attribute. See
185 * documentation for ReadAttribute above for details on how AttributeTypeInfo
186 * works.
Jerry Johnsb697b292022-03-29 22:24:11 -0700187 *
188 * A const view-only reference to the underlying ReadClient is passed in through the OnSubscriptionEstablishedCallbackType
189 * argument. This reference is valid until the error callback is invoked at which point, this reference is no longer valid
190 * and should not be used any more.
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -0500191 */
192template <typename AttributeTypeInfo>
193CHIP_ERROR SubscribeAttribute(
Marc Lepage9a595a42021-12-16 03:53:52 -0500194 Messaging::ExchangeManager * exchangeMgr, const SessionHandle & sessionHandle, EndpointId endpointId,
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -0500195 typename TypedReadAttributeCallback<typename AttributeTypeInfo::DecodableType>::OnSuccessCallbackType onReportCb,
196 typename TypedReadAttributeCallback<typename AttributeTypeInfo::DecodableType>::OnErrorCallbackType onErrorCb,
197 uint16_t aMinIntervalFloorSeconds, uint16_t aMaxIntervalCeilingSeconds,
198 typename TypedReadAttributeCallback<typename AttributeTypeInfo::DecodableType>::OnSubscriptionEstablishedCallbackType
Song GUOe17d7672022-01-22 10:11:27 +0800199 onSubscriptionEstablishedCb = nullptr,
yunhanw-googledce80072022-06-08 13:16:49 -0700200 typename TypedReadAttributeCallback<typename AttributeTypeInfo::DecodableType>::OnResubscriptionAttemptCallbackType
201 onResubscriptionAttemptCb = nullptr,
Jeff Tungad58d802022-10-26 07:37:36 -0700202 bool fabricFiltered = true, bool keepPreviousSubscriptions = false, const Optional<DataVersion> & aDataVersion = NullOptional,
203 typename detail::SubscriptionOnDoneCallback onDoneCb = nullptr)
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -0500204{
205 return SubscribeAttribute<typename AttributeTypeInfo::DecodableType>(
206 exchangeMgr, sessionHandle, endpointId, AttributeTypeInfo::GetClusterId(), AttributeTypeInfo::GetAttributeId(), onReportCb,
yunhanw-googledce80072022-06-08 13:16:49 -0700207 onErrorCb, aMinIntervalFloorSeconds, aMaxIntervalCeilingSeconds, onSubscriptionEstablishedCb, onResubscriptionAttemptCb,
Jeff Tungad58d802022-10-26 07:37:36 -0700208 fabricFiltered, keepPreviousSubscriptions, aDataVersion, onDoneCb);
Boris Zbarskya8b7f4b2021-12-09 09:14:58 -0500209}
210
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500211namespace detail {
212
yunhanw-google5c705302021-12-06 17:41:52 -0800213template <typename DecodableEventType>
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500214struct ReportEventParams : public app::ReadPrepareParams
215{
Boris Zbarsky772c0602022-03-02 12:40:35 -0500216 ReportEventParams(const SessionHandle & sessionHandle) : app::ReadPrepareParams(sessionHandle) {}
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500217 typename TypedReadEventCallback<DecodableEventType>::OnSuccessCallbackType mOnReportCb;
218 typename TypedReadEventCallback<DecodableEventType>::OnErrorCallbackType mOnErrorCb;
yunhanw-googledbc91b72022-05-27 11:08:03 -0700219 typename TypedReadEventCallback<DecodableEventType>::OnDoneCallbackType mOnDoneCb;
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500220 typename TypedReadEventCallback<DecodableEventType>::OnSubscriptionEstablishedCallbackType mOnSubscriptionEstablishedCb =
221 nullptr;
yunhanw-googledce80072022-06-08 13:16:49 -0700222 typename TypedReadEventCallback<DecodableEventType>::OnResubscriptionAttemptCallbackType mOnResubscriptionAttemptCb = nullptr;
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500223 app::ReadClient::InteractionType mReportType = app::ReadClient::InteractionType::Read;
224};
225
226template <typename DecodableEventType>
227CHIP_ERROR ReportEvent(Messaging::ExchangeManager * apExchangeMgr, EndpointId endpointId,
yunhanw-googlecd44f9b2022-03-09 10:36:31 -0800228 ReportEventParams<DecodableEventType> && readParams, bool aIsUrgentEvent)
yunhanw-googlef3d6b7c2021-11-17 13:42:38 -0800229{
yunhanw-google2cd1e1e2022-01-29 16:20:00 -0800230 ClusterId clusterId = DecodableEventType::GetClusterId();
231 EventId eventId = DecodableEventType::GetEventId();
yunhanw-googlef3d6b7c2021-11-17 13:42:38 -0800232 app::InteractionModelEngine * engine = app::InteractionModelEngine::GetInstance();
233 CHIP_ERROR err = CHIP_NO_ERROR;
234
yunhanw-googlecd44f9b2022-03-09 10:36:31 -0800235 auto readPaths = Platform::MakeUnique<app::EventPathParams>(endpointId, clusterId, eventId, aIsUrgentEvent);
yunhanw-google2cd1e1e2022-01-29 16:20:00 -0800236 VerifyOrReturnError(readPaths != nullptr, CHIP_ERROR_NO_MEMORY);
237
238 readParams.mpEventPathParamsList = readPaths.get();
239
yunhanw-googlef3d6b7c2021-11-17 13:42:38 -0800240 readParams.mEventPathParamsListSize = 1;
241
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500242 auto callback = chip::Platform::MakeUnique<TypedReadEventCallback<DecodableEventType>>(
yunhanw-googledce80072022-06-08 13:16:49 -0700243 readParams.mOnReportCb, readParams.mOnErrorCb, readParams.mOnDoneCb, readParams.mOnSubscriptionEstablishedCb,
244 readParams.mOnResubscriptionAttemptCb);
yunhanw-googlef3d6b7c2021-11-17 13:42:38 -0800245
Jerry Johns4efc5822021-10-19 14:22:36 -0700246 VerifyOrReturnError(callback != nullptr, CHIP_ERROR_NO_MEMORY);
247
Jerry Johns118ab9d2021-12-21 18:01:54 -0800248 auto readClient = chip::Platform::MakeUnique<app::ReadClient>(engine, apExchangeMgr, *callback.get(), readParams.mReportType);
yunhanw-google2cd1e1e2022-01-29 16:20:00 -0800249 VerifyOrReturnError(readClient != nullptr, CHIP_ERROR_NO_MEMORY);
250
251 if (readClient->IsSubscriptionType())
252 {
253 readPaths.release();
254 err = readClient->SendAutoResubscribeRequest(std::move(readParams));
255 ReturnErrorOnFailure(err);
256 }
257 else
258 {
259 err = readClient->SendRequest(readParams);
260 ReturnErrorOnFailure(err);
261 }
Jerry Johns4efc5822021-10-19 14:22:36 -0700262
263 //
264 // At this point, we'll get a callback through the OnDone callback above regardless of success or failure
265 // of the read operation to permit us to free up the callback object. So, release ownership of the callback
266 // object now to prevent it from being reclaimed at the end of this scoped block.
267 //
Michael Spanga2f17432022-01-26 20:52:14 -0500268 callback->AdoptReadClient(std::move(readClient));
yunhanw-googledbc91b72022-05-27 11:08:03 -0700269 callback.release();
Jerry Johns118ab9d2021-12-21 18:01:54 -0800270
Jerry Johns4efc5822021-10-19 14:22:36 -0700271 return err;
272}
273
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500274} // namespace detail
275
276/*
277 * A typed read event function that takes as input a template parameter that encapsulates the type information
278 * for a given attribute as well as callbacks for success and failure and either returns a decoded cluster-object representation
279 * of the requested attribute through the provided success callback or calls the provided failure callback.
280 *
281 * The DecodableEventType is generally expected to be a ClusterName::Events::EventName::DecodableEventType struct, but any
282 * object that contains type information exposed through a 'DecodableType' type declaration as well as GetClusterId() and
283 * GetEventId() methods is expected to work.
yunhanw-googledbc91b72022-05-27 11:08:03 -0700284 *
285 * @param[in] onSuccessCb Used to deliver event data received through the Read interactions
286 * @param[in] onErrorCb failureCb will be called when an error occurs *after* a successful call to ReadEvent.
287 * @param[in] onDoneCb OnDone will be called when ReadClient has finished all work for event retrieval, it is possible that there
288 * is no event.
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500289 */
290template <typename DecodableEventType>
Marc Lepage9a595a42021-12-16 03:53:52 -0500291CHIP_ERROR ReadEvent(Messaging::ExchangeManager * exchangeMgr, const SessionHandle & sessionHandle, EndpointId endpointId,
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500292 typename TypedReadEventCallback<DecodableEventType>::OnSuccessCallbackType onSuccessCb,
yunhanw-googledbc91b72022-05-27 11:08:03 -0700293 typename TypedReadEventCallback<DecodableEventType>::OnErrorCallbackType onErrorCb,
294 typename TypedReadEventCallback<DecodableEventType>::OnDoneCallbackType onDoneCb)
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500295{
296 detail::ReportEventParams<DecodableEventType> params(sessionHandle);
297 params.mOnReportCb = onSuccessCb;
298 params.mOnErrorCb = onErrorCb;
yunhanw-googledbc91b72022-05-27 11:08:03 -0700299 params.mOnDoneCb = onDoneCb;
yunhanw-googlecd44f9b2022-03-09 10:36:31 -0800300 return detail::ReportEvent(exchangeMgr, endpointId, std::move(params), false /*aIsUrgentEvent*/);
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500301}
302
303/**
304 * A functon that allows subscribing to one particular event. This works
305 * similarly to ReadEvent but keeps reporting events as they are emitted.
306 */
307template <typename DecodableEventType>
yunhanw-googledce80072022-06-08 13:16:49 -0700308CHIP_ERROR SubscribeEvent(
309 Messaging::ExchangeManager * exchangeMgr, const SessionHandle & sessionHandle, EndpointId endpointId,
310 typename TypedReadEventCallback<DecodableEventType>::OnSuccessCallbackType onReportCb,
311 typename TypedReadEventCallback<DecodableEventType>::OnErrorCallbackType onErrorCb, uint16_t minIntervalFloorSeconds,
312 uint16_t maxIntervalCeilingSeconds,
313 typename TypedReadEventCallback<DecodableEventType>::OnSubscriptionEstablishedCallbackType onSubscriptionEstablishedCb =
314 nullptr,
315 typename TypedReadEventCallback<DecodableEventType>::OnResubscriptionAttemptCallbackType onResubscriptionAttemptCb = nullptr,
316 bool keepPreviousSubscriptions = false, bool aIsUrgentEvent = false)
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500317{
318 detail::ReportEventParams<DecodableEventType> params(sessionHandle);
319 params.mOnReportCb = onReportCb;
320 params.mOnErrorCb = onErrorCb;
yunhanw-googledbc91b72022-05-27 11:08:03 -0700321 params.mOnDoneCb = nullptr;
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500322 params.mOnSubscriptionEstablishedCb = onSubscriptionEstablishedCb;
yunhanw-googledce80072022-06-08 13:16:49 -0700323 params.mOnResubscriptionAttemptCb = onResubscriptionAttemptCb;
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500324 params.mMinIntervalFloorSeconds = minIntervalFloorSeconds;
325 params.mMaxIntervalCeilingSeconds = maxIntervalCeilingSeconds;
Boris Zbarsky772c0602022-03-02 12:40:35 -0500326 params.mKeepSubscriptions = keepPreviousSubscriptions;
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500327 params.mReportType = app::ReadClient::InteractionType::Subscribe;
yunhanw-googlecd44f9b2022-03-09 10:36:31 -0800328 return detail::ReportEvent(exchangeMgr, endpointId, std::move(params), aIsUrgentEvent);
Boris Zbarsky5018ff92021-12-10 00:38:45 -0500329}
330
Jerry Johns4efc5822021-10-19 14:22:36 -0700331} // namespace Controller
332} // namespace chip
Wang Qixiang14bdc242023-08-04 13:04:55 +0800333#endif // CHIP_CONFIG_ENABLE_READ_CLIENT