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:
Wenyong Huang
2021-10-21 13:58:34 +08:00
committed by GitHub
parent 225f5d0a64
commit 32242988ed
143 changed files with 5377 additions and 4627 deletions

View File

@ -8,30 +8,30 @@
#include <stdbool.h>
#include <string.h>
static unsigned char*
read_file_to_buffer (const char *filename, int *ret_size)
static unsigned char *
read_file_to_buffer(const char *filename, int *ret_size)
{
unsigned char *buffer;
FILE *file;
int file_size, read_size;
if (!(file = fopen (filename, "r")))
if (!(file = fopen(filename, "r")))
return NULL;
fseek (file, 0, SEEK_END);
file_size = ftell (file);
fseek (file, 0, SEEK_SET);
fseek(file, 0, SEEK_END);
file_size = ftell(file);
fseek(file, 0, SEEK_SET);
if (!(buffer = malloc (file_size))) {
fclose (file);
if (!(buffer = malloc(file_size))) {
fclose(file);
return NULL;
}
read_size = fread (buffer, 1, file_size, file);
fclose (file);
read_size = fread(buffer, 1, file_size, file);
fclose(file);
if (read_size < file_size) {
free (buffer);
free(buffer);
return NULL;
}
@ -41,20 +41,19 @@ read_file_to_buffer (const char *filename, int *ret_size)
}
static int
print_help ()
print_help()
{
printf ("Usage: binarydump -o <file> -n <name> input_file\n");
printf ("Options:\n");
printf (" -o <file> Place the output into <file>\n");
printf (" -n <name> The name of array <file>\n");
printf("Usage: binarydump -o <file> -n <name> input_file\n");
printf("Options:\n");
printf(" -o <file> Place the output into <file>\n");
printf(" -n <name> The name of array <file>\n");
return -1;
}
static bool
bin_file_dump (const unsigned char *file, int size,
const char *bin_file_output,
const char *array_name)
bin_file_dump(const unsigned char *file, int size, const char *bin_file_output,
const char *array_name)
{
unsigned i = 0;
const unsigned char *p = file, *p_end = file + size;
@ -63,7 +62,8 @@ bin_file_dump (const unsigned char *file, int size,
if (!file_output)
return false;
fprintf(file_output, "\nunsigned char __aligned(4) %s[] = {\n ", array_name);
fprintf(file_output, "\nunsigned char __aligned(4) %s[] = {\n ",
array_name);
while (p < p_end) {
fprintf(file_output, "0x%02X", *p++);
@ -86,7 +86,7 @@ bin_file_dump (const unsigned char *file, int size,
}
int
main (int argc, char *argv[])
main(int argc, char *argv[])
{
unsigned char *file;
int size;
@ -94,33 +94,33 @@ main (int argc, char *argv[])
const char *bin_file_input, *array_file_output = NULL, *array_name = NULL;
for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
if (!strcmp (argv[0], "-o")) {
if (!strcmp(argv[0], "-o")) {
++argv;
if (--argc == 0)
return print_help ();
return print_help();
array_file_output = *argv;
}
else if (!strcmp (argv[0], "-n")) {
else if (!strcmp(argv[0], "-n")) {
++argv;
if (--argc == 0)
return print_help ();
return print_help();
array_name = *argv;
}
else
return print_help ();
return print_help();
}
if (!array_file_output || !array_name)
return print_help ();
return print_help();
bin_file_input = *argv;
if (!(file = read_file_to_buffer (bin_file_input, &size)))
if (!(file = read_file_to_buffer(bin_file_input, &size)))
return -1;
ret = bin_file_dump (file, size, array_file_output, array_name);
ret = bin_file_dump(file, size, array_file_output, array_name);
free (file);
free(file);
return ret ? 0 : -1;
}

View File

@ -11,32 +11,36 @@
#include "bi-inc/attr_container.h"
#include "er-coap-constants.h"
static char *read_file_to_buffer(const char *filename, int *ret_size);
static char *
read_file_to_buffer(const char *filename, int *ret_size);
int send_request_to_applet_success = 0;
const char *label_for_request = "request1";
int event_listener_counter = 0;
char *applet_buf[1024 * 1024];
const char *host_agent_ip = "127.0.0.1";
void f_aee_response_handler(void *usr_ctx, aee_response_t *response)
void
f_aee_response_handler(void *usr_ctx, aee_response_t *response)
{
if (response == NULL) {
printf("########## request timeout!!! \n");
} else {
char *str = (char *) usr_ctx;
}
else {
char *str = (char *)usr_ctx;
printf("#### dump response ####\n");
printf("#### user data: %s \n", str);
printf("#### status: %d \n", response->status);
if (response->payload != NULL)
attr_container_dump((attr_container_t *) response->payload);
attr_container_dump((attr_container_t *)response->payload);
}
}
void f_aee_event_listener(const char *url, void *event, int fmt)
void
f_aee_event_listener(const char *url, void *event, int fmt)
{
printf("######## event is received. url: %s, fmt:%d ############\n", url,
fmt);
fmt);
attr_container_t *attr_obj = (attr_container_t *) event;
attr_container_t *attr_obj = (attr_container_t *)event;
attr_container_dump(attr_obj);
/*
@ -48,7 +52,8 @@ void f_aee_event_listener(const char *url, void *event, int fmt)
*/
}
static int print_menu_and_select(void)
static int
print_menu_and_select(void)
{
char s[256];
int choice;
@ -83,7 +88,8 @@ static int print_menu_and_select(void)
return 0;
}
static void install_applet(int index)
static void
install_applet(int index)
{
char applet_name[64];
char applet_file_name[64];
@ -94,15 +100,15 @@ static void install_applet(int index)
printf("Installing TestApplet%d...\n", index);
snprintf(applet_name, sizeof(applet_name), "TestApplet%d", index);
snprintf(applet_file_name, sizeof(applet_file_name), "./TestApplet%d.wasm",
index);
index);
buf = read_file_to_buffer(applet_file_name, &size);
if (!buf) {
printf("Install Applet failed: read file %s error.\n",
applet_file_name);
applet_file_name);
return;
}
//step2. install applet
// step2. install applet
ret = aee_applet_install(buf, "wasm", size, applet_name, 5000);
if (ret) {
printf("%s install success\n", applet_name);
@ -110,7 +116,8 @@ static void install_applet(int index)
free(buf);
}
static void uninstall_applet(int index)
static void
uninstall_applet(int index)
{
int ret;
char applet_name[64];
@ -118,51 +125,56 @@ static void uninstall_applet(int index)
ret = aee_applet_uninstall(applet_name, "wasm", 5000);
if (ret) {
printf("uninstall %s success\n", applet_name);
} else {
}
else {
printf("uninstall %s failed\n", applet_name);
}
}
static void send_request(int index)
static void
send_request(int index)
{
char url[64];
int ret;
aee_request_t req;
const char *user_context = "label for request";
attr_container_t *attr_obj = attr_container_create(
"Send Request to Applet");
attr_container_t *attr_obj =
attr_container_create("Send Request to Applet");
attr_container_set_string(&attr_obj, "String key", "Hello");
attr_container_set_int(&attr_obj, "Int key", 1000);
attr_container_set_int64(&attr_obj, "Int64 key", 0x77BBCCDD11223344LL);
//specify the target wasm app
// specify the target wasm app
snprintf(url, sizeof(url), "/app/TestApplet%d/url1", index);
//not specify the target wasm app
//snprintf(url, sizeof(url), "url1");
// not specify the target wasm app
// snprintf(url, sizeof(url), "url1");
aee_request_init(&req, url, COAP_PUT);
aee_request_set_payload(&req, attr_obj,
attr_container_get_serialize_length(attr_obj),
PAYLOAD_FORMAT_ATTRIBUTE_OBJECT);
ret = aee_request_send(&req, f_aee_response_handler, (void *) user_context,
10000);
attr_container_get_serialize_length(attr_obj),
PAYLOAD_FORMAT_ATTRIBUTE_OBJECT);
ret = aee_request_send(&req, f_aee_response_handler, (void *)user_context,
10000);
if (ret) {
printf("send request to TestApplet1 success\n");
}
}
static void register_event(const char *event_path)
static void
register_event(const char *event_path)
{
hostclient_register_event(event_path, f_aee_event_listener);
}
static void unregister_event(const char *event_path)
static void
unregister_event(const char *event_path)
{
hostclient_unregister_event(event_path);
}
static void query_applets()
static void
query_applets()
{
aee_applet_list_t applet_lst;
aee_applet_list_init(&applet_lst);
@ -189,7 +201,7 @@ read_file_to_buffer(const char *filename, int *ret_size)
return NULL;
}
if (!(buffer = (char *) malloc(file_size))) {
if (!(buffer = (char *)malloc(file_size))) {
fclose(fl);
return NULL;
}
@ -206,7 +218,8 @@ read_file_to_buffer(const char *filename, int *ret_size)
return buffer;
}
static void auto_test()
static void
auto_test()
{
int i;
int interval = 1000; /* ms */
@ -228,7 +241,8 @@ static void auto_test()
}
}
void exit_program()
void
exit_program()
{
hostclient_shutdown();
exit(0);
@ -240,7 +254,7 @@ main()
{
bool ret;
//step1. host client init
// step1. host client init
ret = hostclient_initialize(host_agent_ip, 3456);
if (!ret) {
@ -281,5 +295,7 @@ main()
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
// 5. Go to Project > Add New Item to create new code files, or
// Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project
// and select the .sln file

View File

@ -0,0 +1,19 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_app.h"
void
on_init()
{
printf("Hello, I was installed.\n");
}
void
on_destroy()
{
/* real destroy work including killing timer and closing sensor is
* accomplished in wasm app library version of on_destroy() */
}

View File

@ -1,12 +1,13 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_app.h"
#include "wa-inc/request.h"
void res1_handler(request_t *request)
void
res1_handler(request_t *request)
{
response_t response[1];
attr_container_t *payload;
@ -18,9 +19,9 @@ void res1_handler(request_t *request)
printf("url: %s\n", request->url);
printf("action: %d\n", request->action);
printf("payload:\n");
if (request->payload
!= NULL&& request->payload_len > 0 && request->fmt == FMT_ATTR_CONTAINER)
attr_container_dump((attr_container_t *) request->payload);
if (request->payload != NULL && request->payload_len > 0
&& request->fmt == FMT_ATTR_CONTAINER)
attr_container_dump((attr_container_t *)request->payload);
printf("#### dump request end ###\n");
payload = attr_container_create("wasm app response payload");
@ -31,15 +32,17 @@ void res1_handler(request_t *request)
attr_container_set_string(&payload, "key2", "value2");
make_response_for_request(request, response);
set_response(response, CONTENT_2_05,
FMT_ATTR_CONTAINER, (const char *)payload, attr_container_get_serialize_length(payload));
set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER,
(const char *)payload,
attr_container_get_serialize_length(payload));
printf("reciver: %lu, mid:%d\n", response->reciever, response->mid);
api_response_send(response);
attr_container_destroy(payload);
}
void res2_handler(request_t *request)
void
res2_handler(request_t *request)
{
response_t response[1];
make_response_for_request(request, response);
@ -49,14 +52,17 @@ void res2_handler(request_t *request)
printf("### user resource 2 handler called\n");
}
void on_init()
void
on_init()
{
/* register resource uri */
api_register_resource_handler("/res1", res1_handler);
api_register_resource_handler("/res2", res2_handler);
}
void on_destroy()
void
on_destroy()
{
/* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */
/* real destroy work including killing timer and closing sensor is
* accomplished in wasm app library version of on_destroy() */
}

View File

@ -1,7 +1,7 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_app.h"
#include "wa-inc/timer_wasm_app.h"
@ -9,7 +9,8 @@
int num = 0;
void publish_overheat_event()
void
publish_overheat_event()
{
attr_container_t *event;
@ -19,7 +20,7 @@ void publish_overheat_event()
printf("###app publish event begin ###\n");
api_publish_event("alert/overheat", FMT_ATTR_CONTAINER, event,
attr_container_get_serialize_length(event));
attr_container_get_serialize_length(event));
printf("###app publish event end ###\n");
@ -27,13 +28,15 @@ void publish_overheat_event()
}
/* Timer callback */
void timer1_update(user_timer_t timer)
void
timer1_update(user_timer_t timer)
{
printf("Timer update %d\n", num++);
publish_overheat_event();
}
void start_timer()
void
start_timer()
{
user_timer_t timer;
@ -42,12 +45,15 @@ void start_timer()
api_timer_restart(timer, 1000);
}
void on_init()
void
on_init()
{
start_timer();
}
void on_destroy()
void
on_destroy()
{
/* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */
/* real destroy work including killing timer and closing sensor is
* accomplished in wasm app library version of on_destroy() */
}

View File

@ -1,7 +1,7 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_app.h"
#include "wa-inc/request.h"
@ -9,7 +9,8 @@
uint32 mid;
unsigned long sender;
void my_response_handler(response_t *response, void *user_data)
void
my_response_handler(response_t *response, void *user_data)
{
attr_container_t *payload;
printf("### user resource 1 handler called\n");
@ -23,15 +24,17 @@ void my_response_handler(response_t *response, void *user_data)
response->mid = mid;
response->reciever = sender;
set_response(response, CONTENT_2_05,
FMT_ATTR_CONTAINER, (const char *)payload, attr_container_get_serialize_length(payload));
set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER,
(const char *)payload,
attr_container_get_serialize_length(payload));
printf("reciver: %lu, mid:%d\n", response->reciever, response->mid);
api_response_send(response);
attr_container_destroy(payload);
}
static void test_send_request(const char *url, const char *tag)
static void
test_send_request(const char *url, const char *tag)
{
request_t request[1];
@ -39,28 +42,33 @@ static void test_send_request(const char *url, const char *tag)
api_send_request(request, my_response_handler, (void *)tag);
}
void res1_handler(request_t *request)
void
res1_handler(request_t *request)
{
mid = request->mid;
sender = request->sender;
test_send_request("url1", "a general request");
}
void res2_handler(request_t *request)
void
res2_handler(request_t *request)
{
mid = request->mid;
sender = request->sender;
test_send_request("/app/App1/url1", "a general request");
}
void on_init()
void
on_init()
{
/* register resource uri */
api_register_resource_handler("/res1", res1_handler);
api_register_resource_handler("/res2", res2_handler);
}
void on_destroy()
void
on_destroy()
{
/* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */
/* real destroy work including killing timer and closing sensor is
* accomplished in wasm app library version of on_destroy() */
}

View File

@ -1,12 +1,13 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_app.h"
#include "wa-inc/request.h"
void res1_handler(request_t *request)
void
res1_handler(request_t *request)
{
response_t response[1];
attr_container_t *payload;
@ -19,7 +20,7 @@ void res1_handler(request_t *request)
printf("[resp] action: %d\n", request->action);
printf("[resp] payload:\n");
if (request->payload != NULL && request->fmt == FMT_ATTR_CONTAINER)
attr_container_dump((attr_container_t *) request->payload);
attr_container_dump((attr_container_t *)request->payload);
printf("[resp] #### dump request end ###\n");
payload = attr_container_create("wasm app response payload");
@ -30,23 +31,27 @@ void res1_handler(request_t *request)
attr_container_set_string(&payload, "key2", "value2");
make_response_for_request(request, response);
set_response(response, CONTENT_2_05,
FMT_ATTR_CONTAINER, (const char *)payload, attr_container_get_serialize_length(payload));
set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER,
(const char *)payload,
attr_container_get_serialize_length(payload));
printf("[resp] response payload len %d\n",
attr_container_get_serialize_length(payload));
attr_container_get_serialize_length(payload));
printf("[resp] reciver: %lu, mid:%d\n", response->reciever, response->mid);
api_response_send(response);
attr_container_destroy(payload);
}
void on_init()
void
on_init()
{
/* register resource uri */
api_register_resource_handler("/url1", res1_handler);
}
void on_destroy()
void
on_destroy()
{
/* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */
/* real destroy work including killing timer and closing sensor is
* accomplished in wasm app library version of on_destroy() */
}

