forked from BulldogDrummond/aldl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serio-dummy.c
158 lines (142 loc) · 3.52 KB
/
serio-dummy.c
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
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include "serio.h"
#include "aldl-io.h"
#include "error.h"
#include "config.h"
/************ SCOPE *********************************
A dummy serial handler object that pretends to be
a fake LT1 (EE) ECM and just send random garbage
(00-FF) as a datastream.
****************************************************/
/****************GLOBALSn'STRUCTURES*****************************/
unsigned char *databuff;
char txmode;
void gen_pkt();
/****************FUNCTIONS**************************************/
void serial_close() {
#ifdef SERIAL_VERBOSE
printf("SERIAL CLOSE (discarded)\n");
#endif
return;
}
void gen_pkt() {
int x;
databuff[0]=0xF4;
databuff[1]=0x92;
databuff[2]=0x01;
for(x=3;x<63;x++) databuff[x] = ( (byte)rand() % 256 ) - 1;
databuff[63] = checksum_generate(databuff,63);
#ifdef DUMMY_CORRUPTION_ENABLE
/* insert random bullshit sometimes */
if( ( (byte)rand() % 100 ) < DUMMY_CORRPUTION_RATE ) {
#ifdef SERIAL_VERBOSE
printf("serial dummy driver - inserting random corruption!\n");
#endif
for(x=0;x<=DUMMY_CORRUPTION_AMOUNT;x++) {
databuff[(byte)rand() % 60] = ( (byte)rand() % 256 ) - 1;
}
}
#endif
}
int serial_init(char *port) {
#ifdef SERIAL_VERBOSE
printf("Serial dummy driver initialized!\n");
#endif
txmode=0;
databuff=malloc(64);
return 1;
}
void serial_purge() {
#ifdef SERIAL_VERBOSE
printf("SERIAL PURGE RX/TX (Dummy Ignored)\n");
#endif
return;
}
void serial_purge_rx() {
#ifdef SERIAL_VERBOSE
printf("SERIAL PURGE RX (Dummy Ignored)\n");
#endif
return;
}
void serial_purge_tx() {
#ifdef SERIAL_VERBOSE
printf("SERIAL PURGE TX (Dummy Ignored)\n");
#endif
return;
}
int serial_write(byte *str, int len) {
#ifdef SERIAL_VERBOSE
printf("WRITE: ");
printhexstring(str,len);
#endif
/* determine mode */
if(len == 4 && str[0] == 0xF4 && str[1] == 0x56 && \
str[2] == 0x08 && str[3] == 0xAE) {
txmode = 1;
}
return 0;
}
inline int serial_read(byte *str, int len) {
if(txmode == 0) { /* idle traffic req */
usleep(SERIAL_BYTES_PER_MS * 64 * 1000); /* fake baud delay */
str[0] = 0x33;
txmode++;
#ifdef SERIAL_VERBOSE
printf("DUMMY MODE: Idle Traffic Req: ");
printhexstring(str,1);
#endif
return 1;
} if(txmode == 1) { /* shutup req */
usleep(SERIAL_BYTES_PER_MS * 5 * 1000); /* fake baud delay */
str[0] = 0xF4;
str[1] = 0x56;
str[2] = 0x08;
str[3] = 0xAE;
txmode++;
#ifdef SERIAL_VERBOSE
printf("DUMMY MODE: Silence Request: ");
printhexstring(str,4);
#endif
return 4;
} if(txmode == 2) { /* data request reply */
usleep(SERIAL_BYTES_PER_MS * 5 * 1000); /* fake baud delay */
txmode = 3;
str[0] = 0xF4;
str[1] = 0x57;
str[2] = 0x01;
str[3] = 0x00;
str[4] = 0xB4;
#ifdef SERIAL_VERBOSE
printf("DUMMY MODE: Data Req. Reply: ");
printhexstring(str,5);
#endif
return 5;
} if(txmode == 3) { /* data send */
usleep(SERIAL_BYTES_PER_MS * len * 1000); /* fake baud delay */
txmode = 2;
gen_pkt();
#ifdef SERIAL_VERBOSE
printf("DUMMY MODE: Generated packet...\n");
#endif
int x;
for(x=0;x<len;x++) {
str[x] = databuff[x];
}
return len;
}
return 0;
}
void serial_help_devs() {
error(1,ERROR_GENERAL,"this serial driver has no devices......");
}
int serial_get_status() {
return 1;
}