aboutsummaryrefslogtreecommitdiffstats
path: root/src/steering_wheel_json.c
blob: 4377a729b4c49db0a40be81c575c7d97c73edcd4 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <json-c/json.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "af-steering-wheel-binding.h"
#include "steering_wheel_json.h"
#include "prop_info.h"
#include "prop_search.h"

extern double gearRatio[];

struct wheel_info_t *wheel_info;

static int parse_property(struct wheel_info_t *wheel_info, int idx, json_object *obj_property)
{
	int var_type = 0;
	char *name = NULL;

	if(obj_property)
	{
		json_object_object_foreach(obj_property, key, val)
		{
			if (strcmp("PROPERTY", key) == 0)
			{
				name = (char *)json_object_get_string(val);
			}
			else if(strcmp("TYPE", key) == 0)
			{
				const char *_varname = json_object_get_string(val);
				var_type = string2vartype(_varname);
			}
		}

		wheel_info->property[idx].name = strdup(name);
		wheel_info->property[idx].var_type = (unsigned char)var_type;
	}

	return 0;
}

#define DEFAULT_PROP_CNT (16)
static int parse_propertys(json_object *obj_propertys)
{
	int err = 0;
	json_object * obj_property;
	size_t ms = sizeof(struct wheel_info_t) + (size_t)(DEFAULT_PROP_CNT*sizeof(struct prop_info_t));

	if(obj_propertys)
	{
		wheel_info = malloc(ms);
		if (wheel_info == NULL)
		{
			ERRMSG("Not enogh memory");
			return 1;
		}
		memset(wheel_info, 0 , ms);

		enum json_type type = json_object_get_type(obj_propertys);
		if (type == json_type_array)
		{
			int array_len = json_object_array_length(obj_propertys);
			if(array_len > DEFAULT_PROP_CNT)
			{
				wheel_info = realloc(wheel_info, sizeof(struct wheel_info_t) + ((size_t)array_len * sizeof(struct prop_info_t)));
				if (wheel_info == NULL)
				{
					ERRMSG("not enogh memory");
					exit(1);
				}

				memset(&wheel_info->property[DEFAULT_PROP_CNT], 0, (size_t)(array_len - DEFAULT_PROP_CNT)*sizeof(struct prop_info_t));
			}

			for (int i = 0; i < array_len; i++)
			{
				obj_property = json_object_array_get_idx(obj_propertys, i);
				err = parse_property(wheel_info, i, obj_property);
			}

			wheel_info->nData = (unsigned int)array_len;
			addAllPropDict(wheel_info);
		}
	}

	return err;
}

static int parse_json(json_object *obj)
{
	int err = 0;
    json_object_object_foreach(obj, key, val)
	{
        if (strcmp(key,"PROPERTYS") == 0)
        {
			err += parse_propertys(val);
		}
        else
        {
			++err;
			ERRMSG("json: Unknown  key \"%s\"", key);
		}
    }
	return err;
}

int wheel_define_init(const char *fname)
{
	struct json_object *jobj;
	int fd_wheel_map;
	struct stat stbuf;
	char *filebuf;

	fd_wheel_map = afb_daemon_rootdir_open_locale(fname, O_RDONLY, NULL);
	if (fd_wheel_map < 0)
	{
		ERRMSG("wheel map is not access");
		return -1;
	}

	FILE *fp = fdopen(fd_wheel_map,"r");
	if (fp == NULL)
	{
		ERRMSG("canno read wheel map file");
		return -1;
	}

	if (fstat(fd_wheel_map, &stbuf) == -1)
	{
		ERRMSG("cant get file state");
		return -1;
	}

	filebuf = (char *)malloc((size_t)stbuf.st_size);
	fread(filebuf, 1, (size_t)stbuf.st_size, fp);
	fclose(fp);

	jobj = json_tokener_parse(filebuf);
	if (jobj == NULL)
	{
		ERRMSG("cannot data from file \"%s\"",fname);
		free(filebuf);
		return 1;
	}

	parse_json(jobj);

	json_object_put(jobj);

	free(filebuf);
	return 0;
}

