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

Commit 1541bfd

Browse files
HYChangHYChang
authored andcommitted
feat(endpoint): Implement the simulator platform
This commit implements the simulator platform for endpoint. The simulator is an instance of endpoint HAL, dedicated to control-flow and protocol verification. The simulator will be used for testing the endpoint functionality and verified the endpoint behavior. The process of adding a new platform has been simplified. Only need to provide "impl.c" and "build.mk" inside new platform. See endpoint-hal.md for more information. The section "hal/Makefile" inside endpoint-hal.md has been removed. The hal/Makefile will not be used anymore. Close #640
1 parent 35fa8e8 commit 1541bfd

File tree

11 files changed

+249
-69
lines changed

11 files changed

+249
-69
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ LEGATO_FLAGS := $(foreach flags, $(LEGATO_FLAGS), -C $(flags))
5050

5151
# Include the build command from the specific target
5252
include endpoint/platform/$(TARGET)/build.mk
53+
export TARGET
5354

5455
all: $(DEPS) cert
5556

docs/endpoint-hal.md

Lines changed: 46 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@ Hardware Abstract Layer(HAL) defines a standard interface for hardware vendors t
44

55
## How to implement new device
66

7-
Create a directory for the new device under `devices`.
8-
7+
Create a directory for the new device under `platform`.
98
```bash
10-
$ mkdir -p devices/mydevice
9+
$ mkdir -p platform/mydevice
1110
```
12-
13-
* Create `impl.c` and `Makefile` under the new device directory.
11+
* Create `impl.c` and `build.mk` under the new device directory.
1412
* Include `device.h` into the new device header or the new device source file.
1513

14+
For `build.mk` you should define the `platform-build-command`. Here are the example from the WP7702 module:
15+
16+
```makefile
17+
platform-build-command = \
18+
cd endpoint && leaf shell -c "mkapp -v -t wp77xx $(LEGATO_FLAGS) endpoint.adef"
19+
```
20+
1621
Here are some operations needed to be implemented for new device:
1722

1823
* device_operations
@@ -37,68 +42,50 @@ Here are the functions needed to be registered/unregistered inside `impl.c`:
3742
* unregister_device : unregistered device
3843
* DECLARE_DEVICE : this must be declared inside `impl.c`
3944

40-
Add the new device into `hal/Makefile`:
41-
42-
* Append the device object to DEVICE_OBJS
43-
44-
* Add the new device build target(mydevice.o)
45-
46-
```makefile
47-
DEVICE_OBJS = wp7702.o emulator.o device.o mydevice.o
48-
export DEVICE_OBJS
49-
50-
all: $(DEVICE_OBJS)
51-
52-
mydevice.o: device.o
53-
$(MAKE) -C ../devices/mydevice
54-
```
55-
56-
Implement a new device which is created under `devices` directory, and edit the Makefile. The example device is named as `mydevice`:
57-
58-
```makefile
59-
all: mydevice.o
60-
mydevice.o: impl.c
61-
$(CC) $(CFLAGS) $(INCLUDES) -c $^ -o $@
62-
```
63-
64-
`$(CC)`,`$(CFLAGS)` and `$(INCLUDES)` are specified by build system. `CC` sets the default compiler for the project. `CFLAGS` are the default flags that would be passed to default compiler during compiling time. `INCLUDES` flag includes headers inside sub-projects and third-party libraries. You can also modify these flags inside your device's Makefile.
65-
6645
impl.c
6746

6847
```c
69-
#include "device.h"
70-
71-
static inline void register_emulator(void);
72-
static inline void unregister_emulator(void);
73-
74-
static struct device_operations emulator_ops = {.init = &emulator_init,
75-
.fini = &emulator_release,
76-
.get_key = &emulator_get_key,
77-
.get_device_id = &emulator_get_device_id};
78-
79-
static struct uart_operations emulator_uart = {
80-
.init = &uart_init, .write = &uart_write, .read = &uart_read, .clean = &uart_clean};
81-
82-
static struct device_type emulator_device_type = {
83-
.name = "emulator", .op = &emulator_ops, .uart = &emulator_uart, .sec_ops = &emulator_sec_ops};
84-
85-
static inline void register_emulator(void) {
86-
int err = register_device(&emulator_device_type);
87-
if (err) LOG_ERROR("register emulator device error:%d", err);
88-
}
48+
#include "endpoint/hal/device.h"
8949

90-
static inline void unregister_emulator(void) {
91-
int err = unregister_device(&emulator_device_type);
92-
if (err) LOG_ERROR("unregister device emulator error:%d", err);
50+
static status_t simulator_init(void) {
51+
status_t err = register_device(&simulator_device_type);
52+
if (err != SC_OK) LE_ERROR("register simulator device error:%d", err);
53+
return err;
9354
}
9455

95-
static int emulator_init(void) {
96-
register_emulator();
97-
return DEVICE_OK;
56+
static void simulator_release(void) {
57+
status_t ret = unregister_device(&simulator_device_type);
58+
LE_INFO("unregister simulator return: %d", ret);
9859
}
9960

100-
static void emulator_release(void) { unregister_emulator(); }
61+
static const struct device_operations simulator_ops = {
62+
.init = simulator_init,
63+
.fini = simulator_release,
64+
.get_key = simulator_get_key,
65+
.get_device_id = simulator_get_device_id,
66+
};
67+
68+
static const struct uart_operations simulator_uart = {
69+
.init = uart_init,
70+
.write = uart_write,
71+
.read = uart_read,
72+
.clean = uart_clean,
73+
};
74+
75+
static const struct secure_store_operations simulator_sec_ops = {
76+
.init = sec_init,
77+
.write = sec_write,
78+
.read = sec_read,
79+
.delete = sec_delete,
80+
};
81+
82+
struct device_type simulator_device_type = {
83+
.name = "simulator",
84+
.op = &simulator_ops,
85+
.uart = &simulator_uart,
86+
.sec_ops = &simulator_sec_ops,
87+
};
10188

10289
// must be declared at the end of impl.c
103-
DECLARE_DEVICE(emulator);
90+
DECLARE_DEVICE(simulator);
10491
```

