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 * Utility functions for working with OpenThread.
29 #include <openthread/error.h>
30 #include <openthread/ip6.h>
31 #include <openthread/thread.h>
33 #include <core/CHIPError.h>
34 #include <inet/IPAddress.h>
35 #include <inet/IPPrefix.h>
38 namespace DeviceLayer {
42 * @def CHIP_CONFIG_OPENTHREAD_ERROR_MIN
45 * The base value for OpenThread errors when mapped into the CHIP error space.
47 #ifndef CHIP_CONFIG_OPENTHREAD_ERROR_MIN
48 #define CHIP_CONFIG_OPENTHREAD_ERROR_MIN 9000000
49 #endif // CHIP_CONFIG_OPENTHREAD_ERROR_MIN
52 * @def CHIP_CONFIG_OPENTHREAD_ERROR_MAX
55 * The max value for OpenThread errors when mapped into the CHIP error space.
57 #ifndef CHIP_CONFIG_OPENTHREAD_ERROR_MAX
58 #define CHIP_CONFIG_OPENTHREAD_ERROR_MAX 9000999
59 #endif // CHIP_CONFIG_OPENTHREAD_ERROR_MAX
61 extern CHIP_ERROR MapOpenThreadError(otError otErr);
62 extern void RegisterOpenThreadErrorFormatter(void);
63 extern void LogOpenThreadStateChange(otInstance * otInst, uint32_t flags);
64 extern void LogOpenThreadPacket(const char * titleStr, otMessage * pkt);
65 extern bool IsOpenThreadMeshLocalAddress(otInstance * otInst, const Inet::IPAddress & addr);
66 extern const char * OpenThreadRoleToStr(otDeviceRole role);
68 inline otIp6Address ToOpenThreadIP6Address(const Inet::IPAddress & addr)
71 memcpy(otAddr.mFields.m32, addr.Addr, sizeof(otAddr.mFields.m32));
75 inline Inet::IPAddress ToIPAddress(const otIp6Address & otAddr)
78 memcpy(addr.Addr, otAddr.mFields.m32, sizeof(addr.Addr));
82 inline Inet::IPPrefix ToIPPrefix(const otIp6Prefix & otPrefix)
84 Inet::IPPrefix prefix;
85 prefix.IPAddr = ToIPAddress(otPrefix.mPrefix);
86 prefix.Length = otPrefix.mLength;
90 } // namespace Internal
91 } // namespace DeviceLayer