1 /*
2 * Copyright (c) 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 #include "event_publisher.h"
17
18 #include "battery_srv_client.h"
19 #include "common_event_manager.h"
20 #include "common_event_support.h"
21 #include "net_supplier_info.h"
22 #include "want.h"
23 #include "work_sched_hilog.h"
24
25 namespace OHOS {
26 namespace WorkScheduler {
27 namespace {
28 static const std::string NETWORK = "network";
29 static const std::string EV_NETWORK_TYPE_WIFI = "wifi";
30 static const std::string EV_NETWORK_TYPE_DISCONNECT = "disconnect";
31 static const std::string CHARGING = "charging";
32 static const std::string EV_CHARGING_TYPE_USB = "usb";
33 static const std::string EV_CHARGING_TYPE_AC = "ac";
34 static const std::string EV_CHARGING_TYPE_WIRELESS = "wireless";
35 static const std::string EV_CHARGING_TYPE_NONE = "none";
36 static const std::string BATTERY_STATUS = "batteryStatus";
37 static const std::string STORAGE = "storage";
38 static const std::string EV_STORAGE_LOW = "low";
39 static const std::string EV_STORAGE_OKAY = "ok";
40 }
41
Dump(std::string & result,std::string & eventType,std::string & eventValue)42 void EventPublisher::Dump(std::string &result, std::string &eventType, std::string &eventValue)
43 {
44 if (eventType == "event" && eventValue == "info") {
45 result.append("event info:\n")
46 .append(" network wifi: publish COMMON_EVENT_CONNECTIVITY_CHANGE event(wifi).\n")
47 .append(" network disconnect: publish COMMON_EVENT_CONNECTIVITY_CHANGE event(disconnect).\n")
48 .append(" charging usb: publish usb charging event\n")
49 .append(" charging ac: publish ac charging event\n")
50 .append(" charging wireless: publish wireless charging event\n")
51 .append(" charging none: publish unplugged event\n")
52 .append(" storage low: publish COMMON_EVENT_DEVICE_STORAGE_LOW event\n")
53 .append(" storage ok: publish COMMON_EVENT_DEVICE_STORAGE_OKAY event\n")
54 .append(" batteryStatus low: publish COMMON_EVENT_BATTERY_LOW\n")
55 .append(" batteryStatus ok: publish COMMON_EVENT_BATTERY_OKAY\n");
56 } else {
57 PublishEvent(result, eventType, eventValue);
58 }
59 }
60
PublishEvent(std::string & result,std::string & eventType,std::string & eventValue)61 void EventPublisher::PublishEvent(std::string &result, std::string &eventType, std::string &eventValue)
62 {
63 if (eventType == NETWORK) {
64 PublishNetworkEvent(result, eventValue);
65 } else if (eventType == CHARGING) {
66 PublishChargingEvent(result, eventValue);
67 } else if (eventType == STORAGE) {
68 PublishStorageEvent(result, eventValue);
69 } else if (eventType == BATTERY_STATUS) {
70 PublishBatteryStatusEvent(result, eventValue);
71 } else {
72 result.append(std::string("dump -d need right params."));
73 }
74 }
75
PublishNetworkEvent(std::string & result,std::string & eventValue)76 void EventPublisher::PublishNetworkEvent(std::string &result, std::string &eventValue)
77 {
78 EventFwk::Want want;
79 if (eventValue == EV_NETWORK_TYPE_WIFI) {
80 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
81 want.SetParam("NetType", 1);
82 result.append("publishing COMMON_EVENT_WIFI_CONN_STATE\n");
83 EventFwk::CommonEventData data;
84 data.SetWant(want);
85 data.SetCode(NetManagerStandard::NetConnState::NET_CONN_STATE_CONNECTED);
86 bool isSuccess = EventFwk::CommonEventManager::PublishCommonEvent(data);
87 result.append("publish result: " + std::to_string(isSuccess));
88 } else if (eventValue == EV_NETWORK_TYPE_DISCONNECT) {
89 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
90 result.append("publishing COMMON_EVENT_WIFI_CONN_STATE\n");
91 EventFwk::CommonEventData data;
92 data.SetWant(want);
93 data.SetCode(NetManagerStandard::NetConnState::NET_CONN_STATE_DISCONNECTED);
94 bool isSuccess = EventFwk::CommonEventManager::PublishCommonEvent(data);
95 result.append("publish result: " + std::to_string(isSuccess));
96 } else {
97 result.append("dump need right param.");
98 return;
99 }
100 }
101
PublishChargingEvent(std::string & result,std::string & eventValue)102 void EventPublisher::PublishChargingEvent(std::string &result, std::string &eventValue)
103 {
104 EventFwk::Want want;
105 EventFwk::CommonEventData data;
106 if (eventValue == EV_CHARGING_TYPE_AC) {
107 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED);
108 data.SetWant(want);
109 data.SetCode(static_cast<uint32_t>(PowerMgr::BatteryPluggedType::PLUGGED_TYPE_AC));
110 } else if (eventValue == EV_CHARGING_TYPE_USB) {
111 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED);
112 data.SetWant(want);
113 data.SetCode(static_cast<uint32_t>(PowerMgr::BatteryPluggedType::PLUGGED_TYPE_USB));
114 } else if (eventValue == EV_CHARGING_TYPE_WIRELESS) {
115 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_CONNECTED);
116 data.SetWant(want);
117 data.SetCode(static_cast<uint32_t>(PowerMgr::BatteryPluggedType::PLUGGED_TYPE_WIRELESS));
118 } else if (eventValue == EV_CHARGING_TYPE_NONE) {
119 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
120 data.SetWant(want);
121 data.SetCode(static_cast<uint32_t>(PowerMgr::BatteryPluggedType::PLUGGED_TYPE_NONE));
122 } else {
123 result.append("dump need right param.");
124 return;
125 }
126 EventFwk::CommonEventPublishInfo publishInfo;
127 publishInfo.SetOrdered(false);
128 if (EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo)) {
129 result.append("publish charging event ret: true");
130 } else {
131 result.append("publish charging event ret: false");
132 }
133 }
134
PublishStorageEvent(std::string & result,std::string & eventValue)135 void EventPublisher::PublishStorageEvent(std::string &result, std::string &eventValue)
136 {
137 EventFwk::Want want;
138 if (eventValue == EV_STORAGE_LOW) {
139 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_DEVICE_STORAGE_LOW);
140 result.append("publishing COMMON_EVENT_DEVICE_STORAGE_LOW\n");
141 } else if (eventValue == EV_STORAGE_OKAY) {
142 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_DEVICE_STORAGE_OK);
143 result.append("publishing COMMON_EVENT_DEVICE_STORAGE_OKAY\n");
144 } else {
145 result.append("dump need right param.");
146 return;
147 }
148 EventFwk::CommonEventData data;
149 data.SetWant(want);
150 bool isSuccess = EventFwk::CommonEventManager::PublishCommonEvent(data);
151 result.append("publish result: " + std::to_string(isSuccess));
152 }
153
PublishBatteryStatusEvent(std::string & result,std::string & eventValue)154 void EventPublisher::PublishBatteryStatusEvent(std::string &result, std::string &eventValue)
155 {
156 EventFwk::Want want;
157 if (eventValue == EV_STORAGE_LOW) {
158 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_LOW);
159 result.append("publishing COMMON_EVENT_BATTERY_LOW\n");
160 EventFwk::CommonEventData data;
161 data.SetWant(want);
162 const int32_t lowCapacity = 0;
163 data.SetCode(lowCapacity);
164 bool isSuccess = EventFwk::CommonEventManager::PublishCommonEvent(data);
165 result.append("publish result: " + std::to_string(isSuccess));
166 } else if (eventValue == EV_STORAGE_OKAY) {
167 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_OKAY);
168 result.append("publishing COMMON_EVENT_BATTERY_OKAY\n");
169 EventFwk::CommonEventData data;
170 data.SetWant(want);
171 const int32_t fullCapacity = 100;
172 data.SetCode(fullCapacity);
173 bool isSuccess = EventFwk::CommonEventManager::PublishCommonEvent(data);
174 result.append("publish result: " + std::to_string(isSuccess));
175 } else {
176 result.append("dump need right param.");
177 return;
178 }
179 }
180 } // namespace WorkScheduler
181 } // namespace OHOS