天下之事常成于困约,而败于奢靡。这篇文章主要讲述HDF驱动框架探路:openharmony最新源码,打通应用态到内核态相关的知识,希望能为你提供帮助。
【本文正在参与优质创作者激励】
老规矩还是将最终希望跑出来的效果放出来。如下:
文章图片
@toc
前言
- 1.首先本文章的源码是2021.11.25拉的openharmony的master分支。之前也有一些大神基于3516应用态打通到了内核态,不过都很久远了,尤其是3.0LTS发布之后,整个框架都改动过了,按照大神们的操作我这边基于最新的源码都没有测试成功。有大佬可以留言指导一拨儿。
- 2.本文章是基于Hi3516轻量系统来打造的。 应用态打通内核态的总体框架图
文章图片
从上图可以很直观的看出驱动开发的过程中基于linux开发和基于openharmony开发的最大区别
- 1.linux驱动开发中会涉及到应用层的glic等,以及驱动层的内核函数调用,但是在openharmony这些以及都被HDF框架给隐藏起来了,所以我们只需要根据HDF框架来进行openharmony框架开发,就可以了。
- 2.openharmony应用层其实是调用了HDF框架的驱动消息机制 1.应用程序 2.1 应用层业务代码
- 1.在源码根目录下新建myapp目录
- 2.然后进入myapp目录新建helloworld_test.c,然后将下列代码拷贝入内
#include < fcntl.h> #include < sys/stat.h> #include < sys/ioctl.h> #include < unistd.h> #include "hdf_log.h" #include "hdf_sbuf.h" #include "hdf_io_service_if.h"
#define SAMPLE_SERVICE_NAME " GPIO_TEST"
#define SAMPLE_WRITE_READ 0
int g_replyFlag = 0;
static int OnDevEventReceived(void priv,uint32_t id, struct HdfSBuf data)
{
const char string = HdfSbufReadString(data);
if (string == NULL) {
HDF_LOGE(" fail to read string in event data" );
g_replyFlag = 1;
return HDF_FAILURE;
}
HDF_LOGE(" %s: dev event received: %u %s" ,(char )priv, id, string);
g_replyFlag = 1;
return HDF_SUCCESS;
}
static int SendEvent(struct Hdfioservice serv, char eventData)
{
int ret = 0;
struct HdfSBuf *data = https://www.songbingjia.com/android/HdfSBufObtainDefaultSize();
if (data =https://www.songbingjia.com/android/= NULL) {
HDF_LOGE(" fail to obtain sbuf data" );
return 1;
}
struct HdfSBuf *reply = HdfSBufObtainDefaultSize();
if (reply == NULL) {
HDF_LOGE("fail to obtain sbuf reply");
ret = HDF_DEV_ERR_NO_MEMORY;
goto out;
}if (!HdfSbufWriteString(data, eventData)) {
HDF_LOGE("fail to write sbuf");
ret = HDF_FAILURE;
goto out;
}ret = serv->
dispatcher->
Dispatch(&
serv->
object, SAMPLE_WRITE_READ, data, reply);
if (ret != HDF_SUCCESS) {
HDF_LOGE("fail to send service call");
goto out;
}int replyData = https://www.songbingjia.com/android/0;
if (!HdfSbufReadInt32(reply, &
replyData)) {
HDF_LOGE("fail to get service call reply");
ret = HDF_ERR_INVALID_OBJECT;
goto out;
}
HDF_LOGE("Get reply is: %d", replyData);
【HDF驱动框架探路(openharmony最新源码,打通应用态到内核态)】out:
HdfSBufRecycle(data);
HdfSBufRecycle(reply);
return ret;
}
int main()
{
char sendData = https://www.songbingjia.com/android/" hi" ;
struct HdfIoService serv = HdfIoServiceBind(SAMPLE_SERVICE_NAME);
if (serv == NULL) {
HDF_LOGE(" fail to get service %s" , SAMPLE_SERVICE_NAME);
return HDF_FAILURE;
}
HDF_LOGE(" success to get service %s" , SAMPLE_SERVICE_NAME);
static struct HdfDevEventlistener listener = {
.callBack = OnDevEventReceived,
.priv ="Service0"
};
if (HdfDeviceRegisterEventListener(serv, &
listener) != HDF_SUCCESS) {
HDF_LOGE("fail to register event listener");
return HDF_FAILURE;
}
HDF_LOGE("success to register event listener");
if (SendEvent(serv, sendData)) {
HDF_LOGE("fail to send event");
return HDF_FAILURE;
}/* wait for event receive event finishing */
while (g_replyFlag == 0) {
sleep(1);
}if (HdfDeviceUnregisterEventListener(serv, &
listener)) {
HDF_LOGE("fail tounregister listener");
return HDF_FAILURE;
}HdfIoServiceRecycle(serv);
return HDF_SUCCESS;
}
## 1.2 应用层业务代码以可执行文件编译进系统
- 进入源码目录找到drivers/adapter/uhdf/manager/BUILD.gn 文件,然后在文件末尾拷贝下述代码。```c
HDF_FRAMEWORKS = "//drivers/framework"executable("helloworld_test") {
sources = ["//myapp/helloworld_test.c" ]
include_dirs = [
"$HDF_FRAMEWORKS/ability/sbuf/include",
"$HDF_FRAMEWORKS/core/shared/include",
"$HDF_FRAMEWORKS/core/host/include",
"$HDF_FRAMEWORKS/core/master/include",
"$HDF_FRAMEWORKS/include/core",
"$HDF_FRAMEWORKS/include/utils",
"$HDF_FRAMEWORKS/utils/include",
"$HDF_FRAMEWORKS/include/osal",
"//drivers/adapter/uhdf/posix/include",
"//third_party/bounds_checking_function/include",
"//base/hiviewdfx/hilog_lite/interfaces/native/innerkits",
]deps = [
"//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared",
"//drivers/adapter/uhdf/manager:hdf_core",
"//drivers/adapter/uhdf/posix:hdf_posix_osal",
]public_deps = [ "//third_party/bounds_checking_function:libsec_shared" ]
defines = [ "__USER__" ]cflags = [
"-Wall",
"-Wextra",
"-Wno-format",
"-Wno-format-extra-args",
]
}
2.驱动代码这里驱动是直接引用了源码中已经存在的一个驱动程序:GPIO_TEST
2.1 驱动业务层代码
- 进入源码目录,找到drivers/framework/test/unittest/platform/common/gpio_driver_test.c文件,然后对比下图进行修改。
文章图片
执行hb build -f进行编译,然后烧录,接着打开串口就可以看到效果了
51CTO和华为官方战略合作共建的鸿蒙技术社区
https://harmonyos.51cto.com/#bkwz
::: hljs-center
文章图片
:::
推荐阅读
- 传输文件协议FTP
- c语言实现简单版扫雷
- 文本分类(Keras+RNN vs 传统机器学习)
- DNS域名解析服务
- Renix绑定流详解——网络测试仪实操
- #yyds干货盘点#数据分析从零开始实战,PythonPandas与各类数据库
- #yyds干货盘点#three.js中如何切换相机视角
- 技术分享| Sip与WebRTC互通-SRProxy开源库讲解
- Redis持久化RDB和AOF优缺点,你都了解吗()