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

Commit

Permalink
fix(endpoint): Unify error/status code for consistency
Browse files Browse the repository at this point in the history
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
  • Loading branch information
splasky committed May 14, 2020
1 parent f74226a commit c46fa34
Show file tree
Hide file tree
Showing 19 changed files with 190 additions and 197 deletions.
5 changes: 0 additions & 5 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,3 @@ cc_library(
"@entangled//common/model:transaction",
],
)

cc_library(
name = "defined_error",
hdrs = ["defined_error.h"],
)
41 changes: 0 additions & 41 deletions common/defined_error.h

This file was deleted.

39 changes: 39 additions & 0 deletions common/ta_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ extern "C" {
#define SC_MODULE_MQTT (0x0A << SC_MODULE_SHIFT)
#define SC_MODULE_STORAGE (0x0B << SC_MODULE_SHIFT)
#define SC_MODULE_CORE (0x0C << SC_MODULE_SHIFT)
#define SC_MODULE_ENDPOINT (0x0D << SC_MODULE_SHIFT)
/** @} */

/** @name serverity code */
Expand Down Expand Up @@ -179,6 +180,28 @@ typedef enum {
SC_UTILS_TIMER_ERROR = 0x02 | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
/**< Errors occurred in timer function */
SC_UTILS_TIMER_EXPIRED = 0x03 | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
/**< Failed to send message */
SC_UTILS_HTTPS_SEND_ERROR = 0x04 | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
/**< HTTPS module initialize error */
SC_UTILS_HTTPS_INIT_ERROR = 0x05 | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
/**< HTTPS X509 certificate parse error */
SC_UTILS_HTTPS_X509_ERROR = 0x06 | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
/**< HTTPS initial connection error */
SC_UTILS_HTTPS_CONN_ERROR = 0x07 | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
/**< HTTPS setting SSL config error */
SC_UTILS_HTTPS_SSL_ERROR = 0x08 | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
/**< HTTPS response error */
SC_UTILS_HTTPS_RESPONSE_ERROR = 0x09 | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
/**< Error occured when serialize message */
SC_UTILS_TEXT_SERIALIZE = 0x0A | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
/**< Error occured when deserialize message */
SC_UTILS_TEXT_DESERIALIZE = 0x0B | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
/**< Out of memory error */
SC_UTILS_OOM_ERROR = 0x0C | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
/**< Overflow error */
SC_UTILS_OVERFLOW_ERROR = 0x0D | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
/**< Error occured when encrypt or descrypt message */
SC_UTILS_CIPHER_ERROR = 0x0E | SC_MODULE_UTILS | SC_SEVERITY_FATAL,
/**< Timer expired */

// HTTP module
Expand Down Expand Up @@ -231,6 +254,22 @@ typedef enum {
SC_CORE_IRI_UNSYNC = 0x03 | SC_MODULE_CORE | SC_SEVERITY_FATAL,
/**< IRI host is unsynchronized */

// Endpoint module
/**< Failed to initialize the device */
SC_ENDPOINT_DEVICE_INIT = 0x01 | SC_MODULE_ENDPOINT | SC_SEVERITY_FATAL,
/**< Failed to finalize the device */
SC_ENDPOINT_DEVICE_FINI = 0x02 | SC_MODULE_ENDPOINT | SC_SEVERITY_FATAL,
/**< Uart error occured in device component */
SC_ENDPOINT_UART = 0x03 | SC_MODULE_ENDPOINT | SC_SEVERITY_FATAL,
/**< Error occured inside secure storage */
SC_ENDPOINT_SEC_FAULT = 0x04 | SC_MODULE_ENDPOINT | SC_SEVERITY_MINOR,
/**< Error occured when item not found inside secure storage */
SC_ENDPOINT_SEC_ITEM_NOT_FOUND = 0x05 | SC_MODULE_ENDPOINT | SC_SEVERITY_MINOR,
/**< Error occured when the secure storage service is unavailable */
SC_ENDPOINT_SEC_UNAVAILABLE = 0x06 | SC_MODULE_ENDPOINT | SC_SEVERITY_MINOR,
/**< Error occured when the sending transfer message */
SC_ENDPOINT_SEND_TRANSFER = 0x07 | SC_MODULE_ENDPOINT | SC_SEVERITY_FATAL,

} status_t;

typedef enum {
Expand Down
18 changes: 9 additions & 9 deletions endpoint/endpoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ const char* SSL_SEED = STRINGIZE_VALUE_OF(TA_SSL_SEED);

#define SEND_TRANSACTION_API "transaction/"

endpoint_retcode_t send_transaction_information(int value, const char* message, const char* message_fmt,
const char* tag, const char* address, const char* next_address,
const uint8_t* private_key, const char* device_id, uint8_t* iv) {
status_t send_transaction_information(int value, const char* message, const char* message_fmt, const char* tag,
const char* address, const char* next_address, const uint8_t* private_key,
const char* device_id, uint8_t* iv) {
char tryte_msg[MAX_MSG_LEN] = {0};
char msg[MAX_MSG_LEN] = {0};
char ciphertext[MAX_MSG_LEN] = {0};
Expand All @@ -42,7 +42,7 @@ endpoint_retcode_t send_transaction_information(int value, const char* message,
if (ret < 0) {
// FIXME: Replace to default logger
fprintf(stderr, "message is to long");
return RET_FAULT;
return SC_ENDPOINT_SEND_TRANSFER;
}

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

// FIXME: Replace to default logger
if (ret != RET_OK) {
if (ret != SC_OK) {
fprintf(stderr, "%s\n", "encrypt msg error");
return ret;
}
Expand All @@ -72,14 +72,14 @@ endpoint_retcode_t send_transaction_information(int value, const char* message,
if (ret < 0) {
// FIXME: Replace to default logger
fprintf(stderr, "message is to long");
return RET_FAULT;
return SC_ENDPOINT_SEND_TRANSFER;
}

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

return RET_OK;
return SC_OK;
}
10 changes: 5 additions & 5 deletions endpoint/endpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define MAX_MSG_LEN 1024

#include <stdint.h>
#include "common/defined_error.h"
#include "common/ta_errors.h"

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

#endif // ENDPOINT_H
14 changes: 7 additions & 7 deletions endpoint/hal/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,28 @@ device_t *ta_device(const char *type) {
return *p;
}

endpoint_retcode_t register_device(struct device_type *dv) {
endpoint_retcode_t res = RET_OK;
status_t register_device(struct device_type *dv) {
status_t res = SC_OK;
struct device_type **p;
if (dv->next) {
return -RET_DEVICE_INIT;
return SC_DEVICE_INIT;
}
p = find_device(dv->name, strlen(dv->name));
if (*p) {
res = -RET_DEVICE_INIT;
res = SC_DEVICE_INIT;
} else {
*p = dv;
}
return res;
}

endpoint_retcode_t unregister_device(struct device_type *dv) {
status_t unregister_device(struct device_type *dv) {
for (struct device_type **tmp = &devices; *tmp != NULL; tmp = &(*tmp)->next) {
if (dv == *tmp) {
*tmp = dv->next;
dv->next = NULL;
return RET_OK;
return SC_OK;
}
}
return -RET_DEVICE_FINI;
return SC_DEVICE_FINI;
}
62 changes: 31 additions & 31 deletions endpoint/hal/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extern "C" {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defined_error.h"
#include "ta_errors.h"

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

struct device_operations {
endpoint_retcode_t (*init)(void); /**< initialize device */
void (*fini)(void); /**< destructor of device */
endpoint_retcode_t (*get_key)(uint8_t *); /**< get device private key */
endpoint_retcode_t (*get_device_id)(uint8_t *); /**< get device id */
status_t (*init)(void); /**< initialize device */
void (*fini)(void); /**< destructor of device */
status_t (*get_key)(uint8_t *); /**< get device private key */
status_t (*get_device_id)(uint8_t *); /**< get device id */
};

struct uart_operations {
endpoint_retcode_t (*init)(const uint8_t *device); /**< initialize uart */
void (*write)(const int fd, const char *cmd); /**< write command to uart */
char *(*read)(const int fd); /**< read from uart */
void (*clean)(const int fd); /**< flush uart buffer */
status_t (*init)(const uint8_t *device); /**< initialize uart */
void (*write)(const int fd, const char *cmd); /**< write command to uart */
char *(*read)(const int fd); /**< read from uart */
void (*clean)(const int fd); /**< flush uart buffer */
};

struct secure_store_operations {
endpoint_retcode_t (*init)(void); /**< initialize secure storage */
status_t (*init)(void); /**< initialize secure storage */
/**
* @brief Write an item to secure storage
*
Expand All @@ -50,12 +50,12 @@ struct secure_store_operations {
* @param[in] buf_size Length of data
*
* @return
* - #RET_OK on success
* - #SC_OK on success
* - #RET_NO_MEMORY on no memory error
* - #RET_UNAVAILABLE on unavailable secure storage
* - #RET_FAULT on some other error
* - #SC_ENDPOINT_SEC_UNAVAILABLE on unavailable secure storage
* - #SC_ENDPOINT_SEC_FAULT on some other error
*/
endpoint_retcode_t (*write)(const char *name, const uint8_t *buf, size_t buf_size);
status_t (*write)(const char *name, const uint8_t *buf, size_t buf_size);
/**
* @brief Read an item from secure storage
*
Expand All @@ -64,25 +64,25 @@ struct secure_store_operations {
* @param[out] buf_size Pointer to length of data
*
* @return
* - #RET_OK on success
* - #RET_OVERFLOW error on the buffer is too small
* - #RET_NOT_FOUND error on the item not found inside secure storage
* - #RET_UNAVAILABLE error if the storage is currently unavailable
* - #RET_FAULT on some other error
* - #SC_OK on success
* - #SC_UTILS_OVERFLOW_ERROR error on the buffer is too small
* - #SC_ENDPOINT_SEC_ITEM_NOT_FOUND error on the item not found inside secure storage
* - #SC_ENDPOINT_SEC_UNAVAILABLE error if the storage is currently unavailable
* - #SC_ENDPOINT_SEC_FAULT on some other error
*/
endpoint_retcode_t (*read)(const char *name, uint8_t *buf, size_t *buf_size);
status_t (*read)(const char *name, uint8_t *buf, size_t *buf_size);
/**
* @brief Delete item in secure storage
*
* @param[in] name Name of item
*
* @return
* - #RET_OK on success
* - #RET_NOT_FOUND error on the item not found inside secure storage
* - #RET_UNAVAILABLE error if the storage is currently unavailable
* - #RET_FAULT if there are some other error
* - #SC_OK on success
* - #SC_ENDPOINT_SEC_ITEM_NOT_FOUND error on the item not found inside secure storage
* - #SC_ENDPOINT_SEC_UNAVAILABLE error if the storage is currently unavailable
* - #SC_ENDPOINT_SEC_FAULT if there are some other error
*/
endpoint_retcode_t (*delete)(const char *name);
status_t (*delete)(const char *name);
};

struct device_type {
Expand Down Expand Up @@ -112,21 +112,21 @@ device_t *ta_device(const char *device);
* @param[in] dv Device to register
*
* @return
* - #RET_OK on success
* - #RET_DEVICE_INIT on failed
* - #SC_OK on success
* - #SC_DEVICE_INIT on failed
*/
endpoint_retcode_t register_device(struct device_type *dv);
status_t register_device(struct device_type *dv);

/**
* @brief Unregister device
*
* @param[in] dv Device to unregister
*
* @return
* - #RET_OK on success
* - #RET_DEVICE_FINI on failed
* - #SC_OK on success
* - #SC_DEVICE_FINI on failed
*/
endpoint_retcode_t unregister_device(struct device_type *dv);
status_t unregister_device(struct device_type *dv);

#ifdef __cplusplus
}
Expand Down
8 changes: 4 additions & 4 deletions endpoint/test_endpoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "common/defined_error.h"
#include "common/macros.h"
#include "common/ta_errors.h"
#include "endpoint.h"
#include "tests/test_define.h"
#include "utils/cipher.h"
Expand Down Expand Up @@ -61,9 +61,9 @@ void test_endpoint(void) {
strftime(time_str, 26, "%Y-%m-%d %H:%M:%S", tm_info);
gen_rand_trytes(next_addr, ADDR_LEN);

endpoint_retcode_t ret = send_transaction_information(TEST_VALUE, TEST_MESSAGE, TEST_MESSAGE_FMT, TEST_TAG,
TEST_ADDRESS, next_addr, test_key, TEST_DEVICE_ID, iv);
TEST_ASSERT(ret == RET_OK);
status_t ret = send_transaction_information(TEST_VALUE, TEST_MESSAGE, TEST_MESSAGE_FMT, TEST_TAG, TEST_ADDRESS,
next_addr, test_key, TEST_DEVICE_ID, iv);
TEST_ASSERT(ret == SC_OK);
}

int main(int argc, char *argv[]) {
Expand Down
Loading

0 comments on commit c46fa34

Please sign in to comment.