0.01.00
netif.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, The OpenThread Authors.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the copyright holder nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
34 #ifndef NET_NETIF_HPP_
35 #define NET_NETIF_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include "common/locator.hpp"
40 #include "common/message.hpp"
41 #include "common/tasklet.hpp"
42 #include "mac/mac_frame.hpp"
43 #include "net/ip6_address.hpp"
44 #include "net/socket.hpp"
45 
46 namespace ot {
47 namespace Ip6 {
48 
49 class Ip6;
50 
66 {
67 public :
73  {
74  kEui64 = 27,
75  };
77  uint8_t mLength;
79 };
80 
86 {
87  friend class Netif;
88 
89 public:
96  const Address &GetAddress(void) const { return *static_cast<const Address *>(&mAddress); }
97 
104  Address &GetAddress(void) { return *static_cast<Address *>(&mAddress); }
105 
112  uint8_t GetScope(void) const {
113  return mScopeOverrideValid ? static_cast<uint8_t>(mScopeOverride) : GetAddress().GetScope();
114  }
115 
122  const NetifUnicastAddress *GetNext(void) const { return static_cast<const NetifUnicastAddress *>(mNext); }
123 
130  NetifUnicastAddress *GetNext(void) { return static_cast<NetifUnicastAddress *>(mNext); }
131 };
132 
138 {
139  friend class Netif;
140 
141 public:
148  const Address &GetAddress(void) const { return *static_cast<const Address *>(&mAddress); }
149 
156  Address &GetAddress(void) { return *static_cast<Address *>(&mAddress); }
157 
164  const NetifMulticastAddress *GetNext(void) const { return static_cast<const NetifMulticastAddress *>(mNext); }
165 
173  return static_cast<NetifMulticastAddress *>(const_cast<otNetifMulticastAddress *>(mNext));
174  }
175 };
176 
182 {
183  friend class Netif;
184 
185 public:
191  mCallback(NULL),
192  mContext(NULL),
193  mNext(NULL) {
194  }
195 
203  void Set(otStateChangedCallback aCallback, void *aContext) {
204  mCallback = aCallback;
205  mContext = aContext;
206  }
207 
214  bool IsFree(void) { return (mCallback == NULL); }
215 
220  void Free(void) {
221  mCallback = NULL;
222  mContext = NULL;
223  mNext = NULL;
224  }
225 
235  bool IsServing(otStateChangedCallback aCallback, void *aContext) {
236  return (aCallback == mCallback && aContext == mContext);
237  }
238 
239 private:
240  void Callback(uint32_t aFlags) {
241  if (mCallback != NULL) {
242  mCallback(aFlags, mContext);
243  }
244  }
245 
246  otStateChangedCallback mCallback;
247  void *mContext;
248  NetifCallback *mNext;
249 };
250 
255 class Netif: public InstanceLocator
256 {
257  friend class Ip6;
258 
259 public:
267  Netif(otInstance &aInstance, int8_t aInterfaceId);
268 
274  Netif *GetNext(void) const { return mNext; }
275 
282  int8_t GetInterfaceId(void) const { return mInterfaceId; }
283 
290  const NetifUnicastAddress *GetUnicastAddresses(void) const { return mUnicastAddresses; }
291 
301  otError AddUnicastAddress(NetifUnicastAddress &aAddress);
302 
312  otError RemoveUnicastAddress(const NetifUnicastAddress &aAddress);
313 
324  otError AddExternalUnicastAddress(const NetifUnicastAddress &aAddress);
325 
336  otError RemoveExternalUnicastAddress(const Address &aAddress);
337 
343  void RemoveAllExternalUnicastAddresses(void);
344 
353  bool IsUnicastAddress(const Address &aAddress) const;
354 
364  bool IsMulticastSubscribed(const Address &aAddress) const;
365 
373  otError SubscribeAllRoutersMulticast(void);
374 
382  otError UnsubscribeAllRoutersMulticast(void);
383 
390  const NetifMulticastAddress *GetMulticastAddresses(void) const { return mMulticastAddresses; }
391 
401  otError SubscribeMulticast(NetifMulticastAddress &aAddress);
402 
412  otError UnsubscribeMulticast(const NetifMulticastAddress &aAddress);
413 
425  otError SubscribeExternalMulticast(const Address &aAddress);
426 
437  otError UnsubscribeExternalMulticast(const Address &aAddress);
438 
443  void UnsubscribeAllExternalMulticastAddresses(void);
444 
451  bool IsMulticastPromiscuousEnabled(void) { return mMulticastPromiscuous; }
452 
459  void SetMulticastPromiscuous(bool aEnabled) { mMulticastPromiscuous = aEnabled; }
460 
469  otError RegisterCallback(NetifCallback &aCallback);
470 
479  otError RemoveCallback(NetifCallback &aCallback);
480 
487  bool IsStateChangedCallbackPending(void) { return mStateChangedFlags != 0; }
488 
497  void SetStateChangedFlags(uint32_t aFlags);
498 
507  virtual otError SendMessage(Message &aMessage) = 0;
508 
517  virtual otError GetLinkAddress(LinkAddress &aAddress) const = 0;
518 
530  virtual otError RouteLookup(const Address &aSource, const Address &aDestination,
531  uint8_t *aPrefixMatch) = 0;
532 
533 private:
534  static void HandleStateChangedTask(Tasklet &aTasklet);
535  void HandleStateChangedTask(void);
536  static Netif &GetOwner(const Context &aContext);
537 
538  NetifCallback *mCallbacks;
539  NetifUnicastAddress *mUnicastAddresses;
540  NetifMulticastAddress *mMulticastAddresses;
541  int8_t mInterfaceId;
542  bool mMulticastPromiscuous;
543  Tasklet mStateChangedTask;
544  Netif *mNext;
545 
546  uint32_t mStateChangedFlags;
547 
550 
551  static const otNetifMulticastAddress kRealmLocalAllMplForwardersMulticastAddress;
552  static const otNetifMulticastAddress kLinkLocalAllNodesMulticastAddress;
553  static const otNetifMulticastAddress kRealmLocalAllNodesMulticastAddress;
554  static const otNetifMulticastAddress kLinkLocalAllRoutersMulticastAddress;
555  static const otNetifMulticastAddress kRealmLocalAllRoutersMulticastAddress;
556 };
557 
563 } // namespace Ip6
564 } // namespace ot
565 
566 #endif // NET_NETIF_HPP_
NetifUnicastAddress * GetNext(void)
This method returns the next unicast address assigned to the interface.
Definition: netif.hpp:130
bool IsServing(otStateChangedCallback aCallback, void *aContext)
This method tests whether the object is set to the provided elements.
Definition: netif.hpp:235
bool IsFree(void)
This method tests whether the object is free or in use.
Definition: netif.hpp:214
This type represents all the static / global variables used by OpenThread allocated in one place...
Definition: openthread-instance.h:59
void Free(void)
This method frees the object.
Definition: netif.hpp:220
Definition: cli.cpp:90
const NetifUnicastAddress * GetNext(void) const
This method returns the next unicast address assigned to the interface.
Definition: netif.hpp:122
const NetifUnicastAddress * GetUnicastAddresses(void) const
This method returns a pointer to the list of unicast addresses.
Definition: netif.hpp:290
This file includes definitions for IPv6 addresses.
This class implements an IPv6 address object.
Definition: ip6_address.hpp:60
uint8_t mLength
Length of link address.
Definition: netif.hpp:77
This file includes definitions for locator class for OpenThread objects.
Address & GetAddress(void)
This method returns the multicast address.
Definition: netif.hpp:156
This class implements an IPv6 network interface multicast address.
Definition: netif.hpp:137
Address & GetAddress(void)
This method returns the unicast address.
Definition: netif.hpp:104
This class represents an IPv6 Link Address.
Definition: netif.hpp:65
#define OPENTHREAD_CONFIG_MAX_EXT_IP_ADDRS
The maximum number of supported IPv6 addresses allows to be externally added.
Definition: openthread-core-default-config.h:271
This class is used to represent a tasklet.
Definition: tasklet.hpp:64
void Set(otStateChangedCallback aCallback, void *aContext)
This method sets the callback information.
Definition: netif.hpp:203
This class implements the core IPv6 message processing.
Definition: ip6.hpp:101
const Address & GetAddress(void) const
This method returns the unicast address.
Definition: netif.hpp:96
This structure represents an IPv6 network interface unicast address.
Definition: types.h:1089
This structure represents an IEEE 802.15.4 Extended Address.
Definition: mac_frame.hpp:84
const Address & GetAddress(void) const
This method returns the multicast address.
Definition: netif.hpp:148
This file includes definitions for generating and processing IEEE 802.15.4 MAC frames.
NetifMulticastAddress * GetNext(void)
This method returns the next multicast address subscribed to the interface.
Definition: netif.hpp:172
This class implements an IPv6 network interface unicast address.
Definition: netif.hpp:85
This class implements definitions for maintaining a pointer to arbitrary context information.
Definition: context.hpp:61
This class represents a message.
Definition: message.hpp:195
This file includes definitions for tasklets and the tasklet scheduler.
HardwareType mType
Link address type.
Definition: netif.hpp:76
bool IsMulticastPromiscuousEnabled(void)
This method checks if multicast promiscuous mode is enabled on the network interface.
Definition: netif.hpp:451
HardwareType
Hardware types.
Definition: netif.hpp:72
This file includes definitions for IPv6 sockets.
This file includes definitions for the message buffer pool and message buffers.
This class implements locator for otInstance object.
Definition: locator.hpp:63
#define OPENTHREAD_CONFIG_MAX_EXT_MULTICAST_IP_ADDRS
The maximum number of supported IPv6 multicast addresses allows to be externally added.
Definition: openthread-core-default-config.h:281
Netif * GetNext(void) const
This method returns the next network interface in the list.
Definition: netif.hpp:274
bool IsStateChangedCallbackPending(void)
This method indicates whether or not a state changed callback is pending.
Definition: netif.hpp:487
void SetMulticastPromiscuous(bool aEnabled)
This method enables multicast promiscuous mode on the network interface.
Definition: netif.hpp:459
void(OTCALL * otStateChangedCallback)(uint32_t aFlags, void *aContext)
This function pointer is called to notify certain configuration or state changes within OpenThread...
Definition: instance.h:230
uint8_t GetScope(void) const
This method returns the IPv6 scope value.
Definition: netif.hpp:112
Mac::ExtAddress mExtAddress
Link address.
Definition: netif.hpp:78
NetifCallback(void)
This constructor initializes the object.
Definition: netif.hpp:190
const NetifMulticastAddress * GetMulticastAddresses(void) const
This method returns a pointer to the list of multicast addresses.
Definition: netif.hpp:390
This class implements network interface handlers.
Definition: netif.hpp:181
const NetifMulticastAddress * GetNext(void) const
This method returns the next multicast address subscribed to the interface.
Definition: netif.hpp:164
This class implements an IPv6 network interface.
Definition: netif.hpp:255
This structure represents an IPv6 network interface multicast address.
Definition: types.h:1105
otError
This enumeration represents error codes used throughout OpenThread.
Definition: types.h:107
This file includes compile-time configuration constants for OpenThread.
int8_t GetInterfaceId(void) const
This method returns the network interface identifier.
Definition: netif.hpp:282