Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2025 Project CHIP Authors
4 : * All rights reserved.
5 : *
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 :
19 : /**
20 : * @file
21 : * This file implements the Matter Connection object that maintains a Wi-Fi PAF connection
22 : *
23 : */
24 :
25 : #include <inttypes.h>
26 : #include <lib/support/CodeUtils.h>
27 : #include <lib/support/logging/CHIPLogging.h>
28 : #include <platform/ConnectivityManager.h>
29 : #include <transport/raw/MessageHeader.h>
30 : #include <transport/raw/WiFiPAF.h>
31 :
32 : using namespace chip::System;
33 : using namespace chip::WiFiPAF;
34 :
35 : namespace chip {
36 : namespace Transport {
37 1 : CHIP_ERROR WiFiPAFBase::Init(const WiFiPAFListenParameters & param)
38 : {
39 1 : ChipLogDetail(Inet, "WiFiPAFBase::Init - setting/overriding transport");
40 1 : mWiFiPAFLayer = DeviceLayer::ConnectivityMgr().GetWiFiPAF();
41 1 : SetWiFiPAFLayerTransportToSelf();
42 1 : mWiFiPAFLayer->SetWiFiPAFState(State::kInitialized);
43 1 : return CHIP_NO_ERROR;
44 : }
45 :
46 0 : CHIP_ERROR WiFiPAFBase::SendMessage(const Transport::PeerAddress & address, PacketBufferHandle && msgBuf)
47 : {
48 0 : VerifyOrReturnError(address.GetTransportType() == Type::kWiFiPAF, CHIP_ERROR_INVALID_ARGUMENT);
49 0 : VerifyOrReturnError(mWiFiPAFLayer->GetWiFiPAFState() != State::kNotReady, CHIP_ERROR_INCORRECT_STATE);
50 :
51 0 : WiFiPAFSession sessionInfo = { .nodeId = address.GetRemoteId() };
52 0 : WiFiPAFSession * pTxInfo = mWiFiPAFLayer->GetPAFInfo(chip::WiFiPAF::PafInfoAccess::kAccNodeId, sessionInfo);
53 0 : if (pTxInfo == nullptr)
54 : {
55 : /*
56 : The session does not exist
57 : */
58 0 : ChipLogError(Inet, "WiFi-PAF: No valid session whose nodeId: %" PRIu64, address.GetRemoteId());
59 0 : return CHIP_ERROR_INCORRECT_STATE;
60 : }
61 0 : return mWiFiPAFLayer->SendMessage(*pTxInfo, std::move(msgBuf));
62 : }
63 :
64 0 : bool WiFiPAFBase::CanSendToPeer(const Transport::PeerAddress & address)
65 : {
66 0 : if (mWiFiPAFLayer != nullptr)
67 : {
68 0 : return (mWiFiPAFLayer->GetWiFiPAFState() != State::kNotReady) && (address.GetTransportType() == Type::kWiFiPAF);
69 : }
70 0 : return false;
71 : }
72 :
73 0 : CHIP_ERROR WiFiPAFBase::WiFiPAFMessageReceived(WiFiPAFSession & RxInfo, PacketBufferHandle && buffer)
74 : {
75 0 : auto pPafInfo = mWiFiPAFLayer->GetPAFInfo(chip::WiFiPAF::PafInfoAccess::kAccSessionId, RxInfo);
76 0 : if (pPafInfo == nullptr)
77 : {
78 : /*
79 : The session does not exist
80 : */
81 0 : ChipLogError(Inet, "WiFi-PAF: No valid session whose Id: %u", RxInfo.id);
82 0 : return CHIP_ERROR_INCORRECT_STATE;
83 : }
84 :
85 0 : if ((pPafInfo->id != RxInfo.id) || (pPafInfo->peer_id != RxInfo.peer_id) ||
86 0 : (memcmp(pPafInfo->peer_addr, RxInfo.peer_addr, sizeof(uint8_t) * 6) != 0))
87 : {
88 : /*
89 : The packet is from the wrong sender
90 : */
91 0 : ChipLogError(Inet, "WiFi-PAF: packet from unexpected node:");
92 0 : ChipLogError(Inet, "session: [id: %u, peer_id: %u, [%02x:%02x:%02x:%02x:%02x:%02x]", pPafInfo->id, pPafInfo->peer_id,
93 : pPafInfo->peer_addr[0], pPafInfo->peer_addr[1], pPafInfo->peer_addr[2], pPafInfo->peer_addr[3],
94 : pPafInfo->peer_addr[4], pPafInfo->peer_addr[5]);
95 0 : ChipLogError(Inet, "pkt: [id: %u, peer_id: %u, [%02x:%02x:%02x:%02x:%02x:%02x]", RxInfo.id, RxInfo.peer_id,
96 : RxInfo.peer_addr[0], RxInfo.peer_addr[1], RxInfo.peer_addr[2], RxInfo.peer_addr[3], RxInfo.peer_addr[4],
97 : RxInfo.peer_addr[5]);
98 0 : return CHIP_ERROR_INCORRECT_STATE;
99 : }
100 0 : HandleMessageReceived(Transport::PeerAddress(Transport::Type::kWiFiPAF, pPafInfo->nodeId), std::move(buffer));
101 0 : return CHIP_NO_ERROR;
102 : }
103 :
104 0 : CHIP_ERROR WiFiPAFBase::WiFiPAFMessageSend(WiFiPAFSession & TxInfo, PacketBufferHandle && msgBuf)
105 : {
106 0 : VerifyOrReturnError(mWiFiPAFLayer->GetWiFiPAFState() != State::kNotReady, CHIP_ERROR_INCORRECT_STATE);
107 0 : return DeviceLayer::ConnectivityMgr().WiFiPAFSend(TxInfo, std::move(msgBuf));
108 : }
109 :
110 0 : CHIP_ERROR WiFiPAFBase::WiFiPAFCloseSession(WiFiPAFSession & SessionInfo)
111 : {
112 0 : VerifyOrReturnError(mWiFiPAFLayer->GetWiFiPAFState() != State::kNotReady, CHIP_ERROR_INCORRECT_STATE);
113 0 : TEMPORARY_RETURN_IGNORED DeviceLayer::ConnectivityMgr().WiFiPAFShutdown(SessionInfo.id, SessionInfo.role);
114 0 : mWiFiPAFLayer->SetWiFiPAFState(State::kInitialized);
115 :
116 0 : return CHIP_NO_ERROR;
117 : }
118 :
119 0 : bool WiFiPAFBase::WiFiPAFResourceAvailable(void)
120 : {
121 0 : return DeviceLayer::ConnectivityMgr().WiFiPAFResourceAvailable();
122 : }
123 :
124 0 : CHIP_ERROR WiFiPAFBase::SendAfterConnect(PacketBufferHandle && msg)
125 : {
126 0 : CHIP_ERROR err = CHIP_ERROR_NO_MEMORY;
127 :
128 0 : for (size_t i = 0; i < mPendingPacketsSize; i++)
129 : {
130 0 : if (mPendingPackets[i].IsNull())
131 : {
132 0 : mPendingPackets[i] = std::move(msg);
133 0 : err = CHIP_NO_ERROR;
134 0 : break;
135 : }
136 : }
137 :
138 0 : return err;
139 : }
140 :
141 : } // namespace Transport
142 : } // namespace chip
|