K1 OH5.0 系统定制说明
修订记录
| 修订版本 | 修订日期 | 修订说明 |
| 001 | 2025-01-13 | 初始版本 |
| 002 | 2025-04-18 | 添加“camera通路定制”章节 |
| 003 | 2025-05-27 | 添加“USB通信定制”章节 |
| 004 | 2025-05-29 | 添加“支持存储外设”章节 |
1. UI 定制
1.1 更换 Boot Logo
Boot Logo 可存放在以下两个位置:
-
通用 Boot Logo: 路径:
device/soc/spacemit/k1/kernel/boot/bootfs该目录下包含同一图案的四个方向版本(0°、90°、180°、270°)。 每个方案根据需要选择不同方向的 Logo, 一般情况下使用 0° 图像。|-- boot_logo_spacemit_0.bmp
|-- boot_logo_spacemit_180.bmp
|-- boot_logo_spacemit_270.bmp
`-- boot_logo_spacemit_90.bmp -
特定方案专用 Boot Logo: 路径:
device/board/spacemit/xxx/kernel/boot/bootfs示例:|-- boot_logo_ry_270.bmp
Boot Logo 需为 32-bit BMP 格式图像。
添加新 Logo 后,在对应方案目录下修改
device/board/spacemit/musepaper/kernel/build_kernel.sh 文件,
在脚本中加入拷贝命令以便在打包时替换:
cp ${OHOS_SOURCE_ROOT}/device/soc/${DEVICE_BOARD}/k1/kernel/boot/bootfs/boot_logo_spacemit_0.bmp ${OHOS_IMAGES_DIR}/bootfs/bianbu.bmp
1.2 更换 Kernel Logo
Kernel Logo 显示在 Boot Logo 与开机动画之间,用于避免启动过程中的黑屏。一般情况下,Kernel Logo 与 Boot Logo 图案保持一致。
**Kernel Logo **放置在 device/board/spacemit/common/kernel_logo,如下:
|-- kernel_logo_ry_270.ppm
|-- kernel_logo_spacemit_0.ppm
|-- kernel_logo_spacemit_180.ppm
|-- kernel_logo_spacemit_270.ppm
`-- kernel_logo_spacemit_90.ppm
Kernel Logo 使用 PPM 格式。
添加新 Logo 后,在对应方案的
device/board/spacemit/xxx/kernel/build_kernel.sh
脚本中添加以下命令,将 logo 文件拷贝至内核编译路径:
cp -rf ${OHOS_SOURCE_ROOT}/device/board/${DEVICE_BOARD}/common/kernel_logo/kernel_logo_spacemit_0.ppm ${KERNEL_BUILD_ROOT}/drivers/video/logo/logo_linux_clut224.ppm
1.2.1 BMP 转 PPM
-
安装转换工具
sudo apt-get install netpbm -
执行脚本,将 BMP 转为 PPM, 转换后会在当前目录生成
xxx.ppm文件。./bmp2ppm.sh xxx.bmp
脚本内容如下:
#!/bin/bash
if [ " $1" == " " ];
then
echo "usage:$0 bmp_file"
exit 0
fi
if [ -f "$1" ]
then
echo $1
else
echo "no find file [$1]"
exit 0
fi
name=${1%%.*}
bmptopnm $1 > $name.pnm
pnmquant 224 $name.pnm > $name.clut224.pnm
pnmtoplainpnm $name.clut224.pnm > $name.ppm
rm $name.pnm $name.clut224.pnm
1.3 更换开机动画
开机动画可以通过两种方式更换:
- 更换原生动画
- 添加定制动画
1.3.1 更换原生动画
原生开机动画文件位于:
foundation/graphic/graphic_2d/frameworks/bootanimation/data/bootpic.zip
可以直接替换该 zip 包来自定义动画。
zip 包结构如下:
|-- config.json
`-- OH_bootAni compressed
|-- OH_bootAni_00000.jpg
|-- OH_bootAni_00001.jpg
|-- OH_bootAni_00002.jpg
|-- OH_bootAni_00003.jpg
|-- ...
|-- OH_bootAni_00146.jpg
|-- OH_bootAni_00147.jpg
|-- OH_bootAni_00148.jpg
`-- OH_bootAni_00149.jpg
config.json 文件示例:
{
"Remark": "FrameRate Support 30, 60 frame rate configuration",
"FrameRate": 30
}
若要自定义动画,请保持与上述文件结构和命名格式一致。
1.3.2 添加定制动画
若要为特定方案添加自定义动画,可在
vendor/spacemit/xxx
目录下新建 bootanimation_custom_config.json 文件,配置如下:
{
"cust.bootanimation.pics": "/sys_prod/etc/bootanimation/cust_bootpic.zip",
"cust.bootanimation.sounds": "/sys_prod/etc/bootanimation/cust_bootsound.wav",
"cust.bootanimation.video": "/sys_prod/etc/bootanimation/cust_bootvideo.mp4"
}
然后将 对应的动画资源文件放置在配置路径下(如 /sys_prod/etc/bootanimation/)。
1.4 更换开机音乐
开机音乐可以通过两种方式更换:
- 更换原生音乐
- 添加定制音乐
1.4.1 更换原生音乐
原生开机音乐位于:
foundation/graphic/graphic_2d/frameworks/bootanimation/data/bootsound.wav
可以直接替换该 .wav 文件为自定义音乐。
建议音乐时长不要过长(建议 10s 内)。
1.4.2 添加定制音乐
若要为特定方案添加自定义开机音乐,可在
vendor/spacemit/xxx
目录下新建 bootanimation_custom_config.json 文件,并配置如下:
{
"cust.bootanimation.pics": "/sys_prod/etc/bootanimation/cust_bootpic.zip",
"cust.bootanimation.sounds": "/sys_prod/etc/bootanimation/cust_bootsound.wav",
"cust.bootanimation.video": "/sys_prod/etc/bootanimation/cust_bootvideo.mp4"
}
将自定义的开机音乐文件放置在配置路径下即可。
1.5 更换开机视频
开机视频可通过以下两种方式更换:
- 更换原生开机视频
- 添加定制视频
1.5.1 更换原生视频
将原生路径 foundation/graphic/graphic_2d/frameworks/bootanimation/data/bootvideo.mp4 下的视频替换为新文件。
要求:
- 编码格式为 H.264
- 分辨率比例与屏幕一致
1.5.2 添加定制视频
在 vendor/spacemit 目录下添加配置文件 bootanimation_custom_config.json,内容示例如下:
{
"cust.bootanimation.pics": "/sys_prod/etc/bootanimation/cust_bootpic.zip",
"cust.bootanimation.sounds": "/sys_prod/etc/bootanimation/cust_bootsound.wav",
"cust.bootanimation.video": "/sys_prod/etc/bootanimation/cust_bootvideo.mp4"
}
并将对应的定制视频放入上述路径中即可。
1.6 开机视频切换为开机动画
系统默认使用开机视频。如需切换为开机动画,请关闭开机视频播放功能:
在 vendor/spacemit/xxx/config.json 文件中,找到 "component": "graphic_2d",并添加以下配置项:"graphic_2d_feature_player_framework_enable = false"
示例修改如下:
fuqiang@snode2:~/workspace/oh5_r_release/vendor/spacemit/musepaper2$ git diff
diff --git a/musepaper2/config.json b/musepaper2/config.json
index d70ad50..ef16ca6 100755
--- a/musepaper2/config.json
+++ b/musepaper2/config.json
@@ -21,7 +21,8 @@
"subsystem": "graphic",
"components": [
{
"component": "graphic_2d",
"features": [
"graphic_2d_feature_ace_enable_gpu = true",
"graphic_2d_feature_parallel_render_enable = true",
- "graphic_2d_feature_rs_enable_eglimage = true"
+ "graphic_2d_feature_rs_enable_eglimage = true",
+ "graphic_2d_feature_player_framework_enable = false"
]
},
{
1.7 去除对话框
1.7.1 去除 USB 连接时弹出的确认框
相关 hap 的源码位于 base/usb/usb_manager/frameworks/dialog/dialog_ui/usb_right_dialog
- 临时方案:直接删掉对应的 hap 即可
D:\>hdc shell
# mount -o remount,rw /
# cd /system/app/com.usb.right
# rm usb_right_dialog.hap
# cd /
# rm -rf /data/*
# reboot
- 彻底方案:在固件中删掉该 hap, 在
vendor/spacemit/xxx/preinstall-config/install_list.json里删除以下内容:
{
"app_dir": "/system/app/com.usb.right",
"removable": true
},
重新编译固件即可
2. 应用定制
2.1 预置应用
2.1.1 添加预置应用
-
将 hap 放置于
applications/standard/hap -
修改
applications/standard/hap/BUILD.gn配置,如下例(以浏览器 hap 为例):diff --git a/BUILD.gn b/BUILD.gn
index c9ea451..87ed7aa 100755
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -407,6 +407,13 @@ ohos_prebuilt_etc("tetris_hap") {
subsystem_name = "applications"
}
+ohos_prebuilt_etc("broswer_hap") {
+ source = "Broswer.hap"
+ module_install_dir = "app/ohos.samples.browser"
+ part_name = "prebuilt_hap"
+ subsystem_name = "applications"
+}
+
group("hap") {
deps = [
":calendarData_hap",
@@ -464,6 +471,7 @@ group("hap") {
"//applications/standard/hap:wuziqi_hap",
"//applications/standard/hap:deviceinfo_hap",
"//applications/standard/hap:tetris_hap",
+ "//applications/standard/hap:broswer_hap",
]
if (defined(product_name) && product_name == "watchos") {
deps -= [ -
修改
vendor/spacemit/xxx/preinstall-config/install_list.json配置进行安装,如下例:diff --git a/musepaper/preinstall-config/install_list.json b/musepaper/preinstall-config/install_list.json
index dd859c2..0107e85 100755
--- a/musepaper/preinstall-config/install_list.json
+++ b/musepaper/preinstall-config/install_list.json
@@ -220,6 +220,10 @@
"app_dir": "/system/app/ohos.samples.game2048",
"removable": false
},
+ {
+ "app_dir": "/system/app/ohos.samples.label",
+ "removable": false
+ },
{
"app_dir": "/system/app/com.example.Digital_Huarong_Road",
"removable": false
2.1.2 删除预置应用
删除应用是添加应用的逆操作,可参考上章节,将 vendor/spacemit/xxx/preinstall-config/install_list.json 中的对应 hap 的配置删除即可。
2.1.3 预置应用可卸载
默认情况下,部分系统预置应用不可卸载。如需允许用户卸载该应用,可在 vendor/spacemit/xxx/preinstall-config/install_list.json 中修改如下:
{
"app_dir" : "/system/app/com.ohos.note",
"removable" : true
},
这样即可让该预置应用支持用户手动卸载。
2.2 替换系统应用
系统应用按照 Ability 类型 可分为两类:
- UIAbility:此类系统应用能够展示在桌面,如 Settings、Photos、Camera 等。
- ExtensionAbility:此系统应用有的创建了窗口从而能显示界面,而有的则属于无页面的常驻服务去提供对应的能力, 如 Launcher、SystemUI、Settingsdata 等。
由于这些系统应用通常以系统权限运行,无法通过 hdc install 或 IDE 自动安装,因此需要使用 系统替换方式 进行更新。
示例:替换 Launcher 应用的替换步骤如下:
-
定位 Launcher 文件路径: 找到 Launcher 预置 hap 路径
/system/app/com.ohos.launcher,目录下包含两个 hap 文件:Launcher.hap和Launcher_Settings.hap -
执行替换命令
hdc shell mount -o remount,rw /
hdc shell rm -rf /data/*
hdc file send Launcher.hap /system/app/com.ohos.launcher/Launcher.hap
hdc file send Launcher_Settings.hap /system/app/com.ohos.launcher/Launcher_Settings.hap
hdc shell reboot -
系统重启后,替换安装新构建的 Launcher 即可生效。
2.3 相册
2.3.1 推送视频
-
准备推送的视频文件
xxx.mp4视频 -
通过 hdc 将视频推入设备:
D:\>hdc file send D:\xxx.mp4 /storage/media/100/local/files/Videos/ -
执行媒体扫描命令:
D:\>hdc shell
# scanner
# -
打开相册应用播放视频。若未显示,请将相册应用后台关闭后重新进入。
2.3.2 推送图片
-
准备要推送的图片文件
xxx.jpg或者xxx.png图片 -
通过 hdc 将图片推到设备:
D:\>hdc file send D:\xxx.jpg /storage/media/100/local/files/Pictures/ -
执行媒体扫描命令:
D:\>hdc shell
# scanner
# -
打开相册应用播放视频。若未显示,请将相册应用后台关闭后重新进入。
2.4 Launcher
2.4.1 替换背景图片
背景图片需准备两张:一张清晰,一张模糊,用于配对显示。如下:

主要修改下面几个位置:
-
修改 systemui,替换下面图片后,重新编译 systemui,将新生成的 hap 放入
applications/standard/hap文件夹applications/standard/systemui/product/phone/dropdownpanel/src/main/resources/phone/media/dropdownpanel_bgc.jpg -
修改 launcher,替换下面图片后,重新编译 launcher,将新生成的 hap 放入
applications/standard/hap文件applications/standard/launcher/product/phone/src/main/ets/common/pics
applications/standard/launcher/feature/appcenter/src/main/ets/default/common/pics/img_wallpaper_form_manager.jpg
applications/standard/launcher/feature/form/src/main/ets/default/common/pics/ic_wallpaper_form_manager.jpg
applications/standard/launcher/feature/recents/src/main/ets/default/common/pics/ic_wallpaper_recent.jpg -
修改系统,替换下面图片后,和上面修改的 hap 一起编译生成新的系统镜像
base/theme/wallpaper_mgr/frameworks/native/data/
2.4.2 替换 Launcher
-
给自定义应用打补丁,伪装成 Launcher
diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets
index 8e2d24a..f72e81a 100644
--- a/entry/src/main/ets/pages/Index.ets
+++ b/entry/src/main/ets/pages/Index.ets
@@ -1,3 +1,8 @@
+import hilog from '@ohos.hilog';
+import systemParameterEnhance from '@ohos.systemParameterEnhance';
+
+const DOMAIN = 0x0000;
+
@Entry
@Component
struct Index {
@@ -20,4 +25,26 @@ struct Index {
.height('100%')
.width('100%')
}
+
+ onPageShow(): void {
+ hilog.info(DOMAIN, 'launcher', 'onPageShow');
+ let firstActivate:boolean | undefined = AppStorage.get('firstActivate');
+ if (typeof firstActivate !== 'boolean') {
+ hilog.info(DOMAIN, 'launcher', 'first activate not defined');
+ AppStorage.setOrCreate('firstActivate', true);
+ this.voteBootEvent();
+ } else if (firstActivate) {
+ this.voteBootEvent();
+ }
+ }
+
+ private voteBootEvent(): void {
+ try {
+ AppStorage.setOrCreate('firstActivate', false);
+ systemParameterEnhance.setSync('bootevent.launcher.ready', 'true');
+ hilog.info(DOMAIN, 'launcher', 'set bootevent.launcher.ready -> true');
+ } catch (err) {
+ hilog.error(DOMAIN, 'launcher', 'set voteBootEvent err: %{public}s', JSON.stringify(err));
+ }
+ }
}
\ No newline at end of file
diff --git a/entry/src/main/module.json5 b/entry/src/main/module.json5
index e691821..0812247 100644
--- a/entry/src/main/module.json5
+++ b/entry/src/main/module.json5
@@ -2,8 +2,9 @@
"module": {
"name": "entry",
"type": "entry",
+ "srcEntry": "./ets/entryability/EntryAbility.ets",
"description": "$string:module_desc",
- "mainElement": "EntryAbility",
+ "mainElement": "com.ohos.launcher.MainAbility",
"deviceTypes": [
"default"
],
@@ -12,7 +13,8 @@
"pages": "$profile:main_pages",
"abilities": [
{
- "name": "EntryAbility",
+ "priority": 2,
+ "name": "com.ohos.launcher.MainAbility",
"srcEntry": "./ets/entryability/EntryAbility.ets",
"description": "$string:EntryAbility_desc",
"icon": "$media:layered_image",
@@ -23,28 +25,17 @@
"skills": [
{
"entities": [
- "entity.system.home"
+ "entity.system.home",
+ "flag.home.intent.from.system"
],
"actions": [
- "action.system.home"
+ "action.system.home",
+ "com.ohos.action.main",
+ "action.form.publish",
]
}
]
}
- ],
- "extensionAbilities": [
- {
- "name": "EntryBackupAbility",
- "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets",
- "type": "backup",
- "exported": false,
- "metadata": [
- {
- "name": "ohos.extension.backup",
- "resource": "$profile:backup_config"
- }
- ]
- }
]
}
}
\ No newline at end of file -
获取应用 fingerprint 提高 hilog 打印等级并关闭隐私
hilog -b D
hilog -p off
hilog | grep fingerprint > /mnt/1.txt尝试安装 hap,可在 hilog 中检索到 fingerprint
hdc file send your_launcher.hap /mnt/Launcher.hap
hdc shell bm install -p /mnt/Launcher.hap -
更新
install_list_capability.json将新的 fingerprint 拷贝到install_list_capability.json中 Launcher 对应的位置。 更新到设备/system/etc/app/install_list_capability.jsonhdc file send D:\_Resource\oh\install_list_capability.json /mnt/
hdc shell mount -o rw,remount /
hdc shell cp /mnt/install_list_capability.json /system/etc/app/install_list_capability.json -
删除
launcher_settings.hap删除/system/hap/com.ohos.launcher/launcher_settings.haphdc shell rm /system/hap/com.ohos.launcher/launcher_settings.hap -
安装应用
hdc file send your_launcher.hap /mnt/Launcher.hap
hdc shell mount -o rw,remount /
hdc shell cp /mnt/Launcher.hap /system/app/com.ohos.launcher/Launcher.hap
hdc shell rm -rf /data/* -
reboot
hdc shell reboot
2.5 锁屏
2.5.1 去掉锁屏
-
在
applications/standard/hap/BUILD.gn文件中去除锁屏应用相关的代码,如下:diff --git a/BUILD.gn b/BUILD.gn
index e213683..7382b97 100755
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -463,7 +463,6 @@ group("hap") {
"//applications/standard/hap:note_hap",
"//applications/standard/hap:notificationManagement_hap",
"//applications/standard/hap:photos_hap",
- "//applications/standard/hap:screenLock_hap",
"//applications/standard/hap:screen_shot_hap",
"//applications/standard/hap:settingsData_hap",
"//applications/standard/hap:settings_faceauth_hap",
@@ -518,7 +517,6 @@ group("hap") {
"//applications/standard/hap:navigationBar_hap",
"//applications/standard/hap:notificationManagement_hap",
"//applications/standard/hap:photos_hap",
- "//applications/standard/hap:screenLock_hap",
"//applications/standard/hap:screen_shot_hap",
"//applications/standard/hap:settingsData_hap",
"//applications/standard/hap:settings_faceauth_hap", -
在
foundation/systemabilitymgr/safwk/etc/profile/foundation.cfg文件中去除开机时对锁屏应用状态的检测,如下:diff --git a/etc/profile/foundation.cfg b/etc/profile/foundation.cfg
index f2573fa..1a21510 100644
--- a/etc/profile/foundation.cfg
+++ b/etc/profile/foundation.cfg
@@ -25,7 +25,6 @@
"name" : "services:restartfoundation",
"cmds" : [
"unset_bootevent bootevent.appfwk.ready",
- "unset_bootevent bootevent.lockscreen.ready",
"unset_bootevent bootevent.launcher.ready",
"reset appspawn",
"reset accountmgr",
@@ -141,7 +140,6 @@
"bootevents": [
"bootevent.wms.fullscreen.ready",
"bootevent.appfwk.ready",
- "bootevent.lockscreen.ready",
"bootevent.launcher.ready",
"bootevent.wms.ready"
], -
去除窗口控制器对锁屏信号的响应;因为这里我们只是去掉了锁屏应用,但触发锁屏的机制还在,比如说按键触发和遥控器触发,只是没有实际的响应,如果不去掉,当再触发进入锁屏的机制,可能会出现应用界面冻结。 修改
foundation/window/window_manager/wmserver/src/window_controller.cpp文件,如下:diff --git a/wmserver/src/window_controller.cpp b/wmserver/src/window_controller.cpp
index 32c6a60ec..98df12ffc 100644
--- a/wmserver/src/window_controller.cpp
+++ b/wmserver/src/window_controller.cpp
@@ -783,8 +783,8 @@ void WindowController::NotifyDisplayStateChange(DisplayId defaultDisplayId, sptr
WLOGFD("NotifyDisplayStateChange start: %{public}u", type);
switch (type) {
case DisplayStateChangeType::BEFORE_SUSPEND: {
- isScreenLocked_ = true;
- windowRoot_->ProcessWindowStateChange(WindowState::STATE_FROZEN, WindowStateChangeReason::KEYGUARD);
+ //isScreenLocked_ = true;
+ //windowRoot_->ProcessWindowStateChange(WindowState::STATE_FROZEN, WindowStateChangeReason::KEYGUARD);
break;
}
case DisplayStateChangeType::BEFORE_UNLOCK: {
副作用:无下拉菜单
2.6. USB 通信定制
2.6.1. 使用 USB DDK 通信
-
使用 Deveco Studio 创建 Native C++ 工程 在
entry/build-profile.json5添加 riscv64 适配, 如下:{
"apiType": "stageMode",
"buildOption": {
"externalNativeOptions": {
"path": "./src/main/cpp/CMakeLists.txt",
"arguments": "",
"abiFilters": [
"riscv64"
],
"cppFlags": "",
}
},
"buildOptionSet": [
{
"name": "release",
"arkOptions": {
"obfuscation": {
"ruleOptions": {
"enable": false,
"files": [
"./obfuscation-rules.txt"
]
}
}
},
"nativeLib": {
"debugSymbol": {
"strip": true,
"exclude": []
}
}
},
],
"targets": [
{
"name": "default"
},
{
"name": "ohosTest",
}
]
} -
在
entry/src/main/module.json5申请权限:...
"requestPermissions": [
{
"name": "ohos.permission.ACCESS_DDK_USB"
}
]
... -
链接动态库 修改
entry/src/main/cpp/CMakeLists.txt添加相关 lib:target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libusb_ndk.z.so) -
entry/src/main/cpp/napi_init.cpp中添加 USB 读写逻辑:#include "napi/native_api.h"
#include <usb/usb_ddk_api.h>
#include <usb/usb_ddk_types.h>
#include <hilog/log.h>
uint64_t interfaceHandle = 0;
struct UsbDeviceMemMap *devMmap = nullptr;
static napi_value UsbInit(napi_env env, napi_callback_info info)
{
napi_value result;
// 初始化USB DDK
OH_Usb_Init();
struct UsbDeviceDescriptor devDesc;
uint64_t deviceId = 0x0000000200000002; //对应耳机usb节点:/dev/bus/usb/001/003 001对应高八位 004对应第八位
// 获取设备描述符
OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
struct UsbDdkConfigDescriptor *config = nullptr;
// 获取配置描述符
OH_Usb_GetConfigDescriptor(deviceId, 1, &config);
// 根据配置描述符,找到所需要通信的interfaceIndex
uint8_t interfaceIndex = 0;
// 声明接口
int ret = OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle);
// 释放配置描述符
OH_Usb_FreeConfigDescriptor(config);
napi_create_int32(env, ret, &result);
return result;
}
static napi_value UsbSend(napi_env env, napi_callback_info info)
{
napi_value result;
// 超时时间,设置为1s;
uint32_t timeout = 1000;
//获取deviceid、devicekey
struct UsbControlRequestSetup setupWrite1;
setupWrite1.bmRequestType = 0x40;
setupWrite1.bRequest = 0x09;
setupWrite1.wValue = 0x200;
setupWrite1.wIndex = 0x4;
setupWrite1.wLength = 0x10;
uint8_t dataWrite1[10] = {0xA5,0x5A,0xFA,0x02,0x02,0x00,0x16};
uint32_t dataWriteLen1 = 10;
// 发送控制写请求
OH_Usb_SendControlWriteRequest(interfaceHandle, &setupWrite1, timeout, dataWrite1, dataWriteLen1);
struct UsbControlRequestSetup setupRead;
setupRead.bmRequestType = 0xC0;
setupRead.bRequest = 0x01;
setupRead.wValue = 0x101;
setupRead.wIndex = 0x4;
setupRead.wLength = 0x100;
uint8_t dataRead[256] = {};
uint32_t dataReadLen = 256;
// 发送控制读请求
int ret = OH_Usb_SendControlReadRequest(interfaceHandle, &setupRead, timeout, dataRead, &dataReadLen);
for(int i = 0; i < 10; i++){
OH_LOG_Print(_LOG_APP_, _LOG_ERROR_, 0xff, "usb-ddk", "data read:%{public}x", dataRead[i]);
}
napi_create_int32(env, ret, &result);
return result;
}
static napi_value UsbDestroy(napi_env env, napi_callback_info info)
{
napi_value result;
// 销毁缓冲区
// 释放接口
int ret = OH_Usb_ReleaseInterface(interfaceHandle);
// 释放USB DDK
OH_Usb_Release();
napi_create_int32(env, ret, &result);
return result;
}
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{ "usbInit", nullptr, UsbInit, nullptr, nullptr, nullptr, _napi_default_, nullptr },
{ "usbDestroy", nullptr, UsbDestroy, nullptr, nullptr, nullptr, _napi_default_, nullptr },
{ "usbSend", nullptr, UsbSend, nullptr, nullptr, nullptr, _napi_default_, nullptr }
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END
static napi_module demoModule = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "entry",
.nm_priv = ((void*)0),
.reserved = { 0 },
};
extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
{
napi_module_register(&demoModule);
}entry/src/main/cpp/types/libentry/Index.d.ts中添加声明:**export const **usbInit: () => number;
**export **const usbDestroy: () => number;
export const usbSend: () => number; -
UI 中调用:
**import **testNapi **from **'libentry.so';
@Entry
@Component
**struct **Index {
@State message: string = 'Control Red Led';
build() {
Row() {
Column() {
Text(**this**.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
testNapi.usbInit(); //传入资源对象,以及访问的rawfile文件夹名称
testNapi.usbSend();
testNapi.usbDestroy();
})
}
.width('100%')
}
.height('100%')
}
}
2.6.2. 使用 HID DDK 通信
注意:由于 OH5.0 版本 hid ddk 的功能实现尚未完全,可通过直接读写 hidraw 节点实现通信。
-
内核打开 hidraw 配置(
kernel\linux\spacemit_kernel-6.6\arch\riscv\configs\k1_defconfig)CONFIG_HIDRAW=y修改 hidraw 节点读写权限(
base\startup\init\ueventd\etc\ueventd.config)/dev/bus/usb/* 0666 0 0 -
使用 Deveco Studio 创建 Native C++ 工程 在
entry/build-profile.json5添加 riscv64 适配{
"apiType": "stageMode",
"buildOption": {
"externalNativeOptions": {
"path": "./src/main/cpp/CMakeLists.txt",
"arguments": "",
"abiFilters": [
"riscv64"
],
"cppFlags": "",
}
},
"buildOptionSet": [
{
"name": "release",
"arkOptions": {
"obfuscation": {
"ruleOptions": {
"enable": false,
"files": [
"./obfuscation-rules.txt"
]
}
}
},
"nativeLib": {
"debugSymbol": {
"strip": true,
"exclude": []
}
}
},
],
"targets": [
{
"name": "default"
},
{
"name": "ohosTest",
}
]
} -
链接动态库 修改
entry/src/main/cpp/CMakeLists.txt添加相关 lib:target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so) -
entry/src/main/cpp/napi_init.cpp中添加 HID 节点读写逻辑,写数据示例如下:#include "napi/native_api.h"
#include <hilog/log.h>
#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <cstdint>
#include <cerrno>
static napi_value HidTest(napi_env env, napi_callback_info info)
{
napi_value result;
const uint8_t hid_data[] = {
0x2E, 0xAA, 0xEC, 0x6B, 0x00, 0x07, 0xA2, 0xA4,
0x43, 0xA5, 0xA5, 0xC1, 0x5A, 0xF6, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
const size_t data_len = sizeof(hid_data);
int fd = open("/dev/hidraw1", O_WRONLY);
if (fd == -1) {
OH_LOG_Print(_LOG_APP_, _LOG_ERROR_, 0xff, "hid-ddk", "open hidraw error");
}
ssize_t write_result = write(fd, hid_data, data_len);
if (write_result == -1) {
OH_LOG_Print(_LOG_APP_, _LOG_ERROR_, 0xff, "hid-ddk", "write error:%{public}s",std::strerror(errno));
}
if (write_result != static_cast<ssize_t>(data_len)) {
OH_LOG_Print(_LOG_APP_, _LOG_ERROR_, 0xff, "hid-ddk", "only write:%{public}zu",data_len);
} else {
OH_LOG_Print(_LOG_APP_, _LOG_ERROR_, 0xff, "hid-ddk", "write success:%{public}zu",write_result);
}
close(fd);
fd = -1;
napi_create_uint32(env, write_result, &result);
return result;
}
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{ "hidTest", nullptr, HidTest, nullptr, nullptr, nullptr, _napi_default_, nullptr },
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END
static napi_module demoModule = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "entry",
.nm_priv = ((void*)0),
.reserved = { 0 },
};
extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
{
napi_module_register(&demoModule);
} -
entry/src/main/cpp/types/libentry/Index.d.ts中添加声明:**export const hidTest: () => number;** -
UI 中调用:
**import **testNapi **from **'libentry.so';
@Entry
@Component
**struct **Index {
@State message: string = 'Control Red Led';
build() {
Row() {
Column() {
Text(**this**.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
testNapi.hidTest();
})
}
.width('100%')
}
.height('100%')
}
}
2.7 应用自启动
目前支持在锁屏应用中拉起目标应用来实现自启动。需要在 vendor/spacemit/xxx/etc/param/product_xxx.para 中添加并配置如下属性:
const.product.appboot.enabled=1 # 1:开启应用自启动 0:关闭应用自启动
const.product.appboot.bundlename="org.ohosdev.foh" # 应用包名
const.product.appboot.modulename="entry" # 模块名
const.product.appboot.abilityname="EntryAbility" # Ability名称
添加上述属性后,重新编译生成镜像文件(img),并烧录到设备上,即可生效。
注意:需要确保自启动的目标应用存在于系统中。
3. 系统定制
3.1 添加系统属性
3.1.1 固定值系统属性
可通过在以下路径添加属性来定义或覆盖系统属性:
vendor/spacemit/xxx/etc/param/product_xxx.para
示例如下:
const.product.name="MUSEPAPER"
const.product.manufacturer="SPACEMIT"
const.product.brand="MUSEPAPER"
const.software.model="1.0.0.1"
const.product.hardwareversion="1.0.0.0"
const.product.software.version="1.0.0.1"
const.product.incremental.version="1.0.0.0"
const.build.product="MUSEPAPER"
添加的属性可以是已经存在的系统属性,这里进行覆盖,也可以是新添加的自定义名称的属性。
3.1.2 读取值系统属性
与固定值属性不同,读取值系统属性是在系统启动时,通过读取系统节点等方式动态生成并写入到系统属性中的。
这类属性的逻辑需要在以下文件中实现:
base/startup/init/services/init/standard/init.c
代码已经添加了获取 CPU 型号和 MEM 大小的例子,以下以获取内存大小为例说明:
-
添加通过节点读取信息的接口
void get_memory_size(char *buffer) {
FILE *fp = fopen("/proc/meminfo", "r");
if (!fp) {
perror("Could not open /proc/meminfo");
return;
}
char line[BUFFER_SIZE];
while (fgets(line, sizeof(line), fp)) {
if (strstr(line, "MemTotal")) {
char *colon = strchr(line, ':');
if (colon) {
unsigned long mem_kb;
sscanf(colon + 1, "%lu", &mem_kb);
float mem_gb = mem_kb / (1024.0 * 1024.0);
snprintf(buffer, BUFFER_SIZE, "%d GB", closest_value(mem_gb));
break;
}
}
}
fclose(fp);
} -
在
RegisteSystemParameter中调用接口并写入属性void RegisteSystemParameter(void){
...
char memory_size[BUFFER_SIZE] = "Unknown";
...
get_memory_size(memory_size);
...
i = SystemWriteParameter("const.product.mem", memory_size);
INIT_LOGI("const.product.mem = %s, ret = %d", memory_size, i);
}
3.2 电源管理定制
主要修改文件:vendor/spacemit/xxx/power_config/power_mode_config.xml,示例如下:
<!--
Power Mode Definitions:
MODE_NORMAL = 600,
MODE_POWER_SAVE = 601,
MODE_PERFORMANCE = 602,
MODE_EXTREME_POWER_SAVE = 603,
-->
<!--
Action Definitions:
DisplayOffTime = 101,
SystemAutoSleepTime = 102,
AutoAdjustBrightness = 103,
AutoWindowRotation = 107,
SystemBrightness = 115,
VibratorsState = 120,
-->
<switch_proxy version="1">
<proxy id="600">
<switch id="101" value="300000" recover_flag="0"/>
<switch id="102" value="0" recover_flag="0"/>
<switch id="103" value="-1" recover_flag="0"/>
<switch id="107" value="1" recover_flag="0"/>
<switch id="115" value="76" recover_flag="0"/>
<switch id="120" value="1" recover_flag="0"/>
</proxy>
<proxy id="601">
<switch id="101" value="10000" recover_flag="0"/>
<switch id="102" value="5000" recover_flag="0"/>
<switch id="103" value="-1" recover_flag="0"/>
<switch id="107" value="-1" recover_flag="0"/>
<switch id="115" value="50" recover_flag="0"/>
<switch id="120" value="-1" recover_flag="0"/>
</proxy>
<proxy id="602">
<switch id="101" value="-1" recover_flag="0"/>
<switch id="102" value="-1" recover_flag="0"/>
<switch id="103" value="-1" recover_flag="0"/>
<switch id="107" value="1" recover_flag="0"/>
<switch id="115" value="255" recover_flag="0"/>
<switch id="120" value="1" recover_flag="0"/>
</proxy>
<proxy id="603">
<switch id="101" value="5000" recover_flag="0"/>
<switch id="102" value="1000" recover_flag="0"/>
<switch id="103" value="-1" recover_flag="0"/>
<switch id="107" value="-1" recover_flag="0"/>
<switch id="115" value="25" recover_flag="0"/>
<switch id="120" value="-1" recover_flag="0"/>
</proxy>
</switch_proxy>
该文件定义了多种电源模式管理模式(正常、省电、性能、超级省电)及对应参数(关屏时间、休眠时间等)。
3.2.1 设置不关屏休眠
系统默认使用 MODE_NORMAL 模式,可通过命令切换到其他模式, 如需防止休眠,可切换为性能模式:
power-shell setmode 602
3.2.2 设置关屏时间
通过 DisplayOffTime (101) 控制关屏时长,单位为毫秒(ms),示例如下:
<switch id="101" value="300000" recover_flag="0"/>
//300000 表示开机后,3分钟后关屏
<switch id="101" value="-1" recover_flag="0"/>
//-1 表示永不关屏