View File

@ -1,7 +1,7 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_app.h"
#include "wa-inc/timer_wasm_app.h"
@ -9,7 +9,8 @@
int num = 0;
void publish_overheat_event()
void
publish_overheat_event()
{
attr_container_t *event;
@ -19,7 +20,7 @@ void publish_overheat_event()
printf("###app publish event begin ###\n");
api_publish_event("alert/overheat", FMT_ATTR_CONTAINER, event,
attr_container_get_serialize_length(event));
attr_container_get_serialize_length(event));
printf("###app publish event end ###\n");
@ -27,13 +28,15 @@ void publish_overheat_event()
}
/* Timer callback */
void timer1_update(user_timer_t timer)
void
timer1_update(user_timer_t timer)
{
printf("Timer update %d\n", num++);
publish_overheat_event();
}
void start_timer()
void
start_timer()
{
user_timer_t timer;
@ -42,12 +45,15 @@ void start_timer()
api_timer_restart(timer, 1000);
}
void on_init()
void
on_init()
{
start_timer();
}
void on_destroy()
void
on_destroy()
{
/* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */
/* real destroy work including killing timer and closing sensor is
* accomplished in wasm app library version of on_destroy() */
}

View File

@ -1,7 +1,7 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_app.h"
#include "wa-inc/request.h"
@ -9,7 +9,8 @@
uint32 mid;
unsigned long sender;
void over_heat_event_handler(request_t *request)
void
over_heat_event_handler(request_t *request)
{
response_t response[1];
attr_container_t *payload;
@ -23,28 +24,33 @@ void over_heat_event_handler(request_t *request)
response->mid = mid;
response->reciever = sender;
set_response(response, CONTENT_2_05,
FMT_ATTR_CONTAINER, (const char *)payload, attr_container_get_serialize_length(payload));
set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER,
(const char *)payload,
attr_container_get_serialize_length(payload));
printf("reciver: %lu, mid:%d\n", response->reciever, response->mid);
api_response_send(response);
attr_container_destroy(payload);
}
void res1_handler(request_t *request)
void
res1_handler(request_t *request)
{
mid = request->mid;
sender = request->sender;
api_subscribe_event("alert/overheat", over_heat_event_handler);
}
void on_init()
void
on_init()
{
/* register resource uri */
api_register_resource_handler("/res1", res1_handler);
}
void on_destroy()
void
on_destroy()
{
/* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */
/* real destroy work including killing timer and closing sensor is
* accomplished in wasm app library version of on_destroy() */
}

