add user api support for uart

Signed-off-by: haizhouyang <yanghaizhou1@huawei.com>
This commit is contained in:
haizhouyang
2022-02-09 14:17:03 +08:00
parent e636906d3a
commit 332d09e913
11 changed files with 1082 additions and 724 deletions
-4
View File
@@ -33,9 +33,7 @@
#include "platform_if.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */
/**
@@ -367,9 +365,7 @@ int32_t UartSetAttribute(DevHandle handle, struct UartAttribute *attribute);
int32_t UartSetTransMode(DevHandle handle, enum UartTransMode mode);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif /* PAL_UART_IF_H */
+5 -5
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2021 Huawei Device Co., Ltd.
* Copyright (c) 2020-2022 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.
@@ -11,13 +11,12 @@
#include "hdf_base.h"
#include "hdf_device_desc.h"
#include "hdf_sbuf.h"
#include "osal_atomic.h"
#include "uart_if.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */
/**
@@ -142,10 +141,11 @@ static inline int32_t UartHostPollEvent(struct UartHost *host, void *filep, void
return host->method->pollEvent(host, filep, table);
}
int32_t UartIoDispatch(struct HdfDeviceIoClient *client, int cmd,
struct HdfSBuf *data, struct HdfSBuf *reply);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif /* UART_CORE_H */
+6 -158
View File
@@ -11,16 +11,18 @@
#include "osal_mem.h"
#include "uart_if.h"
#define HDF_LOG_TAG uart_core
#define HDF_LOG_TAG uart_core_c
int32_t UartHostInit(struct UartHost *host)
{
int32_t ret;
if (host == NULL || host->method == NULL) {
HDF_LOGE("%s: host or method is NULL", __func__);
return HDF_ERR_INVALID_PARAM;
}
if (OsalAtomicRead(&host->atom) == 1) {
HDF_LOGE("%s: device is busy", __func__);
return HDF_ERR_DEVICE_BUSY;
}
OsalAtomicInc(&host->atom);
@@ -28,6 +30,7 @@ int32_t UartHostInit(struct UartHost *host)
ret = host->method->Init(host);
if (ret != HDF_SUCCESS) {
OsalAtomicDec(&host->atom);
HDF_LOGE("%s: host init failed", __func__);
return ret;
}
}
@@ -37,13 +40,14 @@ int32_t UartHostInit(struct UartHost *host)
int32_t UartHostDeinit(struct UartHost *host)
{
int32_t ret;
if (host == NULL || host->method == NULL) {
HDF_LOGE("%s: host or method is NULL", __func__);
return HDF_ERR_INVALID_PARAM;
}
if (host->method->Deinit != NULL) {
ret = host->method->Deinit(host);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: host deinit failed", __func__);
return ret;
}
}
@@ -51,162 +55,6 @@ int32_t UartHostDeinit(struct UartHost *host)
return HDF_SUCCESS;
}
static int32_t UartUserRead(struct UartHost *host, struct HdfSBuf *reply)
{
size_t size;
int32_t len;
uint8_t *buf = NULL;
size = HdfSbufGetCapacity(reply);
if (size == 0) {
HDF_LOGE("%s: the size of Sbuf is 0", __func__);
return HDF_ERR_INVALID_PARAM;
}
buf = (uint8_t *)OsalMemCalloc(sizeof(*buf) * size);
if (buf == NULL) {
HDF_LOGE("%s: OsalMemCalloc error", __func__);
return HDF_ERR_MALLOC_FAIL;
}
len = UartHostRead(host, buf, size);
if (len <= 0) {
HDF_LOGE("%s: UartHostRead error, len is %d", __func__, len);
OsalMemFree(buf);
return len;
}
if (!HdfSbufWriteBuffer(reply, buf, len)) {
HDF_LOGE("%s: sbuf write buffer failed", __func__);
OsalMemFree(buf);
return HDF_ERR_IO;
}
OsalMemFree(buf);
return HDF_SUCCESS;
}
static int32_t UartUserWrite(struct UartHost *host, struct HdfSBuf *data)
{
size_t size;
uint8_t *buf = NULL;
if (!HdfSbufReadBuffer(data, (const void **)&buf, &size)) {
HDF_LOGE("%s: sbuf read buffer failed", __func__);
return HDF_ERR_IO;
}
return UartHostWrite(host, buf, size);
}
static int32_t UartUserGetBaud(struct UartHost *host, struct HdfSBuf *reply)
{
int32_t ret;
uint32_t baudRate;
ret = UartHostGetBaud(host, &baudRate);
if (ret != HDF_SUCCESS) {
return ret;
}
if (!HdfSbufWriteBuffer(reply, &baudRate, sizeof(baudRate))) {
HDF_LOGE("%s: sbuf write buffer failed", __func__);
return HDF_ERR_IO;
}
return HDF_SUCCESS;
}
static int32_t UartUserSetBaud(struct UartHost *host, struct HdfSBuf *data)
{
size_t size;
uint32_t *baudRate = NULL;
if (!HdfSbufReadBuffer(data, (const void **)&baudRate, &size)) {
HDF_LOGE("%s: sbuf read buffer failed", __func__);
return HDF_ERR_IO;
}
return UartHostSetBaud(host, *baudRate);
}
static int32_t UartUserGetAttribute(struct UartHost *host, struct HdfSBuf *reply)
{
int32_t ret;
struct UartAttribute attribute;
ret = UartHostGetAttribute(host, &attribute);
if (ret != HDF_SUCCESS) {
return ret;
}
if (!HdfSbufWriteBuffer(reply, &attribute, sizeof(attribute))) {
HDF_LOGE("%s: sbuf write buffer failed", __func__);
return HDF_ERR_IO;
}
return HDF_SUCCESS;
}
static int32_t UartUserSetAttribute(struct UartHost *host, struct HdfSBuf *data)
{
size_t size;
struct UartAttribute *attribute = NULL;
if (!HdfSbufReadBuffer(data, (const void **)&attribute, &size)) {
HDF_LOGE("%s: sbuf read buffer failed", __func__);
return HDF_ERR_IO;
}
return UartHostSetAttribute(host, attribute);
}
static int32_t UartUserSetTransMode(struct UartHost *host, struct HdfSBuf *data)
{
size_t size;
enum UartTransMode *mode = NULL;
if (!HdfSbufReadBuffer(data, (const void **)&mode, &size) || mode == NULL) {
HDF_LOGE("%s: sbuf read buffer failed", __func__);
return HDF_ERR_IO;
}
return UartHostSetTransMode(host, *mode);
}
static int32_t UartIoDispatch(struct HdfDeviceIoClient *client, int cmd,
struct HdfSBuf *data, struct HdfSBuf *reply)
{
struct UartHost *host = NULL;
if (client == NULL) {
HDF_LOGE("%s: client is NULL", __func__);
return HDF_ERR_INVALID_OBJECT;
}
if (client->device == NULL) {
HDF_LOGE("%s: client->device is NULL", __func__);
return HDF_ERR_INVALID_OBJECT;
}
if (client->device->service == NULL) {
HDF_LOGE("%s: client->device->service is NULL", __func__);
return HDF_ERR_INVALID_OBJECT;
}
host = (struct UartHost *)client->device->service;
switch (cmd) {
case UART_IO_INIT:
return UartHostInit(host);
case UART_IO_DEINIT:
return UartHostDeinit(host);
case UART_IO_READ:
return UartUserRead(host, reply);
case UART_IO_WRITE:
return UartUserWrite(host, data);
case UART_IO_GET_BAUD:
return UartUserGetBaud(host, reply);
case UART_IO_SET_BAUD:
return UartUserSetBaud(host, data);
case UART_IO_GET_ATTRIBUTE:
return UartUserGetAttribute(host, reply);
case UART_IO_SET_ATTRIBUTE:
return UartUserSetAttribute(host, data);
case UART_IO_SET_TRANSMODE:
return UartUserSetTransMode(host, data);
default:
return HDF_ERR_NOT_SUPPORT;
}
}
void UartHostDestroy(struct UartHost *host)
{
if (host == NULL) {
+107 -256
View File
@@ -1,256 +1,107 @@
/*
* Copyright (c) 2020-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 "uart_if.h"
#include "securec.h"
#ifndef __USER__
#include "devsvc_manager_clnt.h"
#else
#include "hdf_io_service_if.h"
#endif
#include "hdf_log.h"
#include "osal_mem.h"
#ifndef __USER__
#include "uart_core.h"
#endif
#define HDF_LOG_TAG uart_if_c
#define UART_HOST_NAME_LEN 32
static void *UartGetObjGetByBusNum(uint32_t num)
{
int ret;
void *obj = NULL;
char *name = NULL;
name = (char *)OsalMemCalloc(UART_HOST_NAME_LEN + 1);
if (name == NULL) {
return NULL;
}
ret = snprintf_s(name, UART_HOST_NAME_LEN + 1, UART_HOST_NAME_LEN,
"HDF_PLATFORM_UART_%u", num);
if (ret < 0) {
HDF_LOGE("%s: snprintf_s failed", __func__);
OsalMemFree(name);
return NULL;
}
#ifdef __USER__
obj = (void *)HdfIoServiceBind(name);
#else
obj = (void *)DevSvcManagerClntGetService(name);
#endif
OsalMemFree(name);
return obj;
}
static void UartPutObjByPointer(const void *obj)
{
if (obj == NULL) {
return;
}
#ifdef __USER__
HdfIoServiceRecycle((struct HdfIoService *)obj);
#endif
};
DevHandle UartOpen(uint32_t port)
{
int32_t ret;
void *handle = NULL;
handle = UartGetObjGetByBusNum(port);
if (handle == NULL) {
HDF_LOGE("%s: get handle error", __func__);
return NULL;
}
#ifdef __USER__
struct HdfIoService *service = (struct HdfIoService *)handle;
if (service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
HDF_LOGE("%s: service is invalid", __func__);
UartPutObjByPointer(handle);
return NULL;
}
ret = service->dispatcher->Dispatch(&service->object, UART_IO_INIT, NULL, NULL);
#else
ret = UartHostInit((struct UartHost *)handle);
#endif
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: UartHostInit error, ret %d", __func__, ret);
UartPutObjByPointer(handle);
return NULL;
}
return (DevHandle)handle;
}
void UartClose(DevHandle handle)
{
int32_t ret;
if (handle == NULL) {
HDF_LOGE("%s: handle is NULL", __func__);
return;
}
#ifdef __USER__
struct HdfIoService *service = (struct HdfIoService *)handle;
if (service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
HDF_LOGE("%s: service is invalid", __func__);
UartPutObjByPointer(handle);
return;
}
ret = service->dispatcher->Dispatch(&service->object, UART_IO_DEINIT, NULL, NULL);
#else
ret = UartHostDeinit((struct UartHost *)handle);
#endif
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: UartHostDeinit error, ret %d", __func__, ret);
}
UartPutObjByPointer(handle);
}
#ifdef __USER__
static int32_t UartUserReceive(DevHandle handle, void *data, uint32_t size, enum UartIoCmd cmd)
{
int32_t ret;
uint32_t rLen;
const void *rBuf = NULL;
struct HdfSBuf *reply = NULL;
struct HdfIoService *service = (struct HdfIoService *)handle;
if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
HDF_LOGE("%s: service is invalid", __func__);
return HDF_ERR_INVALID_PARAM;
}
/* Four bytes are used to store the buffer length, and four bytes are used to align the memory. */
reply = HdfSbufObtain(size + 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, cmd, NULL, reply);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: failed to read, ret %d", __func__, ret);
goto __EXIT;
}
if (!HdfSbufReadBuffer(reply, &rBuf, &rLen)) {
HDF_LOGE("%s: sbuf read buffer failed", __func__);
ret = HDF_ERR_IO;
goto __EXIT;
}
if (size != rLen && cmd != UART_IO_READ) {
HDF_LOGE("%s: read error, size %u, rLen %u", __func__, size, rLen);
ret = HDF_FAILURE;
goto __EXIT;
}
if (memcpy_s(data, size, rBuf, rLen) != EOK) {
HDF_LOGE("%s: memcpy rBuf failed", __func__);
ret = HDF_ERR_IO;
goto __EXIT;
}
if (cmd == UART_IO_READ) {
ret = (int32_t)rLen;
}
__EXIT:
HdfSbufRecycle(reply);
return ret;
}
static int32_t UartUserSend(DevHandle handle, void *data, uint32_t size, enum UartIoCmd cmd)
{
int32_t ret;
struct HdfSBuf *buf = NULL;
struct HdfIoService *service = (struct HdfIoService *)handle;
if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
HDF_LOGE("%s: service is invalid", __func__);
return HDF_ERR_INVALID_PARAM;
}
buf = HdfSbufObtain(size);
if (buf == NULL) {
HDF_LOGE("%s: failed to obtain buf", __func__);
return HDF_ERR_MALLOC_FAIL;
}
if (!HdfSbufWriteBuffer(buf, data, size)) {
HDF_LOGE("%s: sbuf write buffer failed", __func__);
HdfSbufRecycle(buf);
return HDF_ERR_IO;
}
ret = service->dispatcher->Dispatch(&service->object, cmd, buf, NULL);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: failed to write, ret %d", __func__, ret);
}
HdfSbufRecycle(buf);
return ret;
}
#endif
int32_t UartRead(DevHandle handle, uint8_t *data, uint32_t size)
{
#ifdef __USER__
return UartUserReceive(handle, data, size, UART_IO_READ);
#else
return UartHostRead((struct UartHost *)handle, data, size);
#endif
}
int32_t UartWrite(DevHandle handle, uint8_t *data, uint32_t size)
{
#ifdef __USER__
return UartUserSend(handle, data, size, UART_IO_WRITE);
#else
return UartHostWrite((struct UartHost *)handle, data, size);
#endif
}
int32_t UartGetBaud(DevHandle handle, uint32_t *baudRate)
{
#ifdef __USER__
return UartUserReceive(handle, baudRate, sizeof(*baudRate), UART_IO_GET_BAUD);
#else
return UartHostGetBaud((struct UartHost *)handle, baudRate);
#endif
}
int32_t UartSetBaud(DevHandle handle, uint32_t baudRate)
{
#ifdef __USER__
return UartUserSend(handle, &baudRate, sizeof(baudRate), UART_IO_SET_BAUD);
#else
return UartHostSetBaud((struct UartHost *)handle, baudRate);
#endif
}
int32_t UartGetAttribute(DevHandle handle, struct UartAttribute *attribute)
{
#ifdef __USER__
return UartUserReceive(handle, attribute, sizeof(*attribute), UART_IO_GET_ATTRIBUTE);
#else
return UartHostGetAttribute((struct UartHost *)handle, attribute);
#endif
}
int32_t UartSetAttribute(DevHandle handle, struct UartAttribute *attribute)
{
#ifdef __USER__
return UartUserSend(handle, attribute, sizeof(*attribute), UART_IO_SET_ATTRIBUTE);
#else
return UartHostSetAttribute((struct UartHost *)handle, attribute);
#endif
}
int32_t UartSetTransMode(DevHandle handle, enum UartTransMode mode)
{
#ifdef __USER__
return UartUserSend(handle, &mode, sizeof(mode), UART_IO_SET_TRANSMODE);
#else
return UartHostSetTransMode((struct UartHost *)handle, mode);
#endif
}
/*
* Copyright (c) 2020-2022 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 "uart_if.h"
#include "devsvc_manager_clnt.h"
#include "hdf_log.h"
#include "osal_mem.h"
#include "securec.h"
#include "uart_core.h"
#define HDF_LOG_TAG uart_if_c
#define UART_HOST_NAME_LEN 32
static void *UartGetObjGetByBusNum(uint32_t num)
{
int ret;
char name[UART_HOST_NAME_LEN + 1] = { 0 };
ret = snprintf_s(name, UART_HOST_NAME_LEN + 1, UART_HOST_NAME_LEN,
"HDF_PLATFORM_UART_%u", num);
if (ret < 0) {
HDF_LOGE("%s: snprintf_s failed", __func__);
return NULL;
}
return (void *)DevSvcManagerClntGetService(name);
}
static void UartPutObjByPointer(const void *obj)
{
if (obj == NULL) {
return;
}
}
DevHandle UartOpen(uint32_t port)
{
int32_t ret;
void *handle = NULL;
handle = UartGetObjGetByBusNum(port);
if (handle == NULL) {
HDF_LOGE("%s: get handle error", __func__);
return NULL;
}
ret = UartHostInit((struct UartHost *)handle);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: UartHostInit error, ret %d", __func__, ret);
UartPutObjByPointer(handle);
return NULL;
}
return (DevHandle)handle;
}
void UartClose(DevHandle handle)
{
int32_t ret;
if (handle == NULL) {
HDF_LOGE("%s: handle is NULL", __func__);
return;
}
ret = UartHostDeinit((struct UartHost *)handle);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: UartHostDeinit error, ret %d", __func__, ret);
}
UartPutObjByPointer(handle);
}
int32_t UartRead(DevHandle handle, uint8_t *data, uint32_t size)
{
return UartHostRead((struct UartHost *)handle, data, size);
}
int32_t UartWrite(DevHandle handle, uint8_t *data, uint32_t size)
{
return UartHostWrite((struct UartHost *)handle, data, size);
}
int32_t UartGetBaud(DevHandle handle, uint32_t *baudRate)
{
return UartHostGetBaud((struct UartHost *)handle, baudRate);
}
int32_t UartSetBaud(DevHandle handle, uint32_t baudRate)
{
return UartHostSetBaud((struct UartHost *)handle, baudRate);
}
int32_t UartGetAttribute(DevHandle handle, struct UartAttribute *attribute)
{
return UartHostGetAttribute((struct UartHost *)handle, attribute);
}
int32_t UartSetAttribute(DevHandle handle, struct UartAttribute *attribute)
{
return UartHostSetAttribute((struct UartHost *)handle, attribute);
}
int32_t UartSetTransMode(DevHandle handle, enum UartTransMode mode)
{
return UartHostSetTransMode((struct UartHost *)handle, mode);
}
+348
View File
@@ -0,0 +1,348 @@
/*
* Copyright (c) 2022 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 "uart_if.h"
#include "hdf_io_service_if.h"
#include "hdf_log.h"
#include "osal_mem.h"
#include "securec.h"
#define HDF_LOG_TAG uart_if_u_c
#define UART_HOST_NAME_LEN 32
static void *UartGetObjGetByBusNum(uint32_t num)
{
int ret;
char name[UART_HOST_NAME_LEN + 1] = { 0 };
ret = snprintf_s(name, UART_HOST_NAME_LEN + 1, UART_HOST_NAME_LEN,
"HDF_PLATFORM_UART_%u", num);
if (ret < 0) {
HDF_LOGE("%s: snprintf_s failed", __func__);
return NULL;
}
return (void *)HdfIoServiceBind(name);
}
static void UartPutObjByPointer(const void *obj)
{
if (obj == NULL) {
return;
}
HdfIoServiceRecycle((struct HdfIoService *)obj);
};
DevHandle UartOpen(uint32_t port)
{
int32_t ret;
void *handle = NULL;
handle = UartGetObjGetByBusNum(port);
if (handle == NULL) {
HDF_LOGE("%s: get handle error", __func__);
return NULL;
}
struct HdfIoService *service = (struct HdfIoService *)handle;
if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
HDF_LOGE("%s: service is invalid", __func__);
UartPutObjByPointer(handle);
return NULL;
}
ret = service->dispatcher->Dispatch(&service->object, UART_IO_INIT, NULL, NULL);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: UartHostInit error, ret %d", __func__, ret);
UartPutObjByPointer(handle);
return NULL;
}
return (DevHandle)handle;
}
void UartClose(DevHandle handle)
{
int32_t ret;
if (handle == NULL) {
HDF_LOGE("%s: handle is NULL", __func__);
return;
}
struct HdfIoService *service = (struct HdfIoService *)handle;
if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
HDF_LOGE("%s: service is invalid", __func__);
UartPutObjByPointer(handle);
return;
}
ret = service->dispatcher->Dispatch(&service->object, UART_IO_DEINIT, NULL, NULL);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: UartHostDeinit error, ret %d", __func__, ret);
}
UartPutObjByPointer(handle);
}
static int32_t UartDispatch(DevHandle handle, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
{
int32_t ret;
struct HdfIoService *service = (struct HdfIoService *)handle;
if (service == NULL) {
HDF_LOGE("%s: service is null", __func__);
return HDF_ERR_INVALID_OBJECT;
}
if (service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
HDF_LOGE("%s: dispatcher is null", __func__);
return HDF_ERR_INVALID_PARAM;
}
ret = service->dispatcher->Dispatch(&service->object, cmd, data, reply);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: Dispatch failed: %d", __func__, ret);
}
return ret;
}
int32_t UartRead(DevHandle handle, uint8_t *buf, uint32_t len)
{
int32_t ret;
struct HdfSBuf *data = NULL;
struct HdfSBuf *reply = NULL;
uint32_t tmpLen;
const void *tmpBuf = NULL;
if (buf == NULL || len == 0) {
return HDF_ERR_INVALID_PARAM;
}
data = HdfSbufObtainDefaultSize();
if (data == NULL) {
HDF_LOGE("%s: failed to obtain data buf", __func__);
return HDF_ERR_MALLOC_FAIL;
}
if (!HdfSbufWriteUint32(data, len)) {
HDF_LOGE("%s: write read size failed!", __func__);
HdfSbufRecycle(data);
return HDF_ERR_IO;
}
reply = HdfSbufObtain(len + sizeof(uint64_t));
if (reply == NULL) {
HDF_LOGE("%s: failed to obtain reply buf", __func__);
HdfSbufRecycle(data);
return HDF_ERR_MALLOC_FAIL;
}
ret = UartDispatch(handle, UART_IO_READ, data, reply);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: UartDispatch failed: %d", __func__, ret);
goto EXIT;
}
if (!HdfSbufReadBuffer(reply, &tmpBuf, &tmpLen)) {
HDF_LOGE("%s: sbuf read buffer failed", __func__);
ret = HDF_ERR_IO;
goto EXIT;
}
if (tmpLen > 0 && memcpy_s(buf, len, tmpBuf, tmpLen) != EOK) {
HDF_LOGE("%s: memcpy buf failed", __func__);
ret = HDF_ERR_IO;
goto EXIT;
}
ret = tmpLen;
EXIT:
HdfSbufRecycle(data);
HdfSbufRecycle(reply);
return ret;
}
int32_t UartWrite(DevHandle handle, uint8_t *buf, uint32_t len)
{
int32_t ret;
struct HdfSBuf *data = NULL;
if (buf == NULL || len == 0) {
return HDF_ERR_INVALID_PARAM;
}
data = HdfSbufObtainDefaultSize();
if (data == NULL) {
HDF_LOGE("%s: failed to obtain write buf", __func__);
return HDF_ERR_MALLOC_FAIL;
}
if (!HdfSbufWriteBuffer(data, buf, len)) {
HDF_LOGE("%s: sbuf write buffer failed", __func__);
HdfSbufRecycle(data);
return HDF_ERR_IO;
}
ret = UartDispatch(handle, UART_IO_WRITE, data, NULL);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: UartDispatch failed: %d", __func__, ret);
}
HdfSbufRecycle(data);
return ret;
}
int32_t UartGetBaud(DevHandle handle, uint32_t *baudRate)
{
int32_t ret;
struct HdfSBuf *reply = NULL;
if (baudRate == NULL) {
return HDF_ERR_INVALID_PARAM;
}
reply = HdfSbufObtainDefaultSize();
if (reply == NULL) {
HDF_LOGE("%s: failed to obtain reply buf", __func__);
return HDF_ERR_MALLOC_FAIL;
}
ret = UartDispatch(handle, UART_IO_GET_BAUD, NULL, reply);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: failed to write, ret %d", __func__, ret);
HdfSbufRecycle(reply);
return ret;
}
if (!HdfSbufReadUint32(reply, baudRate)) {
HDF_LOGE("%s: read from reply failed", __func__);
ret = HDF_ERR_IO;
}
HdfSbufRecycle(reply);
return ret;
}
int32_t UartSetBaud(DevHandle handle, uint32_t baudRate)
{
int32_t ret;
struct HdfSBuf *data = NULL;
data = HdfSbufObtainDefaultSize();
if (data == NULL) {
HDF_LOGE("%s: failed to obtain data buf", __func__);
return HDF_ERR_MALLOC_FAIL;
}
if (!HdfSbufWriteUint32(data, baudRate)) {
HDF_LOGE("%s: sbuf write baud rate failed", __func__);
HdfSbufRecycle(data);
return HDF_ERR_IO;
}
ret = UartDispatch(handle, UART_IO_SET_BAUD, data, NULL);
HdfSbufRecycle(data);
return ret;
}
int32_t UartGetAttribute(DevHandle handle, struct UartAttribute *attribute)
{
int32_t ret;
struct HdfSBuf *reply = NULL;
uint32_t tmpLen;
const void *tmpBuf = NULL;
if (attribute == NULL) {
return HDF_ERR_INVALID_PARAM;
}
reply = HdfSbufObtainDefaultSize();
if (reply == NULL) {
HDF_LOGE("%s: failed to obtain reply buf", __func__);
return HDF_ERR_MALLOC_FAIL;
}
ret = UartDispatch(handle, UART_IO_GET_ATTRIBUTE, NULL, reply);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: UartDispatch failed: %d", __func__, ret);
HdfSbufRecycle(reply);
return HDF_ERR_IO;
}
if (!HdfSbufReadBuffer(reply, &tmpBuf, &tmpLen)) {
HDF_LOGE("%s: sbuf read buffer failed", __func__);
HdfSbufRecycle(reply);
return HDF_ERR_IO;
}
if (tmpLen != sizeof(*attribute)) {
HDF_LOGE("%s: reply data len not match, exp:%zu, got:%u",
__func__, sizeof(*attribute), tmpLen);
HdfSbufRecycle(reply);
return HDF_ERR_IO;
}
if (memcpy_s(attribute, sizeof(*attribute), tmpBuf, tmpLen) != EOK) {
HDF_LOGE("%s: memcpy buf failed", __func__);
HdfSbufRecycle(reply);
return HDF_ERR_IO;
}
HdfSbufRecycle(reply);
return HDF_SUCCESS;
}
int32_t UartSetAttribute(DevHandle handle, struct UartAttribute *attribute)
{
int32_t ret;
struct HdfSBuf *data = NULL;
if (attribute == NULL) {
return HDF_ERR_INVALID_PARAM;
}
data = HdfSbufObtainDefaultSize();
if (data == NULL) {
HDF_LOGE("%s: failed to obtain write buf", __func__);
return HDF_ERR_MALLOC_FAIL;
}
if (!HdfSbufWriteBuffer(data, (void *)attribute, sizeof(attribute))) {
HDF_LOGE("%s: sbuf write attribute failed", __func__);
HdfSbufRecycle(data);
return HDF_ERR_IO;
}
ret = UartDispatch(handle, UART_IO_SET_ATTRIBUTE, data, NULL);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: UartDispatch failed: %d", __func__, ret);
}
HdfSbufRecycle(data);
return ret;
}
int32_t UartSetTransMode(DevHandle handle, enum UartTransMode mode)
{
int32_t ret;
struct HdfSBuf *data = NULL;
data = HdfSbufObtainDefaultSize();
if (data == NULL) {
HDF_LOGE("%s: failed to obtain data buf", __func__);
return HDF_ERR_MALLOC_FAIL;
}
if (!HdfSbufWriteUint32(data, (uint32_t)mode)) {
HDF_LOGE("%s: sbuf write mode failed", __func__);
HdfSbufRecycle(data);
return HDF_ERR_IO;
}
ret = UartDispatch(handle, UART_IO_SET_TRANSMODE, data, NULL);
HdfSbufRecycle(data);
return ret;
}
+184
View File
@@ -0,0 +1,184 @@
/*
* Copyright (c) 2020-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_log.h"
#include "osal_mem.h"
#include "uart_core.h"
#include "uart_if.h"
#define HDF_LOG_TAG uart_servie_c
#define UART_RBUF_MALLOC_SIZE_MAX 65535
static int32_t UartIoRead(struct UartHost *host, struct HdfSBuf *data, struct HdfSBuf *reply)
{
uint32_t len;
int32_t ret;
uint8_t *buf = NULL;
if (!HdfSbufReadUint32(data, &len)) {
HDF_LOGE("%s: sbuf read data len failed", __func__);
return HDF_ERR_IO;
}
if (len == 0 || len >= UART_RBUF_MALLOC_SIZE_MAX) {
HDF_LOGE("%s: invalid buf len:%u", __func__, len);
return HDF_ERR_INVALID_PARAM;
}
buf = (uint8_t *)OsalMemCalloc(len);
if (buf == NULL) {
HDF_LOGE("%s: OsalMemCalloc error", __func__);
return HDF_ERR_MALLOC_FAIL;
}
ret = UartHostRead(host, buf, len);
if (ret < 0) {
HDF_LOGE("%s: Uart host read failed:%d", __func__, ret);
OsalMemFree(buf);
return HDF_ERR_IO;
}
if (!HdfSbufWriteBuffer(reply, (ret == 0) ? NULL : buf, ret)) {
HDF_LOGE("%s: sbuf write buffer failed", __func__);
OsalMemFree(buf);
return HDF_ERR_IO;
}
OsalMemFree(buf);
return HDF_SUCCESS;
}
static int32_t UartIoWrite(struct UartHost *host, struct HdfSBuf *data)
{
size_t size;
uint8_t *buf = NULL;
if (!HdfSbufReadBuffer(data, (const void **)&buf, &size)) {
HDF_LOGE("%s: sbuf read buffer failed", __func__);
return HDF_ERR_IO;
}
return UartHostWrite(host, buf, size);
}
static int32_t UartIoGetBaud(struct UartHost *host, struct HdfSBuf *reply)
{
int32_t ret;
uint32_t baudRate;
ret = UartHostGetBaud(host, &baudRate);
if (ret != HDF_SUCCESS) {
return ret;
}
if (!HdfSbufWriteUint32(reply, baudRate)) {
HDF_LOGE("%s: sbuf write buffer failed", __func__);
return HDF_ERR_IO;
}
return HDF_SUCCESS;
}
static int32_t UartIoSetBaud(struct UartHost *host, struct HdfSBuf *data)
{
uint32_t baudRate;
if (!HdfSbufReadUint32(data, &baudRate)) {
HDF_LOGE("%s: sbuf read buffer failed", __func__);
return HDF_ERR_IO;
}
return UartHostSetBaud(host, baudRate);
}
static int32_t UartIoGetAttribute(struct UartHost *host, struct HdfSBuf *reply)
{
int32_t ret;
struct UartAttribute attribute;
ret = UartHostGetAttribute(host, &attribute);
if (ret != HDF_SUCCESS) {
return ret;
}
if (!HdfSbufWriteBuffer(reply, &attribute, sizeof(attribute))) {
HDF_LOGE("%s: sbuf write buffer failed", __func__);
return HDF_ERR_IO;
}
return HDF_SUCCESS;
}
static int32_t UartIoSetAttribute(struct UartHost *host, struct HdfSBuf *data)
{
uint32_t size;
struct UartAttribute *attribute = NULL;
if (!HdfSbufReadBuffer(data, (const void **)&attribute, &size)) {
HDF_LOGE("%s: sbuf read buffer failed", __func__);
return HDF_ERR_IO;
}
if (size != sizeof(*attribute)) {
HDF_LOGE("%s: sbuf read size not match, exp:%zu, got:%u",
__func__, sizeof(*attribute), size);
return HDF_ERR_IO;
}
return UartHostSetAttribute(host, attribute);
}
static int32_t UartIoSetTransMode(struct UartHost *host, struct HdfSBuf *data)
{
uint32_t mode;
if (!HdfSbufReadUint32(data, &mode)) {
HDF_LOGE("%s: sbuf read mode failed", __func__);
return HDF_ERR_IO;
}
return UartHostSetTransMode(host, mode);
}
int32_t UartIoDispatch(struct HdfDeviceIoClient *client, int cmd,
struct HdfSBuf *data, struct HdfSBuf *reply)
{
struct UartHost *host = NULL;
if (client == NULL) {
HDF_LOGE("%s: client is NULL", __func__);
return HDF_ERR_INVALID_OBJECT;
}
if (client->device == NULL) {
HDF_LOGE("%s: client->device is NULL", __func__);
return HDF_ERR_INVALID_OBJECT;
}
if (client->device->service == NULL) {
HDF_LOGE("%s: client->device->service is NULL", __func__);
return HDF_ERR_INVALID_OBJECT;
}
host = (struct UartHost *)client->device->service;
switch (cmd) {
case UART_IO_INIT:
return UartHostInit(host);
case UART_IO_DEINIT:
return UartHostDeinit(host);
case UART_IO_READ:
return UartIoRead(host, data, reply);
case UART_IO_WRITE:
return UartIoWrite(host, data);
case UART_IO_GET_BAUD:
return UartIoGetBaud(host, reply);
case UART_IO_SET_BAUD:
return UartIoSetBaud(host, data);
case UART_IO_GET_ATTRIBUTE:
return UartIoGetAttribute(host, reply);
case UART_IO_SET_ATTRIBUTE:
return UartIoSetAttribute(host, data);
case UART_IO_SET_TRANSMODE:
return UartIoSetTransMode(host, data);
default:
return HDF_ERR_NOT_SUPPORT;
}
}
@@ -10,28 +10,15 @@
#include <cstdio>
#include <cstdlib>
#include <fcntl.h>
#include <gtest/gtest.h>
#include <string>
#include <unistd.h>
#include <gtest/gtest.h>
#include "hdf_uhdf_test.h"
#include "hdf_io_service_if.h"
#include "uart_test.h"
using namespace testing::ext;
static const string HDF_TEST_NAME = "/dev/hdf_test";
enum HdfLiteUartTestCmd {
UAER_WRITE_TEST = 0,
UART_READ_TEST,
UART_SET_BAUD_TEST,
UART_GET_BAUD_TEST,
UART_SET_ATTRIBUTE_TEST,
UART_GET_ATTRIBUTE_TEST,
UART_SET_TRANSMODE_TEST,
UART_RELIABILITY_TEST,
UART_PERFORMANCE_TEST,
};
class HdfLiteUartTest : public testing::Test {
public:
static void SetUpTestCase();
@@ -58,7 +45,6 @@ void HdfLiteUartTest::TearDown()
{
}
#ifdef HDF_LITEOS_TEST
/**
* @tc.name: UartSetTransModeTest001
* @tc.desc: uart function test
@@ -67,10 +53,13 @@ void HdfLiteUartTest::TearDown()
*/
HWTEST_F(HdfLiteUartTest, UartSetTransModeTest001, TestSize.Level1)
{
struct HdfTestMsg msg = {TEST_PAL_UART_TYPE, UART_SET_TRANSMODE_TEST, -1};
struct HdfTestMsg msg = {TEST_PAL_UART_TYPE, UART_TEST_CMD_SET_TRANSMODE, -1};
EXPECT_EQ(0, HdfTestSendMsgToService(&msg));
printf("%s: kernel test done, then for user...\n", __func__);
EXPECT_EQ(0, UartTestExecute(UART_TEST_CMD_SET_TRANSMODE));
printf("%s: exit!\n", __func__);
}
#endif
/**
* @tc.name: UartWriteTest001
@@ -80,8 +69,12 @@ HWTEST_F(HdfLiteUartTest, UartSetTransModeTest001, TestSize.Level1)
*/
HWTEST_F(HdfLiteUartTest, UartWriteTest001, TestSize.Level1)
{
struct HdfTestMsg msg = {TEST_PAL_UART_TYPE, UAER_WRITE_TEST, -1};
struct HdfTestMsg msg = {TEST_PAL_UART_TYPE, UART_TEST_CMD_WRITE, -1};
EXPECT_EQ(0, HdfTestSendMsgToService(&msg));
printf("%s: kernel test done, then for user...\n", __func__);
EXPECT_EQ(0, UartTestExecute(UART_TEST_CMD_WRITE));
printf("%s: exit!\n", __func__);
}
/**
@@ -92,8 +85,12 @@ HWTEST_F(HdfLiteUartTest, UartWriteTest001, TestSize.Level1)
*/
HWTEST_F(HdfLiteUartTest, UartReadTest001, TestSize.Level1)
{
struct HdfTestMsg msg = { TEST_PAL_UART_TYPE, UART_READ_TEST, -1};
struct HdfTestMsg msg = { TEST_PAL_UART_TYPE, UART_TEST_CMD_READ, -1};
EXPECT_EQ(0, HdfTestSendMsgToService(&msg));
printf("%s: kernel test done, then for user...\n", __func__);
EXPECT_EQ(0, UartTestExecute(UART_TEST_CMD_READ));
printf("%s: exit!\n", __func__);
}
/**
@@ -104,8 +101,12 @@ HWTEST_F(HdfLiteUartTest, UartReadTest001, TestSize.Level1)
*/
HWTEST_F(HdfLiteUartTest, UartSetBaudTest001, TestSize.Level1)
{
struct HdfTestMsg msg = {TEST_PAL_UART_TYPE, UART_SET_BAUD_TEST, -1};
struct HdfTestMsg msg = {TEST_PAL_UART_TYPE, UART_TEST_CMD_SET_BAUD, -1};
EXPECT_EQ(0, HdfTestSendMsgToService(&msg));
printf("%s: kernel test done, then for user...\n", __func__);
EXPECT_EQ(0, UartTestExecute(UART_TEST_CMD_SET_BAUD));
printf("%s: exit!\n", __func__);
}
/**
@@ -116,8 +117,12 @@ HWTEST_F(HdfLiteUartTest, UartSetBaudTest001, TestSize.Level1)
*/
HWTEST_F(HdfLiteUartTest, UartGetBaudTest001, TestSize.Level1)
{
struct HdfTestMsg msg = {TEST_PAL_UART_TYPE, UART_GET_BAUD_TEST, -1};
struct HdfTestMsg msg = {TEST_PAL_UART_TYPE, UART_TEST_CMD_GET_BAUD, -1};
EXPECT_EQ(0, HdfTestSendMsgToService(&msg));
printf("%s: kernel test done, then for user...\n", __func__);
EXPECT_EQ(0, UartTestExecute(UART_TEST_CMD_GET_BAUD));
printf("%s: exit!\n", __func__);
}
/**
@@ -128,8 +133,12 @@ HWTEST_F(HdfLiteUartTest, UartGetBaudTest001, TestSize.Level1)
*/
HWTEST_F(HdfLiteUartTest, UartSetAttributeTest001, TestSize.Level1)
{
struct HdfTestMsg msg = {TEST_PAL_UART_TYPE, UART_SET_ATTRIBUTE_TEST, -1};
struct HdfTestMsg msg = {TEST_PAL_UART_TYPE, UART_TEST_CMD_SET_ATTRIBUTE, -1};
EXPECT_EQ(0, HdfTestSendMsgToService(&msg));
printf("%s: kernel test done, then for user...\n", __func__);
EXPECT_EQ(0, UartTestExecute(UART_TEST_CMD_SET_ATTRIBUTE));
printf("%s: exit!\n", __func__);
}
/**
@@ -140,8 +149,12 @@ HWTEST_F(HdfLiteUartTest, UartSetAttributeTest001, TestSize.Level1)
*/
HWTEST_F(HdfLiteUartTest, UartGetAttributeTest001, TestSize.Level1)
{
struct HdfTestMsg msg = {TEST_PAL_UART_TYPE, UART_GET_ATTRIBUTE_TEST, -1};
struct HdfTestMsg msg = {TEST_PAL_UART_TYPE, UART_TEST_CMD_GET_ATTRIBUTE, -1};
EXPECT_EQ(0, HdfTestSendMsgToService(&msg));
printf("%s: kernel test done, then for user...\n", __func__);
EXPECT_EQ(0, UartTestExecute(UART_TEST_CMD_GET_ATTRIBUTE));
printf("%s: exit!\n", __func__);
}
/**
@@ -152,6 +165,10 @@ HWTEST_F(HdfLiteUartTest, UartGetAttributeTest001, TestSize.Level1)
*/
HWTEST_F(HdfLiteUartTest, UartReliabilityTest001, TestSize.Level1)
{
struct HdfTestMsg msg = {TEST_PAL_UART_TYPE, UART_RELIABILITY_TEST, -1};
struct HdfTestMsg msg = {TEST_PAL_UART_TYPE, UART_TEST_CMD_RELIABILITY, -1};
EXPECT_EQ(0, HdfTestSendMsgToService(&msg));
printf("%s: kernel test done, then for user...\n", __func__);
EXPECT_EQ(0, UartTestExecute(UART_TEST_CMD_RELIABILITY));
printf("%s: exit!\n", __func__);
}
@@ -0,0 +1,137 @@
/*
* Copyright (c) 2022 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 "uart_test.h"
#include "device_resource_if.h"
#include "hdf_base.h"
#include "hdf_device_desc.h"
#include "hdf_log.h"
#include "osal_mem.h"
static struct UartTestConfig g_config;
static int32_t UartTestDispatch(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;
}
HDF_LOGE("%s: g_config.len is %d ", __func__, g_config.len);
if (!HdfSbufWriteBuffer(reply, g_config.wbuf, g_config.len)) {
HDF_LOGE("%s: write config wbuf failed", __func__);
return HDF_ERR_IO;
}
} else {
HDF_LOGE("%s: cmd is not support", __func__);
return HDF_ERR_NOT_SUPPORT;
}
return HDF_SUCCESS;
}
static int32_t UartTestReadConfig(struct UartTestConfig *config, const struct DeviceResourceNode *node)
{
int32_t ret;
int32_t i;
uint32_t *tmp = NULL;
struct DeviceResourceIface *drsOps = NULL;
drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
if (drsOps == NULL || drsOps->GetUint32 == NULL || drsOps->GetUint32Array == NULL) {
HDF_LOGE("%s: invalid drs ops fail!", __func__);
return HDF_FAILURE;
}
ret = drsOps->GetUint32(node, "port", &config->port, 0);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: read port fail", __func__);
return HDF_FAILURE;
}
ret = drsOps->GetUint32(node, "len", &config->len, 0);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: read len fail", __func__);
return HDF_FAILURE;
}
config->wbuf = (uint8_t *)OsalMemCalloc(config->len);
if (config->wbuf == NULL) {
HDF_LOGE("%s: wbuf OsalMemCalloc error\n", __func__);
return HDF_ERR_MALLOC_FAIL;
}
tmp = (uint32_t *)OsalMemCalloc(config->len * sizeof(uint32_t));
if (tmp == NULL) {
HDF_LOGE("%s: tmp OsalMemCalloc error\n", __func__);
OsalMemFree(config->wbuf);
return HDF_ERR_MALLOC_FAIL;
}
ret = drsOps->GetUint32Array(node, "wbuf", tmp, config->len, 0);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: read wbuf fail\n", __func__);
OsalMemFree(config->wbuf);
OsalMemFree(tmp);
return HDF_FAILURE;
}
for (i = 0; i < config->len; i++) {
config->wbuf[i] = tmp[i];
}
OsalMemFree(tmp);
config->rbuf = (uint8_t *)OsalMemCalloc(config->len);
if (config->rbuf == NULL) {
HDF_LOGE("%s: rbuf OsalMemCalloc error\n", __func__);
OsalMemFree(config->wbuf);
return HDF_ERR_MALLOC_FAIL;
}
return HDF_SUCCESS;
}
static int32_t UartTestBind(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 = UartTestReadConfig(&g_config, device->property);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: read config failed", __func__);
return ret;
}
service.Dispatch = UartTestDispatch;
device->service = &service;
return HDF_SUCCESS;
}
static int32_t UartTestInit(struct HdfDeviceObject *device)
{
(void)device;
return HDF_SUCCESS;
}
static void UartTestRelease(struct HdfDeviceObject *device)
{
if (device != NULL) {
device->service = NULL;
}
return;
}
struct HdfDriverEntry g_uartTestEntry = {
.moduleVersion = 1,
.Bind = UartTestBind,
.Init = UartTestInit,
.Release = UartTestRelease,
.moduleName = "PLATFORM_UART_TEST",
};
HDF_INIT(g_uartTestEntry);
+223 -248
View File
@@ -1,79 +1,189 @@
/*
* Copyright (c) 2020-2021 Huawei Device Co., Ltd.
* Copyright (c) 2020-2022 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 "device_resource_if.h"
#include "uart_test.h"
#include "hdf_base.h"
#include "hdf_device_desc.h"
#include "hdf_io_service_if.h"
#include "hdf_log.h"
#include "osal_mem.h"
#include "osal_time.h"
#include "securec.h"
#include "uart_if.h"
#include "uart_test.h"
#define HDF_LOG_TAG uart_test_c
struct UartTestFunc {
enum UartTestCmd type;
int32_t (*Func)(struct UartTest *test);
};
#define HDF_LOG_TAG uart_test
#define BITS_PER_WORD 10
#define MAX_SPEED_HZ 10000000
static int32_t UartWriteTest(struct UartTest *test)
static int32_t UartTestGetConfig(struct UartTestConfig *config)
{
if (UartWrite(test->handle, test->wbuf, test->len) != HDF_SUCCESS) {
HDF_LOGE("%s: error", __func__);
return HDF_FAILURE;
int32_t ret;
struct HdfSBuf *reply = NULL;
struct HdfIoService *service = NULL;
struct UartTestConfig *cfg;
const void *buf = NULL;
uint32_t len;
service = HdfIoServiceBind("UART_TEST");
if (service == NULL) {
HDF_LOGE("%s: failed to bind service", __func__);
return HDF_ERR_NOT_SUPPORT;
}
HDF_LOGE("%s: success", __func__);
reply = HdfSbufObtainDefaultSize();
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__);
HdfSbufRecycle(reply);
return ret;
}
if (!HdfSbufReadBuffer(reply, (const void **)&cfg, &len)) {
HDF_LOGE("%s: Read buf failed", __func__);
HdfSbufRecycle(reply);
return HDF_ERR_IO;
}
if (len != sizeof(*cfg)) {
HDF_LOGE("%s: cfg size:%zu, read size:%u", __func__, sizeof(*cfg), len);
HdfSbufRecycle(reply);
return HDF_ERR_IO;
}
if (memcpy_s(config, sizeof(*config), cfg, sizeof(*cfg)) != EOK) {
HDF_LOGE("%s: Memcpy buf failed", __func__);
HdfSbufRecycle(reply);
return HDF_ERR_IO;
}
if (!HdfSbufReadBuffer(reply, (const void **)&buf, &len)) {
HDF_LOGE("%s: Read buf failed", __func__);
HdfSbufRecycle(reply);
return HDF_ERR_IO;
}
if (len != config->len) {
HDF_LOGE("%s: cfg size:%zu, read size:%u", __func__, sizeof(*cfg), len);
HdfSbufRecycle(reply);
return HDF_ERR_IO;
}
config->wbuf = NULL;
config->wbuf = (uint8_t *)OsalMemCalloc(len);
if (config->wbuf == NULL) {
HDF_LOGE("%s: malloc wbuf failed", __func__);
HdfSbufRecycle(reply);
return HDF_ERR_MALLOC_FAIL;
}
if (memcpy_s(config->wbuf, config->len, buf, len) != EOK) {
HDF_LOGE("%s: Memcpy wbuf failed", __func__);
HdfSbufRecycle(reply);
return HDF_ERR_IO;
}
HdfSbufRecycle(reply);
HDF_LOGD("%s: Done", __func__);
HdfIoServiceRecycle(service);
return HDF_SUCCESS;
}
static int32_t UartReadTest(struct UartTest *test)
{
if (UartSetTransMode(test->handle, UART_MODE_RD_NONBLOCK) != HDF_SUCCESS) {
HDF_LOGE("%s: error", __func__);
struct UartTester *UartTesterGet(void)
{
int32_t ret;
static struct UartTester tester;
ret = UartTestGetConfig(&tester.config);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: read config failed:%d", __func__, ret);
return NULL;
}
tester.handle = UartOpen(tester.config.port);
if (tester.handle == NULL) {
HDF_LOGE("%s: open uart port:%u fail!", __func__, tester.config.port);
return NULL;
}
return &tester;
}
static void UartTesterPut(struct UartTester *tester)
{
if (tester == NULL || tester->handle == NULL) {
HDF_LOGE("%s: uart handle is null", __func__);
return;
}
UartClose(tester->handle);
tester->handle = NULL;
}
static int32_t UartWriteTest(struct UartTester *tester)
{
int32_t ret;
ret = UartWrite(tester->handle, tester->config.wbuf, tester->config.len);
HDF_LOGE("%s: len is %d", __func__, tester->config.len);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: write failed", __func__);
return HDF_FAILURE;
}
if (UartRead(test->handle, test->rbuf, test->len) != 0) {
HDF_LOGE("%s: error", __func__);
HDF_LOGD("%s: success", __func__);
return HDF_SUCCESS;
}
static int32_t UartReadTest(struct UartTester *tester)
{
int32_t ret;
ret = UartSetTransMode(tester->handle, UART_MODE_RD_NONBLOCK);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: transmode error.", __func__);
return HDF_FAILURE;
}
HDF_LOGE("%s: success", __func__);
ret = UartRead(tester->handle, tester->config.rbuf, tester->config.len);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: read failed", __func__);
return HDF_FAILURE;
}
HDF_LOGD("%s: success", __func__);
return HDF_SUCCESS;
}
#define BAUD_921600 921600
static int32_t UartSetBaudTest(struct UartTest *test)
static int32_t UartSetBaudTest(struct UartTester *tester)
{
if (UartSetBaud(test->handle, BAUD_921600) != HDF_SUCCESS) {
HDF_LOGE("%s: error", __func__);
int32_t ret;
ret = UartSetBaud(tester->handle, BAUD_921600);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: set baud failed", __func__);
return HDF_FAILURE;
}
HDF_LOGE("%s: success", __func__);
HDF_LOGD("%s: success", __func__);
return HDF_SUCCESS;
}
static int32_t UartGetBaudTest(struct UartTest *test)
static int32_t UartGetBaudTest(struct UartTester *tester)
{
int32_t ret;
uint32_t baud;
if (UartGetBaud(test->handle, &baud) != HDF_SUCCESS) {
HDF_LOGE("%s: error", __func__);
ret = UartGetBaud(tester->handle, &baud);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: get baud failed", __func__);
return HDF_FAILURE;
}
HDF_LOGE("%s: baud %u", __func__, baud);
HDF_LOGE("%s: success", __func__);
HDF_LOGD("%s: baud %u success", __func__, baud);
return HDF_SUCCESS;
}
static int32_t UartSetAttributeTest(struct UartTest *test)
static int32_t UartSetAttributeTest(struct UartTester *tester)
{
struct UartAttribute attribute;
int32_t ret;
attribute.dataBits = UART_ATTR_DATABIT_7;
attribute.parity = UART_ATTR_PARITY_NONE;
@@ -82,253 +192,118 @@ static int32_t UartSetAttributeTest(struct UartTest *test)
attribute.cts = UART_ATTR_CTS_DIS;
attribute.fifoRxEn = UART_ATTR_RX_FIFO_EN;
attribute.fifoTxEn = UART_ATTR_TX_FIFO_EN;
if (UartSetAttribute(test->handle, &attribute) != HDF_SUCCESS) {
HDF_LOGE("%s: error", __func__);
ret = UartSetAttribute(tester->handle, &attribute);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: set attribute failed", __func__);
return HDF_FAILURE;
}
HDF_LOGE("%s: success", __func__);
HDF_LOGD("%s: success", __func__);
return HDF_SUCCESS;
}
static int32_t UartGetAttributeTest(struct UartTest *test)
static int32_t UartGetAttributeTest(struct UartTester *tester)
{
struct UartAttribute attribute;
int32_t ret;
if (UartGetAttribute(test->handle, &attribute) != HDF_SUCCESS) {
HDF_LOGE("%s: error", __func__);
ret = UartGetAttribute(tester->handle, &attribute);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: get attribute failed", __func__);
return HDF_FAILURE;
}
HDF_LOGE("dataBits %u", attribute.dataBits);
HDF_LOGE("parity %u", attribute.parity);
HDF_LOGE("stopBits %u", attribute.stopBits);
HDF_LOGE("rts %u", attribute.rts);
HDF_LOGE("cts %u", attribute.cts);
HDF_LOGE("fifoRxEn %u", attribute.fifoRxEn);
HDF_LOGE("fifoTxEn %u", attribute.fifoTxEn);
HDF_LOGE("%s: success", __func__);
HDF_LOGD("dataBits %u", attribute.dataBits);
HDF_LOGD("parity %u", attribute.parity);
HDF_LOGD("stopBits %u", attribute.stopBits);
HDF_LOGD("rts %u", attribute.rts);
HDF_LOGD("cts %u", attribute.cts);
HDF_LOGD("fifoRxEn %u", attribute.fifoRxEn);
HDF_LOGD("fifoTxEn %u", attribute.fifoTxEn);
HDF_LOGD("%s: success", __func__);
return HDF_SUCCESS;
}
static int32_t UartSetTransModeTest(struct UartTest *test)
static int32_t UartSetTransModeTest(struct UartTester *tester)
{
if (UartSetTransMode(test->handle, UART_MODE_RD_NONBLOCK) != HDF_SUCCESS) {
HDF_LOGE("%s: error", __func__);
int32_t ret;
ret = UartSetTransMode(tester->handle, UART_MODE_RD_NONBLOCK);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: set transmode failed", __func__);
return HDF_FAILURE;
}
HDF_LOGE("%s: success", __func__);
HDF_LOGD("%s: success", __func__);
return HDF_SUCCESS;
}
static int32_t UartReliabilityTest(struct UartTest *test)
static int32_t UartReliabilityTest(struct UartTester *tester)
{
uint32_t baud;
struct UartAttribute attribute = {0};
(void)UartSetTransMode(test->handle, UART_MODE_RD_NONBLOCK);
(void)UartSetTransMode(test->handle, -1);
(void)UartWrite(test->handle, test->wbuf, test->len);
(void)UartWrite(test->handle, NULL, -1);
(void)UartRead(test->handle, test->rbuf, test->len);
(void)UartRead(test->handle, NULL, -1);
(void)UartSetBaud(test->handle, BAUD_921600);
(void)UartSetBaud(test->handle, -1);
(void)UartGetBaud(test->handle, &baud);
(void)UartGetBaud(test->handle, NULL);
(void)UartSetAttribute(test->handle, &attribute);
(void)UartSetAttribute(test->handle, NULL);
(void)UartGetAttribute(test->handle, &attribute);
(void)UartGetAttribute(test->handle, NULL);
HDF_LOGE("%s: success", __func__);
(void)UartSetTransMode(tester->handle, UART_MODE_RD_NONBLOCK);
(void)UartSetTransMode(tester->handle, -1);
(void)UartWrite(tester->handle, tester->config.wbuf, tester->config.len);
(void)UartWrite(tester->handle, NULL, -1);
(void)UartRead(tester->handle, tester->config.rbuf, tester->config.len);
(void)UartRead(tester->handle, NULL, -1);
(void)UartSetBaud(tester->handle, BAUD_921600);
(void)UartSetBaud(tester->handle, -1);
(void)UartGetBaud(tester->handle, &baud);
(void)UartGetBaud(tester->handle, NULL);
(void)UartSetAttribute(tester->handle, &attribute);
(void)UartSetAttribute(tester->handle, NULL);
(void)UartGetAttribute(tester->handle, &attribute);
(void)UartGetAttribute(tester->handle, NULL);
HDF_LOGD("%s: success", __func__);
return HDF_SUCCESS;
}
static int32_t UartTestAll(struct UartTest *test)
{
int32_t total = 0;
int32_t error = 0;
if (UartSetTransModeTest(test) != HDF_SUCCESS) {
error++;
}
total++;
if (UartWriteTest(test) != HDF_SUCCESS) {
error++;
}
total++;
if (UartReadTest(test) != HDF_SUCCESS) {
error++;
}
total++;
if (UartSetBaudTest(test) != HDF_SUCCESS) {
error++;
}
total++;
if (UartGetBaudTest(test) != HDF_SUCCESS) {
error++;
}
total++;
if (UartSetAttributeTest(test) != HDF_SUCCESS) {
error++;
}
total++;
if (UartGetAttributeTest(test) != HDF_SUCCESS) {
error++;
}
total++;
if (UartReliabilityTest(test) != HDF_SUCCESS) {
error++;
}
total++;
HDF_LOGE("%s: Uart Test Total %d Error %d", __func__, total, error);
return HDF_SUCCESS;
}
struct UartTestFunc g_uartTestFunc[] = {
{ UAER_WRITE_TEST, UartWriteTest },
{ UART_READ_TEST, UartReadTest },
{ UART_SET_BAUD_TEST, UartSetBaudTest },
{ UART_GET_BAUD_TEST, UartGetBaudTest },
{ UART_SET_ATTRIBUTE_TEST, UartSetAttributeTest },
{ UART_GET_ATTRIBUTE_TEST, UartGetAttributeTest },
{ UART_SET_TRANSMODE_TEST, UartSetTransModeTest },
{ UART_RELIABILITY_TEST, UartReliabilityTest },
{ UART_PERFORMANCE_TEST, NULL },
{ UART_TEST_ALL, UartTestAll },
struct UartTestEntry {
int cmd;
int32_t (*func)(struct UartTester *tester);
const char *name;
};
static int32_t UartTestEntry(struct UartTest *test, int32_t cmd)
{
int32_t i;
int32_t ret = HDF_ERR_NOT_SUPPORT;
static struct UartTestEntry g_entry[] = {
{ UART_TEST_CMD_WRITE, UartWriteTest, "UartWriteTest" },
{ UART_TEST_CMD_READ, UartReadTest, "UartReadTest" },
{ UART_TEST_CMD_SET_BAUD, UartSetBaudTest, "UartSetBaudTest" },
{ UART_TEST_CMD_GET_BAUD, UartGetBaudTest, "UartGetBaudTest" },
{ UART_TEST_CMD_SET_ATTRIBUTE, UartSetAttributeTest, "UartSetAttributeTest" },
{ UART_TEST_CMD_GET_ATTRIBUTE, UartGetAttributeTest, "UartGetAttributeTest" },
{ UART_TEST_CMD_SET_TRANSMODE, UartSetTransModeTest, "UartSetTransModeTest" },
{ UART_TEST_CMD_RELIABILITY, UartReliabilityTest, "UartReliabilityTest" },
{ UART_TEST_CMD_PERFORMANCE, NULL, "NULL" },
};
if (test == NULL) {
int32_t UartTestExecute(int cmd)
{
uint32_t i;
int32_t ret = HDF_ERR_NOT_SUPPORT;
struct UartTester *tester = NULL;
tester = UartTesterGet();
if (tester == NULL) {
HDF_LOGE("%s: tester is null", __func__);
return HDF_ERR_INVALID_OBJECT;
}
test->handle = UartOpen(test->port);
if (test->handle == NULL) {
HDF_LOGE("%s: spi test get handle fail", __func__);
return HDF_FAILURE;
if (cmd > UART_TEST_CMD_MAX) {
HDF_LOGE("%s: invalid cmd:%d", __func__, cmd);
ret = HDF_ERR_NOT_SUPPORT;
goto __EXIT__;
}
for (i = 0; i < sizeof(g_uartTestFunc) / sizeof(g_uartTestFunc[0]); i++) {
if (cmd == g_uartTestFunc[i].type && g_uartTestFunc[i].Func != NULL) {
ret = g_uartTestFunc[i].Func(test);
HDF_LOGE("%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(tester);
break;
}
UartClose(test->handle);
return ret;
__EXIT__:
HDF_LOGE("[%s][======cmd:%d====ret:%d======]", __func__, cmd, ret);
UartTesterPut(tester);
return ret;
}
static int32_t UartTestBind(struct HdfDeviceObject *device)
{
static struct UartTest 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 UartTestInitFromHcs(struct UartTest *test, const struct DeviceResourceNode *node)
{
int32_t ret;
uint32_t i;
uint32_t *tmp = NULL;
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 || face->GetUint32Array == NULL) {
HDF_LOGE("%s: GetUint32 or GetUint32Array not support", __func__);
return HDF_ERR_NOT_SUPPORT;
}
ret = face->GetUint32(node, "port", &test->port, 0);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: read port fail", __func__);
return HDF_FAILURE;
}
ret = face->GetUint32(node, "len", &test->len, 0);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: read len fail", __func__);
return HDF_FAILURE;
}
test->wbuf = (uint8_t *)OsalMemCalloc(test->len);
if (test->wbuf == NULL) {
HDF_LOGE("%s: wbuf OsalMemCalloc error\n", __func__);
return HDF_ERR_MALLOC_FAIL;
}
tmp = (uint32_t *)OsalMemCalloc(test->len * sizeof(uint32_t));
if (tmp == NULL) {
HDF_LOGE("%s: tmp OsalMemCalloc error\n", __func__);
OsalMemFree(test->wbuf);
return HDF_ERR_MALLOC_FAIL;
}
ret = face->GetUint32Array(node, "wbuf", tmp, test->len, 0);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: read wbuf fail", __func__);
OsalMemFree(test->wbuf);
OsalMemFree(tmp);
return HDF_FAILURE;
}
for (i = 0; i < test->len; i++) {
test->wbuf[i] = tmp[i];
}
OsalMemFree(tmp);
test->rbuf = (uint8_t *)OsalMemCalloc(test->len);
if (test->rbuf == NULL) {
HDF_LOGE("%s: rbuf OsalMemCalloc error\n", __func__);
OsalMemFree(test->wbuf);
return HDF_ERR_MALLOC_FAIL;
}
return HDF_SUCCESS;
}
static int32_t UartTestInit(struct HdfDeviceObject *device)
{
struct UartTest *test = NULL;
if (device == NULL || device->service == NULL || device->property == NULL) {
HDF_LOGE("%s: invalid parameter", __func__);
return HDF_ERR_INVALID_PARAM;
}
test = (struct UartTest *)device->service;
UartTestInitFromHcs(test, device->property);
HDF_LOGE("%s: success", __func__);
test->TestEntry = UartTestEntry;
return HDF_SUCCESS;
}
static void UartTestRelease(struct HdfDeviceObject *device)
{
struct UartTest *test = NULL;
if (device == NULL) {
return;
}
test = (struct UartTest *)device->service;
if (test == NULL) {
return;
}
if (test->wbuf != NULL) {
OsalMemFree(test->wbuf);
}
if (test->rbuf != NULL) {
OsalMemFree(test->rbuf);
}
}
struct HdfDriverEntry g_uartTestEntry = {
.moduleVersion = 1,
.Bind = UartTestBind,
.Init = UartTestInit,
.Release = UartTestRelease,
.moduleName = "PLATFORM_UART_TEST",
};
HDF_INIT(g_uartTestEntry);
+27 -20
View File
@@ -9,36 +9,43 @@
#ifndef UART_TEST_H
#define UART_TEST_H
#include "hdf_device_desc.h"
#include "platform_if.h"
#include "uart_if.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
int32_t UartTestExecute(int cmd);
enum UartTestCmd {
UAER_WRITE_TEST = 0,
UART_READ_TEST,
UART_SET_BAUD_TEST,
UART_GET_BAUD_TEST,
UART_SET_ATTRIBUTE_TEST,
UART_GET_ATTRIBUTE_TEST,
UART_SET_TRANSMODE_TEST,
UART_RELIABILITY_TEST,
UART_PERFORMANCE_TEST,
UART_TEST_ALL,
UART_TEST_CMD_WRITE = 0,
UART_TEST_CMD_READ = 1,
UART_TEST_CMD_SET_BAUD = 2,
UART_TEST_CMD_GET_BAUD = 3,
UART_TEST_CMD_SET_ATTRIBUTE = 4,
UART_TEST_CMD_GET_ATTRIBUTE = 5,
UART_TEST_CMD_SET_TRANSMODE = 6,
UART_TEST_CMD_RELIABILITY = 7,
UART_TEST_CMD_PERFORMANCE = 8,
UART_TEST_CMD_MAX = 9,
};
struct UartTest {
struct IDeviceIoService service;
struct HdfDeviceObject *device;
int32_t (*TestEntry)(struct UartTest *test, int32_t cmd);
struct UartTestConfig {
uint32_t port;
uint32_t len;
uint8_t *wbuf;
uint8_t *rbuf;
DevHandle handle;
};
static inline struct UartTest *GetUartTest(void)
{
return (struct UartTest *)DevSvcManagerClntGetService("UART_TEST");
struct UartTester {
struct UartTestConfig config;
DevHandle handle;
uint16_t total;
uint16_t fails;
};
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* UART_TEST_H */
@@ -13,16 +13,11 @@
int32_t HdfUartUnitTestEntry(HdfTestMsg *msg)
{
struct UartTest *test = NULL;
if (msg == NULL) {
return HDF_FAILURE;
}
test = GetUartTest();
if (test == NULL || test->TestEntry == NULL) {
msg->result = HDF_FAILURE;
return HDF_FAILURE;
}
msg->result = test->TestEntry(test, msg->subCmd);
msg->result = UartTestExecute(msg->subCmd);
return msg->result;
}