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

Commit

Permalink
feat(endpoint): Implement the simulator platform
Browse files Browse the repository at this point in the history
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
  • Loading branch information
splasky committed Jul 6, 2020
1 parent 35fa8e8 commit 1541bfd
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 69 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ LEGATO_FLAGS := $(foreach flags, $(LEGATO_FLAGS), -C $(flags))

# Include the build command from the specific target
include endpoint/platform/$(TARGET)/build.mk
export TARGET

all: $(DEPS) cert

Expand Down
105 changes: 46 additions & 59 deletions docs/endpoint-hal.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ Hardware Abstract Layer(HAL) defines a standard interface for hardware vendors t

## How to implement new device

Create a directory for the new device under `devices`.

Create a directory for the new device under `platform`.
```bash
$ mkdir -p devices/mydevice
$ mkdir -p platform/mydevice
```

* Create `impl.c` and `Makefile` under the new device directory.
* Create `impl.c` and `build.mk` under the new device directory.
* Include `device.h` into the new device header or the new device source file.

For `build.mk` you should define the `platform-build-command`. Here are the example from the WP7702 module:

```makefile
platform-build-command = \
cd endpoint && leaf shell -c "mkapp -v -t wp77xx $(LEGATO_FLAGS) endpoint.adef"
```

Here are some operations needed to be implemented for new device:

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

Add the new device into `hal/Makefile`:

* Append the device object to DEVICE_OBJS

* Add the new device build target(mydevice.o)

```makefile
DEVICE_OBJS = wp7702.o emulator.o device.o mydevice.o
export DEVICE_OBJS

all: $(DEVICE_OBJS)

mydevice.o: device.o
$(MAKE) -C ../devices/mydevice
```

Implement a new device which is created under `devices` directory, and edit the Makefile. The example device is named as `mydevice`:

```makefile
all: mydevice.o
mydevice.o: impl.c
$(CC) $(CFLAGS) $(INCLUDES) -c $^ -o $@
```

`$(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.

impl.c

```c
#include "device.h"

static inline void register_emulator(void);
static inline void unregister_emulator(void);

static struct device_operations emulator_ops = {.init = &emulator_init,
.fini = &emulator_release,
.get_key = &emulator_get_key,
.get_device_id = &emulator_get_device_id};

static struct uart_operations emulator_uart = {
.init = &uart_init, .write = &uart_write, .read = &uart_read, .clean = &uart_clean};

static struct device_type emulator_device_type = {
.name = "emulator", .op = &emulator_ops, .uart = &emulator_uart, .sec_ops = &emulator_sec_ops};

static inline void register_emulator(void) {
int err = register_device(&emulator_device_type);
if (err) LOG_ERROR("register emulator device error:%d", err);
}
#include "endpoint/hal/device.h"

