WebAssembly Micro Runtime first version
This commit is contained in:
112
core/iwasm/lib/app-libs/base/bh_platform.c
Normal file
112
core/iwasm/lib/app-libs/base/bh_platform.c
Normal file
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* 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 "bh_platform.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
static bool is_little_endian()
|
||||
{
|
||||
long i = 0x01020304;
|
||||
unsigned char* c = (unsigned char*) &i;
|
||||
return (*c == 0x04) ? true : false;
|
||||
}
|
||||
|
||||
static void swap32(uint8* pData)
|
||||
{
|
||||
uint8 value = *pData;
|
||||
*pData = *(pData + 3);
|
||||
*(pData + 3) = value;
|
||||
|
||||
value = *(pData + 1);
|
||||
*(pData + 1) = *(pData + 2);
|
||||
*(pData + 2) = value;
|
||||
}
|
||||
|
||||
static void swap16(uint8* pData)
|
||||
{
|
||||
uint8 value = *pData;
|
||||
*(pData) = *(pData + 1);
|
||||
*(pData + 1) = value;
|
||||
}
|
||||
|
||||
uint32 htonl(uint32 value)
|
||||
{
|
||||
uint32 ret;
|
||||
if (is_little_endian()) {
|
||||
ret = value;
|
||||
swap32((uint8*) &ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
uint32 ntohl(uint32 value)
|
||||
{
|
||||
return htonl(value);
|
||||
}
|
||||
|
||||
uint16 htons(uint16 value)
|
||||
{
|
||||
uint16 ret;
|
||||
if (is_little_endian()) {
|
||||
ret = value;
|
||||
swap16(&ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
uint16 ntohs(uint16 value)
|
||||
{
|
||||
return htons(value);
|
||||
}
|
||||
|
||||
char *wa_strdup(const char *s)
|
||||
{
|
||||
char *s1 = NULL;
|
||||
if (s && (s1 = wa_malloc(strlen(s) + 1)))
|
||||
memcpy(s1, s, strlen(s) + 1);
|
||||
return s1;
|
||||
}
|
||||
|
||||
#define RSIZE_MAX 0x7FFFFFFF
|
||||
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n)
|
||||
{
|
||||
char *dest = (char*) s1;
|
||||
char *src = (char*) s2;
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (s1 == NULL || s1max > RSIZE_MAX) {
|
||||
return -1;
|
||||
}
|
||||
if (s2 == NULL || n > s1max) {
|
||||
memset(dest, 0, s1max);
|
||||
return -1;
|
||||
}
|
||||
memcpy(dest, src, n);
|
||||
return 0;
|
||||
}
|
||||
59
core/iwasm/lib/app-libs/base/bh_platform.h
Executable file
59
core/iwasm/lib/app-libs/base/bh_platform.h
Executable file
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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 DEPS_IWASM_APP_LIBS_BASE_BH_PLATFORM_H_
|
||||
#define DEPS_IWASM_APP_LIBS_BASE_BH_PLATFORM_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef unsigned char uint8;
|
||||
typedef char int8;
|
||||
typedef unsigned short uint16;
|
||||
typedef short int16;
|
||||
typedef unsigned int uint32;
|
||||
typedef int int32;
|
||||
|
||||
#ifndef NULL
|
||||
# define NULL ((void*) 0)
|
||||
#endif
|
||||
|
||||
#ifndef __cplusplus
|
||||
#define true 1
|
||||
#define false 0
|
||||
#define inline __inline
|
||||
#endif
|
||||
|
||||
// all wasm-app<->native shared source files should use wa_malloc/wa_free.
|
||||
// they will be mapped to different implementations in each side
|
||||
#ifndef wa_malloc
|
||||
#define wa_malloc malloc
|
||||
#endif
|
||||
|
||||
#ifndef wa_free
|
||||
#define wa_free free
|
||||
#endif
|
||||
|
||||
char *wa_strdup(const char *s);
|
||||
|
||||
uint32 htonl(uint32 value);
|
||||
uint32 ntohl(uint32 value);
|
||||
uint16 htons(uint16 value);
|
||||
uint16 ntohs(uint16 value);
|
||||
|
||||
int
|
||||
b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n);
|
||||
|
||||
#endif /* DEPS_IWASM_APP_LIBS_BASE_BH_PLATFORM_H_ */
|
||||
357
core/iwasm/lib/app-libs/base/request.c
Normal file
357
core/iwasm/lib/app-libs/base/request.c
Normal file
@ -0,0 +1,357 @@
|
||||
/*
|
||||
* 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 "attr-container.h"
|
||||
#include "request.h"
|
||||
#include "shared_utils.h"
|
||||
#include "wasm_app.h"
|
||||
|
||||
#define TRANSACTION_TIMEOUT_MS 5000
|
||||
|
||||
typedef enum {
|
||||
Reg_Event, Reg_Request
|
||||
} reg_type_t;
|
||||
|
||||
typedef struct _res_register {
|
||||
struct _res_register *next;
|
||||
const char * url;
|
||||
reg_type_t reg_type;
|
||||
void (*request_handler)(request_t *);
|
||||
} res_register_t;
|
||||
|
||||
typedef struct transaction {
|
||||
struct transaction *next;
|
||||
int mid;
|
||||
unsigned int time; /* start time */
|
||||
response_handler_f handler;
|
||||
void *user_data;
|
||||
} transaction_t;
|
||||
|
||||
static res_register_t * g_resources = NULL;
|
||||
|
||||
static transaction_t *g_transactions = NULL;
|
||||
|
||||
static user_timer_t g_trans_timer = NULL;
|
||||
|
||||
static transaction_t *transaction_find(int mid)
|
||||
{
|
||||
transaction_t *t = g_transactions;
|
||||
|
||||
while (t) {
|
||||
if (t->mid == mid)
|
||||
return t;
|
||||
t = t->next;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* new transaction is added to the tail of the list, so the list
|
||||
* is sorted by expiry time naturally.
|
||||
*/
|
||||
static void transaction_add(transaction_t *trans)
|
||||
{
|
||||
transaction_t *t;
|
||||
|
||||
if (g_transactions == NULL) {
|
||||
g_transactions = trans;
|
||||
return;
|
||||
}
|
||||
|
||||
t = g_transactions;
|
||||
while (t) {
|
||||
if (t->next == NULL) {
|
||||
t->next = trans;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void transaction_remove(transaction_t *trans)
|
||||
{
|
||||
transaction_t *prev = NULL, *current = g_transactions;
|
||||
|
||||
while (current) {
|
||||
if (current == trans) {
|
||||
if (prev == NULL) {
|
||||
g_transactions = current->next;
|
||||
free(current);
|
||||
return;
|
||||
}
|
||||
prev->next = current->next;
|
||||
free(current);
|
||||
return;
|
||||
}
|
||||
prev = current;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
bool is_event_type(request_t * req)
|
||||
{
|
||||
return req->action == COAP_EVENT;
|
||||
}
|
||||
|
||||
static bool register_url_handler(const char *url,
|
||||
request_handler_f request_handler, reg_type_t reg_type)
|
||||
{
|
||||
res_register_t * r = g_resources;
|
||||
|
||||
while (r) {
|
||||
if (reg_type == r->reg_type && strcmp(r->url, url) == 0) {
|
||||
r->request_handler = request_handler;
|
||||
return true;
|
||||
}
|
||||
r = r->next;
|
||||
}
|
||||
|
||||
r = (res_register_t *) malloc(sizeof(res_register_t));
|
||||
if (r == NULL)
|
||||
return false;
|
||||
|
||||
memset(r, 0, sizeof(*r));
|
||||
|
||||
r->url = strdup(url);
|
||||
if (!r->url) {
|
||||
free(r);
|
||||
return false;
|
||||
}
|
||||
|
||||
r->request_handler = request_handler;
|
||||
r->reg_type = reg_type;
|
||||
r->next = g_resources;
|
||||
g_resources = r;
|
||||
|
||||
// tell app mgr to route this url to me
|
||||
if (reg_type == Reg_Request)
|
||||
wasm_register_resource(url);
|
||||
else
|
||||
wasm_sub_event(url);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool api_register_resource_handler(const char *url,
|
||||
request_handler_f request_handler)
|
||||
{
|
||||
return register_url_handler(url, request_handler, Reg_Request);
|
||||
}
|
||||
|
||||
static void transaction_timeout_handler(user_timer_t timer)
|
||||
{
|
||||
transaction_t *cur, *expired = NULL;
|
||||
unsigned int elpased_ms, now = wasm_get_sys_tick_ms();
|
||||
|
||||
/*
|
||||
* Since he transaction list is sorted by expiry time naturally,
|
||||
* we can easily get all expired transactions.
|
||||
* */
|
||||
cur = g_transactions;
|
||||
while (cur) {
|
||||
if (now < cur->time)
|
||||
elpased_ms = now + (0xFFFFFFFF - cur->time) + 1;
|
||||
else
|
||||
elpased_ms = now - cur->time;
|
||||
|
||||
if (elpased_ms >= TRANSACTION_TIMEOUT_MS) {
|
||||
g_transactions = cur->next;
|
||||
cur->next = expired;
|
||||
expired = cur;
|
||||
cur = g_transactions;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* call each transaction's handler with response set to NULL */
|
||||
cur = expired;
|
||||
while (cur) {
|
||||
transaction_t *tmp = cur;
|
||||
cur->handler(NULL, cur->user_data);
|
||||
cur = cur->next;
|
||||
free(tmp);
|
||||
}
|
||||
|
||||
/*
|
||||
* If the transaction list is not empty, restart the timer according
|
||||
* to the first transaction. Otherwise, stop the timer.
|
||||
*/
|
||||
if (g_transactions != NULL) {
|
||||
unsigned int elpased_ms, ms_to_expiry, now = wasm_get_sys_tick_ms();
|
||||
if (now < g_transactions->time) {
|
||||
elpased_ms = now + (0xFFFFFFFF - g_transactions->time) + 1;
|
||||
} else {
|
||||
elpased_ms = now - g_transactions->time;
|
||||
}
|
||||
ms_to_expiry = TRANSACTION_TIMEOUT_MS - elpased_ms;
|
||||
api_timer_restart(g_trans_timer, ms_to_expiry);
|
||||
} else {
|
||||
api_timer_cancel(g_trans_timer);
|
||||
g_trans_timer = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void api_send_request(request_t * request, response_handler_f response_handler,
|
||||
void * user_data)
|
||||
{
|
||||
int size;
|
||||
char *buffer;
|
||||
transaction_t *trans;
|
||||
|
||||
if ((trans = (transaction_t *) malloc(sizeof(transaction_t))) == NULL) {
|
||||
printf(
|
||||
"send request: allocate memory for request transaction failed!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
memset(trans, 0, sizeof(transaction_t));
|
||||
trans->handler = response_handler;
|
||||
trans->mid = request->mid;
|
||||
trans->time = wasm_get_sys_tick_ms();
|
||||
trans->user_data = user_data;
|
||||
|
||||
if ((buffer = pack_request(request, &size)) == NULL) {
|
||||
printf("send request: pack request failed!\n");
|
||||
free(trans);
|
||||
return;
|
||||
}
|
||||
|
||||
transaction_add(trans);
|
||||
|
||||
/* if the trans is the 1st one, start the timer */
|
||||
if (trans == g_transactions) {
|
||||
/* assert(g_trans_timer == NULL); */
|
||||
if (g_trans_timer == NULL) {
|
||||
g_trans_timer = api_timer_create(TRANSACTION_TIMEOUT_MS,
|
||||
false,
|
||||
true, transaction_timeout_handler);
|
||||
}
|
||||
}
|
||||
|
||||
wasm_post_request(buffer, size);
|
||||
|
||||
free_req_resp_packet(buffer);
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* APIs for the native layers to callback for request/response arrived to this app
|
||||
*
|
||||
*/
|
||||
|
||||
void on_response(char * buffer, int size)
|
||||
{
|
||||
response_t response[1];
|
||||
transaction_t *trans;
|
||||
|
||||
if (NULL == unpack_response(buffer, size, response)) {
|
||||
printf("unpack response failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if ((trans = transaction_find(response->mid)) == NULL) {
|
||||
printf("cannot find the transaction\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* When the 1st transaction get response:
|
||||
* 1. If the 2nd trans exist, restart the timer according to its expiry time;
|
||||
* 2. Otherwise, stop the timer since there is no more transactions;
|
||||
*/
|
||||
if (trans == g_transactions) {
|
||||
if (trans->next != NULL) {
|
||||
unsigned int elpased_ms, ms_to_expiry, now = wasm_get_sys_tick_ms();
|
||||
if (now < trans->next->time) {
|
||||
elpased_ms = now + (0xFFFFFFFF - trans->next->time) + 1;
|
||||
} else {
|
||||
elpased_ms = now - trans->next->time;
|
||||
}
|
||||
ms_to_expiry = TRANSACTION_TIMEOUT_MS - elpased_ms;
|
||||
api_timer_restart(g_trans_timer, ms_to_expiry);
|
||||
} else {
|
||||
api_timer_cancel(g_trans_timer);
|
||||
g_trans_timer = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
trans->handler(response, trans->user_data);
|
||||
transaction_remove(trans);
|
||||
}
|
||||
|
||||
void on_request(char *buffer, int size)
|
||||
{
|
||||
request_t request[1];
|
||||
bool is_event;
|
||||
res_register_t *r = g_resources;
|
||||
|
||||
if (NULL == unpack_request(buffer, size, request)) {
|
||||
printf("unpack request failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
is_event = is_event_type(request);
|
||||
|
||||
while (r) {
|
||||
if ((is_event && r->reg_type == Reg_Event)
|
||||
|| (!is_event && r->reg_type == Reg_Request)) {
|
||||
if (check_url_start(request->url, strlen(request->url), r->url)
|
||||
> 0) {
|
||||
r->request_handler(request);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
r = r->next;
|
||||
}
|
||||
|
||||
printf("on_request: exit. no service handler\n");
|
||||
}
|
||||
|
||||
void api_response_send(response_t *response)
|
||||
{
|
||||
int size;
|
||||
char * buffer = pack_response(response, &size);
|
||||
if (buffer == NULL)
|
||||
return;
|
||||
|
||||
wasm_response_send(buffer, size);
|
||||
free_req_resp_packet(buffer);
|
||||
}
|
||||
|
||||
/// event api
|
||||
|
||||
bool api_publish_event(const char *url, int fmt, void *payload, int payload_len)
|
||||
{
|
||||
int size;
|
||||
request_t request[1];
|
||||
init_request(request, url, COAP_EVENT, fmt, payload, payload_len);
|
||||
char * buffer = pack_request(request, &size);
|
||||
if (buffer == NULL)
|
||||
return false;
|
||||
wasm_post_request(buffer, size);
|
||||
|
||||
free_req_resp_packet(buffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool api_subscribe_event(const char * url, request_handler_f handler)
|
||||
{
|
||||
return register_url_handler(url, handler, Reg_Event);
|
||||
}
|
||||
|
||||
49
core/iwasm/lib/app-libs/base/request.h
Normal file
49
core/iwasm/lib/app-libs/base/request.h
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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_REQUEST_H_
|
||||
#define _AEE_REQUEST_H_
|
||||
|
||||
#include "native_interface.h"
|
||||
#include "shared_utils.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
bool is_event_type(request_t * req);
|
||||
|
||||
typedef void (*request_handler_f)(request_t *);
|
||||
typedef void (*response_handler_f)(response_t *, void *);
|
||||
|
||||
// Request APIs
|
||||
bool api_register_resource_handler(const char *url, request_handler_f);
|
||||
void api_send_request(request_t * request, response_handler_f response_handler,
|
||||
void * user_data);
|
||||
|
||||
void api_response_send(response_t *response);
|
||||
|
||||
// event API
|
||||
bool api_publish_event(const char *url, int fmt, void *payload,
|
||||
int payload_len);
|
||||
|
||||
bool api_subscribe_event(const char * url, request_handler_f handler);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
106
core/iwasm/lib/app-libs/base/timer.c
Normal file
106
core/iwasm/lib/app-libs/base/timer.c
Normal file
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* 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 "timer_wasm_app.h"
|
||||
#include "native_interface.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#if 1
|
||||
#include <stdio.h>
|
||||
#else
|
||||
#define printf (...)
|
||||
#endif
|
||||
|
||||
struct user_timer {
|
||||
struct user_timer * next;
|
||||
int timer_id;
|
||||
void (*user_timer_callback)(user_timer_t);
|
||||
};
|
||||
|
||||
struct user_timer * g_timers = NULL;
|
||||
|
||||
user_timer_t api_timer_create(int interval, bool is_period, bool auto_start,
|
||||
void (*on_timer_update)(user_timer_t))
|
||||
{
|
||||
|
||||
int timer_id = wasm_create_timer(interval, is_period, auto_start);
|
||||
|
||||
//TODO
|
||||
struct user_timer * timer = (struct user_timer *) malloc(
|
||||
sizeof(struct user_timer));
|
||||
if (timer == NULL) {
|
||||
// TODO: remove the timer_id
|
||||
printf("### api_timer_create malloc faild!!! \n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(timer, 0, sizeof(*timer));
|
||||
timer->timer_id = timer_id;
|
||||
timer->user_timer_callback = on_timer_update;
|
||||
|
||||
if (g_timers == NULL)
|
||||
g_timers = timer;
|
||||
else {
|
||||
timer->next = g_timers;
|
||||
g_timers = timer;
|
||||
}
|
||||
|
||||
return timer;
|
||||
}
|
||||
|
||||
void api_timer_cancel(user_timer_t timer)
|
||||
{
|
||||
user_timer_t t = g_timers, prev = NULL;
|
||||
|
||||
wasm_timer_cancel(timer->timer_id);
|
||||
|
||||
while (t) {
|
||||
if (t == timer) {
|
||||
if (prev == NULL) {
|
||||
g_timers = t->next;
|
||||
free(t);
|
||||
} else {
|
||||
prev->next = t->next;
|
||||
free(t);
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
prev = t;
|
||||
t = t->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void api_timer_restart(user_timer_t timer, int interval)
|
||||
{
|
||||
wasm_timer_restart(timer->timer_id, interval);
|
||||
}
|
||||
|
||||
void on_timer_callback(int timer_id)
|
||||
{
|
||||
struct user_timer * t = g_timers;
|
||||
|
||||
while (t) {
|
||||
if (t->timer_id == timer_id) {
|
||||
t->user_timer_callback(t);
|
||||
break;
|
||||
}
|
||||
t = t->next;
|
||||
}
|
||||
}
|
||||
|
||||
43
core/iwasm/lib/app-libs/base/timer_wasm_app.h
Normal file
43
core/iwasm/lib/app-libs/base/timer_wasm_app.h
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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_TIMER_H_
|
||||
#define _AEE_TIMER_H_
|
||||
|
||||
#include "bh_platform.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//TODO:
|
||||
#define bh_queue_t void
|
||||
|
||||
/* board producer define user_timer */
|
||||
struct user_timer;
|
||||
typedef struct user_timer * user_timer_t;
|
||||
|
||||
// Timer APIs
|
||||
user_timer_t api_timer_create(int interval, bool is_period, bool auto_start,
|
||||
void (*on_user_timer_update)(user_timer_t));
|
||||
void api_timer_cancel(user_timer_t timer);
|
||||
void api_timer_restart(user_timer_t timer, int interval);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
92
core/iwasm/lib/app-libs/base/wasm_app.h
Normal file
92
core/iwasm/lib/app-libs/base/wasm_app.h
Normal file
@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the Contiki operating system.
|
||||
*/
|
||||
|
||||
#ifndef _LIB_AEE_H_
|
||||
#define _LIB_AEE_H_
|
||||
|
||||
#include "native_interface.h"
|
||||
#include "shared_utils.h"
|
||||
#include "attr-container.h"
|
||||
#include "request.h"
|
||||
#include "sensor.h"
|
||||
#include "timer_wasm_app.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* CoAP request method codes */
|
||||
typedef enum {
|
||||
COAP_GET = 1, COAP_POST, COAP_PUT, COAP_DELETE, COAP_EVENT = (COAP_DELETE
|
||||
+ 2)
|
||||
} coap_method_t;
|
||||
|
||||
/* CoAP response codes */
|
||||
typedef enum {
|
||||
NO_ERROR = 0,
|
||||
|
||||
CREATED_2_01 = 65, /* CREATED */
|
||||
DELETED_2_02 = 66, /* DELETED */
|
||||
VALID_2_03 = 67, /* NOT_MODIFIED */
|
||||
CHANGED_2_04 = 68, /* CHANGED */
|
||||
CONTENT_2_05 = 69, /* OK */
|
||||
CONTINUE_2_31 = 95, /* CONTINUE */
|
||||
|
||||
BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */
|
||||
UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */
|
||||
BAD_OPTION_4_02 = 130, /* BAD_OPTION */
|
||||
FORBIDDEN_4_03 = 131, /* FORBIDDEN */
|
||||
NOT_FOUND_4_04 = 132, /* NOT_FOUND */
|
||||
METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */
|
||||
NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */
|
||||
PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */
|
||||
REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */
|
||||
UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */
|
||||
|
||||
INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */
|
||||
NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */
|
||||
BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */
|
||||
SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */
|
||||
GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */
|
||||
PROXYING_NOT_SUPPORTED_5_05 = 165, /* PROXYING_NOT_SUPPORTED */
|
||||
|
||||
/* Erbium errors */
|
||||
MEMORY_ALLOCATION_ERROR = 192, PACKET_SERIALIZATION_ERROR,
|
||||
|
||||
/* Erbium hooks */
|
||||
MANUAL_RESPONSE, PING_RESPONSE
|
||||
} coap_status_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* end of _LIB_AEE_H_ */
|
||||
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
|
||||
45
core/iwasm/lib/app-libs/libc/lib-base.h
Normal file
45
core/iwasm/lib/app-libs/libc/lib-base.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 _LIB_BASE_H_
|
||||
#define _LIB_BASE_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void *malloc(size_t size);
|
||||
void *calloc(size_t n, size_t size);
|
||||
void free(void *ptr);
|
||||
int memcmp(const void *s1, const void *s2, size_t n);
|
||||
void *memcpy(void *dest, const void *src, size_t n);
|
||||
void *memmove(void *dest, const void *src, size_t n);
|
||||
void *memset(void *s, int c, size_t n);
|
||||
int putchar(int c);
|
||||
int snprintf(char *str, size_t size, const char *format, ...);
|
||||
int sprintf(char *str, const char *format, ...);
|
||||
char *strchr(const char *s, int c);
|
||||
int strcmp(const char *s1, const char *s2);
|
||||
char *strcpy(char *dest, const char *src);
|
||||
size_t strlen(const char *s);
|
||||
int strncmp(const char * str1, const char * str2, size_t n);
|
||||
char *strncpy(char *dest, const char *src, unsigned long n);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user