diff --git a/model/misc/dsoftbus/include/hdf_dsoftbus_driver.h b/model/misc/dsoftbus/include/hdf_dsoftbus_driver.h new file mode 100644 index 0000000000000000000000000000000000000000..f3d0c9a986cbb84feb5f0585c0f66f8edf6cfc4c --- /dev/null +++ b/model/misc/dsoftbus/include/hdf_dsoftbus_driver.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#ifndef HDF_DSOFTBUS_DRIVER_H +#define HDF_DSOFTBUS_DRIVER_H + +#include "hdf_base.h" +#include "hdf_sbuf.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t HdfSoftbusBroadcastEvent(uint32_t moudleId, const struct HdfSBuf *data); + +#ifdef __cplusplus +} +#endif + +#endif // HDF_DSOFTBUS_DRIVER_H \ No newline at end of file diff --git a/model/misc/dsoftbus/include/module_manager.h b/model/misc/dsoftbus/include/module_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..a59ef5fe1b4ab0127010b1ed12b8fee25949efdf --- /dev/null +++ b/model/misc/dsoftbus/include/module_manager.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#ifndef SOFTBUS_MODULE_MANAGER_H +#define SOFTBUS_MODULE_MANAGER_H + +#include "hdf_base.h" +#include "hdf_device_desc.h" +#include "hdf_sbuf.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + SOFTBUS_MODULE_WLAN_PARAM_MONITOR = 0, + SOFTBUS_MODULE_MAX_INDEX, +} SoftbusDriverModuleId; + +typedef int32_t (*SoftbusDriverModuleInit)(struct HdfDeviceObject *device); +typedef void (*SoftbusDriverModuleDeinit)(void); +typedef void (*SoftbusDriverModuleProcess)(const struct HdfSBuf *reqData, struct HdfSBuf *rspData); + +typedef struct { + int32_t moduleId; + SoftbusDriverModuleInit init; + SoftbusDriverModuleDeinit deinit; + SoftbusDriverModuleProcess process; +} SoftbusDriverModule; + +int32_t SoftbusModuleManagerInit(struct HdfDeviceObject *device); +void SoftbusModuleManagerDeinit(void); +void SoftbusDispatchModuleCommand(int32_t moduleId, const struct HdfSBuf *reqData, struct HdfSBuf *rspData); + +#ifdef __cplusplus +} +#endif + +#endif // SOFTBUS_MODULE_MANAGER_H \ No newline at end of file diff --git a/model/misc/dsoftbus/include/wlan_param_monitor.h b/model/misc/dsoftbus/include/wlan_param_monitor.h new file mode 100644 index 0000000000000000000000000000000000000000..96eae6b154e4580ebd1db4c8e18fc5ec6434541b --- /dev/null +++ b/model/misc/dsoftbus/include/wlan_param_monitor.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#ifndef SOFTBUS_WLAN_PARAM_MONITOR_H +#define SOFTBUS_WLAN_PARAM_MONITOR_H + +#include "hdf_base.h" +#include "hdf_device_desc.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t SoftbusWlanParamMonitorInit(struct HdfDeviceObject *device); +void SoftbusWlanParamMonitorDeinit(void); +void SoftbusWlanParamMonitorProcess(const struct HdfSBuf *reqData, struct HdfSBuf *rspData); + +#ifdef __cplusplus +} +#endif + +#endif // SOFTBUS_WLAN_PARAM_MONITOR_H \ No newline at end of file diff --git a/model/misc/dsoftbus/src/hdf_dsoftbus_driver.c b/model/misc/dsoftbus/src/hdf_dsoftbus_driver.c new file mode 100644 index 0000000000000000000000000000000000000000..c837ced89514b313ea21d1744491ea4daf108277 --- /dev/null +++ b/model/misc/dsoftbus/src/hdf_dsoftbus_driver.c @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include "hdf_base.h" +#include "hdf_device_desc.h" +#include "hdf_log.h" +#include "module_manager.h" + +#define HDF_LOG_TAG "hdf_dsoftbus" + +static struct HdfDeviceObject *g_hdfDevObj = NULL; + +int32_t HdfSoftbusBroadcastEvent(uint32_t moudleId, const struct HdfSBuf *data) +{ + if (g_hdfDevObj == NULL) { + return HDF_FAILURE; + } + return HdfDeviceSendEvent(g_hdfDevObj, moudleId, data); +} + +static int32_t DispatchCommand(struct HdfDeviceIoClient *client, int moduleId, + struct HdfSBuf *reqData, struct HdfSBuf *rspData) +{ + (void)client; + SoftbusDispatchModuleCommand(moduleId, reqData, rspData); + return HDF_SUCCESS; +} + +static int32_t HdfSoftbusDriverBind(struct HdfDeviceObject *dev) +{ + static struct IDeviceIoService softbusService = { + .object.objectId = 1, + .Dispatch = DispatchCommand, + }; + + if (dev == NULL) { + HDF_LOGE("hdf device object is null"); + return HDF_FAILURE; + } + dev->service = &softbusService; + HDF_LOGE("softbus driver bind success"); + return HDF_SUCCESS; +} + +static int32_t HdfSoftbusDriverInit(struct HdfDeviceObject *device) +{ + if (device == NULL) { + HDF_LOGE("device object is null"); + return HDF_ERR_INVALID_PARAM; + } + g_hdfDevObj = device; + return SoftbusModuleManagerInit(device); +} + +static void HdfSoftbusDriverRelease(struct HdfDeviceObject *object) +{ + (void)object; + HDF_LOGE("softbus driver release success"); +} + +static struct HdfDriverEntry g_hdfSoftbusEntry = { + .moduleVersion = 1, + .Bind = HdfSoftbusDriverBind, + .Init = HdfSoftbusDriverInit, + .Release = HdfSoftbusDriverRelease, + .moduleName = "HDF_DSOFTBUS" +}; + +HDF_INIT(g_hdfSoftbusEntry); \ No newline at end of file diff --git a/model/misc/dsoftbus/src/module_manager.c b/model/misc/dsoftbus/src/module_manager.c new file mode 100644 index 0000000000000000000000000000000000000000..ea9f741bba0f9633582c80784342ca257173b562 --- /dev/null +++ b/model/misc/dsoftbus/src/module_manager.c @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include "module_manager.h" + +#include "hdf_log.h" +#ifdef ENABLE_WLAN_PARAM_MONITOR +#include "wlan_param_monitor.h" +#endif + +#define HDF_LOG_TAG "hdf_dsoftbus" + +static SoftbusDriverModule g_modules[] = { +#ifdef ENABLE_WLAN_PARAM_MONITOR + { + .init = SoftbusWlanParamMonitorInit, + .deinit = SoftbusWlanParamMonitorDeinit, + .process = SoftbusWlanParamMonitorProcess, + }, +#endif +}; + +void SoftbusDispatchModuleCommand(int32_t moduleId, const struct HdfSBuf *reqData, struct HdfSBuf *rspData) +{ + int32_t i; + + for (i = 0; i < HDF_ARRAY_SIZE(g_modules); ++i) { + if (g_modules[i].moduleId != moduleId) { + continue; + } + if (g_modules[i].process == NULL) { + HDF_LOGE("module(%d) no process function", moduleId); + break; + } + g_modules[i].process(reqData, rspData); + return; + } + HDF_LOGE("no moduleId: %d registered", moduleId); +} + +int32_t SoftbusModuleManagerInit(struct HdfDeviceObject *device) +{ + int32_t i; + + for (i = 0; i < HDF_ARRAY_SIZE(g_modules); ++i) { + if (g_modules[i].init == NULL) { + HDF_LOGE("module(%d) is no init function", g_modules[i].moduleId); + break; + } + if (g_modules[i].init(device) != HDF_SUCCESS) { + HDF_LOGE("init module(%d) fail", g_modules[i].moduleId); + break; + } + } + if (i < HDF_ARRAY_SIZE(g_modules)) { + for (i = i - 1; i >= 0; --i) { + if (g_modules[i].deinit == NULL) { + continue; + } + g_modules[i].deinit(); + } + return HDF_FAILURE; + } + HDF_LOGE("init softbus module manager success"); + return HDF_SUCCESS; +} + +void SoftbusModuleManagerDeinit(void) +{ + int32_t i; + + for (i = 0; i < HDF_ARRAY_SIZE(g_modules); ++i) { + if (g_modules[i].deinit == NULL) { + continue; + } + g_modules[i].deinit(); + } + HDF_LOGE("deinit softbus module manager"); +} \ No newline at end of file diff --git a/model/misc/dsoftbus/src/wlan_param_monitor.c b/model/misc/dsoftbus/src/wlan_param_monitor.c new file mode 100644 index 0000000000000000000000000000000000000000..07eeb8309a9ce03cffbda2ab6de49d26dae04e8b --- /dev/null +++ b/model/misc/dsoftbus/src/wlan_param_monitor.c @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include "wlan_param_monitor.h" + +#include "hdf_dsoftbus_driver.h" +#include "hdf_log.h" +#include "module_manager.h" +#include "osal_time.h" +#include "osal_timer.h" + +#define HDF_LOG_TAG "wlan_param_monitor" + +#define WLAN_PARAM_REPORT_INTERVAL 1000 + +typedef enum { + CMD_START_MONITOR = 0, + CMD_STOP_MONITOR, + CMD_MAX_INDEX +} Command; + +typedef enum { + EVENT_WLAN_PARAM = 0, + EVENT_MAX_INDEX +} Event; + +typedef struct { + OSAL_DECLARE_TIMER(timer); + bool isTimerStart; +} WlanParamMonitorCtrl; + +typedef struct { + uint32_t event; + uint32_t value; +} ReportInfo; + +static WlanParamMonitorCtrl g_wlanParamMonitorCtrl = { + .isTimerStart = false, +}; + +static void WlanParamReportTimer(uintptr_t arg) +{ + ReportInfo info; + struct HdfSBuf *data = NULL; + + (void)arg; + info.event = EVENT_WLAN_PARAM; + info.value = (uint32_t)OsalGetSysTimeMs(); + data = HdfSBufObtainDefaultSize(); + if (data == NULL) { + HDF_LOGE("get sbuf fail"); + return; + } + if (!HdfSbufWriteBuffer(data, (const void *)&info, sizeof(info))) { + HDF_LOGE("sbuf write report value fail"); + HdfSBufRecycle(data); + return; + } + HdfSoftbusBroadcastEvent(SOFTBUS_MODULE_WLAN_PARAM_MONITOR, data); + HdfSBufRecycle(data); +} + +static void ProcessStartMonitor(void) +{ + if (g_wlanParamMonitorCtrl.isTimerStart) { + HDF_LOGE("wlan param monitor timer is already started"); + return; + } + if (OsalTimerCreate(&g_wlanParamMonitorCtrl.timer, WLAN_PARAM_REPORT_INTERVAL, + WlanParamReportTimer, 0) != HDF_SUCCESS) { + HDF_LOGE("create wlan param monitor timer fail"); + return; + } + if (OsalTimerStartLoop(&g_wlanParamMonitorCtrl.timer) != HDF_SUCCESS) { + OsalTimerDelete(&g_wlanParamMonitorCtrl.timer); + HDF_LOGE("start wlan param monitor timer fail"); + return; + } + g_wlanParamMonitorCtrl.isTimerStart = true; +} + +static void ProcessStopMonitor(void) +{ + if (!g_wlanParamMonitorCtrl.isTimerStart) { + HDF_LOGE("wlan param monitor timer is not started"); + return; + } + if (OsalTimerDelete(&g_wlanParamMonitorCtrl.timer) != HDF_SUCCESS) { + HDF_LOGE("delete wlan param monitor timer fail"); + } else { + g_wlanParamMonitorCtrl.isTimerStart = false; + } +} + +int32_t SoftbusWlanParamMonitorInit(struct HdfDeviceObject *device) +{ + (void)device; + HDF_LOGI("SoftbusWlanParamMonitorInit init"); + return HDF_SUCCESS; +} + +void SoftbusWlanParamMonitorDeinit(void) +{ + if (g_wlanParamMonitorCtrl.isTimerStart) { + ProcessStartMonitor(); + } +} + +void SoftbusWlanParamMonitorProcess(const struct HdfSBuf *reqData, struct HdfSBuf *rspData) +{ + uint32_t cmd; + const void *data = NULL; + uint32_t dataSize; + + (void)rspData; + if (reqData == NULL) { + HDF_LOGE("reqData is null"); + return; + } + if (!HdfSbufReadBuffer((struct HdfSBuf *)reqData, &data, &dataSize)) { + HDF_LOGE("read command fail"); + return; + } + cmd = *((uint32_t *)data); + HDF_LOGI("process command: %d", cmd); + switch (cmd) { + case CMD_START_MONITOR: + ProcessStartMonitor(); + break; + case CMD_STOP_MONITOR: + ProcessStopMonitor(); + break; + default: + break; + } +} \ No newline at end of file