• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 FILEMANAGEMENT_DFS_SERVICE_CONTROL_CMD_PARSER_H
17 #define FILEMANAGEMENT_DFS_SERVICE_CONTROL_CMD_PARSER_H
18 
19 #include <atomic>
20 #include <functional>
21 #include <string>
22 
23 namespace OHOS {
24 namespace Storage {
25 namespace DistributedFile {
26 
27 static const char DETERMINE_DEVICE_TYPE_KEY[] = "persist.distributed_scene.sys_settings_data_sync";
28 static const uint16_t DFS_CMD_VERSION = 60;
29 
30 enum ControlCmdType {
31     CMD_UNKNOWN = 0,
32     CMD_CHECK_ALLOW_CONNECT = 1,
33     CMD_MSG_RESPONSE = 2,
34     CMD_PUBLISH_NOTIFICATION = 3,
35     CMD_CANCEL_NOTIFICATION = 4,
36     CMD_ACTIVE_DISCONNECT = 5,
37 };
38 
39 struct ControlCmd {
40     static std::atomic<int32_t> nextMsgId;
41 
42     uint16_t version = DFS_CMD_VERSION;
43     int32_t msgId = 0;
44     int32_t msgType = 0;
45     std::string msgBody = "";
46     std::string networkId = "";
47 
ControlCmdControlCmd48     ControlCmd() : msgId(nextMsgId.fetch_add(1, std::memory_order_relaxed)) {}
49 };
50 
51 inline std::atomic<int32_t> ControlCmd::nextMsgId;
52 
53 class ControlCmdParser {
54 public:
55     static bool ParseFromJson(const std::string &jsonStr, ControlCmd &outCmd);
56     static bool SerializeToJson(const ControlCmd &cmd, std::string &outJsonStr);
57 
58     static bool HandleRequest(const ControlCmd &inCmd, ControlCmd &outCmd);
59 
60     static bool CheckAllowConnect(const ControlCmd &inCmd, ControlCmd &outCmd);
61     static bool PublishNotification(const ControlCmd &inCmd, ControlCmd &outCmd);
62     static bool CancelNotification(const ControlCmd &inCmd, ControlCmd &outCmd);
63     static bool DisconnectByRemote(const ControlCmd &inCmd, ControlCmd &outCmd);
64 
65     static void RegisterDisconnectCallback(std::function<void(std::string)> cb);
66 
67 private:
68     static std::function<void(std::string)> disconnectCallback_;
69 };
70 
71 } // namespace DistributedFile
72 } // namespace Storage
73 } // namespace OHOS
74 
75 #endif // FILEMANAGEMENT_DFS_SERVICE_CONTROL_CMD_PARSER_H