From fcfc3ecb3780983fe5106334096849747ccbe290 Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Sun, 9 Jul 2023 19:36:36 +0200 Subject: [PATCH] Matter fix session not being removed from memory (#19081) * Matter fix session not being removed from memory * Fix --- .../src/embedded/Matter_Commissioning.be | 6 +-- .../src/embedded/Matter_Fabric.be | 4 +- .../solidified_Matter_Commissioning.h | 4 +- .../src/solidify/solidified_Matter_Fabric.h | 49 +++++++++++-------- 4 files changed, 36 insertions(+), 27 deletions(-) diff --git a/lib/libesp32/berry_matter/src/embedded/Matter_Commissioning.be b/lib/libesp32/berry_matter/src/embedded/Matter_Commissioning.be index df9ca5343..3202ce0e1 100644 --- a/lib/libesp32/berry_matter/src/embedded/Matter_Commissioning.be +++ b/lib/libesp32/berry_matter/src/embedded/Matter_Commissioning.be @@ -130,7 +130,7 @@ class Matter_Commisioning_Context # record the initiator_session_id session.__future_initiator_session_id = pbkdfparamreq.initiator_session_id session.__future_local_session_id = self.device.sessions.gen_local_session_id() - tasmota.log(format("MTR: New_Session(%6i) from '[%s]:%i'", session.__future_local_session_id, msg.remote_ip, msg.remote_port), 3) + tasmota.log(format("MTR: +Session (%6i) from '[%s]:%i'", session.__future_local_session_id, msg.remote_ip, msg.remote_port), 3) # prepare response var pbkdfparamresp = matter.PBKDFParamResponse() @@ -370,7 +370,7 @@ class Matter_Commisioning_Context session.set_mode_CASE() session.__future_initiator_session_id = sigma1.initiator_session_id # update initiator_session_id session.__future_local_session_id = self.device.sessions.gen_local_session_id() - tasmota.log(format("MTR: New_Session(%6i) from '[%s]:%i'", session.__future_local_session_id, msg.remote_ip, msg.remote_port), 3) + tasmota.log(format("MTR: +Session (%6i) from '[%s]:%i'", session.__future_local_session_id, msg.remote_ip, msg.remote_port), 3) # Generate and Send Sigma2_Resume session.shared_secret = session_resumption.shared_secret @@ -458,7 +458,7 @@ class Matter_Commisioning_Context session.__future_initiator_session_id = sigma1.initiator_session_id # update initiator_session_id session.__future_local_session_id = self.device.sessions.gen_local_session_id() - tasmota.log(format("MTR: New_Session(%6i) from '[%s]:%i'", session.__future_local_session_id, msg.remote_ip, msg.remote_port), 3) + tasmota.log(format("MTR: +Session (%6i) from '[%s]:%i'", session.__future_local_session_id, msg.remote_ip, msg.remote_port), 3) # tasmota.log("MTR: fabric="+matter.inspect(session._fabric), 4) # tasmota.log("MTR: no_private_key="+session.get_pk().tohex(), 4) diff --git a/lib/libesp32/berry_matter/src/embedded/Matter_Fabric.be b/lib/libesp32/berry_matter/src/embedded/Matter_Fabric.be index 9e08d6f34..fb149ff45 100644 --- a/lib/libesp32/berry_matter/src/embedded/Matter_Fabric.be +++ b/lib/libesp32/berry_matter/src/embedded/Matter_Fabric.be @@ -247,7 +247,9 @@ class Matter_Fabric : Matter_Expirable def add_session(s) if self._sessions.find(s) == nil while size(self._sessions) >= self._MAX_CASE - self._sessions.remove(self._sessions.find(self.get_oldest_session())) + var session_deleted = self.get_oldest_session() + self._sessions.remove(self._sessions.find(session_deleted)) + self._store.remove_session(session_deleted) end self._sessions.push(s) end diff --git a/lib/libesp32/berry_matter/src/solidify/solidified_Matter_Commissioning.h b/lib/libesp32/berry_matter/src/solidify/solidified_Matter_Commissioning.h index da660771d..7c5b07704 100644 --- a/lib/libesp32/berry_matter/src/solidify/solidified_Matter_Commissioning.h +++ b/lib/libesp32/berry_matter/src/solidify/solidified_Matter_Commissioning.h @@ -286,7 +286,7 @@ be_local_closure(Matter_Commisioning_Context_parse_PBKDFParamRequest, /* name /* K27 */ be_nested_str_weak(device), /* K28 */ be_nested_str_weak(sessions), /* K29 */ be_nested_str_weak(gen_local_session_id), - /* K30 */ be_nested_str_weak(MTR_X3A_X20New_Session_X28_X256i_X29_X20from_X20_X27_X5B_X25s_X5D_X3A_X25i_X27), + /* K30 */ be_nested_str_weak(MTR_X3A_X20_X2BSession_X20_X20_X20_X28_X256i_X29_X20from_X20_X27_X5B_X25s_X5D_X3A_X25i_X27), /* K31 */ be_nested_str_weak(remote_ip), /* K32 */ be_nested_str_weak(remote_port), /* K33 */ be_nested_str_weak(PBKDFParamResponse), @@ -1436,7 +1436,7 @@ be_local_closure(Matter_Commisioning_Context_parse_Sigma1, /* name */ /* K41 */ be_nested_str_weak(initiator_session_id), /* K42 */ be_nested_str_weak(__future_local_session_id), /* K43 */ be_nested_str_weak(gen_local_session_id), - /* K44 */ be_nested_str_weak(MTR_X3A_X20New_Session_X28_X256i_X29_X20from_X20_X27_X5B_X25s_X5D_X3A_X25i_X27), + /* K44 */ be_nested_str_weak(MTR_X3A_X20_X2BSession_X20_X20_X20_X28_X256i_X29_X20from_X20_X27_X5B_X25s_X5D_X3A_X25i_X27), /* K45 */ be_nested_str_weak(remote_ip), /* K46 */ be_nested_str_weak(remote_port), /* K47 */ be_nested_str_weak(resumption_id), diff --git a/lib/libesp32/berry_matter/src/solidify/solidified_Matter_Fabric.h b/lib/libesp32/berry_matter/src/solidify/solidified_Matter_Fabric.h index af3123804..f8d35d9da 100644 --- a/lib/libesp32/berry_matter/src/solidify/solidified_Matter_Fabric.h +++ b/lib/libesp32/berry_matter/src/solidify/solidified_Matter_Fabric.h @@ -151,44 +151,51 @@ be_local_closure(Matter_Fabric_add_session, /* name */ 0, /* has sup protos */ NULL, /* no sub protos */ 1, /* has constants */ - ( &(const bvalue[ 6]) { /* constants */ + ( &(const bvalue[ 8]) { /* constants */ /* K0 */ be_nested_str_weak(_sessions), /* K1 */ be_nested_str_weak(find), /* K2 */ be_nested_str_weak(_MAX_CASE), - /* K3 */ be_nested_str_weak(remove), - /* K4 */ be_nested_str_weak(get_oldest_session), - /* K5 */ be_nested_str_weak(push), + /* K3 */ be_nested_str_weak(get_oldest_session), + /* K4 */ be_nested_str_weak(remove), + /* K5 */ be_nested_str_weak(_store), + /* K6 */ be_nested_str_weak(remove_session), + /* K7 */ be_nested_str_weak(push), }), be_str_weak(add_session), &be_const_str_solidified, - ( &(const binstruction[27]) { /* code */ + ( &(const binstruction[32]) { /* code */ 0x88080100, // 0000 GETMBR R2 R0 K0 0x8C080501, // 0001 GETMET R2 R2 K1 0x5C100200, // 0002 MOVE R4 R1 0x7C080400, // 0003 CALL R2 2 0x4C0C0000, // 0004 LDNIL R3 0x1C080403, // 0005 EQ R2 R2 R3 - 0x780A0012, // 0006 JMPF R2 #001A + 0x780A0017, // 0006 JMPF R2 #001F 0x6008000C, // 0007 GETGBL R2 G12 0x880C0100, // 0008 GETMBR R3 R0 K0 0x7C080200, // 0009 CALL R2 1 0x880C0102, // 000A GETMBR R3 R0 K2 0x28080403, // 000B GE R2 R2 R3 - 0x780A0008, // 000C JMPF R2 #0016 - 0x88080100, // 000D GETMBR R2 R0 K0 - 0x8C080503, // 000E GETMET R2 R2 K3 - 0x88100100, // 000F GETMBR R4 R0 K0 - 0x8C100901, // 0010 GETMET R4 R4 K1 - 0x8C180104, // 0011 GETMET R6 R0 K4 - 0x7C180200, // 0012 CALL R6 1 - 0x7C100400, // 0013 CALL R4 2 - 0x7C080400, // 0014 CALL R2 2 - 0x7001FFF0, // 0015 JMP #0007 - 0x88080100, // 0016 GETMBR R2 R0 K0 - 0x8C080505, // 0017 GETMET R2 R2 K5 - 0x5C100200, // 0018 MOVE R4 R1 - 0x7C080400, // 0019 CALL R2 2 - 0x80000000, // 001A RET 0 + 0x780A000D, // 000C JMPF R2 #001B + 0x8C080103, // 000D GETMET R2 R0 K3 + 0x7C080200, // 000E CALL R2 1 + 0x880C0100, // 000F GETMBR R3 R0 K0 + 0x8C0C0704, // 0010 GETMET R3 R3 K4 + 0x88140100, // 0011 GETMBR R5 R0 K0 + 0x8C140B01, // 0012 GETMET R5 R5 K1 + 0x5C1C0400, // 0013 MOVE R7 R2 + 0x7C140400, // 0014 CALL R5 2 + 0x7C0C0400, // 0015 CALL R3 2 + 0x880C0105, // 0016 GETMBR R3 R0 K5 + 0x8C0C0706, // 0017 GETMET R3 R3 K6 + 0x5C140400, // 0018 MOVE R5 R2 + 0x7C0C0400, // 0019 CALL R3 2 + 0x7001FFEB, // 001A JMP #0007 + 0x88080100, // 001B GETMBR R2 R0 K0 + 0x8C080507, // 001C GETMET R2 R2 K7 + 0x5C100200, // 001D MOVE R4 R1 + 0x7C080400, // 001E CALL R2 2 + 0x80000000, // 001F RET 0 }) ) );