Apply clang format for samples files (#833)

Apply clang format for c source files under samples folder
This commit is contained in:
Wenyong Huang
2021-11-15 12:48:35 +08:00
committed by GitHub
parent 37a14c9825
commit 3ded9ece83
58 changed files with 1261 additions and 957 deletions

View File

@ -45,11 +45,16 @@ static char *uart_device = "/dev/ttyS2";
static int baudrate = B115200;
#endif
extern void init_sensor_framework();
extern void exit_sensor_framework();
extern void exit_connection_framework();
extern int aee_host_msg_callback(void *msg, uint32_t msg_len);
extern bool init_connection_framework();
extern void
init_sensor_framework();
extern void
exit_sensor_framework();
extern void
exit_connection_framework();
extern int
aee_host_msg_callback(void *msg, uint32_t msg_len);
extern bool
init_connection_framework();
#ifndef CONNECTION_UART
int listenfd = -1;
@ -63,7 +68,8 @@ int uartfd = -1;
static bool server_mode = false;
// Function designed for chat between client and server.
void* func(void* arg)
void *
func(void *arg)
{
char buff[MAX];
int n;
@ -77,7 +83,8 @@ void* func(void* arg)
if (sockfd == -1) {
printf("socket creation failed...\n");
return NULL;
} else
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
@ -86,11 +93,12 @@ void* func(void* arg)
servaddr.sin_port = htons(port);
// connect the client socket to server socket
if (connect(sockfd, (SA*) &servaddr, sizeof(servaddr)) != 0) {
if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) != 0) {
printf("connection with the server failed...\n");
sleep(10);
continue;
} else {
}
else {
printf("connected to the server..\n");
}
@ -101,7 +109,7 @@ void* func(void* arg)
// read the message from client and copy it in buffer
n = read(sockfd, buff, sizeof(buff));
// print buffer which contains the client contents
//fprintf(stderr, "recieved %d bytes from host: %s", n, buff);
// fprintf(stderr, "recieved %d bytes from host: %s", n, buff);
// socket disconnected
if (n <= 0)
@ -115,12 +123,14 @@ void* func(void* arg)
close(sockfd);
}
static bool host_init()
static bool
host_init()
{
return true;
}
int host_send(void * ctx, const char *buf, int size)
int
host_send(void *ctx, const char *buf, int size)
{
int ret;
@ -139,7 +149,8 @@ int host_send(void * ctx, const char *buf, int size)
return -1;
}
void host_destroy()
void
host_destroy()
{
if (server_mode)
close(listenfd);
@ -149,16 +160,19 @@ void host_destroy()
pthread_mutex_unlock(&sock_lock);
}
/* clang-format off */
host_interface interface = {
.init = host_init,
.send = host_send,
.destroy = host_destroy
};
/* clang-format on */
/* Change it to 1 when fuzzing test */
#define WASM_ENABLE_FUZZ_TEST 0
void* func_server_mode(void* arg)
void *
func_server_mode(void *arg)
{
int clilent;
struct sockaddr_in serv_addr, cli_addr;
@ -178,14 +192,14 @@ void* func_server_mode(void* arg)
}
/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
/* Now bind the host address using bind() call.*/
if (bind(listenfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
if (bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
exit(1);
}
@ -196,7 +210,7 @@ void* func_server_mode(void* arg)
while (1) {
pthread_mutex_lock(&sock_lock);
sockfd = accept(listenfd, (struct sockaddr *) &cli_addr, &clilent);
sockfd = accept(listenfd, (struct sockaddr *)&cli_addr, &clilent);
pthread_mutex_unlock(&sock_lock);
@ -228,7 +242,7 @@ void* func_server_mode(void* arg)
}
#if WASM_ENABLE_FUZZ_TEST != 0
/* Exit the process when host disconnect.
* This is helpful for reproducing failure case. */
This is helpful for reproducing failure case. */
close(sockfd);
exit(1);
#endif
@ -236,7 +250,8 @@ void* func_server_mode(void* arg)
}
#else
static int parse_baudrate(int baud)
static int
parse_baudrate(int baud)
{
switch (baud) {
case 9600:
@ -279,7 +294,8 @@ static int parse_baudrate(int baud)
return -1;
}
}
static bool uart_init(const char *device, int baudrate, int *fd)
static bool
uart_init(const char *device, int baudrate, int *fd)
{
int uart_fd;
struct termios uart_term;
@ -310,7 +326,8 @@ static bool uart_init(const char *device, int baudrate, int *fd)
return true;
}
static void *func_uart_mode(void *arg)
static void *
func_uart_mode(void *arg)
{
int n;
char buff[MAX];
@ -337,7 +354,8 @@ static void *func_uart_mode(void *arg)
return NULL;
}
static int uart_send(void * ctx, const char *buf, int size)
static int
uart_send(void *ctx, const char *buf, int size)
{
int ret;
@ -346,23 +364,28 @@ static int uart_send(void * ctx, const char *buf, int size)
return ret;
}
static void uart_destroy()
static void
uart_destroy()
{
close(uartfd);
}
static host_interface interface = { .send = uart_send, .destroy = uart_destroy };
/* clang-format off */
static host_interface interface = {
.send = uart_send,
.destroy = uart_destroy
};
/* clang-format on */
#endif
static attr_container_t * read_test_sensor(void * sensor)
static attr_container_t *
read_test_sensor(void *sensor)
{
//luc: for test
attr_container_t *attr_obj = attr_container_create("read test sensor data");
if (attr_obj) {
bool ret = attr_container_set_string(&attr_obj, "name", "read test sensor");
bool ret =
attr_container_set_string(&attr_obj, "name", "read test sensor");
if (!ret) {
attr_container_destroy(attr_obj);
return NULL;
@ -372,15 +395,17 @@ static attr_container_t * read_test_sensor(void * sensor)
return NULL;
}
static bool config_test_sensor(void * s, void * config)
static bool
config_test_sensor(void *s, void *config)
{
return false;
}
static char global_heap_buf[1024 * 1024] = { 0 };
static void showUsage()
/* clang-format off */
static void
showUsage()
{
#ifndef CONNECTION_UART
printf("Usage:\n");
@ -401,8 +426,10 @@ static void showUsage()
printf("\t<Baudrate> represents the UART device baudrate and the default is 115200\n");
#endif
}
/* clang-format on */
static bool parse_args(int argc, char *argv[])
static bool
parse_args(int argc, char *argv[])
{
int c;
@ -410,14 +437,14 @@ static bool parse_args(int argc, char *argv[])
int optIndex = 0;
static struct option longOpts[] = {
#ifndef CONNECTION_UART
{ "server_mode", no_argument, NULL, 's' },
{ "host_address", required_argument, NULL, 'a' },
{ "port", required_argument, NULL, 'p' },
{ "server_mode", no_argument, NULL, 's' },
{ "host_address", required_argument, NULL, 'a' },
{ "port", required_argument, NULL, 'p' },
#else
{ "uart", required_argument, NULL, 'u' },
{ "baudrate", required_argument, NULL, 'b' },
{ "uart", required_argument, NULL, 'u' },
{ "baudrate", required_argument, NULL, 'b' },
#endif
{ "help", required_argument, NULL, 'h' },
{ "help", required_argument, NULL, 'h' },
{ 0, 0, 0, 0 }
};
@ -461,7 +488,8 @@ static bool parse_args(int argc, char *argv[])
}
// Driver function
int iwasm_main(int argc, char *argv[])
int
iwasm_main(int argc, char *argv[])
{
RuntimeInitArgs init_args;
korp_tid tid;
@ -499,22 +527,19 @@ int iwasm_main(int argc, char *argv[])
/* sensor framework */
init_sensor_framework();
// add the sys sensor objects
add_sys_sensor("sensor_test",
"This is a sensor for test",
0,
1000,
read_test_sensor,
config_test_sensor);
add_sys_sensor("sensor_test", "This is a sensor for test", 0, 1000,
read_test_sensor, config_test_sensor);
start_sensor_framework();
#ifndef CONNECTION_UART
if (server_mode)
os_thread_create(&tid, func_server_mode, NULL,
BH_APPLET_PRESERVED_STACK_SIZE);
BH_APPLET_PRESERVED_STACK_SIZE);
else
os_thread_create(&tid, func, NULL, BH_APPLET_PRESERVED_STACK_SIZE);
#else
os_thread_create(&tid, func_uart_mode, NULL, BH_APPLET_PRESERVED_STACK_SIZE);
os_thread_create(&tid, func_uart_mode, NULL,
BH_APPLET_PRESERVED_STACK_SIZE);
#endif
app_manager_startup(&interface);

View File

@ -3,8 +3,11 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
extern void iwasm_main();
int main(int argc, char *argv[])
extern void
iwasm_main();
int
main(int argc, char *argv[])
{
iwasm_main(argc, argv);
return 0;

View File

@ -13,33 +13,35 @@ static int num = 0;
static user_timer_t g_timer;
static connection_t *g_conn = NULL;
void on_data1(connection_t *conn,
conn_event_type_t type,
const char *data,
uint32 len,
void *user_data)
void
on_data1(connection_t *conn, conn_event_type_t type, const char *data,
uint32 len, void *user_data)
{
if (type == CONN_EVENT_TYPE_DATA) {
char message[64] = {0};
char message[64] = { 0 };
memcpy(message, data, len);
printf("Client got a message from server -> %s\n", message);
} else if (type == CONN_EVENT_TYPE_DISCONNECT) {
}
else if (type == CONN_EVENT_TYPE_DISCONNECT) {
printf("connection is close by server!\n");
} else {
}
else {
printf("error: got unknown event type!!!\n");
}
}
/* Timer callback */
void timer1_update(user_timer_t timer)
void
timer1_update(user_timer_t timer)
{
char message[64] = {0};
char message[64] = { 0 };
/* Reply to server */
snprintf(message, sizeof(message), "Hello %d", num++);
api_send_on_connection(g_conn, message, strlen(message));
}
void my_close_handler(request_t * request)
void
my_close_handler(request_t *request)
{
response_t response[1];
@ -53,7 +55,8 @@ void my_close_handler(request_t * request)
api_response_send(response);
}
void on_init()
void
on_init()
{
user_timer_t timer;
attr_container_t *args;
@ -78,7 +81,8 @@ void on_init()
api_timer_restart(timer, 1000);
}
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() */

View File

@ -9,7 +9,8 @@
int num = 0;
void publish_overheat_event()
void
publish_overheat_event()
{
attr_container_t *event;
@ -17,18 +18,20 @@ void publish_overheat_event()
attr_container_set_string(&event, "warning", "temperature is over high");
api_publish_event("alert/overheat", FMT_ATTR_CONTAINER, event,
attr_container_get_serialize_length(event));
attr_container_get_serialize_length(event));
attr_container_destroy(event);
}
/* Timer callback */
void timer1_update(user_timer_t timer)
void
timer1_update(user_timer_t timer)
{
publish_overheat_event();
}
void start_timer()
void
start_timer()
{
user_timer_t timer;
@ -37,12 +40,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

@ -6,20 +6,23 @@
#include "wasm_app.h"
#include "wa-inc/request.h"
void over_heat_event_handler(request_t *request)
void
over_heat_event_handler(request_t *request)
{
printf("### user over heat event handler called\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);
}
void on_init()
void
on_init()
{
api_subscribe_event("alert/overheat", over_heat_event_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() */

View File

@ -6,7 +6,8 @@
#include "wasm_app.h"
#include "wa-inc/request.h"
static void url1_request_handler(request_t *request)
static void
url1_request_handler(request_t *request)
{
response_t response[1];
attr_container_t *payload;
@ -14,7 +15,7 @@ static void url1_request_handler(request_t *request)
printf("[resp] ### user resource 1 handler called\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);
payload = attr_container_create("wasm app response payload");
if (payload == NULL)
@ -24,16 +25,15 @@ static void url1_request_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,
(void *)payload,
set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER, (void *)payload,
attr_container_get_serialize_length(payload));
api_response_send(response);
attr_container_destroy(payload);
}
static void url2_request_handler(request_t *request)
static void
url2_request_handler(request_t *request)
{
response_t response[1];
make_response_for_request(request, response);
@ -43,14 +43,16 @@ static void url2_request_handler(request_t *request)
printf("### user resource 2 handler called\n");
}
void on_init()
void
on_init()
{
/* register resource uri */
api_register_resource_handler("/url1", url1_request_handler);
api_register_resource_handler("/url2", url2_request_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() */

View File

@ -6,28 +6,30 @@
#include "wasm_app.h"
#include "wa-inc/request.h"
static void my_response_handler(response_t *response, void *user_data)
static void
my_response_handler(response_t *response, void *user_data)
{
char *tag = (char *) user_data;
char *tag = (char *)user_data;
if (response == NULL) {
printf("[req] request timeout!\n");
return;
}
printf("[req] response handler called mid:%d, status:%d, fmt:%d, payload:%p, len:%d, tag:%s\n",
printf("[req] response handler called mid:%d, status:%d, fmt:%d, "
"payload:%p, len:%d, tag:%s\n",
response->mid, response->status, response->fmt, response->payload,
response->payload_len, tag);
if (response->payload != NULL
&& response->payload_len > 0
if (response->payload != NULL && response->payload_len > 0
&& response->fmt == FMT_ATTR_CONTAINER) {
printf("[req] dump the response payload:\n");
attr_container_dump((attr_container_t *) response->payload);
attr_container_dump((attr_container_t *)response->payload);
}
}
static void test_send_request(char *url, char *tag)
static void
test_send_request(char *url, char *tag)
{
request_t request[1];
@ -35,13 +37,15 @@ static void test_send_request(char *url, char *tag)
api_send_request(request, my_response_handler, tag);
}
void on_init()
void
on_init()
{
test_send_request("/app/request_handler/url1", "a request to target app");
test_send_request("url1", "a general request");
}
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() */

View File

@ -9,14 +9,15 @@
static sensor_t sensor = NULL;
/* 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");
attr_container_dump(event);
}
void on_init()
void
on_init()
{
char *user_data;
attr_container_t *config;
@ -39,7 +40,8 @@ void on_init()
*/
}
void on_destroy()
void
on_destroy()
{
if (NULL != sensor) {
sensor_config(sensor, 0, 0, 0);

View File

@ -10,12 +10,14 @@
static int num = 0;
/* Timer callback */
void timer1_update(user_timer_t timer)
void
timer1_update(user_timer_t timer)
{
printf("Timer update %d\n", num++);
}
void on_init()
void
on_init()
{
user_timer_t timer;
@ -24,7 +26,8 @@ void on_init()
api_timer_restart(timer, 1000);
}
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() */