From 33a3edbaf731fd409bfd6e9aa3ad46a194a2f9a4 Mon Sep 17 00:00:00 2001 From: patacongo Date: Wed, 31 Oct 2012 16:04:45 +0000 Subject: Add apps/examples/wgetjson from Darcy Gong git-svn-id: http://svn.code.sf.net/p/nuttx/code/trunk@5281 42af7a65-404d-4744-a932-0658087f49c3 --- apps/ChangeLog.txt | 3 +- apps/examples/Kconfig | 1 + apps/examples/Make.defs | 4 + apps/examples/Makefile | 4 +- apps/examples/README.txt | 9 + apps/examples/wgetjson/Kconfig | 23 ++ apps/examples/wgetjson/Makefile | 105 +++++++ .../wgetjson/webserver/wgetjson/json_cmd.php | 13 + apps/examples/wgetjson/wgetjson_main.c | 305 +++++++++++++++++++++ apps/netutils/codecs/md5.c | 4 +- apps/netutils/json/cJSON.c | 10 +- 11 files changed, 472 insertions(+), 9 deletions(-) create mode 100644 apps/examples/wgetjson/Kconfig create mode 100644 apps/examples/wgetjson/Makefile create mode 100644 apps/examples/wgetjson/webserver/wgetjson/json_cmd.php create mode 100644 apps/examples/wgetjson/wgetjson_main.c diff --git a/apps/ChangeLog.txt b/apps/ChangeLog.txt index 4d147d91d..db56d6681 100644 --- a/apps/ChangeLog.txt +++ b/apps/ChangeLog.txt @@ -392,5 +392,6 @@ * COPYING: Licensing information added. * apps/netutils/codec and include/netutils/urldecode.h, base64.h, and md5.h: A port of the BASE46, MD5 and URL CODEC library from Darcy Gong. - * nslib/nsh_codeccmd.c: NSH commands to use the CODEC library. + * nsnlib/nsh_codeccmd.c: NSH commands to use the CODEC library. Contributed by Darcy Gong. + * apps/examples/wgetjson: Test example contributed by Darcy Gong diff --git a/apps/examples/Kconfig b/apps/examples/Kconfig index f851ad8bd..7f0c8e365 100644 --- a/apps/examples/Kconfig +++ b/apps/examples/Kconfig @@ -54,5 +54,6 @@ source "$APPSDIR/examples/usbstorage/Kconfig" source "$APPSDIR/examples/usbterm/Kconfig" source "$APPSDIR/examples/watchdog/Kconfig" source "$APPSDIR/examples/wget/Kconfig" +source "$APPSDIR/examples/wgetjson/Kconfig" source "$APPSDIR/examples/wlan/Kconfig" source "$APPSDIR/examples/xmlrpc/Kconfig" diff --git a/apps/examples/Make.defs b/apps/examples/Make.defs index ad5653c82..67c1685fe 100644 --- a/apps/examples/Make.defs +++ b/apps/examples/Make.defs @@ -234,6 +234,10 @@ ifeq ($(CONFIG_EXAMPLES_WGET),y) CONFIGURED_APPS += examples/wget endif +ifeq ($(CONFIG_EXAMPLES_WGETJSON),y) +CONFIGURED_APPS += examples/wgetjson +endif + ifeq ($(CONFIG_EXAMPLES_WLAN),y) CONFIGURED_APPS += examples/wlan endif diff --git a/apps/examples/Makefile b/apps/examples/Makefile index 0ebc300e4..d8a2c9664 100644 --- a/apps/examples/Makefile +++ b/apps/examples/Makefile @@ -42,7 +42,7 @@ SUBDIRS += helloxx hidkbd igmp json lcdrw mm modbus mount nettest nsh null nx SUBDIRS += nxconsole nxffs nxflat nxhello nximage nxlines nxtext ostest SUBDIRS += pashello pipe poll pwm qencoder rgmp romfs serloop telnetd SUBDIRS += thttpd tiff touchscreen udp uip usbserial sendmail usbstorage -SUBDIRS += usbterm watchdog wget wlan +SUBDIRS += usbterm watchdog wget wgetjson wlan # Sub-directories that might need context setup. Directories may need # context setup for a variety of reasons, but the most common is because @@ -58,7 +58,7 @@ CNTXTDIRS = pwm ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) CNTXTDIRS += adc can cdcacm composite dhcpd discover ftpd json modbus -CNTXTDIRS += nettest qencoder telnetd watchdog +CNTXTDIRS += nettest qencoder telnetd watchdog wgetjson endif ifeq ($(CONFIG_EXAMPLES_HELLO_BUILTIN),y) diff --git a/apps/examples/README.txt b/apps/examples/README.txt index 907467bb1..f254ce154 100644 --- a/apps/examples/README.txt +++ b/apps/examples/README.txt @@ -1747,7 +1747,16 @@ examples/wget CONFIGURED_APPS += resolv CONFIGURED_APPS += webclient +examples/wget +^^^^^^^^^^^^^ + + Uses wget to get a JSON encoded file, then decodes the file. + + CONFIG_EXAMPLES_WDGETJSON_MAXSIZE - Max. JSON Buffer Size + CONFIG_EXAMPLES_EXAMPLES_WGETJSON_URL - wget URL + examples/xmlrpc +^^^^^^^^^^^^^^^ This example exercises the "Embeddable Lightweight XML-RPC Server" which is discussed at: diff --git a/apps/examples/wgetjson/Kconfig b/apps/examples/wgetjson/Kconfig new file mode 100644 index 000000000..2e3b4b3eb --- /dev/null +++ b/apps/examples/wgetjson/Kconfig @@ -0,0 +1,23 @@ +# +# For a description of the syntax of this configuration file, +# see misc/tools/kconfig-language.txt. +# + +config EXAMPLES_WGETJSON + bool "wget JSON Example" + default n + depends on NETUTILS_JSON + ---help--- + Enable the wget JSON example + +if EXAMPLES_WGETJSON + +config EXAMPLES_WGETJSON_MAXSIZE + int "Max. JSON Buffer Size" + default 10240 + +config EXAMPLES_WGETJSON_URL + string "wget URL" + default "http://10.0.0.1/wgetjson/json_cmd.php" + +endif diff --git a/apps/examples/wgetjson/Makefile b/apps/examples/wgetjson/Makefile new file mode 100644 index 000000000..e8c3e5065 --- /dev/null +++ b/apps/examples/wgetjson/Makefile @@ -0,0 +1,105 @@ +############################################################################ +# apps/examples/wgetjson/Makefile +# +# Copyright (C) 2012 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# 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. +# +############################################################################ + +-include $(TOPDIR)/.config +-include $(TOPDIR)/Make.defs +include $(APPDIR)/Make.defs + +# Hello, World! Example + +ASRCS = +CSRCS = wgetjson_main.c + +AOBJS = $(ASRCS:.S=$(OBJEXT)) +COBJS = $(CSRCS:.c=$(OBJEXT)) + +SRCS = $(ASRCS) $(CSRCS) +OBJS = $(AOBJS) $(COBJS) + +ifeq ($(WINTOOL),y) + BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}" +else + BIN = "$(APPDIR)/libapps$(LIBEXT)" +endif + +ROOTDEPPATH = --dep-path . + +# Buttons built-in application info + +APPNAME = wgetjson +PRIORITY = SCHED_PRIORITY_DEFAULT +STACKSIZE = 2048 + +# Common build + +VPATH = + +all: .built +.PHONY: context clean depend distclean + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +.built: $(OBJS) + @( for obj in $(OBJS) ; do \ + $(call ARCHIVE, $(BIN), $${obj}); \ + done ; ) + @touch .built + +.context: +ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) + $(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main) + @touch $@ +endif + +context: .context + +.depend: Makefile $(SRCS) + @$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep + @touch $@ + +depend: .depend + +clean: + @rm -f *.o *~ .*.swp .built + $(call CLEAN) + +distclean: clean + @rm -f Make.dep .depend + +-include Make.dep diff --git a/apps/examples/wgetjson/webserver/wgetjson/json_cmd.php b/apps/examples/wgetjson/webserver/wgetjson/json_cmd.php new file mode 100644 index 000000000..2b66ff18f --- /dev/null +++ b/apps/examples/wgetjson/webserver/wgetjson/json_cmd.php @@ -0,0 +1,13 @@ + \ No newline at end of file diff --git a/apps/examples/wgetjson/wgetjson_main.c b/apps/examples/wgetjson/wgetjson_main.c new file mode 100644 index 000000000..7f3096797 --- /dev/null +++ b/apps/examples/wgetjson/wgetjson_main.c @@ -0,0 +1,305 @@ +/**************************************************************************** + * examples/wgetjson/wgetjson_main.c + * + * 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 + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +#ifndef CONFIG_EXAMPLES_WGETJSON_MAXSIZE +# define CONFIG_EXAMPLES_WGETJSON_MAXSIZE 1024*10 +#endif + +#ifndef CONFIG_EXAMPLES_EXAMPLES_WGETJSON_URL +# define CONFIG_EXAMPLES_EXAMPLES_WGETJSON_URL "http://10.0.0.1/wgetjson/json_cmd.php" +#endif + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static char *g_json_buff = NULL; +static int g_json_bufflen = 0; +static bool g_has_json = false; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: wgetjson_callback + ****************************************************************************/ + +static void wgetjson_callback(FAR char **buffer, int offset, int datend, + FAR int *buflen, FAR void *arg) +{ + int len = datend - offset,org=len; + + if (len <= 0) + { + return; + } + + 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; + return; + } + + if (g_json_bufflen+len > CONFIG_EXAMPLES_WGETJSON_MAXSIZE) + { + len = CONFIG_EXAMPLES_WGETJSON_MAXSIZE - g_json_bufflen; + } + + g_json_buff = realloc(g_json_buff, g_json_bufflen + len + 1); + memcpy(&g_json_buff[g_json_bufflen-1], &((*buffer)[offset]), len); + g_json_buff[g_json_bufflen + len] = 0; + g_json_bufflen += org; + } +} + +/**************************************************************************** + * 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) + { + printf("Error before: [%s]\n",cJSON_GetErrorPtr()); + } + else + { + out = cJSON_Print(json); + cJSON_Delete(json); + printf("%s\n", out); + free(out); + } +} +#endif + +/**************************************************************************** + * Name: wgetjson_json_item_callback + ****************************************************************************/ + +static int wgetjson_json_item_callback(const char *name,int type,cJSON *item) +{ + if (strlen(name) > 8 && !memcmp(name, "/(null)", 7)) + { + name += 8; + g_has_json = true; + } + + if (!strcmp(name, "name")) + { + printf("name:\t\t\t%s \n", item->valuestring); + // todo something.... + } + else if (strcmp(name, "format/type")==0) + { + printf("format/type:\t\t%s \n", item->valuestring); + // todo something.... + } + else if (!strcmp(name, "format/width")) + { + printf("format/width:\t\t%d \n", item->valueint); + // todo something.... + } + else if (!strcmp(name, "format/height")) + { + printf("format/height:\t\t%d \n", item->valueint); + // todo something.... + } + else if (!strcmp(name, "format/interlace")) + { + printf("format/interlace:\t%s \n", (item->valueint) ? "true" : "false"); + // todo something.... + } + else if (!strcmp(name, "format/frame rate")) + { + printf("format/frame rate:\t%d \n", item->valueint); + // todo something.... + } + + return 1; +} + +/**************************************************************************** + * Name: wgetjson_json_item_scan + ****************************************************************************/ + +static void wgetjson_json_item_scan(cJSON *item, const char *prefix) +{ + char *newprefix; + int dorecurse; + + while (item) + { + newprefix = malloc(strlen(prefix) + strlen(item->string) + 2); + sprintf(newprefix, "%s/%s", prefix, item->string); + + 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 = ""; + + json=cJSON_Parse(text); + 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 + ****************************************************************************/ + +int wgetjson_main(int argc, char *argv[]) +{ + char *buffer = NULL; + char *url = CONFIG_EXAMPLES_EXAMPLES_WGETJSON_URL; + int ret; + + buffer = malloc(512); + wgetjson_json_release(); + + printf("URL: %s\n", url); + + ret = wget(url, buffer, 512, wgetjson_callback, NULL); + if (ret < 0) + { + printf("get json size: %d\n",g_json_bufflen); + } + else + { + 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); + return 0; +} diff --git a/apps/netutils/codecs/md5.c b/apps/netutils/codecs/md5.c index da25d14b1..bf0394b9d 100644 --- a/apps/netutils/codecs/md5.c +++ b/apps/netutils/codecs/md5.c @@ -60,6 +60,8 @@ #include +#include +#include #include #include @@ -392,7 +394,7 @@ char *md5_hash(const uint8_t * addr, const size_t len) md5_sum(addr, len, digest); for (i = 0; i < 16; i++) { - vsprintf(&hash[i * 2], "%.2x", digest[i]); + sprintf(&hash[i * 2], "%.2x", digest[i]); } hash[32] = 0; diff --git a/apps/netutils/json/cJSON.c b/apps/netutils/json/cJSON.c index 85091c948..e5120ec42 100644 --- a/apps/netutils/json/cJSON.c +++ b/apps/netutils/json/cJSON.c @@ -1382,7 +1382,7 @@ void cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem /* Create basic types: */ -cJSON *cJSON_CreateNull() +cJSON *cJSON_CreateNull(void) { cJSON *item = cJSON_New_Item(); if (item) @@ -1393,7 +1393,7 @@ cJSON *cJSON_CreateNull() return item; } -cJSON *cJSON_CreateTrue() +cJSON *cJSON_CreateTrue(void) { cJSON *item = cJSON_New_Item(); if (item) @@ -1404,7 +1404,7 @@ cJSON *cJSON_CreateTrue() return item; } -cJSON *cJSON_CreateFalse() +cJSON *cJSON_CreateFalse(void) { cJSON *item = cJSON_New_Item(); if (item) @@ -1451,7 +1451,7 @@ cJSON *cJSON_CreateString(const char *string) return item; } -cJSON *cJSON_CreateArray() +cJSON *cJSON_CreateArray(void) { cJSON *item = cJSON_New_Item(); if (item) @@ -1462,7 +1462,7 @@ cJSON *cJSON_CreateArray() return item; } -cJSON *cJSON_CreateObject() +cJSON *cJSON_CreateObject(void) { cJSON *item = cJSON_New_Item(); if (item) -- cgit v1.2.3