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
PublishDhcpEvent(const char * action,const int code,const char * data)18 static bool PublishDhcpEvent(const char *action, const int code, const char *data)
19 {
20 return true;
21 }
22
PublishDhcpIpv4ResultEvent(const int code,const char * data,const char * ifname)23 bool PublishDhcpIpv4ResultEvent(const int code, const char *data, const char *ifname)
24 {
25 return true;
26 }
27 #else
28 #include "securec.h"
29 #include "common_event.h"
30 #include "common_event_data.h"
31 #include "common_event_manager.h"
32
33 #undef LOG_TAG
34 #define LOG_TAG "WifiDhcpApi"
35
36 using namespace OHOS::EventFwk;
37
PublishDhcpEvent(const char * action,const int code,const char * data)38 static bool PublishDhcpEvent(const char *action, const int code, const char *data)
39 {
40 Want want;
41 want.SetAction(action);
42 CommonEventData commonData;
43 commonData.SetWant(want);
44 commonData.SetCode(code);
45 commonData.SetData(data);
46 if (!CommonEventManager::PublishCommonEvent(commonData)) {
47 LOGE("PublishDhcpEvent() PublishCommonEvent failed, action:%{public}s, code:%{public}d, data:%{public}s.",
48 action, code, data);
49 return false;
50 }
51 LOGI("PublishDhcpEvent() PublishCommonEvent success, action:%{public}s, code:%{public}d, data:%{private}s.",
52 action, code, data);
53 return true;
54 }
55
PublishDhcpIpv4ResultEvent(const int code,const char * data,const char * ifname)56 bool PublishDhcpIpv4ResultEvent(const int code, const char *data, const char *ifname)
57 {
58 char strAction[STRING_MAX_LEN] = {0};
59 if (ifname == NULL) {
60 if (strncpy_s(strAction, sizeof(strAction), EVENT_GET_IPV4, strlen(EVENT_GET_IPV4)) != EOK) {
61 LOGE("PublishDhcpIpv4ResultEvent() strncpy_s %{public}s failed!", EVENT_GET_IPV4);
62 return false;
63 }
64 } else {
65 if (snprintf_s(strAction, STRING_MAX_LEN, STRING_MAX_LEN - 1, "%s.%s", EVENT_GET_IPV4, ifname) < 0) {
66 LOGE("PublishDhcpIpv4ResultEvent() snprintf_s %{public}s failed!", ifname);
67 return false;
68 }
69 }
70 return PublishDhcpEvent(strAction, code, data);
71 }
72 #endif
73