Skip to content

Commit f0a012e

Browse files
author
dbitbox
committed
Merge pull request #63 from dbitbox/cleaning
house cleaning, add error codes
2 parents 58333f9 + 3572655 commit f0a012e

35 files changed

+1890
-2098
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ before_script:
2525

2626
script:
2727
- mkdir build && cd build
28-
- cmake .. -DCONTINUOUS_INTEGRATION=1 && make
28+
- cmake .. -DCONTINUOUS_INTEGRATION=1 -DBUILD_COVERAGE=OFF && make
2929
- make test
30+
- bin/tests_api
31+
- bin/tests_unit

CMakeLists.txt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ set(version_file "${CMAKE_CURRENT_SOURCE_DIR}/src/version.h")
2222
# Options for building
2323

2424
option(BUILD_TESTS "Build the unit tests." ON)
25+
option(BUILD_COVERAGE "Compile with test coverage flags." OFF)
2526
option(BUILD_DOCUMENTATION "Build the Doxygen documentation." ON)
2627
option(CMAKE_VERBOSE_MAKEFILE "Verbose build." OFF)
2728
option(USE_UECC_LIB "Use micro ECC instead bitcoin's secp256k1 library." OFF)
@@ -45,6 +46,7 @@ endif()
4546

4647
message(STATUS "Verbose: ${CMAKE_VERBOSE_MAKEFILE}")
4748
message(STATUS "Testing: ${BUILD_TESTS}")
49+
message(STATUS "Coverage: ${BUILD_COVERAGE}")
4850
message(STATUS "Documentation: ${BUILD_DOCUMENTATION}")
4951

5052

@@ -59,8 +61,8 @@ set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/lib)
5961
# Compiler flags
6062

6163
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
62-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Os")
6364
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -g") # valgrind
65+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Os")
6466
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -W -Wall -Wextra -Werror -pedantic -Wredundant-decls -Wstrict-prototypes -Wundef -Wshadow -Wpointer-arith -Wmultichar -Wformat-nonliteral -Winit-self -Wformat-security -Wold-style-definition -Wmissing-include-dirs -Wbad-function-cast -Winline -Wnested-externs -Wfloat-equal -Wmissing-declarations -Wswitch-default -Wwrite-strings -Wcast-qual -Wmissing-prototypes")
6567

6668
if(APPLE)
@@ -78,6 +80,15 @@ message(STATUS "C Flags: ${CMAKE_C_FLAGS}")
7880
message(STATUS "C link flags: ${CMAKE_C_LINK_FLAGS}")
7981

8082

83+
#-----------------------------------------------------------------------------
84+
# Test coverage flags
85+
86+
if(BUILD_COVERAGE)
87+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
88+
set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -fprofile-arcs")
89+
endif()
90+
91+
8192
#-----------------------------------------------------------------------------
8293
# Get submodules
8394

@@ -172,6 +183,7 @@ if(USE_UECC_LIB)
172183
add_definitions(-DECC_USE_UECC_LIB)
173184
endif()
174185

186+
175187
#-----------------------------------------------------------------------------
176188
# Build source
177189

@@ -181,7 +193,8 @@ if(BUILD_TESTS)
181193
add_subdirectory(tests)
182194
add_test(NAME tests_unit COMMAND tests_unit)
183195
add_test(NAME tests_openssl COMMAND tests_openssl 200)
196+
add_test(NAME tests_cmdline COMMAND tests_cmdline '{"name":""}')
184197
add_test(NAME tests_api COMMAND tests_api)
185-
add_test(NAME tests_secp256k1 COMMAND tests_secp256k1 4)
198+
add_test(NAME tests_secp256k1 COMMAND tests_secp256k1 2)
186199
enable_testing()
187200
endif()

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ set(C-SOURCES
1717
secp256k1.c
1818
utils.c
1919
wallet.c
20+
flags.c
2021
)
2122

