文章目录
需求场景
需求
系统提供关机和重启的接口,应用需要自己去控制设备关机、重启。
场景
个人在十多年的设备端应用开发和系统应用开发的经理遇到在对接客户过程中,不管RK、全志、MTK 平台开发中,我司提供系统、硬件 方案,或者主板方案 时候,客户自己定制上层软件或者客户作为普通应用,需要和我司系统Launcher联调控制,要求我司提供 系统的重启、关机功能接口。
经历
在10多年对接客户过程中,不同客户,反反复复会问到实现方案,需要提供协助,要求系统提供支持。个人其实也不太愿意将基本的问题讲得明明白白,奈何相同的问题连续支持了10多年。 此小问题觉得蛮有意思的,就总结下方便后续客户沟通,支持。
一、解决思路和方案
实际困难点情况
-
普通应用开发者角度:
基本上提这个需求的所有的客户,他们的应用工程师在接收到这个需求的时候,想都不想就是这个跟应用无关,需要系统支持;
对于从手机端应用转到设备端开发应用的时候,思维一般都停留在手机端应用开发的思维,如果需要控制设备端的功能【节点控制、系统功能控制、gpio控制、串口控制、tcpip控制、can协议控制】,基本上第一感觉就是GG了,头皮发麻。 比较排斥,甩锅到系统。 -
系统开发者角度:机器功能有的,系统工程师或者驱动工程师也不太会的,大家普遍认为系统应用都能够控制,为啥还需要系统去协助呢?自己看下系统功能怎么实现,如果遇到问题再具体说明下遇到什么问题,而不是直接抛过来;
系统工程师、驱动工程师本身对应用功能比较排斥,非系统处理的,应用自己搞得定。 -
不同的平台RK、全志、MTK,大多数应用工程师认为搞定了,换了个平台 直接歇菜,也不知道具体原因在哪里?
所以:一些基本的功能实现,系统工程师和应用工程师都会扯皮的,特别是小公司;普通应用工程师、系统工程师确实搞不定,思路不对,各种扯皮。
普遍思路
应用开发者可能经过大量的查找资料
用Shell 命令实现
我遇到的客户工程师个部分系统工程师的普遍思路,直接命令来干 ;网上基本上也是这个思路;好多系统工程师也给的这样的方案。
案例代码如下:
public static void reboot() {
String rebootCMd = "reboot";
execCommand(rebootCMd, false, true);
}
public static CommandResult execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) {
int result = -1;
if (commands == null || commands.length == 0) {
return new CommandResult(result, null, null);
}
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
DataOutputStream os = null;
try {
if (BuildConfig.CHIP_PLATFORM.equals(RK.name())) {
process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
} else if (BuildConfig.CHIP_PLATFORM.equals(ALLWINNER.name())) {
process = Runtime.getRuntime().exec(COMMAND_SH);
}
os = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command == null) {
continue;
}
// donnot use os.writeBytes(commmand), avoid chinese charset error
os.write(command.getBytes());
os.writeBytes(COMMAND_LINE_END);
os.flush();
}
os.writeBytes(COMMAND_EXIT);
os.flush();
result = process.waitFor();
if (isNeedResultMsg) {
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (IOException e) {
e.printStackTrace();