OpenCores
URL https://opencores.org/ocsvn/plasma/plasma/trunk

Subversion Repositories plasma

[/] [plasma/] [trunk/] [tools/] [etermip.c] - Blame information for rev 269

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 242 rhoads
/*--------------------------------------------------------------------
2
 * TITLE: etermip
3
 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4
 * DATE CREATED: 6/13/07
5
 * FILENAME: etermip.c
6
 * PROJECT: Plasma CPU core
7
 * COPYRIGHT: Software placed into the public domain by the author.
8
 *    Software 'as is' without warranty.  Author liable for nothing.
9
 * DESCRIPTION:
10
 *    A terminal program supporting downloading new Plasma applications
11
 *    and Ethernet packet transfers.  Based on WinPcap example code.
12
 *    Requires WinPcap library at http://www.winpcap.org/.
13
 *--------------------------------------------------------------------*/
14
#include <windows.h>
15
#include <stdio.h>
16
#include <conio.h>
17
 
18
//#define SIMULATE_PLASMA
19
 
20
#if 0
21
   #include "pcap.h"
22
#else
23
   //From "pcap.h"
24
   #define PCAP_ERRBUF_SIZE 256
25
   typedef struct pcap_if {
26
      struct pcap_if *next;
27
      char *name;               /* name to hand to "pcap_open_live()" */
28
      char *description;        /* textual description of interface, or NULL */
29
      struct pcap_addr *addresses;
30
      unsigned long flags;      /* PCAP_IF_ interface flags */
31
   } pcap_if_t;
32
   struct pcap_pkthdr {
33
      struct timeval ts;        /* time stamp */
34
      unsigned long caplen;     /* length of portion present */
35
      unsigned long len;        /* length this packet (off wire) */
36
   };
37
   typedef struct pcap pcap_t;
38
 
39
   int pcap_findalldevs(pcap_if_t **, char *);
40
   void pcap_freealldevs(pcap_if_t *);
41
   pcap_t *pcap_open_live(const char *, int, int, int, char *);
42
   int pcap_setnonblock(pcap_t *, int, char *);
43
   int pcap_sendpacket(pcap_t *, const u_char *, int);
44
   const unsigned char *pcap_next(pcap_t *, struct pcap_pkthdr *);
45
#endif
46
 
47
//ETHER FIELD                 OFFSET   LENGTH   VALUE
48
#define ETHERNET_DEST         0        //6
49
#define ETHERNET_SOURCE       6        //6
50
#define ETHERNET_FRAME_TYPE   12       //2      IP=0x0800; ARP=0x0806
51
#define IP_PROTOCOL           23       //1      TCP=0x06;PING=0x01;UDP=0x11
52
#define IP_SOURCE             26       //4
53
 
