string GetCommandReturnType(CommandFlavor gen, TpmStruct resp, string methodName, out string returnFieldName) { returnFieldName = null; if (gen == CommandFlavor.AsyncCommand) { return("void"); } string returnType = "void"; var respFields = resp.NonTagFields; if (ForceJustOneReturnParm.Contains(methodName)) { respFields = respFields.Take(1).ToArray(); } if (respFields.Count() > 1) { return(resp.Name); } if (respFields.Count() == 1) { returnFieldName = respFields[0].Name; returnType = TransType(respFields[0]); } return(returnType); }
void GenCommand(TpmStruct req) { var resp = GetRespStruct(req); var cmdName = GetCommandName(req); string respType = resp.Name; string cmdCode = "TPM_CC." + cmdName; var reqFields = req.NonTagFields; var respFields = resp.NonTagFields; if (ForceJustOneReturnParm.Contains(cmdName)) { respFields = respFields.Take(1).ToArray(); } int numOutParms = respFields.Count(); string returnType = numOutParms == 1 ? respFields[0].TypeName : numOutParms == 0 ? "void" : respType; // javadoc annotation string annotation = req.Comment; if (reqFields.Count() != 0) { annotation += eol + eol + "Args:"; foreach (var f in reqFields) { annotation += eol + GetParamComment(f, " ", " "); } } if (respFields.Count() != 0) { annotation += eol + eol + "Returns:"; annotation += eol + GetReturnComment(respFields); } // method definition string paramList = This; string reqStructInitList = ""; foreach (var f in reqFields) { paramList += ", " + f.Name; // ": " + f.TypeName + ", "; reqStructInitList += (reqStructInitList.Length == 0 ? "" : ", ") + f.Name; } TabIn($"def {cmdName}({paramList}):"); WriteComment(annotation); Write($"req = {req.Name}({reqStructInitList})"); Write($"respBuf = {ThisMember}dispatchCommand({cmdCode}, req)"); respType = numOutParms > 0 ? ", " + respType : ""; if (numOutParms == 1) { Write($"res = {ThisMember}processResponse(respBuf{respType})"); Write($"return res.{respFields[0].Name} if res else None"); } else { Write($"return {ThisMember}processResponse(respBuf{respType})"); } TabOut(); Write($"# {cmdName}()"); Write(""); } // GenCommand()
} // GenCommands() void GenCommand(TpmStruct req) { const string NS = "tt."; var resp = GetRespStruct(req); var cmdName = GetCommandName(req); string respType = NS + resp.Name; string cmdCode = NS + "TPM_CC." + cmdName; var fields = req.NonTagFields; var respFields = resp.NonTagFields; if (ForceJustOneReturnParm.Contains(cmdName)) { respFields = respFields.Take(1).ToArray(); } int numOutParms = respFields.Count(); string returnType = numOutParms == 1 ? NsQualifiedType(respFields[0], NS) : numOutParms == 0 ? "void" : respType; // javadoc annotation string annotation = req.Comment + eol + eol; foreach (var f in fields) { annotation += GetParamComment(f) + eol; } annotation += GetReturnComment(respFields); WriteComment(annotation); // method definition string paramList = ""; string reqStructInitList = ""; foreach (var f in fields) { string parmTypeName = NsQualifiedType(f, NS); string comma = Separator(f, fields, ", "); paramList += f.Name + ": " + parmTypeName + ", "; reqStructInitList += f.Name + comma; } var indent = new string(' ', cmdName.Length + 2); string respParams = returnType == null ? "" : "res?: " + returnType; if (paramList != "") { Write($"{cmdName}({paramList}"); Write($"{indent}continuation: (err: TpmError, {respParams}) => void)"); } else { Write($"{cmdName}(continuation: (err: TpmError, {respParams}) => void)"); } Write("{"); TabIn(); Write($"let req = new {NS}{req.Name}({reqStructInitList});"); Write($"{ThisMember}dispatchCommand({cmdCode}, req, (respBuf: TpmBuffer): void => {{"); respType = numOutParms > 0 ? ", " + respType : ""; Write($"let res = {ThisMember}processResponse(respBuf{respType});"); string res = "", resField = ""; if (numOutParms > 0) { res = ", res"; if (numOutParms == 1) { resField = $"?.{respFields[0].Name}"; } } Write($"setImmediate(continuation, {ThisMember}lastError{res}{resField}); }});"); TabOut($"}} // {cmdName}()"); } // GenCommand()
} // GenCommands() void GenCommand(TpmStruct req) { var resp = GetRespStruct(req); var cmdName = GetCommandName(req); string respType = resp.Name; string cmdCode = "TPM_CC." + cmdName; var fields = req.NonTagFields; var respFields = resp.NonTagFields; if (ForceJustOneReturnParm.Contains(cmdName)) { respFields = respFields.Take(1).ToArray(); } int numOutParms = respFields.Count(); string returnType = numOutParms == 1 ? respFields[0].TypeName : numOutParms == 0 ? "void" : respType; // javadoc annotation string annotation = req.Comment + eol + eol; foreach (var f in fields) { annotation += GetParamComment(f) + eol; } annotation += GetReturnComment(respFields); WriteComment(annotation); // method definition string paramList = ""; string reqStructInitList = ""; foreach (var f in fields) { string comma = Separator(f, fields, ", "); paramList += f.TypeName + " " + f.Name + comma; reqStructInitList += f.Name + comma; } Write($"public {returnType} {cmdName}({paramList})"); TabIn("{"); Write($"{req.Name} req = new {req.Name}({reqStructInitList});"); string outParm = "null"; if (numOutParms != 0) { Write($"{respType} resp = new {respType}();"); outParm = "resp"; } Write($"DispatchCommand({cmdCode}, req, {outParm});"); if (numOutParms == 0) { Write("return;"); } else if (numOutParms == 1) { Write("return resp." + respFields[0].Name + ";"); } else { Write("return resp;"); } TabOut("}"); } // GenCommand()