From 9080cd3b02e862c8ea0699a13c74403c09861c6f Mon Sep 17 00:00:00 2001
From: Marius Tache <102153746+marius-alex-tache@users.noreply.github.com>
Date: Fri, 5 Apr 2024 02:54:15 +0300
Subject: [PATCH] [NXP] Update smu2 manager api (#32799)

* [k32w1] Remove persistent storage delegate usage in SMU2 manager

SMU2 manager can use the KeyValueStoreManager API directly, without needing
a persistent storage delegate instance.

Signed-off-by: marius-alex-tache <marius.tache@nxp.com>

* [k32w1] Update SMU2 init usage in lighting app

Signed-off-by: marius-alex-tache <marius.tache@nxp.com>

---------

Signed-off-by: marius-alex-tache <marius.tache@nxp.com>
---
 .../nxp/k32w/k32w1/main/AppTask.cpp           |  2 +-
 src/platform/nxp/k32w/k32w1/SMU2Manager.cpp   | 19 +++++++++----------
 src/platform/nxp/k32w/k32w1/SMU2Manager.h     |  3 +--
 3 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/examples/lighting-app/nxp/k32w/k32w1/main/AppTask.cpp b/examples/lighting-app/nxp/k32w/k32w1/main/AppTask.cpp
index 96f5a5b96b3248..4c1d0875470ee4 100644
--- a/examples/lighting-app/nxp/k32w/k32w1/main/AppTask.cpp
+++ b/examples/lighting-app/nxp/k32w/k32w1/main/AppTask.cpp
@@ -236,7 +236,7 @@ void AppTask::InitServer(intptr_t arg)
 #endif
 
 #if defined(USE_SMU2_DYNAMIC)
-    VerifyOrDie(SMU2::Init(initParams.persistentStorageDelegate) == CHIP_NO_ERROR);
+    VerifyOrDie(SMU2::Init() == CHIP_NO_ERROR);
 #endif
 
     // Init ZCL Data Model and start server
diff --git a/src/platform/nxp/k32w/k32w1/SMU2Manager.cpp b/src/platform/nxp/k32w/k32w1/SMU2Manager.cpp
index f4e0216bb8955a..f7c54ea7c796b7 100644
--- a/src/platform/nxp/k32w/k32w1/SMU2Manager.cpp
+++ b/src/platform/nxp/k32w/k32w1/SMU2Manager.cpp
@@ -25,10 +25,13 @@
  */
 
 #include "SMU2Manager.h"
+
 #include <platform/CHIPDeviceLayer.h>
+#include <platform/KeyValueStoreManager.h>
 
 using namespace chip::DeviceLayer;
 using namespace chip::DeviceLayer::Internal;
+using namespace chip::DeviceLayer::PersistedStorage;
 
 namespace chip::SMU2 {
 namespace {
@@ -39,8 +42,6 @@ static const uint32_t AREA_SIZE  = (AREA_END - AREA_START);
 
 uint8_t mAreaId = 0;
 
-PersistentStorageDelegate * mStorage = nullptr;
-
 memAreaCfg_t mAreaDescriptor;
 bool mDeviceCommissioned = false;
 bool mUseAllocator       = false;
@@ -92,7 +93,7 @@ void EventHandler(const ChipDeviceEvent * event, intptr_t)
         if (mDeviceCommissioned)
         {
             mUseAllocator = true;
-            mStorage->SyncSetKeyValue(GetSMU2AllocatorKey().KeyName(), (void *) &mUseAllocator, (uint16_t) sizeof(mUseAllocator));
+            KeyValueStoreMgr().Put(GetSMU2AllocatorKey().KeyName(), (void *) &mUseAllocator, (uint16_t) sizeof(mUseAllocator));
             ResetBLEController();
             RegisterArea();
         }
@@ -103,22 +104,20 @@ void EventHandler(const ChipDeviceEvent * event, intptr_t)
 
 } // anonymous namespace
 
-CHIP_ERROR Init(PersistentStorageDelegate * storage)
+CHIP_ERROR Init()
 {
     CHIP_ERROR err = CHIP_NO_ERROR;
     uint16_t size  = (uint16_t) sizeof(mUseAllocator);
-    mStorage       = storage;
-
-    VerifyOrReturnError(storage != nullptr, CHIP_ERROR_INCORRECT_STATE);
 
     PlatformMgr().AddEventHandler(EventHandler, reinterpret_cast<intptr_t>(nullptr));
 
-    err = mStorage->SyncGetKeyValue(GetSMU2AllocatorKey().KeyName(), (void *) &mUseAllocator, size);
+    size_t bytesRead = 0;
+    err              = KeyValueStoreMgr().Get(GetSMU2AllocatorKey().KeyName(), (void *) &mUseAllocator, size, &bytesRead);
 
     if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
     {
         mUseAllocator = false;
-        err           = mStorage->SyncSetKeyValue(GetSMU2AllocatorKey().KeyName(), (void *) &mUseAllocator, size);
+        err           = KeyValueStoreMgr().Put(GetSMU2AllocatorKey().KeyName(), (void *) &mUseAllocator, size);
     }
     ReturnErrorOnFailure(err);
 
@@ -137,7 +136,7 @@ CHIP_ERROR Deactivate(void)
     if (mUseAllocator)
     {
         mUseAllocator = false;
-        err           = mStorage->SyncDeleteKeyValue(GetSMU2AllocatorKey().KeyName());
+        err           = KeyValueStoreMgr().Delete(GetSMU2AllocatorKey().KeyName());
         ReturnErrorOnFailure(err);
 
         UnregisterArea();
diff --git a/src/platform/nxp/k32w/k32w1/SMU2Manager.h b/src/platform/nxp/k32w/k32w1/SMU2Manager.h
index 7044d452727476..141229b8e6a932 100644
--- a/src/platform/nxp/k32w/k32w1/SMU2Manager.h
+++ b/src/platform/nxp/k32w/k32w1/SMU2Manager.h
@@ -27,12 +27,11 @@
 #pragma once
 
 #include "fsl_component_mem_manager.h"
-#include <lib/core/CHIPPersistentStorageDelegate.h>
 #include <lib/support/DefaultStorageKeyAllocator.h>
 
 namespace chip::SMU2 {
 
-CHIP_ERROR Init(PersistentStorageDelegate * storage);
+CHIP_ERROR Init();
 CHIP_ERROR Deactivate(void);
 void * Allocate(size_t size);