aboutsummaryrefslogtreecommitdiffstats
path: root/src/wheel-service.c
blob: 38303e8bb43dd0a2fcb430c9e11dcc4131b36806 (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 * Copyright (c) 2017-2019 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 <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <fcntl.h>
#include <math.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <json-c/json.h>
#include <sys/stat.h>
#include <pthread.h>
#include <systemd/sd-event.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>


#include "wheel-service.h"
#include "steering_wheel_json.h"
#include "prop_search.h"
#include "js_raw.h"
#include "can_encoder.h"

#define ENABLE_EVENT_DROP
#define STEERING_WHEEL_JSON	"/etc/steering_wheel.json"
#define BUS_MAP_CONF "/etc/dev-mapping.conf"

struct wheel_conf
{
	char *devname;
};

struct transmission_bus_conf
{
	char *hs;
	char *ls;
};
static struct transmission_bus_conf trans_conf;

/*
 * notify function
 */
int notify_property_changed(struct prop_info_t *property_info)
{
	DBG_NOTICE("notify_property_changed name=%s,value=%d", property_info->name, property_info->curValue);

	int rc = push(makeCanData(property_info));
	if (rc < 0)
	{
		DBG_ERROR("push failed");
		return -1;
	}

	return 0;
}

/*
 * transmission loop
 */
static void *transmission_event_loop(void *args)
{
	int s; /* can raw socket */
	int required_mtu;
	int mtu;
	int enable_canfd = 1;
	struct sockaddr_can addr;
	struct canfd_frame frame;
	struct ifreq ifr;
//	int retry = 0;

	/* open socket */
	if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
		perror("open socket failed");
		return 0;
	}

	addr.can_family = AF_CAN;
	strcpy(ifr.ifr_name, trans_conf.hs);
	/* wait until hs device start */
	while(1) {
		if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
			clear();		/* clear transmission msg queue */
			perror("SIOCGIFINDEX");
			sleep(2);
		}
		else
		{
			break;
		}
	}

	addr.can_ifindex = ifr.ifr_ifindex;

	/* disable default receive filter on this RAW socket */
	/* This is obsolete as we do not read from the socket at all, but for */
	/* this reason we can remove the receive list in the Kernel to save a */
	/* little (really a very little!) CPU usage.                          */
	setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);

	if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
		perror("bind");
		return 0;
	}

	while(1)
	{
		struct can_data_t* p = pop();
		if(p == NULL)
		{
			/* sleep 150ms */
			usleep(150000);
			continue;
		}

		/* parse CAN frame */
		required_mtu = parse_canframe(p->dat, &frame);
		free(p);
		if (!required_mtu){
			DBG_ERROR("\nWrong CAN-frame format! Try:\n\n");
			DBG_ERROR("    <can_id>#{R|data}          for CAN 2.0 frames\n");
			DBG_ERROR("    <can_id>##<flags>{data}    for CAN FD frames\n\n");
			DBG_ERROR("<can_id> can have 3 (SFF) or 8 (EFF) hex chars\n");
			DBG_ERROR("{data} has 0..8 (0..64 CAN FD) ASCII hex-values (optionally");
			DBG_ERROR(" separated by '.')\n");
			DBG_ERROR("<flags> a single ASCII Hex value (0 .. F) which defines");
			DBG_ERROR(" canfd_frame.flags\n\n");
			DBG_ERROR("e.g. 5A1#11.2233.44556677.88 / 123#DEADBEEF / 5AA# / ");
			DBG_ERROR("123##1 / 213##311\n     1F334455#1122334455667788 / 123#R ");
			DBG_ERROR("for remote transmission request.\n\n");
			continue;
		}

		if (required_mtu > CAN_MTU) {

			/* check if the frame fits into the CAN netdevice */
			if (ioctl(s, SIOCGIFMTU, &ifr) < 0) {
				perror("SIOCGIFMTU");
				continue;
			}
			mtu = ifr.ifr_mtu;

			if (mtu != CANFD_MTU) {
				DBG_ERROR("CAN interface ist not CAN FD capable - sorry.\n");
				continue;
			}

			/* interface is ok - try to switch the socket into CAN FD mode */
			if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES,
				       &enable_canfd, sizeof(enable_canfd))){
				DBG_ERROR("error when enabling CAN FD support\n");
				continue;
			}

			/* ensure discrete CAN FD length values 0..8, 12, 16, 20, 24, 32, 64 */
			frame.len = can_dlc2len(can_len2dlc(frame.len));
		}

		/* send frame */
		if (write(s, &frame, required_mtu) != required_mtu) {
			perror("write");
		}
	}
}
/*
 * transmission thread main function
 */
static pthread_t runTransmissionLoop(void)
{
	pthread_t thread_id = 0;
    int ret = pthread_create(&thread_id, NULL, transmission_event_loop, NULL);
	if(ret != 0) {
		DBG_ERROR( "Cannot run eventloop due to error:%d", errno);
		return -1;
	}

	return thread_id;
}

/*
 * connect to js device
 */
