uart_dispatch_sample.c 2.3 KB
Newer Older
W
wenjun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/* Copyright 2020 Huawei Device Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "uart_core.h"
#include "uart_dispatch_sample.h"
#include "hdf_log.h"
#include "hdf_sbuf.h"
#include "osal_mem.h"
#include "uart_pl011_sample.h"

#define HDF_LOG_TAG "uart_dispatch_sample"

static int32_t SampleDispatchWrite(struct UartDevice *device, struct HdfSBuf *txBuf)
{
    uint32_t idx;
    uint32_t dataSize = 0;
    const uint8_t *data = NULL;
    struct UartRegisterMap *regMap = (struct UartRegisterMap *)device->resource.physBase;

    if (regMap == NULL) {
        HDF_LOGE("%s: regMap is NULL", __func__);
        return HDF_FAILURE;
    }

    if (!HdfSbufReadBuffer(txBuf, (const void **)&data, &dataSize)) {
        HDF_LOGE("%s: Failed to read sbuf", __func__);
        return HDF_FAILURE;
    }
    regMap = (struct UartRegisterMap *)device->resource.physBase;
    for (idx = 0; idx < dataSize; idx++) {
        UartPl011Write(regMap, data[idx]);
    }
    return HDF_SUCCESS;
}

48
int32_t SampleDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply)
W
wenjun 已提交
49 50 51 52 53 54
{
    int32_t result = HDF_FAILURE;
    if (client == NULL || client->device == NULL) {
        HDF_LOGE("%s: client or client->device is NULL", __func__);
        return result;
    }
55 56 57 58 59 60
    struct UartHost *uartHost = (struct UartHost *)client->device->service;
    if (uartHost == NULL) {
        HDF_LOGE("%s: uartHost is NULL", __func__);
        return result;
    }
    struct UartDevice *uartDevice = (struct UartDevice *)uartHost->priv;
W
wenjun 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74
    if (uartDevice == NULL) {
        HDF_LOGE("%s: uartDevice is NULL", __func__);
        return result;
    }
    switch (cmdId) {
        case UART_WRITE: {
            result = SampleDispatchWrite(uartDevice, data);
            break;
        }
        default:
            break;
    }
    return result;
}