]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Families/IONetworking/IONetworkMedium.cpp
7a10de58b08c6cf93b7c348f2de4a81bec574990
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
31 #include <IOKit/assert.h>
32 #include <IOKit/IOLib.h>
33 #include <libkern/c++/OSObject.h>
34 #include <libkern/c++/OSNumber.h>
35 #include <libkern/c++/OSCollectionIterator.h>
36 #include <libkern/c++/OSDictionary.h>
37 #include <libkern/c++/OSSerialize.h>
38 #include <IOKit/network/IONetworkMedium.h>
40 //---------------------------------------------------------------------------
41 // OSMetaClass macros.
43 #define super OSObject
44 OSDefineMetaClassAndStructors( IONetworkMedium
, OSObject
)
45 OSMetaClassDefineReservedUnused( IONetworkMedium
, 0);
46 OSMetaClassDefineReservedUnused( IONetworkMedium
, 1);
47 OSMetaClassDefineReservedUnused( IONetworkMedium
, 2);
48 OSMetaClassDefineReservedUnused( IONetworkMedium
, 3);
50 //---------------------------------------------------------------------------
51 // Initialize an IONetworkMedium instance.
53 // type: The medium type, the fields are encoded with bits defined in
56 // speed: The maximum (or the only) link speed supported over this medium
57 // in units of bits per second.
59 // flags: An optional flag for the medium object.
60 // See IONetworkMedium.h for defined flags.
62 // index: An optional 32-bit index assigned by the caller. Drivers can use
63 // this to store an index or a pointer to a media table inside the
64 // driver, or it may map to a driver defined media type.
66 // name: An name to assign to this medium object. If 0, then a name
67 // will be created based on the medium type given using nameForType().
69 // Returns true on success, false otherwise.
71 bool IONetworkMedium::init(IOMediumType type
,
75 const char * name
= 0)
77 if ( super::init() == false )
86 _name
= OSSymbol::withCString(name
);
88 _name
= IONetworkMedium::nameForType(type
);
96 //---------------------------------------------------------------------------
97 // Factory method which performs allocation and initialization
98 // of an IONetworkMedium instance.
100 // Returns an IONetworkMedium instance on success, or 0 otherwise.
102 IONetworkMedium
* IONetworkMedium::medium(IOMediumType type
,
106 const char * name
= 0)
108 IONetworkMedium
* medium
= new IONetworkMedium
;
110 if (medium
&& !medium
->init(type
, speed
, flags
, index
, name
))
119 //---------------------------------------------------------------------------
120 // Free the IONetworkMedium instance.
122 void IONetworkMedium::free()
132 //---------------------------------------------------------------------------
133 // Return the assigned medium type.
135 IOMediumType
IONetworkMedium::getType() const
140 //---------------------------------------------------------------------------
141 // Return the medium flags.
143 UInt32
IONetworkMedium::getFlags() const
148 //---------------------------------------------------------------------------
149 // Return the maximum medium speed.
151 UInt64
IONetworkMedium::getSpeed() const
156 //---------------------------------------------------------------------------
157 // Return the assigned index.
159 UInt32
IONetworkMedium::getIndex() const
164 //---------------------------------------------------------------------------
165 // Return the name for this instance.
167 const OSSymbol
* IONetworkMedium::getName() const
172 //---------------------------------------------------------------------------
173 // Given a medium type, create an unique OSymbol name for the medium.
174 // The caller is responsible for releasing the OSSymbol object returned.
176 // type: A medium type. See IONetworkMedium.h for type encoding.
178 // Returns an OSSymbol created based on the type provided.
180 const OSSymbol
* IONetworkMedium::nameForType(IOMediumType type
)
184 sprintf(buffer
, "%08lx", type
);
186 // Caller must remember to free the OSSymbol!
188 return OSSymbol::withCString(buffer
);
191 //---------------------------------------------------------------------------
192 // Test for equality between two IONetworkMedium objects.
193 // Two IONetworkMedium objects are considered equal if
194 // they have similar properties assigned to them during initialization.
196 // medium: An IONetworkMedium to test against the IONetworkMedium
197 // object being called.
199 // Returns true if equal, false otherwise.
201 bool IONetworkMedium::isEqualTo(const IONetworkMedium
* medium
) const
203 return ( (medium
->_name
== _name
) &&
204 (medium
->_type
== _type
) &&
205 (medium
->_flags
== _flags
) &&
206 (medium
->_speed
== _speed
) &&
207 (medium
->_index
== _index
) );
210 //---------------------------------------------------------------------------
211 // Test for equality between a IONetworkMedium object and an OSObject.
212 // The OSObject is considered equal to the IONetworkMedium object if the
213 // OSObject is an IONetworkMedium, and they have similar properties assigned
214 // to them during initialization.
216 // obj: An OSObject to test against the IONetworkMedium object being called.
218 // Returns true if equal, false otherwise.
220 bool IONetworkMedium::isEqualTo(const OSMetaClassBase
* obj
) const
222 IONetworkMedium
* medium
;
223 if ((medium
= OSDynamicCast(IONetworkMedium
, obj
)))
224 return isEqualTo(medium
);
229 //---------------------------------------------------------------------------
230 // Create an OSData containing an IOMediumDescriptor structure (not copied),
231 // and ask the OSData to serialize.
233 // s: An OSSerialize object to handle the serialization.
235 // Returns true on success, false otherwise.
237 static bool addNumberToDict(OSDictionary
* dict
,
242 OSNumber
* num
= OSNumber::withNumber(val
, bits
);
245 if ( num
== 0 ) return false;
246 ret
= dict
->setObject( key
, num
);
251 bool IONetworkMedium::serialize(OSSerialize
* s
) const
256 dict
= OSDictionary::withCapacity(4);
257 if ( dict
== 0 ) return false;
259 addNumberToDict(dict
, kIOMediumType
, getType());
260 addNumberToDict(dict
, kIOMediumSpeed
, getSpeed(), 64);
261 addNumberToDict(dict
, kIOMediumIndex
, getIndex());
262 addNumberToDict(dict
, kIOMediumFlags
, getFlags());
264 ret
= dict
->serialize(s
);
270 //---------------------------------------------------------------------------
271 // A helper function to add an IONetworkMedium object to a given dictionary.
272 // The name of the medium is used as the key for the new dictionary entry.
274 // dict: An OSDictionary object where the medium object should be added to.
275 // medium: The IONetworkMedium object to add to the dictionary.
277 // Returns true on success, false otherwise.
279 bool IONetworkMedium::addMedium(OSDictionary
* dict
,
280 const IONetworkMedium
* medium
)
282 // Arguments type checking.
284 if (!OSDynamicCast(OSDictionary
, dict
) ||
285 !OSDynamicCast(IONetworkMedium
, medium
))
288 return dict
->setObject(medium
->getName(), medium
);
291 //---------------------------------------------------------------------------
292 // A helper function to remove an entry in a dictionary with a key that
293 // matches the name of the IONetworkMedium object provided.
295 // dict: An OSDictionary object where the medium object should be removed
297 // medium: The name of this medium object is used as the removal key.
299 void IONetworkMedium::removeMedium(OSDictionary
* dict
,
300 const IONetworkMedium
* medium
)
302 // Arguments type checking.
304 if (!OSDynamicCast(OSDictionary
, dict
) ||
305 !OSDynamicCast(IONetworkMedium
, medium
))
308 dict
->removeObject(medium
->getName());
311 //---------------------------------------------------------------------------
312 // Iterate through a dictionary and return an IONetworkMedium entry that
313 // satisfies the matching criteria. Returns 0 if there is no match.
315 IONetworkMedium
* IONetworkMedium::getMediumWithType(
316 const OSDictionary
* dict
,
318 IOMediumType mask
= 0)
320 OSCollectionIterator
* iter
;
322 IONetworkMedium
* medium
;
323 IONetworkMedium
* match
= 0;
327 // Shouldn't withCollection take an (const OSDictionary *) argument?
329 iter
= OSCollectionIterator::withCollection((OSDictionary
*) dict
);
333 while ( (key
= (OSSymbol
*) iter
->getNextObject()) )
335 medium
= OSDynamicCast(IONetworkMedium
, dict
->getObject(key
));
336 if (medium
== 0) continue;
338 if ( ( (medium
->getType() ^ type
) & ~mask
) == 0 )
350 IONetworkMedium
* IONetworkMedium::getMediumWithIndex(
351 const OSDictionary
* dict
,
355 OSCollectionIterator
* iter
;
357 IONetworkMedium
* medium
;
358 IONetworkMedium
* match
= 0;
362 // Shouldn't withCollection take an (const OSDictionary *) argument?
364 iter
= OSCollectionIterator::withCollection((OSDictionary
*) dict
);
368 while ( (key
= (OSSymbol
*) iter
->getNextObject()) )
370 medium
= OSDynamicCast(IONetworkMedium
, dict
->getObject(key
));
371 if (medium
== 0) continue;
373 if ( ( (medium
->getIndex() ^ index
) & ~mask
) == 0 )