疯壳出品鸿蒙os-驱动程序接收应用程序的消息

charlesc
发布于 2021-3-23 09:36
1.2w浏览
0收藏

当明确驱动已经加载完成时,用户态应用可通过接口:

struct HdfIoService *HdfIoServiceBind(const char *serviceName, mode_t permission)

获取驱动的服务,获取到服务后通过服务中的Dispatch方法向驱动发送消息。该接口在hdf_io_service_if.h文件中有如下声明:

疯壳出品鸿蒙os-驱动程序接收应用程序的消息-鸿蒙开发者社区

*参数serviceName 表示指向要获取的服务名称的指针

 

*参数 permission 表示创建设备节点的权限,从用户空间调用此函数时,可以使用默认值0

 

*如果操作成功,函数返回指向驱动程序服务对象的指针,否则返回NULL

 

我们在之前添加的myapp应用的基础上新添获取 “sample_service” 服务的接口并调用Dispatch方法向驱动发送消息,如下:疯壳出品鸿蒙os-驱动程序接收应用程序的消息-鸿蒙开发者社区

App完整程序在文末附上!

 

此外我们在驱动中还要实现服务基类成员IDeviceIoService中的Dispatch方法,如下:

我们先看一下IDeviceIoService结构体(在源码hdf_device_desc.h中):

疯壳出品鸿蒙os-驱动程序接收应用程序的消息-鸿蒙开发者社区其中Dispatch是一个函数指针,HdfDeviceIoClient结构体定义如下(同文件中):

疯壳出品鸿蒙os-驱动程序接收应用程序的消息-鸿蒙开发者社区

 

1.驱动文件sample_driver.c中新增dispatch方法:

 

根据上面的图片,我们定义一个跟Disapatch兼容的函数体如下:疯壳出品鸿蒙os-驱动程序接收应用程序的消息-鸿蒙开发者社区2.驱动文件中修改bind函数,绑定我们上面定义的Disaptch函数:

疯壳出品鸿蒙os-驱动程序接收应用程序的消息-鸿蒙开发者社区

 

接下来我们将源码重新编译,将生成的文件烧录到板子中执行,app正常运行后结果如下:疯壳出品鸿蒙os-驱动程序接收应用程序的消息-鸿蒙开发者社区打印的信息很明显是符合我们的程序逻辑的!

 

源码


#include <stdio.h>

#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"

 

 

int main (int argc,char **argv){
 

printf("hello hmos\n");

 

for (int i=0;i<argc;i++)

printf("%s\n",argv[i]);

 

        struct HdfIoService *serv = HdfIoServiceBind("sample_service", 0);

        if (serv == NULL) {
            HDF_LOGE("fail to get service %s\n", "sample_service");

            return HDF_FAILURE;

        } else {
            HDF_LOGE("get service %s\n", "sample_service");

        }

 

        //分配两个消息交互需要的hdfsbuf

        struct HdfSBuf *data = HdfSBufObtainDefaultSize();

        if (data == NULL) {
            HDF_LOGE("fail to obtain sbuf data");

            return -1;

        }

        struct HdfSBuf *reply = HdfSBufObtainDefaultSize();

        if (reply == NULL) {
            HDF_LOGE("fail to obtain sbuf reply");

            goto out;

        }

 

        //向data中写入交互信息

        if (!HdfSbufWriteString(data, (const char*)"fengke test string")) {
            HDF_LOGE("fail to write sbuf");

            goto out;

        }

 

        //调用dispatch

        int ret = serv->dispatcher->Dispatch(&serv->object,(int)0xff , data, reply);

        if (ret != HDF_SUCCESS) {
            HDF_LOGE("fail to send service call");

            goto out;

        }

out:

        HdfSBufRecycle(data);

        HdfSBufRecycle(reply);

        HdfIoServiceRecycle(serv);

 

return 0;

}

 

 

驱动:

#include "hdf_device_desc.h"  // HDF框架对驱动开放相关能力接口的头文件

#include "hdf_log.h"          // HDF 框架提供的日志接口头文件

 

#define HDF_LOG_TAG sample_driver   // 打印日志所包含的标签,如果不定义则用默认定义的HDF_TAG标签

 

//驱动服务结构的定义

struct ISampleDriverService {
    struct IDeviceIoService ioService;       // 服务结构的首个成员必须是IDeviceIoService类型的成员

    //以下可添加自定义的驱动的服务接口

};

 

// Dispatch是用来处理用户态发下来的消息

int32_t SampleDriverDispatch(struct HdfDeviceIoClient *client, int cmdCode, struct HdfSBuf *data, struct HdfSBuf *reply)

{
    HDF_LOGE("%s received:\n",__func__);

    HDF_LOGE("cmdCode:%d\n",cmdCode);

    HDF_LOGE("app data:%s\n",(const char*)HdfSbufReadString(data));

 

    return 0;

}

 

//驱动对外提供的服务能力,将相关的服务接口绑定到HDF框架

int32_t HdfSampleDriverBind(struct HdfDeviceObject *deviceObject) {
    // deviceObject为HDF框架给每一个驱动创建的设备对象,用来保存设备相关的私有数据和服务接口

    if (deviceObject== NULL) {
        HDF_LOGE("Sample device object is null!");

        return -1;

    }

    static struct ISampleDriverService mysampleDriver = {
      .ioService.Dispatch = SampleDriverDispatch,

    };

 

    deviceObject->service = &mysampleDriver.ioService;

    return 0;

}

 

// 驱动自身业务初始的接口

int32_t HdfSampleDriverInit(struct HdfDeviceObject *deviceObject) {
    return 0;

}

 

// 驱动资源释放的接口

void HdfSampleDriverRelease(struct HdfDeviceObject *deviceObject) {
    return;

}

 

// 定义驱动入口的对象,必须为HdfDriverEntry(在hdf_device_desc.h中定义)类型的全局变量

struct HdfDriverEntry g_sampleDriverEntry = {
    .moduleVersion = 1,

    .moduleName = "sample_driver",

    .Bind = HdfSampleDriverBind,

    .Init = HdfSampleDriverInit,

    .Release = HdfSampleDriverRelease,

};

 

// 调用HDF_INIT将驱动入口注册到HDF框架中,在加载驱动时HDF框架会先调用Bind函数,再调用Init函数加载该驱动,当Init调用异常时,HDF框架会调用Release释放驱动资源并退出。

HDF_INIT(g_sampleDriverEntry);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.

 

 

 

 

 

————————————————
版权声明:本文为CSDN博主「fengkesz」的原创文章

标签
已于2021-3-23 09:36:08修改
收藏
回复
举报


回复
    相关推荐
    恭喜您,今日已阅读两篇内容,特奖励+2声望, 快来领取吧。