|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +#include "MatterPowerSource.h" |
| 4 | + |
| 5 | +using namespace ::chip; |
| 6 | +using namespace ::chip::Platform; |
| 7 | +using namespace ::chip::Credentials; |
| 8 | +using namespace ::chip::app::Clusters; |
| 9 | + |
| 10 | +const EmberAfDeviceType gPowerSourceDeviceTypes[] = { { DEVICE_TYPE_POWER_SOURCE, DEVICE_VERSION_DEFAULT } }; |
| 11 | + |
| 12 | +// Power source cluster attributes |
| 13 | +DECLARE_DYNAMIC_ATTRIBUTE_LIST_BEGIN(powerSourceAttrs) |
| 14 | +DECLARE_DYNAMIC_ATTRIBUTE(PowerSource::Attributes::BatPercentRemaining::Id, INT8U, 1, 0), /* BatPercentRemaining */ |
| 15 | +DECLARE_DYNAMIC_ATTRIBUTE(PowerSource::Attributes::FeatureMap::Id, BITMAP32, 4, 0), /* FeatureMap */ |
| 16 | +DECLARE_DYNAMIC_ATTRIBUTE(PowerSource::Attributes::ClusterRevision::Id, INT16U, 2, 0), /* ClusterRevision */ |
| 17 | +DECLARE_DYNAMIC_ATTRIBUTE_LIST_END(); |
| 18 | + |
| 19 | +// Power source cluster list |
| 20 | +DECLARE_DYNAMIC_CLUSTER_LIST_BEGIN(powerSourceEndpointClusters) |
| 21 | +DECLARE_DYNAMIC_CLUSTER(PowerSource::Id, powerSourceAttrs, nullptr, nullptr), |
| 22 | +DECLARE_DYNAMIC_CLUSTER(Descriptor::Id, descriptorAttrs, nullptr, nullptr), |
| 23 | +DECLARE_DYNAMIC_CLUSTER(BridgedDeviceBasicInformation::Id, bridgedDeviceBasicAttrs, nullptr, nullptr) |
| 24 | +DECLARE_DYNAMIC_CLUSTER_LIST_END; |
| 25 | + |
| 26 | +/***************************************************************************//** |
| 27 | + * Constructor for MatterPowerSource |
| 28 | + ******************************************************************************/ |
| 29 | +MatterPowerSource::MatterPowerSource() : |
| 30 | + power_source_device(nullptr), |
| 31 | + device_endpoint(nullptr), |
| 32 | + endpoint_dataversion_storage(nullptr), |
| 33 | + initialized(false) |
| 34 | +{ |
| 35 | + ; |
| 36 | +} |
| 37 | + |
| 38 | +/***************************************************************************//** |
| 39 | + * Destructor for MatterPowerSource |
| 40 | + ******************************************************************************/ |
| 41 | +MatterPowerSource::~MatterPowerSource() |
| 42 | +{ |
| 43 | + this->end(); |
| 44 | +} |
| 45 | + |
| 46 | +/***************************************************************************//** |
| 47 | + * Initializes the MatterPowerSource instance |
| 48 | + * |
| 49 | + * @return true if the initialization succeeded, false otherwise |
| 50 | + ******************************************************************************/ |
| 51 | +bool MatterPowerSource::begin() |
| 52 | +{ |
| 53 | + if (this->initialized) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + // Create new device |
| 58 | + DevicePowerSource* power_source = new (std::nothrow)DevicePowerSource("Power Source", 0); |
| 59 | + if (power_source == nullptr) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + power_source->SetReachable(true); |
| 63 | + power_source->SetProductName("Power Source"); |
| 64 | + |
| 65 | + // Set the device instance pointer in the base class |
| 66 | + this->base_matter_device = power_source; |
| 67 | + |
| 68 | + // Create new endpoint |
| 69 | + EmberAfEndpointType* new_endpoint = (EmberAfEndpointType*)malloc(sizeof(EmberAfEndpointType)); |
| 70 | + if (new_endpoint == nullptr) { |
| 71 | + delete(power_source); |
| 72 | + return false; |
| 73 | + } |
| 74 | + new_endpoint->cluster = powerSourceEndpointClusters; |
| 75 | + new_endpoint->clusterCount = ArraySize(powerSourceEndpointClusters); |
| 76 | + new_endpoint->endpointSize = 0; |
| 77 | + |
| 78 | + // Create data version storage for the endpoint |
| 79 | + size_t dataversion_size = ArraySize(powerSourceEndpointClusters) * sizeof(DataVersion); |
| 80 | + DataVersion* new_power_source_data_version = (DataVersion*)malloc(dataversion_size); |
| 81 | + if (new_power_source_data_version == nullptr) { |
| 82 | + delete(power_source); |
| 83 | + free(new_endpoint); |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + // Add new endpoint |
| 88 | + int result = AddDeviceEndpoint(power_source, |
| 89 | + new_endpoint, |
| 90 | + Span<const EmberAfDeviceType>(gPowerSourceDeviceTypes), |
| 91 | + Span<DataVersion>(new_power_source_data_version, dataversion_size), 1); |
| 92 | + if (result < 0) { |
| 93 | + delete(power_source); |
| 94 | + free(new_endpoint); |
| 95 | + free(new_power_source_data_version); |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + this->power_source_device = power_source; |
| 100 | + this->device_endpoint = new_endpoint; |
| 101 | + this->endpoint_dataversion_storage = new_power_source_data_version; |
| 102 | + this->initialized = true; |
| 103 | + return true; |
| 104 | +} |
| 105 | + |
| 106 | +/***************************************************************************//** |
| 107 | + * Deinitializes the MatterPowerSource instance |
| 108 | + ******************************************************************************/ |
| 109 | +void MatterPowerSource::end() |
| 110 | +{ |
| 111 | + if (!this->initialized) { |
| 112 | + return; |
| 113 | + } |
| 114 | + (void)RemoveDeviceEndpoint(this->power_source_device); |
| 115 | + free(this->device_endpoint); |
| 116 | + free(this->endpoint_dataversion_storage); |
| 117 | + delete(this->power_source_device); |
| 118 | + this->initialized = false; |
| 119 | +} |
| 120 | + |
| 121 | +void MatterPowerSource::set_bat_percent_remaining_raw(uint8_t value) |
| 122 | +{ |
| 123 | + if (!this->initialized) { |
| 124 | + return; |
| 125 | + } |
| 126 | + PlatformMgr().LockChipStack(); |
| 127 | + this->power_source_device->SetBatPercentRemaining(value); |
| 128 | + PlatformMgr().UnlockChipStack(); |
| 129 | +} |
| 130 | + |
| 131 | +void MatterPowerSource::set_bat_percent_remaining(double percent) |
| 132 | +{ |
| 133 | + uint8_t out_value = static_cast<uint8_t>(percent * 2.0l); |
| 134 | + this->set_bat_percent_remaining_raw(out_value); |
| 135 | +} |
| 136 | + |
| 137 | +uint8_t MatterPowerSource::get_bat_percent_remaining() |
| 138 | +{ |
| 139 | + return this->power_source_device->GetBatPercentRemaining(); |
| 140 | +} |
0 commit comments