qnx之resource manager(三)
device-specific and per-open data
学习目的
1.举例引入概念
2.代码编写步骤
1.举例引入概念
a time resource manager,如下所示:
该resource manager必须要有以下特性:
1.操作多个设备
•/dev/time/now
•/dev/time/min
•/dev/time/hour
2.在read时,能返回多个数据
•返回一次数据
•在后面的reads中,返回0以表示读到了文件的最后
3.维护open和read之间的上下文
注:要实现以上需求,必须要满足以下要求:a.name和device的数量都知道;b.device的数量不是特别大(少于100)
2.代码编写步骤:
1.注册这3个新name
resmgr_attach (dpp, &rattr, “/dev/time/now”,
_FTYPE_ANY, 0, &connect_funcs, &io_funcs, &nowattr);
resmgr_attach (dpp, &rattr, “/dev/time/hour”,
_FTYPE_ANY, 0, &connect_funcs, &io_funcs, &hourattr);
resmgr_attach (dpp, &rattr, “/dev/time/min”,
_FTYPE_ANY, 0, &connect_funcs, &io_funcs, &minattr);
或者
char *devnames [NumDevices] = { … initializers … };
…
for (i = 0; i < NumDevices; i++) {
resmgr_attach (…, devnames [i], …);
}
图解:
2.编写I/O和connect 函数相关
每一个open()都匹配一个ocb,并且指向我们打开的设备的attribute structure。
attribute structure指向更高级别的structure(mount structure),它描述了 普通mountpoint 信息。
重写attr,以包含我们自己的数据:
struct Timeattr_s;
#define IOFUNC_ATTR_T struct Timeattr_s
#include <sys/iofunc.h>
#include <sys/neutrino.h>
typedef struct Timeattr_s {
iofunc_attr_t attr; // encapsulate iofunc layer’s
// attr
char * format; // our data
} Timeattr_
导致io_open函数原型变化:
int io_open (..., RESMGR_HANDLE_T *handle, ...)
变为
int io_open (..., Timeattr_t *tattr, ...)
重写ocb,以包含我们自己的数据:
struct Timeocb_s;
#define IOFUNC_OCB_T struct Timeocb_s
#include <sys/iofunc.h>
#include <sys/neutrino.h>
typedef struct Timeocb_s {
iofunc_ocb_t ocb; // encapsulate iofunc layer’s ocb
char *buffer; // for buffer of data we return
int bufsize; // how many bytes are in buffer
} Timeocb_t;
使io_read函数原型变化:
int io_read (..., RESMGR_OCB_T *ocb)
变为
int io_read (..., Timeocb_t *tocb)
ocb的动态分配和释放
1:先定义iofunc_funcs_t结构体,
2:然后将该结构体地址放入iofunc_mount_t结构体中,
3:再将iofunc_mount_t结构体地址放入iofunc_attr_t
IOFUNC_OCB_T *time_ocb_calloc (resmgr_context_t *ctp,
IOFUNC_ATTR_T *attr) //由iofunc_open_default()函数调用
void time_ocb_free (IOFUNC_OCB_T *ocb)//由iofunc_close_ocb_default()函数调用
Timeocb_t *
time_ocb_calloc (resmgr_context_t *ctp, Timeattr_t *tattr)
{
Timeocb_t *tocb;
tocb = calloc (1, sizeof (Timeocb_t));
// do anything else to our per ocb data
tocb -> buffer = NULL;
return tocb;
}
void
time_ocb_free (Timeocb_t *tocb)
{
if (tocb -> buffer) {
free (tocb -> buffer);
}
free (tocb);
}
//1
iofunc_funcs_t time_ocb_funcs = {
_IOFUNC_NFUNCS,
time_ocb_calloc,
time_ocb_free
};
//