完成框架搭建后,编写自己的主函数起onvif服务

编写makefile

objs = onvif.o onvif_func.o duration.o soapC.o soapServer.o stdsoap2.o

onvif:$(objs)
    gcc -o onvif $(objs)

.PHONY:clean
clean:
    #-rm onvif
    rm *[!C.o].o

发现提示好多函数没有定义,在头文件soapStub.h中定义的,直接把没有定义的函数声明拷贝到一个onvif_func.c中

暂时用ue等工具实现一个空函数,

将;替换为^p{^p^treturn SOAP_OK;^p}

就可以实现素有函数的空实现,然后编译通过就可以了。。。

下面就是启动onvif服务端代码的具体实现了。。

组播setsockopt:no such device问题的解决方法

route add -net 224.0.0.0 netmask 224.0.0.0 eth0

然后

4、ProbeMatches代码

这样就创建了基本的服务端和客户端的代码了,下面需要添加具体的代码了。

其中包括:

(1)创建组播用的udp socket,绑定组播地址为239.255.255.250,端口为3702,因为ws-discovery的组播地址和端口就是为239.255.255.250和3702

(2)在产生的Probe函数中添加ProbeMatches代码
首先是udp socket

  1. int bind_server_udp1(int server_s)
  2. {
  3. struct sockaddr_in local_addr;
  4. memset(&local_addr,0,sizeof(local_addr));
  5. local_addr.sin_family = AF_INET;
  6. local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  7. local_addr.sin_port = htons(3702);
  8. return bind(server_s,(struct sockaddr*)&local_addr,sizeof(local_addr));
  9. }
  10. static int create_server_socket_udp(void)
  11. {
  12. int server_udp;
  13. unsigned char one = 1;
  14. int sock_opt = 1;
  15. //server_udp = socket(PF_INET, SOCK_DGRAM, 0);
  16. server_udp = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  17. if (server_udp == -1) {
  18. printf("unable to create socket\n");
  19. }
  20. /* reuse socket addr */
  21. if ((setsockopt(server_udp, SOL_SOCKET, SO_REUSEADDR, (void *) &sock_opt,
  22. sizeof (sock_opt))) == -1) {
  23. printf("setsockopt\n");
  24. }
  25. if ((setsockopt(server_udp, IPPROTO_IP, IP_MULTICAST_LOOP,
  26. &one, sizeof (unsigned char))) == -1) {
  27. printf("setsockopt\n");
  28. }
  29. struct ip_mreq mreq;
  30. mreq.imr_multiaddr.s_addr = inet_addr("239.255.255.250");
  31. mreq.imr_interface.s_addr = htonl(INADDR_ANY);
  32. if(setsockopt(server_udp,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq))==-1){
  33. perror("memberchip error\n");
  34. }
  35. return server_udp;
  36. }

需要注意几点:1/设置socket属性SO_REUSEADDR,2、设置socket属性IP_ADD_MEMBERSHIP,目的是让3702的端口能够重复绑定,一家加入组播组。