View File

@ -1,7 +1,7 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_app.h"
#include "wa-inc/request.h"
@ -11,13 +11,15 @@
int num = 0;
/* Timer callback */
void timer1_update(user_timer_t timer)
void
timer1_update(user_timer_t timer)
{
if (num < 2)
num++;
}
void res1_handler(request_t *request)
void
res1_handler(request_t *request)
{
user_timer_t timer;
@ -29,13 +31,13 @@ void res1_handler(request_t *request)
make_response_for_request(request, response);
set_response(response, CONTENT_2_05,
FMT_ATTR_CONTAINER, NULL, 0);
set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER, NULL, 0);
api_response_send(response);
}
void res2_handler(request_t *request)
void
res2_handler(request_t *request)
{
response_t response[1];
attr_container_t *payload;
@ -52,25 +54,27 @@ void res2_handler(request_t *request)
make_response_for_request(request, response);
set_response(response, CONTENT_2_05,
FMT_ATTR_CONTAINER, (const char *)payload,
attr_container_get_serialize_length(payload));
set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER,
(const char *)payload,
attr_container_get_serialize_length(payload));
printf("reciver: %lu, mid:%d\n", response->reciever, response->mid);
api_response_send(response);
attr_container_destroy(payload);
}
}
void on_init()
void
on_init()
{
/* register resource uri */
api_register_resource_handler("/res1", res1_handler);
api_register_resource_handler("/check_timer", res2_handler);
}
void on_destroy()
void
on_destroy()
{
/* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */
/* real destroy work including killing timer and closing sensor is
* accomplished in wasm app library version of on_destroy() */
}

View File

@ -1,7 +1,7 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_app.h"
#include "wa-inc/request.h"
@ -11,8 +11,8 @@ uint32 mid;
unsigned long sender;
/* Sensor event callback*/
void sensor_event_handler(sensor_t sensor, attr_container_t *event,
void *user_data)
void
sensor_event_handler(sensor_t sensor, attr_container_t *event, void *user_data)
{
printf("### app get sensor event\n");
@ -28,15 +28,17 @@ void sensor_event_handler(sensor_t sensor, attr_container_t *event,
response->mid = mid;
response->reciever = sender;
set_response(response, CONTENT_2_05,
FMT_ATTR_CONTAINER, (const char *)payload, attr_container_get_serialize_length(payload));
set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER,
(const char *)payload,
attr_container_get_serialize_length(payload));
printf("reciver: %lu, mid:%d\n", response->reciever, response->mid);
api_response_send(response);
attr_container_destroy(payload);
}
void res1_handler(request_t *request)
void
res1_handler(request_t *request)
{
mid = request->mid;
sender = request->sender;
@ -57,13 +59,16 @@ void res1_handler(request_t *request)
printf("### app on_init 4\n");
}
void on_init()
void
on_init()
{
/* register resource uri */
api_register_resource_handler("/res1", res1_handler);
}
void on_destroy()
void
on_destroy()
{
/* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */
/* real destroy work including killing timer and closing sensor is
* accomplished in wasm app library version of on_destroy() */
}

View File

@ -0,0 +1,70 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_app.h"
#include "wa-inc/request.h"
#include "wa-inc/sensor.h"
uint32 mid;
unsigned long sender;
sensor_t sensor;
/* Sensor event callback*/
void
sensor_event_handler(sensor_t sensor, attr_container_t *event, void *user_data)
{
printf("### app get sensor event\n");
response_t response[1];
attr_container_t *payload;
payload = attr_container_create("wasm app response payload");
if (payload == NULL)
return;
attr_container_set_string(&payload, "key1", "value1");
response->mid = mid;
response->reciever = sender;
set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER,
(const char *)payload,
attr_container_get_serialize_length(payload));
printf("reciver: %lu, mid:%d\n", response->reciever, response->mid);
api_response_send(response);
attr_container_destroy(payload);
}
void
res1_handler(request_t *request)
{
mid = request->mid;
sender = request->sender;
char *user_data;
attr_container_t *config;
printf("### app on_init 1\n");
/* open a sensor */
user_data = malloc(100);
printf("### app on_init 2\n");
sensor = sensor_open("sensor_test", 0, sensor_event_handler, user_data);
printf("### app on_init 3\n");
}
void
on_init()
{
/* register resource uri */
api_register_resource_handler("/res1", res1_handler);
}
void
on_destroy()
{
if (NULL != sensor) {
sensor_close(sensor);
}
}

View File

@ -1,16 +0,0 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_app.h"
void on_init()
{
printf("Hello, I was installed.\n");
}
void on_destroy()
{
/* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */
}

View File

