[efr32] remove unnecessary nvm3 calls (#5178)

This removes unnecessary open/close calls that were being made when
handling nvm operations.

In a normal mode of operation, the nvm open call should be made only
once during initialization.

The only way these changes could be verified was by building a binary,
flashing it on a node and making sure that the node's network operations
(like forming a network) were okay.
diff --git a/examples/platforms/efr32mg12/flash.c b/examples/platforms/efr32mg12/flash.c
index ffbaa20..2fa52c3 100644
--- a/examples/platforms/efr32mg12/flash.c
+++ b/examples/platforms/efr32mg12/flash.c
@@ -28,8 +28,7 @@
 
 /**
  * @file
- *   This file implements the OpenThread platform abstraction for the non-volatile
- *   storage for the EFR32 platform using the Silabs Nvm3 interface.
+ *   This file implements the OpenThread platform abstraction for the non-volatile storage.
  */
 
 #include "openthread-core-efr32-config.h"
@@ -101,13 +100,13 @@
 
 #else // Defaults to Silabs nvm3 system
 
-#include "nvm3.h"
 #include "nvm3_default.h"
 #include <string.h>
 #include <openthread/platform/settings.h>
 #include "common/code_utils.hpp"
+#include "common/logging.hpp"
 
-#define NVM3KEY_DOMAIN_OPENTHREAD 0x20000U // NVM3 Default Instance key space region for Thread stack
+#define NVM3KEY_DOMAIN_OPENTHREAD 0x20000U
 #define NUM_INDEXED_SETTINGS \
     OPENTHREAD_CONFIG_MLE_MAX_CHILDREN // Indexed key types are only supported for kKeyChildInfo (=='child table').
 #define ENUM_NVM3_KEY_LIST_SIZE 4      // List size used when enumerating nvm3 keys.
@@ -116,24 +115,13 @@
 static nvm3_ObjectKey_t makeNvm3ObjKey(uint16_t otSettingsKey, int index);
 static otError          mapNvm3Error(Ecode_t nvm3Res);
 
-extern nvm3_Init_t    nvm3_defaultInitData;
-extern nvm3_Handle_t *nvm3_defaultHandle;
-
 void otPlatSettingsInit(otInstance *aInstance)
 {
     OT_UNUSED_VARIABLE(aInstance);
 
-    otError err;
-    bool    needClose = false;
-
-    err = mapNvm3Error(nvm3_open(nvm3_defaultHandle, &nvm3_defaultInitData));
-    SuccessOrExit(err);
-    needClose = true;
-
-exit:
-    if (needClose)
+    if (mapNvm3Error(nvm3_open(nvm3_defaultHandle, nvm3_defaultInit)) != OT_ERROR_NONE)
     {
-        nvm3_close(nvm3_defaultHandle);
+        otLogDebgPlat("Error initializing nvm3 instance");
     }
 }
 
@@ -153,13 +141,8 @@
     OT_UNUSED_VARIABLE(aInstance);
 
     otError  err;
-    bool     needClose   = false;
     uint16_t valueLength = 0;
 
-    err = mapNvm3Error(nvm3_open(nvm3_defaultHandle, &nvm3_defaultInitData));
-    SuccessOrExit(err);
-    needClose = true;
-
     nvm3_ObjectKey_t nvm3Key  = makeNvm3ObjKey(aKey, 0); // The base nvm3 key value.
     bool             idxFound = false;
     int              idx      = 0;
@@ -211,11 +194,6 @@
     }
 
 exit:
-    if (needClose)
-    {
-        nvm3_close(nvm3_defaultHandle);
-    }
-
     if (aValueLength != NULL)
     {
         *aValueLength = valueLength; // always return actual nvm3 object length.
@@ -258,13 +236,7 @@
 
     OT_UNUSED_VARIABLE(aInstance);
 
-    otError err;
-    bool    needClose = false;
-
-    err = mapNvm3Error(nvm3_open(nvm3_defaultHandle, &nvm3_defaultInitData));
-    SuccessOrExit(err);
-    needClose = true;
-
+    otError          err;
     nvm3_ObjectKey_t nvm3Key  = makeNvm3ObjKey(aKey, 0); // The base nvm3 key value.
     bool             idxFound = false;
     int              idx      = 0;
@@ -306,11 +278,6 @@
     }
 
 exit:
-    if (needClose)
-    {
-        nvm3_close(nvm3_defaultHandle);
-    }
-
     return err;
 }
 
@@ -334,11 +301,6 @@
     // nvm3 object is created at the first available Key + index.
 
     otError err;
-    bool    needClose = false;
-
-    err = mapNvm3Error(nvm3_open(nvm3_defaultHandle, &nvm3_defaultInitData));
-    SuccessOrExit(err);
-    needClose = true;
 
     if ((aValueLength == 0) || (aValue == NULL))
     {
@@ -369,10 +331,6 @@
     }
 
 exit:
