linux设备上的Onvif 实现19:发现及鉴权示例程序

前面讲述了一大堆函数用法,现在给出具体实例程序,完成了搜索摄像头和鉴权。

/******************************************************************************
* Name:   MySearchOnvif
*
* Desc:   search onvif device
* Param: none   
* Return: int,  return device count
* Global:  
* Note:  
* Author:   Tom-hongtao.gao
* -------------------------------------
* Log:   2013/09/25, Create this function by Tom-hongtao.gao
******************************************************************************/
int MySearchOnvif(void)
{
    int count = 0;  //获得的设信息备个数
    int i;
    BOOL bret=FALSE;
   
    /*--------存储数据反初始化-------------*/
    DLDeinit();
   
    /************初始化*************/   
    struct soap *soap;  //soap环境变量   
    soap = soap_new(); //为soap申请变量空间,并初始化
    if(soap==NULL)
    {
        printf("%s : %d, can't create new soap! \n",__FUNCTION__, __LINE__);
        return 0;
    }   
    soap_set_namespaces(soap, namespaces); //设置soap的namespaces
   
    /* ---- 探测设备------ */
    count = MySendProbe(soap);
    if(count<=0)
    {
        printf("%s : %d, can't find ipcamera!  \n ",__FUNCTION__, __LINE__);
        //清除soap
        soap_end(soap); // clean up and remove deserialized data
        soap_free(soap);//detach and free runtime context
        return 0;
    }
   
    /* ---- 检测每个设备能力------ */   
    for(i=0; i<count;i++)
    {  
        /* ---- 开始获取设备能力------ */
        bret = MyGetCapabilities(soap, i);
        if(bret==FALSE)
        {
            printf("--ERROR: MyGetCapabilities() return false!  \n"); 
            DLSetAuthorization(i, 2);  //设置为鉴权失败
            continue;
        }
       
        /* ---- 开始获取媒体信息------ */
        bret = MyGetProfiles(soap, i);
        if(bret==FALSE)
        {
            printf("--ERROR: MyGetProfiles() return false!  \n");           
            continue;
        }

        /* ---- 分析是否支持该设备------ */
        bret = MyIsSupportDevice(i);
        if(bret==FALSE)
        {
            printf("--ERROR: MyIsSupportDevice() return false!  \n");   

            /* 开始修改媒体配置信息,设为能力范围内的参数。
               配置失败放弃此设备;成功就继续执行。 */
            bret = MyModifyVideoEncoderConfigurations(soap, i);
            if(bret==FALSE)
            {
                printf("--ERROR: MyModifyVideoEncoderConfigurations() return false!  \n");       
                continue;
            }
        }

        /* ---- 开始获取流媒体RTSP地址------ */       
        int sizeprofile = DLGetSizeProfile(i);
        int j=0;
        for(j=0; j<sizeprofile; j++)
        {
            bret = MyGetStreamUri(soap, i, j);
            if(bret==FALSE)
            {
                printf("--ERROR: GetStreamUri(0) return false!  \n");
            } 
        }
        PrintfNodebyIndex(i);
    }

    bret = DLSaveCfgFile();
    if(bret==FALSE)
    {
        printf("--ERROR: DLSaveCfgFile() return false!  \n");           
    }
    else
    {
        printf("-- DLSaveCfgFile() save success! \n"); 
    }

    printf("--now delete devicenode list!  \n");       
    DLDeleteAll();  
   
    //清除soap
    soap_end(soap); // clean up and remove deserialized data
    soap_free(soap);//detach and free runtime context

    return count;

}

