Skip to content

Commit

Permalink
feat: Migrate ta-endpoint into legato
Browse files Browse the repository at this point in the history
Add legato build target into gitignore.

Add new file util/utils.h util/utils.c for utitlity functions.

Add build libta_endpoint.so library target for legato app ta_app into Makefile.

Add ta_app build target and cleanup target into Makefile.

Remove redefined macro inside `conn_http.h`.

Add new directory endpointComponent and files for legato app ta_app.

Move utility functions inside `main.c` into utils.h utils.c.

Add doxgen comment for utils.h.

Fix compile warning inside utils directory files.

Closes DLTcollab#20
  • Loading branch information
splasky committed Feb 25, 2020
1 parent 3beb007 commit a862354
Show file tree
Hide file tree
Showing 15 changed files with 343 additions and 112 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ build/
addr_log.log
tests/build/
*.c.d
*/*.c.d
*/*.c.d

# Legato build targets
_build_ta-endpoint/
ta_endpoint.wp77xx.update
24 changes: 17 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ INCLUDES = -I$(THIRD_PARTY_PATH)/http-parser -I$(MBEDTLS_PATH)/include -I$(ROOT_
LIBS = $(MBEDTLS_PATH)/library/libmbedx509.a $(MBEDTLS_PATH)/library/libmbedtls.a $(MBEDTLS_PATH)/library/libmbedcrypto.a
export INCLUDES

UTILS_OBJS = $(UTILS_PATH)/crypto_utils.o $(UTILS_PATH)/serializer.o $(UTILS_PATH)/tryte_byte_conv.o $(UTILS_PATH)/uart_utils.o
UTILS_OBJS = $(UTILS_PATH)/crypto_utils.o $(UTILS_PATH)/serializer.o $(UTILS_PATH)/tryte_byte_conv.o $(UTILS_PATH)/uart_utils.o $(UTILS_PATH)/utils.o
# We need to modify this rule here to be compatible to the situation
# that we have several different ways of connectivity in the future
CONNECTIVITY_OBJS = conn_http.o

OBJS = main.o $(HTTP_PARSER_PATH)/http_parser.o $(UTILS_OBJS) $(CONNECTIVITY_OBJS)
OBJS = main.o http_parser.o $(UTILS_OBJS) $(CONNECTIVITY_OBJS)
TALIB = libta_endpoint.so

.SUFFIXES:.c .o
.PHONY: all clean test pre-build

all: pre-build ta_client mbedtls_make
all: pre-build ta_client mbedtls_make $(TALIB) ta_app

ta_client: mbedtls_make $(OBJS)
@echo Linking: $@ ....
Expand All @@ -51,27 +52,36 @@ mbedtls_make:
if [ $$? != 0 ]; then exit 1; fi; \
done

$(TALIB): $(UTILS_OBJS) $(CONNECTIVITY_OBJS) http_parser.o
$(CC) -shared -o $@ $<

conn_http.o: connectivity/conn_http.c
@echo Compiling $@ ...
$(CC) -v -c $(CFLAGS) $(INCLUDES) -o $@ $<

$(CC) -c $(CFLAGS) $(INCLUDES) -o $@ $<
http_parser.o: $(THIRD_PARTY_PATH)/http-parser/http_parser.c $(THIRD_PARTY_PATH)/http-parser/http_parser.h
$(CC) -c $(CFLAGS) $(INCLUDES) -o $@ $<
main.o: main.c $(UTILS_OBJS)
@echo Compiling: $< ....
$(CC) -c $(CFLAGS) $(INCLUDES) -o $@ $<
$(UTILS_OBJS):
$(MAKE) -C $(UTILS_PATH)
test: $(TEST_PATH) $(UTILS_OBJS)
$(MAKE) -C $(TEST_PATH)
ta_app: $(TALIB)
mkapp -v -t wp77xx ta-endpoint.adef

clean: clean_client clean_third_party clean_test
clean: clean_client clean_third_party clean_test clean_build_app

clean_test:
$(MAKE) -C $(TEST_PATH) clean

clean_client:
$(MAKE) -C $(UTILS_PATH) clean
$(MAKE) -C $(CONNECTIVITY_PATH) clean
rm -f ta_client *.o *.c.d
rm -f ta_client *.o *.c.d $(TALIB)

clean_build_app:
rm -rf _build_* *.update

clean_third_party: clean_mbedtls clean_http_parser

Expand Down
2 changes: 1 addition & 1 deletion connectivity/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ all: conn_http.o
.NOTPARALLEL:
connectivity/conn_http.o: conn_http.c
@echo Compiling $@ ...
$(CC) -v -c $(CFLAGS) $(INCLUDES) -MMD -MF conn_http.c.d -o $@ $<
$(CC) -fPIC -c $(CFLAGS) $(INCLUDES) -MMD -MF conn_http.c.d -o $@ $<

clean:
rm -f *.o *.c.d
Expand Down
3 changes: 0 additions & 3 deletions connectivity/conn_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#ifdef __cplusplus
extern "C" {
#endif

#include <stdbool.h>
#include "defined_error.h"
#include "http_parser.h"
Expand All @@ -23,8 +22,6 @@ extern "C" {
#include "mbedtls/net.h"
#include "mbedtls/ssl.h"

#define HTTP_OK 200

char *http_res_body;

typedef struct {
Expand Down
41 changes: 41 additions & 0 deletions endpointComponent/Component.cdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
sources:
{
endpoint.c
}

cflags:
{
-g -O0
-DDEBUG=1
-I$CURDIR/../utils
-I$CURDIR/../connectivity
-I$CURDIR/../third_party/http-parser
-I$CURDIR/../third_party/mbedtls/include
}

ldflags:
{
-L$CURDIR/../
}

bundles:
{
file:
{
[r] resolv.conf /etc/
$CURDIR/../libta_endpoint.so /lib/
}
}

requires:
{
device:
{
[rw] /dev/ttyHS0 /dev/ttyHS0
}

lib:
{
libta_endpoint.so
}
}
96 changes: 96 additions & 0 deletions endpointComponent/endpoint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "interfaces.h"
#include "legato.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include "connectivity/conn_http.h"
#include "utils/crypto_utils.h"
#include "utils/serializer.h"
#include "utils/tryte_byte_conv.h"
#include "utils/uart_utils.h"
#include "utils/utils.h"

#define MSG "%s:%s"

COMPONENT_INIT {
uint8_t ciphertext[1024] = {0}, iv[16] = {0}, raw_msg[1000] = {0};
uint32_t raw_msg_len = 1 + ADDR_LEN + 20, ciphertext_len = 0, msg_len;
char tryte_msg[1024] = {0}, msg[1024] = {0}, addr[ADDR_LEN + 1] = ADDRESS, next_addr[ADDR_LEN + 1] = {0};
srand(time(NULL));

// init uart for modem
int fd = uart_init();
if (fd < 0) {
LE_ERROR("Error in initializing UART\n");
}

// create log file
if (log_address(next_addr) != 0) {
LE_ERROR("log address failed\n");
}

char *response = NULL;
time_t timer;
char time_str[26];
struct tm *tm_info;

fd_set rset;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 500;
// TODO:change to none blocking I/O descriptor
while (true) {
FD_ZERO(&rset);
FD_SET(fd, &rset);
select(fd + 1, &rset, NULL, NULL, &tv);

if (FD_ISSET(fd, &rset)) {
time(&timer);
tm_info = localtime(&timer);
strftime(time_str, 26, "%Y-%m-%d %H:%M:%S", tm_info);
printf("%s\n", time_str);
gen_trytes(ADDR_LEN, next_addr);

response = uart_read(fd);
// real transmitted data
snprintf((char *)raw_msg, raw_msg_len, MSG, next_addr, response);
LE_DEBUG("Raw_msg:%s\n", raw_msg);
uint8_t private_key[AES_BLOCK_SIZE * 2] = {0};
uint8_t id[IMSI_LEN + 1] = {0};

if (get_aes_key(private_key) != 0) {
LE_ERROR("%s\n", "get aes key error");
}
// fetch Device_ID (IMSI, len <= 15)
if (get_device_id((char *)id) != 0) {
LE_ERROR("%s\n", "get device id error");
}

ciphertext_len = ta_encrypt(raw_msg, strlen((char *)raw_msg), ciphertext, 1024, iv, private_key, id);
if (ciphertext_len == 0) {
LE_ERROR("%s\n", "ta_encrypt msg error");
}
serialize_msg(iv, ciphertext_len, ciphertext, msg, &msg_len);
bytes_to_trytes((const unsigned char *)msg, msg_len, tryte_msg);

// Init http session. verify: check the server CA cert.
char msg_body[1024];
gen_tryte_msg(tryte_msg, addr, msg_body);
if (send_https_msg(HOST, PORT, API, msg_body, 1024) != HTTP_OK) {
LE_ERROR("Response from ta server failed\n");
}

strncpy(addr, next_addr, ADDR_LEN);
free(response);
response = NULL;
printf(
"========================Finishing Sending "
"Transaction========================\n\n");
}
if (tcflush(fd, TCIOFLUSH) != 0) {
perror("tcflush error");
}
}
}
2 changes: 2 additions & 0 deletions endpointComponent/resolv.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nameserver 8.8.8.8
nameserver 8.8.4.4
100 changes: 13 additions & 87 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,94 +1,21 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include "connectivity/conn_http.h"
#include "http_parser.h"
#include "utils/crypto_utils.h"
#include "utils/serializer.h"
#include "utils/tryte_byte_conv.h"
#include "utils/uart_utils.h"

#define HOST "tangle-accel.puyuma.org"
#define PORT "443"
#define API "transaction/"
#define SSL_SEED "nonce"
#define REQ_BODY \
"{\"value\": 0, \"tag\": \"POWEREDBYTANGLEACCELERATOR9\", \"message\": " \
"\"%s\", \"address\":\"%s\"}\r\n\r\n"
#define ADDRESS \
"POWEREDBYTANGLEACCELERATOR999999999999999999999999999999999999999999999999" \
"999999A"
#define ADDR_LEN 81
#include <termios.h>
#include <unistd.h>
#include "conn_http.h"
#include "crypto_utils.h"
#include "serializer.h"
#include "tryte_byte_conv.h"
#include "uart_utils.h"
#include "utils.h"

#ifndef DEBUG
#define MSG "%s:%s"
#else
#define MSG "%s:THISISMSG9THISISMSG9THISISMSG"
#define ADDR_LOG_PATH "addr_log.log"
#endif

static char addr_log_template[] = "\n%s\n";

// Test mode only, should not use on deployment
void gen_trytes(uint16_t len, char *out) {
const char tryte_alphabet[] = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ";
uint8_t rand_index;
for (int i = 0; i < len; i++) {
rand_index = rand() % 27;
out[i] = tryte_alphabet[rand_index];
}
}

int send_https_msg(char const *const host, char const *const port, char const *const api, char const *const tryte_msg,
char const *const addr) {
char req_body[1024] = {}, res[4096] = {0};
char *req = NULL;
int ret = 0;
sprintf(req_body, REQ_BODY, tryte_msg, addr);
set_post_request(api, host, atoi(port), req_body, &req);

#ifdef DEBUG
printf("req packet = \n%s", req);
#endif

http_parser_settings settings;
settings.on_body = parser_body_callback;
http_parser *parser = malloc(sizeof(http_parser));

connect_info_t info = {.https = true};
http_open(&info, SSL_SEED, host, port);
http_send_request(&info, req);
http_read_response(&info, res, sizeof(res) / sizeof(char));
http_close(&info);
http_parser_init(parser, HTTP_RESPONSE);
http_parser_execute(parser, &settings, res, strlen(res));
printf("HTTP Response: %s\n", http_res_body);
free(http_res_body);
http_res_body = NULL;

ret = parser->status_code;
free(parser);
free(req);
return ret;
}

int log_address(char *next_addr) {
FILE *fp;
char addr_log[ADDR_LEN + 3];
// Append the next address to the address log file
fp = fopen(ADDR_LOG_PATH, "a");
if (!fp) {
perror("open addr_log.log failed:");
fclose(fp);
return -1;
}
snprintf(addr_log, 83, addr_log_template, next_addr);
fputs(addr_log, fp);
fclose(fp);
return 0;
}

int main(int argc, char *argv[]) {
uint8_t ciphertext[1024] = {0}, iv[16] = {0}, raw_msg[1000] = {0};
uint32_t raw_msg_len = 1 + ADDR_LEN + 20, ciphertext_len = 0, msg_len;
Expand Down Expand Up @@ -119,7 +46,6 @@ int main(int argc, char *argv[]) {
tv.tv_sec = 0;
tv.tv_usec = 500;
while (true) {
// TODO add select
FD_ZERO(&rset);
FD_SET(fd, &rset);
select(fd + 1, &rset, NULL, NULL, &tv);
Expand All @@ -130,7 +56,6 @@ int main(int argc, char *argv[]) {
tm_info = localtime(&timer);
strftime(time_str, 26, "%Y-%m-%d %H:%M:%S", tm_info);
printf("%s\n", time_str);
// TODO: not good
gen_trytes(ADDR_LEN, next_addr);

#ifndef DEBUG
Expand All @@ -155,7 +80,7 @@ int main(int argc, char *argv[]) {
return -1;
}
// fetch Device_ID (IMSI, len <= 15)
if (get_device_id(id) != 0) {
if (get_device_id((char *)id) != 0) {
fprintf(stderr, "%s\n", "get device id error");
return -1;
}
Expand All @@ -182,8 +107,10 @@ int main(int argc, char *argv[]) {
bytes_to_trytes((const unsigned char *)msg, msg_len, tryte_msg);

// Init http session. verify: check the server CA cert.
if (send_https_msg(HOST, PORT, API, tryte_msg, addr) != HTTP_OK) {
fprintf(stderr, "Response from ta server failed");
char msg_body[1024];
gen_tryte_msg(tryte_msg, addr, msg_body);
if (send_https_msg(HOST, PORT, API, msg_body, 1024) != HTTP_OK) {
fprintf(stderr, "Response from ta server failed\n");
}

strncpy(addr, next_addr, ADDR_LEN);
Expand All @@ -199,6 +126,5 @@ int main(int argc, char *argv[]) {
}
}
#endif

return 0;
}
Loading

0 comments on commit a862354

Please sign in to comment.