2223
set(C-HW-SOURCES

src/ataes132.c

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ uint32_t aes_eeprom_read(uint32_t u32_start_address, uint16_t u16_length,
113113
0xXX 0xXX 0xXX 0xXX 0xXX 0xXX 0xXX ... 0xXX
114114
Return block: [Count(1) || Return Code (1) | Data(...) || CRC (2)]
115115
*/
116-
void aes_process(uint8_t const *command, uint16_t cmd_len,
117-
uint8_t *response_block, uint16_t response_len)
116+
int aes_process(uint8_t const *command, uint16_t cmd_len,
117+
uint8_t *response_block, uint16_t response_len)
118118
{
119119
char errmsg[128];
120120
uint32_t ret = 0;
@@ -148,9 +148,10 @@ void aes_process(uint8_t const *command, uint16_t cmd_len,
148148
break;
149149
}
150150
if (cnt++ > timeout) {
151-
sprintf(errmsg, "Status buffer 0: %u %lu", aes_status, ret);
152-
commander_fill_report("ataes", errmsg, DBB_ERROR);
153-
return;
151+
snprintf(errmsg, sizeof(errmsg), "%s Status buffer 0: %u %lu",
152+
flag_msg(DBB_ERR_MEM_ATAES), aes_status, ret);
153+
commander_fill_report(cmd_str(CMD_ataes), errmsg, DBB_ERR_MEM_ATAES);
154+
return DBB_ERROR;
154155
}
155156
delay_ms(delay);
156157
}
@@ -162,9 +163,10 @@ void aes_process(uint8_t const *command, uint16_t cmd_len,
162163
if (!ret) {
163164
break;
164165
} else if (cnt++ > timeout) {
165-
sprintf(errmsg, "Reset buffer 1: %lu", ret);
166-
commander_fill_report("ataes", errmsg, DBB_ERROR);
167-
return;
166+
snprintf(errmsg, sizeof(errmsg), "%s Reset buffer 1: %lu", flag_msg(DBB_ERR_MEM_ATAES),
167+
ret);
168+
commander_fill_report(cmd_str(CMD_ataes), errmsg, DBB_ERR_MEM_ATAES);
169+
return DBB_ERROR;
168170
}
169171
}
170172

@@ -178,9 +180,10 @@ void aes_process(uint8_t const *command, uint16_t cmd_len,
178180
if ((aes_status & 0x40) && !ret) {
179181
break;
180182
} else if (cnt++ > timeout) {
181-
sprintf(errmsg, "Status buffer 1: %u %lu", aes_status, ret);
182-
commander_fill_report("ataes", errmsg, DBB_ERROR);
183-
return;
183+
snprintf(errmsg, sizeof(errmsg), "%s Status buffer 1: %u %lu",
184+
flag_msg(DBB_ERR_MEM_ATAES), aes_status, ret);
185+
commander_fill_report(cmd_str(CMD_ataes), errmsg, DBB_ERR_MEM_ATAES);
186+
return DBB_ERROR;
184187
}
185188
delay_ms(delay);
186189
}
@@ -192,9 +195,10 @@ void aes_process(uint8_t const *command, uint16_t cmd_len,
192195
if (!ret) {
193196
break;
194197
} else if (cnt++ > timeout) {
195-
sprintf(errmsg, "Command buffer: %lu", ret);
196-
commander_fill_report("ataes", errmsg, DBB_ERROR);
197-
return;
198+
snprintf(errmsg, sizeof(errmsg), "%s Command buffer: %lu", flag_msg(DBB_ERR_MEM_ATAES),
199+
ret);
200+
commander_fill_report(cmd_str(CMD_ataes), errmsg, DBB_ERR_MEM_ATAES);
201+
return DBB_ERROR;
198202
}
199203
}
200204

@@ -206,9 +210,10 @@ void aes_process(uint8_t const *command, uint16_t cmd_len,
206210
break;
207211
}
208212
if (cnt++ > timeout) {
209-
sprintf(errmsg, "Status buffer 2: %u %lu", aes_status, ret);
210-
commander_fill_report("ataes", errmsg, DBB_ERROR);
211-
return;
213+
snprintf(errmsg, sizeof(errmsg), "%s Status buffer 2: %u %lu",
214+
flag_msg(DBB_ERR_MEM_ATAES), aes_status, ret);
215+
commander_fill_report(cmd_str(CMD_ataes), errmsg, DBB_ERR_MEM_ATAES);
216+
return DBB_ERROR;
212217
}
213218
delay_ms(delay);
214219
}
@@ -220,9 +225,10 @@ void aes_process(uint8_t const *command, uint16_t cmd_len,
220225
if (!ret) {
221226
break;
222227
} else if (cnt++ > timeout) {
223-
sprintf(errmsg, "Reset buffer 2: %lu", ret);
224-
commander_fill_report("ataes", errmsg, DBB_ERROR);
225-
return;
228+
snprintf(errmsg, sizeof(errmsg), "%s Reset buffer 2: %lu", flag_msg(DBB_ERR_MEM_ATAES),
229+
ret);
230+
commander_fill_report(cmd_str(CMD_ataes), errmsg, DBB_ERR_MEM_ATAES);
231+
return DBB_ERROR;
226232
}
227233
}
228234

@@ -233,20 +239,23 @@ void aes_process(uint8_t const *command, uint16_t cmd_len,
233239
if (!ret) {
234240
break;
235241
} else if (cnt++ > timeout) {
236-
sprintf(errmsg, "Response buffer 1: %lu", ret);
237-
commander_fill_report("ataes", errmsg, DBB_ERROR);
238-
return;
242+
snprintf(errmsg, sizeof(errmsg), "%s Response buffer 1: %lu",
243+
flag_msg(DBB_ERR_MEM_ATAES), ret);
244+
commander_fill_report(cmd_str(CMD_ataes), errmsg, DBB_ERR_MEM_ATAES);
245+
return DBB_ERROR;
239246
}
240247
}
248+
249+
return DBB_OK;
241250
}
242251

243252

