├── JsonRPCServer.cpp ├── JsonRPCServer.h ├── LICENSE ├── README.md ├── aJson ├── Examples │ ├── Json_Example │ │ ├── Json_Example.ino │ │ └── Json_Serial.ino │ ├── Json_Serial │ │ └── Json_Serial.ino │ └── MultiLevelParsing │ │ └── MultiLevelParsing.ino ├── README.md ├── aJSON.cpp ├── aJSON.h ├── keywords.txt └── utility │ ├── stringbuffer.c │ └── stringbuffer.h ├── examples └── ToggleLED │ └── ToggleLED.ino └── keywords.txt /JsonRPCServer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | JsonRPCServer.cpp - Simple JSON-RPC Server for Arduino 3 | Created by Meir Tseitlin, March 5, 2014. This code is based on https://code.google.com/p/ajson-rpc/ 4 | Released under GPLv2 license. 5 | */ 6 | #include "Arduino.h" 7 | #include "aJSON.h" 8 | #include "JsonRPCServer.h" 9 | 10 | JsonRPCServer::JsonRPCServer(Stream* stream): _jsonStream(stream) { 11 | 12 | } 13 | 14 | 15 | void JsonRPCServer::begin(int capacity) 16 | { 17 | mymap = (FuncMap *)malloc(sizeof(FuncMap)); 18 | mymap->capacity = capacity; 19 | mymap->used = 0; 20 | mymap->mappings = (Mapping*)malloc(capacity * sizeof(Mapping)); 21 | memset(mymap->mappings, 0, capacity * sizeof(Mapping)); 22 | 23 | // Register all json procedures (pure virtual - implemented in derivatives with macros) 24 | registerProcs(); 25 | } 26 | 27 | void JsonRPCServer::registerMethod(String methodName, JSON_PROC_STATIC_T callback, JSON_RPC_RET_TYPE type) 28 | { 29 | // only write keyvalue pair if we allocated enough memory for it 30 | if (mymap->used < mymap->capacity) 31 | { 32 | Mapping* mapping = &(mymap->mappings[mymap->used++]); 33 | mapping->name = methodName; 34 | mapping->callback = callback; 35 | mapping->retType = type; 36 | } 37 | } 38 | 39 | 40 | void JsonRPCServer::processMessage(aJsonObject *msg) 41 | { 42 | aJsonObject* method = aJson.getObjectItem(msg, "method"); 43 | if (!method) 44 | { 45 | // not a valid Json-RPC message 46 | Serial.flush(); 47 | return; 48 | } 49 | 50 | aJsonObject* params = aJson.getObjectItem(msg, "params"); 51 | if (!params) 52 | { 53 | Serial.flush(); 54 | return; 55 | } 56 | 57 | String methodName = method->valuestring; 58 | for (int i=0; iused; i++) 59 | { 60 | Mapping* mapping = &(mymap->mappings[i]); 61 | if (methodName.equals(mapping->name)) 62 | { 63 | switch (mapping->retType) { 64 | case JSON_RPC_RET_TYPE_NONE: 65 | mapping->callback(this, params); 66 | break; 67 | case JSON_RPC_RET_TYPE_NUMERIC: 68 | { 69 | JSON_PROC_NUM_STATIC_T numCallback = (JSON_PROC_NUM_STATIC_T) mapping->callback; 70 | 71 | int ret = numCallback(this, params); 72 | 73 | // Create answer 74 | aJsonObject *answer = aJson.createObject(); 75 | aJson.addItemToObject(answer, "result", aJson.createItem((int)ret)); 76 | aJson.addItemToObject(answer, "error", aJson.createNull()); 77 | 78 | // Send answer 79 | aJson.print(answer, &_jsonStream); 80 | 81 | // Delete answer 82 | aJson.deleteItem(answer); 83 | } 84 | break; 85 | case JSON_RPC_RET_TYPE_STRING: 86 | { 87 | //String ret = mapping->callback(this, params); 88 | JSON_PROC_STRING_STATIC_T stringCallback = (JSON_PROC_STRING_STATIC_T) mapping->callback; 89 | 90 | String ret = stringCallback(this, params); 91 | 92 | // Create answer 93 | aJsonObject *answer = aJson.createObject(); 94 | aJson.addItemToObject(answer, "result", aJson.createItem(ret.c_str())); 95 | aJson.addItemToObject(answer, "error", aJson.createNull()); 96 | 97 | // Send answer 98 | aJson.print(answer, &_jsonStream); 99 | 100 | // Delete answer 101 | aJson.deleteItem(answer); 102 | 103 | } 104 | break; 105 | } 106 | 107 | return; 108 | } 109 | } 110 | } 111 | 112 | void JsonRPCServer::process() { 113 | 114 | /* add main program code here */ 115 | if (_jsonStream.available()) { 116 | 117 | // skip any accidental whitespace like newlines 118 | _jsonStream.skip(); 119 | } 120 | 121 | if (_jsonStream.available()) { 122 | 123 | aJsonObject *msg = aJson.parse(&_jsonStream); 124 | if (msg) { 125 | processMessage(msg); 126 | aJson.deleteItem(msg); 127 | } else { 128 | Serial.flush(); 129 | } 130 | } 131 | 132 | } -------------------------------------------------------------------------------- /JsonRPCServer.h: -------------------------------------------------------------------------------- 1 | /* 2 | JsonRPCServer.h - Simple JSON-RPC Server for Arduino 3 | Created by Meir Tseitlin, March 5, 2014. This code is based on https://code.google.com/p/ajson-rpc/ 4 | Released under GPLv2 license. 5 | */ 6 | 7 | #ifndef JsonRPC_h 8 | #define JsonRPC_h 9 | 10 | #include "Arduino.h" 11 | #include 12 | #include "aJSON.h" 13 | 14 | enum JSON_RPC_RET_TYPE { 15 | JSON_RPC_RET_TYPE_NONE, 16 | JSON_RPC_RET_TYPE_NUMERIC, 17 | JSON_RPC_RET_TYPE_STRING 18 | }; 19 | 20 | class JsonRPCServer; 21 | 22 | typedef void (JsonRPCServer::*JSON_PROC_T)(aJsonObject*); 23 | typedef void (*JSON_PROC_STATIC_T)(JsonRPCServer*, aJsonObject*); 24 | 25 | typedef int (JsonRPCServer::*JSON_PROC_NUM_T)(aJsonObject*); 26 | typedef int (*JSON_PROC_NUM_STATIC_T)(JsonRPCServer*, aJsonObject*); 27 | 28 | typedef String (JsonRPCServer::*JSON_PROC_STRING_T)(aJsonObject*); 29 | typedef String (*JSON_PROC_STRING_STATIC_T)(JsonRPCServer*, aJsonObject*); 30 | 31 | 32 | struct Mapping 33 | { 34 | String name; 35 | JSON_PROC_STATIC_T callback; 36 | JSON_RPC_RET_TYPE retType; 37 | }; 38 | 39 | struct FuncMap 40 | { 41 | Mapping* mappings; 42 | unsigned int capacity; 43 | unsigned int used; 44 | }; 45 | 46 | 47 | #define DECLARE_JSON_PROC(CONTAINER_NAME, METHOD_NAME, RET_TYPE) \ 48 | RET_TYPE METHOD_NAME(aJsonObject* params); \ 49 | static RET_TYPE stat_##METHOD_NAME(CONTAINER_NAME* container, aJsonObject* params) { \ 50 | return container->METHOD_NAME(params); \ 51 | } 52 | 53 | #define BEGIN_JSON_REGISTRATION \ 54 | protected: \ 55 | void registerProcs() { 56 | 57 | #define REGISTER_JSON_PROC(METHOD_NAME, RET_TYPE) \ 58 | registerMethod(#METHOD_NAME, (JSON_PROC_STATIC_T) &stat_##METHOD_NAME, RET_TYPE) 59 | 60 | 61 | #define END_JSON_REGISTRATION \ 62 | } 63 | 64 | class JsonRPCServer 65 | { 66 | public: 67 | 68 | JsonRPCServer(Stream* stream); 69 | 70 | void begin(int capacity); 71 | void process(); 72 | protected: 73 | void registerMethod(String methodName, JSON_PROC_STATIC_T callback, JSON_RPC_RET_TYPE type = JSON_RPC_RET_TYPE_NONE); 74 | void processMessage(aJsonObject *msg); 75 | virtual void registerProcs() = 0; 76 | private: 77 | FuncMap* mymap; 78 | aJsonStream _jsonStream; 79 | }; 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | arduino-json-rpc 2 | ================ 3 | 4 | Simple JSON-RPC implementation for Arduino over serial line. 5 | Useful for controlling Arduino from another PC, Single Board Computer (e.g. Beaglebone) or over RF communication 6 | 7 | Read more: http://www.cloud-rocket.com/2014/03/serial-json-rpc-server-arduino/ -------------------------------------------------------------------------------- /aJson/Examples/Json_Example/Json_Example.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | #include 5 | 6 | const prog_char PROGMEM RETRIEVING_NAME[] ="Retrieving name\n"; 7 | const prog_char PROGMEM ERROR_RETRIEVING_NAME[] ="Error retrieving name\n"; 8 | const prog_char PROGMEM SUCCESSFULLY_RETRIEVED_NAME[] ="Successfully retrieved Name:"; 9 | const prog_char PROGMEM PARSING_OBJECT[] ="Parsing String\n"; 10 | const prog_char PROGMEM ERROR_PARSING_OBJECT[] ="Error parsing Object\n"; 11 | const prog_char PROGMEM SUCCESSFULLY_PARSED_OBJECT[] ="Successfully parsed Object\n"; 12 | const prog_char PROGMEM DELETING_OBJECT_STRING[] = "Deleting the object\n"; 13 | const prog_char PROGMEM FORMAT_FAILED_STRING[] = "Failed to create Format Object\n"; 14 | const prog_char PROGMEM OUTPUT_STRING_ERROR[] = "Error creating output String\n"; 15 | const prog_char PROGMEM RESULT_PRINTING_STRING[] = "Printing the result:\n"; 16 | const prog_char PROGMEM ADDING_FRAMERATE_STRING[] = "Adding frame rate to the format\n"; 17 | const prog_char PROGMEM ADDING_INTERLACE_STRING[] = "Adding interlace to the format\n"; 18 | const prog_char PROGMEM ADDING_HEIGHT_STRING[] = "Adding height to the format\n"; 19 | const prog_char PROGMEM ADDING_WIDTH_STRING[] = "Adding width to the format\n"; 20 | const prog_char PROGMEM ADDING_TYPE_STRING[] = "Adding type to the format\n"; 21 | const prog_char PROGMEM ADDING_FORMAT_STRING[] = "Adding format to the object\n"; 22 | const prog_char PROGMEM ADDING_LENGTH_STRING[] = "Adding length to the object\n"; 23 | const prog_char PROGMEM CREATING_FROMAT_STRING[] = "Creating format object\n"; 24 | const prog_char PROGMEM ADDING_NAME_STRING[] = "Adding name to the object\n"; 25 | const prog_char PROGMEM OBJECT_CREATION_FAILED_STRING[] = "Failed to create the object\n"; 26 | const prog_char PROGMEM OBJECT_CREATE_STRING[] = "Created a Object\n"; 27 | const prog_char PROGMEM HELLO_STRING[] = "********************\nTesting aJson\n*****************\n"; 28 | 29 | // The setup() method runs once, when the sketch starts 30 | void setup() { 31 | Serial.begin(9600); 32 | printProgStr(HELLO_STRING); 33 | freeMem("start"); 34 | } 35 | 36 | void testObjects() { 37 | 38 | aJsonObject* root = aJson.createObject(); 39 | if (root != NULL) { 40 | printProgStr( OBJECT_CREATE_STRING); 41 | } 42 | else { 43 | printProgStr( OBJECT_CREATION_FAILED_STRING); 44 | return; 45 | } 46 | printProgStr( ADDING_NAME_STRING); 47 | aJson.addItemToObject(root, "name", aJson.createItem( 48 | "Jack (\"Bee\") Nimble")); 49 | printProgStr( CREATING_FROMAT_STRING); 50 | aJsonObject* fmt = aJson.createObject(); 51 | if (fmt != NULL) { 52 | printProgStr( ADDING_FORMAT_STRING); 53 | aJson.addItemToObject(root, "format", fmt); 54 | printProgStr( ADDING_TYPE_STRING); 55 | aJson.addStringToObject(fmt, "type", "rect"); 56 | printProgStr( ADDING_WIDTH_STRING); 57 | aJson.addNumberToObject(fmt, "width", 1920); 58 | printProgStr( ADDING_HEIGHT_STRING); 59 | aJson.addNumberToObject(fmt, "height", 1080); 60 | printProgStr( ADDING_INTERLACE_STRING); 61 | aJson.addFalseToObject(fmt, "interlace"); 62 | printProgStr( ADDING_FRAMERATE_STRING); 63 | aJson.addNumberToObject(fmt, "frame rate", 24); 64 | printProgStr( ADDING_LENGTH_STRING); 65 | aJson.addNumberToObject(fmt, "length", 1.29); 66 | } 67 | else { 68 | printProgStr( FORMAT_FAILED_STRING); 69 | return; 70 | } 71 | 72 | freeMem("with object"); 73 | printProgStr( RESULT_PRINTING_STRING); 74 | char* string = aJson.print(root); 75 | if (string != NULL) { 76 | Serial.println(string); 77 | } 78 | else { 79 | printProgStr( OUTPUT_STRING_ERROR); 80 | } 81 | 82 | printProgStr( DELETING_OBJECT_STRING); 83 | aJson.deleteItem(root); 84 | freeMem("after deletion"); 85 | 86 | printProgStr(PARSING_OBJECT); 87 | Serial.println(string); 88 | root = aJson.parse(string); 89 | free(string); 90 | freeMem("after printing"); 91 | if (root != NULL) { 92 | printProgStr(SUCCESSFULLY_PARSED_OBJECT); 93 | } 94 | else { 95 | printProgStr( ERROR_PARSING_OBJECT); 96 | return; 97 | } 98 | printProgStr( RESULT_PRINTING_STRING); 99 | string = aJson.print(root); 100 | if (string != NULL) { 101 | Serial.println(string); 102 | free(string); 103 | } 104 | else { 105 | printProgStr( OUTPUT_STRING_ERROR); 106 | } 107 | freeMem("after 2nd printing"); 108 | 109 | aJsonObject* name = aJson.getObjectItem(root, "name"); 110 | if (name != NULL) { 111 | printProgStr( SUCCESSFULLY_RETRIEVED_NAME); 112 | Serial.println(name->valuestring); 113 | } 114 | else { 115 | printProgStr( ERROR_RETRIEVING_NAME); 116 | } 117 | 118 | printProgStr( DELETING_OBJECT_STRING); 119 | aJson.deleteItem(root); 120 | freeMem("after deleting object"); 121 | } 122 | 123 | void testArrays() { 124 | aJsonObject* root = aJson.createArray(); 125 | if (root != NULL) { 126 | printProgStr( OBJECT_CREATE_STRING); 127 | } 128 | else { 129 | printProgStr( OBJECT_CREATION_FAILED_STRING); 130 | return; 131 | } 132 | aJsonObject* day; 133 | day=aJson.createItem("Monday"); 134 | aJson.addItemToArray(root, day); 135 | day=aJson.createItem("Tuesday"); 136 | aJson.addItemToArray(root, day); 137 | day=aJson.createItem("Wednesday"); 138 | aJson.addItemToArray(root, day); 139 | day=aJson.createItem("Thursday"); 140 | aJson.addItemToArray(root, day); 141 | day=aJson.createItem("Friday"); 142 | aJson.addItemToArray(root, day); 143 | day=aJson.createItem("Saturday"); 144 | aJson.addItemToArray(root, day); 145 | day=aJson.createItem("Sunday"); 146 | aJson.addItemToArray(root, day); 147 | 148 | freeMem("with object"); 149 | printProgStr( RESULT_PRINTING_STRING); 150 | 151 | char* string = aJson.print(root); 152 | if (string != NULL) { 153 | Serial.println(string); 154 | } 155 | else { 156 | printProgStr( OUTPUT_STRING_ERROR); 157 | } 158 | 159 | printProgStr( DELETING_OBJECT_STRING); 160 | freeMem("after printing"); 161 | 162 | aJson.deleteItem(root); 163 | freeMem("after deletion"); 164 | 165 | printProgStr(PARSING_OBJECT); 166 | Serial.println(string); 167 | root = aJson.parse(string); 168 | 169 | free(string); 170 | freeMem("after parsing"); 171 | 172 | if (root != NULL) { 173 | printProgStr(SUCCESSFULLY_PARSED_OBJECT); 174 | } 175 | else { 176 | printProgStr( ERROR_PARSING_OBJECT); 177 | return; 178 | } 179 | printProgStr( RESULT_PRINTING_STRING); 180 | 181 | string = aJson.print(root); 182 | if (string != NULL) { 183 | Serial.println(string); 184 | free(string); 185 | } 186 | else { 187 | printProgStr( OUTPUT_STRING_ERROR); 188 | } 189 | 190 | aJson.deleteItem(root); 191 | freeMem("after 2nd printing"); 192 | } 193 | 194 | 195 | // the loop() method runs over and over again, 196 | // as long as the Arduino has power 197 | void loop() { 198 | testObjects(); 199 | delay(1000); 200 | testArrays(); 201 | delay(1000); 202 | } 203 | 204 | // given a PROGMEM string, use Serial.print() to send it out 205 | // this is needed to save precious memory 206 | //htanks to todbot for this http://todbot.com/blog/category/programming/ 207 | void printProgStr(const prog_char* str) { 208 | char c; 209 | if (!str) { 210 | return; 211 | } 212 | while ((c = pgm_read_byte(str))) { 213 | Serial.write(c); 214 | str++; 215 | } 216 | } 217 | 218 | //Code to print out the free memory 219 | 220 | struct __freelist { 221 | size_t sz; 222 | struct __freelist *nx; 223 | }; 224 | 225 | extern char * const __brkval; 226 | extern struct __freelist *__flp; 227 | 228 | uint16_t freeMem(uint16_t *biggest) 229 | { 230 | char *brkval; 231 | char *cp; 232 | unsigned freeSpace; 233 | struct __freelist *fp1, *fp2; 234 | 235 | brkval = __brkval; 236 | if (brkval == 0) { 237 | brkval = __malloc_heap_start; 238 | } 239 | cp = __malloc_heap_end; 240 | if (cp == 0) { 241 | cp = ((char *)AVR_STACK_POINTER_REG) - __malloc_margin; 242 | } 243 | if (cp <= brkval) return 0; 244 | 245 | freeSpace = cp - brkval; 246 | 247 | for (*biggest = 0, fp1 = __flp, fp2 = 0; 248 | fp1; 249 | fp2 = fp1, fp1 = fp1->nx) { 250 | if (fp1->sz > *biggest) *biggest = fp1->sz; 251 | freeSpace += fp1->sz; 252 | } 253 | 254 | return freeSpace; 255 | } 256 | 257 | uint16_t biggest; 258 | 259 | void freeMem(char* message) { 260 | Serial.print(message); 261 | Serial.print(":\t"); 262 | Serial.println(freeMem(&biggest)); 263 | } 264 | 265 | 266 | 267 | -------------------------------------------------------------------------------- /aJson/Examples/Json_Example/Json_Serial.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Sample sketch communicating over Serial using JSON 3 | 4 | This sketch communicates over serial link with a computer, sending 5 | JSON-encoded state of its analog pints every second and accepting 6 | per-line JSON messages with desired values of PWM pins on input. 7 | 8 | Circuit: 9 | * (Optional) Analog sensors attached to analog pin. 10 | * (Optional) LEDs attached to PWM pins 9 and 8. 11 | 12 | created 1 November 2012 13 | by Petr Baudis 14 | 15 | https://github.com/interactive-matter/ajson 16 | This code is in the public domain. 17 | */ 18 | 19 | #include 20 | 21 | unsigned long last_print = 0; 22 | aJsonStream serial_stream(&Serial); 23 | 24 | void setup() 25 | { 26 | Serial.begin(9600); 27 | } 28 | 29 | /* Generate message like: { "analog": [0, 200, 400, 600, 800, 1000] } */ 30 | aJsonObject *createMessage() 31 | { 32 | aJsonObject *msg = aJson.createObject(); 33 | 34 | int analogValues[6]; 35 | for (int i = 0; i < 6; i++) { 36 | analogValues[i] = analogRead(i); 37 | } 38 | aJsonObject *analog = aJson.createIntArray(analogValues, 6); 39 | aJson.addItemToObject(msg, "analog", analog); 40 | 41 | return msg; 42 | } 43 | 44 | /* Process message like: { "pwm": { "8": 0, "9": 128 } } */ 45 | void processMessage(aJsonObject *msg) 46 | { 47 | aJsonObject *pwm = aJson.getObjectItem(msg, "pwm"); 48 | if (!pwm) { 49 | Serial.println("no pwm data"); 50 | return; 51 | } 52 | 53 | const static int pins[] = { 8, 9 }; 54 | const static int pins_n = 2; 55 | for (int i = 0; i < pins_n; i++) { 56 | char pinstr[3]; 57 | snprintf(pinstr, sizeof(pinstr), "%d", pins[i]); 58 | 59 | aJsonObject *pwmval = aJson.getObjectItem(pwm, pinstr); 60 | if (!pwmval) continue; /* Value not provided, ok. */ 61 | if (pwmval->type != aJson_Int) { 62 | Serial.print("invalid data type "); 63 | Serial.print(pwmval->type, DEC); 64 | Serial.print(" for pin "); 65 | Serial.println(pins[i], DEC); 66 | continue; 67 | } 68 | 69 | Serial.print("setting pin "); 70 | Serial.print(pins[i], DEC); 71 | Serial.print(" to value "); 72 | Serial.println(pwmval->valueint, DEC); 73 | analogWrite(pins[i], pwmval->valueint); 74 | } 75 | } 76 | 77 | void loop() 78 | { 79 | if (millis() - last_print > 1000) { 80 | /* One second elapsed, send message. */ 81 | aJsonObject *msg = createMessage(); 82 | aJson.print(msg, &serial_stream); 83 | Serial.println(); /* Add newline. */ 84 | aJson.deleteItem(msg); 85 | last_print = millis(); 86 | } 87 | 88 | if (serial_stream.available()) { 89 | /* First, skip any accidental whitespace like newlines. */ 90 | serial_stream.skip(); 91 | } 92 | 93 | if (serial_stream.available()) { 94 | /* Something real on input, let's take a look. */ 95 | aJsonObject *msg = aJson.parse(&serial_stream); 96 | processMessage(msg); 97 | aJson.deleteItem(msg); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /aJson/Examples/Json_Serial/Json_Serial.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Sample sketch communicating over Serial using JSON 3 | 4 | This sketch communicates over serial link with a computer, sending 5 | JSON-encoded state of its analog pints every second and accepting 6 | per-line JSON messages with desired values of PWM pins on input. 7 | 8 | Circuit: 9 | * (Optional) Analog sensors attached to analog pin. 10 | * (Optional) LEDs attached to PWM pins 9 and 8. 11 | 12 | created 1 November 2012 13 | by Petr Baudis 14 | 15 | https://github.com/interactive-matter/ajson 16 | This code is in the public domain. 17 | */ 18 | 19 | #include 20 | 21 | unsigned long last_print = 0; 22 | aJsonStream serial_stream(&Serial); 23 | 24 | void setup() 25 | { 26 | Serial.begin(9600); 27 | } 28 | 29 | /* Generate message like: { "analog": [0, 200, 400, 600, 800, 1000] } */ 30 | aJsonObject *createMessage() 31 | { 32 | aJsonObject *msg = aJson.createObject(); 33 | 34 | int analogValues[6]; 35 | for (int i = 0; i < 6; i++) { 36 | analogValues[i] = analogRead(i); 37 | } 38 | aJsonObject *analog = aJson.createIntArray(analogValues, 6); 39 | aJson.addItemToObject(msg, "analog", analog); 40 | 41 | return msg; 42 | } 43 | 44 | /* Process message like: { "pwm": { "8": 0, "9": 128 } } */ 45 | void processMessage(aJsonObject *msg) 46 | { 47 | aJsonObject *pwm = aJson.getObjectItem(msg, "pwm"); 48 | if (!pwm) { 49 | Serial.println("no pwm data"); 50 | return; 51 | } 52 | 53 | const static int pins[] = { 8, 9 }; 54 | const static int pins_n = 2; 55 | for (int i = 0; i < pins_n; i++) { 56 | char pinstr[3]; 57 | snprintf(pinstr, sizeof(pinstr), "%d", pins[i]); 58 | 59 | aJsonObject *pwmval = aJson.getObjectItem(pwm, pinstr); 60 | if (!pwmval) continue; /* Value not provided, ok. */ 61 | if (pwmval->type != aJson_Int) { 62 | Serial.print("invalid data type "); 63 | Serial.print(pwmval->type, DEC); 64 | Serial.print(" for pin "); 65 | Serial.println(pins[i], DEC); 66 | continue; 67 | } 68 | 69 | Serial.print("setting pin "); 70 | Serial.print(pins[i], DEC); 71 | Serial.print(" to value "); 72 | Serial.println(pwmval->valueint, DEC); 73 | analogWrite(pins[i], pwmval->valueint); 74 | } 75 | } 76 | 77 | void loop() 78 | { 79 | if (millis() - last_print > 1000) { 80 | /* One second elapsed, send message. */ 81 | aJsonObject *msg = createMessage(); 82 | aJson.print(msg, &serial_stream); 83 | Serial.println(); /* Add newline. */ 84 | aJson.deleteItem(msg); 85 | last_print = millis(); 86 | } 87 | 88 | if (serial_stream.available()) { 89 | /* First, skip any accidental whitespace like newlines. */ 90 | serial_stream.skip(); 91 | } 92 | 93 | if (serial_stream.available()) { 94 | /* Something real on input, let's take a look. */ 95 | aJsonObject *msg = aJson.parse(&serial_stream); 96 | processMessage(msg); 97 | aJson.deleteItem(msg); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /aJson/Examples/MultiLevelParsing/MultiLevelParsing.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Sample sketch to demonstrate Multi Level JSON parsing in Arduino 3 | 4 | This sketch parses the complexly nested JSON 5 | 6 | Libraries needed: 7 | 8 | - aJson library for JSON parsing - https://github.com/interactive-matter/aJson 9 | 10 | Circuit: 11 | 12 | You don't need any circuit, but need the Arduino board 13 | 14 | Author: 15 | 16 | Sudar - 17 | Refer to http://hardwarefun.com/tutorials/parsing-json-in-arduino 18 | 19 | License: 20 | 21 | BeerWare ;) 22 | 23 | */ 24 | #include 25 | 26 | // function definitions 27 | char* parseJson(char *jsonString) ; 28 | 29 | // Json string to parse 30 | char jsonString[] = "{\"query\":{\"count\":1,\"created\":\"2012-08-04T14:46:03Z\",\"lang\":\"en-US\",\"results\":{\"item\":{\"title\":\"Handling FTP usernames with @ in them\"}}}}"; 31 | 32 | void setup() { 33 | Serial.begin(9600); 34 | Serial.println(jsonString); 35 | Serial.println("Starting to parse"); 36 | 37 | char* value = parseJson(jsonString); 38 | 39 | if (value) { 40 | Serial.print(F("Successfully Parsed: ")); 41 | Serial.println(value); 42 | } else { 43 | Serial.print(F("There was some problem in parsing the JSON")); 44 | } 45 | } 46 | 47 | /** 48 | * Parse the JSON String. Uses aJson library 49 | * 50 | * Refer to http://hardwarefun.com/tutorials/parsing-json-in-arduino 51 | */ 52 | char* parseJson(char *jsonString) 53 | { 54 | char* value; 55 | 56 | aJsonObject* root = aJson.parse(jsonString); 57 | 58 | if (root != NULL) { 59 | //Serial.println("Parsed successfully 1 " ); 60 | aJsonObject* query = aJson.getObjectItem(root, "query"); 61 | 62 | if (query != NULL) { 63 | //Serial.println("Parsed successfully 2 " ); 64 | aJsonObject* results = aJson.getObjectItem(query, "results"); 65 | 66 | if (results != NULL) { 67 | //Serial.println("Parsed successfully 3 " ); 68 | aJsonObject* item = aJson.getObjectItem(results, "item"); 69 | 70 | if (item != NULL) { 71 | //Serial.println("Parsed successfully 4 " ); 72 | aJsonObject* title = aJson.getObjectItem(item, "title"); 73 | 74 | if (title != NULL) { 75 | //Serial.println("Parsed successfully 5 " ); 76 | value = title->valuestring; 77 | } 78 | } 79 | } 80 | } 81 | } 82 | 83 | if (value) { 84 | return value; 85 | } else { 86 | return NULL; 87 | } 88 | } 89 | 90 | void loop() { 91 | // Nothing to do 92 | ; 93 | } 94 | -------------------------------------------------------------------------------- /aJson/README.md: -------------------------------------------------------------------------------- 1 | aJson v1.0 2 | ================ 3 | Copyright (c) 2010, Interactive Matter, Marcus Nowotny 4 | 5 | Based on the cJSON Library, Copyright (C) 2009 Dave Gamble 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | Welcome to aJson. 26 | ================ 27 | 28 | aJson is the attempt to port a complete JSON implementation to Arduino. It is based on the cJSON implementation, reduced in size and removing one or two features: 29 | 30 | - The code has very limited support on ATmega168 - there is just not enough memory and 31 | memory fragmentation is a serious problem 32 | - Arrays and Lists are max 255 elements big 33 | - There is no proper Unicode handling in this code 34 | - There is an internal buffer eating up 256 bytes of ram 35 | 36 | Most of the limitation will be gone in one of the future releases. 37 | 38 | JSON is described best here: http://www.json.org/ 39 | It's like XML, but fat-free. You use it to move data around, store things, or just 40 | generally represent your program's state. 41 | JSON is especially useful to exchange data efficiently with e.g. JavaScript, Java, C++, 42 | Processing or anything else 43 | 44 | aJson is a library to receive, understand, create or modify JSON strings directly in the 45 | Arduino. JSON is quite a standard, so that is perfect for exchanging data with other 46 | applications. I combination with HTTP it is suitable to implement REST Web Services. 47 | 48 | aJson provides functions to parse JSON strings to object models. Handle, search and 49 | create and modify JSON Object structures. 50 | 51 | This is some JSON from this page: http://www.json.org/fatfree.html 52 | 53 | ```javascript 54 | { 55 | "name": "Jack (\"Bee\") Nimble", 56 | "format": { 57 | "type": "rect", 58 | "width": 1920, 59 | "height": 1080, 60 | "interlace": false, 61 | "frame rate": 24 62 | } 63 | } 64 | 65 | ``` 66 | 67 | Parsing JSON 68 | ================ 69 | 70 | To parse such a structure with aJson you simply convert it to a object tree: 71 | 72 | ```c 73 | aJsonObject* jsonObject = aJson.parse(json_string); 74 | ``` 75 | 76 | (assuming you got the JSON string in the variable json_string - as a char*) 77 | 78 | This is an object. We're in C. We don't have objects. But we do have structs. 79 | Therefore the objects are translated into structs, with all the drawbacks it brings.s 80 | 81 | Now we can e.g. retrieve the value for name: 82 | 83 | ```c 84 | aJsonObject* name = aJson.getObjectItem(root, "name"); 85 | ``` 86 | 87 | The value of name can be retrieved via: 88 | 89 | ```c 90 | Serial.println(name->valuestring); 91 | ``` 92 | 93 | Note that the aJsonObject has a union which holds all possible value types as 94 | overlays - you can get only useful data for the type which you have at hand. You can get 95 | the type as 96 | 97 | ```c 98 | name->type 99 | ``` 100 | 101 | which can be either aJson_False, aJson_True, aJson_NULL, aJson_Number, aJson_String, aJson_Array 102 | or aJson_Object. For aJson_Number you can use value.number.valueint or value.number.valuedouble, for aJson_String 103 | you can use value.valuestring, for True or False, you can use value.valuebool. 104 | 105 | To render the object back to a string you can simply call 106 | 107 | ```c 108 | char *json_String=aJson.print(jsonObject); 109 | ``` 110 | 111 | Finished? Delete the root (this takes care of everything else). 112 | 113 | ```c 114 | aJson.delete(root); 115 | ``` 116 | 117 | This deletes the objects and all values referenced by it. 118 | 119 | Parsing streams 120 | -------------- 121 | 122 | As you can see this will eat up lots of memory. Storing the original string and the JSON object is a bit too much 123 | for your Arduino - it will most likely use up all the memory. Therefore it is better to parse streams instead of strings. 124 | A stream in C is a FILE* - on Arduino there are some special streams, but later adapters will be provided. 125 | So if you for example read from a FILE* stream you can simply call 126 | 127 | ```c 128 | aJsonObject* jsonObject = aJson.parse(file); 129 | ``` 130 | 131 | By that you will not have to store the JSON string in memory. 132 | 133 | Filtering while parsing 134 | -------------- 135 | 136 | Any JSON respond can have object name/value pairs your code either does not understand or is not interested in. 137 | To avoid those values to go into your memory you can simply add filters to your parsing request. 138 | A set of filter is just a list of names you are interested in, ended by a null value. If you are 139 | only interested in "name", "format", "height" and "width" in the above example you can do it like: 140 | 141 | ```c 142 | char** jsonFilter = {"name,"format","height","width",NULL}; 143 | aJsonObject* jsonObject = aJson.parse(json_string,json_filter); 144 | ``` 145 | (assuming you got the JSON string in the variable json_string - as a char*) 146 | 147 | By that only the following structure is parsed - the rest will be ignored: 148 | 149 | ```javascript 150 | { 151 | "name": "Jack (\"Bee\") Nimble", 152 | "format": { 153 | "width": 1920, 154 | "height": 1080, 155 | } 156 | } 157 | ``` 158 | 159 | It is good practice to always use the filtering feature to parse JSON answers, to avoid unknown objects swamping your 160 | memory. 161 | 162 | Creating JSON Objects from code 163 | ================ 164 | 165 | If you want to see how you'd build this struct in code? 166 | 167 | ```c 168 | aJsonObject *root,*fmt; 169 | root=aJson.createObject(); 170 | aJson.addItemToObject(root, "name", aJson.createItem("Jack (\"Bee\") Nimble")); 171 | aJson.addItemToObject(root, "format", fmt = aJson.createObject()); 172 | aJson.addStringToObject(fmt,"type", "rect"); 173 | aJson.addNumberToObject(fmt,"width", 1920); 174 | aJson.addNumberToObject(fmt,"height", 1080); 175 | aJson.addFalseToObject (fmt,"interlace"); 176 | aJson.addNumberToObject(fmt,"frame rate", 24); 177 | ``` 178 | 179 | The root object has: Object Type and a Child 180 | The Child has name "name", with value "Jack ("Bee") Nimble", and a sibling: 181 | Sibling has type Object, name "format", and a child. 182 | That child has type String, name "type", value "rect", and a sibling: 183 | Sibling has type Number, name "width", value 1920, and a sibling: 184 | Sibling has type Number, name "height", value 1080, and a sibling: 185 | Sibling hs type False, name "interlace", and a sibling: 186 | Sibling has type Number, name "frame rate", value 24 187 | 188 | If you want to create an array it works nearly the same way: 189 | 190 | ```c 191 | aJsonObject* root = aJson.createArray(); 192 | 193 | aJsonObject* day; 194 | day=aJson.createItem("Monday"); 195 | aJson.addItemToArray(root, day); 196 | day=aJson.createItem("Tuesday"); 197 | aJson.addItemToArray(root, day); 198 | day=aJson.createItem("Wednesday"); 199 | aJson.addItemToArray(root, day); 200 | day=aJson.createItem("Thursday"); 201 | aJson.addItemToArray(root, day); 202 | day=aJson.createItem("Friday"); 203 | aJson.addItemToArray(root, day); 204 | day=aJson.createItem("Saturday"); 205 | aJson.addItemToArray(root, day); 206 | day=aJson.createItem("Sunday"); 207 | aJson.addItemToArray(root, day); 208 | ``` 209 | 210 | 211 | The whole library (nicely provided by cJSON) is optimized for easy usage. You can create and modify 212 | the object as easy as possible. 213 | 214 | aJson Data Structures 215 | ================ 216 | 217 | aJson stores JSON objects in struct objects: 218 | 219 | ```c 220 | // The aJson structure: 221 | typedef struct aJsonObject { 222 | char *name; // The item's name string, if this item is the child of, or is in the list of subitems of an object. 223 | struct aJsonObject *next, *prev; // next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem 224 | struct aJsonObject *child; // An array or object item will have a child pointer pointing to a chain of the items in the array/object. 225 | 226 | char type; // The type of the item, as above. 227 | 228 | union { 229 | char *valuestring; // The item's string, if type==aJson_String 230 | char valuebool; //the items value for true & false 231 | int valueint; // The item's number, if type==aJson_Number 232 | float valuefloat; // The item's number, if type==aJson_Number 233 | }; 234 | } aJsonObject; 235 | ``` 236 | 237 | By default all values are 0 unless set by virtue of being meaningful. 238 | 239 | Note that the aJsonObject has a union 'value' which holds all possible value types as 240 | overlays - you can get only useful data for the type which you have at hand. You can get 241 | the type as 242 | 243 | ```c 244 | name->type 245 | ``` 246 | 247 | which can be either aJson_False, aJson_True, aJson_NULL, aJson_Number, aJson_String, aJson_Array 248 | or aJson_Object. For aJson_Number you can use value.number.valueint or value.number.valuedouble. 249 | If you're expecting an int, read valueint, if not read valuedouble. For aJson_String 250 | you can use value.valuestring, for True or False, you can use value.valuebool. 251 | 252 | next/prev is a doubly linked list of siblings. next takes you to your sibling, 253 | prev takes you back from your sibling to you. 254 | Only objects and arrays have a "child", and it's the head of the doubly linked list. 255 | A "child" entry will have prev==0, but next potentially points on. The last sibling has next=0. 256 | The type expresses Null/True/False/Number/String/Array/Object, all of which are #defined in 257 | aJson.h 258 | 259 | Any entry which is in the linked list which is the child of an object will have a "string" 260 | which is the "name" of the entry. When I said "name" in the above example, that's "string". 261 | "string" is the JSON name for the 'variable name' if you will. 262 | 263 | Now you can trivially walk the lists, recursively, and parse as you please. 264 | You can invoke aJson.parse to get aJson to parse for you, and then you can take 265 | the root object, and traverse the structure (which is, formally, an N-tree), 266 | and tokenise as you please. 267 | 268 | Lists in aJson 269 | ================ 270 | 271 | Lists are easily handled in aJson, to create a list you can simply use the provided API functions: 272 | 273 | ```c 274 | aJson.createArray(objects,24); 275 | ``` 276 | 277 | You simply pass a array of the respective type: char*[], int[] and so on. 278 | 279 | aJSON doesn't make any assumptions about what order you create things in. 280 | You can attach the objects, as above, and later add children to each 281 | of those objects with 282 | 283 | ```c 284 | aJson.addItemToArray() 285 | ``` 286 | 287 | or remove them with 288 | 289 | ```c 290 | aJson.deleteItemFromArray() - which also deletes the objects, or 291 | aJson.detachItemFromArray() - which does not free the memory 292 | ``` 293 | 294 | As soon as you call aJson.print(), it renders the structure to text. 295 | 296 | 297 | Have Fun! 298 | -------------------------------------------------------------------------------- /aJson/aJSON.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloud-rocket/arduino-json-rpc/74b970d627b82442c3fc29f5253e5744479a05c0/aJson/aJSON.cpp -------------------------------------------------------------------------------- /aJson/aJSON.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2001, Interactive Matter, Marcus Nowotny 3 | 4 | Based on the cJSON Library, Copyright (C) 2009 Dave Gamble 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef aJson__h 26 | #define aJson__h 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | /****************************************************************************** 33 | * Definitions 34 | ******************************************************************************/ 35 | // aJson Types: 36 | #define aJson_False 0 37 | #define aJson_True 1 38 | #define aJson_NULL 2 39 | #define aJson_Int 3 40 | #define aJson_Float 4 41 | #define aJson_String 5 42 | #define aJson_Array 6 43 | #define aJson_Object 7 44 | 45 | #define aJson_IsReference 128 46 | 47 | #ifndef EOF 48 | #define EOF -1 49 | #endif 50 | 51 | // The aJson structure: 52 | typedef struct aJsonObject { 53 | char *name; // The item's name string, if this item is the child of, or is in the list of subitems of an object. 54 | struct aJsonObject *next, *prev; // next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem 55 | struct aJsonObject *child; // An array or object item will have a child pointer pointing to a chain of the items in the array/object. 56 | 57 | char type; // The type of the item, as above. 58 | 59 | union { 60 | char *valuestring; // The item's string, if type==aJson_String 61 | char valuebool; //the items value for true & false 62 | int valueint; // The item's number, if type==aJson_Number 63 | double valuefloat; // The item's number, if type==aJson_Number 64 | }; 65 | } aJsonObject; 66 | 67 | /* aJsonStream is stream representation of aJson for its internal use; 68 | * it is meant to abstract out differences between Stream (e.g. serial 69 | * stream) and Client (which may or may not be connected) or provide even 70 | * stream-ish interface to string buffers. */ 71 | class aJsonStream : public Print { 72 | public: 73 | aJsonStream(Stream *stream_) 74 | : stream_obj(stream_), bucket(EOF) 75 | {} 76 | /* Use this to check if more data is available, as aJsonStream 77 | * can read some more data than really consumed and automatically 78 | * skips separating whitespace if you use this method. */ 79 | virtual bool available(); 80 | 81 | int parseNumber(aJsonObject *item); 82 | int printInt(aJsonObject *item); 83 | int printFloat(aJsonObject *item); 84 | 85 | int parseString(aJsonObject *item); 86 | int printStringPtr(const char *str); 87 | int printString(aJsonObject *item); 88 | 89 | int skip(); 90 | 91 | int parseValue(aJsonObject *item, char** filter); 92 | int printValue(aJsonObject *item); 93 | 94 | int parseArray(aJsonObject *item, char** filter); 95 | int printArray(aJsonObject *item); 96 | 97 | int parseObject(aJsonObject *item, char** filter); 98 | int printObject(aJsonObject *item); 99 | 100 | protected: 101 | /* Blocking load of character, returning EOF if the stream 102 | * is exhausted. */ 103 | /* Base implementation just looks at bucket, returns EOF 104 | * otherwise; descendats take care of the real reading. */ 105 | virtual int getch(); 106 | virtual size_t readBytes(uint8_t *buffer, size_t len); 107 | /* Return the character back to the front of the stream 108 | * after loading it with getch(). Only returning a single 109 | * character is supported. */ 110 | virtual void ungetch(char ch); 111 | 112 | /* Inherited from class Print. */ 113 | virtual size_t write(uint8_t ch); 114 | 115 | /* stream attribute is used only from virtual functions, 116 | * therefore an object inheriting aJsonStream may avoid 117 | * using streams completely. */ 118 | Stream *stream_obj; 119 | /* Use this accessor for stream retrieval; some subclasses 120 | * may use their own stream subclass. */ 121 | virtual inline Stream *stream() { return stream_obj; } 122 | 123 | /* bucket is EOF by default. Otherwise, it is a character 124 | * to be returned by next getch() - returned by a call 125 | * to ungetch(). */ 126 | int bucket; 127 | }; 128 | 129 | /* JSON stream that consumes data from a connection (usually 130 | * Ethernet client) until the connection is closed. */ 131 | class aJsonClientStream : public aJsonStream { 132 | public: 133 | aJsonClientStream(Client *stream_) 134 | : aJsonStream(NULL), client_obj(stream_) 135 | {} 136 | 137 | private: 138 | virtual int getch(); 139 | 140 | Client *client_obj; 141 | virtual inline Client *stream() { return client_obj; } 142 | }; 143 | 144 | /* JSON stream that is bound to input and output string buffer. This is 145 | * for internal usage by string-based aJsonClass methods. */ 146 | /* TODO: Elastic output buffer support. */ 147 | class aJsonStringStream : public aJsonStream { 148 | public: 149 | /* Either of inbuf, outbuf can be NULL if you do not care about 150 | * particular I/O direction. */ 151 | aJsonStringStream(char *inbuf_, char *outbuf_ = NULL, size_t outbuf_len_ = 0) 152 | : aJsonStream(NULL), inbuf(inbuf_), outbuf(outbuf_), outbuf_len(outbuf_len_) 153 | { 154 | inbuf_len = inbuf ? strlen(inbuf) : 0; 155 | } 156 | 157 | virtual bool available(); 158 | 159 | private: 160 | virtual int getch(); 161 | virtual size_t write(uint8_t ch); 162 | 163 | char *inbuf, *outbuf; 164 | size_t inbuf_len, outbuf_len; 165 | }; 166 | 167 | class aJsonClass { 168 | /****************************************************************************** 169 | * Constructors 170 | ******************************************************************************/ 171 | 172 | /****************************************************************************** 173 | * User API 174 | ******************************************************************************/ 175 | public: 176 | // Supply a block of JSON, and this returns a aJson object you can interrogate. Call aJson.deleteItem when finished. 177 | aJsonObject* parse(aJsonStream* stream); //Reads from a stream 178 | aJsonObject* parse(aJsonStream* stream,char** filter_values); //Read from a file, but only return values include in the char* array filter_values 179 | aJsonObject* parse(char *value); //Reads from a string 180 | // Render a aJsonObject entity to text for transfer/storage. Free the char* when finished. 181 | int print(aJsonObject *item, aJsonStream* stream); 182 | char* print(aJsonObject* item); 183 | //Renders a aJsonObject directly to a output stream 184 | char stream(aJsonObject *item, aJsonStream* stream); 185 | // Delete a aJsonObject entity and all sub-entities. 186 | void deleteItem(aJsonObject *c); 187 | 188 | // Returns the number of items in an array (or object). 189 | unsigned char getArraySize(aJsonObject *array); 190 | // Retrieve item number "item" from array "array". Returns NULL if unsuccessful. 191 | aJsonObject* getArrayItem(aJsonObject *array, unsigned char item); 192 | // Get item "string" from object. Case insensitive. 193 | aJsonObject* getObjectItem(aJsonObject *object, const char *string); 194 | 195 | // These calls create a aJsonObject item of the appropriate type. 196 | aJsonObject* createNull(); 197 | aJsonObject* createTrue(); 198 | aJsonObject* createFalse(); 199 | aJsonObject* createItem(char b); 200 | aJsonObject* createItem(int num); 201 | aJsonObject* createItem(double num); 202 | aJsonObject* createItem(const char *string); 203 | aJsonObject* createArray(); 204 | aJsonObject* createObject(); 205 | 206 | // These utilities create an Array of count items. 207 | aJsonObject* createIntArray(int *numbers, unsigned char count); 208 | aJsonObject* createFloatArray(double *numbers, unsigned char count); 209 | aJsonObject* createDoubleArray(double *numbers, unsigned char count); 210 | aJsonObject* createStringArray(const char **strings, unsigned char count); 211 | 212 | // Append item to the specified array/object. 213 | void addItemToArray(aJsonObject *array, aJsonObject *item); 214 | void addItemToObject(aJsonObject *object, const char *string, 215 | aJsonObject *item); 216 | // Append reference to item to the specified array/object. Use this when you want to add an existing aJsonObject to a new aJsonObject, but don't want to corrupt your existing aJsonObject. 217 | void addItemReferenceToArray(aJsonObject *array, aJsonObject *item); 218 | void addItemReferenceToObject(aJsonObject *object, const char *string, 219 | aJsonObject *item); 220 | 221 | // Remove/Detach items from Arrays/Objects. 222 | aJsonObject* detachItemFromArray(aJsonObject *array, unsigned char which); 223 | void deleteItemFromArray(aJsonObject *array, unsigned char which); 224 | aJsonObject* detachItemFromObject(aJsonObject *object, const char *string); 225 | void deleteItemFromObject(aJsonObject *object, const char *string); 226 | 227 | // Update array items. 228 | void replaceItemInArray(aJsonObject *array, unsigned char which, 229 | aJsonObject *newitem); 230 | void replaceItemInObject(aJsonObject *object, const char *string, 231 | aJsonObject *newitem); 232 | 233 | void addNullToObject(aJsonObject* object, const char* name); 234 | void addTrueToObject(aJsonObject* object, const char* name); 235 | void addFalseToObject(aJsonObject* object, const char* name); 236 | void addNumberToObject(aJsonObject* object, const char* name, int n); 237 | void addNumberToObject(aJsonObject* object, const char* name, double n); 238 | void addStringToObject(aJsonObject* object, const char* name, 239 | const char* s); 240 | 241 | protected: 242 | friend class aJsonStream; 243 | static aJsonObject* newItem(); 244 | 245 | private: 246 | void suffixObject(aJsonObject *prev, aJsonObject *item); 247 | 248 | aJsonObject* createReference(aJsonObject *item); 249 | }; 250 | 251 | extern aJsonClass aJson; 252 | 253 | #endif 254 | -------------------------------------------------------------------------------- /aJson/keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For aJson 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | aJson KEYWORD1 10 | aJsonObject KEYWORD1 11 | 12 | ####################################### 13 | # Methods and Functions (KEYWORD2) 14 | ####################################### 15 | 16 | parse KEYWORD2 17 | print KEYWORD2 18 | deleteItem KEYWORD2 19 | getArraySize KEYWORD2 20 | getArraySize KEYWORD2 21 | getObjectItem KEYWORD2 22 | createNull KEYWORD2 23 | createTrue KEYWORD2 24 | createFalse KEYWORD2 25 | createBool KEYWORD2 26 | createNumber KEYWORD2 27 | createString KEYWORD2 28 | createArray KEYWORD2 29 | createObject KEYWORD2 30 | createIntArray KEYWORD2 31 | createFloatArray KEYWORD2 32 | createDoubleArray KEYWORD2 33 | createStringArray KEYWORD2 34 | addItemToArray KEYWORD2 35 | addItemToObject KEYWORD2 36 | addItemReferenceToArray KEYWORD2 37 | addItemReferenceToObject KEYWORD2 38 | detachItemFromArray KEYWORD2 39 | deleteItemFromArray KEYWORD2 40 | detachItemFromObject KEYWORD2 41 | deleteItemFromObject KEYWORD2 42 | replaceItemInArray KEYWORD2 43 | replaceItemInObject KEYWORD2 44 | addNullToObject KEYWORD2 45 | addTrueToObject KEYWORD2 46 | addFalseToObject KEYWORD2 47 | addNumberToObject KEYWORD2 48 | addStringToObject KEYWORD2 49 | 50 | 51 | ####################################### 52 | # Constants (LITERAL1) 53 | ####################################### 54 | 55 | aJson_False LITERAL1 56 | aJson_True LITERAL1 57 | aJson_NULL LITERAL1 58 | aJson_Number LITERAL1 59 | aJson_String LITERAL1 60 | aJson_Array LITERAL1 61 | aJson_Object LITERAL1 62 | aJson_IsReference LITERAL1 -------------------------------------------------------------------------------- /aJson/utility/stringbuffer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * aJson 3 | * stringbuffer.c 4 | * 5 | * http://interactive-matter.org/ 6 | * 7 | * This file is part of aJson. 8 | * 9 | * aJson is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * aJson is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with aJson. If not, see . 20 | * 21 | * Created on: 14.10.2010 22 | * Author: marcus 23 | */ 24 | #include 25 | #include 26 | #include "stringbuffer.h" 27 | 28 | //Default buffer size for strings 29 | #define BUFFER_SIZE 256 30 | //there is a static buffer allocated, which is used to decode strings to 31 | //strings cannot be longer than the buffer 32 | char global_buffer[BUFFER_SIZE]; 33 | 34 | string_buffer* 35 | stringBufferCreate(void) 36 | { 37 | string_buffer* result = malloc(sizeof(string_buffer)); 38 | if (result == NULL) 39 | { 40 | return NULL; 41 | } 42 | result->string = global_buffer; 43 | memset((void*) global_buffer, 0, BUFFER_SIZE); 44 | //unused - but will be usefull after realloc got fixd 45 | /* if (result->string==NULL) { 46 | free(result); 47 | return NULL; 48 | } 49 | result->memory=BUFFER_DEFAULT_SIZE;*/ 50 | result->memory = BUFFER_SIZE; 51 | result->string_length = 0; 52 | return result; 53 | } 54 | 55 | char 56 | stringBufferAdd(char value, string_buffer* buffer) 57 | { 58 | if (buffer->string_length >= buffer->memory) 59 | { 60 | //this has to be enabled after realloc works 61 | /*char* new_string = (char*) realloc((void*) buffer->string, (buffer->memory 62 | + BUFFER_DEFAULT_SIZE) * sizeof(char)); 63 | if (new_string == NULL) 64 | { 65 | free(buffer->string); 66 | buffer->string = NULL; 67 | return -1; 68 | } else { 69 | buffer->string = new_string; 70 | } 71 | buffer->memory += BUFFER_DEFAULT_SIZE;*/ 72 | //in the meantime we just drop it 73 | return 0; //EOF would be a better choice - but that breaks json decoding 74 | } 75 | buffer->string[buffer->string_length] = value; 76 | buffer->string_length += 1; 77 | return 0; 78 | } 79 | 80 | char* 81 | stringBufferToString(string_buffer* buffer) 82 | { 83 | //this is the realloc dependent function - it does not work 84 | // char* result = buffer->string; 85 | //ensure that the string ends with 0 86 | if (buffer->string_length == 0 || buffer->string[(buffer->string_length - 1)] 87 | != 0) 88 | { 89 | stringBufferAdd(0, buffer); 90 | } 91 | /* char* string = realloc(result, buffer->string_length); 92 | if (string==NULL) { 93 | free(result); 94 | } 95 | buffer->string=NULL; 96 | free(buffer); 97 | return string;*/ 98 | char* result = malloc(buffer->string_length * sizeof(char)); 99 | if (result == NULL) 100 | { 101 | return NULL; 102 | } 103 | strcpy(result, global_buffer); 104 | buffer->string = NULL; 105 | free(buffer); 106 | return result; 107 | } 108 | 109 | void 110 | stringBufferFree(string_buffer* buffer) 111 | { 112 | if (buffer == NULL) 113 | { 114 | //hmm it was null before - whatever 115 | return; 116 | } 117 | //this is not needed in this realloc free concept 118 | /* 119 | if (buffer->string!=NULL) { 120 | free(buffer->string); 121 | } 122 | */ 123 | free(buffer); 124 | } 125 | 126 | -------------------------------------------------------------------------------- /aJson/utility/stringbuffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * aJson 3 | * stringbuffer.h 4 | * 5 | * http://interactive-matter.org/ 6 | * 7 | * This file is part of aJson. 8 | * 9 | * aJson is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation, either version 3 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * aJson is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * You should have received a copy of the GNU General Public License 19 | * along with aJson. If not, see . 20 | * 21 | * Created on: 14.10.2010 22 | * Author: marcus 23 | */ 24 | 25 | #ifndef STRINGBUFFER_H_ 26 | #define STRINGBUFFER_H_ 27 | 28 | typedef struct 29 | { 30 | char* string; 31 | unsigned int memory; 32 | unsigned int string_length; 33 | } string_buffer; 34 | 35 | #ifdef __cplusplus 36 | extern "C" 37 | { 38 | #endif 39 | 40 | string_buffer* 41 | stringBufferCreate(void); 42 | 43 | char 44 | stringBufferAdd(char value, string_buffer* buffer); 45 | 46 | char* 47 | stringBufferToString(string_buffer* buffer); 48 | 49 | void 50 | stringBufferFree(string_buffer* buffer); 51 | 52 | #ifdef __cplusplus 53 | } 54 | #endif 55 | #endif /* STRINGBUFFER_H_ */ 56 | -------------------------------------------------------------------------------- /examples/ToggleLED/ToggleLED.ino: -------------------------------------------------------------------------------- 1 | /* 2 | JsonRPC Library - ToggleLED 3 | 4 | Demonstrates how to toggle the status of a LED by sending a 5 | json message to the arduino over the serial connection. 6 | 7 | The following python script can be used to toggle the LED 8 | after the sketch has been uploaded to the arduino: 9 | 10 | import sys 11 | import serial 12 | import time 13 | 14 | port = '/dev/ttyACM0' 15 | ser = serial.Serial(port, 115200) 16 | 17 | # give the serial connection 2 seconds to settle 18 | time.sleep(2) 19 | 20 | ser.write('{"method": "init", "params": {} }') 21 | 22 | time.sleep(1) 23 | 24 | ser.write('{"method": "toggleLED", "params": {"status": true} }') 25 | time.sleep(5) 26 | ser.write('{"method": "toggleLED", "params": {"status": false} }') 27 | 28 | # wait 2 seconds before closing the serial connection 29 | time.sleep(2) 30 | ser.close() 31 | 32 | */ 33 | 34 | // include the aJSON library 35 | #include 36 | // include the JsonRPC library 37 | #include 38 | 39 | 40 | 41 | class TargetController: public JsonRPCServer { 42 | public: 43 | TargetController(Stream* stream); 44 | 45 | DECLARE_JSON_PROC(TargetController, init, int); 46 | DECLARE_JSON_PROC(TargetController, toggleLED, String); 47 | 48 | 49 | BEGIN_JSON_REGISTRATION 50 | REGISTER_JSON_PROC(init, JSON_RPC_RET_TYPE_NUMERIC); 51 | REGISTER_JSON_PROC(toggleLED, JSON_RPC_RET_TYPE_STRING); 52 | END_JSON_REGISTRATION 53 | 54 | 55 | private: 56 | 57 | // on most arduino boards, pin 13 is connected to a LED 58 | int _led; 59 | 60 | }; 61 | 62 | 63 | 64 | 65 | 66 | TargetController::TargetController(Stream* stream): JsonRPCServer(stream), _led(13) { 67 | // TODO Auto-generated constructor stub 68 | 69 | } 70 | 71 | int TargetController::init(aJsonObject* params) { 72 | 73 | // initialize the digital pin as an output 74 | pinMode(_led, OUTPUT); 75 | 76 | return true; 77 | } 78 | 79 | String TargetController::toggleLED(aJsonObject* params) 80 | { 81 | aJsonObject* statusParam = aJson.getObjectItem(params, "status"); 82 | boolean requestedStatus = statusParam -> valuebool; 83 | 84 | if (requestedStatus) 85 | { 86 | digitalWrite(_led, HIGH); 87 | return "led HIGH"; 88 | } 89 | else 90 | { 91 | digitalWrite(_led, LOW); 92 | return "led LOW"; 93 | } 94 | } 95 | 96 | 97 | 98 | 99 | TargetController jsonController(&Serial); 100 | 101 | 102 | void setup() 103 | { 104 | // start up the serial interface 105 | Serial.begin(115200); 106 | Serial.println("Initializing JSON RPC server"); 107 | 108 | /* add setup code here */ 109 | 110 | jsonController.begin(2); 111 | } 112 | 113 | void loop() 114 | { 115 | 116 | jsonController.process(); 117 | } 118 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For JsonRPCServer 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | JsonRPCServer KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | registerMethod KEYWORD2 16 | processMessage KEYWORD2 17 | 18 | ####################################### 19 | # Constants (LITERAL1) 20 | ####################################### 21 | 22 | --------------------------------------------------------------------------------