📄 driver.c
字号:
handle->owner = pg_event_running;
handle->iam = enum_iam_left; //我是左手接口句柄
if(pg_event_running->held_device == NULL) //事件还没有打开过设备
{
handle->next = handle; //句柄指针自成双向循环链表
handle->previous = handle;
pg_event_running->held_device = handle; //事件指针指向设备链表
}else //事件已经有打开的设备
{
//以下把新设备插入到事件打开的设备队列头部
handle->next = pg_event_running->held_device;
handle->previous = pg_event_running->held_device->previous;
pg_event_running->held_device->previous->next = handle;
pg_event_running->held_device->previous = handle;
}
result = handle;
}else
result = NULL;
end_of_dev_open_left:
mutex_post(&tg_dev_mutex);
return result;
}
//----打开后代设备左手接口-----------------------------------------------------
//功能: 打开后代设备的左手接口,搜索ancestor设备的整个子设备树,找到名称与
// scion_name匹配的资源结点,然后从泛设备句柄池中分配控制块,把左手接口指针
// 赋值给句柄后,返回该句柄指针。
//参数: ancestor,被打开设备的祖先设备。
// scion_name,设备名字符串,包含路径名,
// timeout,超时设置,单位是毫秒,cn_timeout_forever=无限等待,0则立即按
// 超时返回。非0值将被向上调整为cn_tick_ms的整数倍
//返回: 成功打开设备(含经过阻塞后打开)返回设备句柄,否则返回NULL.
//-----------------------------------------------------------------------------
struct dev_handle *dev_open_left_scion(struct dev_handle *ancestor,
char *scion_name, uint32_t timeout)
{
struct pan_device *pan;
struct dev_handle *handle;
struct dev_handle *result;
if(ancestor == NULL)
return NULL;
if( ! mutex_pend(&tg_dev_mutex,timeout)) //这是保护设备树的互斥量
return NULL;
//在设备树中搜索设备
pan = (struct pan_device*)rsc_search(&ancestor->dev_interfase->node,
scion_name);
if(pan == NULL) //如果没有找到子设备,返回空
{
result = NULL;
goto end_of_dev_open_left_scion;
}
if(semp_pend(pan->left_semp,timeout)==false)//获取信号量
{
result = NULL;
goto end_of_dev_open_left_scion;
}
handle = mb_malloc(pg_device_handle_pool,0); //从池中分配句柄内存块
if(handle != NULL)
{//成功分配句柄
handle->dev_interfase = pan;
handle->owner = pg_event_running;
handle->iam = enum_iam_left; //我是左手接口句柄
if(pg_event_running->held_device == NULL) //事件还没有打开过设备
{
handle->next = handle; //句柄指针自成双向循环链表
handle->previous = handle;
pg_event_running->held_device = handle; //事件指针指向设备链表
}else //事件已经有打开的设备
{
//以下把新设备插入到事件打开的设备队列头部
handle->next = pg_event_running->held_device;
handle->previous = pg_event_running->held_device->previous;
pg_event_running->held_device->previous->next = handle;
pg_event_running->held_device->previous = handle;
}
result = handle;
}else
{
semp_post(pan->left_semp);
result = NULL;
}
end_of_dev_open_left_scion:
mutex_post(&tg_dev_mutex);
return result;
}
//----快速打开设备左手接口-----------------------------------------------------
//功能: 按快速打开设备左手接口,只适用于曾经打开且一直没有被删除的设备,因为不用
// 按设备名查找设备,速度很快
//参数: handle,待打开的设备句柄,实际上存的是泛设备指针,
// timeout,超时设置,单位是毫秒,cn_timeout_forever=无限等待,0则立即按
// 超时返回。非0值将被向上调整为cn_tick_ms的整数倍
//返回: 成功打开设备则返回true,否则返回flase.
//备注: 如果有多个用户共享本设备,为了充分共享,每次使用完毕后应该关闭设备,当需要
// 再次用同一句柄打开时,可以使用本函数.
//-----------------------------------------------------------------------------
bool_t dev_open_left_again(struct dev_handle *handle,uint32_t timeout)
{
struct pan_device *pan;
bool_t result;
if (handle == NULL) //句柄空
return false;
if( ! mutex_pend(&tg_dev_mutex,timeout)) //这是保护设备树的互斥量
return false;
pan = (struct pan_device *)handle; //句柄里存的实际是泛设备指针
if(semp_pend(pan->left_semp,timeout)==false)//获取信号量
{
result = false;
goto end_of_dev_open_left_again;
}
handle = mb_malloc(pg_device_handle_pool,0); //分配泛设备句柄控制块
if(handle == NULL)
{//分配不成功
handle = pan; //恢复句柄指针
result = false;
}else
{
handle->dev_interfase = pan;
handle->owner = pg_event_running;
handle->iam = enum_iam_right; //我是左手接口句柄
if(pg_event_running->held_device == NULL) //事件还没有打开过设备
{
handle->next = handle;
handle->previous = handle;
pg_event_running->held_device = handle;
}else //事件已经有打开的设备
{
//以下把新设备插入到事件打开的设备队列头部
handle->next = pg_event_running->held_device;
handle->previous = pg_event_running->held_device->previous;
pg_event_running->held_device->previous->next = handle;
pg_event_running->held_device->previous = handle;
}
result = true;
}
end_of_dev_open_left_again:
mutex_post(&tg_dev_mutex);
return result;
}
//----打开设备右手接口---------------------------------------------------------
//功能: 根据设备名打开设备的右手接口,搜索整个设备资源树,找到名称与name匹配的
// 资源结点,然后从泛设备句柄池中分配控制块,把右手接口指针赋值给句柄后,
// 返回该句柄指针。
//参数: name,设备名字符串,包含路径名,但不包含'dev\'这4个字符
// timeout,超时设置,单位是毫秒,cn_timeout_forever=无限等待,0则立即按
// 超时返回。非0值将被向上调整为cn_tick_ms的整数倍
//返回: 成功打开设备返回设备句柄,否则返回NULL.
//-----------------------------------------------------------------------------
struct dev_handle *dev_open_right(char *name,uint32_t timeout)
{
struct pan_device *pan;
struct dev_handle *handle;
struct dev_handle *result;
if( ! mutex_pend(&tg_dev_mutex,timeout)) //这是保护设备树的互斥量
return NULL;
//在设备树中搜索设备
pan = (struct pan_device *)rsc_search(&pg_device_root->node,name);
if(pan == NULL) //如果没有找到name设备,返回空
{
result = NULL;
goto end_of_dev_open_right;
}
if(semp_pend(pan->right_semp,timeout)==false)//获取信号量,这是保护设备本身的
{
result = NULL;
goto end_of_dev_open_right;
}
//在分配内存之前判断信号量,可避免在没有信号的情况下长时间占用内存
handle = mb_malloc(pg_device_handle_pool,0); //从池中分配句柄内存块
if(handle != NULL)
{//成功分配句柄
handle->dev_interfase = pan;
handle->owner = pg_event_running;
handle->iam = enum_iam_right; //我是右手接口句柄
if(pg_event_running->held_device == NULL) //事件还没有打开过设备
{
handle->next = handle; //句柄指针自成双向循环链表
handle->previous = handle;
pg_event_running->held_device = handle; //事件指针指向设备链表
}else //事件已经有打开的设备
{
//以下把新设备插入到事件打开的设备队列头部
handle->next = pg_event_running->held_device;
handle->previous = pg_event_running->held_device->previous;
pg_event_running->held_device->previous->next = handle;
pg_event_running->held_device->previous = handle;
}
result = handle;
}else
result = NULL;
end_of_dev_open_right:
mutex_post(&tg_dev_mutex);
return result;
}
//----打开后代设备右手接口-----------------------------------------------------
//功能: 打开后代设备的右手接口,搜索ancestor设备的整个子设备树,找到名称与
// scion_name匹配的资源结点,然后从泛设备句柄池中分配控制块,把右手接口指针
// 赋值给句柄后,返回该句柄指针。
//参数: ancestor,被打开设备的祖先设备。
// scion_name,设备名字符串,包含路径名,
// timeout,超时设置,单位是毫秒,cn_timeout_forever=无限等待,0则立即按
// 超时返回。非0值将被向上调整为cn_tick_ms的整数倍
//返回: 成功打开设备(含经过阻塞后打开)返回设备句柄,否则返回NULL.
//-----------------------------------------------------------------------------
struct dev_handle *dev_open_right_scion(struct dev_handle *ancestor,
char *scion, uint32_t timeout)
{
struct pan_device *pan;
struct dev_handle *handle;
struct dev_handle *result;
if(ancestor == NULL)
return NULL;
if( ! mutex_pend(&tg_dev_mutex,timeout)) //这是保护设备树的互斥量
return NULL;
//在设备树中搜索设备
pan = (struct pan_device*)rsc_search(&ancestor->dev_interfase->node,
scion);
if(pan == NULL) //如果没有找到子设备,返回空
{
result = NULL;
goto end_of_dev_open_right_scion;
}
if(semp_pend(pan->right_semp,timeout)==false)//获取信号量
{
result = false;
goto end_of_dev_open_right_scion;
}
handle = mb_malloc(pg_device_handle_pool,0); //从池中分配句柄内存块
if(handle != NULL)
{//成功分配句柄
handle->dev_interfase = pan;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -