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

Subversion Repositories etherlab

[/] [etherlab/] [trunk/] [net/] [EtherSocket/] [src/] [EtherSocket.cs] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 idiolatrie
/******************************************************************************
2
 * ETHERLAB - FPGA To C# To LABVIEW Bridge                                    *
3
 ******************************************************************************
4
 *                                                                            *
5
 * Copyright (C)2012  Mathias Hörtnagl           *
6
 *                                                                            *
7
 * This program is free software: you can redistribute it and/or modify       *
8
 * it under the terms of the GNU General Public License as published by       *
9
 * the Free Software Foundation, either version 3 of the License, or          *
10
 * (at your option) any later version.                                        *
11
 *                                                                            *
12
 * This program is distributed in the hope that it will be useful,            *
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
15
 * GNU General Public License for more details.                               *
16
 *                                                                            *
17
 * You should have received a copy of the GNU General Public License          *
18
 * along with this program.  If not, see .      *
19
 ******************************************************************************/
20
using System;
21
using System.Collections.Generic;
22
using System.Threading;
23
 
24
using PcapDotNet.Core;
25
using PcapDotNet.Packets;
26
using PcapDotNet.Packets.Ethernet;
27
 
28
namespace EtherLab
29
{
30
    /// 
31
    /// A socket for EtherLab communication.
32
    ///
33
    /// 
34
    /// Sending
35
    ///
36
    /// To send data, first update any channel's data with .
37
    /// Subsequently call  to send the updated EtherLab
38
    /// packet. If  isn't invoked at least once,
39
    ///  will NOT send the packet, as there is no updated
40
    /// data available.
41
    /// 
42
    /// 
43
    ///     /**
44
    ///      * Create a new instance of EtherSocket with default
45
    ///      * destination MAC. The first parameter identifies the
46
    ///      * network device to select.
47
    ///      */
48
    ///     EtherSocket es = new EtherSocket(0, "00:1f:16:01:95:a5");
49
    ///
50
    ///     // Update data for channel H and B.
51
    ///     es.update(EChannel.CHANNEL_H, 0x1234);
52
    ///     es.update(EChannel.CHANNEL_B, 0x4321);
53
    ///
54
    ///     // Send the updated packet.
55
    ///     es.send();
56
    /// 
57
    /// 
58
    /// 
59
    ///
60
    /// 
61
    /// Receiving
62
    ///
63
    /// EtherSocket starts a new Thread automatically for the reception process.
64
    /// Data is read constatntly from the networ device.
65
    /// To read a channel's current data chunk, invoke .
66
    ///
67
    /// NOTE: There is no locking mechanism, to circumvent reading data while
68
    ///       new data is read from the network device.
69
    /// 
70
    /// 
71
    ///     /**
72
    ///      * Create a new instance of EtherSocket with default
73
    ///      * destination MAC. The first parameter identifies the
74
    ///      * network device to select.
75
    ///      */
76
    ///     EtherSocket es = new EtherSocket(0, "00:1f:16:01:95:a5");
77
    ///
78
    ///     ushort data;
79
    ///
80
    ///     // Read data of channel H;
81
    ///     data = es.read(EChannel.CHANNEL_H);
82
    /// 
83
    /// 
84
    /// 
85
    /// 
86
    public sealed class EtherSocket
87
    {
88
        /// 
89
        /// Packet receiver filter. Capture packages with EtherType = 0x0000
90
        /// and with a Version 2 field only.
91
        /// 
92
        private const String RCV_FILTER
93
            = "(ether[12:2] = 0x0000) and (ether[14] = 0x02)";
94
 
95
        private LivePacketDevice device;
96
        private PacketCommunicator com;
97
        private PacketBuilder builder;
98
        private EtherLabLayer sendLayer;
99
        private EtherLabLayer receiveLayer;
100
        private Thread receiverThread;
101
 
102
        /// 
103
        /// Creates a new EtherSocket with "00:0a:35:00:00:00" as a default
104
        /// destination MAC.
105
        /// 
106
        /// The integer ID of the network device.
107
        /// The source MAC address.
108
        public EtherSocket(int deviceId, String srcMAC)
109
            : this(deviceId, srcMAC, "00:0a:35:00:00:00")
110
        {
111
        }
112
 
113
        /// 
114
        /// Creates a new EtherSocket.
115
        /// 
116
        /// The integer ID of the network device.
117
        /// The source MAC address.
118
        /// The destination MAC address.
119
        public EtherSocket(int deviceId, String srcMAC, String dstMAC)
120
        {
121
            IList allDevices = LivePacketDevice.AllLocalMachine;
122
 
123
            if ( (allDevices.Count == 0) || (allDevices.Count < deviceId) )
124
            {
125
                return;
126
            }
127
 
128
            device = allDevices[deviceId];
129
            com = device.Open(56, PacketDeviceOpenAttributes.Promiscuous, 1000);
130
 
131
            // Filter packages.
132
            using (BerkeleyPacketFilter filter = com.CreateFilter(RCV_FILTER))
133
            {
134
                com.SetFilter(filter);
135
            }
136
 
137
            EthernetLayer ethernetLayer = new EthernetLayer
138
            {
139
                Source = new MacAddress(srcMAC),
140
                Destination = new MacAddress(dstMAC)
141
            };
142
 
143
            sendLayer = new EtherLabLayer();
144
 
145
            builder = new PacketBuilder(ethernetLayer, sendLayer);
146
 
147
            // Start receiver thread.
148
            receiverThread = new Thread(this.receive);
149
            receiverThread.Start();
150
        }
151
 
152
        /// 
153
        /// Update channel data and set channel flag. Call send() to send
154
        /// the updated data in a new packet.
155
        /// 
156
        /// The channel to be updated.
157
        /// The new data for that channel.
158
        public void update(EChannel channel, ushort channelData)
159
        {
160
            sendLayer.update(channel, channelData);
161
        }
162
 
163
        /// 
164
        /// Sends the current EtherLab packet and resets the channel
165
        /// flags to 0. A packet is sent only if at least one channel
166
        /// flag is set.
167
        /// 
168
        public void send()
169
        {
170
            if (sendLayer.pendingSendData())
171
            {
172
                com.SendPacket(builder.Build(DateTime.Now));
173
                sendLayer.reset();
174
            }
175
        }
176
 
177
        /// 
178
        /// Receive a single packet. Packet data can be read with .
179
        /// 
180
        private void receive()
181
        {
182
            com.ReceivePackets(0, PacketHandler);
183
        }
184
 
185
        /// 
186
        /// Packet reception callback.
187
        /// 
188
        /// The received packet.
189
        private void PacketHandler(Packet packet)
190
        {
191
            EtherLabDatagram etherLabPacket
192
                = new EtherLabDatagram(packet.Buffer, EthernetDatagram.HeaderLength, 18);
193
 
194
            receiveLayer = (EtherLabLayer) etherLabPacket.ExtractLayer();
195
        }
196
 
197
        /// 
198
        /// Read data for a channel from current received packet. If no packet
199
        /// is available yet, this function returns 0.
200
        /// 
201
        /// The channel to be read from.
202
        /// The current channel data.
203
        public ushort read(EChannel channel)
204
        {
205
            return (receiveLayer != null) ? receiveLayer.read(channel) : ushort.MinValue;
206
        }
207
    }
208
}

powered by: WebSVN 2.1.0

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