Skip to content

Commit 98bacfe

Browse files
authored
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.
1 parent 635084c commit 98bacfe

File tree

7 files changed

+74
-40
lines changed

7 files changed

+74
-40
lines changed

core/app-framework/sensor/app/sensor_api.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ uint32
1616
wasm_sensor_open(const char *name, int instance);
1717

1818
bool
19-
wasm_sensor_config(uint32 sensor, int interval, int bit_cfg, int delay);
19+
wasm_sensor_config(uint32 sensor, uint32 interval, int bit_cfg, uint32 delay);
2020

2121
bool
22-
wasm_sensor_config_with_attr_container(uint32 sensor, char *buffer, int len);
22+
wasm_sensor_config_with_attr_container(uint32 sensor, char *buffer, uint32 len);
2323

2424
bool
2525
wasm_sensor_close(uint32 sensor);

core/app-framework/sensor/native/runtime_sensor.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "bh_platform.h"
1010

1111
static sys_sensor_t *g_sys_sensors = NULL;
12-
static int g_sensor_id_max = 0;
12+
static uint32 g_sensor_id_max = 0;
1313

1414
static sensor_client_t *
1515
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)
8585
}
8686

8787
bool
88-
wasm_sensor_config(wasm_exec_env_t exec_env, uint32 sensor, int interval,
89-
int bit_cfg, int delay)
88+
wasm_sensor_config(wasm_exec_env_t exec_env, uint32 sensor, uint32 interval,
89+
int bit_cfg, uint32 delay)
9090
{
9191
wasm_module_inst_t module_inst = get_module_inst(exec_env);
9292
attr_container_t *attr_cont;
@@ -115,9 +115,9 @@ wasm_sensor_config(wasm_exec_env_t exec_env, uint32 sensor, int interval,
115115

116116
if (s->config != NULL) {
117117
attr_cont = attr_container_create("config sensor");
118-
attr_container_set_int(&attr_cont, "interval", interval);
118+
attr_container_set_int(&attr_cont, "interval", (int)interval);
119119
attr_container_set_int(&attr_cont, "bit_cfg", bit_cfg);
120-
attr_container_set_int(&attr_cont, "delay", delay);
120+
attr_container_set_int(&attr_cont, "delay", (int)delay);
121121
s->config(s, attr_cont);
122122
attr_container_destroy(attr_cont);
123123
}
@@ -138,7 +138,7 @@ wasm_sensor_open(wasm_exec_env_t exec_env, char *name, int instance)
138138
sensor_client_t *c;
139139
sys_sensor_t *s = find_sys_sensor(name, instance);
140140
if (s == NULL)
141-
return -1;
141+
return (uint32)-1;
142142

143143
unsigned int mod_id =
144144
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)
150150
if (c) {
151151
// the app already opened this sensor
152152
os_mutex_unlock(&s->lock);
153-
return -1;
153+
return (uint32)-1;
154154
}
155155

156156
sensor_client_t *client =
157157
(sensor_client_t *)wasm_runtime_malloc(sizeof(sensor_client_t));
158158
if (client == NULL) {
159159
os_mutex_unlock(&s->lock);
160-
return -1;
160+
return (uint32)-1;
161161
}
162162

163163
memset(client, 0, sizeof(sensor_client_t));
@@ -176,7 +176,7 @@ wasm_sensor_open(wasm_exec_env_t exec_env, char *name, int instance)
176176
return s->sensor_id;
177177
}
178178

179-
return -1;
179+
return (uint32)-1;
180180
}
181181

182182
bool
@@ -294,7 +294,7 @@ add_sys_sensor(char *name, char *description, int instance,
294294
}
295295

296296
g_sensor_id_max++;
297-
if (g_sensor_id_max == -1)
297+
if (g_sensor_id_max == UINT32_MAX)
298298
g_sensor_id_max++;
299299
s->sensor_id = g_sensor_id_max;
300300

@@ -366,10 +366,10 @@ find_sensor_client(sys_sensor_t *sensor, unsigned int client_id,
366366
}
367367

368368
// return the milliseconds to next check
369-
int
369+
uint32
370370
check_sensor_timers()
371371
{
372-
int ms_to_next_check = -1;
372+
uint32 ms_to_next_check = UINT32_MAX;
373373
uint32 now = (uint32)bh_get_tick_ms();
374374

375375
sys_sensor_t *s = g_sys_sensors;
@@ -395,12 +395,12 @@ check_sensor_timers()
395395

396396
s->last_read = now;
397397

398-
if (ms_to_next_check == -1 || (ms_to_next_check < s->read_interval))
398+
if (s->read_interval < ms_to_next_check)
399399
ms_to_next_check = s->read_interval;
400400
}
401401
else {
402-
int remaining = s->read_interval - elpased_ms;
403-
if (ms_to_next_check == -1 || (ms_to_next_check < remaining))
402+
uint32 remaining = s->read_interval - elpased_ms;
403+
if (remaining < ms_to_next_check)
404404
ms_to_next_check = remaining;
405405
}
406406

core/app-framework/sensor/native/runtime_sensor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ typedef struct _sys_sensor *sensor_obj_t;
1717
typedef struct _sensor_client {
1818
struct _sensor_client *next;
1919
unsigned int client_id; // the app id
20-
int interval;
20+
uint32 interval;
2121
int bit_cfg;
22-
int delay;
22+
uint32 delay;
2323
void (*client_callback)(void *client, uint32, attr_container_t *);
2424
} sensor_client_t;
2525

