Apply clang-format for more source files (#795)
Apply clang-format for C source files in folder core/app-mgr, core/app-framework, and test-tools. And rename folder component_test to component-test, update zephyr build document. Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
@ -13,14 +13,16 @@
|
||||
*
|
||||
*/
|
||||
|
||||
static bool is_little_endian()
|
||||
static bool
|
||||
is_little_endian()
|
||||
{
|
||||
long i = 0x01020304;
|
||||
unsigned char* c = (unsigned char*) &i;
|
||||
unsigned char *c = (unsigned char *)&i;
|
||||
return (*c == 0x04) ? true : false;
|
||||
}
|
||||
|
||||
static void swap32(uint8* pData)
|
||||
static void
|
||||
swap32(uint8 *pData)
|
||||
{
|
||||
uint8 value = *pData;
|
||||
*pData = *(pData + 3);
|
||||
@ -31,31 +33,35 @@ static void swap32(uint8* pData)
|
||||
*(pData + 2) = value;
|
||||
}
|
||||
|
||||
static void swap16(uint8* pData)
|
||||
static void
|
||||
swap16(uint8 *pData)
|
||||
{
|
||||
uint8 value = *pData;
|
||||
*(pData) = *(pData + 1);
|
||||
*(pData + 1) = value;
|
||||
}
|
||||
|
||||
uint32 htonl(uint32 value)
|
||||
uint32
|
||||
htonl(uint32 value)
|
||||
{
|
||||
uint32 ret;
|
||||
if (is_little_endian()) {
|
||||
ret = value;
|
||||
swap32((uint8*) &ret);
|
||||
swap32((uint8 *)&ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
uint32 ntohl(uint32 value)
|
||||
uint32
|
||||
ntohl(uint32 value)
|
||||
{
|
||||
return htonl(value);
|
||||
}
|
||||
|
||||
uint16 htons(uint16 value)
|
||||
uint16
|
||||
htons(uint16 value)
|
||||
{
|
||||
uint16 ret;
|
||||
if (is_little_endian()) {
|
||||
@ -67,12 +73,14 @@ uint16 htons(uint16 value)
|
||||
return value;
|
||||
}
|
||||
|
||||
uint16 ntohs(uint16 value)
|
||||
uint16
|
||||
ntohs(uint16 value)
|
||||
{
|
||||
return htons(value);
|
||||
}
|
||||
|
||||
char *wa_strdup(const char *s)
|
||||
char *
|
||||
wa_strdup(const char *s)
|
||||
{
|
||||
char *s1 = NULL;
|
||||
if (s && (s1 = WA_MALLOC(strlen(s) + 1)))
|
||||
|
||||
37
core/app-framework/base/app/bh_platform.h
Executable file → Normal file
37
core/app-framework/base/app/bh_platform.h
Executable file → Normal file
@ -16,7 +16,7 @@ typedef unsigned int uint32;
|
||||
typedef int int32;
|
||||
|
||||
#ifndef NULL
|
||||
# define NULL ((void*) 0)
|
||||
#define NULL ((void *)0)
|
||||
#endif
|
||||
|
||||
#ifndef __cplusplus
|
||||
@ -35,28 +35,31 @@ typedef int int32;
|
||||
#define WA_FREE free
|
||||
#endif
|
||||
|
||||
|
||||
uint32 htonl(uint32 value);
|
||||
uint32 ntohl(uint32 value);
|
||||
uint16 htons(uint16 value);
|
||||
uint16 ntohs(uint16 value);
|
||||
|
||||
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)
|
||||
#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)
|
||||
#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_ */
|
||||
|
||||
@ -29,4 +29,3 @@ wasm_sub_event(const char *url);
|
||||
#endif
|
||||
|
||||
#endif /* end of _REQ_RESP_API_H_ */
|
||||
|
||||
|
||||
@ -13,13 +13,11 @@
|
||||
|
||||
#define TRANSACTION_TIMEOUT_MS 5000
|
||||
|
||||
typedef enum {
|
||||
Reg_Event, Reg_Request
|
||||
} reg_type_t;
|
||||
typedef enum { Reg_Event, Reg_Request } reg_type_t;
|
||||
|
||||
typedef struct _res_register {
|
||||
struct _res_register *next;
|
||||
const char * url;
|
||||
const char *url;
|
||||
reg_type_t reg_type;
|
||||
void (*request_handler)(request_t *);
|
||||
} res_register_t;
|
||||
@ -32,13 +30,14 @@ typedef struct transaction {
|
||||
void *user_data;
|
||||
} transaction_t;
|
||||
|
||||
static res_register_t * g_resources = NULL;
|
||||
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)
|
||||
static transaction_t *
|
||||
transaction_find(int mid)
|
||||
{
|
||||
transaction_t *t = g_transactions;
|
||||
|
||||
@ -55,7 +54,8 @@ static transaction_t *transaction_find(int mid)
|
||||
* 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)
|
||||
static void
|
||||
transaction_add(transaction_t *trans)
|
||||
{
|
||||
transaction_t *t;
|
||||
|
||||
@ -73,7 +73,8 @@ static void transaction_add(transaction_t *trans)
|
||||
}
|
||||
}
|
||||
|
||||
static void transaction_remove(transaction_t *trans)
|
||||
static void
|
||||
transaction_remove(transaction_t *trans)
|
||||
{
|
||||
transaction_t *prev = NULL, *current = g_transactions;
|
||||
|
||||
@ -93,15 +94,17 @@ static void transaction_remove(transaction_t *trans)
|
||||
}
|
||||
}
|
||||
|
||||
static bool is_event_type(request_t * req)
|
||||
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)
|
||||
static bool
|
||||
register_url_handler(const char *url, request_handler_f request_handler,
|
||||
reg_type_t reg_type)
|
||||
{
|
||||
res_register_t * r = g_resources;
|
||||
res_register_t *r = g_resources;
|
||||
|
||||
while (r) {
|
||||
if (reg_type == r->reg_type && strcmp(r->url, url) == 0) {
|
||||
@ -111,7 +114,7 @@ static bool register_url_handler(const char *url,
|
||||
r = r->next;
|
||||
}
|
||||
|
||||
r = (res_register_t *) malloc(sizeof(res_register_t));
|
||||
r = (res_register_t *)malloc(sizeof(res_register_t));
|
||||
if (r == NULL)
|
||||
return false;
|
||||
|
||||
@ -137,13 +140,15 @@ static bool register_url_handler(const char *url,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool api_register_resource_handler(const char *url,
|
||||
request_handler_f request_handler)
|
||||
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)
|
||||
static void
|
||||
transaction_timeout_handler(user_timer_t timer)
|
||||
{
|
||||
transaction_t *cur, *expired = NULL;
|
||||
unsigned int elpased_ms, now = wasm_get_sys_tick_ms();
|
||||
@ -164,7 +169,8 @@ static void transaction_timeout_handler(user_timer_t timer)
|
||||
cur->next = expired;
|
||||
expired = cur;
|
||||
cur = g_transactions;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -186,27 +192,30 @@ static void transaction_timeout_handler(user_timer_t timer)
|
||||
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 {
|
||||
}
|
||||
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 {
|
||||
}
|
||||
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)
|
||||
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) {
|
||||
if ((trans = (transaction_t *)malloc(sizeof(transaction_t))) == NULL) {
|
||||
printf(
|
||||
"send request: allocate memory for request transaction failed!\n");
|
||||
"send request: allocate memory for request transaction failed!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -228,9 +237,8 @@ void api_send_request(request_t * request, response_handler_f response_handler,
|
||||
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);
|
||||
g_trans_timer = api_timer_create(TRANSACTION_TIMEOUT_MS, false,
|
||||
true, transaction_timeout_handler);
|
||||
}
|
||||
}
|
||||
|
||||
@ -241,11 +249,13 @@ void api_send_request(request_t * request, response_handler_f response_handler,
|
||||
|
||||
/*
|
||||
*
|
||||
* APIs for the native layers to callback for request/response arrived to this app
|
||||
* APIs for the native layers to callback for request/response arrived to this
|
||||
* app
|
||||
*
|
||||
*/
|
||||
|
||||
void on_response(char * buffer, int size)
|
||||
void
|
||||
on_response(char *buffer, int size)
|
||||
{
|
||||
response_t response[1];
|
||||
transaction_t *trans;
|
||||
@ -262,7 +272,8 @@ void on_response(char * buffer, int size)
|
||||
|
||||
/*
|
||||
* When the 1st transaction get response:
|
||||
* 1. If the 2nd trans exist, restart the timer according to its expiry time;
|
||||
* 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) {
|
||||
@ -270,12 +281,14 @@ void on_response(char * buffer, int size)
|
||||
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 {
|
||||
}
|
||||
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 {
|
||||
}
|
||||
else {
|
||||
api_timer_cancel(g_trans_timer);
|
||||
g_trans_timer = NULL;
|
||||
}
|
||||
@ -285,7 +298,8 @@ void on_response(char * buffer, int size)
|
||||
transaction_remove(trans);
|
||||
}
|
||||
|
||||
void on_request(char *buffer, int size)
|
||||
void
|
||||
on_request(char *buffer, int size)
|
||||
{
|
||||
request_t request[1];
|
||||
bool is_event;
|
||||
@ -300,9 +314,9 @@ void on_request(char *buffer, int size)
|
||||
|
||||
while (r) {
|
||||
if ((is_event && r->reg_type == Reg_Event)
|
||||
|| (!is_event && r->reg_type == Reg_Request)) {
|
||||
|| (!is_event && r->reg_type == Reg_Request)) {
|
||||
if (check_url_start(request->url, strlen(request->url), r->url)
|
||||
> 0) {
|
||||
> 0) {
|
||||
r->request_handler(request);
|
||||
return;
|
||||
}
|
||||
@ -314,10 +328,11 @@ void on_request(char *buffer, int size)
|
||||
printf("on_request: exit. no service handler\n");
|
||||
}
|
||||
|
||||
void api_response_send(response_t *response)
|
||||
void
|
||||
api_response_send(response_t *response)
|
||||
{
|
||||
int size;
|
||||
char * buffer = pack_response(response, &size);
|
||||
char *buffer = pack_response(response, &size);
|
||||
if (buffer == NULL)
|
||||
return;
|
||||
|
||||
@ -327,12 +342,13 @@ 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_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);
|
||||
char *buffer = pack_request(request, &size);
|
||||
if (buffer == NULL)
|
||||
return false;
|
||||
wasm_post_request(buffer, size);
|
||||
@ -342,8 +358,8 @@ bool api_publish_event(const char *url, int fmt, void *payload, int payload_len)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool api_subscribe_event(const char * url, request_handler_f handler)
|
||||
bool
|
||||
api_subscribe_event(const char *url, request_handler_f handler)
|
||||
{
|
||||
return register_url_handler(url, handler, Reg_Event);
|
||||
}
|
||||
|
||||
|
||||
@ -16,22 +16,23 @@
|
||||
#endif
|
||||
|
||||
struct user_timer {
|
||||
struct user_timer * next;
|
||||
struct user_timer *next;
|
||||
int timer_id;
|
||||
void (*user_timer_callback)(user_timer_t);
|
||||
};
|
||||
|
||||
struct user_timer * g_timers = NULL;
|
||||
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)
|
||||
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));
|
||||
// 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");
|
||||
@ -52,7 +53,8 @@ user_timer_t api_timer_create(int interval, bool is_period, bool auto_start,
|
||||
return timer;
|
||||
}
|
||||
|
||||
void api_timer_cancel(user_timer_t timer)
|
||||
void
|
||||
api_timer_cancel(user_timer_t timer)
|
||||
{
|
||||
user_timer_t t = g_timers, prev = NULL;
|
||||
|
||||
@ -63,26 +65,30 @@ void api_timer_cancel(user_timer_t timer)
|
||||
if (prev == NULL) {
|
||||
g_timers = t->next;
|
||||
free(t);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
prev->next = t->next;
|
||||
free(t);
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
prev = t;
|
||||
t = t->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void api_timer_restart(user_timer_t timer, int interval)
|
||||
void
|
||||
api_timer_restart(user_timer_t timer, int interval)
|
||||
{
|
||||
wasm_timer_restart(timer->timer_id, interval);
|
||||
}
|
||||
|
||||
void on_timer_callback(int timer_id)
|
||||
void
|
||||
on_timer_callback(int timer_id)
|
||||
{
|
||||
struct user_timer * t = g_timers;
|
||||
struct user_timer *t = g_timers;
|
||||
|
||||
while (t) {
|
||||
if (t->timer_id == timer_id) {
|
||||
@ -92,4 +98,3 @@ void on_timer_callback(int timer_id)
|
||||
t = t->next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -34,4 +34,3 @@ wasm_get_sys_tick_ms(void);
|
||||
#endif
|
||||
|
||||
#endif /* end of _TIMER_API_H_ */
|
||||
|
||||
|
||||
@ -12,7 +12,6 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/* CoAP request method codes */
|
||||
typedef enum {
|
||||
COAP_GET = 1,
|
||||
@ -26,39 +25,40 @@ typedef enum {
|
||||
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 */
|
||||
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 */
|
||||
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 */
|
||||
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 */
|
||||
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,
|
||||
MEMORY_ALLOCATION_ERROR = 192,
|
||||
PACKET_SERIALIZATION_ERROR,
|
||||
|
||||
/* Erbium hooks */
|
||||
MANUAL_RESPONSE, PING_RESPONSE
|
||||
MANUAL_RESPONSE,
|
||||
PING_RESPONSE
|
||||
} coap_status_t;
|
||||
|
||||
|
||||
/**
|
||||
* @typedef request_handler_f
|
||||
*
|
||||
@ -87,7 +87,6 @@ typedef void (*request_handler_f)(request_t *request);
|
||||
*/
|
||||
typedef void (*response_handler_f)(response_t *response, void *user_data);
|
||||
|
||||
|
||||
/*
|
||||
*****************
|
||||
* Request APIs
|
||||
@ -102,7 +101,8 @@ typedef void (*response_handler_f)(response_t *response, void *user_data);
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool api_register_resource_handler(const char *url, request_handler_f handler);
|
||||
bool
|
||||
api_register_resource_handler(const char *url, request_handler_f handler);
|
||||
|
||||
/**
|
||||
* @brief Send request asynchronously.
|
||||
@ -111,8 +111,9 @@ bool api_register_resource_handler(const char *url, request_handler_f handler);
|
||||
* @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);
|
||||
void
|
||||
api_send_request(request_t *request, response_handler_f response_handler,
|
||||
void *user_data);
|
||||
|
||||
/**
|
||||
* @brief Send response.
|
||||
@ -130,8 +131,8 @@ void api_send_request(request_t * request, response_handler_f response_handler,
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
void api_response_send(response_t *response);
|
||||
|
||||
void
|
||||
api_response_send(response_t *response);
|
||||
|
||||
/*
|
||||
*****************
|
||||
@ -149,9 +150,8 @@ void api_response_send(response_t *response);
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool api_publish_event(const char *url, int fmt, void *payload,
|
||||
int payload_len);
|
||||
|
||||
bool
|
||||
api_publish_event(const char *url, int fmt, void *payload, int payload_len);
|
||||
|
||||
/**
|
||||
* @brief Subscribe an event.
|
||||
@ -161,7 +161,8 @@ bool api_publish_event(const char *url, int fmt, void *payload,
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool api_subscribe_event(const char * url, request_handler_f handler);
|
||||
bool
|
||||
api_subscribe_event(const char *url, request_handler_f handler);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@ -14,7 +14,7 @@ extern "C" {
|
||||
|
||||
/* board producer define user_timer */
|
||||
struct user_timer;
|
||||
typedef struct user_timer * user_timer_t;
|
||||
typedef struct user_timer *user_timer_t;
|
||||
|
||||
/**
|
||||
* @typedef on_user_timer_update_f
|
||||
@ -43,15 +43,17 @@ typedef void (*on_user_timer_update_f)(user_timer_t timer);
|
||||
*
|
||||
* @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);
|
||||
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);
|
||||
void
|
||||
api_timer_cancel(user_timer_t timer);
|
||||
|
||||
/**
|
||||
* @brief Restart timer.
|
||||
@ -59,7 +61,8 @@ void api_timer_cancel(user_timer_t timer);
|
||||
* @param timer the timer to cancel
|
||||
* @param interval the timer interval
|
||||
*/
|
||||
void api_timer_restart(user_timer_t timer, int interval);
|
||||
void
|
||||
api_timer_restart(user_timer_t timer, int interval);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@ -13,7 +13,6 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -10,16 +10,15 @@
|
||||
#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"
|
||||
/* 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)
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@ -27,4 +27,3 @@ wasm_sub_event(wasm_exec_env_t exec_env, char *url);
|
||||
#endif
|
||||
|
||||
#endif /* end of _REQ_RESP_API_H_ */
|
||||
|
||||
|
||||
@ -8,7 +8,8 @@
|
||||
#include "wasm_export.h"
|
||||
#include "bh_assert.h"
|
||||
|
||||
extern void module_request_handler(request_t *request, void *user_data);
|
||||
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)
|
||||
@ -33,8 +34,8 @@ 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);
|
||||
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);
|
||||
}
|
||||
@ -54,8 +55,8 @@ wasm_post_request(wasm_exec_env_t exec_env, char *buffer, int size)
|
||||
// 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);
|
||||
unsigned int mod_id =
|
||||
app_manager_get_module_id(Module_WASM_App, module_inst);
|
||||
bh_assert(mod_id != ID_NONE);
|
||||
req->sender = mod_id;
|
||||
|
||||
@ -74,11 +75,10 @@ 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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -6,13 +6,17 @@
|
||||
#ifndef LIB_BASE_RUNTIME_LIB_H_
|
||||
#define LIB_BASE_RUNTIME_LIB_H_
|
||||
|
||||
|
||||
#include "runtime_timer.h"
|
||||
|
||||
void 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);
|
||||
void
|
||||
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_ */
|
||||
|
||||
@ -9,7 +9,6 @@
|
||||
#include "bh_platform.h"
|
||||
#include "wasm_export.h"
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
@ -23,15 +22,14 @@ typedef unsigned int timer_id_t;
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
|
||||
@ -40,4 +38,3 @@ wasm_get_sys_tick_ms(wasm_exec_env_t exec_env);
|
||||
#endif
|
||||
|
||||
#endif /* end of _TIMER_API_H_ */
|
||||
|
||||
|
||||
@ -22,7 +22,7 @@ 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);
|
||||
module_data *module = module_data_list_lookup_id(mod_id);
|
||||
if (module == NULL)
|
||||
return;
|
||||
|
||||
@ -40,7 +40,8 @@ wasm_timer_callback(timer_id_t id, unsigned int mod_id)
|
||||
* the module from module id. It is for avoiding that situation.
|
||||
*/
|
||||
|
||||
void * thread_modulers_timer_check(void * arg)
|
||||
void *
|
||||
thread_modulers_timer_check(void *arg)
|
||||
{
|
||||
uint32 ms_to_expiry;
|
||||
uint64 us_to_wait;
|
||||
@ -48,8 +49,8 @@ void * thread_modulers_timer_check(void * arg)
|
||||
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);
|
||||
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) {
|
||||
@ -57,7 +58,7 @@ void * thread_modulers_timer_check(void * arg)
|
||||
ms_to_expiry = next;
|
||||
}
|
||||
|
||||
elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
|
||||
elem = (timer_ctx_node_t *)bh_list_elem_next(elem);
|
||||
}
|
||||
os_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
|
||||
@ -93,8 +94,8 @@ init_wasm_timer()
|
||||
would recursive lock the mutex */
|
||||
os_recursive_mutex_init(&g_timer_ctx_list_mutex);
|
||||
|
||||
os_thread_create(&tm_tid, thread_modulers_timer_check,
|
||||
NULL, BH_APPLET_PRESERVED_STACK_SIZE);
|
||||
os_thread_create(&tm_tid, thread_modulers_timer_check, NULL,
|
||||
BH_APPLET_PRESERVED_STACK_SIZE);
|
||||
}
|
||||
|
||||
void
|
||||
@ -106,15 +107,15 @@ exit_wasm_timer()
|
||||
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);
|
||||
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));
|
||||
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;
|
||||
@ -132,11 +133,10 @@ create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
|
||||
void
|
||||
destroy_module_timer_ctx(unsigned int module_id)
|
||||
{
|
||||
timer_ctx_node_t* elem;
|
||||
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);
|
||||
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);
|
||||
@ -145,7 +145,7 @@ destroy_module_timer_ctx(unsigned int module_id)
|
||||
break;
|
||||
}
|
||||
|
||||
elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
|
||||
elem = (timer_ctx_node_t *)bh_list_elem_next(elem);
|
||||
}
|
||||
os_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
}
|
||||
@ -153,16 +153,15 @@ destroy_module_timer_ctx(unsigned int module_id)
|
||||
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);
|
||||
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_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);
|
||||
@ -189,8 +188,7 @@ 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)
|
||||
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);
|
||||
@ -203,4 +201,3 @@ wasm_get_sys_tick_ms(wasm_exec_env_t exec_env)
|
||||
{
|
||||
return (uint32)bh_get_tick_ms();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user