Skip to content

Commit e80b608

Browse files
dssengfabiobaltieri
authored andcommitted
settings: tfm_psa: improve code style
Fix SonarQube warnings, move variable declarations to the top of the block according to Zephyr code style Signed-off-by: Dmitrii Sharshakov <[email protected]>
1 parent 7c37621 commit e80b608

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

subsys/settings/src/settings_tfm_psa.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ static int store_entries(void)
6868
{
6969
psa_status_t status;
7070
psa_storage_uid_t uid = SETTINGS_PSA_UID_RANGE_BEGIN;
71+
size_t write_size = SETTINGS_PSA_MAX_ASSET_SIZE;
7172
size_t remaining = sizeof(entries);
7273
const uint8_t *data_ptr = (const uint8_t *)&entries;
7374

@@ -77,7 +78,6 @@ static int store_entries(void)
7778
* number of allocated UIDs and to allocate bytes in the most efficient way.
7879
*/
7980
while (remaining > 0) {
80-
size_t write_size = SETTINGS_PSA_MAX_ASSET_SIZE;
8181
if (remaining < SETTINGS_PSA_MAX_ASSET_SIZE) {
8282
write_size = remaining;
8383
}
@@ -106,16 +106,17 @@ static int load_entries(void)
106106
psa_status_t status;
107107
size_t bytes_read;
108108
psa_storage_uid_t uid = SETTINGS_PSA_UID_RANGE_BEGIN;
109+
size_t read_size = SETTINGS_PSA_MAX_ASSET_SIZE;
109110
size_t remaining = sizeof(entries);
110111
uint8_t *data_ptr = (uint8_t *)&entries;
112+
int i;
111113

112114
/*
113115
* Each storage UID is treated like a sector. Data is written to each KV pair until
114116
* that data field is full, before incrementing the UID. This is done to minimize the
115117
* number of allocated UIDs and to allocate bytes in the most efficient way.
116118
*/
117119
while (remaining > 0) {
118-
size_t read_size = SETTINGS_PSA_MAX_ASSET_SIZE;
119120
if (remaining < SETTINGS_PSA_MAX_ASSET_SIZE) {
120121
read_size = remaining;
121122
}
@@ -130,7 +131,7 @@ static int load_entries(void)
130131
uid++;
131132
}
132133

133-
for (int i = 0; i < CONFIG_SETTINGS_TFM_PSA_NUM_ENTRIES; i++) {
134+
for (i = 0; i < CONFIG_SETTINGS_TFM_PSA_NUM_ENTRIES; i++) {
134135
if (strnlen(entries[i].name, SETTINGS_MAX_NAME_LEN) != 0) {
135136
entries_count++;
136137
}
@@ -164,17 +165,18 @@ static ssize_t settings_psa_read_fn(void *back_end, void *data, size_t len)
164165

165166
static int settings_psa_load(struct settings_store *cs, const struct settings_load_arg *arg)
166167
{
168+
int i;
167169
int ret;
168170

169-
for (int i = 0; i < entries_count; i++) {
171+
for (i = 0; i < entries_count; i++) {
170172
if (strnlen(entries[i].name, SETTINGS_MAX_NAME_LEN) != 0) {
171173
/*
172174
* Pass the key to the settings handler with it's index as an argument,
173175
* to be read during callback function later.
174176
*/
175177
ret = settings_call_set_handler(entries[i].name, entries[i].val_len,
176178
settings_psa_read_fn, (void *)&i,
177-
(void *)arg);
179+
arg);
178180
if (ret) {
179181
return ret;
180182
}
@@ -187,6 +189,9 @@ static int settings_psa_load(struct settings_store *cs, const struct settings_lo
187189
static int settings_psa_save(struct settings_store *cs, const char *name, const char *value,
188190
size_t val_len)
189191
{
192+
int index;
193+
bool delete;
194+
190195
if (entries_count >= CONFIG_SETTINGS_TFM_PSA_NUM_ENTRIES) {
191196
LOG_ERR("%s: Max settings reached: %d", __func__,
192197
CONFIG_SETTINGS_TFM_PSA_NUM_ENTRIES);
@@ -198,9 +203,6 @@ static int settings_psa_save(struct settings_store *cs, const char *name, const
198203
return -EINVAL;
199204
}
200205

201-
int index;
202-
bool delete;
203-
204206
/* Find out if we are doing a delete */
205207
delete = ((value == NULL) || (val_len == 0));
206208

@@ -215,7 +217,6 @@ static int settings_psa_save(struct settings_store *cs, const char *name, const
215217
if (strncmp(entries[index].name, name, SETTINGS_MAX_NAME_LEN) == 0) {
216218
break;
217219
} else if (entries[index].val_len == 0) {
218-
219220
/* Setting already deleted */
220221
if (delete) {
221222
LOG_DBG("%s: %s Already deleted!", __func__, name);
@@ -268,21 +269,21 @@ void worker_persist_entries_struct_fn(struct k_work *work)
268269

269270
int settings_backend_init(void)
270271
{
271-
psa_status_t status;
272-
273272
/* Load settings from storage */
274-
status = load_entries();
273+
psa_status_t status = load_entries();
274+
275+
if (status != PSA_SUCCESS) {
276+
if (status != PSA_ERROR_DOES_NOT_EXIST) {
277+
LOG_ERR("Error %s metadata: status %d", "loading", status);
278+
return -EIO;
279+
}
275280

276-
/* If resource DNE, we need to allocate it */
277-
if (status == PSA_ERROR_DOES_NOT_EXIST) {
281+
/* If resource does not exist, we need to allocate it */
278282
status = store_entries();
279-
if (status) {
280-
LOG_ERR("Error storing metadata in %s: (status %d)", __func__, status);
283+
if (status != PSA_SUCCESS) {
284+
LOG_ERR("Error %s metadata: status %d", "storing", status);
281285
return -EIO;
282286
}
283-
} else if (status) {
284-
LOG_ERR("Error loading metadata in %s: (status %d)", __func__, status);
285-
return -EIO;
286287
}
287288

288289
settings_dst_register(&default_settings_psa);

0 commit comments

Comments
 (0)