• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef KERNEL_TALKER_H
16 #define KERNEL_TALKER_H
17 
18 #include <atomic>
19 #include <fcntl.h>
20 #include <functional>
21 #include <memory>
22 #include <mutex>
23 #include <poll.h>
24 #include <thread>
25 #include <unistd.h>
26 #include <unordered_set>
27 
28 #include "mountpoint/mount_point.h"
29 #include "network/base_session.h"
30 #include "utils_log.h"
31 
32 namespace OHOS {
33 namespace Storage {
34 namespace DistributedFile {
35 constexpr int CID_MAX_LEN = 64;
36 struct NotifyParam {
37     int32_t notify;
38     int32_t fd;
39     uint16_t udpPort;
40     uint8_t deviceType;
41     int32_t flag;
42     int32_t reserved;
43     char remoteCid[CID_MAX_LEN];
44 } __attribute__((packed));
45 
46 class KernelTalker final : protected NoCopyable, public std::enable_shared_from_this<KernelTalker> {
47 public:
KernelTalker(std::weak_ptr<MountPoint> mountPoint,std::function<void (NotifyParam &)> getSessionCallback,std::function<void (const std::string &)> closeSessionCallback)48     explicit KernelTalker(std::weak_ptr<MountPoint> mountPoint,
49                           std::function<void(NotifyParam &)> getSessionCallback,
50                           std::function<void(const std::string &)> closeSessionCallback)
51         : mountPoint_(mountPoint), GetSessionCallback_(getSessionCallback), CloseSessionCallback_(closeSessionCallback)
52     {
53     }
54     KernelTalker() = default;
55     ~KernelTalker() = default;
56 
57     void SinkSessionTokernel(std::shared_ptr<BaseSession> session);
58     void SinkDevslTokernel(const std::string &cid, uint32_t devsl);
59     void SinkOfflineCmdToKernel(std::string cid);
60 
61     void CreatePollThread();
62     void WaitForPollThreadExited();
63 
64 private:
65     template<typename T>
SetCmd(T & cmd)66     void SetCmd(T &cmd)
67     {
68         auto spt = mountPoint_.lock();
69         if (spt == nullptr) {
70             LOGE("mountPoint is not exist! bad weak_ptr");
71             return;
72         }
73         std::string ctrlPath = spt->GetMountArgument().GetCtrlPath();
74         LOGI("cmd path:%{public}s", ctrlPath.c_str());
75         std::lock_guard<std::mutex> lock(cmdMutex_);
76 
77         char *realPath = realpath(ctrlPath.c_str(), nullptr);
78         if (realPath == nullptr) {
79             return;
80         }
81 
82         int file = open(realPath, O_RDWR);
83         free(realPath);
84         if (file < 0) {
85             LOGE("Open node file error. %{public}d", errno);
86             return;
87         }
88         int err = write(file, &cmd, sizeof(T));
89         if (err < 0) {
90             LOGE("write return err. %{public}d", errno);
91         }
92         close(file);
93     }
94 
95     void PollRun();
96     void HandleAllNotify(int fd);
97     void NotifyHandler(NotifyParam &param);
98 
99     std::weak_ptr<MountPoint> mountPoint_;
100     std::mutex cmdMutex_;
101     std::atomic<bool> isRunning_ {true};
102     std::unique_ptr<std::thread> pollThread_ {nullptr};
103     std::function<void(NotifyParam &)> GetSessionCallback_ {nullptr};
104     std::function<void(const std::string &)> CloseSessionCallback_ {nullptr};
105 };
106 } // namespace DistributedFile
107 } // namespace Storage
108 } // namespace OHOS
109 #endif // KERNEL_TALKER_H