留给自己用的


package com.huawei.sdtrp.command;

import java.util.logging.Logger;

/**
* <pre>
* 命令解析器,默认实现
* 方法一:
* 使用ClassLorder来加载(包名+CMD+"Command")命令类路径
* 具有包名限制
* 方法二:
* 在实际Action中,采用Spring注入包名称属性,再使用命令解析器实例化,具体的命令对象(可行,但是设计不太合理[不是Action要做的事情])
* 方法三:
* 有没有另外的方式,来构造命令对象???(只知道命令字符串)
* </pre>
* @author cWX194449
*
*/
public class CommandParserImpl implements CommandParser {

private static Logger log = Logger.getLogger(CommandParserImpl.class.getCanonicalName());

private String packageNameSpace = "com.huawei.sdtrp.command";//命令实现类包路径

@Override
public Command parse(String command) {
//这种方法,包名定死了
// String packageNameSpace = "com.huawei.sdtrp.command" ;
String suffix = "Command";
String fullCommandName = packageNameSpace.concat(".").concat(command).concat(suffix);

try {
// return (Command)Class.forName(fullCommandName).newInstance();
return (Command)this.getClass().getClassLoader().loadClass(fullCommandName).newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}

return null;


/*
String suffix = "Command";

ActionContext ac = ActionContext.getContext();
ac.

WebApplicationContext ctx = WebApplicationContextUtils
.getWebApplicationContext(config.getServletContext());

return (Command)ctx.getBean(command.concat(suffix));

return null;
*/
}

}


//Action
package com.huawei.sdtrp.action;

import com.huawei.sdtrp.command.Command;
import com.huawei.sdtrp.command.CommandParser;
import com.huawei.sdtrp.model.ExecutedResult;
import com.huawei.sdtrp.model.Terminal;
import com.opensymphony.xwork2.ActionSupport;

/**
* 基本Action
* @author cWX194449
*
*/
public abstract class BaseAction extends ActionSupport {

private static final long serialVersionUID = 1L;

private Terminal terminal ;//表单属性搜集

public Terminal getTerminal() {
return terminal;
}

public void setTerminal(Terminal terminal) {
this.terminal = terminal;
}

private CommandParser commandParser ;//注入

public CommandParser getCommandParser() {
return commandParser;
}

public void setCommandParser(CommandParser commandParser) {
this.commandParser = commandParser;
}

private ExecutedResult executedResult ;

public ExecutedResult getExecutedResult() {
return executedResult;
}

public void setExecutedResult(ExecutedResult executedResult) {
this.executedResult = executedResult;
}

@Override
public String execute() throws Exception {
// Object object = this.commandParser.parse(this.terminal.getTerminalData().getCmd()).execute();
// Object object = this.generateCommand(terminal).execute();
// return null == object ?"NULL":object.toString();
if(this.validateParameters()){
this.executedResult = this.generateCommand(terminal).execute();
}
if(null == this.executedResult){//异常,构造命令执行结果
// String msg = this.terminal.getTerminalData().getCmd().concat("命令请求异常");
// throw new Exception(msg);
//TODO 异常命令执行结果,构造
this.executedResult = new ExecutedResult();
}
/*以流的形式,响应终端*/
this.responseTerminal();
// return this.executedResult.getResultStatus();
return null;
}

/**
* 抽象工厂CommandParser产生Command对象
* @param terminal
* @return
*/
public Command generateCommand(Terminal terminal){
// return this.commandParser.parse(this.terminal.getTerminalData().getCmd());
// Command command = this.commandParser.parse(this.terminal.getTerminalData().getCmd());
Command command = this.commandParser.parse(this.terminal.getCmd());
command.init(terminal);
return command ;
};

/**
* 请求参数,检验(default:true相当于不验证)
* 若false,需要重新赋值给命令执行结果
* (过滤非法请求)
* @return
*/
public boolean validateParameters(){
return true;
}

/**
* <pre>
* 以流的形式,写回到终端
* 根据每个每个命令,返回的内容不一样,需要自已定义实现化
* </pre>
*/
public abstract void responseTerminal();
}

