mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-05-13 21:21:22 +00:00
Fix sensor framework timer issue and update sensor sample (#917)
Fix the sensor framework timer issue reported by #884 when setting `ms_to_next_check`, and unify the type of time related args/vars to uint32 to avoid potential type conversion issues, and fix the compile warnings. And update the sensor sample by creating two sensors to confirm that the fix works correctly.
This commit is contained in:
parent
635084c9b2
commit
98bacfe6bb
|
@ -16,10 +16,10 @@ uint32
|
||||||
wasm_sensor_open(const char *name, int instance);
|
wasm_sensor_open(const char *name, int instance);
|
||||||
|
|
||||||
bool
|
bool
|
||||||
wasm_sensor_config(uint32 sensor, int interval, int bit_cfg, int delay);
|
wasm_sensor_config(uint32 sensor, uint32 interval, int bit_cfg, uint32 delay);
|
||||||
|
|
||||||
bool
|
bool
|
||||||
wasm_sensor_config_with_attr_container(uint32 sensor, char *buffer, int len);
|
wasm_sensor_config_with_attr_container(uint32 sensor, char *buffer, uint32 len);
|
||||||
|
|
||||||
bool
|
bool
|
||||||
wasm_sensor_close(uint32 sensor);
|
wasm_sensor_close(uint32 sensor);
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
#include "bh_platform.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 uint32 g_sensor_id_max = 0;
|
||||||
|
|
||||||
static sensor_client_t *
|
static sensor_client_t *
|
||||||
find_sensor_client(sys_sensor_t *sensor, unsigned int client_id,
|
find_sensor_client(sys_sensor_t *sensor, unsigned int client_id,
|
||||||
|
@ -85,8 +85,8 @@ wasm_sensor_callback(void *client, uint32 sensor_id, void *user_data)
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
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, uint32 interval,
|
||||||
int bit_cfg, int delay)
|
int bit_cfg, uint32 delay)
|
||||||
{
|
{
|
||||||
wasm_module_inst_t module_inst = get_module_inst(exec_env);
|
wasm_module_inst_t module_inst = get_module_inst(exec_env);
|
||||||
attr_container_t *attr_cont;
|
attr_container_t *attr_cont;
|
||||||
|
@ -115,9 +115,9 @@ wasm_sensor_config(wasm_exec_env_t exec_env, uint32 sensor, int interval,
|
||||||
|
|
||||||
if (s->config != NULL) {
|
if (s->config != NULL) {
|
||||||
attr_cont = attr_container_create("config sensor");
|
attr_cont = attr_container_create("config sensor");
|
||||||
attr_container_set_int(&attr_cont, "interval", interval);
|
attr_container_set_int(&attr_cont, "interval", (int)interval);
|
||||||
attr_container_set_int(&attr_cont, "bit_cfg", bit_cfg);
|
attr_container_set_int(&attr_cont, "bit_cfg", bit_cfg);
|
||||||
attr_container_set_int(&attr_cont, "delay", delay);
|
attr_container_set_int(&attr_cont, "delay", (int)delay);
|
||||||
s->config(s, attr_cont);
|
s->config(s, attr_cont);
|
||||||
attr_container_destroy(attr_cont);
|
attr_container_destroy(attr_cont);
|
||||||
}
|
}
|
||||||
|
@ -138,7 +138,7 @@ wasm_sensor_open(wasm_exec_env_t exec_env, char *name, int instance)
|
||||||
sensor_client_t *c;
|
sensor_client_t *c;
|
||||||
sys_sensor_t *s = find_sys_sensor(name, instance);
|
sys_sensor_t *s = find_sys_sensor(name, instance);
|
||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
return -1;
|
return (uint32)-1;
|
||||||
|
|
||||||
unsigned int mod_id =
|
unsigned int mod_id =
|
||||||
app_manager_get_module_id(Module_WASM_App, module_inst);
|
app_manager_get_module_id(Module_WASM_App, module_inst);
|
||||||
|
@ -150,14 +150,14 @@ wasm_sensor_open(wasm_exec_env_t exec_env, char *name, int instance)
|
||||||
if (c) {
|
if (c) {
|
||||||
// the app already opened this sensor
|
// the app already opened this sensor
|
||||||
os_mutex_unlock(&s->lock);
|
os_mutex_unlock(&s->lock);
|
||||||
return -1;
|
return (uint32)-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
sensor_client_t *client =
|
sensor_client_t *client =
|
||||||
(sensor_client_t *)wasm_runtime_malloc(sizeof(sensor_client_t));
|
(sensor_client_t *)wasm_runtime_malloc(sizeof(sensor_client_t));
|
||||||
if (client == NULL) {
|
if (client == NULL) {
|
||||||
os_mutex_unlock(&s->lock);
|
os_mutex_unlock(&s->lock);
|
||||||
return -1;
|
return (uint32)-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(client, 0, sizeof(sensor_client_t));
|
memset(client, 0, sizeof(sensor_client_t));
|
||||||
|
@ -176,7 +176,7 @@ wasm_sensor_open(wasm_exec_env_t exec_env, char *name, int instance)
|
||||||
return s->sensor_id;
|
return s->sensor_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
return (uint32)-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
@ -294,7 +294,7 @@ add_sys_sensor(char *name, char *description, int instance,
|
||||||
}
|
}
|
||||||
|
|
||||||
g_sensor_id_max++;
|
g_sensor_id_max++;
|
||||||
if (g_sensor_id_max == -1)
|
if (g_sensor_id_max == UINT32_MAX)
|
||||||
g_sensor_id_max++;
|
g_sensor_id_max++;
|
||||||
s->sensor_id = g_sensor_id_max;
|
s->sensor_id = g_sensor_id_max;
|
||||||
|
|
||||||
|
@ -366,10 +366,10 @@ find_sensor_client(sys_sensor_t *sensor, unsigned int client_id,
|
||||||
}
|
}
|
||||||
|
|
||||||
// return the milliseconds to next check
|
// return the milliseconds to next check
|
||||||
int
|
uint32
|
||||||
check_sensor_timers()
|
check_sensor_timers()
|
||||||
{
|
{
|
||||||
int ms_to_next_check = -1;
|
uint32 ms_to_next_check = UINT32_MAX;
|
||||||
uint32 now = (uint32)bh_get_tick_ms();
|
uint32 now = (uint32)bh_get_tick_ms();
|
||||||
|
|
||||||
sys_sensor_t *s = g_sys_sensors;
|
sys_sensor_t *s = g_sys_sensors;
|
||||||
|
@ -395,12 +395,12 @@ check_sensor_timers()
|
||||||
|
|
||||||
s->last_read = now;
|
s->last_read = now;
|
||||||
|
|
||||||
if (ms_to_next_check == -1 || (ms_to_next_check < s->read_interval))
|
if (s->read_interval < ms_to_next_check)
|
||||||
ms_to_next_check = s->read_interval;
|
ms_to_next_check = s->read_interval;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
int remaining = s->read_interval - elpased_ms;
|
uint32 remaining = s->read_interval - elpased_ms;
|
||||||
if (ms_to_next_check == -1 || (ms_to_next_check < remaining))
|
if (remaining < ms_to_next_check)
|
||||||
ms_to_next_check = remaining;
|
ms_to_next_check = remaining;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,9 +17,9 @@ typedef struct _sys_sensor *sensor_obj_t;
|
||||||
typedef struct _sensor_client {
|
typedef struct _sensor_client {
|
||||||
struct _sensor_client *next;
|
struct _sensor_client *next;
|
||||||
unsigned int client_id; // the app id
|
unsigned int client_id; // the app id
|
||||||
int interval;
|
uint32 interval;
|
||||||
int bit_cfg;
|
int bit_cfg;
|
||||||
int delay;
|
uint32 delay;
|
||||||
void (*client_callback)(void *client, uint32, attr_container_t *);
|
void (*client_callback)(void *client, uint32, attr_container_t *);
|
||||||
} sensor_client_t;
|
} sensor_client_t;
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ void
|
||||||
refresh_read_interval(sensor_obj_t sensor);
|
refresh_read_interval(sensor_obj_t sensor);
|
||||||
void
|
void
|
||||||
sensor_cleanup_callback(uint32 module_id);
|
sensor_cleanup_callback(uint32 module_id);
|
||||||
int
|
uint32
|
||||||
check_sensor_timers();
|
check_sensor_timers();
|
||||||
void
|
void
|
||||||
reschedule_sensor_read();
|
reschedule_sensor_read();
|
||||||
|
|
|
@ -88,8 +88,8 @@ static void
|
||||||
thread_sensor_check(void *arg)
|
thread_sensor_check(void *arg)
|
||||||
{
|
{
|
||||||
while (sensor_check_thread_run) {
|
while (sensor_check_thread_run) {
|
||||||
int ms_to_expiry = check_sensor_timers();
|
uint32 ms_to_expiry = check_sensor_timers();
|
||||||
if (ms_to_expiry == -1)
|
if (ms_to_expiry == UINT32_MAX)
|
||||||
ms_to_expiry = 5000;
|
ms_to_expiry = 5000;
|
||||||
os_mutex_lock(&mutex);
|
os_mutex_lock(&mutex);
|
||||||
os_cond_reltimedwait(&cond, &mutex, ms_to_expiry * 1000);
|
os_cond_reltimedwait(&cond, &mutex, ms_to_expiry * 1000);
|
||||||
|
|
|
@ -14,8 +14,8 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool
|
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, uint32 interval,
|
||||||
int bit_cfg, int delay);
|
int bit_cfg, uint32 delay);
|
||||||
uint32
|
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);
|
||||||
|
|
||||||
|
|
|
@ -526,8 +526,10 @@ iwasm_main(int argc, char *argv[])
|
||||||
|
|
||||||
/* sensor framework */
|
/* sensor framework */
|
||||||
init_sensor_framework();
|
init_sensor_framework();
|
||||||
// add the sys sensor objects
|
/* add the sys sensor objects */
|
||||||
add_sys_sensor("sensor_test", "This is a sensor for test", 0, 1000,
|
add_sys_sensor("sensor_test1", "This is a sensor for test", 0, 1000,
|
||||||
|
read_test_sensor, config_test_sensor);
|
||||||
|
add_sys_sensor("sensor_test2", "This is a sensor for test", 0, 1000,
|
||||||
read_test_sensor, config_test_sensor);
|
read_test_sensor, config_test_sensor);
|
||||||
start_sensor_framework();
|
start_sensor_framework();
|
||||||
|
|
||||||
|
|
|
@ -6,46 +6,78 @@
|
||||||
#include "wasm_app.h"
|
#include "wasm_app.h"
|
||||||
#include "wa-inc/sensor.h"
|
#include "wa-inc/sensor.h"
|
||||||
|
|
||||||
static sensor_t sensor = NULL;
|
static sensor_t sensor1 = NULL;
|
||||||
|
static sensor_t sensor2 = NULL;
|
||||||
|
static char *user_data = NULL;
|
||||||
|
|
||||||
/* Sensor event callback*/
|
/* Sensor event callback*/
|
||||||
void
|
void
|
||||||
sensor_event_handler(sensor_t sensor, attr_container_t *event, void *user_data)
|
sensor_event_handler(sensor_t sensor, attr_container_t *event, void *user_data)
|
||||||
{
|
{
|
||||||
printf("### app get sensor event\n");
|
if (sensor == sensor1) {
|
||||||
attr_container_dump(event);
|
printf("### app get sensor event from sensor1\n");
|
||||||
|
attr_container_dump(event);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf("### app get sensor event from sensor2\n");
|
||||||
|
attr_container_dump(event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
on_init()
|
on_init()
|
||||||
{
|
{
|
||||||
char *user_data;
|
|
||||||
attr_container_t *config;
|
attr_container_t *config;
|
||||||
|
|
||||||
printf("### app on_init 1\n");
|
printf("### app on_init 1\n");
|
||||||
/* open a sensor */
|
/* open a sensor */
|
||||||
user_data = malloc(100);
|
user_data = malloc(100);
|
||||||
|
if (!user_data) {
|
||||||
|
printf("allocate memory failed\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
printf("### app on_init 2\n");
|
printf("### app on_init 2\n");
|
||||||
sensor = sensor_open("sensor_test", 0, sensor_event_handler, user_data);
|
sensor1 = sensor_open("sensor_test1", 0, sensor_event_handler, user_data);
|
||||||
printf("### app on_init 3\n");
|
if (!sensor1) {
|
||||||
|
printf("open sensor1 failed\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
/* config the sensor */
|
/* config the sensor */
|
||||||
sensor_config(sensor, 1000, 0, 0);
|
sensor_config(sensor1, 1000, 0, 0);
|
||||||
printf("### app on_init 4\n");
|
|
||||||
|
|
||||||
|
printf("### app on_init 3\n");
|
||||||
|
sensor2 = sensor_open("sensor_test2", 0, sensor_event_handler, user_data);
|
||||||
|
if (!sensor2) {
|
||||||
|
printf("open sensor2 failed\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/* config the sensor */
|
||||||
|
sensor_config(sensor2, 5000, 0, 0);
|
||||||
|
|
||||||
|
printf("### app on_init 4\n");
|
||||||
/*
|
/*
|
||||||
config = attr_container_create("sensor config");
|
config = attr_container_create("sensor config");
|
||||||
sensor_config(sensor, config);
|
sensor_config(sensor, config);
|
||||||
attr_container_destroy(config);
|
attr_container_destroy(config);
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
on_destroy()
|
on_destroy()
|
||||||
{
|
{
|
||||||
if (NULL != sensor) {
|
if (NULL != sensor1) {
|
||||||
sensor_config(sensor, 0, 0, 0);
|
sensor_config(sensor1, 0, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (NULL != sensor2) {
|
||||||
|
sensor_config(sensor2, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (NULL != user_data) {
|
||||||
|
free(user_data);
|
||||||
|
}
|
||||||
|
|
||||||
/* real destroy work including killing timer and closing sensor is
|
/* real destroy work including killing timer and closing sensor is
|
||||||
accomplished in wasm app library version of on_destroy() */
|
accomplished in wasm app library version of on_destroy() */
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user