/* * Copyright (C) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "dhcp_api.h" #ifdef OHOS_ARCH_LITE bool PublishDhcpIpv4ResultEvent(const int code, const char *data, const char *ifname) { return true; } #else #include "securec.h" #include "common_event.h" #include "common_event_data.h" #include "common_event_manager.h" #undef LOG_TAG #define LOG_TAG "WifiDhcpApi" using namespace OHOS::EventFwk; static const std::string DHCP_SUB_PERMISSION = "ohos.permission.GET_WIFI_INFO_INTERNAL"; static bool PublishDhcpEvent(const char *action, const int code, const char *data, const std::vector &permissions) { Want want; want.SetAction(action); CommonEventData commonData; commonData.SetWant(want); commonData.SetCode(code); commonData.SetData(data); if (action == nullptr || data == nullptr) { LOGE("PublishDhcpEvent() action or data is nullptr"); return false; } if (permissions.size() > 0) { CommonEventPublishInfo publishInfo; publishInfo.SetSubscriberPermissions(permissions); if (!CommonEventManager::PublishCommonEvent(commonData, publishInfo)) { LOGE("failed to publish event[%{public}s], code:%{public}d", action, code); return false; } LOGI("successed to publish event:%{public}s, code:%{public}d, data:%{private}s.", action, code, data); return true; } if (!CommonEventManager::PublishCommonEvent(commonData)) { LOGE("PublishDhcpEvent() PublishCommonEvent failed, action:%{public}s, code:%{public}d, data:%{public}s.", action, code, data); return false; } LOGI("PublishDhcpEvent() PublishCommonEvent success, action:%{public}s, code:%{public}d, data:%{private}s.", action, code, data); return true; } bool PublishDhcpIpv4ResultEvent(const int code, const char *data, const char *ifname) { char strAction[STRING_MAX_LEN] = {0}; if (ifname == nullptr) { if (strncpy_s(strAction, sizeof(strAction), EVENT_GET_IPV4, strlen(EVENT_GET_IPV4)) != EOK) { LOGE("PublishDhcpIpv4ResultEvent() strncpy_s %{public}s failed!", EVENT_GET_IPV4); return false; } } else { if (snprintf_s(strAction, STRING_MAX_LEN, STRING_MAX_LEN - 1, "%s.%s", EVENT_GET_IPV4, ifname) < 0) { LOGE("PublishDhcpIpv4ResultEvent() snprintf_s %{public}s failed!", ifname); return false; } } std::vector dhcpPermissions; dhcpPermissions.emplace_back(DHCP_SUB_PERMISSION); return PublishDhcpEvent(strAction, code, data, dhcpPermissions); } #endif