Skip to content

Commit

Permalink
Merge pull request #139 from cruise2018/iot_link
Browse files Browse the repository at this point in the history
modify:driver:modify the driver interface and make it more standard
  • Loading branch information
cruise2018 authored Jul 29, 2019
2 parents bf03d2e + 2ac9f46 commit 5e3a2d0
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 131 deletions.
17 changes: 11 additions & 6 deletions iot_link/at/at.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ static at_cb_t g_at_cb; //this is the at controller here
static int __cmd_send(const void *buf,size_t buflen,uint32_t timeout)
{
int i = 0;
int ret = 0;
ssize_t ret = 0;
int debugmode;
uint8_t *msg;
ret = los_dev_write(g_at_cb.devhandle,0,(unsigned char *)buf,buflen,timeout);
ret = los_dev_write(g_at_cb.devhandle,0,buf,buflen,timeout);
if(ret > 0)
{
msg = (uint8_t *)buf;
Expand All @@ -116,19 +116,24 @@ static int __cmd_send(const void *buf,size_t buflen,uint32_t timeout)
default:
break;
}
}
ret = 0;
}
else
{
ret = -1;
}
return ret;
}

//this function used to receive data from the AT channel
static int __resp_rcv(void *buf,int buflen,uint32_t timeout)
static int __resp_rcv(void *buf,size_t buflen,uint32_t timeout)
{
int i = 0;
int ret = 0;
ssize_t ret = 0;
int debugmode;
uint8_t *msg;

ret = los_dev_read(g_at_cb.devhandle,0,(unsigned char *)buf,buflen,timeout);
ret = los_dev_read(g_at_cb.devhandle,0,buf,buflen,timeout);
if(ret > 0)
{
msg = buf;
Expand Down
13 changes: 6 additions & 7 deletions iot_link/driver/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
```
typedef void* los_driv_t ;//returned by the driver register
typedef bool_t (*fn_devopen) (void *pri,s32_t flag);
typedef s32_t (*fn_devread) (void *pri,u32_t offset,u8_t *buf,s32_t len,u32_t timeout);
typedef s32_t (*fn_devwrite) (void *pri,u32_t offset,u8_t *buf,s32_t len,u32_t timeout);
typedef ssize_t (*fn_devread) (void *pri,size_t offset,void *buf,size_t len,uint32_t timeout);
typedef ssize_t (*fn_devwrite) (void *pri,size_t offset,const void *buf,size_t len,uint32_t timeout);
typedef void (*fn_devclose) (void *pri);
typedef bool_t (*fn_devioctl) (void *pri,u32_t cmd, void *para,s32_t len);
typedef bool_t (*fn_devinit) (void *pri);
Expand Down Expand Up @@ -79,8 +79,8 @@ OSDRIV_EXPORT(varname,drivname,operate,pridata,flagmask)
//these interface by the application
typedef void* los_dev_t ; //this type is returned by the dev open
los_dev_t los_dev_open (const char *name,u32_t flag);
s32_t los_dev_read (los_dev_t dev,u32_t offset,u8_t *buf,s32_t len,u32_t timeout);
s32_t los_dev_write (los_dev_t dev,u32_t offset,u8_t *buf,s32_t len,u32_t timeout);
ssize_t los_dev_read (los_dev_t dev,size_t offset,void *buf,size_t len,uint32_t timeout);
ssize_t los_dev_write (los_dev_t dev,size_t offset,const void *buf,size_t len, uint32_t timeout);
bool_t los_dev_close (los_dev_t dev);
bool_t los_dev_ioctl (los_dev_t dev,u32_t cmd,void *para,s32_t paralen);
off_t los_dev_seek (los_dev_t dev,off_t offset, int fromwhere);
Expand All @@ -90,7 +90,6 @@ off_t los_dev_seek (los_dev_t dev,off_t offset, int fromwhere);

#### 重要的配置选项

1. 如果使能驱动框架,则需要在target_config.h中配置LOSCFG_ENABLE_DRIVER
2. 如果使能设备文件系统,则需要在target_config.h中配置LOSCFG_ENABLE_DEVFS,设备文件系统本身依赖VFS;同时如果需要通过设备文件系统操作设备,则需要配置驱动框架
3. 包含路径:驱动的注册和使用需要包含los_dev.h,需要在编译器的PATH中添加component/driver目录
1. 如果使能驱动框架,则需要添加driver目录,并且在编译中定义CONFIG_DRIVER_ENABLE=1
3. 包含路径:驱动的注册和使用需要包含driver.h

4 changes: 2 additions & 2 deletions iot_link/driver/dev_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static ssize_t devfs_read (struct file *file, char *buf, size_t len)
los_dev_t dev;

dev = file->f_data;
ret = los_dev_read (dev,file->f_offset,(u8_t *)buf,len,cn_devfs_timeout);
ret = los_dev_read (dev,file->f_offset,(void *)buf,len,cn_devfs_timeout);
if(ret > 0)
{
file->f_offset += ret;
Expand All @@ -97,7 +97,7 @@ static ssize_t devfs_write (struct file *file, const char *buf, size_t len)
los_dev_t dev;

dev = file->f_data;
ret = los_dev_write (dev,file->f_offset,(u8_t *)buf,len,cn_devfs_timeout);
ret = los_dev_write (dev,file->f_offset,(const void *)buf,len,cn_devfs_timeout);
if(ret > 0)
{
file->f_offset += ret;
Expand Down
24 changes: 13 additions & 11 deletions iot_link/driver/dev_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#include <driver.h>
#include <sys/fcntl.h>

#include <osal.h>

//this file implement some demo to test the device module
#define cn_testdriv_buf_len 256

Expand Down Expand Up @@ -168,10 +170,10 @@ static int __driv_open(int argc,const char *argv[]) //dirvopen drivname flag
OSSHELL_EXPORT_CMD(__driv_open,"drivopen","drivopen name flag");


static int __driv_write(int argc,const char *argv[]) //drivewrite string timeout
static ssize_t __driv_write(int argc,const char *argv[]) //drivewrite string timeout
{
int ret;
unsigned int timeout = 0;
ssize_t ret;
uint32_t timeout = 0;

if(argc != 3)
{
Expand All @@ -180,7 +182,7 @@ static int __driv_write(int argc,const char *argv[]) //drivewrite string timeout
}

timeout = strtoul(argv[2],NULL,0);
ret = los_dev_write(s_shell_opendev,0,(unsigned char *)argv[1],strlen(argv[1]),timeout);
ret = los_dev_write(s_shell_opendev,0,(const void *)argv[1],strlen(argv[1]),timeout);
printf("write:%d bytes\n\r",ret);

return 0;
Expand All @@ -189,12 +191,12 @@ static int __driv_write(int argc,const char *argv[]) //drivewrite string timeout
OSSHELL_EXPORT_CMD(__driv_write,"drivwrite","drivwrite string timeout");


static int __driv_read(int argc,const char *argv[]) //driveread len timeout
static ssize_t __driv_read(int argc,const char *argv[]) //driveread len timeout
{
int ret;
unsigned int timeout = 0;
int len ;
unsigned char *buf;
ssize_t ret;
uint32_t timeout = 0;
size_t len ;
void *buf;

if(argc != 3)
{
Expand All @@ -205,10 +207,10 @@ static int __driv_read(int argc,const char *argv[]) //driveread len timeout
len = strtoul(argv[1],NULL,0);
timeout = strtoul(argv[2],NULL,0);

buf = malloc(len);
buf = osal_malloc(len);
ret = los_dev_read(s_shell_opendev,0,buf,len,timeout);
printf("read:%d bytes\n\r",ret);
free(buf);
osal_free(buf);
return 0;
}

Expand Down
48 changes: 24 additions & 24 deletions iot_link/driver/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include <stdlib.h>
#include <driver.h>

#if CFG_DRIVER_ENABLE
#if CONFIG_DRIVER_ENABLE

#include <sys/fcntl.h>
#include <sys/types.h>
Expand Down Expand Up @@ -70,10 +70,10 @@ struct driv_cb
unsigned int drivstatus; //show the state here like init or something like this
los_dev_t devlst; //the open list,support the multi open
//following member used for the debug
unsigned int total_write; //how many data has been sent
unsigned int total_read; //how many data has received
unsigned int opencounter; //reference counter
unsigned int errno; //the last errno has happend
size_t total_write; //how many data has been sent
size_t total_read; //how many data has received
size_t opencounter; //reference counter
unsigned int errno; //the last errno has happend
};
typedef struct
{
Expand Down Expand Up @@ -106,36 +106,36 @@ function :bsp developer use this function to add a device to the system
parameters :
instruction :NULL if failed else return the device handle
*******************************************************************************/
los_driv_t los_driv_register(const char *name, const los_driv_op_t *op,\
void *pri,unsigned int flagmask)

los_driv_t los_driv_register(os_driv_para_t *para)
{
struct driv_cb *driv = NULL;

if((NULL == name)||(NULL == op))
if((NULL == para->name)||(NULL == para->op))
{
goto EXIT_PARAS;
}

driv = malloc(sizeof(struct driv_cb));
driv = osal_malloc(sizeof(struct driv_cb));
if(NULL == driv)
{
goto EXIT_MALLOC;
}
memset(driv,0,sizeof(struct driv_cb));

//do the member initialize
driv->name = name;
driv->op = op;
driv->pri = pri;
driv->flagmask = flagmask;
driv->name = para->name;
driv->op = para->op;
driv->pri = para->pri;
driv->flagmask = para->flag;

//add it to the device list if no device with the same name exsit
if(false == osal_mutex_lock(s_los_driv_module.lock))
{
goto EXIT_MUTEX;
}

if(NULL != __driv_match(name))
if(NULL != __driv_match(para->name))
{
goto EXIT_EXISTED;
}
Expand All @@ -152,7 +152,7 @@ los_driv_t los_driv_register(const char *name, const los_driv_op_t *op,\
EXIT_EXISTED:
osal_mutex_unlock(s_los_driv_module.lock);
EXIT_MUTEX:
free(driv);
osal_free(driv);
driv = NULL;
EXIT_MALLOC:
EXIT_PARAS:
Expand Down Expand Up @@ -198,7 +198,7 @@ bool_t los_driv_unregister(const char *name)
pre->nxt = tmp->nxt;
}

free(tmp);
osal_free(tmp);
ret = true;

s_los_driv_module.drivnum--;
Expand Down Expand Up @@ -246,7 +246,7 @@ static void osdriv_load_static(void){
#endif
for(i =0;i<num;i++)
{
los_driv_register(para->name,para->op,para->pri,para->flag);
los_driv_register(para);
para++;
}

Expand Down Expand Up @@ -303,7 +303,7 @@ los_dev_t los_dev_open (const char *name,unsigned int flag)
goto EXIT_PARAERR;
}

dev = malloc(sizeof(struct dev_cb));
dev = osal_malloc(sizeof(struct dev_cb));
if (NULL == dev)
{
goto EXIT_MEMERR;
Expand Down Expand Up @@ -367,7 +367,7 @@ los_dev_t los_dev_open (const char *name,unsigned int flag)
EXIT_DRIVERR:
osal_mutex_unlock(s_los_driv_module.lock);
EXIT_MUTEXERR:
free(dev);
osal_free(dev);
dev = NULL;
EXIT_MEMERR:
EXIT_PARAERR:
Expand Down Expand Up @@ -436,7 +436,7 @@ bool_t los_dev_close (los_dev_t dev)
driv->drivstatus &= (~cn_driv_status_initialized);
}

free(dev);
osal_free(dev);
driv->opencounter--;

osal_mutex_unlock(s_los_driv_module.lock);
Expand All @@ -460,9 +460,9 @@ parameters :dev,returned by the los_dev_open function
timeout:the waittime if no data current
instruction :how many data has been read to the buf
*******************************************************************************/
int los_dev_read (los_dev_t dev,unsigned int offset,unsigned char *buf,int len,unsigned int timeout)
ssize_t los_dev_read (los_dev_t dev,size_t offset, void *buf,size_t len,uint32_t timeout)
{
int ret = 0;
ssize_t ret = 0;
struct dev_cb *devcb;
struct driv_cb *drivcb;

Expand Down Expand Up @@ -495,9 +495,9 @@ parameters :dev,returned by the los_dev_open function
timeout:the waittime if no data current
instruction :how many data has been written to the device
*******************************************************************************/
int los_dev_write (los_dev_t dev,unsigned int offset,unsigned char *buf,int len,unsigned int timeout)
ssize_t los_dev_write (los_dev_t dev,size_t offset,const void *buf,size_t len, uint32_t timeout)
{
int ret = 0;
ssize_t ret = 0;
struct dev_cb *devcb;
struct driv_cb *drivcb;

Expand Down
31 changes: 16 additions & 15 deletions iot_link/driver/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@

#include <osal.h>

#define CFG_DRIVER_ENABLE 1 //for the test

typedef void* los_driv_t ;//returned by the driver register
typedef bool_t (*fn_devopen) (void *pri,int flag);
typedef int (*fn_devread) (void *pri,unsigned int offset,unsigned char *buf,int len,unsigned int timeout);
typedef int (*fn_devwrite) (void *pri,unsigned int offset,unsigned char *buf,int len,unsigned int timeout);
typedef ssize_t (*fn_devread) (void *pri,size_t offset,void *buf,size_t len,uint32_t timeout);
typedef ssize_t (*fn_devwrite) (void *pri,size_t offset,const void *buf,size_t len,uint32_t timeout);
typedef void (*fn_devclose) (void *pri);
typedef bool_t (*fn_devioctl) (void *pri,unsigned int cmd, void *para,int len);
typedef bool_t (*fn_devinit) (void *pri);
Expand All @@ -69,33 +68,35 @@ typedef struct
//attention that whether the device support multi read and write depend on the device itself
typedef void* los_dev_t ; //this type is returned by the dev open

#if CFG_DRIVER_ENABLE
//we also supply a macro to add a driver staticly ,which means
typedef struct
{
const char *name; //device driver name
los_driv_op_t *op; //device operate functions
void *pri; //private data,will be passed to op functions
uint32_t flag; //flags, like O_RDONLY O_WRONLY O_RDWR
}os_driv_para_t;

#if CONFIG_DRIVER_ENABLE
//device module init entry
bool_t los_driv_init(void);
//these interface called by the bsp develop
los_driv_t los_driv_register(const char *name, const los_driv_op_t *op,void *pri,unsigned int flagmask);
los_driv_t los_driv_register(os_driv_para_t *para);
bool_t los_driv_unregister(const char *name) ;
bool_t los_driv_event(los_driv_t driv,unsigned int event,void *para);
//attention that the device dirver could use this function to send some event to the application,
//something like the device could be read or write or something like this for async call

//these interface by the application
los_dev_t los_dev_open (const char *name,unsigned int flag);
int los_dev_read (los_dev_t dev,unsigned int offset,unsigned char *buf,int len,unsigned int timeout);
int los_dev_write (los_dev_t dev,unsigned int offset,unsigned char *buf,int len,unsigned int timeout);
ssize_t los_dev_read (los_dev_t dev,size_t offset,void *buf,size_t len,uint32_t timeout);
ssize_t los_dev_write (los_dev_t dev,size_t offset,const void *buf,size_t len, uint32_t timeout);
bool_t los_dev_close (los_dev_t dev);
bool_t los_dev_ioctl (los_dev_t dev,unsigned int cmd,void *para,int paralen);
off_t los_dev_seek (los_dev_t dev,off_t offset, int fromwhere);
//attention that the cmd and para used in ioctl must compatible with the driver development

//we also supply a macro to add a driver staticly ,which means
typedef struct
{
const char *name; //device driver name
los_driv_op_t *op; //device operate functions
void *pri; //private data,will be passed to op functions
unsigned int flag; //flags, like O_RDONLY O_WRONLY O_RDWR
}os_driv_para_t;


#define OSDRIV_EXPORT(varname,drivname,operate,pridata,flagmask) \
static const os_driv_para_t varname __attribute__((used,section("osdriv")))= \
Expand Down
Loading

0 comments on commit 5e3a2d0

Please sign in to comment.