3 * Copyright (c) 2020-2021 Project CHIP Authors
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * This file defines the CHIP Device Network Provisioning object.
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>
33 #if CONFIG_DEVICE_LAYER
34 #include <platform/CHIPDeviceLayer.h>
39 class DLL_EXPORT NetworkProvisioningDelegate
44 * Called when network provisioning generates a new message that should be sent to peer.
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
51 virtual CHIP_ERROR SendSecureMessage(Protocols::Id protocol, uint8_t msgType, System::PacketBufferHandle msgBuf)
58 * Called when network provisioning fails with an error
60 * @param error error code
62 virtual void OnNetworkProvisioningError(CHIP_ERROR error) {}
66 * Called when the network provisioning is complete
68 virtual void OnNetworkProvisioningComplete() {}
70 virtual ~NetworkProvisioningDelegate() {}
73 class DLL_EXPORT NetworkProvisioning
76 enum MsgTypes : uint8_t
78 kWiFiAssociationRequest = 0,
79 kIPAddressAssigned = 1,
80 kThreadAssociationRequest = 2
83 void Init(NetworkProvisioningDelegate * delegate);
85 ~NetworkProvisioning();
87 CHIP_ERROR SendNetworkCredentials(const char * ssid, const char * passwd);
88 CHIP_ERROR SendThreadCredentials(const DeviceLayer::Internal::DeviceNetworkInfo & threadData);
90 CHIP_ERROR HandleNetworkProvisioningMessage(uint8_t msgType, const System::PacketBufferHandle & msgBuf);
94 * Get the IP address assigned to the device during network provisioning
97 * @return The IP address of the device
99 const Inet::IPAddress & GetIPAddress() const { return mDeviceAddress; }
102 NetworkProvisioningDelegate * mDelegate = nullptr;
104 Inet::IPAddress mDeviceAddress = Inet::IPAddress::Any;
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.
112 * @param addr The IP address of the device
114 CHIP_ERROR SendIPAddress(const Inet::IPAddress & addr);
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.
122 CHIP_ERROR SendCurrentIPv4Address();
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,
129 CHIP_ERROR DecodeThreadAssociationRequest(const System::PacketBufferHandle & msgBuf);
131 #if CONFIG_DEVICE_LAYER
132 static void ConnectivityHandler(const DeviceLayer::ChipDeviceEvent * event, intptr_t arg);
133 #endif // CONFIG_DEVICE_LAYER