forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCastingPlayerDiscovery.h
154 lines (131 loc) · 5.02 KB
/
CastingPlayerDiscovery.h
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
*
* Copyright (c) 2023 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.
*/
#pragma once
#include "CastingPlayer.h"
#include "Types.h"
#include <controller/CHIPCommissionableNodeController.h>
#include <controller/DeviceDiscoveryDelegate.h>
#include <vector>
namespace matter {
namespace casting {
namespace core {
/**
* @brief Represents CastingPlayerDiscovery state.
*
*/
enum CastingPlayerDiscoveryState
{
DISCOVERY_NOT_READY, // Default state, mClientDelegate isn't initialized
DISCOVERY_READY, // After SetDelegate and before StartDiscovery, mClientDelegate is initialized
DISCOVERY_RUNNING, // After StartDiscovery success
};
/**
* @brief DiscoveryDelegate handles callbacks as CastingPlayers are discovered, updated, or lost
* from the network.
*/
class DLL_EXPORT DiscoveryDelegate
{
public:
virtual ~DiscoveryDelegate() {}
virtual void HandleOnAdded(memory::Strong<CastingPlayer> player) = 0;
virtual void HandleOnUpdated(memory::Strong<CastingPlayer> players) = 0;
// virtual void HandleOnRemoved(memory::Strong<CastingPlayer> players) = 0;
};
class CastingPlayer; // Forward declaration of the CastingPlayer class
class CastingPlayerDiscovery;
/**
* @brief DeviceDiscoveryDelegateImpl defines functionality for when callback
* OnDiscoveredDevice is called.
*/
class DeviceDiscoveryDelegateImpl : public chip::Controller::DeviceDiscoveryDelegate
{
private:
DiscoveryDelegate * mClientDelegate = nullptr;
public:
DeviceDiscoveryDelegateImpl() {}
DeviceDiscoveryDelegateImpl(DiscoveryDelegate * delegate) { mClientDelegate = delegate; }
void OnDiscoveredDevice(const chip::Dnssd::CommissionNodeData & nodeData) override;
};
/**
* @brief CastingPlayerDiscovery is a singleton utility class for discovering CastingPlayers.
*/
class CastingPlayerDiscovery
{
private:
std::vector<memory::Strong<CastingPlayer>> mCastingPlayers;
// This vector is used to store CastingPlayers that we might want to connect to. This ensures
// that CastingPlayers are not deleted prior to calling verify VerifyOrEstablishConnection() on
// the CastingPlayer we want to connect to.
std::vector<memory::Strong<CastingPlayer>> mCastingPlayersInternal;
DeviceDiscoveryDelegateImpl mDelegate;
CastingPlayerDiscovery();
static CastingPlayerDiscovery * _castingPlayerDiscovery;
CastingPlayerDiscovery(CastingPlayerDiscovery & other) = delete;
void operator=(const CastingPlayerDiscovery &) = delete;
chip::Controller::CommissionableNodeController mCommissionableNodeController;
CastingPlayerDiscoveryState mState = DISCOVERY_NOT_READY;
/**
* @brief Clear CastingPlayers in mCastingPlayersInternal with ConnectionState == CASTING_PLAYER_NOT_CONNECTED
*/
void ClearDisconnectedCastingPlayersInternal();
/**
* @brief Clear all CastingPlayers in mCastingPlayersInternal
*/
void ClearCastingPlayersInternal();
public:
static CastingPlayerDiscovery * GetInstance();
~CastingPlayerDiscovery()
{
ChipLogError(AppServer, "CastingPlayerDiscovery destructor() called");
mCastingPlayers.clear();
mCastingPlayersInternal.clear();
}
/**
* @brief Starts the discovery for CastingPlayers
*
* @param filterBydeviceType if passed as a non-zero value, CastingPlayerDiscovery will only callback on the DiscoveryDelegate
* with CastingPlayers whose deviceType matches filterBydeviceType
* @return CHIP_ERROR - CHIP_NO_ERROR if discovery for CastingPlayers started successfully, specific error code otherwise.
*/
CHIP_ERROR StartDiscovery(uint32_t filterBydeviceType = 0);
/**
* @brief Stop the discovery for CastingPlayers
*
* @return CHIP_ERROR - CHIP_NO_ERROR if discovery for CastingPlayers stopped successfully, specific error code otherwise.
*/
CHIP_ERROR StopDiscovery();
void SetDelegate(DiscoveryDelegate * clientDelegate)
{
ChipLogProgress(Discovery, "CastingPlayerDiscovery::SetDelegate() called");
if (clientDelegate == nullptr)
{
mState = DISCOVERY_NOT_READY;
}
else
{
mState = DISCOVERY_READY;
}
mDelegate = DeviceDiscoveryDelegateImpl(clientDelegate);
}
std::vector<memory::Strong<CastingPlayer>> GetCastingPlayers() { return mCastingPlayers; }
friend class DeviceDiscoveryDelegateImpl;
friend class CastingPlayer;
};
}; // namespace core
}; // namespace casting
}; // namespace matter