gPTP Documentation
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
avbts_osnet.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 
3  Copyright (c) 2009-2012, Intel Corporation
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8 
9  1. Redistributions of source code must retain the above copyright notice,
10  this list of conditions and the following disclaimer.
11 
12  2. Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15 
16  3. Neither the name of the Intel Corporation nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  POSSIBILITY OF SUCH DAMAGE.
31 
32 ******************************************************************************/
33 
34 #ifndef AVBTS_OSNET_HPP
35 #define AVBTS_OSNET_HPP
36 
37 #include <stdint.h>
38 #include <string.h>
39 #include <map>
40 #include <ieee1588.hpp>
41 #include <ptptypes.hpp>
42 
45 #define FACTORY_NAME_LENGTH 48
46 #define DEFAULT_TIMEOUT 1
52 class LinkLayerAddress:public InterfaceLabel {
53  private:
55  uint8_t addr[ETHER_ADDR_OCTETS];
56  public:
61  };
62 
69  LinkLayerAddress(uint64_t address_scalar) {
70  uint8_t *ptr;
71  address_scalar <<= 16;
72  for (ptr = addr; ptr < addr + ETHER_ADDR_OCTETS; ++ptr) {
73  *ptr = (address_scalar & 0xFF00000000000000ULL) >> 56;
74  address_scalar <<= 8;
75  }
76  }
77 
84  LinkLayerAddress(uint8_t * address_octet_array) {
85  uint8_t *ptr;
86  for (ptr = addr; ptr < addr + ETHER_ADDR_OCTETS;
87  ++ptr, ++address_octet_array)
88  {
89  *ptr = *address_octet_array;
90  }
91  }
92 
100  bool operator==(const LinkLayerAddress & cmp) const {
101  return memcmp
102  (this->addr, cmp.addr, ETHER_ADDR_OCTETS) == 0 ? true : false;
103  }
104 
112  bool operator<(const LinkLayerAddress & cmp)const {
113  return memcmp
114  (this->addr, cmp.addr, ETHER_ADDR_OCTETS) < 0 ? true : false;
115  }
116 
124  bool operator>(const LinkLayerAddress & cmp)const {
125  return memcmp
126  (this->addr, cmp.addr, ETHER_ADDR_OCTETS) < 0 ? true : false;
127  }
128 
137  void toOctetArray(uint8_t * address_octet_array) {
138  uint8_t *ptr;
139  for (ptr = addr; ptr < addr + ETHER_ADDR_OCTETS;
140  ++ptr, ++address_octet_array)
141  {
142  *address_octet_array = *ptr;
143  }
144  }
145 };
146 
153  private:
155  char *name;
156  public:
166  InterfaceName(char *name, int length) {
167  this->name = new char[length + 1];
168  PLAT_strncpy(this->name, name, length);
169  }
170 
177  bool operator==(const InterfaceName & cmp) const {
178  return strcmp(name, cmp.name) == 0 ? true : false;
179  }
180 
187  bool operator<(const InterfaceName & cmp)const {
188  return strcmp(name, cmp.name) < 0 ? true : false;
189  }
190 
197  bool operator>(const InterfaceName & cmp)const {
198  return strcmp(name, cmp.name) < 0 ? true : false;
199  }
200 
208  bool toString(char *string, size_t length) {
209  if (length >= strlen(name) + 1) {
210  PLAT_strncpy(string, name, length);
211  return true;
212  }
213  return false;
214  }
215 };
216 
222  private:
223  /*<! Factory name*/
224  char name[FACTORY_NAME_LENGTH];
225  factory_name_t();
226  public:
231  factory_name_t(const char *name_a) {
232  PLAT_strncpy(name, name_a, FACTORY_NAME_LENGTH - 1);
233  }
234 
241  bool operator==(const factory_name_t & cmp) {
242  return strcmp(cmp.name, this->name) == 0 ? true : false;
243  }
244 
251  bool operator<(const factory_name_t & cmp)const {
252  return strcmp(cmp.name, this->name) < 0 ? true : false;
253  }
254 
261  bool operator>(const factory_name_t & cmp)const {
262  return strcmp(cmp.name, this->name) > 0 ? true : false;
263  }
264 };
265 
272 typedef enum { net_trfail, net_fatal, net_succeed } net_result;
273 
278  public:
287  virtual net_result send
288  (LinkLayerAddress * addr, uint8_t * payload, size_t length,
289  bool timestamp) = 0;
290 
298  virtual net_result nrecv
299  (LinkLayerAddress * addr, uint8_t * payload, size_t & length) = 0;
300 
306  virtual void getLinkLayerAddress(LinkLayerAddress * addr) = 0;
307 
311  virtual unsigned getPayloadOffset() = 0;
315  virtual ~OSNetworkInterface() = 0;
316 };
317 
319 
321 
325 typedef std::map < factory_name_t, OSNetworkInterfaceFactory * >FactoryMap_t;
326 
331  public:
338  static bool registerFactory
340  FactoryMap_t::iterator iter = factoryMap.find(id);
341  if (iter != factoryMap.end())
342  return false;
343  factoryMap[id] = factory;
344  return true;
345  }
346 
355  static bool buildInterface
357  HWTimestamper * timestamper) {
358  return factoryMap[id]->createInterface
359  (iface, iflabel, timestamper);
360  }
361  virtual ~OSNetworkInterfaceFactory() = 0;
362 private:
363  virtual bool createInterface
364  (OSNetworkInterface ** iface, InterfaceLabel * iflabel,
365  HWTimestamper * timestamper) = 0;
366  static FactoryMap_t factoryMap;
367 };
368 
369 inline OSNetworkInterfaceFactory::~OSNetworkInterfaceFactory() { }
370 
371 #endif
Definition: avbts_osnet.hpp:330
Definition: ieee1588.hpp:101
Definition: ieee1588.hpp:445
bool operator==(const factory_name_t &cmp)
Operator '==' overloading method Compares cmp to the factory name.
Definition: avbts_osnet.hpp:241
virtual void getLinkLayerAddress(LinkLayerAddress *addr)=0
Get Link Layer address (mac address)
InterfaceName()
Definition: avbts_osnet.hpp:160
bool toString(char *string, size_t length)
Gets interface name from the class' internal variable.
Definition: avbts_osnet.hpp:208
bool operator==(const LinkLayerAddress &cmp) const
Operator '==' overloading method. It provides a comparison between cmp and the class ethernet address...
Definition: avbts_osnet.hpp:100
void toOctetArray(uint8_t *address_octet_array)
Gets first 6 bytes from ethernet address of object LinkLayerAddress.
Definition: avbts_osnet.hpp:137
std::map< factory_name_t, OSNetworkInterfaceFactory * > FactoryMap_t
Definition: avbts_osnet.hpp:320
#define ETHER_ADDR_OCTETS
Definition: ptptypes.hpp:41
LinkLayerAddress()
Definition: avbts_osnet.hpp:60
factory_name_t(const char *name_a)
Definition: avbts_osnet.hpp:231
bool operator<(const InterfaceName &cmp) const
Operator '<' overloading method. Compares cmp to the interface name.
Definition: avbts_osnet.hpp:187
static bool buildInterface(OSNetworkInterface **iface, factory_name_t id, InterfaceLabel *iflabel, HWTimestamper *timestamper)
Builds the network interface.
Definition: avbts_osnet.hpp:356
Definition: avbts_osnet.hpp:52
LinkLayerAddress(uint64_t address_scalar)
Definition: avbts_osnet.hpp:69
bool operator>(const LinkLayerAddress &cmp) const
Operator '>' overloading method. It provides a comparison between cmp and the class ethernet addre...
Definition: avbts_osnet.hpp:124
bool operator<(const factory_name_t &cmp) const
Operator '<' overloading method Compares cmp to the factory name.
Definition: avbts_osnet.hpp:251
#define FACTORY_NAME_LENGTH
Definition: avbts_osnet.hpp:45
Definition: avbts_osnet.hpp:277
errno_t PLAT_strncpy(char *dest, const char *src, rsize_t max)
virtual net_result nrecv(LinkLayerAddress *addr, uint8_t *payload, size_t &length)=0
Receives data.
static bool registerFactory(factory_name_t id, OSNetworkInterfaceFactory *factory)
Registers network factory.
Definition: avbts_osnet.hpp:339
virtual ~OSNetworkInterface()=0
Definition: avbts_osnet.hpp:318
bool operator>(const InterfaceName &cmp) const
Operator '>' overloading method. Compares cmp to the interface name.
Definition: avbts_osnet.hpp:197
LinkLayerAddress(uint8_t *address_octet_array)
Definition: avbts_osnet.hpp:84
InterfaceName(char *name, int length)
Definition: avbts_osnet.hpp:166
net_result
Definition: avbts_osnet.hpp:272
bool operator==(const InterfaceName &cmp) const
Operator '==' overloading method. Compares parameter cmp to the interface name.
Definition: avbts_osnet.hpp:177
Definition: avbts_osnet.hpp:221
bool operator>(const factory_name_t &cmp) const
Operator '>' overloading method Compares cmp to the factory name.
Definition: avbts_osnet.hpp:261
virtual net_result send(LinkLayerAddress *addr, uint8_t *payload, size_t length, bool timestamp)=0
Sends a packet to a remote address.
Definition: avbts_osnet.hpp:152
virtual unsigned getPayloadOffset()=0
Provides generic method for getting the payload offset.
bool operator<(const LinkLayerAddress &cmp) const
Operator '<' overloading method. It provides a comparison between cmp and the class ethernet addre...
Definition: avbts_osnet.hpp:112