Skip to content

Commit

Permalink
[WIP]feat: Consolidate Legato AF intergration
Browse files Browse the repository at this point in the history
Add legato build target into gitignore.
Closes DLTcollab#20
Closes DLTcollab#29
  • Loading branch information
splasky committed Mar 27, 2020
1 parent e6173c1 commit ecd52c1
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 28 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: 31 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,44 +35,65 @@ UTILS_OBJS = $(UTILS_PATH)/crypto_utils.o $(UTILS_PATH)/serializer.o $(UTILS_PAT
# 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

CFLAGS += -DTA_HOST=tangle-accel.puyuma.org -DTA_PORT=434 -DTA_API=transaction/

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

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
42 changes: 42 additions & 0 deletions legato/Component.cdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
sources:
{
endpoint.c
}

cflags:
{
-g -O0
-DDEBUG=1
-DTA_HOST=tangle-accel.puyuma.org -DTA_PORT=434 -DTA_API=transaction/
-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
}
}
152 changes: 152 additions & 0 deletions legato/endpoint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#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.h"

#define MSG "%s:%s"

#define SSL_SEED "nonce"
#define ADDRESS \
"POWEREDBYTANGLEACCELERATOR999999999999999999999999999999999999999999999999" \
"999999A"
#define ADDR_LEN 81
#define ADDR_LOG_PATH "ta-endpoint.log"

const char *HOST = STRINGIZE(TA_HOST);
const char *PORT = STRINGIZE(TA_PORT);
const char *API = STRINGIZE(TA_API);

/**
* 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 (write_address(ADDR_LOG_PATH, next_addr, ADDR_LEN) != 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, SSL_SEED) != 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 legato/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
32 changes: 17 additions & 15 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@
#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";
#define SSL_SEED "nonce"
#define ADDRESS \
"POWEREDBYTANGLEACCELERATOR999999999999999999999999999999999999999999999999" \
"999999A"
#define ADDR_LEN 81
#define ADDR_LOG_PATH "ta-endpoint.log"

int log_address(uint8_t *next_addr) {
FILE *fp;
Expand Down Expand Up @@ -67,8 +71,8 @@ int main(int argc, char *argv[]) {
return -1;
}
#else
if (log_address(next_addr)) {
fprintf(stderr, "log address failed");
if (write_address(ADDR_LOG_PATH, next_addr, ADDR_LEN) != 0) {
fprintf(stderr, "log address failed\n");
return -1;
}
#endif
Expand All @@ -84,7 +88,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 @@ -101,24 +104,24 @@ int main(int argc, char *argv[]) {
response = uart_read(fd);
#else
response = strdup("This is a test");
printf("next_addr = %s \n", next_addr);
log_address(next_addr);
printf("next_addr = %.*s \n", ADDR_LEN, next_addr);
write_address(ADDR_LOG_PATH, next_addr, ADDR_LEN);
#endif
// real transmitted data
#ifndef DEBUG
snprintf((char *)raw_msg, raw_msg_len, MSG, next_addr, response);
snprintf(raw_msg, raw_msg_len, MSG, next_addr, response);
#else
snprintf((char *)raw_msg, raw_msg_len, MSG, next_addr);
snprintf(raw_msg, raw_msg_len, MSG, next_addr);
#endif
printf("Raw Message: %s\n", raw_msg);
uint8_t private_key[AES_BLOCK_SIZE * 2] = {0};
uint8_t id[IMSI_LEN + 1] = {0};
uint8_t private_key[AES_KEY_SIZE] = {0};
uint8_t id[IMSI_LEN] = {0};
#ifndef DEBUG
if (get_aes_key(private_key) != 0) {
fprintf(stderr, "%s\n", "get aes key error");
return -1;
}
// fetch Device_ID (IMSI, len <= 15)
// fetch Device_ID (IMSI, len <= 16)
if (get_device_id(id) != 0) {
fprintf(stderr, "%s\n", "get device id error");
return -1;
Expand All @@ -137,9 +140,9 @@ int main(int argc, char *argv[]) {
memcpy(private_key, key, 16);
memcpy(iv, iv_global, 16);
#endif
ciphertext_len = encrypt(raw_msg, strlen((char *)raw_msg), ciphertext, 1024, iv, private_key, id);
ciphertext_len = ta_encrypt(raw_msg, strlen(raw_msg), ciphertext, 1024, iv, private_key, id);
if (ciphertext_len == 0) {
fprintf(stderr, "%s\n", "encrypt msg error");
fprintf(stderr, "%s\n", "ta_encrypt msg error");
return -1;
}
serialize_msg(iv, ciphertext_len, ciphertext, msg, &msg_len);
Expand All @@ -161,6 +164,5 @@ int main(int argc, char *argv[]) {
}
}
#endif

return 0;
}
12 changes: 12 additions & 0 deletions ta-endpoint.adef
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
executables:
{
taEndpoint = (legato)
}

processes:
{
run:
{
(taEndpoint)
}
}
Loading

0 comments on commit ecd52c1

Please sign in to comment.