@ -1,67 +0,0 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_app.h"
#include "wa-inc/request.h"
#include "wa-inc/sensor.h"
uint32 mid;
unsigned long sender;
sensor_t sensor;
/* Sensor event callback*/
void sensor_event_handler(sensor_t sensor, attr_container_t *event, void *user_data) {
printf("### app get sensor event\n");
response_t response[1];
attr_container_t *payload;
payload = attr_container_create("wasm app response payload");
if (payload == NULL)
return;
attr_container_set_string(&payload, "key1", "value1");
response->mid = mid;
response->reciever = sender;
set_response(response,
CONTENT_2_05,
FMT_ATTR_CONTAINER,
(const char *)payload,
attr_container_get_serialize_length(payload));
printf("reciver: %lu, mid:%d\n", response->reciever, response->mid);
api_response_send(response);
attr_container_destroy(payload);
}
void res1_handler(request_t *request)
{
mid = request->mid;
sender = request->sender;
char *user_data;
attr_container_t *config;
printf("### app on_init 1\n");
/* open a sensor */
user_data = malloc(100);
printf("### app on_init 2\n");
sensor = sensor_open("sensor_test", 0, sensor_event_handler, user_data);
printf("### app on_init 3\n");
}
void on_init()
{
/* register resource uri */
api_register_resource_handler("/res1", res1_handler);
}
void on_destroy()
{
if(NULL != sensor){
sensor_close(sensor);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -27,51 +27,57 @@
extern "C" {
#endif
#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
#if !defined(__WINDOWS__) \
&& (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) \
|| defined(_WIN32))
#define __WINDOWS__
#endif
#ifdef __WINDOWS__
/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 3 define options:
CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols
CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default)
CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol
For *nix builds that support visibility attribute, you can define similar behavior by
setting default visibility to hidden by adding
-fvisibility=hidden (for gcc)
or
-xldscope=hidden (for sun cc)
to CFLAGS
then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does
/**
* When compiling for windows, we specify a specific calling convention to avoid
* issues where we are being called from a project with a different default
* calling convention. For windows you have 3 define options:
* CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever
* dllexport symbols
* CJSON_EXPORT_SYMBOLS - Define this on library build when you want to
* dllexport symbols (default)
* CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol
*
* For *nix builds that support visibility attribute, you can define similar
* behavior by setting default visibility to hidden by adding
* -fvisibility=hidden (for gcc)
* or
* -xldscope=hidden (for sun cc)
* to CFLAGS, then using the CJSON_API_VISIBILITY flag to "export" the same
* symbols the way CJSON_EXPORT_SYMBOLS does
*/
#define CJSON_CDECL __cdecl
#define CJSON_STDCALL __stdcall
/* export symbols by default, this is necessary for copy pasting the C and header file */
#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS)
/* export symbols by default, this is necessary for copy pasting the C and
header file */
#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) \
&& !defined(CJSON_EXPORT_SYMBOLS)
#define CJSON_EXPORT_SYMBOLS
#endif
#if defined(CJSON_HIDE_SYMBOLS)
#define CJSON_PUBLIC(type) type CJSON_STDCALL
#define CJSON_PUBLIC(type) type CJSON_STDCALL
#elif defined(CJSON_EXPORT_SYMBOLS)
#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL
#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL
#elif defined(CJSON_IMPORT_SYMBOLS)
#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL
#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL
#endif
#else /* !__WINDOWS__ */
#define CJSON_CDECL
#define CJSON_STDCALL
#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY)
#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type
#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined(__SUNPRO_C)) \
&& defined(CJSON_API_VISIBILITY)
#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type
#else
#define CJSON_PUBLIC(type) type
#endif
@ -86,24 +92,26 @@ extern "C" {
/* cJSON Types: */
#define cJSON_Invalid (0)
#define cJSON_False (1 << 0)
#define cJSON_True (1 << 1)
#define cJSON_NULL (1 << 2)
#define cJSON_False (1 << 0)
#define cJSON_True (1 << 1)
#define cJSON_NULL (1 << 2)
#define cJSON_Number (1 << 3)
#define cJSON_String (1 << 4)
#define cJSON_Array (1 << 5)
#define cJSON_Array (1 << 5)
#define cJSON_Object (1 << 6)
#define cJSON_Raw (1 << 7) /* raw json */
#define cJSON_Raw (1 << 7) /* raw json */
#define cJSON_IsReference 256
#define cJSON_StringIsConst 512
/* The cJSON structure: */
typedef struct cJSON {
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
/* next/prev allow you to walk array/object chains. Alternatively, use
GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *next;
struct cJSON *prev;
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
/* An array or object item will have a child pointer pointing to a chain of
the items in the array/object. */
struct cJSON *child;
/* The type of the item, as above. */
@ -116,74 +124,101 @@ typedef struct cJSON {
/* The item's number, if type==cJSON_Number */
double valuedouble;
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
/* The item's name string, if this item is the child of, or is in the list
of subitems of an object. */
char *string;
} cJSON;
typedef struct cJSON_Hooks {
/* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */
/* malloc/free are CDECL on Windows regardless of the default calling
* convention of the compiler, so ensure the hooks allow passing those
* functions directly. */
void *(CJSON_CDECL *malloc_fn)(size_t sz);
void (CJSON_CDECL *free_fn)(void *ptr);
void(CJSON_CDECL *free_fn)(void *ptr);
} cJSON_Hooks;
typedef int cJSON_bool;
/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them.
* This is to prevent stack overflows. */
/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse
them. This is to prevent stack overflows. */
#ifndef CJSON_NESTING_LIMIT
#define CJSON_NESTING_LIMIT 1000
#endif
/* returns the version of cJSON as a string */
CJSON_PUBLIC(const char*) cJSON_Version(void);
CJSON_PUBLIC(const char *) cJSON_Version(void);
/* Supply malloc, realloc and free functions to cJSON */
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks *hooks);
/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
/* Memory Management: the caller is always responsible to free the results from
* all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib
* free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is
* cJSON_PrintPreallocated, where the caller has full responsibility of the
* buffer. */
/* Supply a block of JSON, and this returns a cJSON object you can interrogate.
*/
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
/* ParseWithOpts allows you to require (and check) that the JSON is null
* terminated, and to retrieve the pointer to the final byte parsed. */
/* If you supply a ptr in return_parse_end and parsing fails, then
* return_parse_end will contain a pointer to the error so will match
* cJSON_GetErrorPtr(). */
CJSON_PUBLIC(cJSON *)
cJSON_ParseWithOpts(const char *value, const char **return_parse_end,
cJSON_bool require_null_terminated);
/* Render a cJSON entity to text for transfer/storage. */
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
/* Render a cJSON entity to text for transfer/storage without any formatting. */
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format);
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess
* at the final size. guessing well reduces reallocation. fmt=0 gives
* unformatted, =1 gives formatted */
CJSON_PUBLIC(char *)
cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
/* Render a cJSON entity to text using a buffer already allocated in memory with
* given length. Returns 1 on success and 0 on failure. */
/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will
* use, so to be safe allocate 5 bytes more than you actually need */
CJSON_PUBLIC(cJSON_bool)
cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length,
const cJSON_bool format);
/* Delete a cJSON entity and all subentities. */
CJSON_PUBLIC(void) cJSON_Delete(cJSON *c);
/* Returns the number of items in an array (or object). */
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */
/* Retrieve item number "index" from array "array". Returns NULL if
* unsuccessful. */
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
/* Get item "string" from object. Case insensitive. */
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
CJSON_PUBLIC(cJSON *)
cJSON_GetObjectItem(const cJSON *const object, const char *const string);
CJSON_PUBLIC(cJSON *)
cJSON_GetObjectItemCaseSensitive(const cJSON *const object,
const char *const string);
CJSON_PUBLIC(cJSON_bool)
cJSON_HasObjectItem(const cJSON *object, const char *string);
/* For analysing failed parses. This returns a pointer to the parse error.
* You'll probably need to look a few chars back to make sense of it. Defined
* when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
/* Check if the item is a string and return its valuestring */
CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item);
/* These functions check the type of an item */
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON *const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON *const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON *const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON *const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON *const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON *const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON *const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON *const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON *const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON *const item);
/* These calls create a cJSON item of the appropriate type. */
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
@ -198,10 +233,10 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
/* Create a string where valuestring references a string so
* it will not be freed by cJSON_Delete */
it will not be freed by cJSON_Delete */
CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string);
/* Create an object/arrray that only references it's elements so
* they will not be freed by cJSON_Delete */
they will not be freed by cJSON_Delete */
CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child);
@ -213,64 +248,110 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count);
/* Append item to the specified array/object. */
CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object.
* WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before
* writing to `item->string` */
CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
CJSON_PUBLIC(void)
cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
/* Use this when string is definitely const (i.e. a literal, or as good as), and
* will definitely survive the cJSON object. WARNING: When this function was
* used, make sure to always check that (item->type & cJSON_StringIsConst) is
* zero before writing to `item->string` */
CJSON_PUBLIC(void)
cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
/* Append reference to item to the specified array/object. Use this when you
* want to add an existing cJSON to a new cJSON, but don't want to corrupt your
* existing cJSON. */
CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
CJSON_PUBLIC(void)
cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
/* Remove/Detatch items from Arrays/Objects. */
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
CJSON_PUBLIC(cJSON *)
cJSON_DetachItemViaPointer(cJSON *parent, cJSON *const item);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
CJSON_PUBLIC(cJSON *)
cJSON_DetachItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(cJSON *)
cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
CJSON_PUBLIC(void)
cJSON_DeleteItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(void)
cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
/* Update array items. */
CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement);
CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem);
CJSON_PUBLIC(void)
cJSON_InsertItemInArray(
cJSON *array, int which,
cJSON *newitem); /* Shifts pre-existing items to the right. */
CJSON_PUBLIC(cJSON_bool)
cJSON_ReplaceItemViaPointer(cJSON *const parent, cJSON *const item,
cJSON *replacement);
CJSON_PUBLIC(void)
cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
CJSON_PUBLIC(void)
cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem);
CJSON_PUBLIC(void)
cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string,
cJSON *newitem);
/* Duplicate a cJSON item */
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
need to be released. With recurse!=0, it will duplicate any children connected to the item.
The item->next and ->prev pointers are always zero on return from Duplicate. */
/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal.
* case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */
CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive);
/* Duplicate will create a new, identical cJSON item to the one you pass, in new
memory that will need to be released. With recurse!=0, it will duplicate any
children connected to the item. The item->next and ->prev pointers are always
zero on return from Duplicate. */
/* Recursively compare two cJSON items for equality. If either a or b is NULL or
* invalid, they will be considered unequal.
* case_sensitive determines if object keys are treated case sensitive (1) or
* case insensitive (0) */
CJSON_PUBLIC(cJSON_bool)
cJSON_Compare(const cJSON *const a, const cJSON *const b,
const cJSON_bool case_sensitive);
CJSON_PUBLIC(void) cJSON_Minify(char *json);
/* Helper functions for creating and adding items to an object at the same time.
* They return the added item or NULL on failure. */
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name);
They return the added item or NULL on failure. */
CJSON_PUBLIC(cJSON *)
cJSON_AddNullToObject(cJSON *const object, const char *const name);
CJSON_PUBLIC(cJSON *)
cJSON_AddTrueToObject(cJSON *const object, const char *const name);
CJSON_PUBLIC(cJSON *)
cJSON_AddFalseToObject(cJSON *const object, const char *const name);
CJSON_PUBLIC(cJSON *)
cJSON_AddBoolToObject(cJSON *const object, const char *const name,
const cJSON_bool boolean);
CJSON_PUBLIC(cJSON *)
cJSON_AddNumberToObject(cJSON *const object, const char *const name,
const double number);
CJSON_PUBLIC(cJSON *)
cJSON_AddStringToObject(cJSON *const object, const char *const name,
const char *const string);
CJSON_PUBLIC(cJSON *)
cJSON_AddRawToObject(cJSON *const object, const char *const name,
const char *const raw);
CJSON_PUBLIC(cJSON *)
cJSON_AddObjectToObject(cJSON *const object, const char *const name);
CJSON_PUBLIC(cJSON *)
cJSON_AddArrayToObject(cJSON *const object, const char *const name);
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
/* When assigning an integer value, it needs to be propagated to valuedouble
too. */
#define cJSON_SetIntValue(object, number) \
((object) ? (object)->valueint = (object)->valuedouble = (number) \
: (number))
/* helper for the cJSON_SetNumberValue macro */
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
#define cJSON_SetNumberValue(object, number) \
((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) \
: (number))
/* Macro for iterating over an array or object */
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
#define cJSON_ArrayForEach(element, array) \
for (element = (array != NULL) ? (array)->child : NULL; element != NULL; \
element = element->next)
/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */
/* malloc/free objects using the malloc/free functions that have been set with
cJSON_InitHooks */
CJSON_PUBLIC(void *) cJSON_malloc(size_t size);
CJSON_PUBLIC(void) cJSON_free(void *object);

