GetServices:獲取媒體地址(有些h265的攝像頭必須用到這個介面,得到獲取能力時沒獲取到的另一個媒體地址) 鑒權:但是在使用這個介面之前是需要鑒權的。ONVIF協議規定,部分介面需要鑒權,部分介面不需要鑒權,在調用需要鑒權的介面時不使用鑒權,會導致介面調用失敗。實現鑒權的方式之一可以調用g ...
GetServices:獲取媒體地址(有些h265的攝像頭必須用到這個介面,得到獲取能力時沒獲取到的另一個媒體地址)
鑒權:但是在使用這個介面之前是需要鑒權的。ONVIF協議規定,部分介面需要鑒權,部分介面不需要鑒權,在調用需要鑒權的介面時不使用鑒權,會導致介面調用失敗。實現鑒權的方式之一可以調用gSOAP源碼中的 soap_wsse_add_UsernameTokenDigest()函數。要安裝依賴庫OpenSSL
實現代碼:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #include "soapH.h" #include "stdsoap2.h" #include "soapStub.h" #include "wsseapi.h" #include "wsdd.nsmap" //命名空間 static struct soap* ONVIF_Initsoap(struct SOAP_ENV__Header *header, const char *was_To, const char *was_Action, int timeout) { struct soap *soap = NULL; // soap環境變數 unsigned char macaddr[6]; char _HwId[1024]; unsigned int Flagrand; soap = soap_new(); if(soap == NULL) { printf("[%d]soap = NULL\n", __LINE__); return NULL; } soap_set_namespaces(soap, namespaces); // 設置soap的namespaces,即設置命名空間 // 設置超時(超過指定時間沒有數據就退出) if(timeout > 0) { soap->recv_timeout = timeout; soap->send_timeout = timeout; soap->connect_timeout = timeout; } else { //Maximum waittime : 20s soap->recv_timeout = 20; soap->send_timeout = 20; soap->connect_timeout = 20; } soap_default_SOAP_ENV__Header(soap, header); //Create SessionID randomly,生成uuid(windows下叫guid,linux下叫uuid),格式為urn:uuid:8-4-4-4-12,由系統隨機產生 srand((int)time(0)); Flagrand = rand()%9000 + 8888; macaddr[0] = 0x1; macaddr[1] = 0x2; macaddr[2] = 0x3; macaddr[3] = 0x4; macaddr[4] = 0x5; macaddr[5] = 0x6; sprintf(_HwId, "urn:uuid:%ud68a-1dd2-11b2-a105-%02X%02X%02X%02X%02X%02X", Flagrand, macaddr[0], macaddr[1], macaddr[2],macaddr[3],macaddr[4],macaddr[5]); header->wsa__MessageID = (char *)malloc(100); memset(header->wsa__MessageID, 0, 100); strncpy(header->wsa__MessageID, _HwId, strlen(_HwId)); //wsa__MessageID存放的是uuid if(was_Action != NULL) { header->wsa__Action = (char*)malloc(1024); memset(header->wsa__Action, '\0', 1024); strncpy(header->wsa__Action, was_Action, 1024); // } if(was_To != NULL) { header->wsa__To = (char *)malloc(1024); memset(header->wsa__To, '\0', 1024); strncpy(header->wsa__To, was_To, 1024);//"urn:schemas-xmlsoap-org:ws:2005:04:discovery"; } soap->header = header; return soap; } //釋放函數 void ONVIF_soap_delete(struct soap *soap) { soap_destroy(soap); // remove deserialized class instances (C++ only) soap_end(soap); // Clean up deserialized data (except class instances) and temporary data soap_free(soap); // Reset and deallocate the context created with soap_new or soap_copy } //鑒權 static int ONVIF_SetAuthInfo(struct soap *soap, const char *username, const char *password) { int result = 0; if((NULL != username) || (NULL != password)){ soap_wsse_add_UsernameTokenDigest(soap, NULL, username, password); }else{ printf("un etAuth\n"); result = -1; } return result; } int main(int argc,char *argv[]) { int i = 0; int ret = 0; char secvre_addr[] = "http://172.168.0.211/onvif/device_service"; //設備搜索獲取得到的服務地址 struct SOAP_ENV__Header header; struct _tds__GetServices *tds__GetServices; struct _tds__GetServicesResponse *tds__GetServicesResponse; struct soap* soap = ONVIF_Initsoap(&header, NULL, NULL, 5); //tds__GetServices->IncludeCapability = (enum xsd__boolean *)soap_malloc(soap, sizeof(int)); //*(tds__GetServices->IncludeCapability) = (enum xsd__boolean)0; tds__GetServices->IncludeCapability = 0; ONVIF_SetAuthInfo(soap,"admin","123456"); //鑒權 soap_call___tds__GetServices(soap,secvre_addr,NULL, tds__GetServices, tds__GetServicesResponse); if(soap->error){ ret = -1; printf("soap error: %d, %s, %s\n", soap->error, *soap_faultcode(soap), *soap_faultstring(soap)); return ret; }else{ if (tds__GetServicesResponse->Service[i].Namespace != NULL ){ for(i=0; i<tds__GetServicesResponse->__sizeService; i++) { if(strcmp(tds__GetServicesResponse->Service[i].Namespace, "http://www.onvif.org/ver20/media/wsdl") == 0) { printf(" media_addr[%d] %s\n", i, tds__GetServicesResponse->Service[i].XAddr); } if(strcmp(tds__GetServicesResponse->Service[i].Namespace, "http://www.onvif.org/ver10/media/wsdl") == 0) { printf(" media_addr->XAddr[%d] %s\n", i, tds__GetServicesResponse->Service[i].XAddr); } } } } ONVIF_soap_delete(soap); return ret; }
編譯:gcc -o test get_Uri_test.c stdsoap2.c soapC.c md5.c dom.c mecevp.c smdevp.c threads.c wsaapi.c wsseapi.c soapClient.c -I import/ -DWITH_OPENSSL -lssl -lcrypto -ldl -pthread
結果: