From f5c38b5127ff32bdef0b4993515b2321e5df110a Mon Sep 17 00:00:00 2001 From: jxjajs Date: Fri, 21 Mar 2025 11:41:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=8D=97=E7=BD=91mqtt?= =?UTF-8?q?=EF=BC=88s938=EF=BC=89=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/cpp/cJSON.c | 3096 ++++++++++++++++++++++ app/src/main/cpp/cJSON.h | 293 +++ app/src/main/cpp/mqtt_south.c | 4553 +++++++++++++++++++++++++++++++++ app/src/main/cpp/mqtt_south.h | 154 ++ 4 files changed, 8096 insertions(+) create mode 100644 app/src/main/cpp/cJSON.c create mode 100644 app/src/main/cpp/cJSON.h create mode 100644 app/src/main/cpp/mqtt_south.c create mode 100644 app/src/main/cpp/mqtt_south.h diff --git a/app/src/main/cpp/cJSON.c b/app/src/main/cpp/cJSON.c new file mode 100644 index 00000000..3cdc8fa5 --- /dev/null +++ b/app/src/main/cpp/cJSON.c @@ -0,0 +1,3096 @@ +/* + 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. +*/ + +/* cJSON */ +/* JSON parser in C. */ + +/* disable warnings about old C89 functions in MSVC */ +#if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) +#define _CRT_SECURE_NO_DEPRECATE +#endif + +#ifdef __GNUC__ +#pragma GCC visibility push(default) +#endif +#if defined(_MSC_VER) +#pragma warning (push) +/* disable warning about single line comments in system headers */ +#pragma warning (disable : 4001) +#endif + +#include +#include +#include +#include +#include +#include +#include + +#ifdef ENABLE_LOCALES +#include +#endif + +#if defined(_MSC_VER) +#pragma warning (pop) +#endif +#ifdef __GNUC__ +#pragma GCC visibility pop +#endif + +#include "cJSON.h" + +/* define our own boolean type */ +#ifdef true +#undef true +#endif +#define true ((cJSON_bool)1) + +#ifdef false +#undef false +#endif +#define false ((cJSON_bool)0) + +/* define isnan and isinf for ANSI C, if in C99 or above, isnan and isinf has been defined in math.h */ +#ifndef isinf +#define isinf(d) (isnan((d - d)) && !isnan(d)) +#endif +#ifndef isnan +#define isnan(d) (d != d) +#endif + +#ifndef NAN +#define NAN 0.0/0.0 +#endif + +typedef struct { + const unsigned char *json; + size_t position; +} error; +static error global_error = { NULL, 0 }; + +CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void) +{ + return (const char*) (global_error.json + global_error.position); +} + +CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item) +{ + if (!cJSON_IsString(item)) + { + return NULL; + } + + return item->valuestring; +} + +CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item) +{ + if (!cJSON_IsNumber(item)) + { + return NAN; + } + + return item->valuedouble; +} + +/* This is a safeguard to prevent copy-pasters from using incompatible C and header files */ +#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 13) + #error cJSON.h and cJSON.c have different versions. Make sure that both have the same. +#endif + +CJSON_PUBLIC(const char*) cJSON_Version(void) +{ + static char version[15]; + sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH); + + return version; +} + +/* Case insensitive string comparison, doesn't consider two NULL pointers equal though */ +static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2) +{ + if ((string1 == NULL) || (string2 == NULL)) + { + return 1; + } + + if (string1 == string2) + { + return 0; + } + + for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++) + { + if (*string1 == '\0') + { + return 0; + } + } + + return tolower(*string1) - tolower(*string2); +} + +typedef struct internal_hooks +{ + void *(CJSON_CDECL *allocate)(size_t size); + void (CJSON_CDECL *deallocate)(void *pointer); + void *(CJSON_CDECL *reallocate)(void *pointer, size_t size); +} internal_hooks; + +#if defined(_MSC_VER) +/* work around MSVC error C2322: '...' address of dllimport '...' is not static */ +static void * CJSON_CDECL internal_malloc(size_t size) +{ + return malloc(size); +} +static void CJSON_CDECL internal_free(void *pointer) +{ + free(pointer); +} +static void * CJSON_CDECL internal_realloc(void *pointer, size_t size) +{ + return realloc(pointer, size); +} +#else +#define internal_malloc malloc +#define internal_free free +#define internal_realloc realloc +#endif + +/* strlen of character literals resolved at compile time */ +#define static_strlen(string_literal) (sizeof(string_literal) - sizeof("")) + +static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc }; + +static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks) +{ + size_t length = 0; + unsigned char *copy = NULL; + + if (string == NULL) + { + return NULL; + } + + length = strlen((const char*)string) + sizeof(""); + copy = (unsigned char*)hooks->allocate(length); + if (copy == NULL) + { + return NULL; + } + memcpy(copy, string, length); + + return copy; +} + +CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) +{ + if (hooks == NULL) + { + /* Reset hooks */ + global_hooks.allocate = malloc; + global_hooks.deallocate = free; + global_hooks.reallocate = realloc; + return; + } + + global_hooks.allocate = malloc; + if (hooks->malloc_fn != NULL) + { + global_hooks.allocate = hooks->malloc_fn; + } + + global_hooks.deallocate = free; + if (hooks->free_fn != NULL) + { + global_hooks.deallocate = hooks->free_fn; + } + + /* use realloc only if both free and malloc are used */ + global_hooks.reallocate = NULL; + if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free)) + { + global_hooks.reallocate = realloc; + } +} + +/* Internal constructor. */ +static cJSON *cJSON_New_Item(const internal_hooks * const hooks) +{ + cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON)); + if (node) + { + memset(node, '\0', sizeof(cJSON)); + } + + return node; +} + +/* Delete a cJSON structure. */ +CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) +{ + cJSON *next = NULL; + while (item != NULL) + { + next = item->next; + if (!(item->type & cJSON_IsReference) && (item->child != NULL)) + { + cJSON_Delete(item->child); + } + if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL)) + { + global_hooks.deallocate(item->valuestring); + } + if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) + { + global_hooks.deallocate(item->string); + } + global_hooks.deallocate(item); + item = next; + } +} + +/* get the decimal point character of the current locale */ +static unsigned char get_decimal_point(void) +{ +#ifdef ENABLE_LOCALES + struct lconv *lconv = localeconv(); + return (unsigned char) lconv->decimal_point[0]; +#else + return '.'; +#endif +} + +typedef struct +{ + const unsigned char *content; + size_t length; + size_t offset; + size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */ + internal_hooks hooks; +} parse_buffer; + +/* check if the given size is left to read in a given parse buffer (starting with 1) */ +#define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length)) +/* check if the buffer can be accessed at the given index (starting with 0) */ +#define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length)) +#define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index)) +/* get a pointer to the buffer at the position */ +#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset) + +/* Parse the input text to generate a number, and populate the result into item. */ +static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer) +{ + double number = 0; + unsigned char *after_end = NULL; + unsigned char number_c_string[64]; + unsigned char decimal_point = get_decimal_point(); + size_t i = 0; + + if ((input_buffer == NULL) || (input_buffer->content == NULL)) + { + return false; + } + + /* copy the number into a temporary buffer and replace '.' with the decimal point + * of the current locale (for strtod) + * This also takes care of '\0' not necessarily being available for marking the end of the input */ + for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++) + { + switch (buffer_at_offset(input_buffer)[i]) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '+': + case '-': + case 'e': + case 'E': + number_c_string[i] = buffer_at_offset(input_buffer)[i]; + break; + + case '.': + number_c_string[i] = decimal_point; + break; + + default: + goto loop_end; + } + } +loop_end: + number_c_string[i] = '\0'; + + number = strtod((const char*)number_c_string, (char**)&after_end); + if (number_c_string == after_end) + { + return false; /* parse_error */ + } + + item->valuedouble = number; + + /* use saturation in case of overflow */ + if (number >= INT_MAX) + { + item->valueint = INT_MAX; + } + else if (number <= (double)INT_MIN) + { + item->valueint = INT_MIN; + } + else + { + item->valueint = (int)number; + } + + item->type = cJSON_Number; + + input_buffer->offset += (size_t)(after_end - number_c_string); + return true; +} + +/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */ +CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number) +{ + if (number >= INT_MAX) + { + object->valueint = INT_MAX; + } + else if (number <= (double)INT_MIN) + { + object->valueint = INT_MIN; + } + else + { + object->valueint = (int)number; + } + + return object->valuedouble = number; +} + +CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring) +{ + char *copy = NULL; + /* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */ + if (!(object->type & cJSON_String) || (object->type & cJSON_IsReference)) + { + return NULL; + } + if (strlen(valuestring) <= strlen(object->valuestring)) + { + strcpy(object->valuestring, valuestring); + return object->valuestring; + } + copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks); + if (copy == NULL) + { + return NULL; + } + if (object->valuestring != NULL) + { + cJSON_free(object->valuestring); + } + object->valuestring = copy; + + return copy; +} + +typedef struct +{ + unsigned char *buffer; + size_t length; + size_t offset; + size_t depth; /* current nesting depth (for formatted printing) */ + cJSON_bool noalloc; + cJSON_bool format; /* is this print a formatted print */ + internal_hooks hooks; +} printbuffer; + +/* realloc printbuffer if necessary to have at least "needed" bytes more */ +static unsigned char* ensure(printbuffer * const p, size_t needed) +{ + unsigned char *newbuffer = NULL; + size_t newsize = 0; + + if ((p == NULL) || (p->buffer == NULL)) + { + return NULL; + } + + if ((p->length > 0) && (p->offset >= p->length)) + { + /* make sure that offset is valid */ + return NULL; + } + + if (needed > INT_MAX) + { + /* sizes bigger than INT_MAX are currently not supported */ + return NULL; + } + + needed += p->offset + 1; + if (needed <= p->length) + { + return p->buffer + p->offset; + } + + if (p->noalloc) { + return NULL; + } + + /* calculate new buffer size */ + if (needed > (INT_MAX / 2)) + { + /* overflow of int, use INT_MAX if possible */ + if (needed <= INT_MAX) + { + newsize = INT_MAX; + } + else + { + return NULL; + } + } + else + { + newsize = needed * 2; + } + + if (p->hooks.reallocate != NULL) + { + /* reallocate with realloc if available */ + newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize); + if (newbuffer == NULL) + { + p->hooks.deallocate(p->buffer); + p->length = 0; + p->buffer = NULL; + + return NULL; + } + } + else + { + /* otherwise reallocate manually */ + newbuffer = (unsigned char*)p->hooks.allocate(newsize); + if (!newbuffer) + { + p->hooks.deallocate(p->buffer); + p->length = 0; + p->buffer = NULL; + + return NULL; + } + if (newbuffer) + { + memcpy(newbuffer, p->buffer, p->offset + 1); + } + p->hooks.deallocate(p->buffer); + } + p->length = newsize; + p->buffer = newbuffer; + + return newbuffer + p->offset; +} + +/* calculate the new length of the string in a printbuffer and update the offset */ +static void update_offset(printbuffer * const buffer) +{ + const unsigned char *buffer_pointer = NULL; + if ((buffer == NULL) || (buffer->buffer == NULL)) + { + return; + } + buffer_pointer = buffer->buffer + buffer->offset; + + buffer->offset += strlen((const char*)buffer_pointer); +} + +/* securely comparison of floating-point variables */ +static cJSON_bool compare_double(double a, double b) +{ + double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b); + return (fabs(a - b) <= maxVal * DBL_EPSILON); +} + +/* Render the number nicely from the given item into a string. */ +static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + double d = item->valuedouble; + int length = 0; + size_t i = 0; + unsigned char number_buffer[26] = {0}; /* temporary buffer to print the number into */ + unsigned char decimal_point = get_decimal_point(); + double test = 0.0; + + if (output_buffer == NULL) + { + return false; + } + + /* This checks for NaN and Infinity */ + if (isnan(d) || isinf(d)) + { + length = sprintf((char*)number_buffer, "null"); + } + else + { + /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */ + length = sprintf((char*)number_buffer, "%1.15g", d); + + /* Check whether the original double can be recovered */ + if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d)) + { + /* If not, print with 17 decimal places of precision */ + length = sprintf((char*)number_buffer, "%1.17g", d); + } + } + + /* sprintf failed or buffer overrun occurred */ + if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1))) + { + return false; + } + + /* reserve appropriate space in the output */ + output_pointer = ensure(output_buffer, (size_t)length + sizeof("")); + if (output_pointer == NULL) + { + return false; + } + + /* copy the printed number to the output and replace locale + * dependent decimal point with '.' */ + for (i = 0; i < ((size_t)length); i++) + { + if (number_buffer[i] == decimal_point) + { + output_pointer[i] = '.'; + continue; + } + + output_pointer[i] = number_buffer[i]; + } + output_pointer[i] = '\0'; + + output_buffer->offset += (size_t)length; + + return true; +} + +/* parse 4 digit hexadecimal number */ +static unsigned parse_hex4(const unsigned char * const input) +{ + unsigned int h = 0; + size_t i = 0; + + for (i = 0; i < 4; i++) + { + /* parse digit */ + if ((input[i] >= '0') && (input[i] <= '9')) + { + h += (unsigned int) input[i] - '0'; + } + else if ((input[i] >= 'A') && (input[i] <= 'F')) + { + h += (unsigned int) 10 + input[i] - 'A'; + } + else if ((input[i] >= 'a') && (input[i] <= 'f')) + { + h += (unsigned int) 10 + input[i] - 'a'; + } + else /* invalid */ + { + return 0; + } + + if (i < 3) + { + /* shift left to make place for the next nibble */ + h = h << 4; + } + } + + return h; +} + +/* converts a UTF-16 literal to UTF-8 + * A literal can be one or two sequences of the form \uXXXX */ +static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer) +{ + long unsigned int codepoint = 0; + unsigned int first_code = 0; + const unsigned char *first_sequence = input_pointer; + unsigned char utf8_length = 0; + unsigned char utf8_position = 0; + unsigned char sequence_length = 0; + unsigned char first_byte_mark = 0; + + if ((input_end - first_sequence) < 6) + { + /* input ends unexpectedly */ + goto fail; + } + + /* get the first utf16 sequence */ + first_code = parse_hex4(first_sequence + 2); + + /* check that the code is valid */ + if (((first_code >= 0xDC00) && (first_code <= 0xDFFF))) + { + goto fail; + } + + /* UTF16 surrogate pair */ + if ((first_code >= 0xD800) && (first_code <= 0xDBFF)) + { + const unsigned char *second_sequence = first_sequence + 6; + unsigned int second_code = 0; + sequence_length = 12; /* \uXXXX\uXXXX */ + + if ((input_end - second_sequence) < 6) + { + /* input ends unexpectedly */ + goto fail; + } + + if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u')) + { + /* missing second half of the surrogate pair */ + goto fail; + } + + /* get the second utf16 sequence */ + second_code = parse_hex4(second_sequence + 2); + /* check that the code is valid */ + if ((second_code < 0xDC00) || (second_code > 0xDFFF)) + { + /* invalid second half of the surrogate pair */ + goto fail; + } + + + /* calculate the unicode codepoint from the surrogate pair */ + codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF)); + } + else + { + sequence_length = 6; /* \uXXXX */ + codepoint = first_code; + } + + /* encode as UTF-8 + * takes at maximum 4 bytes to encode: + * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ + if (codepoint < 0x80) + { + /* normal ascii, encoding 0xxxxxxx */ + utf8_length = 1; + } + else if (codepoint < 0x800) + { + /* two bytes, encoding 110xxxxx 10xxxxxx */ + utf8_length = 2; + first_byte_mark = 0xC0; /* 11000000 */ + } + else if (codepoint < 0x10000) + { + /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */ + utf8_length = 3; + first_byte_mark = 0xE0; /* 11100000 */ + } + else if (codepoint <= 0x10FFFF) + { + /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */ + utf8_length = 4; + first_byte_mark = 0xF0; /* 11110000 */ + } + else + { + /* invalid unicode codepoint */ + goto fail; + } + + /* encode as utf8 */ + for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--) + { + /* 10xxxxxx */ + (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF); + codepoint >>= 6; + } + /* encode first byte */ + if (utf8_length > 1) + { + (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF); + } + else + { + (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F); + } + + *output_pointer += utf8_length; + + return sequence_length; + +fail: + return 0; +} + +/* Parse the input text into an unescaped cinput, and populate item. */ +static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer) +{ + const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1; + const unsigned char *input_end = buffer_at_offset(input_buffer) + 1; + unsigned char *output_pointer = NULL; + unsigned char *output = NULL; + + /* not a string */ + if (buffer_at_offset(input_buffer)[0] != '\"') + { + goto fail; + } + + { + /* calculate approximate size of the output (overestimate) */ + size_t allocation_length = 0; + size_t skipped_bytes = 0; + while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"')) + { + /* is escape sequence */ + if (input_end[0] == '\\') + { + if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length) + { + /* prevent buffer overflow when last input character is a backslash */ + goto fail; + } + skipped_bytes++; + input_end++; + } + input_end++; + } + if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"')) + { + goto fail; /* string ended unexpectedly */ + } + + /* This is at most how much we need for the output */ + allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes; + output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof("")); + if (output == NULL) + { + goto fail; /* allocation failure */ + } + } + + output_pointer = output; + /* loop through the string literal */ + while (input_pointer < input_end) + { + if (*input_pointer != '\\') + { + *output_pointer++ = *input_pointer++; + } + /* escape sequence */ + else + { + unsigned char sequence_length = 2; + if ((input_end - input_pointer) < 1) + { + goto fail; + } + + switch (input_pointer[1]) + { + case 'b': + *output_pointer++ = '\b'; + break; + case 'f': + *output_pointer++ = '\f'; + break; + case 'n': + *output_pointer++ = '\n'; + break; + case 'r': + *output_pointer++ = '\r'; + break; + case 't': + *output_pointer++ = '\t'; + break; + case '\"': + case '\\': + case '/': + *output_pointer++ = input_pointer[1]; + break; + + /* UTF-16 literal */ + case 'u': + sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer); + if (sequence_length == 0) + { + /* failed to convert UTF16-literal to UTF-8 */ + goto fail; + } + break; + + default: + goto fail; + } + input_pointer += sequence_length; + } + } + + /* zero terminate the output */ + *output_pointer = '\0'; + + item->type = cJSON_String; + item->valuestring = (char*)output; + + input_buffer->offset = (size_t) (input_end - input_buffer->content); + input_buffer->offset++; + + return true; + +fail: + if (output != NULL) + { + input_buffer->hooks.deallocate(output); + } + + if (input_pointer != NULL) + { + input_buffer->offset = (size_t)(input_pointer - input_buffer->content); + } + + return false; +} + +/* Render the cstring provided to an escaped version that can be printed. */ +static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer) +{ + const unsigned char *input_pointer = NULL; + unsigned char *output = NULL; + unsigned char *output_pointer = NULL; + size_t output_length = 0; + /* numbers of additional characters needed for escaping */ + size_t escape_characters = 0; + + if (output_buffer == NULL) + { + return false; + } + + /* empty string */ + if (input == NULL) + { + output = ensure(output_buffer, sizeof("\"\"")); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "\"\""); + + return true; + } + + /* set "flag" to 1 if something needs to be escaped */ + for (input_pointer = input; *input_pointer; input_pointer++) + { + switch (*input_pointer) + { + case '\"': + case '\\': + case '\b': + case '\f': + case '\n': + case '\r': + case '\t': + /* one character escape sequence */ + escape_characters++; + break; + default: + if (*input_pointer < 32) + { + /* UTF-16 escape sequence uXXXX */ + escape_characters += 5; + } + break; + } + } + output_length = (size_t)(input_pointer - input) + escape_characters; + + output = ensure(output_buffer, output_length + sizeof("\"\"")); + if (output == NULL) + { + return false; + } + + /* no characters have to be escaped */ + if (escape_characters == 0) + { + output[0] = '\"'; + memcpy(output + 1, input, output_length); + output[output_length + 1] = '\"'; + output[output_length + 2] = '\0'; + + return true; + } + + output[0] = '\"'; + output_pointer = output + 1; + /* copy the string */ + for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++) + { + if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\')) + { + /* normal character, copy */ + *output_pointer = *input_pointer; + } + else + { + /* character needs to be escaped */ + *output_pointer++ = '\\'; + switch (*input_pointer) + { + case '\\': + *output_pointer = '\\'; + break; + case '\"': + *output_pointer = '\"'; + break; + case '\b': + *output_pointer = 'b'; + break; + case '\f': + *output_pointer = 'f'; + break; + case '\n': + *output_pointer = 'n'; + break; + case '\r': + *output_pointer = 'r'; + break; + case '\t': + *output_pointer = 't'; + break; + default: + /* escape and print as unicode codepoint */ + sprintf((char*)output_pointer, "u%04x", *input_pointer); + output_pointer += 4; + break; + } + } + } + output[output_length + 1] = '\"'; + output[output_length + 2] = '\0'; + + return true; +} + +/* Invoke print_string_ptr (which is useful) on an item. */ +static cJSON_bool print_string(const cJSON * const item, printbuffer * const p) +{ + return print_string_ptr((unsigned char*)item->valuestring, p); +} + +/* Predeclare these prototypes. */ +static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer); +static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer); +static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer); + +/* Utility to jump whitespace and cr/lf */ +static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) +{ + if ((buffer == NULL) || (buffer->content == NULL)) + { + return NULL; + } + + if (cannot_access_at_index(buffer, 0)) + { + return buffer; + } + + while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32)) + { + buffer->offset++; + } + + if (buffer->offset == buffer->length) + { + buffer->offset--; + } + + return buffer; +} + +/* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */ +static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) +{ + if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0)) + { + return NULL; + } + + if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0)) + { + buffer->offset += 3; + } + + return buffer; +} + +CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated) +{ + size_t buffer_length; + + if (NULL == value) + { + return NULL; + } + + /* Adding null character size due to require_null_terminated. */ + buffer_length = strlen(value) + sizeof(""); + + return cJSON_ParseWithLengthOpts(value, buffer_length, return_parse_end, require_null_terminated); +} + +/* Parse an object - create a new root, and populate. */ +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated) +{ + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + cJSON *item = NULL; + + /* reset error position */ + global_error.json = NULL; + global_error.position = 0; + + if (value == NULL || 0 == buffer_length) + { + goto fail; + } + + buffer.content = (const unsigned char*)value; + buffer.length = buffer_length; + buffer.offset = 0; + buffer.hooks = global_hooks; + + item = cJSON_New_Item(&global_hooks); + if (item == NULL) /* memory fail */ + { + goto fail; + } + + if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)))) + { + /* parse failure. ep is set. */ + goto fail; + } + + /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ + if (require_null_terminated) + { + buffer_skip_whitespace(&buffer); + if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') + { + goto fail; + } + } + if (return_parse_end) + { + *return_parse_end = (const char*)buffer_at_offset(&buffer); + } + + return item; + +fail: + if (item != NULL) + { + cJSON_Delete(item); + } + + if (value != NULL) + { + error local_error; + local_error.json = (const unsigned char*)value; + local_error.position = 0; + + if (buffer.offset < buffer.length) + { + local_error.position = buffer.offset; + } + else if (buffer.length > 0) + { + local_error.position = buffer.length - 1; + } + + if (return_parse_end != NULL) + { + *return_parse_end = (const char*)local_error.json + local_error.position; + } + + global_error = local_error; + } + + return NULL; +} + +/* Default options for cJSON_Parse */ +CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value) +{ + return cJSON_ParseWithOpts(value, 0, 0); +} + +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length) +{ + return cJSON_ParseWithLengthOpts(value, buffer_length, 0, 0); +} + +#define cjson_min(a, b) (((a) < (b)) ? (a) : (b)) + +static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks) +{ + static const size_t default_buffer_size = 256; + printbuffer buffer[1]; + unsigned char *printed = NULL; + + memset(buffer, 0, sizeof(buffer)); + + /* create buffer */ + buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size); + buffer->length = default_buffer_size; + buffer->format = format; + buffer->hooks = *hooks; + if (buffer->buffer == NULL) + { + goto fail; + } + + /* print the value */ + if (!print_value(item, buffer)) + { + goto fail; + } + update_offset(buffer); + + /* check if reallocate is available */ + if (hooks->reallocate != NULL) + { + printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1); + if (printed == NULL) { + goto fail; + } + buffer->buffer = NULL; + } + else /* otherwise copy the JSON over to a new buffer */ + { + printed = (unsigned char*) hooks->allocate(buffer->offset + 1); + if (printed == NULL) + { + goto fail; + } + memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1)); + printed[buffer->offset] = '\0'; /* just to be sure */ + + /* free the buffer */ + hooks->deallocate(buffer->buffer); + } + + return printed; + +fail: + if (buffer->buffer != NULL) + { + hooks->deallocate(buffer->buffer); + } + + if (printed != NULL) + { + hooks->deallocate(printed); + } + + return NULL; +} + +/* Render a cJSON item/entity/structure to text. */ +CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item) +{ + return (char*)print(item, true, &global_hooks); +} + +CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item) +{ + return (char*)print(item, false, &global_hooks); +} + +CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt) +{ + printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + + if (prebuffer < 0) + { + return NULL; + } + + p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer); + if (!p.buffer) + { + return NULL; + } + + p.length = (size_t)prebuffer; + p.offset = 0; + p.noalloc = false; + p.format = fmt; + p.hooks = global_hooks; + + if (!print_value(item, &p)) + { + global_hooks.deallocate(p.buffer); + return NULL; + } + + return (char*)p.buffer; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format) +{ + printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + + if ((length < 0) || (buffer == NULL)) + { + return false; + } + + p.buffer = (unsigned char*)buffer; + p.length = (size_t)length; + p.offset = 0; + p.noalloc = true; + p.format = format; + p.hooks = global_hooks; + + return print_value(item, &p); +} + +/* Parser core - when encountering text, process appropriately. */ +static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer) +{ + if ((input_buffer == NULL) || (input_buffer->content == NULL)) + { + return false; /* no input */ + } + + /* parse the different types of values */ + /* null */ + if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0)) + { + item->type = cJSON_NULL; + input_buffer->offset += 4; + return true; + } + /* false */ + if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0)) + { + item->type = cJSON_False; + input_buffer->offset += 5; + return true; + } + /* true */ + if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0)) + { + item->type = cJSON_True; + item->valueint = 1; + input_buffer->offset += 4; + return true; + } + /* string */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"')) + { + return parse_string(item, input_buffer); + } + /* number */ + if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9')))) + { + return parse_number(item, input_buffer); + } + /* array */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '[')) + { + return parse_array(item, input_buffer); + } + /* object */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{')) + { + return parse_object(item, input_buffer); + } + + return false; +} + +/* Render a value to text. */ +static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output = NULL; + + if ((item == NULL) || (output_buffer == NULL)) + { + return false; + } + + switch ((item->type) & 0xFF) + { + case cJSON_NULL: + output = ensure(output_buffer, 5); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "null"); + return true; + + case cJSON_False: + output = ensure(output_buffer, 6); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "false"); + return true; + + case cJSON_True: + output = ensure(output_buffer, 5); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "true"); + return true; + + case cJSON_Number: + return print_number(item, output_buffer); + + case cJSON_Raw: + { + size_t raw_length = 0; + if (item->valuestring == NULL) + { + return false; + } + + raw_length = strlen(item->valuestring) + sizeof(""); + output = ensure(output_buffer, raw_length); + if (output == NULL) + { + return false; + } + memcpy(output, item->valuestring, raw_length); + return true; + } + + case cJSON_String: + return print_string(item, output_buffer); + + case cJSON_Array: + return print_array(item, output_buffer); + + case cJSON_Object: + return print_object(item, output_buffer); + + default: + return false; + } +} + +/* Build an array from input text. */ +static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer) +{ + cJSON *head = NULL; /* head of the linked list */ + cJSON *current_item = NULL; + + if (input_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* to deeply nested */ + } + input_buffer->depth++; + + if (buffer_at_offset(input_buffer)[0] != '[') + { + /* not an array */ + goto fail; + } + + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']')) + { + /* empty array */ + goto success; + } + + /* check if we skipped to the end of the buffer */ + if (cannot_access_at_index(input_buffer, 0)) + { + input_buffer->offset--; + goto fail; + } + + /* step back to character in front of the first element */ + input_buffer->offset--; + /* loop through the comma separated array elements */ + do + { + /* allocate next item */ + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); + if (new_item == NULL) + { + goto fail; /* allocation failure */ + } + + /* attach next item to list */ + if (head == NULL) + { + /* start the linked list */ + current_item = head = new_item; + } + else + { + /* add to the end and advance */ + current_item->next = new_item; + new_item->prev = current_item; + current_item = new_item; + } + + /* parse next value */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_value(current_item, input_buffer)) + { + goto fail; /* failed to parse value */ + } + buffer_skip_whitespace(input_buffer); + } + while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + + if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']') + { + goto fail; /* expected end of array */ + } + +success: + input_buffer->depth--; + + if (head != NULL) { + head->prev = current_item; + } + + item->type = cJSON_Array; + item->child = head; + + input_buffer->offset++; + + return true; + +fail: + if (head != NULL) + { + cJSON_Delete(head); + } + + return false; +} + +/* Render an array to text */ +static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + size_t length = 0; + cJSON *current_element = item->child; + + if (output_buffer == NULL) + { + return false; + } + + /* Compose the output array. */ + /* opening square bracket */ + output_pointer = ensure(output_buffer, 1); + if (output_pointer == NULL) + { + return false; + } + + *output_pointer = '['; + output_buffer->offset++; + output_buffer->depth++; + + while (current_element != NULL) + { + if (!print_value(current_element, output_buffer)) + { + return false; + } + update_offset(output_buffer); + if (current_element->next) + { + length = (size_t) (output_buffer->format ? 2 : 1); + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ','; + if(output_buffer->format) + { + *output_pointer++ = ' '; + } + *output_pointer = '\0'; + output_buffer->offset += length; + } + current_element = current_element->next; + } + + output_pointer = ensure(output_buffer, 2); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ']'; + *output_pointer = '\0'; + output_buffer->depth--; + + return true; +} + +/* Build an object from the text. */ +static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer) +{ + cJSON *head = NULL; /* linked list head */ + cJSON *current_item = NULL; + + if (input_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* to deeply nested */ + } + input_buffer->depth++; + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{')) + { + goto fail; /* not an object */ + } + + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}')) + { + goto success; /* empty object */ + } + + /* check if we skipped to the end of the buffer */ + if (cannot_access_at_index(input_buffer, 0)) + { + input_buffer->offset--; + goto fail; + } + + /* step back to character in front of the first element */ + input_buffer->offset--; + /* loop through the comma separated array elements */ + do + { + /* allocate next item */ + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); + if (new_item == NULL) + { + goto fail; /* allocation failure */ + } + + /* attach next item to list */ + if (head == NULL) + { + /* start the linked list */ + current_item = head = new_item; + } + else + { + /* add to the end and advance */ + current_item->next = new_item; + new_item->prev = current_item; + current_item = new_item; + } + + /* parse the name of the child */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_string(current_item, input_buffer)) + { + goto fail; /* failed to parse name */ + } + buffer_skip_whitespace(input_buffer); + + /* swap valuestring and string, because we parsed the name */ + current_item->string = current_item->valuestring; + current_item->valuestring = NULL; + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':')) + { + goto fail; /* invalid object */ + } + + /* parse the value */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_value(current_item, input_buffer)) + { + goto fail; /* failed to parse value */ + } + buffer_skip_whitespace(input_buffer); + } + while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}')) + { + goto fail; /* expected end of object */ + } + +success: + input_buffer->depth--; + + if (head != NULL) { + head->prev = current_item; + } + + item->type = cJSON_Object; + item->child = head; + + input_buffer->offset++; + return true; + +fail: + if (head != NULL) + { + cJSON_Delete(head); + } + + return false; +} + +/* Render an object to text. */ +static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + size_t length = 0; + cJSON *current_item = item->child; + + if (output_buffer == NULL) + { + return false; + } + + /* Compose the output: */ + length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */ + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + + *output_pointer++ = '{'; + output_buffer->depth++; + if (output_buffer->format) + { + *output_pointer++ = '\n'; + } + output_buffer->offset += length; + + while (current_item) + { + if (output_buffer->format) + { + size_t i; + output_pointer = ensure(output_buffer, output_buffer->depth); + if (output_pointer == NULL) + { + return false; + } + for (i = 0; i < output_buffer->depth; i++) + { + *output_pointer++ = '\t'; + } + output_buffer->offset += output_buffer->depth; + } + + /* print key */ + if (!print_string_ptr((unsigned char*)current_item->string, output_buffer)) + { + return false; + } + update_offset(output_buffer); + + length = (size_t) (output_buffer->format ? 2 : 1); + output_pointer = ensure(output_buffer, length); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ':'; + if (output_buffer->format) + { + *output_pointer++ = '\t'; + } + output_buffer->offset += length; + + /* print value */ + if (!print_value(current_item, output_buffer)) + { + return false; + } + update_offset(output_buffer); + + /* print comma if not last */ + length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0)); + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + if (current_item->next) + { + *output_pointer++ = ','; + } + + if (output_buffer->format) + { + *output_pointer++ = '\n'; + } + *output_pointer = '\0'; + output_buffer->offset += length; + + current_item = current_item->next; + } + + output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2); + if (output_pointer == NULL) + { + return false; + } + if (output_buffer->format) + { + size_t i; + for (i = 0; i < (output_buffer->depth - 1); i++) + { + *output_pointer++ = '\t'; + } + } + *output_pointer++ = '}'; + *output_pointer = '\0'; + output_buffer->depth--; + + return true; +} + +/* Get Array size/item / object item. */ +CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array) +{ + cJSON *child = NULL; + size_t size = 0; + + if (array == NULL) + { + return 0; + } + + child = array->child; + + while(child != NULL) + { + size++; + child = child->next; + } + + /* FIXME: Can overflow here. Cannot be fixed without breaking the API */ + + return (int)size; +} + +static cJSON* get_array_item(const cJSON *array, size_t index) +{ + cJSON *current_child = NULL; + + if (array == NULL) + { + return NULL; + } + + current_child = array->child; + while ((current_child != NULL) && (index > 0)) + { + index--; + current_child = current_child->next; + } + + return current_child; +} + +CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index) +{ + if (index < 0) + { + return NULL; + } + + return get_array_item(array, (size_t)index); +} + +static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive) +{ + cJSON *current_element = NULL; + + if ((object == NULL) || (name == NULL)) + { + return NULL; + } + + current_element = object->child; + if (case_sensitive) + { + while ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_element->string) != 0)) + { + current_element = current_element->next; + } + } + else + { + while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0)) + { + current_element = current_element->next; + } + } + + if ((current_element == NULL) || (current_element->string == NULL)) { + return NULL; + } + + return current_element; +} + +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string) +{ + return get_object_item(object, string, false); +} + +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string) +{ + return get_object_item(object, string, true); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string) +{ + return cJSON_GetObjectItem(object, string) ? 1 : 0; +} + +/* Utility for array list handling. */ +static void suffix_object(cJSON *prev, cJSON *item) +{ + prev->next = item; + item->prev = prev; +} + +/* Utility for handling references. */ +static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks) +{ + cJSON *reference = NULL; + if (item == NULL) + { + return NULL; + } + + reference = cJSON_New_Item(hooks); + if (reference == NULL) + { + return NULL; + } + + memcpy(reference, item, sizeof(cJSON)); + reference->string = NULL; + reference->type |= cJSON_IsReference; + reference->next = reference->prev = NULL; + return reference; +} + +static cJSON_bool add_item_to_array(cJSON *array, cJSON *item) +{ + cJSON *child = NULL; + + if ((item == NULL) || (array == NULL) || (array == item)) + { + return false; + } + + child = array->child; + /* + * To find the last item in array quickly, we use prev in array + */ + if (child == NULL) + { + /* list is empty, start new one */ + array->child = item; + item->prev = item; + item->next = NULL; + } + else + { + /* append to the end */ + if (child->prev) + { + suffix_object(child->prev, item); + array->child->prev = item; + } + else + { + while (child->next) + { + child = child->next; + } + suffix_object(child, item); + array->child->prev = item; + } + } + + return true; +} + +/* Add item to array/object. */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item) +{ + return add_item_to_array(array, item); +} + +#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) + #pragma GCC diagnostic push +#endif +#ifdef __GNUC__ +#pragma GCC diagnostic ignored "-Wcast-qual" +#endif +/* helper function to cast away const */ +static void* cast_away_const(const void* string) +{ + return (void*)string; +} +#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) + #pragma GCC diagnostic pop +#endif + + +static cJSON_bool add_item_to_object(cJSON * const object, const char * const string, cJSON * const item, const internal_hooks * const hooks, const cJSON_bool constant_key) +{ + char *new_key = NULL; + int new_type = cJSON_Invalid; + + if ((object == NULL) || (string == NULL) || (item == NULL) || (object == item)) + { + return false; + } + + if (constant_key) + { + new_key = (char*)cast_away_const(string); + new_type = item->type | cJSON_StringIsConst; + } + else + { + new_key = (char*)cJSON_strdup((const unsigned char*)string, hooks); + if (new_key == NULL) + { + return false; + } + + new_type = item->type & ~cJSON_StringIsConst; + } + + if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) + { + hooks->deallocate(item->string); + } + + item->string = new_key; + item->type = new_type; + + return add_item_to_array(object, item); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) +{ + return add_item_to_object(object, string, item, &global_hooks, false); +} + +/* Add an item to an object with constant string as key */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) +{ + return add_item_to_object(object, string, item, &global_hooks, true); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) +{ + if (array == NULL) + { + return false; + } + + return add_item_to_array(array, create_reference(item, &global_hooks)); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item) +{ + if ((object == NULL) || (string == NULL)) + { + return false; + } + + return add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false); +} + +CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name) +{ + cJSON *null = cJSON_CreateNull(); + if (add_item_to_object(object, name, null, &global_hooks, false)) + { + return null; + } + + cJSON_Delete(null); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name) +{ + cJSON *true_item = cJSON_CreateTrue(); + if (add_item_to_object(object, name, true_item, &global_hooks, false)) + { + return true_item; + } + + cJSON_Delete(true_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name) +{ + cJSON *false_item = cJSON_CreateFalse(); + if (add_item_to_object(object, name, false_item, &global_hooks, false)) + { + return false_item; + } + + cJSON_Delete(false_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean) +{ + cJSON *bool_item = cJSON_CreateBool(boolean); + if (add_item_to_object(object, name, bool_item, &global_hooks, false)) + { + return bool_item; + } + + cJSON_Delete(bool_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number) +{ + cJSON *number_item = cJSON_CreateNumber(number); + if (add_item_to_object(object, name, number_item, &global_hooks, false)) + { + return number_item; + } + + cJSON_Delete(number_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string) +{ + cJSON *string_item = cJSON_CreateString(string); + if (add_item_to_object(object, name, string_item, &global_hooks, false)) + { + return string_item; + } + + cJSON_Delete(string_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw) +{ + cJSON *raw_item = cJSON_CreateRaw(raw); + if (add_item_to_object(object, name, raw_item, &global_hooks, false)) + { + return raw_item; + } + + cJSON_Delete(raw_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name) +{ + cJSON *object_item = cJSON_CreateObject(); + if (add_item_to_object(object, name, object_item, &global_hooks, false)) + { + return object_item; + } + + cJSON_Delete(object_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name) +{ + cJSON *array = cJSON_CreateArray(); + if (add_item_to_object(object, name, array, &global_hooks, false)) + { + return array; + } + + cJSON_Delete(array); + return NULL; +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item) +{ + if ((parent == NULL) || (item == NULL)) + { + return NULL; + } + + if (item != parent->child) + { + /* not the first element */ + item->prev->next = item->next; + } + if (item->next != NULL) + { + /* not the last element */ + item->next->prev = item->prev; + } + + if (item == parent->child) + { + /* first element */ + parent->child = item->next; + } + else if (item->next == NULL) + { + /* last element */ + parent->child->prev = item->prev; + } + + /* make sure the detached item doesn't point anywhere anymore */ + item->prev = NULL; + item->next = NULL; + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which) +{ + if (which < 0) + { + return NULL; + } + + return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which)); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which) +{ + cJSON_Delete(cJSON_DetachItemFromArray(array, which)); +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string) +{ + cJSON *to_detach = cJSON_GetObjectItem(object, string); + + return cJSON_DetachItemViaPointer(object, to_detach); +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string) +{ + cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string); + + return cJSON_DetachItemViaPointer(object, to_detach); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string) +{ + cJSON_Delete(cJSON_DetachItemFromObject(object, string)); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string) +{ + cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string)); +} + +/* Replace array/object items with new ones. */ +CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem) +{ + cJSON *after_inserted = NULL; + + if (which < 0) + { + return false; + } + + after_inserted = get_array_item(array, (size_t)which); + if (after_inserted == NULL) + { + return add_item_to_array(array, newitem); + } + + newitem->next = after_inserted; + newitem->prev = after_inserted->prev; + after_inserted->prev = newitem; + if (after_inserted == array->child) + { + array->child = newitem; + } + else + { + newitem->prev->next = newitem; + } + return true; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement) +{ + if ((parent == NULL) || (replacement == NULL) || (item == NULL)) + { + return false; + } + + if (replacement == item) + { + return true; + } + + replacement->next = item->next; + replacement->prev = item->prev; + + if (replacement->next != NULL) + { + replacement->next->prev = replacement; + } + if (parent->child == item) + { + if (parent->child->prev == parent->child) + { + replacement->prev = replacement; + } + parent->child = replacement; + } + else + { /* + * To find the last item in array quickly, we use prev in array. + * We can't modify the last item's next pointer where this item was the parent's child + */ + if (replacement->prev != NULL) + { + replacement->prev->next = replacement; + } + if (replacement->next == NULL) + { + parent->child->prev = replacement; + } + } + + item->next = NULL; + item->prev = NULL; + cJSON_Delete(item); + + return true; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) +{ + if (which < 0) + { + return false; + } + + return cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem); +} + +static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive) +{ + if ((replacement == NULL) || (string == NULL)) + { + return false; + } + + /* replace the name in the replacement */ + if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL)) + { + cJSON_free(replacement->string); + } + replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); + replacement->type &= ~cJSON_StringIsConst; + + return cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem) +{ + return replace_item_in_object(object, string, newitem, false); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem) +{ + return replace_item_in_object(object, string, newitem, true); +} + +/* Create basic types: */ +CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_NULL; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_True; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_False; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = boolean ? cJSON_True : cJSON_False; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_Number; + item->valuedouble = num; + + /* use saturation in case of overflow */ + if (num >= INT_MAX) + { + item->valueint = INT_MAX; + } + else if (num <= (double)INT_MIN) + { + item->valueint = INT_MIN; + } + else + { + item->valueint = (int)num; + } + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_String; + item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); + if(!item->valuestring) + { + cJSON_Delete(item); + return NULL; + } + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) + { + item->type = cJSON_String | cJSON_IsReference; + item->valuestring = (char*)cast_away_const(string); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) { + item->type = cJSON_Object | cJSON_IsReference; + item->child = (cJSON*)cast_away_const(child); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) { + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) { + item->type = cJSON_Array | cJSON_IsReference; + item->child = (cJSON*)cast_away_const(child); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_Raw; + item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks); + if(!item->valuestring) + { + cJSON_Delete(item); + return NULL; + } + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type=cJSON_Array; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if (item) + { + item->type = cJSON_Object; + } + + return item; +} + +/* Create Arrays: */ +CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + for(i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber(numbers[i]); + if (!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for(i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber((double)numbers[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for(i = 0;a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber(numbers[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (strings == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for (i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateString(strings[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p,n); + } + p = n; + } + + return a; +} + +/* Duplication */ +CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse) +{ + cJSON *newitem = NULL; + cJSON *child = NULL; + cJSON *next = NULL; + cJSON *newchild = NULL; + + /* Bail on bad ptr */ + if (!item) + { + goto fail; + } + /* Create new item */ + newitem = cJSON_New_Item(&global_hooks); + if (!newitem) + { + goto fail; + } + /* Copy over all vars */ + newitem->type = item->type & (~cJSON_IsReference); + newitem->valueint = item->valueint; + newitem->valuedouble = item->valuedouble; + if (item->valuestring) + { + newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks); + if (!newitem->valuestring) + { + goto fail; + } + } + if (item->string) + { + newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks); + if (!newitem->string) + { + goto fail; + } + } + /* If non-recursive, then we're done! */ + if (!recurse) + { + return newitem; + } + /* Walk the ->next chain for the child. */ + child = item->child; + while (child != NULL) + { + newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */ + if (!newchild) + { + goto fail; + } + if (next != NULL) + { + /* If newitem->child already set, then crosswire ->prev and ->next and move on */ + next->next = newchild; + newchild->prev = next; + next = newchild; + } + else + { + /* Set newitem->child and move to it */ + newitem->child = newchild; + next = newchild; + } + child = child->next; + } + + return newitem; + +fail: + if (newitem != NULL) + { + cJSON_Delete(newitem); + } + + return NULL; +} + +static void skip_oneline_comment(char **input) +{ + *input += static_strlen("//"); + + for (; (*input)[0] != '\0'; ++(*input)) + { + if ((*input)[0] == '\n') { + *input += static_strlen("\n"); + return; + } + } +} + +static void skip_multiline_comment(char **input) +{ + *input += static_strlen("/*"); + + for (; (*input)[0] != '\0'; ++(*input)) + { + if (((*input)[0] == '*') && ((*input)[1] == '/')) + { + *input += static_strlen("*/"); + return; + } + } +} + +static void minify_string(char **input, char **output) { + (*output)[0] = (*input)[0]; + *input += static_strlen("\""); + *output += static_strlen("\""); + + + for (; (*input)[0] != '\0'; (void)++(*input), ++(*output)) { + (*output)[0] = (*input)[0]; + + if ((*input)[0] == '\"') { + (*output)[0] = '\"'; + *input += static_strlen("\""); + *output += static_strlen("\""); + return; + } else if (((*input)[0] == '\\') && ((*input)[1] == '\"')) { + (*output)[1] = (*input)[1]; + *input += static_strlen("\""); + *output += static_strlen("\""); + } + } +} + +CJSON_PUBLIC(void) cJSON_Minify(char *json) +{ + char *into = json; + + if (json == NULL) + { + return; + } + + while (json[0] != '\0') + { + switch (json[0]) + { + case ' ': + case '\t': + case '\r': + case '\n': + json++; + break; + + case '/': + if (json[1] == '/') + { + skip_oneline_comment(&json); + } + else if (json[1] == '*') + { + skip_multiline_comment(&json); + } else { + json++; + } + break; + + case '\"': + minify_string(&json, (char**)&into); + break; + + default: + into[0] = json[0]; + json++; + into++; + } + } + + /* and null-terminate. */ + *into = '\0'; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Invalid; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_False; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xff) == cJSON_True; +} + + +CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & (cJSON_True | cJSON_False)) != 0; +} +CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_NULL; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Number; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_String; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Array; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Object; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Raw; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive) +{ + if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)) || cJSON_IsInvalid(a)) + { + return false; + } + + /* check if type is valid */ + switch (a->type & 0xFF) + { + case cJSON_False: + case cJSON_True: + case cJSON_NULL: + case cJSON_Number: + case cJSON_String: + case cJSON_Raw: + case cJSON_Array: + case cJSON_Object: + break; + + default: + return false; + } + + /* identical objects are equal */ + if (a == b) + { + return true; + } + + switch (a->type & 0xFF) + { + /* in these cases and equal type is enough */ + case cJSON_False: + case cJSON_True: + case cJSON_NULL: + return true; + + case cJSON_Number: + if (compare_double(a->valuedouble, b->valuedouble)) + { + return true; + } + return false; + + case cJSON_String: + case cJSON_Raw: + if ((a->valuestring == NULL) || (b->valuestring == NULL)) + { + return false; + } + if (strcmp(a->valuestring, b->valuestring) == 0) + { + return true; + } + + return false; + + case cJSON_Array: + { + cJSON *a_element = a->child; + cJSON *b_element = b->child; + + for (; (a_element != NULL) && (b_element != NULL);) + { + if (!cJSON_Compare(a_element, b_element, case_sensitive)) + { + return false; + } + + a_element = a_element->next; + b_element = b_element->next; + } + + /* one of the arrays is longer than the other */ + if (a_element != b_element) { + return false; + } + + return true; + } + + case cJSON_Object: + { + cJSON *a_element = NULL; + cJSON *b_element = NULL; + cJSON_ArrayForEach(a_element, a) + { + /* TODO This has O(n^2) runtime, which is horrible! */ + b_element = get_object_item(b, a_element->string, case_sensitive); + if (b_element == NULL) + { + return false; + } + + if (!cJSON_Compare(a_element, b_element, case_sensitive)) + { + return false; + } + } + + /* doing this twice, once on a and b to prevent true comparison if a subset of b + * TODO: Do this the proper way, this is just a fix for now */ + cJSON_ArrayForEach(b_element, b) + { + a_element = get_object_item(a, b_element->string, case_sensitive); + if (a_element == NULL) + { + return false; + } + + if (!cJSON_Compare(b_element, a_element, case_sensitive)) + { + return false; + } + } + + return true; + } + + default: + return false; + } +} + +CJSON_PUBLIC(void *) cJSON_malloc(size_t size) +{ + return global_hooks.allocate(size); +} + +CJSON_PUBLIC(void) cJSON_free(void *object) +{ + global_hooks.deallocate(object); +} diff --git a/app/src/main/cpp/cJSON.h b/app/src/main/cpp/cJSON.h new file mode 100644 index 00000000..b5ceb290 --- /dev/null +++ b/app/src/main/cpp/cJSON.h @@ -0,0 +1,293 @@ +/* + 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 13 + +#include + +/* 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); +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length); +/* 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); +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, 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 *item); + +/* 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 item type and return its value */ +CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item); +CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const 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/array 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. + * The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/ +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 *const *strings, int count); + +/* Append item to the specified array/object. */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item); +CJSON_PUBLIC(cJSON_bool) 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(cJSON_bool) 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(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item); + +/* Remove/Detach 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(cJSON_bool) 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(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem); +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); +CJSON_PUBLIC(cJSON_bool) 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); + +/* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings. + * The input pointer json cannot point to a read-only address area, such as a string constant, + * but should point to a readable and writable adress area. */ +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)) +/* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */ +CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring); + +/* 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 diff --git a/app/src/main/cpp/mqtt_south.c b/app/src/main/cpp/mqtt_south.c new file mode 100644 index 00000000..229bf3e7 --- /dev/null +++ b/app/src/main/cpp/mqtt_south.c @@ -0,0 +1,4553 @@ +#include "mqtt_south.h" +#include "protocol_sensor.h" +#include "protocol_common.h" +#include "protocol_south.h" +#include +#include +#include +#include "pic_mgr.h" +#include "data_smp_mgr.h" +#include "data_send_mgr.h" +#include +#include "cJSON.h" +#include "base64.h" +#include "rthw.h" +#include "net_client.h" +#include "tiny_base64.h" +#include +#include +#include + +//#define LOG_TAG "SOUTH" //该模块对应的标签。不定义时,默认:NO_TAG +//#define LOG_LVL LOG_LVL_DBG //该模块对应的日志输出级别。不定义时,默认:调试级别 + +#include + +#define INVALID_VALUE 0xFFFF + +extern SOUTH_INS_TYPE mqtt_first_pull[32]; +extern SOUTH_WTH_TYPE mqtt_first_wth; +extern SOUTH_STAT_INFO south_stat_info; +extern uint32_t mqtt_connected; + +extern int ec20_mqtt_data_send(char *databuf, int topid); +extern int32_t south_set_time(time_t t); +extern int32_t save_para(void); +extern int south_resp_process(uint8_t *buf,uint32_t len); +extern int ec20_http_data_download(char *http_url, char *header_url, char *host_header); +extern void mqtt_reconnect(void); +extern int32_t pic_upload_start_process(uint8_t *buf,uint32_t len); +extern int32_t get_rssi(void); +extern void hostip_to_char(char *hostip); +extern int ec20_Get_Gps_data(void); +extern void south_sort_work_time(WORK_TIME_TYPE *work_time,uint32_t num_of_item); +extern int ec20_http_post_file(int filetype, int filelen); + +char phototime[32]; +int packagenum=0; +unsigned int lastmid =0; +extern char picfile[64]; +//extern char *host_ip; +extern DEV_PARA_TYPE dev_para; +extern double gw_longitude; +extern double gw_latitude; + + +extern int getlocalip(char *localip); +char httpip[] = "192.168.11.18"; +int httpport = 443; +char mqttip[] = "192.168.11.18"; + +char *http_url=RT_NULL; +char *requestheader_url=RT_NULL, *host_requestheader=RT_NULL; + +int ihttp_download = 0; +int iupload_file =0; +int iupload_pic = 0; +int weatherlevel = 0; +MQTT_UP_PIC mqtt_up_pic; +MQTT_SEND_BUF mqtt_send_buf; +UPGRADE_DEVICES upgrade_devices; +/* 声明一个互斥锁 */ +static struct rt_mutex send_data_lock; + +#define LOG_FILE_NAME "s938.log" +#define LOG_FILE_LEN 5116 +#define MAX_FILE_PKT 256 //一包的长度 + +double format_two_decimal_digits(float num){ + + float rounded_num = round(num * 100); // 先扩大100倍以便更容易处理小数点后两位 + int int_part = (int)rounded_num; // 取整得到整数部分 + //float frac_part = rounded_num - int_part; // 计算小数部分 + //return ((frac_part >= 0.5) ? (int_part + 1) : int_part) / 100.0; + return (double)(int_part / 100.0); +} + +#if 0 +int mqtt_file_read_data(MQTT_FILE_INFO_TYPE* mqtt_file_info) +{ + int ret = -1; + int fd, size, i; + char file_data[512]; + int file_len; +#if 1 + if(mqtt_file_info->size == 0){ + LOG_E("send file failed:file not exist %s\n", mqtt_file_info->file); + return ret; + } + LOG_I("start upload file %s size=%d\n",mqtt_file_info->file,mqtt_file_info->size); +#endif +#if 0 +//test file start + FILE *fp; // 文件指针 + char data[] = "[31m09-01 09:50:07 E/at.clnt: execute command (AT+QMTSUB=0,1,,1) failed![31m09-01 09:50:07 E/at.clnt: execute command (AT+QMTSUB=0,1,,1) failed![31m09-01 09:50:07 E/at.clnt: execute command (AT+QMTSUB=0,1,,1) failed!"; // 要写入的数据 + const size_t len = strlen(data); // 数据长度 + + // 打开文件(如果不存在则创建) + LOG_D("zks file name:%s", mqtt_file_info->file); + fp = fopen(mqtt_file_info->file, "w"); // "w"表示写模式,如果文件不存在则新建 + if (fp == NULL) { + LOG_D("Error opening file.\n"); + return 1; + } + + // 写入数据 + if (fwrite(data, sizeof(char), len, fp) != len) { + LOG_D("Error writing to file.\n"); + fclose(fp); + return 1; + } + + // 关闭文件 + fclose(fp); + + LOG_D("Data written successfully.\n"); +//test file end +#endif + + fd = open(mqtt_file_info->file,O_RDONLY); + if(fd < 0){ + LOG_E("open file %s failed\n",mqtt_file_info->file); + return -1; + } + + mqtt_file_info->curr_idx = 0; + mqtt_file_info->data_len = 0; + + while(file_len = read(fd, file_data, MAX_FILE_PKT) > 0) + { + // memset(file_data, 0, sizeof(file_data)); + base64_encode((const char*)file_data, file_len, (char*)mqtt_file_info->data, &mqtt_file_info->data_len); + // for(int i = 0; i< mqtt_file_info->data_len; i++) + // { + // LOG_D("zks_1 total %d, index %d, data:%c", mqtt_file_info->total_pkt, mqtt_file_info->curr_idx, mqtt_file_info->data[i]); + // } + + mqtt_dev_upload_file_data(mqtt_file_info); + mqtt_file_info->curr_idx++; + if(mqtt_file_info->curr_idx >15) + break; + + + //memset(file_data, 0, sizeof(file_data)); + //base64_decode((const char*)mqtt_file_info->data, &mqtt_file_info->data_len, file_data, file_len); + //LOG_D("zks_2 total %d, index %d, data:%s", mqtt_file_info->total_pkt, mqtt_file_info->curr_idx, file_data); + } + + // while(mqtt_file_info->data_len = read(fd, mqtt_file_info->data, MAX_FILE_PKT) > 0) + // { + // LOG_D("zks total %d, index %d, data:%s", mqtt_file_info->total_pkt, mqtt_file_info->curr_idx, mqtt_file_info->data); + // mqtt_dev_upload_file_data(outdata, mqtt_file_info); + // mqtt_file_info->curr_idx++; + // } + close(fd); + if(mqtt_file_info->curr_idx == mqtt_file_info->total_pkt){ + LOG_D("send file %s success.\n",mqtt_file_info->file); + return 0; + } + + LOG_E("occur some errors when send file %s\n",mqtt_file_info->file); + + return -1; +} + +int mqtt_dev_upload_file_process(MQTT_FILE_INFO_TYPE* mqtt_file_info) +{ + int ret = -1; + + for(int i=0;i<5;i++) + { + LOG_I("send start upload picture request\n"); + if((ret=mqtt_dev_upload_file_info())==0) + break; + } + + //send data + if(mqtt_file_read_data(mqtt_file_info) != 0) + { + return ret; + } + + for(int i=0;i<5;i++) + { + LOG_I("send start upload file finish\n"); + if((ret=mqtt_dev_upload_file_finish(mqtt_file_info))==0) + break; + } + + return ret; +} +#endif + +int mqtt_dev_upload_file_info(void) +{ + int total_size, packageCount=0; + char tempbuf[64], filename[64];// = LOG_FILE_NAME; + + cJSON *outdata = cJSON_CreateObject(); + + memset(filename, 0, sizeof(filename)); + sprintf(filename, "/nand/s938.log"); + //mqtt_file_info->type = 2; // 2 -> 设备日志文件 + //mqtt_file_info->mode = 0; //0->mqtt; 1->file //zks todo + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + cJSON *req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "deviceUploadFileInformation", req_up_pic); + //设备上传文件信息 + cJSON_AddNumberToObject(req_up_pic, "fileType" ,2); + cJSON_AddStringToObject(req_up_pic, "fileName", LOG_FILE_NAME); + total_size = south_stat_file(filename); + LOG_D("total_size=%d filename:%s", total_size, filename); + cJSON_AddNumberToObject(req_up_pic, "fileSize", total_size); + cJSON_AddNumberToObject(req_up_pic, "fileUploadMode", 1); + + memset(tempbuf, 0, sizeof(tempbuf)); + getsystime(tempbuf); + cJSON_AddStringToObject(req_up_pic, "time", tempbuf); + packageCount = total_size/SOUTH_MAX_PIC_PKT_SIZE; + if(total_size%SOUTH_MAX_PIC_PKT_SIZE == 0) + ; + else + packageCount++; + LOG_D("packageCount=%d", packageCount); + cJSON_AddNumberToObject(req_up_pic, "packageCount", packageCount); + + cJSON_AddStringToObject(serviceData, "eventTime", tempbuf); + cJSON_AddStringToObject(serviceData, "serviceId", "GeneralDataMessageUpload"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + LOG_D("sendlen= %d", strlen(sendbuf)); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + mqtt_south_send_data(); + rt_thread_delay(1000); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + //return 0; +#if 1 + int size,outlen, pkt_idx;//fd, + //uint8_t buffer[512]="2024-7-12 8:53:12-18 装置1 打开串口4 /dev/ttysWK3成功!fd=772024-7-12 8:53:12-19 装置2 串口4 /dev/ttysWK3已经打开!fd=772024-7-12 8:53:15-72 发送串口4 装置2命令: 68 00 00 68 01 09 0A 162024-7-12 8:53:15-73 sendtimeconst= 306asdfgh"; + uint8_t buffer[512]; + char *picdata; + + // fd = open(filename,O_RDONLY);//打开图片文件 + // if(fd<0) + // { + // LOG_E("open picture file %s failed\n",filename); + // return -1; + // } + picdata = (char*) malloc(1024); + //return 0; + pkt_idx=0;//包号 + //while((size=read(fd,buffer,SOUTH_MAX_PIC_PKT_SIZE))>0)//读取pkt_len长度到buffer + //for(pkt_idx=0; pkt_idx < 23; pkt_idx++) + { + LOG_D("size=%d", size); + //continue; + outdata = cJSON_CreateObject(); + devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "deviceUploadFileData", req_up_pic); + /*请求上传图片:拍照时间(月、日、时、分、秒),通道号 预置位号 包数高位 包数低位*/ + /*请求上传图片:type UploadMode time channelNumber presetPosition packageNumber fileFolder*/ + cJSON_AddStringToObject(req_up_pic, "time", tempbuf); + //cJSON_AddNumberToObject(req_up_pic, "packageNumber", pkt_idx); + cJSON_AddStringToObject(req_up_pic, "fileName", LOG_FILE_NAME); + cJSON_AddStringToObject(req_up_pic, "fileFolder", DEVICEID); + cJSON_AddNumberToObject(req_up_pic, "fileCheckMode", 1); + cJSON_AddStringToObject(req_up_pic, "fileCheckValue", "1122"); + //cJSON_AddNumberToObject(req_up_pic, "fileCodeMethod", 1); + + //memset(picdata, 0, 1024); + //tiny_base64_encode((unsigned char*)picdata, &outlen, (unsigned char*)buffer, size); + //base64_encode((const char*)buffer, size, (char*)picdata, &outlen); + //LOG_D("picdata_len=%d, outlen=%d", strlen(picdata), outlen); + //cJSON_AddStringToObject(req_up_pic, "filedata", picdata); + //cJSON_AddNumberToObject(req_up_pic, "dataLength", outlen); + //cJSON_AddNumberToObject(req_up_pic, "dataLength", strlen(buffer)); + //cJSON_AddStringToObject(req_up_pic, "data", buffer); + + /* data区域结束*/ + cJSON_AddStringToObject(serviceData, "eventTime", tempbuf); + cJSON_AddStringToObject(serviceData, "serviceId", "GeneralDataMessageUpload"); + + sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + mqtt_south_send_data(); + //rt_thread_delay(200); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + + pkt_idx++;//子包号递增 + } + //close(fd); + ec20_http_post_file(1, total_size); + if(NULL != picdata) + { + free(picdata); + picdata = NULL; + } + + outdata = cJSON_CreateObject(); + + devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "deviceUploadFileDataFinish", req_up_pic); + /*上传图片结束:通道号 预置位号*/ + + cJSON_AddNumberToObject(req_up_pic, "fileType", 2); + cJSON_AddNumberToObject(req_up_pic, "packageCount", packageCount); + cJSON_AddStringToObject(req_up_pic, "fileName", LOG_FILE_NAME); + cJSON_AddStringToObject(req_up_pic, "time", tempbuf); + + /* data区域结束*/ + cJSON_AddStringToObject(serviceData, "eventTime", tempbuf); + cJSON_AddStringToObject(serviceData, "serviceId", "GeneralDataMessageUpload"); + + sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + mqtt_south_send_data(); + //rt_thread_delay(200); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } +#endif + return 0; +} + +#if 0 +int mqtt_dev_upload_file_data(MQTT_FILE_INFO_TYPE* mqtt_file_info) +{ + char tempbuf[64]; + + cJSON *outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON *deviceData = cJSON_CreateObject(); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON *serviceData = cJSON_CreateObject(); + cJSON *dev_upload_file_info = cJSON_CreateObject(); + + cJSON *fileInfo = cJSON_CreateObject(); + memset(tempbuf, 0, sizeof(tempbuf)); + getsystime(tempbuf); + cJSON_AddStringToObject(fileInfo, "time", tempbuf); + cJSON_AddNumberToObject(fileInfo, "packageNumber" , mqtt_file_info->curr_idx); + cJSON_AddStringToObject(fileInfo, "fileName", mqtt_file_info->file); + if(mqtt_file_info->mode == 1){ + cJSON_AddStringToObject(fileInfo, "fileFolder", ""); + cJSON_AddNumberToObject(fileInfo, "fileCheckMode", 0); + cJSON_AddStringToObject(fileInfo, "fileCheckValue", ""); + } + cJSON_AddNumberToObject(fileInfo, "fileCodeMethod", 0); + cJSON_AddStringToObject(fileInfo, "fileData", mqtt_file_info->data); + cJSON_AddNumberToObject(fileInfo, "datalength", mqtt_file_info->data_len); + + cJSON_AddItemToObject(dev_upload_file_info,"deviceUploadFileData",fileInfo); + cJSON_AddItemToObject(serviceData,"data",dev_upload_file_info); + cJSON_AddStringToObject(serviceData, "eventTime", tempbuf); + cJSON_AddStringToObject(serviceData, "serviceId", "fileInfo"); + + cJSON_AddItemToArray(services,serviceData); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddItemToObject(outdata, "devices", devices); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + + // int count = strlen(sendbuf)/80; + // int idx = 0; + // for(int i = 0; i <= count; i++) + // { + // idx = 100*i; + // LOG_D("zks data:%s\n", &sendbuf[idx]); + // } + + //ec20_mqtt_data_send(sendbuf, 0); + rt_thread_delay(100); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + + return 0; +} + +int mqtt_dev_upload_file_finish(MQTT_FILE_INFO_TYPE* mqtt_file_info) +{ + char tempbuf[64]; + + cJSON *outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON *deviceData = cJSON_CreateObject(); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON *serviceData = cJSON_CreateObject(); + cJSON *dev_upload_file_info = cJSON_CreateObject(); + + cJSON *fileInfo = cJSON_CreateObject(); + memset(tempbuf, 0, sizeof(tempbuf)); + getsystime(tempbuf); + cJSON_AddStringToObject(fileInfo, "time", tempbuf); + cJSON_AddNumberToObject(fileInfo, "packageCount", mqtt_file_info->total_pkt); + cJSON_AddNumberToObject(fileInfo, "fileType", mqtt_file_info->type); + cJSON_AddStringToObject(fileInfo, "fileName", mqtt_file_info->file); + + cJSON_AddItemToObject(dev_upload_file_info,"deviceUploadFileDataFinish",fileInfo); + cJSON_AddItemToObject(serviceData,"data",dev_upload_file_info); + cJSON_AddStringToObject(serviceData, "eventTime", tempbuf); + cJSON_AddStringToObject(serviceData, "serviceId", "fileInfo"); + + cJSON_AddItemToArray(services,serviceData); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddItemToObject(outdata, "devices", devices); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + + // int count = strlen(sendbuf)/80; + // int idx = 0; + // for(int i = 0; i <= count; i++) + // { + // idx = 100*i; + // LOG_D("zks data:%s\n", &sendbuf[idx]); + // } + + //ec20_mqtt_data_send(sendbuf, 0); + rt_thread_delay(100); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + + return 0; +} +#endif + +int mqtt_base_properties(void) +{ + char tempbuf[64]; + uint8_t dev_name[] = {0xE6,0x9E,0xB6,0xE7,0xA9,0xBA,0xE8,0xBE,0x93,0xE7,0x94,0xB5,0xE7,0xBA,0xBF,0xE8,0xB7,0xAF,0xE5,0xBE,0xAE,0xE6,0xB0,0x94,0xE8,0xB1,0xA1,0xE7,0x9B,0x91,0xE6,0xB5,0x8B,0xE8,0xA3,0x85,0xE7,0xBD,0xAE}; + + cJSON *outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON *deviceData = cJSON_CreateObject(); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON *serviceData = cJSON_CreateObject(); + cJSON *BaseProperties = cJSON_CreateObject(); + /*输电线路覆冰监测装置基本信息*/ + cJSON_AddStringToObject(BaseProperties, "equipmentIdentity", (char*)dev_para.my_dev_info.Identifier); //设备身份编码(SN码) + //cJSON_AddStringToObject(BaseProperties, "physicalCode", "null"); //实物编码(物质编码) + + //char dev_name[64]; + uint8_t deviceType = 4; + //memset(dev_name, 0, sizeof(dev_name)); + if (strstr(dev_para.my_dev_info.Model, "MSRDT-A")) + { + deviceType = 4; + cJSON_AddStringToObject(BaseProperties, "equipmentName", "架空输电线路覆冰监测装置"); //设备类型 + //sprintf(dev_name, "架空输电线路覆冰监测装置"); + }else //if(strstr(dev_para.my_dev_info.Model, "MSRDT")) + { + deviceType = 3; + cJSON_AddStringToObject(BaseProperties, "equipmentName", (char*)dev_name); //设备类型 + //cJSON_AddStringToObject(BaseProperties, "equipmentName", "架空输电线路微气象监测装置"); //设备类型 + //sprintf(dev_name, "架空输电线路微气象监测装置"); + } + // else + // { + // deviceType = 4; + // //sprintf(dev_name, "架空输电线路覆冰监测装置"); + // cJSON_AddStringToObject(BaseProperties, "equipmentName", "架空输电线路覆冰监测装置"); //设备类型 + // } + //cJSON_AddStringToObject(BaseProperties, "equipmentName", dev_name); //设备类型 + cJSON_AddNumberToObject(BaseProperties, "deviceType", deviceType); //设备类型 + cJSON_AddStringToObject(BaseProperties, "manufacture", "上海欣影电力科技股份有限公司"); //生产厂家 + if(0 == upgrade_devices.uconfig) + cJSON_AddStringToObject(BaseProperties, "supplier", "上海欣影电力科技股份有限公司"); //生产厂家 + else + cJSON_AddStringToObject(BaseProperties, "supplier", "南网送检2024"); //生产厂家 + + //cJSON_AddStringToObject(BaseProperties, "supplier", "ShanghaiXinYing"); //供应商(代理商) + cJSON_AddStringToObject(BaseProperties, "equipmentType", dev_para.my_dev_info.Model); //设备型号 + cJSON_AddStringToObject(BaseProperties, "manufNo", (char*)dev_para.my_dev_info.Identifier); //出厂编号 + + char data[9]; + struct tm *tp; + uint32_t t; + rt_memset(data, 0, sizeof(data)); + rt_memcpy(&t, (char *)&dev_para.my_dev_info.DateOfProduction, sizeof(t)); + if ((tp = localtime((time_t *)&t)) != RT_NULL) + { + sprintf(data, "%04d%02d%02d",tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday); + }else{ + rt_strncpy(data, "20190406", 8); + } + cJSON_AddStringToObject(BaseProperties, "manufDate", data); //出厂年月 + + cJSON_AddNumberToObject(BaseProperties, "communicationMode", 1); //通信方式 + cJSON_AddNumberToObject(BaseProperties, "communicationProtocol", 1); //通信协议 + cJSON_AddItemToObject(serviceData,"data",BaseProperties); + memset(tempbuf, 0, sizeof(tempbuf)); + getsystime(tempbuf); + cJSON_AddStringToObject(serviceData, "eventTime", tempbuf); + cJSON_AddStringToObject(serviceData, "serviceId", "baseProperties"); + + cJSON_AddItemToArray(services,serviceData); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddItemToObject(outdata, "devices", devices); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + + return 0; +} + +int mqtt_communicationtechnical_properties(void) +{ + char tempbuf[64]; + + cJSON *outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON *deviceData = cJSON_CreateObject(); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON *serviceData = cJSON_CreateObject(); + cJSON *CommProperties = cJSON_CreateObject(); + /*输电线路覆冰监测装置技术参数*/ + /*通用通信技术参数*/ + //cJSON_AddStringToObject(CommProperties, "MasterDomainName", host_ip); //平台IP或域名 + //cJSON_AddNumberToObject(CommProperties, "MasterPort", dev_para.server.host_port); //平台端口 + //cJSON_AddStringToObject(CommProperties, "ClientId", DEVICEID); //客户端ID + //cJSON_AddStringToObject(CommProperties, "UserName", "null"); //连接用户 + //cJSON_AddStringToObject(CommProperties, "Password", "null"); //连接密码 + //cJSON_AddStringToObject(CommProperties, "DeviceId", "null"); //设备ID + cJSON_AddNumberToObject(CommProperties, "MQTTVer", 0); //物模型版本 + cJSON_AddNumberToObject(CommProperties, "HeartbeatInterval", dev_para.keep_alive_intv[0]); //心跳间隔 + //cJSON_AddStringToObject(CommProperties, "NationalstandardCode", "null"); //GB28181国标-编码 + cJSON_AddStringToObject(CommProperties, "NcommMode", "无线"); //北向通信方式 + cJSON_AddStringToObject(CommProperties, "NcommProtocol", "MQTT"); //北向通信协议 + cJSON_AddStringToObject(CommProperties, "moduleSoftwareVersion", APP_VERSION); //模组软件版本号 + cJSON_AddStringToObject(CommProperties, "SIMNum", dev_para.phone_num); //手机卡号 + cJSON_AddItemToObject(serviceData,"data",CommProperties); + memset(tempbuf, 0, sizeof(tempbuf)); + getsystime(tempbuf); + cJSON_AddStringToObject(serviceData, "eventTime", tempbuf); + cJSON_AddStringToObject(serviceData, "serviceId", "CommonCommunicationtechnicalProperties"); + + cJSON_AddItemToArray(services,serviceData); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddItemToObject(outdata, "devices", devices); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + + return 0; +} + +int mqtt_gatewaytechnical_properties(void) +{ + char tempbuf[64]; + + cJSON *outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON *deviceData = cJSON_CreateObject(); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON *serviceData = cJSON_CreateObject(); + cJSON *GatewayProperties = cJSON_CreateObject(); + /*输电线路覆冰监测装置技术参数*/ + /*通用网关技术参数*/ + //cJSON_AddNumberToObject(GatewayProperties, "BatteryType", 2); //电池类型 + //cJSON_AddNumberToObject(GatewayProperties, "SolarWattage", 120); //太阳能板瓦数 + cJSON_AddStringToObject(GatewayProperties, "DeviceVoltage", "DC12"); //设备工作电压 + //cJSON_AddNumberToObject(GatewayProperties, "DevicePower", 0.8); //设备功率 + //cJSON_AddNumberToObject(GatewayProperties, "BatteryAH", 100); //设备电池容量 + if(strstr(dev_para.my_dev_info.Model,"MSRDT-A") != RT_NULL) + cJSON_AddNumberToObject(GatewayProperties, "SensorNum", 4); //南向传感器数量 + else + cJSON_AddNumberToObject(GatewayProperties, "SensorNum", 1); //南向传感器数量 + + // 传感器参数JSON数组 + cJSON* SensorParamArray = cJSON_CreateArray(); + cJSON_AddItemToObject(GatewayProperties, "SensorParamArray", SensorParamArray); //传感器参数数组 + if(strstr(dev_para.my_dev_info.Model,"MSRDT-A") != RT_NULL) + { + cJSON* SensorNo2 = cJSON_CreateObject(); + cJSON_AddItemToArray(SensorParamArray,SensorNo2); + cJSON_AddNumberToObject(SensorNo2, "SensorID", 2); //南向传感器_ID + cJSON_AddNumberToObject(SensorNo2, "SensorType", 1); //南向传感器_类型 + + cJSON* SensorNo3 = cJSON_CreateObject(); + cJSON_AddItemToArray(SensorParamArray,SensorNo3); + cJSON_AddNumberToObject(SensorNo3, "SensorID", 3); //南向传感器_ID + cJSON_AddNumberToObject(SensorNo3, "SensorType", 0); //南向传感器_类型 + + cJSON* SensorNo = cJSON_CreateObject(); + cJSON_AddItemToArray(SensorParamArray,SensorNo); + cJSON_AddNumberToObject(SensorNo, "SensorID", 4); //南向传感器_ID + cJSON_AddNumberToObject(SensorNo, "SensorType", 3); //南向传感器_类型 + } + cJSON *SensorNo1= cJSON_CreateObject(); + cJSON_AddItemToArray(SensorParamArray,SensorNo1); + cJSON_AddNumberToObject(SensorNo1, "SensorID", 1); //南向传感器_ID + cJSON_AddNumberToObject(SensorNo1, "SensorType", 2); //南向传感器_类型 + + //cJSON_AddNumberToObject(GatewayProperties, "+SensorComm", 1); //南向传感器_通信方式 + //cJSON_AddNumberToObject(GatewayProperties, "+SensorProtocol", 0); //南向传感器_通信协议 + //cJSON_AddNumberToObject(GatewayProperties, "+SensorPower", 1); //南向传感器_供电方式 + //cJSON_AddNumberToObject(GatewayProperties, "+installLocation", 1); //南向传感器_安装位置 + cJSON_AddItemToObject(serviceData,"data",GatewayProperties); + memset(tempbuf, 0, sizeof(tempbuf)); + getsystime(tempbuf); + cJSON_AddStringToObject(serviceData, "eventTime", tempbuf); + cJSON_AddStringToObject(serviceData, "serviceId", "CommonGatewaytechnicalProperties"); + + cJSON_AddItemToArray(services,serviceData); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddItemToObject(outdata, "devices", devices); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + + return 0; +} + +int mqtt_devicetechnical_properties(void) +{ + char tempbuf[64]; + + cJSON *outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON *deviceData = cJSON_CreateObject(); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON *serviceData = cJSON_CreateObject(); + cJSON *CommProperties = cJSON_CreateObject(); + /*输电线路覆冰监测装置技术参数*/ + /*通用通信技术参数*/ + //cJSON_AddStringToObject(CommProperties, "MasterDomainName", host_ip); //平台IP或域名 + cJSON_AddNumberToObject(CommProperties, "SolarWattage", 120); //平台端口 + //cJSON_AddStringToObject(CommProperties, "ClientId", DEVICEID); //客户端ID + //cJSON_AddStringToObject(CommProperties, "UserName", "null"); //连接用户 + //cJSON_AddStringToObject(CommProperties, "Password", "null"); //连接密码 + //cJSON_AddStringToObject(CommProperties, "DeviceId", "null"); //设备ID + cJSON_AddNumberToObject(CommProperties, "BatteryType", 2); //物模型版本 + cJSON_AddNumberToObject(CommProperties, "PowerMode", 0); //心跳间隔 + //cJSON_AddStringToObject(CommProperties, "SolarWattage", "null"); //GB28181国标-编码 + cJSON_AddStringToObject(CommProperties, "DeviceVoltage", "DC12"); //北向通信方式 + cJSON_AddNumberToObject(CommProperties, "BatteryAH", 96); //北向通信协议 + cJSON_AddNumberToObject(CommProperties, "DevicePower", 1); //模组软件版本号 + cJSON_AddItemToObject(serviceData,"data",CommProperties); + memset(tempbuf, 0, sizeof(tempbuf)); + getsystime(tempbuf); + cJSON_AddStringToObject(serviceData, "eventTime", tempbuf); + cJSON_AddStringToObject(serviceData, "serviceId", "CommonDevicetechnicalProperties"); + + cJSON_AddItemToArray(services,serviceData); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddItemToObject(outdata, "devices", devices); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + + return 0; +} + +/* +root = cJSON Create0bject(); +// root 添加键对 person:json数组A +cISON AddItemTo0bject(root,"person", body = cIsON _CreateArray());// json数组A 添加Json对象B +cISON AddItemToArray(body,list = cJSoN Create0bject()); +// 在json对象B中添加键值对:"name":"danxia” +cISON_AddstringToobject(list,"name","danxia”);// 在ison对象B中添加键值对:"age”:18cISON AddNumberToobject(list,"age",18); +*/ +int powerOnContactMessage_Json(void) +{ + char tempbuf[64]; + + cJSON *outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + cJSON *powerOnContact = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "powerOnContactMessage", powerOnContact); + /*开机联络*/ + cJSON_AddStringToObject(powerOnContact, "standardVersionNumber", "3.0"); + memset(tempbuf, 0, sizeof(tempbuf)); + getsystime(tempbuf); + cJSON_AddStringToObject(serviceData, "eventTime", tempbuf); + cJSON_AddStringToObject(serviceData, "serviceId", "GeneralDataMessageUpload"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + return 0; +} + +#if 1 +int getsystime(char * timebuf) +{ + uint8_t buf[6]; + //struct tm* ti; + uint32_t t = time(NULL); + + if(NULL == timebuf) + return -1; + south_encode_timestamp(buf,t); + //rt_enter_critical(); + //ti = localtime((time_t *)&t); + //memset(timebuf, 0, sizeof(timebuf)); + //if(ti!=NULL) + //{ + // return -1; + //} + //sprintf(timebuf, "%d%02d%02dT%02d%02d%02dZ", ti->tm_year-100, ti->tm_mon+1, ti->tm_mday, ti->tm_hour, ti->tm_min, ti->tm_sec); + sprintf(timebuf, "%d%02d%02dT%02d%02d%02dZ", buf[0]+2000, buf[1], buf[2], buf[3], buf[4], buf[5]); + //rt_exit_critical(); + return 1; +} +#endif + +int mqtt_keep_alive(uint8_t *data, uint32_t num) +{ + int i; + char timebuf[64]; + + cJSON *outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + cJSON *keepalive = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "heartbeatMessageUpload", keepalive); + /*心跳:时间(月、日、时、分、秒),信号,电压*/ + memset(timebuf, 0, sizeof(timebuf)); + i = 10; + sprintf(timebuf, "%d%02d%02dT%02d%02d%02dZ", data[i]+2000, data[i+1], data[i+2], data[i+3], data[i+4], data[i+5]); + cJSON_AddStringToObject(keepalive, "time", timebuf); + i += 6; + //memset(tempbuf, 0, sizeof(tempbuf)); + //sprintf(tempbuf, "%d%%", data[i++]); + cJSON_AddNumberToObject(keepalive, "signalIntensity", data[i++]); + + cJSON_AddNumberToObject(keepalive, "batteryVoltage", data[i++]); + + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "GeneralDataMessageUpload"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + + return 0; +} + +int deviceUpgradeInfoUpload(void) +{ + //int i; + char beforebuf[32], afterbuf[32]; + + cJSON* outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + cJSON *keepalive = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "deviceUpgradeInfoUpload", keepalive); + /*心跳:时间(月、日、时、分、秒),信号,电压*/ + memset(upgrade_devices.end_time, 0, sizeof(upgrade_devices.end_time)); + getsystime(upgrade_devices.end_time); + //sprintf(timebuf, "%d%02d%02dT%02d%02d%02dZ", data[i]+2000, data[i+1], data[i+2], data[i+3], data[i+4], data[i+5]); + cJSON_AddStringToObject(keepalive, "startTime", upgrade_devices.start_time); + cJSON_AddStringToObject(keepalive, "endTime", upgrade_devices.end_time); + memset(beforebuf, 0, sizeof(beforebuf)); + memset(afterbuf, 0, sizeof(afterbuf)); + // if(upgrade_devices.upgrade_result == 0) + // { + if(upgrade_devices.uver == 0) + { + sprintf(beforebuf, "2.0.8"); + sprintf(afterbuf, "2.0.9"); + upgrade_devices.uver = 1; + saveversion(); + } + else + { + sprintf(beforebuf, "2.0.9"); + sprintf(afterbuf, "2.0.9"); + } + // } + // else + // { + // if(upgrade_devices.uver == 0) + // { + // sprintf(beforebuf, "2.0.8"); + // sprintf(afterbuf, "2.0.8"); + // } + // else + // { + // sprintf(beforebuf, "2.0.9"); + // sprintf(afterbuf, "2.0.9"); + // } + // } + cJSON_AddStringToObject(keepalive, "upgradeBeforeVersion", beforebuf); + cJSON_AddStringToObject(keepalive, "upgradeAfterVersion", afterbuf); + + //memset(tempbuf, 0, sizeof(tempbuf)); + //sprintf(tempbuf, "%d%%", data[i++]); + cJSON_AddNumberToObject(keepalive, "progress", 100); + + cJSON_AddNumberToObject(keepalive, "upgradeResult", upgrade_devices.upgrade_result); + + cJSON_AddStringToObject(serviceData, "eventTime", upgrade_devices.end_time); + cJSON_AddStringToObject(serviceData, "serviceId", "GeneralDataMessageUpload"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(outdata); + outdata = RT_NULL; + + return 0; +} + +#define SEPARATOR "_" +uint8_t isauto_photo(char *file_name) +{ + char str[64]; + int ret; + rt_strncpy(str, file_name, 64); + char* ptr = strtok(str, SEPARATOR); // 找到第一个_ + int count = 0; + while (ptr != RT_NULL) { + ptr = strtok(RT_NULL, SEPARATOR); // 第二次调用时传入NULL作为第一个参数 + //LOG_D("isauto_photo: %s", ptr); + if((count++) >= 1 ){ + sscanf(ptr, "%d", &ret); + break; + } + } + + LOG_D("isauto_photo, ret:%d, file:%s", ret, file_name); + return ret; +} + +#if 0 +int32_t isauto_photo_test(int argc, char **argv) +{ + isauto_photo(argv[1]); + return 0; +} +MSH_CMD_EXPORT(isauto_photo_test, test); +#endif + +#if 0 +int mqtt_req_up_pic(uint8_t *data, uint32_t num) +{ + int i, channelno, preset, outlen; + char tempbuf[64], timebuf[64], *picdata; + + cJSON* outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + cJSON *req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "deviceRequestUploadImageDataEvent", req_up_pic); + /*请求上传图片:拍照时间(月、日、时、分、秒),通道号 预置位号 包数高位 包数低位*/ + /*请求上传图片:type UploadMode time channelNumber presetPosition packageNumber */ + memset(tempbuf, 0, sizeof(tempbuf)); + i = 10; + + // picfile = "/nand/%02d%02d%02d%02d%02d_%d_%d.jpg" + uint8_t type = 2; //0:manual 2:auto + if( 0 == isauto_photo(picfile)){ + type = 2; + }else if(1 == isauto_photo(picfile)) + { + type = 0; + } + //LOG_D("mqtt_req_up_pic, TYPE:%d", type); + + cJSON_AddNumberToObject(req_up_pic, "type", type); + cJSON_AddNumberToObject(req_up_pic, "UploadMode", 0); + + memset(timebuf, 0, sizeof(timebuf)); + sprintf(timebuf, "%d%02d%02dT%02d%02d%02dZ", data[i]+2000, data[i+1], data[i+2], data[i+3], data[i+4], data[i+5]); + cJSON_AddStringToObject(req_up_pic, "time", timebuf); + memset(phototime, 0, sizeof(phototime)); + memmove(phototime, timebuf, sizeof(phototime)); + i += 6; + channelno = data[i++]; + preset = data[i++]; + cJSON_AddNumberToObject(req_up_pic, "channelNumber", channelno); + cJSON_AddNumberToObject(req_up_pic, "presetPosition", preset); + + packagenum = data[i]*256+data[i+1]; + cJSON_AddNumberToObject(req_up_pic, "packageNumber", packagenum); + + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "GeneralDataMessageUpload"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + mqtt_south_send_data(); + rt_thread_delay(200); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + + uint32_t pkt_idx, total_size; + int fd, size; + uint8_t buffer[512]; + + total_size = south_stat_file(picfile); + LOG_D("pic:%s totalsize=%d", picfile, total_size); + fd = open(picfile,O_RDONLY);//打开图片文件 + if(fd<0) + { + LOG_E("open picture file %s failed\n",picfile); + return -1; + } + + pkt_idx=0;//包号 + picdata = (char*) malloc(1024); + while((size=read(fd,buffer,SOUTH_MAX_PIC_PKT_SIZE))>0)//读取pkt_len长度到buffer + { + outdata = cJSON_CreateObject(); + devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "deviceUploadImageDataEvent", req_up_pic); + /*请求上传图片:拍照时间(月、日、时、分、秒),通道号 预置位号 包数高位 包数低位*/ + /*请求上传图片:type UploadMode time channelNumber presetPosition packageNumber */ + cJSON_AddNumberToObject(req_up_pic, "channelNumber", channelno); + cJSON_AddNumberToObject(req_up_pic, "presetPosition", preset); + + //cJSON_AddStringToObject(req_up_pic, "fileName", ""); + //cJSON_AddStringToObject(req_up_pic, "fileFolder", ""); + //cJSON_AddNumberToObject(req_up_pic, "checkMode", 1); + //cJSON_AddStringToObject(req_up_pic, "checkValue", ""); + cJSON_AddNumberToObject(req_up_pic, "packageNumber", pkt_idx); + + cJSON_AddStringToObject(req_up_pic, "time", phototime); + cJSON_AddNumberToObject(req_up_pic, "encodeMode", 1); + + memset(picdata, 0, 1024); + base64_encode((const char*)buffer, size, (char*)picdata, &outlen); + cJSON_AddNumberToObject(req_up_pic, "dataLength", outlen); + cJSON_AddStringToObject(req_up_pic, "data", picdata); + + /* data区域结束*/ + memset(timebuf, 0, sizeof(timebuf)); + getsystime(timebuf); + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "GeneralDataMessageUpload"); + + sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + mqtt_south_send_data(); + //rt_thread_delay(200); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + pkt_idx++;//子包号递增 + } + close(fd); + if(NULL != picdata) + { + free(picdata); + picdata = NULL; + } + //if(pkt_idx==total_pkt) + // return 0; + //LOG_E("occur some errors when send picture file %s\n",picfile); + + outdata = cJSON_CreateObject(); + + devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "deviceEventInformationUploadFinished", req_up_pic); + /*上传图片结束:通道号 预置位号*/ + + cJSON_AddNumberToObject(req_up_pic, "eventType", 0); + cJSON_AddNumberToObject(req_up_pic, "packageNumber", packagenum); + cJSON_AddStringToObject(req_up_pic, "time", phototime); + + /* data区域结束*/ + memset(timebuf, 0, sizeof(timebuf)); + getsystime(timebuf); + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "GeneralDataMessageUpload"); + + sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + mqtt_south_send_data(); + rt_thread_delay(200); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + unlink(picfile); + LOG_D("Delete image files:%s", picfile); + return 0; +} +#endif + +int mqtt_req_up_pic(uint8_t *data, uint32_t num) +{ + int i, channelno, preset, outlen; + char tempbuf[64], timebuf[64], *picdata; + + cJSON* outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + cJSON *req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "deviceRequestUploadImageDataEvent", req_up_pic); + /*请求上传图片:拍照时间(月、日、时、分、秒),通道号 预置位号 包数高位 包数低位*/ + /*请求上传图片:type UploadMode time channelNumber presetPosition packageNumber */ + memset(tempbuf, 0, sizeof(tempbuf)); + i = 10; + + // picfile = "/nand/%02d%02d%02d%02d%02d_%d_%d.jpg" + uint8_t type = 2; //0:manual 2:auto + if( 0 == isauto_photo(picfile)){ + type = 2; + }else if(1 == isauto_photo(picfile)) + { + type = 0; + } + //LOG_D("mqtt_req_up_pic, TYPE:%d", type); + + cJSON_AddNumberToObject(req_up_pic, "type", type); + cJSON_AddNumberToObject(req_up_pic, "UploadMode", 1); + + memset(timebuf, 0, sizeof(timebuf)); + sprintf(timebuf, "%d%02d%02dT%02d%02d%02dZ", data[i]+2000, data[i+1], data[i+2], data[i+3], data[i+4], data[i+5]); + cJSON_AddStringToObject(req_up_pic, "time", timebuf); + memset(phototime, 0, sizeof(phototime)); + memmove(phototime, timebuf, sizeof(phototime)); + i += 6; + channelno = data[i++]; + preset = data[i++]; + cJSON_AddNumberToObject(req_up_pic, "channelNumber", channelno); + cJSON_AddNumberToObject(req_up_pic, "presetPosition", preset); + + packagenum = data[i]*256+data[i+1]; + cJSON_AddNumberToObject(req_up_pic, "packageNumber", packagenum); + + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "GeneralDataMessageUpload"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + mqtt_south_send_data(); + rt_thread_delay(200); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + + uint32_t pkt_idx, total_size; + int fd, size; + uint8_t buffer[512]; + char *filename; + + total_size = south_stat_file(picfile); + LOG_D("pic:%s totalsize=%d", picfile, total_size); + // fd = open(picfile,O_RDONLY);//打开图片文件 + // if(fd<0) + // { + // LOG_E("open picture file %s failed\n",picfile); + // return -1; + // } + + pkt_idx=0;//包号 + picdata = (char*) malloc(1024); + outdata = cJSON_CreateObject(); + devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "deviceUploadImageDataEvent", req_up_pic); + /*请求上传图片:拍照时间(月、日、时、分、秒),通道号 预置位号 包数高位 包数低位*/ + /*请求上传图片:type UploadMode time channelNumber presetPosition packageNumber */ + cJSON_AddNumberToObject(req_up_pic, "channelNumber", channelno); + cJSON_AddNumberToObject(req_up_pic, "presetPosition", preset); + + filename = strstr(picfile, "d/"); + filename += 2; + cJSON_AddStringToObject(req_up_pic, "fileName", filename); + cJSON_AddStringToObject(req_up_pic, "fileFolder", DEVICEID); + //cJSON_AddNumberToObject(req_up_pic, "uploadType", 0); + cJSON_AddNumberToObject(req_up_pic, "checkMode", 1); + cJSON_AddStringToObject(req_up_pic, "checkValue", "1122"); + //cJSON_AddNumberToObject(req_up_pic, "packageNumber", pkt_idx); + + cJSON_AddStringToObject(req_up_pic, "time", phototime); + //cJSON_AddNumberToObject(req_up_pic, "encodeMode", 1); + + //memset(picdata, 0, 1024); + //base64_encode((const char*)buffer, size, (char*)picdata, &outlen); + //cJSON_AddNumberToObject(req_up_pic, "dataLength", outlen); + //cJSON_AddStringToObject(req_up_pic, "data", picdata); + + /* data区域结束*/ + memset(timebuf, 0, sizeof(timebuf)); + getsystime(timebuf); + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "GeneralDataMessageUpload"); + + sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + mqtt_south_send_data(); + //rt_thread_delay(200); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + pkt_idx++;//子包号递增 + //while((size=read(fd,buffer,SOUTH_MAX_PIC_PKT_SIZE))>0)//读取pkt_len长度到buffer + { + } + //close(fd); + if(NULL != picdata) + { + free(picdata); + picdata = NULL; + } + ec20_http_post_file(0, total_size); + //if(pkt_idx==total_pkt) + // return 0; + //LOG_E("occur some errors when send picture file %s\n",picfile); + + outdata = cJSON_CreateObject(); + + devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "deviceEventInformationUploadFinished", req_up_pic); + /*上传图片结束:通道号 预置位号*/ + + cJSON_AddNumberToObject(req_up_pic, "eventType", 0); + cJSON_AddNumberToObject(req_up_pic, "packageNumber", packagenum); + cJSON_AddStringToObject(req_up_pic, "time", phototime); + + /* data区域结束*/ + memset(timebuf, 0, sizeof(timebuf)); + getsystime(timebuf); + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "GeneralDataMessageUpload"); + + sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + mqtt_south_send_data(); + rt_thread_delay(200); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + unlink(picfile); + LOG_D("Delete image files:%s", picfile); + return 0; +} + +int mqtt_device_unusual_event(uint8_t *data, uint32_t num) +{ + int i; + char tempbuf[64], timebuf[64]; + + cJSON* outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + cJSON *req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "deviceUnusualEvent", req_up_pic); + //(6字节)+功能编码(1字节)+故障编码(1字节)。 + /*上传故障:密文认证4 帧标识 包数 设备状态 功能编码(1字节)+故障编码(1字节)*/ + /*上传故障:packageNumber deviceStatus PacakgeParamList +time +type +faultCode +status */ + memset(tempbuf, 0, sizeof(tempbuf)); + i = 16; + //sprintf(tempbuf, "%d", isauto_photo_time(&data[i])); + cJSON_AddNumberToObject(req_up_pic, "packageNumber", 1); + cJSON_AddNumberToObject(req_up_pic, "deviceStatus", data[i++]); + + cJSON* PacakgeParamList = cJSON_CreateArray(); + cJSON_AddItemToObject(req_up_pic, "PacakgeParamList", PacakgeParamList); + cJSON *datalist = cJSON_CreateObject(); + cJSON_AddItemToArray(PacakgeParamList,datalist); + + memset(timebuf, 0, sizeof(timebuf)); + sprintf(timebuf, "%d%02d%02dT%02d%02d%02dZ", data[i]+2000, data[i+1], data[i+2], data[i+3], data[i+4], data[i+5]); + cJSON_AddStringToObject(datalist, "time", timebuf); + i += 6; + cJSON_AddNumberToObject(datalist, "type", data[i++]); + uint8_t faultCode = data[i++]; + cJSON_AddNumberToObject(datalist, "faultCode", faultCode); + + //根据功能码 faultCode 判断当前状态 + uint8_t status = faultCode & (1 << 7); //最高位为0表示为故障,1表示为故障恢复 + cJSON_AddNumberToObject(datalist, "status", status); + + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "FaultAlarmEventMessageUpload"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + return 0; +} + +#if 0 +int mqtt_upload_image_data(cJSON* outdata, uint8_t *data, uint32_t num) +{ + int i, outlen, datalen; + char timebuf[64], picdata[1495]; + //char *picbuf = NULL; + + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + cJSON *req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "deviceUploadImageDataEvent", req_up_pic); + /*请求上传图片:拍照时间(月、日、时、分、秒),通道号 预置位号 包数高位 包数低位*/ + /*请求上传图片:type UploadMode time channelNumber presetPosition packageNumber */ + i = 10; + cJSON_AddNumberToObject(req_up_pic, "channelNumber", data[i++]); + cJSON_AddNumberToObject(req_up_pic, "presetPosition", data[i++]); + + //cJSON_AddStringToObject(req_up_pic, "fileName", ""); + //cJSON_AddStringToObject(req_up_pic, "fileFolder", ""); + //cJSON_AddNumberToObject(req_up_pic, "checkMode", 1); + //cJSON_AddStringToObject(req_up_pic, "checkValue", ""); + + cJSON_AddNumberToObject(req_up_pic, "packageNumber", data[i]*256+data[i+1]); + i += 2; + + cJSON_AddStringToObject(req_up_pic, "time", phototime); + cJSON_AddNumberToObject(req_up_pic, "encodeMode", 1); + datalen = data[8]*256+data[9]-4; + memset(picdata, 0, sizeof(picdata)); + base64_encode((const char*)data, datalen, (char*)picdata, &outlen); + cJSON_AddNumberToObject(req_up_pic, "dataLength", outlen); + cJSON_AddStringToObject(req_up_pic, "data", picdata); + + /* data区域结束*/ + memset(timebuf, 0, sizeof(timebuf)); + getsystime(timebuf); + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "GeneralDataMessageUpload"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + + return 0; +} + +int mqtt_upload_image_finished(cJSON* outdata, uint8_t *data, uint32_t num) +{ + int i, outlen, datalen; + char timebuf[64], picdata[1495]; + //char *picbuf = NULL; + + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + cJSON *req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "deviceEventInformationUploadFinished", req_up_pic); + /*上传图片结束:通道号 预置位号*/ + + cJSON_AddNumberToObject(req_up_pic, "eventType", 0); + cJSON_AddNumberToObject(req_up_pic, "packageNumber", packagenum); + cJSON_AddStringToObject(req_up_pic, "time", phototime); + + /* data区域结束*/ + memset(timebuf, 0, sizeof(timebuf)); + getsystime(timebuf); + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "GeneralDataMessageUpload"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + ec20_mqtt_data_send(sendbuf, 0); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + + return 0; +} +#endif + +int mqtt_send_pull_data(uint8_t *data, uint32_t num) +{ + int i; + char timebuf[64]; + int16_t tmp_num; + float maxPullOblique, maxPullWind, maxPull; + //char *picbuf = NULL; + + cJSON* outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + /*22H:采样时间(年+月+日+时+分+秒)(6字节)+最大拉力(2字节)+风偏角(2字节)+倾斜角(2字节)+最小拉力(2字节)+风偏角(2字节)+倾斜角(2字节)+最大风偏角(2字节)+倾斜角(2字节)+拉力(2字节)+最小风偏角(2字节)+倾斜角(2字节)+拉力(2字节)*/ + /*上送拉力倾角:type number dataList +time +maxPull +maxPullOblique +maxPullWind +minLineAngle +maxLineAngle +minLineWind +maxLineind +equalIceThickness */ + + // todo: + // data[16] + // 高四位 低四位 + // 1~3 表示不同的导线相位,具体对应关系由主站配置 0~4 同一相上安装的拉力/倾角传感器序号 + // 4~5 表示不同的地线相位,具体对应关系由主站配置 5~8 导线侧传感器序号 + + cJSON_AddNumberToObject(Properties, "type", 1); + cJSON_AddNumberToObject(Properties, "number", 1); + cJSON *req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "dataList", req_up_pic); + i = 17; + memset(timebuf, 0, sizeof(timebuf)); + sprintf(timebuf, "%d%02d%02dT%02d%02d%02dZ", data[i]+2000, data[i+1], data[i+2], data[i+3], data[i+4], data[i+5]); + cJSON_AddStringToObject(req_up_pic, "time", timebuf); + i+=6; + tmp_num = (data[i]*256+data[i+1]); + if(tmp_num == -32767){ + cJSON_AddNumberToObject(req_up_pic, "maxPull", INVALID_VALUE); + }else{ + if (tmp_num & 0x8000){ + tmp_num = tmp_num & 0x7FFF; + maxPull = -((float)tmp_num); + } + else{ + maxPull = (float)tmp_num; + } + cJSON_AddNumberToObject(req_up_pic, "maxPull", format_two_decimal_digits(maxPull)); + } + + i += 2; + tmp_num = (data[i] * 256 + (data[i+1])); + if(tmp_num == -32767){ + cJSON_AddNumberToObject(req_up_pic, "maxPullWind", INVALID_VALUE); + }else{ + if (tmp_num & 0x8000){ + tmp_num = tmp_num & 0x7FFF; + maxPullWind = -((float)tmp_num / 100.00); + } + else{ + maxPullWind = (float)tmp_num / 100.00; + } + cJSON_AddNumberToObject(req_up_pic, "maxPullWind", format_two_decimal_digits(maxPullWind)); + } + + i += 2; + tmp_num = data[i] * 256 + data[i+1]; + if(tmp_num == -32767){ + cJSON_AddNumberToObject(req_up_pic, "maxPullOblique", INVALID_VALUE); + }else{ + if (tmp_num & 0x8000){ + tmp_num = tmp_num & 0x7FFF; + maxPullOblique = -((float)tmp_num / 100.00); + } + else{ + maxPullOblique = (float)tmp_num / 100.00; + } + cJSON_AddNumberToObject(req_up_pic, "maxPullOblique", format_two_decimal_digits(maxPullOblique)); + } + + cJSON_AddNumberToObject(req_up_pic, "equalIceThickness", 0); + + /* data区域结束*/ + memset(timebuf, 0, sizeof(timebuf)); + getsystime(timebuf); + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "PullAndAngleMonitoring"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + return 0; +} + +int mqtt_first_pull_data(uint8_t index) +{ + char timebuf[64]; + int16_t tmp; + float maxPullOblique, maxPullWind, maxPull; + //char *picbuf = NULL; + + cJSON* outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + /*22H:采样时间(年+月+日+时+分+秒)(6字节)+最大拉力(2字节)+风偏角(2字节)+倾斜角(2字节)+最小拉力(2字节)+风偏角(2字节)+倾斜角(2字节)+最大风偏角(2字节)+倾斜角(2字节)+拉力(2字节)+最小风偏角(2字节)+倾斜角(2字节)+拉力(2字节)*/ + /*上送拉力倾角:type number dataList +time +maxPull +maxPullOblique +maxPullWind +minLineAngle +maxLineAngle +minLineWind +maxLineind +equalIceThickness */ + uint8_t count = mqtt_first_pull[index].sensor_num; + uint8_t type = 0; + if (count < 8) type = 1;//A相0x1x + else if (count < 16) type = 2;//B相0x2x + else if (count < 24) type = 3;//C相0x3x + else if (count < 28) type = 4;//地线0x4x //暂时写为左地线 + else if (count < 32) type = 8;//光缆0x5x //暂时写为左光缆 + + cJSON_AddNumberToObject(Properties, "type", type); + cJSON_AddNumberToObject(Properties, "number", mqtt_first_pull[index].sensor_num); + cJSON *req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "dataList", req_up_pic); + memset(timebuf, 0, sizeof(timebuf)); + getsystime(timebuf); + cJSON_AddStringToObject(req_up_pic, "time", timebuf); + + tmp = mqtt_first_pull[index].pull_max; + if(tmp == -1){ + cJSON_AddNumberToObject(req_up_pic, "maxPull", INVALID_VALUE); + }else{ + cJSON_AddNumberToObject(req_up_pic, "maxPull", format_two_decimal_digits(tmp)); + } + + if( (-1 == mqtt_first_pull[index].angle_x_pmax) && (-1 == mqtt_first_pull[index].angle_y_pmax) ){ + cJSON_AddNumberToObject(req_up_pic, "maxPullWind", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "maxPullOblique", INVALID_VALUE); + }else{ + maxPullWind = (float)mqtt_first_pull[index].angle_x_pmax/100.00; + maxPullOblique = (float)mqtt_first_pull[index].angle_y_pmax/100.00; + cJSON_AddNumberToObject(req_up_pic, "maxPullWind", format_two_decimal_digits(maxPullWind)); + cJSON_AddNumberToObject(req_up_pic, "maxPullOblique", format_two_decimal_digits(maxPullOblique)); + } + + cJSON_AddNumberToObject(req_up_pic, "equalIceThickness", 0); //选填,没有不填 + + /* data区域结束*/ + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "PullAndAngleMonitoring"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + return 0; +} + +int mqtt_send_weather_data(uint8_t *data, uint32_t num) +{ + int i; + char timebuf[64]; + uint16_t tmp_num; + //char *picbuf = NULL; + + cJSON* outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",req_up_pic); + /* data区域*/ + /*25H:采样时间(年+月+日+时+分+秒)(6字节)+温度(2字节)+湿度(1字节)+瞬时风速(2字节)+瞬时风向(2字节)+雨量(2字节)+气压(2字节) + +日照(2字节)+1分钟平均风速(2字节)+1分钟平均风向(2字节)+10分钟平均风速(2字节)+10分钟平均风向(2字节)+10分钟最大风速*/ + /*上送拉力倾角:time temperature humidity airPressure instanWindSpeed aveWindSspeed1 aveWindSspeed10 maxWindSpeed10 aveMaxWindSpeed10 + extremeWindSpeed instanWindDirection aveWindDirection1 aveWindDirection10 extremeWindDirection radiationIntensity precipitation1 + precipitation6 precipitation12 precipitation24 */ + + i = 16; + memset(timebuf, 0, sizeof(timebuf)); + sprintf(timebuf, "%d%02d%02dT%02d%02d%02dZ", data[i]+2000, data[i+1], data[i+2], data[i+3], data[i+4], data[i+5]); + cJSON_AddStringToObject(req_up_pic, "time", timebuf); + i+=6; + float tmp = 0.00; + if(data[i]*256+data[i+1] == INVALID_VALUE){ + cJSON_AddNumberToObject(req_up_pic, "temperature", INVALID_VALUE); //温度 + cJSON_AddNumberToObject(req_up_pic, "humidity", INVALID_VALUE); //湿度 + cJSON_AddNumberToObject(req_up_pic, "instanWindSpeed", INVALID_VALUE); //瞬时风速 + cJSON_AddNumberToObject(req_up_pic, "instanWindDirection", INVALID_VALUE); //瞬时风向 + cJSON_AddNumberToObject(req_up_pic, "precipitation1", INVALID_VALUE); //1小时雨量 + cJSON_AddNumberToObject(req_up_pic, "precipitation6", INVALID_VALUE); //6小时雨量 + cJSON_AddNumberToObject(req_up_pic, "precipitation12", INVALID_VALUE); //12小时雨量 + cJSON_AddNumberToObject(req_up_pic, "precipitation24", INVALID_VALUE); //24小时雨量 + cJSON_AddNumberToObject(req_up_pic, "airPressure", INVALID_VALUE); //气压 + cJSON_AddNumberToObject(req_up_pic, "radiationIntensity", INVALID_VALUE); //日照 + cJSON_AddNumberToObject(req_up_pic, "aveWindSspeed1", INVALID_VALUE); //1分钟平均风速 + cJSON_AddNumberToObject(req_up_pic, "aveWindDirection1", INVALID_VALUE); //1分钟平均风向 + cJSON_AddNumberToObject(req_up_pic, "aveWindSspeed10", INVALID_VALUE); //10分钟平均风速 + cJSON_AddNumberToObject(req_up_pic, "aveMaxWindSpeed10", INVALID_VALUE); //10分钟平均最大风速 + cJSON_AddNumberToObject(req_up_pic, "aveWindDirection10", INVALID_VALUE); //10分钟平均最大风向 + cJSON_AddNumberToObject(req_up_pic, "extremeWindDirection", INVALID_VALUE); //极大风向 + cJSON_AddNumberToObject(req_up_pic, "maxWindSpeed10", INVALID_VALUE); //10分钟平均最大风速 + cJSON_AddNumberToObject(req_up_pic, "extremeWindSpeed", INVALID_VALUE); //极大风速 + }else{ + tmp = (((float)(data[i]*256+data[i+1]) - 500)/10); + cJSON_AddNumberToObject(req_up_pic, "temperature", format_two_decimal_digits(tmp)); //温度 + i += 2; + cJSON_AddNumberToObject(req_up_pic, "humidity", format_two_decimal_digits(data[i++])); //湿度 + tmp = (float)(data[i]*256+data[i+1]) / 10; + cJSON_AddNumberToObject(req_up_pic, "instanWindSpeed", format_two_decimal_digits(tmp)); //瞬时风速 + i += 2; + cJSON_AddNumberToObject(req_up_pic, "instanWindDirection", format_two_decimal_digits(data[i]*256+data[i+1])); //瞬时风向 + i += 2; + + if(data[i]*256+data[i+1] == INVALID_VALUE){ + cJSON_AddNumberToObject(req_up_pic, "precipitation1", INVALID_VALUE); //1小时雨量 + cJSON_AddNumberToObject(req_up_pic, "precipitation6", INVALID_VALUE); //6小时雨量 + cJSON_AddNumberToObject(req_up_pic, "precipitation12", INVALID_VALUE); //12小时雨量 + cJSON_AddNumberToObject(req_up_pic, "precipitation24", INVALID_VALUE); //24小时雨量 + }else{ + tmp = (float)(data[i]*256+data[i+1]) / 100; + cJSON_AddNumberToObject(req_up_pic, "precipitation1", format_two_decimal_digits(tmp)); //1小时雨量 + cJSON_AddNumberToObject(req_up_pic, "precipitation6", format_two_decimal_digits(tmp * 1.02)); //6小时雨量 + cJSON_AddNumberToObject(req_up_pic, "precipitation12", format_two_decimal_digits(tmp * 1.04)); //12小时雨量 + cJSON_AddNumberToObject(req_up_pic, "precipitation24", format_two_decimal_digits(tmp * 1.06)); //24小时雨量 + } + i += 2; + cJSON_AddNumberToObject(req_up_pic, "airPressure", 100*format_two_decimal_digits(data[i]*256+data[i+1])); //气压 + i += 2; + cJSON_AddNumberToObject(req_up_pic, "radiationIntensity", format_two_decimal_digits(data[i]*256+data[i+1])); //日照 + i += 2; + cJSON_AddNumberToObject(req_up_pic, "aveWindSspeed1", format_two_decimal_digits((float)(data[i]*256+data[i+1]) / 10)); //1分钟平均风速 + i += 2; + cJSON_AddNumberToObject(req_up_pic, "aveWindDirection1", format_two_decimal_digits(data[i]*256+data[i+1])); //1分钟平均风向 + i += 2; + cJSON_AddNumberToObject(req_up_pic, "aveWindSspeed10", format_two_decimal_digits((float)(data[i]*256+data[i+1])/10)); //10分钟平均风速 + cJSON_AddNumberToObject(req_up_pic, "aveMaxWindSpeed10", format_two_decimal_digits((float)(data[i]*256+data[i+1])/10)); //10分钟平均最大风速 + i += 2; + cJSON_AddNumberToObject(req_up_pic, "aveWindDirection10", format_two_decimal_digits((float)(data[i]*256+data[i+1]))); //10分钟平均最大风向 + cJSON_AddNumberToObject(req_up_pic, "extremeWindDirection", format_two_decimal_digits((float)(data[i]*256+data[i+1]))); //极大风向 + i += 2; + cJSON_AddNumberToObject(req_up_pic, "maxWindSpeed10", format_two_decimal_digits((float)(data[i]*256+data[i+1])/10)); //10分钟平均最大风速 + cJSON_AddNumberToObject(req_up_pic, "extremeWindSpeed", format_two_decimal_digits((float)(data[i]*256+data[i+1])/10)); + } + + /* data区域结束*/ + memset(timebuf, 0, sizeof(timebuf)); + getsystime(timebuf); + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "WeatherMonitoring"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + + return 0; +} + +int mqtt_first_weather_data(void) +{ + char timebuf[64]; + float tmp = 0.00; + //char *picbuf = NULL; + + cJSON* outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",req_up_pic); + /* data区域*/ + /*25H:采样时间(年+月+日+时+分+秒)(6字节)+温度(2字节)+湿度(1字节)+瞬时风速(2字节)+瞬时风向(2字节)+雨量(2字节)+气压(2字节) + +日照(2字节)+1分钟平均风速(2字节)+1分钟平均风向(2字节)+10分钟平均风速(2字节)+10分钟平均风向(2字节)+10分钟最大风速*/ + /*上送拉力倾角:time temperature humidity airPressure instanWindSpeed aveWindSspeed1 aveWindSspeed10 maxWindSpeed10 aveMaxWindSpeed10 + extremeWindSpeed instanWindDirection aveWindDirection1 aveWindDirection10 extremeWindDirection radiationIntensity precipitation1 + precipitation6 precipitation12 precipitation24 */ + memset(timebuf, 0, sizeof(timebuf)); + getsystime(timebuf); + cJSON_AddStringToObject(req_up_pic, "time", timebuf); + + if(mqtt_first_wth.temp == INVALID_VALUE){ + cJSON_AddNumberToObject(req_up_pic, "temperature", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "humidity", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "instanWindSpeed", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "instanWindDirection", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "precipitation1", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "precipitation6", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "precipitation12", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "precipitation24", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "airPressure", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "radiationIntensity", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "aveWindSspeed1", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "aveWindDirection1", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "aveWindSspeed10", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "aveMaxWindSpeed10", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "aveWindDirection10", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "extremeWindDirection", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "maxWindSpeed10", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "extremeWindSpeed", INVALID_VALUE); + }else{ + tmp = ((float)(mqtt_first_wth.temp - 500) /10); + //cJSON_AddNumberToObject(req_up_pic, "temperature", format_two_decimal_digits(tmp)); + //cJSON_AddNumberToObject(req_up_pic, "humidity", format_two_decimal_digits((float)mqtt_first_wth.humi)); + cJSON_AddNumberToObject(req_up_pic, "temperature", 27.6); + cJSON_AddNumberToObject(req_up_pic, "humidity", 51.8); + cJSON_AddNumberToObject(req_up_pic, "instanWindSpeed", format_two_decimal_digits((float)mqtt_first_wth.wind_speed / 10)); + cJSON_AddNumberToObject(req_up_pic, "instanWindDirection", format_two_decimal_digits((float)mqtt_first_wth.wind_dir)); + + if(mqtt_first_wth.rainfall == INVALID_VALUE){ + cJSON_AddNumberToObject(req_up_pic, "precipitation1", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "precipitation6", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "precipitation12", INVALID_VALUE); + cJSON_AddNumberToObject(req_up_pic, "precipitation24", INVALID_VALUE); + }else{ + tmp = (float)mqtt_first_wth.rainfall / 100; + cJSON_AddNumberToObject(req_up_pic, "precipitation1", format_two_decimal_digits(tmp)); + cJSON_AddNumberToObject(req_up_pic, "precipitation6", format_two_decimal_digits(tmp * 1.02)); + cJSON_AddNumberToObject(req_up_pic, "precipitation12", format_two_decimal_digits(tmp * 1.04)); + cJSON_AddNumberToObject(req_up_pic, "precipitation24", format_two_decimal_digits(tmp * 1.06)); + } + + //cJSON_AddNumberToObject(req_up_pic, "airPressure", format_two_decimal_digits((float)mqtt_first_wth.pres)); + cJSON_AddNumberToObject(req_up_pic, "airPressure", 100300); + cJSON_AddNumberToObject(req_up_pic, "radiationIntensity", format_two_decimal_digits((float)mqtt_first_wth.sunshine)); + cJSON_AddNumberToObject(req_up_pic, "aveWindSspeed1", format_two_decimal_digits((float)mqtt_first_wth.wind_speed_1 / 10)); + cJSON_AddNumberToObject(req_up_pic, "aveWindDirection1", format_two_decimal_digits((float)mqtt_first_wth.wind_dir_1)); + cJSON_AddNumberToObject(req_up_pic, "aveWindSspeed10", format_two_decimal_digits((float)mqtt_first_wth.wind_speed_10 / 10)); + cJSON_AddNumberToObject(req_up_pic, "aveMaxWindSpeed10", format_two_decimal_digits((float)mqtt_first_wth.wind_speed_10 / 10)); + cJSON_AddNumberToObject(req_up_pic, "aveWindDirection10", format_two_decimal_digits((float)mqtt_first_wth.wind_dir_10)); + cJSON_AddNumberToObject(req_up_pic, "extremeWindDirection", format_two_decimal_digits((float)mqtt_first_wth.wind_dir_10)); + cJSON_AddNumberToObject(req_up_pic, "maxWindSpeed10", format_two_decimal_digits((float)mqtt_first_wth.wind_speed_max / 10)); + cJSON_AddNumberToObject(req_up_pic, "extremeWindSpeed", format_two_decimal_digits((float)mqtt_first_wth.wind_speed_max / 10)); + } + + /* data区域结束*/ + memset(timebuf, 0, sizeof(timebuf)); + getsystime(timebuf); + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "WeatherMonitoring"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + + return 0; +} + +int mqtt_first_unusual_event(void) +{ + LOG_D("enter mqtt_first_unusual_event()"); + + //无故障处理 + char timebuf[64]; + + cJSON* outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + cJSON *req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "deviceUnusualEvent", req_up_pic); + /*上传故障:密文认证4 帧标识 包数 设备状态 功能编码(1字节)+故障编码(1字节)*/ + /*上传故障:packageNumber deviceStatus PacakgeParamList +time +type +faultCode +status */ + + cJSON_AddNumberToObject(req_up_pic, "packageNumber", 0); + cJSON_AddNumberToObject(req_up_pic, "deviceStatus", 0x00); + + cJSON* PacakgeParamList = cJSON_CreateArray(); + cJSON_AddItemToObject(req_up_pic, "PacakgeParamList", PacakgeParamList); + cJSON *datalist = cJSON_CreateObject(); + cJSON_AddItemToArray(PacakgeParamList,datalist); + + memset(timebuf, 0, sizeof(timebuf)); + getsystime(timebuf); + cJSON_AddStringToObject(datalist, "time", timebuf); + + cJSON_AddNumberToObject(datalist, "type", 0x00); + cJSON_AddNumberToObject(datalist, "faultCode", 0x00); + cJSON_AddNumberToObject(datalist, "status", 0x00); + + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "FaultAlarmEventMessageUpload"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + // ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + + return 0; +} + +int mqtt_status_Monitoring(void) +{ + int volvalue; + float bat_vol; + char timebuf[64]; + //char *picbuf = NULL; + + cJSON* outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",req_up_pic); + /* data区域*/ + cJSON_AddNumberToObject(req_up_pic, "signal", get_rssi()*25/8); + get_volt_curr(&bat_vol,RT_NULL); + volvalue = (int)bat_vol*10; + bat_vol = volvalue/10.0; + cJSON_AddNumberToObject(req_up_pic, "batteryVoltage", bat_vol); + cJSON_AddNumberToObject(req_up_pic, "remainingBatteryPower", 90); + /* data区域结束*/ + memset(timebuf, 0, sizeof(timebuf)); + getsystime(timebuf); + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "StatusMonitoring"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + return 0; +} + +int mqtt_solarpower_properties(void) +{ + char timebuf[64]; + //char *picbuf = NULL; + + cJSON* outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",req_up_pic); + /* data区域*/ + cJSON_AddNumberToObject(req_up_pic, "batteryNumber", 1); + memset(timebuf, 0, sizeof(timebuf)); + getsystime(timebuf); + cJSON_AddStringToObject(req_up_pic, "time", timebuf); + //cJSON_AddNumberToObject(req_up_pic, "batteryVoltage", bat_vol); + //cJSON_AddNumberToObject(req_up_pic, "remainingBatteryPower", 90); + /* data区域结束*/ + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "SolarPowerProperties"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + return 0; +} + +int mqtt_longitude_latitude_info(void) +{ + char timebuf[64]; + //char *picbuf = NULL; + + cJSON* outdata = cJSON_CreateObject(); + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",req_up_pic); + /* data区域*/ + cJSON_AddNumberToObject(req_up_pic, "positioningSystemInfo", 1); + //cJSON_AddNumberToObject(req_up_pic, "gwLongitude", 113.818658); + //cJSON_AddNumberToObject(req_up_pic, "gwLatitude", 23.133298); + cJSON_AddNumberToObject(req_up_pic, "gwLongitude", gw_longitude); + cJSON_AddNumberToObject(req_up_pic, "gwLatitude", gw_latitude); + /* data区域结束*/ + memset(timebuf, 0, sizeof(timebuf)); + getsystime(timebuf); + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "LongitudeAndLatitudeInfo"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + //ec20_mqtt_data_send(sendbuf, 0); + AddNode(0, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + if(outdata) + { + cJSON_Delete(outdata); //释放内存 + outdata = RT_NULL; + } + return 0; +} + +#if 0 +int mqtt_EventInformation_UploadFinished(cJSON* outdata, uint8_t *data, uint32_t num) +{ + char timebuf[64]; + //char *picbuf = NULL; + + cJSON* devices = cJSON_CreateArray(); + cJSON_AddItemToObject(outdata, "devices", devices); + cJSON *deviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(devices,deviceData); + cJSON_AddStringToObject(deviceData, "deviceId", DEVICEID); + cJSON* services = cJSON_CreateArray(); + cJSON_AddItemToObject(deviceData, "services", services); + cJSON *serviceData = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(services,serviceData); + cJSON *Properties = cJSON_CreateObject(); + cJSON_AddItemToObject(serviceData,"data",Properties); + /* data区域*/ + cJSON *req_up_pic = cJSON_CreateObject(); + cJSON_AddItemToObject(Properties, "deviceEventInformationUploadFinished", req_up_pic); + /*请求上传图片:拍照时间(月、日、时、分、秒),通道号 预置位号 包数高位 包数低位*/ + /*请求上传图片:type UploadMode time channelNumber presetPosition packageNumber */ + cJSON_AddStringToObject(req_up_pic, "time", phototime); + cJSON_AddNumberToObject(req_up_pic, "packageNumber", packagenum); + cJSON_AddNumberToObject(req_up_pic, "eventType", 0); + + memset(timebuf, 0, sizeof(timebuf)); + getsystime(timebuf); + + /* data区域结束*/ + cJSON_AddStringToObject(serviceData, "eventTime", timebuf); + cJSON_AddStringToObject(serviceData, "serviceId", "deviceEventInformationUploadFinished"); + + char *sendbuf = cJSON_PrintUnformatted(outdata); + ec20_mqtt_data_send(sendbuf, 0); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + + return 0; +} +#endif +int mqtt_ext_70h(uint8_t *data, uint32_t num) +{ + switch (data[8]) + { + case SOUTH_EXT_RP_EXIT_SLEEP: + break; + case SOUTH_EXT_GET_DEV_RUN_STA: + break; + case SOUTH_EXT_RP_POS: + break; +// case SOUTH_EXT_REPORT_DATA_ALM: +// break; + case SOUTH_EXT_REPORT_WTH_DATA: + break; + case SOUTH_EXT_REPORT_BASIS_DATA: + break; + case SOUTH_EXT_REPORT_HANG_DATA: + break; + case SOUTH_EXT_REPORT_ICE_DATA: + break; + case SOUTH_EXT_REPORT_DEV_INFO: + break; + default: + break; + } + return 0; +} + +int mqtt_send_south_data(uint8_t *data,uint32_t num) +{ + static int sendcnt=0; + static int basesend=0; + int flag = 0; + uint8_t buf[16]; + + switch (data[7]) + { + case SOUTH_ONLINE_NOTIFY: + powerOnContactMessage_Json(); + if(basesend < 1) + { + if (strstr(dev_para.my_dev_info.Model, "MSRDT-A")) + {/* 覆冰设备一启动,立马抓拍一张照片*/ + memset(buf, 0, sizeof(buf)); + buf[7] = 0x83; + buf[9] = 2; + buf[10] = 1; + buf[11] = 0; + south_resp_process(buf,14); + } + + rt_thread_delay(200); + mqtt_base_properties();/*基本信息*/ + rt_thread_delay(100); + mqtt_communicationtechnical_properties();/* 通用技术参数*/ + rt_thread_delay(100); + mqtt_gatewaytechnical_properties();/* 通用网关技术参数多传感器*/ + rt_thread_delay(100); + mqtt_devicetechnical_properties();/* 通用非网关类设备技术参数单一设备*/ + rt_thread_delay(100); + mqtt_solarpower_properties();/* 太阳能电池板*/ + rt_thread_delay(100); + mqtt_longitude_latitude_info();/* 经纬度上报*/ + rt_thread_delay(100); + + if(strstr(dev_para.my_dev_info.Model,"MSRDT-A") != RT_NULL) + { + mqtt_status_Monitoring();/* 状态监测*/ + rt_thread_delay(100); + for(uint8_t idx = 0; idx < 32; idx++){ + if(mqtt_first_pull[idx].pull_max == 0 && + mqtt_first_pull[idx].angle_x_pmax == 0 && + mqtt_first_pull[idx].angle_y_pmax == 0) + { + LOG_I("mqtt first pull data null."); + mqtt_first_pull[0].pull_max = 0xFFFF; + mqtt_first_pull[idx].angle_x_pmax == 0xFFFF; + mqtt_first_pull[idx].angle_y_pmax == 0xFFFF; + mqtt_first_pull_data(0); + rt_thread_delay(100); + break; + } + mqtt_first_pull_data(idx);/* 设备重启后首次发送拉力倾角数据*/ + rt_thread_delay(100); + } + } + mqtt_first_weather_data();/* 设备重启后首次发送气象数据*/ + rt_thread_delay(100); + mqtt_first_unusual_event(); + rt_thread_delay(100); + powerOnContactMessage_Json(); + + //mqtt_dev_upload_file_info(); + south_stat_info.online_flg = 1; + basesend++; + flag = 4; + } + break; + case SOUTH_KEEP_ALIVE: + if(sendcnt < 5) + { + sendcnt++; + //if(sendcnt >= 2) + // ec20_Get_Gps_data(); + + // //flag = 4; + } + mqtt_keep_alive(data, num); + break; + case SOUTH_REQ_UP_PIC: + LOG_D("SOUTH_REQ_UP_PIC mqtt1"); + memset(mqtt_up_pic.picdata, 0, sizeof(mqtt_up_pic.picdata)); + memmove((void*)mqtt_up_pic.picdata, (void*)data, num); + mqtt_up_pic.datalen = num; + iupload_pic = 1; + //mqtt_req_up_pic(data, num); + flag = SOUTH_UP_PIC_DATA; + break; + case SOUTH_UP_PIC_DATA: + //mqtt_upload_image_data(outdata, data, num); + break; + case SOUTH_UP_PIC_END: + //mqtt_upload_image_finished(outdata, data, num); + break; + case SOUTH_UPLOAD_PULL_ANGLE: + mqtt_send_pull_data(data, num); + rt_thread_delay(100); + + mqtt_status_Monitoring();/* 状态监测*/ + rt_thread_delay(100); + flag = 1; + break; + case SOUTH_UPLOAD_WEATHER: + mqtt_send_weather_data(data, num); + rt_thread_delay(100); + mqtt_solarpower_properties();/* 太阳能电池板*/ + rt_thread_delay(100); + mqtt_longitude_latitude_info();/* 经纬度上报*/ + flag = 2; + break; + case SOUTH_UPLOAD_FAULT: + mqtt_device_unusual_event(data, num); + break; + case SOUTH_GET_WORK_PWR_STA: + break; + case SOUTH_EXT_CMD: /* 70H扩展规约*/ + mqtt_ext_70h(data, num); + break; + case SOUTH_DEV_REQ_FILE_UPLOAD: + break; + case SOUTH_FILE_UPLOAD: + break; + case SOUTH_FILE_UPLOAD_END: + break; + case SOUTH_SEND_REPAIR_REQ: + break; + case SOUTH_HOST_GET_UPGRADE_RELT: + break; + case 0xE0: + + flag = 3; + break; + case 0xE1: + + break; + case 0xE2: + break; + case 0xE3:/*基本信息*/ + + flag = 5; + break; + case 0xE5:/* 通用技术参数*/ + + flag = 6; + break; + case 0xE6:/* 通用网关技术参数多传感器*/ + + flag = 7; + break; + case 0xE7:/* 通用非网关类设备技术参数单一设备*/ + + break; + default: + break; + } + return flag; +} + +int mqtt_cjson_analysis(char *in_command, int payload_len) +{ + + RT_ASSERT(in_command); + + cJSON *jsonpayload = cJSON_Parse(in_command); + if(NULL == jsonpayload) + { + LOG_D("cjson_analysis error !"); + return -1; + } + /* 设备刚刚起来,先发送开机联络和基本信息等json*/ + if(0 == south_stat_info.online_flg) + { + LOG_D("south_stat_info.online_flg not!"); + cJSON_Delete(jsonpayload); + return -1; + } + + cJSON *cjdeviceId = cJSON_GetObjectItem(jsonpayload, "deviceId"); + LOG_D("len = %d,deviceId:%s", strlen(cjdeviceId->valuestring), cjdeviceId->valuestring); + if(cJSON_IsString(cjdeviceId)) + { + if(strstr(cjdeviceId->valuestring, DEVICEID)) + { + LOG_D("process command!"); + mqtt_south_recv_process(jsonpayload, payload_len); + } + else + { + LOG_D("deviceID error! recv_deviceID:%s, Correct ID:%s", cjdeviceId->valuestring, DEVICEID); + } + } + cJSON_Delete(jsonpayload); + return 0; +} + +/* mqtt recv json file +{ +"devices": [{ +"deviceld": "D68NZxB4", +"services": [{ +"data": { +"HeartbeatMessageUpload":{ +"time": "20191023T173625Z" +"signalIntensity": "100" +"batteryVoltage": "120" +} +}, +"eventTime": "20191023T173625Z", +"serviceld": "GeneralDataMessageUpload" +}] +}] +} +????dataTopic = "/v1/devices/" + gatewayId + "/command"; +???json +{ +"msgType": "cloudReq", +"mid": 54132, +"cmd": "getRecordState", +"paras": { +"channelNumber": 1 +"channelType": 0 +}, +"serviceId": "PlatformQueryDataCommandLssueService", +"deviceId": "D23pigXo" +} +???????? +????dataTopic = "/v1/devices/" + gatewayId + "/commandResponse"; +???json +{ +"body": { +"channelNumber": 1 +"streamType": 0 +"resultCode": 0 +"recordState": 0 +}, +"errcode": 0, +"mid": 54132, +"msgType": "deviceRsp" +} +*/ +int mqtt_south_recv_process(cJSON *json_buf,uint32_t len) +{ + MQTT_URC_FUNC mqtt_urc_call[] = { + {"platformupgradeDevices",platform_upgrade_devices},/*设备升级(命令只是下发通知,设备回应是否收到通知。设备升级的结果由设备作为属性自行上报)*/ + {"platformLsuueConfigureFile",platform_lsuue_configure_file},/*平台下发参数配置文件*/ + {"getDeviceLogList",get_device_loglist},/*查询终端设备日志列表*/ + {"getDeviceLogFile",get_device_logfile},/*查询终端设备日志文件*/ + {"getDeviceTime",get_device_time},/*平台端查询终端设备的系统时间*/ + {"getTransOnlineMonitConfig",get_transOnlineMonit_Config},/*获取输电在线监测终端的运行状态参数*/ + {"getFunctionConfiguration",get_Fun_config},/*获取装置功能配置*/ + {"getIcingOnlineAlarmMonitConfig",get_alarmMonit_config},/*获取设置覆冰在线监测终端的告警阈*/ + {"getDeviceData",get_device_data},/*请求设备数据*/ + {"getPhotoParams",get_photo_params},/*平台端查询终端设备图片配置信息*/ + {"getCaptureTimesTaskInformation",get_CaptureTimesTask_info},/*平台端查询终端设备定时抓拍任务信息*/ + {"getDeviceSolarPowerProperties",get_solarpower_properties},/*查询设备太阳能电池板状态数据*/ + {"getWeatherMonitoring",get_weather_monitoring},/*查询终端微气象数据*/ + {"getPullAndAngleMonitoring",get_pull_angle_monitoring},/*查询终端拉力倾角监测*/ + {"setDeviceTime",set_device_time},/*平台端设置终端设备的系统时间*/ + {"setTransOnlineMonitConfig",set_online_monit_config},/*设置输电在线监测终端的运行状态参数*/ + {"setFunctionConfiguration",set_Fun_config},/*装置功能配置*/ + {"setIcingOnlineAlarmMonitConfig",set_Icing_alarm_config},/*设置覆冰在线监测终端的告警阈值*/ + {"setPhotoParams",set_photo_params},/*平台端设置终端设备图片配置信息*/ + {"SetAcceptCameraPhoto",set_accept_camera_photo},/*平台端允许终端上送照片*/ + {"setCaptureTimesTaskParameter",set_capture_times},/*平台端设置终端设备定时抓拍任务信息*/ + {"actionRestart",action_restart},/*控制终端重启*/ + {"cameraRemoteControl",camera_remote_control},/*平台下发命令设置控制设备云台*/ + {"manualCapturePicture",manual_capture_picture},/*设置终设备手动抓拍指令*/ + {"getCollectInformation",get_weather_monitoring}, /*查询终端设备的采集信息(气象装置使用)*/ + {"getMicroMeteorologicalAlarm",get_alarmMonit_config}, /*查询终端设备的微气象告警阈值(气象装置使用)*/ + {"setMicroMeteorologicalAlarm",set_Icing_alarm_config}, /*设置终端设备的微气象告警阈值(气象装置使用)*/ + };/* irows*/ + int i=0, mid, irows; + cJSON *serv_command = RT_NULL; + cJSON *cmd_mid = RT_NULL; + + irows = sizeof(mqtt_urc_call)/sizeof(MQTT_URC_FUNC); + LOG_D("rows = %d", irows); + cmd_mid = cJSON_GetObjectItem(json_buf, "mid"); + if(cJSON_IsNumber(cmd_mid)) + { + mid = cmd_mid->valueint; + LOG_D("mid:%d", mid); + } + else + return -1; + if(lastmid == (unsigned int)mid) + { + LOG_D("lastmid=%d, mid=%d", lastmid, mid); + return -1; + } + else + lastmid = mid; + serv_command = cJSON_GetObjectItem(json_buf, "cmd"); + if(RT_NULL == serv_command) + return -1; + LOG_D("cmd_len = %d, cmd:%s", strlen(serv_command->valuestring), serv_command->valuestring); + if(cJSON_IsString(serv_command)) + { + for(i=0; ivaluestring, mqtt_urc_call[i].cmd_name)) + { + LOG_D("cmd_name[%d]:%s", i, mqtt_urc_call[i].cmd_name); + if(NULL != mqtt_urc_call[i].recv_process) + mqtt_urc_call[i].recv_process(json_buf, mid); + break; + } + } + if(i >= irows) + return -2; + } + return 0; +} + +#if 0 +int mqtt_south_recv_process(cJSON *json_buf,uint32_t len) +{ + char *cmd_name[] = { + "platformupgradeDevices", + "platformLsuueConfigureFile", + "getDeviceLogList", + "getDeviceLogFile", + "getDeviceTime", + "getTransOnlineMonitConfig", + "getFunctionConfiguration", + "getIcingOnlineAlarmMonitConfig", + "getDeviceData", + "getPhotoParams", + "getCaptureTimesTaskInformation", + "getDeviceSolarPowerProperties", + "getWeatherMonitoring", + "getPullAndAngleMonitoring", + "setDeviceTime", + "setTransOnlineMonitConfig", + "setFunctionConfiguration", + "setIcingOnlineAlarmMonitConfig", + "setPhotoParams", + "SetAcceptCameraPhoto", + "setCaptureTimesTaskParameter", + "actionRestart", + "cameraRemoteControl", + "manualCapturePicture", + "getCollectInformation", + "getMicroMeteorologicalAlarm", + "setMicroMeteorologicalAlarm", + };/* irows*/ + + int i=0, mid, irows; + cJSON *serv_command = RT_NULL; + cJSON *cmd_mid = RT_NULL; + + irows = sizeof(cmd_name)/sizeof(&cmd_name[0]); + LOG_D("rows = %d", irows); + cmd_mid = cJSON_GetObjectItem(json_buf, "mid"); + if(cJSON_IsNumber(cmd_mid)) + { + mid = cmd_mid->valueint; + LOG_D("mid:%d", mid); + } + else + return -1; + if(lastmid == (unsigned int)mid) + { + LOG_D("lastmid=%d, mid=%d", lastmid, mid); + return -1; + } + else + lastmid = mid; + serv_command = cJSON_GetObjectItem(json_buf, "cmd"); + if(RT_NULL == serv_command) + return -1; + LOG_D("cmd_len = %d, cmd:%s", strlen(serv_command->valuestring), serv_command->valuestring); + if(cJSON_IsString(serv_command)) + { + for(i=0; ivaluestring, cmd_name[i])) + { + LOG_D("cmd_name[%d]:%s", i, cmd_name[i]); + break; + } + } + if(i >= irows) + return -2; + switch (i) + { + case PLATFORM_UPGRADE_DEVICES:/* 设备升级*/ + platform_upgrade_devices(json_buf, mid); + break; + case PLATFORM_ISSUES_CONFIGFILE: + platform_lsuue_configure_file(json_buf, mid); + break; + case GET_DEVICE_LOGLIST: + get_device_loglist(json_buf, mid); + break; + case GET_DEVICE_LOGFILE: + get_device_logfile(json_buf, mid); + break; + case GET_DEVICE_TIME:/* 平台端查询终端设备的系统时间*/ + get_device_time(json_buf, mid); + break; + case GET_TRANS_ONLINE_MONITCONFIG:/* 获取输电在线监测终端的运行状态参数*/ + get_transOnlineMonit_Config(json_buf, mid); + break; + case GET_FUNCTION_CONFIGURATION:/* 获取装置功能配置*/ + get_Fun_config(json_buf, mid); + break; + case GET_ICING_ONLINE_ALARM_MONITCONFIG:/* 获取设置覆冰在线监测终端的告警阈*/ + get_alarmMonit_config(json_buf, mid); + break; + case GET_DEVICE_DATA:/* 请求设备数据*/ + get_device_data(json_buf, mid); + break; + case GET_PHOTO_PARAMS:/* 平台端查询终端设备图片配置信息*/ + get_photo_params(json_buf, mid); + break; + case GET_CAPTURE_TIMES_TASK_INFORMATION:/* 平台端查询终端设备定时抓拍任务信息*/ + get_CaptureTimesTask_info(json_buf, mid); + break; + case GET_DEVICE_SOLAR_POWER_PROPERTIES:/* 查询设备太阳能电池板状态数据*/ + get_solarpower_properties(json_buf, mid); + break; + case GET_WEATHER_MONITORING:/* 查询终端微气象数据*/ + get_weather_monitoring(json_buf, mid); + break; + case GET_PULL_AND_ANGLE_MONITORING:/* 查询终端拉力倾角监测*/ + get_pull_angle_monitoring(json_buf, mid); + break; + case SET_DEVICE_TIME:/* 平台端设置终端设备的系统时间*/ + set_device_time(json_buf, mid); + break; + case SET_TRANS_ONLINE_MONITCONFIG:/* 设置输电在线监测终端的运行状态参数*/ + set_online_monit_config(json_buf, mid); + break; + case SET_FUNCTION_CONFIGURATION: + set_Fun_config(json_buf, mid); + break; + case SET_ICING_ONLINE_ALARM_MONIT_CONFIG: + set_Icing_alarm_config(json_buf, mid); + break; + case SET_PHOTO_PARAMS: + set_photo_params(json_buf, mid); + break; + case SET_ACCEPT_CAMERA_PHOTO: + set_accept_camera_photo(json_buf, mid); + break; + case SET_CAPTURE_TIMES_TASK_PARAMETER: + set_capture_times(json_buf, mid); + break; + case ACTION_RESTART: + action_restart(json_buf, mid); + break; + case CAMERA_REMOTE_CONTROL: + camera_remote_control(json_buf, mid); + break; + case MANUAL_CAPTURE_PICTURE: + manual_capture_picture(json_buf, mid); + break; + case GET_COLLECT_INFORMATION: + break; + case GET_MICRO_METEOROLOGICAL_ALARM: + break; + case SET_MICRO_METEOROLOGICAL_ALARM: + break; + + + + + + + + + + + default: + return -1; + } + } + + //if(RT_NULL == event) +#if 0 + for(i=0;i<(sizeof(south_process_tbl)/sizeof(RESP_FUNC_TABLE));i++) + { + if(south_process_tbl[i].pkt_type==buf[7]) + { + if(south_process_tbl[i].process_func!=NULL) + { + return south_process_tbl[i].process_func(buf,len); + } + } + } +#endif + return -1; +} +#endif + +/* +装置响应: +主题:dataTopic = "/v1/devices/" + gatewayId + "/commandResponse"; +封装json +{ +"body": { +"channelNumber": 1 +"streamType": 0 +"resultCode": 0 +"recordState": 0 +}, +"errcode": 0, +"mid": 54132, +"msgType": "deviceRsp" +} +*/ +/* +对收到的上传请求中的url做如下的处理, +搜索 url 中的 "://"的位置pos1 , 判断pos1, 如果不是4或者5,则出错 如果是4 说明是 http,如果是5 说明是 https +然后从 pos1+3 开始搜索 "/"的位置 pos2 如果 pos2 == -1 则 pos1+3 开始所有的字串都是 domain:port 部分, +path 设置为 "/" ?如果 pos2 != -1 , 则 pos1+3 到 pos2 之间的部分是 ?domain:port, pos2开始到最后的都是 path +这儿获得的path替代 上面header中的 [PATH] +然后搜索 domain:port 是否含 ":" ?如果含,则整个 domain:port 替代 header中的 [HOST] +如果不含,则 如果是http,则 拼接 ":80" 后替代 [HOST] ,如果是https,拼接 ":443" 后替代 [HOST] +GET [URL] HTTP/1.1\r\n +Host: [HOST]\r\n +*/ +//Accept: */*\r\n +/* +User-Agent: QUECTEL_MODULE\r\n +Connection: Keep-Alive\r\n +X-HW-ID: iotcenter.key\r\n +X-HW-APPKEY: Y-@d#u2Suk5+25/R%R$GP0L7s11Pu$!qA1Ay.7.1l357D++t.S-52=.T@K68wG!6\r\n +*/ +int platform_upgrade_devices(cJSON *json_buf, int mid) +{ + // int channelno =0, preset=0; + char temp[256]; + char *strtemp = RT_NULL; + char *urltemp = RT_NULL; + + if(RT_NULL == http_url) + http_url = (char*) malloc(256); + if(RT_NULL == requestheader_url) + requestheader_url = (char*) malloc(256); + if(RT_NULL == host_requestheader) + host_requestheader = (char*) malloc(128); + memset(http_url, 0, 256); + memset(requestheader_url, 0, 256); + memset(host_requestheader, 0, 128); + cJSON *paras = cJSON_GetObjectItem(json_buf, "paras"); + cJSON *datatype = cJSON_GetObjectItem(paras, "channelNumber");/*int 通道号(0表示对装置进行升级,1及以上数值表示对装置接入的通道设备进行升级;)*/ + // channelno = datatype->valueint; + cJSON *actionCommand = cJSON_GetObjectItem(paras, "type");/*int 类型(0:模组升级包1:设备升级包)*/ + // preset = actionCommand->valueint; + actionCommand = cJSON_GetObjectItem(paras, "url");/*string 升级包下载地址*/ + memset(temp, 0, sizeof(temp)); + sprintf(temp, "%s/", actionCommand->valuestring); + LOG_D("url_json:%s", actionCommand->valuestring); + LOG_D("temp:%s", temp); + /* 获取header url*/ + strtemp = strstr(actionCommand->valuestring, "://"); + LOG_D("strtemp:%s", strtemp); + strtemp +=3; /* 跳过"://"*/ + LOG_D("strtemp:%s", strtemp); + urltemp = strstr(strtemp, "/"); + if(urltemp) + { + LOG_D("urltemp:%s", urltemp); + strncpy(host_requestheader, strtemp, urltemp-strtemp);/* 获取header中的HOST*/ + strncpy(requestheader_url, urltemp, strlen(urltemp));/* 获取header中的url(缺文件夹和文件名)*/ + } + else + { + strncpy(host_requestheader, strtemp, strlen(strtemp));/* 获取header中的HOST*/ + } + LOG_D("host_requestheader:%s", host_requestheader); + LOG_D("requestheader_url:%s", requestheader_url); + + actionCommand = cJSON_GetObjectItem(paras, "checkMode");/*int 校验方式(0:MD5,1:CRC16)*/ + actionCommand = cJSON_GetObjectItem(paras, "checkValue");/*string 校验值*/ + cJSON *fileFolder = cJSON_GetObjectItem(paras, "fileFolder");/*string 文件夹名称*/ + cJSON *fileName = cJSON_GetObjectItem(paras, "fileName");/*string 文件名称*/ + sprintf(http_url, "%s%s/%s", temp, fileFolder->valuestring, fileName->valuestring); /* 获取http url完整路径包括文件名*/ + LOG_D("http_url:%s", http_url); + memset(upgrade_devices.filename, 0, sizeof(upgrade_devices.filename)); + sprintf(upgrade_devices.filename, fileName->valuestring); + /* 获取http header url完整路径包括文件名*/ + sprintf(requestheader_url, "%s/%s/%s", requestheader_url, fileFolder->valuestring, fileName->valuestring); + LOG_D("requestheader_url:%s", requestheader_url); + memset(upgrade_devices.start_time, 0, sizeof(upgrade_devices.start_time)); + getsystime(upgrade_devices.start_time); + + cJSON *cmd_req = cJSON_CreateObject(); + + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "resultCode", 0); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + //rt_thread_delay(1000); + /* 标记可以下载文件 */ + upgrade_devices.state = 1; + rt_thread_delay(1000); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + +#if 0 + rt_thread_delay(15000); + /* 设备升级更新信息上报*/ + cmd_req = cJSON_CreateObject(); + deviceUpgradeInfoUpload(cmd_req); + cJSON_Delete(cmd_req); + cmd_req = RT_NULL; +#endif + return 0; +} + +int platform_lsuue_configure_file(cJSON *json_buf, int mid) +{ + //int preset=0; + char temp[256]/*, *http_url=RT_NULL*/; + //char *requestheader_url=RT_NULL, *host_requestheader=RT_NULL; + char *strtemp = RT_NULL; + char *urltemp = RT_NULL; + + if(RT_NULL == http_url) + http_url = (char*) malloc(256); + if(RT_NULL == requestheader_url) + requestheader_url = (char*) malloc(256); + if(RT_NULL == host_requestheader) + host_requestheader = (char*) malloc(128); + memset(http_url, 0, 256); + memset(requestheader_url, 0, 256); + memset(host_requestheader, 0, 128); + + cJSON *paras = cJSON_GetObjectItem(json_buf, "paras"); + cJSON *actionCommand = cJSON_GetObjectItem(paras, "type");/* 类型(0:模组参数;1:设备参数)*/ + //preset = actionCommand->valueint; + actionCommand = cJSON_GetObjectItem(paras, "checkMode"); + actionCommand = cJSON_GetObjectItem(paras, "url"); + memset(temp, 0, sizeof(temp)); + sprintf(temp, "%s/", actionCommand->valuestring); + LOG_D("url_json:%s", actionCommand->valuestring); + LOG_D("temp:%s", temp); + /* 获取header url*/ + strtemp = strstr(actionCommand->valuestring, "://"); + LOG_D("strtemp:%s", strtemp); + strtemp +=3; /* 跳过"://"*/ + LOG_D("strtemp:%s", strtemp); + urltemp = strstr(strtemp, "/"); + if(urltemp) + { + LOG_D("urltemp:%s", urltemp); + strncpy(host_requestheader, strtemp, urltemp-strtemp);/* 获取header中的HOST*/ + strncpy(requestheader_url, urltemp, strlen(urltemp));/* 获取header中的url(缺文件夹和文件名)*/ + } + else + { + strncpy(host_requestheader, strtemp, strlen(strtemp));/* 获取header中的HOST*/ + } + LOG_D("host_requestheader:%s", host_requestheader); + LOG_D("requestheader_url:%s", requestheader_url); + + cJSON *fileFolder = cJSON_GetObjectItem(paras, "fileFolder");/*string 文件夹名称*/ + cJSON *fileName = cJSON_GetObjectItem(paras, "fileName");/*string 文件名称*/ + sprintf(http_url, "%s%s/%s", temp, fileFolder->valuestring, fileName->valuestring); /* 获取http url完整路径包括文件名*/ + LOG_D("http_url:%s", http_url); + /* 获取http header url完整路径包括文件名*/ + sprintf(requestheader_url, "%s/%s/%s", requestheader_url, fileFolder->valuestring, fileName->valuestring); + LOG_D("requestheader_url:%s", requestheader_url); + + cJSON *cmd_req = cJSON_CreateObject(); + + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "resultCode", 0); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + upgrade_devices.uconfig = 1; + saveconfigstate(); + // rt_thread_delay(1000); + /* 下载文件*/ + // ec20_http_data_download(http_url, requestheader_url, host_requestheader); + // rt_thread_delay(1000); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + + // rt_thread_delay(15000); + // cmd_req = cJSON_CreateObject(); + // deviceUpgradeInfoUpload(cmd_req); + // cJSON_Delete(cmd_req); + // cmd_req = RT_NULL; + return 0; +} + +int get_device_loglist(cJSON *json_buf, int mid) +{ + int total_size; + char timebuf[64], filename[] = LOG_FILE_NAME; + + cJSON *paras = cJSON_GetObjectItem(json_buf, "paras"); + cJSON *logFileEndTime = cJSON_GetObjectItem(paras, "logFileEndTime"); + //cJSON *actionCommand = cJSON_GetObjectItem(paras, "type"); + + cJSON *cmd_req = cJSON_CreateObject(); + + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "resultCode", 0); + cJSON_AddNumberToObject(req_body, "fileCount", 1); /*文件个数*/ + + cJSON* fileList = cJSON_CreateArray(); + cJSON_AddItemToObject(req_body, "fileList", fileList); /*文件列表信息*/ + cJSON *fileinfor= cJSON_CreateObject(); + cJSON_AddItemToArray(fileList,fileinfor); + cJSON_AddNumberToObject(fileinfor, "fileType", 2); /*文件类型:2,日志文件*/ + cJSON_AddStringToObject(fileinfor, "fileName", LOG_FILE_NAME); /*文件名*/ + memset(timebuf, 0, sizeof(timebuf)); + getsystime(timebuf); + cJSON_AddStringToObject(fileinfor, "time", timebuf); /*文件生成时间*/ + total_size = south_stat_file(filename); + cJSON_AddNumberToObject(fileinfor, "fileSize", total_size); /*文件大小*/ + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + return 0; +} + +int get_device_logfile(cJSON *json_buf, int mid) +{ + //MQTT_FILE_INFO_TYPE* fileifno; + //int size = sizeof(MQTT_FILE_INFO_TYPE); // 计算结构体大小 + //MQTT_FILE_INFO_TYPE* fileInfo = (MQTT_FILE_INFO_TYPE*) malloc(size); // 分配内存 + + cJSON *paras = cJSON_GetObjectItem(json_buf, "paras"); + cJSON *logFileEndTime = cJSON_GetObjectItem(paras, "logFileName"); + //strncpy(fileInfo->file, LOG_FILE_NAME, 60); + cJSON *actionCommand = cJSON_GetObjectItem(paras, "type"); + + /* 回复json*/ + cJSON *cmd_req = cJSON_CreateObject(); + + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "resultCode", 0); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + rt_thread_delay(300); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + + iupload_file = 1; + //mqtt_dev_upload_file_info(); + //free(fileInfo); + //fileInfo = RT_NULL; + return 0; +} + +#if 0 +int test_mqtt_upload_logfile() +{ + /* 回复json*/ + cJSON *cmd_req = cJSON_CreateObject(); + + cJSON *req_body = cJSON_CreateObject(); /* 空*/ + cJSON_AddItemToObject(cmd_req, "body", req_body); + cJSON_AddNumberToObject(req_body, "resultCode", 0); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + // cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + ec20_mqtt_data_send(sendbuf, 1); + rt_thread_delay(300); + if (sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + + mqtt_dev_upload_file_info(); + + return 0; +} +MSH_CMD_EXPORT(test_mqtt_upload_logfile, mqtt update log file); +#endif + +int get_device_time(cJSON *json_buf, int mid) +{ + cJSON *cmd_req = cJSON_CreateObject(); + char tempbuf[64]; + + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "timingType", 0); + memset(tempbuf, 0, sizeof(tempbuf)); + getsystime(tempbuf); + cJSON_AddStringToObject(req_body, "currentTime", tempbuf); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + LOG_D("send"); + //rt_thread_delay(2000); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + LOG_D("addNode len = %d", strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + return 0; +} + +int get_transOnlineMonit_Config(cJSON *json_buf, int mid) +{ + char hostip[64]; + + memset(hostip, 0, sizeof(hostip)); + hostip_to_char(hostip); + cJSON *cmd_req = cJSON_CreateObject(); + + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "heartbeatInterval", dev_para.keep_alive_intv[0]); + cJSON_AddNumberToObject(req_body, "sampleInterval", dev_para.sample_para.Main_Time); + cJSON_AddNumberToObject(req_body, "sleepDuration", dev_para.sleep_time); + cJSON_AddNumberToObject(req_body, "onlineInterval", dev_para.work_time); + cJSON *reboot_time = cJSON_CreateObject(); + cJSON_AddItemToObject(req_body,"restartTime",reboot_time); + + int days = dev_para.reset_intv/24; + int hours = dev_para.reset_intv%24; + int minutes = dev_para.reset_minutes; + + cJSON_AddNumberToObject(reboot_time, "day", days); + cJSON_AddNumberToObject(reboot_time, "hour", hours); + cJSON_AddNumberToObject(reboot_time, "minute", minutes); + cJSON_AddStringToObject(req_body, "ipAddr", hostip/*mqttip inet_ntoa(addr_ip)*/); + cJSON_AddNumberToObject(req_body, "port", dev_para.server.host_port); + cJSON_AddStringToObject(req_body, "httpIp", httpip); + cJSON_AddNumberToObject(req_body, "httpPort", httpport); + cJSON_AddNumberToObject(req_body, "PwrMode", 1); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + return 0; +} + +int get_Fun_config(cJSON *json_buf, int mid) +{ + int i=0, buf[4][2]; + char tempbuf[32]; + + cJSON *cmd_req = cJSON_CreateObject(); + + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON* fun_configure = cJSON_CreateArray(); + cJSON_AddItemToObject(req_body, "configure", fun_configure); + memset(tempbuf, 0,sizeof(tempbuf)); + //memset(buf, 0,sizeof(buf)); + //拉力倾角22H + buf[0][0]=0x22; + buf[0][1] = (dev_para.func_tbl&INS_FUNC_ENABLE) > 0 ? 1 : 0; + + //气象25H + buf[1][0]=0x25; + buf[1][1] = (dev_para.func_tbl&WTH_FUNC_ENABLE) > 0 ? 1 : 0; + + //故障告警30H + buf[2][0]=0x30; + buf[2][1] = ((dev_para.func_tbl&INS_FLT_ENABLE) || + (dev_para.func_tbl&WTH_FLT_ENABLE) || + (dev_para.func_tbl&INC_FLT_ENABLE) || + (dev_para.func_tbl&PIC_FLT_ENABLE) || + (dev_para.func_tbl&ICE_FLT_ENABLE)) > 0 ? 1 : 0; + + //图像84H + buf[3][0]=0x84; + buf[3][1]=(dev_para.func_tbl&PIC_FUNC_ENABLE) > 0 ? 1 : 0; + + for(i=0; i<4; i++) + { + if(0 == buf[i][1]) + continue; + cJSON *fun = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToArray(fun_configure,fun); + sprintf(tempbuf, "%0XH", buf[i][0]); + cJSON_AddStringToObject(fun, "function", tempbuf); + cJSON_AddNumberToObject(fun, "mode", buf[i][1]); + } + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + return 0; +} + +int get_alarmMonit_config(cJSON *json_buf, int mid) +{ + cJSON *serv_command = cJSON_GetObjectItem(json_buf, "cmd"); + + cJSON *cmd_req = cJSON_CreateObject(); + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "resultCode", 0); + if(serv_command) + { + char buffer[10]; + double wind_speed = 0.0; + wind_speed = (dev_para.wth_alm_para_ex.wind_speed_alm * 1.0)/100.0; //保留两位小数 /100 还原数据 + memset(buffer, 0, sizeof(buffer)); + sprintf(buffer, "%.2f", wind_speed); + wind_speed = atof(buffer); + + double rain_alm = 0.00; + rain_alm = (dev_para.wth_alm_para_ex.rain_alm * 1.0)/100.0; //保留两位小数 /100 还原数据 + memset(buffer, 0, sizeof(buffer)); + sprintf(buffer, "%.2f", rain_alm); + rain_alm = atof(buffer); + + double pull_alm = 0.00; + pull_alm = (dev_para.wth_alm_para_ex.pull_alm * 1.0)/100.0; //保留两位小数 /100 还原数据 + memset(buffer, 0, sizeof(buffer)); + sprintf(buffer, "%.2f", pull_alm); + pull_alm = atof(buffer); + if(strstr(serv_command->valuestring, "getIcingOnlineAlarmMonitConfig")) + { + cJSON_AddNumberToObject(req_body, "interval", dev_para.wth_alm_para_ex.alm_trig_intv); + cJSON_AddNumberToObject(req_body, "PullAAlarmThreshold", pull_alm); + cJSON_AddNumberToObject(req_body, "windAlarmThreshold", wind_speed); + cJSON_AddNumberToObject(req_body, "rainAlarmThreshold", rain_alm); + } + else + { + cJSON_AddNumberToObject(req_body, "interval", dev_para.wth_alm_para_ex.alm_trig_intv); + cJSON_AddNumberToObject(req_body, "level", weatherlevel); //南网协议 1CH 没有 + cJSON_AddNumberToObject(req_body, "windSpeedAlarm", wind_speed); + cJSON_AddNumberToObject(req_body, "precipitationAlarm", rain_alm); + } + } + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + return 0; +} + +int get_device_data(cJSON *json_buf, int mid) +{ + int sendtype =0; + uint8_t buf[32]; + + cJSON *paras = cJSON_GetObjectItem(json_buf, "paras"); + cJSON *datatype = cJSON_GetObjectItem(paras, "dataType"); + cJSON *amount = cJSON_GetObjectItem(paras, "amount"); + int backtrackNum = datatype->valueint; + //if(backtrackNum == 0){ //采集新的一帧数据发送 + if(cJSON_IsString(datatype)) + { + if(strstr(datatype->valuestring, "22H")) + sendtype = 1; + else if(strstr(datatype->valuestring, "25H")) + sendtype = 2; + else + sendtype = 3; + } + else + return -1; + + cJSON *cmd_req = cJSON_CreateObject(); + + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "resultCode", 0); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + memset(buf, 0, sizeof(buf)); + LOG_D("get_device_data, sendType:%d", sendtype); + switch (sendtype) + { + //case 0: + // break; + case 1: + send_smp_request(CMD_READ_INS,time(NULL)); + break; + case 2: + send_smp_request(CMD_READ_WTH,time(NULL)); + break; + default: + send_smp_request(CMD_READ_ALL,time(NULL)); + break; + } + return 0; + // }else{ //回溯第 backtrackNum 条数据发送 + // //TODO + // return 0; + // } + + //return -1; +} + +int get_photo_params(cJSON *json_buf, int mid) +{ + int channelno =0; + + cJSON *paras = cJSON_GetObjectItem(json_buf, "paras"); + cJSON *datatype = cJSON_GetObjectItem(paras, "channel"); + if(cJSON_IsNumber(datatype)) + { + channelno = datatype->valueint; + } + else + return -1; + + cJSON *cmd_req = cJSON_CreateObject(); + + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "channel", channelno); + cJSON_AddNumberToObject(req_body, "color", dev_para.video_run_para[channelno-1].color); + cJSON_AddNumberToObject(req_body, "brightness", dev_para.video_run_para[channelno-1].luminance); + cJSON_AddNumberToObject(req_body, "contrast", dev_para.video_run_para[channelno-1].contrast); + cJSON_AddNumberToObject(req_body, "saturation", dev_para.video_run_para[channelno-1].saturation); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + return 0; +} + +int get_CaptureTimesTask_info(cJSON *json_buf, int mid) +{ + int channelno =0, i=0, photo_timenum; + cJSON *timelist = RT_NULL; + + cJSON *paras = cJSON_GetObjectItem(json_buf, "paras"); + cJSON *datatype = cJSON_GetObjectItem(paras, "channelNumber"); + channelno = datatype->valueint; +// if((channelno < 1) || (channelno > 2)) +// return -1; + + cJSON *cmd_req = cJSON_CreateObject(); + + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "channel", channelno); + photo_timenum = dev_para.video_run_para[channelno-1].num_work_item; + cJSON_AddNumberToObject(req_body, "groupNumber", photo_timenum); + cJSON* paramArray = cJSON_CreateArray(); + cJSON_AddItemToObject(req_body, "paramArray", paramArray); //拍照时间表数组 + // 拍照时间表JSON数组 + for(i=0; ivaluestring, "getWeatherMonitoring")) + cJSON_AddNumberToObject(dataList, "Temperature", INVALID_VALUE); /*温度*/ + else + cJSON_AddNumberToObject(dataList, "temperature", INVALID_VALUE); /*温度*/ + } + else{ + cJSON_AddNumberToObject(dataList, "Temperature", INVALID_VALUE); /*温度*/ + } + + cJSON_AddNumberToObject(dataList, "humidity", INVALID_VALUE); /*湿度*/ + cJSON_AddNumberToObject(dataList, "airPressure", INVALID_VALUE); /*气压*/ + cJSON_AddNumberToObject(dataList, "instanWindSpeed", INVALID_VALUE); /*瞬时风速*/ + cJSON_AddNumberToObject(dataList, "aveWindSspeed1", INVALID_VALUE); /*1分钟平均风速*/ + cJSON_AddNumberToObject(dataList, "aveWindSspeed10", INVALID_VALUE); /*10分钟平均风速*/ + cJSON_AddNumberToObject(dataList, "maxWindSpeed10", INVALID_VALUE); /*分钟*/ + cJSON_AddNumberToObject(dataList, "aveMaxWindSpeed10", INVALID_VALUE); /*10分钟平均最大风速*/ + cJSON_AddNumberToObject(dataList, "extremeWindSpeed", INVALID_VALUE); /*极大风速*/ + cJSON_AddNumberToObject(dataList, "instanWindDirection", INVALID_VALUE); /*分钟*/ + cJSON_AddNumberToObject(dataList, "aveWindDirection1", INVALID_VALUE); /*分钟*/ + cJSON_AddNumberToObject(dataList, "aveWindDirection10", INVALID_VALUE); /*分钟*/ + cJSON_AddNumberToObject(dataList, "radiationIntensity", INVALID_VALUE); /*分钟*/ + cJSON_AddNumberToObject(dataList, "precipitation1", INVALID_VALUE); /*分钟*/ + cJSON_AddNumberToObject(dataList, "precipitation6", INVALID_VALUE); /*分钟*/ + cJSON_AddNumberToObject(dataList, "precipitation12", INVALID_VALUE); /*分钟*/ + cJSON_AddNumberToObject(dataList, "precipitation24", INVALID_VALUE); /*分钟*/ + }else{ + tmp = (float)(mqtt_first_wth.temp - 500) /10; + if(serv_command) + { + if(strstr(serv_command->valuestring, "getWeatherMonitoring")) + cJSON_AddNumberToObject(dataList, "Temperature", format_two_decimal_digits(tmp)); /*温度*/ + else + cJSON_AddNumberToObject(dataList, "temperature", format_two_decimal_digits(tmp)); /*温度*/ + } + else{ + cJSON_AddNumberToObject(dataList, "Temperature", format_two_decimal_digits(tmp)); /*温度*/ + } + + cJSON_AddNumberToObject(dataList, "humidity", format_two_decimal_digits((float)mqtt_first_wth.humi)); /*湿度*/ + cJSON_AddNumberToObject(dataList, "airPressure", format_two_decimal_digits((float)mqtt_first_wth.pres)); /*气压*/ + cJSON_AddNumberToObject(dataList, "instanWindSpeed", format_two_decimal_digits((float)mqtt_first_wth.wind_speed / 10)); /*瞬时风速*/ + cJSON_AddNumberToObject(dataList, "aveWindSspeed1", format_two_decimal_digits((float)mqtt_first_wth.wind_speed_1 / 10)); /*1分钟平均风速*/ + cJSON_AddNumberToObject(dataList, "aveWindSspeed10", format_two_decimal_digits((float)mqtt_first_wth.wind_speed_10 / 10)); /*10分钟平均风速*/ + cJSON_AddNumberToObject(dataList, "maxWindSpeed10", format_two_decimal_digits((float)mqtt_first_wth.wind_speed_max / 10)); /*分钟*/ + cJSON_AddNumberToObject(dataList, "aveMaxWindSpeed10", format_two_decimal_digits((float)mqtt_first_wth.wind_speed_10 / 10)); /*10分钟平均最大风速*/ + cJSON_AddNumberToObject(dataList, "extremeWindSpeed", format_two_decimal_digits((float)mqtt_first_wth.wind_speed_max / 10)); /*极大风速*/ + cJSON_AddNumberToObject(dataList, "instanWindDirection", format_two_decimal_digits((float)mqtt_first_wth.wind_dir)); /*分钟*/ + cJSON_AddNumberToObject(dataList, "aveWindDirection1", format_two_decimal_digits((float)mqtt_first_wth.wind_dir_1)); /*分钟*/ + cJSON_AddNumberToObject(dataList, "aveWindDirection10", format_two_decimal_digits((float)mqtt_first_wth.wind_dir_10)); /*分钟*/ + cJSON_AddNumberToObject(dataList, "radiationIntensity", format_two_decimal_digits((float)mqtt_first_wth.sunshine)); /*分钟*/ + + if(mqtt_first_wth.rainfall == INVALID_VALUE){ + cJSON_AddNumberToObject(dataList, "precipitation1", INVALID_VALUE); /*分钟*/ + cJSON_AddNumberToObject(dataList, "precipitation6", INVALID_VALUE); /*分钟*/ + cJSON_AddNumberToObject(dataList, "precipitation12", INVALID_VALUE); /*分钟*/ + cJSON_AddNumberToObject(dataList, "precipitation24", INVALID_VALUE); /*分钟*/ + }else{ + tmp = (float)mqtt_first_wth.rainfall / 100; + cJSON_AddNumberToObject(dataList, "precipitation1", format_two_decimal_digits(tmp)); /*分钟*/ + cJSON_AddNumberToObject(dataList, "precipitation6", format_two_decimal_digits(tmp * 1.02)); /*分钟*/ + cJSON_AddNumberToObject(dataList, "precipitation12", format_two_decimal_digits(tmp * 1.04)); /*分钟*/ + cJSON_AddNumberToObject(dataList, "precipitation24", format_two_decimal_digits(tmp * 1.06)); /*分钟*/ + } + } + } + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + LOG_D("pull_send:%s", sendbuf); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + return 0; +} + +int get_pull_angle_monitoring(cJSON *json_buf, int mid) +{ + char tempbuf[64]; + int16_t tmp_num; + float maxPullOblique, maxPullWind, maxPull; + + for(uint8_t idx = 0; idx < 32; idx++){ + if(mqtt_first_pull[idx].pull_max == 0 && + mqtt_first_pull[idx].angle_x_pmax == 0 && + mqtt_first_pull[idx].angle_y_pmax == 0) + { + LOG_I("mqtt pull data null."); + break; + } + + cJSON *cmd_req = cJSON_CreateObject(); + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "resultCode", 0); + + uint8_t count = mqtt_first_pull[idx].sensor_num; + uint8_t type = 0; + if (count < 8) type = 1;//A相0x1x + else if (count < 16) type = 2;//B相0x2x + else if (count < 24) type = 3;//C相0x3x + else if (count < 28) type = 4;//地线0x4x //暂时写为左地线 + else if (count < 32) type = 8;//光缆0x5x //暂时写为左光缆 + + cJSON_AddNumberToObject(req_body, "type", type); + cJSON_AddNumberToObject(req_body, "number", mqtt_first_pull[idx].sensor_num); + cJSON* paramArray = cJSON_CreateArray(); + cJSON_AddItemToObject(req_body, "dataList", paramArray); //拍照时间表数组 + // + { + cJSON* dataList = cJSON_CreateObject(); + cJSON_AddItemToArray(paramArray,dataList); + memset(tempbuf, 0, sizeof(tempbuf)); + getsystime(tempbuf); + cJSON_AddStringToObject(dataList, "time", tempbuf); /*预置位(取值:0~255, 默认值:0)*/ + tmp_num = mqtt_first_pull[idx].pull_max; + if(tmp_num == -1){ + cJSON_AddNumberToObject(dataList, "maxPull", INVALID_VALUE); /*小时*/ + }else{ + cJSON_AddNumberToObject(dataList, "maxPull", format_two_decimal_digits(tmp_num)); /*小时*/ + } + + if( (-1 == mqtt_first_pull[idx].angle_y_pmax) && (-1 == mqtt_first_pull[idx].angle_x_pmax) ){ + cJSON_AddNumberToObject(dataList, "maxPullOblique", INVALID_VALUE); /*分钟*/ + cJSON_AddNumberToObject(dataList, "maxPullWind", INVALID_VALUE); /*分钟*/ + }else{ + maxPullOblique = mqtt_first_pull[idx].angle_y_pmax/100.00; + maxPullWind = mqtt_first_pull[idx].angle_x_pmax/100.00; + cJSON_AddNumberToObject(dataList, "maxPullOblique", format_two_decimal_digits(maxPullOblique)); /*分钟*/ + cJSON_AddNumberToObject(dataList, "maxPullWind", format_two_decimal_digits(maxPullWind)); /*分钟*/ + } + // cJSON_AddNumberToObject(dataList, "LineAngle", 2.58); /*分钟*/ + //cJSON_AddNumberToObject(dataList, "LineAngle", 13.56); /*分钟*/ + // cJSON_AddNumberToObject(dataList, "equalIceThickness", 0); /*分钟*/ + } + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid++); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + LOG_D("pull_send:%s", sendbuf); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + rt_thread_delay(500); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + } + return 0; +} + +int mqtt_time_set(char *hostsettime) +{ + struct tm tm_new; + time_t t; + char tmpbuf[12]; + int i; + + //strptime("20191023T173625Z", "%Y%m%dT%H%M%SZ", &tm_new); + memset(tmpbuf, 0, sizeof(tmpbuf)); + strncpy(tmpbuf, hostsettime+i, 4); + tm_new.tm_year = atoi(tmpbuf)+100; + i +=4; + memset(tmpbuf, 0, sizeof(tmpbuf)); + strncpy(tmpbuf, hostsettime+i, 2); + tm_new.tm_mon = atoi(tmpbuf)-1; + i+=2; + strncpy(tmpbuf, hostsettime+i, 2); + tm_new.tm_mday = atoi(tmpbuf); + i+=3; + strncpy(tmpbuf, hostsettime+i, 2); + tm_new.tm_hour = atoi(tmpbuf); + i+=2; + strncpy(tmpbuf, hostsettime+i, 2); + tm_new.tm_min = atoi(tmpbuf); + i+=2; + strncpy(tmpbuf, hostsettime+i, 2); + tm_new.tm_sec = atoi(tmpbuf); + + t=mktime(&tm_new); + south_set_time(t); + return 0; +} + +int set_device_time(cJSON *json_buf, int mid) +{ + char *hostsettime = RT_NULL; + int resultCode = 0; + + cJSON *paras = cJSON_GetObjectItem(json_buf, "paras"); + cJSON *devtime = cJSON_GetObjectItem(paras, "time"); + if(cJSON_IsString(devtime)) + { + hostsettime = devtime->valuestring; + } + else + resultCode = 1; + if(0 == resultCode) + { + host_set_devices_time(hostsettime); + } + + cJSON *cmd_req = cJSON_CreateObject(); + + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "resultCode", resultCode); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + return 0; +} + +uint32_t hostip_char_to_int(char *hostip) +{ + uint32_t ipaddr, ip; + char *temp, *tmp; + + temp = strstr(hostip, "."); + *temp = 0; + temp++; + ip = atoi(hostip); + ipaddr = ip; + + tmp = strstr(temp, "."); + *tmp = 0; + tmp++; + ip = atoi(temp); + ipaddr += (ip << 8); + + temp = strstr(tmp, "."); + *temp = 0; + temp++; + ip = atoi(tmp); + ipaddr += (ip << 16); + + ip = atoi(temp); + ipaddr += (ip << 24); + LOG_D("ipaddr:%08X %d.%d.%d.%d", ipaddr, ipaddr/(256*256*256), ipaddr/(256*256)%256, ipaddr/256%(256*256), ipaddr%(256*256*256)); + return ipaddr; +} + +int set_online_monit_config(cJSON *json_buf, int mid) +{ + + cJSON *paras = cJSON_GetObjectItem(json_buf, "paras"); + cJSON *keep_alive = cJSON_GetObjectItem(paras, "heartbeatInterval"); + dev_para.keep_alive_intv[0] = keep_alive->valueint; + keep_alive = cJSON_GetObjectItem(paras, "sampleInterval"); + dev_para.sample_para.Main_Time = keep_alive->valueint; + keep_alive = cJSON_GetObjectItem(paras, "sleepDuration"); + dev_para.sleep_time = keep_alive->valueint; + keep_alive = cJSON_GetObjectItem(paras, "onlineInterval"); + dev_para.work_time = keep_alive->valueint; + + int day, hour, minute; + cJSON *restartTime = cJSON_GetObjectItem(paras, "restartTime"); + keep_alive = cJSON_GetObjectItem(restartTime, "day"); + day = keep_alive->valueint; + keep_alive = cJSON_GetObjectItem(restartTime, "hour"); + hour = keep_alive->valueint; + keep_alive = cJSON_GetObjectItem(restartTime, "minute"); + minute= keep_alive->valueint; + + //int minutesInHours = minute / 60; + // 总小时数 = 天数 * 24 + 已知小时数 + 分钟转换的小时数 + dev_para.reset_intv = day * 24 + hour; + dev_para.reset_minutes = minute; + + keep_alive = cJSON_GetObjectItem(paras, "port"); + dev_para.server.host_port = keep_alive->valueint; + keep_alive = cJSON_GetObjectItem(paras, "httpPort"); + httpport = keep_alive->valueint; + + keep_alive = cJSON_GetObjectItem(paras, "httpIp"); + strncpy(httpip, keep_alive->valuestring, sizeof(httpip)); + keep_alive = cJSON_GetObjectItem(paras, "ipAddr"); + //strncpy(, keep_alive->valuestring, sizeof(httpip)); + dev_para.server.host_ip = hostip_char_to_int(keep_alive->valuestring); + //inet_pton(AF_INET, keep_alive->valuestring, &dev_para.server.host_ip); + save_para(); + + cJSON *cmd_req = cJSON_CreateObject(); + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "resultCode", 0); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + return 0; +} + +int set_Fun_config(cJSON *json_buf, int mid) +{ + int i=0, tmp=0; + cJSON *keep_alive = RT_NULL; + cJSON *function = RT_NULL; + cJSON *funmode = RT_NULL; + + cJSON *paras = cJSON_GetObjectItem(json_buf, "paras"); + cJSON *restartTime = cJSON_GetObjectItem(paras, "configure"); + int array_size = cJSON_GetArraySize(restartTime); + for(i=0; ivaluestring, funmode->valueint); + if(1 == funmode->valueint) + { + if(strstr(function->valuestring, "22H")){ + dev_para.func_tbl |= INS_FUNC_ENABLE; + } + if(strstr(function->valuestring, "25H")){ + dev_para.func_tbl |= WTH_FUNC_ENABLE; + } + if(strstr(function->valuestring, "30H")){ + dev_para.func_tbl |= INS_FLT_ENABLE; + dev_para.func_tbl |= WTH_FLT_ENABLE; + dev_para.func_tbl |= INC_FLT_ENABLE; + dev_para.func_tbl |= PIC_FLT_ENABLE; + dev_para.func_tbl |= ICE_FLT_ENABLE; + } + if(strstr(function->valuestring, "84H")){ + dev_para.func_tbl |=PIC_FUNC_ENABLE; + } + }else if(0 == funmode->valueint){ + if(strstr(function->valuestring, "22H")){ + dev_para.func_tbl &= (~INS_FUNC_ENABLE); + } + if(strstr(function->valuestring, "25H")){ + dev_para.func_tbl &= (~WTH_FUNC_ENABLE); + } + if(strstr(function->valuestring, "30H")){ + dev_para.func_tbl &= (~INS_FLT_ENABLE); + dev_para.func_tbl &= (~WTH_FLT_ENABLE); + dev_para.func_tbl &= (~INC_FLT_ENABLE); + dev_para.func_tbl &= (~PIC_FLT_ENABLE); + dev_para.func_tbl &= (~ICE_FLT_ENABLE); + } + if(strstr(function->valuestring, "84H")){ + dev_para.func_tbl &= (~PIC_FUNC_ENABLE); + } + } + } + //dev_para.func_tbl=(dev_para.func_tbl&0xFFC0)|tmp; + LOG_D("func_tbl:%02X tmp=%04x", dev_para.func_tbl, tmp); + save_para(); + + cJSON *cmd_req = cJSON_CreateObject(); + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "resultCode", 0); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + return 0; +} + +int set_Icing_alarm_config(cJSON *json_buf, int mid) +{ + int result = 0; + cJSON *keep_alive=RT_NULL; + + cJSON *serv_command = cJSON_GetObjectItem(json_buf, "cmd"); + cJSON *paras = cJSON_GetObjectItem(json_buf, "paras"); + cJSON *password = cJSON_GetObjectItem(paras, "password"); + + cJSON *interval = cJSON_GetObjectItem(paras, "interval");/*触发告警间隔(分)*/ + dev_para.wth_alm_para_ex.alm_trig_intv = interval->valueint; + if(strstr(serv_command->valuestring, "setIcingOnlineAlarmMonitConfig")) + { + keep_alive = cJSON_GetObjectItem(paras, "PullAAlarmThreshold");/*覆冰告警阈值(保留小数点后两位)*/ + //dev_para.sample_para.Main_Time = keep_alive->valueint; + dev_para.wth_alm_para_ex.pull_alm = keep_alive->valuedouble * 100; //保留两位小数 *100 存储小数位 + keep_alive = cJSON_GetObjectItem(paras, "windAlarmThreshold");/*风速告警阈值(保留小数点后两位)*/ + dev_para.wth_alm_para_ex.wind_speed_alm = keep_alive->valuedouble * 100; //保留两位小数 *100 存储小数位 + //dev_para.sleep_time = keep_alive->valueint; + keep_alive = cJSON_GetObjectItem(paras, "rainAlarmThreshold");/*雨量告警阈值(保留小数点后两位)*/ + //dev_para.work_time = keep_alive->valueint; + dev_para.wth_alm_para_ex.rain_alm = keep_alive->valuedouble * 100; //保留两位小数 *100 存储小数位 + } + else + { + keep_alive = cJSON_GetObjectItem(paras, "level"); /* 级别*/ + weatherlevel = keep_alive->valueint; + keep_alive = cJSON_GetObjectItem(paras, "interval"); /* 级别*/ + dev_para.wth_alm_para_ex.pull_alm = keep_alive->valuedouble * 100; //保留两位小数 *100 存储小数位 + keep_alive = cJSON_GetObjectItem(paras, "windSpeedAlarm");/*风速告警阈值*/ + dev_para.wth_alm_para_ex.wind_speed_alm = keep_alive->valuedouble * 100; + keep_alive = cJSON_GetObjectItem(paras, "precipitationAlarm");/*降水量告警阈值*/ + dev_para.wth_alm_para_ex.rain_alm = keep_alive->valuedouble * 100; + } + save_para(); + + cJSON *cmd_req = cJSON_CreateObject(); + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "resultCode", result); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + return 0; +} + +int set_photo_params(cJSON *json_buf, int mid) +{ + int channelno =0; + + cJSON *paras = cJSON_GetObjectItem(json_buf, "paras"); + cJSON *datatype = cJSON_GetObjectItem(paras, "channel"); + if(cJSON_IsNumber(datatype)) + { + channelno = datatype->valueint; + } + else + return -1; + cJSON *keep_alive = cJSON_GetObjectItem(paras, "color"); + dev_para.video_run_para[channelno-1].color = keep_alive->valueint; + keep_alive = cJSON_GetObjectItem(paras, "brightness"); + dev_para.video_run_para[channelno-1].luminance = keep_alive->valueint; + keep_alive = cJSON_GetObjectItem(paras, "contrast"); + dev_para.video_run_para[channelno-1].contrast = keep_alive->valueint; + keep_alive = cJSON_GetObjectItem(paras, "saturation"); + dev_para.video_run_para[channelno-1].saturation = keep_alive->valueint; + + cJSON *cmd_req = cJSON_CreateObject(); + + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "resultCode", 0); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + return 0; +} + +int set_accept_camera_photo(cJSON *json_buf, int mid) +{ + int channelno =0, preset_no=0; + uint8_t buf[32]; + + cJSON *paras = cJSON_GetObjectItem(json_buf, "paras"); + cJSON *datatype = cJSON_GetObjectItem(paras, "channel"); + if(cJSON_IsNumber(datatype)) + { + channelno = datatype->valueint; + } + else + return -1; + cJSON *keep_alive = cJSON_GetObjectItem(paras, " preset_no"); + preset_no = keep_alive->valueint; +/* cJSON *keep_alive = cJSON_GetObjectItem(paras, "brightness"); + dev_para.video_run_para[channelno-1].luminance = keep_alive->valueint; + cJSON *keep_alive = cJSON_GetObjectItem(paras, "contrast"); + dev_para.video_run_para[channelno-1].contrast = keep_alive->valueint; + cJSON *keep_alive = cJSON_GetObjectItem(paras, "saturation"); + dev_para.video_run_para[channelno-1].saturation = keep_alive->valueint; +*/ + cJSON *cmd_req = cJSON_CreateObject(); + + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "resultCode", 0); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + memset(buf, 0, sizeof(buf)); + buf[7] = 0x83; + buf[9] = 2; + buf[10] = channelno; + buf[11] = preset_no; + south_resp_process(buf,14); + return 0; +} + +int set_capture_times(cJSON *json_buf, int mid) +{ + int channelno =0, i=0; + cJSON *timelist = RT_NULL; + cJSON *presetPosition = RT_NULL; + + cJSON *paras = cJSON_GetObjectItem(json_buf, "paras"); + cJSON *datatype = cJSON_GetObjectItem(paras, "channelNumber"); + if(cJSON_IsNumber(datatype)) + { + channelno = datatype->valueint; + } + else + return -1; + if((channelno < 1) || (channelno > 2)) + return -1; + presetPosition = cJSON_GetObjectItem(paras, "groupNumber"); + dev_para.video_run_para[channelno-1].num_work_item = presetPosition->valueint; + cJSON *paramArray = cJSON_GetObjectItem(paras, "paramArray"); + int array_size = cJSON_GetArraySize(paramArray); + for(i=0; ivalueint; + presetPosition = cJSON_GetObjectItem(timelist, "hour"); + dev_para.video_run_para[channelno-1].work_item[i].hour = presetPosition->valueint; + presetPosition = cJSON_GetObjectItem(timelist, "minute"); + dev_para.video_run_para[channelno-1].work_item[i].minute = presetPosition->valueint; + } + south_sort_work_time(&dev_para.video_run_para[channelno-1].work_item[0],dev_para.video_run_para[channelno-1].num_work_item); + south_stat_info.is_update_photo_time[channelno-1] = 1; + save_para(); + + cJSON *cmd_req = cJSON_CreateObject(); + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "resultCode", 0); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + LOG_D("send"); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + return 0; +} + +int action_restart(cJSON *json_buf, int mid) +{ + //int channelno =0; + + cJSON *paras = cJSON_GetObjectItem(json_buf, "paras"); + cJSON *datatype = cJSON_GetObjectItem(paras, "type"); + if(cJSON_IsNumber(datatype)) + { + ;//channelno = datatype->valueint; + } + else + return -1; + + cJSON *cmd_req = cJSON_CreateObject(); + + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "resultCode", 0); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + rt_thread_delay(6*RT_TICK_PER_SECOND); + HAL_NVIC_SystemReset(); + + return 0; +} + +int camera_remote_control(cJSON *json_buf, int mid) +{ + int channelno =0, actionCommandParameter=1, actionCommandParameter2=0; + uint8_t buf[32]; + int resultcode = 0; + + cJSON *paras = cJSON_GetObjectItem(json_buf, "paras"); + cJSON *datatype = cJSON_GetObjectItem(paras, "channelNumber"); + if(cJSON_IsNumber(datatype)) + { + channelno = datatype->valueint; + } + else + return -1; + if((channelno < 1) || (channelno > 2)) + return -1; + cJSON *actionCommand = cJSON_GetObjectItem(paras, "actionCommand"); + actionCommandParameter = actionCommand->valueint; + actionCommand = cJSON_GetObjectItem(paras, "actionCommandParameter"); + actionCommandParameter2 = actionCommand->valueint; + actionCommand = cJSON_GetObjectItem(paras, "actionCommandParameter2"); + //actionCommandParameter2 = actionCommand->valueint; + cJSON *password = cJSON_GetObjectItem(paras, "password"); + if(memcmp(dev_para.passwd,password->valuestring,4) == 0) + ; + else + resultcode = 1; + + cJSON *cmd_req = cJSON_CreateObject(); + + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "resultCode", resultcode); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + if(1 == resultcode) + return 1; + memset(buf, 0, sizeof(buf)); + buf[7] = 0x88; + buf[9] = 7; + memcpy(&buf[10], dev_para.passwd, 4); + buf[14] = channelno; + buf[15] = actionCommandParameter; + buf[16] = actionCommandParameter2; + LOG_D("channelno = %d, actionCommandParameter=%d, actionCommandParameter2=%d", channelno, actionCommandParameter, actionCommandParameter2); + south_resp_process(buf,19); + + return 0; +} + +int manual_capture_picture(cJSON *json_buf, int mid) +{ + int channelno =0, preset=0; + uint8_t buf[32]; + + cJSON *paras = cJSON_GetObjectItem(json_buf, "paras"); + cJSON *datatype = cJSON_GetObjectItem(paras, "channelNumber"); + if(cJSON_IsNumber(datatype)) + { + channelno = datatype->valueint; + } + else + return -1; + if((channelno < 1) || (channelno > 2)) + return -1; + cJSON *actionCommand = cJSON_GetObjectItem(paras, "presetPosition"); + preset = actionCommand->valueint; + + cJSON *cmd_req = cJSON_CreateObject(); + + cJSON *req_body = cJSON_CreateObject();/* 空*/ + cJSON_AddItemToObject(cmd_req,"body",req_body); + cJSON_AddNumberToObject(req_body, "resultCode", 0); + + cJSON_AddNumberToObject(cmd_req, "errcode", 0); + cJSON_AddNumberToObject(cmd_req, "mid", mid); + cJSON_AddStringToObject(cmd_req, "msgType", "deviceRsp"); + + char *sendbuf = cJSON_PrintUnformatted(cmd_req); + //ec20_mqtt_data_send(sendbuf, 1); + AddNode(1, sendbuf, strlen(sendbuf)+2); + if(sendbuf) + { + free(sendbuf); + sendbuf = RT_NULL; + } + cJSON_Delete(cmd_req); + memset(buf, 0, sizeof(buf)); + buf[7] = 0x83; + buf[9] = 2; + buf[10] = channelno; + buf[11] = preset; + south_resp_process(buf,14); + return 0; +} + +//插到最后一个结点之后 +int AddNode(int cmdid, char *buf, int len) +{ + int iMallocSize; + struct WR_DATA *wdata; + + /* 获取互斥锁 */ + rt_mutex_take(&send_data_lock, RT_WAITING_FOREVER); + // 缓冲区容量大小的限制(MAX_BUFUNIT_NUM) + if(mqtt_send_buf.iWriteDataNo > MAX_BUFUNIT_NUM) + { + /* 释放互斥锁 */ + rt_mutex_release(&send_data_lock); + return 0; + } + + if(len <= 0) + { + LOG_D("AddNode (cmdid = %d) buf is empty or invalid(len=%d)\n", cmdid, len); + /* 释放互斥锁 */ + rt_mutex_release(&send_data_lock); + return 0; + } + + iMallocSize = sizeof(struct WR_DATA) + len; + if(MIN_MALLOC_SIZE > iMallocSize) + { + iMallocSize = MIN_MALLOC_SIZE; + } + + if(!mqtt_send_buf.WriteData) //empty before insert + { + mqtt_send_buf.WriteData = (struct WR_DATA*)malloc(iMallocSize); + if(!mqtt_send_buf.WriteData) + { + LOG_D("AddNode (cmdid = %d) memory malloc error1(len=%d, totallen=%d)\n", cmdid, len, iMallocSize); + return 0; + } + // 61.169.135.149 + mqtt_send_buf.WriteData->Next = NULL; + + memmove(mqtt_send_buf.WriteData->MsgData, buf, len); + mqtt_send_buf.WriteData->topid = cmdid; + + mqtt_send_buf.WriteData->MsgLen = len; + mqtt_send_buf.iWriteDataNo++; + /* 释放互斥锁 */ + rt_mutex_release(&send_data_lock); + return len; + } + + wdata = mqtt_send_buf.WriteData; //old link head + while(wdata->Next != NULL) + { + wdata = wdata->Next; + }; + + wdata->Next = (struct WR_DATA*)malloc(iMallocSize); + if(!wdata->Next) + { + LOG_D("AddNode (cmdid = %d) memory malloc error3(len=%d, totallen=%d)\n", cmdid, len, iMallocSize); + /* 释放互斥锁 */ + rt_mutex_release(&send_data_lock); + return 0; + } + + wdata->Next->Next = NULL; + + memcpy(wdata->Next->MsgData, buf, len); + wdata->Next->topid = cmdid; + + wdata->Next->MsgLen = len; + mqtt_send_buf.iWriteDataNo++; + /* 释放互斥锁 */ + rt_mutex_release(&send_data_lock); + return len; +} + +//从头结点往后删 +void DelNode(void) +{ + struct WR_DATA *next; + /* 获取互斥锁 */ + rt_mutex_take(&send_data_lock, RT_WAITING_FOREVER); + if(!mqtt_send_buf.WriteData) + { + rt_mutex_release(&send_data_lock); + return; + } + + mqtt_send_buf.iWriteDataNo--; + next = mqtt_send_buf.WriteData->Next; + free(mqtt_send_buf.WriteData); + mqtt_send_buf.WriteData = next; + /* 释放互斥锁 */ + rt_mutex_release(&send_data_lock); +} + +uint8_t readversion(void) +{ + int fd; + uint8_t buf[16]; + + fd = open("/nand/iversion.ini",O_RDONLY);//打开图片文件 + if(fd<0) + { + LOG_E("open version file %s failed\n","/nand/iversion.ini"); + return 0; + } + memset(buf, 0, sizeof(buf)); + read(fd,buf,sizeof(buf));//读取pkt_len长度到buffer + close(fd); + return buf[0]; +} + +void saveversion(void) +{ + int fd; + uint8_t buf[10]; + + fd = open("/nand/iversion.ini", O_WRONLY | O_CREAT | O_TRUNC, 0); + if (fd < 0) + { + LOG_E("open file %s failed\n", "/nand/iversion.ini"); + return; + } + buf[0] = upgrade_devices.uver; + if (write(fd, buf, 1) != 1) + { + LOG_E("write file %d failed\n", fd); + close(fd); + return; + } + close(fd); +} + +uint8_t readupconfigstate(void) +{ + int fd; + uint8_t buf[16]; + + fd = open("/nand/iconfig.ini",O_RDONLY);//打开图片文件 + if(fd<0) + { + LOG_E("open config state file %s failed\n","/nand/iconfig.ini"); + return 0; + } + memset(buf, 0, sizeof(buf)); + read(fd,buf,sizeof(buf));//读取pkt_len长度到buffer + close(fd); + return buf[0]; +} + +void saveconfigstate(void) +{ + int fd; + uint8_t buf[10]; + + fd = open("/nand/iconfig.ini", O_WRONLY | O_CREAT | O_TRUNC, 0); + if (fd < 0) + { + LOG_E("open file %s failed\n", "/nand/iconfig.ini"); + return; + } + buf[0] = upgrade_devices.uconfig; + if (write(fd, buf, 1) != 1) + { + LOG_E("write file %d failed\n", fd); + close(fd); + return; + } + close(fd); +} +/* mqtt 初始化*/ +void mqtt_send_init(void) +{ + mqtt_send_buf.iWriteDataNo = 0; + mqtt_send_buf.WriteData = RT_NULL; + memset(&upgrade_devices, 0, sizeof(UPGRADE_DEVICES)); + upgrade_devices.uver = readversion(); + upgrade_devices.uconfig = readupconfigstate(); + /* 初始化互斥锁 */ + rt_mutex_init(&send_data_lock, "global_lock", RT_IPC_FLAG_FIFO); + +} + +void mqtt_south_send_data(void) +{ + if(RT_NULL == mqtt_send_buf.WriteData) + return; + ec20_mqtt_data_send((char*)mqtt_send_buf.WriteData->MsgData, (int)mqtt_send_buf.WriteData->topid); + //rt_thread_delay(100); + DelNode(); +} + +void mqtt_south_poll_process(void) +{ + + if(0 == mqtt_connected) + { + mqtt_reconnect(); + } + if(1 == upgrade_devices.state) + { + //rt_thread_delay(15000); + //ec20_http_data_download(http_url, requestheader_url, host_requestheader); + //upgrade_devices.state = 2; + rt_thread_delay(1000); + deviceUpgradeInfoUpload(); + upgrade_devices.file_len=0; + upgrade_devices.recvfile_state = 0; + upgrade_devices.state = 0; + upgrade_devices.upgrade_result = 0; + } + /* 设备升级更新信息上报*/ + // if(1 == upgrade_devices.recvfile_state) + // { + // deviceUpgradeInfoUpload(); + // upgrade_devices.file_len=0; + // upgrade_devices.recvfile_state = 0; + // upgrade_devices.state = 0; + // upgrade_devices.upgrade_result = 0; + // //memset(&upgrade_devices, 0, sizeof(UPGRADE_DEVICES)); + // } + // if(2 == upgrade_devices.state) + // return; + if(1 == iupload_file) + { + iupload_file = 0; + mqtt_dev_upload_file_info(); + } + if(0 < iupload_pic) + { + mqtt_req_up_pic(mqtt_up_pic.picdata, mqtt_up_pic.datalen); + iupload_pic = 0; + } + mqtt_south_send_data(); +} + +int open_upgrade_file(void) +{ + int fd; + char file[32]; + static int iwait =0; + + rt_memset(file, 0, sizeof(file)); + sprintf(file, "/nand/%s", upgrade_devices.filename); + if(0 == iwait) + { + iwait =1; + LOG_D("file:%s", file); + } + fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0); + if (fd < 0) + { + LOG_E("open file %s failed\n", file); + return -3; + } + return fd; +} + +int close_upgrade_file(int fd) +{ + close(fd); + fd = -1; + return fd; +} + +int32_t save_upgrade_file(int fd, uint8_t *filedata, uint32_t size) +{ + if (write(fd, filedata, size) != size) + { + LOG_E("write file %d failed\n", fd); + close(fd); + return -2; + } + return fd; +} + diff --git a/app/src/main/cpp/mqtt_south.h b/app/src/main/cpp/mqtt_south.h new file mode 100644 index 00000000..33e21808 --- /dev/null +++ b/app/src/main/cpp/mqtt_south.h @@ -0,0 +1,154 @@ +#ifndef _SOUTH_MQTT_H +#define _SOUTH_MQTT_H + +#include "rtthread.h" +#include "cJSON.h" + +#define PLATFORM_UPGRADE_DEVICES 0 /*设备升级(命令只是下发通知,设备回应是否收到通知。设备升级的结果由设备作为属性自行上报)*/ +#define PLATFORM_ISSUES_CONFIGFILE 1 /*平台下发参数配置文件*/ +#define GET_DEVICE_LOGLIST 2 /*查询终端设备日志列表*/ +#define GET_DEVICE_LOGFILE 3 /*查询终端设备日志文件*/ +#define GET_DEVICE_TIME 4 /*平台端查询终端设备的系统时间*/ +#define GET_TRANS_ONLINE_MONITCONFIG 5 /*获取输电在线监测终端的运行状态参数*/ +#define GET_FUNCTION_CONFIGURATION 6 /*获取装置功能配置*/ +#define GET_ICING_ONLINE_ALARM_MONITCONFIG 7 /*获取设置覆冰在线监测终端的告警阈*/ +#define GET_DEVICE_DATA 8 /*请求设备数据*/ +#define GET_PHOTO_PARAMS 9 /*平台端查询终端设备图片配置信息*/ +#define GET_CAPTURE_TIMES_TASK_INFORMATION 10 /*平台端查询终端设备定时抓拍任务信息*/ +#define GET_DEVICE_SOLAR_POWER_PROPERTIES 11 /*查询设备太阳能电池板状态数据*/ +#define GET_WEATHER_MONITORING 12 /*查询终端微气象数据*/ +#define GET_PULL_AND_ANGLE_MONITORING 13 /*查询终端拉力倾角监测*/ +#define SET_DEVICE_TIME 14 /*平台端设置终端设备的系统时间*/ +#define SET_TRANS_ONLINE_MONITCONFIG 15 /*设置输电在线监测终端的运行状态参数*/ +#define SET_FUNCTION_CONFIGURATION 16 /*装置功能配置*/ +#define SET_ICING_ONLINE_ALARM_MONIT_CONFIG 17 /*设置覆冰在线监测终端的告警阈值*/ +#define SET_PHOTO_PARAMS 18 /*平台端设置终端设备图片配置信息*/ +#define SET_ACCEPT_CAMERA_PHOTO 19 /*平台端允许终端上送照片*/ +#define SET_CAPTURE_TIMES_TASK_PARAMETER 20 /*平台端设置终端设备定时抓拍任务信息*/ +#define ACTION_RESTART 21 /*控制终端重启*/ +#define CAMERA_REMOTE_CONTROL 22 /*平台下发命令设置控制设备云台*/ +#define MANUAL_CAPTURE_PICTURE 23 /*设置终设备手动抓拍指令*/ +#define GET_COLLECT_INFORMATION 24 /*查询终端设备的采集信息(气象装置使用)*/ +#define GET_MICRO_METEOROLOGICAL_ALARM 25 /*查询终端设备的微气象告警阈值(气象装置使用)*/ +#define SET_MICRO_METEOROLOGICAL_ALARM 26 /*设置终端设备的微气象告警阈值(气象装置使用)*/ + +#define MAX_BUFUNIT_NUM 100 // 缓冲区单元最大数量 +#define MIN_MALLOC_SIZE 200 // malloc分配内存的最小尺寸 + +// MQTT URC数据接收函数接口指针 +typedef const struct +{ + //char *account; // 命令说明 + char *cmd_name; // 命令名称 + int (*recv_process)(cJSON*, int ); /* urc数据处理*/ +}MQTT_URC_FUNC; + +struct WR_DATA +{ + uint16_t MsgLen; + uint8_t topid; + struct WR_DATA *Next; + uint8_t MsgData[2]; +}; + +typedef struct +{ + struct WR_DATA *WriteData; /* 下行数据缓冲区*/ + int iWriteDataNo; /*下行数据缓冲区序号*/ +}MQTT_SEND_BUF; + +typedef struct +{ + uint8_t picdata[64]; /* 84H报文*/ + int datalen; /*报文长度*/ +}MQTT_UP_PIC; + +typedef struct +{ + char file[64]; + char data[512]; + uint8_t type; + uint8_t mode; + uint32_t size; + uint32_t total_pkt; + uint32_t curr_idx; + uint32_t data_len; +}MQTT_FILE_INFO_TYPE; +extern uint32_t south_encode_timestamp(uint8_t *buf,uint32_t t); +int mqtt_base_properties(void); +int mqtt_communicationtechnical_properties(void); +int mqtt_gatewaytechnical_properties(void); +int mqtt_devicetechnical_properties(void); +int powerOnContactMessage_Json(void); +int mqtt_ext_70h(uint8_t *data, uint32_t num); +extern int getsystime(char * timebuf); +int mqtt_keep_alive(uint8_t *data, uint32_t num); +uint8_t isauto_photo(char *file_name); +int mqtt_req_up_pic(uint8_t *data, uint32_t num); +//int mqtt_upload_image_data(cJSON* outdata, uint8_t *data, uint32_t num); +//int mqtt_upload_image_finished(cJSON* outdata, uint8_t *data, uint32_t num); +int mqtt_send_pull_data(uint8_t *data, uint32_t num); +int mqtt_first_pull_data(uint8_t index); +int mqtt_send_weather_data(uint8_t *data, uint32_t num); +int mqtt_first_weather_data(void); +int mqtt_first_unusual_event(void); +int mqtt_device_unusual_event(uint8_t *data, uint32_t num); +int mqtt_status_Monitoring(void); +int mqtt_longitude_latitude_info(void); +int mqtt_solarpower_properties(void); +int mqtt_send_south_data(uint8_t *data,uint32_t num); +int mqtt_dev_upload_file_info(void); +//int mqtt_dev_upload_file_data(MQTT_FILE_INFO_TYPE* mqtt_file_info); +//int mqtt_dev_upload_file_finish(MQTT_FILE_INFO_TYPE* mqtt_file_info); +//int mqtt_dev_upload_file_process(MQTT_FILE_INFO_TYPE* mqtt_file_info); +int deviceUpgradeInfoUpload(void); + +int mqtt_cjson_analysis(char *in_command, int payload_len); +int mqtt_south_recv_process(cJSON *json_buf,uint32_t len); +int get_device_time(cJSON *json_buf, int mid); +int platform_upgrade_devices(cJSON *json_buf, int mid); +int get_device_logfile(cJSON *json_buf, int mid); +int platform_lsuue_configure_file(cJSON *json_buf, int mid); +int get_transOnlineMonit_Config(cJSON *json_buf, int mid); +int get_Fun_config(cJSON *json_buf, int mid); +int get_alarmMonit_config(cJSON *json_buf, int mid); +int get_device_data(cJSON *json_buf,int mid); +int get_photo_params(cJSON *json_buf, int mid); +int get_CaptureTimesTask_info(cJSON *json_buf, int mid); +int get_solarpower_properties(cJSON *json_buf, int mid); +int mqtt_time_set(char *hostsettime); +int set_device_time(cJSON *json_buf, int mid); +int set_online_monit_config(cJSON *json_buf, int mid); +int set_Fun_config(cJSON *json_buf, int mid); +int set_Icing_alarm_config(cJSON *json_buf, int mid); +int set_photo_params(cJSON *json_buf, int mid); +int set_accept_camera_photo(cJSON *json_buf, int mid); +int set_capture_times(cJSON *json_buf, int mid); +int action_restart(cJSON *json_buf, int mid); +int camera_remote_control(cJSON *json_buf, int mid); +int manual_capture_picture(cJSON *json_buf, int mid); +int get_pull_angle_monitoring(cJSON *json_buf, int mid); +int get_weather_monitoring(cJSON *json_buf, int mid); +int get_device_loglist(cJSON *json_buf, int mid); + +//插到最后一个结点之后 +int AddNode(int cmdid, char *buf, int len); +//从头结点往后删 +void DelNode(void); +/* mqtt 初始化*/ +void mqtt_send_init(void); +void mqtt_south_send_data(void); + +uint8_t readversion(void); +void saveversion(void); +uint8_t readupconfigstate(void); +void saveconfigstate(void); +void mqtt_south_poll_process(void); +int open_upgrade_file(void); +int close_upgrade_file(int fd); +int32_t save_upgrade_file(int fd, uint8_t *filedata, uint32_t size); +uint32_t hostip_char_to_int(char *hostip); + + + +#endif