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

Commit cd3c1eb

Browse files
authored
Merge pull request #601 from DLTcollab/endpoint-logger
fix(endpoint): Use the logger
2 parents e530f90 + 79b6b43 commit cd3c1eb

File tree

7 files changed

+88
-19
lines changed

7 files changed

+88
-19
lines changed

endpoint/endpoint_core.c

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <stdlib.h>
1212
#include <string.h>
1313
#include <time.h>
14+
#include "common/logger.h"
1415
#include "http_parser.h"
1516
#include "utils/cipher.h"
1617
#include "utils/connectivity/conn_http.h"
@@ -31,9 +32,31 @@ static const char* SSL_SEED = STR(ENDPOINT_SSL_SEED);
3132

3233
#define SEND_TRANSACTION_API "transaction/"
3334

34-
status_t send_transaction_information(int value, const char* message, const char* message_fmt, const char* tag,
35-
const char* address, const char* next_address, const uint8_t* private_key,
36-
const char* device_id, uint8_t* iv) {
35+
#define ENDPOINT_LOGGER "endpoint"
36+
37+
static logger_id_t logger_id;
38+
39+
void endpoint_init() {
40+
ta_logger_init();
41+
// Logger initialization of endpoint
42+
logger_id = logger_helper_enable(ENDPOINT_LOGGER, LOGGER_DEBUG, true);
43+
44+
// Logger initialization of other included components
45+
cipher_logger_init();
46+
}
47+
48+
void endpoint_destroy() {
49+
// Logger release of other included components
50+
cipher_logger_release();
51+
52+
// Logger release of endpoint
53+
logger_helper_release(logger_id);
54+
logger_helper_destroy();
55+
}
56+
57+
status_t send_transaction_information(int value, const char* message, const char* message_fmt,
58+
const char* tag, const char* address, const char* next_address,
59+
const uint8_t* private_key, const char* device_id, uint8_t* iv) {
3760
char tryte_msg[MAX_MSG_LEN] = {0};
3861
char msg[MAX_MSG_LEN] = {0};
3962
char req_body[MAX_MSG_LEN] = {0};
@@ -42,8 +65,7 @@ status_t send_transaction_information(int value, const char* message, const char
4265

4366
int ret = snprintf((char*)raw_msg, MAX_MSG_LEN, "%s:%s", next_address, message);
4467
if (ret < 0) {
45-
// FIXME: Replace to default logger
46-
fprintf(stderr, "message is to long");
68+
ta_log_error("The message is too long.\n");
4769
return SC_ENDPOINT_SEND_TRANSFER;
4870
}
4971

@@ -65,9 +87,8 @@ status_t send_transaction_information(int value, const char* message, const char
6587
ret = aes_encrypt(&encrypt_ctx);
6688
memcpy(iv, encrypt_ctx.iv, AES_IV_SIZE);
6789

68-
// FIXME: Replace to default logger
69-
if (ret != SC_OK) {
70-
fprintf(stderr, "%s\n", "encrypt msg error");
90+
if (ret != RET_OK) {
91+
ta_log_error("Encrypt message error.\n");
7192
return ret;
7293
}
7394
serialize_msg(&encrypt_ctx, msg, &msg_len);
@@ -77,14 +98,12 @@ status_t send_transaction_information(int value, const char* message, const char
7798

7899
ret = snprintf(req_body, MAX_MSG_LEN, REQ_BODY, value, tryte_msg, message_fmt, tag, address);
79100
if (ret < 0) {
80-
// FIXME: Replace to default logger
81-
fprintf(stderr, "message is to long");
101+
ta_log_error("The message is too long.\n");
82102
return SC_ENDPOINT_SEND_TRANSFER;
83103
}
84104

85-
if (send_https_msg(HOST, PORT, SEND_TRANSACTION_API, req_body, SSL_SEED) != SC_OK) {
86-
// FIXME: Replace to default logger
87-
fprintf(stderr, "%s\n", "send http message error");
105+
if (send_https_msg(HOST, PORT, SEND_TRANSACTION_API, req_body, MAX_MSG_LEN, SSL_SEED) != RET_OK) {
106+
ta_log_error("http message sending error.\n");
88107
return SC_ENDPOINT_SEND_TRANSFER;
89108
}
90109

endpoint/endpoint_core.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515
#include <stdint.h>
1616
#include "common/ta_errors.h"
1717

18+
/**
19+
* @brief Initialization of endpoint
20+
*/
21+
void endpoint_init(void);
22+
23+
/**
24+
* @brief Destruction of endpoint
25+
*/
26+
void endpoint_destroy(void);
27+
1828
/**
1929
* @brief Send transaction information to tangle accelerator
2030
*

endpoint/test_endpoint_core.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ void test_endpoint(void) {
6262

6363
int main(void) {
6464
UNITY_BEGIN();
65+
endpoint_init();
66+
6567
RUN_TEST(test_endpoint);
68+
69+
endpoint_destroy();
6670
return UNITY_END();
6771
}

tests/unit-test/test_cipher.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,15 @@ void test_cipher2(void) {
125125
int main(void) {
126126
UNITY_BEGIN();
127127

128+
// Initialize logger
129+
if (ta_logger_init() != SC_OK) {
130+
return EXIT_FAILURE;
131+
}
132+
133+
cipher_logger_init();
128134
RUN_TEST(test_cipher1);
129135
RUN_TEST(test_cipher2);
136+
cipher_logger_release();
130137

131138
return UNITY_END();
132139
}

utils/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ cc_library(
5050
hdrs = ["cipher.h"],
5151
deps = [
5252
"//common:ta_errors",
53+
"//common:ta_logger",
5354
"@mbedtls_2_16_6",
5455
],
5556
)

utils/cipher.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,30 @@
1212
#include <stdlib.h>
1313
#include <string.h>
1414
#include <time.h>
15+
#include "common/logger.h"
1516
#include "mbedtls/aes.h"
1617
#include "mbedtls/md.h"
1718
#include "mbedtls/platform_util.h"
1819

1920
#define MAX_TIMESTAMP_LEN 20
21+
#define CIPHER_LOGGER "cipher"
22+
23+
static logger_id_t logger_id;
24+
25+
void cipher_logger_init() { logger_id = logger_helper_enable(CIPHER_LOGGER, LOGGER_DEBUG, true); }
26+
27+
int cipher_logger_release() {
28+
logger_helper_release(logger_id);
29+
if (logger_helper_destroy() != RC_OK) {
30+
ta_log_error("Destroying logger failed %s.\n", CIPHER_LOGGER);
31+
return EXIT_FAILURE;
32+
}
33+
34+
return 0;
35+
}
2036

2137
status_t aes_decrypt(ta_cipher_ctx* cipher_ctx) {
22-
// FIXME: Add logger and some checks here
38+
// FIXME: Add some checks here
2339
mbedtls_aes_context ctx;
2440
mbedtls_md_context_t sha_ctx;
2541
int status;
@@ -84,15 +100,14 @@ status_t aes_decrypt(ta_cipher_ctx* cipher_ctx) {
84100
}
85101
status = SC_OK;
86102
exit:
87-
// FIXME: Use default logger instead
88-
if (!err) fprintf(stderr, "%s\n", err);
103+
if (!err) ta_log_error("%s\n", err);
89104
mbedtls_aes_free(&ctx);
90105
mbedtls_md_free(&sha_ctx);
91106
return status;
92107
}
93108

94109
status_t aes_encrypt(ta_cipher_ctx* cipher_ctx) {
95-
// FIXME: Add logger and some checks here
110+
// FIXME: Add some checks here
96111
char* err = NULL;
97112
int status = 0;
98113
uint8_t* plaintext = cipher_ctx->plaintext;
@@ -172,8 +187,7 @@ status_t aes_encrypt(ta_cipher_ctx* cipher_ctx) {
172187
memcpy(cipher_ctx->hmac, digest, TA_AES_HMAC_SIZE);
173188
status = SC_OK;
174189
exit:
175-
// FIXME: Use default logger instead
176-
if (!err) fprintf(stderr, "%s\n", err);
190+
if (!err) ta_log_error("%s\n", err);
177191
mbedtls_aes_free(&ctx);
178192
mbedtls_md_free(&sha_ctx);
179193
return status;

utils/cipher.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ typedef struct ta_cipher_ctx {
3838
uint64_t timestamp; /**< Timestamp for generate hmac */
3939
} ta_cipher_ctx;
4040

41+
/**
42+
* @brief Initialize logger of cipher
43+
*/
44+
void cipher_logger_init();
45+
46+
/**
47+
* @brief Release logger of cipher
48+
*
49+
* @return
50+
* - zero on success
51+
* - EXIT_FAILURE on error
52+
*/
53+
int cipher_logger_release();
54+
4155
/**
4256
* @brief Encrypt plaintext
4357
*

0 commit comments

Comments
 (0)