Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / transport / NetworkProvisioning.h
1 /*
2  *
3  *    Copyright (c) 2020-2021 Project CHIP Authors
4  *
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17
18 /**
19  *    @file
20  *      This file defines the CHIP Device Network Provisioning object.
21  *
22  */
23
24 #pragma once
25
26 #include <core/CHIPCore.h>
27 #include <platform/internal/DeviceNetworkInfo.h>
28 #include <protocols/Protocols.h>
29 #include <support/BufferWriter.h>
30 #include <system/SystemPacketBuffer.h>
31 #include <transport/RendezvousSessionDelegate.h>
32
33 #if CONFIG_DEVICE_LAYER
34 #include <platform/CHIPDeviceLayer.h>
35 #endif
36
37 namespace chip {
38
39 class DLL_EXPORT NetworkProvisioningDelegate
40 {
41 public:
42     /**
43      * @brief
44      *   Called when network provisioning generates a new message that should be sent to peer.
45      *
46      * @param protocol Protocol ID for the message
47      * @param msgType Message type
48      * @param msgBuf the new message that should be sent to the peer
49      * @return CHIP_ERROR Error thrown when sending the message
50      */
51     virtual CHIP_ERROR SendSecureMessage(Protocols::Id protocol, uint8_t msgType, System::PacketBufferHandle msgBuf)
52     {
53         return CHIP_NO_ERROR;
54     }
55
56     /**
57      * @brief
58      *   Called when network provisioning fails with an error
59      *
60      * @param error error code
61      */
62     virtual void OnNetworkProvisioningError(CHIP_ERROR error) {}
63
64     /**
65      * @brief
66      *   Called when the network provisioning is complete
67      */
68     virtual void OnNetworkProvisioningComplete() {}
69
70     virtual ~NetworkProvisioningDelegate() {}
71 };
72
73 class DLL_EXPORT NetworkProvisioning
74 {
75 public:
76     enum MsgTypes : uint8_t
77     {
78         kWiFiAssociationRequest   = 0,
79         kIPAddressAssigned        = 1,
80         kThreadAssociationRequest = 2
81     };
82
83     void Init(NetworkProvisioningDelegate * delegate);
84
85     ~NetworkProvisioning();
86
87     CHIP_ERROR SendNetworkCredentials(const char * ssid, const char * passwd);
88     CHIP_ERROR SendThreadCredentials(const DeviceLayer::Internal::DeviceNetworkInfo & threadData);
89
90     CHIP_ERROR HandleNetworkProvisioningMessage(uint8_t msgType, const System::PacketBufferHandle & msgBuf);
91
92     /**
93      * @brief
94      *  Get the IP address assigned to the device during network provisioning
95      *  process.
96      *
97      * @return The IP address of the device
98      */
99     const Inet::IPAddress & GetIPAddress() const { return mDeviceAddress; }
100
101 private:
102     NetworkProvisioningDelegate * mDelegate = nullptr;
103
104     Inet::IPAddress mDeviceAddress = Inet::IPAddress::Any;
105
106     /**
107      * @brief
108      *  The device can use this function to send its IP address to
109      *  commissioner. This would generally be called during network
110      *  provisioning of the device, after the IP address assignment.
111      *
112      * @param addr The IP address of the device
113      */
114     CHIP_ERROR SendIPAddress(const Inet::IPAddress & addr);
115
116     /**
117      * @brief
118      *  The device can use this function to send its current IP address to
119      *  commissioner. This would generally be called during network
120      *  provisioning of the device, when the device already has an IP address.
121      */
122     CHIP_ERROR SendCurrentIPv4Address();
123
124     static size_t EncodedStringSize(const char * str);
125     static CHIP_ERROR EncodeString(const char * str, Encoding::LittleEndian::BufferWriter & bbuf);
126     static CHIP_ERROR DecodeString(const uint8_t * input, size_t input_len, Encoding::LittleEndian::BufferWriter & bbuf,
127                                    size_t & consumed);
128
129     CHIP_ERROR DecodeThreadAssociationRequest(const System::PacketBufferHandle & msgBuf);
130
131 #if CONFIG_DEVICE_LAYER
132     static void ConnectivityHandler(const DeviceLayer::ChipDeviceEvent * event, intptr_t arg);
133 #endif // CONFIG_DEVICE_LAYER
134 };
135
136 } // namespace chip