• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15
16import * as fs from 'fs';
17import { FuncObj, HdfRootInfo } from "../datatype";
18import { format } from 'util';
19import { getTab, replaceAll } from '../../common/tool';
20
21// 干脆这个用来表示hdf的service.h算了, 暂时先把hdf和sa的分开
22
23// 生成方法实现
24export function getServiceFuncParamStr(funcObj: FuncObj) {
25  let paramStr = '';
26  for (let i = 0; i < funcObj.parameters.length; ++i) {
27    // paramStr += (i === 0) ? '' : ', ';
28    // paramStr += funcObj.parameters[i].type + ' ' + funcObj.parameters[i].name + ', ';
29    paramStr += format('const %s& %s, ', funcObj.parameters[i].type, funcObj.parameters[i].name);
30  }
31  if (funcObj.returns !== 'void') {
32    let outName = funcObj.name + 'Out'
33    if (funcObj.returns === 'string') {
34      funcObj.returns = 'std::string';
35    }
36    paramStr += funcObj.returns + '& ' + outName;
37  } else {
38    // 如果返回值是void, 去掉参数列表结尾逗号
39    paramStr = paramStr.substring(0, paramStr.length - 2);
40  }
41  return paramStr;
42}
43
44export function doGenServiceHFile(rootInfo: HdfRootInfo, fileContent: string): string {
45  let hdiServiceFuncH = '';
46  let funcTab = getTab(1);
47  let funcList: FuncObj[] = rootInfo.funcs;
48  for (let i = 0; i < funcList.length; ++i) {
49    // xxx_interface_service.h方法定义
50    let paramStr = getServiceFuncParamStr(funcList[i]);
51    hdiServiceFuncH += (i === 0) ? '' : '\n' + funcTab;
52    hdiServiceFuncH += format('int32_t %s(%s) override;', funcList[i].name, paramStr)
53  }
54  let upperName = rootInfo.driverName.substring(0, 1).toLocaleUpperCase();
55  let marcoName = upperName + rootInfo.driverName.substring(1, rootInfo.driverName.length);
56  let upperDriverName = rootInfo.driverName.toLocaleUpperCase();
57  fileContent = replaceAll(fileContent, '[driverName]', rootInfo.driverName);
58  fileContent = replaceAll(fileContent, '[marcoName]', marcoName);
59  fileContent = replaceAll(fileContent, '[driverUpperName]', upperDriverName);
60  fileContent = replaceAll(fileContent, '[serviceFuncDeclare]', hdiServiceFuncH);
61  return fileContent;
62}
63
64export function genServiceHFile(rootInfo: HdfRootInfo, filePath: string, fileContent: string) {
65  fileContent = doGenServiceHFile(rootInfo, fileContent);
66  fs.writeFileSync(filePath, fileContent);
67}