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

Commit 3f83bd2

Browse files
author
HYChang
committed
feat(endpoint): Implement wp77xx platform
This commit Implements the wp77xx platform for endpoint. The AirPrime WP7702 LPWA module is the first verified device. See https://www.sierrawireless.com/products-and-solutions/embedded-solutions/products/wp7702/ for more information. The resolv.conf is removed. Change to use the system's default /etc/resolv.conf. Close #679
1 parent 5b3fe83 commit 3f83bd2

File tree

14 files changed

+386
-48
lines changed

14 files changed

+386
-48
lines changed

Makefile

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ MOSQUITTO_LIB := $(MOSQUITTO_DIR)/lib/libmosquitto.so.1
55
PEM_DIR = pem
66
# Default pem file. See pem/README.md for more information
77
PEM := $(PEM_DIR)/cert.pem
8-
NAMESERVER := 8.8.8.8
9-
RESOLV_CONF_DIR := endpoint/endpointComp
108
OUTPUT_BASE_DIR := output_base
119
# Endpoint build target. The default intends to the platform of your development system.
1210
TARGET := simulator
@@ -16,32 +14,41 @@ TESTS := false
1614
# The endpoint uses HTTP connection to transmit encrypted data by default.
1715
# See "HTTPS Connection Support" in docs/endpoint.md for more information.
1816
ENFORCE_EP_HTTPS := false
19-
# The CFLAGS to pass during endpoint app build
20-
ENDPOINT_CFLAGS :=
17+
# The flags that will be preprocessed by mkapp program, part of Legato Application Framework.
18+
# Eventually, the processed flags are passed as compiler-time options.
19+
LEGATO_FLAGS :=
2120
DEPS += $(DCURL_LIB)
2221

2322
# Determine to enable HTTPS connection
2423
ifeq ($(ENFORCE_EP_HTTPS), true)
25-
ENDPOINT_CFLAGS += -C -DENDPOINT_HTTPS
24+
LEGATO_FLAGS += -DENDPOINT_HTTPS
2625
endif
2726

2827
# Determine to build test suite
2928
ifeq ($(TESTS), true)
30-
ENDPOINT_CFLAGS += -C -DENABLE_ENDPOINT_TEST
29+
LEGATO_FLAGS += -DENABLE_ENDPOINT_TEST
3130
endif
3231

32+
# The tangle-acclerator host for endpoint to connect
3333
ifdef EP_TA_HOST
34-
ENDPOINT_CFLAGS += -C -DEP_TA_HOST=${EP_TA_HOST}
34+
LEGATO_FLAGS += -DEP_TA_HOST=${EP_TA_HOST}
3535
endif
36-
36+
# The tangle-acclerator port for endpoint to connect
3737
ifdef EP_TA_PORT
38-
ENDPOINT_CFLAGS += -C -DEP_TA_PORT=${EP_TA_PORT}
38+
LEGATO_FLAGS += -DEP_TA_PORT=${EP_TA_PORT}
3939
endif
40-
40+
# The ssl seed for endpoint (optional)
4141
ifdef EP_SSL_SEED
42-
ENDPOINT_CFLAGS += -C -DEP_SSL_SEED=${EP_SSL_SEED}
42+
LEGATO_FLAGS += -DEP_SSL_SEED=${EP_SSL_SEED}
4343
endif
4444

45+
# Pass target into endpoint build process
46+
LEGATO_FLAGS += -DTARGET=$(TARGET)
47+
48+
# Prepend the "-C" flag at the beginging for passing cflags into mkapp
49+
LEGATO_FLAGS := $(foreach flags, $(LEGATO_FLAGS), -C $(flags))
50+
51+
# Include the build command from the specific target
4552
include endpoint/platform/$(TARGET)/build.mk
4653

4754
all: $(DEPS) cert
@@ -63,8 +70,6 @@ $(MOSQUITTO_LIB): $(MOSQUITTO_DIR)
6370