244253
// Pass NULL to read only or write only
245-
void aes_eeprom(uint16_t LEN, uint32_t ADDR, uint8_t *userdata_read,
246-
const uint8_t *userdata_write)
254+
int aes_eeprom(uint16_t LEN, uint32_t ADDR, uint8_t *userdata_read,
255+
const uint8_t *userdata_write)
247256
{
248257
int ret;
249-
char message[64];
258+
char errmsg[64];
250259

251260
uint8_t aes_status = 0;
252261
uint8_t delay = 2; // msec
@@ -256,18 +265,19 @@ void aes_eeprom(uint16_t LEN, uint32_t ADDR, uint8_t *userdata_read,
256265
if (userdata_write != NULL) {
257266
ret = aes_eeprom_write(ADDR, LEN, userdata_write);
258267
if (ret) {
259-
sprintf(message, "EEPROM write error %i.", ret);
260-
commander_fill_report("eeprom", message, DBB_ERROR);
261-
return;
268+
snprintf(errmsg, sizeof(errmsg), "%s EEPROM write: %i", flag_msg(DBB_ERR_MEM_ATAES), ret);
269+
commander_fill_report(cmd_str(CMD_ataes), errmsg, DBB_ERR_MEM_ATAES);
270+
return DBB_ERROR;
262271
}
263272
while (1) {
264273
ret = aes_eeprom_read(AES_MEM_ADDR_STATUS, 1, &aes_status);
265274
if (!(aes_status & 0x81) && !ret) { // 0x81 = no error and device ready
266275
break;
267276
} else if (cnt++ > timeout) {
268-
sprintf(message, "EEPROM write error [status]: %u %i", aes_status, ret);
269-
commander_fill_report("eeprom", message, DBB_ERROR);
270-
return;
277+
snprintf(errmsg, sizeof(errmsg), "%s EEPROM write status: %u %i",
278+
flag_msg(DBB_ERR_MEM_ATAES), aes_status, ret);
279+
commander_fill_report(cmd_str(CMD_ataes), errmsg, DBB_ERR_MEM_ATAES);
280+
return DBB_ERROR;
271281
}
272282
delay_ms(delay);
273283
}
@@ -276,21 +286,24 @@ void aes_eeprom(uint16_t LEN, uint32_t ADDR, uint8_t *userdata_read,
276286
if (userdata_read != NULL) {
277287
ret = aes_eeprom_read(ADDR, LEN, userdata_read);
278288
if (ret) {
279-
sprintf(message, "EEPROM read error %i.", ret);
280-
commander_fill_report("eeprom", message, DBB_ERROR);
281-
return;
289+
snprintf(errmsg, sizeof(errmsg), "%s EEPROM read: %i", flag_msg(DBB_ERR_MEM_ATAES), ret);
290+
commander_fill_report(cmd_str(CMD_ataes), errmsg, DBB_ERR_MEM_ATAES);
291+
return DBB_ERROR;
282292
}
283293
while (1) {
284294
ret = aes_eeprom_read(AES_MEM_ADDR_STATUS, 1, &aes_status);
285295
if (!aes_status && !ret) {
286296
break;
287297
} else if (cnt++ > timeout) {
288-
sprintf(message, "EEPROM read error [status]: %u %i", aes_status, ret);
289-
commander_fill_report("eeprom", message, DBB_ERROR);
290-
return;
298+
snprintf(errmsg, sizeof(errmsg), "%s EEPROM read status: %u %i",
299+
flag_msg(DBB_ERR_MEM_ATAES), aes_status, ret);
300+
commander_fill_report(cmd_str(CMD_ataes), errmsg, DBB_ERR_MEM_ATAES);
301+
return DBB_ERROR;
291302
}
292303
delay_ms(delay);
293304
}
294305
}
306+
307+
return DBB_OK;
295308
}
296309

src/ataes132.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
#ifndef _AES132_H_
2929
#define _AES132_H_
3030

31+
3132
#include <stdint.h>
3233

34+
3335
#define AES_DEVICE_ADDR 0x50u
3436
#define AES_MEM_ADDR_LEN 2
3537
#define AES_MEM_ADDR_IO 0xFE00
@@ -39,16 +41,17 @@
3941
#define AES_TWI_ID ID_TWI0
4042
#define AES_TWI_SPEED 10000
4143

44+
4245
void aes_init(void);
4346
uint8_t aes_eeprom_write(uint32_t u32_start_address, uint16_t u16_length,
4447
uint8_t const *p_wr_buffer);
4548
uint32_t aes_eeprom_read(uint32_t u32_start_address, uint16_t u16_length,
4649
uint8_t *p_rd_buffer);
4750
void aes_calculate_crc(uint8_t length, const uint8_t *data, uint8_t *crc);
48-
void aes_process(uint8_t const *command, uint16_t cmd_len, uint8_t *response_block,
49-
uint16_t response_len);
50-
void aes_eeprom(uint16_t LEN, uint32_t ADDR, uint8_t *userdata_read,
51-
const uint8_t *userdata_write);
51+
int aes_process(uint8_t const *command, uint16_t cmd_len, uint8_t *response_block,
52+
uint16_t response_len);
53+
int aes_eeprom(uint16_t LEN, uint32_t ADDR, uint8_t *userdata_read,
54+
const uint8_t *userdata_write);
5255

5356

54-
#endif
57+
#endif

0 commit comments

Comments
 (0)