File: Device.cs

package info (click to toggle)
lat 1.2.3-10
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,036 kB
  • sloc: cs: 12,058; sh: 8,616; xml: 469; makefile: 399
file content (183 lines) | stat: -rw-r--r-- 6,276 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/***************************************************************************
 *  Device.cs
 *
 *  Copyright (C) 2005 Novell
 *  Written by Aaron Bockover (aaron@aaronbock.net)
 ****************************************************************************/

/*  THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW: 
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a
 *  copy of this software and associated documentation files (the "Software"),  
 *  to deal in the Software without restriction, including without limitation  
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,  
 *  and/or sell copies of the Software, and to permit persons to whom the  
 *  Software is furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in 
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 *  DEALINGS IN THE SOFTWARE.
 */
 
using System;
using System.Collections;
using DBus;

namespace NetworkManager
{
    public enum DeviceType {
        Unknown,
        Wired,
        Wireless
    }
    
    [Interface("org.freedesktop.NetworkManager.Devices")]
    internal abstract class DeviceProxy
    {
        /* Unsupported methods: 
            
            getProperties
            setLinkActive
            getCapabilities
        */

        [Method] public abstract string getName();
        [Method] public abstract uint getMode();
        [Method] public abstract int getType();
        [Method] public abstract string getHalUdi();
        [Method] public abstract uint getIP4Address();
        [Method] public abstract string getHWAddress();
        [Method] public abstract bool getLinkActive();
        [Method] public abstract NetworkProxy getActiveNetwork();
        [Method] public abstract NetworkProxy [] getNetworks();
    }
    
    public class Device : IEnumerable
    {
        private DeviceProxy device;
        
        internal Device(DeviceProxy device)
        {
            this.device = device;
        }
        
        public override string ToString()
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            builder.Append("Name:             " + Name + "\n");
            builder.Append("Type:             " + Type + "\n");
            builder.Append("Mode:             " + Mode + "\n");
            builder.Append("HAL UDI:          " + HalUdi + "\n");
            builder.Append("IP4 Address:      " + IP4Address + "\n");
            builder.Append("Hardware Address: " + HardwareAddress + "\n");
            builder.Append("Link Active:      " + (IsLinkActive ? "Yes" : "No") + "\n");
            builder.Append("Networks: \n");
            
            int i = 0;
            foreach(Network network in Networks) {
                builder.Append("  [" + (i++) + "] Name:      " + network.Name + "\n");
                builder.Append("      Active:    " + (network.Equals(ActiveNetwork) ? "Yes" : "No") + "\n");
                builder.Append("      Strength:  " + network.Strength + "\n");
                builder.Append("      Frequency: " + network.Frequency + "\n");
                builder.Append("      Rate:      " + network.Rate + "\n");
                builder.Append("      Encrypted: " + (network.IsEncrypted ? "Yes" : "No") + "\n");
                builder.Append("      Mode:      " + network.Mode + "\n");
            }
            
            if(i == 0) {
                builder.Append("  (none)\n");
            }
            
            return builder.ToString();
        }
             
        public string Name {
            get {
                return device.getName();
            }
        }
        
        public DeviceType Type {
            get {
                switch(device.getType()) {
                    case 1: return DeviceType.Wired;
                    case 2: return DeviceType.Wireless;
                    default: return DeviceType.Unknown;
                }
            }
        }
        
        public uint Mode {
            get {
                return device.getMode();
            }
        }

        public string HalUdi {
            get {
                return device.getHalUdi();
            }
        }

        public System.Net.IPAddress IP4Address {
            get {
                return new System.Net.IPAddress(device.getIP4Address());
            }
        }
        
        public string HardwareAddress {
            get {
                return device.getHWAddress();
            }
        }
        
        public bool IsLinkActive {
            get {
                return device.getLinkActive();
            }
        }
        
        public Network ActiveNetwork {
            get {
                if(Type != DeviceType.Wireless) {
                    return null;
                }
                
                try {
                    return new Network(device.getActiveNetwork());
                } catch(DBusException) {
                    return null;
                }
            }
        }
        
        public IEnumerator GetEnumerator()
        {
            foreach(NetworkProxy network in device.getNetworks()) {
                yield return new Network(network);
            }
        }
        
        public Network [] Networks {
            get {
                ArrayList list = new ArrayList();
                
                try {
                    foreach(NetworkProxy network in device.getNetworks()) {
                        list.Add(new Network(network));
                    }
                } catch(Exception) {
                }
                
                return list.ToArray(typeof(Network)) as Network [];
            }
        }
    }
}