|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2023 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 | + |
| 21 | +#include "Types.h" |
| 22 | +#include "lib/support/logging/CHIPLogging.h" |
| 23 | + |
| 24 | +#include <inet/IPAddress.h> |
| 25 | +#include <inet/InetInterface.h> |
| 26 | +#include <string.h> |
| 27 | +#include <vector> |
| 28 | + |
| 29 | +namespace matter { |
| 30 | +namespace casting { |
| 31 | +namespace core { |
| 32 | + |
| 33 | +enum ConnectionError |
| 34 | +{ |
| 35 | + NO_CONNECTION_ERROR = 0, |
| 36 | + FAILED_TO_CONNECT = 1 |
| 37 | +}; |
| 38 | + |
| 39 | +using ConnectCallback = std::function<void(ConnectionError)>; |
| 40 | +using DisconnectCallback = std::function<void(void)>; |
| 41 | + |
| 42 | +const int kPortMaxLength = 5; // port is uint16_t |
| 43 | +const int kIdMaxLength = chip::Dnssd::kHostNameMaxLength + kPortMaxLength; |
| 44 | + |
| 45 | +class CastingPlayerAttributes |
| 46 | +{ |
| 47 | +public: |
| 48 | + char id[kIdMaxLength] = {}; |
| 49 | + char deviceName[chip::Dnssd::kMaxDeviceNameLen + 1] = {}; |
| 50 | + char hostName[chip::Dnssd::kHostNameMaxLength + 1] = {}; |
| 51 | + char instanceName[chip::Dnssd::kHostNameMaxLength + 1] = {}; |
| 52 | + unsigned int numIPs; // number of valid IP addresses |
| 53 | + chip::Inet::IPAddress ipAddresses[chip::Dnssd::CommonResolutionData::kMaxIPAddresses]; |
| 54 | + uint16_t port; |
| 55 | + uint16_t productId; |
| 56 | + uint16_t vendorId; |
| 57 | + uint32_t deviceType; |
| 58 | +}; |
| 59 | + |
| 60 | +/** |
| 61 | + * @brief CastingPlayer represents a Matter commissioner that is able to play media to a physical |
| 62 | + * output or to a display screen which is part of the device. |
| 63 | + */ |
| 64 | +class CastingPlayer : public std::enable_shared_from_this<CastingPlayer> |
| 65 | +{ |
| 66 | +private: |
| 67 | + // std::vector<memory::Strong<Endpoint>> endpoints; |
| 68 | + bool mConnected = false; |
| 69 | + CastingPlayerAttributes mAttributes; |
| 70 | + |
| 71 | +public: |
| 72 | + CastingPlayer(CastingPlayerAttributes playerAttributes) { mAttributes = playerAttributes; } |
| 73 | + const char * GetId() const { return mAttributes.id; } |
| 74 | + |
| 75 | + const char * GetDeviceName() const { return mAttributes.deviceName; } |
| 76 | + |
| 77 | + const char * GetHostName() const { return mAttributes.hostName; } |
| 78 | + |
| 79 | + const char * GetInstanceName() const { return mAttributes.instanceName; } |
| 80 | + |
| 81 | + uint GetNumIPs() const { return mAttributes.numIPs; } |
| 82 | + |
| 83 | + chip::Inet::IPAddress * GetIPAddresses() { return mAttributes.ipAddresses; } |
| 84 | + |
| 85 | + uint16_t GetPort() { return mAttributes.port; } |
| 86 | + |
| 87 | + uint16_t GetProductId() const { return mAttributes.productId; } |
| 88 | + |
| 89 | + uint16_t GetVendorId() const { return mAttributes.vendorId; } |
| 90 | + |
| 91 | + uint32_t GetDeviceType() const { return mAttributes.deviceType; } |
| 92 | + |
| 93 | + // public: |
| 94 | + // void RegisterEndpoint(const memory::Strong<Endpoint> endpoint) { endpoints.push_back(endpoint); } |
| 95 | + |
| 96 | + // const std::vector<memory::Strong<Endpoint>> GetEndpoints() const { return endpoints; } |
| 97 | + |
| 98 | + /** |
| 99 | + * @brief Compares based on the Id |
| 100 | + */ |
| 101 | + bool operator==(const CastingPlayer & other) const |
| 102 | + { |
| 103 | + int compareResult = strcmp(this->mAttributes.id, other.mAttributes.id); |
| 104 | + return (compareResult == 0) ? 1 : 0; |
| 105 | + } |
| 106 | + |
| 107 | +public: |
| 108 | + /** |
| 109 | + * @return true if this CastingPlayer is connected to the CastingApp |
| 110 | + */ |
| 111 | + bool IsConnected() const { return mConnected; } |
| 112 | + |
| 113 | + void Connect(const long timeout, ConnectCallback onCompleted); |
| 114 | + void Disconnect(const long timeout, DisconnectCallback onCompleted); |
| 115 | + |
| 116 | + void LogDetail() const |
| 117 | + { |
| 118 | + if (strlen(mAttributes.id) != 0) |
| 119 | + { |
| 120 | + ChipLogDetail(Discovery, "\tID: %s", mAttributes.id); |
| 121 | + } |
| 122 | + if (strlen(mAttributes.deviceName) != 0) |
| 123 | + { |
| 124 | + ChipLogDetail(Discovery, "\tName: %s", mAttributes.deviceName); |
| 125 | + } |
| 126 | + if (strlen(mAttributes.hostName) != 0) |
| 127 | + { |
| 128 | + ChipLogDetail(Discovery, "\tHost Name: %s", mAttributes.hostName); |
| 129 | + } |
| 130 | + if (strlen(mAttributes.instanceName) != 0) |
| 131 | + { |
| 132 | + ChipLogDetail(Discovery, "\tInstance Name: %s", mAttributes.instanceName); |
| 133 | + } |
| 134 | + if (mAttributes.numIPs > 0) |
| 135 | + { |
| 136 | + ChipLogDetail(Discovery, "\tNumber of IPs: %u", mAttributes.numIPs); |
| 137 | + } |
| 138 | + char buf[chip::Inet::IPAddress::kMaxStringLength]; |
| 139 | + if (strlen(mAttributes.ipAddresses[0].ToString(buf)) != 0) |
| 140 | + { |
| 141 | + for (unsigned j = 0; j < mAttributes.numIPs; j++) |
| 142 | + { |
| 143 | + char * ipAddressOut = mAttributes.ipAddresses[j].ToString(buf); |
| 144 | + ChipLogDetail(AppServer, "\tIP Address #%d: %s", j + 1, ipAddressOut); |
| 145 | + } |
| 146 | + } |
| 147 | + if (mAttributes.port > 0) |
| 148 | + { |
| 149 | + ChipLogDetail(Discovery, "\tPort: %u", mAttributes.port); |
| 150 | + } |
| 151 | + if (mAttributes.productId > 0) |
| 152 | + { |
| 153 | + ChipLogDetail(Discovery, "\tProduct ID: %u", mAttributes.productId); |
| 154 | + } |
| 155 | + if (mAttributes.vendorId > 0) |
| 156 | + { |
| 157 | + ChipLogDetail(Discovery, "\tVendor ID: %u", mAttributes.vendorId); |
| 158 | + } |
| 159 | + if (mAttributes.deviceType > 0) |
| 160 | + { |
| 161 | + ChipLogDetail(Discovery, "\tDevice Type: %" PRIu32, mAttributes.deviceType); |
| 162 | + } |
| 163 | + } |
| 164 | +}; |
| 165 | + |
| 166 | +}; // namespace core |
| 167 | +}; // namespace casting |
| 168 | +}; // namespace matter |
0 commit comments