1 /* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include "wifi_p2p_service_request_list.h" 16 #include "wifi_logger.h" 17 18 DEFINE_WIFILOG_P2P_LABEL("WifiP2pServiceRequestList"); 19 20 namespace OHOS { 21 namespace Wifi { WifiP2pServiceRequestList()22WifiP2pServiceRequestList::WifiP2pServiceRequestList() : updateIndic(0), frequency(0), dialogToken(-1), p2pDevice(), srvReqList() 23 {} AddServiceRequest(const WifiP2pServiceRequest & req)24bool WifiP2pServiceRequestList::AddServiceRequest(const WifiP2pServiceRequest &req) 25 { 26 for (auto it : srvReqList) { 27 if (it == req) { 28 return false; 29 } 30 } 31 srvReqList.push_back(req); 32 return true; 33 } RemoveServiceRequest(const WifiP2pServiceRequest & req)34bool WifiP2pServiceRequestList::RemoveServiceRequest(const WifiP2pServiceRequest &req) 35 { 36 for (auto it = srvReqList.begin(); it != srvReqList.end(); it++) { 37 if (*it == req) { 38 srvReqList.erase(it); 39 return true; 40 } 41 } 42 return false; 43 } ClearServiceRequest()44bool WifiP2pServiceRequestList::ClearServiceRequest() 45 { 46 srvReqList.clear(); 47 return true; 48 } GetServiceRequestList() const49const std::vector<WifiP2pServiceRequest> &WifiP2pServiceRequestList::GetServiceRequestList() const 50 { 51 return srvReqList; 52 } 53 SetUpdateIndic(unsigned short setUpdateIndic)54void WifiP2pServiceRequestList::SetUpdateIndic(unsigned short setUpdateIndic) 55 { 56 updateIndic = setUpdateIndic; 57 } GetUpdateIndic() const58unsigned short WifiP2pServiceRequestList::GetUpdateIndic() const 59 { 60 return updateIndic; 61 } SetFrequency(int setFrequency)62void WifiP2pServiceRequestList::SetFrequency(int setFrequency) 63 { 64 frequency = setFrequency; 65 } GetFrequency() const66int WifiP2pServiceRequestList::GetFrequency() const 67 { 68 return frequency; 69 } SetDialogToken(int setDialogToken)70void WifiP2pServiceRequestList::SetDialogToken(int setDialogToken) 71 { 72 dialogToken = setDialogToken; 73 } GetDialogToken() const74int WifiP2pServiceRequestList::GetDialogToken() const 75 { 76 return dialogToken; 77 } GetTlvs() const78std::vector<unsigned char> WifiP2pServiceRequestList::GetTlvs() const 79 { 80 std::vector<unsigned char> ret; 81 82 for (auto tlvsIter = srvReqList.begin(); tlvsIter != srvReqList.end(); ++tlvsIter) { 83 std::vector<unsigned char> buf = tlvsIter->GetTlv(); 84 for (auto dataIter = buf.begin(); dataIter != buf.end(); ++dataIter) { 85 ret.push_back(*dataIter); 86 } 87 } 88 89 return ret; 90 } SetDevice(const WifiP2pDevice & device)91void WifiP2pServiceRequestList::SetDevice(const WifiP2pDevice &device) 92 { 93 p2pDevice = device; 94 } 95 GetDevice() const96const WifiP2pDevice &WifiP2pServiceRequestList::GetDevice() const 97 { 98 return p2pDevice; 99 } 100 ParseTlvs2ReqList(const std::vector<unsigned char> & tlvs)101bool WifiP2pServiceRequestList::ParseTlvs2ReqList(const std::vector<unsigned char> &tlvs) 102 { 103 std::size_t leftLength = tlvs.size(); 104 std::size_t vectorLenth = leftLength; 105 std::size_t headLength = SERVICE_TLV_LENGTH_SIZE + PROTOCOL_SIZE + TRANSACTION_ID_SIZE; 106 std::size_t pos = 0; 107 while (leftLength > 0) { 108 if (leftLength < headLength) { 109 WIFI_LOGW("Failed to format input tlv packet!"); 110 return false; 111 } 112 113 if (pos >= vectorLenth - 1) { 114 WIFI_LOGW("pos is overflow!"); 115 break; 116 } 117 unsigned short length = tlvs[pos] + (tlvs[pos + 1] << CHAR_BIT); 118 unsigned short dataLength = length - PROTOCOL_SIZE - TRANSACTION_ID_SIZE; 119 int protocolType = tlvs[pos + SERVICE_TLV_LENGTH_SIZE]; 120 unsigned char transId = tlvs[pos + SERVICE_TLV_LENGTH_SIZE + PROTOCOL_SIZE]; 121 122 if (dataLength > leftLength - headLength || dataLength < 0) { 123 WIFI_LOGW("A tlv packet error!"); 124 return false; 125 } 126 127 pos += headLength; 128 std::vector<unsigned char> query; 129 for (unsigned short i = 0; i < dataLength; ++i) { 130 query.push_back(tlvs[pos + i]); 131 } 132 pos += dataLength; 133 134 WifiP2pServiceRequest buf; 135 buf.SetProtocolType(static_cast<P2pServicerProtocolType>(protocolType)); 136 buf.SetTransactionId(transId); 137 buf.SetQuery(query); 138 srvReqList.push_back(buf); 139 leftLength = leftLength - headLength - dataLength; 140 } 141 return true; 142 } 143 } // namespace Wifi 144 } // namespace OHOS 145