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

Subversion Repositories etherlab

[/] [etherlab/] [trunk/] [net/] [EtherSocket/] [src/] [etherlab/] [EtherLabLayer.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 PcapDotNet.Packets;
21
using PcapDotNet.Packets.Ethernet;
22
 
23
namespace EtherLab
24
{
25
    /// 
26
    /// Represents the EtherLab layer for Pcap.NET.
27
    /// 
28
    public sealed class EtherLabLayer : SimpleLayer, IEthernetNextLayer
29
    {
30
        /// 
31
        /// The version number of the EtherLab protocol.
32
        /// 
33
        public byte Version { set; get; }
34
 
35
        /// 
36
        /// The channel number octet. Specifies the virtual channel this
37
        /// package belongs to.
38
        /// 
39
        public byte Channel { set; get; }
40
 
41
        /// 
42
        /// The channel data. 8 channels, 16 bit wide each. Data segment is 40
43
        /// byte wide as we need at least 42 byte of payload for an Ethernet
44
        /// package (remaining 2 bytes are EtherLab header).
45
        /// 
46
        public byte[] Data { get; set; }
47
 
48
        /// 
49
        /// The default MAC Address value when this layer is the Ethernet payload.
50
        /// null means there is no default value.
51
        /// 
52
        public MacAddress? PreviousLayerDefaultDestination
53
        {
54
            get { return EthernetDatagram.BroadcastAddress; }
55
        }
56
 
57
        /// 
58
        /// The Ethernet Type the Ethernet layer should write when this layer is the Ethernet payload.
59
        /// 
60
        public EthernetType PreviousLayerEtherType
61
        {
62
            get { return EthernetType.None; }
63
        }
64
 
65
        /// 
66
        /// The number of bytes this layer will take.
67
        /// 
68
        public override int Length
69
        {
70
            get { return EtherLabDatagram.HeaderLength + 16; }
71
        }
72
 
73
        /// 
74
        /// Two EtherLabLayers are always equal. I could not find reasonable
75
        /// comparison parameters.
76
        /// 
77
        /// An EtherLabLayer instance.
78
        /// True if the other instance is not null and an instance of EtherLabLayer.
79
        public override bool Equals(Layer other)
80
        {
81
            return other != null && other.GetType() == GetType();
82
        }
83
 
84
        /// 
85
        /// Constructor for a default EtherLab package.
86
        /// Version 1.
87
        /// 
88
        public EtherLabLayer()
89
        {
90
            Version = 1;
91
            Channel = 0;
92
            Data = new byte[16];
93
        }
94
 
95
        /// 
96
        /// Writes the layer to the buffer.
97
        /// This method ignores the payload length, and the previous and next layers.
98
        /// 
99
        /// The buffer to write the layer to.
100
        /// The offset in the buffer to start writing the layer at.
101
        protected sealed override void Write(byte[] buffer, int offset)
102
        {
103
            EtherLabDatagram.Write(buffer, offset, Version, Channel, Data);
104
        }
105
 
106
        /// 
107
        /// Checks, whether there is new data, that has not yet been sent.
108
        /// 
109
        /// True, if at least one channel holds new data.
110
        public bool pendingSendData()
111
        {
112
            return Channel != 0;
113
        }
114
 
115
        /// 
116
        /// Update channel data and set channel flag. Upon next send, updated
117
        /// data will be transmitted.
118
        /// 
119
        /// The channel to be updated.
120
        /// The new data.
121
        public void update(EChannel channel, ushort channelData)
122
        {
123
            int pos = (int)channel;
124
            Channel |= (byte)(1 << pos);
125
            Data.Write(2 * pos, channelData, Endianity.Big);
126
        }
127
 
128
        /// 
129
        /// Read data for a channel from current received packet. If no packet
130
        /// is available yet, this function returns 0.
131
        /// 
132
        /// The channel to be read from.
133
        /// The current channel data.
134
        public ushort read(EChannel channel)
135
        {
136
            int pos = (int)channel;
137
            return Data.ReadUShort(2 * pos, Endianity.Big);
138
        }
139
 
140
        /// 
141
        /// Reset channel flags to zero. Clear the flags after a package has
142
        /// been sent. Only set channel flags trigger an update on hardware
143
        /// level.
144
        /// 
145
        public void reset()
146
        {
147
            Channel = 0;
148
        }
149
    }
150
}

powered by: WebSVN 2.1.0

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