2012-10-31 17:04:45 +01:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/wgetjson/wgetjson_main.c
|
2012-10-31 17:04:45 +01:00
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Darcy Gong
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/netlib.h"
|
|
|
|
#include "netutils/webclient.h"
|
|
|
|
#include "netutils/cJSON.h"
|
2012-10-31 17:04:45 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-02 22:06:11 +02:00
|
|
|
* Pre-processor Definitions
|
2012-10-31 17:04:45 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_EXAMPLES_WGETJSON_MAXSIZE
|
2012-11-03 01:00:56 +01:00
|
|
|
# define CONFIG_EXAMPLES_WGETJSON_MAXSIZE 1024
|
2012-10-31 17:04:45 +01:00
|
|
|
#endif
|
|
|
|
|
2012-11-03 01:00:56 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_WGETJSON_URL
|
|
|
|
# define CONFIG_EXAMPLES_WGETJSON_URL "http://10.0.0.1/wgetjson/json_cmd.php"
|
2012-10-31 17:04:45 +01:00
|
|
|
#endif
|
|
|
|
|
2012-11-03 01:00:56 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_WGETPOST_URL
|
|
|
|
# define CONFIG_EXAMPLES_WGETPOST_URL "http://10.0.0.1/wgetjson/post_cmd.php"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MULTI_POST_NDATA 3
|
|
|
|
|
2012-10-31 17:04:45 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-25 21:51:10 +01:00
|
|
|
static FAR char *g_json_buff = NULL;
|
|
|
|
static int g_json_bufflen = 0;
|
|
|
|
static bool g_has_json = false;
|
2012-10-31 17:04:45 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-09-29 09:52:59 +02:00
|
|
|
static int wgetjson_postdebug_callback(FAR char **buffer, int offset,
|
|
|
|
int datend, FAR int *buflen,
|
|
|
|
FAR void *arg)
|
2012-11-03 01:00:56 +01:00
|
|
|
{
|
|
|
|
int len = datend - offset;
|
|
|
|
if (len <= 0)
|
|
|
|
{
|
|
|
|
printf("Callback No Data!\n");
|
2020-09-29 09:52:59 +02:00
|
|
|
return 0;
|
2012-11-03 01:00:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
((*buffer)[datend]) = '\0';
|
|
|
|
printf("Callback Data(Length:%d):\n%s\n", len, &((*buffer)[offset]));
|
2020-09-29 09:52:59 +02:00
|
|
|
return 0;
|
2012-11-03 01:00:56 +01:00
|
|
|
}
|
|
|
|
|
2012-10-31 17:04:45 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: wgetjson_callback
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-09-29 09:52:59 +02:00
|
|
|
static int wgetjson_callback(FAR char **buffer, int offset, int datend,
|
|
|
|
FAR int *buflen, FAR void *arg)
|
2012-10-31 17:04:45 +01:00
|
|
|
{
|
2014-11-25 21:51:10 +01:00
|
|
|
FAR char *new_json_buff;
|
|
|
|
int len = datend - offset;
|
|
|
|
int org = len;
|
2012-10-31 17:04:45 +01:00
|
|
|
|
|
|
|
if (len <= 0)
|
|
|
|
{
|
2020-09-29 09:52:59 +02:00
|
|
|
return 0;
|
2012-10-31 17:04:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_json_buff)
|
|
|
|
{
|
|
|
|
g_json_buff = malloc(len + 1);
|
|
|
|
memcpy(g_json_buff, &((*buffer)[offset]), len);
|
|
|
|
g_json_buff[len] = 0;
|
|
|
|
g_json_bufflen = len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (g_json_bufflen >= CONFIG_EXAMPLES_WGETJSON_MAXSIZE)
|
|
|
|
{
|
|
|
|
g_json_bufflen += org;
|
2020-09-29 09:52:59 +02:00
|
|
|
return 0;
|
2012-10-31 17:04:45 +01:00
|
|
|
}
|
|
|
|
|
2020-09-29 04:49:56 +02:00
|
|
|
if (g_json_bufflen + len > CONFIG_EXAMPLES_WGETJSON_MAXSIZE)
|
2012-10-31 17:04:45 +01:00
|
|
|
{
|
|
|
|
len = CONFIG_EXAMPLES_WGETJSON_MAXSIZE - g_json_bufflen;
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:49:56 +02:00
|
|
|
new_json_buff = (FAR char *)realloc(g_json_buff,
|
|
|
|
g_json_bufflen + len + 1);
|
2014-11-25 21:51:10 +01:00
|
|
|
if (new_json_buff)
|
|
|
|
{
|
|
|
|
g_json_buff = new_json_buff;
|
2021-09-12 01:58:50 +02:00
|
|
|
memcpy(&g_json_buff[g_json_bufflen], &((*buffer)[offset]),
|
2020-09-29 04:49:56 +02:00
|
|
|
len);
|
2014-11-25 21:51:10 +01:00
|
|
|
g_json_buff[g_json_bufflen + len] = 0;
|
|
|
|
g_json_bufflen += org;
|
|
|
|
}
|
2012-10-31 17:04:45 +01:00
|
|
|
}
|
2020-09-29 09:52:59 +02:00
|
|
|
|
|
|
|
return 0;
|
2012-10-31 17:04:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: wgetjson_json_release
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void wgetjson_json_release(void)
|
|
|
|
{
|
|
|
|
if (g_json_buff)
|
|
|
|
{
|
|
|
|
free(g_json_buff);
|
|
|
|
g_json_buff = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_json_bufflen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: wgetjson_doit
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if 0 /* Not used */
|
|
|
|
static void wgetjson_doit(char *text)
|
|
|
|
{
|
|
|
|
char *out;
|
|
|
|
cJSON *json;
|
|
|
|
|
|
|
|
json = cJSON_Parse(text);
|
|
|
|
if (!json)
|
|
|
|
{
|
2020-09-29 04:49:56 +02:00
|
|
|
printf("Error before: [%s]\n", cJSON_GetErrorPtr());
|
2012-10-31 17:04:45 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out = cJSON_Print(json);
|
|
|
|
cJSON_Delete(json);
|
|
|
|
printf("%s\n", out);
|
|
|
|
free(out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: wgetjson_json_item_callback
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-09-29 04:49:56 +02:00
|
|
|
static int wgetjson_json_item_callback(const char *name, int type,
|
|
|
|
cJSON *item)
|
2012-10-31 17:04:45 +01:00
|
|
|
{
|
|
|
|
if (strlen(name) > 8 && !memcmp(name, "/(null)", 7))
|
|
|
|
{
|
|
|
|
name += 8;
|
|
|
|
g_has_json = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(name, "name"))
|
|
|
|
{
|
2021-12-26 23:10:48 +01:00
|
|
|
printf("name:\t\t\t%s\n", item->valuestring);
|
2020-09-29 04:49:56 +02:00
|
|
|
|
|
|
|
/* todo something.... */
|
2012-10-31 17:04:45 +01:00
|
|
|
}
|
2020-09-29 04:49:56 +02:00
|
|
|
else if (strcmp(name, "format/type") == 0)
|
2012-10-31 17:04:45 +01:00
|
|
|
{
|
2021-12-26 23:10:48 +01:00
|
|
|
printf("format/type:\t\t%s\n", item->valuestring);
|
2020-09-29 04:49:56 +02:00
|
|
|
|
|
|
|
/* todo something.... */
|
2012-10-31 17:04:45 +01:00
|
|
|
}
|
|
|
|
else if (!strcmp(name, "format/width"))
|
|
|
|
{
|
2021-12-26 23:10:48 +01:00
|
|
|
printf("format/width:\t\t%d\n", item->valueint);
|
2020-09-29 04:49:56 +02:00
|
|
|
|
|
|
|
/* todo something.... */
|
2012-10-31 17:04:45 +01:00
|
|
|
}
|
|
|
|
else if (!strcmp(name, "format/height"))
|
|
|
|
{
|
2021-12-26 23:10:48 +01:00
|
|
|
printf("format/height:\t\t%d\n", item->valueint);
|
2020-09-29 04:49:56 +02:00
|
|
|
|
|
|
|
/* todo something.... */
|
2012-10-31 17:04:45 +01:00
|
|
|
}
|
|
|
|
else if (!strcmp(name, "format/interlace"))
|
|
|
|
{
|
2021-12-26 23:10:48 +01:00
|
|
|
printf("format/interlace:\t%s\n",
|
2020-09-29 04:49:56 +02:00
|
|
|
(item->valueint) ? "true" : "false");
|
|
|
|
|
|
|
|
/* todo something.... */
|
2012-10-31 17:04:45 +01:00
|
|
|
}
|
|
|
|
else if (!strcmp(name, "format/frame rate"))
|
|
|
|
{
|
2021-12-26 23:10:48 +01:00
|
|
|
printf("format/frame rate:\t%d\n", item->valueint);
|
2020-09-29 04:49:56 +02:00
|
|
|
|
|
|
|
/* todo something.... */
|
2012-10-31 17:04:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: wgetjson_json_item_scan
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void wgetjson_json_item_scan(cJSON *item, const char *prefix)
|
|
|
|
{
|
|
|
|
char *newprefix;
|
|
|
|
int dorecurse;
|
|
|
|
|
|
|
|
while (item)
|
|
|
|
{
|
2020-09-29 09:37:18 +02:00
|
|
|
const char *string = item->string ? item->string : "(null)";
|
|
|
|
newprefix = malloc(strlen(prefix) + strlen(string) + 2);
|
|
|
|
sprintf(newprefix, "%s/%s", prefix, string);
|
2012-10-31 17:04:45 +01:00
|
|
|
|
|
|
|
dorecurse = wgetjson_json_item_callback(newprefix, item->type, item);
|
|
|
|
if (item->child && dorecurse)
|
|
|
|
{
|
|
|
|
wgetjson_json_item_scan(item->child, newprefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
item = item->next;
|
|
|
|
free(newprefix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: wgetjson_json_parse
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int wgetjson_json_parse(char *text)
|
|
|
|
{
|
|
|
|
cJSON *json;
|
|
|
|
char *path = "";
|
|
|
|
|
2020-09-29 04:49:56 +02:00
|
|
|
json = cJSON_Parse(text);
|
2012-10-31 17:04:45 +01:00
|
|
|
if (!json)
|
|
|
|
{
|
|
|
|
printf("Error before: [%s]\n", cJSON_GetErrorPtr());
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wgetjson_json_item_scan(json, path);
|
|
|
|
cJSON_Delete(json);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: wgetjson_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
2012-10-31 17:04:45 +01:00
|
|
|
{
|
|
|
|
char *buffer = NULL;
|
2012-11-03 01:00:56 +01:00
|
|
|
int buffer_len = 512;
|
|
|
|
char *url = CONFIG_EXAMPLES_WGETJSON_URL;
|
|
|
|
int ret = -1;
|
|
|
|
int option;
|
|
|
|
bool is_post = false;
|
|
|
|
bool is_post_multi = false;
|
2020-09-29 04:49:56 +02:00
|
|
|
bool badarg = false;
|
|
|
|
bool is_debug = false;
|
2012-11-03 01:00:56 +01:00
|
|
|
char *post_buff = NULL;
|
|
|
|
int post_buff_len = 0;
|
|
|
|
char *post_single_name = "type";
|
|
|
|
char *post_single_value = "string";
|
2020-09-29 04:49:56 +02:00
|
|
|
char *post_multi_names[MULTI_POST_NDATA] =
|
|
|
|
{
|
|
|
|
"name", "gender", "country"
|
|
|
|
};
|
|
|
|
|
|
|
|
char *post_multi_values[MULTI_POST_NDATA] =
|
|
|
|
{
|
|
|
|
"darcy", "man", "china"
|
|
|
|
};
|
|
|
|
|
2020-09-29 09:52:59 +02:00
|
|
|
webclient_sink_callback_t wget_cb = wgetjson_callback;
|
2012-11-03 01:00:56 +01:00
|
|
|
|
|
|
|
while ((option = getopt(argc, argv, ":pPD")) != ERROR)
|
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'p':
|
|
|
|
is_post = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'P':
|
|
|
|
is_post = true;
|
|
|
|
is_post_multi = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
|
|
|
is_debug = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ':':
|
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (badarg)
|
|
|
|
{
|
2020-09-29 04:49:56 +02:00
|
|
|
printf("usage: wgetjson -p(single post) -P(multi post) "
|
|
|
|
"-D(debug wget callback)\n");
|
2012-11-03 01:00:56 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_debug)
|
|
|
|
{
|
|
|
|
wget_cb = wgetjson_postdebug_callback;
|
|
|
|
}
|
2012-10-31 17:04:45 +01:00
|
|
|
|
2012-11-03 01:00:56 +01:00
|
|
|
if (is_post)
|
|
|
|
{
|
2020-09-29 04:49:56 +02:00
|
|
|
buffer_len = 512 * 2;
|
2012-11-03 01:00:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer = malloc(buffer_len);
|
2012-10-31 17:04:45 +01:00
|
|
|
wgetjson_json_release();
|
|
|
|
|
2020-09-29 09:52:59 +02:00
|
|
|
struct webclient_context ctx;
|
|
|
|
webclient_set_defaults(&ctx);
|
|
|
|
ctx.buffer = buffer;
|
|
|
|
ctx.buflen = buffer_len;
|
|
|
|
ctx.sink_callback = wget_cb;
|
|
|
|
ctx.sink_callback_arg = NULL;
|
2012-11-03 01:00:56 +01:00
|
|
|
if (is_post)
|
|
|
|
{
|
|
|
|
url = CONFIG_EXAMPLES_WGETPOST_URL;
|
2020-09-29 09:40:53 +02:00
|
|
|
printf("URL: %s\n", url);
|
2012-11-03 01:00:56 +01:00
|
|
|
if (is_post_multi)
|
|
|
|
{
|
2020-09-29 04:49:56 +02:00
|
|
|
post_buff_len = web_posts_strlen(post_multi_names,
|
|
|
|
post_multi_values,
|
|
|
|
MULTI_POST_NDATA);
|
2012-11-03 01:00:56 +01:00
|
|
|
post_buff = malloc(post_buff_len);
|
2020-09-29 04:49:56 +02:00
|
|
|
web_posts_str(post_buff, &post_buff_len, post_multi_names,
|
|
|
|
post_multi_values, MULTI_POST_NDATA);
|
2012-11-03 01:00:56 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-29 04:49:56 +02:00
|
|
|
post_buff_len = web_post_strlen(post_single_name,
|
|
|
|
post_single_value);
|
2012-11-03 01:00:56 +01:00
|
|
|
post_buff = malloc(post_buff_len);
|
2020-09-29 04:49:56 +02:00
|
|
|
web_post_str(post_buff, &post_buff_len, post_single_name,
|
|
|
|
post_single_value);
|
2012-11-03 01:00:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (post_buff)
|
|
|
|
{
|
2020-09-29 09:52:59 +02:00
|
|
|
const char *header = "Content-Type: "
|
|
|
|
"application/x-www-form-urlencoded";
|
|
|
|
ctx.method = "POST";
|
|
|
|
ctx.url = url;
|
|
|
|
ctx.headers = &header;
|
|
|
|
ctx.nheaders = 1;
|
|
|
|
webclient_set_static_body(&ctx, post_buff, strlen(post_buff));
|
|
|
|
ret = webclient_perform(&ctx);
|
2012-11-03 01:00:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-29 09:40:53 +02:00
|
|
|
printf("URL: %s\n", url);
|
2020-09-29 09:52:59 +02:00
|
|
|
ctx.method = "GET";
|
|
|
|
ctx.url = url;
|
|
|
|
ret = webclient_perform(&ctx);
|
2012-11-03 01:00:56 +01:00
|
|
|
}
|
|
|
|
|
2012-10-31 17:04:45 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-09-29 04:49:56 +02:00
|
|
|
printf("get json size: %d\n", g_json_bufflen);
|
2012-10-31 17:04:45 +01:00
|
|
|
}
|
2012-11-03 01:00:56 +01:00
|
|
|
else if (!is_debug)
|
2012-10-31 17:04:45 +01:00
|
|
|
{
|
|
|
|
g_has_json = false;
|
|
|
|
if (wgetjson_json_parse(g_json_buff) == OK && g_has_json)
|
|
|
|
{
|
|
|
|
printf("Parse OK\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Parse error\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
g_has_json = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wgetjson_json_release();
|
|
|
|
free(buffer);
|
2012-11-03 01:00:56 +01:00
|
|
|
if (post_buff)
|
|
|
|
{
|
|
|
|
free(post_buff);
|
|
|
|
}
|
|
|
|
|
2012-10-31 17:04:45 +01:00
|
|
|
return 0;
|
|
|
|
}
|