一个设备接口实例

[复制链接]
1308|8
 楼主| keer_zu 发表于 2021-11-29 16:15 | 显示全部楼层 |阅读模式

device.h:
  1. #ifndef __DEVICE_H__
  2. #define __DEVICE_H__

  3. #ifndef NULL
  4. #define NULL ((void *)0)
  5. #endif

  6. typedef int (*on_recv_fun)(void *);

  7. typedef struct {
  8.         on_recv_fun on_recv;
  9. }device_methods_t;


  10. typedef struct {
  11.         void *pdev;
  12.         device_methods_t* methods;
  13. }device_interface_t;


  14. int register_dev(const char* name, device_interface_t devi);


  15. #endif
 楼主| keer_zu 发表于 2021-11-29 20:17 | 显示全部楼层
device.c:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "device.h"

  4. device_interface_t g_device;

  5. int register_dev(const char* name, device_interface_t *devi)
  6. {
  7.         if(devi == NULL) {
  8.                 printf("%d: devi is NULL!\n",__LINE__ + 1);
  9.                 return -__LINE__;
  10.         }
  11.        
  12.         if(name == NULL || devi->methods == NULL || devi->pdev == NULL) {
  13.                 printf("%d: name or methods or pdev is NULL!\n",__LINE__ + 1);
  14.                 return -__LINE__;
  15.         }

  16.         g_device.methods = devi->methods;
  17.         g_device.pdev = devi->pdev;
  18.        
  19.         return 0;
  20. }




 楼主| keer_zu 发表于 2021-11-29 20:42 | 显示全部楼层

dev_485.c:

  1. #include <stdio.h>
  2. #include "dev_485.h"

  3. static int on_485_recv(void *param)
  4. {
  5.         if(param ==NULL){
  6.                 printf("%d: param NULL!\n",__LINE__ + 1);
  7.                 return -__LINE__;
  8.         }
  9.         
  10.         return 0;
  11. }


  12. device_methods_t dev_485_methords = {
  13.         .on_recv = on_485_recv
  14. };

  15. device_485_t g_dev_485;



  16. int dev_485_init(void)
  17. {
  18.         device_interface_t devi = {
  19.                 .pdev = &g_dev_485,
  20.                 .methods = &dev_485_methords
  21.         };

  22.         int ret = register_dev("485",&devi);
  23.         
  24.         return ret;
  25. }


 楼主| keer_zu 发表于 2021-11-29 20:44 | 显示全部楼层
dev_485.h:

  1. #ifndef __DEV_485_H__
  2. #define __DEV_485_H__

  3. #include "device.h"

  4. typedef struct {
  5.         char *name;
  6. }device_485_t;


  7. int dev_485_init(void);


  8. #endif


qin552011373 发表于 2021-11-30 09:29 | 显示全部楼层
这些都是楼主自己项目里面自己写的吗?
 楼主| keer_zu 发表于 2021-11-30 10:39 | 显示全部楼层
qin552011373 发表于 2021-11-30 09:29
这些都是楼主自己项目里面自己写的吗?

上面的代码是我昨天下午刚写的,用在当前项目中的
qin552011373 发表于 2021-12-1 11:17 | 显示全部楼层
keer_zu 发表于 2021-11-30 10:39
上面的代码是我昨天下午刚写的,用在当前项目中的

不问的话还以为你从网上搞的,主要是速度太快了
 楼主| keer_zu 发表于 2021-12-2 10:14 | 显示全部楼层
qin552011373 发表于 2021-12-1 11:17
不问的话还以为你从网上搞的,主要是速度太快了

之前的讲解和文件系统的例子是从别的地方拷贝的,这个例子是自己写的。
qin552011373 发表于 2021-12-2 14:04 | 显示全部楼层
keer_zu 发表于 2021-12-2 10:14
之前的讲解和文件系统的例子是从别的地方拷贝的,这个例子是自己写的。 ...

我以为全部自己手写的,不过也很快了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1488

主题

12949

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部