Skip to content

Commit 0c5fb61

Browse files
Alexander Zilberkantadbridge
authored andcommitted
Optimize error handling
1 parent 41adc4e commit 0c5fb61

File tree

1 file changed

+54
-84
lines changed
  • components/TARGET_PSA/services/psa_prot_internal_storage/COMPONENT_PSA_SRV_IMPL

1 file changed

+54
-84
lines changed

components/TARGET_PSA/services/psa_prot_internal_storage/COMPONENT_PSA_SRV_IMPL/pits_impl.cpp

Lines changed: 54 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -66,35 +66,63 @@ static KVStore *get_kvstore_instance(void)
6666
return kvstore;
6767
}
6868

69-
static void generate_fn(char *tdb_filename, uint32_t tdb_file_len, uint32_t uid, uint32_t pid)
69+
/*
70+
* \brief Convert KVStore stauts codes to PSA internal storage status codes
71+
*
72+
* \param[in] status - KVStore status code
73+
* \return PSA internal storage status code
74+
*/
75+
static psa_its_status_t convert_status(int status)
76+
{
77+
switch (status) {
78+
case MBED_SUCCESS:
79+
return PSA_ITS_SUCCESS;
80+
case MBED_ERROR_WRITE_PROTECTED:
81+
return PSA_ITS_ERROR_WRITE_ONCE;
82+
case MBED_ERROR_MEDIA_FULL:
83+
return PSA_ITS_ERROR_INSUFFICIENT_SPACE;
84+
case MBED_ERROR_ITEM_NOT_FOUND:
85+
return PSA_ITS_ERROR_KEY_NOT_FOUND;
86+
default:
87+
return PSA_ITS_ERROR_STORAGE_FAILURE;
88+
}
89+
}
90+
91+
/*
92+
* \breif Generate KVStore file name
93+
*
94+
* Generate KVStore file name by Base64 encoding PID and UID with a delimiter.
95+
* Delimiter is required for determining between PID and UID.
96+
*
97+
* \param[out] tdb_filename - pointer to a buffer for the file name
98+
* \param[in] tdb_filename_size - output buffer size
99+
* \param[in] uid - PSA internal storage unique ID
100+
* \param[in] pid - owner PSA partition ID
101+
*/
102+
static void generate_fn(char *tdb_filename, uint32_t tdb_filename_size, uint32_t uid, uint32_t pid)
70103
{
71104
MBED_ASSERT(tdb_filename != NULL);
72-
MBED_ASSERT(tdb_file_len >= PSA_ITS_FILENAME_MAX_LEN);
105+
MBED_ASSERT(tdb_filename_size == PSA_ITS_FILENAME_MAX_LEN);
73106

74107
uint8_t filename_idx = 0;
75-
uint32_t tmp_uid = uid;
76-
uint32_t tmp_pid = pid;
77108

78109
// Iterate on UID; each time convert 6 bits of UID into a character; first iteration must be done
79110
do {
80-
MBED_ASSERT(filename_idx <= PSA_ITS_FILENAME_MAX_LEN);
81-
tdb_filename[filename_idx++] = base64_coding_table[tmp_uid & 0x3F];
82-
tmp_uid = tmp_uid >> 6;
83-
} while (tmp_uid != 0);
111+
tdb_filename[filename_idx++] = base64_coding_table[uid & 0x3F];
112+
uid = uid >> 6;
113+
} while (uid != 0);
84114

85115
// Write delimiter
86-
MBED_ASSERT(filename_idx <= PSA_ITS_FILENAME_MAX_LEN);
87116
tdb_filename[filename_idx++] = '#';
88117

89118
// Iterate on PID; each time convert 6 bits of PID into a character; first iteration must be done
90119
do {
91-
MBED_ASSERT(filename_idx <= PSA_ITS_FILENAME_MAX_LEN);
92-
tdb_filename[filename_idx++] = base64_coding_table[tmp_pid & 0x3F];
93-
tmp_pid = tmp_pid >> 6;
94-
} while (tmp_pid != 0);
120+
tdb_filename[filename_idx++] = base64_coding_table[pid & 0x3F];
121+
pid = pid >> 6;
122+
} while (pid != 0);
95123

124+
tdb_filename[filename_idx++] = '\0';
96125
MBED_ASSERT(filename_idx <= PSA_ITS_FILENAME_MAX_LEN);
97-
tdb_filename[filename_idx] = '\0';
98126
}
99127

100128

@@ -116,23 +144,9 @@ psa_its_status_t psa_its_set_impl(uint32_t pid, uint32_t uid, uint32_t data_leng
116144
kv_create_flags = KVStore::WRITE_ONCE_FLAG;
117145
}
118146

119-
int kvstore_status = kvstore->set(kv_key, p_data, data_length, kv_create_flags);
120-
121-
psa_its_status_t status = PSA_ITS_SUCCESS;
122-
if (kvstore_status != MBED_SUCCESS) {
123-
switch (kvstore_status) {
124-
case MBED_ERROR_WRITE_PROTECTED:
125-
status = PSA_ITS_ERROR_WRITE_ONCE;
126-
break;
127-
case MBED_ERROR_MEDIA_FULL:
128-
status = PSA_ITS_ERROR_INSUFFICIENT_SPACE;
129-
break;
130-
default:
131-
status = PSA_ITS_ERROR_STORAGE_FAILURE;
132-
}
133-
}
147+
int status = kvstore->set(kv_key, p_data, data_length, kv_create_flags);
134148

