summaryrefslogtreecommitdiff
path: root/shared_json.c
blob: d12012178615e046dfabc2f809b9166e37f9ce25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/****************************************************************************

NAME
   shared_json.c - move data between in-core and JSON structures

DESCRIPTION
   This module uses the generic JSON parser to get data from JSON
representations to gps.h structures. These functions are used in both
the daemon and the client library.

PERMISSIONS
  Written by Eric S. Raymond, 2009
  This file is Copyright (c) 2009-2018 The GPSD project
  SPDX-License-Identifier: BSD-2-clause

***************************************************************************/

#include <math.h>
#include <stdbool.h>

#include "gpsd.h"
#ifdef SOCKET_EXPORT_ENABLE
#include "gps_json.h"

int json_device_read(const char *buf,
		     struct devconfig_t *dev,
		     const char **endptr)
{
    char tbuf[JSON_DATE_MAX+1];
    /* *INDENT-OFF* */
    const struct json_attr_t json_attrs_device[] = {
	{"class",      t_check,      .dflt.check = "DEVICE"},

        {"path",       t_string,     .addr.string  = dev->path,
	                                .len = sizeof(dev->path)},
	{"activated",  t_string,     .addr.string = tbuf,
			                .len = sizeof(tbuf)},
	{"activated",  t_real,       .addr.real = &dev->activated},
	{"flags",      t_integer,    .addr.integer = &dev->flags},
	{"driver",     t_string,     .addr.string  = dev->driver,
	                                .len = sizeof(dev->driver)},
	{"subtype",    t_string,     .addr.string  = dev->subtype,
	                                .len = sizeof(dev->subtype)},
        {"hexdata",    t_string,     .addr.string  = dev->hexdata,
	                                .len = sizeof(dev->hexdata)},
	{"native",     t_integer,    .addr.integer = &dev->driver_mode,
				        .dflt.integer = DEVDEFAULT_NATIVE},
	{"bps",	       t_uinteger,   .addr.uinteger = &dev->baudrate,
				        .dflt.uinteger = DEVDEFAULT_BPS},
	{"parity",     t_character,  .addr.character = &dev->parity,
                                        .dflt.character = DEVDEFAULT_PARITY},
	{"stopbits",   t_uinteger,   .addr.uinteger = &dev->stopbits,
				        .dflt.uinteger = DEVDEFAULT_STOPBITS},
	{"cycle",      t_real,       .addr.real = &dev->cycle,
				        .dflt.real = NAN},
	{"mincycle",   t_real,       .addr.real = &dev->mincycle,
				        .dflt.real = NAN},
	{NULL},
    };
    /* *INDENT-ON* */
    int status;

    tbuf[0] = '\0';
    status = json_read_object(buf, json_attrs_device, endptr);
    if (status != 0)
	return status;

    if (isfinite(dev->activated) == 0) {
	if (tbuf[0] == '\0')
	    dev->activated = NAN;
	else
	    dev->activated = iso8601_to_unix(tbuf);
    }

    return 0;
}

int json_watch_read(const char *buf,
		    struct gps_policy_t *ccp,
		    const char **endptr)
{
    bool dummy_pps_flag;
    /* *INDENT-OFF* */
    struct json_attr_t chanconfig_attrs[] = {
	{"class",          t_check,    .dflt.check = "WATCH"},

	{"enable",         t_boolean,  .addr.boolean = &ccp->watcher,
                                          .dflt.boolean = true},
	{"json",           t_boolean,  .addr.boolean = &ccp->json,
                                          .nodefault = true},
	{"raw",	           t_integer,  .addr.integer = &ccp->raw,
	                                  .nodefault = true},
	{"nmea",	   t_boolean,  .addr.boolean = &ccp->nmea,
	                                  .nodefault = true},
	{"scaled",         t_boolean,  .addr.boolean = &ccp->scaled},
	{"timing",         t_boolean,  .addr.boolean = &ccp->timing},
	{"split24",        t_boolean,  .addr.boolean = &ccp->split24},
	{"pps",            t_boolean,  .addr.boolean = &ccp->pps},
	{"device",         t_string,   .addr.string = ccp->devpath,
	                                  .len = sizeof(ccp->devpath)},
	{"remote",         t_string,   .addr.string = ccp->remote,
	                                  .len = sizeof(ccp->remote)},
	{"pps",            t_boolean,  .addr.boolean = &dummy_pps_flag,
                                          .nodefault = false},
	{NULL},
    };
    /* *INDENT-ON* */
    int status;

    status = json_read_object(buf, chanconfig_attrs, endptr);
    return status;
}

#endif /* SOCKET_EXPORT_ENABLE */

/* shared_json.c ends here */