//Action
package com.huawei.sdtrp.action;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.huawei.sdtrp.model.ExecutedResult;
import com.opensymphony.xwork2.ActionContext;

/**
* 注册动作
* 1、请求数据校验
* 2、授权检查
* @author cWX194449
*
*/
public class RegisterationAction extends BaseAction {

private static final long serialVersionUID = -4767244117752570601L;

private String encoding = "UTF-8";

@Override
public boolean validateParameters() {
// TODO Auto-generated method stub
return true;
}

@Override
public void responseTerminal() {
ActionContext context = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
Map session = context.getSession();

ExecutedResult executedResult = this.getExecutedResult();
try {
OutputStream out = response.getOutputStream();
byte[] cmdBytes = "Cmd=".concat(executedResult.getCmd()).getBytes(encoding);
out.write(cmdBytes);//命令回写
out.write("&".getBytes(encoding));//分隔符

byte[] codeBytes = "Code=".concat(executedResult.getCode()).getBytes(encoding);
out.write(codeBytes);//状态回写
out.write("&".getBytes(encoding));//分隔符

byte[] descriptionBytes = "Description=".concat(executedResult.getDescription()).getBytes(encoding);
out.write(descriptionBytes);
// out.write("&".getBytes(encoding));//分隔符

out.flush();
// out.close();
} catch (IOException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}

}

<think>我们正在讨论FreeRTOS的配置文件FreeRTOSConfig.h和内核实现task.c。用户询问freertos.h头文件是否提供用户自定义函数声明的位置。根据FreeRTOS的常见结构,freertos.h通常是一个汇总头文件,用于包含其他FreeRTOS头文件。用户自定义的函数声明通常不会放在freertos.h中,而是放在自己的应用代码中或者自定义的头文件中。但是,用户可能希望在某些情况下在FreeRTOS的头文件中添加自定义函数声明。实际上,FreeRTOS并不建议直接修改其内核头文件(如freertos.h),因为这样会影响可移植性。通常,用户自定义的函数声明应该放在用户自己的头文件中,然后在需要的地方包含该头文件。这样,当FreeRTOS升级时,用户自定义的修改不会与官方代码冲突。然而,参考引用[1]中提到,用户可以通过FreeRTOSConfig.h进行系统定制。FreeRTOSConfig.h是用户自定义配置文件,用于设置系统参数和功能剪裁。这个文件是用户应用程序的一部分,不是内核代码的一部分。因此,如果用户想要在FreeRTOS的上下文中声明自定义函数,建议在FreeRTOSConfig.h中声明?实际上,FreeRTOSConfig.h主要用于配置宏定义,而不是函数声明。函数声明应该放在单独的头文件中。所以,回答用户的问题:freertos.h头文件本身并不提供用户自定义函数声明的位置。用户应该在自己的头文件中声明自定义函数,然后在需要的地方包含该头文件。但是,我们注意到用户可能是在特定的上下文中提出这个问题。也许用户看到某个示例代码在某个位置声明了自定义函数。我们可以进一步说明:在FreeRTOS中,用户自定义的函数(例如任务函数、钩子函数等)通常是在用户自己的C文件中声明和定义的。例如:-任务函数:在创建任务时,任务函数作为参数传入,因此需要在创建任务的C文件中声明(或定义在之前)。-钩子函数(如空闲钩子、Tick钩子等)需要在配置文件中通过宏(如configUSE_IDLE_HOOK)启用,并在用户文件中实现,然后在FreeRTOSConfig.h中配置相应的宏指向这些函数(例如,通过extern声明钩子函数,并在某个C文件中定义)。具体到钩子函数的声明,通常的做法是:1.在FreeRTOSConfig.h中,通过宏来配置使用钩子函数,例如:#defineconfigUSE_IDLE_HOOK12.在用户代码的某个C文件中实现钩子函数,例如:voidvApplicationIdleHook(void){...}3.如果需要在多个文件中使用这个函数的声明,可以在一个用户自定义的头文件中声明,然后在需要的地方包含该头文件。但是,FreeRTOSConfig.h并不是用来声明函数的地方,它主要是配置宏。因此,总结:freertos.h不用于用户自定义函数声明。用户自定义函数应该在用户自己的头文件中声明,并在C文件中定义。对于FreeRTOS的钩子函数,用户需要在C文件中实现,并且不需要在freertos.h中声明,因为FreeRTOS内核在调用钩子函数时已经通过链接器找到了这些函数(前提是用户实现了它们)。但是,有一个特殊情况:如果用户需要在一个头文件中声明一个函数,而这个头文件会被多个C文件包含,那么用户应该创建一个自己的头文件(如my_functions.h)来存放这些声明。所以,回答用户的问题:FreeRTOS的freertos.h头文件不提供用户自定义函数声明的位置。用户应该在自己的头文件中声明自定义函数。为了更清晰,我们可以举例:假设用户有一个自定义函数`voidmy_function(void);`,那么应该这样:-在my_functions.h中:`voidmy_function(void);`-在my_functions.c中:`voidmy_function(void){...}`-在需要使用这个函数的C文件中包含my_functions.h。这样,用户自定义函数的声明就不会与FreeRTOS的头文件混在一起。根据引用[2]提到的task.c,这是FreeRTOS内核的一部分,用户不应该修改它。同样,freertos.h也是内核头文件的一部分,用户也不应该修改。因此,我们强烈建议不要修改FreeRTOS的内核头文件(如freertos.h、task.h等),而是使用用户自己的文件。最后,生成相关问题:§§相关问题§§1.如何在FreeRTOS中自定义钩子函数?2.FreeRTOSConfig.h文件主要配置哪些内容?3.为什么不应该修改FreeRTOS的内核头文件?4.在FreeRTOS中,如何组织用户自定义的头文件和源文件?</think>### FreeRTOS freertos.h 用户自定义函数声明位置分析 在FreeRTOS中,**`freertos.h` 头文件本身不提供用户自定义函数声明的位置**。根据FreeRTOS的设计规范和源码结构: 1. **`freertos.h` 的核心作用** `freertos.h` 是FreeRTOS的主包含文件,主要用于集成其他核心头文件(如 `task.h`, `queue.h`, `semphr.h` 等)。其典型实现如下: ```c #ifndef FREERTOS_H #define FREERTOS_H #include "task.h" #include "queue.h" #include "timers.h" // ...其他内核头文件 #endif ``` 它本身**不包含任何用户自定义函数的声明接口**,也不建议用户直接修改此文件[^1][^2]。 2. **用户自定义函数的推荐位置** 用户自定义函数(如任务函数、回调函数等)应通过以下方式管理: - **独立头文件声明** 在项目目录中创建自定义头文件(如 `user_app.h`),声明函数: ```c // user_app.h void vUserTaskFunction(void *pvParameters); // 自定义任务函数 void vCustomCallback(TimerHandle_t xTimer); // 自定义定时器回调 ``` - **源文件定义** 在 `.c` 文件(如 `user_app.c`)中实现函数: ```c #include "user_app.h" void vUserTaskFunction(void *pvParameters) { while(1) { // 任务逻辑 } } ``` 3. **特殊场景:钩子函数(Hooks)** 若需实现FreeRTOS钩子函数(如空闲任务钩子),需通过 **`FreeRTOSConfig.h`** 配置启用,并在用户文件中声明: ```c // FreeRTOSConfig.h #define configUSE_IDLE_HOOK 1 // 启用空闲钩子 ``` ```c // user_hooks.c void vApplicationIdleHook(void) { // 自定义空闲任务处理 } ``` 钩子函数**无需在 `freertos.h` 中声明**,内核通过配置宏自动调用[^1]。 ### 关键原则 - **不修改内核文件**:直接修改 `freertos.h` 或内核头文件会破坏可移植性,升级FreeRTOS版本时将导致兼容问题。 - **分离用户代码**:自定义函数应完全置于用户目录中,通过 `#include` 引入到需要的位置[^2]。 - **配置优先**:系统级定制(如剪裁功能)通过 `FreeRTOSConfig.h` 实现,而非修改内核头文件[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值