ONVIF协议网络摄像机(IPC)客户端程序开发(8):获取设备基本信息

来源:互联网 发布:上海网络机柜回收 编辑:程序博客网 时间:2024/06/04 19:12

1 专栏导读

本专栏第一篇文章「专栏开篇」列出了专栏的完整目录,按目录顺序阅读,有助于你的理解,专栏前面文章讲过的知识点(或代码段),后面文章不会赘述。为了节省篇幅,突出重点,在文章中展示的示例代码仅仅是关键代码,你可以在「专栏开篇」中获取完整代码。

如有错误,欢迎你的留言纠正!让我们共同成长!你的「点赞」「打赏」是对我最大的支持和鼓励!

2 原理简介

上一篇文章介绍了如何搜索IPC摄像头,搜索出IPC后,就有了该IPC的Web Services地址,接下来就能通过一系列的ONVIF接口访问IPC。本文将介绍如何获取IPC摄像头的基本信息,即调用GetDeviceInformation接口。

有关GetDeviceInformation接口的描述,可以参阅devicemgmt.wsdl文档(https://www.onvif.org/onvif/ver10/device/wsdl/devicemgmt.wsdl),如下图所示:


图1

3 示例代码

使用GetDeviceInformation接口获取设备基本信息的示例代码很简单,直接调用soap_call___tds__GetDeviceInformation接口即可,附上关键源码:

/* socket超时时间(单秒秒) */#define SOAP_SOCK_TIMEOUT    (10)#define SOAP_CHECK_ERROR(result, soap, str) \    do { \        if (SOAP_OK != (result) || SOAP_OK != (soap)->error) { \            soap_perror((soap), (str)); \            if (SOAP_OK == (result)) { \                (result) = (soap)->error; \            } \            goto EXIT; \        } \} while (0)/**************************************************************************函数:ONVIF_GetDeviceInformation**功能:获取设备基本信息**参数:        [in] DeviceXAddr - 设备服务地址**返回:        0表明成功,非0表明失败**备注:************************************************************************/int ONVIF_GetDeviceInformation(const char *DeviceXAddr){    int result = 0;    struct soap *soap = NULL;    struct _tds__GetDeviceInformation           devinfo_req;    struct _tds__GetDeviceInformationResponse   devinfo_resp;    SOAP_ASSERT(NULL != DeviceXAddr);SOAP_ASSERT(NULL != (soap = ONVIF_soap_new(SOAP_SOCK_TIMEOUT)));    memset(&devinfo_req, 0x00, sizeof(devinfo_req));    memset(&devinfo_resp, 0x00, sizeof(devinfo_resp));    result = soap_call___tds__GetDeviceInformation(soap, DeviceXAddr, NULL, &devinfo_req, &devinfo_resp);    SOAP_CHECK_ERROR(result, soap, "GetDeviceInformation");    dump_tds__GetDeviceInformationResponse(&devinfo_resp);EXIT:    if (NULL != soap) {        ONVIF_soap_delete(soap);    }    return result;}void cb_discovery(char *DeviceXAddr){    ONVIF_GetDeviceInformation(DeviceXAddr);}int main(int argc, char **argv){    ONVIF_DetectDevice(cb_discovery);    return 0;}

如果顺利,执行结果类似:

================= + dump_tds__GetDeviceInformationResponse + >>>Manufacturer:       Network Digital VideoModel:              HDIPC-S25Serial Number:      1129464245Hardware Id:        IPCameraFirmware Version:   3.3.1.16================= - dump_tds__GetDeviceInformationResponse - <<<

4 鉴权失败

对于需要鉴权的IPC(如大华IPC),会执行失败,错误信息类似:

[soap] GetDeviceInformation error: 401, is internal, HTTP Error

使用Wireshark抓包工具对IPC应答的HTTP信息进行抓包,发现错误信息包含「401 Unauthorized」,即鉴权(认证)失败的意思。调用ONVIF接口,市面上有些IPC要求必须携带用户名、密码认证信息(如大华IPC),而有些IPC则不需要携带认证信息。所以才会有如上两种执行结果。

有关ONVIF鉴权(认证),下一篇文章将单独介绍。为了让测试顺利通过,可以先通过WEB浏览器登录后台,关闭鉴权,如下图所示。


图2

阅读全文
0 0