static inline void unregister_emulator(void) {
int err = unregister_device(&emulator_device_type);
if (err) LOG_ERROR("unregister device emulator error:%d", err);
static status_t simulator_init(void) {
status_t err = register_device(&simulator_device_type);
if (err != SC_OK) LE_ERROR("register simulator device error:%d", err);
return err;
}

static int emulator_init(void) {
register_emulator();
return DEVICE_OK;
static void simulator_release(void) {
status_t ret = unregister_device(&simulator_device_type);
LE_INFO("unregister simulator return: %d", ret);
}

static void emulator_release(void) { unregister_emulator(); }
static const struct device_operations simulator_ops = {
.init = simulator_init,
.fini = simulator_release,
.get_key = simulator_get_key,
.get_device_id = simulator_get_device_id,
};

static const struct uart_operations simulator_uart = {
.init = uart_init,
.write = uart_write,
.read = uart_read,
.clean = uart_clean,
};

static const struct secure_store_operations simulator_sec_ops = {
.init = sec_init,
.write = sec_write,
.read = sec_read,
.delete = sec_delete,
};

struct device_type simulator_device_type = {
.name = "simulator",
.op = &simulator_ops,
.uart = &simulator_uart,
.sec_ops = &simulator_sec_ops,
};

// must be declared at the end of impl.c
DECLARE_DEVICE(emulator);
DECLARE_DEVICE(simulator);
```
6 changes: 6 additions & 0 deletions docs/endpoint-platforms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Endpoint support platforms

The following platforms are verified platforms of endpoint

* simulator on x86_64 platform : The development platform for testing endpoint.
* [AirPrime WP7702 LPWA Module](https://www.sierrawireless.com/products-and-solutions/embedded-solutions/products/wp7702/) : The product platform for endpoint.
3 changes: 3 additions & 0 deletions endpoint/endpoint.adef
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ processes:
}
}

#if ${LEGATO_TARGET} = localhost
#else
bindings:
{
endpoint.endpointComp.le_secStore -> secStore.le_secStore
endpoint.endpointComp.le_sim -> modemService.le_sim
}
#endif

start: manual
7 changes: 6 additions & 1 deletion endpoint/endpointComp/Component.cdef
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ sources:
{
${CURDIR}/../endpoint_core.c
${CURDIR}/../hal/device.c
${CURDIR}/../platform/wp77xx/impl.c

// include the specific platform
${CURDIR}/../platform/${TARGET}/impl.c

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

Expand Down Expand Up @@ -87,6 +89,8 @@ cflags:
-I${CURDIR}/../../output_base/external/mbedtls_2_16_6/include
}

#if ${LEGATO_TARGET} = localhost
#else
requires:
{
device:
Expand All @@ -100,3 +104,4 @@ requires:
modemServices/le_sim.api
}
}
#endif
8 changes: 4 additions & 4 deletions endpoint/endpoint_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ status_t send_transaction_information(const char* host, const char* port, const

const char* ta_host = host ? host : STR(EP_TA_HOST);
const char* ta_port = port ? port : STR(EP_TA_PORT);
char ipv4[16];
char ipv4[NI_MAXHOST];
if (resolve_ip_address(ta_host, ipv4) != SC_OK) {
return SC_ENDPOINT_DNS_RESOLVE_ERROR;
}
Expand Down Expand Up @@ -130,7 +130,7 @@ status_t send_transaction_information(const char* host, const char* port, const
return SC_OK;
}

status_t resolve_ip_address(const char* host, char result[16]) {
status_t resolve_ip_address(const char* host, char* result) {
struct addrinfo hints;
struct addrinfo* res;

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

for (struct addrinfo* re = res; res != NULL; re = re->ai_next) {
char host_buf[1024];
char host_buf[NI_MAXHOST];
int ret = getnameinfo(re->ai_addr, re->ai_addrlen, host_buf, sizeof(host_buf), NULL, 0, NI_NUMERICHOST);
if (ret == 0) {
snprintf(result, 16, "%s", host_buf);
snprintf(result, NI_MAXHOST, "%s", host_buf);
break;
} else {
ta_log_error("Getnameinfo returned: %s\n", gai_strerror(ret));
Expand Down
2 changes: 1 addition & 1 deletion endpoint/endpoint_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ status_t send_transaction_information(const char* host, const char* port, const
* @param[out] result The buffer to store the IPV4 address output
* @return #status_t
*/
status_t resolve_ip_address(const char* host, char result[16]);
status_t resolve_ip_address(const char* host, char* result);

#endif // ENDPOINT_CORE_H
9 changes: 5 additions & 4 deletions endpoint/hal/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
* "LICENSE" at the root of this distribution.
*/

#include "legato.h"

#include "device.h"
#include "le_log.h"

static struct device_type *devices;

Expand All @@ -20,14 +23,12 @@ static struct device_type **find_device(const char *name, unsigned len) {
device_t *ta_device(const char *type) {
struct device_type **p;
if (devices->next) {
// TODO:Use logger
fprintf(stderr, "No device type registered!");
LE_ERROR("No device type registered!");
return NULL;
}
p = find_device(type, strlen(type));
if (*p) {
// TODO:Use logger
fprintf(stderr, "Device type %s not found", type);
LE_INFO("Device type %s not found", type);
}
return *p;
}
Expand Down
9 changes: 9 additions & 0 deletions endpoint/platform/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "device",
deps = [
"//endpoint/hal",
"//endpoint/simluator",
],
)
9 changes: 9 additions & 0 deletions endpoint/platform/simulator/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "simulator",
srcs = ["simulator.c"],
deps = [
"//endpoint/hal",
],
)
Loading

0 comments on commit 1541bfd

Please sign in to comment.