关于 gsoap的安装及生产.c .h文件 参考 Croxd大神的文章 这里贴上链接 https://blog.csdn.net/weixin_42432281/article/details/84818575
1. 在复制 Croxd大神 关于ipc摄像头的代码时,发现编译不通过,一直报段错误,所以这里对大神的代码进行了一点小小的修改。大神原文链接:https://blog.csdn.net/weixin_42432281/article/details/88715196
2. 给出大神文章报错的地方。
错误原因是 给req分配内存了,但是没有给 Response 分配内存,但是在函数调用时用到了 Response 指向的内存块,所以报错。
3. 对代码的一点小小改进。
#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 ret = 0;
char sercer_addr[] = "http://192.168.6.231/onvif/device_service"; //设备搜索得到的地址
struct SOAP_ENV__Header header;
struct soap* soap = ONVIF_Initsoap(&header, NULL, NULL, 5);
struct _tds__GetCapabilities *req = (struct _tds__GetCapabilities*)malloc(sizeof(struct _tds__GetCapabilities));
struct _tds__GetCapabilitiesResponse *Response = (struct _tds__GetCapabilitiesResponse*)malloc(sizeof(struct _tds__GetCapabilitiesResponse));
req->__sizeCategory = 1;
req->Category = (enum tt__CapabilityCategory *)soap_malloc(soap, sizeof(int));
*(req->Category) = (enum tt__CapabilityCategory)5; //5表示:tt__CapabilityCategory__Media
ONVIF_SetAuthInfo(soap,"admin","123456"); //鉴权,输入摄像头的用户名、密码
ret = soap_call___tds__GetCapabilities(soap, sercer_addr, NULL,req, Response);
if(soap->error)
{
ret = -1;
printf("soap error: %d, %s, %s\n", soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
return ret;
}
else
{
if(NULL != Response->Capabilities)
{
if (Response->Capabilities->Media != NULL)
{
if (Response->Capabilities->Media->XAddr != NULL)
{
printf(" media_addr: %s \n", Response->Capabilities->Media->XAddr);
}
}
}
}
if(NULL != req)
{
free(req);
req = NULL;
}
if(NULL != Response)
{
free(Response);
Response = NULL;
}
ONVIF_soap_delete(soap);
return ret;
}
4. 编译。
编译的话在公司用的老版本的gsoap,需要加上 duration.c,不然会报错:
但是用新版本的gsoap,没有加这个文件编译通过了。这里给出两个编译命令,看哪个正确用哪个吧。
gcc -o test get_GetCapabilities_test.c stdsoap2.c soapC.c dom.c mecevp.c smdevp.c threads.c wsaapi.c wsseapi.c soapClient.c -I import/ -DWITH_OPENSSL -lssl -lcrypto -ldl
gcc -o test get_GetCapabilities_test.c stdsoap2.c duration.c soapC.c dom.c mecevp.c smdevp.c threads.c wsaapi.c wsseapi.c soapClient.c -I import/ -DWITH_OPENSSL -lssl -lcrypto -ldl
如果在构建gsoap框架代码时,忘记加在onvif.h 中添加 #import “wsse.h” ,那么如果用到需要鉴权的指令的话,会报一大堆错误:
知道原因之后就可以解决这个问题了,只需要在 onvif.h 第100行左右加上 #import “wsse.h” 即可。如图,第108行:
重新构建框架,生成一堆 .c和 .h,具体方法参考开头给出的链接。
5. 运行结果