Skip to content

Commit 4ab7cb6

Browse files
committed
Minimize write and read function
Signed-off-by: cybnon <stefan.weber93@googlemail.com>
1 parent e10fb94 commit 4ab7cb6

File tree

1 file changed

+18
-50
lines changed

1 file changed

+18
-50
lines changed

src/atecc608_handler.cpp

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,13 @@ int atecc_handler_inject_priv_key(int slot, uint8_t* priv_key){
208208
return status;
209209
}
210210

211-
/** save data in slot
211+
/** Write 64 bytes of data to a slot
212212
* \param[in] slot slot number to which data is to be written
213-
* \param[in] data data byte array to write
214-
* \param[in] data_len length of the data byte array (must be a multiple of 32)
213+
* \param[in] data data byte array to write (must be 64 bytes)
215214
* \return ATCA_SUCCESS on success, otherwise an error code.
216215
*/
217-
int atecc_handler_write_data(int slot, uint8_t* data, size_t data_len) {
218-
if (!data || data_len == 0) {
216+
int atecc_handler_write_data(int slot, const uint8_t* data) {
217+
if (!data) {
219218
return ATCA_BAD_PARAM;
220219
}
221220

@@ -224,38 +223,22 @@ int atecc_handler_write_data(int slot, uint8_t* data, size_t data_len) {
224223
return status;
225224
}
226225

227-
// Calculate the number of blocks and any remaining bytes
228-
size_t num_blocks = data_len / 32;
229-
size_t remaining_bytes = data_len % 32;
230-
231-
// Write full blocks
232-
for (size_t i = 0; i < num_blocks; i++) {
233-
status = atcab_write_zone(ATCA_ZONE_DATA, slot, i, 0, &data[i * 32], 32);
234-
if (status != ATCA_SUCCESS) {
235-
return status;
236-
}
237-
}
238-
239-
// Write remaining bytes if any
240-
if (remaining_bytes > 0) {
241-
status = atcab_write_zone(ATCA_ZONE_DATA, slot, num_blocks, 0, &data[num_blocks * 32], remaining_bytes);
242-
if (status != ATCA_SUCCESS) {
243-
return status;
244-
}
226+
// Write 64 bytes at once
227+
status = atcab_write_zone(ATCA_ZONE_DATA, slot, 0, 0, data, 64);
228+
if (status != ATCA_SUCCESS) {
229+
printf("Write error: %d\n", status);
245230
}
246231

247-
return ATCA_SUCCESS;
232+
return status;
248233
}
249234

250-
251-
/* read data from slot
235+
/** Read 64 bytes of data from a slot
252236
* \param[in] slot slot number from which data is to be read
253-
* \param[out] data buffer to store the read data
254-
* \param[in] data_len length of the data byte array (must be a multiple of 32)
237+
* \param[out] data buffer to store the read data (must be at least 64 bytes)
255238
* \return ATCA_SUCCESS on success, otherwise an error code.
256239
*/
257-
int atecc_handler_read_data(int slot, uint8_t* data, size_t data_len) {
258-
if (!data || data_len == 0) {
240+
int atecc_handler_read_data(int slot, uint8_t* data) {
241+
if (!data) {
259242
return ATCA_BAD_PARAM;
260243
}
261244

@@ -264,30 +247,15 @@ int atecc_handler_read_data(int slot, uint8_t* data, size_t data_len) {
264247
return status;
265248
}
266249

267-
// Calculate the number of blocks and any remaining bytes
268-
size_t num_blocks = data_len / 32;
269-
size_t remaining_bytes = data_len % 32;
270-
271-
// Read full blocks
272-
for (size_t i = 0; i < num_blocks; i++) {
273-
status = atcab_read_zone(ATCA_ZONE_DATA, slot, i, 0, &data[i * 32], 32);
274-
if (status != ATCA_SUCCESS) {
275-
return status;
276-
}
277-
}
278-
279-
// Read remaining bytes if any
280-
if (remaining_bytes > 0) {
281-
status = atcab_read_zone(ATCA_ZONE_DATA, slot, num_blocks, 0, &data[num_blocks * 32], remaining_bytes);
282-
if (status != ATCA_SUCCESS) {
283-
return status;
284-
}
250+
// Read 64 bytes at once
251+
status = atcab_read_zone(ATCA_ZONE_DATA, slot, 0, 0, data, 64);
252+
if (status != ATCA_SUCCESS) {
253+
printf("Read error: %d\n", status);
285254
}
286255

287-
return ATCA_SUCCESS;
256+
return status;
288257
}
289258

290-
291259
/** \brief Initialize atecc object and bus
292260
* \param[in] slot slot number of key to be written
293261
* \param[in] pub_key public key will be written here

0 commit comments

Comments
 (0)