@@ -66,35 +66,63 @@ static KVStore *get_kvstore_instance(void)
66
66
return kvstore;
67
67
}
68
68
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)
70
103
{
71
104
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);
73
106
74
107
uint8_t filename_idx = 0 ;
75
- uint32_t tmp_uid = uid;
76
- uint32_t tmp_pid = pid;
77
108
78
109
// Iterate on UID; each time convert 6 bits of UID into a character; first iteration must be done
79
110
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 );
84
114
85
115
// Write delimiter
86
- MBED_ASSERT (filename_idx <= PSA_ITS_FILENAME_MAX_LEN);
87
116
tdb_filename[filename_idx++] = ' #' ;
88
117
89
118
// Iterate on PID; each time convert 6 bits of PID into a character; first iteration must be done
90
119
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 );
95
123
124
+ tdb_filename[filename_idx++] = ' \0 ' ;
96
125
MBED_ASSERT (filename_idx <= PSA_ITS_FILENAME_MAX_LEN);
97
- tdb_filename[filename_idx] = ' \0 ' ;
98
126
}
99
127
100
128
@@ -116,23 +144,9 @@ psa_its_status_t psa_its_set_impl(uint32_t pid, uint32_t uid, uint32_t data_leng
116
144
kv_create_flags = KVStore::WRITE_ONCE_FLAG;
117
145
}
118
146
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);
134
148
135
- return status;
149
+ return convert_status ( status) ;
136
150
}
137
151
138
152
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
145
159
generate_fn (kv_key, PSA_ITS_FILENAME_MAX_LEN, uid, pid);
146
160
147
161
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);
160
163
161
- if (kvstore_status == MBED_SUCCESS) {
164
+ if (status == MBED_SUCCESS) {
162
165
if (data_offset > kv_info.size ) {
163
166
return PSA_ITS_ERROR_OFFSET_INVALID;
164
167
}
@@ -173,24 +176,16 @@ psa_its_status_t psa_its_get_impl(uint32_t pid, uint32_t uid, uint32_t data_offs
173
176
}
174
177
175
178
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);
177
180
178
- if (kvstore_status == MBED_SUCCESS) {
181
+ if (status == MBED_SUCCESS) {
179
182
if (actual_size < data_length) {
180
183
status = PSA_ITS_ERROR_INCORRECT_SIZE;
181
184
}
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
- }
190
185
}
191
186
}
192
187
193
- return status;
188
+ return convert_status ( status) ;
194
189
}
195
190
196
191
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
203
198
generate_fn (kv_key, PSA_ITS_FILENAME_MAX_LEN, uid, pid);
204
199
205
200
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);
218
202
219
- if (kvstore_status == MBED_SUCCESS) {
203
+ if (status == MBED_SUCCESS) {
220
204
p_info->flags = 0 ;
221
205
if (kv_info.flags & KVStore::WRITE_ONCE_FLAG) {
222
206
p_info->flags |= PSA_ITS_WRITE_ONCE_FLAG;
223
207
}
224
208
p_info->size = (uint32_t )(kv_info.size ); // kv_info.size is of type size_t
225
209
}
226
210
227
- return status;
211
+ return convert_status ( status) ;
228
212
}
229
213
230
214
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)
236
220
char kv_key[PSA_ITS_FILENAME_MAX_LEN] = {' \0 ' };
237
221
generate_fn (kv_key, PSA_ITS_FILENAME_MAX_LEN, uid, pid);
238
222
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);
254
224
255
- return status;
225
+ return convert_status ( status) ;
256
226
}
257
227
258
228
#ifdef __cplusplus
0 commit comments