static int parse_gear_para_json(json_object *obj)
{
	int err = 0;
	json_object * obj_speed_para;
	char *pos_name = NULL;
	double pos_val = 0;

    json_object_object_foreach(obj, key, val)
	{
        if (strcmp(key,"GEAR_PARA") == 0)
        {
        	enum json_type type = json_object_get_type(val);
			if (type == json_type_array)
			{
				int array_len = json_object_array_length(val);
				NOTICEMSG("array_len:%d!", array_len);
				for (int i = 0; i < array_len; i++)
				{
					obj_speed_para = json_object_array_get_idx(val, i);
					if(obj_speed_para)
					{
						json_object_object_foreach(obj_speed_para, key, val)
						{
							///
							if (strcmp("POS", key) == 0)
							{
								pos_name = (char *)json_object_get_string(val);
							}
							else if(strcmp("VAL", key) == 0)
							{
								pos_val = json_object_get_double(val);
							}

							///
							if(strcmp("First", pos_name) == 0)
							{
								gearRatio[1] = (double)(1.0 / pos_val);
							}
							else if(strcmp("Second", pos_name) == 0)
							{
								gearRatio[2] = (double)(1.0 / pos_val);
							}
							else if(strcmp("Third", pos_name) == 0)
							{
								gearRatio[3] = (double)(1.0 / pos_val);
							}
							else if(strcmp("Fourth", pos_name) == 0)
							{
								gearRatio[4] = (double)(1.0 / pos_val);
							}
							else if(strcmp("Fifth", pos_name) == 0)
							{
								gearRatio[5] = (double)(1.0 / pos_val);
							}
							else if(strcmp("Sixth", pos_name) == 0)
							{
								gearRatio[6] = (double)(1.0 / pos_val);
							}
							else if(strcmp("Reverse", pos_name) == 0)
							{
								gearRatio[7] = (double)(1.0 / pos_val);
							}
						}
					}
				}
			}
			else
			{
				ERRMSG("json: Need  array \"%s\"", key);
			}
		}
        else
        {
			ERRMSG("json: Unknown  key \"%s\"", key);
		}
    }
	return err;
}

int wheel_gear_para_init(const char *fname)
{
	struct json_object *jobj;
	int fd_gear_para;
	struct stat stbuf;
	char *filebuf;

	fd_gear_para = afb_daemon_rootdir_open_locale(fname, O_RDONLY, NULL);
	if (fd_gear_para < 0)
	{
		ERRMSG("gear para is not access");
		return -1;
	}

	FILE *fp = fdopen(fd_gear_para,"r");
	if (fp == NULL)
	{
		ERRMSG("canno read gear para file");
		return -1;
	}

	if (fstat(fd_gear_para, &stbuf) == -1)
	{
		ERRMSG("cant get file state.\n");
		return -1;
	}

	filebuf = (char*)malloc((size_t)stbuf.st_size);
	fread(filebuf, 1, (size_t)stbuf.st_size, fp);
	fclose(fp);

	jobj = json_tokener_parse(filebuf);
	if (jobj == NULL)
	{
		ERRMSG("cannot data from file \"%s\"",fname);
		free(filebuf);
		return 1;
	}
	parse_gear_para_json(jobj);

	json_object_put(jobj);

	free(filebuf);
	return 0;
}

struct json_object *property2json(struct prop_info_t *property)
{
	struct json_object *jobj;
	struct json_object *nameObject = NULL;
	struct json_object *valueObject = NULL;

	if (property == NULL)
		return NULL;

	jobj = json_object_new_object();
	if (jobj == NULL)
		return NULL;
	switch(property->var_type)
	{
		case VOID_T:	/* FALLTHROUGH */
		case INT8_T:	/* FALLTHROUGH */
		case INT16_T:	/* FALLTHROUGH */
		case INT_T:		/* FALLTHROUGH */
		case INT32_T:	/* FALLTHROUGH */
		case INT64_T:	/* FALLTHROUGH */
		case UINT8_T:	/* FALLTHROUGH */
		case UINT16_T:	/* FALLTHROUGH */
		case UINT_T:	/* FALLTHROUGH */
		case UINT32_T:	/* FALLTHROUGH */
		case ENABLE1_T:
			valueObject = json_object_new_int(propertyValue_int(property));
			break;

		case BOOL_T:
			valueObject = json_object_new_boolean(property->curValue.bool_val);
			break;

		default:
			ERRMSG("Unknown value type:%d", property->var_type);
			break;
	}

	if (valueObject == NULL)
	{
		ERRMSG("fail json ValueObject");
		json_object_put(jobj);
		return NULL;
	}

	nameObject = json_object_new_string(property->name);

	json_object_object_add(jobj, "name", nameObject);
	json_object_object_add(jobj, "value", valueObject);

	return jobj;
}