135-
return status;
149+
return convert_status(status);
136150
}
137151

138152
psa_its_status_t psa_its_get_impl(uint32_t pid, uint32_t uid, uint32_t data_offset, uint32_t data_length, void *p_data)
@@ -145,20 +159,9 @@ psa_its_status_t psa_its_get_impl(uint32_t pid, uint32_t uid, uint32_t data_offs
145159
generate_fn(kv_key, PSA_ITS_FILENAME_MAX_LEN, uid, pid);
146160

147161
KVStore::info_t kv_info;
148-
int kvstore_status = kvstore->get_info(kv_key, &kv_info);
149-
150-
psa_its_status_t status = PSA_ITS_SUCCESS;
151-
if (kvstore_status != MBED_SUCCESS) {
152-
switch (kvstore_status) {
153-
case MBED_ERROR_ITEM_NOT_FOUND:
154-
status = PSA_ITS_ERROR_KEY_NOT_FOUND;
155-
break;
156-
default:
157-
status = PSA_ITS_ERROR_STORAGE_FAILURE;
158-
}
159-
}
162+
int status = kvstore->get_info(kv_key, &kv_info);
160163

161-
if (kvstore_status == MBED_SUCCESS) {
164+
if (status == MBED_SUCCESS) {
162165
if (data_offset > kv_info.size) {
163166
return PSA_ITS_ERROR_OFFSET_INVALID;
164167
}
@@ -173,24 +176,16 @@ psa_its_status_t psa_its_get_impl(uint32_t pid, uint32_t uid, uint32_t data_offs
173176
}
174177

175178
size_t actual_size = 0;
176-
kvstore_status = kvstore->get(kv_key, p_data, data_length, &actual_size, data_offset);
179+
status = kvstore->get(kv_key, p_data, data_length, &actual_size, data_offset);
177180

178-
if (kvstore_status == MBED_SUCCESS) {
181+
if (status == MBED_SUCCESS) {
179182
if (actual_size < data_length) {
180183
status = PSA_ITS_ERROR_INCORRECT_SIZE;
181184
}
182-
} else {
183-
switch (kvstore_status) {
184-
case MBED_ERROR_ITEM_NOT_FOUND:
185-
status = PSA_ITS_ERROR_KEY_NOT_FOUND;
186-
break;
187-
default:
188-
status = PSA_ITS_ERROR_STORAGE_FAILURE;
189-
}
190185
}
191186
}
192187

193-
return status;
188+
return convert_status(status);
194189
}
195190

196191
psa_its_status_t psa_its_get_info_impl(uint32_t pid, uint32_t uid, struct psa_its_info_t *p_info)
@@ -203,28 +198,17 @@ psa_its_status_t psa_its_get_info_impl(uint32_t pid, uint32_t uid, struct psa_it
203198
generate_fn(kv_key, PSA_ITS_FILENAME_MAX_LEN, uid, pid);
204199

205200
KVStore::info_t kv_info;
206-
int kvstore_status = kvstore->get_info(kv_key, &kv_info);
207-
208-
psa_its_status_t status = PSA_ITS_SUCCESS;
209-
if (kvstore_status != MBED_SUCCESS) {
210-
switch (kvstore_status) {
211-
case MBED_ERROR_ITEM_NOT_FOUND:
212-
status = PSA_ITS_ERROR_KEY_NOT_FOUND;
213-
break;
214-
default:
215-
status = PSA_ITS_ERROR_STORAGE_FAILURE;
216-
}
217-
}
201+
int status = kvstore->get_info(kv_key, &kv_info);
218202

219-
if (kvstore_status == MBED_SUCCESS) {
203+
if (status == MBED_SUCCESS) {
220204
p_info->flags = 0;
221205
if (kv_info.flags & KVStore::WRITE_ONCE_FLAG) {
222206
p_info->flags |= PSA_ITS_WRITE_ONCE_FLAG;
223207
}
224208
p_info->size = (uint32_t)(kv_info.size); // kv_info.size is of type size_t
225209
}
226210

227-
return status;
211+
return convert_status(status);
228212
}
229213

230214
psa_its_status_t psa_its_remove_impl(uint32_t pid, uint32_t uid)
@@ -236,23 +220,9 @@ psa_its_status_t psa_its_remove_impl(uint32_t pid, uint32_t uid)
236220
char kv_key[PSA_ITS_FILENAME_MAX_LEN] = {'\0'};
237221
generate_fn(kv_key, PSA_ITS_FILENAME_MAX_LEN, uid, pid);
238222

239-
int kvstore_status = kvstore->remove(kv_key);
240-
241-
psa_its_status_t status = PSA_ITS_SUCCESS;
242-
if (kvstore_status != MBED_SUCCESS) {
243-
switch (kvstore_status) {
244-
case MBED_ERROR_WRITE_PROTECTED:
245-
status = PSA_ITS_ERROR_WRITE_ONCE;
246-
break;
247-
case MBED_ERROR_ITEM_NOT_FOUND:
248-
status = PSA_ITS_ERROR_KEY_NOT_FOUND;
249-
break;
250-
default:
251-
status = PSA_ITS_ERROR_STORAGE_FAILURE;
252-
}
253-
}
223+
int status = kvstore->remove(kv_key);
254224

255-
return status;
225+
return convert_status(status);
256226
}
257227

258228
#ifdef __cplusplus

0 commit comments

Comments
 (0)