| 42 | |
| 43 | == Things tested == |
| 44 | |
| 45 | === JSON for protocol === |
| 46 | XML or YAML would have been great, but I tried to use Syck (http://whytheluckystiff.net/syck/) and it didn't seem trivial to use, it looks like it supports a stream parser, we need something more like a DOM to find values returned. |
| 47 | |
| 48 | JSON (with json-c-0.7) gives us that in C and is quite elegant. |
| 49 | |
| 50 | Here's how we can generate JSON: |
| 51 | {{{ |
| 52 | struct json_object *status_object = json_object_new_object(); |
| 53 | json_object_object_add(status_object, "wifidog_version", json_object_new_string(VERSION)); |
| 54 | json_object_object_add(status_object, "protocol_version", json_object_new_double(2.0)); |
| 55 | json_object_object_add(status_object, "node_id", json_object_new_string(node_id)); |
| 56 | json_object_object_add(status_object, "fetch_config", json_object_new_boolean(1)); |
| 57 | |
| 58 | struct json_object *node_status_object = json_object_new_object(); |
| 59 | json_object_object_add(node_status_object, "wifidog_uptime", json_object_new_int(25)); |
| 60 | json_object_object_add(node_status_object, "sys_uptime", json_object_new_int(get_sys_uptime())); |
| 61 | json_object_object_add(node_status_object, "sys_loadavg", json_object_new_double(get_sys_loadavg())); |
| 62 | json_object_object_add(node_status_object, "sys_memfree", json_object_new_int(get_sys_memfree())); |
| 63 | |
| 64 | char * json = json_object_to_json_string(status_object); |
| 65 | }}} |
| 66 | |
| 67 | It returns the JSON string. |
| 68 | |
| 69 | To parse: |
| 70 | |
| 71 | {{{ |
| 72 | struct json_object * json_object = json_tokener_parse(the_string); |
| 73 | struct json_object * value_json_object = json_object_object_get(json_object, "node_id"); |
| 74 | printf("%s\n", json_object_get_string(value_json_object)); |
| 75 | }}} |
| 76 | |
| 77 | This will retrieve the string value of "node_id" at the first level in the tree. |