其次是添加ProbeMatches代码
(1)首先复制client的soap_send___wsdd__ProbeMatches函数到服务端来,因为soap_send___wsdd__ProbeMatches已经写好了用于响应Probe消息的框架了,不用白不用啊。
(2)编写__wsdd__Probe函数,添加如下内容

  1. int  __wsdd__Probe(struct soap* soap, struct wsdd__ProbeType *wsdd__Probe)
  2. {
  3. DBG("__wsdd__Probe\n");
  4. char macaddr[6];
  5. char _IPAddr[INFO_LENGTH];
  6. char _HwId[1024];
  7. wsdd__ProbeMatchesType ProbeMatches;
  8. ProbeMatches.ProbeMatch = (struct wsdd__ProbeMatchType *)soap_malloc(soap, sizeof(struct wsdd__ProbeMatchType));
  9. ProbeMatches.ProbeMatch->XAddrs = (char *)soap_malloc(soap, sizeof(char) * INFO_LENGTH);
  10. ProbeMatches.ProbeMatch->Types = (char *)soap_malloc(soap, sizeof(char) * INFO_LENGTH);
  11. ProbeMatches.ProbeMatch->Scopes = (struct wsdd__ScopesType*)soap_malloc(soap,sizeof(struct wsdd__ScopesType));
  12. ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceProperties = (struct wsa__ReferencePropertiesType*)soap_malloc(soap,sizeof(struct wsa__ReferencePropertiesType));
  13. ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceParameters = (struct wsa__ReferenceParametersType*)soap_malloc(soap,sizeof(struct wsa__ReferenceParametersType));
  14. ProbeMatches.ProbeMatch->wsa__EndpointReference.ServiceName = (struct wsa__ServiceNameType*)soap_malloc(soap,sizeof(struct wsa__ServiceNameType));
  15. ProbeMatches.ProbeMatch->wsa__EndpointReference.PortType = (char **)soap_malloc(soap, sizeof(char *) * SMALL_INFO_LENGTH);
  16. ProbeMatches.ProbeMatch->wsa__EndpointReference.__any = (char **)soap_malloc(soap, sizeof(char*) * SMALL_INFO_LENGTH);
  17. ProbeMatches.ProbeMatch->wsa__EndpointReference.__anyAttribute = (char *)soap_malloc(soap, sizeof(char) * SMALL_INFO_LENGTH);
  18. ProbeMatches.ProbeMatch->wsa__EndpointReference.Address = (char *)soap_malloc(soap, sizeof(char) * INFO_LENGTH);
  19. macaddr[0]=0x01;macaddr[1]=0x01;macaddr[2]=0x01;macaddr[3]=0x01;macaddr[4]=0x01;macaddr[5]=0x01;
  20. sprintf(_HwId,"urn:uuid:2419d68a-2dd2-21b2-a205-%02X%02X%02X%02X%02X%02X",macaddr[0], macaddr[1], macaddr[2], macaddr[3], macaddr[4], macaddr[5]);
  21. sprintf(_IPAddr, "http://%03d.%03d.%1d.%03d/onvif/device_service", 192, 168, 1, 233);
  22. ProbeMatches.__sizeProbeMatch = 1;
  23. ProbeMatches.ProbeMatch->Scopes->__item =(char *)soap_malloc(soap, 1024);
  24. memset(ProbeMatches.ProbeMatch->Scopes->__item,0,sizeof(ProbeMatches.ProbeMatch->Scopes->__item));
  25. //Scopes MUST BE
  26. strcat(ProbeMatches.ProbeMatch->Scopes->__item, "onvif://www.onvif.org/type/NetworkVideoTransmitter");
  27. ProbeMatches.ProbeMatch->Scopes->MatchBy = NULL;
  28. strcpy(ProbeMatches.ProbeMatch->XAddrs, _IPAddr);
  29. strcpy(ProbeMatches.ProbeMatch->Types, wsdd__Probe->Types);
  30. DBG("wsdd__Probe->Types=%s\n",wsdd__Probe->Types);
  31. ProbeMatches.ProbeMatch->MetadataVersion = 1;
  32. //ws-discovery规定 为可选项
  33. ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceProperties->__size = 0;
  34. ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceProperties->__any = NULL;
  35. ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceParameters->__size = 0;
  36. ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceParameters->__any = NULL;
  37. ProbeMatches.ProbeMatch->wsa__EndpointReference.PortType[0] = (char *)soap_malloc(soap, sizeof(char) * SMALL_INFO_LENGTH);
  38. //ws-discovery规定 为可选项
  39. strcpy(ProbeMatches.ProbeMatch->wsa__EndpointReference.PortType[0], "ttl");
  40. ProbeMatches.ProbeMatch->wsa__EndpointReference.ServiceName->__item = NULL;
  41. ProbeMatches.ProbeMatch->wsa__EndpointReference.ServiceName->PortName = NULL;
  42. ProbeMatches.ProbeMatch->wsa__EndpointReference.ServiceName->__anyAttribute = NULL;
  43. ProbeMatches.ProbeMatch->wsa__EndpointReference.__any[0] = (char *)soap_malloc(soap, sizeof(char) * SMALL_INFO_LENGTH);
  44. strcpy(ProbeMatches.ProbeMatch->wsa__EndpointReference.__any[0], "Any");
  45. strcpy(ProbeMatches.ProbeMatch->wsa__EndpointReference.__anyAttribute, "Attribute");
  46. ProbeMatches.ProbeMatch->wsa__EndpointReference.__size = 0;
  47. strcpy(ProbeMatches.ProbeMatch->wsa__EndpointReference.Address, _HwId);
  48. /*注释的部分为可选,注释掉onvif test也能发现ws-d*/
  49. //soap->header->wsa__To = "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous";
  50. //soap->header->wsa__Action = "http://schemas.xmlsoap.org/ws/2005/04/discovery/ProbeMatches";
  51. soap->header->wsa__RelatesTo = (struct wsa__Relationship*)soap_malloc(soap, sizeof(struct wsa__Relationship));
  52. //it's here
  53. soap->header->wsa__RelatesTo->__item = soap->header->wsa__MessageID;
  54. soap->header->wsa__RelatesTo->RelationshipType = NULL;
  55. soap->header->wsa__RelatesTo->__anyAttribute = NULL;
  56. soap->header->wsa__MessageID =(char *)soap_malloc(soap, sizeof(char) * INFO_LENGTH);
  57. strcpy(soap->header->wsa__MessageID,_HwId+4);
  58. /* send over current socket as HTTP OK response: */
  59. /*测试过,第二参数必须http,action随意*/
  60. soap_send___wsdd__ProbeMatches(soap, "http://", NULL, &ProbeMatches);
  61. return SOAP_OK;
  62. }