54
static const unsigned char ethernetAddressNull[] =    {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
55
static const unsigned char ethernetAddressPhantom[] = {0x00, 0x10, 0xdd, 0xce, 0x15, 0xd4};
56
static const unsigned char ethernetAddressPhantom2[] = {0x00, 0x10, 0xdd, 0xce, 0x15, 0xd5};
57
 
58
static pcap_t *adhandle;
59
static HANDLE serial_handle;
60
static int PacketBytes, PacketLength, PacketChecksum, Checksum;
61
static int ChecksumOk, ChecksumError;
62
static unsigned char PacketData[2000];
63
static int EthernetActive;
64
 
65
#ifdef SIMULATE_PLASMA
66
   extern void *IPFrameGet(int freeCount);
67
   extern int IPProcessEthernetPacket(void *frameIn, int length);
68
   extern void IPTick(void);
69
   extern void IPInit(void (*frameSendFunction)());
70
   extern void HtmlInit(int UseFiles);
71
   extern void ConsoleInit(void);
72
   static void *ethFrame;
73
#endif
74
 
75
 
76
int WinPcapInit(void)
77
{
78
        pcap_if_t *alldevs;
79
        pcap_if_t *d;
80
        int inum;
81
        int i=0;
82
   int choice = -1;
83
        char errbuf[PCAP_ERRBUF_SIZE];
84
 
85
   /* Retrieve the device list */
86
        if(pcap_findalldevs(&alldevs, errbuf) == -1)
87
        {
88
                printf("Error in pcap_findalldevs: %s\n", errbuf);
89
                exit(1);
90
        }
91
 
92
        /* Print the list */
93
        for(d = alldevs; d; d=d->next)
94
        {
95
                printf("%d. %s", ++i, d->name);
96
                if (d->description)
97
                        printf(" (%s)\n", d->description);
98
                else
99
                        printf(" (No description available)\n");
100
      if(strstr(d->description, "Generic") == 0)
101
      {
102
         if(choice == -1)
103
            choice = i;
104
         else
105
            choice = -2;
106
      }
107
        }
108
 
109
        if(i==0)
110
        {
111
                printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
112
                return -1;
113
        }
114
 
115
   if(choice >= 0)
116
      inum = choice;
117
   else if(i == 1)
118
      inum = 1;
119
   else
120
   {
121
           printf("Enter the interface number (1-%d):",i);
122
           scanf("%d", &inum);
123
   }
124
   printf("inum = %d\n", inum);
125
 
126
        if(inum < 1 || inum > i)
127
        {
128
                printf("\nInterface number out of range.\n");
129
                /* Free the device list */
130
                pcap_freealldevs(alldevs);
131
                return -1;
132
        }
133
 
134
        /* Jump to the selected adapter */
135
        for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
136
 
137
        /* Open the adapter */
138
        if ((adhandle = pcap_open_live(d->name, // name of the device
139 269 rhoads
                                       65536,   // 65536 grants that the whole packet will be captured on all the MACs.
140
                                       1,       // promiscuous mode (nonzero means promiscuous)
141
                                       10,      // read timeout
142
                                       errbuf   // error buffer
143
                                       )) == NULL)
144 242 rhoads
        {
145
                printf("\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
146
                /* Free the device list */
147
                pcap_freealldevs(alldevs);
148
                return -1;
149
        }
150
 
151
        printf("\nlistening on %s...\n", d->description);
152
 
153
        /* At this point, we don't need any more the device list. Free it */
154
        pcap_freealldevs(alldevs);
155
 
156
   /* start the capture */
157
   pcap_setnonblock(adhandle, 1, errbuf);
158
 
159
   return 0;
160
}
161
 
162
 
163
void EthernetSendPacket(const unsigned char *packet, int length)
164
{
165
   EthernetActive = 1;
166
   pcap_sendpacket(adhandle, packet, length);
167
}
168
 
169
 
170
/* Callback function invoked by libpcap for every incoming packet */
171
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
172
{
173
#ifndef SIMULATE_PLASMA
174
   int i, checksum;
175
   unsigned char buf[80];
176
   DWORD count;
177
#else
178
   int rc;
179
#endif
180
   (void)param;
181
 
182
   if(pkt_data[ETHERNET_FRAME_TYPE] != 0x08)
183
      return;  //not IP or ARP
184
   if(pkt_data[ETHERNET_FRAME_TYPE+1] != 0x00 &&
185
      pkt_data[ETHERNET_FRAME_TYPE+1] != 0x06)
186
      return;  //not IP or ARP
187
   if(memcmp(pkt_data, ethernetAddressNull, 6) &&      //not broadcast address
188
      memcmp(pkt_data+ETHERNET_DEST, ethernetAddressPhantom, 6) &&
189
      memcmp(pkt_data+ETHERNET_DEST, ethernetAddressPhantom2, 6))
190
      return;
191
 
192
#ifndef SIMULATE_PLASMA
193
   //Send the ethernet packet over the serial port
194
   buf[0] = 0xff;
195
   buf[1] = (unsigned char)(header->len >> 8);
196
   buf[2] = (unsigned char)header->len;
197
   checksum = 0;
198
   for(i = 0; i < (int)header->len; ++i)
199
      checksum += pkt_data[i];
200
   buf[3] = (unsigned char)checksum;
201
   WriteFile(serial_handle, buf, 4, &count, NULL);
202
   WriteFile(serial_handle, pkt_data, header->len, &count, NULL);
203
#else
204
   if(ethFrame == NULL)
205
      ethFrame = IPFrameGet(0);
206
   if(ethFrame == NULL)
207
      return;
208
   memcpy(ethFrame, pkt_data, header->len);
209
   rc = IPProcessEthernetPacket(ethFrame, header->len);
210
   if(rc)
211
      ethFrame = NULL;
212
#endif
213
}
214
 
215
/**************************************************************/
216
 
217
long SerialOpen(char *name, long baud)
218
{
219
   DCB dcb;
220
   COMMTIMEOUTS comm_timeouts;
221
   BOOL rc;
222
   serial_handle = CreateFile(name, GENERIC_READ|GENERIC_WRITE,
223
      0, NULL, OPEN_EXISTING, 0, NULL);
224
   if(serial_handle == INVALID_HANDLE_VALUE)
225
      printf("Serial Port In Use!\n");
226
   rc = SetupComm(serial_handle, 16000, 16000);
227
   if(rc == FALSE)
228
      printf("Serial port already in use!!!\n");
229
   rc = GetCommState(serial_handle, &dcb);
230
   if(rc == FALSE)
231
      printf("ERROR2\n");
232
   dcb.BaudRate = baud;
233
   dcb.fBinary = 1;
234
   dcb.ByteSize = 8;
235
   dcb.fAbortOnError = 0;
236
   dcb.StopBits = 0; //ONESTOPBIT;
237
   dcb.fOutX = 0;
238
   dcb.fInX = 0;
239
   dcb.fNull = 0;
240
   dcb.Parity = 0;
241
   dcb.fOutxCtsFlow = 0;
242
   dcb.fOutxDsrFlow = 0;
243
   dcb.fOutX = 0;
244
   dcb.fInX = 0;
245
   dcb.fRtsControl = 0;
246
   rc = SetCommState(serial_handle, &dcb);
247
   if(rc == FALSE)
248
      printf("ERROR3\n");
249
   rc = GetCommTimeouts(serial_handle, &comm_timeouts);
250
   if(rc == FALSE)
251
      printf("ERROR4\n");
252
   comm_timeouts.ReadIntervalTimeout = MAXDWORD;  //non-blocking read
253
   comm_timeouts.ReadTotalTimeoutMultiplier = 0;
254
   comm_timeouts.ReadTotalTimeoutConstant = 0;
255
   comm_timeouts.WriteTotalTimeoutMultiplier = 0;  //blocking write
256
   comm_timeouts.WriteTotalTimeoutConstant = 0;
257
   rc = SetCommTimeouts(serial_handle, &comm_timeouts);
258
   if(rc == FALSE)
259
      printf("ERROR5\n");
260
   return(0);
261
}
262
 
263
 
264
static void UartPacketRead(int value)
265
{
266
   if(PacketBytes == 0 && value == 0xff)
267
   {
268
      ++PacketBytes;
269
   }
270
   else if(PacketBytes == 1)
271
   {
272
      ++PacketBytes;
273
      PacketLength = value << 8;
274
   }
275
   else if(PacketBytes == 2)
276
   {
277
      ++PacketBytes;
278
      PacketLength |= value;
279
      if(PacketLength > 1000)
280
      {
281
         PacketBytes = 0;
282
         printf("Eterm Length Bad! (%d)\n", PacketLength);
283
      }
284
   }
285
   else if(PacketBytes == 3)
286
   {
287
      ++PacketBytes;
288
      PacketChecksum = value;
289
      Checksum = 0;
290
   }
291
   else if(PacketBytes >= 4)
292
   {
293
      if(PacketBytes - 4 < sizeof(PacketData))
294
         PacketData[PacketBytes - 4] = (unsigned char)value;
295
      Checksum += value;
296
      ++PacketBytes;
297
      if(PacketBytes - 4 >= PacketLength)
298
      {
299
         if((unsigned char)Checksum == PacketChecksum)
300
         {
301
            ++ChecksumOk;
302
            EthernetSendPacket(PacketData, PacketLength);
303
         }
304
         else
305
         {
306
            ++ChecksumError;
307
            //printf("ChecksumError(%d %d)!\n", ChecksumOk, ChecksumError);
308
         }
309
         PacketBytes = 0;
310
      }
311
   }
312
}
313
 
314
 
315
long SerialRead(unsigned char *data, unsigned long length)
316
{
317
   DWORD count, bytes;
318
   unsigned char buf[8];
319
 
320
   count = 0;
321
   for(;;)
322
   {
323
      ReadFile(serial_handle, buf, 1, &bytes, NULL);
324
      if(bytes == 0)
325
         break;
326
      if(buf[0] == 0xff || PacketBytes)
327
         UartPacketRead(buf[0]);
328
      else
329
         data[count++] = buf[0];
330
      if(count >= length)
331
         break;
332
   }
333
   return count;
334
}
335
 
336
 
337
//****************************************************
338
 
339
#define BUF_SIZE 1024*128
340
void SendFile(void)
341
{
342
   FILE *in;
343
   unsigned char *buf;
344
   long length;
345
   DWORD count;
346
 
347
   in=fopen("test.bin", "rb");
348
   if(in==NULL) {
349
      printf("Can't find test.bin\n");
350
      return;
351
   }
352
   buf = (unsigned char*)malloc(BUF_SIZE);
353
   memset(buf, 0, BUF_SIZE);
354
   length = (int)fread(buf, 1, BUF_SIZE, in);
355
   fclose(in);
356
   printf("Sending test.bin (length=%d bytes) to target...\n", length);
357
   WriteFile(serial_handle, buf, length, &count, NULL);
358
   printf("Done downloading\n");
359
   free(buf);
360
}
361
 
362
 
363
int main(int argc, char *argv[])
364
{
365
   unsigned int ticksLast = GetTickCount();
366
   int length;
367
   unsigned char buf[80];
368
   DWORD count;
369
   unsigned int ticks;
370 269 rhoads
   int downloadSkip = 0;
371 242 rhoads
   (void)argc;
372
   (void)argv;
373
 
374
   WinPcapInit();
375
#ifndef SIMULATE_PLASMA
376
   SerialOpen("COM1", 57600);
377 269 rhoads
   if(argc != 2 || strcmp(argv[1], "none"))
378
      SendFile();
379
   else
380
      downloadSkip = 1;
381 242 rhoads
#else
382
   IPInit(EthernetSendPacket);
383
   HtmlInit(1);
384
   ConsoleInit();
385
#endif
386
 
387
   for(;;)
388
   {
389
      // Read keypresses
390
      while(kbhit())
391
      {
392
         buf[0] = (unsigned char)getch();
393 269 rhoads
         if(downloadSkip && buf[0] == '`')
394
            SendFile();
395 242 rhoads
         WriteFile(serial_handle, buf, 1, &count, NULL);
396
      }
397
 
398
      // Read UART
399
      for(;;)
400
      {
401
         length = SerialRead(buf, sizeof(buf));
402
         if(length == 0)
403
            break;
404
         buf[length] = 0;
405
         printf("%s", buf);
406
      }
407
 
408
      // Read Ethernet
409
      for(;;)
410
      {
411
         struct pcap_pkthdr header;
412
         const u_char *pkt_data;
413
         pkt_data = pcap_next(adhandle, &header);
414
         if(pkt_data == NULL)
415
            break;
416
         if(EthernetActive)
417
            packet_handler(NULL, &header, pkt_data);
418
      }
419
      Sleep(10);
420
      ticks = GetTickCount();
421
      if(ticks - ticksLast > 1000)
422
      {
423
#ifdef SIMULATE_PLASMA
424
         IPTick();
425
#endif
426
         ticksLast = ticks;
427
      }
428
   }
429
}
430 269 rhoads
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.