kobject的使用基本框架

下面是伪代码  可以根据此框架写自己的kobject属性 

struct myobject{
    int a;
	int b;
	struct kobject object;
}g_object;

struct myattr{
   struct attribute  attr;
   ssize_t	(*show)(struct myobject *myobj,char *);
   ssize_t	(*store)(struct myobject *myobj,const char *, size_t);
};

ssize_t	colorshow(struct myobject *myobj,char *)
{
}
ssize_t	colorstore(struct myobject *myobj,const char *, size_t)
{
}   
ssize_t	sizeshow(struct myobject *myobj,char *)
{
}
ssize_t	sizestore(struct myobject *myobj,const char *, size_t)
{
}

struct myattr at1={.attr={"color",NULL,0477},.show = colorshow,.store=colorstore};
struct myattr at2={.attr={"size",NULL,0477},.show = sizeshow,.store=sizestore};

struct attribute *g_defattr[]={
      &at1.attr,
      &at2.attr	  
};

ssize_t	myshow(struct kobject * obj, struct attribute * attr,char *buff)
{
     struct myobject *o = contain_of(obj,struct myobject,object);
	 struct myattr *at = contin_of(attr,struct myattr,attr);
	 at->show(o,buff);
}

ssize_t	mystore(struct kobject *obj,struct attribute *attr,const char * buff, size_t size)
{
   struct myobject *o = contain_of(obj,struct myobject,object);
   struct myattr *at = contin_of(attr,struct myattr,attr);
   at->show(o,buff,size);
}

g_object.object.kobj_type.sysfs_ops.show = myshow;
g_object.object.kobj_type.sysfs_ops.store = mystore;
g_object.object.kobj_type.default_attrs = g_defattr;

kobject_create_and_add("test",&g_object.object);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值