• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "wlan_param_monitor.h"
10 
11 #include "hdf_dsoftbus_driver.h"
12 #include "hdf_log.h"
13 #include "module_manager.h"
14 #include "osal_time.h"
15 #include "osal_timer.h"
16 
17 #define HDF_LOG_TAG "wlan_param_monitor"
18 
19 #define WLAN_PARAM_REPORT_INTERVAL 1000
20 
21 typedef enum {
22     CMD_START_MONITOR = 0,
23     CMD_STOP_MONITOR,
24     CMD_MAX_INDEX
25 } Command;
26 
27 typedef enum {
28     EVENT_WLAN_PARAM = 0,
29     EVENT_MAX_INDEX
30 } Event;
31 
32 typedef struct {
33     OSAL_DECLARE_TIMER(timer);
34     bool isTimerStart;
35 } WlanParamMonitorCtrl;
36 
37 typedef struct {
38     uint32_t event;
39     uint32_t value;
40 } ReportInfo;
41 
42 static WlanParamMonitorCtrl g_wlanParamMonitorCtrl = {
43     .isTimerStart = false,
44 };
45 
WlanParamReportTimer(uintptr_t arg)46 static void WlanParamReportTimer(uintptr_t arg)
47 {
48     ReportInfo info;
49     struct HdfSBuf *data = NULL;
50 
51     (void)arg;
52     info.event = EVENT_WLAN_PARAM;
53     info.value = (uint32_t)OsalGetSysTimeMs();
54     data = HdfSBufObtainDefaultSize();
55     if (data == NULL) {
56         HDF_LOGE("get sbuf fail");
57         return;
58     }
59     if (!HdfSbufWriteBuffer(data, (const void *)&info, sizeof(info))) {
60         HDF_LOGE("sbuf write report value fail");
61         HdfSBufRecycle(data);
62         return;
63     }
64     HdfSoftbusBroadcastEvent(SOFTBUS_MODULE_WLAN_PARAM_MONITOR, data);
65     HdfSBufRecycle(data);
66 }
67 
ProcessStartMonitor(void)68 static void ProcessStartMonitor(void)
69 {
70     if (g_wlanParamMonitorCtrl.isTimerStart) {
71         HDF_LOGE("wlan param monitor timer is already started");
72         return;
73     }
74     if (OsalTimerCreate(&g_wlanParamMonitorCtrl.timer, WLAN_PARAM_REPORT_INTERVAL,
75         WlanParamReportTimer, 0) != HDF_SUCCESS) {
76         HDF_LOGE("create wlan param monitor timer fail");
77         return;
78     }
79     if (OsalTimerStartLoop(&g_wlanParamMonitorCtrl.timer) != HDF_SUCCESS) {
80         OsalTimerDelete(&g_wlanParamMonitorCtrl.timer);
81         HDF_LOGE("start wlan param monitor timer fail");
82         return;
83     }
84     g_wlanParamMonitorCtrl.isTimerStart = true;
85 }
86 
ProcessStopMonitor(void)87 static void ProcessStopMonitor(void)
88 {
89     if (!g_wlanParamMonitorCtrl.isTimerStart) {
90         HDF_LOGE("wlan param monitor timer is not started");
91         return;
92     }
93     if (OsalTimerDelete(&g_wlanParamMonitorCtrl.timer) != HDF_SUCCESS) {
94         HDF_LOGE("delete wlan param monitor timer fail");
95     } else {
96         g_wlanParamMonitorCtrl.isTimerStart = false;
97     }
98 }
99 
SoftbusWlanParamMonitorInit(struct HdfDeviceObject * device)100 int32_t SoftbusWlanParamMonitorInit(struct HdfDeviceObject *device)
101 {
102     (void)device;
103     HDF_LOGI("SoftbusWlanParamMonitorInit init");
104     return HDF_SUCCESS;
105 }
106 
SoftbusWlanParamMonitorDeinit(void)107 void SoftbusWlanParamMonitorDeinit(void)
108 {
109     if (g_wlanParamMonitorCtrl.isTimerStart) {
110         ProcessStartMonitor();
111     }
112 }
113 
SoftbusWlanParamMonitorProcess(const struct HdfSBuf * reqData,struct HdfSBuf * rspData)114 void SoftbusWlanParamMonitorProcess(const struct HdfSBuf *reqData, struct HdfSBuf *rspData)
115 {
116     uint32_t cmd;
117     const void *data = NULL;
118     uint32_t dataSize;
119 
120     (void)rspData;
121     if (reqData == NULL) {
122         HDF_LOGE("reqData is null");
123         return;
124     }
125     if (!HdfSbufReadBuffer((struct HdfSBuf *)reqData, &data, &dataSize)) {
126         HDF_LOGE("read command fail");
127         return;
128     }
129     cmd = *((uint32_t *)data);
130     HDF_LOGI("process command: %d", cmd);
131     switch (cmd) {
132         case CMD_START_MONITOR:
133             ProcessStartMonitor();
134             break;
135         case CMD_STOP_MONITOR:
136             ProcessStopMonitor();
137             break;
138         default:
139             break;
140     }
141 }