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: %lu", address.GetRemoteId());
59 0 : return CHIP_ERROR_INCORRECT_STATE;
60 : }
61 0 : mWiFiPAFLayer->SendMessage(*pTxInfo, std::move(msgBuf));
62 :
63 0 : return CHIP_NO_ERROR;
64 : }
65 :
66 0 : bool WiFiPAFBase::CanSendToPeer(const Transport::PeerAddress & address)
67 : {
68 0 : if (mWiFiPAFLayer != nullptr)
69 : {
70 0 : return (mWiFiPAFLayer->GetWiFiPAFState() != State::kNotReady) && (address.GetTransportType() == Type::kWiFiPAF);
71 : }
72 0 : return false;
73 : }
74 :
75 0 : CHIP_ERROR WiFiPAFBase::WiFiPAFMessageReceived(WiFiPAFSession & RxInfo, PacketBufferHandle && buffer)
76 : {
77 0 : auto pPafInfo = mWiFiPAFLayer->GetPAFInfo(chip::WiFiPAF::PafInfoAccess::kAccSessionId, RxInfo);
78 0 : if (pPafInfo == nullptr)
79 : {
80 : /*
81 : The session does not exist
82 : */
83 0 : ChipLogError(Inet, "WiFi-PAF: No valid session whose Id: %u", RxInfo.id);
84 0 : return CHIP_ERROR_INCORRECT_STATE;
85 : }
86 :
87 0 : if ((pPafInfo->id != RxInfo.id) || (pPafInfo->peer_id != RxInfo.peer_id) ||
88 0 : (memcmp(pPafInfo->peer_addr, RxInfo.peer_addr, sizeof(uint8_t) * 6) != 0))
89 : {
90 : /*
91 : The packet is from the wrong sender
92 : */
93 0 : ChipLogError(Inet, "WiFi-PAF: packet from unexpected node:");
94 0 : ChipLogError(Inet, "session: [id: %u, peer_id: %u, [%02x:%02x:%02x:%02x:%02x:%02x]", pPafInfo->id, pPafInfo->peer_id,
95 : pPafInfo->peer_addr[0], pPafInfo->peer_addr[1], pPafInfo->peer_addr[2], pPafInfo->peer_addr[3],
96 : pPafInfo->peer_addr[4], pPafInfo->peer_addr[5]);
97 0 : ChipLogError(Inet, "pkt: [id: %u, peer_id: %u, [%02x:%02x:%02x:%02x:%02x:%02x]", RxInfo.id, RxInfo.peer_id,
98 : RxInfo.peer_addr[0], RxInfo.peer_addr[1], RxInfo.peer_addr[2], RxInfo.peer_addr[3], RxInfo.peer_addr[4],
99 : RxInfo.peer_addr[5]);
100 0 : return CHIP_ERROR_INCORRECT_STATE;
101 : }
102 0 : HandleMessageReceived(Transport::PeerAddress(Transport::Type::kWiFiPAF, pPafInfo->nodeId), std::move(buffer));
103 0 : return CHIP_NO_ERROR;
104 : }
105 :
106 0 : CHIP_ERROR WiFiPAFBase::WiFiPAFMessageSend(WiFiPAFSession & TxInfo, PacketBufferHandle && msgBuf)
107 : {
108 0 : VerifyOrReturnError(mWiFiPAFLayer->GetWiFiPAFState() != State::kNotReady, CHIP_ERROR_INCORRECT_STATE);
109 0 : DeviceLayer::ConnectivityMgr().WiFiPAFSend(TxInfo, std::move(msgBuf));
110 :
111 0 : return CHIP_NO_ERROR;
112 : }
113 :
114 0 : CHIP_ERROR WiFiPAFBase::WiFiPAFCloseSession(WiFiPAFSession & SessionInfo)
115 : {
116 0 : VerifyOrReturnError(mWiFiPAFLayer->GetWiFiPAFState() != State::kNotReady, CHIP_ERROR_INCORRECT_STATE);
117 0 : DeviceLayer::ConnectivityMgr().WiFiPAFShutdown(SessionInfo.id, SessionInfo.role);
118 0 : mWiFiPAFLayer->SetWiFiPAFState(State::kInitialized);
119 :
120 0 : return CHIP_NO_ERROR;
121 : }
122 :
123 0 : bool WiFiPAFBase::WiFiPAFResourceAvailable(void)
124 : {
125 0 : return DeviceLayer::ConnectivityMgr().WiFiPAFResourceAvailable();
126 : }
127 :
128 0 : CHIP_ERROR WiFiPAFBase::SendAfterConnect(PacketBufferHandle && msg)
129 : {
130 0 : CHIP_ERROR err = CHIP_ERROR_NO_MEMORY;
131 :
132 0 : for (size_t i = 0; i < mPendingPacketsSize; i++)
133 : {
134 0 : if (mPendingPackets[i].IsNull())
135 : {
136 0 : mPendingPackets[i] = std::move(msg);
137 0 : err = CHIP_NO_ERROR;
138 0 : break;
139 : }
140 : }
141 :
142 0 : return err;
143 : }
144 :
145 : } // namespace Transport
146 : } // namespace chip
|