@@ -54,7 +54,7 @@ void
5454
refresh_read_interval(sensor_obj_t sensor);
5555
void
5656
sensor_cleanup_callback(uint32 module_id);
57-
int
57+
uint32
5858
check_sensor_timers();
5959
void
6060
reschedule_sensor_read();

core/app-framework/sensor/native/sensor_mgr_ref.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ static void
8888
thread_sensor_check(void *arg)
8989
{
9090
while (sensor_check_thread_run) {
91-
int ms_to_expiry = check_sensor_timers();
92-
if (ms_to_expiry == -1)
91+
uint32 ms_to_expiry = check_sensor_timers();
92+
if (ms_to_expiry == UINT32_MAX)
9393
ms_to_expiry = 5000;
9494
os_mutex_lock(&mutex);
9595
os_cond_reltimedwait(&cond, &mutex, ms_to_expiry * 1000);

core/app-framework/sensor/native/sensor_native_api.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ extern "C" {
1414
#endif
1515

1616
bool
17-
wasm_sensor_config(wasm_exec_env_t exec_env, uint32 sensor, int interval,
18-
int bit_cfg, int delay);
17+
wasm_sensor_config(wasm_exec_env_t exec_env, uint32 sensor, uint32 interval,
18+
int bit_cfg, uint32 delay);
1919
uint32
2020
wasm_sensor_open(wasm_exec_env_t exec_env, char *name, int instance);
2121

samples/simple/src/iwasm_main.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,10 @@ iwasm_main(int argc, char *argv[])
526526

527527
/* sensor framework */
528528
init_sensor_framework();
529-
// add the sys sensor objects
530-
add_sys_sensor("sensor_test", "This is a sensor for test", 0, 1000,
529+
/* add the sys sensor objects */
530+
add_sys_sensor("sensor_test1", "This is a sensor for test", 0, 1000,
531+
read_test_sensor, config_test_sensor);
532+
add_sys_sensor("sensor_test2", "This is a sensor for test", 0, 1000,
531533
read_test_sensor, config_test_sensor);
532534
start_sensor_framework();
533535

samples/simple/wasm-apps/sensor.c

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,78 @@
66
#include "wasm_app.h"
77
#include "wa-inc/sensor.h"
88

9-
static sensor_t sensor = NULL;
9+
static sensor_t sensor1 = NULL;
10+
static sensor_t sensor2 = NULL;
11+
static char *user_data = NULL;
1012

1113
/* Sensor event callback*/
1214
void
1315
sensor_event_handler(sensor_t sensor, attr_container_t *event, void *user_data)
1416
{
15-
printf("### app get sensor event\n");
16-
attr_container_dump(event);
17+
if (sensor == sensor1) {
18+
printf("### app get sensor event from sensor1\n");
19+
attr_container_dump(event);
20+
}
21+
else {
22+
printf("### app get sensor event from sensor2\n");
23+
attr_container_dump(event);
24+
}
1725
}
1826

1927
void
2028
on_init()
2129
{
22-
char *user_data;
2330
attr_container_t *config;
2431

2532
printf("### app on_init 1\n");
2633
/* open a sensor */
2734
user_data = malloc(100);
35+
if (!user_data) {
36+
printf("allocate memory failed\n");
37+
return;
38+
}
39+
2840
printf("### app on_init 2\n");
29-
sensor = sensor_open("sensor_test", 0, sensor_event_handler, user_data);
30-
printf("### app on_init 3\n");
41+
sensor1 = sensor_open("sensor_test1", 0, sensor_event_handler, user_data);
42+
if (!sensor1) {
43+
printf("open sensor1 failed\n");
44+
return;
45+
}
46+
/* config the sensor */
47+
sensor_config(sensor1, 1000, 0, 0);
3148

49+
printf("### app on_init 3\n");
50+
sensor2 = sensor_open("sensor_test2", 0, sensor_event_handler, user_data);
51+
if (!sensor2) {
52+
printf("open sensor2 failed\n");
53+
return;
54+
}
3255
/* config the sensor */
33-
sensor_config(sensor, 1000, 0, 0);
34-
printf("### app on_init 4\n");
56+
sensor_config(sensor2, 5000, 0, 0);
3557

58+
printf("### app on_init 4\n");
3659
/*
37-
config = attr_container_create("sensor config");
38-
sensor_config(sensor, config);
39-
attr_container_destroy(config);
40-
*/
60+
config = attr_container_create("sensor config");
61+
sensor_config(sensor, config);
62+
attr_container_destroy(config);
63+
*/
4164
}
4265

4366
void
4467
on_destroy()
4568
{
46-
if (NULL != sensor) {
47-
sensor_config(sensor, 0, 0, 0);
69+
if (NULL != sensor1) {
70+
sensor_config(sensor1, 0, 0, 0);
71+
}
72+
73+
if (NULL != sensor2) {
74+
sensor_config(sensor2, 0, 0, 0);
75+
}
76+
77+
if (NULL != user_data) {
78+
free(user_data);
4879
}
80+
4981
/* real destroy work including killing timer and closing sensor is
5082
accomplished in wasm app library version of on_destroy() */
5183
}

0 commit comments

Comments
 (0)