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

Subversion Repositories plasma

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

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

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

powered by: WebSVN 2.1.0

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