增加h2hdf源码

Signed-off-by: chen-zhongwei050 <chenzhongwei050@chinasoftinc.com>
This commit is contained in:
chen-zhongwei050 2024-07-25 20:02:13 +08:00
parent 482c166ade
commit 16276379e0
18 changed files with 1025 additions and 0 deletions

View File

@ -0,0 +1,34 @@
{
"name": "hdf-gen",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"stdio": "^2.1.3",
"typescript": "^4.9.5"
},
"bin": "./src/main.js",
"pkg": {
"assets": [
"./src/templete/HcsconfigTemplete/hcsconfigTemplete.gen",
"./src/templete/IdlInterfaceTemplete/buildgnTemplete.gen",
"./src/templete/IdlInterfaceTemplete/bundlejsonTemplete.gen",
"./src/templete/IdlInterfaceTemplete/idlInterfaceTemplete.gen",
"./src/templete/PeripheralTemplete/DumpExampleTemplete/buildgnTemplete.gen",
"./src/templete/PeripheralTemplete/DumpExampleTemplete/dumpCTemplete.gen",
"./src/templete/PeripheralTemplete/DumpExampleTemplete/dumpHTemplete.gen",
"./src/templete/PeripheralTemplete/HdiServiceTemplete/buildgnTemplete.gen",
"./src/templete/PeripheralTemplete/HdiServiceTemplete/driverTemplete.gen",
"./src/templete/PeripheralTemplete/HdiServiceTemplete/logHTemplete.gen",
"./src/templete/PeripheralTemplete/HdiServiceTemplete/serviceCppTemplete.gen",
"./src/templete/PeripheralTemplete/HdiServiceTemplete/serviceHTemplete.gen",
"./src/templete/PeripheralTemplete/buildgnTemplete.gen",
"./src/templete/PeripheralTemplete/bundlejsonTemplete.gen"
]
}
}

View File

