Skip to content

Commit 894b5e6

Browse files
[all-devices-app] Refactor Chime audio architecture for POSIX platforms (#71623)
* Refactor Chime audio architecture for POSIX platforms * Include missing chime core and factory changes in PR * Remove chime/impl directory and its BUILD.gn * Restyled by clang-format * Restyled by gn * Use PosixChimeDevice to share implementation between Linux and Darwin * Address review comments and improve status handling in PosixChimeDevice * Restyled by gn * Fix ESP32 build by removing reference to deleted chime/impl directory --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 47d9977 commit 894b5e6

File tree

17 files changed

+254
-177
lines changed

17 files changed

+254
-177
lines changed

examples/all-devices-app/all-devices-common/devices/chime/ChimeDevice.cpp

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616
*/
1717
#include <devices/Types.h>
1818
#include <devices/chime/ChimeDevice.h>
19+
#include <lib/support/StringBuilder.h>
1920
#include <lib/support/logging/CHIPLogging.h>
2021

2122
using namespace chip::app::Clusters;
2223

2324
namespace chip {
2425
namespace app {
2526

26-
ChimeDevice::ChimeDevice(Clusters::ChimeDelegate & delegate, TimerDelegate & timerDelegate) :
27-
SingleEndpointDevice(Span<const DataModel::DeviceTypeEntry>(&Device::Type::kChime, 1)), mDelegate(delegate),
28-
mTimerDelegate(timerDelegate)
27+
ChimeDevice::ChimeDevice(TimerDelegate & timerDelegate, Span<const Sound> sounds) :
28+
SingleEndpointDevice(Span<const DataModel::DeviceTypeEntry>(&Device::Type::kChime, 1)), mTimerDelegate(timerDelegate),
29+
mSounds(sounds)
2930
{}
3031

3132
CHIP_ERROR ChimeDevice::Register(chip::EndpointId endpoint, CodeDrivenDataModelProvider & provider, EndpointId parentId)
@@ -35,7 +36,7 @@ CHIP_ERROR ChimeDevice::Register(chip::EndpointId endpoint, CodeDrivenDataModelP
3536
mIdentifyCluster.Create(IdentifyCluster::Config(endpoint, mTimerDelegate));
3637
ReturnErrorOnFailure(provider.AddCluster(mIdentifyCluster.Registration()));
3738

38-
mChimeCluster.Create(endpoint, mDelegate);
39+
mChimeCluster.Create(endpoint, *this);
3940
ReturnErrorOnFailure(provider.AddCluster(mChimeCluster.Registration()));
4041

4142
return provider.AddEndpoint(mEndpointRegistration);
@@ -62,5 +63,45 @@ Clusters::ChimeCluster & ChimeDevice::ChimeCluster()
6263
return mChimeCluster.Cluster();
6364
}
6465

66+
CHIP_ERROR ChimeDevice::GetChimeSoundByIndex(uint8_t chimeIndex, uint8_t & chimeID, MutableCharSpan & name)
67+
{
68+
if (chimeIndex >= mSounds.size())
69+
{
70+
return CHIP_ERROR_PROVIDER_LIST_EXHAUSTED;
71+
}
72+
73+
const auto & sound = mSounds[chimeIndex];
74+
chimeID = sound.id;
75+
return CopyCharSpanToMutableCharSpan(sound.name, name);
76+
}
77+
78+
CHIP_ERROR ChimeDevice::GetChimeIDByIndex(uint8_t chimeIndex, uint8_t & chimeID)
79+
{
80+
if (chimeIndex >= mSounds.size())
81+
{
82+
return CHIP_ERROR_PROVIDER_LIST_EXHAUSTED;
83+
}
84+
85+
chimeID = mSounds[chimeIndex].id;
86+
return CHIP_NO_ERROR;
87+
}
88+
89+
Protocols::InteractionModel::Status ChimeDevice::PlayChimeSound(uint8_t chimeID)
90+
{
91+
CharSpan soundName = "Unknown"_span;
92+
for (const auto & sound : mSounds)
93+
{
94+
if (sound.id == chimeID)
95+
{
96+
soundName = sound.name;
97+
break;
98+
}
99+
}
100+
101+
ChipLogProgress(AppServer, "ChimeDevice: Playing sound %s", chip::NullTerminated(soundName).c_str());
102+
103+
return Protocols::InteractionModel::Status::Success;
104+
}
105+
65106
} // namespace app
66107
} // namespace chip

examples/all-devices-app/all-devices-common/devices/chime/ChimeDevice.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,23 @@
2020
#include <app/clusters/identify-server/IdentifyCluster.h>
2121
#include <data-model-providers/codedriven/CodeDrivenDataModelProvider.h>
2222
#include <devices/interface/SingleEndpointDevice.h>
23+
#include <lib/support/Span.h>
2324
#include <lib/support/TimerDelegate.h>
2425

2526
namespace chip {
2627
namespace app {
2728

28-
class ChimeDevice : public SingleEndpointDevice
29+
class ChimeDevice : public SingleEndpointDevice, public Clusters::ChimeDelegate
2930
{
3031
public:
31-
ChimeDevice(Clusters::ChimeDelegate & delegate, TimerDelegate & timerDelegate);
32+
struct Sound
33+
{
34+
uint8_t id;
35+
CharSpan name;
36+
};
37+
38+
// Note: sounds array must outlive the ChimeDevice lifetime (i.e. often static or similar)
39+
ChimeDevice(TimerDelegate & timerDelegate, Span<const Sound> sounds);
3240
~ChimeDevice() override = default;
3341

3442
CHIP_ERROR Register(chip::EndpointId endpoint, CodeDrivenDataModelProvider & provider,
@@ -37,9 +45,14 @@ class ChimeDevice : public SingleEndpointDevice
3745

3846
Clusters::ChimeCluster & ChimeCluster();
3947

48+
// ChimeDelegate
49+
CHIP_ERROR GetChimeSoundByIndex(uint8_t chimeIndex, uint8_t & chimeID, MutableCharSpan & name) override;
50+
CHIP_ERROR GetChimeIDByIndex(uint8_t chimeIndex, uint8_t & chimeID) override;
51+
virtual Protocols::InteractionModel::Status PlayChimeSound(uint8_t chimeID) override;
52+
4053
protected:
41-
Clusters::ChimeDelegate & mDelegate;
4254
TimerDelegate & mTimerDelegate;
55+
Span<const Sound> mSounds;
4356
LazyRegisteredServerCluster<Clusters::IdentifyCluster> mIdentifyCluster;
4457
LazyRegisteredServerCluster<Clusters::ChimeCluster> mChimeCluster;
4558
};

examples/all-devices-app/all-devices-common/devices/chime/impl/BUILD.gn

Lines changed: 0 additions & 29 deletions
This file was deleted.

examples/all-devices-app/all-devices-common/devices/chime/impl/LoggingChimeDevice.cpp

Lines changed: 0 additions & 75 deletions
This file was deleted.

examples/all-devices-app/all-devices-common/devices/chime/impl/LoggingChimeDevice.h

Lines changed: 0 additions & 57 deletions
This file was deleted.

examples/all-devices-app/all-devices-common/devices/device-factory/BUILD.gn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ source_set("device-factory") {
2020

2121
public_deps = [
2222
"${chip_root}/examples/all-devices-app/all-devices-common/devices/boolean-state-sensor",
23-
"${chip_root}/examples/all-devices-app/all-devices-common/devices/chime/impl:logging",
23+
"${chip_root}/examples/all-devices-app/all-devices-common/devices/chime",
2424
"${chip_root}/examples/all-devices-app/all-devices-common/devices/dimmable-light/impl:logging",
2525
"${chip_root}/examples/all-devices-app/all-devices-common/devices/occupancy-sensor/impl:toggling",
2626
"${chip_root}/examples/all-devices-app/all-devices-common/devices/on-off-light",

examples/all-devices-app/all-devices-common/devices/device-factory/DeviceFactory.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include <devices/Types.h>
2121
#include <devices/boolean-state-sensor/BooleanStateSensorDevice.h>
22-
#include <devices/chime/impl/LoggingChimeDevice.h>
22+
#include <devices/chime/ChimeDevice.h>
2323
#include <devices/dimmable-light/impl/LoggingDimmableLightDevice.h>
2424
#include <devices/occupancy-sensor/impl/TogglingOccupancySensorDevice.h>
2525
#include <devices/on-off-light/LoggingOnOffLightDevice.h>
@@ -60,6 +60,11 @@ class DeviceFactory
6060

6161
void Init(const Context & context) { mContext.emplace(context); }
6262

63+
void RegisterCreator(const std::string & deviceTypeArg, DeviceCreator && creator)
64+
{
65+
mRegistry[deviceTypeArg] = std::move(creator);
66+
}
67+
6368
bool IsValidDevice(const std::string & deviceTypeArg) { return mRegistry.find(deviceTypeArg) != mRegistry.end(); }
6469

6570
std::unique_ptr<DeviceInterface> Create(const std::string & deviceTypeArg)
@@ -105,13 +110,20 @@ class DeviceFactory
105110
&mContext->timerDelegate, Span<const DataModel::DeviceTypeEntry>(&Device::Type::kWaterLeakDetector, 1));
106111
};
107112
mRegistry["occupancy-sensor"] = []() { return std::make_unique<TogglingOccupancySensorDevice>(); };
108-
mRegistry["chime"] = []() { return std::make_unique<LoggingChimeDevice>(); };
109-
mRegistry["dimmable-light"] = [this]() {
113+
mRegistry["chime"] = [this]() {
114+
VerifyOrDie(mContext.has_value());
115+
static const ChimeDevice::Sound kDefaultSounds[] = {
116+
{ 0, "Ding Dong"_span },
117+
{ 1, "Ring Ring"_span },
118+
};
119+
return std::make_unique<ChimeDevice>(mContext->timerDelegate, Span<const ChimeDevice::Sound>(kDefaultSounds));
120+
};
121+
mRegistry["dimmable-light"] = [this]() {
110122
VerifyOrDie(mContext.has_value());
111123
return std::make_unique<LoggingDimmableLightDevice>(LoggingDimmableLightDevice::Context{
112-
.groupDataProvider = mContext->groupDataProvider,
113-
.fabricTable = mContext->fabricTable,
114-
.timerDelegate = mContext->timerDelegate,
124+
.groupDataProvider = mContext->groupDataProvider,
125+
.fabricTable = mContext->fabricTable,
126+
.timerDelegate = mContext->timerDelegate,
115127
});
116128
};
117129
mRegistry["on-off-light"] = [this]() {

examples/all-devices-app/esp32/main/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ set(SRC_DIRS_LIST
4343
# all-devices-common device modules
4444
"${ALL_DEVICES_COMMON_DIR}/devices/boolean-state-sensor"
4545
"${ALL_DEVICES_COMMON_DIR}/devices/chime"
46-
"${ALL_DEVICES_COMMON_DIR}/devices/chime/impl"
4746
"${ALL_DEVICES_COMMON_DIR}/devices/interface"
4847
"${ALL_DEVICES_COMMON_DIR}/devices/occupancy-sensor"
4948
"${ALL_DEVICES_COMMON_DIR}/devices/occupancy-sensor/impl"

examples/all-devices-app/posix/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import("${chip_root}/src/platform/device.gni")
1818

1919
executable("all-devices-app") {
2020
sources = [
21+
"PosixChimeDevice.cpp",
2122
"include/CHIPProjectAppConfig.h",
2223
"main.cpp",
2324
]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
*
3+
* Copyright (c) 2026 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#include <PosixChimeDevice.h>
18+
#include <lib/support/logging/CHIPLogging.h>
19+
20+
namespace chip {
21+
namespace app {
22+
23+
Protocols::InteractionModel::Status PosixChimeDevice::PlayChimeSound(uint8_t chimeID)
24+
{
25+
// Call base class to log the default message
26+
auto status = ChimeDevice::PlayChimeSound(chimeID);
27+
28+
// TODO: play a real sound on POSIX (Linux/Darwin)
29+
ChipLogProgress(DeviceLayer, "PosixChimeDevice: TODO: Play real sound for ID %d", chimeID);
30+
31+
return status;
32+
}
33+
34+
} // namespace app
35+
} // namespace chip

0 commit comments

Comments
 (0)