[经验分享] @ohos.systemParameterEnhance系统参数接口调用:控制设备硬件(执行shell命令方式) 原创

润开鸿_贾佳豪 显示全部楼层 发表于 2024-8-27 03:13:09

本文介绍如何使用应用 @ohos.systemParameterEnhance (系统参数)(系统接口)来控制设备硬件,可以通过它在系统中执行一段shell命令,从而实现控制设备的效果。接下来以一个实际的样例来演示如何通过它来控制设备以太网接口

开源地址:https://gitee.com/from-north-to-north/ohos.systemParameterEnhance

开发环境

  • DAYU200 rk3568开发板
  • OpenHarmony 4.1r
  • API 10 (full sdk)
  • DevEco Studio 4.1 Release

1.应用开发部分

1.首先安装full sdk

2.修改full_sdk/toolchains/lib/UnsgnedDebugProfileTemplate.json文件

  • apl的值从 normal改为 system_basic
  • app-feature值的由 hos_normal_app改为 hos_system_app

3.新建一个OpenHarmony应用工程,调用如下方法,然后正常自动签名安装。

import systemparameter from '@ohos.systemParameterEnhance';

        Text("关闭eth0以太网接口")
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            try {
              systemparameter.setSync("sys.ifconfig.eth0", "down");
            }catch(e){
              console.log("set unexpected error: " + e);
            }
          })

        Text("开启eth0以太网接口")
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            try {
              systemparameter.setSync("sys.ifconfig.eth0", "up");
            }catch(e){
              console.log("set unexpected error: " + e);
            }
          })

4.然后获取该hap的证书指纹,进行应用特权配置。

hdc shell "bm dump -n 应用的bundle_name | grep finger"

image.png

image.png

5.配置应用特权。提取当前开发板中的特权配置文件install_list_capability.json,文件位于/etc/app/中。

hdc file recv /etc/app/install_list_capability.json D:\

在install_list_capability.json中添加 应用的配置信息

image.png 将特权配置文件install_list_capability.json推送回系统中,覆盖系统配置。然后 重启系统使得系统配置生效。

hdc shell "mount -o remount,rw /"
hdc file send d:\install_list_capability.json /etc/app/install_list_capability.json
hdc shell reboot

系统开发部分

1.新建init services如下,放到开发板/system/etc/init下

{
    "jobs" : [{
            "name" : "param:sys.ifconfig.eth0=up",
            "condition" : "sys.ifconfig.eth0=up",
            "cmds" : [
                "start if_eth0_up"
            ]
        }, {
            "name" : "param:sys.ifconfig.eth0=down",
            "condition" : "sys.ifconfig.eth0=down",
            "cmds" : [
                "start if_eth0_down"
            ]
        }
    ], 
        "services" : [{
            "name" : "if_eth0_up",
            "start-mode" : "condition",
            "path" : ["/system/bin/ifconfig", "eth0", "up"],
            "disabled" : 1,
            "sandbox" : 0,
            "uid" : "root",
            "gid" : ["shell"],
            "once" : 1,
            "secon" : "u:object_r:sh_exec:s0"
        },
        {
            "name" : "if_eth0_down",
            "start-mode" : "condition",
            "path" : ["/system/bin/ifconfig", "eth0", "down"],
            "disabled" : 1,
            "sandbox" : 0,
            "uid" : "root",
            "gid" : ["shell"],
            "once" : 1,
            "secon" : "u:object_r:sh_exec:s0"
        }
    ]
}
hdc shell "mount -o remount,rw /"
hdc file send a.cfg /system/etc/init
hdc shell reboot

image.png

2.关闭selinux

临时关闭可进入shell使用如下命令:
hdc shell setenforce 0

永久性生效,执行以下命令:
hdc shell mount -o rw,remount /
hdc shell "sed -i 's/enforcing/permissive/g' /system/etc/selinux/config"
hdc shell "cat /system/etc/selinux/config |grep SELINUX="
hdc shell reboot

# 或者修改源码
1、base/security/selinux_adapter/selinux.gni里面把selinux_enforce改为false
2、vendor/hihope/rk3568/config.json里面把build_selinux改为false

观察运行效果

在终端执行ifconfig命令来观察以太网接口是否被关闭

拓展

1.如果需要执行/vendor/bin下面的bin文件,cfg文件需要放置在/vendor/etc/init下。

2.可以操作接口执行一下shell命令来控制dayu200的led灯 image.png

参考链接

1.https://laval.csdn.net/64b34a863a5d4a7c4342eade.html

2.https://forums.openharmony.cn/forum.php?mod=viewthread&tid=2109

3.https://gitee.com/openharmony/docs/blob/OpenHarmony-v4.1-Release/zh-cn/application-dev/reference/apis-basic-services-kit/js-apis-system-parameterEnhance-sys.md#systemparametergetsync

4.https://forums.openharmony.cn/forum.php?mod=viewthread&tid=2108

无用

©著作权归作者所有,转载或内容合作请联系作者

您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部