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 #include "dhcp_api.h"
16
17 #ifdef OHOS_ARCH_LITE
PublishDhcpIpv4ResultEvent(const int code,const char * data,const char * ifname)18 bool PublishDhcpIpv4ResultEvent(const int code, const char *data, const char *ifname)
19 {
20 return true;
21 }
22 #else
23 #include "securec.h"
24 #include "common_event.h"
25 #include "common_event_data.h"
26 #include "common_event_manager.h"
27
28 #undef LOG_TAG
29 #define LOG_TAG "WifiDhcpApi"
30
31 using namespace OHOS::EventFwk;
32
33 static const std::string DHCP_SUB_PERMISSION = "ohos.permission.GET_WIFI_INFO_INTERNAL";
34
PublishDhcpEvent(const char * action,const int code,const char * data,const std::vector<std::string> & permissions)35 static bool PublishDhcpEvent(const char *action, const int code, const char *data,
36 const std::vector<std::string> &permissions)
37 {
38 Want want;
39 want.SetAction(action);
40 CommonEventData commonData;
41 commonData.SetWant(want);
42 commonData.SetCode(code);
43 commonData.SetData(data);
44 if (action == nullptr || data == nullptr) {
45 LOGE("PublishDhcpEvent() action or data is nullptr");
46 return false;
47 }
48 if (permissions.size() > 0) {
49 CommonEventPublishInfo publishInfo;
50 publishInfo.SetSubscriberPermissions(permissions);
51 if (!CommonEventManager::PublishCommonEvent(commonData, publishInfo)) {
52 LOGE("failed to publish event[%{public}s], code:%{public}d", action, code);
53 return false;
54 }
55 LOGI("successed to publish event:%{public}s, code:%{public}d, data:%{private}s.", action, code, data);
56 return true;
57 }
58 if (!CommonEventManager::PublishCommonEvent(commonData)) {
59 LOGE("PublishDhcpEvent() PublishCommonEvent failed, action:%{public}s, code:%{public}d, data:%{public}s.",
60 action, code, data);
61 return false;
62 }
63 LOGI("PublishDhcpEvent() PublishCommonEvent success, action:%{public}s, code:%{public}d, data:%{private}s.",
64 action, code, data);
65 return true;
66 }
67
PublishDhcpIpv4ResultEvent(const int code,const char * data,const char * ifname)68 bool PublishDhcpIpv4ResultEvent(const int code, const char *data, const char *ifname)
69 {
70 char strAction[STRING_MAX_LEN] = {0};
71 if (ifname == nullptr) {
72 if (strncpy_s(strAction, sizeof(strAction), EVENT_GET_IPV4, strlen(EVENT_GET_IPV4)) != EOK) {
73 LOGE("PublishDhcpIpv4ResultEvent() strncpy_s %{public}s failed!", EVENT_GET_IPV4);
74 return false;
75 }
76 } else {
77 if (snprintf_s(strAction, STRING_MAX_LEN, STRING_MAX_LEN - 1, "%s.%s", EVENT_GET_IPV4, ifname) < 0) {
78 LOGE("PublishDhcpIpv4ResultEvent() snprintf_s %{public}s failed!", ifname);
79 return false;
80 }
81 }
82 std::vector<std::string> dhcpPermissions;
83 dhcpPermissions.emplace_back(DHCP_SUB_PERMISSION);
84 return PublishDhcpEvent(strAction, code, data, dhcpPermissions);
85 }
86 #endif
87