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

Subversion Repositories plasma

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

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

powered by: WebSVN 2.1.0

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