Contiki-NG
jsontree.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2012, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  */
31 
32 /**
33  * \file
34  * JSON output generation
35  * \author
36  * Niclas Finne <nfi@sics.se>
37  * Joakim Eriksson <joakime@sics.se>
38  */
39 
40 #ifndef JSONTREE_H_
41 #define JSONTREE_H_
42 
43 #include "contiki.h"
44 #include "json.h"
45 
46 #ifdef JSONTREE_CONF_MAX_DEPTH
47 #define JSONTREE_MAX_DEPTH JSONTREE_CONF_MAX_DEPTH
48 #else
49 #define JSONTREE_MAX_DEPTH 10
50 #endif /* JSONTREE_CONF_MAX_DEPTH */
51 
52 #ifdef JSONTREE_CONF_PRETTY
53 #define JSONTREE_PRETTY JSONTREE_CONF_PRETTY
54 #else
55 #define JSONTREE_PRETTY 0
56 #endif /* JSONTREE_CONF_PRETTY */
57 
58 struct jsontree_context {
59  struct jsontree_value *values[JSONTREE_MAX_DEPTH];
60  uint16_t index[JSONTREE_MAX_DEPTH];
61  int (* putchar)(int);
62  uint8_t depth;
63  uint8_t path;
64  int callback_state;
65 };
66 
67 struct jsontree_value {
68  uint8_t type;
69  /* followed by a value */
70 };
71 
72 struct jsontree_string {
73  uint8_t type;
74  const char *value;
75 };
76 
77 struct jsontree_uint {
78  uint8_t type;
79  unsigned int value;
80 };
81 
82 struct jsontree_int {
83  uint8_t type;
84  int value;
85 };
86 
87 /* NOTE: the jsontree_callback set will receive a jsonparse state */
88 struct jsonparse_state;
89 struct jsontree_callback {
90  uint8_t type;
91  int (* output)(struct jsontree_context *js_ctx);
92  int (* set)(struct jsontree_context *js_ctx, struct jsonparse_state *parser);
93 };
94 
95 struct jsontree_pair {
96  const char *name;
97  struct jsontree_value *value;
98 };
99 
100 struct jsontree_object {
101  uint8_t type;
102  uint8_t count;
103  struct jsontree_pair *pairs;
104 };
105 
106 struct jsontree_array {
107  uint8_t type;
108  uint8_t count;
109  struct jsontree_value **values;
110 };
111 
112 struct jsontree_ptr {
113  uint8_t type;
114  const void *value;
115 };
116 
117 #define JSONTREE_STRING(text) {JSON_TYPE_STRING, (text)}
118 #define JSONTREE_PAIR(name, value) {(name), (struct jsontree_value *)(value)}
119 #define JSONTREE_CALLBACK(output, set) {JSON_TYPE_CALLBACK, (output), (set)}
120 
121 #define JSONTREE_OBJECT(name, ...) \
122  static struct jsontree_pair jsontree_pair_##name[] = {__VA_ARGS__}; \
123  static struct jsontree_object name = { \
124  JSON_TYPE_OBJECT, \
125  sizeof(jsontree_pair_##name)/sizeof(struct jsontree_pair), \
126  jsontree_pair_##name }
127 
128 #define JSONTREE_OBJECT_EXT(name, ...) \
129  static struct jsontree_pair jsontree_pair_##name[] = {__VA_ARGS__}; \
130  struct jsontree_object name = { \
131  JSON_TYPE_OBJECT, \
132  sizeof(jsontree_pair_##name)/sizeof(struct jsontree_pair), \
133  jsontree_pair_##name }
134 
135 #define JSONTREE_ARRAY(name, count) \
136  static struct jsontree_value *jsontree_value##name[count]; \
137  static struct jsontree_array name = { \
138  JSON_TYPE_ARRAY, \
139  count, \
140  jsontree_value##name }
141 
142 void jsontree_setup(struct jsontree_context *js_ctx,
143  struct jsontree_value *root, int (* putchar)(int));
144 void jsontree_reset(struct jsontree_context *js_ctx);
145 
146 const char *jsontree_path_name(const struct jsontree_context *js_ctx,
147  int depth);
148 
149 void jsontree_write_uint(const struct jsontree_context *js_ctx,
150  unsigned int value);
151 void jsontree_write_int(const struct jsontree_context *js_ctx, int value);
152 void jsontree_write_atom(const struct jsontree_context *js_ctx,
153  const char *text);
154 void jsontree_write_string(const struct jsontree_context *js_ctx,
155  const char *text);
156 int jsontree_print_next(struct jsontree_context *js_ctx);
157 struct jsontree_value *jsontree_find_next(struct jsontree_context *js_ctx,
158  int type);
159 
160 #endif /* JSONTREE_H_ */
static uint8_t output(const linkaddr_t *localdest)
Take an IP packet and format it to be sent on an 802.15.4 network using 6lowpan.
Definition: sicslowpan.c:1565
A few JSON defines used for parsing and generating JSON.