3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2019 Nest Labs, Inc.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 * Provides a generic implementation of ThreadStackManager features
23 * for use on platforms that use OpenThread with LwIP.
28 #include <platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h>
30 #include <lwip/netif.h>
31 #include <lwip/tcpip.h>
33 #include <openthread/message.h>
36 namespace DeviceLayer {
38 class ThreadStackManagerImpl;
43 * Provides a generic implementation of ThreadStackManager features that works in conjunction
44 * with OpenThread and LwIP.
46 * This template contains implementations of select features from the ThreadStackManager abstract
47 * interface that are suitable for use on devices that employ OpenThread and LwIP together. It is
48 * intended to be inherited, directly or indirectly, by the ThreadStackManagerImpl class, which
49 * also appears as the template's ImplClass parameter.
51 template <class ImplClass>
52 class GenericThreadStackManagerImpl_OpenThread_LwIP : public GenericThreadStackManagerImpl_OpenThread<ImplClass>
55 // ===== Platform-specific methods directly callable by the application.
57 struct netif * ThreadNetIf() const;
60 // ===== Methods that implement the ThreadStackManager abstract interface.
62 void _OnPlatformEvent(const ChipDeviceEvent * event);
64 // ===== Members available to the implementation subclass.
66 CHIP_ERROR DoInit(otInstance * otInst);
67 void UpdateThreadInterface(bool addrChange);
70 // ===== Private members for use by this class only.
72 struct netif * mNetIf;
73 bool mAddrAssigned[LWIP_IPV6_NUM_ADDRESSES];
75 static err_t DoInitThreadNetIf(struct netif * netif);
76 #if LWIP_VERSION_MAJOR < 2
77 static err_t SendPacket(struct netif * netif, struct pbuf * pkt, struct ip6_addr * ipaddr);
79 static err_t SendPacket(struct netif * netif, struct pbuf * pkt, const struct ip6_addr * ipaddr);
81 static void ReceivePacket(otMessage * pkt, void * context);
83 inline ImplClass * Impl() { return static_cast<ImplClass *>(this); }
86 // Instruct the compiler to instantiate the template only when explicitly told to do so.
87 extern template class GenericThreadStackManagerImpl_OpenThread_LwIP<ThreadStackManagerImpl>;
89 template <class ImplClass>
90 inline struct netif * GenericThreadStackManagerImpl_OpenThread_LwIP<ImplClass>::ThreadNetIf() const
95 } // namespace Internal
96 } // namespace DeviceLayer