Skip to content

Commit

Permalink
feat: Consolidate Legato AF intergration
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_legato_app into Makefile.

Add ta_legato_app build target and cleanup target into Makefile.

Remove redefined macro inside `conn_http.h`.

Add new directory endpointComponent and files for legato 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

Closes DLTcollab#29
  • Loading branch information
splasky committed Mar 4, 2020
1 parent 3beb007 commit 211afe4
Show file tree
Hide file tree
Showing 18 changed files with 466 additions and 172 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
41 changes: 30 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,49 +29,68 @@ 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)/url_msg.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)
TA_LIB = libta_endpoint.so

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

all: pre-build ta_client mbedtls_make

## Build ta-client
ta_client: mbedtls_make $(OBJS)
@echo Linking: $@ ....
$(CC) -o $@ $(OBJS) $(LIBS)

mbedtls_make:
@for dir in $(MBEDTLS_PATH); do \
$(MAKE) -C $$dir ; \
if [ $$? != 0 ]; then exit 1; fi; \
done
$(MAKE) -C $(MBEDTLS_PATH) lib

$(TA_LIB): mbedtls_make $(UTILS_OBJS) $(CONNECTIVITY_OBJS) http_parser.o
$(CC) -shared -o $@ $(UTILS_OBJS) $(CONNECTIVITY_OBJS) http_parser.o $(LIBS)

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
$(CC) -c $(CFLAGS) $(INCLUDES) -o $@ $<
main.o: main.c $(UTILS_OBJS)
@echo Compiling: $< ....
$(CC) -c $(CFLAGS) $(INCLUDES) -o $@ $<
$(UTILS_OBJS):
$(MAKE) -C $(UTILS_PATH)

## Build tests
test: $(TEST_PATH) $(UTILS_OBJS)
$(MAKE) -C $(TEST_PATH)

clean: clean_client clean_third_party clean_test
## Build ta-endpoint legato app
legato:
make clean && make ARM=y DEBUG=n $(TA_LIB) -j$(nproc) && mkapp -v -t wp77xx ta-endpoint.adef

## Prints help for targets with comments
help:
@cat $(MAKEFILE_LIST)| \
awk '/^##.*$$/{l1=$$0;getline;l2=(l1 "##" $$0); print l2 $$0}' | \
awk -F"##" '{split($$3,t,":");printf "\033[36m%-30s\033[0m %s\n",t[1],$$2}'

## Clean the directory
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 $(TA_LIB)

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
}
}
143 changes: 143 additions & 0 deletions endpointComponent/endpoint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#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/url_msg.h"

#define MSG "%s:%s"
const char *PORT = TA_PORT;
const char *HOST = TA_HOST;

/**
* Prints the on-line help and exits. This function is called when "-h" or "--help" appears on
* the command-line or when the help command is invoked.
**/
static void PrintHelp(void) {
puts(
"NAME\n"
" ta-enpoint - Transcation information with tangle accerlator.\n"
"\n"
"SYNOPSIS\n"
" ta-endpoint [OPTION]...\n"
" ta-endpoint -h\n"
" ta-endpoint --help\n"
"\n"
"COMMANDS\n"
" help\n"
" Print a help message and exit. Ignore all other arguments.\n"
"\n"
"OPTIONS\n"
" -h\n"
" --host=<host ip or name>\n"
" Setting host ip or name for connect ta accerlator.\n"
" If not setting, would connect default host.\n"
"\n"
" -p N\n"
" --port=<port>\n"
" Setting host port for connect ta accerlator\n"
" If not setting, would connect default port\n");

exit(EXIT_SUCCESS);
}

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

srand(time(NULL));

// Register a function to be called if -h or --help appears on the command-line.
le_arg_SetFlagCallback(PrintHelp, NULL, "help");
// Set host to the value HOST given by "-h <string>" or "--host=<string>"
le_arg_SetStringVar(&HOST, "h", "host");
// Set port to the value PORT given by "-p N" or "--port=N"
le_arg_SetStringVar(&PORT, "p", "port");

// Perform command-line argument processing.
le_arg_Scan();

// 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_rand_trytes(ADDR_LEN, next_addr);

response = uart_read(fd);
// real transmitted data
snprintf(raw_msg, raw_msg_len, MSG, next_addr, response);
LE_DEBUG("Raw_msg:%s\n", raw_msg);
uint8_t private_key[AES_KEY_SIZE] = {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(id) != 0) {
LE_ERROR("%s\n", "get device id error");
}

ciphertext_len = ta_encrypt(raw_msg, strlen(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");
}

memcpy(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
Loading

0 comments on commit 211afe4

Please sign in to comment.