6471
# Build endpoint Legato app
6572
legato: cert
66-
# Generate resolv.conf
67-
echo "nameserver $(NAMESERVER)" > $(RESOLV_CONF_DIR)/resolv.conf
6873
# Fetch the required external source code
6974
# FIXME: Use 'fetch' instead of 'build' to avoid extra building actions.
7075
# The 'build' option is for getting the header file like 'mam/mam/mam_endpoint_t_set.h',

common/ta_errors.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,22 @@ const char* ta_error_to_string(status_t err) {
180180
return "Failed to finalize the device";
181181
case SC_ENDPOINT_UART:
182182
return "UART error occurred in device component";
183+
case SC_ENDPOINT_UART_SET_ATTR:
184+
return "UART error occurred when setting UART attribute";
183185
case SC_ENDPOINT_SEC_FAULT:
184186
return "Error occurred inside secure storage";
185187
case SC_ENDPOINT_SEC_ITEM_NOT_FOUND:
186188
return "Item not found inside secure storage";
187189
case SC_ENDPOINT_SEC_UNAVAILABLE:
188190
return "Secure storage service is unavailable";
189191
case SC_ENDPOINT_SEND_TRANSFER:
190-
return "Error occurred when the sending transfer message";
192+
return "Error occurred when sending the transfer message";
193+
case SC_ENDPOINT_GET_KEY_ERROR:
194+
return "Error occurred when get private key from endpoint device";
195+
case SC_ENDPOINT_GET_DEVICE_ID_ERROR:
196+
return "Error occurred when get device id from endpoint device";
197+
case SC_ENDPOINT_DNS_RESOLVE_ERROR:
198+
return "Error occurred when resolving the domain name";
191199

192200
default:
193201
return "Unknown error.";

common/ta_errors.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,22 @@ typedef enum {
237237
/**< Failed to finalize the device */
238238
SC_ENDPOINT_UART = 0x03 | SC_MODULE_ENDPOINT | SC_SEVERITY_FATAL,
239239
/**< UART error occurred in device component */
240-
SC_ENDPOINT_SEC_FAULT = 0x04 | SC_MODULE_ENDPOINT | SC_SEVERITY_MINOR,
240+
SC_ENDPOINT_UART_SET_ATTR = 0x04 | SC_MODULE_ENDPOINT | SC_SEVERITY_FATAL,
241+
/**< Error occurred when setting UART attribute */
242+
SC_ENDPOINT_SEC_FAULT = 0x05 | SC_MODULE_ENDPOINT | SC_SEVERITY_MINOR,
241243
/**< Error occurred inside secure storage */
242-
SC_ENDPOINT_SEC_ITEM_NOT_FOUND = 0x05 | SC_MODULE_ENDPOINT | SC_SEVERITY_MINOR,
244+
SC_ENDPOINT_SEC_ITEM_NOT_FOUND = 0x06 | SC_MODULE_ENDPOINT | SC_SEVERITY_MINOR,
243245
/**< Item not found inside secure storage */
244-
SC_ENDPOINT_SEC_UNAVAILABLE = 0x06 | SC_MODULE_ENDPOINT | SC_SEVERITY_MINOR,
246+
SC_ENDPOINT_SEC_UNAVAILABLE = 0x07 | SC_MODULE_ENDPOINT | SC_SEVERITY_MINOR,
245247
/**< Secure storage service is unavailable */
246-
SC_ENDPOINT_SEND_TRANSFER = 0x07 | SC_MODULE_ENDPOINT | SC_SEVERITY_FATAL,
247-
/**< Error occurred when the sending transfer message */
248+
SC_ENDPOINT_SEND_TRANSFER = 0x08 | SC_MODULE_ENDPOINT | SC_SEVERITY_FATAL,
249+
/**< Error occurred when sending transfer message */
250+
SC_ENDPOINT_GET_KEY_ERROR = 0x09 | SC_MODULE_ENDPOINT | SC_SEVERITY_FATAL,
251+
/**< Failed to get the private key */
252+
SC_ENDPOINT_GET_DEVICE_ID_ERROR = 0x0A | SC_MODULE_ENDPOINT | SC_SEVERITY_FATAL,
253+
/**< Failed to get the device id */
254+
SC_ENDPOINT_DNS_RESOLVE_ERROR = 0x0B | SC_MODULE_ENDPOINT | SC_SEVERITY_FATAL,
255+
/**< Failed to resolve the domain name address */
248256

249257
} status_t;
250258

docs/endpoint.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@ $ leaf setup legato-stable -p swi-wp77_3.0.0
5252

5353
Finally, make the wp77xx endpoint target. Be careful with the directory of tangle-accelerator. It should be located within the workspace directory.
5454

55+
The host-port pair and SSL seed can be set at build-time and run-time. The run-time command line option '--host', '--port' and '--ssl-seed" are added.
56+
57+
For setting `host`, `port` and `ssl seed` during compile-time. Add `EP_TA_HOST=xxx.xxxx.xxx`, `EP_TA_HOST=xxxx` and `EP_SSL_SEED=xxxxxxxxx` option. If you doesn't change the host and port, the default host will be set to `localhost` and default port will be set to `8000`.
58+
5559
```shell
5660
$ git clone https://github.com/DLTcollab/tangle-accelerator.git
5761
$ cd tangle-accelerator
58-
$ make TARGET=wp77xx legato # build endpoint as wp77xx target
59-
$ make TESTS=true TARGET=wp77xx legato # build endpoint as wp77xx target in test mode
62+
$ make TARGET=wp77xx EP_TA_HOST=node.deviceproof.org EP_TA_PORT=5566 legato # build endpoint as wp77xx target, and set the connected host to "node.deviceproof.org" with port 5566
63+
$ make TESTS=true TARGET=wp77xx EP_TA_HOST=node.deviceproof.org EP_TA_PORT=5566 legato # build endpoint as wp77xx target in test mode
6064
```
6165

6266
### How to build endpoint application for native target

endpoint/endpoint.adef

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ processes:
1111
}
1212
}
1313