-    if (needClose)
-    {
-        nvm3_close(nvm3_defaultHandle);
-    }
     return err;
 }
 
diff --git a/examples/platforms/efr32mg13/flash.c b/examples/platforms/efr32mg13/flash.c
index ffbaa20..2fa52c3 100644
--- a/examples/platforms/efr32mg13/flash.c
+++ b/examples/platforms/efr32mg13/flash.c
@@ -28,8 +28,7 @@
 
 /**
  * @file
- *   This file implements the OpenThread platform abstraction for the non-volatile
- *   storage for the EFR32 platform using the Silabs Nvm3 interface.
+ *   This file implements the OpenThread platform abstraction for the non-volatile storage.
  */
 
 #include "openthread-core-efr32-config.h"
@@ -101,13 +100,13 @@
 
 #else // Defaults to Silabs nvm3 system
 
-#include "nvm3.h"
 #include "nvm3_default.h"
 #include <string.h>
 #include <openthread/platform/settings.h>
 #include "common/code_utils.hpp"
+#include "common/logging.hpp"
 
-#define NVM3KEY_DOMAIN_OPENTHREAD 0x20000U // NVM3 Default Instance key space region for Thread stack
+#define NVM3KEY_DOMAIN_OPENTHREAD 0x20000U
 #define NUM_INDEXED_SETTINGS \
     OPENTHREAD_CONFIG_MLE_MAX_CHILDREN // Indexed key types are only supported for kKeyChildInfo (=='child table').
 #define ENUM_NVM3_KEY_LIST_SIZE 4      // List size used when enumerating nvm3 keys.
@@ -116,24 +115,13 @@
 static nvm3_ObjectKey_t makeNvm3ObjKey(uint16_t otSettingsKey, int index);
 static otError          mapNvm3Error(Ecode_t nvm3Res);
 
-extern nvm3_Init_t    nvm3_defaultInitData;
-extern nvm3_Handle_t *nvm3_defaultHandle;
-
 void otPlatSettingsInit(otInstance *aInstance)
 {
     OT_UNUSED_VARIABLE(aInstance);
 
-    otError err;
-    bool    needClose = false;
-
-    err = mapNvm3Error(nvm3_open(nvm3_defaultHandle, &nvm3_defaultInitData));
-    SuccessOrExit(err);
-    needClose = true;
-
-exit:
-    if (needClose)
+    if (mapNvm3Error(nvm3_open(nvm3_defaultHandle, nvm3_defaultInit)) != OT_ERROR_NONE)
     {
-        nvm3_close(nvm3_defaultHandle);
+        otLogDebgPlat("Error initializing nvm3 instance");
     }
 }
 
@@ -153,13 +141,8 @@
     OT_UNUSED_VARIABLE(aInstance);
 
     otError  err;
-    bool     needClose   = false;
     uint16_t valueLength = 0;
 
-    err = mapNvm3Error(nvm3_open(nvm3_defaultHandle, &nvm3_defaultInitData));
-    SuccessOrExit(err);
-    needClose = true;
-
     nvm3_ObjectKey_t nvm3Key  = makeNvm3ObjKey(aKey, 0); // The base nvm3 key value.
     bool             idxFound = false;
     int              idx      = 0;
@@ -211,11 +194,6 @@
     }
 
 exit:
-    if (needClose)
-    {
-        nvm3_close(nvm3_defaultHandle);
-    }
-
     if (aValueLength != NULL)
     {
         *aValueLength = valueLength; // always return actual nvm3 object length.
@@ -258,13 +236,7 @@
 
     OT_UNUSED_VARIABLE(aInstance);
 
-    otError err;
-    bool    needClose = false;
-
-    err = mapNvm3Error(nvm3_open(nvm3_defaultHandle, &nvm3_defaultInitData));
-    SuccessOrExit(err);
-    needClose = true;
-
+    otError          err;
     nvm3_ObjectKey_t nvm3Key  = makeNvm3ObjKey(aKey, 0); // The base nvm3 key value.
     bool             idxFound = false;
     int              idx      = 0;
@@ -306,11 +278,6 @@
     }
 
 exit:
-    if (needClose)
-    {
-        nvm3_close(nvm3_defaultHandle);
-    }
-
     return err;
 }
 
@@ -334,11 +301,6 @@
     // nvm3 object is created at the first available Key + index.
 
     otError err;
-    bool    needClose = false;
-
-    err = mapNvm3Error(nvm3_open(nvm3_defaultHandle, &nvm3_defaultInitData));
-    SuccessOrExit(err);
-    needClose = true;
 
     if ((aValueLength == 0) || (aValue == NULL))
     {
@@ -369,10 +331,6 @@
     }
 
 exit:
