summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/simple/wasm-apps/sensor.c
blob: c45ff67d9a8bf853de4e3360e84ce79c804b5e10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "wasm_app.h"
#include "wa-inc/sensor.h"

static sensor_t sensor1 = NULL;
static sensor_t sensor2 = NULL;
static char *user_data = NULL;

/* Sensor event callback*/
void
sensor_event_handler(sensor_t sensor, attr_container_t *event, void *user_data)
{
    if (sensor == sensor1) {
        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
on_init()
{
    attr_container_t *config;

    printf("### app on_init 1\n");
    /* open a sensor */
    user_data = malloc(100);
    if (!user_data) {
        printf("allocate memory failed\n");
        return;
    }

    printf("### app on_init 2\n");
    sensor1 = sensor_open("sensor_test1", 0, sensor_event_handler, user_data);
    if (!sensor1) {
        printf("open sensor1 failed\n");
        return;
    }
    /* config the sensor */
    sensor_config(sensor1, 1000, 0, 0);

    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");
    sensor_config(sensor, config);
    attr_container_destroy(config);
    */
}

void
on_destroy()
{
    if (NULL != sensor1) {
        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
       accomplished in wasm app library version of on_destroy() */
}