11
11
#include <nvs_flash.h>
12
12
#include <esp_log.h>
13
13
#include "esp_err.h"
14
+ #include <cJSON.h>
15
+
14
16
#include "nvm.h"
15
17
#include "router_globals.h"
16
18
#include "utils.h"
19
+ #include "mac_filter.h"
17
20
18
21
static const char * TAG = "utils/nvm" ;
19
22
@@ -66,3 +69,113 @@ esp_err_t nvm_erase(void)
66
69
nvs_close (nvs_handle );
67
70
return ESP_OK ;
68
71
}
72
+
73
+ //-----------------------------------------------------------------------------
74
+ cJSON * retrieve_mac_addresses_as_json () {
75
+ nvs_handle_t handle ;
76
+ esp_err_t err = nvs_open (PARAM_NAMESPACE , NVS_READONLY , & handle );
77
+ if (err != ESP_OK ) {
78
+ return NULL ;
79
+ }
80
+
81
+ size_t required_size ;
82
+ err = nvs_get_str (handle , NVS_KEY , NULL , & required_size );
83
+ if (err != ESP_OK || required_size == 0 ) {
84
+ nvs_close (handle );
85
+ return NULL ;
86
+ }
87
+
88
+ char * buffer = malloc (required_size );
89
+ if (buffer == NULL ) {
90
+ nvs_close (handle );
91
+ return NULL ;
92
+ }
93
+
94
+ err = nvs_get_str (handle , NVS_KEY , buffer , & required_size );
95
+ if (err != ESP_OK ) {
96
+ free (buffer );
97
+ nvs_close (handle );
98
+ return NULL ;
99
+ }
100
+
101
+ cJSON * json = cJSON_Parse (buffer );
102
+ free (buffer );
103
+ nvs_close (handle );
104
+ return json ;
105
+ }
106
+
107
+ //-----------------------------------------------------------------------------
108
+ esp_err_t save_mac_addresses_as_json (cJSON * json ) {
109
+ nvs_handle_t handle ;
110
+ esp_err_t err = nvs_open (PARAM_NAMESPACE , NVS_READWRITE , & handle );
111
+ if (err != ESP_OK ) {
112
+ cJSON_Delete (json );
113
+ return err ;
114
+ }
115
+
116
+ char * serialized = cJSON_Print (json );
117
+ if (serialized == NULL ) {
118
+ cJSON_Delete (json );
119
+ nvs_close (handle );
120
+ return ESP_ERR_NO_MEM ;
121
+ }
122
+
123
+ err = nvs_set_str (handle , NVS_KEY , serialized );
124
+
125
+ ESP_LOGI ("MACS ARE" ,"%s" ,serialized );
126
+ free (serialized );
127
+ nvs_close (handle );
128
+ return err ;
129
+ }
130
+
131
+ //-----------------------------------------------------------------------------
132
+ esp_err_t store_mac_address_in_nvs (const char * macAddress ) {
133
+ cJSON * json = retrieve_mac_addresses_as_json ();
134
+ if (json == NULL ) {
135
+ json = cJSON_CreateArray ();
136
+ }
137
+
138
+ int numMacs = cJSON_GetArraySize (json );
139
+ if (numMacs >= MAX_MAC_ADDRESSES ) {
140
+ cJSON_Delete (json );
141
+ return ESP_ERR_NO_MEM ;
142
+ }
143
+
144
+ for (int i = 0 ; i < numMacs ; i ++ ) {
145
+ const char * storedMac = cJSON_GetArrayItem (json , i )-> valuestring ;
146
+ if (strcmp (storedMac , macAddress ) == 0 ) {
147
+ cJSON_Delete (json );
148
+ return ESP_OK ;
149
+ }
150
+ }
151
+
152
+ cJSON_AddItemToArray (json , cJSON_CreateString (macAddress ));
153
+
154
+ esp_err_t err = save_mac_addresses_as_json (json );
155
+ cJSON_Delete (json );
156
+ return err ;
157
+ }
158
+
159
+ //-----------------------------------------------------------------------------
160
+ esp_err_t remove_mac_address_from_nvs (const char * macAddress ) {
161
+ cJSON * json = retrieve_mac_addresses_as_json ();
162
+ if (json == NULL ) {
163
+ return ESP_ERR_NVS_NOT_FOUND ;
164
+ }
165
+
166
+ int numMacs = cJSON_GetArraySize (json );
167
+ for (int i = 0 ; i < numMacs ; i ++ ) {
168
+ const char * storedMac = cJSON_GetArrayItem (json , i )-> valuestring ;
169
+ if (strcmp (storedMac , macAddress ) == 0 ) {
170
+ cJSON_DeleteItemFromArray (json , i );
171
+
172
+ esp_err_t err = save_mac_addresses_as_json (json );
173
+ cJSON_Delete (json );
174
+ return err ;
175
+ }
176
+ }
177
+ cJSON_Delete (json );
178
+ return ESP_ERR_NOT_FOUND ;
179
+ }
180
+
181
+ //-----------------------------------------------------------------------------
0 commit comments