GetCapabilities:获取能力,主要目的获取设备能力信息(获取媒体服务地址)
鉴权:但是在调用获取设备能力之前是需要鉴权的。ONVIF协议规定,部分接口需要鉴权,部分接口不需要鉴权,在调用需要鉴权的接口时不使用鉴权,会导致接口调用失败。实现鉴权的方式之一可以调用gSOAP源码中的 soap_wsse_add_UsernameTokenDigest()函数。要安装依赖库OpenSSL
实现代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <assert.h> 5 6 #include "soapH.h" 7 #include "stdsoap2.h" 8 #include "soapStub.h" 9 #include "wsseapi.h" 10 11 #include "wsdd.nsmap" //命名空间 12 13 static struct soap* ONVIF_Initsoap(struct SOAP_ENV__Header *header, const char *was_To, const char *was_Action, int timeout) 14 { 15 struct soap *soap = NULL; // soap环境变量 16 unsigned char macaddr[6]; 17 char _HwId[1024]; 18 unsigned int Flagrand; 19 20 soap = soap_new(); 21 if(soap == NULL) 22 { 23 printf("[%d]soap = NULL\n", __LINE__); 24 return NULL; 25 } 26 27 soap_set_namespaces(soap, namespaces); // 设置soap的namespaces,即设置命名空间 28 29 // 设置超时(超过指定时间没有数据就退出) 30 if(timeout > 0) 31 { 32 soap->recv_timeout = timeout; 33 soap->send_timeout = timeout; 34 soap->connect_timeout = timeout; 35 } 36 else 37 { 38 //Maximum waittime : 20s 39 soap->recv_timeout = 20; 40 soap->send_timeout = 20; 41 soap->connect_timeout = 20; 42 } 43 44 soap_default_SOAP_ENV__Header(soap, header); 45 46 //Create SessionID randomly,生成uuid(windows下叫guid,linux下叫uuid),格式为urn:uuid:8-4-4-4-12,由系统随机产生 47 srand((int)time(0)); 48 Flagrand = rand()%9000 + 8888; 49 macaddr[0] = 0x1; 50 macaddr[1] = 0x2; 51 macaddr[2] = 0x3; 52 macaddr[3] = 0x4; 53 macaddr[4] = 0x5; 54 macaddr[5] = 0x6; 55 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]); 56 header->wsa__MessageID = (char *)malloc(100); 57 memset(header->wsa__MessageID, 0, 100); 58 strncpy(header->wsa__MessageID, _HwId, strlen(_HwId)); //wsa__MessageID存放的是uuid 59 60 if(was_Action != NULL) 61 { 62 header->wsa__Action = (char*)malloc(1024); 63 memset(header->wsa__Action, '\0', 1024); 64 strncpy(header->wsa__Action, was_Action, 1024); // 65 } 66 if(was_To != NULL) 67 { 68 header->wsa__To = (char *)malloc(1024); 69 memset(header->wsa__To, '\0', 1024); 70 strncpy(header->wsa__To, was_To, 1024);//"urn:schemas-xmlsoap-org:ws:2005:04:discovery"; 71 } 72 soap->header = header; 73 return soap; 74 } 75 76 77 //释放函数 78 void ONVIF_soap_delete(struct soap *soap) 79 { 80 soap_destroy(soap); // remove deserialized class instances (C++ only) 81 soap_end(soap); // Clean up deserialized data (except class instances) and temporary data 82 soap_free(soap); // Reset and deallocate the context created with soap_new or soap_copy 83 } 84 85 86 //鉴权 87 static int ONVIF_SetAuthInfo(struct soap *soap, const char *username, const char *password) 88 { 89 int result = 0; 90 if((NULL != username) || (NULL != password)){ 91 soap_wsse_add_UsernameTokenDigest(soap, NULL, username, password); 92 }else{ 93 printf("un etAuth\n"); 94 result = -1; 95 } 96 97 return result; 98 } 99 100 101 102 int main(int argc,char *argv[]) 103 { 104 int ret = 0; 10