ec.c 16.8 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2007 - 2012
	Copyright Stichting C.A. Muller Radioastronomiestation, 2007 - 2012

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <time.h>
#include <pthread.h>
#include <semaphore.h>

#include "controller_sample.h"

#include "ec.h"
#include "ec_int.h"
#include "log.h"

static int ec_sock;
struct sockaddr_ll sockaddr_ll;

struct ec_pdo_frame {
	unsigned char tx_frame[1500];
	unsigned char rx_frame[1500];
	
	struct ec_frame_hdr *frame_hdr;
	uint64_t *time;
	uint16_t *time_wkc;
	size_t flen;
	uint32_t nr;
	int tx_users;
	int rx_users;
	int ready;
};

static struct ec_pdo_frame tx_pdo_real = {
	frame_hdr: (struct ec_frame_hdr *)&tx_pdo_real.tx_frame,
	flen: sizeof(struct ec_frame_hdr),
	nr: -1,
	tx_users: 0,
	rx_users: 0,
};
static struct ec_pdo_frame *tx_pdo = &tx_pdo_real;

static struct ec_pdo_frame rx_pdo = {
	frame_hdr: (struct ec_frame_hdr *)&rx_pdo.tx_frame,
	flen: sizeof(struct ec_frame_hdr),
	nr: -1,
	tx_users: 0,
	rx_users: 0,
};

static int rx_pdo_enabled = 1;
static int tx_pdo_enabled = 1;
static int single_pdo = 0;

static int ec_rx_pdo_add_time(void)
{
	struct ec_dgram_hdr *dgram;
	struct ec_dgram_hdr *prev;
	uint16_t *wkc;
	
	rx_pdo.rx_users = ec_slave_count();

	dgram = (struct ec_dgram_hdr *)(rx_pdo.tx_frame + rx_pdo.flen);
	rx_pdo.time = (uint64_t *)(rx_pdo.tx_frame + rx_pdo.flen + sizeof(struct ec_dgram_hdr));
	
	rx_pdo.flen += sizeof(struct ec_dgram_hdr);
	rx_pdo.flen += sizeof(uint64_t) + 2;

	printf("Adding %zu bytes for clock distribution to rx_pdo, total pdo is now: %zu bytes\n",
	    sizeof(uint64_t), rx_pdo.flen);

	wkc = (uint16_t *)(rx_pdo.tx_frame + rx_pdo.flen - 2);
	*wkc = 0;
	rx_pdo.time_wkc = (uint16_t *)(rx_pdo.rx_frame + rx_pdo.flen - 2);

	EC_FRAME_HDR_SET(rx_pdo.frame_hdr,
	    rx_pdo.flen - sizeof(struct ec_frame_hdr),
	    EC_FRAME_HDR_TYPE_EC);

	dgram->idx = 0;

	dgram->cmd = EC_DGRAM_HDR_CMD_BWR;
	dgram->addr.position.pos = 0x0000;
	dgram->addr.position.off = ESC_ADDR_MAP_DC_SYSTEM_TIME;

	EC_DGRAM_HDR_SET(dgram, sizeof(uint64_t), 0, 0);

	for (prev = (struct ec_dgram_hdr *)(rx_pdo.tx_frame + sizeof(struct ec_frame_hdr));
	    prev != dgram;
	    prev = ((void*)prev) + EC_DGRAM_HDR_LEN(prev) + sizeof(struct ec_dgram_hdr) + 2) {
		EC_DGRAM_HDR_SET(prev, EC_DGRAM_HDR_LEN(prev), EC_DGRAM_HDR_C(prev), 1);
	}
	
	return 0;
}

