main-common.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. *
  3. * Copyright (c) 2020 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. #include "AllClustersCommandDelegate.h"
  19. #include "WindowCoveringManager.h"
  20. #include "air-quality-instance.h"
  21. #include "dishwasher-mode.h"
  22. #include "include/tv-callbacks.h"
  23. #include "laundry-washer-controls-delegate-impl.h"
  24. #include "laundry-washer-mode.h"
  25. #include "operational-state-delegate-impl.h"
  26. #include "resource-monitoring-delegates.h"
  27. #include "rvc-modes.h"
  28. #include "tcc-mode.h"
  29. #include <app-common/zap-generated/attributes/Accessors.h>
  30. #include <app/CommandHandler.h>
  31. #include <app/att-storage.h>
  32. #include <app/clusters/identify-server/identify-server.h>
  33. #include <app/clusters/laundry-washer-controls-server/laundry-washer-controls-server.h>
  34. #include <app/clusters/mode-base-server/mode-base-server.h>
  35. #include <app/server/Server.h>
  36. #include <app/util/af.h>
  37. #include <app/util/attribute-storage.h>
  38. #include <lib/support/CHIPMem.h>
  39. #include <new>
  40. #include <platform/DeviceInstanceInfoProvider.h>
  41. #include <platform/DiagnosticDataProvider.h>
  42. #include <platform/PlatformManager.h>
  43. #include <static-supported-temperature-levels.h>
  44. #include <system/SystemPacketBuffer.h>
  45. #include <transport/SessionManager.h>
  46. #include <transport/raw/PeerAddress.h>
  47. #include <Options.h>
  48. using namespace chip;
  49. using namespace chip::app;
  50. using namespace chip::DeviceLayer;
  51. namespace {
  52. constexpr const char kChipEventFifoPathPrefix[] = "/tmp/chip_all_clusters_fifo_";
  53. LowPowerManager sLowPowerManager;
  54. NamedPipeCommands sChipNamedPipeCommands;
  55. AllClustersCommandDelegate sAllClustersCommandDelegate;
  56. Clusters::WindowCovering::WindowCoveringManager sWindowCoveringManager;
  57. Clusters::TemperatureControl::AppSupportedTemperatureLevelsDelegate sAppSupportedTemperatureLevelsDelegate;
  58. // Please refer to https://github.com/CHIP-Specifications/connectedhomeip-spec/blob/master/src/namespaces
  59. constexpr const uint8_t kNamespaceCommon = 7;
  60. // Common Number Namespace: 7, tag 0 (Zero)
  61. constexpr const uint8_t kTagCommonZero = 0;
  62. // Common Number Namespace: 7, tag 1 (One)
  63. constexpr const uint8_t kTagCommonOne = 1;
  64. // Common Number Namespace: 7, tag 2 (Two)
  65. constexpr const uint8_t kTagCommonTwo = 2;
  66. constexpr const uint8_t kNamespacePosition = 8;
  67. // Common Position Namespace: 8, tag: 0 (Left)
  68. constexpr const uint8_t kTagPositionLeft = 0;
  69. // Common Position Namespace: 8, tag: 1 (Right)
  70. constexpr const uint8_t kTagPositionRight = 1;
  71. // Common Position Namespace: 8, tag: 3 (Bottom)
  72. constexpr const uint8_t kTagPositionBottom = 3;
  73. const Clusters::Descriptor::Structs::SemanticTagStruct::Type gEp0TagList[] = {
  74. { .namespaceID = kNamespaceCommon, .tag = kTagCommonZero }, { .namespaceID = kNamespacePosition, .tag = kTagPositionBottom }
  75. };
  76. const Clusters::Descriptor::Structs::SemanticTagStruct::Type gEp1TagList[] = {
  77. { .namespaceID = kNamespaceCommon, .tag = kTagCommonOne }, { .namespaceID = kNamespacePosition, .tag = kTagPositionLeft }
  78. };
  79. const Clusters::Descriptor::Structs::SemanticTagStruct::Type gEp2TagList[] = {
  80. { .namespaceID = kNamespaceCommon, .tag = kTagCommonTwo }, { .namespaceID = kNamespacePosition, .tag = kTagPositionRight }
  81. };
  82. } // namespace
  83. #ifdef EMBER_AF_PLUGIN_DISHWASHER_ALARM_SERVER
  84. extern void MatterDishwasherAlarmServerInit();
  85. #endif
  86. void OnIdentifyStart(::Identify *)
  87. {
  88. ChipLogProgress(Zcl, "OnIdentifyStart");
  89. }
  90. void OnIdentifyStop(::Identify *)
  91. {
  92. ChipLogProgress(Zcl, "OnIdentifyStop");
  93. }
  94. void OnTriggerEffect(::Identify * identify)
  95. {
  96. switch (identify->mCurrentEffectIdentifier)
  97. {
  98. case Clusters::Identify::EffectIdentifierEnum::kBlink:
  99. ChipLogProgress(Zcl, "Clusters::Identify::EffectIdentifierEnum::kBlink");
  100. break;
  101. case Clusters::Identify::EffectIdentifierEnum::kBreathe:
  102. ChipLogProgress(Zcl, "Clusters::Identify::EffectIdentifierEnum::kBreathe");
  103. break;
  104. case Clusters::Identify::EffectIdentifierEnum::kOkay:
  105. ChipLogProgress(Zcl, "Clusters::Identify::EffectIdentifierEnum::kOkay");
  106. break;
  107. case Clusters::Identify::EffectIdentifierEnum::kChannelChange:
  108. ChipLogProgress(Zcl, "Clusters::Identify::EffectIdentifierEnum::kChannelChange");
  109. break;
  110. default:
  111. ChipLogProgress(Zcl, "No identifier effect");
  112. return;
  113. }
  114. }
  115. static Identify gIdentify0 = {
  116. chip::EndpointId{ 0 }, OnIdentifyStart, OnIdentifyStop, Clusters::Identify::IdentifyTypeEnum::kVisibleIndicator,
  117. OnTriggerEffect,
  118. };
  119. static Identify gIdentify1 = {
  120. chip::EndpointId{ 1 }, OnIdentifyStart, OnIdentifyStop, Clusters::Identify::IdentifyTypeEnum::kVisibleIndicator,
  121. OnTriggerEffect,
  122. };
  123. namespace {
  124. class ExampleDeviceInstanceInfoProvider : public DeviceInstanceInfoProvider
  125. {
  126. public:
  127. void Init(DeviceInstanceInfoProvider * defaultProvider) { mDefaultProvider = defaultProvider; }
  128. CHIP_ERROR GetVendorName(char * buf, size_t bufSize) override { return mDefaultProvider->GetVendorName(buf, bufSize); }
  129. CHIP_ERROR GetVendorId(uint16_t & vendorId) override { return mDefaultProvider->GetVendorId(vendorId); }
  130. CHIP_ERROR GetProductName(char * buf, size_t bufSize) override { return mDefaultProvider->GetProductName(buf, bufSize); }
  131. CHIP_ERROR GetProductId(uint16_t & productId) override { return mDefaultProvider->GetProductId(productId); }
  132. CHIP_ERROR GetPartNumber(char * buf, size_t bufSize) override { return mDefaultProvider->GetPartNumber(buf, bufSize); }
  133. CHIP_ERROR GetProductURL(char * buf, size_t bufSize) override { return mDefaultProvider->GetPartNumber(buf, bufSize); }
  134. CHIP_ERROR GetProductLabel(char * buf, size_t bufSize) override { return mDefaultProvider->GetProductLabel(buf, bufSize); }
  135. CHIP_ERROR GetSerialNumber(char * buf, size_t bufSize) override { return mDefaultProvider->GetSerialNumber(buf, bufSize); }
  136. CHIP_ERROR GetManufacturingDate(uint16_t & year, uint8_t & month, uint8_t & day) override
  137. {
  138. return mDefaultProvider->GetManufacturingDate(year, month, day);
  139. }
  140. CHIP_ERROR GetHardwareVersion(uint16_t & hardwareVersion) override
  141. {
  142. return mDefaultProvider->GetHardwareVersion(hardwareVersion);
  143. }
  144. CHIP_ERROR GetHardwareVersionString(char * buf, size_t bufSize) override
  145. {
  146. return mDefaultProvider->GetHardwareVersionString(buf, bufSize);
  147. }
  148. CHIP_ERROR GetRotatingDeviceIdUniqueId(MutableByteSpan & uniqueIdSpan) override
  149. {
  150. return mDefaultProvider->GetRotatingDeviceIdUniqueId(uniqueIdSpan);
  151. }
  152. CHIP_ERROR GetProductFinish(Clusters::BasicInformation::ProductFinishEnum * finish) override;
  153. CHIP_ERROR GetProductPrimaryColor(Clusters::BasicInformation::ColorEnum * primaryColor) override;
  154. private:
  155. DeviceInstanceInfoProvider * mDefaultProvider;
  156. };
  157. CHIP_ERROR ExampleDeviceInstanceInfoProvider::GetProductFinish(Clusters::BasicInformation::ProductFinishEnum * finish)
  158. {
  159. // Our example device claims to have a Satin finish for now. We can make
  160. // this configurable as needed.
  161. *finish = Clusters::BasicInformation::ProductFinishEnum::kSatin;
  162. return CHIP_NO_ERROR;
  163. }
  164. CHIP_ERROR ExampleDeviceInstanceInfoProvider::GetProductPrimaryColor(Clusters::BasicInformation::ColorEnum * primaryColor)
  165. {
  166. // Our example device claims to have a nice purple color for now. We can
  167. // make this configurable as needed.
  168. *primaryColor = Clusters::BasicInformation::ColorEnum::kPurple;
  169. return CHIP_NO_ERROR;
  170. }
  171. ExampleDeviceInstanceInfoProvider gExampleDeviceInstanceInfoProvider;
  172. } // namespace
  173. void ApplicationInit()
  174. {
  175. std::string path = kChipEventFifoPathPrefix + std::to_string(getpid());
  176. if (sChipNamedPipeCommands.Start(path, &sAllClustersCommandDelegate) != CHIP_NO_ERROR)
  177. {
  178. ChipLogError(NotSpecified, "Failed to start CHIP NamedPipeCommands");
  179. sChipNamedPipeCommands.Stop();
  180. }
  181. auto * defaultProvider = GetDeviceInstanceInfoProvider();
  182. if (defaultProvider != &gExampleDeviceInstanceInfoProvider)
  183. {
  184. gExampleDeviceInstanceInfoProvider.Init(defaultProvider);
  185. SetDeviceInstanceInfoProvider(&gExampleDeviceInstanceInfoProvider);
  186. }
  187. #ifdef EMBER_AF_PLUGIN_DISHWASHER_ALARM_SERVER
  188. MatterDishwasherAlarmServerInit();
  189. #endif
  190. Clusters::TemperatureControl::SetInstance(&sAppSupportedTemperatureLevelsDelegate);
  191. SetTagList(/* endpoint= */ 0, Span<const Clusters::Descriptor::Structs::SemanticTagStruct::Type>(gEp0TagList));
  192. SetTagList(/* endpoint= */ 1, Span<const Clusters::Descriptor::Structs::SemanticTagStruct::Type>(gEp1TagList));
  193. SetTagList(/* endpoint= */ 2, Span<const Clusters::Descriptor::Structs::SemanticTagStruct::Type>(gEp2TagList));
  194. }
  195. void ApplicationShutdown()
  196. {
  197. // These may have been initialised via the emberAfXxxClusterInitCallback methods. We need to destroy them before shutdown.
  198. Clusters::DishwasherMode::Shutdown();
  199. Clusters::LaundryWasherMode::Shutdown();
  200. Clusters::RvcCleanMode::Shutdown();
  201. Clusters::RvcRunMode::Shutdown();
  202. Clusters::RefrigeratorAndTemperatureControlledCabinetMode::Shutdown();
  203. Clusters::HepaFilterMonitoring::Shutdown();
  204. Clusters::ActivatedCarbonFilterMonitoring::Shutdown();
  205. Clusters::AirQuality::Shutdown();
  206. Clusters::OperationalState::Shutdown();
  207. Clusters::RvcOperationalState::Shutdown();
  208. if (sChipNamedPipeCommands.Stop() != CHIP_NO_ERROR)
  209. {
  210. ChipLogError(NotSpecified, "Failed to stop CHIP NamedPipeCommands");
  211. }
  212. }
  213. using namespace chip::app::Clusters::LaundryWasherControls;
  214. void emberAfLaundryWasherControlsClusterInitCallback(EndpointId endpoint)
  215. {
  216. LaundryWasherControlsServer::SetDefaultDelegate(endpoint, &LaundryWasherControlDelegate::getLaundryWasherControlDelegate());
  217. }
  218. void emberAfLowPowerClusterInitCallback(EndpointId endpoint)
  219. {
  220. ChipLogProgress(NotSpecified, "Setting LowPower default delegate to global manager");
  221. Clusters::LowPower::SetDefaultDelegate(endpoint, &sLowPowerManager);
  222. }
  223. void emberAfWindowCoveringClusterInitCallback(chip::EndpointId endpoint)
  224. {
  225. sWindowCoveringManager.Init(endpoint);
  226. Clusters::WindowCovering::SetDefaultDelegate(endpoint, &sWindowCoveringManager);
  227. Clusters::WindowCovering::ConfigStatusUpdateFeatures(endpoint);
  228. }