WebAssembly Micro Runtime first version
This commit is contained in:
130
core/iwasm/lib/app-libs/extension/sensor/sensor.c
Normal file
130
core/iwasm/lib/app-libs/extension/sensor/sensor.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 "sensor.h"
|
||||
#include "native_interface.h"
|
||||
|
||||
typedef struct _sensor {
|
||||
struct _sensor * next;
|
||||
char *name;
|
||||
uint32 handle;
|
||||
void (*sensor_callback)(sensor_t, attr_container_t *, void *);
|
||||
void *user_data;
|
||||
} sensor;
|
||||
|
||||
static sensor_t g_sensors = NULL;
|
||||
|
||||
sensor_t sensor_open(const char* name, int index,
|
||||
void (*sensor_event_handler)(sensor_t, attr_container_t *, void *),
|
||||
void *user_data)
|
||||
{
|
||||
uint32 id = wasm_sensor_open(name, index);
|
||||
if (id == -1)
|
||||
return NULL;
|
||||
|
||||
//create local node for holding the user callback
|
||||
sensor_t sensor = (sensor_t) malloc(sizeof(struct _sensor));
|
||||
if (sensor == NULL)
|
||||
return NULL;
|
||||
|
||||
memset(sensor, 0, sizeof(struct _sensor));
|
||||
sensor->handle = id;
|
||||
sensor->name = strdup(name);
|
||||
sensor->user_data = user_data;
|
||||
sensor->sensor_callback = sensor_event_handler;
|
||||
|
||||
if (!sensor->name) {
|
||||
free(sensor);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (g_sensors == NULL) {
|
||||
g_sensors = sensor;
|
||||
} else {
|
||||
sensor->next = g_sensors;
|
||||
g_sensors = sensor;
|
||||
}
|
||||
|
||||
return sensor;
|
||||
}
|
||||
|
||||
bool sensor_config_with_attr_container(sensor_t sensor, attr_container_t *cfg)
|
||||
{
|
||||
char * buffer;
|
||||
int len;
|
||||
|
||||
bool ret = wasm_sensor_config_with_attr_container(sensor->handle, buffer,
|
||||
len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool sensor_config(sensor_t sensor, int interval, int bit_cfg, int delay)
|
||||
{
|
||||
bool ret = wasm_sensor_config(sensor->handle, interval, bit_cfg, delay);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool sensor_close(sensor_t sensor)
|
||||
{
|
||||
|
||||
wasm_sensor_close(sensor->handle);
|
||||
|
||||
// remove local node
|
||||
sensor_t s = g_sensors;
|
||||
sensor_t prev = NULL;
|
||||
while (s) {
|
||||
if (s == sensor) {
|
||||
if (prev == NULL) {
|
||||
g_sensors = s->next;
|
||||
} else {
|
||||
prev->next = s->next;
|
||||
}
|
||||
free(s->name);
|
||||
free(s);
|
||||
return true;
|
||||
} else {
|
||||
prev = s;
|
||||
s = s->next;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* API for native layer to callback for sensor events
|
||||
*
|
||||
*/
|
||||
|
||||
void on_sensor_event(uint32 sensor_id, char * buffer, int len)
|
||||
{
|
||||
attr_container_t * sensor_data = (attr_container_t *) buffer;
|
||||
|
||||
// ??? use buffer or the attributs struct?
|
||||
|
||||
// lookup the sensor and call the handlers
|
||||
sensor_t s = g_sensors;
|
||||
sensor_t prev = NULL;
|
||||
while (s) {
|
||||
if (s->handle == sensor_id) {
|
||||
s->sensor_callback(s, sensor_data, s->user_data);
|
||||
break;
|
||||
}
|
||||
|
||||
s = s->next;
|
||||
}
|
||||
}
|
||||
45
core/iwasm/lib/app-libs/extension/sensor/sensor.h
Normal file
45
core/iwasm/lib/app-libs/extension/sensor/sensor.h
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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 _AEE_SENSOR_H_
|
||||
#define _AEE_SENSOR_H_
|
||||
|
||||
#include "attr-container.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//TODO:
|
||||
#define bh_queue_t void
|
||||
|
||||
/* board producer define sensor */
|
||||
struct _sensor;
|
||||
typedef struct _sensor *sensor_t;
|
||||
|
||||
// Sensor APIs
|
||||
sensor_t sensor_open(const char* name, int index,
|
||||
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);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user