File: socket_can.c

package info (click to toggle)
softgun 0.14-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,040 kB
  • ctags: 11,989
  • sloc: ansic: 70,651; makefile: 232
file content (205 lines) | stat: -rw-r--r-- 5,357 bytes parent folder | download | duplicates (2)
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
/*
 * ----------------------------------------------------
 *
 * TCP socket based CAN message forwarding
 * for CAN-chip emulators 
 * (C) 2004  Lightmaze Solutions AG
 *   Author: Jochen Karrer
 *
 *  This program is free software; you can distribute it and/or modify it
 *  under the terms of the GNU General Public License (Version 2) as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope 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, write to the Free Software Foundation, Inc.,
 *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
 * ----------------------------------------------------
 */
#include <socket_can.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include "fio.h"
#include "configfile.h"


static void
close_connection(Connection *con)
{
	Connection *cursor,*prev;
	CanController *contr=con->canController;
        close(con->sockfd);
	if(con->rfh_is_active) {
		FIO_RemoveFileHandler(&con->rfh);
		con->rfh_is_active=0;
	}
        if(con->wfh_is_active) {
                FIO_RemoveFileHandler(&con->wfh);
                con->wfh_is_active=0;
        }
	for(prev=NULL,cursor=contr->con_list;cursor;prev=cursor,cursor=cursor->next)
        {
                if(cursor==con)     {
                        if(prev) {
                                prev->next=cursor->next;
                        } else {
                                contr->con_list=cursor->next;
                        }
			break;
                }
        }
        free(con);
	fprintf(stderr,"CAN-Bus socket connection closed\n");
        return;
}

/*
 * ------------------------------------------------------
 * Can Send: Interface function for the Chip Emulator,
 *	sends a CAN-Message to all sockets connected 
 *	a CAN Controller
 * ------------------------------------------------------
 */ 

void
CanSend(CanController *contr,CAN_MSG *msg) {
	/* To lazy for buffering and Writefilehandlers, may be added later */
	Connection *con,*next;
	int result;
	msg->id=htonl(msg->id);	
	for(con=contr->con_list;con; con=next) {
		next=con->next;
		result = write(con->sockfd,msg,sizeof(*msg));
		if(result<0) {
			if(errno==EAGAIN) {
				fprintf(stderr,"Tcp Buffer overrun, skiping CAN-Message\n");	
			} else {
				close_connection(con);
			}
		} else if (result==0) {
			close_connection(con);
		}
	}
	return;
}


static int
read_from_sock(void *cd,int mask)
{
	Connection *con = cd;
	CanController *contr=con->canController;
	int result,count=0;
	char *cbuf=(char*)&con->imsg;
	result=read(con->sockfd,cbuf+con->ibuf_wp,sizeof(CAN_MSG)-con->ibuf_wp);
	if(result>0) {
		count=result;
	} else if(result == 0) {
		close_connection(con);
		return 0;
	} else {
		if(errno==EAGAIN) {
			return 0;
		} else {
			close_connection(con);
			return 0;
		}
	}
	con->ibuf_wp+=count;
	if(con->ibuf_wp>=sizeof(CAN_MSG)) {
		con->imsg.id = ntohl(con->imsg.id);
		con->ibuf_wp=0;	
		contr->cops->receive(contr->clientData,&con->imsg);
	}
	return 0;
	
}

static void
tcp_connect(int sockfd,char *host,unsigned short port,void *cd)
{
	Connection *con=malloc(sizeof(Connection));	
	CanController *contr=cd;
	if(!con) {
		fprintf(stderr,"Out of memory\n");
		exit(4326);
	}
	memset(con,0,sizeof(*con));
	con->canController=contr;
	con->sockfd=sockfd;
	fcntl(sockfd,F_SETFL,O_NONBLOCK);
	if(contr->con_list) {
		con->next=contr->con_list;
	} else {
		con->next=NULL;
	} 
	contr->con_list=con;
	FIO_AddFileHandler(&con->rfh,sockfd,FIO_READABLE,read_from_sock,con);
	con->rfh_is_active=1;
	fprintf(stderr,"New socket connection to emulated CAN-Controller\n");
		
}

/*
 * ----------------------------------------------------------
 * Create a new Cancontroller with a Listening TCP-Socket
 * ----------------------------------------------------------
 */
CanController * 
CanSocketInterface_New(CanChipOperations *cops,const char *name,void *clientData) 
{
	CanController *contr=malloc(sizeof(CanController));	
	int port;
	if(!contr) {
		fprintf(stderr,"Out of memory\n");
		exit(7645);
	}
	if(Config_ReadInt32(&port,name,"port")<0) {
		free(contr);
		return 0;
	}
	memset(contr,0,sizeof(*contr));
	contr->cops=cops;	
	contr->clientData=clientData;
	contr->listen_fd=FIO_InitTcpServer(&contr->tserv,tcp_connect,contr,"127.0.0.1",port);
	if(contr->listen_fd<0) {
		fprintf(stderr,"Can not open TCP Listening Port %d for CAN-Emulator: ",port);
		perror("");
		free(contr);
		return NULL;
	}
	return contr;
}
void 
CanStopRx(CanController *contr) {
	Connection *cursor;
	for(cursor=contr->con_list;cursor;cursor=cursor->next)
        {
		if(cursor->rfh_is_active) {
			cursor->rfh_is_active=0;
			FIO_RemoveFileHandler(&cursor->rfh);
		}
        }
}
void 
CanStartRx(CanController *contr) {
	Connection *cursor;
	for(cursor=contr->con_list;cursor;cursor=cursor->next)
        {
		if(!cursor->rfh_is_active) {
			cursor->rfh_is_active=1;
			FIO_AddFileHandler(&cursor->rfh,cursor->sockfd,FIO_READABLE,read_from_sock,cursor);
		}
        }
}