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

@ -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" */