static int connection(struct wheel_conf *conf)
{
	int js;
	int ret;
	int rc;
	sd_event_source *source;
	sd_event *loop = NULL;

	js = js_open(conf->devname);
	if (js < 0)
	{
		js_close(js);
		DBG_ERROR("can't connect to joy stick, the event loop failed");
		return 0;
	}

	if(init_timer() < 0)
	{
		js_close(js);
		DBG_ERROR("can't start update timer");
		return 0;
	}

	/* get the default event loop */
	rc = sd_event_default(&loop);
	if (rc < 0) {
		DBG_ERROR("connection to default event loop failed: %s\n", strerror(-rc));
		loop = NULL;
		return 0;
	}

	ret = sd_event_add_io(
				loop,
				&source,
				js,
				EPOLLIN,
				on_event, NULL);
	if (ret < 0)
	{
		js_close(js);
		DBG_ERROR("can't add event, the event loop failed");
		return 0;
	}

	DBG_NOTICE("connect to JS(%s), event loop started",conf->devname);

	return 0;
}

/*
 * read /etc/dev-mapping.conf file
 */
static int readTransBus(void)
{
	char *line = NULL;
	char bus_name[4], bus_val[10];
	size_t len = 0;
	ssize_t read;

	FILE *fp = fopen(BUS_MAP_CONF,"r");
	if (fp == NULL)
	{
		DBG_ERROR("cannot read /etc/dev-mapping.conf");
		return -1;
	}

	while((	read = getline(&line, &len, fp)) != -1) {
		if(line == NULL || line[0] == '[')
			continue;

		memset(bus_name, 0, sizeof(bus_name));
		memset(bus_val, 0, sizeof(bus_val));

		sscanf(line, "%2s=\"%s", bus_name, bus_val);
		bus_val[strlen(bus_val)-1] = '\0';
		if (strcmp(bus_name, "hs") == 0)
		{
			trans_conf.hs = strdup(bus_val);
		}
		else if (strcmp(bus_name, "ls") == 0)
		{
			trans_conf.ls = strdup(bus_val);
		}
	}

	if(line != NULL)
		free(line);

	fclose(fp);
//	DBG_ERROR("readTransBus end, hs:%s,ls:%s", trans_conf.hs, trans_conf.ls);
	return 0;
}

/*
 * parse configuration file (steering_wheel.json)
 */
static int init_conf(int fd_conf, struct wheel_conf *conf)
{

	char *filebuf = NULL;
	json_object *jobj = NULL;
	struct stat stbuf;

	FILE *fp = fdopen(fd_conf,"r");
	if (fp == NULL)
	{
		DBG_ERROR("cannot read configuration file(steering_wheel.json)");
		return -1;
	}

	if (fstat(fd_conf, &stbuf) == -1)
	{
		DBG_ERROR("can't get file state");
		return -1;
	}

//	fseek(fp, 0, SEEK_SET);
	filebuf = (char *)malloc(stbuf.st_size);
	fread(filebuf, 1, stbuf.st_size, fp);
	fclose(fp);

	jobj = json_tokener_parse(filebuf);
	if (jobj == NULL)
//	if (is_error(jobj))
	{
		DBG_ERROR("json: Invalid steering_wheel.json format");
		return -1;
	}
	json_object_object_foreach(jobj, key, val)
	{
		if (strcmp(key,"dev_name") == 0)
		{
			conf->devname = strdup(json_object_get_string(val));
		}
		else if (strcmp(key,"wheel_map") == 0)
		{
			wheel_define_init(json_object_get_string(val));
		}
		else if (strcmp(key,"gear_para") == 0)
		{
			wheel_gear_para_init(json_object_get_string(val));
		}
	}
	json_object_put(jobj);
	free(filebuf);

	return 0;
}

/*
 * init js device
 */
static int init()
{
	DBG_NOTICE("init");

	int fd_conf;
	static struct wheel_conf conf;

	init_can_encoder();

	if (readTransBus())
	{
		DBG_ERROR("read file  (/etc/dev-mapping.conf) failed");
		return -1;
	}

	fd_conf = open(STEERING_WHEEL_JSON, O_RDONLY);
	if (fd_conf < 0)
	{
		DBG_ERROR("wheel configuration (steering_wheel.json) is not access");
		return -1;
	}
	if (init_conf(fd_conf, &conf))
	{
		DBG_ERROR("wheel json file  (steering_wheel_map.json) is not access");
		return -1;
	}

	return connection(&conf);
}

/*
 * entry function
 */
int main(void)
{
	int rc;
	pthread_t thread_id;
	sd_event *loop = NULL;

	/* device init  */
	rc = init();
	if (rc < 0) {
		DBG_ERROR("js device init failed: %s\n", strerror(-rc));
		return 1;
	}

	/* start post thread */
	thread_id = runTransmissionLoop();
	if (thread_id < 0) {
		DBG_ERROR("run post thread failed: %s\n", strerror(-thread_id));
	}

	/* get the default event loop */
	rc = sd_event_default(&loop);
	if (rc < 0) {
		DBG_ERROR("connection to default event loop failed: %s\n", strerror(-rc));
		return 1;
	}

	/* loop until end */
	for(;;)
		sd_event_run(loop, 30000000);
	return 0;
}