• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 
16 #ifndef OHOS_FUNCTION_H
17 #define OHOS_FUNCTION_H
18 
19 #include <iostream>
20 #include <fstream>
21 #include <cstring>
22 #include <iomanip>
23 #include <arpa/inet.h>
24 #include <string>
25 #include <cstdio>
26 
27 #include "dhcp_define.h"
28 
29 #ifndef OHOS_ARCH_LITE
30 #include "common_event_subscriber.h"
31 #include "common_event.h"
32 #include "common_event_data.h"
33 #include "common_event_manager.h"
34 #endif
35 
36 namespace OHOS {
37 namespace Wifi {
38 class DhcpFunc {
39 public:
DhcpFunc()40     DhcpFunc() {}
~DhcpFunc()41     ~DhcpFunc() {}
42 
43     static bool Ip4StrConToInt(const std::string& strIp, uint32_t& uIp, bool bHost = true);
44     static std::string Ip4IntConToStr(uint32_t uIp, bool bHost = true);
45     static bool Ip6StrConToChar(const std::string& strIp, uint8_t chIp[], size_t uSize);
46     static std::string Ip6CharConToStr(uint8_t chIp[], int size);
47     static bool CheckIpStr(const std::string& strIp);
48     static int GetLocalIp(const std::string strInf, std::string& strIp, std::string& strMask);
49     static int GetLocalMac(const std::string ethInf, std::string& ethMac);
50     static int CheckRangeNetwork(const std::string strInf, const std::string strBegin, const std::string strEnd);
51     static bool CheckSameNetwork(const uint32_t srcIp, const uint32_t dstIp, const uint32_t maskIp);
52 
53     static bool IsExistFile(const std::string& filename);
54     static bool CreateFile(const std::string& filename, const std::string& filedata);
55     static bool RemoveFile(const std::string& filename);
56     static bool AddFileLineData(const std::string& filename, const std::string& prevdata, const std::string& linedata);
57     static bool DelFileLineData(const std::string& filename, const std::string& linedata);
58     static bool ModifyFileLineData(const std::string& filename, const std::string& srcdata, const std::string& dstdata);
59     static int FormatString(struct DhcpPacketResult &result);
60     static int InitPidfile(const std::string& piddir, const std::string& pidfile);
61     static pid_t GetPID(const std::string& pidfile);
62     static int CheckProRunning(const pid_t proPid, const std::string& proName);
63     static int CreateDirs(const std::string dirs, int mode = DIR_DEFAULT_MODE);
64     static bool SplitString(
65         const std::string src, const std::string delim, const int count, std::vector<std::string> &splits);
66 #ifdef OHOS_ARCH_LITE
67     static int GetDhcpPacketResult(const std::string& filename, struct DhcpPacketResult &result);
68 #else
69     static bool SubscribeDhcpCommonEvent(
70         const std::shared_ptr<OHOS::EventFwk::CommonEventSubscriber> &subscriber);
71     static bool UnsubscribeDhcpCommonEvent(
72         const std::shared_ptr<OHOS::EventFwk::CommonEventSubscriber> &subscriber);
73 
74     static bool PublishDhcpEvent(const std::string action, const int code, const std::string data);
75 #endif
76 };
77 }  // namespace Wifi
78 }  // namespace OHOS
79 #endif