-    if (needClose)
-    {
-        nvm3_close(nvm3_defaultHandle);
-    }
     return err;
 }
 
diff --git a/examples/platforms/efr32mg21/flash.c b/examples/platforms/efr32mg21/flash.c
index ffbaa20..2fa52c3 100644
--- a/examples/platforms/efr32mg21/flash.c
+++ b/examples/platforms/efr32mg21/flash.c
@@ -28,8 +28,7 @@
 
 /**
  * @file
- *   This file implements the OpenThread platform abstraction for the non-volatile
- *   storage for the EFR32 platform using the Silabs Nvm3 interface.
+ *   This file implements the OpenThread platform abstraction for the non-volatile storage.
  */
 
 #include "openthread-core-efr32-config.h"
@@ -101,13 +100,13 @@
 
 #else // Defaults to Silabs nvm3 system
 
-#include "nvm3.h"
 #include "nvm3_default.h"
 #include <string.h>
 #include <openthread/platform/settings.h>
 #include "common/code_utils.hpp"
+#include "common/logging.hpp"
 
-#define NVM3KEY_DOMAIN_OPENTHREAD 0x20000U // NVM3 Default Instance key space region for Thread stack
+#define NVM3KEY_DOMAIN_OPENTHREAD 0x20000U
 #define NUM_INDEXED_SETTINGS \
     OPENTHREAD_CONFIG_MLE_MAX_CHILDREN // Indexed key types are only supported for kKeyChildInfo (=='child table').
 #define ENUM_NVM3_KEY_LIST_SIZE 4      // List size used when enumerating nvm3 keys.
@@ -116,24 +115,13 @@
 static nvm3_ObjectKey_t makeNvm3ObjKey(uint16_t otSettingsKey, int index);
 static otError          mapNvm3Error(Ecode_t nvm3Res);
 
-extern nvm3_Init_t    nvm3_defaultInitData;
-extern nvm3_Handle_t *nvm3_defaultHandle;
-
 void otPlatSettingsInit(otInstance *aInstance)
 {
     OT_UNUSED_VARIABLE(aInstance);
 
-    otError err;
-    bool    needClose = false;
-
-    err = mapNvm3Error(nvm3_open(nvm3_defaultHandle, &nvm3_defaultInitData));
-    SuccessOrExit(err);
-    needClose = true;
-
-exit:
-    if (needClose)
+    if (mapNvm3Error(nvm3_open(nvm3_defaultHandle, nvm3_defaultInit)) != OT_ERROR_NONE)
     {
-        nvm3_close(nvm3_defaultHandle);
+        otLogDebgPlat("Error initializing nvm3 instance");
     }
 }
 
@@ -153,13 +141,8 @@
     OT_UNUSED_VARIABLE(aInstance);
 
     otError  err;
-    bool     needClose   = false;
     uint16_t valueLength = 0;
 
-    err = mapNvm3Error(nvm3_open(nvm3_defaultHandle, &nvm3_defaultInitData));
-    SuccessOrExit(err);
-    needClose = true;
-
     nvm3_ObjectKey_t nvm3Key  = makeNvm3ObjKey(aKey, 0); // The base nvm3 key value.
     bool             idxFound = false;
     int              idx      = 0;
@@ -211,11 +194,6 @@
     }
 
 exit:
-    if (needClose)
-    {
-        nvm3_close(nvm3_defaultHandle);
-    }
-
     if (aValueLength != NULL)
     {
         *aValueLength = valueLength; // always return actual nvm3 object length.
@@ -258,13 +236,7 @@
 
     OT_UNUSED_VARIABLE(aInstance);
 
-    otError err;
-    bool    needClose = false;
-
-    err = mapNvm3Error(nvm3_open(nvm3_defaultHandle, &nvm3_defaultInitData));
-    SuccessOrExit(err);
-    needClose = true;
-
+    otError          err;
     nvm3_ObjectKey_t nvm3Key  = makeNvm3ObjKey(aKey, 0); // The base nvm3 key value.
     bool             idxFound = false;
     int              idx      = 0;
@@ -306,11 +278,6 @@
     }
 
 exit:
-    if (needClose)
-    {
-        nvm3_close(nvm3_defaultHandle);
-    }
-
     return err;
 }
 
@@ -334,11 +301,6 @@
     // nvm3 object is created at the first available Key + index.
 
     otError err;
-    bool    needClose = false;
-
-    err = mapNvm3Error(nvm3_open(nvm3_defaultHandle, &nvm3_defaultInitData));
-    SuccessOrExit(err);
-    needClose = true;
 
     if ((aValueLength == 0) || (aValue == NULL))
     {
@@ -369,10 +331,6 @@
     }
 
 exit:
-    if (needClose)
-    {
-        nvm3_close(nvm3_defaultHandle);
-    }
     return err;
 }