comparison gpf/tst/DRV/thif_link.c @ 0:509db1a7b7b8

initial import: leo2moko-r1
author Space Falcon <falcon@ivan.Harhan.ORG>
date Mon, 01 Jun 2015 03:24:05 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:509db1a7b7b8
1 /*
2 +------------------------------------------------------------------------------
3 | File: thif_link.c
4 +------------------------------------------------------------------------------
5 | Copyright 2002 Texas Instruments Berlin, AG
6 | All rights reserved.
7 |
8 | This file is confidential and a trade secret of Texas
9 | Instruments Berlin, AG
10 | The receipt of or possession of this file does not convey
11 | any rights to reproduce or disclose its contents or to
12 | manufacture, use, or sell anything it may describe, in
13 | whole, or in part, without the specific written consent of
14 | Texas Instruments Berlin, AG.
15 +-----------------------------------------------------------------------------
16 | Purpose : This Modul contains the serial driver adaptation
17 +-----------------------------------------------------------------------------
18 */
19
20 #ifndef __THIF_LINK_C__
21 #define __THIF_LINK_C__
22 #endif
23
24 /* these types are used in thif_socket.h */
25 #define Char char
26 #define Bool8 char
27 #define Int8 signed char
28 #define Uint8 unsigned char
29 #define UInt16 unsigned short int
30 #define Uint16 unsigned short int
31 #define Uint32 unsigned long int
32 #define Int32 long int
33
34
35 #ifdef _TARGET_
36 #include "thif_link.h"
37 #include "\global_af\thif\thif_socket.h"
38 #include "string.h"
39 #else
40 #include <windows.h>
41 #include <stdio.h>
42 #include "usart.h"
43 #include "stdlib.h"
44 #include "string.h"
45 #include "tools.h"
46 #endif
47
48 #include "vsi.h"
49 #include "drvconf.h"
50 #include "tstheader.h"
51 #include "os.h"
52
53
54
55
56
57 /*==== TYPES ======================================================*/
58
59 typedef struct
60 {
61 USHORT Handle;
62 USHORT EnabledSignalType;
63 T_DRV_CB_FUNC Callback;
64 char Connected;
65 Int8 thif_link;
66 } T_THIF_LINK_DATA;
67
68
69 typedef union {
70 USHORT scalar;
71 Uint8 array[2];
72 } SWAPPER;
73
74 /*==== CONSTANTS ==================================================*/
75
76 #define ALLOWED_THIF_LINK_SIGNALS (DRV_SIGTYPE_READ|DRV_SIGTYPE_CONNECT)
77 #define MAX_CONFIGSTR_LEN 50
78 #define NO_GOTDATA_CALLBACK 0
79 #define rxBufSz 0x800
80 #define txBufSz 0x800
81 #define WAIT_FOR_RECONNECT 500
82
83 /*==== EXTERNALS ==================================================*/
84
85
86 /*==== VARIABLES ==================================================*/
87
88 T_THIF_LINK_DATA THIF_LINK_Data;
89 static T_DRV_SIGNAL Signal;
90
91
92 Uint8 rxBuffer[rxBufSz];
93 Uint8 txBuffer[txBufSz];
94 Uint8 *prxBuffer = &rxBuffer[0];
95 Uint8 *ptxBuffer = &txBuffer[0];
96
97 GLOBAL OS_TIME WT;
98 GLOBAL OS_TIME RT;
99
100 /* static unsigned long int foolishSizeArgument = 0; */
101
102
103 /*==== FUNCTIONS ==================================================*/
104
105 #ifndef MIN
106 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
107 #endif
108
109 /*
110 +--------------------------------------------------------------------+
111 | PROJECT : GSM-Frame (8415) MODULE : thif_link |
112 | STATE : code ROUTINE : swapUSHORT |
113 +--------------------------------------------------------------------+
114
115 PURPOSE : byte sex handling
116
117 */
118 void swapUSHORT(USHORT* candidate)
119 {
120 SWAPPER source;
121 SWAPPER dest;
122
123 source.scalar = *candidate;
124 dest.array[0] = source.array[1];
125 dest.array[1] = source.array[0];
126 *candidate = dest.scalar;
127
128 }
129 /*
130 +--------------------------------------------------------------------+
131 | PROJECT : GSM-Frame (8415) MODULE : thif_link |
132 | STATE : code ROUTINE : beSureToBeConnected |
133 +--------------------------------------------------------------------+
134
135 PURPOSE : callback function of the driver
136
137 */
138
139 void beSureToBeConnected()
140 {
141 if (THIF_LINK_Data.thif_link < 0)
142 {
143 thif_link_Open();
144 }
145 }
146
147 /*
148 +--------------------------------------------------------------------+
149 | PROJECT : GSM-Frame (8415) MODULE : thif_link |
150 | STATE : code ROUTINE : thif_binding_Callback |
151 +--------------------------------------------------------------------+
152
153 PURPOSE : callback function of the driver
154
155 */
156 void thif_binding_Callback (Int8 sockId) /* this is a bridge to connect the THIF binding callback to the TR-defined callback (see thif_link_Open) */
157 {
158 if ( THIF_LINK_Data.EnabledSignalType & DRV_SIGTYPE_READ )
159 {
160 Signal.SignalType = DRV_SIGTYPE_READ;
161 Signal.DrvHandle = THIF_LINK_Data.Handle;
162 (THIF_LINK_Data.Callback)( &Signal );
163 }
164 }
165
166
167 /*
168 +--------------------------------------------------------------------+
169 | PROJECT : GSM-Frame (8415) MODULE : thif_link |
170 | STATE : code ROUTINE : thif_link_Exit |
171 +--------------------------------------------------------------------+
172
173 PURPOSE : exit a driver
174
175 */
176 void thif_link_Exit ( void )
177 {
178 thif_link_Close();
179 }
180
181 /*
182 +--------------------------------------------------------------------+
183 | PROJECT : GSM-Frame (8415) MODULE : thif_link |
184 | STATE : code ROUTINE : thif_link_Open |
185 +--------------------------------------------------------------------+
186
187 PURPOSE : open connection to driver
188
189 */
190 USHORT thif_link_Open(void)
191 {
192 LONG result = -1;
193
194 while (result < 0)
195 {
196 result = thif_socket_open(THIF_LINK_NAME, thif_binding_Callback, prxBuffer, rxBufSz, ptxBuffer, txBufSz );
197 if (result < 0)
198 {
199 //os_SuspendTask ( 0, WAIT_FOR_RECONNECT);
200 }
201 }
202 THIF_LINK_Data.thif_link = result;
203 THIF_LINK_Data.EnabledSignalType = DRV_SIGTYPE_READ;
204
205 Signal.SignalType = DRV_SIGTYPE_CONNECT;
206 Signal.DrvHandle = THIF_LINK_Data.Handle;
207 Signal.UserData = NULL;
208 (THIF_LINK_Data.Callback)( &Signal );
209
210 THIF_LINK_Data.Connected = TRUE;
211 return DRV_OK;
212 }
213
214 /*
215 +--------------------------------------------------------------------+
216 | PROJECT : GSM-Frame (8415) MODULE : thif_link |
217 | STATE : code ROUTINE : thif_link_Close |
218 +--------------------------------------------------------------------+
219
220 PURPOSE : exit a driver
221
222 */
223 USHORT thif_link_Close(void )
224 {
225 if (thif_socket_close(THIF_LINK_Data.thif_link) < 0)
226 {
227 return DRV_INTERNAL_ERROR;
228 }
229 THIF_LINK_Data.thif_link = -1;
230 THIF_LINK_Data.EnabledSignalType = DRV_SIGTYPE_DISCONNECT;
231
232 Signal.DrvHandle = THIF_LINK_Data.Handle;
233 Signal.UserData = NULL;
234 (THIF_LINK_Data.Callback)( &Signal );
235
236 THIF_LINK_Data.Connected = FALSE;
237 return DRV_OK;
238 }
239
240 /*
241 +--------------------------------------------------------------------+
242 | PROJECT : GSM-Frame (8415) MODULE : thif_link |
243 | STATE : code ROUTINE : thif_link_Read |
244 +--------------------------------------------------------------------+
245
246 PURPOSE : read data from driver
247
248 */
249 USHORT thif_link_Read ( void *Buffer, ULONG *SizeInBytes)
250 {
251
252 OS_TIME timeBefore;
253 OS_TIME timeAfter;
254 T_HANDLE caller;
255 USHORT retVal;
256
257 /*
258 ULONG to_read = *SizeInBytes;
259 Uint8* buff = (Uint8*) Buffer;
260
261 beSureToBeConnected();
262 caller = os_MyHandle();
263
264 if (foolishSizeArgument == 0)
265 {
266 to_read = thif_socket_read(THIF_LINK_Data.thif_link, buff, 2);
267 foolishSizeArgument = buff[0]*0xff + buff[1];
268 }
269 to_read= foolishSizeArgument;
270
271 //os_GetTime (caller, &timeBefore);
272 to_read = MIN(to_read, *SizeInBytes);
273 *SizeInBytes = thif_socket_read(THIF_LINK_Data.thif_link, buff, to_read);
274
275 if (*SizeInBytes <= foolishSizeArgument)
276 {
277 foolishSizeArgument -= *SizeInBytes;
278 retVal = DRV_OK;
279 }
280 else
281 {
282 foolishSizeArgument = 0;
283 retVal = DRV_INTERNAL_ERROR;
284 }
285
286 //os_GetTime (caller, &timeAfter);
287 //RT= timeAfter - timeBefore;
288 return retVal;
289 */
290 *SizeInBytes = thif_socket_read( THIF_LINK_Data.thif_link, Buffer, *SizeInBytes );
291 return DRV_OK;
292
293 }
294
295 /*
296 +--------------------------------------------------------------------+
297 | PROJECT : GSM-Frame (8415) MODULE : thif_link |
298 | STATE : code ROUTINE : thif_link_Write_to_socket |
299 +--------------------------------------------------------------------+
300
301 PURPOSE : write data to driver
302
303 */
304 void thif_link_Write_to_socket (void *Buffer, ULONG sizeInBytes )
305 {
306 T_HANDLE caller;
307 Uint8* buff;
308
309 OS_TIME timeBefore;
310 OS_TIME timeAfter;
311 ULONG to_write;
312
313 LONG written = -1;
314
315
316 buff = (Uint8*) Buffer;
317 to_write = sizeInBytes;
318 caller = os_MyHandle();
319 while (to_write > 0)
320 {
321 //os_GetTime (caller, &timeBefore);
322 written = thif_socket_write(THIF_LINK_Data.thif_link, buff, to_write);
323 //os_GetTime (caller, &timeAfter);
324 //WT = timeAfter - timeBefore;
325 if (written < 0)
326 {
327 //os_SuspendTask (caller, WAIT_FOR_RECONNECT);
328 }
329 else if (written == 0)
330 {
331 os_SuspendTask (caller, 2000);
332 //break;
333 }
334 else
335 {
336 to_write -= written;
337 buff += written;
338 }
339 }
340 }
341
342 /*
343 +--------------------------------------------------------------------+
344 | PROJECT : GSM-Frame (8415) MODULE : thif_link |
345 | STATE : code ROUTINE : thif_link_Write |
346 +--------------------------------------------------------------------+
347
348 PURPOSE : relay 2 byte header & data to thif_link_Write_to_socket
349
350 */
351 USHORT thif_link_Write ( void *Buffer, ULONG *SizeInBytes )
352 {
353 USHORT TSTSocketMarker;
354 Uint8* buff;
355
356 beSureToBeConnected();
357
358 /* TST's socket interface expects a 2-byte header containing the length of the real message to receive
359 in funtion socket_Read. We just send that header, converting it to little endian in advance
360 TSTSocketMarker = (USHORT) *SizeInBytes;
361 swapUSHORT(&TSTSocketMarker);
362
363 buff = (Uint8*) &TSTSocketMarker;
364 thif_link_Write_to_socket( buff, 2);
365 */
366
367 buff = (Uint8*) Buffer;
368 thif_link_Write_to_socket(buff, *SizeInBytes);
369
370 //thif_socket_flush(THIF_LINK_Data.thif_link);
371 return DRV_OK;
372 }
373
374 /*
375 +--------------------------------------------------------------------+
376 | PROJECT : GSM-Frame (8415) MODULE : thif_link |
377 | STATE : code ROUTINE : thif_link_Flush |
378 +--------------------------------------------------------------------+
379
380 PURPOSE : flush data to driver
381
382 */
383 USHORT thif_link_Flush (void)
384 {
385 beSureToBeConnected();
386 return thif_socket_flush(THIF_LINK_Data.thif_link);
387 }
388
389
390 /*
391 +--------------------------------------------------------------------+
392 | PROJECT : GSM-Frame (8415) MODULE : thif_link |
393 | STATE : code ROUTINE : thif_link_SetSignal |
394 +--------------------------------------------------------------------+
395
396 PURPOSE : enable signal for the driver
397
398 */
399 USHORT thif_link_SetSignal ( USHORT SignalType )
400 {
401 if ( !(SignalType & ALLOWED_THIF_LINK_SIGNALS) )
402 {
403 return DRV_INVALID_PARAMS;
404 }
405 else
406 {
407 THIF_LINK_Data.EnabledSignalType |= SignalType;
408 }
409
410 return DRV_OK;
411 }
412
413 /*
414 +--------------------------------------------------------------------+
415 | PROJECT : GSM-Frame (8415) MODULE : thif_link |
416 | STATE : code ROUTINE : thif_link_ResetSignal |
417 +--------------------------------------------------------------------+
418
419 PURPOSE : disable signal for the driver
420
421 */
422 USHORT thif_link_ResetSignal ( USHORT SignalType )
423 {
424 if ( !(SignalType & ALLOWED_THIF_LINK_SIGNALS) )
425 {
426 return DRV_INVALID_PARAMS;
427 }
428 else
429 {
430 THIF_LINK_Data.EnabledSignalType &= ~SignalType;
431 }
432
433 return DRV_OK;
434 }
435
436 /*
437 +--------------------------------------------------------------------+
438 | PROJECT : GSM-Frame (8415) MODULE : thif_link |
439 | STATE : code ROUTINE : thif_link_SetConfig |
440 +--------------------------------------------------------------------+
441
442 PURPOSE : set configuration for the driver
443
444 */
445 USHORT thif_link_SetConfig ( char *buffer )
446 {
447 char token [64];
448 unsigned int len;
449 USHORT result;
450
451 result = DRV_OK;
452
453 if ( (len = GetNextToken (buffer, token, " #")) == 0)
454 {
455 result = thif_link_Open();
456 }
457 else
458 {
459 buffer += (len+1);
460 }
461
462 if ( !strcmp ( "OPEN", token ) )
463 {
464 result = thif_link_Open();
465 }
466 else if ( !strcmp ( "CLOSE", token ) )
467 {
468 result = thif_link_Close();
469 }
470 else if ( !strcmp ( "EXIT", token ) )
471 {
472 thif_link_Exit();
473 }
474 else
475 {
476 result = DRV_INVALID_PARAMS;
477 }
478
479 if ((result != DRV_OK) && (result != DRV_INVALID_PARAMS))
480 {
481 result = DRV_NOTCONFIGURED;
482 }
483
484 return result;
485 }
486
487
488 /*
489 +--------------------------------------------------------------------+
490 | PROJECT : GSM-Frame (8415) MODULE : thif_link |
491 | STATE : code ROUTINE : thif_link_Init |
492 +--------------------------------------------------------------------+
493
494 PURPOSE : initialize driver
495
496 */
497 USHORT thif_link_Init ( USHORT DrvHandle, T_DRV_CB_FUNC CallbackFunc, T_DRV_EXPORT const **DrvInfo )
498 {
499 static const T_DRV_EXPORT thif_link_Info =
500 {
501 THIF_LINK_NAME,
502 0,
503 {
504 #ifdef _WIN32_
505 thif_link_Init,
506 #endif
507 thif_link_Exit,
508 thif_link_Read,
509 thif_link_Write,
510 NULL, /* Look */
511 NULL, /* Clear */
512 thif_link_Flush,
513 thif_link_SetSignal,
514 thif_link_ResetSignal,
515 thif_link_SetConfig,
516 NULL,
517 NULL,
518 }
519 };
520
521 THIF_LINK_Data.Handle = DrvHandle;
522
523 THIF_LINK_Data.EnabledSignalType = 0;
524
525 THIF_LINK_Data.Callback = CallbackFunc;
526
527 THIF_LINK_Data.Connected = FALSE;
528
529 *DrvInfo = &thif_link_Info;
530
531 return DRV_OK;
532 }
533