|
| 1 | +// Copyright 2026 Espressif Systems (Shanghai) PTE LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <esp_matter_data_model_priv.h> |
| 16 | +#include <app/ClusterCallbacks.h> |
| 17 | +#include <app/SafeAttributePersistenceProvider.h> |
| 18 | +#include <data_model/esp_matter_data_model.h> |
| 19 | +#include <data_model/esp_matter_endpoint.h> |
| 20 | +#include <data_model_provider/esp_matter_data_model_provider.h> |
| 21 | +#include <unordered_map> |
| 22 | +#include "integration.h" |
| 23 | + |
| 24 | +using namespace chip; |
| 25 | +using namespace chip::app; |
| 26 | +using namespace chip::app::Clusters; |
| 27 | +using namespace chip::app::Clusters::CameraAvStreamManagement; |
| 28 | +using namespace esp_matter; |
| 29 | + |
| 30 | +namespace { |
| 31 | +std::unordered_map<EndpointId, LazyRegisteredServerCluster<CameraAVStreamManagementCluster>> gServers; |
| 32 | +std::unordered_map<EndpointId, CameraAvStreamManagementConfig> gConfigs; |
| 33 | + |
| 34 | +bool IsClusterEnabled(EndpointId endpointId) |
| 35 | +{ |
| 36 | + cluster_t *cluster = cluster::get(endpointId, CameraAvStreamManagement::Id); |
| 37 | + return cluster != nullptr; |
| 38 | +} |
| 39 | + |
| 40 | +uint32_t GetFeatureMap(EndpointId endpointId) |
| 41 | +{ |
| 42 | + attribute_t *attribute = attribute::get(endpointId, CameraAvStreamManagement::Id, Globals::Attributes::FeatureMap::Id); |
| 43 | + VerifyOrReturnValue(attribute, 0); |
| 44 | + |
| 45 | + esp_matter_attr_val_t val = esp_matter_invalid(NULL); |
| 46 | + VerifyOrReturnValue(attribute::get_val_internal(attribute, &val) == ESP_OK, 0); |
| 47 | + return val.val.u32; |
| 48 | +} |
| 49 | + |
| 50 | +BitFlags<OptionalAttribute> GetOptionalAttributes(EndpointId endpointId) |
| 51 | +{ |
| 52 | + BitFlags<OptionalAttribute> optionalAttrs; |
| 53 | + |
| 54 | + if (endpoint::is_attribute_enabled(endpointId, CameraAvStreamManagement::Id, |
| 55 | + CameraAvStreamManagement::Attributes::HardPrivacyModeOn::Id)) { |
| 56 | + optionalAttrs.Set(OptionalAttribute::kHardPrivacyModeOn); |
| 57 | + } |
| 58 | + if (endpoint::is_attribute_enabled(endpointId, CameraAvStreamManagement::Id, |
| 59 | + CameraAvStreamManagement::Attributes::NightVisionIllum::Id)) { |
| 60 | + optionalAttrs.Set(OptionalAttribute::kNightVisionIllum); |
| 61 | + } |
| 62 | + if (endpoint::is_attribute_enabled(endpointId, CameraAvStreamManagement::Id, |
| 63 | + CameraAvStreamManagement::Attributes::MicrophoneAGCEnabled::Id)) { |
| 64 | + optionalAttrs.Set(OptionalAttribute::kMicrophoneAGCEnabled); |
| 65 | + } |
| 66 | + if (endpoint::is_attribute_enabled(endpointId, CameraAvStreamManagement::Id, |
| 67 | + CameraAvStreamManagement::Attributes::ImageRotation::Id)) { |
| 68 | + optionalAttrs.Set(OptionalAttribute::kImageRotation); |
| 69 | + } |
| 70 | + if (endpoint::is_attribute_enabled(endpointId, CameraAvStreamManagement::Id, |
| 71 | + CameraAvStreamManagement::Attributes::ImageFlipHorizontal::Id)) { |
| 72 | + optionalAttrs.Set(OptionalAttribute::kImageFlipHorizontal); |
| 73 | + } |
| 74 | + if (endpoint::is_attribute_enabled(endpointId, CameraAvStreamManagement::Id, |
| 75 | + CameraAvStreamManagement::Attributes::ImageFlipVertical::Id)) { |
| 76 | + optionalAttrs.Set(OptionalAttribute::kImageFlipVertical); |
| 77 | + } |
| 78 | + if (endpoint::is_attribute_enabled(endpointId, CameraAvStreamManagement::Id, |
| 79 | + CameraAvStreamManagement::Attributes::StatusLightEnabled::Id)) { |
| 80 | + optionalAttrs.Set(OptionalAttribute::kStatusLightEnabled); |
| 81 | + } |
| 82 | + if (endpoint::is_attribute_enabled(endpointId, CameraAvStreamManagement::Id, |
| 83 | + CameraAvStreamManagement::Attributes::StatusLightBrightness::Id)) { |
| 84 | + optionalAttrs.Set(OptionalAttribute::kStatusLightBrightness); |
| 85 | + } |
| 86 | + return optionalAttrs; |
| 87 | +} |
| 88 | +} // namespace |
| 89 | + |
| 90 | +namespace chip::app::Clusters::CameraAvStreamManagement { |
| 91 | + |
| 92 | +void SetConfig(EndpointId endpointId, const CameraAvStreamManagementConfig &config) |
| 93 | +{ |
| 94 | + gConfigs[endpointId] = config; |
| 95 | +} |
| 96 | + |
| 97 | +const CameraAvStreamManagementConfig * GetConfig(EndpointId endpointId) |
| 98 | +{ |
| 99 | + auto it = gConfigs.find(endpointId); |
| 100 | + return (it == gConfigs.end()) ? nullptr : &it->second; |
| 101 | +} |
| 102 | + |
| 103 | +void SetDelegate(EndpointId endpointId, CameraAVStreamManagementDelegate * delegate) |
| 104 | +{ |
| 105 | + if (gConfigs.find(endpointId) == gConfigs.end()) { |
| 106 | + ChipLogError(AppServer, "Camera AV Stream Management config not found for endpoint %u", endpointId); |
| 107 | + return; |
| 108 | + } |
| 109 | + gConfigs[endpointId].delegate = delegate; |
| 110 | +} |
| 111 | + |
| 112 | +CameraAVStreamManagementCluster * GetServer(EndpointId endpointId) |
| 113 | +{ |
| 114 | + if (!gServers[endpointId].IsConstructed()) { |
| 115 | + return nullptr; |
| 116 | + } |
| 117 | + |
| 118 | + return &gServers[endpointId].Cluster(); |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace chip::app::Clusters::CameraAvStreamManagement |
| 122 | + |
| 123 | +void ESPMatterCameraAvStreamManagementClusterServerInitCallback(EndpointId endpointId) |
| 124 | +{ |
| 125 | + if (!IsClusterEnabled(endpointId)) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + if (!gServers[endpointId].IsConstructed()) { |
| 130 | + const CameraAvStreamManagementConfig *config = CameraAvStreamManagement::GetConfig(endpointId); |
| 131 | + if (config == nullptr || config->delegate == nullptr) { |
| 132 | + ChipLogError(AppServer, "Camera AV Stream Management config/delegate missing for endpoint %u", endpointId); |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + SafeAttributePersistenceProvider * provider = GetSafeAttributePersistenceProvider(); |
| 137 | + if (provider == nullptr) { |
| 138 | + ChipLogError(AppServer, "SafeAttributePersistenceProvider not available for endpoint %u", endpointId); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + ChipLogProgress(AppServer, "Registering Camera AV Stream Management on endpoint %u", endpointId); |
| 143 | + BitFlags<Feature> features(GetFeatureMap(endpointId)); |
| 144 | + BitFlags<OptionalAttribute> optionalAttrs = GetOptionalAttributes(endpointId); |
| 145 | + |
| 146 | + gServers[endpointId].Create(CameraAVStreamManagementCluster::Context{ *provider }, *config->delegate, endpointId, features, |
| 147 | + optionalAttrs, |
| 148 | + config->maxConcurrentEncoders, config->maxEncodedPixelRate, config->videoSensorParams, |
| 149 | + config->nightVisionUsesInfrared, config->minViewPortRes, config->rateDistortionTradeOffPoints, |
| 150 | + config->maxContentBufferSize, config->microphoneCapabilities, config->speakerCapabilities, |
| 151 | + config->twoWayTalkSupport, config->snapshotCapabilities, config->maxNetworkBandwidth, |
| 152 | + config->supportedStreamUsages, config->streamUsagePriorities); |
| 153 | + } |
| 154 | + |
| 155 | + CHIP_ERROR err = data_model::provider::get_instance().registry().Register(gServers[endpointId].Registration()); |
| 156 | + if (err != CHIP_NO_ERROR) { |
| 157 | + ChipLogError(AppServer, |
| 158 | + "Failed to register Camera AV Stream Management on endpoint %u - Error: %" CHIP_ERROR_FORMAT, endpointId, |
| 159 | + err.Format()); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +void ESPMatterCameraAvStreamManagementClusterServerShutdownCallback(EndpointId endpointId, ClusterShutdownType shutdownType) |
| 164 | +{ |
| 165 | + auto serverIt = gServers.find(endpointId); |
| 166 | + VerifyOrReturn(serverIt != gServers.end()); |
| 167 | + VerifyOrReturn(serverIt->second.IsConstructed()); |
| 168 | + |
| 169 | + CHIP_ERROR err = data_model::provider::get_instance().registry().Unregister(&serverIt->second.Cluster(), shutdownType); |
| 170 | + if (err != CHIP_NO_ERROR) { |
| 171 | + ChipLogError(AppServer, |
| 172 | + "Failed to unregister Camera AV Stream Management on endpoint %u - Error: %" CHIP_ERROR_FORMAT, endpointId, |
| 173 | + err.Format()); |
| 174 | + } |
| 175 | + if (shutdownType == ClusterShutdownType::kPermanentRemove) { |
| 176 | + serverIt->second.Destroy(); |
| 177 | + gServers.erase(serverIt); |
| 178 | + gConfigs.erase(endpointId); |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +void MatterCameraAvStreamManagementPluginServerInitCallback() {} |
| 183 | + |
| 184 | +void MatterCameraAvStreamManagementPluginServerShutdownCallback() {} |
0 commit comments