From 0ccb3b8fd26f28fd9445e31fff794aa7e370bbcd Mon Sep 17 00:00:00 2001 From: yinshuqing Date: Wed, 22 Dec 2021 15:08:22 +0800 Subject: [PATCH] test: Test case splitting Signed-off-by: yinshuqing --- support/platform/src/pwm/pwm_if.c | 1 + .../unittest/platform/common/pwm_diver_test.c | 132 +++++ test/unittest/platform/common/pwm_test.c | 453 +++++++++--------- test/unittest/platform/common/pwm_test.h | 18 +- .../platform/entry/hdf_pwm_entry_test.c | 17 +- 5 files changed, 381 insertions(+), 240 deletions(-) create mode 100755 test/unittest/platform/common/pwm_diver_test.c diff --git a/support/platform/src/pwm/pwm_if.c b/support/platform/src/pwm/pwm_if.c index 1a3c9c3cf..5d6eac91e 100755 --- a/support/platform/src/pwm/pwm_if.c +++ b/support/platform/src/pwm/pwm_if.c @@ -175,6 +175,7 @@ int32_t PwmSetConfig(DevHandle handle, struct PwmConfig *config) ret = PwmDeviceSetConfig(handle, config); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: PwmSetConfig failed, ret: %d", __func__, ret); + return HDF_FAILURE; } HDF_LOGI("%s: success.", __func__); diff --git a/test/unittest/platform/common/pwm_diver_test.c b/test/unittest/platform/common/pwm_diver_test.c new file mode 100755 index 000000000..a2c4c07c8 --- /dev/null +++ b/test/unittest/platform/common/pwm_diver_test.c @@ -0,0 +1,132 @@ +/* + * 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 "pwm_test.h" +#include "device_resource_if.h" +#include "hdf_base.h" +#include "hdf_device_desc.h" +#include "hdf_log.h" + +#define HDF_LOG_TAG pwm_driver_test_c + +static struct PwmTestConfig g_config; + +static int32_t PwmTestDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + if (cmd == 0) { + if (reply == NULL) { + HDF_LOGE("%s: reply is null!", __func__); + return HDF_ERR_INVALID_PARAM; + } + if (!HdfSbufWriteBuffer(reply, &g_config, sizeof(g_config))) { + HDF_LOGE("%s: write reply failed", __func__); + return HDF_ERR_IO; + } + } else { + return HDF_ERR_NOT_SUPPORT; + } + + return HDF_SUCCESS; +} + +static int32_t PwmTestReadConfig(struct PwmTestConfig *config, const struct DeviceResourceNode *node) +{ + int32_t ret; + struct DeviceResourceIface *drsOps = NULL; + uint32_t temp; + + drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (drsOps == NULL) { + HDF_LOGE("%s: drsOps is null.", __func__); + return HDF_FAILURE; + } + + if (drsOps->GetUint32 == NULL) { + HDF_LOGE("%s: GetUint32 not support.", __func__); + return HDF_ERR_NOT_SUPPORT; + } + + ret = drsOps->GetUint32(node, "num", &(config->num), 0); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: read num fail.", __func__); + return HDF_FAILURE; + } + + ret = drsOps->GetUint32(node, "period", &(config->cfg.period), 0); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: read period fail.", __func__); + return HDF_FAILURE; + } + + ret = drsOps->GetUint32(node, "duty", &(config->cfg.duty), 0); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: read duty fail.", __func__); + return HDF_FAILURE; + } + + ret = drsOps->GetUint32(node, "polarity", &(temp), 0); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: read polarity fail.", __func__); + return HDF_FAILURE; + } + config->cfg.polarity = temp; + + ret = drsOps->GetUint32(node, "status", &(temp), 0); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: read status fail", __func__); + return HDF_FAILURE; + } + config->cfg.status = temp; + + return HDF_SUCCESS; +} + +static int32_t PwmTestBind(struct HdfDeviceObject *device) +{ + int32_t ret; + static struct IDeviceIoService service; + + if (device == NULL || device->property == NULL) { + HDF_LOGE("%s: device or config is null!", __func__); + return HDF_ERR_IO; + } + + ret = PwmTestReadConfig(&g_config, device->property); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: read config failed", __func__); + return ret; + } + + service.Dispatch = PwmTestDispatch; + device->service = &service; + return HDF_SUCCESS; +} + +static int32_t PwmTestInit(struct HdfDeviceObject *device) +{ + (void)device; + return HDF_SUCCESS; +} + +static void PwmTestRelease(struct HdfDeviceObject *device) +{ + if (device != NULL) { + device->service = NULL; + } + HDF_LOGI("%s: Done!", __func__); + return; +} + +struct HdfDriverEntry g_pwmTestEntry = { + .moduleVersion = 1, + .Bind = PwmTestBind, + .Init = PwmTestInit, + .Release = PwmTestRelease, + .moduleName = "PLATFORM_PWM_TEST", +}; +HDF_INIT(g_pwmTestEntry); \ No newline at end of file diff --git a/test/unittest/platform/common/pwm_test.c b/test/unittest/platform/common/pwm_test.c index d0340de49..14e6923fc 100644 --- a/test/unittest/platform/common/pwm_test.c +++ b/test/unittest/platform/common/pwm_test.c @@ -8,63 +8,134 @@ #include "pwm_test.h" #include "device_resource_if.h" +#include "hdf_io_service_if.h" #include "hdf_base.h" #include "hdf_log.h" +#include "osal_mem.h" #include "osal_time.h" +#include "securec.h" #define HDF_LOG_TAG pwm_test #define SEQ_OUTPUT_DELAY 100 /* Delay time of sequential output, unit: ms */ #define OUTPUT_WAVES_DELAY 1 /* Delay time of waves output, unit: second */ -#define TEST_WAVES_NUMBER 10 /* The number of waves for test. */ +#define TEST_WAVES_NUMBER 10 /* The number of waves for tester. */ -struct PwmTestFunc { - enum PwmTestCmd type; - int32_t (*Func)(struct PwmTest *test); -}; - -static DevHandle PwmTestGetHandle(struct PwmTest *test) +static int32_t PwmTesterGetConfig(struct PwmTestConfig *config) { - return PwmOpen(test->num); + int32_t ret; + struct HdfSBuf *reply = NULL; + struct HdfIoService *service = NULL; + const void *buf = NULL; + uint32_t len; + + HDF_LOGD("%s: enter", __func__); + service = HdfIoServiceBind("PWM_TEST"); + if (service == NULL) { + return HDF_ERR_NOT_SUPPORT; + } + + reply = HdfSBufObtain(sizeof(*config) + sizeof(uint64_t)); + if (reply == NULL) { + HDF_LOGE("%s: failed to obtain reply", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + ret = service->dispatcher->Dispatch(&service->object, 0, NULL, reply); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: remote dispatch failed", __func__); + return ret; + } + + if (!HdfSbufReadBuffer(reply, &buf, &len)) { + HDF_LOGE("%s: read buf failed", __func__); + HdfSBufRecycle(reply); + return HDF_ERR_IO; + } + + if (len != sizeof(*config)) { + HDF_LOGE("%s: config size:%zu, read size:%u", __func__, sizeof(*config), len); + HdfSBufRecycle(reply); + return HDF_ERR_IO; + } + + if (memcpy_s(config, sizeof(*config), buf, sizeof(*config)) != EOK) { + HDF_LOGE("%s: memcpy buf failed", __func__); + HdfSBufRecycle(reply); + return HDF_ERR_IO; + } + + HdfSBufRecycle(reply); + HDF_LOGD("%s: exit", __func__); + return HDF_SUCCESS; } -static void PwmTestReleaseHandle(DevHandle handle) +struct PwmTester *PwmTesterGet(void) { - if (handle == NULL) { - HDF_LOGE("%s: pwm handle is null.", __func__); - return; + int32_t ret; + static struct PwmTester tester; + static bool hasInit = false; + + HDF_LOGE("%s: enter", __func__); + if (hasInit) { + return &tester; + } + + ret = PwmTesterGetConfig(&tester.config); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: read config failed:%d", __func__, ret); + return NULL; } - PwmClose(handle); + + tester.handle = PwmOpen(tester.config.num); + if (tester.handle == NULL) { + HDF_LOGE("%s: open adc device:%u failed", __func__, tester.config.num); + return NULL; + } + + hasInit = true; + HDF_LOGI("%s: done", __func__); + return &tester; } -static int32_t PwmSetConfigTest(struct PwmTest *test) +static int32_t PwmSetConfigTest(void) { int32_t ret; + struct PwmTester *tester = NULL; struct PwmConfig cfg = {0}; uint32_t number; HDF_LOGI("%s: enter. Test [PwmSetConfig].", __func__); - number = test->cfg.number; - test->cfg.number = ((number > 0) ? 0 : TEST_WAVES_NUMBER); - HDF_LOGI("%s: Set number %u.", __func__, test->cfg.number); - ret = PwmSetConfig(test->handle, &(test->cfg)); + tester = PwmTesterGet(); + if (tester == NULL) { + HDF_LOGE("%s: get tester failed", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + number = tester->config.cfg.number; + tester->config.cfg.number = ((number > 0) ? 0 : TEST_WAVES_NUMBER); + HDF_LOGI("%s: Set number %u.", __func__, tester->config.cfg.number); + ret = PwmSetConfig(tester->handle, &(tester->config.cfg)); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmSetConfig] failed, ret %d.", __func__, ret); return HDF_FAILURE; } + OsalSleep(OUTPUT_WAVES_DELAY); - test->cfg.number = number; - HDF_LOGI("%s: Set number %u.", __func__, test->cfg.number); - ret = PwmSetConfig(test->handle, &(test->cfg)); + tester->config.cfg.number = number; + HDF_LOGI("%s: Set number %u.", __func__, tester->config.cfg.number); + ret = PwmSetConfig(tester->handle, &(tester->config.cfg)); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmSetConfig] failed, ret %d.", __func__, ret); return HDF_FAILURE; } - ret = PwmGetConfig(test->handle, &cfg); + + ret = PwmGetConfig(tester->handle, &cfg); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmGetConfig] failed, ret %d.", __func__, ret); return HDF_FAILURE; } - if (memcmp(&cfg, &(test->cfg), sizeof(cfg)) != 0) { + + if (memcmp(&cfg, &(tester->config.cfg), sizeof(cfg)) != 0) { HDF_LOGE("%s: [memcmp] failed.", __func__); return HDF_FAILURE; } @@ -73,162 +144,224 @@ static int32_t PwmSetConfigTest(struct PwmTest *test) return ret; } -static int32_t PwmGetConfigTest(struct PwmTest *test) +static int32_t PwmGetConfigTest(void) { int32_t ret; + struct PwmTester *tester = NULL; struct PwmConfig cfg = {0}; HDF_LOGI("%s: enter. Test [PwmGetConfig].", __func__); - ret = PwmGetConfig(test->handle, &cfg); + tester = PwmTesterGet(); + if (tester == NULL) { + HDF_LOGE("%s: get tester failed", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + ret = PwmGetConfig(tester->handle, &cfg); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmGetConfig] failed, ret %d.", __func__, ret); return HDF_FAILURE; } + HDF_LOGI("%s: success.", __func__); return ret; } -static int32_t PwmSetPeriodTest(struct PwmTest *test) +static int32_t PwmSetPeriodTest(void) { int32_t ret; + struct PwmTester *tester = NULL; struct PwmConfig cfg = {0}; uint32_t period; - period = test->cfg.period + test->originCfg.period; + tester = PwmTesterGet(); + if (tester == NULL) { + HDF_LOGE("%s: get tester failed", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + period = tester->config.cfg.period + tester->config.originCfg.period; HDF_LOGI("%s: enter. Test [PwmSetPeriod] period %u.", __func__, period); - ret = PwmSetPeriod(test->handle, period); + ret = PwmSetPeriod(tester->handle, period); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmSetPeriod] failed, ret %d.", __func__, ret); return HDF_FAILURE; } - ret = PwmGetConfig(test->handle, &cfg); + + ret = PwmGetConfig(tester->handle, &cfg); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmGetConfig] failed, ret %d.", __func__, ret); return HDF_FAILURE; } + if (cfg.period != period) { HDF_LOGE("%s: failed.", __func__); + HDF_LOGE("%s: failed.cfg.period:%d period:%d", __func__, cfg.period, period); return HDF_FAILURE; } + HDF_LOGI("%s: success.", __func__); return ret; } -static int32_t PwmSetDutyTest(struct PwmTest *test) +static int32_t PwmSetDutyTest(void) { int32_t ret; + struct PwmTester *tester = NULL; struct PwmConfig cfg = {0}; uint32_t duty; - duty = test->cfg.duty + test->originCfg.duty; + tester = PwmTesterGet(); + if (tester == NULL) { + HDF_LOGE("%s: get tester failed", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + duty = tester->config.cfg.duty+ tester->config.originCfg.duty; HDF_LOGI("%s: enter. Test [PwmSetDuty] duty %u.", __func__, duty); - ret = PwmSetDuty(test->handle, duty); + ret = PwmSetDuty(tester->handle, duty); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmSetDuty] failed, ret %d.", __func__, ret); return HDF_FAILURE; } - ret = PwmGetConfig(test->handle, &cfg); + + ret = PwmGetConfig(tester->handle, &cfg); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmGetConfig] failed, ret %d.", __func__, ret); return HDF_FAILURE; } + if (cfg.duty != duty) { HDF_LOGE("%s: failed.", __func__); return HDF_FAILURE; } + HDF_LOGI("%s: success.", __func__); return ret; } -static int32_t PwmSetPolarityTest(struct PwmTest *test) +static int32_t PwmSetPolarityTest(void) { int32_t ret; + struct PwmTester *tester = NULL; struct PwmConfig cfg = {0}; HDF_LOGI("%s: enter.", __func__); - test->cfg.polarity = PWM_NORMAL_POLARITY; - HDF_LOGI("%s: Test [PwmSetPolarity] polarity %u.", __func__, test->cfg.polarity); - ret = PwmSetPolarity(test->handle, test->cfg.polarity); + tester = PwmTesterGet(); + if (tester == NULL) { + HDF_LOGE("%s: get tester failed", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + tester->config.cfg.polarity = PWM_NORMAL_POLARITY; + HDF_LOGI("%s: Test [PwmSetPolarity] polarity %u.", __func__, tester->config.cfg.polarity); + ret = PwmSetPolarity(tester->handle, tester->config.cfg.polarity); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmSetPolarity] failed, ret %d.", __func__, ret); return HDF_FAILURE; } - test->cfg.polarity = PWM_INVERTED_POLARITY; - HDF_LOGI("%s: Test [PwmSetPolarity] polarity %u.", __func__, test->cfg.polarity); - ret = PwmSetPolarity(test->handle, test->cfg.polarity); + + tester->config.cfg.polarity = PWM_INVERTED_POLARITY; + HDF_LOGI("%s: Test [PwmSetPolarity] polarity %u.", __func__, tester->config.cfg.polarity); + ret = PwmSetPolarity(tester->handle, tester->config.cfg.polarity); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmSetPolarity] failed, ret %d.", __func__, ret); return HDF_FAILURE; } - ret = PwmGetConfig(test->handle, &cfg); + + ret = PwmGetConfig(tester->handle, &cfg); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmGetConfig] failed, ret %d.", __func__, ret); return HDF_FAILURE; } - if (cfg.polarity != test->cfg.polarity) { + + if (cfg.polarity != tester->config.cfg.polarity) { HDF_LOGE("%s: failed.", __func__); return HDF_FAILURE; } + HDF_LOGI("%s: success.", __func__); return ret; } -static int32_t PwmEnableTest(struct PwmTest *test) +static int32_t PwmEnableTest(void) { int32_t ret; + struct PwmTester *tester = NULL; struct PwmConfig cfg = {0}; HDF_LOGI("%s: enter.", __func__); - ret = PwmDisable(test->handle); + tester = PwmTesterGet(); + if (tester == NULL) { + HDF_LOGE("%s: get tester failed", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + ret = PwmDisable(tester->handle); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmDisable] failed, ret %d.", __func__, ret); return HDF_FAILURE; } + HDF_LOGI("%s: Test [PwmEnable] enable.", __func__); - ret = PwmEnable(test->handle); + ret = PwmEnable(tester->handle); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmEnable] failed, ret %d.", __func__, ret); return HDF_FAILURE; } - ret = PwmGetConfig(test->handle, &cfg); + + ret = PwmGetConfig(tester->handle, &cfg); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmGetConfig] failed, ret %d.", __func__, ret); return HDF_FAILURE; } + if (cfg.status == PWM_DISABLE_STATUS) { HDF_LOGE("%s: failed", __func__); return HDF_FAILURE; } + HDF_LOGI("%s: success.", __func__); return ret; } -static int32_t PwmDisableTest(struct PwmTest *test) +static int32_t PwmDisableTest(void) { int32_t ret; + struct PwmTester *tester = NULL; struct PwmConfig cfg = {0}; HDF_LOGI("%s: enter.", __func__); - ret = PwmEnable(test->handle); + tester = PwmTesterGet(); + if (tester == NULL) { + HDF_LOGE("%s: get tester failed", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + ret = PwmEnable(tester->handle); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmEnable] failed, ret %d.", __func__, ret); return HDF_FAILURE; } + HDF_LOGI("%s: Test [PwmDisable] disable.", __func__); - ret = PwmDisable(test->handle); + ret = PwmDisable(tester->handle); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmDisable] failed, ret %d.", __func__, ret); return HDF_FAILURE; } - ret = PwmGetConfig(test->handle, &cfg); + + ret = PwmGetConfig(tester->handle, &cfg); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmGetConfig] failed, ret %d.", __func__, ret); return HDF_FAILURE; } + if (cfg.status == PWM_ENABLE_STATUS) { HDF_LOGE("%s: failed.", __func__); return HDF_FAILURE; } + HDF_LOGI("%s: success.", __func__); return ret; } @@ -236,76 +369,46 @@ static int32_t PwmDisableTest(struct PwmTest *test) #define TEST_PERIOD 2147483647 #define TEST_DUTY 2147483647 #define TEST_POLARITY 10 -static int32_t PwmReliabilityTest(struct PwmTest *test) +static int32_t PwmReliabilityTest(void) { + struct PwmTester *tester = NULL; struct PwmConfig cfg = {0}; - (void)PwmSetConfig(test->handle, &(test->cfg)); - (void)PwmSetConfig(test->handle, NULL); - (void)PwmGetConfig(test->handle, &cfg); - (void)PwmGetConfig(test->handle, NULL); + tester = PwmTesterGet(); + if (tester == NULL) { + HDF_LOGE("%s: get tester failed", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + (void)PwmSetConfig(tester->handle, &(tester->config.cfg)); + (void)PwmSetConfig(tester->handle, NULL); + (void)PwmGetConfig(tester->handle, &cfg); + (void)PwmGetConfig(tester->handle, NULL); - (void)PwmSetPeriod(test->handle, 0); - (void)PwmSetPeriod(test->handle, TEST_PERIOD); + (void)PwmSetPeriod(tester->handle, 0); + (void)PwmSetPeriod(tester->handle, TEST_PERIOD); - (void)PwmSetDuty(test->handle, 0); - (void)PwmSetDuty(test->handle, TEST_DUTY); + (void)PwmSetDuty(tester->handle, 0); + (void)PwmSetDuty(tester->handle, TEST_DUTY); - (void)PwmSetPolarity(test->handle, 0); - (void)PwmSetPolarity(test->handle, TEST_POLARITY); + (void)PwmSetPolarity(tester->handle, 0); + (void)PwmSetPolarity(tester->handle, TEST_POLARITY); - (void)PwmEnable(test->handle); - (void)PwmEnable(test->handle); + (void)PwmEnable(tester->handle); + (void)PwmEnable(tester->handle); - (void)PwmDisable(test->handle); - (void)PwmDisable(test->handle); + (void)PwmDisable(tester->handle); + (void)PwmDisable(tester->handle); HDF_LOGI("%s: success.", __func__); return HDF_SUCCESS; } -static int32_t PwmTestAll(struct PwmTest *test) -{ - int32_t total = 0; - int32_t error = 0; - - if (PwmSetPeriodTest(test) != HDF_SUCCESS) { - error++; - } - total++; - if (PwmSetDutyTest(test) != HDF_SUCCESS) { - error++; - } - total++; - if (PwmSetPolarityTest(test) != HDF_SUCCESS) { - error++; - } - total++; - if (PwmGetConfigTest(test) != HDF_SUCCESS) { - error++; - } - total++; - if (PwmEnableTest(test) != HDF_SUCCESS) { - error++; - } - total++; - if (PwmSetConfigTest(test) != HDF_SUCCESS) { - error++; - } - total++; - if (PwmDisableTest(test) != HDF_SUCCESS) { - error++; - } - total++; - if (PwmReliabilityTest(test) != HDF_SUCCESS) { - error++; - } - total++; - - HDF_LOGI("%s: Pwm Test Total %d Error %d.", __func__, total, error); - return HDF_SUCCESS; -} +struct PwmTestEntry { + int cmd; + int32_t (*func)(void); +}; -static struct PwmTestFunc g_pwmTestFunc[] = { +static struct PwmTestEntry g_entry[] = { { PWM_SET_PERIOD_TEST, PwmSetPeriodTest }, { PWM_SET_DUTY_TEST, PwmSetDutyTest }, { PWM_SET_POLARITY_TEST, PwmSetPolarityTest }, @@ -314,135 +417,49 @@ static struct PwmTestFunc g_pwmTestFunc[] = { { PWM_SET_CONFIG_TEST, PwmSetConfigTest }, { PWM_GET_CONFIG_TEST, PwmGetConfigTest }, { PWM_RELIABILITY_TEST, PwmReliabilityTest }, - { PWM_TEST_ALL, PwmTestAll }, }; -static int32_t PwmTestEntry(struct PwmTest *test, int32_t cmd) +int32_t PwmTestExecute(int cmd) { - int32_t i; + uint32_t i; int32_t ret = HDF_ERR_NOT_SUPPORT; + struct PwmTester *tester = NULL; - if (test == NULL) { - return HDF_ERR_INVALID_OBJECT; + if (cmd > PWM_TEST_CMD_MAX) { + HDF_LOGE("%s: invalid cmd:%d", __func__, cmd); + ret = HDF_ERR_NOT_SUPPORT; + goto __EXIT__; } - OsalMSleep(SEQ_OUTPUT_DELAY); - test->handle = PwmTestGetHandle(test); - if (test->handle == NULL) { - HDF_LOGE("%s: pwm test get handle fail.", __func__); - return HDF_FAILURE; + + tester = PwmTesterGet(); + if (tester == NULL) { + HDF_LOGE("%s: get tester failed", __func__); + return HDF_ERR_INVALID_OBJECT; } + // At first test case. if (cmd == PWM_SET_PERIOD_TEST) { - ret = PwmGetConfig(test->handle, &(test->originCfg)); + ret = PwmGetConfig(tester->handle, &(tester->config.originCfg)); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: [PwmGetConfig] failed, ret %d.", __func__, ret); return HDF_FAILURE; } } - for (i = 0; i < sizeof(g_pwmTestFunc) / sizeof(g_pwmTestFunc[0]); i++) { - if (cmd == g_pwmTestFunc[i].type && g_pwmTestFunc[i].Func != NULL) { - ret = g_pwmTestFunc[i].Func(test); - HDF_LOGI("%s: cmd %d ret %d.", __func__, cmd, ret); - break; + + for (i = 0; i < sizeof(g_entry) / sizeof(g_entry[0]); i++) { + if (g_entry[i].cmd != cmd || g_entry[i].func == NULL) { + continue; } + ret = g_entry[i].func(); + break; } + // At last test case. if (cmd == PWM_DISABLE_TEST) { - PwmSetConfig(test->handle, &(test->originCfg)); + PwmSetConfig(tester->handle, &(tester->config.originCfg)); } - PwmTestReleaseHandle(test->handle); - OsalMSleep(SEQ_OUTPUT_DELAY); +__EXIT__: + HDF_LOGE("[%s][======cmd:%d====ret:%d======]", __func__, cmd, ret); return ret; -} - -static int32_t PwmTestBind(struct HdfDeviceObject *device) -{ - static struct PwmTest test; - - if (device != NULL) { - device->service = &test.service; - } else { - HDF_LOGE("%s: device is NULL.", __func__); - } - HDF_LOGE("%s: success.", __func__); - return HDF_SUCCESS; -} - -static int32_t PwmTestInitFromHcs(struct PwmTest *test, const struct DeviceResourceNode *node) -{ - int32_t ret; - uint32_t tmp; - - struct DeviceResourceIface *face = NULL; - - face = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); - if (face == NULL) { - HDF_LOGE("%s: face is null.", __func__); - return HDF_FAILURE; - } - if (face->GetUint32 == NULL) { - HDF_LOGE("%s: GetUint32 not support.", __func__); - return HDF_ERR_NOT_SUPPORT; - } - ret = face->GetUint32(node, "num", &(test->num), 0); - if (ret != HDF_SUCCESS) { - HDF_LOGE("%s: read num fail.", __func__); - return HDF_FAILURE; - } - ret = face->GetUint32(node, "period", &(test->cfg.period), 0); - if (ret != HDF_SUCCESS) { - HDF_LOGE("%s: read period fail.", __func__); - return HDF_FAILURE; - } - ret = face->GetUint32(node, "duty", &(test->cfg.duty), 0); - if (ret != HDF_SUCCESS) { - HDF_LOGE("%s: read duty fail.", __func__); - return HDF_FAILURE; - } - ret = face->GetUint32(node, "polarity", &tmp, 0); - if (ret != HDF_SUCCESS) { - HDF_LOGE("%s: read polarity fail.", __func__); - return HDF_FAILURE; - } - test->cfg.polarity = tmp; - ret = face->GetUint32(node, "status", &tmp, 0); - if (ret != HDF_SUCCESS) { - HDF_LOGE("%s: read status fail", __func__); - return HDF_FAILURE; - } - test->cfg.status = tmp; - return HDF_SUCCESS; -} - -static int32_t PwmTestInit(struct HdfDeviceObject *device) -{ - struct PwmTest *test = NULL; - - if (device == NULL || device->service == NULL || device->property == NULL) { - HDF_LOGE("%s: invalid parameter.", __func__); - return HDF_ERR_INVALID_PARAM; - } - test = (struct PwmTest *)device->service; - if (PwmTestInitFromHcs(test, device->property) != HDF_SUCCESS) { - return HDF_FAILURE; - } - HDF_LOGE("%s: success.", __func__); - test->TestEntry = PwmTestEntry; - return HDF_SUCCESS; -} - -static void PwmTestRelease(struct HdfDeviceObject *device) -{ - (void)device; - HDF_LOGE("%s: success.", __func__); -} - -struct HdfDriverEntry g_pwmTestEntry = { - .moduleVersion = 1, - .Bind = PwmTestBind, - .Init = PwmTestInit, - .Release = PwmTestRelease, - .moduleName = "PLATFORM_PWM_TEST", -}; -HDF_INIT(g_pwmTestEntry); +} \ No newline at end of file diff --git a/test/unittest/platform/common/pwm_test.h b/test/unittest/platform/common/pwm_test.h index 35cfddddd..fad0315f4 100644 --- a/test/unittest/platform/common/pwm_test.h +++ b/test/unittest/platform/common/pwm_test.h @@ -12,6 +12,8 @@ #include "hdf_device_desc.h" #include "pwm_if.h" +int32_t PwmTestExecute(int cmd); + enum PwmTestCmd { PWM_SET_PERIOD_TEST = 0, PWM_SET_DUTY_TEST, @@ -21,22 +23,18 @@ enum PwmTestCmd { PWM_SET_CONFIG_TEST, PWM_GET_CONFIG_TEST, PWM_RELIABILITY_TEST, - PWM_TEST_ALL, + PWM_TEST_CMD_MAX, }; -struct PwmTest { - struct IDeviceIoService service; - struct HdfDeviceObject *device; - int32_t (*TestEntry)(struct PwmTest *test, int32_t cmd); +struct PwmTestConfig { uint32_t num; struct PwmConfig cfg; struct PwmConfig originCfg; - DevHandle handle; }; -static inline struct PwmTest *GetPwmTest(void) -{ - return (struct PwmTest *)DevSvcManagerClntGetService("PWM_TEST"); -} +struct PwmTester { + struct PwmTestConfig config; + DevHandle handle; +}; #endif /* PWM_TEST_H */ diff --git a/test/unittest/platform/entry/hdf_pwm_entry_test.c b/test/unittest/platform/entry/hdf_pwm_entry_test.c index 8bb761a7c..b7a51fca2 100644 --- a/test/unittest/platform/entry/hdf_pwm_entry_test.c +++ b/test/unittest/platform/entry/hdf_pwm_entry_test.c @@ -14,19 +14,12 @@ int32_t HdfPwmUnitTestEntry(HdfTestMsg *msg) { - struct PwmTest *test = NULL; + HDF_LOGD("%s: enter", __func__); if (msg == NULL) { - HDF_LOGE("%s: msg is NULL!", __func__); return HDF_FAILURE; } - test = GetPwmTest(); - if (test == NULL || test->TestEntry == NULL) { - HDF_LOGE("%s: test or TestEntry is NULL!", __func__); - msg->result = HDF_FAILURE; - return HDF_FAILURE; - } - HDF_LOGI("%s: call [TestEntry]", __func__); - msg->result = test->TestEntry(test, msg->subCmd); - return msg->result; -} + + msg->result = PwmTestExecute(msg->subCmd); + return HDF_SUCCESS; +} \ No newline at end of file -- Gitee