Skip to content

Commit 6f79e03

Browse files
committed
fix(matter): fixes identify and double begin() call
1 parent ba8d04e commit 6f79e03

16 files changed

+134
-42
lines changed

Diff for: libraries/Matter/examples/MatterOnIdentify/MatterOnIdentify.ino

+47-11
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,31 @@ uint32_t button_time_stamp = 0; // debouncing control
5151
bool button_state = false; // false = released | true = pressed
5252
const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission
5353

54+
// Identify Flag and blink time - Blink the LED
55+
const uint8_t identifyLedPin = ledPin; // uses the same LED as the Light - change if needed
56+
volatile bool identifyFlag = false; // Flag to start the Blink when in Identify state
57+
bool identifyBlink = false; // Blink state when in Identify state
58+
5459
// Matter Protocol Endpoint (On/OFF Light) Callback
5560
bool onOffLightCallback(bool state) {
5661
digitalWrite(ledPin, state ? HIGH : LOW);
5762
// This callback must return the success state to Matter core
5863
return true;
5964
}
6065

61-
bool onIdentifyLightCallback(bool identifyIsActive, uint8_t counter) {
62-
log_i("Identify Cluster is %s, counter: %d", identifyIsActive ? "Active" : "Inactive", counter);
66+
// Identification shall be done by Blink in Red or just the GPIO when no LED_BUILTIN is not defined
67+
bool onIdentifyLightCallback(bool identifyIsActive) {
68+
log_i("Identify Cluster is %s", identifyIsActive ? "Active" : "Inactive");
6369
if (identifyIsActive) {
64-
// Start Blinking the light
65-
OnOffLight.toggle();
70+
// Start Blinking the light in loop()
71+
identifyFlag = true;
72+
identifyBlink = !OnOffLight; // Start with the inverted light state
6673
} else {
6774
// Stop Blinking and restore the light to the its last state
68-
OnOffLight.updateAccessory();
75+
identifyFlag = false;
76+
// force returning to the original state by toggling the light twice
77+
OnOffLight.toggle();
78+
OnOffLight.toggle();
6979
}
7080
return true;
7181
}
@@ -76,12 +86,16 @@ void setup() {
7686
// Initialize the LED GPIO
7787
pinMode(ledPin, OUTPUT);
7888

89+
Serial.begin(115200);
90+
7991
// Manually connect to WiFi
8092
WiFi.begin(ssid, password);
8193
// Wait for connection
8294
while (WiFi.status() != WL_CONNECTED) {
8395
delay(500);
96+
Serial.print(".");
8497
}
98+
Serial.println();
8599

86100
// Initialize at least one Matter EndPoint
87101
OnOffLight.begin();
@@ -95,16 +109,38 @@ void setup() {
95109
// Matter beginning - Last step, after all EndPoints are initialized
96110
Matter.begin();
97111

112+
// Check Matter Accessory Commissioning state, which may change during execution of loop()
98113
if (!Matter.isDeviceCommissioned()) {
99-
log_i("Matter Node is not commissioned yet.");
100-
log_i("Initiate the device discovery in your Matter environment.");
101-
log_i("Commission it to your Matter hub with the manual pairing code or QR code");
102-
log_i("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
103-
log_i("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
114+
Serial.println("");
115+
Serial.println("Matter Node is not commissioned yet.");
116+
Serial.println("Initiate the device discovery in your Matter environment.");
117+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
118+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
119+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
120+
// waits for Matter Occupancy Sensor Commissioning.
121+
uint32_t timeCount = 0;
122+
while (!Matter.isDeviceCommissioned()) {
123+
delay(100);
124+
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
125+
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
126+
}
127+
}
128+
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
104129
}
105130
}
106131

107132
void loop() {
133+
// check if the Ligth is in identify state and blink it every 500ms (delay loop time)
134+
if (identifyFlag) {
135+
#ifdef LED_BUILTIN
136+
uint8_t brightness = 32 * identifyBlink;
137+
rgbLedWrite(identifyLedPin, brightness, 0, 0);
138+
#else
139+
digitalWrite(identifyLedPin, identifyBlink ? HIGH : LOW);
140+
#endif
141+
identifyBlink = !identifyBlink;
142+
}
143+
108144
// Check if the button has been pressed
109145
if (digitalRead(buttonPin) == LOW && !button_state) {
110146
// deals with button debouncing
@@ -124,5 +160,5 @@ void loop() {
124160
button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so
125161
}
126162

127-
delay(500);
163+
delay(500); // works as a debounce for the button and also for the LED blink
128164
}

Diff for: libraries/Matter/src/Matter.cpp

+1-9
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ static bool _matter_has_started = false;
3030
static node::config_t node_config;
3131
static node_t *deviceNode = NULL;
3232

33-
typedef void *app_driver_handle_t;
34-
esp_err_t matter_light_attribute_update(
35-
app_driver_handle_t driver_handle, uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val
36-
);
37-
3833
// This callback is called for every attribute update. The callback implementation shall
3934
// handle the desired attributes and return an appropriate error code. If the attribute
4035
// is not of your interest, please do not return an error code and strictly return ESP_OK.
@@ -72,22 +67,19 @@ static esp_err_t app_identification_cb(identification::callback_type_t type, uin
7267
esp_err_t err = ESP_OK;
7368
MatterEndPoint *ep = (MatterEndPoint *)priv_data; // endpoint pointer to base class
7469
// Identify the endpoint sending a counter to the application
75-
static uint8_t counter = 0;
7670
bool identifyIsActive = false;
7771

7872
if (type == identification::callback_type_t::START) {
7973
log_v("Identification callback: START");
80-
counter = 0;
8174
identifyIsActive = true;
8275
} else if (type == identification::callback_type_t::EFFECT) {
8376
log_v("Identification callback: EFFECT");
84-
counter++;
8577
} else if (type == identification::callback_type_t::STOP) {
8678
identifyIsActive = false;
8779
log_v("Identification callback: STOP");
8880
}
8981
if (ep != NULL) {
90-
err = ep->endpointIdentifyCB(endpoint_id, identifyIsActive, counter) ? ESP_OK : ESP_FAIL;
82+
err = ep->endpointIdentifyCB(endpoint_id, identifyIsActive) ? ESP_OK : ESP_FAIL;
9183
}
9284

9385
return err;

Diff for: libraries/Matter/src/MatterEndPoint.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ class MatterEndPoint {
103103
virtual bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) = 0;
104104

105105
// This callback is invoked when clients interact with the Identify Cluster of an specific endpoint.
106-
bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) {
106+
bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled) {
107107
if (_onEndPointIdentifyCB) {
108-
return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter);
108+
return _onEndPointIdentifyCB(identifyIsEnabled);
109109
}
110110
return true;
111111
}
112112
// User callaback for the Identify Cluster functionality
113-
using EndPointIdentifyCB = std::function<bool(bool, uint8_t)>;
113+
using EndPointIdentifyCB = std::function<bool(bool)>;
114114
void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) {
115115
_onEndPointIdentifyCB = onEndPointIdentifyCB;
116116
}

Diff for: libraries/Matter/src/MatterEndpoints/MatterColorLight.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,13 @@ MatterColorLight::~MatterColorLight() {
162162

163163
bool MatterColorLight::begin(bool initialState, espHsvColor_t _colorHSV) {
164164
ArduinoMatter::_init();
165-
rgb_color_light::config_t light_config;
166165

166+
if (getEndPointId() != 0) {
167+
log_e("Matter RGB Color Light with Endpoint Id %d device has already been created.", getEndPointId());
168+
return false;
169+
}
170+
171+
rgb_color_light::config_t light_config;
167172
light_config.on_off.on_off = initialState;
168173
light_config.on_off.lighting.start_up_on_off = nullptr;
169174
onOffState = initialState;

Diff for: libraries/Matter/src/MatterEndpoints/MatterColorTemperatureLight.cpp

+16-11
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ using namespace chip::app::Clusters;
2626
bool MatterColorTemperatureLight::attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) {
2727
bool ret = true;
2828
if (!started) {
29-
log_e("Matter CW_WW Light device has not begun.");
29+
log_e("Matter Temperature Light device has not begun.");
3030
return false;
3131
}
3232

33-
log_d("CW_WW Attr update callback: endpoint: %u, cluster: %u, attribute: %u, val: %u", endpoint_id, cluster_id, attribute_id, val->val.u32);
33+
log_d("Temperature Attr update callback: endpoint: %u, cluster: %u, attribute: %u, val: %u", endpoint_id, cluster_id, attribute_id, val->val.u32);
3434

3535
if (endpoint_id == getEndPointId()) {
3636
switch (cluster_id) {
3737
case OnOff::Id:
3838
if (attribute_id == OnOff::Attributes::OnOff::Id) {
39-
log_d("CW_WW Light On/Off State changed to %d", val->val.b);
39+
log_d("Temperature Light On/Off State changed to %d", val->val.b);
4040
if (_onChangeOnOffCB != NULL) {
4141
ret &= _onChangeOnOffCB(val->val.b);
4242
}
@@ -50,7 +50,7 @@ bool MatterColorTemperatureLight::attributeChangeCB(uint16_t endpoint_id, uint32
5050
break;
5151
case LevelControl::Id:
5252
if (attribute_id == LevelControl::Attributes::CurrentLevel::Id) {
53-
log_d("CW_WW Light Brightness changed to %d", val->val.u8);
53+
log_d("Temperature Light Brightness changed to %d", val->val.u8);
5454
if (_onChangeBrightnessCB != NULL) {
5555
ret &= _onChangeBrightnessCB(val->val.u8);
5656
}
@@ -64,7 +64,7 @@ bool MatterColorTemperatureLight::attributeChangeCB(uint16_t endpoint_id, uint32
6464
break;
6565
case ColorControl::Id:
6666
if (attribute_id == ColorControl::Attributes::ColorTemperatureMireds::Id) {
67-
log_d("CW_WW Light Temperature changed to %d", val->val.u16);
67+
log_d("Temperature Light Temperature changed to %d", val->val.u16);
6868
if (_onChangeTemperatureCB != NULL) {
6969
ret &= _onChangeTemperatureCB(val->val.u16);
7070
}
@@ -89,8 +89,13 @@ MatterColorTemperatureLight::~MatterColorTemperatureLight() {
8989

9090
bool MatterColorTemperatureLight::begin(bool initialState, uint8_t brightness, uint16_t ColorTemperature) {
9191
ArduinoMatter::_init();
92-
color_temperature_light::config_t light_config;
9392

93+
if (getEndPointId() != 0) {
94+
log_e("Matter Temperature Light with Endpoint Id %d device has already been created.", getEndPointId());
95+
return false;
96+
}
97+
98+
color_temperature_light::config_t light_config;
9499
light_config.on_off.on_off = initialState;
95100
light_config.on_off.lighting.start_up_on_off = nullptr;
96101
onOffState = initialState;
@@ -108,12 +113,12 @@ bool MatterColorTemperatureLight::begin(bool initialState, uint8_t brightness, u
108113
// endpoint handles can be used to add/modify clusters.
109114
endpoint_t *endpoint = color_temperature_light::create(node::get(), &light_config, ENDPOINT_FLAG_NONE, (void *)this);
110115
if (endpoint == nullptr) {
111-
log_e("Failed to create CW_WW light endpoint");
116+
log_e("Failed to create Temperature Light endpoint");
112117
return false;
113118
}
114119

115120
setEndPointId(endpoint::get_id(endpoint));
116-
log_i("CW_WW Light created with endpoint_id %d", getEndPointId());
121+
log_i("Temperature Light created with endpoint_id %d", getEndPointId());
117122

118123
/* Mark deferred persistence for some attributes that might be changed rapidly */
119124
cluster_t *level_control_cluster = cluster::get(endpoint, LevelControl::Id);
@@ -134,7 +139,7 @@ void MatterColorTemperatureLight::end() {
134139

135140
bool MatterColorTemperatureLight::setOnOff(bool newState) {
136141
if (!started) {
137-
log_e("Matter CW_WW Light device has not begun.");
142+
log_e("Matter Temperature Light device has not begun.");
138143
return false;
139144
}
140145

@@ -175,7 +180,7 @@ bool MatterColorTemperatureLight::toggle() {
175180

176181
bool MatterColorTemperatureLight::setBrightness(uint8_t newBrightness) {
177182
if (!started) {
178-
log_w("Matter CW_WW Light device has not begun.");
183+
log_w("Matter Temperature Light device has not begun.");
179184
return false;
180185
}
181186

@@ -206,7 +211,7 @@ uint8_t MatterColorTemperatureLight::getBrightness() {
206211

207212
bool MatterColorTemperatureLight::setColorTemperature(uint16_t newTemperature) {
208213
if (!started) {
209-
log_w("Matter CW_WW Light device has not begun.");
214+
log_w("Matter Temperature Light device has not begun.");
210215
return false;
211216
}
212217

Diff for: libraries/Matter/src/MatterEndpoints/MatterContactSensor.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ MatterContactSensor::~MatterContactSensor() {
4343
bool MatterContactSensor::begin(bool _contactState) {
4444
ArduinoMatter::_init();
4545

46+
if (getEndPointId() != 0) {
47+
log_e("Matter Contact Sensor with Endpoint Id %d device has already been created.", getEndPointId());
48+
return false;
49+
}
50+
4651
contact_sensor::config_t contact_sensor_config;
4752
contact_sensor_config.boolean_state.state_value = _contactState;
4853

Diff for: libraries/Matter/src/MatterEndpoints/MatterDimmableLight.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ MatterDimmableLight::~MatterDimmableLight() {
7575

7676
bool MatterDimmableLight::begin(bool initialState, uint8_t brightness) {
7777
ArduinoMatter::_init();
78-
dimmable_light::config_t light_config;
78+
if (getEndPointId() != 0) {
79+
log_e("Matter Dimmable Light with Endpoint Id %d device has already been created.", getEndPointId());
80+
return false;
81+
}
7982

83+
dimmable_light::config_t light_config;
8084
light_config.on_off.on_off = initialState;
8185
light_config.on_off.lighting.start_up_on_off = nullptr;
8286
onOffState = initialState;

Diff for: libraries/Matter/src/MatterEndpoints/MatterEnhancedColorLight.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,13 @@ MatterEnhancedColorLight::~MatterEnhancedColorLight() {
178178

179179
bool MatterEnhancedColorLight::begin(bool initialState, espHsvColor_t _colorHSV, uint8_t brightness, uint16_t ColorTemperature) {
180180
ArduinoMatter::_init();
181-
enhanced_color_light::config_t light_config;
182181

182+
if (getEndPointId() != 0) {
183+
log_e("Matter Enhanced ColorLight with Endpoint Id %d device has already been created.", getEndPointId());
184+
return false;
185+
}
186+
187+
enhanced_color_light::config_t light_config;
183188
light_config.on_off.on_off = initialState;
184189
light_config.on_off.lighting.start_up_on_off = nullptr;
185190
onOffState = initialState;

Diff for: libraries/Matter/src/MatterEndpoints/MatterFan.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ bool MatterFan::attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uin
8585
bool MatterFan::begin(uint8_t percent, FanMode_t fanMode, FanModeSequence_t fanModeSeq) {
8686
ArduinoMatter::_init();
8787

88-
// endpoint handles can be used to add/modify clusters.
88+
if (getEndPointId() != 0) {
89+
log_e("Matter Fan with Endpoint Id %d device has already been created.", getEndPointId());
90+
return false;
91+
}
92+
93+
// endpoint handles can be used to add/modify clusters.
8994
fan::config_t fan_config;
9095
fan_config.fan_control.fan_mode = fanMode;
9196
fan_config.fan_control.percent_current = percent;

Diff for: libraries/Matter/src/MatterEndpoints/MatterGenericSwitch.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,17 @@ bool MatterGenericSwitch::attributeChangeCB(uint16_t endpoint_id, uint32_t clust
4242

4343
bool MatterGenericSwitch::begin() {
4444
ArduinoMatter::_init();
45-
generic_switch::config_t switch_config;
4645

46+
if (getEndPointId() != 0) {
47+
log_e("Matter Generic Switch with Endpoint Id %d device has already been created.", getEndPointId());
48+
return false;
49+
}
50+
51+
generic_switch::config_t switch_config;
4752
// endpoint handles can be used to add/modify clusters.
4853
endpoint_t *endpoint = generic_switch::create(node::get(), &switch_config, ENDPOINT_FLAG_NONE, (void *)this);
4954
if (endpoint == nullptr) {
50-
log_e("Failed to create Generic switch endpoint");
55+
log_e("Failed to create Generic Switch endpoint");
5156
return false;
5257
}
5358
// Add group cluster to the switch endpoint

Diff for: libraries/Matter/src/MatterEndpoints/MatterHumiditySensor.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ MatterHumiditySensor::~MatterHumiditySensor() {
4343
bool MatterHumiditySensor::begin(uint16_t _rawHumidity) {
4444
ArduinoMatter::_init();
4545

46+
if (getEndPointId() != 0) {
47+
log_e("Matter Humidity Sensor with Endpoint Id %d device has already been created.", getEndPointId());
48+
return false;
49+
}
50+
4651
// is it a valid percentage value?
4752
if (_rawHumidity > 10000) {
4853
log_e("Humidity Sensor Percentage value out of range [0..100].");

Diff for: libraries/Matter/src/MatterEndpoints/MatterOccupancySensor.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ MatterOccupancySensor::~MatterOccupancySensor() {
5252
bool MatterOccupancySensor::begin(bool _occupancyState, OccupancySensorType_t _occupancySensorType) {
5353
ArduinoMatter::_init();
5454

55+
if (getEndPointId() != 0) {
56+
log_e("Matter Occupancy Sensor with Endpoint Id %d device has already been created.", getEndPointId());
57+
return false;
58+
}
59+
5560
occupancy_sensor::config_t occupancy_sensor_config;
5661
occupancy_sensor_config.occupancy_sensing.occupancy = _occupancyState;
5762
occupancy_sensor_config.occupancy_sensing.occupancy_sensor_type = _occupancySensorType;

Diff for: libraries/Matter/src/MatterEndpoints/MatterOnOffLight.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,13 @@ MatterOnOffLight::~MatterOnOffLight() {
5959

6060
bool MatterOnOffLight::begin(bool initialState) {
6161
ArduinoMatter::_init();
62-
on_off_light::config_t light_config;
6362

63+
if (getEndPointId() != 0) {
64+
log_e("Matter On-Off Light with Endpoint Id %d device has already been created.", getEndPointId());
65+
return false;
66+
}
67+
68+
on_off_light::config_t light_config;
6469
light_config.on_off.on_off = initialState;
6570
light_config.on_off.lighting.start_up_on_off = nullptr;
6671
onOffState = initialState;

Diff for: libraries/Matter/src/MatterEndpoints/MatterOnOffPlugin.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,13 @@ MatterOnOffPlugin::~MatterOnOffPlugin() {
5959

6060
bool MatterOnOffPlugin::begin(bool initialState) {
6161
ArduinoMatter::_init();
62-
on_off_plugin_unit::config_t plugin_config;
6362

63+
if (getEndPointId() != 0) {
64+
log_e("Matter On-Off Plugin with Endpoint Id %d device has already been created.", getEndPointId());
65+
return false;
66+
}
67+
68+
on_off_plugin_unit::config_t plugin_config;
6469
plugin_config.on_off.on_off = initialState;
6570
plugin_config.on_off.lighting.start_up_on_off = nullptr;
6671

0 commit comments

Comments
 (0)