@ -0,0 +1,285 @@
/*
* 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.
*/
const path = require('path');
const fs = require('fs');
/**
* 获取Json文件内容
* @returns
*/
function getJsonCfg(jsonFilePath) {
let jsonCfg = null; // json 配置文件
let jsonFile = fs.readFileSync(jsonFilePath, { encoding: 'utf8' });
jsonCfg = JSON.parse(jsonFile);
return jsonCfg;
}
function replaceAll(s, sfrom, sto) {
while (s.indexOf(sfrom) >= 0) {
s = s.replace(sfrom, sto);
}
return s;
}
// 创建路径
function createDirectorySync(directoryPath) {
try {
if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, { recursive: true });
}
} catch (err) {
console.error(`无法创建文件夹 ${directoryPath}: ${err}`);
}
}
function pathJoin(...args) {
return path.join(...args);
}
// 写文件
function writeFile(fn, str) {
fs.writeFileSync(fn, str, { encoding: 'utf8' });
}
function utf8ArrayToStr(array) {
let char2, char3;
let outStr = '';
let len = array.length;
let i = 0;
while (i < len) {
let ch = array[i++];
switch (ch >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
outStr += String.fromCharCode(ch);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
outStr += String.fromCharCode(((ch & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
outStr += String.fromCharCode(((ch & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return outStr;
}
function readFile(fn) {
if (!fs.existsSync(fn)) {
return '';
}
let data = fs.readFileSync(fn);
data = utf8ArrayToStr(data);
return data;
}
/* driverframework
* drivername:用户输入的驱动名out:生成框架路径
* 1. 读取json文件模板
* 2. 替换模板中的名字并写文件输出
*/
function genDriverFramework(driverName, out = '') {
// 读取Json文件获取各模板路径
let frameworkJsonPath = path.join(__dirname, './templete/framework.json');
let frameworkJson = getJsonCfg(frameworkJsonPath);
let frameworkPath = pathJoin(out, 'hdf');
let namespaceName = driverName.substring(0,1).toUpperCase() + driverName.substring(1, driverName.length);
let idlFileName = 'I' + namespaceName + 'Interface';
let rootInfo = {
'driverName': driverName,
'namespaceName': namespaceName,
'idlFileName': idlFileName,
}
// 生成Hcs配置文件
genHcsconfigFile(frameworkJson, driverName, frameworkPath);
// 生成Idl接口
genInterface(frameworkPath, frameworkJson, rootInfo);
// 生成hdi_service
genPeripheral(frameworkPath,frameworkJson, rootInfo);
}
function genPeripheral(frameworkPath, frameworkJson, rootInfo) {
let peripheralPath = pathJoin(frameworkPath, 'Peripheral/' + rootInfo.driverName);
createDirectorySync(peripheralPath);
// dump文件路径dump.c 与 BUILD.gn
// build.gn out/hdf/Peripheral/xxx/hal/BUILD.gn
genExampleDumpfile(peripheralPath, frameworkJson, rootInfo);
// hdi路径
genHdiService(peripheralPath, frameworkJson, rootInfo);
// 日志文件路径 out/hdf/Peripheral/xxx/utils/interface/xxx_log.h
genLogFile(peripheralPath, rootInfo, frameworkJson);
// 生成编译文件bundle.json和BUILD.gn文件
genBuildFile(peripheralPath, frameworkJson, rootInfo);
}
function genBuildFile(peripheralPath, frameworkJson, rootInfo) {
// out/hdf/Peripheral/xxx/bundle.json
let genBundlejsonPath = pathJoin(peripheralPath, 'bundle.json');
let bundlejsonPath = path.join(__dirname, frameworkJson.PeripheralTemplete.bundlejsonTemplete);
let bundlejsonContent = readFile(bundlejsonPath);
bundlejsonContent = replaceAll(bundlejsonContent, '[driver_name]', rootInfo.driverName);
writeFile(genBundlejsonPath, bundlejsonContent);
// out/hdf/Peripheral/xxx/BUILD.gn
let genBuildgnPath = pathJoin(peripheralPath, 'BUILD.gn');
let buildgnPath = path.join(__dirname, frameworkJson.PeripheralTemplete.buildgnTemplete);
let buildgnContent = readFile(buildgnPath);
buildgnContent = replaceAll(buildgnContent, '[driver_name]', rootInfo.driverName);
writeFile(genBuildgnPath, buildgnContent);
}
function genLogFile(peripheralPath, rootInfo, frameworkJson) {
let genLogPath = pathJoin(peripheralPath, 'utils/interface');
createDirectorySync(genLogPath);
let genLogFile = pathJoin(genLogPath, rootInfo.driverName + '_log.h');
let logPath = path.join(__dirname, frameworkJson.PeripheralTemplete.HdiServiceTemplete.logHTemplete);
let logContent = readFile(logPath);
logContent = replaceAll(logContent, '[upper_driver_name]', rootInfo.driverName.toUpperCase());
writeFile(genLogFile, logContent);
}
function genHdiService(peripheralPath, frameworkJson, rootInfo) {
let hdiPath = pathJoin(peripheralPath, 'hdi_service');
let driverInterName = rootInfo.namespaceName + 'Interface';
// 创建hdi_service文件夹
createDirectorySync(hdiPath);
// out/hdf/Peripheral/xxx/hdi_service/xxx_interface_driver.cpp
let genHdiDriverPath = pathJoin(hdiPath, rootInfo.driverName + '_interface_driver.cpp');
let driverPath = path.join(__dirname, frameworkJson.PeripheralTemplete.HdiServiceTemplete.driverTemplete);
let driverContent = readFile(driverPath);
driverContent = replaceAll(driverContent, '[driver_name]', rootInfo.driverName);
driverContent = replaceAll(driverContent, '[driver_idl_name]', rootInfo.idlFileName);
driverContent = replaceAll(driverContent, '[driver_inter_name]', driverInterName);
driverContent = replaceAll(driverContent, '[driver_namespace_name]', rootInfo.namespaceName);
writeFile(genHdiDriverPath, driverContent);
// out/hdf/Peripheral/xxx/hdi_service/xxx_interface_service.cpp
let genHdiServiceCppPath = pathJoin(hdiPath, rootInfo.driverName + '_interface_service.cpp');
let serviceCppPath = path.join(__dirname, frameworkJson.PeripheralTemplete.HdiServiceTemplete.serviceCppTemplete);
let serviceCppContent = readFile(serviceCppPath);
serviceCppContent = replaceAll(serviceCppContent, '[driver_name]', rootInfo.driverName);
serviceCppContent = replaceAll(serviceCppContent, '[driver_idl_name]', rootInfo.idlFileName);
serviceCppContent = replaceAll(serviceCppContent, '[driver_inter_name]', driverInterName);
serviceCppContent = replaceAll(serviceCppContent, '[driver_namespace_name]', rootInfo.namespaceName);
writeFile(genHdiServiceCppPath, serviceCppContent);
// out/hdf/Peripheral/xxx/hdi_service/xxx_interface_service.h
let genHdiServiceHPath = pathJoin(hdiPath, rootInfo.driverName + '_interface_service.h');
let serviceHPath = path.join(__dirname, frameworkJson.PeripheralTemplete.HdiServiceTemplete.serviceHTemplete);
let serviceHContent = readFile(serviceHPath);
serviceHContent = replaceAll(serviceHContent, '[driver_name]', rootInfo.driverName);
serviceHContent = replaceAll(serviceHContent, '[driver_idl_name]', rootInfo.idlFileName);
serviceHContent = replaceAll(serviceHContent, '[driver_inter_name]', driverInterName);
serviceHContent = replaceAll(serviceHContent, '[driver_namespace_name]', rootInfo.namespaceName);
serviceHContent = replaceAll(serviceHContent, '[upper_driver_name]', rootInfo.driverName.toUpperCase());
writeFile(genHdiServiceHPath, serviceHContent);
// 生成hdi_service下面的BUILD.gn: out/hdf/Peripheral/xxx/hdi_service/
let genHdiServiceGnPath = pathJoin(hdiPath, 'BUILD.gn');
let serviceGnPath = path.join(__dirname, frameworkJson.PeripheralTemplete.HdiServiceTemplete.buildgnTemplete);
let serviceGnContent = readFile(serviceGnPath);
serviceGnContent = replaceAll(serviceGnContent, '[driver_name]', rootInfo.driverName);
writeFile(genHdiServiceGnPath, serviceGnContent);
}
function genExampleDumpfile(peripheralPath, frameworkJson, rootInfo) {
let dumpExamplePath = pathJoin(peripheralPath, 'hal');
createDirectorySync(dumpExamplePath);
let genDumpExampleGnPath = pathJoin(dumpExamplePath, 'BUILD.gn');
let dumpExampleGnPath = path.join(__dirname, frameworkJson.PeripheralTemplete.DumpExampleTemplete.buildgnTemplete);
let dumpExampleGnContent = readFile(dumpExampleGnPath);
dumpExampleGnContent = replaceAll(dumpExampleGnContent, '[driver_name]', rootInfo.driverName);
writeFile(genDumpExampleGnPath, dumpExampleGnContent);
// dump.c out/hdf/Peripheral/xxx/hal/xxx_dump.c
let genDumpCExamplePath = pathJoin(dumpExamplePath, rootInfo.driverName + '_dump.c');
let dumpCExamplePath = path.join(__dirname, frameworkJson.PeripheralTemplete.DumpExampleTemplete.dumpCTemplete);
let dumpCExampleContent = readFile(dumpCExamplePath);
dumpCExampleContent = replaceAll(dumpCExampleContent, '[driver_name]', rootInfo.driverName);
dumpCExampleContent = replaceAll(dumpCExampleContent, '[driver_func_name]', rootInfo.namespaceName);
writeFile(genDumpCExamplePath, dumpCExampleContent);
// dump.h out/hdf/Peripheral/xxx/hal/include/xxx_dump.h
let genDumpHExamplePath = pathJoin(dumpExamplePath, 'include');
createDirectorySync(genDumpHExamplePath);
let dumpHExamplePath = path.join(__dirname, frameworkJson.PeripheralTemplete.DumpExampleTemplete.dumpHTemplete);
let dumpHExampleContent = readFile(dumpHExamplePath);
dumpHExampleContent = replaceAll(dumpHExampleContent, '[driver_name_toupper]', rootInfo.driverName.toUpperCase());
dumpHExampleContent = replaceAll(dumpHExampleContent, '[driver_func_name]', rootInfo.namespaceName);
let genDumpHExampleFile = pathJoin(genDumpHExamplePath, rootInfo.driverName + '_dump.h');
writeFile(genDumpHExampleFile, dumpHExampleContent);
}
function genInterface(frameworkPath, frameworkJson, rootInfo) {
// idl文件路径 out/hdf/IdlInterface/foo/v1_0/IXxxInterface.idl
let idlPath = pathJoin(frameworkPath, 'IdlInterface/' + rootInfo.driverName);
let genIdlFilepath = pathJoin(idlPath, 'v1_0/');
createDirectorySync(genIdlFilepath);
let idlFilepath = path.join(__dirname, frameworkJson.IdlInterfaceTemplete.idlInterfaceTemplete);
let idlFileContent = readFile(idlFilepath);
idlFileContent = replaceAll(idlFileContent, '[driver_name]', rootInfo.driverName);
idlFileContent = replaceAll(idlFileContent, '[driver_idl_name]', rootInfo.idlFileName);
let genIdlFile = pathJoin(genIdlFilepath, rootInfo.idlFileName + '.idl');
writeFile(genIdlFile, idlFileContent);
// idl接口bundlejson路径 out/hdf/IdlInterface/foo/bundle.json
let genIdlBundlejsonPath = pathJoin(idlPath, 'bundle.json');
let idlBundlejsonPath = path.join(__dirname, frameworkJson.IdlInterfaceTemplete.bundlejsonTemplete);
let idlBundlejsonContent = readFile(idlBundlejsonPath);
idlBundlejsonContent = replaceAll(idlBundlejsonContent, '[driver_name]', rootInfo.driverName);
writeFile(genIdlBundlejsonPath, idlBundlejsonContent);
// idl接口BUILD.gn路径 out/hdf/IdlInterface/foo/v1_0/BUILD.gn
let genIdlBuildgnPath = pathJoin(genIdlFilepath, 'BUILD.gn');
let idlBuildgnPath = path.join(__dirname, frameworkJson.IdlInterfaceTemplete.buildgnTemplete);
let idlBuildgnContent = readFile(idlBuildgnPath);
idlBuildgnContent = replaceAll(idlBuildgnContent, '[driver_name]', rootInfo.driverName);
idlBuildgnContent = replaceAll(idlBuildgnContent, '[driver_idl_name]', rootInfo.idlFileName);
writeFile(genIdlBuildgnPath, idlBuildgnContent);
}
function genHcsconfigFile(frameworkJson, driverName, frameworkPath) {
// 读取hcs配置文件模板 hcs配置文件
let hcsconfigPath = path.join(__dirname, frameworkJson.HcsconfigTemplete);
let hcsconfigContent = readFile(hcsconfigPath);
// 生成hcs config文件内容: 根据用户输入的drivername配置hcs文件
hcsconfigContent = replaceAll(hcsconfigContent, '[driver_name]', driverName);
// 生成device_info.hcs 配置文件路径 out/hdf/HcsConfig/device_info.hcs
let genHcsconfigPath = pathJoin(frameworkPath, 'HcsConfig');
createDirectorySync(genHcsconfigPath);
let genHcsconfigFile = pathJoin(genHcsconfigPath, 'device_info.hcs');
writeFile(genHcsconfigFile, hcsconfigContent);
}
module.exports = {
genDriverFramework
};

34
src/cli/h2hdf/src/main.js Normal file
View File

@ -0,0 +1,34 @@
/*
* 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.
*/
const stdio = require('stdio');
const main = require('./generate');
let ops = stdio.getopt({
// 输入driver name ,输入一个字符串默认为hello
'drivername': { key: 'n', args: 1, description: 'driver name', default: 'hello' },
// 输出文件夹路径
'out': { key: 'o', args: 1, description: 'output directory', default: '.' },
});
let drivername = ops.drivername;
// 若drivername不为空则生成否则打印错误信息
if (drivername.trim().length !== 0 && checkInput(drivername)) {
main.genDriverFramework(drivername);
}
function checkInput(input) {
const regex = /\b[a-zA-Z_][a-zA-Z0-9_]*\b/;
return regex.test(input);
}

View File

@ -0,0 +1,13 @@
[driver_name] :: host {
hostName = "[driver_name]_host";
priority = 50;
[driver_name]_device :: device {
device0 :: deviceNode {
preload = 0;
policy = 2;
priority = 100;
moduleName = "lib[driver_name]_driver.z.so";
serviceName = "[driver_name]_interface_service";
}
}
}

View File

@ -0,0 +1,11 @@
import("//drivers/hdf_core/adapter/uhdf2/hdi.gni") # 编译idl必须要导入的模板
hdi("[driver_name]") { # 目标名称会生成两个so分别对应 lib[driver_name]_client_v1.0.z.so 和 lib[driver_name]_stub_v1.0.z.so
# package = "ohos.hdi.[driver_name].v1_0" # 包名必须与idl路径匹配
module_name = "[driver_name]_service" # module_name控制dirver文件中驱动描 述符(struct HdfDriverEntry)的moduleName
sources = [ # 参与编译的idl文件
"[driver_idl_name].idl", # 接口idl
]
language = "cpp" # 控制idl生成c或c++代码 可选择`c`或`cpp`
subsystem_name = "hdf" # 子系统名
part_name = "drivers_interface_[driver_name]" # 组件名
}

View File

@ -0,0 +1,62 @@
{
"name": "@ohos/drivers_interface_[driver_name]",
"description": "[driver_name] device driver interface",
"version": "4.1",
"license": "Apache License 2.0",
"publishAs": "code-segment",
"segment": {
"destPath": "drivers/interface/[driver_name]"
},
"dirs": {},
"scripts": {},
"component": {
"name": "drivers_interface_[driver_name]",
"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/[driver_name]/v1_0:[driver_name]_idl_target"
],
"test": [
],
"inner_kits": [
{
"name": "//drivers/interface/[driver_name]/v1_0:lib[driver_name]_proxy_1.0",
"header": {
"header_files": [
],
"header_base": "//drivers/interface/[driver_name]"
}
},
{
"name": "//drivers/interface/[driver_name]/v1_0:lib[driver_name]_stub_1.0",
"header": {
"header_files": [
],
"header_base": "//drivers/interface/[driver_name]"
}
},
{
"name": "//drivers/interface/[driver_name]/v1_0:[driver_name]_idl_headers",
"header": {
"header_files": [
],
"header_base": "//drivers/interface/[driver_name]"
}
}
]
}
}
}

View File

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

View File

@ -0,0 +1,51 @@
#Copyright (c) 2022-2023 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.
#
import("//build/ohos.gni")
config("libhdi_[driver_name]_pub_config") {
visibility = [ ":*" ]
}
ohos_shared_library("hdi_[driver_name]") {
public_configs = [ ":libhdi_[driver_name]_pub_config" ]
sources = [
"[driver_name]_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_[driver_name]"
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) 2023 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 "[driver_name]_dump.h"
#include <securec.h>
#include <stdio.h>
#include "devhost_dump_reg.h"
#include "hdf_base.h"
#include "[driver_name]_log.h"
#define HDF_LOG_TAG uhdf_[driver_name]_service
// -c dump the helloworld info
static const char *g_dumpHelp =
" usage:\n"
" -h, --help: dump help\n"
" -c, --channel: dump the [driver_name] 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: [driver_name]dump: print hello world !", __func__);
return HDF_SUCCESS;
}
static int32_t Dump[driver_func_name]Channel(struct HdfSBuf *reply)
{
int32_t ret;
HDF_LOGI("%{public}s: get [driver_name] 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 [driver_func_name]DriverDump(struct HdfSBuf *data, struct HdfSBuf *reply)
{
uint32_t i;
uint32_t argv = 0;
HDF_LOGI("%{public}s: get [driver_name] 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) {
Dump[driver_func_name]Channel(reply);
continue;
}
}
return HDF_SUCCESS;
}
int32_t Get[driver_func_name]Dump(struct HdfSBuf *data, struct HdfSBuf *reply)
{
HDF_LOGI("%{public}s: get [driver_name] dump begin", __func__);
int32_t ret = [driver_func_name]DriverDump(data, reply);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%{public}s: get [driver_name] dump failed", __func__);
return HDF_FAILURE;
}
return HDF_SUCCESS;
}

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2023 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.
*/
#ifndef [driver_name_toupper]_DUMP_H
#define [driver_name_toupper]_DUMP_H
#include <securec.h>
#include <stdio.h>
#include "hdf_sbuf.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */
int32_t Get[driver_func_name]Dump(struct HdfSBuf *data, struct HdfSBuf *reply);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif /* HDI_[driver_name_toupper]_DUMP_H */

View File

@ -0,0 +1,98 @@
# Copyright (c) 2022-2023 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.
HDF_CORE_PATH = "../../../hdf_core"
import("//build/ohos.gni")
import("$HDF_CORE_PATH/adapter/uhdf2/uhdf.gni")
ohos_shared_library("lib[driver_name]_interface_service_1.0") {
include_dirs = [
".",
"../utils/interface",
"../hal/include"
]
sources = [ "[driver_name]_interface_service.cpp"]
deps = [ "../hal:hdi_[driver_name]" ]
cflags = [
"-Wall",
"-Wextra",
"-Werror",
"-fsigned-char",
"-fno-common",
"-fno-strict-aliasing",
]
if (is_standard_system) {
external_deps = [
"c_utils:utils",
"drivers_interface_[driver_name]:[driver_name]_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_[driver_name]"
}
ohos_shared_library("lib[driver_name]_driver") {
include_dirs = [
]
sources = [ "[driver_name]_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_[driver_name]:lib[driver_name]_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_[driver_name]"
}
group("hdf_[driver_name]_service") {
deps = [
":lib[driver_name]_driver",
":lib[driver_name]_interface_service_1.0",
]
}

View File

@ -0,0 +1,115 @@
#include <hdf_base.h>
#include <hdf_device_desc.h>
#include <hdf_log.h>
#include <hdf_sbuf_ipc.h>
#include "v1_0/[driver_name]_interface_stub.h"
#define HDF_LOG_TAG [driver_name]_interface_driver
using namespace OHOS::HDI::[driver_namespace_name]::V1_0;
struct Hdf[driver_inter_name]Host {
struct IDeviceIoService ioService;
OHOS::sptr<OHOS::IRemoteObject> stub;
};
// 处理客户端请求的Dispatch方法
static int32_t [driver_inter_name]DriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
struct HdfSBuf *reply)
{
auto *hdf[driver_inter_name]Host = CONTAINER_OF(client->device->service, struct Hdf[driver_inter_name]Host, ioService);
OHOS::MessageParcel *dataParcel = nullptr;
OHOS::MessageParcel *replyParcel = nullptr;
OHOS::MessageOption option;
if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
HDF_LOGE("%{public}s: invalid data sbuf object to dispatch", __func__);
return HDF_ERR_INVALID_PARAM;
}
if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
HDF_LOGE("%{public}s: invalid reply sbuf object to dispatch", __func__);
return HDF_ERR_INVALID_PARAM;
}
return hdf[driver_inter_name]Host->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
}
// 驱动自身业务初始化的接口
static int Hdf[driver_inter_name]DriverInit(struct HdfDeviceObject *deviceObject)
{
HDF_LOGI("%{public}s: driver init start", __func__);
return HDF_SUCCESS;
}
// 将驱动对外提供的服务能力接口绑定到HDF框架
static int Hdf[driver_inter_name]DriverBind(struct HdfDeviceObject *deviceObject)
{
HDF_LOGI("%{public}s: driver bind start", __func__);
auto *hdf[driver_inter_name]Host = new (std::nothrow) Hdf[driver_inter_name]Host;
if (hdf[driver_inter_name]Host == nullptr) {
HDF_LOGE("%{public}s: failed to create create Hdf[driver_inter_name]Host object", __func__);
return HDF_FAILURE;
}
hdf[driver_inter_name]Host->ioService.Dispatch = [driver_inter_name]DriverDispatch;
hdf[driver_inter_name]Host->ioService.Open = NULL;
hdf[driver_inter_name]Host->ioService.Release = NULL;
auto serviceImpl = OHOS::HDI::[driver_namespace_name]::V1_0::[driver_idl_name]::Get(true);
if (serviceImpl == nullptr) {
HDF_LOGE("%{public}s: failed to get of implement service", __func__);
delete hdf[driver_inter_name]Host;
return HDF_FAILURE;
}
hdf[driver_inter_name]Host->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
OHOS::HDI::[driver_namespace_name]::V1_0::[driver_idl_name]::GetDescriptor());
if (hdf[driver_inter_name]Host->stub == nullptr) {
HDF_LOGE("%{public}s: failed to get stub object", __func__);
delete hdf[driver_inter_name]Host;
return HDF_FAILURE;
}
deviceObject->service = &hdf[driver_inter_name]Host->ioService;
HDF_LOGI("%{public}s: driver bind end", __func__);
return HDF_SUCCESS;
}
// 驱动释放资源的接口
static void Hdf[driver_inter_name]DriverRelease(struct HdfDeviceObject *deviceObject)
{
HDF_LOGI("%{public}s: driver release start", __func__);
if (deviceObject->service == nullptr) {
return;
}
auto *hdf[driver_inter_name]Host = CONTAINER_OF(deviceObject->service, struct Hdf[driver_inter_name]Host, ioService);
if (hdf[driver_inter_name]Host != nullptr) {
delete hdf[driver_inter_name]Host;
}
}
/*
* 定义驱动入口的对象必须为HdfDriverEntry在hdf_device_desc.h中定义类型的全局变量。
*/
struct HdfDriverEntry g_[driver_name]interfaceDriverEntry = {
.moduleVersion = 1,
.moduleName = "[driver_name]_service",
.Bind = Hdf[driver_inter_name]DriverBind,
.Init = Hdf[driver_inter_name]DriverInit,
.Release = Hdf[driver_inter_name]DriverRelease,
};
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/*
* 调用HDF_INIT将驱动入口注册到HDF框架中。
* 在加载驱动时HDF框架会先调用Bind函数再调用Init函数加载该驱动当Init调用异常时HDF框架会调用Release释放驱动资源并退出。
*/
HDF_INIT(g_[driver_name]interfaceDriverEntry);
#ifdef __cplusplus
}
#endif /* __cplusplus */

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) 2024 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.
*/
#ifndef [upper_driver_name]_UHDF_LOG_H
#define [upper_driver_name]_UHDF_LOG_H
#include "hdf_log.h"
#ifdef LOG_DOMAIN
#undef LOG_DOMAIN
#endif
#define LOG_DOMAIN 0xD002516
#endif //[upper_driver_name]_UHDF_LOG_H

View File

@ -0,0 +1,29 @@
#include "v1_0/[driver_name]_interface_service.h"
#include <hdf_base.h>
#include "[driver_name]_log.h"
#include "devhost_dump_reg.h"
#include "[driver_name]_dump.h"
#define HDF_LOG_TAG [driver_name]_interface_service
namespace OHOS {
namespace HDI {
namespace [driver_namespace_name] {
namespace V1_0 {
extern "C" [driver_idl_name] *[driver_inter_name]ImplGetInstance(void)
{
DevHostRegisterDumpHost(Get[driver_namespace_name]Dump);
HDF_LOGI("%{public}s: [driver_idl_name] init", __func__);
return new (std::nothrow) [driver_inter_name]Service();
}
int32_t [driver_inter_name]Service::Helloworld(const std::string& sendMsg, std::string& recvMsg)
{
HDF_LOGI("%{public}s: [driver_namespace_name]Service::Helloworld print", __func__);
return HDF_SUCCESS;
}
} // V1_0
} // [driver_namespace_name]
} // HDI
} // OHOS

View File

@ -0,0 +1,23 @@
#ifndef OHOS_HDI_[upper_driver_name]_V1_0_[upper_driver_name]INTERFACESERVICE_H
#define OHOS_HDI_[upper_driver_name]_V1_0_[upper_driver_name]INTERFACESERVICE_H
#include "v1_0/i[driver_name]_interface.h"
namespace OHOS {
namespace HDI {
namespace [driver_namespace_name] {
namespace V1_0 {
class [driver_inter_name]Service : public OHOS::HDI::[driver_namespace_name]::V1_0::[driver_idl_name] {
public:
[driver_inter_name]Service() = default;
virtual ~[driver_inter_name]Service() = default;
int32_t Helloworld(const std::string& sendMsg, std::string& recvMsg) override;
};
} // V1_0
} // [driver_namespace_name]
} // HDI
} // OHOS
#endif // OHOS_HDI_[upper_driver_name]_V1_0_[upper_driver_name]INTERFACESERVICE_H

View File

@ -0,0 +1,18 @@
# Copyright (c) 2022 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.
group("[driver_name]_entry") {
deps = [ "hdi_service:hdf_[driver_name]_service" ]
}

View File

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

View File

@ -0,0 +1,24 @@
{
"HcsconfigTemplete":"./templete/HcsconfigTemplete/hcsconfigTemplete.gen",
"IdlInterfaceTemplete":{
"buildgnTemplete":"./templete/IdlInterfaceTemplete/buildgnTemplete.gen",
"bundlejsonTemplete":"./templete/IdlInterfaceTemplete/bundlejsonTemplete.gen",
"idlInterfaceTemplete":"./templete/IdlInterfaceTemplete/idlInterfaceTemplete.gen"
},
"PeripheralTemplete": {
"DumpExampleTemplete": {
"buildgnTemplete":"./templete/PeripheralTemplete/DumpExampleTemplete/buildgnTemplete.gen",
"dumpCTemplete":"./templete/PeripheralTemplete/DumpExampleTemplete/dumpCTemplete.gen",
"dumpHTemplete":"./templete/PeripheralTemplete/DumpExampleTemplete/dumpHTemplete.gen"
},
"HdiServiceTemplete": {
"buildgnTemplete":"./templete/PeripheralTemplete/HdiServiceTemplete/buildgnTemplete.gen",
"driverTemplete":"./templete/PeripheralTemplete/HdiServiceTemplete/driverTemplete.gen",
"logHTemplete":"./templete/PeripheralTemplete/HdiServiceTemplete/logHTemplete.gen",
"serviceCppTemplete":"./templete/PeripheralTemplete/HdiServiceTemplete/serviceCppTemplete.gen",
"serviceHTemplete":"./templete/PeripheralTemplete/HdiServiceTemplete/serviceHTemplete.gen"
},
"buildgnTemplete":"./templete/PeripheralTemplete/buildgnTemplete.gen",
"bundlejsonTemplete":"./templete/PeripheralTemplete/bundlejsonTemplete.gen"
}
}