1 專欄導讀
本專欄第一篇文章「專欄開篇」列出了專欄的完整目錄,按目錄順序閱讀,有助於你的理解,專欄前面文章講過的知識點(或代碼段),後面文章不會贅述。爲了節省篇幅,突出重點,在文章中展示的示例代碼僅僅是關鍵代碼,你可以在「專欄開篇」中獲取完整代碼。
如有錯誤,歡迎你的留言糾正!讓我們共同成長!你的「點贊」或「打賞」是對我最大的支持和鼓勵!
2 原理簡介
上一篇文章介紹瞭如何搜索IPC攝像頭,搜索出IPC後,就有了該IPC的Web Services地址,接下來就能通過一系列的ONVIF接口訪問IPC。本文將介紹如何獲取IPC攝像頭的基本信息,即調用GetDeviceInformation接口。
有關GetDeviceInformation接口的描述,可以參閱devicemgmt.wsdl文檔(https://www.onvif.org/onvif/ver10/device/wsdl/devicemgmt.wsdl),如下圖所示:
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 Video
Model: HDIPC-S25
Serial Number: 1129464245
Hardware Id: IPCamera
Firmware 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瀏覽器登錄後臺,關閉鑑權,如下圖所示。