int ec_rx_pdo_add(struct ec_dgram_addr *addr, unsigned char **data, size_t len)
{
	struct ec_dgram_hdr *dgram;
	struct ec_dgram_hdr *prev;
	uint16_t *wkc;
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
126
127
128
129
130
131
132
133
134
135
136
137
138
	for (prev = (struct ec_dgram_hdr *)(rx_pdo.tx_frame + sizeof(struct ec_frame_hdr));
	    (size_t)prev < (size_t)rx_pdo.tx_frame + rx_pdo.flen;
	    prev = ((void*)prev) + EC_DGRAM_HDR_LEN(prev) + sizeof(struct ec_dgram_hdr) + 2) {
		if (addr->type == EC_DGRAM_ADDR_AUTO_INC &&
		    prev->cmd == EC_DGRAM_HDR_CMD_APRD &&
		    prev->addr.position.pos == addr->addr.position.pos &&
		    prev->addr.position.off == addr->addr.position.off) {
			printf("Double registration for rx PDO");
			*data = (unsigned char *)prev + sizeof(struct ec_dgram_hdr);
			return 0;
		}
	}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
	dgram = (struct ec_dgram_hdr *)(rx_pdo.tx_frame + rx_pdo.flen);
	*data = rx_pdo.rx_frame + rx_pdo.flen + sizeof(struct ec_dgram_hdr);
	
	rx_pdo.flen += sizeof(struct ec_dgram_hdr);
	rx_pdo.flen += len + 2;

	printf("Adding %zu bytes data to rx_pdo, total pdo is now: %zu bytes\n",
	    len, rx_pdo.flen);

	wkc = (uint16_t*)(rx_pdo.tx_frame + rx_pdo.flen - 2);
	*wkc = 0;

	EC_FRAME_HDR_SET(rx_pdo.frame_hdr,
	    rx_pdo.flen - sizeof(struct ec_frame_hdr),
	    EC_FRAME_HDR_TYPE_EC);

	dgram->idx = 0;

	switch (addr->type) {
		case EC_DGRAM_ADDR_AUTO_INC:
			dgram->cmd = EC_DGRAM_HDR_CMD_APRD;
			dgram->addr.position.pos = addr->addr.position.pos;
			dgram->addr.position.off = addr->addr.position.off;
			printf("Address: %x.%x\n", addr->addr.position.pos,
			    addr->addr.position.off);
			break;
		default:
			printf("Addressing mode not implemented!\n");
			return 1;
	}

	EC_DGRAM_HDR_SET(dgram, len, 0, 0);

	for (prev = (struct ec_dgram_hdr *)(rx_pdo.tx_frame + sizeof(struct ec_frame_hdr));
	    prev != dgram;
	    prev = ((void*)prev) + EC_DGRAM_HDR_LEN(prev) + sizeof(struct ec_dgram_hdr) + 2) {
		EC_DGRAM_HDR_SET(prev, EC_DGRAM_HDR_LEN(prev), EC_DGRAM_HDR_C(prev), 1);
	}
	
	return 0;
}

