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:
1450
test-tools/host-tool/external/cJSON/cJSON.c
vendored
1450
test-tools/host-tool/external/cJSON/cJSON.c
vendored
File diff suppressed because it is too large
Load Diff
287
test-tools/host-tool/external/cJSON/cJSON.h
vendored
287
test-tools/host-tool/external/cJSON/cJSON.h
vendored
@ -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);
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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" */
|
||||
|
||||
@ -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 { \
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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" */
|
||||
|
||||
Reference in New Issue
Block a user