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) 2009-2018 The GPSD project
14   SPDX-License-Identifier: BSD-2-clause
15 
16 ***************************************************************************/
17 
18 #include "gpsd_config.h"  /* must be before all includes */
19 
20 #include <math.h>
21 #include <stdbool.h>
22 
23 #include "gpsd.h"
24 #ifdef SOCKET_EXPORT_ENABLE
25 #include "gps_json.h"
26 #include "timespec.h"
27 
json_device_read(const char * buf,struct devconfig_t * dev,const char ** endptr)28 int json_device_read(const char *buf,
29 		     struct devconfig_t *dev,
30 		     const char **endptr)
31 {
32     double d_cycle, d_mincycle;
33     /* *INDENT-OFF* */
34     const struct json_attr_t json_attrs_device[] = {
35 	{"class",      t_check,      .dflt.check = "DEVICE"},
36 
37         {"path",       t_string,     .addr.string  = dev->path,
38 	                                .len = sizeof(dev->path)},
39         // odd, device->gpsdata.online is sent, but put in dev->activated?
40 	{"activated",  t_time,       .addr.ts = &dev->activated,
41 				        .dflt.ts = {0, 0}},
42 	{"flags",      t_integer,    .addr.integer = &dev->flags},
43 	{"driver",     t_string,     .addr.string  = dev->driver,
44 	                                .len = sizeof(dev->driver)},
45 	{"subtype",    t_string,     .addr.string  = dev->subtype,
46 	                                .len = sizeof(dev->subtype)},
47 	{"subtype1",   t_string,     .addr.string  = dev->subtype1,
48 	                                .len = sizeof(dev->subtype1)},
49         {"hexdata",    t_string,     .addr.string  = dev->hexdata,
50 	                                .len = sizeof(dev->hexdata)},
51 	{"native",     t_integer,    .addr.integer = &dev->driver_mode,
52 				        .dflt.integer = DEVDEFAULT_NATIVE},
53 	{"bps",	       t_uinteger,   .addr.uinteger = &dev->baudrate,
54 				        .dflt.uinteger = DEVDEFAULT_BPS},
55 	{"parity",     t_character,  .addr.character = &dev->parity,
56                                         .dflt.character = DEVDEFAULT_PARITY},
57 	{"stopbits",   t_uinteger,   .addr.uinteger = &dev->stopbits,
58 				        .dflt.uinteger = DEVDEFAULT_STOPBITS},
59 	{"cycle",      t_real,       .addr.real = &d_cycle,
60 				        .dflt.real = NAN},
61 	{"mincycle",   t_real,       .addr.real = &d_mincycle,
62 				        .dflt.real = NAN},
63 	{NULL},
64     };
65     /* *INDENT-ON* */
66     int status;
67 
68     status = json_read_object(buf, json_attrs_device, endptr);
69     if (status != 0)
70 	return status;
71 
72     if (0 == isfinite(d_cycle)) {
73 	dev->cycle.tv_sec = 0;
74 	dev->cycle.tv_nsec = 0;
75     } else {
76 	DTOTS(&dev->cycle, d_cycle);
77     }
78     if (0 == isfinite(d_mincycle)) {
79 	dev->mincycle.tv_sec = 0;
80 	dev->mincycle.tv_nsec = 0;
81     } else {
82 	DTOTS(&dev->cycle, d_mincycle);
83     }
84 
85     return 0;
86 }
87 
json_watch_read(const char * buf,struct gps_policy_t * ccp,const char ** endptr)88 int json_watch_read(const char *buf,
89 		    struct gps_policy_t *ccp,
90 		    const char **endptr)
91 {
92     bool dummy_pps_flag;
93     /* *INDENT-OFF* */
94     struct json_attr_t chanconfig_attrs[] = {
95 	{"class",          t_check,    .dflt.check = "WATCH"},
96 
97 	{"enable",         t_boolean,  .addr.boolean = &ccp->watcher,
98                                           .dflt.boolean = true},
99 	{"json",           t_boolean,  .addr.boolean = &ccp->json,
100                                           .nodefault = true},
101 	{"raw",	           t_integer,  .addr.integer = &ccp->raw,
102 	                                  .nodefault = true},
103 	{"nmea",	   t_boolean,  .addr.boolean = &ccp->nmea,
104 	                                  .nodefault = true},
105 	{"scaled",         t_boolean,  .addr.boolean = &ccp->scaled},
106 	{"timing",         t_boolean,  .addr.boolean = &ccp->timing},
107 	{"split24",        t_boolean,  .addr.boolean = &ccp->split24},
108 	{"pps",            t_boolean,  .addr.boolean = &ccp->pps},
109 	{"device",         t_string,   .addr.string = ccp->devpath,
110 	                                  .len = sizeof(ccp->devpath)},
111 	{"remote",         t_string,   .addr.string = ccp->remote,
112 	                                  .len = sizeof(ccp->remote)},
113 	{"pps",            t_boolean,  .addr.boolean = &dummy_pps_flag,
114                                           .nodefault = false},
115 	{NULL},
116     };
117     /* *INDENT-ON* */
118     int status;
119 
120     status = json_read_object(buf, chanconfig_attrs, endptr);
121     return status;
122 }
123 
124 #endif /* SOCKET_EXPORT_ENABLE */
125 
126 /* shared_json.c ends here */
127