Merge branch 'master' of http://git.komect.net/ISG/secogateway
This commit is contained in:
commit
277ad370c3
|
@ -1,285 +0,0 @@
|
|||
/*
|
||||
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef cJSON__h
|
||||
#define cJSON__h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#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
|
||||
|
||||
*/
|
||||
|
||||
#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)
|
||||
#define CJSON_EXPORT_SYMBOLS
|
||||
#endif
|
||||
|
||||
#if defined(CJSON_HIDE_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) type CJSON_STDCALL
|
||||
#elif defined(CJSON_EXPORT_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL
|
||||
#elif defined(CJSON_IMPORT_SYMBOLS)
|
||||
#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
|
||||
#else
|
||||
#define CJSON_PUBLIC(type) type
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* project version */
|
||||
#define CJSON_VERSION_MAJOR 1
|
||||
#define CJSON_VERSION_MINOR 7
|
||||
#define CJSON_VERSION_PATCH 12
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/* cJSON Types: */
|
||||
#define cJSON_Invalid (0)
|
||||
#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_Object (1 << 6)
|
||||
#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 */
|
||||
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. */
|
||||
struct cJSON *child;
|
||||
|
||||
/* The type of the item, as above. */
|
||||
int type;
|
||||
|
||||
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
|
||||
char *valuestring;
|
||||
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
|
||||
int valueint;
|
||||
/* 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. */
|
||||
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. */
|
||||
void *(CJSON_CDECL *malloc_fn)(size_t sz);
|
||||
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. */
|
||||
#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);
|
||||
|
||||
/* Supply malloc, realloc and free functions to cJSON */
|
||||
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. */
|
||||
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);
|
||||
|
||||
/* 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);
|
||||
/* 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. */
|
||||
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(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);
|
||||
|
||||
/* These calls create a cJSON item of the appropriate type. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
|
||||
/* raw json */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
|
||||
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 */
|
||||
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 */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child);
|
||||
|
||||
/* These utilities create an Array of count items. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
|
||||
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_AddItemReferenceToArray(cJSON *array, 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_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);
|
||||
|
||||
/* 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);
|
||||
|
||||
/* 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);
|
||||
|
||||
|
||||
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);
|
||||
|
||||
/* 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))
|
||||
|
||||
/* 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)
|
||||
|
||||
/* 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);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -48,6 +48,9 @@ enum pdelivmsgtype{
|
|||
NLMSG_PDELIV_DEBUGFS = 0x11, /*用户态发送给内核态的请求消息,用于查看pdiliv模块本身的状态及配置,与业务无关*/
|
||||
NLMSG_RECV_RAW_PKT = 0x12,/*上送DPI的报文消息*/
|
||||
PDNLGRP_REQUEST,
|
||||
/*for trace*/
|
||||
TRACE_CFG_POLICY_REQ = 0x20,
|
||||
TRACE_CFG_POLICY_REPLY,
|
||||
NLMGS_PDELIVERY_MAX_TYPE,
|
||||
};
|
||||
|
||||
|
@ -78,9 +81,7 @@ enum commcfgmsgtype{
|
|||
COMMMSGNL_BASE = 0x10,/*netlink 保留控制消息*/
|
||||
COMMNMSG_CFG_DEBUGFS = 0x11,/*keep the same with NLMSG_PDELIV_DEBUGFS */
|
||||
COMMNMSG_POLICYCONF,
|
||||
/*for trace*/
|
||||
TRACE_CFG_POLICY = 0x20,
|
||||
TRACE_CFG_MAX = 0X25,
|
||||
|
||||
/*add your msg type here:*/
|
||||
|
||||
/*commcfg nsmsg max type,must :add your msg type before this */
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
#ifndef __S2J_H__
|
||||
#define __S2J_H__
|
||||
|
||||
#include <cJSON/cJSON.h>
|
||||
#include <cjson/cJSON.h>
|
||||
#include <string.h>
|
||||
#include "s2jdef.h"
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
#ifndef __S2JDEF_H__
|
||||
#define __S2JDEF_H__
|
||||
|
||||
#include <cJSON/cJSON.h>
|
||||
#include <cjson/cJSON.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -35,7 +35,7 @@ PLAT_LINUX_SRCS = $(COMMON_SRCS)
|
|||
PLAT_ARM64_SRCS = $(COMMON_SRCS)
|
||||
|
||||
# gcc CFLAGS
|
||||
PLAT_ARM64_CFLAGS := -I../modules/netlink_api/ -I../modules/cfgrcv -I../modules/conntrack_api/api/ -I../../Common -I../common/trace
|
||||
PLAT_ARM64_CFLAGS := -I../modules/netlink_api/ -I../modules/pdelivery -I../modules/conntrack_api/api/ -I../../Common -I../common/trace
|
||||
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)
|
||||
|
||||
# this line must be at below of thus, because of...
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
# target name, the target name must have the same name of c source file
|
||||
TARGET_NAME=pdeliv
|
||||
|
||||
# target
|
||||
# for linux module driver: KO
|
||||
# for application: EXE
|
||||
# for dynamic library: DLL
|
||||
TARGET_TYPE = EXE
|
||||
|
||||
# target object
|
||||
# for application: APP
|
||||
# for device driver: DRV
|
||||
TARGET_OBJ = APP
|
||||
|
||||
# custom install dir
|
||||
TARGET_BOX =
|
||||
|
||||
#debug mode or release mode
|
||||
DEBUG = TRUE
|
||||
|
||||
PLAT_LINUX ?= TRUE
|
||||
PLAT_ARM64 ?= TRUE
|
||||
|
||||
VPATH = ../user/pdeliv_u
|
||||
|
||||
# source code
|
||||
|
||||
# set the source file, don't used .o because of ...
|
||||
|
||||
COMMON_SRCS = pdelivery_main.c
|
||||
|
||||
# MRS Board Source Files
|
||||
PLAT_LINUX_SRCS = $(COMMON_SRCS)
|
||||
PLAT_ARM64_SRCS = $(COMMON_SRCS)
|
||||
|
||||
# gcc CFLAGS
|
||||
PLAT_ARM64_CFLAGS := -I../../Common -I../user/netlink_uapi
|
||||
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)
|
||||
|
||||
|
||||
PLAT_ARM64_LDFLAGS :=
|
||||
PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS)
|
||||
|
||||
ARM64_LIBS := ./pdeliv-rcvtest-arm64.so
|
||||
LINUX_LIBS := ./pdeliv-rcvtest-linux.so
|
||||
|
||||
|
||||
|
||||
ifeq ($(PLAT_ARM64), TRUE)
|
||||
DEPEND_LIB += ./debug/libnetlinku-arm64.so
|
||||
USER_CLEAN_ITEMS += ./libnetlinku-arm64.so
|
||||
endif
|
||||
|
||||
ifeq ($(PLAT_LINUX), TRUE)
|
||||
DEPEND_LIB += ./debug/libnetlinku-linux.so
|
||||
USER_CLEAN_ITEMS += ./libnetlinku-linux.so
|
||||
endif
|
||||
|
||||
|
||||
# this line must be at below of thus, because of...
|
||||
include ../../Common/common.Makefile
|
||||
|
||||
ifneq ($(MAKECMDGOALS), clean)
|
||||
ifneq ($(MAKECMDGOALS), cleanall)
|
||||
ifneq ($(notdir $(DEPEND_LIB)), $(wildcard $(DEPEND_LIB)))
|
||||
$(shell $(CP) $(DEPEND_LIB) ./)
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(MAKECMDGOALS), )
|
||||
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
|
||||
else
|
||||
ifeq ($(MAKECMDGOALS), all)
|
||||
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
|
||||
endif
|
||||
endif
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
# target name, the target name must have the same name of c source file
|
||||
TARGET_NAME=pdeliv
|
||||
|
||||
# target
|
||||
# for linux module driver: KO
|
||||
# for application: EXE
|
||||
# for dynamic library: DLL
|
||||
TARGET_TYPE = EXE
|
||||
|
||||
# target object
|
||||
# for application: APP
|
||||
# for device driver: DRV
|
||||
TARGET_OBJ = APP
|
||||
|
||||
# custom install dir
|
||||
TARGET_BOX =
|
||||
|
||||
#debug mode or release mode
|
||||
DEBUG = TRUE
|
||||
|
||||
PLAT_LINUX ?= TRUE
|
||||
PLAT_ARM64 ?= TRUE
|
||||
|
||||
VPATH = ../user/pdeliv_u
|
||||
|
||||
# source code
|
||||
|
||||
# set the source file, don't used .o because of ...
|
||||
|
||||
COMMON_SRCS = pdelivery_main.c
|
||||
|
||||
# MRS Board Source Files
|
||||
PLAT_LINUX_SRCS = $(COMMON_SRCS)
|
||||
PLAT_ARM64_SRCS = $(COMMON_SRCS)
|
||||
|
||||
# gcc CFLAGS
|
||||
PLAT_ARM64_CFLAGS := -I../../Common -I../user/netlink_uapi
|
||||
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)
|
||||
|
||||
|
||||
PLAT_ARM64_LDFLAGS :=
|
||||
PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS)
|
||||
|
||||
ARM64_LIBS := ./pdeliv-rcvtest-arm64.so
|
||||
LINUX_LIBS := ./pdeliv-rcvtest-linux.so
|
||||
|
||||
|
||||
|
||||
ifeq ($(PLAT_ARM64), TRUE)
|
||||
DEPEND_LIB += ./debug/libnetlinku-arm64.so
|
||||
USER_CLEAN_ITEMS += ./libnetlinku-arm64.so
|
||||
endif
|
||||
|
||||
ifeq ($(PLAT_LINUX), TRUE)
|
||||
DEPEND_LIB += ./debug/libnetlinku-linux.so
|
||||
USER_CLEAN_ITEMS += ./libnetlinku-linux.so
|
||||
endif
|
||||
|
||||
|
||||
# this line must be at below of thus, because of...
|
||||
include ../../Common/common.Makefile
|
||||
|
||||
ifneq ($(MAKECMDGOALS), clean)
|
||||
ifneq ($(MAKECMDGOALS), cleanall)
|
||||
ifneq ($(notdir $(DEPEND_LIB)), $(wildcard $(DEPEND_LIB)))
|
||||
$(shell $(CP) $(DEPEND_LIB) ./)
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(MAKECMDGOALS), )
|
||||
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
|
||||
else
|
||||
ifeq ($(MAKECMDGOALS), all)
|
||||
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
|
||||
endif
|
||||
endif
|
||||
|
|
@ -9,8 +9,9 @@ typedef enum {
|
|||
} reply_op_t;
|
||||
|
||||
enum {
|
||||
TRACE_MSG_POLICY_REQ = 0x0,
|
||||
TRACE_MSG_POLICY_REQ = 0x0/*,
|
||||
TRACE_MSG_POLICY_REPLY
|
||||
*/
|
||||
};
|
||||
|
||||
typedef struct _trace_hdr {
|
||||
|
|
|
@ -253,7 +253,7 @@ int nf_nlmsg_multicast(struct netlinkk_cfg *g_nlcfg, struct sk_buff *skb)
|
|||
if (ret < 0)
|
||||
{
|
||||
//g_nlcfg->dfs.send_fail ++;
|
||||
printk(KERN_ERR "Error while sending pkt to user, ret id: %d\n", ret);
|
||||
//printk(KERN_ERR "Error while sending pkt to user, ret id: %d\n", ret);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -205,12 +205,14 @@ unsigned int pdelivery_hook_func(void *priv,
|
|||
int payload_len; // with padding, but ok for echo
|
||||
struct iphdr *iph;
|
||||
int ret = -1;
|
||||
|
||||
|
||||
#if 0
|
||||
iph = ip_hdr(skb);
|
||||
printk(KERN_INFO "pdelivery_hook_func:pktlen=%d,mac_header = %d IP:",skb->len,skb->mac_len);
|
||||
printk_ipaddress(iph->saddr);
|
||||
printk(KERN_INFO "-->");
|
||||
printk_ipaddress(iph->daddr);
|
||||
#endif
|
||||
|
||||
payload = skb_mac_header(skb);
|
||||
payload_len = skb->len + skb->mac_len;/**/
|
||||
|
@ -235,7 +237,7 @@ unsigned int pdelivery_hook_func(void *priv,
|
|||
/*int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data);*/
|
||||
/********************************************************************************/
|
||||
|
||||
#if 1
|
||||
#if 0
|
||||
printk(KERN_DEBUG "%02x %02x %02x %02x %02x %02x %02x %02x\r\n",
|
||||
*((char*)out_payload),*((char*)out_payload+1),
|
||||
*((char*)out_payload+2),*((char*)out_payload+3),
|
||||
|
@ -249,14 +251,14 @@ unsigned int pdelivery_hook_func(void *priv,
|
|||
goto failure;
|
||||
}
|
||||
|
||||
printk(KERN_INFO "pdelivery_hook_func() end.\n");
|
||||
//printk(KERN_INFO "pdelivery_hook_func() end.\n");
|
||||
g_nlcfg.dfs.send_succ ++;
|
||||
g_nlcfg.msg_processer[NLMSG_RECV_RAW_PKT].dfs.send_succ++;
|
||||
|
||||
return NF_ACCEPT;/*must return a value*/
|
||||
|
||||
failure:
|
||||
printk(KERN_INFO " failed in pdelivery_hook_func!\n");
|
||||
//printk(KERN_INFO " failed in pdelivery_hook_func!\n");
|
||||
g_nlcfg.dfs.send_fail ++;
|
||||
g_nlcfg.msg_processer[NLMSG_RECV_RAW_PKT].dfs.send_fail++;
|
||||
|
||||
|
|
|
@ -3,11 +3,11 @@
|
|||
#include <net/netlink.h>
|
||||
|
||||
#include "trace_def.h"
|
||||
#include "../netlink_api/libnetlink_k.h"
|
||||
#include "libnetlink_k.h"
|
||||
#include "trace_msg.h"
|
||||
#include "commuapinl.h"
|
||||
#include "conntrack_api.h"
|
||||
#include "cfgrcv_kinit.h"
|
||||
#include "pdeliverynl_kinit.h"
|
||||
|
||||
|
||||
|
||||
|
@ -37,7 +37,7 @@ static int trace_rcv_policy(struct sk_buff *skb, struct nlmsghdr *nlh, struct ne
|
|||
|
||||
printk(KERN_INFO "Trace recv policy, msg_type:%u", nlh->nlmsg_type);
|
||||
switch (nlh->nlmsg_type) {
|
||||
case TRACE_CFG_POLICY:
|
||||
case TRACE_CFG_POLICY_REQ:
|
||||
buf = nlmsg_data(nlh);
|
||||
if (buf == NULL) {
|
||||
printk(KERN_ERR"Receiving data is null");
|
||||
|
@ -104,7 +104,7 @@ static int trace_rcv_policy(struct sk_buff *skb, struct nlmsghdr *nlh, struct ne
|
|||
printk(KERN_ERR"Allocating skb memory is failure");
|
||||
goto FAIL;
|
||||
}
|
||||
reply_hdr = nlmsg_put(reply_skb, 0, 0, TRACE_MSG_POLICY_REPLY, TRACE_REPLY_SZ, 0);
|
||||
reply_hdr = nlmsg_put(reply_skb, 0, 0, TRACE_CFG_POLICY_REPLY, TRACE_REPLY_SZ, 0);
|
||||
if (reply_hdr == NULL) {
|
||||
printk(KERN_ERR"Putting length of reply is failure");
|
||||
goto FAIL;
|
||||
|
@ -117,14 +117,16 @@ static int trace_rcv_policy(struct sk_buff *skb, struct nlmsghdr *nlh, struct ne
|
|||
reply = (trace_reply_t *)reply_data;
|
||||
memcpy(&reply->hdr, hdr, sizeof(*hdr));
|
||||
reply->result = ret;
|
||||
|
||||
reply_ret = cfgnl_unicast(reply_skb, nlh->nlmsg_pid);
|
||||
|
||||
reply_ret = pdeliv_unicast(reply_skb, nlh->nlmsg_pid);
|
||||
if (reply_ret != 0) {
|
||||
printk(KERN_ERR"Reply message is failure");
|
||||
goto FAIL;
|
||||
}
|
||||
|
||||
nlmsg_free(reply_skb);
|
||||
printk(KERN_INFO"Reply message final");
|
||||
|
||||
//nlmsg_free(reply_skb);
|
||||
return 0;
|
||||
|
||||
FAIL:
|
||||
|
@ -138,14 +140,14 @@ FAIL:
|
|||
static int __init trace_init(void)
|
||||
{
|
||||
printk(KERN_INFO"Trace is initiating");
|
||||
cfg_msgtype_register(TRACE_CFG_POLICY, trace_rcv_policy, NULL, NULL);
|
||||
pdeliv_msgtype_register(TRACE_CFG_POLICY_REQ, trace_rcv_policy, NULL, NULL);
|
||||
printk(KERN_INFO"Trace is initiated");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __exit trace_exit(void)
|
||||
{
|
||||
cfg_msgtype_unregister(TRACE_CFG_POLICY);
|
||||
pdeliv_msgtype_unregister(TRACE_CFG_POLICY_REQ);
|
||||
|
||||
}
|
||||
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load Diff
|
@ -36,7 +36,7 @@ int set_user_policy()
|
|||
commnl_addattr_l(&req.n, sizeof(req), POLICYCONFA_SRCIP, debug, debug_len);
|
||||
|
||||
/*发送组装好的netlink消息*/
|
||||
if (pdeliv_talk(1, &req.n, NULL) < 0)
|
||||
if (pdeliv_talk(0, &req.n, NULL) < 0)
|
||||
{
|
||||
return -2;
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ int main( int argc, char **argv)
|
|||
printf("cfgchannel main begin:\r\n");
|
||||
|
||||
/*创建通道*/
|
||||
ret = pdelivnl_open();
|
||||
ret = pdelivnl_open(0);
|
||||
if(ret < 0)
|
||||
{
|
||||
printf("pdelivnl_open fail,exit.\r\n");
|
||||
|
@ -79,7 +79,7 @@ int main( int argc, char **argv)
|
|||
}
|
||||
|
||||
/*关闭netlink通道*/
|
||||
pdelivnl_close();
|
||||
pdelivnl_close(0);
|
||||
|
||||
printf("cfgchannel main exit!\r\n");
|
||||
return 0;
|
||||
|
|
|
@ -293,14 +293,15 @@ void commcfgnl_close()
|
|||
return;
|
||||
}
|
||||
|
||||
int pdelivnl_open()
|
||||
int pdelivnl_open(unsigned int group)
|
||||
{
|
||||
return commnl_open_byproto(PDNLGRP_ALLRAW, NETLINK_PDELIVERY);
|
||||
return commnl_open_byproto(group, NETLINK_PDELIVERY);
|
||||
}
|
||||
|
||||
void pdelivnl_close()
|
||||
|
||||
void pdelivnl_close(unsigned int group)
|
||||
{
|
||||
commnl_close(PDNLGRP_ALLRAW,NETLINK_PDELIVERY);
|
||||
commnl_close(group,NETLINK_PDELIVERY);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -769,7 +770,7 @@ int pdeliv_main(pdelivnl_listen_filter_t process_pkt)
|
|||
printf("pdeliv_main begin:\r\n");
|
||||
|
||||
/*创建通道*/
|
||||
ret = pdelivnl_open();
|
||||
ret = pdelivnl_open(PDNLGRP_ALLRAW);
|
||||
if(ret < 0)
|
||||
{
|
||||
printf("pdelivnl_open fail,exit.\r\n");
|
||||
|
@ -781,7 +782,7 @@ int pdeliv_main(pdelivnl_listen_filter_t process_pkt)
|
|||
|
||||
|
||||
/*关闭netlink通道*/
|
||||
pdelivnl_close();
|
||||
pdelivnl_close(PDNLGRP_ALLRAW);
|
||||
|
||||
printf("pdeliv_main exit!\r\n");
|
||||
return 0;
|
||||
|
|
|
@ -114,8 +114,8 @@ void commcfgnl_close();
|
|||
/*输出参数: struct upmnl_handle * upmh ,存放创建的通道相关信息。*/
|
||||
/*返回值:0通道创建成果;< 0,失败 */
|
||||
/****************************************************************/
|
||||
int pdelivnl_open();
|
||||
void pdelivnl_close();
|
||||
int pdelivnl_open(unsigned int group);
|
||||
void pdelivnl_close(unsigned int group);
|
||||
|
||||
//int upnl_dump_type(struct upmnl_handle *upmh, unsigned int type);
|
||||
|
||||
|
|
|
@ -51,7 +51,7 @@ int set_pdeliv_debug_waitack(unsigned char * msgtype)
|
|||
commnl_addattr_l(&req.n, sizeof(req), NLDEBUG_MSG_TYPE, msgtype, debug_len);
|
||||
|
||||
/*发送组装好的netlink消息*/
|
||||
if (pdeliv_talk(1, &req.n, answer) < 0)
|
||||
if (pdeliv_talk(0, &req.n, answer) < 0)
|
||||
{
|
||||
printf("set_user_policy_waitack rcv ack msg faild.\r\n");
|
||||
return -2;
|
||||
|
@ -110,7 +110,7 @@ int main( int argc, char **argv)
|
|||
}
|
||||
|
||||
/*创建通道*/
|
||||
ret = pdelivnl_open();
|
||||
ret = pdelivnl_open(0);
|
||||
if(ret < 0)
|
||||
{
|
||||
printf("pdelivnl_open fail,exit.\r\n");
|
||||
|
@ -126,7 +126,7 @@ int main( int argc, char **argv)
|
|||
}
|
||||
|
||||
/*关闭netlink通道*/
|
||||
pdelivnl_close();
|
||||
pdelivnl_close(0);
|
||||
|
||||
printf("cfgchannel main exit!\r\n");
|
||||
return 0;
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
#include "redisMq.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
bool ret = redisPubInit();
|
||||
if (!ret)
|
||||
{
|
||||
printf("Init failed.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = redisPubConnect();
|
||||
if (!ret)
|
||||
{
|
||||
printf("connect failed.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
//while (true)
|
||||
{
|
||||
//redisPublish("test-channel", "Test message");
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
redisPubDisconnect();
|
||||
redisPubUninit();
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
#include "redisMq.h"
|
||||
|
||||
void test_func_recv(struct RecvMsg_t *msg)
|
||||
{
|
||||
if (msg->msg)
|
||||
{
|
||||
printf("%s,%d,%llu,%s,%d\n", __FUNCTION__, __LINE__, pthread_self(), msg->msg, msg->len);
|
||||
freeMsg(msg);
|
||||
}
|
||||
}
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
bool ret = redisSubInit();
|
||||
if (!ret)
|
||||
{
|
||||
printf("Init failed.\n");
|
||||
return 0;
|
||||
}
|
||||
redisRegisterChannelFunc("test-channel",test_func_recv);
|
||||
|
||||
|
||||
ret = redisSubConnect();
|
||||
if (!ret)
|
||||
{
|
||||
printf("Connect failed.\n");
|
||||
return 0;
|
||||
}
|
||||
redisSubscriber("test-channel");
|
||||
|
||||
while (true)
|
||||
{
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
redisSubDisconnect();
|
||||
redisSubUninit();
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
#ifndef __REDIS_CHANNRL_H_
|
||||
#define __REDIS_CHANNRL_H_
|
||||
|
||||
/* 建议 命名按照 redis_chan_模块名_功能*/
|
||||
#define REDIS_CHAN_REDISMQ_TEST ("redis_chan_redismq_test")
|
||||
|
||||
#endif
|
|
@ -34,6 +34,8 @@
|
|||
|
||||
#define SESS_HASH_INDEX(seq) (seq >> HASH_SESS_TAB_BITS)
|
||||
|
||||
#define NETLINK_GROUP_TRACE_ID 0
|
||||
|
||||
typedef struct _cb_arg {
|
||||
struct hlist_node node;
|
||||
uint32_t seq;
|
||||
|
@ -97,50 +99,68 @@ END:
|
|||
return ret;
|
||||
}
|
||||
|
||||
static void *cb_thread(void *arg)
|
||||
static trace_ret_t msg_handle(const trace_reply_t *msg)
|
||||
{
|
||||
ssize_t size;
|
||||
trace_reply_t msg;
|
||||
|
||||
return NULL; // todo
|
||||
|
||||
while(1) {
|
||||
if (g_client_stop) {
|
||||
SYSLOG_INFO("Callback thread is stopping");
|
||||
break;
|
||||
}
|
||||
|
||||
// todo 需要考虑长期没收到消息,hash怎么清除数据
|
||||
// size = msgrcv(g_client_msgid, &msg, sizeof(msg.mtext), g_pid, MSG_NOERROR | IPC_NOWAIT);
|
||||
if ((size == -1)/* || (size != sizeof(msg.mtext))*/) {
|
||||
if ((errno != ENOMSG)) {
|
||||
printf("msg queue receive is failure:%d\n", errno);
|
||||
}
|
||||
|
||||
// usleep(SLEEP_THREAD_TIME); //防止CPU占用过高,睡眠一会儿,让出CPU todo
|
||||
continue;
|
||||
}
|
||||
|
||||
cb_arg_t *cb_arg = NULL;
|
||||
if (get_and_del_arg_from_hlist(msg.hdr.seq, &cb_arg) == TRACE_FAILURE) {
|
||||
SYSLOG_ERR("Get arg is failure");
|
||||
break;
|
||||
}
|
||||
|
||||
if (cb_arg == NULL) {
|
||||
SYSLOG_INFO("The seq:[%u] is not found", msg.hdr.seq);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (cb_arg->cb != NULL) {
|
||||
SYSLOG_DEBUG("Execute callback of seq:[%u]", msg.hdr.seq);
|
||||
cb_arg->cb(msg.result, cb_arg->arg);
|
||||
} else {
|
||||
SYSLOG_DEBUG("The callback of seq:[%u] is not set", msg.hdr.seq);
|
||||
}
|
||||
free(cb_arg);
|
||||
cb_arg_t *cb_arg = NULL;
|
||||
if (get_and_del_arg_from_hlist(msg->hdr.seq, &cb_arg) == TRACE_FAILURE) {
|
||||
SYSLOG_ERR("Get arg is failure");
|
||||
return TRACE_FAILURE;
|
||||
}
|
||||
|
||||
if (cb_arg == NULL) {
|
||||
SYSLOG_INFO("The seq:[%u] is not found", msg->hdr.seq);
|
||||
return TRACE_FAILURE;
|
||||
}
|
||||
|
||||
if (cb_arg->cb != NULL) {
|
||||
SYSLOG_DEBUG("Execute callback of seq:[%u]", msg->hdr.seq);
|
||||
cb_arg->cb(msg->result, cb_arg->arg);
|
||||
} else {
|
||||
SYSLOG_DEBUG("The callback of seq:[%u] is not set", msg->hdr.seq);
|
||||
}
|
||||
free(cb_arg);
|
||||
|
||||
return TRACE_SUCCESS;
|
||||
}
|
||||
|
||||
static int trace_recv_handle(struct pdelivnl_ctrl_data *ctrl,
|
||||
struct nlmsghdr *n, void *arg)
|
||||
{
|
||||
trace_reply_t *reply;
|
||||
trace_ret_t ret;
|
||||
|
||||
SYSLOG_INFO("Trace receives reply message, msg_type:%u", n->nlmsg_type);
|
||||
switch (n->nlmsg_type) {
|
||||
case TRACE_CFG_POLICY_REPLY:
|
||||
reply = (trace_reply_t *)NLMSG_DATA(n);
|
||||
if (sizeof(*reply) < (n->nlmsg_len - NLMSG_HDRLEN)) {
|
||||
SYSLOG_WARN("The length of the reply message is required to be %u, but fact length is %u",
|
||||
sizeof(*reply), (n->nlmsg_len - NLMSG_HDRLEN));
|
||||
break;
|
||||
}
|
||||
ret = msg_handle(reply);
|
||||
if (ret != TRACE_SUCCESS) {
|
||||
SYSLOG_ERR("Processing message is fail");
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
SYSLOG_WARN("Unknown type:%u of the message is received from netlink", n->nlmsg_type);
|
||||
break;
|
||||
}
|
||||
|
||||
SYSLOG_INFO("Reply message of trace finalizes, msg_type:%u", n->nlmsg_type);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void *cb_thread(void *arg)
|
||||
{
|
||||
SYSLOG_INFO("Callback thread is started");
|
||||
|
||||
pdelivnl_listen(NETLINK_GROUP_TRACE_ID, trace_recv_handle, NULL);
|
||||
|
||||
SYSLOG_INFO("Callback thread is stopped");
|
||||
return NULL;
|
||||
}
|
||||
|
@ -153,12 +173,13 @@ static trace_ret_t cfg_channel_send(const uint32_t seq, const trace_policy_t *p
|
|||
|
||||
hdr->nlmsg_len = NLMSG_HDRLEN;
|
||||
hdr->nlmsg_flags = NLM_F_REQUEST /* | NLM_F_ACK */;
|
||||
hdr->nlmsg_type = TRACE_CFG_POLICY;
|
||||
hdr->nlmsg_type = TRACE_CFG_POLICY_REQ;
|
||||
hdr->nlmsg_pid = getpid();
|
||||
|
||||
trace_req_t req;
|
||||
req.hdr.ver = 1;
|
||||
req.hdr.seq = seq;
|
||||
req.hdr.is_reply = is_reply;
|
||||
memcpy(&req.policy, policy, sizeof(*policy));
|
||||
|
||||
commnl_addattr_l(hdr, sizeof(buf), TRACE_MSG_POLICY_REQ, &req, sizeof(trace_req_t));
|
||||
|
@ -172,7 +193,8 @@ static trace_ret_t cfg_channel_send(const uint32_t seq, const trace_policy_t *p
|
|||
SYSLOG_DEBUG(" protocol:%u, app_type:%u", req.policy.protocol, req.policy.app_type);
|
||||
|
||||
/*发送组装好的netlink消息*/
|
||||
if (commcfg_send(hdr) < 0) {
|
||||
//if (commcfg_send(hdr) < 0) {
|
||||
if (pdelivnl_send(NETLINK_GROUP_TRACE_ID, hdr) < 0) {
|
||||
SYSLOG_ERR("Message(seq:%u) which been sent is failure", seq);
|
||||
return TRACE_FAILURE;
|
||||
}
|
||||
|
@ -185,7 +207,7 @@ static trace_ret_t cfg_channel_send(const uint32_t seq, const trace_policy_t *p
|
|||
static void cfg_channel_close()
|
||||
{
|
||||
if (g_channel_open >= 0) {
|
||||
commcfgnl_close();
|
||||
pdelivnl_close(NETLINK_GROUP_TRACE_ID);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -207,7 +229,8 @@ trace_ret_t trace_client_init()
|
|||
}
|
||||
}
|
||||
|
||||
g_channel_open = commcfgnl_open();
|
||||
//g_channel_open = commcfgnl_open();
|
||||
g_channel_open = pdelivnl_open(NETLINK_GROUP_TRACE_ID);
|
||||
if(g_channel_open < 0)
|
||||
{
|
||||
SYSLOG_ERR("pdelivnl_open fail:%d", g_channel_open);
|
||||
|
@ -347,6 +370,7 @@ static trace_ret_t __trace_async_exec(const trace_policy_t *in,
|
|||
}
|
||||
|
||||
is_reply = REPLY_OP_NEED;
|
||||
SYSLOG_DEBUG("Need reply:%u from kernel", is_reply);
|
||||
}
|
||||
|
||||
ret = cfg_channel_send(seq, in, is_reply);
|
||||
|
|
|
@ -38,6 +38,10 @@ COMMON_CFLAGS := -I../../Common
|
|||
PLAT_LINUX_CFLAGS += $(COMMON_CFLAGS)
|
||||
PLAT_ARM64_CFLAGS += $(COMMON_CFLAGS)
|
||||
|
||||
COMMON_LIBS := -lcjson
|
||||
LINUX_LIBS := $(COMMON_LIBS)
|
||||
ARM64_LIBS := $(COMMON_LIBS)
|
||||
|
||||
# this line must be at below of thus, because of...
|
||||
include ../../Common/common.Makefile
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <getopt.h>
|
||||
#include <cjson/cJSON.h>
|
||||
|
||||
#include "compile.h"
|
||||
|
||||
|
@ -13,6 +14,9 @@
|
|||
int main(int argc, char **argv)
|
||||
{
|
||||
int c, optidx = 0;
|
||||
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
|
||||
static const struct option long_opts[] = {
|
||||
{ "help", no_argument, NULL, 'h' },
|
||||
{ "version", no_argument, NULL, 'v' },
|
||||
|
@ -20,6 +24,7 @@ int main(int argc, char **argv)
|
|||
{NULL, 0, NULL, 0}
|
||||
};
|
||||
|
||||
cJSON_Delete(root);
|
||||
while((c = getopt_long(argc, argv, "hv", long_opts, &optidx)) != -1)
|
||||
{
|
||||
switch (c)
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1 +0,0 @@
|
|||
Subproject commit a51e9abb2b1ddfc4300e44695ce2d240224c995c
|
|
@ -0,0 +1,22 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Armink (armink.ztl@gmail.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
'Software'), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -0,0 +1,61 @@
|
|||
# C结构体与 JSON 快速互转库
|
||||
|
||||
---
|
||||
|
||||
## struct2json
|
||||
|
||||
[struct2json](https://github.com/armink/struct2json) 是一个开源的C结构体与 JSON 快速互转库,它可以快速实现 **结构体对象** 与 **JSON 对象** 之间序列化及反序列化要求。快速、简洁的 API 设计,大大降低直接使用 JSON 解析库来实现此类功能的代码复杂度。
|
||||
|
||||
## 起源
|
||||
|
||||
把面向对象设计应用到C语言中,是当下很流行的设计思想。由于C语言中没有类,所以一般使用结构体 `struct` 充当类,那么结构体变量就是对象。有了对象之后,很多时候需要考虑对象的序列化及反序列化问题。C语言不像很多高级语言拥有反射等机制,使得对象序列化及反序列化被原生的支持。
|
||||
|
||||
对于C语言来说,序列化为 JSON 字符串是个不错的选择,所以就得使用 [cJSON](https://github.com/kbranigan/cJSON) 这类 JSON 解析库,但是使用后的代码冗余且逻辑性差,所以萌生对cJSON库进行二次封装,实现一个 struct 与 JSON 之间快速互转的库。 struct2json 就诞生于此。下面是 struct2json 主要使用场景:
|
||||
|
||||
- **持久化** :结构体对象序列化为 JSON 对象后,可直接保存至文件、Flash,实现对结构体对象的掉电存储;
|
||||
- **通信** :高级语言对JSON支持的很友好,例如: Javascript、Groovy 就对 JSON 具有原生的支持,所以 JSON 也可作为C语言与其他语言软件之间的通信协议格式及对象传递格式;
|
||||
- **可视化** :序列化为 JSON 后的对象,可以更加直观的展示到控制台或者 UI 上,可用于产品调试、产品二次开发等场景;
|
||||
|
||||
## 如何使用
|
||||
|
||||
### 声明结构体
|
||||
|
||||
如下声明了两个结构体,结构体 `Hometown` 是结构体 `Student` 的子结构体
|
||||
|
||||
```C
|
||||
/* 籍贯 */
|
||||
typedef struct {
|
||||
char name[16];
|
||||
} Hometown;
|
||||
|
||||
/* 学生 */
|
||||
typedef struct {
|
||||
uint8_t id;
|
||||
uint8_t score[8];
|
||||
char name[10];
|
||||
double weight;
|
||||
Hometown hometown;
|
||||
} Student;
|
||||
```
|
||||
|
||||
### 将结构体对象序列化为 JSON 对象
|
||||
|
||||
|使用前([源文件](https://github.com/armink/struct2json/blob/master/docs/zh/assets/not_use_struct2json.c))|使用后([源文件](https://github.com/armink/struct2json/blob/master/docs/zh/assets/used_struct2json.c))|
|
||||
|:-----:|:-----:|
|
||||
|![结构体转JSON-使用前](https://git.oschina.net/Armink/struct2json/raw/master/docs/zh/images/not_use_struct2json.png)| ![结构体转JSON-使用后](https://git.oschina.net/Armink/struct2json/raw/master/docs/zh/images/used_struct2json.png)|
|
||||
|
||||
### 将 JSON 对象反序列化为结构体对象
|
||||
|
||||
|使用前([源文件](https://github.com/armink/struct2json/blob/master/docs/zh/assets/not_use_struct2json_for_json.c))|使用后([源文件](https://github.com/armink/struct2json/blob/master/docs/zh/assets/used_struct2json_for_json.c))|
|
||||
|:-----:|:-----:|
|
||||
|![JSON转结构体-使用前](https://git.oschina.net/Armink/struct2json/raw/master/docs/zh/images/not_use_struct2json_for_json.png)| ![JSON转结构体-使用后](https://git.oschina.net/Armink/struct2json/raw/master/docs/zh/images/used_struct2json_for_json.png)|
|
||||
|
||||
欢迎大家 **fork and pull request**([Github](https://github.com/armink/struct2json)|[OSChina](http://git.oschina.net/armink/struct2json)|[Coding](https://coding.net/u/armink/p/struct2json/git)) 。如果觉得这个开源项目很赞,可以点击[项目主页](https://github.com/armink/struct2json) 右上角的**Star**,同时把它推荐给更多有需要的朋友。
|
||||
|
||||
## 文档
|
||||
|
||||
具体内容参考[`\docs\zh\`](https://github.com/armink/struct2json/tree/master/docs/zh)下的文件。务必保证在 **阅读文档** 后再使用。
|
||||
|
||||
## 许可
|
||||
|
||||
MIT Copyright (c) armink.ztl@gmail.com
|
|
@ -0,0 +1,122 @@
|
|||
/*
|
||||
* This file is part of the struct2json Library.
|
||||
*
|
||||
* Copyright (c) 2015, Armink, <armink.ztl@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* 'Software'), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Function: demo for this library.
|
||||
* Created on: 2015-10-14
|
||||
*/
|
||||
|
||||
#include "s2j.h"
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
char name[16];
|
||||
} Hometown;
|
||||
|
||||
typedef struct {
|
||||
uint8_t id;
|
||||
double weight;
|
||||
uint8_t score[8];
|
||||
char name[10];
|
||||
Hometown hometown;
|
||||
} Student;
|
||||
|
||||
/**
|
||||
* Student JSON object to structure object
|
||||
*
|
||||
* @param json_obj JSON object
|
||||
*
|
||||
* @return structure object
|
||||
*/
|
||||
static void *json_to_struct(cJSON* json_obj) {
|
||||
/* create Student structure object */
|
||||
s2j_create_struct_obj(struct_student, Student);
|
||||
|
||||
/* deserialize data to Student structure object. */
|
||||
s2j_struct_get_basic_element(struct_student, json_obj, int, id);
|
||||
s2j_struct_get_array_element(struct_student, json_obj, int, score);
|
||||
s2j_struct_get_basic_element(struct_student, json_obj, string, name);
|
||||
s2j_struct_get_basic_element(struct_student, json_obj, double, weight);
|
||||
|
||||
/* deserialize data to Student.Hometown structure object. */
|
||||
s2j_struct_get_struct_element(struct_hometown, struct_student, json_hometown, json_obj, Hometown, hometown);
|
||||
s2j_struct_get_basic_element(struct_hometown, json_hometown, string, name);
|
||||
|
||||
/* return Student structure object pointer */
|
||||
return struct_student;
|
||||
}
|
||||
|
||||
/**
|
||||
* Student structure object to JSON object
|
||||
*
|
||||
* @param struct_obj structure object
|
||||
*
|
||||
* @param JSON object
|
||||
*/
|
||||
static cJSON *struct_to_json(void* struct_obj) {
|
||||
Student *struct_student = (Student *)struct_obj;
|
||||
|
||||
/* create Student JSON object */
|
||||
s2j_create_json_obj(json_student);
|
||||
|
||||
/* serialize data to Student JSON object. */
|
||||
s2j_json_set_basic_element(json_student, struct_student, int, id);
|
||||
s2j_json_set_basic_element(json_student, struct_student, double, weight);
|
||||
s2j_json_set_array_element(json_student, struct_student, int, score, 8);
|
||||
s2j_json_set_basic_element(json_student, struct_student, string, name);
|
||||
|
||||
/* serialize data to Student.Hometown JSON object. */
|
||||
s2j_json_set_struct_element(json_hometown, json_student, struct_hometown, struct_student, Hometown, hometown);
|
||||
s2j_json_set_basic_element(json_hometown, struct_hometown, string, name);
|
||||
|
||||
/* return Student JSON object pointer */
|
||||
return json_student;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
static Student orignal_student_obj = {
|
||||
.id = 24,
|
||||
.weight = 71.2,
|
||||
.score = {1, 2, 3, 4, 5, 6, 7, 8},
|
||||
.name = "armink",
|
||||
.hometown.name = "China",
|
||||
};
|
||||
|
||||
/* serialize Student structure object */
|
||||
cJSON *json_student = struct_to_json(&orignal_student_obj);
|
||||
/* deserialize Student structure object */
|
||||
Student *converted_student_obj = json_to_struct(json_student);
|
||||
|
||||
/* compare Student structure object */
|
||||
if(memcmp(&orignal_student_obj, converted_student_obj, sizeof(Student))) {
|
||||
printf("Converted failed!\n");
|
||||
} else {
|
||||
printf("Converted OK!\n");
|
||||
}
|
||||
|
||||
s2j_delete_json_obj(json_student);
|
||||
s2j_delete_struct_obj(converted_student_obj);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
|File or folder name |Description|
|
||||
|:----- |:----|
|
||||
|en |English documents|
|
||||
|zh |中文文档(简体)|
|
|
@ -0,0 +1,194 @@
|
|||
# struct2json API 文档
|
||||
|
||||
---
|
||||
|
||||
所有支持的API接口都在 `\struct2json\inc\s2j.h` 中声明。以下内容较多,建议使用 CTRL+F 搜索。
|
||||
|
||||
## 1、用户使用接口
|
||||
|
||||
### 1.1 初始化
|
||||
|
||||
初始化的struct2json库。
|
||||
|
||||
> 注:目前主要初始化该库及cJSON库所需的内存管理方法。默认使用的 `malloc` 及 `free` 作为内存管理方法,如果使用默认内存管理方式,则无需初始化。
|
||||
|
||||
```C
|
||||
void s2j_init(S2jHook *hook)
|
||||
```
|
||||
|
||||
|参数 |描述|
|
||||
|:----- |:----|
|
||||
|hook |指定的内存管理方法|
|
||||
|
||||
### 1.2 将结构体对象转换(序列化)为JSON对象
|
||||
|
||||
注意:以下API均采用宏定义方式,在使用时与常用API方法形式略有不同,请注意查看Demo。
|
||||
|
||||
#### 1.2.1 创建JSON对象
|
||||
|
||||
```C
|
||||
s2j_create_json_obj(json_obj)
|
||||
```
|
||||
|
||||
|参数 |描述|
|
||||
|:----- |:----|
|
||||
|json_obj |JSON对象|
|
||||
|
||||
#### 1.2.2 转换基本类型元素
|
||||
|
||||
注意:这里的结构体元素基本类型只支持 `int` 、 `string` 及 `double` ,其他类型转换时应考虑选取三者中的其中一种作目标类型,选择标准为两者类型间可被无损相互转换。例如:结构体元素类型为 `uint8_t` 、`uint16_t` 、`int16_t` 、 `size_t` 、`某指针地址` 等类型可以选择 `int` 作为入参。
|
||||
|
||||
```C
|
||||
s2j_json_set_basic_element(to_json, from_struct, type, element)
|
||||
```
|
||||
|
||||
|参数 |描述|
|
||||
|:----- |:----|
|
||||
|to_json |目标JSON对象|
|
||||
|from_struct |源结构体对象|
|
||||
|type |源结构体对象元素类型,这里只支持int、string、double|
|
||||
|element |源结构体对象元素|
|
||||
|
||||
#### 1.2.3 转换数组类型元素
|
||||
|
||||
```C
|
||||
s2j_json_set_array_element(to_json, from_struct, type, element, size)
|
||||
```
|
||||
|
||||
|参数 |描述|
|
||||
|:----- |:----|
|
||||
|to_json |目标JSON对象|
|
||||
|from_struct |源结构体对象|
|
||||
|type |源结构体对象元素的基本类型。参考1.2.2中对基本类型的要求|
|
||||
|element |源结构体对象元素|
|
||||
|size |源结构体对象数组元素的长度|
|
||||
|
||||
#### 1.2.4 转换结构体类型元素(即子结构体)
|
||||
|
||||
```C
|
||||
s2j_json_set_struct_element(child_json, to_json, child_struct, from_struct, type, element)
|
||||
```
|
||||
|
||||
|参数 |描述|
|
||||
|:----- |:----|
|
||||
|child_json |目标JSON对象的子JSON对象|
|
||||
|to_json |目标JSON对象|
|
||||
|child_struct |源结构体对象的子结构体对象|
|
||||
|from_struct |源结构体对象|
|
||||
|type |源结构体对象元素(子结构体)类型|
|
||||
|element |源结构体对象元素|
|
||||
|
||||
例子:
|
||||
```C
|
||||
typedef struct {
|
||||
char name[16];
|
||||
} Hometown;
|
||||
|
||||
typedef struct {
|
||||
uint8_t id;
|
||||
char name[10];
|
||||
Hometown hometown;
|
||||
} Student;
|
||||
|
||||
Student orignal_struct_student = {
|
||||
.id = 24,
|
||||
.name = "armink",
|
||||
.hometown.name = "China",
|
||||
}
|
||||
Student *struct_student = &orignal_struct_student;
|
||||
/* 创建空Student JSON对象 */
|
||||
s2j_create_json_obj(json_student);
|
||||
/* 序列化(转换)Student结构体的id元素 */
|
||||
s2j_json_set_basic_element(json_student, struct_student, int, id);
|
||||
/* 序列化Student结构体的name元素 */
|
||||
s2j_json_set_basic_element(json_student, struct_student, string, name);
|
||||
/* 序列化Student结构体的子结构体hometown元素 */
|
||||
s2j_json_set_struct_element(json_hometown, json_student, struct_hometown, struct_student, Hometown, hometown);
|
||||
/* 序列化Hometown结构体的name元素 */
|
||||
s2j_json_set_basic_element(json_hometown, struct_hometown, string, name);
|
||||
```
|
||||
|
||||
### 1.3 将JSON对象转换(反序列化)为结构体对象
|
||||
|
||||
#### 1.3.1 创建结构体对象
|
||||
|
||||
```C
|
||||
s2j_create_struct_obj(struct_obj, type)
|
||||
```
|
||||
|
||||
|参数 |描述|
|
||||
|:----- |:----|
|
||||
|struct_obj |结构体对象|
|
||||
|type |结构体类型|
|
||||
|
||||
#### 1.3.2 转换基本类型元素
|
||||
|
||||
```C
|
||||
s2j_struct_get_basic_element(to_struct, from_json, type, element)
|
||||
```
|
||||
|
||||
|参数 |描述|
|
||||
|:----- |:----|
|
||||
|to_struct |目标结构体对象|
|
||||
|from_json |源JSON对象|
|
||||
|type |目标结构体对象元素类型。参考1.2.2中对基本类型的要求|
|
||||
|element |目标结构体对象元素|
|
||||
|
||||
#### 1.3.3 转换数组类型元素
|
||||
|
||||
```C
|
||||
s2j_struct_get_array_element(to_struct, from_json, type, element)
|
||||
```
|
||||
|
||||
|参数 |描述|
|
||||
|:----- |:----|
|
||||
|to_struct |目标结构体对象|
|
||||
|from_json |源JSON对象|
|
||||
|type |目标结构体对象元素类型。参考1.2.2中对基本类型的要求|
|
||||
|element |目标结构体对象元素|
|
||||
|
||||
#### 1.3.4 转换结构体类型元素(即子JSON)
|
||||
|
||||
```C
|
||||
s2j_struct_get_struct_element(child_struct, to_struct, child_json, from_json, type, element)
|
||||
```
|
||||
|
||||
|参数 |描述|
|
||||
|:----- |:----|
|
||||
|child_struct |目标结构体对象的子结构体对象|
|
||||
|to_struct |目标结构体对象|
|
||||
|child_json |源JSON对象的子JSON对象|
|
||||
|from_json |源JSON对象|
|
||||
|type |源JSON对象元素(子JSON)类型|
|
||||
|element |源JSON对象元素|
|
||||
|
||||
例子:
|
||||
```C
|
||||
typedef struct {
|
||||
char name[16];
|
||||
} Hometown;
|
||||
|
||||
typedef struct {
|
||||
uint8_t id;
|
||||
char name[10];
|
||||
Hometown hometown;
|
||||
} Student;
|
||||
|
||||
char orignal_json_string_student[] = "{\"id\":24, \"name\":\"armink\", \"hometown\":{\"name\":\"China\"}}";
|
||||
/* 创建Student JSON对象 */
|
||||
cJSON *json_student = cJSON_Parse(orignal_json_string_student);
|
||||
/* 创建空Student结构体对象 */
|
||||
s2j_create_struct_obj(struct_student, Student);
|
||||
/* 反序列化Student结构体的id元素 */
|
||||
s2j_struct_get_basic_element(struct_student, json_student, int, id);
|
||||
/* 反序列化Student结构体的name元素 */
|
||||
s2j_struct_get_basic_element(struct_student, json_student, string, name);
|
||||
/* 反序列化Student结构体的子结构体hometown元素 */
|
||||
s2j_struct_get_struct_element(struct_hometown, struct_student, json_hometown, json_student, Hometown, hometown);
|
||||
/* 反序列化Hometown结构体的name元素 */
|
||||
s2j_struct_get_basic_element(struct_hometown, json_hometown, string, name);
|
||||
```
|
||||
|
||||
## 2、注意
|
||||
|
||||
- 该库只适用于C99及其以上标准编译器
|
|
@ -0,0 +1,30 @@
|
|||
cJSON *struct_to_json(void* struct_obj) {
|
||||
Student *struct_student = (Student *) struct_obj;
|
||||
cJSON *score, *score_element;
|
||||
size_t index = 0;
|
||||
|
||||
/* 创建Student JSON对象 */
|
||||
cJSON *json_student = cJSON_CreateObject();
|
||||
|
||||
/* 序列化数据到Student JSON对象 */
|
||||
cJSON_AddNumberToObject(json_student, "id", struct_student->id);
|
||||
cJSON_AddNumberToObject(json_student, "weight", struct_student->weight);
|
||||
score = cJSON_CreateArray();
|
||||
if (score) {
|
||||
while (index < 8) {
|
||||
score_element = cJSON_CreateNumber(struct_student->score[index++]);
|
||||
cJSON_AddItemToArray(score, score_element);
|
||||
}
|
||||
cJSON_AddItemToObject(json_student, "score", score);
|
||||
}
|
||||
cJSON_AddStringToObject(json_student, "name", struct_student->name);
|
||||
|
||||
/* 序列化数据到Student.Hometown JSON对象 */
|
||||
Hometown *hometown_struct = &(struct_student->hometown);
|
||||
cJSON *hometown_json = cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(json_student, "hometown", hometown_json);
|
||||
cJSON_AddStringToObject(hometown_json, "name", hometown_struct->name);
|
||||
|
||||
/* 返回Student JSON对象指针 */
|
||||
return json_student;
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
void *json_to_struct(cJSON* json_obj) {
|
||||
cJSON *json_temp, *score, *score_element;
|
||||
Student *struct_student;
|
||||
size_t index = 0, score_size = 0;
|
||||
|
||||
/* 创建Student结构体对象 */
|
||||
struct_student = s2j_malloc(sizeof(Student));
|
||||
if (struct_student) {
|
||||
memset(struct_student, 0, sizeof(Student));
|
||||
}
|
||||
|
||||
/* 反序列化数据到Student结构体对象 */
|
||||
json_temp = cJSON_GetObjectItem(json_obj, "id");
|
||||
if (json_temp) {
|
||||
struct_student->id = json_temp->valueint;
|
||||
}
|
||||
score = cJSON_GetObjectItem(json_obj, "score")
|
||||
if (score) {
|
||||
score_size = cJSON_GetArraySize(score);
|
||||
while (index < score_size) {
|
||||
score_element = cJSON_GetArrayItem(score, index);
|
||||
if (score_element) {
|
||||
struct_student->score[index++] = score_element->valueint;
|
||||
}
|
||||
}
|
||||
}
|
||||
json_temp = cJSON_GetObjectItem(json_obj, "name");
|
||||
if (json_temp) {
|
||||
strcpy(struct_student->name, json_temp->valuestring);
|
||||
}
|
||||
json_temp = cJSON_GetObjectItem(json_obj, "weight");
|
||||
if (json_temp) {
|
||||
struct_student->weight = json_temp->valuedouble;
|
||||
}
|
||||
|
||||
/* 反序列化数据到Student.Hometown结构体对象 */
|
||||
Hometown *struct_hometown = &(struct_student->hometown);
|
||||
cJSON *json_hometown = cJSON_GetObjectItem(json_obj, "hometown");
|
||||
json_temp = cJSON_GetObjectItem(json_hometown, "name");
|
||||
if (json_temp) {
|
||||
strcpy(struct_hometown->name, json_temp->valuestring);
|
||||
}
|
||||
|
||||
/* 返回Student结构体对象指针 */
|
||||
return struct_student;
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
cJSON *struct_to_json(void* struct_obj) {
|
||||
Student *struct_student = (Student *)struct_obj;
|
||||
|
||||
/* 创建Student JSON对象 */
|
||||
s2j_create_json_obj(json_student);
|
||||
|
||||
/* 序列化数据到Student JSON对象 */
|
||||
s2j_json_set_basic_element(json_student, struct_student, int, id);
|
||||
s2j_json_set_basic_element(json_student, struct_student, double, weight);
|
||||
s2j_json_set_array_element(json_student, struct_student, int, score, 8);
|
||||
s2j_json_set_basic_element(json_student, struct_student, string, name);
|
||||
|
||||
/* 序列化数据到Student.Hometown JSON对象 */
|
||||
s2j_json_set_struct_element(json_hometown, json_student, struct_hometown, struct_student, Hometown, hometown);
|
||||
s2j_json_set_basic_element(json_hometown, struct_hometown, string, name);
|
||||
|
||||
/* 返回Student JSON对象指针 */
|
||||
return json_student;
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
void *json_to_struct(cJSON* json_obj) {
|
||||
/* 创建Student结构体对象 */
|
||||
s2j_create_struct_obj(struct_student, Student);
|
||||
|
||||
/* 反序列化数据到Student结构体对象 */
|
||||
s2j_struct_get_basic_element(struct_student, json_obj, int, id);
|
||||
s2j_struct_get_array_element(struct_student, json_obj, int, score);
|
||||
s2j_struct_get_basic_element(struct_student, json_obj, string, name);
|
||||
s2j_struct_get_basic_element(struct_student, json_obj, double, weight);
|
||||
|
||||
/* 反序列化数据到Student.Hometown结构体对象 */
|
||||
s2j_struct_get_struct_element(struct_hometown, struct_student, json_hometown, json_obj, Hometown, hometown);
|
||||
s2j_struct_get_basic_element(struct_hometown, json_hometown, string, name);
|
||||
|
||||
/* 返回Student结构体对象指针 */
|
||||
return struct_student;
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 84 KiB |
Binary file not shown.
After Width: | Height: | Size: 70 KiB |
Binary file not shown.
After Width: | Height: | Size: 58 KiB |
Binary file not shown.
After Width: | Height: | Size: 41 KiB |
|
@ -0,0 +1,3 @@
|
|||
|文件名 |描述|
|
||||
|:----- |:----|
|
||||
|api.md |API 说明|
|
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* This file is part of the struct2json Library.
|
||||
*
|
||||
* Copyright (c) 2015, Armink, <armink.ztl@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* 'Software'), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Function: It is an head file for this library. You can see all be called functions.
|
||||
* Created on: 2015-10-14
|
||||
*/
|
||||
|
||||
#ifndef __S2J_H__
|
||||
#define __S2J_H__
|
||||
|
||||
#include <cJSON/cJSON.h>
|
||||
#include <string.h>
|
||||
#include "s2jdef.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* struct2json software version number */
|
||||
#define S2J_SW_VERSION "1.0.3"
|
||||
|
||||
/* Create JSON object */
|
||||
#define s2j_create_json_obj(json_obj) \
|
||||
S2J_CREATE_JSON_OBJECT(json_obj)
|
||||
|
||||
/* Delete JSON object */
|
||||
#define s2j_delete_json_obj(json_obj) \
|
||||
S2J_DELETE_JSON_OBJECT(json_obj)
|
||||
|
||||
/* Set basic type element for JSON object */
|
||||
#define s2j_json_set_basic_element(to_json, from_struct, type, element) \
|
||||
S2J_JSON_SET_BASIC_ELEMENT(to_json, from_struct, type, element)
|
||||
|
||||
/* Set array type element for JSON object */
|
||||
#define s2j_json_set_array_element(to_json, from_struct, type, element, size) \
|
||||
S2J_JSON_SET_ARRAY_ELEMENT(to_json, from_struct, type, element, size)
|
||||
|
||||
/* Set child structure type element for JSON object */
|
||||
#define s2j_json_set_struct_element(child_json, to_json, child_struct, from_struct, type, element) \
|
||||
S2J_JSON_SET_STRUCT_ELEMENT(child_json, to_json, child_struct, from_struct, type, element)
|
||||
|
||||
/* Create structure object */
|
||||
#define s2j_create_struct_obj(struct_obj, type) \
|
||||
S2J_CREATE_STRUCT_OBJECT(struct_obj, type)
|
||||
|
||||
/* Delete structure object */
|
||||
#define s2j_delete_struct_obj(struct_obj) \
|
||||
S2J_DELETE_STRUCT_OBJECT(struct_obj)
|
||||
|
||||
/* Get basic type element for structure object */
|
||||
#define s2j_struct_get_basic_element(to_struct, from_json, type, element) \
|
||||
S2J_STRUCT_GET_BASIC_ELEMENT(to_struct, from_json, type, element)
|
||||
|
||||
/* Get array type element for structure object */
|
||||
#define s2j_struct_get_array_element(to_struct, from_json, type, element) \
|
||||
S2J_STRUCT_GET_ARRAY_ELEMENT(to_struct, from_json, type, element)
|
||||
|
||||
/* Get child structure type element for structure object */
|
||||
#define s2j_struct_get_struct_element(child_struct, to_struct, child_json, from_json, type, element) \
|
||||
S2J_STRUCT_GET_STRUCT_ELEMENT(child_struct, to_struct, child_json, from_json, type, element)
|
||||
|
||||
/* s2j.c */
|
||||
extern S2jHook s2jHook;
|
||||
void s2j_init(S2jHook *hook);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __S2J_H__ */
|
|
@ -0,0 +1,150 @@
|
|||
/*
|
||||
* This file is part of the struct2json Library.
|
||||
*
|
||||
* Copyright (c) 2015, Armink, <armink.ztl@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* 'Software'), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Function: It is an head file for this library.
|
||||
* Created on: 2015-10-14
|
||||
*/
|
||||
|
||||
#ifndef __S2JDEF_H__
|
||||
#define __S2JDEF_H__
|
||||
|
||||
#include <cJSON/cJSON.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
void *(*malloc_fn)(size_t sz);
|
||||
void (*free_fn)(void *ptr);
|
||||
} S2jHook, *S2jHook_t;
|
||||
|
||||
#define S2J_STRUCT_GET_int_ELEMENT(to_struct, from_json, _element) \
|
||||
json_temp = cJSON_GetObjectItem(from_json, #_element); \
|
||||
if (json_temp) (to_struct)->_element = json_temp->valueint;
|
||||
|
||||
#define S2J_STRUCT_GET_string_ELEMENT(to_struct, from_json, _element) \
|
||||
json_temp = cJSON_GetObjectItem(from_json, #_element); \
|
||||
if (json_temp) strcpy((to_struct)->_element, json_temp->valuestring);
|
||||
|
||||
#define S2J_STRUCT_GET_double_ELEMENT(to_struct, from_json, _element) \
|
||||
json_temp = cJSON_GetObjectItem(from_json, #_element); \
|
||||
if (json_temp) (to_struct)->_element = json_temp->valuedouble;
|
||||
|
||||
#define S2J_STRUCT_ARRAY_GET_int_ELEMENT(to_struct, from_json, _element, index) \
|
||||
(to_struct)->_element[index] = from_json->valueint;
|
||||
|
||||
#define S2J_STRUCT_ARRAY_GET_string_ELEMENT(to_struct, from_json, _element, index) \
|
||||
strcpy((to_struct)->_element[index], from_json->valuestring);
|
||||
|
||||
#define S2J_STRUCT_ARRAY_GET_double_ELEMENT(to_struct, from_json, _element, index) \
|
||||
(to_struct)->_element[index] = from_json->valuedouble;
|
||||
|
||||
#define S2J_STRUCT_ARRAY_GET_ELEMENT(to_struct, from_json, type, _element, index) \
|
||||
S2J_STRUCT_ARRAY_GET_##type##_ELEMENT(to_struct, from_json, _element, index)
|
||||
|
||||
#define S2J_JSON_SET_int_ELEMENT(to_json, from_struct, _element) \
|
||||
cJSON_AddNumberToObject(to_json, #_element, (from_struct)->_element);
|
||||
|
||||
#define S2J_JSON_SET_double_ELEMENT(to_json, from_struct, _element) \
|
||||
cJSON_AddNumberToObject(to_json, #_element, (from_struct)->_element);
|
||||
|
||||
#define S2J_JSON_SET_string_ELEMENT(to_json, from_struct, _element) \
|
||||
cJSON_AddStringToObject(to_json, #_element, (from_struct)->_element);
|
||||
|
||||
#define S2J_JSON_ARRAY_SET_int_ELEMENT(to_json, from_struct, _element, index) \
|
||||
cJSON_AddItemToArray(to_json, cJSON_CreateNumber((from_struct)->_element[index]));
|
||||
|
||||
#define S2J_JSON_ARRAY_SET_double_ELEMENT(to_json, from_struct, _element, index) \
|
||||
cJSON_AddItemToArray(to_json, cJSON_CreateNumber((from_struct)->_element[index]));
|
||||
|
||||
#define S2J_JSON_ARRAY_SET_string_ELEMENT(to_json, from_struct, _element, index) \
|
||||
cJSON_AddItemToArray(to_json, cJSON_CreateString((from_struct)->_element[index]));
|
||||
|
||||
#define S2J_JSON_ARRAY_SET_ELEMENT(to_json, from_struct, type, _element, index) \
|
||||
S2J_JSON_ARRAY_SET_##type##_ELEMENT(to_json, from_struct, _element, index)
|
||||
|
||||
|
||||
#define S2J_CREATE_JSON_OBJECT(json_obj) \
|
||||
cJSON *json_obj = cJSON_CreateObject();
|
||||
|
||||
#define S2J_DELETE_JSON_OBJECT(json_obj) \
|
||||
cJSON_Delete(json_obj);
|
||||
|
||||
#define S2J_JSON_SET_BASIC_ELEMENT(to_json, from_struct, type, _element) \
|
||||
S2J_JSON_SET_##type##_ELEMENT(to_json, from_struct, _element)
|
||||
|
||||
#define S2J_JSON_SET_ARRAY_ELEMENT(to_json, from_struct, type, _element, size) \
|
||||
{ \
|
||||
cJSON *array; \
|
||||
size_t index = 0; \
|
||||
array = cJSON_CreateArray(); \
|
||||
if (array) { \
|
||||
while (index < size) { \
|
||||
S2J_JSON_ARRAY_SET_ELEMENT(array, from_struct, type, _element, index++); \
|
||||
} \
|
||||
cJSON_AddItemToObject(to_json, #_element, array); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define S2J_JSON_SET_STRUCT_ELEMENT(child_json, to_json, child_struct, from_struct, type, _element) \
|
||||
type *child_struct = &((from_struct)->_element); \
|
||||
cJSON *child_json = cJSON_CreateObject(); \
|
||||
if (child_json) cJSON_AddItemToObject(to_json, #_element, child_json);
|
||||
|
||||
#define S2J_CREATE_STRUCT_OBJECT(struct_obj, type) \
|
||||
cJSON *json_temp; \
|
||||
type *struct_obj = s2jHook.malloc_fn(sizeof(type)); \
|
||||
if (struct_obj) memset(struct_obj, 0, sizeof(type));
|
||||
|
||||
#define S2J_DELETE_STRUCT_OBJECT(struct_obj) \
|
||||
s2jHook.free_fn(struct_obj);
|
||||
|
||||
#define S2J_STRUCT_GET_BASIC_ELEMENT(to_struct, from_json, type, _element) \
|
||||
S2J_STRUCT_GET_##type##_ELEMENT(to_struct, from_json, _element)
|
||||
|
||||
#define S2J_STRUCT_GET_ARRAY_ELEMENT(to_struct, from_json, type, _element) \
|
||||
{ \
|
||||
cJSON *array, *array_element; \
|
||||
size_t index = 0, size = 0; \
|
||||
array = cJSON_GetObjectItem(from_json, #_element); \
|
||||
if (array) { \
|
||||
size = cJSON_GetArraySize(array); \
|
||||
while (index < size) { \
|
||||
array_element = cJSON_GetArrayItem(array, index); \
|
||||
if (array_element) S2J_STRUCT_ARRAY_GET_ELEMENT(to_struct, array_element, type, _element, index++); \
|
||||
} \
|
||||
} \
|
||||
}
|
||||
|
||||
#define S2J_STRUCT_GET_STRUCT_ELEMENT(child_struct, to_struct, child_json, from_json, type, _element) \
|
||||
type *child_struct = &((to_struct)->_element); \
|
||||
cJSON *child_json = cJSON_GetObjectItem(from_json, #_element);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __S2JDEF_H__ */
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* This file is part of the struct2json Library.
|
||||
*
|
||||
* Copyright (c) 2015, Armink, <armink.ztl@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* 'Software'), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Function: Initialize interface for this library.
|
||||
* Created on: 2015-10-14
|
||||
*/
|
||||
|
||||
#include <s2j.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
S2jHook s2jHook = {
|
||||
.malloc_fn = malloc,
|
||||
.free_fn = free,
|
||||
};
|
||||
|
||||
/**
|
||||
* struct2json library initialize
|
||||
* @note It will initialize cJSON library hooks.
|
||||
*/
|
||||
void s2j_init(S2jHook *hook) {
|
||||
/* initialize cJSON library */
|
||||
cJSON_InitHooks((cJSON_Hooks *)hook);
|
||||
/* initialize hooks */
|
||||
if (hook) {
|
||||
s2jHook.malloc_fn = (hook->malloc_fn) ? hook->malloc_fn : malloc;
|
||||
s2jHook.free_fn = (hook->free_fn) ? hook->free_fn : free;
|
||||
} else {
|
||||
s2jHook.malloc_fn = malloc;
|
||||
s2jHook.free_fn = free;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue