add工具生成模板

Signed-off-by: chen-zhongwei050 <chenzhongwei050@chinasoftinc.com>
This commit is contained in:
chen-zhongwei050 2024-08-09 09:19:53 +08:00
parent 264b43b864
commit d2ab355d9c
14 changed files with 724 additions and 0 deletions

View File

@ -0,0 +1,13 @@
hello :: host {
hostName = "hello_host";
priority = 50;
hello_device :: device {
device0 :: deviceNode {
preload = 0;
policy = 2;
priority = 100;
moduleName = "libhello_driver.z.so";
serviceName = "hello_interface_service";
}
}
}

View File

@ -0,0 +1,62 @@
{
"name": "@ohos/drivers_interface_hello",
"description": "hello device driver interface",
"version": "4.1",
"license": "Apache License 2.0",
"publishAs": "code-segment",
"segment": {
"destPath": "drivers/interface/hello"
},
"dirs": {},
"scripts": {},
"component": {
"name": "drivers_interface_hello",
"subsystem": "hdf",
"syscap": [],
"adapted_system_type": ["standard"],
"rom": "675KB",
"ram": "1024KB",
"deps": {
"components": [
"ipc",
"hdf_core",
"hilog",
"c_utils"
],
"third_party": []
},
"build": {
"sub_component": [
"//drivers/interface/hello/v1_0:hello_idl_target"
],
"test": [
],
"inner_kits": [
{
"name": "//drivers/interface/hello/v1_0:libhello_proxy_1.0",
"header": {
"header_files": [
],
"header_base": "//drivers/interface/hello"
}
},
{
"name": "//drivers/interface/hello/v1_0:libhello_stub_1.0",
"header": {
"header_files": [
],
"header_base": "//drivers/interface/hello"
}
},
{
"name": "//drivers/interface/hello/v1_0:hello_idl_headers",
"header": {
"header_files": [
],
"header_base": "//drivers/interface/hello"
}
}
]
}
}
}

View File

@ -0,0 +1,23 @@
# Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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.
import("//drivers/hdf_core/adapter/uhdf2/hdi.gni") # 编译idl必须要导入的模板
hdi("hello") { # 目标名称会生成两个so分别对应 libhello_client_v1.0.z.so 和 libhello_stub_v1.0.z.so
module_name = "hello_service" # module_name控制dirver文件中驱动描 述符(struct HdfDriverEntry)的moduleName
sources = [ # 参与编译的idl文件
"IHelloInterface.idl", # 接口idl
]
language = "cpp" # 控制idl生成c或c++代码 可选择`c`或`cpp`
subsystem_name = "hdf" # 子系统名
part_name = "drivers_interface_hello" # 组件名
}

View File

@ -0,0 +1,5 @@
package ohos.hdi.hello.v1_0;
interface IHelloInterface {
Helloworld([in] String sendMsg, [out] String recvMsg);
}

View File

@ -0,0 +1,18 @@
# Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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.
group("hello_entry") {
deps = [ "hdi_service:hdf_hello_service" ]
}

View File

@ -0,0 +1,46 @@
{
"name": "@ohos/drivers_peripheral_hello",
"description": "hello device driver",
"version": "4.1",
"license": "Apache License 2.0",
"publishAs": "code-segment",
"segment": {
"destPath": "drivers/peripheral/hello"
},
"dirs": {},
"scripts": {},
"component": {
"name": "drivers_peripheral_hello",
"subsystem": "hdf",
"features": [
],
"syscap": [],
"adapted_system_type": ["standard"],
"rom": "675KB",
"ram": "7400KB",
"deps": {
"components": [
"ipc",
"hdf_core",
"hilog",
"c_utils",
"drivers_interface_hello",
"hitrace",
"hilog_lite"
],
"third_party": [
"bounds_checking_function"
]
},
"build": {
"sub_component": [
"//drivers/peripheral/hello:hello_entry"
],
"test": [
],
"inner_kits": [
]
}
}
}

View File

@ -0,0 +1,51 @@
#Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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.
#
import("//build/ohos.gni")
config("libhdi_hello_pub_config") {
visibility = [ ":*" ]
}
ohos_shared_library("hdi_hello") {
public_configs = [ ":libhdi_hello_pub_config" ]
sources = [
"hello_dump.c",
]
include_dirs = [
"include",
"../utils/interface",
"//third_party/bounds_checking_function/include",
]
cflags = [
"-Wall",
"-Wextra",
"-Werror",
"-fsigned-char",
"-fno-common",
"-fno-strict-aliasing",
]
install_images = [ chipset_base_dir ]
subsystem_name = "hdf"
part_name = "drivers_peripheral_hello"
if (is_standard_system) {
external_deps = [
"c_utils:utils",
"hdf_core:libhdf_host",
"hdf_core:libhdf_utils",
"hilog:libhilog",
]
} else {
external_deps = [ "hilog:libhilog" ]
}
}

View File

@ -0,0 +1,114 @@
/*
* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 "hello_dump.h"
#include <securec.h>
#include <stdio.h>
#include "devhost_dump_reg.h"
#include "hdf_base.h"
#include "hello_log.h"
#define HDF_LOG_TAG uhdf_hello_service
// -c dump the helloworld info
static const char *g_dumpHelp =
" usage:\n"
" -h, --help: dump help\n"
" -c, --channel: dump the hello channel info\n";
static uint32_t ShowHelloworldInfo(struct HdfSBuf *reply)
{
int32_t ret;
const char *helloWorldMessage = "Hello, World!";
ret = HdfSbufWriteString(reply, helloWorldMessage);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%{public}s: write hello world info failed", __func__);
return HDF_FAILURE;
}
HDF_LOGI("%{public}s: hellodump: print hello world !", __func__);
return HDF_SUCCESS;
}
static int32_t DumpHelloChannel(struct HdfSBuf *reply)
{
int32_t ret;
HDF_LOGI("%{public}s: get hello dump channel begin", __func__);
ret = ShowHelloworldInfo(reply);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%{public}s: show hello world info failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
static int32_t HelloDriverDump(struct HdfSBuf *data, struct HdfSBuf *reply)
{
uint32_t i;
uint32_t argv = 0;
HDF_LOGI("%{public}s: get hello dump begin xx", __func__);
if (data == NULL || reply == NULL) {
return HDF_FAILURE;
}
if (!HdfSbufReadUint32(data, &argv)) {
HDF_LOGE("%{public}s: read argv failed", __func__);
return HDF_FAILURE;
}
if (argv == 0) {
if (!HdfSbufWriteString(reply, g_dumpHelp)) {
HDF_LOGE("%{public}s: write -h failed", __func__);
return HDF_FAILURE;
}
}
for (i = 0; i < argv; i++) {
const char *value = HdfSbufReadString(data);
if (value == NULL) {
HDF_LOGE("%{public}s value is invalid", __func__);
return HDF_FAILURE;
}
if (strcmp(value, "-h") == HDF_SUCCESS) {
if (!HdfSbufWriteString(reply, g_dumpHelp)) {
HDF_LOGE("%{public}s: write -h failed", __func__);
return HDF_FAILURE;
}
continue;
} else if (strcmp(value, "-c") == HDF_SUCCESS) {
DumpHelloChannel(reply);
continue;
}
}
return HDF_SUCCESS;
}
int32_t GetHelloDump(struct HdfSBuf *data, struct HdfSBuf *reply)
{
HDF_LOGI("%{public}s: get hello dump begin", __func__);
int32_t ret = HelloDriverDump(data, reply);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%{public}s: get hello dump failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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.
*/
#ifndef HELLO_DUMP_H
#define HELLO_DUMP_H
#include <securec.h>
#include <stdio.h>
#include "hdf_sbuf.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */
int32_t GetHelloDump(struct HdfSBuf *data, struct HdfSBuf *reply);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif /* HDI_HELLO_DUMP_H */

View File

@ -0,0 +1,98 @@
# Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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.
HDF_CORE_PATH = "../../../hdf_core"
import("//build/ohos.gni")
import("$HDF_CORE_PATH/adapter/uhdf2/uhdf.gni")
ohos_shared_library("libhello_interface_service_1.0") {
include_dirs = [
".",
"../utils/interface",
"../hal/include"
]
sources = [ "hello_interface_service.cpp"]
deps = [ "../hal:hdi_hello" ]
cflags = [
"-Wall",
"-Wextra",
"-Werror",
"-fsigned-char",
"-fno-common",
"-fno-strict-aliasing",
]
if (is_standard_system) {
external_deps = [
"c_utils:utils",
"drivers_interface_hello:hello_idl_headers",
"hdf_core:libhdf_host",
"hilog:libhilog",
"hitrace:hitrace_meter",
]
} else {
external_deps = [ "hilog:libhilog" ]
}
external_deps += [ "ipc:ipc_single" ]
install_images = [ chipset_base_dir ]
subsystem_name = "hdf"
part_name = "drivers_peripheral_hello"
}
ohos_shared_library("libhello_driver") {
include_dirs = [
]
sources = [ "hello_interface_driver.cpp" ]
cflags = [
"-Wall",
"-Wextra",
"-Werror",
"-fsigned-char",
"-fno-common",
"-fno-strict-aliasing",
]
if (is_standard_system) {
external_deps = [
"c_utils:utils",
"drivers_interface_hello:libhello_stub_1.0",
"hdf_core:libhdf_host",
"hdf_core:libhdf_ipc_adapter",
"hdf_core:libhdf_utils",
"hdf_core:libhdi",
"hilog:libhilog",
"ipc:ipc_single",
]
} else {
external_deps = [
"hilog:libhilog",
"ipc:ipc_single",
]
}
shlib_type = "hdi"
install_images = [ chipset_base_dir ]
subsystem_name = "hdf"
part_name = "drivers_peripheral_hello"
}
group("hdf_hello_service") {
deps = [
":libhello_driver",
":libhello_interface_service_1.0",
]
}

View File

@ -0,0 +1,146 @@
/*
* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 <hdf_base.h>
#include <hdf_device_desc.h>
#include <hdf_log.h>
#include <hdf_sbuf_ipc.h>
#include "v1_0/hello_interface_stub.h"
#define HDF_LOG_TAG hello_interface_driver
using namespace OHOS::HDI::Hello::V1_0;
struct HdfHelloInterfaceHost {
struct IDeviceIoService ioService;
OHOS::sptr<OHOS::IRemoteObject> stub;
};
/*
* Dispatch方法: IO请求
* clientHdfDeviceIoClient结构体的指针
* cmdIdID
* dataHdfSBuf结构体的指针
* replyHdfSBuf结构体的指针
*/
static int32_t HelloInterfaceDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
struct HdfSBuf *reply)
{
auto *hdfHelloInterfaceHost = CONTAINER_OF(client->device->service, struct HdfHelloInterfaceHost, ioService);
// 声明两个MessageParcel对象用于序列化和反序列化IPC通信中的数据
OHOS::MessageParcel *dataParcel = nullptr;
OHOS::MessageParcel *replyParcel = nullptr;
// 创建一个MessageOption对象用于设置IPC通信的选项。
OHOS::MessageOption option;
// 响应序列化将HdfSBuf中的数据转换为MessageParcel对象。如果转换失败记录错误并返回错误代码。
if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
HDF_LOGE("%{public}s: invalid data sbuf object to dispatch", __func__);
return HDF_ERR_INVALID_PARAM;
}
// 数据序列化尝试将响应数据的HdfSBuf转换为MessageParcel对象。如果失败也记录错误并返回错误代码。
if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
HDF_LOGE("%{public}s: invalid reply sbuf object to dispatch", __func__);
return HDF_ERR_INVALID_PARAM;
}
// 调用stub对象的SendRequest方法发送请求。这个方法执行实际的IPC调用将cmdId和序列化后的请求数据dataParcel发送给服务端并将响应数据反序列化到replyParcel中。
return hdfHelloInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
}
// 驱动自身业务初始化的接口
static int HdfHelloInterfaceDriverInit(struct HdfDeviceObject *deviceObject)
{
HDF_LOGI("%{public}s: driver init start", __func__);
return HDF_SUCCESS;
}
// 将驱动对外提供的服务能力接口绑定到HDF框架
static int HdfHelloInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
{
HDF_LOGI("%{public}s: driver bind start", __func__);
// 创建对象:该对象是驱动服务的具体实现
auto *hdfHelloInterfaceHost = new (std::nothrow) HdfHelloInterfaceHost;
if (hdfHelloInterfaceHost == nullptr) {
HDF_LOGE("%{public}s: failed to create create HdfHelloInterfaceHost object", __func__);
return HDF_FAILURE;
}
// 为ioService结构体设置回调函数设置的Dispatch函数用于处理IO请求
hdfHelloInterfaceHost->ioService.Dispatch = HelloInterfaceDriverDispatch;
hdfHelloInterfaceHost->ioService.Open = NULL;
hdfHelloInterfaceHost->ioService.Release = NULL;
auto serviceImpl = OHOS::HDI::Hello::V1_0::IHelloInterface::Get(true);
if (serviceImpl == nullptr) {
HDF_LOGE("%{public}s: failed to get of implement service", __func__);
delete hdfHelloInterfaceHost;
return HDF_FAILURE;
}
// 使用ObjectCollector的GetOrNewObject方法获取或创建一个Stub对象。Stub对象是服务接口的客户端代理用于发起远程调用。
hdfHelloInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
OHOS::HDI::Hello::V1_0::IHelloInterface::GetDescriptor());
if (hdfHelloInterfaceHost->stub == nullptr) {
HDF_LOGE("%{public}s: failed to get stub object", __func__);
delete hdfHelloInterfaceHost;
return HDF_FAILURE;
}
// 将ioService绑定到deviceObject,这样HDF框架就可以通过deviceObject来访问服务
deviceObject->service = &hdfHelloInterfaceHost->ioService;
HDF_LOGI("%{public}s: driver bind end", __func__);
return HDF_SUCCESS;
}
// 驱动释放资源的接口
static void HdfHelloInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
{
HDF_LOGI("%{public}s: driver release start", __func__);
if (deviceObject->service == nullptr) {
return;
}
auto *hdfHelloInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfHelloInterfaceHost, ioService);
if (hdfHelloInterfaceHost != nullptr) {
delete hdfHelloInterfaceHost;
}
}
/*
* HdfDriverEntryhdf_device_desc.h中定义
*/
struct HdfDriverEntry g_hellointerfaceDriverEntry = {
.moduleVersion = 1,
.moduleName = "hello_service",
.Bind = HdfHelloInterfaceDriverBind,
.Init = HdfHelloInterfaceDriverInit,
.Release = HdfHelloInterfaceDriverRelease,
};
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/*
* HDF_INIT将驱动入口注册到HDF框架中
* HDF框架会先调用Bind函数Init函数加载该驱动Init调用异常时HDF框架会调用Release释放驱动资源并退出
*/
HDF_INIT(g_hellointerfaceDriverEntry);
#ifdef __cplusplus
}
#endif /* __cplusplus */

View File

@ -0,0 +1,47 @@
/*
* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 "v1_0/hello_interface_service.h"
#include <hdf_base.h>
#include "hello_log.h"
#include "devhost_dump_reg.h"
#include "hello_dump.h"
#define HDF_LOG_TAG hello_interface_service
namespace OHOS {
namespace HDI {
namespace Hello {
namespace V1_0 {
extern "C" IHelloInterface *HelloInterfaceImplGetInstance(void)
{
// עᨩdumper
DevHostRegisterDumpHost(GetHelloDump);
// [hdf-gen] Todo
HDF_LOGI("%{public}s: IHelloInterface init", __func__);
return new (std::nothrow) HelloInterfaceService();
}
int32_t HelloInterfaceService::Helloworld(const std::string& sendMsg, std::string& recvMsg)
{
// [hdf-gen] Todo
HDF_LOGI("%{public}s: HelloService::Helloworld print", __func__);
return HDF_SUCCESS;
}
} // V1_0
} // Hello
} // HDI
} // OHOS

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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.
*/
#ifndef OHOS_HDI_HELLO_V1_0_HELLOINTERFACESERVICE_H
#define OHOS_HDI_HELLO_V1_0_HELLOINTERFACESERVICE_H
#include "v1_0/ihello_interface.h"
namespace OHOS {
namespace HDI {
namespace Hello {
namespace V1_0 {
class HelloInterfaceService : public OHOS::HDI::Hello::V1_0::IHelloInterface {
public:
HelloInterfaceService() = default;
virtual ~HelloInterfaceService() = default;
int32_t Helloworld(const std::string& sendMsg, std::string& recvMsg) override;
};
} // V1_0
} // Hello
} // HDI
} // OHOS
#endif // OHOS_HDI_HELLO_V1_0_HELLOINTERFACESERVICE_H

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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.
*/
#ifndef HELLO_UHDF_LOG_H
#define HELLO_UHDF_LOG_H
#include "hdf_log.h"
#ifdef LOG_DOMAIN
#undef LOG_DOMAIN
#endif
#define LOG_DOMAIN 0xD002516
#endif //HELLO_UHDF_LOG_H