14+
bindings:
15+
{
16+
endpoint.endpointComp.le_secStore -> secStore.le_secStore
17+
endpoint.endpointComp.le_sim -> modemService.le_sim
18+
}
19+
1420
start: manual

endpoint/endpointComp/Component.cdef

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
sources:
22
{
33
${CURDIR}/../endpoint_core.c
4+
${CURDIR}/../hal/device.c
5+
${CURDIR}/../platform/wp77xx/impl.c
46

57
${CURDIR}/../../output_base/external/org_iota_common/utils/logger_helper.c
68

@@ -85,19 +87,16 @@ cflags:
8587
-I${CURDIR}/../../output_base/external/mbedtls_2_16_6/include
8688
}
8789

88-
bundles:
89-
{
90-
// List of files copied from the build host into the App for runtime usage
91-
file:
92-
{
93-
resolv.conf /etc/
94-
}
95-
}
96-
9790
requires:
9891
{
9992
device:
10093
{
10194
[rw] /dev/ttyHS0 /dev/ttyHS0
10295
}
96+
97+
api:
98+
{
99+
le_secStore.api
100+
modemServices/le_sim.api
101+
}
103102
}

endpoint/endpointComp/endpoint.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
*/
88

99
#include "endpoint.h"
10+
#include "hal/device.h"
1011

1112
#include "common/ta_errors.h"
1213
#include "endpoint/endpoint_core.h"
1314
#include "le_test.h"
1415
#include "legato.h"
1516
#include "utils/cipher.h"
1617

18+
#include "le_log.h"
19+
1720
#define TEST_VALUE 0
1821
#define TEST_MESSAGE "THISISMSG9THISISMSG9THISISMSG"
1922
#define TEST_MESSAGE_FMT "ascii"
@@ -24,11 +27,7 @@
2427
#define TEST_NEXT_ADDRESS \
2528
"POWEREDBYTANGLEACCELERATOR999999999999999999999999999999999999999999999999" \
2629
"999999B"
27-
#define TEST_DEVICE_ID "470010171566423"
2830

