Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / OpenThread / OpenThreadUtils.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2019 Nest Labs, Inc.
5  *    All rights reserved.
6  *
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
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 /**
21  *    @file
22  *          Utility functions for working with OpenThread.
23  */
24
25 #pragma once
26
27 #include <string.h>
28
29 #include <openthread/error.h>
30 #include <openthread/ip6.h>
31 #include <openthread/thread.h>
32
33 #include <core/CHIPError.h>
34 #include <inet/IPAddress.h>
35 #include <inet/IPPrefix.h>
36
37 namespace chip {
38 namespace DeviceLayer {
39 namespace Internal {
40
41 /**
42  *  @def CHIP_CONFIG_OPENTHREAD_ERROR_MIN
43  *
44  *  @brief
45  *      The base value for OpenThread errors when mapped into the CHIP error space.
46  */
47 #ifndef CHIP_CONFIG_OPENTHREAD_ERROR_MIN
48 #define CHIP_CONFIG_OPENTHREAD_ERROR_MIN 9000000
49 #endif // CHIP_CONFIG_OPENTHREAD_ERROR_MIN
50
51 /**
52  *  @def CHIP_CONFIG_OPENTHREAD_ERROR_MAX
53  *
54  *  @brief
55  *      The max value for OpenThread errors when mapped into the CHIP error space.
56  */
57 #ifndef CHIP_CONFIG_OPENTHREAD_ERROR_MAX
58 #define CHIP_CONFIG_OPENTHREAD_ERROR_MAX 9000999
59 #endif // CHIP_CONFIG_OPENTHREAD_ERROR_MAX
60
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);
67
68 inline otIp6Address ToOpenThreadIP6Address(const Inet::IPAddress & addr)
69 {
70     otIp6Address otAddr;
71     memcpy(otAddr.mFields.m32, addr.Addr, sizeof(otAddr.mFields.m32));
72     return otAddr;
73 }
74
75 inline Inet::IPAddress ToIPAddress(const otIp6Address & otAddr)
76 {
77     Inet::IPAddress addr;
78     memcpy(addr.Addr, otAddr.mFields.m32, sizeof(addr.Addr));
79     return addr;
80 }
81
82 inline Inet::IPPrefix ToIPPrefix(const otIp6Prefix & otPrefix)
83 {
84     Inet::IPPrefix prefix;
85     prefix.IPAddr = ToIPAddress(otPrefix.mPrefix);
86     prefix.Length = otPrefix.mLength;
87     return prefix;
88 }
89
90 } // namespace Internal
91 } // namespace DeviceLayer
92 } // namespace chip