docs/endpoint-platforms.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Endpoint support platforms
2+
3+
The following platforms are verified platforms of endpoint
4+
5+
* simulator on x86_64 platform : The development platform for testing endpoint.
6+
* [AirPrime WP7702 LPWA Module](https://www.sierrawireless.com/products-and-solutions/embedded-solutions/products/wp7702/) : The product platform for endpoint.

endpoint/endpoint.adef

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

14+
#if ${LEGATO_TARGET} = localhost
15+
#else
1416
bindings:
1517
{
1618
endpoint.endpointComp.le_secStore -> secStore.le_secStore
1719
endpoint.endpointComp.le_sim -> modemService.le_sim
1820
}
21+
#endif
1922

2023
start: manual

endpoint/endpointComp/Component.cdef

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ sources:
22
{
33
${CURDIR}/../endpoint_core.c
44
${CURDIR}/../hal/device.c
5-
${CURDIR}/../platform/wp77xx/impl.c
5+
6+
// include the specific platform
7+
${CURDIR}/../platform/${TARGET}/impl.c
68

79
${CURDIR}/../../output_base/external/org_iota_common/utils/logger_helper.c
810

@@ -87,6 +89,8 @@ cflags:
8789
-I${CURDIR}/../../output_base/external/mbedtls_2_16_6/include
8890
}
8991

92+
#if ${LEGATO_TARGET} = localhost
93+
#else
9094
requires:
9195
{
9296
device:
@@ -100,3 +104,4 @@ requires:
100104
modemServices/le_sim.api
101105
}
102106
}
107+
#endif

endpoint/endpoint_core.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ status_t send_transaction_information(const char* host, const char* port, const
7676

7777
const char* ta_host = host ? host : STR(EP_TA_HOST);
7878
const char* ta_port = port ? port : STR(EP_TA_PORT);
79-
char ipv4[16];
79+
char ipv4[NI_MAXHOST];
8080
if (resolve_ip_address(ta_host, ipv4) != SC_OK) {
8181
return SC_ENDPOINT_DNS_RESOLVE_ERROR;
8282
}
@@ -130,7 +130,7 @@ status_t send_transaction_information(const char* host, const char* port, const
130130
return SC_OK;
131131
}
132132

133-
status_t resolve_ip_address(const char* host, char result[16]) {
133+
status_t resolve_ip_address(const char* host, char* result) {
134134
struct addrinfo hints;
135135
struct addrinfo* res;
136136

@@ -152,10 +152,10 @@ status_t resolve_ip_address(const char* host, char result[16]) {
152152
}
153153

154154
for (struct addrinfo* re = res; res != NULL; re = re->ai_next) {
155-
char host_buf[1024];
155+
char host_buf[NI_MAXHOST];
156156
int ret = getnameinfo(re->ai_addr, re->ai_addrlen, host_buf, sizeof(host_buf), NULL, 0, NI_NUMERICHOST);
157157
if (ret == 0) {
158-
snprintf(result, 16, "%s", host_buf);
158+
snprintf(result, NI_MAXHOST, "%s", host_buf);
159159
break;
160160
} else {
161161
ta_log_error("Getnameinfo returned: %s\n", gai_strerror(ret));

endpoint/endpoint_core.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ status_t send_transaction_information(const char* host, const char* port, const
6060
* @param[out] result The buffer to store the IPV4 address output
6161
* @return #status_t
6262
*/
63-
status_t resolve_ip_address(const char* host, char result[16]);
63+
status_t resolve_ip_address(const char* host, char* result);
6464

6565
#endif // ENDPOINT_CORE_H

endpoint/hal/device.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
* "LICENSE" at the root of this distribution.
77
*/
88

9+
#include "legato.h"
10+
911
#include "device.h"
12+
#include "le_log.h"
1013

1114
static struct device_type *devices;
1215

@@ -20,14 +23,12 @@ static struct device_type **find_device(const char *name, unsigned len) {
2023
device_t *ta_device(const char *type) {
2124
struct device_type **p;
2225
if (devices->next) {
23-
// TODO:Use logger
24-
fprintf(stderr, "No device type registered!");
26+
LE_ERROR("No device type registered!");
2527
return NULL;
2628
}
2729
p = find_device(type, strlen(type));
2830
if (*p) {
29-
// TODO:Use logger
30-
fprintf(stderr, "Device type %s not found", type);
31+
LE_INFO("Device type %s not found", type);
3132
}
3233
return *p;
3334
}

endpoint/platform/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
cc_library(
4+
name = "device",
5+
deps = [
6+
"//endpoint/hal",
7+
"//endpoint/simluator",
8+
],
9+
)

endpoint/platform/simulator/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
cc_library(
4+
name = "simulator",
5+
srcs = ["simulator.c"],
6+
deps = [
7+
"//endpoint/hal",
8+
],
9+
)

0 commit comments

Comments
 (0)