29-
const uint8_t test_private_key[AES_CBC_KEY_SIZE] = {82, 142, 184, 64, 74, 105, 126, 65, 154, 116, 14,
30-
193, 208, 41, 8, 115, 158, 252, 228, 160, 79, 5,
31-
167, 185, 13, 159, 135, 113, 49, 209, 58, 68};
3231
const uint8_t test_iv[AES_IV_SIZE] = {164, 3, 98, 193, 52, 162, 107, 252, 184, 42, 74, 225, 157, 26, 88, 72};
3332

3433
static void print_help(void) {
@@ -104,7 +103,10 @@ COMPONENT_INIT {
104103
const char* tag = TEST_TAG;
105104
const char* address = TEST_ADDRESS;
106105
const char* next_address = TEST_NEXT_ADDRESS;
107-
const char* device_id = TEST_DEVICE_ID;
106+
107+
char device_id[16] = {0};
108+
const char* device_id_ptr = device_id;
109+
108110
uint8_t private_key[AES_CBC_KEY_SIZE] = {0};
109111
uint8_t iv[AES_IV_SIZE] = {0};
110112

@@ -120,20 +122,24 @@ COMPONENT_INIT {
120122
le_arg_SetFlagCallback(print_help, "h", "help");
121123
le_arg_Scan();
122124

123-
memcpy(private_key, test_private_key, AES_CBC_KEY_SIZE);
124125
memcpy(iv, test_iv, AES_IV_SIZE);
125126
srand(time(NULL));
126127

128+
device_t* device = ta_device(STRINGIZE(TARGET));
129+
device->op->get_key(private_key);
130+
device->op->get_device_id(device_id);
131+
127132
#ifdef ENABLE_ENDPOINT_TEST
128133
LE_TEST_INIT;
129134
LE_TEST_INFO("=== ENDPOINT TEST BEGIN ===");
130135
LE_TEST(SC_OK == send_transaction_information(host, port, ssl_seed, value, message, message_fmt, tag, address,
131-
next_address, private_key, device_id, iv));
136+
next_address, private_key, device_id_ptr, iv));
132137
LE_TEST_EXIT;
133138
#else
134139
while (true) {
135-
send_transaction_information(host, port, ssl_seed, value, message, message_fmt, tag, address, next_address,
136-
private_key, device_id, iv);
140+
status_t ret = send_transaction_information(host, port, ssl_seed, value, message, message_fmt, tag, address,
141+
next_address, private_key, device_id_ptr, iv);
142+
LE_INFO("Send transaction information return: %d", ret);
137143
sleep(10);
138144
}
139145
#endif

endpoint/endpoint_core.c

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include "utils/text_serializer.h"
2020
#include "utils/tryte_byte_conv.h"
2121

22+
#include <netdb.h>
23+
#include <sys/socket.h>
24+
#include <sys/types.h>
25+
2226
// FIXME: Same as STR() inside tests/test_defined.h
2327
#define STR_HELPER(num) #num
2428
#define STR(num) STR_HELPER(num)
@@ -69,8 +73,14 @@ status_t send_transaction_information(const char* host, const char* port, const
6973
char req_body[MAX_MSG_LEN] = {0};
7074
uint8_t ciphertext[MAX_MSG_LEN] = {0};
7175
uint8_t raw_msg[MAX_MSG_LEN] = {0};
76+
7277
const char* ta_host = host ? host : STR(EP_TA_HOST);
7378
const char* ta_port = port ? port : STR(EP_TA_PORT);
79+
char ipv4[16];
80+
if (resolve_ip_address(ta_host, ipv4) != SC_OK) {
81+
return SC_ENDPOINT_DNS_RESOLVE_ERROR;
82+
}
83+
7484
const char* seed = ssl_seed ? ssl_seed : STR(EP_SSL_SEED);
7585

7686
int ret = snprintf((char*)raw_msg, MAX_MSG_LEN, "%s:%s", next_address, message);
@@ -112,10 +122,46 @@ status_t send_transaction_information(const char* host, const char* port, const
112122
return SC_ENDPOINT_SEND_TRANSFER;
113123
}
114124

115-
if (send_https_msg(ta_host, ta_port, SEND_TRANSACTION_API, req_body, seed) != SC_OK) {
125+
if (send_https_msg(ipv4, ta_port, SEND_TRANSACTION_API, req_body, seed) != SC_OK) {
116126
ta_log_error("http message sending error.\n");
117127
return SC_ENDPOINT_SEND_TRANSFER;
118128
}
119129

120130
return SC_OK;
121131
}
132+
133+
status_t resolve_ip_address(const char* host, char result[16]) {
134+
struct addrinfo hints;
135+
struct addrinfo* res;
136+
137+
/* Obtain address(es) matching host */
138+
memset(result, 0, 16);
139+
memset(&hints, 0, sizeof(struct addrinfo));
140+
hints.ai_family = AF_INET; /* Allow IPV4 format */
141+
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
142+
143+
int ret = getaddrinfo(host, NULL, &hints, &res);
144+
if (ret != 0) {
145+
ta_log_error("Getaddrinfo returned: %s\n", gai_strerror(ret));
146+
return SC_ENDPOINT_DNS_RESOLVE_ERROR;
147+
}
148+
149+
if (res == NULL) { /* No address succeeded */
150+
ta_log_error("Could not resolve host: %s\n", host);
151+
return SC_ENDPOINT_DNS_RESOLVE_ERROR;
152+
}
153+
154+
for (struct addrinfo* re = res; res != NULL; re = re->ai_next) {
155+
char host_buf[1024];
156+
int ret = getnameinfo(re->ai_addr, re->ai_addrlen, host_buf, sizeof(host_buf), NULL, 0, NI_NUMERICHOST);
157+
if (ret == 0) {
158+
snprintf(result, 16, "%s", host_buf);
159+
break;
160+
} else {
161+
ta_log_error("Getnameinfo returned: %s\n", gai_strerror(ret));
162+
}
163+
}
164+
freeaddrinfo(res); /* No longer needed */
165+
166+
return SC_OK;
167+
}

endpoint/endpoint_core.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,13 @@ status_t send_transaction_information(const char* host, const char* port, const
4949
const char* message, const char* message_fmt, const char* tag,
5050
const char* address, const char* next_address, const uint8_t* private_key,
5151
const char* device_id, uint8_t* iv);
52+
/**
53+
* @brief Resolve the server address name
54+
*
55+
* @param[in] host The domain name of the host
56+
* @param[out] result The buffer to store the IPV4 address output
57+
* @return #status_t
58+
*/
59+
status_t resolve_ip_address(const char* host, char result[16]);
5260

5361
#endif // ENDPOINT_CORE_H

endpoint/hal/device.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ extern "C" {
2727
typedef struct device_type device_t;
2828

2929
struct device_operations {
30-
status_t (*init)(void); /**< initialize device */
31-
void (*fini)(void); /**< destructor of device */
32-
status_t (*get_key)(uint8_t *); /**< get device private key */
33-
status_t (*get_device_id)(uint8_t *); /**< get device id */
30+
status_t (*init)(void); /**< initialize device */
31+
void (*fini)(void); /**< destructor of device */
32+
status_t (*get_key)(uint8_t *); /**< get device private key */
33+
status_t (*get_device_id)(char *); /**< get device id */
3434
};
3535

3636
struct uart_operations {
37-
status_t (*init)(const uint8_t *device); /**< initialize uart */
37+
status_t (*init)(const char *device); /**< initialize uart */
3838
void (*write)(const int fd, const char *cmd); /**< write command to uart */
3939
char *(*read)(const int fd); /**< read from uart */
4040
void (*clean)(const int fd); /**< flush uart buffer */
@@ -51,7 +51,6 @@ struct secure_store_operations {
5151
*
5252
* @return
5353
* - #SC_OK on success
54-
* - #RET_NO_MEMORY on no memory error
5554
* - #SC_ENDPOINT_SEC_UNAVAILABLE on unavailable secure storage
5655
* - #SC_ENDPOINT_SEC_FAULT on some other error
5756
*/

0 commit comments

Comments
 (0)