Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit c46fa34

Browse files
author
HYChang
committed
fix(endpoint): Unify error/status code for consistency
The error and the status code inside defined_error.h should be unified. This commit replaces all the endpoint_retcode_t to status_t structure for error/status code consistency. Besides, it also removes the defined_error cc_library and the bazel rule depending on it. Close #598
1 parent f74226a commit c46fa34

File tree

19 files changed

+190
-197
lines changed

19 files changed

+190
-197
lines changed

common/BUILD

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,3 @@ cc_library(
4545
"@entangled//common/model:transaction",
4646
],
4747
)
48-
49-
cc_library(
50-
name = "defined_error",
51-
hdrs = ["defined_error.h"],
52-
)

common/defined_error.h

Lines changed: 0 additions & 41 deletions
This file was deleted.

common/ta_errors.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ extern "C" {
5656
#define SC_MODULE_MQTT (0x0A << SC_MODULE_SHIFT)
5757
#define SC_MODULE_STORAGE (0x0B << SC_MODULE_SHIFT)
5858
#define SC_MODULE_CORE (0x0C << SC_MODULE_SHIFT)
59+
#define SC_MODULE_ENDPOINT (0x0D << SC_MODULE_SHIFT)
5960
/** @} */
6061

6162
/** @name serverity code */
@@ -179,6 +180,28 @@ typedef enum {
179180
SC_UTILS_TIMER_ERROR = 0x02 | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
180181
/**< Errors occurred in timer function */
181182
SC_UTILS_TIMER_EXPIRED = 0x03 | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
183+
/**< Failed to send message */
184+
SC_UTILS_HTTPS_SEND_ERROR = 0x04 | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
185+
/**< HTTPS module initialize error */
186+
SC_UTILS_HTTPS_INIT_ERROR = 0x05 | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
187+
/**< HTTPS X509 certificate parse error */
188+
SC_UTILS_HTTPS_X509_ERROR = 0x06 | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
189+
/**< HTTPS initial connection error */
190+
SC_UTILS_HTTPS_CONN_ERROR = 0x07 | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
191+
/**< HTTPS setting SSL config error */
192+
SC_UTILS_HTTPS_SSL_ERROR = 0x08 | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
193+
/**< HTTPS response error */
194+
SC_UTILS_HTTPS_RESPONSE_ERROR = 0x09 | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
195+
/**< Error occured when serialize message */
196+
SC_UTILS_TEXT_SERIALIZE = 0x0A | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
197+
/**< Error occured when deserialize message */
198+
SC_UTILS_TEXT_DESERIALIZE = 0x0B | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
199+
/**< Out of memory error */
200+
SC_UTILS_OOM_ERROR = 0x0C | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
201+
/**< Overflow error */
202+
SC_UTILS_OVERFLOW_ERROR = 0x0D | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
203+
/**< Error occured when encrypt or descrypt message */
204+
SC_UTILS_CIPHER_ERROR = 0x0E | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
182205
/**< Timer expired */
183206

184207
// HTTP module
@@ -231,6 +254,22 @@ typedef enum {
231254
SC_CORE_IRI_UNSYNC = 0x03 | SC_MODULE_CORE | SC_SEVERITY_FATAL,
232255
/**< IRI host is unsynchronized */
233256

257+
// Endpoint module
258+
/**< Failed to initialize the device */
259+
SC_ENDPOINT_DEVICE_INIT = 0x01 | SC_MODULE_ENDPOINT | SC_SEVERITY_FATAL,
260+
/**< Failed to finalize the device */
261+
SC_ENDPOINT_DEVICE_FINI = 0x02 | SC_MODULE_ENDPOINT | SC_SEVERITY_FATAL,
262+
/**< Uart error occured in device component */
263+
SC_ENDPOINT_UART = 0x03 | SC_MODULE_ENDPOINT | SC_SEVERITY_FATAL,
264+
/**< Error occured inside secure storage */
265+
SC_ENDPOINT_SEC_FAULT = 0x04 | SC_MODULE_ENDPOINT | SC_SEVERITY_MINOR,
266+
/**< Error occured when item not found inside secure storage */
267+
SC_ENDPOINT_SEC_ITEM_NOT_FOUND = 0x05 | SC_MODULE_ENDPOINT | SC_SEVERITY_MINOR,
268+
/**< Error occured when the secure storage service is unavailable */
269+
SC_ENDPOINT_SEC_UNAVAILABLE = 0x06 | SC_MODULE_ENDPOINT | SC_SEVERITY_MINOR,
270+
/**< Error occured when the sending transfer message */
271+
SC_ENDPOINT_SEND_TRANSFER = 0x07 | SC_MODULE_ENDPOINT | SC_SEVERITY_FATAL,
272+
234273
} status_t;
235274

236275
typedef enum {

endpoint/endpoint.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ const char* SSL_SEED = STRINGIZE_VALUE_OF(TA_SSL_SEED);
2929

3030
#define SEND_TRANSACTION_API "transaction/"
3131

32-
endpoint_retcode_t send_transaction_information(int value, const char* message, const char* message_fmt,
33-
const char* tag, const char* address, const char* next_address,
34-
const uint8_t* private_key, const char* device_id, uint8_t* iv) {
32+
status_t send_transaction_information(int value, const char* message, const char* message_fmt, const char* tag,
33+
const char* address, const char* next_address, const uint8_t* private_key,
34+
const char* device_id, uint8_t* iv) {
3535
char tryte_msg[MAX_MSG_LEN] = {0};
3636
char msg[MAX_MSG_LEN] = {0};
3737
char ciphertext[MAX_MSG_LEN] = {0};
@@ -42,7 +42,7 @@ endpoint_retcode_t send_transaction_information(int value, const char* message,
4242
if (ret < 0) {
4343
// FIXME: Replace to default logger
4444
fprintf(stderr, "message is to long");
45-
return RET_FAULT;
45+
return SC_ENDPOINT_SEND_TRANSFER;
4646
}
4747

4848
size_t msg_len = 0;
@@ -59,7 +59,7 @@ endpoint_retcode_t send_transaction_information(int value, const char* message,
5959
memcpy(iv, encrypt_ctx.iv, AES_IV_SIZE);
6060

6161
// FIXME: Replace to default logger
62-
if (ret != RET_OK) {
62+
if (ret != SC_OK) {
6363
fprintf(stderr, "%s\n", "encrypt msg error");
6464
return ret;
6565
}
@@ -72,14 +72,14 @@ endpoint_retcode_t send_transaction_information(int value, const char* message,
7272
if (ret < 0) {
7373
// FIXME: Replace to default logger
7474
fprintf(stderr, "message is to long");
75-
return RET_FAULT;
75+
return SC_ENDPOINT_SEND_TRANSFER;
7676
}
7777

78-
if (send_https_msg(HOST, PORT, SEND_TRANSACTION_API, req_body, MAX_MSG_LEN, SSL_SEED) != RET_OK) {
78+
if (send_https_msg(HOST, PORT, SEND_TRANSACTION_API, req_body, MAX_MSG_LEN, SSL_SEED) != SC_OK) {
7979
// FIXME: Replace to default logger
8080
fprintf(stderr, "%s\n", "send http message error");
81-
return RET_FAULT;
81+
return SC_ENDPOINT_SEND_TRANSFER;
8282
}
8383

84-
return RET_OK;
84+
return SC_OK;
8585
}

endpoint/endpoint.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#define MAX_MSG_LEN 1024
1414

1515
#include <stdint.h>
16-
#include "common/defined_error.h"
16+
#include "common/ta_errors.h"
1717

1818
/**
1919
* @brief Send transaction information to tangle accelerator
@@ -30,10 +30,10 @@
3030
* @param[in] device_id Device id from device
3131
* @param[in/out] iv Initialization vector, must be read/write. The length of iv must be AES_IV_SIZE @see #ta_cipher_ctx
3232
*
33-
* @return #endpoint_retcode_t
33+
* @return #status_t
3434
*/
35-
endpoint_retcode_t send_transaction_information(const int value, const char* message, const char* message_fmt,
36-
const char* tag, const char* address, const char* next_address,
37-
const uint8_t* private_key, const char* device_id, uint8_t* iv);
35+
status_t send_transaction_information(const int value, const char* message, const char* message_fmt, const char* tag,
36+
const char* address, const char* next_address, const uint8_t* private_key,
37+
const char* device_id, uint8_t* iv);
3838

3939
#endif // ENDPOINT_H

endpoint/hal/device.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,28 @@ device_t *ta_device(const char *type) {
3232
return *p;
3333
}
3434

35-
endpoint_retcode_t register_device(struct device_type *dv) {
36-
endpoint_retcode_t res = RET_OK;
35+
status_t register_device(struct device_type *dv) {
36+
status_t res = SC_OK;
3737
struct device_type **p;
3838
if (dv->next) {
39-
return -RET_DEVICE_INIT;
39+
return SC_DEVICE_INIT;
4040
}
4141
p = find_device(dv->name, strlen(dv->name));
4242
if (*p) {
43-
res = -RET_DEVICE_INIT;
43+
res = SC_DEVICE_INIT;
4444
} else {
4545
*p = dv;
4646
}
4747
return res;
4848
}
4949

50-
endpoint_retcode_t unregister_device(struct device_type *dv) {
50+
status_t unregister_device(struct device_type *dv) {
5151
for (struct device_type **tmp = &devices; *tmp != NULL; tmp = &(*tmp)->next) {
5252
if (dv == *tmp) {
5353
*tmp = dv->next;
5454
dv->next = NULL;
55-
return RET_OK;
55+
return SC_OK;
5656
}
5757
}
58-
return -RET_DEVICE_FINI;
58+
return SC_DEVICE_FINI;
5959
}

endpoint/hal/device.h

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extern "C" {
1717
#include <stdio.h>
1818
#include <stdlib.h>
1919
#include <string.h>
20-
#include "defined_error.h"
20+
#include "ta_errors.h"
2121

2222
/*! device initialization entry point */
2323
#define DECLARE_DEVICE(name) \
@@ -27,21 +27,21 @@ extern "C" {
2727
typedef struct device_type device_t;
2828

2929
struct device_operations {
30-
endpoint_retcode_t (*init)(void); /**< initialize device */
31-
void (*fini)(void); /**< destructor of device */
32-
endpoint_retcode_t (*get_key)(uint8_t *); /**< get device private key */
33-
endpoint_retcode_t (*get_device_id)(uint8_t *); /**< get device id */
30+
status_t (*init)(void); /**< initialize device */
31+
void (*fini)(void); /**< destructor of device */
32+
status_t (*get_key)(uint8_t *); /**< get device private key */
33+
status_t (*get_device_id)(uint8_t *); /**< get device id */
3434
};
3535

3636
struct uart_operations {
37-
endpoint_retcode_t (*init)(const uint8_t *device); /**< initialize uart */
38-
void (*write)(const int fd, const char *cmd); /**< write command to uart */
39-
char *(*read)(const int fd); /**< read from uart */
40-
void (*clean)(const int fd); /**< flush uart buffer */
37+
status_t (*init)(const uint8_t *device); /**< initialize uart */
38+
void (*write)(const int fd, const char *cmd); /**< write command to uart */
39+
char *(*read)(const int fd); /**< read from uart */
40+
void (*clean)(const int fd); /**< flush uart buffer */
4141
};
4242

4343
struct secure_store_operations {
44-
endpoint_retcode_t (*init)(void); /**< initialize secure storage */
44+
status_t (*init)(void); /**< initialize secure storage */
4545
/**
4646
* @brief Write an item to secure storage
4747
*
@@ -50,12 +50,12 @@ struct secure_store_operations {
5050
* @param[in] buf_size Length of data
5151
*
5252
* @return
53-
* - #RET_OK on success
53+
* - #SC_OK on success
5454
* - #RET_NO_MEMORY on no memory error
55-
* - #RET_UNAVAILABLE on unavailable secure storage
56-
* - #RET_FAULT on some other error
55+
* - #SC_ENDPOINT_SEC_UNAVAILABLE on unavailable secure storage
56+
* - #SC_ENDPOINT_SEC_FAULT on some other error
5757
*/
58-
endpoint_retcode_t (*write)(const char *name, const uint8_t *buf, size_t buf_size);
58+
status_t (*write)(const char *name, const uint8_t *buf, size_t buf_size);
5959
/**
6060
* @brief Read an item from secure storage
6161
*
@@ -64,25 +64,25 @@ struct secure_store_operations {
6464
* @param[out] buf_size Pointer to length of data
6565
*
6666
* @return
67-
* - #RET_OK on success
68-
* - #RET_OVERFLOW error on the buffer is too small
69-
* - #RET_NOT_FOUND error on the item not found inside secure storage
70-
* - #RET_UNAVAILABLE error if the storage is currently unavailable
71-
* - #RET_FAULT on some other error
67+
* - #SC_OK on success
68+
* - #SC_UTILS_OVERFLOW_ERROR error on the buffer is too small
69+
* - #SC_ENDPOINT_SEC_ITEM_NOT_FOUND error on the item not found inside secure storage
70+
* - #SC_ENDPOINT_SEC_UNAVAILABLE error if the storage is currently unavailable
71+
* - #SC_ENDPOINT_SEC_FAULT on some other error
7272
*/
73-
endpoint_retcode_t (*read)(const char *name, uint8_t *buf, size_t *buf_size);
73+
status_t (*read)(const char *name, uint8_t *buf, size_t *buf_size);
7474
/**
7575
* @brief Delete item in secure storage
7676
*
7777
* @param[in] name Name of item
7878
*
7979
* @return
80-
* - #RET_OK on success
81-
* - #RET_NOT_FOUND error on the item not found inside secure storage
82-
* - #RET_UNAVAILABLE error if the storage is currently unavailable
83-
* - #RET_FAULT if there are some other error
80+
* - #SC_OK on success
81+
* - #SC_ENDPOINT_SEC_ITEM_NOT_FOUND error on the item not found inside secure storage
82+
* - #SC_ENDPOINT_SEC_UNAVAILABLE error if the storage is currently unavailable
83+
* - #SC_ENDPOINT_SEC_FAULT if there are some other error
8484
*/
85-
endpoint_retcode_t (*delete)(const char *name);
85+
status_t (*delete)(const char *name);
8686
};
8787

8888
struct device_type {
@@ -112,21 +112,21 @@ device_t *ta_device(const char *device);
112112
* @param[in] dv Device to register
113113
*
114114
* @return
115-
* - #RET_OK on success
116-
* - #RET_DEVICE_INIT on failed
115+
* - #SC_OK on success
116+
* - #SC_DEVICE_INIT on failed
117117
*/
118-
endpoint_retcode_t register_device(struct device_type *dv);
118+
status_t register_device(struct device_type *dv);
119119

120120
/**
121121
* @brief Unregister device
122122
*
123123
* @param[in] dv Device to unregister
124124
*
125125
* @return
126-
* - #RET_OK on success
127-
* - #RET_DEVICE_FINI on failed
126+
* - #SC_OK on success
127+
* - #SC_DEVICE_FINI on failed
128128
*/
129-
endpoint_retcode_t unregister_device(struct device_type *dv);
129+
status_t unregister_device(struct device_type *dv);
130130

131131
#ifdef __cplusplus
132132
}

endpoint/test_endpoint.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#include <stdio.h>
1111
#include <stdlib.h>
1212
#include <time.h>
13-
#include "common/defined_error.h"
1413
#include "common/macros.h"
14+
#include "common/ta_errors.h"
1515
#include "endpoint.h"
1616
#include "tests/test_define.h"
1717
#include "utils/cipher.h"
@@ -61,9 +61,9 @@ void test_endpoint(void) {
6161
strftime(time_str, 26, "%Y-%m-%d %H:%M:%S", tm_info);
6262
gen_rand_trytes(next_addr, ADDR_LEN);
6363

64-
endpoint_retcode_t ret = send_transaction_information(TEST_VALUE, TEST_MESSAGE, TEST_MESSAGE_FMT, TEST_TAG,
65-
TEST_ADDRESS, next_addr, test_key, TEST_DEVICE_ID, iv);
66-
TEST_ASSERT(ret == RET_OK);
64+
status_t ret = send_transaction_information(TEST_VALUE, TEST_MESSAGE, TEST_MESSAGE_FMT, TEST_TAG, TEST_ADDRESS,
65+
next_addr, test_key, TEST_DEVICE_ID, iv);
66+
TEST_ASSERT(ret == SC_OK);
6767
}
6868

6969
int main(int argc, char *argv[]) {

0 commit comments

Comments
 (0)