diff options
Diffstat (limited to 'src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base')
18 files changed, 1407 insertions, 0 deletions
diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/bh_platform.c b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/bh_platform.c new file mode 100644 index 000000000..1848d0792 --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/bh_platform.c @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#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((uint8 *)&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; +} diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/bh_platform.h b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/bh_platform.h new file mode 100644 index 000000000..8e10dcb64 --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/bh_platform.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#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 + +uint32 +htonl(uint32 value); +uint32 +ntohl(uint32 value); +uint16 +htons(uint16 value); +uint16 +ntohs(uint16 value); + +// We are not worried for the WASM world since the sandbox will catch it. +#define bh_memcpy_s(dst, dst_len, src, src_len) memcpy(dst, src, src_len) + +#ifdef NDEBUG +#define bh_assert(v) (void)0 +#else +#define bh_assert(v) \ + do { \ + if (!(v)) { \ + int _count; \ + printf("ASSERTION FAILED: %s, at %s, line %d", #v, __FILE__, \ + __LINE__); \ + _count = printf("\n"); \ + printf("%d\n", _count / (_count - 1)); \ + } \ + } while (0) +#endif + +#endif /* DEPS_IWASM_APP_LIBS_BASE_BH_PLATFORM_H_ */ diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/req_resp_api.h b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/req_resp_api.h new file mode 100644 index 000000000..575c35732 --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/req_resp_api.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef _REQ_RESP_API_H_ +#define _REQ_RESP_API_H_ + +#include "bh_platform.h" + +#ifdef __cplusplus +extern "C" { +#endif + +bool +wasm_response_send(const char *buf, int size); + +void +wasm_register_resource(const char *url); + +void +wasm_post_request(const char *buf, int size); + +void +wasm_sub_event(const char *url); + +#ifdef __cplusplus +} +#endif + +#endif /* end of _REQ_RESP_API_H_ */ diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/request.c b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/request.c new file mode 100644 index 000000000..3ba44fbc7 --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/request.c @@ -0,0 +1,365 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "bi-inc/attr_container.h" +#include "wa-inc/request.h" +#include "wa-inc/timer_wasm_app.h" +#include "bi-inc/shared_utils.h" +#include "wasm_app.h" +#include "req_resp_api.h" +#include "timer_api.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; + } +} + +static 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, (char *)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); +} diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/timer.c b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/timer.c new file mode 100644 index 000000000..692626ca3 --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/timer.c @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include <stdlib.h> +#include <string.h> + +#include "wa-inc/timer_wasm_app.h" +#include "timer_api.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, + on_user_timer_update_f on_timer_update) +{ + + 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; + } +} diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/timer_api.h b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/timer_api.h new file mode 100644 index 000000000..1fc7555ef --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/timer_api.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef _TIMER_API_H_ +#define _TIMER_API_H_ + +#include "bh_platform.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef unsigned int timer_id_t; + +timer_id_t +wasm_create_timer(int interval, bool is_period, bool auto_start); + +void +wasm_timer_destroy(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); + +#ifdef __cplusplus +} +#endif + +#endif /* end of _TIMER_API_H_ */ diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/wa-inc/request.h b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/wa-inc/request.h new file mode 100644 index 000000000..25830f0a4 --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/wa-inc/request.h @@ -0,0 +1,171 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef _AEE_REQUEST_H_ +#define _AEE_REQUEST_H_ + +#include "bi-inc/shared_utils.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; + +/** + * @typedef request_handler_f + * + * @brief Define the signature of callback function for API + * api_register_resource_handler() to handle request or for API + * api_subscribe_event() to handle event. + * + * @param request pointer of the request to be handled + * + * @see api_register_resource_handler + * @see api_subscribe_event + */ +typedef void (*request_handler_f)(request_t *request); + +/** + * @typedef response_handler_f + * + * @brief Define the signature of callback function for API + * api_send_request() to handle response of a request. + * + * @param response pointer of the response to be handled + * @param user_data user data associated with the request which is set when + * calling api_send_request(). + * + * @see api_send_request + */ +typedef void (*response_handler_f)(response_t *response, void *user_data); + +/* + ***************** + * Request APIs + ***************** + */ + +/** + * @brief Register resource. + * + * @param url url of the resource + * @param handler callback function to handle the request to the resource + * + * @return true if success, false otherwise + */ +bool +api_register_resource_handler(const char *url, request_handler_f handler); + +/** + * @brief Send request asynchronously. + * + * @param request pointer of the request to be sent + * @param response_handler callback function to handle the response + * @param user_data user data + */ +void +api_send_request(request_t *request, response_handler_f response_handler, + void *user_data); + +/** + * @brief Send response. + * + * @param response pointer of the response to be sent + * + * @par + * @code + * void res1_handler(request_t *request) + * { + * response_t response[1]; + * make_response_for_request(request, response); + * set_response(response, DELETED_2_02, 0, NULL, 0); + * api_response_send(response); + * } + * @endcode + */ +void +api_response_send(response_t *response); + +/* + ***************** + * Event APIs + ***************** + */ + +/** + * @brief Publish an event. + * + * @param url url of the event + * @param fmt format of the event payload + * @param payload payload of the event + * @param payload_len length in bytes of the event payload + * + * @return true if success, false otherwise + */ +bool +api_publish_event(const char *url, int fmt, void *payload, int payload_len); + +/** + * @brief Subscribe an event. + * + * @param url url of the event + * @param handler callback function to handle the event. + * + * @return true if success, false otherwise + */ +bool +api_subscribe_event(const char *url, request_handler_f handler); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/wa-inc/timer_wasm_app.h b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/wa-inc/timer_wasm_app.h new file mode 100644 index 000000000..cf158a365 --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/wa-inc/timer_wasm_app.h @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef _AEE_TIMER_H_ +#define _AEE_TIMER_H_ + +#include "bh_platform.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* board producer define user_timer */ +struct user_timer; +typedef struct user_timer *user_timer_t; + +/** + * @typedef on_user_timer_update_f + * + * @brief Define the signature of callback function for API api_timer_create(). + * + * @param timer the timer + * + * @see api_timer_create + */ +typedef void (*on_user_timer_update_f)(user_timer_t timer); + +/* + ***************** + * Timer APIs + ***************** + */ + +/** + * @brief Create timer. + * + * @param interval timer interval + * @param is_period whether the timer is periodic + * @param auto_start whether start the timer immediately after created + * @param on_timer_update callback function called when timer expired + * + * @return the timer created if success, NULL otherwise + */ +user_timer_t +api_timer_create(int interval, bool is_period, bool auto_start, + on_user_timer_update_f on_timer_update); + +/** + * @brief Cancel timer. + * + * @param timer the timer to cancel + */ +void +api_timer_cancel(user_timer_t timer); + +/** + * @brief Restart timer. + * + * @param timer the timer to cancel + * @param interval the timer interval + */ +void +api_timer_restart(user_timer_t timer, int interval); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/wasm_app.cmake b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/wasm_app.cmake new file mode 100644 index 000000000..2313df99d --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/wasm_app.cmake @@ -0,0 +1,13 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +set (WASM_APP_BASE_DIR ${CMAKE_CURRENT_LIST_DIR}) + +include_directories(${WASM_APP_BASE_DIR}) + +add_definitions (-DWASM_ENABLE_BASE_LIB) + +file (GLOB_RECURSE source_all ${WASM_APP_BASE_DIR}/*.c) + +set (WASM_APP_CURRENT_SOURCE ${source_all}) +set (WASM_APP_BASE_DIR ${WASM_APP_BASE_DIR} PARENT_SCOPE) diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/wasm_app.h b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/wasm_app.h new file mode 100644 index 000000000..e7be8a4c1 --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/wasm_app.h @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef _LIB_AEE_H_ +#define _LIB_AEE_H_ + +#include "bi-inc/shared_utils.h" +#include "bi-inc/attr_container.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* end of _LIB_AEE_H_ */ diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/base_lib.inl b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/base_lib.inl new file mode 100644 index 000000000..3c228cc93 --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/base_lib.inl @@ -0,0 +1,14 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + + EXPORT_WASM_API_WITH_SIG(wasm_register_resource, "($)"), + EXPORT_WASM_API_WITH_SIG(wasm_response_send, "(*~)i"), + EXPORT_WASM_API_WITH_SIG(wasm_post_request, "(*~)"), + EXPORT_WASM_API_WITH_SIG(wasm_sub_event, "($)"), + EXPORT_WASM_API_WITH_SIG(wasm_create_timer, "(iii)i"), + EXPORT_WASM_API_WITH_SIG(wasm_timer_destroy, "(i)"), + EXPORT_WASM_API_WITH_SIG(wasm_timer_cancel, "(i)"), + EXPORT_WASM_API_WITH_SIG(wasm_timer_restart, "(ii)"), + EXPORT_WASM_API_WITH_SIG(wasm_get_sys_tick_ms, "()i"), diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/base_lib_export.c b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/base_lib_export.c new file mode 100644 index 000000000..19ac7185c --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/base_lib_export.c @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "lib_export.h" +#include "req_resp_native_api.h" +#include "timer_native_api.h" + +static NativeSymbol extended_native_symbol_defs[] = { +/* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to + add functions to register. */ +#include "base_lib.inl" +}; + +uint32 +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); +} diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/req_resp_native_api.h b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/req_resp_native_api.h new file mode 100644 index 000000000..3e5938772 --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/req_resp_native_api.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef _REQ_RESP_API_H_ +#define _REQ_RESP_API_H_ + +#include "bh_platform.h" +#include "wasm_export.h" + +#ifdef __cplusplus +extern "C" { +#endif + +bool +wasm_response_send(wasm_exec_env_t exec_env, char *buffer, int size); +void +wasm_register_resource(wasm_exec_env_t exec_env, char *url); +void +wasm_post_request(wasm_exec_env_t exec_env, char *buffer, int size); +void +wasm_sub_event(wasm_exec_env_t exec_env, char *url); + +#ifdef __cplusplus +} +#endif + +#endif /* end of _REQ_RESP_API_H_ */ diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/request_response.c b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/request_response.c new file mode 100644 index 000000000..674ba5e9d --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/request_response.c @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "app_manager_export.h" +#include "coap_ext.h" +#include "wasm_export.h" +#include "bh_assert.h" + +extern void +module_request_handler(request_t *request, void *user_data); + +bool +wasm_response_send(wasm_exec_env_t exec_env, char *buffer, int size) +{ + 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(wasm_exec_env_t exec_env, char *url) +{ + wasm_module_inst_t module_inst = get_module_inst(exec_env); + + if (url != NULL) { + unsigned int mod_id = + app_manager_get_module_id(Module_WASM_App, module_inst); + bh_assert(mod_id != ID_NONE); + am_register_resource(url, module_request_handler, mod_id); + } +} + +void +wasm_post_request(wasm_exec_env_t exec_env, char *buffer, int size) +{ + wasm_module_inst_t module_inst = get_module_inst(exec_env); + + 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, module_inst); + bh_assert(mod_id != ID_NONE); + req->sender = mod_id; + + if (req->action == COAP_EVENT) { + am_publish_event(req); + return; + } + + am_dispatch_request(req); + } +} + +void +wasm_sub_event(wasm_exec_env_t exec_env, char *url) +{ + wasm_module_inst_t module_inst = get_module_inst(exec_env); + + if (url != NULL) { + unsigned int mod_id = + app_manager_get_module_id(Module_WASM_App, module_inst); + + bh_assert(mod_id != ID_NONE); + am_register_event(url, mod_id); + } +} diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/runtime_lib.h b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/runtime_lib.h new file mode 100644 index 000000000..477b663b2 --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/runtime_lib.h @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef LIB_BASE_RUNTIME_LIB_H_ +#define LIB_BASE_RUNTIME_LIB_H_ + +#include "runtime_timer.h" + +bool +init_wasm_timer(); +void +exit_wasm_timer(); +timer_ctx_t +get_wasm_timer_ctx(); +timer_ctx_t +create_wasm_timer_ctx(unsigned int module_id, int prealloc_num); +void +destroy_module_timer_ctx(unsigned int module_id); + +#endif /* LIB_BASE_RUNTIME_LIB_H_ */ diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/timer_native_api.h b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/timer_native_api.h new file mode 100644 index 000000000..138e7c60d --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/timer_native_api.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef _TIMER_API_H_ +#define _TIMER_API_H_ + +#include "bh_platform.h" +#include "wasm_export.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef unsigned int timer_id_t; + +/* + * timer interfaces + */ + +typedef unsigned int timer_id_t; + +timer_id_t +wasm_create_timer(wasm_exec_env_t exec_env, int interval, bool is_period, + bool auto_start); +void +wasm_timer_destroy(wasm_exec_env_t exec_env, timer_id_t timer_id); +void +wasm_timer_cancel(wasm_exec_env_t exec_env, timer_id_t timer_id); +void +wasm_timer_restart(wasm_exec_env_t exec_env, timer_id_t timer_id, int interval); +uint32 +wasm_get_sys_tick_ms(wasm_exec_env_t exec_env); + +#ifdef __cplusplus +} +#endif + +#endif /* end of _TIMER_API_H_ */ diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/timer_wrapper.c b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/timer_wrapper.c new file mode 100644 index 000000000..246868849 --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/timer_wrapper.c @@ -0,0 +1,220 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "bh_platform.h" +#include "app_manager_export.h" +#include "../app-manager/module_wasm_app.h" +#include "timer_native_api.h" + +typedef struct { + bh_list_link l; + timer_ctx_t timer_ctx; +} timer_ctx_node_t; + +static bool timer_thread_run = true; + +static bh_list g_timer_ctx_list; +static korp_cond g_timer_ctx_list_cond; +static korp_mutex g_timer_ctx_list_mutex; + +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 *)(uintptr_t)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) +{ + uint32 ms_to_expiry; + uint64 us_to_wait; + + while (timer_thread_run) { + ms_to_expiry = (uint32)-1; + os_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) { + uint32 next = check_app_timers(elem->timer_ctx); + if (next != (uint32)-1) { + if (ms_to_expiry == (uint32)-1 || ms_to_expiry > next) + ms_to_expiry = next; + } + + elem = (timer_ctx_node_t *)bh_list_elem_next(elem); + } + os_mutex_unlock(&g_timer_ctx_list_mutex); + + if (ms_to_expiry == (uint32)-1) + us_to_wait = BHT_WAIT_FOREVER; + else + us_to_wait = (uint64)ms_to_expiry * 1000; + os_mutex_lock(&g_timer_ctx_list_mutex); + os_cond_reltimedwait(&g_timer_ctx_list_cond, &g_timer_ctx_list_mutex, + us_to_wait); + os_mutex_unlock(&g_timer_ctx_list_mutex); + } + + return NULL; +} + +void +wakeup_modules_timer_thread(timer_ctx_t ctx) +{ + os_mutex_lock(&g_timer_ctx_list_mutex); + os_cond_signal(&g_timer_ctx_list_cond); + os_mutex_unlock(&g_timer_ctx_list_mutex); +} + +bool +init_wasm_timer() +{ + korp_tid tm_tid; + bh_list_init(&g_timer_ctx_list); + + if (os_cond_init(&g_timer_ctx_list_cond) != 0) { + return false; + } + /* temp solution for: thread_modulers_timer_check thread + would recursive lock the mutex */ + if (os_recursive_mutex_init(&g_timer_ctx_list_mutex) != 0) { + goto fail1; + } + + if (0 + != os_thread_create(&tm_tid, thread_modulers_timer_check, NULL, + BH_APPLET_PRESERVED_STACK_SIZE)) { + goto fail2; + } + + return true; + +fail2: + os_mutex_destroy(&g_timer_ctx_list_mutex); + +fail1: + os_cond_destroy(&g_timer_ctx_list_cond); + + return false; +} + +void +exit_wasm_timer() +{ + timer_thread_run = false; +} + +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 *)wasm_runtime_malloc(sizeof(timer_ctx_node_t)); + if (node == NULL) { + destroy_timer_ctx(ctx); + return NULL; + } + memset(node, 0, sizeof(*node)); + node->timer_ctx = ctx; + + os_mutex_lock(&g_timer_ctx_list_mutex); + bh_list_insert(&g_timer_ctx_list, node); + os_mutex_unlock(&g_timer_ctx_list_mutex); + + return ctx; +} + +void +destroy_module_timer_ctx(unsigned int module_id) +{ + timer_ctx_node_t *elem; + + os_mutex_lock(&g_timer_ctx_list_mutex); + 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); + wasm_runtime_free(elem); + break; + } + + elem = (timer_ctx_node_t *)bh_list_elem_next(elem); + } + os_mutex_unlock(&g_timer_ctx_list_mutex); +} + +timer_ctx_t +get_wasm_timer_ctx(wasm_module_inst_t module_inst) +{ + module_data *m = app_manager_get_module_data(Module_WASM_App, module_inst); + if (m == NULL) + return NULL; + return m->timer_ctx; +} + +timer_id_t +wasm_create_timer(wasm_exec_env_t exec_env, int interval, bool is_period, + bool auto_start) +{ + wasm_module_inst_t module_inst = get_module_inst(exec_env); + timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst); + bh_assert(timer_ctx); + return sys_create_timer(timer_ctx, interval, is_period, auto_start); +} + +void +wasm_timer_destroy(wasm_exec_env_t exec_env, timer_id_t timer_id) +{ + wasm_module_inst_t module_inst = get_module_inst(exec_env); + timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst); + bh_assert(timer_ctx); + sys_timer_destroy(timer_ctx, timer_id); +} + +void +wasm_timer_cancel(wasm_exec_env_t exec_env, timer_id_t timer_id) +{ + wasm_module_inst_t module_inst = get_module_inst(exec_env); + timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst); + bh_assert(timer_ctx); + sys_timer_cancel(timer_ctx, timer_id); +} + +void +wasm_timer_restart(wasm_exec_env_t exec_env, timer_id_t timer_id, int interval) +{ + wasm_module_inst_t module_inst = get_module_inst(exec_env); + timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst); + bh_assert(timer_ctx); + sys_timer_restart(timer_ctx, timer_id, interval); +} + +uint32 +wasm_get_sys_tick_ms(wasm_exec_env_t exec_env) +{ + return (uint32)bh_get_tick_ms(); +} diff --git a/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/wasm_lib.cmake b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/wasm_lib.cmake new file mode 100644 index 000000000..223320b32 --- /dev/null +++ b/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/wasm_lib.cmake @@ -0,0 +1,13 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +set (WASM_LIB_BASE_DIR ${CMAKE_CURRENT_LIST_DIR}) + +add_definitions (-DWASM_ENABLE_BASE_LIB) + +include_directories(${WASM_LIB_BASE_DIR}) + +file (GLOB_RECURSE source_all ${WASM_LIB_BASE_DIR}/*.c) + +set (WASM_APP_LIB_CURRENT_SOURCE ${source_all}) + |