forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessagesManager.cpp
111 lines (96 loc) · 4.06 KB
/
MessagesManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
*
* Copyright (c) 2024 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 "MessagesManager.h"
#include <app-common/zap-generated/attributes/Accessors.h>
#include <vector>
using namespace std;
using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters::Messages;
using Message = chip::app::Clusters::Messages::Structs::MessageStruct::Type;
using MessageResponseOption = chip::app::Clusters::Messages::Structs::MessageResponseOptionStruct::Type;
// Commands
CHIP_ERROR MessagesManager::HandlePresentMessagesRequest(
const ByteSpan & messageId, const MessagePriorityEnum & priority, const BitMask<MessageControlBitmap> & messageControl,
const DataModel::Nullable<uint32_t> & startTime, const DataModel::Nullable<uint64_t> & duration, const CharSpan & messageText,
const Optional<DataModel::DecodableList<MessageResponseOption>> & responses)
{
ChipLogProgress(Zcl, "HandlePresentMessagesRequest message:%s", std::string(messageText.data(), messageText.size()).c_str());
auto cachedMessage = CachedMessage(messageId, priority, messageControl, startTime, duration,
std::string(messageText.data(), messageText.size()));
if (responses.HasValue())
{
auto iter = responses.Value().begin();
while (iter.Next())
{
auto & response = iter.GetValue();
CachedMessageOption option(response.messageResponseID.Value(),
std::string(response.label.Value().data(), response.label.Value().size()));
cachedMessage.AddOption(option);
}
}
mCachedMessages.push_back(cachedMessage);
// Add your code to present Message
ChipLogProgress(Zcl, "HandlePresentMessagesRequest complete");
return CHIP_NO_ERROR;
}
CHIP_ERROR MessagesManager::HandleCancelMessagesRequest(const DataModel::DecodableList<ByteSpan> & messageIds)
{
auto iter = messageIds.begin();
while (iter.Next())
{
auto & id = iter.GetValue();
mCachedMessages.remove_if([id](CachedMessage & entry) { return entry.MessageIdMatches(id); });
// per spec, the command succeeds even when the message id does not match an existing message
}
return CHIP_NO_ERROR;
}
// Attributes
CHIP_ERROR MessagesManager::HandleGetMessages(AttributeValueEncoder & aEncoder)
{
return aEncoder.EncodeList([this](const auto & encoder) -> CHIP_ERROR {
for (CachedMessage & entry : mCachedMessages)
{
ReturnErrorOnFailure(encoder.Encode(entry.GetMessage()));
}
return CHIP_NO_ERROR;
});
}
CHIP_ERROR MessagesManager::HandleGetActiveMessageIds(AttributeValueEncoder & aEncoder)
{
return aEncoder.EncodeList([this](const auto & encoder) -> CHIP_ERROR {
for (CachedMessage & entry : mCachedMessages)
{
ReturnErrorOnFailure(encoder.Encode(entry.GetMessage().messageID));
}
return CHIP_NO_ERROR;
});
}
// Global Attributes
uint32_t MessagesManager::GetFeatureMap(EndpointId endpoint)
{
BitMask<Feature> FeatureMap;
FeatureMap.Set(Feature::kReceivedConfirmation);
FeatureMap.Set(Feature::kConfirmationResponse);
FeatureMap.Set(Feature::kConfirmationReply);
FeatureMap.Set(Feature::kProtectedMessages);
uint32_t featureMap = FeatureMap.Raw();
ChipLogProgress(Zcl, "GetFeatureMap featureMap=%d", featureMap);
// forcing to all features since this implementation supports all
// Attributes::FeatureMap::Get(endpoint, &featureMap);
return featureMap;
}