"Initial commit to Gerrit"
[profile/ivi/gpsd.git] / shared_json.c
1 /****************************************************************************
2
3 NAME
4    shared_json.c - move data between in-core and JSON structures
5
6 DESCRIPTION 
7    This module uses the generic JSON parser to get data from JSON
8 representations to gps.h structures. These functions are used in both
9 the daemon and the client library.
10
11 PERMISSIONS
12   Written by Eric S. Raymond, 2009
13   This file is Copyright (c) 2010 by the GPSD project
14   BSD terms apply: see the file COPYING in the distribution root for details.
15
16 ***************************************************************************/
17
18 #include <math.h>
19 #include <assert.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <ctype.h>
23
24 #include "gpsd.h"
25 #include "gps_json.h"
26
27 int json_device_read(const char *buf,
28                      /*@out@*/ struct devconfig_t *dev,
29                      /*@null@*/ const char **endptr)
30 {
31     /*@ -fullinitblock @*/
32     /* *INDENT-OFF* */
33     const struct json_attr_t json_attrs_device[] = {
34         {"class",      t_check,      .dflt.check = "DEVICE"},
35         
36         {"path",       t_string,     .addr.string  = dev->path,
37                                         .len = sizeof(dev->path)},
38         {"activated",  t_real,       .addr.real = &dev->activated},
39         {"flags",      t_integer,    .addr.integer = &dev->flags},
40         {"driver",     t_string,     .addr.string  = dev->driver,
41                                         .len = sizeof(dev->driver)},
42         {"subtype",    t_string,     .addr.string  = dev->subtype,
43                                         .len = sizeof(dev->subtype)},
44         {"native",     t_integer,    .addr.integer = &dev->driver_mode,
45                                         .dflt.integer = DEVDEFAULT_NATIVE},
46         {"bps",        t_uinteger,   .addr.uinteger = &dev->baudrate,
47                                         .dflt.uinteger = DEVDEFAULT_BPS},
48         {"parity",     t_character,  .addr.character = &dev->parity,
49                                         .dflt.character = DEVDEFAULT_PARITY},
50         {"stopbits",   t_uinteger,   .addr.uinteger = &dev->stopbits,
51                                         .dflt.uinteger = DEVDEFAULT_STOPBITS},
52         {"cycle",      t_real,       .addr.real = &dev->cycle,
53                                         .dflt.real = NAN},
54         {"mincycle",   t_real,       .addr.real = &dev->mincycle,
55                                         .dflt.real = NAN},
56         {NULL},
57     };
58     /* *INDENT-ON* */
59     /*@ +fullinitblock @*/
60     int status;
61
62     status = json_read_object(buf, json_attrs_device, endptr);
63     if (status != 0)
64         return status;
65
66     return 0;
67 }
68
69 int json_watch_read(const char *buf,
70                     /*@out@*/ struct policy_t *ccp,
71                     /*@null@*/ const char **endptr)
72 {
73     /*@ -fullinitblock @*/
74     /* *INDENT-OFF* */
75     struct json_attr_t chanconfig_attrs[] = {
76         {"class",          t_check,    .dflt.check = "WATCH"},
77         
78         {"enable",         t_boolean,  .addr.boolean = &ccp->watcher,
79                                           .dflt.boolean = true},
80         {"json",           t_boolean,  .addr.boolean = &ccp->json,
81                                           .nodefault = true},
82         {"raw",            t_integer,  .addr.integer = &ccp->raw,
83                                           .nodefault = true},
84         {"nmea",           t_boolean,  .addr.boolean = &ccp->nmea,
85                                           .nodefault = true},
86         {"scaled",         t_boolean,  .addr.boolean = &ccp->scaled},
87         {"timing",         t_boolean,  .addr.boolean = &ccp->timing},
88         {"device",         t_string,   .addr.string = ccp->devpath,
89                                           .len = sizeof(ccp->devpath)},
90         {NULL},
91     };
92     /* *INDENT-ON* */
93     /*@ +fullinitblock @*/
94     int status;
95
96     status = json_read_object(buf, chanconfig_attrs, endptr);
97     return status;
98 }
99
100 /* shared_json.c ends here */