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

Subversion Repositories etherlab

[/] [etherlab/] [trunk/] [net/] [EtherSocket/] [src/] [etherlab/] [EtherLabDatagram.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
 
22
namespace EtherLab
23
{
24
    /// 
25
    /// Represents an EtherLab datagram.
26
    ///
27
    /// An EtherLab Datagram sends data for all active channels in one
28
    /// packet whenever send() is called.
29
    /// A packet, whether sent or received, contains at most 8 independent
30
    /// data chunks of 16 bit size each.
31
    ///
32
    /// 
33
    /// +-----+----------------+----------------+
34
    /// | Bit | 0-7            | 8-15           |
35
    /// +-----+----------------+----------------+
36
    /// | 0   | Version        | Channels       |
37
    /// +-----+----------------+----------------+
38
    /// | 16  | Channel A Data                  |
39
    /// +-----+---------------------------------+
40
    /// | 32  | Channel B Data                  |
41
    /// +-----+---------------------------------+
42
    /// :     :                                 :
43
    /// +-----+---------------------------------+
44
    /// | 112 | Channel H Data                  |
45
    /// +-----+---------------------------------+
46
    /// 
47
    ///
48
    /// 
49
    /// Channel Field
50
    ///
51
    /// The Channels field is 1 byte wide and can thus hold 8 seperate
52
    /// channels. An active channel is repesented by its flag set to 1.
53
    /// 
54
    /// +-----+-------------------------------+
55
    /// | Bit | 8-15                          |
56
    /// +-----+---+---+---+---+---+---+---+---+
57
    /// | 8   | H | G | F | E | D | C | B | A |
58
    /// +-----+---+---+---+---+---+---+---+---+
59
    /// 
60
    /// Each channel is labeled with an alphabetic letter (A to H).
61
    /// 
62
    /// 
63
    public sealed class EtherLabDatagram : Datagram
64
    {
65
        private static class Offset
66
        {
67
            public const int Version = 0;
68
            public const int Channel = 1;
69
            public const int Data = 2;
70
        }
71
 
72
        /// 
73
        /// The number of bytes the EtherLab header takes.
74
        /// 
75
        public const int HeaderLength = 2;
76
 
77
        /// 
78
        /// The version number of the EtherLab protocol.
79
        /// 
80
        public byte Version
81
        {
82
            get { return this[Offset.Version]; }
83
        }
84
 
85
        /// 
86
        /// The channel number octet. Specifies the virtual channel this
87
        /// package belongs to.
88
        /// 
89
        public byte Channel
90
        {
91
            get { return this[Offset.Channel]; }
92
        }
93
 
94
        /// 
95
        /// The channel data. 8 channels, 16 bit wide each. Data segment is 40
96
        /// byte wide as we need at least 42 byte of payload for an Ethernet
97
        /// package (remaining 2 bytes are EtherLab header).
98
        /// 
99
        public byte[] Data
100
        {
101
            get
102
            {
103
                return ReadBytes(Offset.Data, Length - HeaderLength);
104
            }
105
        }
106
 
107
        /// 
108
        /// Constructor for a new EtherLab Datagram.
109
        /// 
110
        /// The buffer to write the layer to.
111
        /// The offset in the buffer to start writing the layer at.
112
        /// The datagram length.
113
        internal EtherLabDatagram(byte[] buffer, int offset, int length)
114
            : base(buffer, offset, length)
115
        {
116
        }
117
 
118
        /// 
119
        /// Creates a Layer that represents the datagram to be used with PacketBuilder.
120
        /// 
121
        public override ILayer ExtractLayer()
122
        {
123
            return new EtherLabLayer
124
            {
125
                Version = Version,
126
                Channel = Channel,
127
                Data = Data
128
            };
129
        }
130
 
131
        /// 
132
        /// Writes the Datagram to the buffer.
133
        /// 
134
        /// The buffer to write the layer to.
135
        /// The offset in the buffer to start writing the layer at.
136
        /// The EtherLab protocol version.
137
        /// The active channels.
138
        /// The channels' data.
139
        internal static void Write(byte[] buffer, int offset, byte version, byte channel, byte[] data)
140
        {
141
            buffer.Write(offset + Offset.Version, version);
142
            buffer.Write(offset + Offset.Channel, channel);
143
 
144
            for (int i = 0; i < 16; i++)
145
                buffer.Write(offset + Offset.Data + i, data[i]);
146
        }
147
    }
148
}

powered by: WebSVN 2.1.0

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