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_logger.h"
16 #include "wifi_p2p_upnp_service_response.h"
17
18 namespace OHOS {
19 namespace Wifi {
WifiP2pUpnpServiceResponse(P2pServiceStatus status,int tranId,std::vector<unsigned char> data)20 WifiP2pUpnpServiceResponse::WifiP2pUpnpServiceResponse(
21 P2pServiceStatus status, int tranId, std::vector<unsigned char> data)
22 : WifiP2pServiceResponse(P2pServicerProtocolType::SERVICE_TYPE_UP_NP, status, tranId, data), version(-1), uniqueServNames()
23 {}
24
Create(P2pServiceStatus status,int tranId,std::vector<unsigned char> data)25 WifiP2pUpnpServiceResponse WifiP2pUpnpServiceResponse::Create(
26 P2pServiceStatus status, int tranId, std::vector<unsigned char> data)
27 {
28 if (status != P2pServiceStatus::PSRS_SUCCESS) {
29 std::vector<unsigned char> nullData;
30 return WifiP2pUpnpServiceResponse(status, tranId, nullData);
31 } else {
32 return WifiP2pUpnpServiceResponse(status, tranId, data);
33 }
34 }
35
GetVersion() const36 int WifiP2pUpnpServiceResponse::GetVersion() const
37 {
38 return version;
39 }
40
GetUniqueServNames() const41 const std::vector<std::string> &WifiP2pUpnpServiceResponse::GetUniqueServNames() const
42 {
43 return uniqueServNames;
44 }
45
ParseData()46 bool WifiP2pUpnpServiceResponse::ParseData()
47 {
48 if (responseData.size() < 1) {
49 return false;
50 }
51 version = responseData.at(0) & 0xff;
52 auto lastIt = responseData.begin() + 1;
53 auto it = responseData.begin() + 1;
54 for (; it != responseData.end(); ) {
55 if (*it == ',') {
56 uniqueServNames.push_back(std::string(lastIt, it));
57 lastIt = it + 1;
58 } else if (*it == ';') {
59 uniqueServNames.push_back(std::string(lastIt, it));
60 lastIt = it + 1;
61 int svrNameLength = static_cast<int>(*lastIt);
62 lastIt++;
63 it = lastIt + svrNameLength;
64 mSvrName = std::string(lastIt, it);
65 return true;
66 }
67 ++it;
68 }
69 uniqueServNames.push_back(std::string(lastIt, it));
70 return true;
71 }
72 } // namespace Wifi
73 } // namespace OHOS