Class ochannel
Introduction
This class implements a Layer 2 Ethernet interface with special features that support Atheros powerline device management. A typical sequence of management events involves selecting the host interface, opening it, finding powerline devices available through it, exchanging messages with those devices then closing the interface.
Essentially, this class stores all information and includes most methods needed to perform Layer 2 Ethernet communications. The application must allocate, encode and decode the frame buffers that are sent and received by this class. The application must also derive powerline network topology from the Bridges and Neighbors methods which are included for convenience.
This class includes a template Ethernet header. Experience has shown that outgoing frame headers are basically static, except for the peer address. The host address and protocol do not change often and both these values are used to filter incoming frames. To minimize duplication of information, the values are all co-located here. This takes some getting-used-to but it makes logistical sense.
This class is declared in ochannel.hpp and defined in ochannel.cpp.
Inheritance
This class inherits the oflagword class to implement the channel flagword. All public flagword properties and methods are accessible.
This class inherits the oethernet class to implemment a template Ethernet header. All public Ethernet header properties and methods are public.
This class inherits the ointerface class to implement the channel Ethernet interface. All class properties and methods are private.
Dependence
This class references static class omemory to print outgoing and incoming frames.
This class references static class oerror to report errors.
Properties
Class properties include all oflagword, oethernet and ointerface properties plus those described below.
Timeout
unsigned Timeoutvoid
Return the channel timeout value.
Methods
Class methods include all oflagword, oethernet and ointerface methods plus those described below.
Bridges
signed Bridgesvoid * memorysize_t extent
Encode memory with a list of the hardware addresses of powerline adapters connected to the channel interface. Return the number of addresses encoded. Argument extent specifies the size of memory in bytes. The return value indicates the number of consecutive addresses actually stored in memory where each address is ETHER_ADDR_LEN bytes. This method will not return more addresses than memory can hold but will return -1 on overflow.
Neighbors
signed Neighborsvoid * memorysize_t extent
Encode memory with a list of the hardware addresses of powerline adapters associated with a specific adapter. Return the number of addresses encoded. The subject powerline adapter is determined by the PeerAddress property when this method is called. Argument extent specifies the size of memory in bytes. The return value indicates the number of consecutive addresses actually stored in memory where each address is ETHER_ADDR_LEN bytes. This method will not return more addresses than memory can hold but will return -1 on overflow.
ReadMessage
signed ReadMessagevoid const * memorysigned extent
Receive an Ethernet frame through the channel interface. Return the number of bytes received. Return -1 on error, 0 on timeout. On success, the return value will often be less than extent. Incoming frames are filtered. Only valid HomePlug AV frames that are addressed to the host will be returned by this method.
SendMessage
signed SendMessagevoid const * memorysigned extent
Transmit an Ethernet frame through the channel interface. Return the number of bytes transmitted. Return -1 on error, 0 on timeout. On success, the return value will equal extent. Outgoing frames are not filtered. Any valid Ethernet frame may be passed to this method.
SetTimeout
unsigned SetTimeoutunsigned timer
Replace the channel timer value.
On WinPcap systems, the timer value specifies the packet capture duration. The ReadMessage operation will not return until the capture period expires. This means that larger timer values may slow performace while smaller values may result in missed frames on busy networks.
On Linux, MacOS x and similar systems, the timer value specifies the maximum time to wait for an incoming frame. The ReadMessage operation will return immediately when a frame arrives. In this sense, the channel timer controls the timeout. This means that large timer values do not significantly affect performance.
Constructors
ochannelunsigned ifindex
Open a raw Ethernet socket, bind it to the numbered interface and activate packet filters. Terminate the application if the socket cannot be opened or the interface does not exist or cannot be accessed. Do not use this constructor unless you are sure that the interface exists and is available. The socket is closed and the interface is released by the class destructor.
ochannelconst char * ifname
Open a raw Ethernet socket, bind it to the named interface and activate packet filters. Terminate the application if the socket cannot be opended or the interface does not exist or cannot be accessed. Do not use this constructor unless you are sure that the interface exists. The socket is closed and the interface is released by the class destructor.
Examples
Enumerating Local Powerline Devices
ochannel channel ("eth2");
byte bridgelist [255][ETHER_ADDR_LEN];
signed bridges = channel.Bridges (bridgelist, sizeof (bridgelist));
signed bridge = 0;
while (bridge < bridges)
{
do something with bridgelist [bridge];
}
This example instantiates channel with interface name eth2. It then allocates bridgelist to allow a maximum of 255 possible hardware device addresses. Integer bridges is initialized to the number of available channel bridges using method Bridges. Integer bridge is the used to iterate through bridgelist.
Normally there will only be one powerline device connected to an interface but there could be more connected through a network switch or hub. Since each adapter is also connected to the powerline, each could be part of a different powerline network consisting of more, remote powerline devices. Consequently, we need to ask each bridge, in turn, to identify it's network neighbors.
Enumerating Remote Powerline Devices
ochannel channel (3);
byte bridgelist [255][ETHER_ADDR_LEN];
byte devicelist [255][ETHER_ADDR_LEN]
signed bridges = channel.Bridges (bridgelist, sizeof (bridgelist));
signed devices;
signed bridge = 0;
signed device = 0;
while (bridge < bridges)
{
channel.ImportPeerAddress (bridgelist [bridge])
devices = channel.Neighbors (devicelist, sizeof(devicelist));
while (device < devices)
{
do something with devicelist [device];
}
}
The example above instantiates channel with interface number 3, this time. It allocates bridgelist and devicelist to allow a maximum of 255 possible hardware device addresses each. Integer bridges is initialized and bridgelist is searched (as before).
Class method ImportPeerAddress is called to specify each bridge as the destination device before calling method Neighbors to find the neighboring powerline adapters.
The list of all channel bridges and their neighbors forms a complete list of powerline adapter accessible through the channel interface. Such a list can be used to populate a drop-down menu for GUI applications. The two operations of finding bridges and finding devices are kept seperate for reasons that may not be obvious at first.