mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-02-11 09:25:20 +00:00
Add a new extension library: connection (#39)
This commit is contained in:
parent
69d62f573a
commit
7f1e024fce
27
README.md
27
README.md
|
@ -339,12 +339,35 @@ void api_timer_restart(user_timer_t timer, int interval);
|
|||
Currently we provide the sensor API's as one library extension sample. In the header file ```lib/app-libs/extension/sensor/sensor.h```, the API set is defined as below:
|
||||
``` C
|
||||
sensor_t sensor_open(const char* name, int index,
|
||||
void(*on_sensor_event)(sensor_t, attr_container_t *, void *),
|
||||
void *user_data);
|
||||
void(*on_sensor_event)(sensor_t, attr_container_t *, void *),
|
||||
void *user_data);
|
||||
bool sensor_config(sensor_t sensor, int interval, int bit_cfg, int delay);
|
||||
bool sensor_config_with_attr_container(sensor_t sensor, attr_container_t *cfg);
|
||||
bool sensor_close(sensor_t sensor);
|
||||
```
|
||||
We provide the connection API's as another sample. In the header file `lib/app-libs/extension/connection/connection.h.`, the API set is defined as below:
|
||||
``` C
|
||||
/* Connection event type */
|
||||
typedef enum {
|
||||
/* Data is received */
|
||||
CONN_EVENT_TYPE_DATA = 1,
|
||||
/* Connection is disconnected */
|
||||
CONN_EVENT_TYPE_DISCONNECT
|
||||
} conn_event_type_t;
|
||||
|
||||
typedef void (*on_connection_event_f)(connection_t *conn,
|
||||
conn_event_type_t type,
|
||||
const char *data,
|
||||
uint32 len,
|
||||
void *user_data);
|
||||
connection_t *api_open_connection(const char *name,
|
||||
attr_container_t *args,
|
||||
on_connection_event_f on_event,
|
||||
void *user_data);
|
||||
void api_close_connection(connection_t *conn);
|
||||
int api_send_on_connection(connection_t *conn, const char *data, uint32 len);
|
||||
bool api_config_connection(connection_t *conn, attr_container_t *cfg);
|
||||
```
|
||||
|
||||
The mechanism of exporting native API to WASM application
|
||||
=======================================================
|
||||
|
|
|
@ -39,8 +39,10 @@ extern "C" {
|
|||
#define SECTION_TYPE_DATA 11
|
||||
|
||||
enum {
|
||||
WASM_Msg_Start = BASE_EVENT_MAX, TIMER_EVENT_WASM, SENSOR_EVENT_WASM,
|
||||
|
||||
WASM_Msg_Start = BASE_EVENT_MAX,
|
||||
TIMER_EVENT_WASM,
|
||||
SENSOR_EVENT_WASM,
|
||||
CONNECTION_EVENT_WASM,
|
||||
WASM_Msg_End = WASM_Msg_Start + 100
|
||||
};
|
||||
|
||||
|
|
|
@ -37,6 +37,7 @@
|
|||
#include "attr_container.h"
|
||||
#include "request.h"
|
||||
#include "sensor.h"
|
||||
#include "connection.h"
|
||||
#include "timer_wasm_app.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
130
core/iwasm/lib/app-libs/extension/connection/connection.c
Normal file
130
core/iwasm/lib/app-libs/extension/connection/connection.c
Normal file
|
@ -0,0 +1,130 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "connection.h"
|
||||
#include "native_interface.h"
|
||||
|
||||
/* Raw connection structure */
|
||||
typedef struct _connection {
|
||||
/* Next connection */
|
||||
struct _connection *next;
|
||||
|
||||
/* Handle of the connection */
|
||||
uint32 handle;
|
||||
|
||||
/* Callback function called when event on this connection occurs */
|
||||
on_connection_event_f on_event;
|
||||
|
||||
/* User data */
|
||||
void *user_data;
|
||||
} connection_t;
|
||||
|
||||
/* Raw connections list */
|
||||
static connection_t *g_conns = NULL;
|
||||
|
||||
connection_t *api_open_connection(const char *name,
|
||||
attr_container_t *args,
|
||||
on_connection_event_f on_event,
|
||||
void *user_data)
|
||||
{
|
||||
connection_t *conn;
|
||||
char *args_buffer = (char *)args;
|
||||
uint32 handle, args_len = attr_container_get_serialize_length(args);
|
||||
|
||||
handle = wasm_open_connection((int32)name, (int32)args_buffer, args_len);
|
||||
if (handle == -1)
|
||||
return NULL;
|
||||
|
||||
conn = (connection_t *)malloc(sizeof(*conn));
|
||||
if (conn == NULL) {
|
||||
wasm_close_connection(handle);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(conn, 0, sizeof(*conn));
|
||||
conn->handle = handle;
|
||||
conn->on_event = on_event;
|
||||
conn->user_data = user_data;
|
||||
|
||||
if (g_conns != NULL) {
|
||||
conn->next = g_conns;
|
||||
g_conns = conn;
|
||||
} else {
|
||||
g_conns = conn;
|
||||
}
|
||||
|
||||
return conn;
|
||||
}
|
||||
|
||||
void api_close_connection(connection_t *c)
|
||||
{
|
||||
connection_t *conn = g_conns, *prev = NULL;
|
||||
|
||||
while (conn) {
|
||||
if (conn == c) {
|
||||
wasm_close_connection(c->handle);
|
||||
if (prev != NULL)
|
||||
prev->next = conn->next;
|
||||
else
|
||||
g_conns = conn->next;
|
||||
free(conn);
|
||||
return;
|
||||
} else {
|
||||
prev = conn;
|
||||
conn = conn->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int api_send_on_connection(connection_t *conn, const char *data, uint32 len)
|
||||
{
|
||||
return wasm_send_on_connection(conn->handle, (int32)data, len);
|
||||
}
|
||||
|
||||
bool api_config_connection(connection_t *conn, attr_container_t *cfg)
|
||||
{
|
||||
char *cfg_buffer = (char *)cfg;
|
||||
uint32 cfg_len = attr_container_get_serialize_length(cfg);
|
||||
|
||||
return wasm_config_connection(conn->handle, (int32)cfg_buffer, cfg_len);
|
||||
}
|
||||
|
||||
void on_connection_data(uint32 handle, char *buffer, uint32 len)
|
||||
{
|
||||
connection_t *conn = g_conns;
|
||||
|
||||
while (conn != NULL) {
|
||||
if (conn->handle == handle) {
|
||||
if (len == 0) {
|
||||
conn->on_event(conn,
|
||||
CONN_EVENT_TYPE_DISCONNECT,
|
||||
NULL,
|
||||
0,
|
||||
conn->user_data);
|
||||
} else {
|
||||
conn->on_event(conn,
|
||||
CONN_EVENT_TYPE_DATA,
|
||||
buffer,
|
||||
len,
|
||||
conn->user_data);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
conn = conn->next;
|
||||
}
|
||||
}
|
||||
|
106
core/iwasm/lib/app-libs/extension/connection/connection.h
Normal file
106
core/iwasm/lib/app-libs/extension/connection/connection.h
Normal file
|
@ -0,0 +1,106 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _CONNECTION_H_
|
||||
#define _CONNECTION_H_
|
||||
|
||||
#include "attr_container.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct _connection;
|
||||
typedef struct _connection connection_t;
|
||||
|
||||
/* Connection event type */
|
||||
typedef enum {
|
||||
/* Data is received */
|
||||
CONN_EVENT_TYPE_DATA = 1,
|
||||
/* Connection is disconnected */
|
||||
CONN_EVENT_TYPE_DISCONNECT
|
||||
} conn_event_type_t;
|
||||
|
||||
/*
|
||||
* @typedef on_connection_event_f
|
||||
*
|
||||
* @param conn the connection that the event belongs to
|
||||
* @param type event type
|
||||
* @param data the data received for CONN_EVENT_TYPE_DATA event
|
||||
* @param len length of the data in byte
|
||||
* @param user_data user data
|
||||
*/
|
||||
typedef void (*on_connection_event_f)(connection_t *conn,
|
||||
conn_event_type_t type,
|
||||
const char *data,
|
||||
uint32 len,
|
||||
void *user_data);
|
||||
|
||||
/*
|
||||
*****************
|
||||
* Connection API's
|
||||
*****************
|
||||
*/
|
||||
|
||||
/*
|
||||
* @brief Open a connection.
|
||||
*
|
||||
* @param name name of the connection, "TCP", "UDP" or "UART"
|
||||
* @param args connection arguments, such as: ip:127.0.0.1, port:8888
|
||||
* @param on_event callback function called when event occurs
|
||||
* @param user_data user data
|
||||
*
|
||||
* @return the connection or NULL means fail
|
||||
*/
|
||||
connection_t *api_open_connection(const char *name,
|
||||
attr_container_t *args,
|
||||
on_connection_event_f on_event,
|
||||
void *user_data);
|
||||
|
||||
/*
|
||||
* @brief Close a connection.
|
||||
*
|
||||
* @param conn connection
|
||||
*/
|
||||
void api_close_connection(connection_t *conn);
|
||||
|
||||
/*
|
||||
* Send data to the connection in non-blocking manner which returns immediately
|
||||
*
|
||||
* @param conn the connection
|
||||
* @param data data buffer to be sent
|
||||
* @param len length of the data in byte
|
||||
*
|
||||
* @return actual length sent, or -1 if fail(maybe underlying buffer is full)
|
||||
*/
|
||||
int api_send_on_connection(connection_t *conn, const char *data, uint32 len);
|
||||
|
||||
/*
|
||||
* @brief Configure connection.
|
||||
*
|
||||
* @param conn the connection
|
||||
* @param cfg configurations
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool api_config_connection(connection_t *conn, attr_container_t *cfg);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
38
core/iwasm/lib/native-interface/connection_api.h
Normal file
38
core/iwasm/lib/native-interface/connection_api.h
Normal file
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef CONNECTION_API_H_
|
||||
#define CONNECTION_API_H_
|
||||
#include "bh_platform.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
uint32 wasm_open_connection(int32 name_offset, int32 args_offset, uint32 len);
|
||||
|
||||
void wasm_close_connection(uint32 handle);
|
||||
|
||||
int wasm_send_on_connection(uint32 handle, int32 data_offset, uint32 len);
|
||||
|
||||
bool wasm_config_connection(uint32 handle, int32 cfg_offset, uint32 len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* CONNECTION_API_H_ */
|
|
@ -75,4 +75,14 @@ void wasm_timer_destory(timer_id_t timer_id);
|
|||
void wasm_timer_cancel(timer_id_t timer_id);
|
||||
void wasm_timer_restart(timer_id_t timer_id, int interval);
|
||||
uint32 wasm_get_sys_tick_ms(void);
|
||||
#endif /* DEPS_SSG_MICRO_RUNTIME_WASM_POC_APP_LIBS_NATIVE_INTERFACE_NATIVE_INTERFACE_H_ */
|
||||
|
||||
/*
|
||||
* *** connection interface ***
|
||||
*/
|
||||
uint32 wasm_open_connection(int32 name_offset, int32 args_offset, uint32 len);
|
||||
void wasm_close_connection(uint32 handle);
|
||||
int wasm_send_on_connection(uint32 handle, int32 data_offset, uint32 len);
|
||||
bool wasm_config_connection(uint32 handle, int32 cfg_offset, uint32 len);
|
||||
|
||||
#endif /* DEPS_SSG_MICRO_RUNTIME_WASM_PO
|
||||
C_APP_LIBS_NATIVE_INTERFACE_NATIVE_INTERFACE_H_ */
|
||||
|
|
20
core/iwasm/lib/native/extension/connection/connection.inl
Normal file
20
core/iwasm/lib/native/extension/connection/connection.inl
Normal file
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
EXPORT_WASM_API(wasm_open_connection),
|
||||
EXPORT_WASM_API(wasm_close_connection),
|
||||
EXPORT_WASM_API(wasm_send_on_connection),
|
||||
EXPORT_WASM_API(wasm_config_connection),
|
86
core/iwasm/lib/native/extension/connection/connection_lib.h
Normal file
86
core/iwasm/lib/native/extension/connection/connection_lib.h
Normal file
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef CONNECTION_LIB_H_
|
||||
#define CONNECTION_LIB_H_
|
||||
|
||||
#include "attr_container.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
*****************
|
||||
* This file defines connection library which should be implemented by different platforms
|
||||
*****************
|
||||
*/
|
||||
|
||||
/*
|
||||
* @brief Open a connection.
|
||||
*
|
||||
* @param name name of the connection, "TCP", "UDP" or "UART"
|
||||
* @param args connection arguments, such as: ip:127.0.0.1, port:8888
|
||||
*
|
||||
* @return 0~0xFFFFFFFE means id of the connection, otherwise(-1) means fail
|
||||
*/
|
||||
typedef uint32 (*connection_open_f)(const char *name, attr_container_t *args);
|
||||
|
||||
/*
|
||||
* @brief Close a connection.
|
||||
*
|
||||
* @param handle of the connection
|
||||
*/
|
||||
typedef void (*connection_close_f)(uint32 handle);
|
||||
|
||||
/*
|
||||
* @brief Send data to the connection in non-blocking manner.
|
||||
*
|
||||
* @param handle of the connection
|
||||
* @param data data buffer to be sent
|
||||
* @param len length of the data in byte
|
||||
*
|
||||
* @return actual length sent, -1 if fail
|
||||
*/
|
||||
typedef int (*connection_send_f)(uint32 handle, const char *data, int len);
|
||||
|
||||
/*
|
||||
* @brief Configure connection.
|
||||
*
|
||||
* @param handle of the connection
|
||||
* @param cfg configurations
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
typedef bool (*connection_config_f)(uint32 handle, attr_container_t *cfg);
|
||||
|
||||
/* Raw connection interface for platform to implement */
|
||||
typedef struct _connection_interface {
|
||||
connection_open_f _open;
|
||||
connection_close_f _close;
|
||||
connection_send_f _send;
|
||||
connection_config_f _config;
|
||||
} connection_interface_t;
|
||||
|
||||
/* Platform must define this interface */
|
||||
extern connection_interface_t connection_impl;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* CONNECTION_LIB_H_ */
|
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "connection_lib.h"
|
||||
#include "wasm_export.h"
|
||||
#include "native_interface.h"
|
||||
|
||||
/* Note:
|
||||
*
|
||||
* This file is the consumer of connection lib which is implemented by different platforms
|
||||
*/
|
||||
|
||||
|
||||
uint32 wasm_open_connection(int32 name_offset, int32 args_offset, uint32 len)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
attr_container_t *args;
|
||||
char *name, *args_buf;
|
||||
|
||||
if (!validate_app_addr(name_offset, 1) ||
|
||||
!validate_app_addr(args_offset, len) ||
|
||||
!(name = addr_app_to_native(name_offset)) ||
|
||||
!(args_buf = addr_app_to_native(args_offset)))
|
||||
return -1;
|
||||
|
||||
args = (attr_container_t *)args_buf;
|
||||
|
||||
if (connection_impl._open != NULL)
|
||||
return connection_impl._open(name, args);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
void wasm_close_connection(uint32 handle)
|
||||
{
|
||||
if (connection_impl._close != NULL)
|
||||
connection_impl._close(handle);
|
||||
}
|
||||
|
||||
int wasm_send_on_connection(uint32 handle, int32 data_offset, uint32 len)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *data;
|
||||
|
||||
if (!validate_app_addr(data_offset, len) ||
|
||||
!(data = addr_app_to_native(data_offset)))
|
||||
return -1;
|
||||
|
||||
if (connection_impl._send != NULL)
|
||||
return connection_impl._send(handle, data, len);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool wasm_config_connection(uint32 handle, int32 cfg_offset, uint32 len)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *cfg_buf;
|
||||
attr_container_t *cfg;
|
||||
|
||||
if (!validate_app_addr(cfg_offset, len) ||
|
||||
!(cfg_buf = addr_app_to_native(cfg_offset)))
|
||||
return false;
|
||||
|
||||
cfg = (attr_container_t *)cfg_buf;
|
||||
|
||||
if (connection_impl._config != NULL)
|
||||
return connection_impl._config(handle, cfg);
|
||||
|
||||
return false;
|
||||
}
|
62
core/iwasm/lib/native/extension/connection/linux/conn_tcp.c
Normal file
62
core/iwasm/lib/native/extension/connection/linux/conn_tcp.c
Normal file
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "conn_tcp.h"
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int tcp_open(char *address, uint16 port)
|
||||
{
|
||||
int sock, ret;
|
||||
struct sockaddr_in servaddr;
|
||||
|
||||
memset(&servaddr, 0, sizeof(servaddr));
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_addr.s_addr = inet_addr(address);
|
||||
servaddr.sin_port = htons(port);
|
||||
|
||||
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (sock == -1)
|
||||
return -1;
|
||||
|
||||
ret = connect(sock, (struct sockaddr*)&servaddr, sizeof(servaddr));
|
||||
if (ret == -1) {
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Put the socket in non-blocking mode */
|
||||
if (fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK) < 0) {
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
int tcp_send(int sock, const char *data, int size)
|
||||
{
|
||||
return send(sock, data, size, 0);
|
||||
}
|
||||
|
||||
int tcp_recv(int sock, char *buffer, int buf_size)
|
||||
{
|
||||
return recv(sock, buffer, buf_size, 0);
|
||||
}
|
37
core/iwasm/lib/native/extension/connection/linux/conn_tcp.h
Normal file
37
core/iwasm/lib/native/extension/connection/linux/conn_tcp.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef CONN_LINUX_TCP_H_
|
||||
#define CONN_LINUX_TCP_H_
|
||||
|
||||
#include "bh_platform.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int tcp_open(char *address, uint16 port);
|
||||
|
||||
int tcp_send(int sock, const char *data, int size);
|
||||
|
||||
int tcp_recv(int sock, char *buffer, int buf_size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
110
core/iwasm/lib/native/extension/connection/linux/conn_uart.c
Normal file
110
core/iwasm/lib/native/extension/connection/linux/conn_uart.c
Normal file
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "conn_uart.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int parse_baudrate(int baud)
|
||||
{
|
||||
switch (baud) {
|
||||
case 9600:
|
||||
return B9600;
|
||||
case 19200:
|
||||
return B19200;
|
||||
case 38400:
|
||||
return B38400;
|
||||
case 57600:
|
||||
return B57600;
|
||||
case 115200:
|
||||
return B115200;
|
||||
case 230400:
|
||||
return B230400;
|
||||
case 460800:
|
||||
return B460800;
|
||||
case 500000:
|
||||
return B500000;
|
||||
case 576000:
|
||||
return B576000;
|
||||
case 921600:
|
||||
return B921600;
|
||||
case 1000000:
|
||||
return B1000000;
|
||||
case 1152000:
|
||||
return B1152000;
|
||||
case 1500000:
|
||||
return B1500000;
|
||||
case 2000000:
|
||||
return B2000000;
|
||||
case 2500000:
|
||||
return B2500000;
|
||||
case 3000000:
|
||||
return B3000000;
|
||||
case 3500000:
|
||||
return B3500000;
|
||||
case 4000000:
|
||||
return B4000000;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int uart_open(char* device, int baudrate)
|
||||
{
|
||||
int uart_fd;
|
||||
struct termios uart_term;
|
||||
|
||||
uart_fd = open(device, O_RDWR | O_NOCTTY);
|
||||
|
||||
if (uart_fd <= 0)
|
||||
return -1;
|
||||
|
||||
memset(&uart_term, 0, sizeof(uart_term));
|
||||
uart_term.c_cflag = parse_baudrate(baudrate) | CS8 | CLOCAL | CREAD;
|
||||
uart_term.c_iflag = IGNPAR;
|
||||
uart_term.c_oflag = 0;
|
||||
|
||||
/* set noncanonical mode */
|
||||
uart_term.c_lflag = 0;
|
||||
uart_term.c_cc[VTIME] = 30;
|
||||
uart_term.c_cc[VMIN] = 1;
|
||||
tcflush(uart_fd, TCIFLUSH);
|
||||
|
||||
if (tcsetattr(uart_fd, TCSANOW, &uart_term) != 0) {
|
||||
close(uart_fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Put the fd in non-blocking mode */
|
||||
if (fcntl(uart_fd, F_SETFL, fcntl(uart_fd, F_GETFL) | O_NONBLOCK) < 0) {
|
||||
close(uart_fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return uart_fd;
|
||||
}
|
||||
|
||||
int uart_send(int fd, const char *data, int size)
|
||||
{
|
||||
return write(fd, data, size);
|
||||
}
|
||||
|
||||
int uart_recv(int fd, char *buffer, int buf_size)
|
||||
{
|
||||
return read(fd, buffer, buf_size);
|
||||
}
|
37
core/iwasm/lib/native/extension/connection/linux/conn_uart.h
Normal file
37
core/iwasm/lib/native/extension/connection/linux/conn_uart.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef CONN_LINUX_UART_H_
|
||||
#define CONN_LINUX_UART_H_
|
||||
|
||||
#include "bh_platform.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int uart_open(char* device, int baudrate);
|
||||
|
||||
int uart_send(int fd, const char *data, int size);
|
||||
|
||||
int uart_recv(int fd, char *buffer, int buf_size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
68
core/iwasm/lib/native/extension/connection/linux/conn_udp.c
Normal file
68
core/iwasm/lib/native/extension/connection/linux/conn_udp.c
Normal file
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "conn_udp.h"
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int udp_open(uint16 port)
|
||||
{
|
||||
int sock, ret;
|
||||
struct sockaddr_in addr;
|
||||
|
||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sock == -1)
|
||||
return -1;
|
||||
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
addr.sin_port = htons(port);
|
||||
|
||||
ret = bind(sock, (struct sockaddr*)&addr, sizeof(addr));
|
||||
if (ret == -1)
|
||||
return -1;
|
||||
|
||||
/* Put the socket in non-blocking mode */
|
||||
if (fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK) < 0) {
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
int udp_send(int sock, struct sockaddr *dest, const char *data, int size)
|
||||
{
|
||||
return sendto(sock, data, size, MSG_CONFIRM, dest, sizeof(*dest));
|
||||
}
|
||||
|
||||
int udp_recv(int sock, char *buffer, int buf_size)
|
||||
{
|
||||
struct sockaddr_in remaddr;
|
||||
socklen_t addrlen = sizeof(remaddr);
|
||||
|
||||
return recvfrom(sock,
|
||||
buffer,
|
||||
buf_size,
|
||||
0,
|
||||
(struct sockaddr *)&remaddr,
|
||||
&addrlen);
|
||||
}
|
37
core/iwasm/lib/native/extension/connection/linux/conn_udp.h
Normal file
37
core/iwasm/lib/native/extension/connection/linux/conn_udp.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef CONN_LINUX_UDP_H_
|
||||
#define CONN_LINUX_UDP_H_
|
||||
|
||||
#include "bh_platform.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int udp_open(uint16 port);
|
||||
|
||||
int udp_send(int sock, struct sockaddr *dest, const char *data, int size);
|
||||
|
||||
int udp_recv(int sock, char *buffer, int buf_size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
|
@ -0,0 +1,572 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Note:
|
||||
* This file implements the linux version connection library which is
|
||||
* defined in connection_lib.h.
|
||||
* It also provides a reference implementation of connections manager.
|
||||
*/
|
||||
|
||||
#include "connection_lib.h"
|
||||
#include "bh_thread.h"
|
||||
#include "app_manager_export.h"
|
||||
#include "module_wasm_app.h"
|
||||
#include "conn_tcp.h"
|
||||
#include "conn_udp.h"
|
||||
#include "conn_uart.h"
|
||||
#include "bh_definition.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#define MAX_EVENTS 10
|
||||
#define IO_BUF_SIZE 256
|
||||
|
||||
/* Connection type */
|
||||
typedef enum conn_type {
|
||||
CONN_TYPE_TCP,
|
||||
CONN_TYPE_UDP,
|
||||
CONN_TYPE_UART,
|
||||
CONN_TYPE_UNKNOWN
|
||||
} conn_type_t;
|
||||
|
||||
/* Sys connection */
|
||||
typedef struct sys_connection {
|
||||
/* Next connection */
|
||||
struct sys_connection *next;
|
||||
|
||||
/* Type */
|
||||
conn_type_t type;
|
||||
|
||||
/* Handle to interact with wasm app */
|
||||
uint32 handle;
|
||||
|
||||
/* Underlying connection ID, may be socket fd */
|
||||
int fd;
|
||||
|
||||
/* Module id that the connection belongs to */
|
||||
uint32 module_id;
|
||||
|
||||
/* Argument, such as dest addr for udp */
|
||||
void *arg;
|
||||
} sys_connection_t;
|
||||
|
||||
/* Epoll instance */
|
||||
static int epollfd;
|
||||
|
||||
/* Connections list */
|
||||
static sys_connection_t *g_connections = NULL;
|
||||
|
||||
/* Max handle */
|
||||
static uint32 g_handle_max = 0;
|
||||
|
||||
/* Lock to protect g_connections and g_handle_max */
|
||||
static korp_mutex g_lock;
|
||||
|
||||
/* Epoll events */
|
||||
static struct epoll_event epoll_events[MAX_EVENTS];
|
||||
|
||||
/* Buffer to receive data */
|
||||
static char io_buf[IO_BUF_SIZE];
|
||||
|
||||
static uint32 _conn_open(const char *name, attr_container_t *args);
|
||||
static void _conn_close(uint32 handle);
|
||||
static int _conn_send(uint32 handle, const char *data, int len);
|
||||
static bool _conn_config(uint32 handle, attr_container_t *cfg);
|
||||
|
||||
/*
|
||||
* Platform implementation of connection library
|
||||
*/
|
||||
connection_interface_t connection_impl = {
|
||||
._open = _conn_open,
|
||||
._close = _conn_close,
|
||||
._send = _conn_send,
|
||||
._config = _conn_config
|
||||
};
|
||||
|
||||
static void add_connection(sys_connection_t *conn)
|
||||
{
|
||||
vm_mutex_lock(&g_lock);
|
||||
|
||||
g_handle_max++;
|
||||
if (g_handle_max == -1)
|
||||
g_handle_max++;
|
||||
conn->handle = g_handle_max;
|
||||
|
||||
if (g_connections) {
|
||||
conn->next = g_connections;
|
||||
g_connections = conn;
|
||||
} else {
|
||||
g_connections = conn;
|
||||
}
|
||||
|
||||
vm_mutex_unlock(&g_lock);
|
||||
}
|
||||
|
||||
#define FREE_CONNECTION(conn) do { \
|
||||
if (conn->arg) \
|
||||
bh_free(conn->arg); \
|
||||
bh_free(conn); \
|
||||
} while (0)
|
||||
|
||||
static int get_app_conns_num(uint32 module_id)
|
||||
{
|
||||
sys_connection_t *conn;
|
||||
int num = 0;
|
||||
|
||||
vm_mutex_lock(&g_lock);
|
||||
|
||||
conn = g_connections;
|
||||
while (conn) {
|
||||
if (conn->module_id == module_id)
|
||||
num++;
|
||||
conn = conn->next;
|
||||
}
|
||||
|
||||
vm_mutex_unlock(&g_lock);
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
static sys_connection_t *find_connection(uint32 handle, bool remove_found)
|
||||
{
|
||||
sys_connection_t *conn, *prev = NULL;
|
||||
|
||||
vm_mutex_lock(&g_lock);
|
||||
|
||||
conn = g_connections;
|
||||
while (conn) {
|
||||
if (conn->handle == handle) {
|
||||
if (remove_found) {
|
||||
if (prev != NULL) {
|
||||
prev->next = conn->next;
|
||||
} else {
|
||||
g_connections = conn->next;
|
||||
}
|
||||
}
|
||||
vm_mutex_unlock(&g_lock);
|
||||
return conn;
|
||||
} else {
|
||||
prev = conn;
|
||||
conn = conn->next;
|
||||
}
|
||||
}
|
||||
|
||||
vm_mutex_unlock(&g_lock);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void cleanup_connections(uint32 module_id)
|
||||
{
|
||||
sys_connection_t *conn, *prev = NULL;
|
||||
|
||||
vm_mutex_lock(&g_lock);
|
||||
|
||||
conn = g_connections;
|
||||
while (conn) {
|
||||
if (conn->module_id == module_id) {
|
||||
epoll_ctl(epollfd, EPOLL_CTL_DEL, conn->fd, NULL);
|
||||
close(conn->fd);
|
||||
|
||||
if (prev != NULL) {
|
||||
prev->next = conn->next;
|
||||
FREE_CONNECTION(conn);
|
||||
conn = prev->next;
|
||||
} else {
|
||||
g_connections = conn->next;
|
||||
FREE_CONNECTION(conn);
|
||||
conn = g_connections;
|
||||
}
|
||||
} else {
|
||||
prev = conn;
|
||||
conn = conn->next;
|
||||
}
|
||||
}
|
||||
|
||||
vm_mutex_unlock(&g_lock);
|
||||
}
|
||||
|
||||
static conn_type_t get_conn_type(const char *name)
|
||||
{
|
||||
if (strcmp(name, "TCP") == 0)
|
||||
return CONN_TYPE_TCP;
|
||||
if (strcmp(name, "UDP") == 0)
|
||||
return CONN_TYPE_UDP;
|
||||
if (strcmp(name, "UART") == 0)
|
||||
return CONN_TYPE_UART;
|
||||
|
||||
return CONN_TYPE_UNKNOWN;
|
||||
}
|
||||
|
||||
/* --- connection lib function --- */
|
||||
static uint32 _conn_open(const char *name, attr_container_t *args)
|
||||
{
|
||||
int fd;
|
||||
sys_connection_t *conn;
|
||||
struct epoll_event ev;
|
||||
uint32 module_id = app_manager_get_module_id(Module_WASM_App);
|
||||
|
||||
if (get_app_conns_num(module_id) >= MAX_CONNECTION_PER_APP)
|
||||
return -1;
|
||||
|
||||
conn = (sys_connection_t *)bh_malloc(sizeof(*conn));
|
||||
if (conn == NULL)
|
||||
return -1;
|
||||
|
||||
memset(conn, 0, sizeof(*conn));
|
||||
conn->module_id = module_id;
|
||||
conn->type = get_conn_type(name);
|
||||
|
||||
/* Generate a handle and add to list */
|
||||
add_connection(conn);
|
||||
|
||||
if (conn->type == CONN_TYPE_TCP) {
|
||||
char *address;
|
||||
uint16 port;
|
||||
|
||||
/* Check and parse connection parameters */
|
||||
if (!attr_container_contain_key(args, "address") ||
|
||||
!attr_container_contain_key(args, "port"))
|
||||
goto fail;
|
||||
|
||||
address = attr_container_get_as_string(args, "address");
|
||||
port = attr_container_get_as_uint16(args, "port");
|
||||
|
||||
/* Connect to TCP server */
|
||||
if ((fd = tcp_open(address, port)) == -1)
|
||||
goto fail;
|
||||
|
||||
} else if (conn->type == CONN_TYPE_UDP) {
|
||||
uint16 port;
|
||||
|
||||
/* Check and parse connection parameters */
|
||||
if (!attr_container_contain_key(args, "bind port"))
|
||||
goto fail;
|
||||
port = attr_container_get_as_uint16(args, "bind port");
|
||||
|
||||
/* Bind port */
|
||||
if ((fd = udp_open(port)) == -1)
|
||||
goto fail;
|
||||
|
||||
} else if (conn->type == CONN_TYPE_UART) {
|
||||
char *device;
|
||||
int baud;
|
||||
|
||||
/* Check and parse connection parameters */
|
||||
if (!attr_container_contain_key(args, "device") ||
|
||||
!attr_container_contain_key(args, "baudrate"))
|
||||
goto fail;
|
||||
device = attr_container_get_as_string(args, "device");
|
||||
baud = attr_container_get_as_int(args, "baudrate");
|
||||
|
||||
/* Open device */
|
||||
if ((fd = uart_open(device, baud)) == -1)
|
||||
goto fail;
|
||||
|
||||
}
|
||||
|
||||
conn->fd = fd;
|
||||
|
||||
/* Set current connection as event data */
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.ptr = conn;
|
||||
|
||||
/* Monitor incoming data */
|
||||
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
|
||||
close(fd);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
return conn->handle;
|
||||
|
||||
fail:
|
||||
find_connection(conn->handle, true);
|
||||
bh_free(conn);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* --- connection lib function --- */
|
||||
static void _conn_close(uint32 handle)
|
||||
{
|
||||
sys_connection_t *conn = find_connection(handle, true);
|
||||
|
||||
if (conn != NULL) {
|
||||
epoll_ctl(epollfd, EPOLL_CTL_DEL, conn->fd, NULL);
|
||||
close(conn->fd);
|
||||
FREE_CONNECTION(conn);
|
||||
}
|
||||
}
|
||||
|
||||
/* --- connection lib function --- */
|
||||
static int _conn_send(uint32 handle, const char *data, int len)
|
||||
{
|
||||
sys_connection_t *conn = find_connection(handle, false);
|
||||
|
||||
if (conn == NULL)
|
||||
return -1;
|
||||
|
||||
if (conn->type == CONN_TYPE_TCP)
|
||||
return tcp_send(conn->fd, data, len);
|
||||
|
||||
if (conn->type == CONN_TYPE_UDP) {
|
||||
struct sockaddr *addr = (struct sockaddr *)conn->arg;
|
||||
return udp_send(conn->fd, addr, data, len);
|
||||
}
|
||||
|
||||
if (conn->type == CONN_TYPE_UART)
|
||||
return uart_send(conn->fd, data, len);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* --- connection lib function --- */
|
||||
static bool _conn_config(uint32 handle, attr_container_t *cfg)
|
||||
{
|
||||
sys_connection_t *conn = find_connection(handle, false);
|
||||
|
||||
if (conn == NULL)
|
||||
return false;
|
||||
|
||||
if (conn->type == CONN_TYPE_UDP) {
|
||||
char *address;
|
||||
uint16_t port;
|
||||
struct sockaddr_in *addr;
|
||||
|
||||
/* Parse remote address/port */
|
||||
if (!attr_container_contain_key(cfg, "address") ||
|
||||
!attr_container_contain_key(cfg, "port"))
|
||||
return false;
|
||||
address = attr_container_get_as_string(cfg, "address");
|
||||
port = attr_container_get_as_uint16(cfg, "port");
|
||||
|
||||
if (conn->arg == NULL) {
|
||||
addr = (struct sockaddr_in *)bh_malloc(sizeof(*addr));
|
||||
if (addr == NULL)
|
||||
return false;
|
||||
|
||||
memset(addr, 0, sizeof(*addr));
|
||||
addr->sin_family = AF_INET;
|
||||
addr->sin_addr.s_addr = inet_addr(address);
|
||||
addr->sin_port = htons(port);
|
||||
|
||||
/* Set remote address as connection arg */
|
||||
conn->arg = addr;
|
||||
} else {
|
||||
addr = (struct sockaddr_in *)conn->arg;
|
||||
addr->sin_addr.s_addr = inet_addr(address);
|
||||
addr->sin_port = htons(port);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* --- connection manager reference implementation ---*/
|
||||
|
||||
typedef struct connection_event {
|
||||
uint32 handle;
|
||||
char *data;
|
||||
uint32 len;
|
||||
} connection_event_t;
|
||||
|
||||
static void connection_event_cleaner(connection_event_t *conn_event)
|
||||
{
|
||||
if (conn_event->data != NULL)
|
||||
bh_free(conn_event->data);
|
||||
bh_free(conn_event);
|
||||
}
|
||||
|
||||
static void post_msg_to_module(sys_connection_t *conn,
|
||||
char *data,
|
||||
uint32 len)
|
||||
{
|
||||
module_data *module = module_data_list_lookup_id(conn->module_id);
|
||||
char *data_copy = NULL;
|
||||
connection_event_t *conn_data_event;
|
||||
bh_message_t msg;
|
||||
|
||||
if (module == NULL)
|
||||
return;
|
||||
|
||||
conn_data_event = (connection_event_t *)bh_malloc(sizeof(*conn_data_event));
|
||||
if (conn_data_event == NULL)
|
||||
return;
|
||||
|
||||
if (len > 0) {
|
||||
data_copy = (char *)bh_malloc(len);
|
||||
if (data_copy == NULL) {
|
||||
bh_free(conn_data_event);
|
||||
return;
|
||||
}
|
||||
memcpy(data_copy, data, len);
|
||||
}
|
||||
|
||||
memset(conn_data_event, 0, sizeof(*conn_data_event));
|
||||
conn_data_event->handle = conn->handle;
|
||||
conn_data_event->data = data_copy;
|
||||
conn_data_event->len = len;
|
||||
|
||||
msg = bh_new_msg(CONNECTION_EVENT_WASM,
|
||||
conn_data_event,
|
||||
sizeof(*conn_data_event),
|
||||
connection_event_cleaner);
|
||||
if (!msg) {
|
||||
connection_event_cleaner(conn_data_event);
|
||||
return;
|
||||
}
|
||||
|
||||
bh_post_msg2(module->queue, msg);
|
||||
}
|
||||
|
||||
static void* polling_thread_routine (void *arg)
|
||||
{
|
||||
while (true) {
|
||||
int i, n;
|
||||
|
||||
n = epoll_wait(epollfd, epoll_events, MAX_EVENTS, -1);
|
||||
|
||||
if (n == -1 && errno != EINTR)
|
||||
continue;
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
sys_connection_t *conn
|
||||
= (sys_connection_t *)epoll_events[i].data.ptr;
|
||||
|
||||
if (conn->type == CONN_TYPE_TCP) {
|
||||
int count = tcp_recv(conn->fd, io_buf, IO_BUF_SIZE);
|
||||
if (count <= 0) {
|
||||
/* Connection is closed by peer */
|
||||
post_msg_to_module(conn, NULL, 0);
|
||||
_conn_close(conn->handle);
|
||||
} else {
|
||||
/* Data is received */
|
||||
post_msg_to_module(conn, io_buf, count);
|
||||
}
|
||||
} else if (conn->type == CONN_TYPE_UDP) {
|
||||
int count = udp_recv(conn->fd, io_buf, IO_BUF_SIZE);
|
||||
if (count > 0)
|
||||
post_msg_to_module(conn, io_buf, count);
|
||||
} else if (conn->type == CONN_TYPE_UART) {
|
||||
int count = uart_recv(conn->fd, io_buf, IO_BUF_SIZE);
|
||||
if (count > 0)
|
||||
post_msg_to_module(conn, io_buf, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void app_mgr_connection_event_callback(module_data *m_data, bh_message_t msg)
|
||||
{
|
||||
uint32 argv[3];
|
||||
wasm_function_inst_t func_on_conn_data;
|
||||
bh_assert(CONNECTION_EVENT_WASM == bh_message_type(msg));
|
||||
wasm_data *wasm_app_data = (wasm_data*) m_data->internal_data;
|
||||
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
|
||||
connection_event_t *conn_event
|
||||
= (connection_event_t *)bh_message_payload(msg);
|
||||
int32 data_offset;
|
||||
|
||||
if (conn_event == NULL)
|
||||
return;
|
||||
|
||||
func_on_conn_data = wasm_runtime_lookup_function(inst, "_on_connection_data",
|
||||
"(i32i32i32)");
|
||||
if (!func_on_conn_data) {
|
||||
printf("Cannot find function _on_connection_data\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* 0 len means connection closed */
|
||||
if (conn_event->len == 0) {
|
||||
argv[0] = conn_event->handle;
|
||||
argv[1] = 0;
|
||||
argv[2] = 0;
|
||||
if (!wasm_runtime_call_wasm(inst, NULL, func_on_conn_data, 3, argv)) {
|
||||
printf(":Got exception running wasm code: %s\n",
|
||||
wasm_runtime_get_exception(inst));
|
||||
wasm_runtime_clear_exception(inst);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
data_offset = wasm_runtime_module_dup_data(inst,
|
||||
conn_event->data,
|
||||
conn_event->len);
|
||||
if (data_offset == 0) {
|
||||
printf("Got exception running wasm code: %s\n",
|
||||
wasm_runtime_get_exception(inst));
|
||||
wasm_runtime_clear_exception(inst);
|
||||
return;
|
||||
}
|
||||
|
||||
argv[0] = conn_event->handle;
|
||||
argv[1] = (uint32) data_offset;
|
||||
argv[2] = conn_event->len;
|
||||
if (!wasm_runtime_call_wasm(inst, NULL, func_on_conn_data, 3, argv)) {
|
||||
printf(":Got exception running wasm code: %s\n",
|
||||
wasm_runtime_get_exception(inst));
|
||||
wasm_runtime_clear_exception(inst);
|
||||
wasm_runtime_module_free(inst, data_offset);
|
||||
return;
|
||||
}
|
||||
wasm_runtime_module_free(inst, data_offset);
|
||||
}
|
||||
}
|
||||
|
||||
bool init_connection_framework()
|
||||
{
|
||||
korp_thread tid;
|
||||
|
||||
epollfd = epoll_create(MAX_EVENTS);
|
||||
if (epollfd == -1)
|
||||
return false;
|
||||
|
||||
if (vm_mutex_init(&g_lock) != BH_SUCCESS) {
|
||||
close(epollfd);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!wasm_register_cleanup_callback(cleanup_connections)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (!wasm_register_msg_callback(CONNECTION_EVENT_WASM,
|
||||
app_mgr_connection_event_callback)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (vm_thread_create(&tid,
|
||||
polling_thread_routine,
|
||||
NULL,
|
||||
BH_APPLET_PRESERVED_STACK_SIZE) != BH_SUCCESS) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
fail:
|
||||
vm_mutex_destroy(&g_lock);
|
||||
close(epollfd);
|
||||
return false;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
set (WASM_LIB_CONN_MGR_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
include_directories(${WASM_LIB_CONN_MGR_DIR})
|
||||
|
||||
|
||||
file (GLOB_RECURSE source_all ${WASM_LIB_CONN_MGR_DIR}/*.c)
|
||||
|
||||
set (WASM_LIB_CONN_MGR_SOURCE ${source_all})
|
||||
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
set (WASM_LIB_CONN_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
include_directories(${WASM_LIB_CONN_DIR})
|
||||
|
||||
|
||||
file (GLOB source_all ${WASM_LIB_CONN_DIR}/*.c)
|
||||
|
||||
set (WASM_LIB_CONN_SOURCE ${source_all})
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Note:
|
||||
* This file implements the linux version connection library which is
|
||||
* defined in connection_lib.h.
|
||||
* It also provides a reference impl of connections manager.
|
||||
*/
|
||||
|
||||
#include "connection_lib.h"
|
||||
|
||||
/*
|
||||
* Platform implementation of connection library
|
||||
*/
|
||||
connection_interface_t connection_impl = {
|
||||
._open = NULL,
|
||||
._close = NULL,
|
||||
._send = NULL,
|
||||
._config = NULL
|
||||
};
|
|
@ -63,6 +63,9 @@
|
|||
/* Max timer number in one app */
|
||||
#define MAX_TIMERS_PER_APP 30
|
||||
|
||||
/* Max connection number in one app */
|
||||
#define MAX_CONNECTION_PER_APP 20
|
||||
|
||||
/* Max resource registration number in one app */
|
||||
#define RESOURCE_REGISTRATION_NUM_MAX 16
|
||||
|
||||
|
|
|
@ -57,6 +57,8 @@ include (${WASM_DIR}/runtime/vmcore-wasm/vmcore.cmake)
|
|||
include (${WASM_DIR}/lib/native/base/wasm_lib_base.cmake)
|
||||
include (${WASM_DIR}/lib/native/libc/wasm_libc.cmake)
|
||||
include (${WASM_DIR}/lib/native/extension/sensor/wasm_lib_sensor.cmake)
|
||||
include (${WASM_DIR}/lib/native/extension/connection/wasm_lib_conn.cmake)
|
||||
include (${WASM_DIR}/lib/native/extension/connection/${TARGET_PLATFORM}/connection_mgr.cmake)
|
||||
include (${WASM_DIR}/lib/native-interface/native_interface.cmake)
|
||||
include (${APP_MGR_DIR}/app-manager/app_mgr.cmake)
|
||||
include (${APP_MGR_DIR}/app-mgr-shared/app_mgr_shared.cmake)
|
||||
|
@ -81,6 +83,8 @@ add_library (vmlib
|
|||
${WASM_LIB_BASE_SOURCE}
|
||||
${WASM_LIB_EXT_SOURCE}
|
||||
${WASM_LIB_SENSOR_SOURCE}
|
||||
${WASM_LIB_CONN_SOURCE}
|
||||
${WASM_LIB_CONN_MGR_SOURCE}
|
||||
${PLATFORM_SHARED_SOURCE}
|
||||
${UTILS_SHARED_SOURCE}
|
||||
${MEM_ALLOC_SHARED_SOURCE}
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
#include "lib_export.h"
|
||||
#include "native_interface.h"
|
||||
#include "connection_api.h"
|
||||
#include "display_indev.h"
|
||||
|
||||
static NativeSymbol extended_native_symbol_defs[] = {
|
||||
#include "runtime_sensor.inl"
|
||||
#include "connection.inl"
|
||||
EXPORT_WASM_API(display_init),
|
||||
EXPORT_WASM_API(display_input_read),
|
||||
EXPORT_WASM_API(display_flush),
|
||||
|
|
|
@ -46,6 +46,7 @@ static int baudrate = B115200;
|
|||
extern void * thread_timer_check(void *);
|
||||
extern void init_sensor_framework();
|
||||
extern int aee_host_msg_callback(void *msg, uint16_t msg_len);
|
||||
extern bool init_connection_framework();
|
||||
|
||||
#ifndef CONNECTION_UART
|
||||
int listenfd = -1;
|
||||
|
@ -441,6 +442,12 @@ int iwasm_main(int argc, char *argv[])
|
|||
if (vm_thread_sys_init() != 0) {
|
||||
goto fail1;
|
||||
}
|
||||
|
||||
if (!init_connection_framework()) {
|
||||
vm_thread_sys_destroy();
|
||||
goto fail1;
|
||||
}
|
||||
|
||||
extern void display_SDL_init();
|
||||
display_SDL_init();
|
||||
|
||||
|
|
|
@ -38,6 +38,7 @@ target_include_directories(app PRIVATE ${IWASM_ROOT}/runtime/include
|
|||
${IWASM_ROOT}/lib/native/base
|
||||
${IWASM_ROOT}/lib/native/libc
|
||||
${IWASM_ROOT}/lib/native/extension/sensor
|
||||
${IWASM_ROOT}/lib/native/extension/connection
|
||||
${IWASM_ROOT}/lib/native-interface
|
||||
${APP_MGR_ROOT}/app-manager
|
||||
${APP_MGR_ROOT}/app-mgr-shared
|
||||
|
@ -69,6 +70,8 @@ set (IWASM_SRCS ${IWASM_ROOT}/runtime/platform/zephyr/wasm_math.c
|
|||
${IWASM_ROOT}/lib/native/base/timer_wrapper.c
|
||||
${IWASM_ROOT}/lib/native/libc/libc_wrapper.c
|
||||
${IWASM_ROOT}/lib/native/extension/sensor/runtime_sensor.c
|
||||
${IWASM_ROOT}/lib/native/extension/connection/connection_wrapper.c
|
||||
${IWASM_ROOT}/lib/native/extension/connection/zephyr/connection_lib_impl.c
|
||||
${IWASM_ROOT}/lib/native-interface/attr_container.c
|
||||
${IWASM_ROOT}/lib/native-interface/restful_utils.c
|
||||
${APP_MGR_ROOT}/app-manager/app_manager.c
|
||||
|
|
|
@ -14,7 +14,17 @@
|
|||
|
||||
CC = emcc
|
||||
LVGL_DIR = ${shell pwd}
|
||||
CFLAGS += -O3 -DLV_CONF_INCLUDE_SIMPLE=1 -I$(LVGL_DIR)/ -I$(LVGL_DIR)/lvgl/ -I$(LVGL_DIR)/lv_drivers/ -I$(LVGL_DIR)/src/ -I../../../core/iwasm/lib/app-libs/base/ -I../../../core/iwasm/lib/native-interface/ -I../../../core/iwasm/lib/app-libs/extension/sensor
|
||||
IWASM_DIR=../../../core/iwasm
|
||||
CFLAGS += -O3 \
|
||||
-DLV_CONF_INCLUDE_SIMPLE=1 \
|
||||
-I$(LVGL_DIR)/ \
|
||||
-I$(LVGL_DIR)/lvgl/ \
|
||||
-I$(LVGL_DIR)/lv_drivers/ \
|
||||
-I$(LVGL_DIR)/src/ \
|
||||
-I$(IWASM_DIR)/lib/app-libs/base/ \
|
||||
-I$(IWASM_DIR)/lib/native-interface/ \
|
||||
-I$(IWASM_DIR)/lib/app-libs/extension/sensor \
|
||||
-I$(IWASM_DIR)/lib/app-libs/extension/connection
|
||||
|
||||
SRCS += lvgl/lv_draw/lv_draw_line.c lvgl/lv_draw/lv_draw_rbasic.c
|
||||
SRCS += lvgl/lv_draw/lv_draw_img.c lvgl/lv_draw/lv_draw_arc.c
|
||||
|
@ -46,10 +56,11 @@ SRCS += lvgl/lv_core/lv_group.c lvgl/lv_core/lv_style.c lvgl/lv_core/lv_indev.c
|
|||
SRCS += lvgl/lv_core/lv_vdb.c lvgl/lv_core/lv_obj.c lvgl/lv_core/lv_refr.c
|
||||
SRCS += $(LVGL_DIR)/src/main.c
|
||||
|
||||
SRCS += ../../../core/iwasm/lib/app-libs/base/timer.c
|
||||
# For app size consideration, not all but necessary app libs are included
|
||||
SRCS += $(IWASM_DIR)/lib/app-libs/base/timer.c
|
||||
all:
|
||||
@$(CC) $(CFLAGS) $(SRCS) \
|
||||
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
|
||||
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=2048\
|
||||
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_request', '_on_sensor_event', '_on_timer_callback']" \
|
||||
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_timer_callback']" \
|
||||
-o ui_app.wasm
|
||||
|
|
|
@ -57,6 +57,8 @@ include (${WASM_DIR}/runtime/vmcore-wasm/vmcore.cmake)
|
|||
include (${WASM_DIR}/lib/native/base/wasm_lib_base.cmake)
|
||||
include (${WASM_DIR}/lib/native/libc/wasm_libc.cmake)
|
||||
include (${WASM_DIR}/lib/native/extension/sensor/wasm_lib_sensor.cmake)
|
||||
include (${WASM_DIR}/lib/native/extension/connection/wasm_lib_conn.cmake)
|
||||
include (${WASM_DIR}/lib/native/extension/connection/${TARGET_PLATFORM}/connection_mgr.cmake)
|
||||
include (${WASM_DIR}/lib/native-interface/native_interface.cmake)
|
||||
include (${APP_MGR_DIR}/app-manager/app_mgr.cmake)
|
||||
include (${APP_MGR_DIR}/app-mgr-shared/app_mgr_shared.cmake)
|
||||
|
@ -83,6 +85,8 @@ add_library (vmlib
|
|||
${WASM_LIB_BASE_SOURCE}
|
||||
${WASM_LIB_EXT_SOURCE}
|
||||
${WASM_LIB_SENSOR_SOURCE}
|
||||
${WASM_LIB_CONN_SOURCE}
|
||||
${WASM_LIB_CONN_MGR_SOURCE}
|
||||
${PLATFORM_SHARED_SOURCE}
|
||||
${UTILS_SHARED_SOURCE}
|
||||
${MEM_ALLOC_SHARED_SOURCE}
|
||||
|
|
|
@ -16,18 +16,13 @@ simple/
|
|||
│ ├── iwasm_main.c
|
||||
│ └── main.c
|
||||
└── wasm-apps
|
||||
├── event_publisher
|
||||
│ └── event_publisher.c
|
||||
├── event_subscriber
|
||||
│ └── event_subscriber.c
|
||||
├── request_handler
|
||||
│ └── request_handler.c
|
||||
├── request_sender
|
||||
│ └── request_sender.c
|
||||
├── sensor
|
||||
│ └── sensor.c
|
||||
└── timer
|
||||
└── timer.c
|
||||
├── connection.c
|
||||
├── event_publisher.c
|
||||
├── event_subscriber.c
|
||||
├── request_handler.c
|
||||
├── request_sender.c
|
||||
├── sensor.c
|
||||
└── timer.c
|
||||
```
|
||||
|
||||
- build.sh<br/>
|
||||
|
@ -75,18 +70,19 @@ Execute the build.sh script then all binaries including wasm application files w
|
|||
|
||||
Out directory structure
|
||||
------------------------------
|
||||
```
|
||||
```
|
||||
out/
|
||||
├── host_tool
|
||||
├── simple
|
||||
└── wasm-apps
|
||||
├── connection.wasm
|
||||
├── event_publisher.wasm
|
||||
├── event_subscriber.wasm
|
||||
├── request_handler.wasm
|
||||
├── request_sender.wasm
|
||||
├── sensor.wasm
|
||||
└── timer.wasm
|
||||
```
|
||||
```
|
||||
|
||||
- host_tool:
|
||||
A small testing tool to interact with WAMR. See the usage of this tool by executing "./host_tool -h".
|
||||
|
@ -100,6 +96,8 @@ out/
|
|||
|
||||
- wasm-apps:
|
||||
Sample wasm applications that demonstrate all APIs of the WAMR programming model. The source codes are in the wasm-apps directory under the root of this project.
|
||||
+ connection.wasm<br/>
|
||||
This application shows the connection programming model. It connects to a TCP server on 127.0.0.1:7777 and periodically sends message to it.
|
||||
+ event_publisher.wasm<br/>
|
||||
This application shows the sub/pub programming model. The pub application publishes the event "alert/overheat" by calling api_publish_event() API. The subscriber could be host_tool or other wasm application.
|
||||
+ event_subscriber.wasm<br/>
|
||||
|
|
|
@ -8,7 +8,7 @@ BUILD_DIR=${PWD}/build
|
|||
IWASM_ROOT=${PWD}/../../core/iwasm
|
||||
APP_LIBS=${IWASM_ROOT}/lib/app-libs
|
||||
NATIVE_LIBS=${IWASM_ROOT}/lib/native-interface
|
||||
APP_LIB_SRC="${APP_LIBS}/base/*.c ${APP_LIBS}/extension/sensor/*.c ${NATIVE_LIBS}/*.c"
|
||||
APP_LIB_SRC="${APP_LIBS}/base/*.c ${APP_LIBS}/extension/sensor/*.c ${APP_LIBS}/extension/connection/*.c ${NATIVE_LIBS}/*.c"
|
||||
WASM_APPS=${PWD}/wasm-apps
|
||||
|
||||
rm -rf ${OUT_DIR}
|
||||
|
@ -49,54 +49,23 @@ echo "#####################build host-tool success"
|
|||
|
||||
echo "#####################build wasm apps"
|
||||
|
||||
cd ${CURR_DIR}
|
||||
cd ${WASM_APPS}
|
||||
|
||||
APP_SRC="${WASM_APPS}/timer/timer.c ${APP_LIB_SRC}"
|
||||
for i in `ls *.c`
|
||||
do
|
||||
APP_SRC="$i ${APP_LIB_SRC}"
|
||||
OUT_FILE=${i%.*}.wasm
|
||||
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
|
||||
-I${APP_LIBS}/extension/connection \
|
||||
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
|
||||
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
|
||||
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
|
||||
'_on_sensor_event', '_on_timer_callback']" \
|
||||
-o ${OUT_DIR}/wasm-apps/timer.wasm ${APP_SRC}
|
||||
|
||||
APP_SRC="${WASM_APPS}/request_handler/request_handler.c ${APP_LIB_SRC}"
|
||||
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
|
||||
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
|
||||
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
|
||||
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
|
||||
'_on_sensor_event', '_on_timer_callback']" \
|
||||
-o ${OUT_DIR}/wasm-apps/request_handler.wasm ${APP_SRC}
|
||||
|
||||
APP_SRC="${WASM_APPS}/request_sender/request_sender.c ${APP_LIB_SRC}"
|
||||
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
|
||||
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
|
||||
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
|
||||
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
|
||||
'_on_sensor_event', '_on_timer_callback']" \
|
||||
-o ${OUT_DIR}/wasm-apps/request_sender.wasm ${APP_SRC}
|
||||
|
||||
APP_SRC="${WASM_APPS}/event_publisher/event_publisher.c ${APP_LIB_SRC}"
|
||||
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
|
||||
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
|
||||
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
|
||||
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
|
||||
'_on_sensor_event', '_on_timer_callback']" \
|
||||
-o ${OUT_DIR}/wasm-apps/event_publisher.wasm ${APP_SRC}
|
||||
|
||||
APP_SRC="${WASM_APPS}/event_subscriber/event_subscriber.c ${APP_LIB_SRC}"
|
||||
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
|
||||
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
|
||||
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
|
||||
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
|
||||
'_on_sensor_event', '_on_timer_callback']" \
|
||||
-o ${OUT_DIR}/wasm-apps/event_subscriber.wasm ${APP_SRC}
|
||||
|
||||
APP_SRC="${WASM_APPS}/sensor/sensor.c ${APP_LIB_SRC}"
|
||||
emcc -O3 -I${APP_LIBS}/base -I${APP_LIBS}/extension/sensor -I${NATIVE_LIBS} \
|
||||
-s WASM=1 -s SIDE_MODULE=1 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 \
|
||||
-s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 \
|
||||
-s "EXPORTED_FUNCTIONS=['_on_init', '_on_destroy', '_on_request', '_on_response', \
|
||||
'_on_sensor_event', '_on_timer_callback']" \
|
||||
-o ${OUT_DIR}/wasm-apps/sensor.wasm ${APP_SRC}
|
||||
|
||||
echo "#####################build wasm apps success"
|
||||
'_on_sensor_event', '_on_timer_callback', '_on_connection_data']" \
|
||||
-o ${OUT_DIR}/wasm-apps/${OUT_FILE} ${APP_SRC}
|
||||
if [ -f ${OUT_DIR}/wasm-apps/${OUT_FILE} ]; then
|
||||
echo "build ${OUT_FILE} success"
|
||||
else
|
||||
echo "build ${OUT_FILE} fail"
|
||||
fi
|
||||
done
|
||||
echo "#####################build wasm apps done"
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
#include "lib_export.h"
|
||||
#include "sensor_api.h"
|
||||
#include "connection_api.h"
|
||||
|
||||
static NativeSymbol extended_native_symbol_defs[] = {
|
||||
#include "runtime_sensor.inl"
|
||||
#include "connection.inl"
|
||||
};
|
||||
|
||||
#include "ext_lib_export.h"
|
||||
|
|
|
@ -46,6 +46,7 @@ static int baudrate = B115200;
|
|||
extern void * thread_timer_check(void *);
|
||||
extern void init_sensor_framework();
|
||||
extern int aee_host_msg_callback(void *msg, uint16_t msg_len);
|
||||
extern bool init_connection_framework();
|
||||
|
||||
#ifndef CONNECTION_UART
|
||||
int listenfd = -1;
|
||||
|
@ -213,7 +214,7 @@ void* func_server_mode(void* arg)
|
|||
sockfd = -1;
|
||||
pthread_mutex_unlock(&sock_lock);
|
||||
|
||||
sleep(2);
|
||||
sleep(1);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -442,6 +443,11 @@ int iwasm_main(int argc, char *argv[])
|
|||
goto fail1;
|
||||
}
|
||||
|
||||
if (!init_connection_framework()) {
|
||||
vm_thread_sys_destroy();
|
||||
goto fail1;
|
||||
}
|
||||
|
||||
init_sensor_framework();
|
||||
|
||||
// timer manager
|
||||
|
|
93
samples/simple/wasm-apps/connection.c
Normal file
93
samples/simple/wasm-apps/connection.c
Normal file
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "wasm_app.h"
|
||||
|
||||
/* User global variable */
|
||||
static int num = 0;
|
||||
static user_timer_t g_timer;
|
||||
static connection_t *g_conn = NULL;
|
||||
|
||||
void on_data1(connection_t *conn,
|
||||
conn_event_type_t type,
|
||||
const char *data,
|
||||
uint32 len,
|
||||
void *user_data)
|
||||
{
|
||||
if (type == CONN_EVENT_TYPE_DATA) {
|
||||
char message[64] = {0};
|
||||
memcpy(message, data, len);
|
||||
printf("Client got a message from server -> %s\n", message);
|
||||
} else if (type == CONN_EVENT_TYPE_DISCONNECT) {
|
||||
printf("connection is close by server!\n");
|
||||
} else {
|
||||
printf("error: got unknown event type!!!\n");
|
||||
}
|
||||
}
|
||||
|
||||
/* Timer callback */
|
||||
void timer1_update(user_timer_t timer)
|
||||
{
|
||||
char message[64] = {0};
|
||||
/* Reply to server */
|
||||
snprintf(message, sizeof(message), "Hello %d", num++);
|
||||
api_send_on_connection(g_conn, message, strlen(message));
|
||||
}
|
||||
|
||||
void my_close_handler(request_t * request)
|
||||
{
|
||||
response_t response[1];
|
||||
|
||||
if (g_conn != NULL) {
|
||||
api_timer_cancel(g_timer);
|
||||
api_close_connection(g_conn);
|
||||
}
|
||||
|
||||
make_response_for_request(request, response);
|
||||
set_response(response, DELETED_2_02, 0, NULL, 0);
|
||||
api_response_send(response);
|
||||
}
|
||||
|
||||
void on_init()
|
||||
{
|
||||
user_timer_t timer;
|
||||
attr_container_t *args;
|
||||
char *str = "this is client!";
|
||||
|
||||
api_register_resource_handler("/close", my_close_handler);
|
||||
|
||||
args = attr_container_create("");
|
||||
attr_container_set_string(&args, "address", "127.0.0.1");
|
||||
attr_container_set_uint16(&args, "port", 7777);
|
||||
|
||||
g_conn = api_open_connection("TCP", args, on_data1, NULL);
|
||||
if (g_conn == NULL) {
|
||||
printf("connect to server fail!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("connect to server success! handle: %p\n", g_conn);
|
||||
|
||||
/* set up a timer */
|
||||
timer = api_timer_create(1000, true, false, timer1_update);
|
||||
api_timer_restart(timer, 1000);
|
||||
}
|
||||
|
||||
void on_destroy()
|
||||
{
|
||||
/* real destroy work including killing timer and closing sensor is
|
||||
accomplished in wasm app library version of on_destroy() */
|
||||
}
|
Loading…
Reference in New Issue
Block a user