/******************************************************************************
* Name:   MyAuthentication
*
* Desc:   开始对指定IPC鉴权,成功后获取RTSP
* Param: int index, 指定节点序号
* Return: BOOL,  TRUE: success,  FALSE: fail
* Global:  
* Note:  
* Author:   Tom-hongtao.gao
* -------------------------------------
* Log:   2013/09/25, Create this function by Tom-hongtao.gao
******************************************************************************/
BOOL MyAuthentication(int index)
{
    int sizeprofile;
    int channel;
    BOOL bret=FALSE;
    BOOL breturn=FALSE;

    /* 0 读取指定IPC的用户名+密码 */
    char username[IPCNAMEMAX+1];  //用户名   
    char password[IPCNAMEMAX+1];  //用户密码
    memset(username, 0, IPCNAMEMAX+1);
    memset(password, 0, IPCNAMEMAX+1);
    bret = ReadUsernamePassword(index,username, password);
    if(bret==FALSE)
    {
        printf("--ERROR: can't read username and password! \n");
        return FALSE;
    }

    /* 1 打开onviflist.dat文件,初始化IPC列表 */
    bret = DLInit();
    if(bret==FALSE)
    {
        printf("--ERROR: DLInit() return false! \n");
        return FALSE;
    }
    printf("%s : %d  000 \n ",__FUNCTION__, __LINE__);
   
    /* 2 对指定的IPC使用鉴权命令查询媒体信息*/
    struct soap *soap;  //soap环境变量          
    soap = soap_new(); //为soap申请变量空间,并初始化
    if(soap==NULL)
    {
        printf("%s : %d, can't create new soap! \n",__FUNCTION__, __LINE__);
        return FALSE;
    }   
    soap_set_namespaces(soap, namespaces); //设置soap的namespaces   


   /* 3---- 开始获取设备能力------ */
   /* 试验表明,每条命令都必须重新添加密码摘要。
      原因可能是接收应答时销毁Header   */
    soap_wsse_add_UsernameTokenDigest(soap, "user", username, password);
    bret = MyGetCapabilities(soap, index);
    if(bret==FALSE)
    {
        printf("--ERROR: MyGetCapabilities() return false!  \n");    
        /* MyGetCapabilities命令:有的摄像头需要鉴权,有的不需要。
           返回FALSE 认为鉴权失败。 */
        DLSetAuthorization(index, 2); 
        goto errorcode;
    }
    else
        DLSetAuthorization(index, 1); 

    printf("%s : %d  000 \n ",__FUNCTION__, __LINE__);
    //PrintfNodebyIndex(index);

    /* 4---- 开始获取媒体信息------ */   
    soap_wsse_add_UsernameTokenDigest(soap, "user", username, password);   
    bret = MyGetProfiles(soap, index);
    if(bret==FALSE)
    {
        printf("--ERROR: MyGetProfiles() return false!  \n");
       
        /* MyGetProfiles命令:有的摄像头需要鉴权,有的不需要。
           返回FALSE 认为鉴权失败。 */
        DLSetAuthorization(index, 2);
        goto errorcode;
    }
    else
        DLSetAuthorization(index, 1); 
   
    //printf("%s : %d  222 \n ",__FUNCTION__, __LINE__);
    PrintfNodebyIndex(index);
   
    /* 5 ---- 分析是否支持该设备------ */
    bret = MyIsSupportDevice(index);
    if(bret==FALSE)
    {
        printf("--ERROR: MyIsSupportDevice() return false!  \n");   

        /* 开始修改媒体配置信息,设为能力范围内的参数。
           配置失败放弃此设备;成功就继续执行。 */
        bret = MyModifyVideoEncoderConfigurations_Auth(soap, index, username, password);
        if(bret==FALSE)
        {
            printf("--ERROR: MyModifyVideoEncoderConfigurations() return false!  \n");
            goto errorcode;
        }
    }
   
    /* 6 ---- 开始获取流媒体RTSP地址------ */
    sizeprofile = DLGetSizeProfile(index);
    int i=0;
    for(i=0; i<sizeprofile; i++)
    {
        soap_wsse_add_UsernameTokenDigest(soap, "user", username, password);
        bret = MyGetStreamUri(soap, index, i);
        if(bret==FALSE)
        {
            printf("--ERROR: GetStreamUri(0) return false!  \n");           
            goto errorcode;
        } 
    }
   
    PrintfNodebyIndex(index);   
    breturn = TRUE;
    goto rightcode;

errorcode:   
    breturn = FALSE;
   
rightcode:   
    /* 7----- 保存数据--------*/   
    bret = DLSaveCfgFile();
    if(bret==FALSE)
    {
        printf("--ERROR: DLSaveCfgFile() return false!  \n");           
    }
    else
    {
        printf("-- DLSaveCfgFile() save success! \n"); 
    }
    DLDeleteAll();   
   
    //清除soap
    soap_end(soap); // clean up and remove deserialized data
    soap_free(soap);//detach and free runtime context

    return breturn;
}

 

 

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值