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
|
||||
841
core/iwasm/lib/native-interface/attr-container.c
Normal file
841
core/iwasm/lib/native-interface/attr-container.c
Normal file
@ -0,0 +1,841 @@
|
||||
/*
|
||||
* 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"
|
||||
|
||||
typedef union jvalue {
|
||||
bool z;
|
||||
int8_t b;
|
||||
uint16_t c;
|
||||
int16_t s;
|
||||
int32_t i;
|
||||
int64_t j;
|
||||
float f;
|
||||
double d;
|
||||
} jvalue;
|
||||
|
||||
#define bh_memcpy_s(dest, dlen, src, slen) do { \
|
||||
int _ret = slen == 0 ? 0 : b_memcpy_s (dest, dlen, src, slen); \
|
||||
(void)_ret; \
|
||||
} while (0)
|
||||
|
||||
static inline int16_t get_int16(const char *buf)
|
||||
{
|
||||
int16_t ret;
|
||||
bh_memcpy_s(&ret, sizeof(int16_t), buf, sizeof(int16_t));
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline uint16_t get_uint16(const char *buf)
|
||||
{
|
||||
return get_int16(buf);
|
||||
}
|
||||
|
||||
static inline int32_t get_int32(const char *buf)
|
||||
{
|
||||
int32_t ret;
|
||||
bh_memcpy_s(&ret, sizeof(int32_t), buf, sizeof(int32_t));
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline uint32_t get_uint32(const char *buf)
|
||||
{
|
||||
return get_int32(buf);
|
||||
}
|
||||
|
||||
static inline int64_t get_int64(const char *buf)
|
||||
{
|
||||
int64_t ret;
|
||||
bh_memcpy_s(&ret, sizeof(int64_t), buf, sizeof(int64_t));
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline uint64_t get_uint64(const char *buf)
|
||||
{
|
||||
return get_int64(buf);
|
||||
}
|
||||
|
||||
static inline void set_int16(char *buf, int16_t v)
|
||||
{
|
||||
bh_memcpy_s(buf, sizeof(int16_t), &v, sizeof(int16_t));
|
||||
}
|
||||
|
||||
static inline void set_uint16(char *buf, uint16_t v)
|
||||
{
|
||||
bh_memcpy_s(buf, sizeof(uint16_t), &v, sizeof(uint16_t));
|
||||
}
|
||||
|
||||
static inline void set_int32(char *buf, int32_t v)
|
||||
{
|
||||
bh_memcpy_s(buf, sizeof(int32_t), &v, sizeof(int32_t));
|
||||
}
|
||||
|
||||
static inline void set_uint32(char *buf, uint32_t v)
|
||||
{
|
||||
bh_memcpy_s(buf, sizeof(uint32_t), &v, sizeof(uint32_t));
|
||||
}
|
||||
|
||||
static inline void set_int64(char *buf, int64_t v)
|
||||
{
|
||||
bh_memcpy_s(buf, sizeof(int64_t), &v, sizeof(int64_t));
|
||||
}
|
||||
|
||||
static inline void set_uint64(char *buf, uint64_t v)
|
||||
{
|
||||
bh_memcpy_s(buf, sizeof(uint64_t), &v, sizeof(uint64_t));
|
||||
}
|
||||
|
||||
char*
|
||||
attr_container_get_attr_begin(const attr_container_t *attr_cont,
|
||||
uint32_t *p_total_length, uint16_t *p_attr_num)
|
||||
{
|
||||
char *p = (char*) attr_cont->buf;
|
||||
uint16_t str_len, attr_num;
|
||||
uint32_t total_length;
|
||||
|
||||
/* skip total length */
|
||||
total_length = get_uint32(p);
|
||||
p += sizeof(uint32_t);
|
||||
if (!total_length)
|
||||
return NULL;
|
||||
|
||||
/* tag length */
|
||||
str_len = get_uint16(p);
|
||||
p += sizeof(uint16_t);
|
||||
if (!str_len)
|
||||
return NULL;
|
||||
|
||||
/* tag content */
|
||||
p += str_len;
|
||||
if (p - attr_cont->buf >= total_length)
|
||||
return NULL;
|
||||
|
||||
/* attribute num */
|
||||
attr_num = get_uint16(p);
|
||||
p += sizeof(uint16_t);
|
||||
if (p - attr_cont->buf >= total_length)
|
||||
return NULL;
|
||||
|
||||
if (p_total_length)
|
||||
*p_total_length = total_length;
|
||||
|
||||
if (p_attr_num)
|
||||
*p_attr_num = attr_num;
|
||||
|
||||
/* first attribute */
|
||||
return p;
|
||||
}
|
||||
|
||||
static char*
|
||||
attr_container_get_attr_next(const char *curr_attr)
|
||||
{
|
||||
char *p = (char*) curr_attr;
|
||||
uint8_t type;
|
||||
|
||||
/* key length and key */
|
||||
p += sizeof(uint16_t) + get_uint16(p);
|
||||
type = *p++;
|
||||
|
||||
/* Short type to Boolean type */
|
||||
if (type >= ATTR_TYPE_SHORT && type <= ATTR_TYPE_BOOLEAN) {
|
||||
p += 1 << (type & 3);
|
||||
return p;
|
||||
}
|
||||
/* String type */
|
||||
else if (type == ATTR_TYPE_STRING) {
|
||||
p += sizeof(uint16_t) + get_uint16(p);
|
||||
return p;
|
||||
}
|
||||
/* ByteArray type */
|
||||
else if (type == ATTR_TYPE_BYTEARRAY) {
|
||||
p += sizeof(uint32_t) + get_uint32(p);
|
||||
return p;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char*
|
||||
attr_container_find_attr(const attr_container_t *attr_cont, const char *key)
|
||||
{
|
||||
uint32_t total_length;
|
||||
uint16_t str_len, attr_num, i;
|
||||
const char *p = attr_cont->buf;
|
||||
|
||||
if (!key)
|
||||
return NULL;
|
||||
|
||||
if (!(p = attr_container_get_attr_begin(attr_cont, &total_length, &attr_num)))
|
||||
return NULL;
|
||||
|
||||
for (i = 0; i < attr_num; i++) {
|
||||
/* key length */
|
||||
if (!(str_len = get_uint16(p)))
|
||||
return NULL;
|
||||
|
||||
if (str_len == strlen(key) + 1
|
||||
&& memcmp(p + sizeof(uint16_t), key, str_len) == 0) {
|
||||
if (p + sizeof(uint16_t) + str_len - attr_cont->buf >= total_length)
|
||||
return NULL;
|
||||
return p;
|
||||
}
|
||||
|
||||
if (!(p = attr_container_get_attr_next(p)))
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char*
|
||||
attr_container_get_attr_end(const attr_container_t *attr_cont)
|
||||
{
|
||||
uint32_t total_length;
|
||||
uint16_t attr_num, i;
|
||||
char *p;
|
||||
|
||||
if (!(p = attr_container_get_attr_begin(attr_cont, &total_length, &attr_num)))
|
||||
return NULL;
|
||||
|
||||
for (i = 0; i < attr_num; i++)
|
||||
if (!(p = attr_container_get_attr_next(p)))
|
||||
return NULL;
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
static char*
|
||||
attr_container_get_msg_end(attr_container_t *attr_cont)
|
||||
{
|
||||
char *p = attr_cont->buf;
|
||||
return p + get_uint32(p);
|
||||
}
|
||||
|
||||
uint16_t attr_container_get_attr_num(const attr_container_t *attr_cont)
|
||||
{
|
||||
uint16_t str_len;
|
||||
/* skip total length */
|
||||
const char *p = attr_cont->buf + sizeof(uint32_t);
|
||||
|
||||
str_len = get_uint16(p);
|
||||
/* skip tag length and tag */
|
||||
p += sizeof(uint16_t) + str_len;
|
||||
|
||||
/* attribute num */
|
||||
return get_uint16(p);
|
||||
}
|
||||
|
||||
static void attr_container_inc_attr_num(attr_container_t *attr_cont)
|
||||
{
|
||||
uint16_t str_len, attr_num;
|
||||
/* skip total length */
|
||||
char *p = attr_cont->buf + sizeof(uint32_t);
|
||||
|
||||
str_len = get_uint16(p);
|
||||
/* skip tag length and tag */
|
||||
p += sizeof(uint16_t) + str_len;
|
||||
|
||||
/* attribute num */
|
||||
attr_num = get_uint16(p) + 1;
|
||||
set_uint16(p, attr_num);
|
||||
}
|
||||
|
||||
attr_container_t *
|
||||
attr_container_create(const char *tag)
|
||||
{
|
||||
attr_container_t *attr_cont;
|
||||
int length, tag_length;
|
||||
char *p;
|
||||
|
||||
tag_length = tag ? strlen(tag) + 1 : 1;
|
||||
length = offsetof(attr_container_t, buf) +
|
||||
/* total length + tag length + tag + reserved 100 bytes */
|
||||
sizeof(uint32_t) + sizeof(uint16_t) + tag_length + 100;
|
||||
|
||||
if (!(attr_cont = attr_container_malloc(length))) {
|
||||
attr_container_printf(
|
||||
"Create attr_container failed: allocate memory failed.\r\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(attr_cont, 0, length);
|
||||
p = attr_cont->buf;
|
||||
|
||||
/* total length */
|
||||
set_uint32(p, length - offsetof(attr_container_t, buf));
|
||||
p += 4;
|
||||
|
||||
/* tag length, tag */
|
||||
set_uint16(p, tag_length);
|
||||
p += 2;
|
||||
if (tag)
|
||||
bh_memcpy_s(p, tag_length, tag, tag_length);
|
||||
|
||||
return attr_cont;
|
||||
}
|
||||
|
||||
void attr_container_destroy(const attr_container_t *attr_cont)
|
||||
{
|
||||
if (attr_cont)
|
||||
attr_container_free((char*) attr_cont);
|
||||
}
|
||||
|
||||
static bool check_set_attr(attr_container_t **p_attr_cont, const char *key)
|
||||
{
|
||||
uint32_t flags;
|
||||
|
||||
if (!p_attr_cont || !*p_attr_cont || !key || strlen(key) == 0) {
|
||||
attr_container_printf(
|
||||
"Set attribute failed: invalid input arguments.\r\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
flags = get_uint32((char*) *p_attr_cont);
|
||||
if (flags & ATTR_CONT_READONLY_SHIFT) {
|
||||
attr_container_printf(
|
||||
"Set attribute failed: attribute container is readonly.\r\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool attr_container_set_attr(attr_container_t **p_attr_cont, const char *key,
|
||||
int type, const void *value, int value_length)
|
||||
{
|
||||
attr_container_t *attr_cont, *attr_cont1;
|
||||
uint16_t str_len;
|
||||
uint32_t total_length, attr_len;
|
||||
char *p, *p1, *attr_end, *msg_end, *attr_buf;
|
||||
|
||||
if (!check_set_attr(p_attr_cont, key)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
attr_cont = *p_attr_cont;
|
||||
p = attr_cont->buf;
|
||||
total_length = get_uint32(p);
|
||||
|
||||
if (!(attr_end = attr_container_get_attr_end(attr_cont))) {
|
||||
attr_container_printf("Set attr failed: get attr end failed.\r\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
msg_end = attr_container_get_msg_end(attr_cont);
|
||||
|
||||
/* key len + key + '\0' + type */
|
||||
attr_len = sizeof(uint16_t) + strlen(key) + 1 + 1;
|
||||
if (type >= ATTR_TYPE_SHORT && type <= ATTR_TYPE_BOOLEAN)
|
||||
attr_len += 1 << (type & 3);
|
||||
else if (type == ATTR_TYPE_STRING)
|
||||
attr_len += sizeof(uint16_t) + value_length;
|
||||
else if (type == ATTR_TYPE_BYTEARRAY)
|
||||
attr_len += sizeof(uint32_t) + value_length;
|
||||
|
||||
if (!(p = attr_buf = attr_container_malloc(attr_len))) {
|
||||
attr_container_printf("Set attr failed: allocate memory failed.\r\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Set the attr buf */
|
||||
str_len = strlen(key) + 1;
|
||||
set_uint16(p, str_len);
|
||||
p += sizeof(uint16_t);
|
||||
bh_memcpy_s(p, str_len, key, str_len);
|
||||
p += str_len;
|
||||
|
||||
*p++ = type;
|
||||
if (type >= ATTR_TYPE_SHORT && type <= ATTR_TYPE_BOOLEAN)
|
||||
bh_memcpy_s(p, 1 << (type & 3), value, 1 << (type & 3));
|
||||
else if (type == ATTR_TYPE_STRING) {
|
||||
set_uint16(p, value_length);
|
||||
p += sizeof(uint16_t);
|
||||
bh_memcpy_s(p, value_length, value, value_length);
|
||||
} else if (type == ATTR_TYPE_BYTEARRAY) {
|
||||
set_uint32(p, value_length);
|
||||
p += sizeof(uint32_t);
|
||||
bh_memcpy_s(p, value_length, value, value_length);
|
||||
}
|
||||
|
||||
if ((p = (char*) attr_container_find_attr(attr_cont, key))) {
|
||||
/* key found */
|
||||
p1 = attr_container_get_attr_next(p);
|
||||
|
||||
if (p1 - p == attr_len) {
|
||||
bh_memcpy_s(p, attr_len, attr_buf, attr_len);
|
||||
attr_container_free(attr_buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (p1 - p + msg_end - attr_end >= attr_len) {
|
||||
memmove(p, p1, attr_end - p1);
|
||||
bh_memcpy_s(p + (attr_end - p1), attr_len, attr_buf, attr_len);
|
||||
attr_container_free(attr_buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
total_length += attr_len + 100;
|
||||
if (!(attr_cont1 = attr_container_malloc(
|
||||
offsetof(attr_container_t, buf) + total_length))) {
|
||||
attr_container_printf(
|
||||
"Set attr failed: allocate memory failed.\r\n");
|
||||
attr_container_free(attr_buf);
|
||||
return false;
|
||||
}
|
||||
|
||||
bh_memcpy_s(attr_cont1, p - (char* )attr_cont, attr_cont,
|
||||
p - (char* )attr_cont);
|
||||
bh_memcpy_s((char* )attr_cont1 + (unsigned )(p - (char* )attr_cont),
|
||||
attr_end - p1, p1, attr_end - p1);
|
||||
bh_memcpy_s(
|
||||
(char* )attr_cont1 + (unsigned )(p - (char* )attr_cont)
|
||||
+ (unsigned )(attr_end - p1), attr_len, attr_buf,
|
||||
attr_len);
|
||||
p = attr_cont1->buf;
|
||||
set_uint32(p, total_length);
|
||||
*p_attr_cont = attr_cont1;
|
||||
/* Free original buffer */
|
||||
attr_container_free(attr_cont);
|
||||
attr_container_free(attr_buf);
|
||||
return true;
|
||||
} else {
|
||||
/* key not found */
|
||||
if (msg_end - attr_end >= attr_len) {
|
||||
bh_memcpy_s(attr_end, msg_end - attr_end, attr_buf, attr_len);
|
||||
attr_container_inc_attr_num(attr_cont);
|
||||
attr_container_free(attr_buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
total_length += attr_len + 100;
|
||||
if (!(attr_cont1 = attr_container_malloc(
|
||||
offsetof(attr_container_t, buf) + total_length))) {
|
||||
attr_container_printf(
|
||||
"Set attr failed: allocate memory failed.\r\n");
|
||||
attr_container_free(attr_buf);
|
||||
return false;
|
||||
}
|
||||
|
||||
bh_memcpy_s(attr_cont1, attr_end - (char* )attr_cont, attr_cont,
|
||||
attr_end - (char* )attr_cont);
|
||||
bh_memcpy_s(
|
||||
(char* )attr_cont1 + (unsigned )(attr_end - (char* )attr_cont),
|
||||
attr_len, attr_buf, attr_len);
|
||||
attr_container_inc_attr_num(attr_cont1);
|
||||
p = attr_cont1->buf;
|
||||
set_uint32(p, total_length);
|
||||
*p_attr_cont = attr_cont1;
|
||||
/* Free original buffer */
|
||||
attr_container_free(attr_cont);
|
||||
attr_container_free(attr_buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool attr_container_set_short(attr_container_t **p_attr_cont, const char *key,
|
||||
short value)
|
||||
{
|
||||
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_SHORT, &value, 2);
|
||||
}
|
||||
|
||||
bool attr_container_set_int(attr_container_t **p_attr_cont, const char *key,
|
||||
int value)
|
||||
{
|
||||
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_INT, &value, 4);
|
||||
}
|
||||
|
||||
bool attr_container_set_int64(attr_container_t **p_attr_cont, const char *key,
|
||||
int64_t value)
|
||||
{
|
||||
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_INT64, &value, 8);
|
||||
}
|
||||
|
||||
bool attr_container_set_byte(attr_container_t **p_attr_cont, const char *key,
|
||||
int8_t value)
|
||||
{
|
||||
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_BYTE, &value, 1);
|
||||
}
|
||||
|
||||
bool attr_container_set_uint16(attr_container_t **p_attr_cont, const char *key,
|
||||
uint16_t value)
|
||||
{
|
||||
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_UINT16, &value,
|
||||
2);
|
||||
}
|
||||
|
||||
bool attr_container_set_float(attr_container_t **p_attr_cont, const char *key,
|
||||
float value)
|
||||
{
|
||||
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_FLOAT, &value, 4);
|
||||
}
|
||||
|
||||
bool attr_container_set_double(attr_container_t **p_attr_cont, const char *key,
|
||||
double value)
|
||||
{
|
||||
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_DOUBLE, &value,
|
||||
8);
|
||||
}
|
||||
|
||||
bool attr_container_set_bool(attr_container_t **p_attr_cont, const char *key,
|
||||
bool value)
|
||||
{
|
||||
int8_t value1 = value ? 1 : 0;
|
||||
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_BOOLEAN, &value1,
|
||||
1);
|
||||
}
|
||||
|
||||
bool attr_container_set_string(attr_container_t **p_attr_cont, const char *key,
|
||||
const char *value)
|
||||
{
|
||||
if (!value) {
|
||||
attr_container_printf("Set attr failed: invald input arguments.\r\n");
|
||||
return false;
|
||||
}
|
||||
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_STRING, value,
|
||||
strlen(value) + 1);;
|
||||
}
|
||||
|
||||
bool attr_container_set_bytearray(attr_container_t **p_attr_cont,
|
||||
const char *key, const int8_t *value, unsigned length)
|
||||
{
|
||||
if (!value) {
|
||||
attr_container_printf("Set attr failed: invald input arguments.\r\n");
|
||||
return false;
|
||||
}
|
||||
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_BYTEARRAY, value,
|
||||
length);;
|
||||
}
|
||||
|
||||
static const char*
|
||||
attr_container_get_attr(const attr_container_t *attr_cont, const char *key)
|
||||
{
|
||||
const char *attr_addr;
|
||||
|
||||
if (!attr_cont || !key) {
|
||||
attr_container_printf(
|
||||
"Get attribute failed: invalid input arguments.\r\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!(attr_addr = attr_container_find_attr(attr_cont, key))) {
|
||||
attr_container_printf("Get attribute failed: lookup key failed.\r\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
/* key len + key + '\0' */
|
||||
return attr_addr + 2 + strlen(key) + 1;
|
||||
}
|
||||
|
||||
#define TEMPLATE_ATTR_BUF_TO_VALUE(attr, key, var_name) do {\
|
||||
jvalue val; \
|
||||
const char *addr = attr_container_get_attr(attr, key); \
|
||||
uint8_t type; \
|
||||
if (!addr) \
|
||||
return 0; \
|
||||
val.j = 0; \
|
||||
type = *(uint8_t*)addr++; \
|
||||
switch (type) { \
|
||||
case ATTR_TYPE_SHORT: \
|
||||
case ATTR_TYPE_INT: \
|
||||
case ATTR_TYPE_INT64: \
|
||||
case ATTR_TYPE_BYTE: \
|
||||
case ATTR_TYPE_UINT16: \
|
||||
case ATTR_TYPE_FLOAT: \
|
||||
case ATTR_TYPE_DOUBLE: \
|
||||
case ATTR_TYPE_BOOLEAN: \
|
||||
bh_memcpy_s(&val, sizeof(val.var_name), addr, 1 << (type & 3)); \
|
||||
break; \
|
||||
case ATTR_TYPE_STRING: \
|
||||
{ \
|
||||
unsigned len= get_uint16(addr); \
|
||||
addr += 2; \
|
||||
if (len > sizeof(val.var_name)) \
|
||||
len = sizeof(val.var_name); \
|
||||
bh_memcpy_s(&val.var_name, sizeof(val.var_name), addr, len); \
|
||||
break; \
|
||||
} \
|
||||
case ATTR_TYPE_BYTEARRAY: \
|
||||
{ \
|
||||
unsigned len= get_uint32(addr); \
|
||||
addr += 4; \
|
||||
if (len > sizeof(val.var_name)) \
|
||||
len = sizeof(val.var_name); \
|
||||
bh_memcpy_s(&val.var_name, sizeof(val.var_name), addr, len); \
|
||||
break; \
|
||||
} \
|
||||
} \
|
||||
return val.var_name; \
|
||||
} while (0)
|
||||
|
||||
short attr_container_get_as_short(const attr_container_t *attr_cont,
|
||||
const char *key)
|
||||
{
|
||||
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, s);
|
||||
}
|
||||
|
||||
int attr_container_get_as_int(const attr_container_t *attr_cont,
|
||||
const char *key)
|
||||
{
|
||||
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, i);
|
||||
}
|
||||
|
||||
int64_t attr_container_get_as_int64(const attr_container_t *attr_cont,
|
||||
const char *key)
|
||||
{
|
||||
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, j);
|
||||
}
|
||||
|
||||
int8_t attr_container_get_as_byte(const attr_container_t *attr_cont,
|
||||
const char *key)
|
||||
{
|
||||
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, b);
|
||||
}
|
||||
|
||||
uint16_t attr_container_get_as_uint16(const attr_container_t *attr_cont,
|
||||
const char *key)
|
||||
{
|
||||
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, s);
|
||||
}
|
||||
|
||||
float attr_container_get_as_float(const attr_container_t *attr_cont,
|
||||
const char *key)
|
||||
{
|
||||
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, f);
|
||||
}
|
||||
|
||||
double attr_container_get_as_double(const attr_container_t *attr_cont,
|
||||
const char *key)
|
||||
{
|
||||
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, d);
|
||||
}
|
||||
|
||||
bool attr_container_get_as_bool(const attr_container_t *attr_cont,
|
||||
const char *key)
|
||||
{
|
||||
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, z);
|
||||
}
|
||||
|
||||
const int8_t*
|
||||
attr_container_get_as_bytearray(const attr_container_t *attr_cont,
|
||||
const char *key, unsigned *array_length)
|
||||
{
|
||||
const char *addr = attr_container_get_attr(attr_cont, key);
|
||||
uint8_t type;
|
||||
uint32_t length;
|
||||
|
||||
if (!addr)
|
||||
return NULL;
|
||||
|
||||
if (!array_length) {
|
||||
attr_container_printf("Get attribute failed: invalid input arguments.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
type = *(uint8_t*) addr++;
|
||||
switch (type) {
|
||||
case ATTR_TYPE_SHORT:
|
||||
case ATTR_TYPE_INT:
|
||||
case ATTR_TYPE_INT64:
|
||||
case ATTR_TYPE_BYTE:
|
||||
case ATTR_TYPE_UINT16:
|
||||
case ATTR_TYPE_FLOAT:
|
||||
case ATTR_TYPE_DOUBLE:
|
||||
case ATTR_TYPE_BOOLEAN:
|
||||
length = 1 << (type & 3);
|
||||
break;
|
||||
case ATTR_TYPE_STRING:
|
||||
length = get_uint16(addr);
|
||||
addr += 2;
|
||||
break;
|
||||
case ATTR_TYPE_BYTEARRAY:
|
||||
length = get_uint32(addr);
|
||||
addr += 4;
|
||||
break;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*array_length = length;
|
||||
return (const int8_t*) addr;
|
||||
}
|
||||
|
||||
char*
|
||||
attr_container_get_as_string(const attr_container_t *attr_cont, const char *key)
|
||||
{
|
||||
unsigned array_length;
|
||||
return (char*) attr_container_get_as_bytearray(attr_cont, key,
|
||||
&array_length);
|
||||
}
|
||||
|
||||
const char*
|
||||
attr_container_get_tag(const attr_container_t *attr_cont)
|
||||
{
|
||||
return attr_cont ?
|
||||
attr_cont->buf + sizeof(uint32_t) + sizeof(uint16_t) : NULL;
|
||||
}
|
||||
|
||||
bool attr_container_contain_key(const attr_container_t *attr_cont,
|
||||
const char *key)
|
||||
{
|
||||
if (!attr_cont || !key || !strlen(key)) {
|
||||
attr_container_printf(
|
||||
"Check contain key failed: invalid input arguments.\r\n");
|
||||
return false;
|
||||
}
|
||||
return attr_container_find_attr(attr_cont, key) ? true : false;
|
||||
}
|
||||
|
||||
unsigned int attr_container_get_serialize_length(
|
||||
const attr_container_t *attr_cont)
|
||||
{
|
||||
const char *p;
|
||||
|
||||
if (!attr_cont) {
|
||||
attr_container_printf(
|
||||
"Get container serialize length failed: invalid input arguments.\r\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
p = attr_cont->buf;
|
||||
return sizeof(uint16_t) + get_uint32(p);
|
||||
}
|
||||
|
||||
bool attr_container_serialize(char *buf, const attr_container_t *attr_cont)
|
||||
{
|
||||
const char *p;
|
||||
uint16_t flags;
|
||||
uint32_t length;
|
||||
|
||||
if (!buf || !attr_cont) {
|
||||
attr_container_printf(
|
||||
"Container serialize failed: invalid input arguments.\r\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
p = attr_cont->buf;
|
||||
length = sizeof(uint16_t) + get_uint32(p);
|
||||
bh_memcpy_s(buf, length, attr_cont, length);
|
||||
/* Set readonly */
|
||||
flags = get_uint16((const char*) attr_cont);
|
||||
set_uint16(buf, flags | (1 << ATTR_CONT_READONLY_SHIFT));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool attr_container_is_constant(const attr_container_t* attr_cont)
|
||||
{
|
||||
uint16_t flags;
|
||||
|
||||
if (!attr_cont) {
|
||||
attr_container_printf(
|
||||
"Container check const: invalid input arguments.\r\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
flags = get_uint16((const char*) attr_cont);
|
||||
return (flags & (1 << ATTR_CONT_READONLY_SHIFT)) ? true : false;
|
||||
}
|
||||
|
||||
void attr_container_dump(const attr_container_t *attr_cont)
|
||||
{
|
||||
uint32_t total_length;
|
||||
uint16_t attr_num, i, type;
|
||||
const char *p, *tag, *key;
|
||||
jvalue value;
|
||||
|
||||
if (!attr_cont)
|
||||
return;
|
||||
|
||||
tag = attr_container_get_tag(attr_cont);
|
||||
if (!tag)
|
||||
return;
|
||||
|
||||
attr_container_printf("Attribute container dump:\n");
|
||||
attr_container_printf("Tag: %s\n", tag);
|
||||
|
||||
p = attr_container_get_attr_begin(attr_cont, &total_length, &attr_num);
|
||||
if (!p)
|
||||
return;
|
||||
|
||||
attr_container_printf("Attribute list:\n");
|
||||
for (i = 0; i < attr_num; i++) {
|
||||
key = p + 2;
|
||||
/* Skip key len and key */
|
||||
p += 2 + get_uint16(p);
|
||||
type = *p++;
|
||||
attr_container_printf(" key: %s", key);
|
||||
|
||||
switch (type) {
|
||||
case ATTR_TYPE_SHORT:
|
||||
bh_memcpy_s(&value.s, sizeof(int16_t), p, sizeof(int16_t));
|
||||
attr_container_printf(", type: short, value: 0x%x\n",
|
||||
value.s & 0xFFFF);
|
||||
p += 2;
|
||||
break;
|
||||
case ATTR_TYPE_INT:
|
||||
bh_memcpy_s(&value.i, sizeof(int32_t), p, sizeof(int32_t));
|
||||
attr_container_printf(", type: int, value: 0x%x\n", value.i);
|
||||
p += 4;
|
||||
break;
|
||||
case ATTR_TYPE_INT64:
|
||||
bh_memcpy_s(&value.j, sizeof(uint64_t), p, sizeof(uint64_t));
|
||||
attr_container_printf(", type: int64, value: 0x%llx\n", value.j);
|
||||
p += 8;
|
||||
break;
|
||||
case ATTR_TYPE_BYTE:
|
||||
bh_memcpy_s(&value.b, 1, p, 1);
|
||||
attr_container_printf(", type: byte, value: 0x%x\n",
|
||||
value.b & 0xFF);
|
||||
p++;
|
||||
break;
|
||||
case ATTR_TYPE_UINT16:
|
||||
bh_memcpy_s(&value.c, sizeof(uint16_t), p, sizeof(uint16_t));
|
||||
attr_container_printf(", type: uint16, value: 0x%x\n", value.c);
|
||||
p += 2;
|
||||
break;
|
||||
case ATTR_TYPE_FLOAT:
|
||||
bh_memcpy_s(&value.f, sizeof(float), p, sizeof(float));
|
||||
attr_container_printf(", type: float, value: %f\n", value.f);
|
||||
p += 4;
|
||||
break;
|
||||
case ATTR_TYPE_DOUBLE:
|
||||
bh_memcpy_s(&value.d, sizeof(double), p, sizeof(double));
|
||||
attr_container_printf(", type: double, value: %f\n", value.d);
|
||||
p += 8;
|
||||
break;
|
||||
case ATTR_TYPE_BOOLEAN:
|
||||
bh_memcpy_s(&value.z, 1, p, 1);
|
||||
attr_container_printf(", type: bool, value: 0x%x\n", value.z);
|
||||
p++;
|
||||
break;
|
||||
case ATTR_TYPE_STRING:
|
||||
attr_container_printf(", type: string, value: %s\n",
|
||||
p + sizeof(uint16_t));
|
||||
p += sizeof(uint16_t) + get_uint16(p);
|
||||
break;
|
||||
case ATTR_TYPE_BYTEARRAY:
|
||||
attr_container_printf(", type: byte array, length: %d\n",
|
||||
get_uint32(p));
|
||||
p += sizeof(uint32_t) + get_uint32(p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
attr_container_printf("\n");
|
||||
}
|
||||
|
||||
436
core/iwasm/lib/native-interface/attr-container.h
Normal file
436
core/iwasm/lib/native-interface/attr-container.h
Normal file
@ -0,0 +1,436 @@
|
||||
/*
|
||||
* 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 _ATTR_CONTAINER_H_
|
||||
#define _ATTR_CONTAINER_H_
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include "bh_platform.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Attribute type */
|
||||
enum {
|
||||
ATTR_TYPE_BEGIN = 1,
|
||||
ATTR_TYPE_SHORT = ATTR_TYPE_BEGIN,
|
||||
ATTR_TYPE_INT,
|
||||
ATTR_TYPE_INT64,
|
||||
ATTR_TYPE_BYTE,
|
||||
ATTR_TYPE_UINT16,
|
||||
ATTR_TYPE_FLOAT,
|
||||
ATTR_TYPE_DOUBLE,
|
||||
ATTR_TYPE_BOOLEAN,
|
||||
ATTR_TYPE_STRING,
|
||||
ATTR_TYPE_BYTEARRAY,
|
||||
ATTR_TYPE_END = ATTR_TYPE_BYTEARRAY
|
||||
};
|
||||
|
||||
#define ATTR_CONT_READONLY_SHIFT 2
|
||||
|
||||
typedef struct attr_container {
|
||||
/* container flag:
|
||||
* bit0, bit1 denote the implemenation algorithm, 00: buffer, 01: link list
|
||||
* bit2 denotes the readonly flag: 1 is readonly and attr cannot be set
|
||||
*/
|
||||
char flags[2];
|
||||
/**
|
||||
* Buffer format
|
||||
* for buffer implementation:
|
||||
* buf length (4 bytes)
|
||||
* tag length (2 bytes)
|
||||
* tag
|
||||
* attr num (2bytes)
|
||||
* attr[0..n-1]:
|
||||
* attr key length (2 bytes)
|
||||
* attr type (1byte)
|
||||
* attr value (length depends on attr type)
|
||||
*/
|
||||
char buf[1];
|
||||
} attr_container_t;
|
||||
|
||||
/**
|
||||
* Create attribute container
|
||||
*
|
||||
* @param tag tag of current attribute container
|
||||
*
|
||||
* @return the created attribute container, NULL if failed
|
||||
*/
|
||||
attr_container_t *
|
||||
attr_container_create(const char *tag);
|
||||
|
||||
/**
|
||||
* Destroy attribute container
|
||||
*
|
||||
* @param attr_cont the attribute container to destroy
|
||||
*/
|
||||
void
|
||||
attr_container_destroy(const attr_container_t *attr_cont);
|
||||
|
||||
/**
|
||||
* Set short attribute in attribute container
|
||||
*
|
||||
* @param p_attr_cont pointer to attribute container to set attribute, and
|
||||
* return the new attribute container if it is re-created
|
||||
* @param key the attribute key
|
||||
* @param value the attribute value
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool
|
||||
attr_container_set_short(attr_container_t **p_attr_cont, const char *key,
|
||||
short value);
|
||||
|
||||
/**
|
||||
* Set int attribute in attribute container
|
||||
*
|
||||
* @param p_attr_cont pointer to attribute container to set attribute, and
|
||||
* return the new attribute container if it is re-created
|
||||
* @param key the attribute key
|
||||
* @param value the attribute value
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool
|
||||
attr_container_set_int(attr_container_t **p_attr_cont, const char *key,
|
||||
int value);
|
||||
|
||||
/**
|
||||
* Set int64 attribute in attribute container
|
||||
*
|
||||
* @param p_attr_cont pointer to attribute container to set attribute, and
|
||||
* return the new attribute container if it is re-created
|
||||
* @param key the attribute key
|
||||
* @param value the attribute value
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool
|
||||
attr_container_set_int64(attr_container_t **p_attr_cont, const char *key,
|
||||
int64_t value);
|
||||
|
||||
/**
|
||||
* Set byte attribute in attribute container
|
||||
*
|
||||
* @param p_attr_cont pointer to attribute container to set attribute, and
|
||||
* return the new attribute container if it is re-created
|
||||
* @param key the attribute key
|
||||
* @param value the attribute value
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool
|
||||
attr_container_set_byte(attr_container_t **p_attr_cont, const char *key,
|
||||
int8_t value);
|
||||
|
||||
/**
|
||||
* Set uint16 attribute in attribute container
|
||||
*
|
||||
* @param p_attr_cont pointer to attribute container to set attribute, and
|
||||
* return the new attribute container if it is re-created
|
||||
* @param key the attribute key
|
||||
* @param value the attribute value
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool
|
||||
attr_container_set_uint16(attr_container_t **p_attr_cont, const char *key,
|
||||
uint16_t value);
|
||||
|
||||
/**
|
||||
* Set float attribute in attribute container
|
||||
*
|
||||
* @param p_attr_cont pointer to attribute container to set attribute, and
|
||||
* return the new attribute container if it is re-created
|
||||
* @param key the attribute key
|
||||
* @param value the attribute value
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool
|
||||
attr_container_set_float(attr_container_t **p_attr_cont, const char *key,
|
||||
float value);
|
||||
|
||||
/**
|
||||
* Set double attribute in attribute container
|
||||
*
|
||||
* @param p_attr_cont pointer to attribute container to set attribute, and
|
||||
* return the new attribute container if it is re-created
|
||||
* @param key the attribute key
|
||||
* @param value the attribute value
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool
|
||||
attr_container_set_double(attr_container_t **p_attr_cont, const char *key,
|
||||
double value);
|
||||
|
||||
/**
|
||||
* Set bool attribute in attribute container
|
||||
*
|
||||
* @param p_attr_cont pointer to attribute container to set attribute, and
|
||||
* return the new attribute container if it is re-created
|
||||
* @param key the attribute key
|
||||
* @param value the attribute value
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool
|
||||
attr_container_set_bool(attr_container_t **p_attr_cont, const char *key,
|
||||
bool value);
|
||||
|
||||
/**
|
||||
* Set string attribute in attribute container
|
||||
*
|
||||
* @param p_attr_cont pointer to attribute container to set attribute, and
|
||||
* return the new attribute container if it is re-created
|
||||
* @param key the attribute key
|
||||
* @param value the attribute value
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool
|
||||
attr_container_set_string(attr_container_t **p_attr_cont, const char *key,
|
||||
const char *value);
|
||||
|
||||
/**
|
||||
* Set bytearray attribute in attribute container
|
||||
*
|
||||
* @param p_attr_cont pointer to attribute container to set attribute, and
|
||||
* return the new attribute container if it is re-created
|
||||
* @param key the attribute key
|
||||
* @param value the bytearray buffer
|
||||
* @param length the bytearray length
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool
|
||||
attr_container_set_bytearray(attr_container_t **p_attr_cont, const char *key,
|
||||
const int8_t *value, unsigned length);
|
||||
|
||||
/**
|
||||
* Get tag of current attribute container
|
||||
*
|
||||
* @param attr_cont the attribute container
|
||||
*
|
||||
* @return tag of current attribute container
|
||||
*/
|
||||
const char*
|
||||
attr_container_get_tag(const attr_container_t *attr_cont);
|
||||
|
||||
/**
|
||||
* Get attribute number of current attribute container
|
||||
*
|
||||
* @param attr_cont the attribute container
|
||||
*
|
||||
* @return attribute number of current attribute container
|
||||
*/
|
||||
uint16_t
|
||||
attr_container_get_attr_num(const attr_container_t *attr_cont);
|
||||
|
||||
/**
|
||||
* Whether the attribute container contains an attribute key.
|
||||
*
|
||||
* @param attr_cont the attribute container
|
||||
* @param key the attribute key
|
||||
*
|
||||
* @return true if key is contained in message, false otherwise
|
||||
*/
|
||||
bool
|
||||
attr_container_contain_key(const attr_container_t *attr_cont, const char *key);
|
||||
|
||||
/**
|
||||
* Get attribute from attribute container and return it as short value,
|
||||
* return 0 if attribute isn't found in message.
|
||||
*
|
||||
* @param attr_cont the attribute container
|
||||
* @param key the attribute key
|
||||
*
|
||||
* @return the short value of the attribute, 0 if key isn't found
|
||||
*/
|
||||
short
|
||||
attr_container_get_as_short(const attr_container_t *attr_cont, const char *key);
|
||||
|
||||
/**
|
||||
* Get attribute from attribute container and return it as int value,
|
||||
* return 0 if attribute isn't found in message.
|
||||
*
|
||||
* @param attr_cont the attribute container
|
||||
* @param key the attribute key
|
||||
*
|
||||
* @return the int value of the attribute, 0 if key isn't found
|
||||
*/
|
||||
int
|
||||
attr_container_get_as_int(const attr_container_t *attr_cont, const char *key);
|
||||
|
||||
/**
|
||||
* Get attribute from attribute container and return it as int64 value,
|
||||
* return 0 if attribute isn't found in attribute container.
|
||||
*
|
||||
* @param attr_cont the attribute container
|
||||
* @param key the attribute key
|
||||
*
|
||||
* @return the long value of the attribute, 0 if key isn't found
|
||||
*/
|
||||
int64_t
|
||||
attr_container_get_as_int64(const attr_container_t *attr_cont, const char *key);
|
||||
|
||||
/**
|
||||
* Get attribute from attribute container and return it as byte value,
|
||||
* return 0 if attribute isn't found in attribute container.
|
||||
*
|
||||
* @param attr_cont the attribute container
|
||||
* @param key the attribute key
|
||||
*
|
||||
* @return the byte value of the attribute, 0 if key isn't found
|
||||
*/
|
||||
int8_t
|
||||
attr_container_get_as_byte(const attr_container_t *attr_cont, const char *key);
|
||||
|
||||
/**
|
||||
* Get attribute from attribute container and return it as uint16 value,
|
||||
* return 0 if attribute isn't found in attribute container.
|
||||
*
|
||||
* @param attr_cont the attribute container
|
||||
* @param key the attribute key
|
||||
*
|
||||
* @return the char value of the attribute, 0 if key isn't found
|
||||
*/
|
||||
uint16_t
|
||||
attr_container_get_as_uint16(const attr_container_t *attr_cont,
|
||||
const char *key);
|
||||
|
||||
/**
|
||||
* Get attribute from attribute container and return it as float value,
|
||||
* return 0 if attribute isn't found in attribute container.
|
||||
*
|
||||
* @param attr_cont the attribute container
|
||||
* @param key the attribute key
|
||||
*
|
||||
* @return the float value of the attribute, 0 if key isn't found
|
||||
*/
|
||||
float
|
||||
attr_container_get_as_float(const attr_container_t *attr_cont, const char *key);
|
||||
|
||||
/**
|
||||
* Get attribute from attribute container and return it as double value,
|
||||
* return 0 if attribute isn't found in attribute container.
|
||||
*
|
||||
* @param attr_cont the attribute container
|
||||
* @param key the attribute key
|
||||
*
|
||||
* @return the double value of the attribute, 0 if key isn't found
|
||||
*/
|
||||
double
|
||||
attr_container_get_as_double(const attr_container_t *attr_cont,
|
||||
const char *key);
|
||||
|
||||
/**
|
||||
* Get attribute from attribute container and return it as bool value,
|
||||
* return false if attribute isn't found in attribute container.
|
||||
*
|
||||
* @param attr_cont the attribute container
|
||||
* @param key the attribute key
|
||||
*
|
||||
* @return the bool value of the attribute, 0 if key isn't found
|
||||
*/
|
||||
bool
|
||||
attr_container_get_as_bool(const attr_container_t *attr_cont, const char *key);
|
||||
|
||||
/**
|
||||
* Get attribute from attribute container and return it as string value,
|
||||
* return NULL if attribute isn't found in attribute container.
|
||||
*
|
||||
* @param attr_cont the attribute container
|
||||
* @param key the attribute key
|
||||
*
|
||||
* @return the string value of the attribute, NULL if key isn't found
|
||||
*/
|
||||
char*
|
||||
attr_container_get_as_string(const attr_container_t *attr_cont,
|
||||
const char *key);
|
||||
|
||||
/**
|
||||
* Get attribute from attribute container and return it as bytearray value,
|
||||
* return 0 if attribute isn't found in attribute container.
|
||||
*
|
||||
* @param attr_cont the attribute container
|
||||
* @param key the attribute key
|
||||
*
|
||||
* @return the bytearray value of the attribute, NULL if key isn't found
|
||||
*/
|
||||
const int8_t*
|
||||
attr_container_get_as_bytearray(const attr_container_t *attr_cont,
|
||||
const char *key, unsigned *array_length);
|
||||
|
||||
/**
|
||||
* Get the buffer size of attribute container
|
||||
*
|
||||
* @param attr_cont the attribute container
|
||||
*
|
||||
* @return the buffer size of attribute container
|
||||
*/
|
||||
unsigned
|
||||
attr_container_get_serialize_length(const attr_container_t *attr_cont);
|
||||
|
||||
/**
|
||||
* Serialize attribute container to a buffer
|
||||
*
|
||||
* @param buf the buffer to receive the serialized data
|
||||
* @param attr_cont the attribute container to be serialized
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool
|
||||
attr_container_serialize(char *buf, const attr_container_t *attr_cont);
|
||||
|
||||
/**
|
||||
* Whether the attribute container is const, or set attribute isn't supported
|
||||
*
|
||||
* @param attr_cont the attribute container
|
||||
*
|
||||
* @return true if const, false otherwise
|
||||
*/
|
||||
bool
|
||||
attr_container_is_constant(const attr_container_t* attr_cont);
|
||||
|
||||
void
|
||||
attr_container_dump(const attr_container_t *attr_cont);
|
||||
|
||||
#ifndef attr_container_malloc
|
||||
#define attr_container_malloc wa_malloc
|
||||
#endif
|
||||
|
||||
#ifndef attr_container_free
|
||||
#define attr_container_free wa_free
|
||||
#endif
|
||||
|
||||
#ifndef attr_container_printf
|
||||
#define attr_container_printf printf
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* end of extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* end of _ATTR_CONTAINER_H_ */
|
||||
|
||||
23
core/iwasm/lib/native-interface/native_interface.cmake
Normal file
23
core/iwasm/lib/native-interface/native_interface.cmake
Normal file
@ -0,0 +1,23 @@
|
||||
# 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.
|
||||
|
||||
set (NATIVE_INTERFACE_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
include_directories(${NATIVE_INTERFACE_DIR})
|
||||
|
||||
|
||||
file (GLOB_RECURSE source_all ${NATIVE_INTERFACE_DIR}/*.c)
|
||||
|
||||
set (NATIVE_INTERFACE_SOURCE ${source_all})
|
||||
|
||||
78
core/iwasm/lib/native-interface/native_interface.h
Normal file
78
core/iwasm/lib/native-interface/native_interface.h
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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_SSG_MICRO_RUNTIME_WASM_POC_APP_LIBS_NATIVE_INTERFACE_NATIVE_INTERFACE_H_
|
||||
#define DEPS_SSG_MICRO_RUNTIME_WASM_POC_APP_LIBS_NATIVE_INTERFACE_NATIVE_INTERFACE_H_
|
||||
|
||||
// note: the bh_plaform.h is the only head file separately
|
||||
// implemented by both [app] and [native] worlds
|
||||
#include "bh_platform.h"
|
||||
|
||||
#define get_module_inst() \
|
||||
wasm_runtime_get_current_module_inst()
|
||||
|
||||
#define validate_app_addr(offset, size) \
|
||||
wasm_runtime_validate_app_addr(module_inst, offset, size)
|
||||
|
||||
#define addr_app_to_native(offset) \
|
||||
wasm_runtime_addr_app_to_native(module_inst, offset)
|
||||
|
||||
#define addr_native_to_app(ptr) \
|
||||
wasm_runtime_addr_native_to_app(module_inst, ptr)
|
||||
|
||||
#define module_malloc(size) \
|
||||
wasm_runtime_module_malloc(module_inst, size)
|
||||
|
||||
#define module_free(offset) \
|
||||
wasm_runtime_module_free(module_inst, offset)
|
||||
|
||||
char *wa_strdup(const char *);
|
||||
|
||||
bool
|
||||
wasm_response_send(int32 buffer_offset, int size);
|
||||
|
||||
void wasm_register_resource(int32 url_offset);
|
||||
|
||||
void wasm_post_request(int32 buffer_offset, int size);
|
||||
|
||||
void wasm_sub_event(int32 url_offset);
|
||||
|
||||
/*
|
||||
* ************* sensor interfaces *************
|
||||
*/
|
||||
|
||||
bool
|
||||
wasm_sensor_config(uint32 sensor, int interval, int bit_cfg, int delay);
|
||||
uint32
|
||||
wasm_sensor_open(int32 name_offset, int instance);
|
||||
bool
|
||||
wasm_sensor_config_with_attr_container(uint32 sensor, int32 buffer_offset,
|
||||
int len);
|
||||
|
||||
bool
|
||||
wasm_sensor_close(uint32 sensor);
|
||||
|
||||
/*
|
||||
* *** timer interface ***
|
||||
*/
|
||||
|
||||
typedef unsigned int timer_id_t;
|
||||
timer_id_t wasm_create_timer(int interval, bool is_period, bool auto_start);
|
||||
void wasm_timer_destory(timer_id_t timer_id);
|
||||
void wasm_timer_cancel(timer_id_t timer_id);
|
||||
void wasm_timer_restart(timer_id_t timer_id, int interval);
|
||||
uint32 wasm_get_sys_tick_ms(void);
|
||||
#endif /* DEPS_SSG_MICRO_RUNTIME_WASM_POC_APP_LIBS_NATIVE_INTERFACE_NATIVE_INTERFACE_H_ */
|
||||
12
core/iwasm/lib/native-interface/readme.txt
Normal file
12
core/iwasm/lib/native-interface/readme.txt
Normal file
@ -0,0 +1,12 @@
|
||||
Attention:
|
||||
=======
|
||||
Only add files are shared by both wasm application and native runtime into this directory!
|
||||
|
||||
The c files are both compiled into the the WASM APP and native runtime.
|
||||
|
||||
native_interface.h
|
||||
=============
|
||||
The interface declaration for the native API which are exposed to the WASM app
|
||||
|
||||
Any API in this file should be incuded with EXPORT_WASM_API() somewhere.
|
||||
|
||||
424
core/iwasm/lib/native-interface/restful_utils.c
Normal file
424
core/iwasm/lib/native-interface/restful_utils.c
Normal file
@ -0,0 +1,424 @@
|
||||
/*
|
||||
* 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 <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "native_interface.h"
|
||||
#include "shared_utils.h"
|
||||
|
||||
/* Serialization of request and response message
|
||||
*
|
||||
* Choices:
|
||||
* We considered a few options:
|
||||
* 1. coap
|
||||
* 2. flatbuffer
|
||||
* 3. cbor
|
||||
* 4. attr-containers of our own
|
||||
* 5. customized serialization for request/response
|
||||
*
|
||||
* Now we choose the #5 mainly because we need to quickly get the URL for dispatching
|
||||
* and sometimes we want to change the URL in the original packet. the request format:
|
||||
* fixed part: version: (1 byte), code (1 byte), fmt(2 byte), mid (4 bytes), sender_id(4 bytes), url_len(2 bytes), payload_len(4bytes)
|
||||
* dynamic part: url (bytes in url_len), payload
|
||||
*
|
||||
* response format:
|
||||
* fixed part: (1 byte), code (1 byte), fmt(2 byte), mid (4 bytes), sender_id(4 bytes), payload_len(4bytes)
|
||||
* dynamic part: payload
|
||||
*/
|
||||
#define REQUES_PACKET_VER 1
|
||||
#define REQUEST_PACKET_FIX_PART_LEN 18
|
||||
#define REQUEST_PACKET_URL_OFFSET REQUEST_PACKET_FIX_PART_LEN
|
||||
#define REQUEST_PACKET_URL_LEN *((uint16*)( (char*) buffer + 12))) //!!! to ensure little endian
|
||||
#define REQUEST_PACKET_PAYLOAD_LEN *((uint32*)( (char*) buffer + 14))) //!!! to ensure little endian
|
||||
#define REQUEST_PACKET_URL(buffer) ((char*) buffer + REQUEST_PACKET_URL_OFFSET)
|
||||
#define REQUEST_PACKET_PAYLOAD(buffer) ((char*) buffer + REQUEST_PACKET_URL_OFFSET + REQUEST_PACKET_URL_LEN(buffer))
|
||||
|
||||
#define RESPONSE_PACKET_FIX_PART_LEN 16
|
||||
|
||||
char * pack_request(request_t *request, int * size)
|
||||
{
|
||||
int url_len = strlen(request->url) + 1;
|
||||
int len = REQUEST_PACKET_FIX_PART_LEN + url_len + request->payload_len;
|
||||
char * packet = (char*) wa_malloc(len);
|
||||
if (packet == NULL)
|
||||
return NULL;
|
||||
|
||||
// TODO: ensure little endian for words and dwords
|
||||
*packet = REQUES_PACKET_VER;
|
||||
*((uint8*) (packet + 1)) = request->action;
|
||||
*((uint16*) (packet + 2)) = htons(request->fmt);
|
||||
*((uint32*) (packet + 4)) = htonl(request->mid);
|
||||
*((uint32*) (packet + 8)) = htonl(request->sender);
|
||||
*((uint16*) (packet + 12)) = htons(url_len);
|
||||
*((uint32*) (packet + 14)) = htonl(request->payload_len);
|
||||
strcpy(packet + REQUEST_PACKET_URL_OFFSET, request->url);
|
||||
memcpy(packet + REQUEST_PACKET_URL_OFFSET + url_len, request->payload,
|
||||
request->payload_len);
|
||||
|
||||
*size = len;
|
||||
return packet;
|
||||
}
|
||||
|
||||
void free_req_resp_packet(char * packet)
|
||||
{
|
||||
wa_free(packet);
|
||||
}
|
||||
|
||||
request_t * unpack_request(char * packet, int size, request_t * request)
|
||||
{
|
||||
if (*packet != REQUES_PACKET_VER) {
|
||||
printf("version fail\n");
|
||||
return NULL;
|
||||
}
|
||||
if (size < REQUEST_PACKET_FIX_PART_LEN) {
|
||||
printf("size error: %d\n", size);
|
||||
return NULL;
|
||||
}
|
||||
uint16 url_len = ntohs(*((uint16*) (packet + 12)));
|
||||
uint32 payload_len = ntohl(*((uint32*) (packet + 14)));
|
||||
|
||||
if (size != ( REQUEST_PACKET_FIX_PART_LEN + url_len + payload_len)) {
|
||||
printf("size error: %d, expect: %d\n", size,
|
||||
REQUEST_PACKET_FIX_PART_LEN + url_len + payload_len);
|
||||
return NULL;
|
||||
}
|
||||
if (*(packet + REQUEST_PACKET_FIX_PART_LEN + url_len - 1) != 0) {
|
||||
printf("url not end with 0\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
request->action = *((uint8*) (packet + 1));
|
||||
request->fmt = ntohs(*((uint16*) (packet + 2)));
|
||||
request->mid = ntohl(*((uint32*) (packet + 4)));
|
||||
request->sender = ntohl(*((uint32*) (packet + 8)));
|
||||
request->payload_len = payload_len;
|
||||
request->url = REQUEST_PACKET_URL(packet);
|
||||
request->payload = packet + REQUEST_PACKET_URL_OFFSET + url_len;
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
char * pack_response(response_t *response, int * size)
|
||||
{
|
||||
int len = RESPONSE_PACKET_FIX_PART_LEN + response->payload_len;
|
||||
char * packet = (char*) wa_malloc(len);
|
||||
if (packet == NULL)
|
||||
return NULL;
|
||||
|
||||
// TODO: ensure little endian for words and dwords
|
||||
*packet = REQUES_PACKET_VER;
|
||||
*((uint8*) (packet + 1)) = response->status;
|
||||
*((uint16*) (packet + 2)) = htons(response->fmt);
|
||||
*((uint32*) (packet + 4)) = htonl(response->mid);
|
||||
*((uint32*) (packet + 8)) = htonl(response->reciever);
|
||||
*((uint32*) (packet + 12)) = htonl(response->payload_len);
|
||||
memcpy(packet + RESPONSE_PACKET_FIX_PART_LEN, response->payload,
|
||||
response->payload_len);
|
||||
|
||||
*size = len;
|
||||
return packet;
|
||||
}
|
||||
|
||||
response_t * unpack_response(char * packet, int size, response_t * response)
|
||||
{
|
||||
if (*packet != REQUES_PACKET_VER)
|
||||
return NULL;
|
||||
if (size < RESPONSE_PACKET_FIX_PART_LEN)
|
||||
return NULL;
|
||||
uint32 payload_len = ntohl(*((uint32*) (packet + 12)));
|
||||
if (size != ( RESPONSE_PACKET_FIX_PART_LEN + payload_len))
|
||||
return NULL;
|
||||
|
||||
response->status = *((uint8*) (packet + 1));
|
||||
response->fmt = ntohs(*((uint16*) (packet + 2)));
|
||||
response->mid = ntohl(*((uint32*) (packet + 4)));
|
||||
response->reciever = ntohl(*((uint32*) (packet + 8)));
|
||||
response->payload_len = payload_len;
|
||||
response->payload = packet + RESPONSE_PACKET_FIX_PART_LEN;
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
request_t *clone_request(request_t *request)
|
||||
{
|
||||
/* deep clone */
|
||||
request_t *req = (request_t *) wa_malloc(sizeof(request_t));
|
||||
if (req == NULL)
|
||||
return NULL;
|
||||
|
||||
memset(req, 0, sizeof(*req));
|
||||
req->action = request->action;
|
||||
req->fmt = request->fmt;
|
||||
req->url = wa_strdup(request->url);
|
||||
req->sender = request->sender;
|
||||
req->mid = request->mid;
|
||||
|
||||
if (req->url == NULL)
|
||||
goto fail;
|
||||
|
||||
req->payload_len = request->payload_len;
|
||||
|
||||
if (request->payload_len) {
|
||||
req->payload = (char *) wa_malloc(request->payload_len);
|
||||
if (!req->payload)
|
||||
goto fail;
|
||||
memcpy(req->payload, request->payload, request->payload_len);
|
||||
} else {
|
||||
// when payload_len is 0, the payload may be used for carrying some handle or integer
|
||||
req->payload = request->payload;
|
||||
}
|
||||
|
||||
return req;
|
||||
|
||||
fail: request_cleaner(req);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void request_cleaner(request_t *request)
|
||||
{
|
||||
if (request->url != NULL)
|
||||
wa_free(request->url);
|
||||
if (request->payload != NULL && request->payload_len > 0)
|
||||
wa_free(request->payload);
|
||||
|
||||
wa_free(request);
|
||||
}
|
||||
|
||||
void response_cleaner(response_t * response)
|
||||
{
|
||||
if (response->payload != NULL && response->payload_len > 0)
|
||||
wa_free(response->payload);
|
||||
|
||||
wa_free(response);
|
||||
}
|
||||
|
||||
response_t * clone_response(response_t * response)
|
||||
{
|
||||
response_t *clone = (response_t *) wa_malloc(sizeof(response_t));
|
||||
if (clone == NULL)
|
||||
return NULL;
|
||||
|
||||
memset(clone, 0, sizeof(*clone));
|
||||
clone->fmt = response->fmt;
|
||||
clone->mid = response->mid;
|
||||
clone->status = response->status;
|
||||
clone->reciever = response->reciever;
|
||||
clone->payload_len = response->payload_len;
|
||||
if (clone->payload_len) {
|
||||
clone->payload = (char *) wa_malloc(response->payload_len);
|
||||
if (!clone->payload)
|
||||
goto fail;
|
||||
memcpy(clone->payload, response->payload, response->payload_len);
|
||||
} else {
|
||||
// when payload_len is 0, the payload may be used for carrying some handle or integer
|
||||
clone->payload = response->payload;
|
||||
}
|
||||
return clone;
|
||||
|
||||
fail: response_cleaner(clone);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
response_t * set_response(response_t * response, int status, int fmt,
|
||||
const char *payload, int payload_len)
|
||||
{
|
||||
response->payload = payload;
|
||||
response->payload_len = payload_len;
|
||||
response->status = status;
|
||||
response->fmt = fmt;
|
||||
return response;
|
||||
}
|
||||
|
||||
response_t * make_response_for_request(request_t * request,
|
||||
response_t * response)
|
||||
{
|
||||
response->mid = request->mid;
|
||||
response->reciever = request->sender;
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
request_t * init_request(request_t * request, char *url, int action, int fmt,
|
||||
void *payload, int payload_len)
|
||||
{
|
||||
static unsigned int mid = 0;
|
||||
request->url = url;
|
||||
request->action = action;
|
||||
request->fmt = fmt;
|
||||
request->payload = payload;
|
||||
request->payload_len = payload_len;
|
||||
request->mid = ++mid;
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
/*
|
||||
check if the "url" is starting with "leading_str"
|
||||
return: 0 - not match; >0 - the offset of matched url, include any "/" at the end
|
||||
notes:
|
||||
1. it ensures the leading_str "/abc" can pass "/abc/cde" and "/abc/, but fail "/ab" and "/abcd".
|
||||
leading_str "/abc/" can pass "/abc"
|
||||
2. it omit the '/' at the first char
|
||||
3. it ensure the leading_str "/abc" can pass "/abc?cde
|
||||
*/
|
||||
|
||||
int check_url_start(const char* url, int url_len, const char * leading_str)
|
||||
{
|
||||
int offset = 0;
|
||||
if (*leading_str == '/')
|
||||
leading_str++;
|
||||
if (url_len > 0 && *url == '/') {
|
||||
url_len--;
|
||||
url++;
|
||||
offset++;
|
||||
}
|
||||
|
||||
int len = strlen(leading_str);
|
||||
if (len == 0)
|
||||
return 0;
|
||||
|
||||
// ensure leading_str not end with "/"
|
||||
if (leading_str[len - 1] == '/') {
|
||||
len--;
|
||||
if (len == 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
// equal length
|
||||
if (url_len == len) {
|
||||
if (memcmp(url, leading_str, url_len) == 0) {
|
||||
return (offset + len);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (url_len < len)
|
||||
return 0;
|
||||
|
||||
else if (memcmp(url, leading_str, len) != 0)
|
||||
return 0;
|
||||
|
||||
else if (url[len] != '/' && url[len] != '?')
|
||||
return 0;
|
||||
else
|
||||
return (offset + len + 1);
|
||||
}
|
||||
|
||||
// * @pattern:
|
||||
// * sample 1: /abcd, match /abcd only
|
||||
// * sample 2: /abcd/ match match "/abcd" and "/abcd/*"
|
||||
// * sample 3: /abcd*, match any url started with "/abcd"
|
||||
// * sample 4: /abcd/*, exclude "/abcd"
|
||||
|
||||
bool match_url(char * pattern, char * matched)
|
||||
{
|
||||
if (*pattern == '/')
|
||||
pattern++;
|
||||
if (*matched == '/')
|
||||
matched++;
|
||||
|
||||
int matched_len = strlen(matched);
|
||||
if (matched_len == 0)
|
||||
return false;
|
||||
|
||||
if (matched[matched_len - 1] == '/') {
|
||||
matched_len--;
|
||||
if (matched_len == 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
int len = strlen(pattern);
|
||||
if (len == 0)
|
||||
return false;
|
||||
|
||||
if (pattern[len - 1] == '/') {
|
||||
len--;
|
||||
if (strncmp(pattern, matched, len) != 0)
|
||||
return false;
|
||||
|
||||
if (len == matched_len)
|
||||
return true;
|
||||
|
||||
if (matched_len > len && matched[len] == '/')
|
||||
return true;
|
||||
|
||||
return false;
|
||||
|
||||
} else if (pattern[len - 1] == '*') {
|
||||
if (pattern[len - 2] == '/') {
|
||||
if (strncmp(pattern, matched, len - 1) == 0)
|
||||
return true;
|
||||
|
||||
else
|
||||
return false;
|
||||
} else {
|
||||
return (strncmp(pattern, matched, len - 1) == 0);
|
||||
}
|
||||
} else {
|
||||
return (strcmp(pattern, matched) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* get the value of the key from following format buffer:
|
||||
* key1=value1;key2=value2;key3=value3
|
||||
*/
|
||||
char * find_key_value(char * buffer, int buffer_len, char * key, char * value,
|
||||
int value_len, char delimiter)
|
||||
{
|
||||
char * p = buffer;
|
||||
int i = 0;
|
||||
int remaining = buffer_len;
|
||||
int key_len = strlen(key);
|
||||
|
||||
char * item_start = p;
|
||||
while (*p != 0 && remaining > 0) {
|
||||
while (*p == ' ' || *p == delimiter) {
|
||||
p++;
|
||||
remaining--;
|
||||
}
|
||||
|
||||
if (remaining <= key_len)
|
||||
return NULL;
|
||||
|
||||
// find the key
|
||||
if (0 == strncmp(p, key, key_len) && p[key_len] == '=') {
|
||||
p += (key_len + 1);
|
||||
remaining -= (key_len + 1);
|
||||
char * v = value;
|
||||
memset(value, 0, value_len);
|
||||
value_len--; // ensure last char is 0
|
||||
while (*p != delimiter && remaining > 0 && value_len > 0) {
|
||||
*v++ = *p++;
|
||||
remaining--;
|
||||
value_len--;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
// goto next key
|
||||
while (*p != delimiter && remaining > 0) {
|
||||
p++;
|
||||
remaining--;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
41
core/iwasm/lib/native-interface/sensor_api.h
Normal file
41
core/iwasm/lib/native-interface/sensor_api.h
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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_NATIVE_INTERFACE_SENSOR_API_H_
|
||||
#define DEPS_IWASM_APP_LIBS_NATIVE_INTERFACE_SENSOR_API_H_
|
||||
#include "bh_platform.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
uint32
|
||||
wasm_sensor_open(const char* name, int instance);
|
||||
|
||||
bool
|
||||
wasm_sensor_config(uint32 sensor, int interval, int bit_cfg, int delay);
|
||||
|
||||
bool
|
||||
wasm_sensor_config_with_attr_container(uint32 sensor, char * buffer, int len);
|
||||
|
||||
bool
|
||||
wasm_sensor_close(uint32 sensor);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* DEPS_IWASM_APP_LIBS_NATIVE_INTERFACE_SENSOR_API_H_ */
|
||||
97
core/iwasm/lib/native-interface/shared_utils.h
Normal file
97
core/iwasm/lib/native-interface/shared_utils.h
Normal file
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* 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_SSG_MICRO_RUNTIME_WASM_POC_APP_LIBS_NATIVE_INTERFACE_SHARED_UTILS_H_
|
||||
#define DEPS_SSG_MICRO_RUNTIME_WASM_POC_APP_LIBS_NATIVE_INTERFACE_SHARED_UTILS_H_
|
||||
|
||||
#include "native_interface.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define FMT_ATTR_CONTAINER 99
|
||||
#define FMT_APP_RAW_BINARY 98
|
||||
|
||||
typedef struct request {
|
||||
// message id
|
||||
uint32 mid;
|
||||
|
||||
// url of the request
|
||||
char *url;
|
||||
|
||||
// action of the request, can be PUT/GET/POST/DELETE
|
||||
int action;
|
||||
|
||||
// payload format, currently only support attr_container_t type
|
||||
int fmt;
|
||||
|
||||
// payload of the request, currently only support attr_container_t type
|
||||
void *payload;
|
||||
|
||||
int payload_len;
|
||||
|
||||
unsigned long sender;
|
||||
} request_t;
|
||||
|
||||
typedef struct response {
|
||||
// message id
|
||||
uint32 mid;
|
||||
|
||||
// status of the response
|
||||
int status;
|
||||
|
||||
// payload format
|
||||
int fmt;
|
||||
|
||||
// payload of the response,
|
||||
void *payload;
|
||||
|
||||
int payload_len;
|
||||
|
||||
unsigned long reciever;
|
||||
} response_t;
|
||||
|
||||
int check_url_start(const char* url, int url_len, const char * leading_str);
|
||||
bool match_url(char * pattern, char * matched);
|
||||
char * find_key_value(char * buffer, int buffer_len, char * key, char * value,
|
||||
int value_len, char delimiter);
|
||||
|
||||
request_t *clone_request(request_t *request);
|
||||
void request_cleaner(request_t *request);
|
||||
|
||||
response_t * clone_response(response_t * response);
|
||||
void response_cleaner(response_t * response);
|
||||
|
||||
response_t * set_response(response_t * response, int status, int fmt,
|
||||
const char *payload, int payload_len);
|
||||
response_t * make_response_for_request(request_t * request,
|
||||
response_t * response);
|
||||
|
||||
request_t * init_request(request_t * request, char *url, int action, int fmt,
|
||||
void *payload, int payload_len);
|
||||
|
||||
char * pack_request(request_t *request, int * size);
|
||||
request_t * unpack_request(char * packet, int size, request_t * request);
|
||||
char * pack_response(response_t *response, int * size);
|
||||
response_t * unpack_response(char * packet, int size, response_t * response);
|
||||
void free_req_resp_packet(char * packet);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* DEPS_SSG_MICRO_RUNTIME_WASM_POC_APP_LIBS_NATIVE_INTERFACE_SHARED_UTILS_H_ */
|
||||
48
core/iwasm/lib/native/base/base-lib-export.c
Normal file
48
core/iwasm/lib/native/base/base-lib-export.c
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "lib-export.h"
|
||||
|
||||
#ifdef WASM_ENABLE_BASE_LIB
|
||||
#include "base-lib-export.h"
|
||||
#endif
|
||||
|
||||
static NativeSymbol extended_native_symbol_defs[] = {
|
||||
/* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to
|
||||
add functions to register. */
|
||||
|
||||
#ifdef WASM_ENABLE_BASE_LIB
|
||||
EXPORT_WASM_API(wasm_register_resource),
|
||||
EXPORT_WASM_API(wasm_response_send),
|
||||
EXPORT_WASM_API(wasm_post_request),
|
||||
EXPORT_WASM_API(wasm_sub_event),
|
||||
EXPORT_WASM_API(wasm_create_timer),
|
||||
EXPORT_WASM_API(wasm_timer_destory),
|
||||
EXPORT_WASM_API(wasm_timer_cancel),
|
||||
EXPORT_WASM_API(wasm_timer_restart),
|
||||
EXPORT_WASM_API(wasm_get_sys_tick_ms),
|
||||
#endif
|
||||
};
|
||||
|
||||
int get_base_lib_export_apis(NativeSymbol **p_base_lib_apis)
|
||||
{
|
||||
*p_base_lib_apis = extended_native_symbol_defs;
|
||||
return sizeof(extended_native_symbol_defs) / sizeof(NativeSymbol);
|
||||
}
|
||||
|
||||
24
core/iwasm/lib/native/base/base-lib-export.h
Normal file
24
core/iwasm/lib/native/base/base-lib-export.h
Normal file
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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 _BASE_LIB_EXPORT_H_
|
||||
#define _BASE_LIB_EXPORT_H_
|
||||
|
||||
#include "attr-container.h"
|
||||
#include "native_interface.h"
|
||||
|
||||
#endif /* end of _BASE_LIB_EXPORT_H_ */
|
||||
|
||||
111
core/iwasm/lib/native/base/request_response.c
Normal file
111
core/iwasm/lib/native/base/request_response.c
Normal file
@ -0,0 +1,111 @@
|
||||
/*
|
||||
* 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 "native_interface.h"
|
||||
#include "app-manager-export.h"
|
||||
#include "coap_ext.h"
|
||||
#include "wasm-export.h"
|
||||
|
||||
extern void module_request_handler(request_t *request, uint32 register_id);
|
||||
|
||||
bool wasm_response_send(int32 buffer_offset, int size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *buffer = NULL;
|
||||
|
||||
if (!validate_app_addr(buffer_offset, size))
|
||||
return false;
|
||||
|
||||
buffer = addr_app_to_native(buffer_offset);
|
||||
|
||||
if (buffer != NULL) {
|
||||
response_t response[1];
|
||||
|
||||
if (NULL == unpack_response(buffer, size, response))
|
||||
return false;
|
||||
|
||||
am_send_response(response);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void wasm_register_resource(int32 url_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *url = NULL;
|
||||
|
||||
if (!validate_app_addr(url_offset, 1))
|
||||
return;
|
||||
|
||||
url = addr_app_to_native(url_offset);
|
||||
|
||||
if (url != NULL) {
|
||||
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
|
||||
am_register_resource(url, module_request_handler, mod_id);
|
||||
}
|
||||
}
|
||||
|
||||
void wasm_post_request(int32 buffer_offset, int size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *buffer = NULL;
|
||||
|
||||
if (!validate_app_addr(buffer_offset, size))
|
||||
return;
|
||||
|
||||
buffer = addr_app_to_native(buffer_offset);
|
||||
|
||||
if (buffer != NULL) {
|
||||
request_t req[1];
|
||||
|
||||
if (!unpack_request(buffer, size, req))
|
||||
return;
|
||||
|
||||
// TODO: add permission check, ensure app can't do harm
|
||||
|
||||
// set sender to help dispatch the response to the sender ap
|
||||
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
|
||||
req->sender = mod_id;
|
||||
|
||||
if (req->action == COAP_EVENT) {
|
||||
am_publish_event(req);
|
||||
return;
|
||||
}
|
||||
|
||||
am_dispatch_request(req);
|
||||
}
|
||||
}
|
||||
|
||||
void wasm_sub_event(int32 url_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *url = NULL;
|
||||
|
||||
if (!validate_app_addr(url_offset, 1))
|
||||
return;
|
||||
|
||||
url = addr_app_to_native(url_offset);
|
||||
|
||||
if (url != NULL) {
|
||||
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
|
||||
|
||||
am_register_event(url, mod_id);
|
||||
}
|
||||
}
|
||||
|
||||
29
core/iwasm/lib/native/base/runtime_lib.h
Normal file
29
core/iwasm/lib/native/base/runtime_lib.h
Normal file
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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_RUNTIME_LIB_H_
|
||||
#define LIB_BASE_RUNTIME_LIB_H_
|
||||
|
||||
#include "native_interface.h"
|
||||
|
||||
#include "runtime_timer.h"
|
||||
|
||||
void init_wasm_timer();
|
||||
timer_ctx_t get_wasm_timer_ctx();
|
||||
timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num);
|
||||
void destory_module_timer_ctx(unsigned int module_id);
|
||||
|
||||
#endif /* LIB_BASE_RUNTIME_LIB_H_ */
|
||||
178
core/iwasm/lib/native/base/timer_wrapper.c
Normal file
178
core/iwasm/lib/native/base/timer_wrapper.c
Normal file
@ -0,0 +1,178 @@
|
||||
/*
|
||||
* 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 "runtime_timer.h"
|
||||
#include "app-manager-export.h"
|
||||
#include "module_wasm_app.h"
|
||||
#include "bh_list.h"
|
||||
#include "bh_thread.h"
|
||||
#include "bh_time.h"
|
||||
|
||||
bh_list g_timer_ctx_list;
|
||||
korp_cond g_timer_ctx_list_cond;
|
||||
korp_mutex g_timer_ctx_list_mutex;
|
||||
typedef struct {
|
||||
bh_list_link l;
|
||||
timer_ctx_t timer_ctx;
|
||||
} timer_ctx_node_t;
|
||||
|
||||
void wasm_timer_callback(timer_id_t id, unsigned int mod_id)
|
||||
{
|
||||
module_data* module = module_data_list_lookup_id(mod_id);
|
||||
if (module == NULL)
|
||||
return;
|
||||
|
||||
// !!! the length parameter must be 0, so the receiver will
|
||||
// not free the payload pointer.
|
||||
bh_post_msg(module->queue, TIMER_EVENT_WASM, (char *) id, 0);
|
||||
}
|
||||
|
||||
///
|
||||
/// why we create a separate link for module timer contexts
|
||||
/// rather than traverse the module list?
|
||||
/// It helps to reduce the lock frequency for the module list.
|
||||
/// Also when we lock the module list and then call the callback for
|
||||
/// timer expire, the callback is request the list lock again for lookup
|
||||
/// the module from module id. It is for avoiding that situation.
|
||||
|
||||
void * thread_modulers_timer_check(void * arg)
|
||||
{
|
||||
|
||||
int ms_to_expiry;
|
||||
while (1) {
|
||||
ms_to_expiry = -1;
|
||||
vm_mutex_lock(&g_timer_ctx_list_mutex);
|
||||
timer_ctx_node_t* elem = (timer_ctx_node_t*) bh_list_first_elem(
|
||||
&g_timer_ctx_list);
|
||||
while (elem) {
|
||||
int next = check_app_timers(elem->timer_ctx);
|
||||
if (next != -1) {
|
||||
if (ms_to_expiry == -1 || ms_to_expiry > next)
|
||||
ms_to_expiry = next;
|
||||
}
|
||||
|
||||
elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
|
||||
}
|
||||
vm_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
|
||||
if (ms_to_expiry == -1)
|
||||
ms_to_expiry = 60 * 1000;
|
||||
vm_mutex_lock(&g_timer_ctx_list_mutex);
|
||||
vm_cond_reltimedwait(&g_timer_ctx_list_cond, &g_timer_ctx_list_mutex,
|
||||
ms_to_expiry);
|
||||
vm_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
void wakeup_modules_timer_thread(timer_ctx_t ctx)
|
||||
{
|
||||
vm_mutex_lock(&g_timer_ctx_list_mutex);
|
||||
vm_cond_signal(&g_timer_ctx_list_cond);
|
||||
vm_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
}
|
||||
|
||||
void init_wasm_timer()
|
||||
{
|
||||
korp_tid tm_tid;
|
||||
bh_list_init(&g_timer_ctx_list);
|
||||
|
||||
vm_cond_init(&g_timer_ctx_list_cond);
|
||||
/* temp solution for: thread_modulers_timer_check thread would recursive lock the mutex */
|
||||
vm_recursive_mutex_init(&g_timer_ctx_list_mutex);
|
||||
|
||||
vm_thread_create(&tm_tid, thread_modulers_timer_check,
|
||||
NULL,
|
||||
BH_APPLET_PRESERVED_STACK_SIZE);
|
||||
}
|
||||
|
||||
timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
|
||||
{
|
||||
timer_ctx_t ctx = create_timer_ctx(wasm_timer_callback,
|
||||
wakeup_modules_timer_thread, prealloc_num, module_id);
|
||||
|
||||
if (ctx == NULL)
|
||||
return NULL;
|
||||
|
||||
timer_ctx_node_t * node = (timer_ctx_node_t*) bh_malloc(
|
||||
sizeof(timer_ctx_node_t));
|
||||
if (node == NULL) {
|
||||
destroy_timer_ctx(ctx);
|
||||
return NULL;
|
||||
}
|
||||
memset(node, 0, sizeof(*node));
|
||||
node->timer_ctx = ctx;
|
||||
|
||||
vm_mutex_lock(&g_timer_ctx_list_mutex);
|
||||
bh_list_insert(&g_timer_ctx_list, node);
|
||||
vm_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
void destory_module_timer_ctx(unsigned int module_id)
|
||||
{
|
||||
vm_mutex_lock(&g_timer_ctx_list_mutex);
|
||||
timer_ctx_node_t* elem = (timer_ctx_node_t*) bh_list_first_elem(
|
||||
&g_timer_ctx_list);
|
||||
while (elem) {
|
||||
if (timer_ctx_get_owner(elem->timer_ctx) == module_id) {
|
||||
bh_list_remove(&g_timer_ctx_list, elem);
|
||||
destroy_timer_ctx(elem->timer_ctx);
|
||||
bh_free(elem);
|
||||
break;
|
||||
}
|
||||
|
||||
elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
|
||||
}
|
||||
vm_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
}
|
||||
|
||||
timer_ctx_t get_wasm_timer_ctx()
|
||||
{
|
||||
module_data * m = app_manager_get_module_data(Module_WASM_App);
|
||||
if (m == NULL)
|
||||
return NULL;
|
||||
return m->timer_ctx;
|
||||
}
|
||||
|
||||
timer_id_t wasm_create_timer(int interval, bool is_period, bool auto_start)
|
||||
{
|
||||
return sys_create_timer(get_wasm_timer_ctx(), interval, is_period,
|
||||
auto_start);
|
||||
}
|
||||
|
||||
void wasm_timer_destory(timer_id_t timer_id)
|
||||
{
|
||||
sys_timer_destory(get_wasm_timer_ctx(), timer_id);
|
||||
}
|
||||
|
||||
void wasm_timer_cancel(timer_id_t timer_id)
|
||||
{
|
||||
sys_timer_cancel(get_wasm_timer_ctx(), timer_id);
|
||||
}
|
||||
|
||||
void wasm_timer_restart(timer_id_t timer_id, int interval)
|
||||
{
|
||||
sys_timer_restart(get_wasm_timer_ctx(), timer_id, interval);
|
||||
}
|
||||
|
||||
extern uint32 get_sys_tick_ms();
|
||||
|
||||
uint32 wasm_get_sys_tick_ms(void)
|
||||
{
|
||||
return (uint32) bh_get_tick_ms();
|
||||
}
|
||||
|
||||
23
core/iwasm/lib/native/base/wasm_lib_base.cmake
Normal file
23
core/iwasm/lib/native/base/wasm_lib_base.cmake
Normal file
@ -0,0 +1,23 @@
|
||||
# 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.
|
||||
|
||||
set (WASM_LIB_BASE_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
include_directories(${WASM_LIB_BASE_DIR})
|
||||
|
||||
|
||||
file (GLOB_RECURSE source_all ${WASM_LIB_BASE_DIR}/*.c)
|
||||
|
||||
set (WASM_LIB_BASE_SOURCE ${source_all})
|
||||
|
||||
425
core/iwasm/lib/native/extension/sensor/runtime_sensor.c
Normal file
425
core/iwasm/lib/native/extension/sensor/runtime_sensor.c
Normal file
@ -0,0 +1,425 @@
|
||||
/*
|
||||
* 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 "runtime_sensor.h"
|
||||
#include "app-manager-export.h"
|
||||
#include "module_wasm_app.h"
|
||||
#include "bh_thread.h"
|
||||
#include "bh_time.h"
|
||||
|
||||
static sys_sensor_t * g_sys_sensors = NULL;
|
||||
static int g_sensor_id_max = 0;
|
||||
static sensor_client_t *find_sensor_client(sys_sensor_t * sensor,
|
||||
unsigned int client_id, bool remove_if_found);
|
||||
|
||||
void (*rechedule_sensor_callback)() = NULL;
|
||||
|
||||
/*
|
||||
* API for the applications to call - don't call it from the runtime
|
||||
*
|
||||
*/
|
||||
|
||||
static void sensor_event_cleaner(sensor_event_data_t *sensor_event)
|
||||
{
|
||||
if (sensor_event->data != NULL) {
|
||||
if (sensor_event->data_fmt == FMT_ATTR_CONTAINER)
|
||||
attr_container_destroy(sensor_event->data);
|
||||
else
|
||||
bh_free(sensor_event->data);
|
||||
}
|
||||
|
||||
bh_free(sensor_event);
|
||||
}
|
||||
|
||||
static void wasm_sensor_callback(void *client, uint32 sensor_id,
|
||||
void *user_data)
|
||||
{
|
||||
attr_container_t *sensor_data = (attr_container_t *) user_data;
|
||||
attr_container_t *sensor_data_clone;
|
||||
int sensor_data_len;
|
||||
sensor_event_data_t *sensor_event;
|
||||
bh_message_t msg;
|
||||
sensor_client_t *c = (sensor_client_t *) client;
|
||||
|
||||
module_data *module = module_data_list_lookup_id(c->client_id);
|
||||
if (module == NULL)
|
||||
return;
|
||||
|
||||
if (sensor_data == NULL)
|
||||
return;
|
||||
|
||||
sensor_data_len = attr_container_get_serialize_length(sensor_data);
|
||||
sensor_data_clone = (attr_container_t *)bh_malloc(sensor_data_len);
|
||||
if (sensor_data_clone == NULL)
|
||||
return;
|
||||
|
||||
/* multiple sensor clients may use/free the sensor data, so make a copy */
|
||||
memcpy(sensor_data_clone, sensor_data, sensor_data_len);
|
||||
|
||||
sensor_event = (sensor_event_data_t *)bh_malloc(sizeof(*sensor_event));
|
||||
if (sensor_event == NULL) {
|
||||
bh_free(sensor_data_clone);
|
||||
return;
|
||||
}
|
||||
|
||||
memset(sensor_event, 0, sizeof(*sensor_event));
|
||||
sensor_event->sensor_id = sensor_id;
|
||||
sensor_event->data = sensor_data_clone;
|
||||
sensor_event->data_fmt = FMT_ATTR_CONTAINER;
|
||||
|
||||
msg = bh_new_msg(SENSOR_EVENT_WASM,
|
||||
sensor_event,
|
||||
sizeof(*sensor_event),
|
||||
sensor_event_cleaner);
|
||||
if (!msg) {
|
||||
sensor_event_cleaner(sensor_event);
|
||||
return;
|
||||
}
|
||||
|
||||
bh_post_msg2(module->queue, msg);
|
||||
}
|
||||
|
||||
bool wasm_sensor_config(uint32 sensor, int interval, int bit_cfg, int delay)
|
||||
{
|
||||
attr_container_t * attr_cont;
|
||||
sensor_client_t * c;
|
||||
sensor_obj_t s = find_sys_sensor_id(sensor);
|
||||
if (s == NULL)
|
||||
return false;
|
||||
|
||||
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
|
||||
|
||||
vm_mutex_lock(&s->lock);
|
||||
|
||||
c = find_sensor_client(s, mod_id, false);
|
||||
if (c == NULL) {
|
||||
vm_mutex_unlock(&s->lock);
|
||||
return false;
|
||||
}
|
||||
|
||||
c->interval = interval;
|
||||
c->bit_cfg = bit_cfg;
|
||||
c->delay = delay;
|
||||
|
||||
vm_mutex_unlock(&s->lock);
|
||||
|
||||
if (s->config != NULL) {
|
||||
attr_cont = attr_container_create("config sensor");
|
||||
attr_container_set_int(&attr_cont, "interval", interval);
|
||||
attr_container_set_int(&attr_cont, "bit_cfg", bit_cfg);
|
||||
attr_container_set_int(&attr_cont, "delay", delay);
|
||||
s->config(s, attr_cont);
|
||||
attr_container_destroy(attr_cont);
|
||||
}
|
||||
|
||||
refresh_read_interval(s);
|
||||
|
||||
reschedule_sensor_read();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32 wasm_sensor_open(int32 name_offset, int instance)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *name = NULL;
|
||||
|
||||
if (!validate_app_addr(name_offset, 1))
|
||||
return -1;
|
||||
|
||||
name = addr_app_to_native(name_offset);
|
||||
|
||||
if (name != NULL) {
|
||||
sensor_client_t *c;
|
||||
sys_sensor_t *s = find_sys_sensor(name, instance);
|
||||
if (s == NULL)
|
||||
return -1;
|
||||
|
||||
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
|
||||
|
||||
vm_mutex_lock(&s->lock);
|
||||
|
||||
c = find_sensor_client(s, mod_id, false);
|
||||
if (c) {
|
||||
// the app already opened this sensor
|
||||
vm_mutex_unlock(&s->lock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
sensor_client_t * client = (sensor_client_t*) bh_malloc(
|
||||
sizeof(sensor_client_t));
|
||||
if (client == NULL) {
|
||||
vm_mutex_unlock(&s->lock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(client, 0, sizeof(sensor_client_t));
|
||||
client->client_id = mod_id;
|
||||
client->client_callback = wasm_sensor_callback;
|
||||
client->interval = s->default_interval;
|
||||
client->next = s->clients;
|
||||
s->clients = client;
|
||||
|
||||
vm_mutex_unlock(&s->lock);
|
||||
|
||||
refresh_read_interval(s);
|
||||
|
||||
reschedule_sensor_read();
|
||||
|
||||
return s->sensor_id;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool wasm_sensor_config_with_attr_container(uint32 sensor, int32 buffer_offset,
|
||||
int len)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *buffer = NULL;
|
||||
|
||||
if (!validate_app_addr(buffer_offset, len))
|
||||
return false;
|
||||
|
||||
buffer = addr_app_to_native(buffer_offset);
|
||||
|
||||
if (buffer != NULL) {
|
||||
attr_container_t * cfg;
|
||||
|
||||
sensor_obj_t s = find_sys_sensor_id(sensor);
|
||||
if (s == NULL)
|
||||
return false;
|
||||
|
||||
if (s->config == NULL)
|
||||
return false;
|
||||
|
||||
return s->config(s, cfg);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool wasm_sensor_close(uint32 sensor)
|
||||
{
|
||||
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
|
||||
unsigned int client_id = mod_id;
|
||||
sensor_obj_t s = find_sys_sensor_id(sensor);
|
||||
sensor_client_t *c;
|
||||
|
||||
if (s == NULL)
|
||||
return false;
|
||||
|
||||
vm_mutex_lock(&s->lock);
|
||||
if ((c = find_sensor_client(s, client_id, true)) != NULL)
|
||||
bh_free(c);
|
||||
vm_mutex_unlock(&s->lock);
|
||||
|
||||
refresh_read_interval(s);
|
||||
|
||||
reschedule_sensor_read();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* sensor framework API - don't expose to the applications
|
||||
*
|
||||
*/
|
||||
void set_sensor_reshceduler(void (*callback)())
|
||||
{
|
||||
rechedule_sensor_callback = callback;
|
||||
}
|
||||
|
||||
// used for other threads to wakeup the sensor read thread
|
||||
void reschedule_sensor_read()
|
||||
{
|
||||
if (rechedule_sensor_callback)
|
||||
rechedule_sensor_callback();
|
||||
}
|
||||
|
||||
void refresh_read_interval(sensor_obj_t sensor)
|
||||
{
|
||||
sensor_client_t *c;
|
||||
uint32 interval = sensor->default_interval;
|
||||
vm_mutex_lock(&sensor->lock);
|
||||
|
||||
c = sensor->clients;
|
||||
if (c)
|
||||
interval = c->interval;
|
||||
|
||||
while (c) {
|
||||
if (c->interval < interval)
|
||||
interval = c->interval;
|
||||
c = c->next;
|
||||
}
|
||||
|
||||
vm_mutex_unlock(&sensor->lock);
|
||||
|
||||
sensor->read_interval = interval;
|
||||
}
|
||||
|
||||
sensor_obj_t add_sys_sensor(char * name, char * description, int instance,
|
||||
uint32 default_interval, void * read_func, void * config_func)
|
||||
{
|
||||
sys_sensor_t * s = (sys_sensor_t *) bh_malloc(sizeof(sys_sensor_t));
|
||||
if (s == NULL)
|
||||
return NULL;
|
||||
|
||||
memset(s, 0, sizeof(*s));
|
||||
s->name = bh_strdup(name);
|
||||
s->sensor_instance = instance;
|
||||
s->default_interval = default_interval;
|
||||
|
||||
if (!s->name) {
|
||||
bh_free(s);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (description) {
|
||||
s->description = bh_strdup(description);
|
||||
if (!s->description) {
|
||||
bh_free(s->name);
|
||||
bh_free(s);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
g_sensor_id_max++;
|
||||
if (g_sensor_id_max == -1)
|
||||
g_sensor_id_max++;
|
||||
s->sensor_id = g_sensor_id_max;
|
||||
|
||||
s->read = read_func;
|
||||
s->config = config_func;
|
||||
|
||||
if (g_sys_sensors == NULL) {
|
||||
g_sys_sensors = s;
|
||||
} else {
|
||||
s->next = g_sys_sensors;
|
||||
g_sys_sensors = s;
|
||||
}
|
||||
|
||||
vm_mutex_init(&s->lock);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
sensor_obj_t find_sys_sensor(const char* name, int instance)
|
||||
{
|
||||
sys_sensor_t * s = g_sys_sensors;
|
||||
while (s) {
|
||||
if (strcmp(s->name, name) == 0 && s->sensor_instance == instance)
|
||||
return s;
|
||||
|
||||
s = s->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sensor_obj_t find_sys_sensor_id(uint32 sensor_id)
|
||||
{
|
||||
sys_sensor_t * s = g_sys_sensors;
|
||||
while (s) {
|
||||
if (s->sensor_id == sensor_id)
|
||||
return s;
|
||||
|
||||
s = s->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sensor_client_t *find_sensor_client(sys_sensor_t * sensor,
|
||||
unsigned int client_id, bool remove_if_found)
|
||||
{
|
||||
sensor_client_t *prev = NULL, *c = sensor->clients;
|
||||
|
||||
while (c) {
|
||||
sensor_client_t *next = c->next;
|
||||
if (c->client_id == client_id) {
|
||||
if (remove_if_found) {
|
||||
if (prev)
|
||||
prev->next = next;
|
||||
else
|
||||
sensor->clients = next;
|
||||
}
|
||||
return c;
|
||||
} else {
|
||||
c = c->next;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// return the milliseconds to next check
|
||||
int check_sensor_timers()
|
||||
{
|
||||
int ms_to_next_check = -1;
|
||||
uint32 now = (uint32) bh_get_tick_ms();
|
||||
|
||||
sys_sensor_t * s = g_sys_sensors;
|
||||
while (s) {
|
||||
uint32 last_read = s->last_read;
|
||||
uint32 elpased_ms = bh_get_elpased_ms(&last_read);
|
||||
|
||||
if (s->read_interval <= 0 || s->clients == NULL) {
|
||||
s = s->next;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (elpased_ms >= s->read_interval) {
|
||||
attr_container_t * data = s->read(s);
|
||||
if (data) {
|
||||
sensor_client_t * client = s->clients;
|
||||
while (client) {
|
||||
client->client_callback(client, s->sensor_id, data);
|
||||
client = client->next;
|
||||
}
|
||||
attr_container_destroy(data);
|
||||
}
|
||||
|
||||
s->last_read = now;
|
||||
|
||||
if (ms_to_next_check == -1 || (ms_to_next_check < s->read_interval))
|
||||
ms_to_next_check = s->read_interval;
|
||||
} else {
|
||||
int remaining = s->read_interval - elpased_ms;
|
||||
if (ms_to_next_check == -1 || (ms_to_next_check < remaining))
|
||||
ms_to_next_check = remaining;
|
||||
|
||||
}
|
||||
|
||||
s = s->next;
|
||||
}
|
||||
|
||||
return ms_to_next_check;
|
||||
}
|
||||
|
||||
void sensor_cleanup_callback(uint32 module_id)
|
||||
{
|
||||
sys_sensor_t * s = g_sys_sensors;
|
||||
|
||||
while (s) {
|
||||
sensor_client_t *c;
|
||||
vm_mutex_lock(&s->lock);
|
||||
if ((c = find_sensor_client(s, module_id, true)) != NULL) {
|
||||
bh_free(c);
|
||||
}
|
||||
vm_mutex_unlock(&s->lock);
|
||||
s = s->next;
|
||||
}
|
||||
}
|
||||
75
core/iwasm/lib/native/extension/sensor/runtime_sensor.h
Normal file
75
core/iwasm/lib/native/extension/sensor/runtime_sensor.h
Normal file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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_EXTENSION_RUNTIME_SENSOR_H_
|
||||
#define LIB_EXTENSION_RUNTIME_SENSOR_H_
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include "attr-container.h"
|
||||
struct _sys_sensor;
|
||||
typedef struct _sys_sensor* sensor_obj_t;
|
||||
|
||||
typedef struct _sensor_client {
|
||||
struct _sensor_client * next;
|
||||
unsigned int client_id; // the app id
|
||||
int interval;
|
||||
int bit_cfg;
|
||||
int delay;
|
||||
void (*client_callback)(void * client, uint32, attr_container_t *);
|
||||
} sensor_client_t;
|
||||
|
||||
typedef struct _sys_sensor {
|
||||
struct _sys_sensor * next;
|
||||
char * name;
|
||||
int sensor_instance;
|
||||
char * description;
|
||||
uint32 sensor_id;
|
||||
sensor_client_t * clients;
|
||||
/* app, sensor mgr and app mgr may access the clients at the same time,
|
||||
* so need a lock to protect the clients */
|
||||
korp_mutex lock;
|
||||
uint32 last_read;
|
||||
uint32 read_interval;
|
||||
uint32 default_interval;
|
||||
|
||||
attr_container_t * (*read)(void *); /* TODO: may support other type return value, such as 'cbor' */
|
||||
bool (*config)(void *, void *);
|
||||
|
||||
} sys_sensor_t;
|
||||
|
||||
sensor_obj_t add_sys_sensor(char * name, char * description, int instance,
|
||||
uint32 default_interval, void * read_func, void * config_func);
|
||||
sensor_obj_t find_sys_sensor(const char* name, int instance);
|
||||
sensor_obj_t find_sys_sensor_id(uint32 sensor_id);
|
||||
void refresh_read_interval(sensor_obj_t sensor);
|
||||
void sensor_cleanup_callback(uint32 module_id);
|
||||
int check_sensor_timers();
|
||||
void reschedule_sensor_read();
|
||||
|
||||
uint32
|
||||
wasm_sensor_open(int32 name_offset, int instance);
|
||||
|
||||
bool
|
||||
wasm_sensor_config(uint32 sensor, int interval, int bit_cfg, int delay);
|
||||
|
||||
bool
|
||||
wasm_sensor_config_with_attr_container(uint32 sensor, int32 buffer_offset,
|
||||
int len);
|
||||
|
||||
bool
|
||||
wasm_sensor_close(uint32 sensor);
|
||||
|
||||
#endif /* LIB_EXTENSION_RUNTIME_SENSOR_H_ */
|
||||
20
core/iwasm/lib/native/extension/sensor/runtime_sensor.inl
Normal file
20
core/iwasm/lib/native/extension/sensor/runtime_sensor.inl
Normal file
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
EXPORT_WASM_API(wasm_sensor_open),
|
||||
EXPORT_WASM_API(wasm_sensor_config),
|
||||
EXPORT_WASM_API(wasm_sensor_config_with_attr_container),
|
||||
EXPORT_WASM_API(wasm_sensor_close),
|
||||
145
core/iwasm/lib/native/extension/sensor/sensor_mgr_ref.c
Normal file
145
core/iwasm/lib/native/extension/sensor/sensor_mgr_ref.c
Normal file
@ -0,0 +1,145 @@
|
||||
/*
|
||||
* 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_common.h"
|
||||
#include "bh_queue.h"
|
||||
#include "bh_thread.h"
|
||||
#include "runtime_sensor.h"
|
||||
#include "attr-container.h"
|
||||
#include "module_wasm_app.h"
|
||||
#include "wasm-export.h"
|
||||
|
||||
/*
|
||||
*
|
||||
* One reference implementation for sensor manager
|
||||
*
|
||||
*
|
||||
*/
|
||||
static korp_cond cond;
|
||||
static korp_mutex mutex;
|
||||
|
||||
void app_mgr_sensor_event_callback(module_data *m_data, bh_message_t msg)
|
||||
{
|
||||
uint32 argv[3];
|
||||
wasm_function_inst_t func_onSensorEvent;
|
||||
|
||||
bh_assert(SENSOR_EVENT_WASM == bh_message_type(msg));
|
||||
wasm_data *wasm_app_data = (wasm_data*) m_data->internal_data;
|
||||
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
|
||||
|
||||
sensor_event_data_t *payload = (sensor_event_data_t*) bh_message_payload(
|
||||
msg);
|
||||
if (payload == NULL)
|
||||
return;
|
||||
|
||||
func_onSensorEvent = wasm_runtime_lookup_function(inst, "_on_sensor_event",
|
||||
"(i32i32i32)");
|
||||
if (!func_onSensorEvent) {
|
||||
printf("Cannot find function onRequest\n");
|
||||
} else {
|
||||
int32 sensor_data_offset;
|
||||
uint32 sensor_data_len;
|
||||
|
||||
if (payload->data_fmt == FMT_ATTR_CONTAINER) {
|
||||
sensor_data_len = attr_container_get_serialize_length(
|
||||
payload->data);
|
||||
} else {
|
||||
printf("Unsupported sensor data format: %d\n", payload->data_fmt);
|
||||
return;
|
||||
}
|
||||
|
||||
sensor_data_offset = wasm_runtime_module_dup_data(inst, payload->data,
|
||||
sensor_data_len);
|
||||
if (sensor_data_offset == 0) {
|
||||
printf("Got exception running wasm code: %s\n",
|
||||
wasm_runtime_get_exception(inst));
|
||||
wasm_runtime_clear_exception(inst);
|
||||
return;
|
||||
}
|
||||
|
||||
argv[0] = payload->sensor_id;
|
||||
argv[1] = (uint32) sensor_data_offset;
|
||||
argv[2] = sensor_data_len;
|
||||
|
||||
if (!wasm_runtime_call_wasm(inst, NULL, func_onSensorEvent, 3, argv)) {
|
||||
printf(":Got exception running wasm code: %s\n",
|
||||
wasm_runtime_get_exception(inst));
|
||||
wasm_runtime_clear_exception(inst);
|
||||
wasm_runtime_module_free(inst, sensor_data_offset);
|
||||
return;
|
||||
}
|
||||
|
||||
wasm_runtime_module_free(inst, sensor_data_offset);
|
||||
}
|
||||
}
|
||||
|
||||
static attr_container_t * read_test_sensor(void * sensor)
|
||||
{
|
||||
//luc: for test
|
||||
attr_container_t *attr_obj = attr_container_create("read test sensor data");
|
||||
if (attr_obj) {
|
||||
attr_container_set_string(&attr_obj, "name", "read test sensor");
|
||||
return attr_obj;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static bool config_test_sensor(void * s, void * config)
|
||||
{
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void * thread_sensor_check(void * arg)
|
||||
{
|
||||
while (1) {
|
||||
int ms_to_expiry = check_sensor_timers();
|
||||
if (ms_to_expiry == -1)
|
||||
ms_to_expiry = 5000;
|
||||
vm_mutex_lock(&mutex);
|
||||
vm_cond_reltimedwait(&cond, &mutex, ms_to_expiry);
|
||||
vm_mutex_unlock(&mutex);
|
||||
}
|
||||
}
|
||||
|
||||
static void cb_wakeup_thread()
|
||||
{
|
||||
vm_cond_signal(&cond);
|
||||
}
|
||||
|
||||
void init_sensor_framework()
|
||||
{
|
||||
// init the mutext and conditions
|
||||
korp_thread tid;
|
||||
vm_cond_init(&cond);
|
||||
vm_mutex_init(&mutex);
|
||||
|
||||
// add the sys sensor objects
|
||||
add_sys_sensor("sensor_test", "This is a sensor for test", 0, 1000,
|
||||
read_test_sensor, config_test_sensor);
|
||||
|
||||
set_sensor_reshceduler(cb_wakeup_thread);
|
||||
|
||||
wasm_register_msg_callback(SENSOR_EVENT_WASM,
|
||||
app_mgr_sensor_event_callback);
|
||||
|
||||
wasm_register_cleanup_callback(sensor_cleanup_callback);
|
||||
|
||||
vm_thread_create(&tid, thread_sensor_check, NULL,
|
||||
BH_APPLET_PRESERVED_STACK_SIZE);
|
||||
|
||||
}
|
||||
|
||||
23
core/iwasm/lib/native/extension/sensor/wasm_lib_sensor.cmake
Normal file
23
core/iwasm/lib/native/extension/sensor/wasm_lib_sensor.cmake
Normal file
@ -0,0 +1,23 @@
|
||||
# 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.
|
||||
|
||||
set (WASM_LIB_SENSOR_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
include_directories(${WASM_LIB_SENSOR_DIR})
|
||||
|
||||
|
||||
file (GLOB_RECURSE source_all ${WASM_LIB_SENSOR_DIR}/*.c)
|
||||
|
||||
set (WASM_LIB_SENSOR_SOURCE ${source_all})
|
||||
|
||||
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "lib-export.h"
|
||||
|
||||
/* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to add functions to register. */
|
||||
|
||||
NativeSymbol extended_native_symbol_defs[] = {
|
||||
|
||||
/*EXPORT_WASM_API(publish_event)*/
|
||||
|
||||
};
|
||||
893
core/iwasm/lib/native/libc/libc_wrapper.c
Normal file
893
core/iwasm/lib/native/libc/libc_wrapper.c
Normal file
@ -0,0 +1,893 @@
|
||||
/*
|
||||
* 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 "wasm-native.h"
|
||||
#include "wasm-export.h"
|
||||
#include "wasm_log.h"
|
||||
#include "wasm_platform_log.h"
|
||||
|
||||
void
|
||||
wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception);
|
||||
uint32
|
||||
wasm_runtime_get_temp_ret(wasm_module_inst_t module);
|
||||
void
|
||||
wasm_runtime_set_temp_ret(wasm_module_inst_t module, uint32 temp_ret);
|
||||
uint32
|
||||
wasm_runtime_get_llvm_stack(wasm_module_inst_t module);
|
||||
void
|
||||
wasm_runtime_set_llvm_stack(wasm_module_inst_t module, uint32 llvm_stack);
|
||||
|
||||
#define get_module_inst() \
|
||||
wasm_runtime_get_current_module_inst()
|
||||
|
||||
#define validate_app_addr(offset, size) \
|
||||
wasm_runtime_validate_app_addr(module_inst, offset, size)
|
||||
|
||||
#define addr_app_to_native(offset) \
|
||||
wasm_runtime_addr_app_to_native(module_inst, offset)
|
||||
|
||||
#define addr_native_to_app(ptr) \
|
||||
wasm_runtime_addr_native_to_app(module_inst, ptr)
|
||||
|
||||
#define module_malloc(size) \
|
||||
wasm_runtime_module_malloc(module_inst, size)
|
||||
|
||||
#define module_free(offset) \
|
||||
wasm_runtime_module_free(module_inst, offset)
|
||||
|
||||
typedef int (*out_func_t)(int c, void *ctx);
|
||||
|
||||
enum pad_type {
|
||||
PAD_NONE, PAD_ZERO_BEFORE, PAD_SPACE_BEFORE, PAD_SPACE_AFTER,
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Output an unsigned long in hex format
|
||||
*
|
||||
* Output an unsigned long on output installed by platform at init time. Should
|
||||
* be able to handle an unsigned long of any size, 32 or 64 bit.
|
||||
* @param num Number to output
|
||||
*
|
||||
* @return N/A
|
||||
*/
|
||||
static void _printf_hex_ulong(out_func_t out, void *ctx,
|
||||
const unsigned long num, enum pad_type padding, int min_width)
|
||||
{
|
||||
int size = sizeof(num) * 2;
|
||||
int found_largest_digit = 0;
|
||||
int remaining = 8; /* 8 digits max */
|
||||
int digits = 0;
|
||||
|
||||
for (; size; size--) {
|
||||
char nibble = (num >> ((size - 1) << 2) & 0xf);
|
||||
|
||||
if (nibble || found_largest_digit || size == 1) {
|
||||
found_largest_digit = 1;
|
||||
nibble += nibble > 9 ? 87 : 48;
|
||||
out((int) nibble, ctx);
|
||||
digits++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (remaining-- <= min_width) {
|
||||
if (padding == PAD_ZERO_BEFORE) {
|
||||
out('0', ctx);
|
||||
} else if (padding == PAD_SPACE_BEFORE) {
|
||||
out(' ', ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (padding == PAD_SPACE_AFTER) {
|
||||
remaining = min_width * 2 - digits;
|
||||
while (remaining-- > 0) {
|
||||
out(' ', ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Output an unsigned long (32-bit) in decimal format
|
||||
*
|
||||
* Output an unsigned long on output installed by platform at init time. Only
|
||||
* works with 32-bit values.
|
||||
* @param num Number to output
|
||||
*
|
||||
* @return N/A
|
||||
*/
|
||||
static void _printf_dec_ulong(out_func_t out, void *ctx,
|
||||
const unsigned long num, enum pad_type padding, int min_width)
|
||||
{
|
||||
unsigned long pos = 999999999;
|
||||
unsigned long remainder = num;
|
||||
int found_largest_digit = 0;
|
||||
int remaining = 10; /* 10 digits max */
|
||||
int digits = 1;
|
||||
|
||||
/* make sure we don't skip if value is zero */
|
||||
if (min_width <= 0) {
|
||||
min_width = 1;
|
||||
}
|
||||
|
||||
while (pos >= 9) {
|
||||
if (found_largest_digit || remainder > pos) {
|
||||
found_largest_digit = 1;
|
||||
out((int) ((remainder / (pos + 1)) + 48), ctx);
|
||||
digits++;
|
||||
} else if (remaining <= min_width && padding < PAD_SPACE_AFTER) {
|
||||
out((int) (padding == PAD_ZERO_BEFORE ? '0' : ' '), ctx);
|
||||
digits++;
|
||||
}
|
||||
remaining--;
|
||||
remainder %= (pos + 1);
|
||||
pos /= 10;
|
||||
}
|
||||
out((int) (remainder + 48), ctx);
|
||||
|
||||
if (padding == PAD_SPACE_AFTER) {
|
||||
remaining = min_width - digits;
|
||||
while (remaining-- > 0) {
|
||||
out(' ', ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void _vprintf(out_func_t out, void *ctx, const char *fmt, va_list ap,
|
||||
wasm_module_inst_t module_inst)
|
||||
{
|
||||
int might_format = 0; /* 1 if encountered a '%' */
|
||||
enum pad_type padding = PAD_NONE;
|
||||
int min_width = -1;
|
||||
int long_ctr = 0;
|
||||
|
||||
/* fmt has already been adjusted if needed */
|
||||
|
||||
while (*fmt) {
|
||||
if (!might_format) {
|
||||
if (*fmt != '%') {
|
||||
out((int) *fmt, ctx);
|
||||
} else {
|
||||
might_format = 1;
|
||||
min_width = -1;
|
||||
padding = PAD_NONE;
|
||||
long_ctr = 0;
|
||||
}
|
||||
} else {
|
||||
switch (*fmt) {
|
||||
case '-':
|
||||
padding = PAD_SPACE_AFTER;
|
||||
goto still_might_format;
|
||||
|
||||
case '0':
|
||||
if (min_width < 0 && padding == PAD_NONE) {
|
||||
padding = PAD_ZERO_BEFORE;
|
||||
goto still_might_format;
|
||||
}
|
||||
/* Fall through */
|
||||
case '1' ... '9':
|
||||
if (min_width < 0) {
|
||||
min_width = *fmt - '0';
|
||||
} else {
|
||||
min_width = 10 * min_width + *fmt - '0';
|
||||
}
|
||||
|
||||
if (padding == PAD_NONE) {
|
||||
padding = PAD_SPACE_BEFORE;
|
||||
}
|
||||
goto still_might_format;
|
||||
|
||||
case 'l':
|
||||
long_ctr++;
|
||||
/* Fall through */
|
||||
case 'z':
|
||||
case 'h':
|
||||
/* FIXME: do nothing for these modifiers */
|
||||
goto still_might_format;
|
||||
|
||||
case 'd':
|
||||
case 'i': {
|
||||
long d;
|
||||
if (long_ctr < 2) {
|
||||
d = va_arg(ap, long);
|
||||
} else {
|
||||
d = (long)va_arg(ap, long long);
|
||||
}
|
||||
|
||||
if (d < 0) {
|
||||
out((int) '-', ctx);
|
||||
d = -d;
|
||||
min_width--;
|
||||
}
|
||||
_printf_dec_ulong(out, ctx, d, padding, min_width);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'u': {
|
||||
unsigned long u;
|
||||
|
||||
if (long_ctr < 2) {
|
||||
u = va_arg(ap, unsigned long);
|
||||
} else {
|
||||
u = (unsigned long)va_arg(ap,
|
||||
unsigned long long);
|
||||
}
|
||||
_printf_dec_ulong(out, ctx, u, padding, min_width);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'p':
|
||||
out('0', ctx);
|
||||
out('x', ctx);
|
||||
/* left-pad pointers with zeros */
|
||||
padding = PAD_ZERO_BEFORE;
|
||||
min_width = 8;
|
||||
/* Fall through */
|
||||
case 'x':
|
||||
case 'X': {
|
||||
unsigned long x;
|
||||
|
||||
if (long_ctr < 2) {
|
||||
x = va_arg(ap, unsigned long);
|
||||
} else {
|
||||
x = (unsigned long)va_arg(ap, unsigned long long);
|
||||
}
|
||||
|
||||
_printf_hex_ulong(out, ctx, x, padding, min_width);
|
||||
break;
|
||||
}
|
||||
|
||||
case 's': {
|
||||
char *s;
|
||||
char *start;
|
||||
int32 s_offset = va_arg(ap, uint32);
|
||||
|
||||
if (!validate_app_addr(s_offset, 1)) {
|
||||
wasm_runtime_set_exception(module_inst, "out of bounds memory access");
|
||||
return;
|
||||
}
|
||||
|
||||
s = start = addr_app_to_native(s_offset);
|
||||
|
||||
while (*s)
|
||||
out((int) (*s++), ctx);
|
||||
|
||||
if (padding == PAD_SPACE_AFTER) {
|
||||
int remaining = min_width - (s - start);
|
||||
while (remaining-- > 0) {
|
||||
out(' ', ctx);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 'c': {
|
||||
int c = va_arg(ap, int);
|
||||
out(c, ctx);
|
||||
break;
|
||||
}
|
||||
|
||||
case '%': {
|
||||
out((int) '%', ctx);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
out((int) '%', ctx);
|
||||
out((int) *fmt, ctx);
|
||||
break;
|
||||
}
|
||||
|
||||
might_format = 0;
|
||||
}
|
||||
|
||||
still_might_format: ++fmt;
|
||||
}
|
||||
}
|
||||
|
||||
struct str_context {
|
||||
char *str;
|
||||
int max;
|
||||
int count;
|
||||
};
|
||||
|
||||
static int sprintf_out(int c, struct str_context *ctx)
|
||||
{
|
||||
if (!ctx->str || ctx->count >= ctx->max) {
|
||||
ctx->count++;
|
||||
return c;
|
||||
}
|
||||
|
||||
if (ctx->count == ctx->max - 1) {
|
||||
ctx->str[ctx->count++] = '\0';
|
||||
} else {
|
||||
ctx->str[ctx->count++] = c;
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
static int printf_out(int c, struct str_context *ctx)
|
||||
{
|
||||
printf("%c", c);
|
||||
ctx->count++;
|
||||
return c;
|
||||
}
|
||||
|
||||
static inline va_list get_va_list(uint32 *args)
|
||||
{
|
||||
union {
|
||||
uint32 u;
|
||||
va_list v;
|
||||
} u;
|
||||
u.u = args[0];
|
||||
return u.v;
|
||||
}
|
||||
|
||||
static bool parse_printf_args(wasm_module_inst_t module_inst, int32 fmt_offset,
|
||||
int32 va_list_offset, const char **p_fmt, va_list *p_va_args)
|
||||
{
|
||||
const char *fmt;
|
||||
union {
|
||||
uintptr_t u;
|
||||
va_list v;
|
||||
} u;
|
||||
|
||||
if (!validate_app_addr(fmt_offset,
|
||||
1) || !validate_app_addr(va_list_offset, sizeof(int32)))
|
||||
return false;
|
||||
|
||||
fmt = (const char*) addr_app_to_native(fmt_offset);
|
||||
u.u = (uintptr_t) addr_app_to_native(va_list_offset);
|
||||
|
||||
*p_fmt = fmt;
|
||||
*p_va_args = u.v;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int _printf_wrapper(int32 fmt_offset, int32 va_list_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
struct str_context ctx = { NULL, 0, 0 };
|
||||
const char *fmt;
|
||||
va_list va_args;
|
||||
|
||||
if (!parse_printf_args(module_inst, fmt_offset, va_list_offset, &fmt, &va_args))
|
||||
return 0;
|
||||
|
||||
_vprintf((out_func_t) printf_out, &ctx, fmt, va_args, module_inst);
|
||||
return ctx.count;
|
||||
}
|
||||
|
||||
static int _sprintf_wrapper(int32 str_offset, int32 fmt_offset,
|
||||
int32 va_list_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
struct str_context ctx;
|
||||
char *str;
|
||||
const char *fmt;
|
||||
va_list va_args;
|
||||
|
||||
if (!validate_app_addr(str_offset, 1))
|
||||
return 0;
|
||||
|
||||
str = addr_app_to_native(str_offset);
|
||||
|
||||
if (!parse_printf_args(module_inst, fmt_offset, va_list_offset, &fmt, &va_args))
|
||||
return 0;
|
||||
|
||||
ctx.str = str;
|
||||
ctx.max = INT_MAX;
|
||||
ctx.count = 0;
|
||||
|
||||
_vprintf((out_func_t) sprintf_out, &ctx, fmt, va_args, module_inst);
|
||||
|
||||
if (ctx.count < ctx.max) {
|
||||
str[ctx.count] = '\0';
|
||||
}
|
||||
|
||||
return ctx.count;
|
||||
}
|
||||
|
||||
static int _snprintf_wrapper(int32 str_offset, int32 size, int32 fmt_offset,
|
||||
int32 va_list_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
struct str_context ctx;
|
||||
char *str;
|
||||
const char *fmt;
|
||||
va_list va_args;
|
||||
|
||||
if (!validate_app_addr(str_offset, size))
|
||||
return 0;
|
||||
|
||||
str = addr_app_to_native(str_offset);
|
||||
|
||||
if (!parse_printf_args(module_inst, fmt_offset, va_list_offset, &fmt, &va_args))
|
||||
return 0;
|
||||
|
||||
ctx.str = str;
|
||||
ctx.max = size;
|
||||
ctx.count = 0;
|
||||
|
||||
_vprintf((out_func_t) sprintf_out, &ctx, fmt, va_args, module_inst);
|
||||
|
||||
if (ctx.count < ctx.max) {
|
||||
str[ctx.count] = '\0';
|
||||
}
|
||||
|
||||
return ctx.count;
|
||||
}
|
||||
|
||||
static int _puts_wrapper(int32 str_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
const char *str;
|
||||
|
||||
if (!validate_app_addr(str_offset, 1))
|
||||
return 0;
|
||||
|
||||
str = addr_app_to_native(str_offset);
|
||||
return printf("%s\n", str);
|
||||
}
|
||||
|
||||
static int _putchar_wrapper(int c)
|
||||
{
|
||||
printf("%c", c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int32 _strdup_wrapper(int32 str_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *str, *str_ret;
|
||||
uint32 len;
|
||||
int32 str_ret_offset = 0;
|
||||
|
||||
if (!validate_app_addr(str_offset, 1))
|
||||
return 0;
|
||||
|
||||
str = addr_app_to_native(str_offset);
|
||||
|
||||
if (str) {
|
||||
len = strlen(str) + 1;
|
||||
|
||||
str_ret_offset = module_malloc(len);
|
||||
if (str_ret_offset) {
|
||||
str_ret = addr_app_to_native(str_ret_offset);
|
||||
memcpy(str_ret, str, len);
|
||||
}
|
||||
}
|
||||
|
||||
return str_ret_offset;
|
||||
}
|
||||
|
||||
static int32 _memcmp_wrapper(int32 s1_offset, int32 s2_offset, int32 size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
void *s1, *s2;
|
||||
|
||||
if (!validate_app_addr(s1_offset, size) || !validate_app_addr(s2_offset, size))
|
||||
return 0;
|
||||
|
||||
s1 = addr_app_to_native(s1_offset);
|
||||
s2 = addr_app_to_native(s2_offset);
|
||||
return memcmp(s1, s2, size);
|
||||
}
|
||||
|
||||
static int32 _memcpy_wrapper(int32 dst_offset, int32 src_offset, int32 size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
void *dst, *src;
|
||||
|
||||
if (size == 0)
|
||||
return dst_offset;
|
||||
|
||||
if (!validate_app_addr(dst_offset, size) || !validate_app_addr(src_offset, size))
|
||||
return dst_offset;
|
||||
|
||||
dst = addr_app_to_native(dst_offset);
|
||||
src = addr_app_to_native(src_offset);
|
||||
memcpy(dst, src, size);
|
||||
return dst_offset;
|
||||
}
|
||||
|
||||
static int32 _memmove_wrapper(int32 dst_offset, int32 src_offset, int32 size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
void *dst, *src;
|
||||
|
||||
if (!validate_app_addr(dst_offset, size) || !validate_app_addr(src_offset, size))
|
||||
return dst_offset;
|
||||
|
||||
dst = addr_app_to_native(dst_offset);
|
||||
src = addr_app_to_native(src_offset);
|
||||
memmove(dst, src, size);
|
||||
return dst_offset;
|
||||
}
|
||||
|
||||
static int32 _memset_wrapper(int32 s_offset, int32 c, int32 size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
void *s;
|
||||
|
||||
if (!validate_app_addr(s_offset, size))
|
||||
return s_offset;
|
||||
|
||||
s = addr_app_to_native(s_offset);
|
||||
memset(s, c, size);
|
||||
return s_offset;
|
||||
}
|
||||
|
||||
static int32 _strchr_wrapper(int32 s_offset, int32 c)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
const char *s;
|
||||
char *ret;
|
||||
|
||||
if (!validate_app_addr(s_offset, 1))
|
||||
return s_offset;
|
||||
|
||||
s = addr_app_to_native(s_offset);
|
||||
ret = strchr(s, c);
|
||||
return ret ? addr_native_to_app(ret) : 0;
|
||||
}
|
||||
|
||||
static int32 _strcmp_wrapper(int32 s1_offset, int32 s2_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
void *s1, *s2;
|
||||
|
||||
if (!validate_app_addr(s1_offset, 1) || !validate_app_addr(s2_offset, 1))
|
||||
return 0;
|
||||
|
||||
s1 = addr_app_to_native(s1_offset);
|
||||
s2 = addr_app_to_native(s2_offset);
|
||||
return strcmp(s1, s2);
|
||||
}
|
||||
|
||||
static int32 _strncmp_wrapper(int32 s1_offset, int32 s2_offset, uint32 size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
void *s1, *s2;
|
||||
|
||||
if (!validate_app_addr(s1_offset, size) || !validate_app_addr(s2_offset, size))
|
||||
return 0;
|
||||
|
||||
s1 = addr_app_to_native(s1_offset);
|
||||
s2 = addr_app_to_native(s2_offset);
|
||||
return strncmp(s1, s2, size);
|
||||
}
|
||||
|
||||
static int32 _strcpy_wrapper(int32 dst_offset, int32 src_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *dst, *src;
|
||||
|
||||
if (!validate_app_addr(dst_offset, 1) || !validate_app_addr(src_offset, 1))
|
||||
return 0;
|
||||
|
||||
dst = addr_app_to_native(dst_offset);
|
||||
src = addr_app_to_native(src_offset);
|
||||
strcpy(dst, src);
|
||||
return dst_offset;
|
||||
}
|
||||
|
||||
static int32 _strncpy_wrapper(int32 dst_offset, int32 src_offset, uint32 size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *dst, *src;
|
||||
|
||||
if (!validate_app_addr(dst_offset, size) || !validate_app_addr(src_offset, size))
|
||||
return 0;
|
||||
|
||||
dst = addr_app_to_native(dst_offset);
|
||||
src = addr_app_to_native(src_offset);
|
||||
strncpy(dst, src, size);
|
||||
return dst_offset;
|
||||
}
|
||||
|
||||
static uint32 _strlen_wrapper(int32 s_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *s;
|
||||
|
||||
if (!validate_app_addr(s_offset, 1))
|
||||
return 0;
|
||||
|
||||
s = addr_app_to_native(s_offset);
|
||||
return strlen(s);
|
||||
}
|
||||
|
||||
static int32 _malloc_wrapper(uint32 size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
return module_malloc(size);
|
||||
}
|
||||
|
||||
static int32 _calloc_wrapper(uint32 nmemb, uint32 size)
|
||||
{
|
||||
uint64 total_size = (uint64) nmemb * (uint64) size;
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
uint32 ret_offset = 0;
|
||||
uint8 *ret_ptr;
|
||||
|
||||
if (total_size > UINT32_MAX)
|
||||
total_size = UINT32_MAX;
|
||||
|
||||
ret_offset = module_malloc((uint32 )total_size);
|
||||
if (ret_offset) {
|
||||
ret_ptr = addr_app_to_native(ret_offset);
|
||||
memset(ret_ptr, 0, (uint32) total_size);
|
||||
}
|
||||
|
||||
return ret_offset;
|
||||
}
|
||||
|
||||
static void _free_wrapper(int32 ptr_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
|
||||
if (!validate_app_addr(ptr_offset, 4))
|
||||
return;
|
||||
return module_free(ptr_offset);
|
||||
}
|
||||
|
||||
static void setTempRet0_wrapper(uint32 temp_ret)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
wasm_runtime_set_temp_ret(module_inst, temp_ret);
|
||||
}
|
||||
|
||||
static uint32 getTempRet0_wrapper()
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
return wasm_runtime_get_temp_ret(module_inst);
|
||||
}
|
||||
|
||||
static uint32 _llvm_bswap_i16_wrapper(uint32 data)
|
||||
{
|
||||
return (data & 0xFFFF0000) | ((data & 0xFF) << 8) | ((data & 0xFF00) >> 8);
|
||||
}
|
||||
|
||||
static uint32 _llvm_bswap_i32_wrapper(uint32 data)
|
||||
{
|
||||
return ((data & 0xFF) << 24) | ((data & 0xFF00) << 8) | ((data & 0xFF0000) >> 8)
|
||||
| ((data & 0xFF000000) >> 24);
|
||||
}
|
||||
|
||||
static uint32 _bitshift64Lshr_wrapper(uint32 uint64_part0, uint32 uint64_part1,
|
||||
uint32 bits)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
union {
|
||||
uint64 value;
|
||||
uint32 parts[2];
|
||||
} u;
|
||||
|
||||
u.parts[0] = uint64_part0;
|
||||
u.parts[1] = uint64_part1;
|
||||
|
||||
u.value >>= bits;
|
||||
/* return low 32bit and save high 32bit to temp ret */
|
||||
wasm_runtime_set_temp_ret(module_inst, (uint32) (u.value >> 32));
|
||||
return (uint32) u.value;
|
||||
}
|
||||
|
||||
static uint32 _bitshift64Shl_wrapper(uint32 int64_part0, uint32 int64_part1,
|
||||
uint32 bits)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
union {
|
||||
int64 value;
|
||||
uint32 parts[2];
|
||||
} u;
|
||||
|
||||
u.parts[0] = int64_part0;
|
||||
u.parts[1] = int64_part1;
|
||||
|
||||
u.value <<= bits;
|
||||
/* return low 32bit and save high 32bit to temp ret */
|
||||
wasm_runtime_set_temp_ret(module_inst, (uint32) (u.value >> 32));
|
||||
return (uint32) u.value;
|
||||
}
|
||||
|
||||
static void _llvm_stackrestore_wrapper(uint32 llvm_stack)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
printf("_llvm_stackrestore called!\n");
|
||||
wasm_runtime_set_llvm_stack(module_inst, llvm_stack);
|
||||
}
|
||||
|
||||
static uint32 _llvm_stacksave_wrapper()
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
printf("_llvm_stacksave called!\n");
|
||||
return wasm_runtime_get_llvm_stack(module_inst);
|
||||
}
|
||||
|
||||
static int32 _emscripten_memcpy_big_wrapper(int32 dst_offset, int32 src_offset,
|
||||
uint32 size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
void *dst, *src;
|
||||
|
||||
if (!validate_app_addr(dst_offset, size) || !validate_app_addr(src_offset, size))
|
||||
return dst_offset;
|
||||
|
||||
dst = addr_app_to_native(dst_offset);
|
||||
src = addr_app_to_native(src_offset);
|
||||
|
||||
memcpy(dst, src, size);
|
||||
return dst_offset;
|
||||
}
|
||||
|
||||
static void abort_wrapper(int32 code)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char buf[32];
|
||||
snprintf(buf, sizeof(buf), "env.abort(%i)", code);
|
||||
wasm_runtime_set_exception(module_inst, buf);
|
||||
}
|
||||
|
||||
static void abortStackOverflow_wrapper(int32 code)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char buf[32];
|
||||
snprintf(buf, sizeof(buf), "env.abortStackOverflow(%i)", code);
|
||||
wasm_runtime_set_exception(module_inst, buf);
|
||||
}
|
||||
|
||||
static void nullFunc_X_wrapper(int32 code)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char buf[32];
|
||||
snprintf(buf, sizeof(buf), "env.nullFunc_X(%i)", code);
|
||||
wasm_runtime_set_exception(module_inst, buf);
|
||||
}
|
||||
|
||||
/* TODO: add function parameter/result types check */
|
||||
#define REG_NATIVE_FUNC(module_name, func_name) \
|
||||
{#module_name, #func_name, func_name##_wrapper}
|
||||
|
||||
typedef struct WASMNativeFuncDef {
|
||||
const char *module_name;
|
||||
const char *func_name;
|
||||
void *func_ptr;
|
||||
} WASMNativeFuncDef;
|
||||
|
||||
static WASMNativeFuncDef native_func_defs[] = {
|
||||
REG_NATIVE_FUNC(env, _printf),
|
||||
REG_NATIVE_FUNC(env, _sprintf),
|
||||
REG_NATIVE_FUNC(env, _snprintf),
|
||||
REG_NATIVE_FUNC(env, _puts),
|
||||
REG_NATIVE_FUNC(env, _putchar),
|
||||
REG_NATIVE_FUNC(env, _memcmp),
|
||||
REG_NATIVE_FUNC(env, _memcpy),
|
||||
REG_NATIVE_FUNC(env, _memmove),
|
||||
REG_NATIVE_FUNC(env, _memset),
|
||||
REG_NATIVE_FUNC(env, _strchr),
|
||||
REG_NATIVE_FUNC(env, _strcmp),
|
||||
REG_NATIVE_FUNC(env, _strcpy),
|
||||
REG_NATIVE_FUNC(env, _strlen),
|
||||
REG_NATIVE_FUNC(env, _strncmp),
|
||||
REG_NATIVE_FUNC(env, _strncpy),
|
||||
REG_NATIVE_FUNC(env, _malloc),
|
||||
REG_NATIVE_FUNC(env, _calloc),
|
||||
REG_NATIVE_FUNC(env, _strdup),
|
||||
REG_NATIVE_FUNC(env, _free),
|
||||
REG_NATIVE_FUNC(env, setTempRet0),
|
||||
REG_NATIVE_FUNC(env, getTempRet0),
|
||||
REG_NATIVE_FUNC(env, _llvm_bswap_i16),
|
||||
REG_NATIVE_FUNC(env, _llvm_bswap_i32),
|
||||
REG_NATIVE_FUNC(env, _bitshift64Lshr),
|
||||
REG_NATIVE_FUNC(env, _bitshift64Shl),
|
||||
REG_NATIVE_FUNC(env, _llvm_stackrestore),
|
||||
REG_NATIVE_FUNC(env, _llvm_stacksave),
|
||||
REG_NATIVE_FUNC(env, _emscripten_memcpy_big),
|
||||
REG_NATIVE_FUNC(env, abort),
|
||||
REG_NATIVE_FUNC(env, abortStackOverflow),
|
||||
REG_NATIVE_FUNC(env, nullFunc_X), };
|
||||
|
||||
void*
|
||||
wasm_native_func_lookup(const char *module_name, const char *func_name)
|
||||
{
|
||||
uint32 size = sizeof(native_func_defs) / sizeof(WASMNativeFuncDef);
|
||||
WASMNativeFuncDef *func_def = native_func_defs;
|
||||
WASMNativeFuncDef *func_def_end = func_def + size;
|
||||
void *ret;
|
||||
|
||||
if (!module_name || !func_name)
|
||||
return NULL;
|
||||
|
||||
while (func_def < func_def_end) {
|
||||
if (!strcmp(func_def->module_name, module_name)
|
||||
&& !strcmp(func_def->func_name, func_name))
|
||||
return (void*) (uintptr_t) func_def->func_ptr;
|
||||
func_def++;
|
||||
}
|
||||
|
||||
if ((ret = wasm_platform_native_func_lookup(module_name, func_name)))
|
||||
return ret;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*************************************
|
||||
* Global Variables *
|
||||
*************************************/
|
||||
|
||||
typedef struct WASMNativeGlobalDef {
|
||||
const char *module_name;
|
||||
const char *global_name;
|
||||
WASMValue global_data;
|
||||
} WASMNativeGlobalDef;
|
||||
|
||||
static WASMNativeGlobalDef native_global_defs[] = { { "env", "STACKTOP",
|
||||
.global_data.u32 = 0 }, { "env", "STACK_MAX", .global_data.u32 = 0 }, { "env",
|
||||
"ABORT", .global_data.u32 = 0 }, { "env", "memoryBase", .global_data.u32 = 0 },
|
||||
{ "env", "__memory_base", .global_data.u32 = 0 }, { "env", "tableBase",
|
||||
.global_data.u32 = 0 }, { "env", "__table_base", .global_data.u32 = 0 }, {
|
||||
"env", "DYNAMICTOP_PTR", .global_data.addr = 0 }, { "env", "tempDoublePtr",
|
||||
.global_data.addr = 0 }, { "global", "NaN", .global_data.u64 =
|
||||
0x7FF8000000000000LL }, { "global", "Infinity", .global_data.u64 =
|
||||
0x7FF0000000000000LL }, };
|
||||
|
||||
bool wasm_native_global_lookup(const char *module_name, const char *global_name,
|
||||
WASMGlobalImport *global)
|
||||
{
|
||||
uint32 size = sizeof(native_global_defs) / sizeof(WASMNativeGlobalDef);
|
||||
WASMNativeGlobalDef *global_def = native_global_defs;
|
||||
WASMNativeGlobalDef *global_def_end = global_def + size;
|
||||
|
||||
if (!module_name || !global_name || !global)
|
||||
return false;
|
||||
|
||||
/* Lookup constant globals which can be defined by table */
|
||||
while (global_def < global_def_end) {
|
||||
if (!strcmp(global_def->module_name, module_name)
|
||||
&& !strcmp(global_def->global_name, global_name)) {
|
||||
global->global_data_linked = global_def->global_data;
|
||||
return true;
|
||||
}
|
||||
global_def++;
|
||||
}
|
||||
|
||||
/* Lookup non-constant globals which cannot be defined by table */
|
||||
if (!strcmp(module_name, "env")) {
|
||||
if (!strcmp(global_name, "_stdin")) {
|
||||
global->global_data_linked.addr = (uintptr_t) stdin;
|
||||
global->is_addr = true;
|
||||
return true;
|
||||
} else if (!strcmp(global_name, "_stdout")) {
|
||||
global->global_data_linked.addr = (uintptr_t) stdout;
|
||||
global->is_addr = true;
|
||||
return true;
|
||||
} else if (!strcmp(global_name, "_stderr")) {
|
||||
global->global_data_linked.addr = (uintptr_t) stderr;
|
||||
global->is_addr = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool wasm_native_init()
|
||||
{
|
||||
/* TODO: qsort the function defs and global defs. */
|
||||
return true;
|
||||
}
|
||||
|
||||
23
core/iwasm/lib/native/libc/wasm_libc.cmake
Normal file
23
core/iwasm/lib/native/libc/wasm_libc.cmake
Normal file
@ -0,0 +1,23 @@
|
||||
# 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.
|
||||
|
||||
set (WASM_LIBC_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
include_directories(${WASM_LIBC_DIR})
|
||||
|
||||
|
||||
file (GLOB_RECURSE source_all ${WASM_LIBC_DIR}/*.c)
|
||||
|
||||
set (WASM_LIBC_SOURCE ${source_all})
|
||||
|
||||
Reference in New Issue
Block a user