File: link_vti.c

package info (click to toggle)
libticables 1.2.0-2
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 3,260 kB
  • ctags: 1,519
  • sloc: ansic: 9,993; sh: 9,265; makefile: 208; sed: 16
file content (262 lines) | stat: -rw-r--r-- 5,642 bytes parent folder | download
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
/* Hey EMACS -*- linux-c -*- */
/* $Id: link_vti.c 3053 2006-11-06 17:08:45Z roms $ */

/*  libticables2 - link cable library, a part of the TiLP project
 *  Copyright (C) 1999-2005  Romain Lievin
 *
 *  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 2 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/* "VTi" virtual link cable unit */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/* 
   This part use Shared Memory Segment for implementing two circular buffers
   between 2 program which use this lib. Each segment is attached 2 times.
   Convention used: 0 is an emulator and 1 is a linking program.
   One shm is used for transferring information from 0 to 1 and the other
   shm is used for transferring from 1 to 0.
   shm0: W -> R
   shm1: R <- W
*/

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#include "../ticables.h"
#include "../logging.h"
#include "../error.h"
#include "../gettext.h"
#include "detect.h"

#define BUF_SIZE 1*1024

/* Circular buffer (0: TIEmu, 1: TiLP) */
typedef struct
{
    uint8_t buf[BUF_SIZE];
    int start;
    int end;
} LinkBuffer;

/* Shm variables */
static key_t ipc_key[2];	// IPC key
static int shmid[2];		// Shm ID
static LinkBuffer *shm[2];	// Shm address

static LinkBuffer* send_buf[2];	// Swapped buffer
static LinkBuffer* recv_buf[2];

static int vti_prepare(CableHandle *h)
{
    // in fact, address & device are unused
    switch(h->port)
    {
    case PORT_0:	// automatic setting
	h->address = 0; //ref_cnt;
	break;
    case PORT_1:	// forced setting, for compatibility
    case PORT_3: 
	h->address = 0; h->device = strdup("0->1"); 
	break;
    case PORT_2:
    case PORT_4:
	h->address = 1; h->device = strdup("1->0"); 
	break;
    default: return ERR_ILLEGAL_ARG;
    }
    
    return 0;
}

static int vti_open(CableHandle *h)
{
    int i;
    
    /* Get a unique (if possible) key */
    for (i = 0; i < 2; i++) 
    {
	if ((ipc_key[i] = ftok("/tmp", i)) == -1) 
	{
	    ticables_warning("unable to get unique key (ftok).\n");
	    return ERR_VTI_IPCKEY;
	}
    }
    
    /* Open a shared memory segment */
    for (i = 0; i < 2; i++) 
    {
	if ((shmid[i] = shmget(ipc_key[i], sizeof(LinkBuffer), 
			       IPC_CREAT | 0666)) == -1) 
	{
	    ticables_warning("unable to open shared memory (shmget).\n");
	    return ERR_VTI_SHMGET;
	}
    }
    
    /* Attach the shm */
    for (i = 0; i < 2; i++) 
    {
	if ((shm[i] = shmat(shmid[i], NULL, 0)) == NULL) 
	{
	    ticables_warning("unable to attach shared memory (shmat).\n");
	    return ERR_VTI_SHMAT;
	}
    }
    
    /* Swap shm */
    send_buf[0] = shm[0];		// 0 -> 1: writing
    recv_buf[0] = shm[1];		// 0 <- 1: reading
    send_buf[1] = shm[1];		// 1 -> 0: writing
    recv_buf[1] = shm[0];		// 1 <- 0: reading

    for (i = 0; i < 2; i++)
	shm[i]->start = shm[i]->end = 0;

    return 0;
}

static int vti_close(CableHandle *h)
{
    int i;
    
    /* Detach segment */
    for (i = 0; i < 2; i++) 
    {
	if (shmdt(shm[i]) == -1) 
	{
	    ticables_warning("shmdt\n");
	    return ERR_VTI_SHMDT;
	}
	
	/* and destroy it */
	if (shmctl(shmid[i], IPC_RMID, NULL) == -1) 
	{
	    ticables_warning("shmctl\n");
	    return ERR_VTI_SHMCTL;
	}
    }
    
    return 0;
}

static int vti_reset(CableHandle *h)
{
	int i;
	
	for (i = 0; i < 2; i++) 
	    shm[i]->start = shm[i]->end = 0;
	
	return 0;
}

static int vti_put(CableHandle *h, uint8_t *data, uint32_t len)
{
    int p = h->address;
    int i;
    tiTIME clk;
    
    for(i = 0; i < len; i++)
    {
	TO_START(clk);
	do 
	{
	    if (TO_ELAPSED(clk, h->timeout))
		return ERR_WRITE_TIMEOUT;
	}
	while (((send_buf[p]->end + 1) & 255) == send_buf[p]->start);
	
	send_buf[p]->buf[send_buf[p]->end] = data[i];
	send_buf[p]->end = (send_buf[p]->end + 1) & 255;
    }
    
    return 0;
}

static int vti_get(CableHandle *h, uint8_t *data, uint32_t len)
{
    int p = h->address;
    int i;
    tiTIME clk;
    
    for(i = 0; i < len; i++)
    {
	TO_START(clk);
	do 
	{
	    if (TO_ELAPSED(clk, h->timeout))
		return ERR_READ_TIMEOUT;
	}
	while (recv_buf[p]->start == recv_buf[p]->end);
	
	
	data[i] = recv_buf[p]->buf[recv_buf[p]->start];
	recv_buf[p]->start = (recv_buf[p]->start + 1) & 255;
    }
    
    return 0;
}

static int vti_probe(CableHandle *h)
{
	return 0;
}

static int vti_check(CableHandle *h, int *status)
{
    int p = h->address;
    *status = !(recv_buf[p]->start == recv_buf[p]->end);
    return 0;
}

static int vti_set_red_wire(CableHandle *h, int b)
{
	return 0;
}

static int vti_set_white_wire(CableHandle *h, int b)
{
	return 0;
}

static int vti_get_red_wire(CableHandle *h)
{
	return 1;
}

static int vti_get_white_wire(CableHandle *h)
{
	return 1;
}

const CableFncts cable_vti = 
{
	CABLE_VTI,
	"VTI",
	N_("Virtual TI"),
	N_("Virtual link for VTi"),
	0,
	&vti_prepare,
	&vti_open, &vti_close, &vti_reset, &vti_probe, NULL,
	&vti_put, &vti_get, &vti_check,
	&vti_set_red_wire, &vti_set_white_wire,
	&vti_get_red_wire, &vti_get_white_wire,
};