想要写出上述代码,是一定要了解SOAP格式的,在WS-Discovery中描述了discovery所用的soap格式

1首先是了解消息头header和ProbeMatches中的内容,非常重要,可以参考这里http://www.w3.org/Submission/ws-addressing/  最好详细的学习一下,里面的内容非常重要。

2其次需要理解的是,其实当你看完ws-addressing后你会发现,骨架代码中的结构体和SOAP消息中的内容是一一对应的,例如:

结构体osap->header对应SOAP消息的<SOAP-ENV:Header></SOAP-ENV:Header>中的内容,包含在header里的内容当然会包含在SOAP的header内。例如:

结构体soap->header->wsa__RelatesTo对应的是<wsa:RelatesTo></wsa:RelatesTo>。

3最后需要理解的是,在代码中的"__"双下划线一般对应xml中的命名空间的":",下划线前是命名空间,后是具体内容。

4最后的最后是要详细的阅读ONVIF Core Specification

下图为响应OnvifTestTool的Probe命令的SOAP消息

结合上图再分析代码就亲切多了。在ONVIF Core Specification的7.3.2.2  Scopes 一节描述了onvif需要的Scopes,这个是需要在程序里填充,具体填充什么,文档里说的很明确:

注意点是在太多,随便漏掉一个都可能会导致搜不到设备,下图是非常重要的一个:

SOAP1.1和SOAP1.2所使用的SOAP-ENV是不同的,ONVIF使用的是SOAP1.1,如果soapcpp2产生的nsmap文件中的SOAP-ENV是SOAP1.2版本的话,那么OnvifTestTool是不会识别设备发出的SOAP消息的。

5、该main函数登场了

  1. int main()
  2. {
  3. int server_udp;
  4. int retval=0;
  5. struct soap *soap_udp;
  6. int fault_flag = 0;
  7. server_udp = create_server_socket_udp();
  8. bind_server_udp1(server_udp);
  9. while(1){
  10. soap_udp=soap_new();
  11. soap_init1(soap_udp, SOAP_IO_UDP);
  12. soap_udp->master = server_udp;
  13. soap_udp->socket = server_udp;
  14. soap_udp->errmode = 0;
  15. soap_udp->bind_flags = 1;
  16. if (!soap_valid_socket(soap_bind(soap_udp, NULL, 3702, 100)))
  17. {
  18. soap_print_fault(soap_udp, stderr);
  19. }
  20. fprintf(stderr,"soap_serve starting..\n");
  21. retval = soap_serve(soap_udp); //阻塞在这里
  22. fprintf(stderr,"retval=%d\n",retval);
  23. if(retval && !(fault_flag))
  24. {
  25. fault_flag = 1;
  26. }
  27. else if(!retval)
  28. {
  29. fault_flag = 0;
  30. }
  31. soap_destroy(soap_udp);
  32. soap_end(soap_udp);
  33. soap_done(soap_udp);
  34. free(soap_udp);
  35. }
  36. }

soap_server函数会一直阻塞,直到接收到SOAP消息,并且该处理是一次性的,所以要将将soap_server放到while里或者独立的线程中。
最后编译运行

问题:哥编译出来的代码,各项都正确啊,就是搜索不到,test工具提示没有返回消息,麻蛋啊。。

自习查看上文才发现,哥的SOAP1.2的,onvif只支持1.1,换了果断可以搜索到。。妹的。。

