• 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 #include "mc_subscription_center.h"
17 
18 #include <map>
19 #include <memory>
20 #include <string>
21 
22 #include "mechbody_controller_enums.h"
23 #include "mechbody_controller_log.h"
24 #include "./command/mc_command_factory.h"
25 
26 namespace OHOS {
27 namespace MechBodyController {
GetInstance()28 SubscriptionCenter& SubscriptionCenter::GetInstance()
29 {
30     static auto instance = new SubscriptionCenter();
31     return *instance;
32 }
33 
34 namespace {
35     const std::string TAG = "SubscriptionCenter";
36 }
37 
Subscribe(uint16_t type,const std::shared_ptr<IMechEventListener> & listener)38 int32_t SubscriptionCenter::Subscribe(uint16_t type, const std::shared_ptr<IMechEventListener> &listener)
39 {
40     CHECK_POINTER_RETURN_VALUE(listener, INVALID_PARAMETERS_ERR, "listener");
41     HILOGI("called, type: 0x%{public}x", type);
42     std::lock_guard<std::mutex> lock(mutex_);
43     std::vector<std::shared_ptr<IMechEventListener>> callbacks;
44     auto it = subscribers_.find(type);
45     if (it == subscribers_.end()) {
46         callbacks.push_back(listener);
47         subscribers_[type] = callbacks;
48         return ERR_OK;
49     }
50     callbacks = it->second;
51     for (auto callback : callbacks) {
52         if (callback == listener) {
53             HILOGW("this callback function has been registered.");
54             return ERR_OK;
55         }
56     }
57     callbacks.push_back(listener);
58     subscribers_[type] = callbacks;
59     return ERR_OK;
60 }
61 
UnSubscribe(uint16_t type,const std::shared_ptr<IMechEventListener> & listener)62 int32_t SubscriptionCenter::UnSubscribe(uint16_t type, const std::shared_ptr<IMechEventListener> &listener)
63 {
64     CHECK_POINTER_RETURN_VALUE(listener, INVALID_PARAMETERS_ERR, "listener");
65     HILOGI("called, type: %{public}d", type);
66     std::lock_guard<std::mutex> lock(mutex_);
67     auto it = subscribers_.find(type);
68     if (it == subscribers_.end()) {
69         HILOGW("this callback function can not found.");
70         return ERR_OK;
71     }
72     std::vector<std::shared_ptr<IMechEventListener>> callbacks = it->second;
73     auto it1 = std::find(callbacks.begin(), callbacks.end(), listener);
74     if (it1 != callbacks.end()) {
75         HILOGW("this callback function has been found.");
76         callbacks.erase(it1);
77         subscribers_[type] = callbacks;
78     } else {
79         HILOGW("this callback function can not found.");
80         return UNREGISTER_FAILED_ERR;
81     }
82     HILOGI("end");
83     return ERR_OK;
84 }
85 
Notify(const std::shared_ptr<CommandBase> & cmd)86 int32_t SubscriptionCenter::Notify(const std::shared_ptr<CommandBase> &cmd)
87 {
88     CHECK_POINTER_RETURN_VALUE(cmd, INVALID_PARAMETERS_ERR, "cmd");
89     HILOGD("cmdType : 0x%{public}x, cmdId: %{public}d", cmd->GetCmdType(), cmd->GetCmdId());
90 
91     std::vector<std::shared_ptr<IMechEventListener>> callbacks;
92     {
93         std::lock_guard<std::mutex> lock(mutex_);
94         auto it = subscribers_.find(cmd->GetCmdType());
95         if (it == subscribers_.end()) {
96             return NOTIFICATION_FAILED_ERR;
97         }
98         callbacks = it->second;
99     }
100     for (auto callback : callbacks) {
101         if (callback == nullptr) {
102             continue;
103         }
104         switch (cmd->GetCmdType()) {
105             case CMD_TYPE_BUTTON_EVENT_NOTIFY:
106                 callback->MechButtonEventNotify(std::static_pointer_cast<RegisterMechCameraKeyEventCmd>(cmd));
107                 break;
108             case CMD_TYPE_PARAM_NOTIFY:
109                 callback->MechParamNotify(std::static_pointer_cast<RegisterMechStateInfoCmd>(cmd));
110                 break;
111             case CMD_TYPE_ATTITUDE_NOTIFY:
112                 callback->MechAttitudeNotify(std::static_pointer_cast<RegisterMechPositionInfoCmd>(cmd));
113                 break;
114             case CMD_TYPE_EXE_RESULT_NOTIFY:
115                 callback->MechExecutionResultNotify(std::static_pointer_cast<RegisterMechControlResultCmd>(cmd));
116                 break;
117             case CMD_TYPE_WHEEL_DATA_NOTIFY:
118                 callback->MechWheelZoomNotify(std::static_pointer_cast<RegisterMechWheelDataCmd>(cmd));
119                 break;
120             case CMD_TYPE_TRACKING_ENABLED_NOTIFY:
121                 callback->MechTrackingStatusNotify(std::static_pointer_cast<RegisterMechTrackingEnableCmd>(cmd));
122                 break;
123             default:
124                 HILOGD("Not found cmdType : 0x%{public}x, cmdId: %{public}d", cmd->GetCmdType(), cmd->GetCmdId());
125                 break;
126         }
127     }
128     HILOGD("end");
129     return ERR_OK;
130 }
131 }  // namespace MechBodyController
132 }  // namespace OHOS
133