[OSADL-svn-commits] r106 - trunks/fddi-trunk/src

OSADL repository commits osadl-svn-commits at lists.osadl.org
Tue Oct 2 11:48:02 CEST 2007


Author: robert
Date: Wed Aug 22 20:59:57 2007
New Revision: 106

Log:
start of can stuff

Modified:
   trunks/fddi-trunk/src/libfddi_can.c

Modified: trunks/fddi-trunk/src/libfddi_can.c
==============================================================================
--- trunks/fddi-trunk/src/libfddi_can.c	(original)
+++ trunks/fddi-trunk/src/libfddi_can.c	Wed Aug 22 20:59:57 2007
@@ -14,6 +14,7 @@
 #endif
 
 #include <errno.h>
+#include <pthread.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
@@ -137,6 +138,11 @@
 struct modbus_tpu modbus_tpu1;
 #endif
 
+struct can_tpu {
+	pthread_t can_thread_id;
+	int endme;
+} can_tpu;
+
 typedef struct can_iface {
 	struct sockaddr_can addr;
 	struct can_frame frame;
@@ -146,6 +152,42 @@
 
 can_iface_t can_iface1;
 
+#define MAXDEV 6
+
+void* _can_thread(void *arg)
+{
+	fd_set rdfs;
+	int fd;
+	int bytes;
+	int retval;
+	struct can_frame frame;
+
+	FD_ZERO(&rdfs);
+	FD_SET(fd, &rdfs);
+
+	while (!(can_tpu.endme)) {
+
+		if ((retval = select(fd, &rdfs, NULL, NULL, NULL)) < 0) {
+			perror("select");
+			can_tpu.endme = 1;
+			continue;
+		}
+
+		bytes = recv(fd, &frame, sizeof(struct can_frame), 0);
+		if (bytes < sizeof(struct can_frame)) {
+			fprintf(stderr, "error: received short frame");
+			exit(-1);
+		}
+
+
+
+
+		sleep(1);
+	}
+
+	return NULL;
+}
+
 
 #if 0
 void _modbus_tpu_out(fddi_tpu_t *tpu)
@@ -197,26 +239,6 @@
 
 int fddi_backend_init(fddi_iface_t *iface)
 {
-	if ((can_iface1.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
-		perror("socket");
-		return -ESOCKTNOSUPPORT;
-	}
-
-	can_iface1.addr.can_family = AF_CAN;
-
-	strcpy(can_iface1.ifr.ifr_name, "vcan0");
-	if (ioctl(can_iface1.socket, SIOCGIFINDEX, &can_iface1.ifr) < 0) {
-		perror("SIOCGIFINDEX");
-		return 1;
-	}
-	can_iface1.addr.can_ifindex = can_iface1.ifr.ifr_ifindex;
-
-	if (bind(can_iface1.socket, (struct sockaddr *)&can_iface1.addr, sizeof(can_iface1.addr)) < 0) {
-		perror("bind");
-		return 1;
-	}
-
-	iface->priv = &can_iface1;
 	return 0;
 }
 
@@ -235,8 +257,31 @@
 	can_iface_t *can_iface = (can_iface_t*)(iface->priv);
 
 	printf("parsing configfile: %s\n", configfile);
-	printf("for interface:      %s\n", iface->id.id_as_string); /* FIXME private */
+//	printf("for interface:      %s\n", iface->id.id_as_string); /* FIXME private */
+
+	/*
+	 * create socket for specified CAN interface
+	 */
+	if ((can_iface1.socket = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
+		perror("opening can socket");
+		return -ESOCKTNOSUPPORT;
+	}
+
+	can_iface1.addr.can_family = AF_CAN;
+
+	strcpy(can_iface1.ifr.ifr_name, "vcan0");
+	if (ioctl(can_iface1.socket, SIOCGIFINDEX, &can_iface1.ifr) < 0) {
+		perror("attaching to can interface");
+		return 1;
+	}
+	can_iface1.addr.can_ifindex = can_iface1.ifr.ifr_ifindex;
+
+	if (bind(can_iface1.socket, (struct sockaddr *)&can_iface1.addr, sizeof(can_iface1.addr)) < 0) {
+		perror("binding to can socket");
+		return 1;
+	}
 
+	iface->priv = &can_iface1;
 	/* FIXME: everything's hardcoded & static for now, we add an xml parser later */
 
 	iface->tpulist_head = &tpu1;
@@ -290,45 +335,20 @@
 	switch (cmd) {
 
 	case CMD_START:
-#if 0
 
-		printf("start\n");
-		/* FIXME loop over all tpus here */
-		{
-			struct modbus_tpu *mtpu = ((struct modbus_tpu*)(iface->tpulist_head->priv));
-
-			mtpu->timer_spec.it_value.tv_sec = 1;
-			mtpu->timer_spec.it_value.tv_nsec = 0;
-			mtpu->timer_spec.it_interval.tv_sec = 0;
-			mtpu->timer_spec.it_interval.tv_nsec = 500*1000*1000;
-
-			if ((retval = timer_settime(mtpu->timer, 0, &mtpu->timer_spec, NULL)) < 0 ) {
-				perror("timer settime");
-				/* FIXME: disarm timers here */
-				return -1;
-			}
-		}
-#endif
-		retval = 0;
+		/* FIXME: check if we have already a thread running */
+		retval = pthread_create(&can_tpu.can_thread_id, NULL, _can_thread, NULL);
+		if (retval != 0)
+			perror("creating can_thread");
 		break;
 
 	case CMD_STOP:
-#if 0
-		/* FIXME: loop over all tpus here */
-		{
-			struct modbus_tpu *mtpu = ((struct modbus_tpu*)(iface->tpulist_head->priv));
-
-			mtpu->timer_spec.it_value.tv_sec = 0;
-			mtpu->timer_spec.it_value.tv_nsec = 0;
-			mtpu->timer_spec.it_interval.tv_sec = 0;
-			mtpu->timer_spec.it_interval.tv_nsec = 0;
-
-			if ((retval = timer_settime(mtpu->timer, 0, &mtpu->timer_spec, NULL)) < 0 ) {
-				perror("timer settime");
-				return -1;
-			}
-		}
-#endif
+
+		can_tpu.endme = 1;
+		printf("waiting for can_thread to finish...");
+		pthread_join(can_tpu.can_thread_id, NULL);
+		printf("done\n");
+
 		retval = 0;
 		break;
 


More information about the OSADL-svn-commits mailing list