onvif开发实战2--总结框架搭建的更多相关文章

  1. onvif开发实战1--总结框架搭建

    Gsoap及开发框架生成: 一:gsoap下载和编译   1.下载Gsoap:地址:http://sourceforge.net/projects/gsoap2/files/gSOAP/ 2.安装: ...

  2. iOS开发——实战OC篇&环境搭建之Xib(玩转UINavigationController与UITabBarController)

    iOS开发——实战OC篇&环境搭建之Xib(玩转UINavigationController与UITabBarController)   前面我们介绍了StoryBoard这个新技术,和纯技术 ...

  3. iOS开发——实战OC篇&环境搭建之纯代码(玩转UINavigationController与UITabBarController)

    iOS开发——实战OC篇&环境搭建之纯代码(玩转UINavigationController与UITabBarController)   这里我们就直接上实例: 一:新建一个项目singleV ...

  4. iOS开发——实战OC篇&环境搭建之StoryBoard(玩转UINavigationController与UITabBarController)

      环境搭建之StoryBoard(玩转UINavigationController与UITabBarController)   研究了这么就IOS开发,都没有所处一个像样或者自己忙一点的项目.最近自 ...

  5. ABP框架个人开发实战(1)_环境搭建

    前言 之前关注ABP框架有一阵子了,一直没有潜下心来实际研究一下.最近想自己建站,以后有自己的功能开发项目,可以在自己的站点上开发,并一步步的完善,所以找个比较好用的框架迫在眉睫,选来选去,决定用AB ...

  6. Spring Boot微服务电商项目开发实战 --- 基础配置及搭建

    根据SpringBoot实现分布式微服务项目近两年的开发经验,今天决定开始做SpringBoot实现分布式微服务项目的系列文章,帮助其他正在使用或计划使用SringBoot开发的小伙伴们.本次系列文章 ...

  7. Cordova+Asp.net Mvc+GIS跨平台移动应用开发实战1-系统初步搭建(附演示,apk,全部源码)

    1.前言 身处在移动互联网的今天,移动应用开发炙手可热,身为程序猿的我们怎么能错过开发一款我们自己的APP.本人算是一个基于.net的GIS开发入门者(马上就大四啦), 暑假在学校参加GIS比赛有大把 ...

  8. Go实战--通过gin-gonic框架搭建restful api服务(github.com/gin-gonic/gin)

    生命不止,继续 go go go !!! 先插播一条广告,给你坚持学习golang的理由: <2017 软件开发薪酬调查:Go 和 Scala 是最赚钱的语言> 言归正传! 之前写过使用g ...

  9. [Lua游戏AI开发指南] 笔记零 - 框架搭建

    一.图书详情 <Lua游戏AI开发指南>,原作名: Learning Game AI Programming with Lua. 豆瓣:https://book.douban.com/su ...

随机推荐

  1. 【Henu ACM Round#19 F】Dispute

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 这一题和这一题很像 (链接 ) 会发现如果a[i]!=b[i]那么就按下i就好了. 然后改变和他相邻的点. 此后a[i]再也不可能和 ...

  2. 洛谷 P1096 Hanoi双塔问题

    P1096 Hanoi双塔问题 题目描述 给定A.B.C三根足够长的细柱,在A柱上放有2n个中间有孔的圆盘,共有n个不同的尺寸,每个尺寸都有两个相同的圆盘,注意这两个圆盘是不加区分的(下图为n=3的情 ...

  3. Ubuntu14.04下安装和&quot;激活&quot;Office2010ProPlus与Visio2010(15.11.20Updated)

    本人用Ubuntu的时候全然没有打游戏的欲望,故而能够更高效的工作. 尽管说LibreOffice.WPS等等有Ubuntu版本号,可是用着还是没有微软的Office顺手,故而折腾了一下怎样安装Off ...

  4. Maven的SSH搭建以及部署

    本人有点傻,研究Maven研究了有一段时间,刚刚有些入门,记录下来方便以后使用 工作环境:jdk7 myeclipse10 maven3.1.1 1 下载maven3.1.1 http://maven ...

  5. C++windows内核编程笔记day11 win32静态库和动态库的使用

    windows库程序: 静态库: 源码被链接到调用的程序或动态库,被调用时,代码最少有1份,文件后缀.LIB 动态库: 函数被程序或其它动态库调用,被调用时,代码仅仅有1份,文件后缀.DLL 静态库( ...

  6. UVA 11991 Easy Problem from Rujia Liu?【STL】

    题目链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142">https://uv ...

  7. git如何从远程非master分支更新到本地对应分支

    git如何从远程非master分支更新到本地对应分支 自己实例 正确步骤 如果本地有分支,那就删除本地分支 删除本地分支::git branch -d 2018_4_18_second 切换分支: g ...

  8. 15.Intellij中配置jdk/tomcat/maven

    转自:https://blog.csdn.net/u010414666/article/details/44465905 继上一篇安装好了Intellij之后,我们可以对Intellij,做一些简单的 ...

  9. MySQL外键的介绍

    在MySQL 3.23.44版本后,InnoDB引擎类型的表支持了外键约束. 1.MySQL中“键”和“索引”的定义相同,所以外键和主键一样也是索引的一种.不同的是MySQL会自动为所有表的主键进行索 ...

  10. window安装MQTT服务器和client

    http://activemq.apache.org/apollo/download.html  官方下载地址   MQTT目录: MQTT简单介绍 window安装MQTT服务器和client ja ...