int ec_tx_pdo_add(struct ec_dgram_addr *addr, unsigned char **data, size_t len)
{
	struct ec_dgram_hdr *dgram;
	struct ec_dgram_hdr *prev;
	uint16_t *wkc;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
186
187
188
189
190
191
192
193
194
195
196
197
198

	for (prev = (struct ec_dgram_hdr *)(tx_pdo->tx_frame + sizeof(struct ec_frame_hdr));
	    (size_t)prev < (size_t)tx_pdo->tx_frame + tx_pdo->flen;
	    prev = ((void*)prev) + EC_DGRAM_HDR_LEN(prev) + sizeof(struct ec_dgram_hdr) + 2) {
		if (addr->type == EC_DGRAM_ADDR_AUTO_INC &&
		    prev->cmd == EC_DGRAM_HDR_CMD_APWR &&
		    prev->addr.position.pos == addr->addr.position.pos &&
		    prev->addr.position.off == addr->addr.position.off) {
			printf("Double registration for tx PDO");
			*data = (unsigned char *)prev + sizeof(struct ec_dgram_hdr);
			return 0;
		}
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
	
	dgram = (struct ec_dgram_hdr *)(tx_pdo->tx_frame + tx_pdo->flen);
	*data = tx_pdo->tx_frame + tx_pdo->flen + sizeof(struct ec_dgram_hdr);
	
	tx_pdo->flen += sizeof(struct ec_dgram_hdr);
	tx_pdo->flen += len + 2;
	
	tx_pdo->tx_users++;

	printf("Adding %zu bytes data to tx_pdo, total pdo is now: %zu bytes\n",
	    len, tx_pdo->flen);
	printf("tx_pdo has %d users\n", tx_pdo->tx_users);

	wkc = (uint16_t*)(tx_pdo->tx_frame + tx_pdo->flen - 2);
	*wkc = 0;

	EC_FRAME_HDR_SET(tx_pdo->frame_hdr,
	    tx_pdo->flen - sizeof(struct ec_frame_hdr),
	    EC_FRAME_HDR_TYPE_EC);

	dgram->idx = 0;

	switch (addr->type) {
		case EC_DGRAM_ADDR_AUTO_INC:
			dgram->cmd = EC_DGRAM_HDR_CMD_APWR;
			dgram->addr.position.pos = addr->addr.position.pos;
			dgram->addr.position.off = addr->addr.position.off;
			break;
		default:
			printf("Addressing mode not implemented!\n");
			return 1;
	}

	EC_DGRAM_HDR_SET(dgram, len, 0, 0);

	for (prev = (struct ec_dgram_hdr *)(tx_pdo->tx_frame + sizeof(struct ec_frame_hdr));
	    prev != dgram;
	    prev = ((void*)prev) + EC_DGRAM_HDR_LEN(prev) + sizeof(struct ec_dgram_hdr) + 2) {
		EC_DGRAM_HDR_SET(prev, EC_DGRAM_HDR_LEN(prev), EC_DGRAM_HDR_C(prev), 1);
	}
	
	return 0;
}

static ssize_t ec_dgram(void *tx_frame, void *rx_frame, size_t len)
{
	int ret;
//	struct timeval timeout;
//	fd_set fdr;
//	int recvdata = 0;
	struct ec_dgram_hdr *rx_dgram1, *tx_dgram1;
	static uint8_t dgram_idx = 0;
	
	dgram_idx++;

	tx_dgram1 = (struct ec_dgram_hdr *)
	    ((char *)tx_frame +  sizeof(struct ec_frame_hdr));
	rx_dgram1 = (struct ec_dgram_hdr *)
	    ((char *)rx_frame +  sizeof(struct ec_frame_hdr));
	
//	/* Make sure there is nothing in the queue left from previous errors */
//	do {
//		timeout.tv_sec = 0;
//		timeout.tv_usec = 0;
//		FD_ZERO(&fdr);
//		FD_SET(ec_sock, &fdr);
//		select(ec_sock + 1, &fdr, NULL, NULL, &timeout);
//		recvdata = FD_ISSET(ec_sock, &fdr);
//		if (recvdata) {
//			unsigned char tmpbuf[2000];
//			ret = recv(ec_sock, tmpbuf, 2000, 0);	
//		}
//	} while (recvdata);

	tx_dgram1->idx = dgram_idx;

	ret = sendto(ec_sock, tx_frame, len, MSG_DONTWAIT,
	    (struct sockaddr *)&sockaddr_ll, sizeof(sockaddr_ll));
	if (ret != len)
		return 1;

	do {
		ret = recv(ec_sock, rx_frame, len, 0);

		if (ret < sizeof(struct ec_frame_hdr)+sizeof(struct ec_dgram_hdr))
			return 1;
	} while (rx_dgram1->idx != dgram_idx);
	
	return 0;

}

int ec_tx_pdo(uint32_t nr)
{
	ssize_t ret;

	if (single_pdo)
		return 0;

	if (!tx_pdo_enabled)
		return 1;

	if (tx_pdo->nr != nr) {
		tx_pdo->ready = 1;
		tx_pdo->nr = nr;
	} else {
		tx_pdo->ready++;
	}
	
	if(tx_pdo->ready < tx_pdo->tx_users) {
			return 0;
	}
	
	ret = ec_dgram(tx_pdo->tx_frame, tx_pdo->rx_frame, tx_pdo->flen);

	return ret;
}

int ec_rx_pdo(uint32_t nr)
{
	ssize_t ret;
	struct timespec t;

	if (!rx_pdo_enabled)
		return 1;

	if (rx_pdo.nr == nr)
		return 0;

	clock_gettime(0, &t);
	*rx_pdo.time = htole64(
	    (946684800 + (uint64_t)t.tv_sec) * 1000000000 +
	    (uint64_t)t.tv_nsec
	);

	ret = ec_dgram(rx_pdo.tx_frame, rx_pdo.rx_frame, rx_pdo.flen);

	if (ret) {
		log_send(LOG_T_ERROR, "Did not send/receive ethercat frame!\n");
		return ret;
	}

	if (*rx_pdo.time_wkc < rx_pdo.rx_users) {
		char tmp[200];
		
		sprintf(tmp, "Only received ack from %d slaves! (%d expected)\n",
		    *rx_pdo.time_wkc, rx_pdo.rx_users);
		log_send(LOG_T_ERROR, tmp);
		return 1;
	}
	
	rx_pdo.nr = nr;
	return 0;
}

int ec_rx_pdo_enable(int enable)
{
	rx_pdo_enabled = enable;
	return 0;
}

int ec_tx_pdo_enable(int enable)
{
	rx_pdo_enabled = enable;
	return 0;
}

int ec_slave_count(void)
{
	char frame[1500];
	struct ec_dgram_hdr *dgram;
	struct ec_frame_hdr *frame_hdr = (struct ec_frame_hdr *)frame;
	char *payload;
	ssize_t flen;
	uint16_t *wkc;
	int ret;
	
	flen = sizeof(struct ec_frame_hdr),
	dgram = (struct ec_dgram_hdr *)(frame + flen);
	payload = (frame + flen + sizeof(struct ec_dgram_hdr));	
	flen += sizeof(struct ec_dgram_hdr);
	flen += sizeof(char) + 2;

	printf("Reading byte 0 of all ethercat slaves\n");
	wkc = (uint16_t *)(frame + flen - 2);
	*wkc = 0;
	*payload = 0x00;

	EC_FRAME_HDR_SET(frame_hdr,
	    flen - sizeof(struct ec_frame_hdr),
	    EC_FRAME_HDR_TYPE_EC);

	dgram->idx = 0;

	dgram->cmd = EC_DGRAM_HDR_CMD_BRD;
	dgram->addr.position.pos = 0x0000;
	dgram->addr.position.off = ESC_ADDR_MAP_TYPE;

	EC_DGRAM_HDR_SET(dgram, sizeof(char), 0, 0);

	ret = ec_dgram(frame, frame, flen);

	if (ret)
	{
		printf("Did not send/receive ethercat frame!\n");
		return -1;
	}
	
	return *wkc;
}

static struct ec_dgram_addr *datagram_addr;
static void *datagram_buffer;
static size_t datagram_len;
static ssize_t datagram_ret;
static bool datagram_read = false;
static bool datagram_write = false;

static bool sample_running = false;
static pthread_mutex_t datagram_lock;
static sem_t datagram_sync_sem;

static void ec_datagram_sample_init(void *arg)
{
	log_send(LOG_T_INFO, "Switching ethercat datagrams to sample-synchronised mode\n");
	pthread_mutex_init(&datagram_lock, NULL);
	sem_init(&datagram_sync_sem, 0, 0);
	
	sample_running = true;
}

static ssize_t ec_datagram_read_real(struct ec_dgram_addr *addr,
    void *buffer, size_t len)
{
	unsigned char frame[1500];
	struct ec_frame_hdr *frame_hdr;
	struct ec_dgram_hdr *dgram_hdr;
	void *data;
	uint16_t *wkc;
	size_t flen =
	    sizeof(struct ec_frame_hdr) +
	    sizeof(struct ec_dgram_hdr) +
	    len +
	    sizeof(uint16_t);
	ssize_t ret;
//	struct timeval timeout;
//	fd_set fdr;

	frame_hdr = (struct ec_frame_hdr*)frame;
	dgram_hdr = (struct ec_dgram_hdr*)(frame + sizeof(struct ec_frame_hdr));
	data = (unsigned char *)dgram_hdr + sizeof(struct ec_dgram_hdr);

	memset(frame, 0, flen);
	wkc = (uint16_t*)(frame + flen - sizeof(uint16_t));
	
	EC_FRAME_HDR_SET(frame_hdr, flen - sizeof(struct ec_frame_hdr),
	    EC_FRAME_HDR_TYPE_EC);
	
	dgram_hdr->idx = 0;

	switch (addr->type) {
		case EC_DGRAM_ADDR_AUTO_INC:
			dgram_hdr->cmd = EC_DGRAM_HDR_CMD_APRD;
			dgram_hdr->addr.position.pos = addr->addr.position.pos;
			dgram_hdr->addr.position.off = addr->addr.position.off;
			break;
		default:
			goto err_add;
	}

	EC_DGRAM_HDR_SET(dgram_hdr, len, 0, 0);
	
	ret = ec_dgram(frame, frame, flen);

	if (ret)
		goto err_sendrecv;

	/* wkc should be updated by slave */
	if (*wkc == 1) {
		memcpy(buffer, data, len);
		return len;
	} else
		return 0;

err_add:
err_sendrecv:
	return -1;
}

ssize_t ec_datagram_read(struct ec_dgram_addr *addr,
    void *buffer, size_t len)
{
	ssize_t ret;
	
	if (!sample_running || controller_sample_context)
		return ec_datagram_read_real(addr, buffer, len);
	
	pthread_mutex_lock(&datagram_lock);
	
	datagram_addr = addr;
	datagram_buffer = buffer;
	datagram_len = len;
	datagram_read = true;
	
	sem_wait(&datagram_sync_sem);
	
	ret = datagram_ret;
	
	pthread_mutex_unlock(&datagram_lock);

	return ret;
}

static ssize_t ec_datagram_write_real(struct ec_dgram_addr *addr,
    void *buffer, size_t len)
{
	unsigned char frame[1500];
	struct ec_frame_hdr *frame_hdr;
	struct ec_dgram_hdr *dgram_hdr;
	void *data;
	uint16_t *wkc;
	size_t flen =
	    sizeof(struct ec_frame_hdr) +
	    sizeof(struct ec_dgram_hdr) +
	    len +
	    sizeof(uint16_t);
	ssize_t ret;
//	struct timeval timeout;
//	fd_set fdr;

	frame_hdr = (struct ec_frame_hdr*)frame;
	dgram_hdr = (struct ec_dgram_hdr*)(frame + sizeof(struct ec_frame_hdr));
	data = (unsigned char *)dgram_hdr + sizeof(struct ec_dgram_hdr);

	memset(frame, 0, flen);
	wkc = (uint16_t*)(frame + flen - sizeof(uint16_t));
	
	EC_FRAME_HDR_SET(frame_hdr, flen - sizeof(struct ec_frame_hdr),
	    EC_FRAME_HDR_TYPE_EC);
	
	dgram_hdr->idx = 0;

	switch (addr->type) {
		case EC_DGRAM_ADDR_AUTO_INC:
			dgram_hdr->cmd = EC_DGRAM_HDR_CMD_APWR;
			dgram_hdr->addr.position.pos = addr->addr.position.pos;
			dgram_hdr->addr.position.off = addr->addr.position.off;
			break;
		default:
			goto err_add;
	}

	EC_DGRAM_HDR_SET(dgram_hdr, len, 0, 0);
	
	memcpy(data, buffer, len);
	
	ret = ec_dgram(frame, frame, flen);

	if (ret)
		goto err_sendrecv;

	/* wkc should be updated by slave */
	if (*wkc == 1) {
		return len;
	} else
		return 0;

err_add:
err_sendrecv:
	return -1;
}

ssize_t ec_datagram_write(struct ec_dgram_addr *addr,
    void *buffer, size_t len)
{
	ssize_t ret;
	
	if (!sample_running || controller_sample_context)
		return ec_datagram_write_real(addr, buffer, len);
	
	pthread_mutex_lock(&datagram_lock);
	
	datagram_addr = addr;
	datagram_buffer = buffer;
	datagram_len = len;
	datagram_write = true;
	
	sem_wait(&datagram_sync_sem);
	
	ret = datagram_ret;
	
	pthread_mutex_unlock(&datagram_lock);

	return ret;
}

void ec_datagram_sample(void)
{
	if (datagram_write) {
		datagram_ret = ec_datagram_write_real(
		    datagram_addr,
		    datagram_buffer,
		    datagram_len);
		datagram_write = false;
		sem_post(&datagram_sync_sem);
	} else if (datagram_read) {
		datagram_ret = ec_datagram_read_real(
		    datagram_addr,
		    datagram_buffer,
		    datagram_len);
		datagram_read = false;
		sem_post(&datagram_sync_sem);
	}
}



static int init_done = 0;

static bool init_success = false;

bool ec_initialized(void)
{
	return init_success;
}

int ec_init(char *ifname, int single_cycle)
{
	struct ifreq ifreq;
	int ret;
	struct timeval timeout;
	double timeout_sec;

	if (init_done)
		return 0;

	single_pdo = single_cycle;
	if (single_pdo) {
		tx_pdo = &rx_pdo;
		printf("Using a single PDO for rx and tx.\n");
	}
	
	printf("Going to initialize interface %s for control data\n", ifname);

	ec_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ETHERCAT));

	if (ec_sock < 0)
		goto err_nosocket;
	
	strncpy(ifreq.ifr_name, ifname, IFNAMSIZ);
	ret = ioctl(ec_sock, SIOCGIFINDEX, &ifreq);
	if (ret < 0) {
		perror("ioctl SIOCGIFINDEX");
		goto err_socket;
	}
	printf("Interface %s index nr found: %d\n", ifname, ifreq.ifr_ifindex);

	sockaddr_ll.sll_family = AF_PACKET;
	sockaddr_ll.sll_protocol = htons(ETH_P_ETHERCAT);
	sockaddr_ll.sll_ifindex = ifreq.ifr_ifindex;
	ret=bind(ec_sock, (struct sockaddr*)&sockaddr_ll, sizeof(sockaddr_ll));
	if (ret < 0) {
		perror("bind()");
		goto err_socket;
	}
	printf("Bind to interface and protocol 0x%04x done\n", ETH_P_ETHERCAT);

	sockaddr_ll.sll_addr[0] = 0xff;
	sockaddr_ll.sll_addr[1] = 0xff;
	sockaddr_ll.sll_addr[2] = 0xff;
	sockaddr_ll.sll_addr[3] = 0xff;
	sockaddr_ll.sll_addr[4] = 0xff;
	sockaddr_ll.sll_addr[5] = 0xff;
	sockaddr_ll.sll_halen = ETH_ALEN;

	/* Using select: set to non-blocking (1)
	   Using SO_SNDTIMEO: set to blocking (0) */
	ioctl(ec_sock, FIONBIO, &(int){0});

	timeout_sec = controller_sample_period() * 4;
	
	timeout.tv_sec = (int)(timeout_sec);
	timeout.tv_usec = (int)(timeout_sec * 1000000) % 1000000;
	printf("Setting rx/tx timeout to %d.%06d sec based on %f sec period\n", 
	    (int)timeout.tv_sec, (int)timeout.tv_usec, controller_sample_period());
	setsockopt(ec_sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
	timeout.tv_sec = (int)(timeout_sec);
	timeout.tv_usec = (int)(timeout_sec * 1000000) % 1000000;
	setsockopt(ec_sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));

	if (ec_slave_count() >= 0) {
		init_success = true;
	} else {
		goto err_init;
	}
	printf("Successfull init, %d slaves found.\n", ec_slave_count());

	ec_rx_pdo_add_time();

	controller_sample_start_hook(ec_datagram_sample_init, NULL);

	init_done = 1;

	return 0;

err_init:
err_socket:
	close(ec_sock);
	ec_sock = -1;
err_nosocket:
	return -1;
}