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 { idlTransferType } from '../../template/functypemap_template'; 19import { format } from 'util'; 20import { getTab, replaceAll } from '../../common/tool'; 21 22// 常用类型转换表, 将C语言常见类型(key)转换为remote data读写函数使用的类型(value) 23// 例如 ErrCode 类型在框架中的系统原型为int类型,这里映射成int32_t, 24// 因为int32_t类型在 DATA_W_MAP/DATA_R_MAP 表中有对应的读写数据方法(WriteInt32/ReadInt32) 25const TYPE_DEF_MAP = new Map([ 26 ['std::string', 'string'], 27 ['char *', 'string'] 28]); 29 30export function getParcelType(srcType: string) { 31 let parcelType = TYPE_DEF_MAP.get(srcType); 32 return parcelType === undefined ? srcType : parcelType; 33} 34 35// idlTransferType 36export function getIdlType(cType: string) { 37 let rawType = getParcelType(cType); 38 for (let index = 0; index < idlTransferType.length; index++) { 39 if (rawType === idlTransferType[index].fromType) { 40 return idlTransferType[index].tranferContent; 41 } 42 } 43 return cType; 44} 45 46export function isReturn(type: string) { 47 if (type) { 48 if (type.indexOf('&')>0 || type.indexOf('**')>0) { 49 return true; 50 } 51 } 52 return false; 53} 54 55export function getIdlFuncParamStr(funcObj: FuncObj) { 56 let idlParams = ''; 57 for (let i = 0; i < funcObj.parameters.length; ++i) { 58 let type = getIdlType(funcObj.parameters[i].type); 59 // idlParams += (i === 0) && (funcObj.returns !== 'void') ? '' : ', '; 60 if (isReturn(funcObj.parameters[i].type)) { 61 idlParams += '[out] ' + type + ' ' + funcObj.parameters[i].name + ', '; 62 } else { 63 idlParams += '[in] ' + type + ' ' + funcObj.parameters[i].name + ', '; 64 } 65 } 66 if (funcObj.returns !== 'void') { 67 let retType = getIdlType(funcObj.returns); 68 let outName = funcObj.name + 'Out' 69 idlParams += '[out] ' + retType + ' ' + outName; 70 } else { 71 // 如果返回值是void, 去掉参数列表结尾逗号 72 idlParams = idlParams.substring(0, idlParams.length - 2); 73 } 74 return idlParams; 75} 76 77export function doGenIdlFile(rootInfo: HdfRootInfo, fileContent: string): string { 78 let funcTab = getTab(1); 79 let idlFuncDefine = ''; 80 let funcList: FuncObj[] = rootInfo.funcs; 81 for (let i = 0; i < funcList.length; ++i) { 82 // 生成idl接口定义 83 let paramIdlStr = getIdlFuncParamStr(funcList[i]); 84 idlFuncDefine += (i === 0) ? '' : '\n' + funcTab; 85 idlFuncDefine += format('%s(%s);', funcList[i].name, paramIdlStr); 86 } 87 let upperName = rootInfo.driverName.substring(0, 1).toLocaleUpperCase(); 88 let marcoName = upperName + rootInfo.driverName.substring(1, rootInfo.driverName.length); 89 fileContent = replaceAll(fileContent, '[driverName]', rootInfo.driverName); 90 fileContent = replaceAll(fileContent, '[marcoName]', marcoName); 91 fileContent = replaceAll(fileContent, '[idlFunDeclare]', idlFuncDefine); 92 return fileContent; 93} 94 95// 每个方法一个文件 96export function genIdlFile(rootInfo: HdfRootInfo, filePath: string, fileContent: string) { 97 fileContent = doGenIdlFile(rootInfo, fileContent); 98 fs.writeFileSync(filePath, fileContent); 99}