summaryrefslogtreecommitdiff
path: root/ais_json.c
blob: 8147d4028f69d2f51e206e914b8eaa1d91d33f49 (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
/****************************************************************************

NAME
   ais_json.c - deserialize AIS JSON

DESCRIPTION
   This module uses the generic JSON parser to get data from AIS
representations to libgps structures.

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

#include <math.h>
#include <assert.h>
#include <string.h>
#include <stddef.h>
#include <stdio.h>

#include "gpsd_config.h"
#include "gpsd.h"
#include "gps_json.h"

/* common fields in every AIS message */

int json_ais_read(const char *buf, 
		    char *path, size_t pathlen,
		    struct ais_t *ais, 
		    const char **endptr)
{

#define AIS_HEADER \
	{"class",          check,    .dflt.check = "AIS"}, \
	{"type",           uinteger, .addr.uinteger = &ais->type}, \
	{"device",         string,   .addr.string = path, \
	                             .len = pathlen}, \
	{"repeat",         uinteger, .addr.uinteger = &ais->repeat}, \
	{"mmsi",           uinteger, .addr.uinteger = &ais->mmsi},

    int status;

    const struct json_attr_t json_ais1[] = {
	AIS_HEADER
	{"status",         uinteger, .addr.uinteger = &ais->type123.status},
	{"turn",           integer,  .addr.integer = &ais->type123.turn},
	{"speed",          uinteger, .addr.uinteger = &ais->type123.speed},
	{"accuracy",       boolean,  .addr.boolean = &ais->type123.accuracy},
	{"lon",            integer,  .addr.integer = &ais->type123.lon},
	{"lat",            integer,  .addr.integer = &ais->type123.lat},
	{"course",         uinteger, .addr.uinteger = &ais->type123.course},
	{"heading",        uinteger, .addr.uinteger = &ais->type123.heading},
	{"second",         uinteger, .addr.uinteger = &ais->type123.second},
	{"maneuver",       uinteger, .addr.uinteger = &ais->type123.maneuver},
	{"raim",           boolean,  .addr.boolean = &ais->type123.raim},
	{"radio",          uinteger, .addr.uinteger = &ais->type123.radio},
	{NULL},
    };

    const struct json_attr_t json_ais4[] = {
	AIS_HEADER
	{NULL},
    };

    const struct json_attr_t json_ais5[] = {
	AIS_HEADER
	{NULL},
    };

    const struct json_attr_t json_ais6[] = {
	AIS_HEADER
	{NULL},
    };

    const struct json_attr_t json_ais7[] = {
	AIS_HEADER
	{NULL},
    };

    const struct json_attr_t json_ais8[] = {
	AIS_HEADER
	{NULL},
    };

    const struct json_attr_t json_ais16[] = {
	AIS_HEADER
	{NULL},
    };

#undef AIS_HEADER

    memset(ais, '\0', sizeof(struct ais_t));

    if (strstr(buf, "\"type\":1,")!=NULL || strstr(buf, "\"type\":2,")!=NULL || strstr(buf, "\"type\":3,")!=NULL) {
	status = json_read_object(buf, json_ais1, endptr);
    } else if (strstr(buf, "\"type\":4,") != NULL || strstr(buf, "\"type\":11,")!=NULL) {
	status = json_read_object(buf, json_ais4, endptr);
    } else if (strstr(buf, "\"type\":5,") != NULL) {
	status = json_read_object(buf, json_ais5, endptr);
    } else if (strstr(buf, "\"type\":6,") != NULL) {
	status = json_read_object(buf, json_ais6, endptr);
    } else if (strstr(buf, "\"type\":7,") != NULL) {
	status = json_read_object(buf, json_ais7, endptr);
    } else if (strstr(buf, "\"type\":8,") != NULL) {
	status = json_read_object(buf, json_ais8, endptr);
    } else if (strstr(buf, "\"type\":16,") != NULL) {
	status = json_read_object(buf, json_ais16, endptr);
    } else {
	return JSON_ERR_MISC;
    }
    return status;
}

/* ais_json.c ends here */