View File

@ -24,37 +24,40 @@ typedef union jvalue {
double d;
} jvalue;
static inline int16_t get_int16(const char *buf)
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)
static inline uint16_t
get_uint16(const char *buf)
{
return get_int16(buf);
}
static inline int32_t get_int32(const char *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)
static inline uint32_t
get_uint32(const char *buf)
{
return get_int32(buf);
}
char* attr_container_get_attr_begin(const attr_container_t *attr_cont,
uint32_t *p_total_length,
uint16_t *p_attr_num);
char *
attr_container_get_attr_begin(const attr_container_t *attr_cont,
uint32_t *p_total_length, uint16_t *p_attr_num);
cJSON *attr2json(const attr_container_t *attr_cont)
cJSON *
attr2json(const attr_container_t *attr_cont)
{
uint32_t total_length;
uint16_t attr_num, i, j, type;
@ -87,91 +90,95 @@ cJSON *attr2json(const attr_container_t *attr_cont)
type = *p++;
switch (type) {
case ATTR_TYPE_SHORT:
bh_memcpy_s(&value.s, sizeof(int16_t), p, sizeof(int16_t));
if (NULL == (obj = cJSON_CreateNumber(value.s)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
/* another approach: cJSON_AddNumberToObject(root, key, value.s) */
p += 2;
break;
case ATTR_TYPE_INT:
bh_memcpy_s(&value.i, sizeof(int32_t), p, sizeof(int32_t));
if (NULL == (obj = cJSON_CreateNumber(value.i)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p += 4;
break;
case ATTR_TYPE_INT64:
bh_memcpy_s(&value.j, sizeof(uint64_t), p, sizeof(uint64_t));
if (NULL == (obj = cJSON_CreateNumber(value.j)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p += 8;
break;
case ATTR_TYPE_BYTE:
bh_memcpy_s(&value.b, 1, p, 1);
if (NULL == (obj = cJSON_CreateNumber(value.b)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p++;
break;
case ATTR_TYPE_UINT16:
bh_memcpy_s(&value.c, sizeof(uint16_t), p, sizeof(uint16_t));
if (NULL == (obj = cJSON_CreateNumber(value.c)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p += 2;
break;
case ATTR_TYPE_FLOAT:
bh_memcpy_s(&value.f, sizeof(float), p, sizeof(float));
if (NULL == (obj = cJSON_CreateNumber(value.f)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p += 4;
break;
case ATTR_TYPE_DOUBLE:
bh_memcpy_s(&value.d, sizeof(double), p, sizeof(double));
if (NULL == (obj = cJSON_CreateNumber(value.d)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p += 8;
break;
case ATTR_TYPE_BOOLEAN:
bh_memcpy_s(&value.z, 1, p, 1);
if (NULL == (obj = cJSON_CreateBool(value.z)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p++;
break;
case ATTR_TYPE_STRING:
if (NULL == (obj = cJSON_CreateString(p + sizeof(uint16_t))))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p += sizeof(uint16_t) + get_uint16(p);
break;
case ATTR_TYPE_BYTEARRAY:
if (NULL == (obj = cJSON_CreateArray()))
goto fail;
cJSON_AddItemToObject(root, key, obj);
for (j = 0; j < get_uint32(p); j++) {
cJSON *item = cJSON_CreateNumber(*(p + sizeof(uint32_t) + j));
if (item == NULL)
case ATTR_TYPE_SHORT:
bh_memcpy_s(&value.s, sizeof(int16_t), p, sizeof(int16_t));
if (NULL == (obj = cJSON_CreateNumber(value.s)))
goto fail;
cJSON_AddItemToArray(obj, item);
}
p += sizeof(uint32_t) + get_uint32(p);
break;
cJSON_AddItemToObject(root, key, obj);
/* another approach: cJSON_AddNumberToObject(root, key, value.s)
*/
p += 2;
break;
case ATTR_TYPE_INT:
bh_memcpy_s(&value.i, sizeof(int32_t), p, sizeof(int32_t));
if (NULL == (obj = cJSON_CreateNumber(value.i)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p += 4;
break;
case ATTR_TYPE_INT64:
bh_memcpy_s(&value.j, sizeof(uint64_t), p, sizeof(uint64_t));
if (NULL == (obj = cJSON_CreateNumber(value.j)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p += 8;
break;
case ATTR_TYPE_BYTE:
bh_memcpy_s(&value.b, 1, p, 1);
if (NULL == (obj = cJSON_CreateNumber(value.b)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p++;
break;
case ATTR_TYPE_UINT16:
bh_memcpy_s(&value.c, sizeof(uint16_t), p, sizeof(uint16_t));
if (NULL == (obj = cJSON_CreateNumber(value.c)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p += 2;
break;
case ATTR_TYPE_FLOAT:
bh_memcpy_s(&value.f, sizeof(float), p, sizeof(float));
if (NULL == (obj = cJSON_CreateNumber(value.f)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p += 4;
break;
case ATTR_TYPE_DOUBLE:
bh_memcpy_s(&value.d, sizeof(double), p, sizeof(double));
if (NULL == (obj = cJSON_CreateNumber(value.d)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p += 8;
break;
case ATTR_TYPE_BOOLEAN:
bh_memcpy_s(&value.z, 1, p, 1);
if (NULL == (obj = cJSON_CreateBool(value.z)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p++;
break;
case ATTR_TYPE_STRING:
if (NULL == (obj = cJSON_CreateString(p + sizeof(uint16_t))))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p += sizeof(uint16_t) + get_uint16(p);
break;
case ATTR_TYPE_BYTEARRAY:
if (NULL == (obj = cJSON_CreateArray()))
goto fail;
cJSON_AddItemToObject(root, key, obj);
for (j = 0; j < get_uint32(p); j++) {
cJSON *item =
cJSON_CreateNumber(*(p + sizeof(uint32_t) + j));
if (item == NULL)
goto fail;
cJSON_AddItemToArray(obj, item);
}
p += sizeof(uint32_t) + get_uint32(p);
break;
}
}
return root;
fail: cJSON_Delete(root);
fail:
cJSON_Delete(root);
return NULL;
}
attr_container_t *json2attr(const cJSON *json_obj)
attr_container_t *
json2attr(const cJSON *json_obj)
{
attr_container_t *attr_cont;
cJSON *item;
@ -187,20 +194,24 @@ attr_container_t *json2attr(const cJSON *json_obj)
if (cJSON_IsNumber(item)) {
attr_container_set_double(&attr_cont, item->string,
item->valuedouble);
} else if (cJSON_IsTrue(item)) {
item->valuedouble);
}
else if (cJSON_IsTrue(item)) {
attr_container_set_bool(&attr_cont, item->string, true);
} else if (cJSON_IsFalse(item)) {
}
else if (cJSON_IsFalse(item)) {
attr_container_set_bool(&attr_cont, item->string, false);
} else if (cJSON_IsString(item)) {
}
else if (cJSON_IsString(item)) {
attr_container_set_string(&attr_cont, item->string,
item->valuestring);
} else if (cJSON_IsArray(item)) {
item->valuestring);
}
else if (cJSON_IsArray(item)) {
int8_t *array;
int i = 0, len = sizeof(int8_t) * cJSON_GetArraySize(item);
cJSON *array_item;
if (0 == len || NULL == (array = (int8_t *) malloc(len)))
if (0 == len || NULL == (array = (int8_t *)malloc(len)))
goto fail;
memset(array, 0, len);
@ -210,24 +221,26 @@ attr_container_t *json2attr(const cJSON *json_obj)
if (!cJSON_IsNumber(array_item))
break;
/* TODO: if array_item->valuedouble > 127 or < -128 */
array[i++] = (int8_t) array_item->valuedouble;
array[i++] = (int8_t)array_item->valuedouble;
}
if (i > 0)
attr_container_set_bytearray(&attr_cont, item->string, array,
i);
i);
free(array);
}
}
return attr_cont;
fail: attr_container_destroy(attr_cont);
fail:
attr_container_destroy(attr_cont);
return NULL;
}
int g_mid = 0;
int gen_random_id()
int
gen_random_id()
{
static bool init = false;
int r;
@ -283,7 +296,8 @@ read_file_to_buffer(const char *filename, int *ret_size)
return buffer;
}
int wirte_buffer_to_file(const char *filename, const char *buffer, int size)
int
wirte_buffer_to_file(const char *filename, const char *buffer, int size)
{
int file, ret;

View File

@ -22,7 +22,8 @@ extern "C" {
*
* @warning the return object should be deleted with cJSON_Delete by caller
*/
cJSON *attr2json(const attr_container_t *attr);
cJSON *
attr2json(const attr_container_t *attr);
/**
* @brief Convert cJSON object to attribute container object.
@ -33,14 +34,16 @@ cJSON *attr2json(const attr_container_t *attr);
*
* @warning the return object should be deleted with attr_container_destroy
*/
attr_container_t *json2attr(const cJSON *json);
attr_container_t *
json2attr(const cJSON *json);
/**
* @brief Generate a random 32 bit integer.
*
* @return the generated random integer
*/
int gen_random_id();
int
gen_random_id();
/**
* @brief Read file content to buffer.
@ -48,11 +51,13 @@ int gen_random_id();
* @param filename the file name to read
* @param ret_size pointer of integer to save file size once return success
*
* @return the created buffer which contains file content if not NULL, NULL means fail
* @return the created buffer which contains file content if not NULL, NULL
* means fail
*
* @warning the return buffer should be deleted with free by caller
*/
char *read_file_to_buffer(const char *filename, int *ret_size);
char *
read_file_to_buffer(const char *filename, int *ret_size);
/**
* @brief Write buffer content to file.
@ -63,7 +68,8 @@ char *read_file_to_buffer(const char *filename, int *ret_size);
*
* @return < 0 means fail, > 0 means the number of bytes actually written
*/
int wirte_buffer_to_file(const char *filename, const char *buffer, int size);
int
wirte_buffer_to_file(const char *filename, const char *buffer, int size);
#ifdef __cplusplus
} /* end of extern "C" */

View File

@ -17,7 +17,7 @@
#include "coap_ext.h"
#include "cJSON.h"
#include "app_manager_export.h" /* for Module_WASM_App */
#include "host_link.h" /* for REQUEST_PACKET */
#include "host_link.h" /* for REQUEST_PACKET */
#include "transport.h"
#define BUF_SIZE 1024
@ -105,7 +105,8 @@ extern int g_mid;
extern unsigned char leading[2];
/* -1 fail, 0 success */
static int send_request(request_t *request, uint16_t msg_type)
static int
send_request(request_t *request, uint16_t msg_type)
{
char *req_p;
int req_size, req_size_n, ret = -1;
@ -119,12 +120,12 @@ static int send_request(request_t *request, uint16_t msg_type)
/* message type */
msg_type = htons(msg_type);
if (!host_tool_send_data(g_conn_fd, (char *) &msg_type, sizeof(msg_type)))
if (!host_tool_send_data(g_conn_fd, (char *)&msg_type, sizeof(msg_type)))
goto ret;
/* payload length */
req_size_n = htonl(req_size);
if (!host_tool_send_data(g_conn_fd, (char *) &req_size_n,
if (!host_tool_send_data(g_conn_fd, (char *)&req_size_n,
sizeof(req_size_n)))
goto ret;
@ -144,7 +145,8 @@ ret:
/**
* return: 0: success, others: fail
*/
static int install(inst_info *info)
static int
install(inst_info *info)
{
request_t request[1] = { 0 };
char *app_file_buf;
@ -155,25 +157,25 @@ static int install(inst_info *info)
if (info->module_type != NULL && url_remain_space > 0)
snprintf(url + strlen(url), url_remain_space, "&type=%s",
info->module_type);
info->module_type);
if (info->heap_size > 0 && url_remain_space > 0)
snprintf(url + strlen(url), url_remain_space, "&heap=%d",
info->heap_size);
info->heap_size);
if (info->timers > 0 && url_remain_space > 0)
snprintf(url + strlen(url), url_remain_space, "&timers=%d",
info->timers);
info->timers);
if (info->watchdog_interval > 0 && url_remain_space > 0)
snprintf(url + strlen(url), url_remain_space, "&wd=%d",
info->watchdog_interval);
info->watchdog_interval);
if ((app_file_buf = read_file_to_buffer(info->file, &app_size)) == NULL)
return -1;
init_request(request, url, COAP_PUT, FMT_APP_RAW_BINARY,
app_file_buf, app_size);
init_request(request, url, COAP_PUT, FMT_APP_RAW_BINARY, app_file_buf,
app_size);
request->mid = gen_random_id();
if (info->module_type == NULL || strcmp(info->module_type, "wasm") == 0)
@ -186,7 +188,8 @@ static int install(inst_info *info)
return ret;
}
static int uninstall(uninst_info *info)
static int
uninstall(uninst_info *info)
{
request_t request[1] = { 0 };
char url[URL_MAX_LEN] = { 0 };
@ -197,14 +200,14 @@ static int uninstall(uninst_info *info)
snprintf(url + strlen(url), url_remain_space, "&type=%s",
info->module_type);
init_request(request, url, COAP_DELETE, FMT_ATTR_CONTAINER,
NULL, 0);
init_request(request, url, COAP_DELETE, FMT_ATTR_CONTAINER, NULL, 0);
request->mid = gen_random_id();
return send_request(request, REQUEST_PACKET);
}
static int query(query_info *info)
static int
query(query_info *info)
{
request_t request[1] = { 0 };
char url[URL_MAX_LEN] = { 0 };
@ -220,7 +223,8 @@ static int query(query_info *info)
return send_request(request, REQUEST_PACKET);
}
static int request(req_info *info)
static int
request(req_info *info)
{
request_t request[1] = { 0 };
attr_container_t *payload = NULL;
@ -232,7 +236,8 @@ static int request(req_info *info)
int payload_file_size;
if ((payload_file = read_file_to_buffer(info->json_payload_file,
&payload_file_size)) == NULL)
&payload_file_size))
== NULL)
return -1;
if (NULL == (json = cJSON_Parse(payload_file))) {
@ -251,8 +256,8 @@ static int request(req_info *info)
free(payload_file);
}
init_request(request, (char *)info->url, info->action,
FMT_ATTR_CONTAINER, payload, payload_len);
init_request(request, (char *)info->url, info->action, FMT_ATTR_CONTAINER,
payload, payload_len);
request->mid = gen_random_id();
ret = send_request(request, REQUEST_PACKET);
@ -268,7 +273,8 @@ fail:
* TODO: currently only support 1 url.
* how to handle multiple responses and set process's exit code?
*/
static int subscribe(reg_info *info)
static int
subscribe(reg_info *info)
{
request_t request[1] = { 0 };
int ret = -1;
@ -293,15 +299,15 @@ static int subscribe(reg_info *info)
char url[URL_MAX_LEN] = { 0 };
char *prefix = info->urls[0] == '/' ? "/event" : "/event/";
snprintf(url, URL_MAX_LEN, "%s%s", prefix, info->urls);
init_request(request, url, COAP_PUT, FMT_ATTR_CONTAINER,
NULL, 0);
init_request(request, url, COAP_PUT, FMT_ATTR_CONTAINER, NULL, 0);
request->mid = gen_random_id();
ret = send_request(request, REQUEST_PACKET);
#endif
return ret;
}
static int unsubscribe(unreg_info *info)
static int
unsubscribe(unreg_info *info)
{
request_t request[1] = { 0 };
int ret = -1;
@ -325,15 +331,15 @@ static int unsubscribe(unreg_info *info)
#else
char url[URL_MAX_LEN] = { 0 };
snprintf(url, URL_MAX_LEN, "%s%s", "/event/", info->urls);
init_request(request, url, COAP_DELETE, FMT_ATTR_CONTAINER,
NULL, 0);
init_request(request, url, COAP_DELETE, FMT_ATTR_CONTAINER, NULL, 0);
request->mid = gen_random_id();
ret = send_request(request, REQUEST_PACKET);
#endif
return ret;
}
static int init()
static int
init()
{
if (g_connection_mode == CONNECTION_MODE_TCP) {
int fd;
@ -353,12 +359,14 @@ static int init()
return -1;
}
static void deinit()
static void
deinit()
{
close(g_conn_fd);
}
static int parse_action(const char *str)
static int
parse_action(const char *str)
{
if (strcasecmp(str, "PUT") == 0)
return COAP_PUT;
@ -371,70 +379,55 @@ static int parse_action(const char *str)
return -1;
}
/* clang-format off */
static void showUsage()
{
printf("\n");
printf("Usage:\n\thost_tool -i|-u|-q|-r|-s|-d ...\n\n");
printf("Usages:\n");
printf(" host_tool -i|-u|-q|-r|-s|-d ...\n");
printf(" host_tool -i <App Name> -f <App File>\n"
" [--type=<App Type>]\n"
" [--heap=<Heap Size>]\n"
" [--timers=<Timers Number>]\n"
" [--watchdog=<Watchdog Interval>]\n"
" [<Control Options> ...] \n");
printf(" host_tool -u <App Name> [<Control Options> ...]\n");
printf(" host_tool -q [<App Name>] [<Control Options> ...]\n");
printf(" host_tool -r <Resource URL> -A <Action> [-p <Payload File>] [<Control Options> ...]\n");
printf(" host_tool -s <Event URLs> [<Control Options> ...]\n");
printf(" host_tool -d <Event URLs> [<Control Options> ...]\n");
printf("\thost_tool -i <App Name> -f <App File>\n"
"\t\t [--type=<App Type>]\n"
"\t\t [--heap=<Heap Size>]\n"
"\t\t [--timers=<Timers Number>]\n"
"\t\t [--watchdog=<Watchdog Interval>]\n"
"\t\t [<Control Options> ...] \n");
printf("\thost_tool -u <App Name> [<Control Options> ...]\n");
printf("\thost_tool -q[<App Name>][<Control Options> ...]\n");
printf(
"\thost_tool -r <Resource URL> -A <Action> [-p <Payload File>] [<Control Options> ...]\n");
printf("\thost_tool -s <Event URLs> [<Control Options> ...]\n");
printf("\thost_tool -d <Event URLs> [<Control Options> ...]\n\n");
printf("\nGeneral Options:\n");
printf(" -i, --install Install an application\n");
printf(" -u, --uninstall Uninstall an application\n");
printf(" -q, --query Query all applications\n");
printf(" -r, --request Send a request\n");
printf(" -s, --register Register event(s)\n");
printf(" -d, --deregister De-register event(s)\n");
printf(" -f, --file Specify app binary file path\n");
printf(" -A, --action Specify action of the request\n");
printf(" -p, --payload Specify payload of the request\n");
printf(
"\t-i, --install Install an application\n");
printf(
"\t-u, --uninstall Uninstall an application\n");
printf(
"\t-q, --query Query all applications\n");
printf("\t-r, --request Send a request\n");
printf("\t-s, --register Register event(s)\n");
printf("\t-d, --deregister De-register event(s)\n");
printf(
"\t-f, --file Specify app binary file path\n");
printf(
"\t-A, --action Specify action of the request\n");
printf(
"\t-p, --payload Specify payload of the request\n");
printf("\n");
printf("\n\tControl Options:\n");
printf(" \t-S <Address>|--address=<Address> Set server address, default to 127.0.0.1\n");
printf(" \t-P <Port>|--port=<Port> Set server port, default to 8888\n");
printf(" \t-D <Device>|--uart=<Device> Set uart device, default to /dev/ttyS2\n");
printf(" \t-B <Baudrate>|--baudrate=<Baudrate> Set uart device baudrate, default to 115200\n");
printf(
"\t-t <timeout>|--timeout=<timeout> Operation timeout in ms, default to 5000\n");
printf(
"\t-a <alive_time>|--alive=<alive_time> Alive time in ms after last operation done, default to 0\n");
printf(
"\t-o <output_file>|--output=<output_file> Redirect the output to output a file\n");
printf(
"\t-U <udp_port>|--udp=<udp_port> Redirect the output to an UDP port in local machine\n");
printf("\nControl Options:\n");
printf(" -S <Address>|--address=<Address> Set server address, default to 127.0.0.1\n");
printf(" -P <Port>|--port=<Port> Set server port, default to 8888\n");
printf(" -D <Device>|--uart=<Device> Set uart device, default to /dev/ttyS2\n");
printf(" -B <Baudrate>|--baudrate=<Baudrate> Set uart device baudrate, default to 115200\n");
printf(" -t <timeout>|--timeout=<timeout> Operation timeout in ms, default to 5000\n");
printf(" -a <alive_time>|--alive=<alive_time> Alive time in ms after last operation done, default to 0\n");
printf(" -o <output_file>|--output=<output_file> Redirect the output to output a file\n");
printf(" -U <udp_port>|--udp=<udp_port> Redirect the output to an UDP port in local machine\n");
printf("\nNotes:\n");
printf("\t<App Name>=name of the application\n");
printf("\t<App File>=path of the application binary file in wasm format\n");
printf(
"\t<Resource URL>=resource descriptor, such as /app/<App Name>/res1 or /res1\n");
printf(
"\t<Event URLs>=event url list separated by ',', such as /event1,/event2,/event3\n");
printf(
"\t<Action>=action of the request, can be PUT, GET, DELETE or POST (case insensitive)\n");
printf("\t<Payload File>=path of the payload file in json format\n");
printf("\t<App Type>=Type of app. Can be 'wasm'(default) or 'jeff'\n");
printf("\t<Heap Size>=Heap size of app.\n");
printf("\t<Timers Number>=Max timers number app can use.\n");
printf("\t<Watchdog Interval>=Watchdog interval in ms.\n");
printf(" <App Name>=name of the application\n");
printf(" <App File>=path of the application binary file in wasm format\n");
printf(" <Resource URL>=resource descriptor, such as /app/<App Name>/res1 or /res1\n");
printf(" <Event URLs>=event url list separated by ',', such as /event1,/event2,/event3\n");
printf(" <Action>=action of the request, can be PUT, GET, DELETE or POST (case insensitive)\n");
printf(" <Payload File>=path of the payload file in json format\n");
printf(" <App Type>=Type of app. Can be 'wasm'(default) or 'jeff'\n");
printf(" <Heap Size>=Heap size of app.\n");
printf(" <Timers Number>=Max timers number app can use.\n");
printf(" <Watchdog Interval>=Watchdog interval in ms.\n");
}
#define CHECK_DUPLICATE_OPERATION do { \

View File

@ -22,7 +22,8 @@
unsigned char leading[2] = { 0x12, 0x34 };
bool tcp_init(const char *address, uint16_t port, int *fd)
bool
tcp_init(const char *address, uint16_t port, int *fd)
{
int sock;
struct sockaddr_in servaddr;
@ -35,7 +36,7 @@ bool tcp_init(const char *address, uint16_t port, int *fd)
servaddr.sin_addr.s_addr = inet_addr(address);
servaddr.sin_port = htons(port);
if (connect(sock, (SA*) &servaddr, sizeof(servaddr)) != 0) {
if (connect(sock, (SA *)&servaddr, sizeof(servaddr)) != 0) {
close(sock);
return false;
}
@ -44,7 +45,8 @@ bool tcp_init(const char *address, uint16_t port, int *fd)
return true;
}
int parse_baudrate(int baud)
int
parse_baudrate(int baud)
{
switch (baud) {
case 9600:
@ -88,7 +90,8 @@ int parse_baudrate(int baud)
}
}
bool uart_init(const char *device, int baudrate, int *fd)
bool
uart_init(const char *device, int baudrate, int *fd)
{
int uart_fd;
struct termios uart_term;
@ -118,12 +121,13 @@ bool uart_init(const char *device, int baudrate, int *fd)
return true;
}
bool udp_send(const char *address, int port, const char *buf, int len)
bool
udp_send(const char *address, int port, const char *buf, int len)
{
int sockfd;
struct sockaddr_in servaddr;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
return false;
memset(&servaddr, 0, sizeof(servaddr));
@ -132,14 +136,15 @@ bool udp_send(const char *address, int port, const char *buf, int len)
servaddr.sin_port = htons(port);
servaddr.sin_addr.s_addr = INADDR_ANY;
sendto(sockfd, buf, len, MSG_CONFIRM,
(const struct sockaddr *)&servaddr, sizeof(servaddr));
sendto(sockfd, buf, len, MSG_CONFIRM, (const struct sockaddr *)&servaddr,
sizeof(servaddr));
close(sockfd);
return true;
}
bool host_tool_send_data(int fd, const char *buf, unsigned int len)
bool
host_tool_send_data(int fd, const char *buf, unsigned int len)
{
int cnt = 0;
ssize_t ret;
@ -170,7 +175,11 @@ resend:
return (ret == len);
}
#define SET_RECV_PHASE(ctx, new_phase) {ctx->phase = new_phase; ctx->size_in_phase = 0;}
#define SET_RECV_PHASE(ctx, new_phase) \
do { \
ctx->phase = new_phase; \
ctx->size_in_phase = 0; \
} while (0)
/*
* input: 1 byte from remote
@ -180,7 +189,8 @@ resend:
* 0 completed packet
* 2 in receiving payload
*/
int on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx)
int
on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx)
{
if (ctx->phase == Phase_Non_Start) {
if (ctx->message.payload) {
@ -206,7 +216,7 @@ int on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx)
}
}
else if (ctx->phase == Phase_Type) {
unsigned char *p = (unsigned char *) &ctx->message.message_type;
unsigned char *p = (unsigned char *)&ctx->message.message_type;
p[ctx->size_in_phase++] = ch;
if (ctx->size_in_phase == sizeof(ctx->message.message_type)) {
@ -215,7 +225,7 @@ int on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx)
}
}
else if (ctx->phase == Phase_Size) {
unsigned char * p = (unsigned char *) &ctx->message.payload_size;
unsigned char *p = (unsigned char *)&ctx->message.payload_size;
p[ctx->size_in_phase++] = ch;
if (ctx->size_in_phase == sizeof(ctx->message.payload_size)) {
@ -233,7 +243,7 @@ int on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx)
return 0;
}
ctx->message.payload = (char *) malloc(ctx->message.payload_size);
ctx->message.payload = (char *)malloc(ctx->message.payload_size);
SET_RECV_PHASE(ctx, Phase_Payload);
}
}

View File

@ -19,7 +19,11 @@ typedef struct {
/* The receive phase of IMRT link message */
typedef enum {
Phase_Non_Start, Phase_Leading, Phase_Type, Phase_Size, Phase_Payload
Phase_Non_Start,
Phase_Leading,
Phase_Type,
Phase_Size,
Phase_Payload
} recv_phase_t;
/* The receive context of IMRT link message */
@ -38,7 +42,8 @@ typedef struct {
*
* @return true if success, false if fail
*/
bool host_tool_send_data(int fd, const char *buf, unsigned int len);
bool
host_tool_send_data(int fd, const char *buf, unsigned int len);
/**
* @brief Handle one byte of IMRT link message
@ -51,7 +56,8 @@ bool host_tool_send_data(int fd, const char *buf, unsigned int len);
* 0 completed packet
* 2 in receiving payload
*/
int on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx);
int
on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx);
/**
* @brief Initialize TCP connection with remote server.
@ -62,7 +68,8 @@ int on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx);
*
* @return true if success, false if fail
*/
bool tcp_init(const char *address, uint16_t port, int *fd);
bool
tcp_init(const char *address, uint16_t port, int *fd);
/**
* @brief Initialize UART connection with remote.
@ -73,7 +80,8 @@ bool tcp_init(const char *address, uint16_t port, int *fd);
*
* @return true if success, false if fail
*/
bool uart_init(const char *device, int baudrate, int *fd);
bool
uart_init(const char *device, int baudrate, int *fd);
/**
* @brief Parse UART baudrate from an integer
@ -90,7 +98,8 @@ bool uart_init(const char *device, int baudrate, int *fd);
* ...
* @endcode
*/
int parse_baudrate(int baud);
int
parse_baudrate(int baud);
/**
* @brief Send data over UDP.
@ -102,7 +111,8 @@ int parse_baudrate(int baud);
*
* @return true if success, false if fail
*/
bool udp_send(const char *address, int port, const char *buf, int len);
bool
udp_send(const char *address, int port, const char *buf, int len);
#ifdef __cplusplus
} /* end of extern "C" */