下面是伪代码 可以根据此框架写自己的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);