Apply clang-format for more source files (#795)

Apply clang-format for C source files in folder core/app-mgr,
core/app-framework, and test-tools.
And rename folder component_test to component-test, update
zephyr build document.

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
Wenyong Huang
2021-10-21 13:58:34 +08:00
committed by GitHub
parent 225f5d0a64
commit 32242988ed
143 changed files with 5377 additions and 4627 deletions

View File

@ -8,12 +8,12 @@
#include "module_wasm_app.h"
#include "bh_platform.h"
static sys_sensor_t * g_sys_sensors = NULL;
static sys_sensor_t *g_sys_sensors = NULL;
static int g_sensor_id_max = 0;
static sensor_client_t *
find_sensor_client(sys_sensor_t * sensor,
unsigned int client_id, bool remove_if_found);
find_sensor_client(sys_sensor_t *sensor, unsigned int client_id,
bool remove_if_found);
void (*rechedule_sensor_callback)() = NULL;
@ -38,30 +38,32 @@ sensor_event_cleaner(sensor_event_data_t *sensor_event)
static void
wasm_sensor_callback(void *client, uint32 sensor_id, void *user_data)
{
attr_container_t *sensor_data = (attr_container_t *) user_data;
attr_container_t *sensor_data = (attr_container_t *)user_data;
attr_container_t *sensor_data_clone;
int sensor_data_len;
sensor_event_data_t *sensor_event;
bh_message_t msg;
sensor_client_t *c = (sensor_client_t *) client;
sensor_client_t *c = (sensor_client_t *)client;
module_data *module = module_data_list_lookup_id(c->client_id);
if (module == NULL)
return;
if (sensor_data == NULL)
return;
return;
sensor_data_len = attr_container_get_serialize_length(sensor_data);
sensor_data_clone = (attr_container_t *)wasm_runtime_malloc(sensor_data_len);
sensor_data_clone =
(attr_container_t *)wasm_runtime_malloc(sensor_data_len);
if (sensor_data_clone == NULL)
return;
/* multiple sensor clients may use/free the sensor data, so make a copy */
bh_memcpy_s(sensor_data_clone, sensor_data_len,
sensor_data, sensor_data_len);
bh_memcpy_s(sensor_data_clone, sensor_data_len, sensor_data,
sensor_data_len);
sensor_event = (sensor_event_data_t *)wasm_runtime_malloc(sizeof(*sensor_event));
sensor_event =
(sensor_event_data_t *)wasm_runtime_malloc(sizeof(*sensor_event));
if (sensor_event == NULL) {
wasm_runtime_free(sensor_data_clone);
return;
@ -72,9 +74,7 @@ wasm_sensor_callback(void *client, uint32 sensor_id, void *user_data)
sensor_event->data = sensor_data_clone;
sensor_event->data_fmt = FMT_ATTR_CONTAINER;
msg = bh_new_msg(SENSOR_EVENT_WASM,
sensor_event,
sizeof(*sensor_event),
msg = bh_new_msg(SENSOR_EVENT_WASM, sensor_event, sizeof(*sensor_event),
sensor_event_cleaner);
if (!msg) {
sensor_event_cleaner(sensor_event);
@ -85,19 +85,18 @@ wasm_sensor_callback(void *client, uint32 sensor_id, void *user_data)
}
bool
wasm_sensor_config(wasm_exec_env_t exec_env,
uint32 sensor, int interval,
wasm_sensor_config(wasm_exec_env_t exec_env, uint32 sensor, int interval,
int bit_cfg, int delay)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
attr_container_t * attr_cont;
sensor_client_t * c;
attr_container_t *attr_cont;
sensor_client_t *c;
sensor_obj_t s = find_sys_sensor_id(sensor);
if (s == NULL)
return false;
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
module_inst);
unsigned int mod_id =
app_manager_get_module_id(Module_WASM_App, module_inst);
bh_assert(mod_id != ID_NONE);
os_mutex_lock(&s->lock);
@ -131,8 +130,7 @@ wasm_sensor_config(wasm_exec_env_t exec_env,
}
uint32
wasm_sensor_open(wasm_exec_env_t exec_env,
char *name, int instance)
wasm_sensor_open(wasm_exec_env_t exec_env, char *name, int instance)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
@ -142,8 +140,8 @@ wasm_sensor_open(wasm_exec_env_t exec_env,
if (s == NULL)
return -1;
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
module_inst);
unsigned int mod_id =
app_manager_get_module_id(Module_WASM_App, module_inst);
bh_assert(mod_id != ID_NONE);
os_mutex_lock(&s->lock);
@ -155,8 +153,8 @@ wasm_sensor_open(wasm_exec_env_t exec_env,
return -1;
}
sensor_client_t * client = (sensor_client_t*) wasm_runtime_malloc(
sizeof(sensor_client_t));
sensor_client_t *client =
(sensor_client_t *)wasm_runtime_malloc(sizeof(sensor_client_t));
if (client == NULL) {
os_mutex_unlock(&s->lock);
return -1;
@ -182,8 +180,8 @@ wasm_sensor_open(wasm_exec_env_t exec_env,
}
bool
wasm_sensor_config_with_attr_container(wasm_exec_env_t exec_env,
uint32 sensor, char *buffer, int len)
wasm_sensor_config_with_attr_container(wasm_exec_env_t exec_env, uint32 sensor,
char *buffer, int len)
{
if (buffer != NULL) {
attr_container_t *cfg = (attr_container_t *)buffer;
@ -204,8 +202,8 @@ bool
wasm_sensor_close(wasm_exec_env_t exec_env, uint32 sensor)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
module_inst);
unsigned int mod_id =
app_manager_get_module_id(Module_WASM_App, module_inst);
unsigned int client_id = mod_id;
sensor_obj_t s = find_sys_sensor_id(sensor);
sensor_client_t *c;
@ -232,19 +230,22 @@ wasm_sensor_close(wasm_exec_env_t exec_env, uint32 sensor)
* sensor framework API - don't expose to the applications
*
*/
void set_sensor_reshceduler(void (*callback)())
void
set_sensor_reshceduler(void (*callback)())
{
rechedule_sensor_callback = callback;
}
// used for other threads to wakeup the sensor read thread
void reschedule_sensor_read()
void
reschedule_sensor_read()
{
if (rechedule_sensor_callback)
rechedule_sensor_callback();
}
void refresh_read_interval(sensor_obj_t sensor)
void
refresh_read_interval(sensor_obj_t sensor)
{
sensor_client_t *c;
uint32 interval = sensor->default_interval;
@ -266,10 +267,10 @@ void refresh_read_interval(sensor_obj_t sensor)
}
sensor_obj_t
add_sys_sensor(char * name, char * description, int instance,
uint32 default_interval, void * read_func, void * config_func)
add_sys_sensor(char *name, char *description, int instance,
uint32 default_interval, void *read_func, void *config_func)
{
sys_sensor_t * s = (sys_sensor_t *) wasm_runtime_malloc(sizeof(sys_sensor_t));
sys_sensor_t *s = (sys_sensor_t *)wasm_runtime_malloc(sizeof(sys_sensor_t));
if (s == NULL)
return NULL;
@ -302,7 +303,8 @@ add_sys_sensor(char * name, char * description, int instance,
if (g_sys_sensors == NULL) {
g_sys_sensors = s;
} else {
}
else {
s->next = g_sys_sensors;
g_sys_sensors = s;
}
@ -312,9 +314,10 @@ add_sys_sensor(char * name, char * description, int instance,
return s;
}
sensor_obj_t find_sys_sensor(const char* name, int instance)
sensor_obj_t
find_sys_sensor(const char *name, int instance)
{
sys_sensor_t * s = g_sys_sensors;
sys_sensor_t *s = g_sys_sensors;
while (s) {
if (strcmp(s->name, name) == 0 && s->sensor_instance == instance)
return s;
@ -324,9 +327,10 @@ sensor_obj_t find_sys_sensor(const char* name, int instance)
return NULL;
}
sensor_obj_t find_sys_sensor_id(uint32 sensor_id)
sensor_obj_t
find_sys_sensor_id(uint32 sensor_id)
{
sys_sensor_t * s = g_sys_sensors;
sys_sensor_t *s = g_sys_sensors;
while (s) {
if (s->sensor_id == sensor_id)
return s;
@ -336,8 +340,9 @@ sensor_obj_t find_sys_sensor_id(uint32 sensor_id)
return NULL;
}
sensor_client_t *find_sensor_client(sys_sensor_t * sensor,
unsigned int client_id, bool remove_if_found)
sensor_client_t *
find_sensor_client(sys_sensor_t *sensor, unsigned int client_id,
bool remove_if_found)
{
sensor_client_t *prev = NULL, *c = sensor->clients;
@ -351,7 +356,8 @@ sensor_client_t *find_sensor_client(sys_sensor_t * sensor,
sensor->clients = next;
}
return c;
} else {
}
else {
c = c->next;
}
}
@ -360,12 +366,13 @@ sensor_client_t *find_sensor_client(sys_sensor_t * sensor,
}
// return the milliseconds to next check
int check_sensor_timers()
int
check_sensor_timers()
{
int ms_to_next_check = -1;
uint32 now = (uint32)bh_get_tick_ms();
sys_sensor_t * s = g_sys_sensors;
sys_sensor_t *s = g_sys_sensors;
while (s) {
uint32 last_read = s->last_read;
uint32 elpased_ms = bh_get_elpased_ms(&last_read);
@ -376,9 +383,9 @@ int check_sensor_timers()
}
if (elpased_ms >= s->read_interval) {
attr_container_t * data = s->read(s);
attr_container_t *data = s->read(s);
if (data) {
sensor_client_t * client = s->clients;
sensor_client_t *client = s->clients;
while (client) {
client->client_callback(client, s->sensor_id, data);
client = client->next;
@ -390,11 +397,11 @@ int check_sensor_timers()
if (ms_to_next_check == -1 || (ms_to_next_check < s->read_interval))
ms_to_next_check = s->read_interval;
} else {
}
else {
int remaining = s->read_interval - elpased_ms;
if (ms_to_next_check == -1 || (ms_to_next_check < remaining))
ms_to_next_check = remaining;
}
s = s->next;
@ -403,9 +410,10 @@ int check_sensor_timers()
return ms_to_next_check;
}
void sensor_cleanup_callback(uint32 module_id)
void
sensor_cleanup_callback(uint32 module_id)
{
sys_sensor_t * s = g_sys_sensors;
sys_sensor_t *s = g_sys_sensors;
while (s) {
sensor_client_t *c;

View File

@ -12,49 +12,58 @@
#include "sensor_native_api.h"
struct _sys_sensor;
typedef struct _sys_sensor* sensor_obj_t;
typedef struct _sys_sensor *sensor_obj_t;
typedef struct _sensor_client {
struct _sensor_client * next;
struct _sensor_client *next;
unsigned int client_id; // the app id
int interval;
int bit_cfg;
int delay;
void (*client_callback)(void * client, uint32, attr_container_t *);
void (*client_callback)(void *client, uint32, attr_container_t *);
} sensor_client_t;
typedef struct _sys_sensor {
struct _sys_sensor * next;
char * name;
struct _sys_sensor *next;
char *name;
int sensor_instance;
char * description;
char *description;
uint32 sensor_id;
sensor_client_t * clients;
sensor_client_t *clients;
/* app, sensor mgr and app mgr may access the clients at the same time,
* so need a lock to protect the clients */
so need a lock to protect the clients */
korp_mutex lock;
uint32 last_read;
uint32 read_interval;
uint32 default_interval;
attr_container_t * (*read)(void *); /* TODO: may support other type return value, such as 'cbor' */
/* TODO: may support other type return value, such as 'cbor' */
attr_container_t *(*read)(void *);
bool (*config)(void *, void *);
} sys_sensor_t;
sensor_obj_t add_sys_sensor(char * name, char * description, int instance,
uint32 default_interval, void * read_func, void * config_func);
sensor_obj_t find_sys_sensor(const char* name, int instance);
sensor_obj_t find_sys_sensor_id(uint32 sensor_id);
void refresh_read_interval(sensor_obj_t sensor);
void sensor_cleanup_callback(uint32 module_id);
int check_sensor_timers();
void reschedule_sensor_read();
void init_sensor_framework();
void start_sensor_framework();
void exit_sensor_framework();
sensor_obj_t
add_sys_sensor(char *name, char *description, int instance,
uint32 default_interval, void *read_func, void *config_func);
sensor_obj_t
find_sys_sensor(const char *name, int instance);
sensor_obj_t
find_sys_sensor_id(uint32 sensor_id);
void
refresh_read_interval(sensor_obj_t sensor);
void
sensor_cleanup_callback(uint32 module_id);
int
check_sensor_timers();
void
reschedule_sensor_read();
void
init_sensor_framework();
void
start_sensor_framework();
void
exit_sensor_framework();
#endif /* LIB_EXTENSION_RUNTIME_SENSOR_H_ */

View File

@ -19,60 +19,62 @@ static korp_cond cond;
static korp_mutex mutex;
static bool sensor_check_thread_run = true;
void app_mgr_sensor_event_callback(module_data *m_data, bh_message_t msg)
void
app_mgr_sensor_event_callback(module_data *m_data, bh_message_t msg)
{
uint32 argv[3];
wasm_function_inst_t func_onSensorEvent;
bh_assert(SENSOR_EVENT_WASM == bh_message_type(msg));
wasm_data *wasm_app_data = (wasm_data*)m_data->internal_data;
wasm_data *wasm_app_data = (wasm_data *)m_data->internal_data;
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
sensor_event_data_t *payload = (sensor_event_data_t*)
bh_message_payload(msg);
sensor_event_data_t *payload =
(sensor_event_data_t *)bh_message_payload(msg);
if (payload == NULL)
return;
func_onSensorEvent = wasm_runtime_lookup_function(inst, "_on_sensor_event",
"(i32i32i32)");
func_onSensorEvent =
wasm_runtime_lookup_function(inst, "_on_sensor_event", "(i32i32i32)");
if (!func_onSensorEvent)
func_onSensorEvent = wasm_runtime_lookup_function(inst, "on_sensor_event",
"(i32i32i32)");
func_onSensorEvent = wasm_runtime_lookup_function(
inst, "on_sensor_event", "(i32i32i32)");
if (!func_onSensorEvent) {
printf("Cannot find function on_sensor_event\n");
} else {
}
else {
int32 sensor_data_offset;
uint32 sensor_data_len;
if (payload->data_fmt == FMT_ATTR_CONTAINER) {
sensor_data_len = attr_container_get_serialize_length(payload->data);
} else {
sensor_data_len =
attr_container_get_serialize_length(payload->data);
}
else {
printf("Unsupported sensor data format: %d\n", payload->data_fmt);
return;
}
sensor_data_offset = wasm_runtime_module_dup_data(inst, payload->data,
sensor_data_len);
sensor_data_offset =
wasm_runtime_module_dup_data(inst, payload->data, sensor_data_len);
if (sensor_data_offset == 0) {
const char *exception = wasm_runtime_get_exception(inst);
if (exception) {
printf("Got exception running wasm code: %s\n",
exception);
printf("Got exception running wasm code: %s\n", exception);
wasm_runtime_clear_exception(inst);
}
return;
}
argv[0] = payload->sensor_id;
argv[1] = (uint32) sensor_data_offset;
argv[1] = (uint32)sensor_data_offset;
argv[2] = sensor_data_len;
if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onSensorEvent,
3, argv)) {
const char *exception = wasm_runtime_get_exception(inst);
bh_assert(exception);
printf(":Got exception running wasm code: %s\n",
exception);
printf(":Got exception running wasm code: %s\n", exception);
wasm_runtime_clear_exception(inst);
wasm_runtime_module_free(inst, sensor_data_offset);
return;
@ -82,8 +84,8 @@ void app_mgr_sensor_event_callback(module_data *m_data, bh_message_t msg)
}
}
static void thread_sensor_check(void * arg)
static void
thread_sensor_check(void *arg)
{
while (sensor_check_thread_run) {
int ms_to_expiry = check_sensor_timers();
@ -95,46 +97,44 @@ static void thread_sensor_check(void * arg)
}
}
static void cb_wakeup_thread()
static void
cb_wakeup_thread()
{
os_cond_signal(&cond);
}
void set_sensor_reshceduler(void (*callback)());
void
set_sensor_reshceduler(void (*callback)());
void init_sensor_framework()
void
init_sensor_framework()
{
// init the mutext and conditions
os_cond_init(&cond);
os_mutex_init(&mutex);
set_sensor_reshceduler(cb_wakeup_thread);
wasm_register_msg_callback(SENSOR_EVENT_WASM,
app_mgr_sensor_event_callback);
wasm_register_cleanup_callback(sensor_cleanup_callback);
}
void start_sensor_framework()
void
start_sensor_framework()
{
korp_tid tid;
os_thread_create(&tid,
(void *)thread_sensor_check,
NULL,
BH_APPLET_PRESERVED_STACK_SIZE);
os_thread_create(&tid, (void *)thread_sensor_check, NULL,
BH_APPLET_PRESERVED_STACK_SIZE);
}
void exit_sensor_framework()
void
exit_sensor_framework()
{
sensor_check_thread_run = false;
reschedule_sensor_read();
//todo: wait the sensor thread termination
// todo: wait the sensor thread termination
}

View File

@ -14,16 +14,14 @@ extern "C" {
#endif
bool
wasm_sensor_config(wasm_exec_env_t exec_env,
uint32 sensor, int interval,
wasm_sensor_config(wasm_exec_env_t exec_env, uint32 sensor, int interval,
int bit_cfg, int delay);
uint32
wasm_sensor_open(wasm_exec_env_t exec_env,
char *name, int instance);
wasm_sensor_open(wasm_exec_env_t exec_env, char *name, int instance);
bool
wasm_sensor_config_with_attr_container(wasm_exec_env_t exec_env,
uint32 sensor, char *buffer, int len);
wasm_sensor_config_with_attr_container(wasm_exec_env_t exec_env, uint32 sensor,
char *buffer, int len);
bool
wasm_sensor_close(wasm_exec_env_t exec_env, uint32 sensor);
@ -33,4 +31,3 @@ wasm_sensor_close(wasm_exec_env_t exec_env, uint32 sensor);
#endif
#endif /* end of _SENSOR_NATIVE_API_H_ */