linux设备上的Onvif 实现3 :gSOAP嵌入式linux下的移植与程序开发

2019-07-12 16:59发布

一、移植

       1、下载源码  http://sourceforge.net/projects/gsoap2/files/      本例下载最新版本为:gsoap_2.8.33.zip  2、解压源码     $ unzip gsoap_2.8.33.zip
      3、平台准备unbuntu1204
          $sudo apt-get install build-essential      $sudo apt-get install libgtk2.0-dev libglib2.0-dev      $sudo apt-get install checkinstall
          $sudo apt-get install flex bison
          $sudo apt-get install openssl
          $sudo apt-cache search libssl | grep SSL
          $sudo apt-get install libssl-dev
  4、编译x86 平台下工具  $ ./configure --prefix=/usr/local/gsopa-x86  $ make  $ make install
      $ export PATH=/usr/local/gSOAP-x86/bin:$PATH  5、编译arm平台工具  ./configure --prefix=/usr/local/gsopa-arm --host=arm-linux  再次配置源码,这次配置目标平台变为arm-linux  修改源码,根据编译错误提示进行适当修改:  修改源码目录下的config.h  注释掉:  #define malloc rpl_malloc  soapcpp和wsdl2h在编译过程中会生成同事这两个工具也在编译时会被用来生成一些特定的文件,所以如果soapcpp和wsdl2h如果被编译成arm架构则无法在PC上执行,也就意味着接下的编译无法完成,所以这里选择不编译这两个文件,而选择使用上节编译所生产的x86架构的对应文件。  修改gsoap/src/Makefile  注释掉  soapcpp2$(EXEEXT): $(soapcpp2_OBJECTS) $(soapcpp2_DEPENDENCIES) $(EXTRA_soapcpp2_DEPENDENCIES)  @rm -f soapcpp2$(EXEEXT)  $(AM_V_CCLD)$(soapcpp2_LINK) $(soapcpp2_OBJECTS) $(soapcpp2_LDADD) $(LIBS)  修改gsoap/wsdl/Makefile  wsdl2h$(EXEEXT): $(wsdl2h_OBJECTS) $(wsdl2h_DEPENDENCIES) $(EXTRA_wsdl2h_DEPENDENCIES)  @rm -f wsdl2h$(EXEEXT)  $(AM_V_CXXLD)$(wsdl2h_LINK) $(wsdl2h_OBJECTS) $(wsdl2h_LDADD) $(LIBS)  拷贝x86架构下的soapcpp和wsdl2h到工程源码目录下:  $ cp /home/linux/gsoap/gsoap-x86/bin/soapcpp2 /home/linux/gsoap/gsoap-2.8/gsoap/src/  $ cp /home/linux/gsoap/gsoap-x86/bin/wsdl2h /home/linux/gsoap/gsoap-2.8/gsoap/wsdl/  编译  $ make  安装  $ make install
二 程序
    1、代码
     add.h点击(此处)折叠或打开
  1. //gsoapopt cw

  2. //gsoap ns2 schema namespace: urn:add

  3. //gsoap ns2 schemaform: unqualified

  4. //gsoap ns2 service name: add

  5. //gsoap ns2 service type: addPortType

  6. //gsoap ns2 service port:http://websrv.cs.fsu.edu/~engelen/addserver.cgi

  7. //gsoap ns2 service namespace: urn:add

  8. //gsoap ns2 service transport: http://schemas.xmlsoap.org/soap/http

  9. //gsoap ns2 service method-style: add rpc

  10. //gsoap ns2 service method-encoding: add http://schemas.xmlsoap.org/soap/encoding/

  11. //gsoap ns2 service method-action: add""

  12. int ns2__add(int num1, int num2, int* sum);
addserver.c

点击(此处)折叠或打开
  1. #include "soapH.h"
  2. #include "add.nsmap"

  3. int main(int args,char**argv)
  4. {
  5.  int m,s;
  6.  struct soap add_soap;
  7.  soap_init(&add_soap);
  8.  soap_set_namespaces(&add_soap,namespaces);
  9.  if(args<2)
  10.  {
  11.     printf("usage:%s ",argv[0]);
  12.     exit(1);
  13.  }
  14.  else
  15.  {
  16.      m = soap_bind(&add_soap,NULL,atoi(argv[1]),100);
  17.      if(m<0)
  18.      {
  19.      soap_print_fault(&add_soap,stderr);
  20.      exit(-1);
  21.      }
  22.      fprintf(stderr,"Socket connection successful:master socket=%d ",m);
  23.      for(;;)
  24.      {
  25.             s = soap_accept(&add_soap);
  26.             if(s<0)
  27.             {
  28.                 soap_print_fault(&add_soap,stderr);
  29.                 exit(-1);
  30.             }
  31.             fprintf(stderr,"socket connecntion successful:slave socket = %d ",s);
  32.             soap_serve(&add_soap);
  33.             soap_end(&add_soap);
  34.      }
  35.   }
  36.   return 0;
  37. }

  38. int ns2__add(struct soap*add_soap,int num1,int num2,int*sum)
  39. {
  40.     *sum = num1+num2;
  41.     return 0;
  42. }
addclient.c

点击(此处)折叠或打开
  1. #include "soapStub.h"
    #include "add.nsmap"

    int add(const char *server,int num1,int num2,int *sum)
    {
        struct soap add_soap;
        int result = 0;
        soap_init(&add_soap);
        soap_set_namespaces(&add_soap,namespaces);
        soap_call_ns2__add(&add_soap,server,NULL,num1,num2,sum);
        printf("server is %s,num1 is %d,num2 is %d ",server,num1,num2);
        
        if(add_soap.error)
        {
            printf("soap error: %d, %s ,%s ",add_soap.error,
            *soap_faultcode(&add_soap),*soap_faultstring(&add_soap));
            result = add_soap.error;
        }    
        soap_end(&add_soap);
        soap_done(&add_soap);
        return result;
    }

addtest.c

点击(此处)折叠或打开
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. int add(const char*server,int num1,int num2,int*sum);

  5. int main(int argc,char**argv)
  6. {
  7.     int resutl = -1;
  8.     char server[128]={0};
  9.     int num1;
  10.     int num2;
  11.     int sum;
  12.     if(argc<4)
  13.     {
  14.         printf("usage:%s num1 num2 ",argv[0]);
  15.         exit(1);
  16.     }
  17.     strcpy(server,argv[1]);
  18.     num1 = atoi(argv[2]);
  19.     num2 = atoi(argv[3]);
  20.     resutl = add(server,num1,num2,&sum);
  21.     if(resutl!=0)
  22.     {
  23.         printf("soap error,errorcode=%d ",resutl);
  24.     }
  25.     else
  26.     {
  27.         printf("%d+%d=%d ",num1,num2,sum);
  28.     }
  29.     return 0;
  30.     
  31. }
//x86 linux Makefile

点击(此处)折叠或打开
  1. GSOAP_ROOT=/usr/local/gSOAP-x86
  2. WSNAME=add
  3. CC= g++ -g -DWITH_NONAMESPACES
  4. INCLUDE=-I $(GSOAP_ROOT)/include
  5. SERVER_OBJS = soapC.o stdsoap2.o soapServer.o addserver.o
  6. CLIENT_OBJS = soapC.o stdsoap2.o soapClient.o addclient.o addtest.o

  7. all:server
  8. server: $(SERVER_OBJS)
  9. $(CC) $(INCLUDE) -o addserver $(SERVER_OBJS)

  10. client: $(CLIENT_OBJS)
  11. $(CC) $(INCLUDE) -o addtest $(CLIENT_OBJS)
  12. cl:
  13. rm -f *.o *.xml *.a *.wsdl *.nsmap soapH.h $(WSNAME)Stub.* $(WSNAME)server ns.xsd $(WSNAME)test
//arm-linux Makefile

点击(此处)折叠或打开
  1. GSOAP_ROOT=/usr/local/gSOAP-arm
  2. WSNAME=add
  3. CC= arm-linux-g++ -g -DWITH_NONAMESPACES
  4. INCLUDE=-I $(GSOAP_ROOT)/include
  5. SERVER_OBJS = soapC.o stdsoap2.o soapServer.o addserver.o
  6. CLIENT_OBJS = soapC.o stdsoap2.o soapClient.o addclient.o addtest.o

  7. all:server
  8. server: $(SERVER_OBJS)
  9. $(CC) $(INCLUDE) -o addserver $(SERVER_OBJS)

  10. client: $(CLIENT_OBJS)
  11. $(CC) $(INCLUDE) -o addtest $(CLIENT_OBJS)
  12. cl:
  13. rm -f *.o *.xml *.a *.wsdl *.nsmap soapH.h $(WSNAME)Stub.* $(WSNAME)server ns.xsd $(WSNAME)test
  2、编译
   先
   $soapcpp2 -c add.h 产生soap文件
   $make client 得到客户端程序
   $make all 得到服务端程序
(可分别编译X86 与 ARM板程序,仅仅是执行位置不一样,编译后程序不依赖任何库,可在任意目录执行)
  3、执行
  服务器端:$./addserver 8000 该服务端口绑定为8000端口
成功:Socket connection successful:master socket=3
客服端连接成功:socket connecntion successful:slave socket = 4

  客服端:$./addclient 192.168.7.7 8000  1 2
               server is 192.168.7.7:8000,num1 is 1,num2 is 2 1+2=3
   这里服务器IP地址,加端口号,加 SOAP参数
三、WSDL天气获取
1、新建目录
$weather
2、从服务器下载头文件
$wsdl2h -c -o  weather.h http://www.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
//可ping是否通
3、生成客户代码
$soapcpp2 -x -C -c weather.h
-x不生成xml文件
-C 仅仅生存客服端代码
4.拷贝gsoap目录下的stdsoap2.h stdsoap2.c 到weather目录下
5、编写客户端程序
// 数据结构参考了weather.h和http://www.webxml.com.cn/files/WeatherWsHelp.pdf
点击(此处)折叠或打开
  1. #include <stdio.h>
  2. #include "soapH.h"
  3. #include <string.h>
  4. #include "WeatherWSSoap.nsmap"

  5. //author: jon/2016/08/01

  6. #define CITY_NAME "深圳"

  7. int main(int argc,char*argv[])
  8. {
  9.     struct soap soap;
  10.     int ret = 0;
  11.     int element= 0;
  12.     int i=0;
  13.     struct _ns1__getWeather myCitWeather;
  14.     myCitWeather.theCityCode = CITY_NAME;
  15.     //myCitWeather.theUserID= 0;
  16.     
  17.     struct _ns1__getWeatherResponse myRetnWeather;
  18.     
  19.     soap_init(&soap);
  20.     soap_set_mode(&soap,SOAP_C_UTFSTRING);
  21.     soap.mode |=SOAP_C_UTFSTRING;
  22.     
  23.     if(soap_call___ns1__getWeather(&soap,NULL,NULL,&myCitWeather,&myRetnWeather)==SOAP_OK)
  24.     {
  25.          int Size = myRetnWeather.getWeatherResult->__sizestring;
  26.          for(i=0;i{
  27.             printf("is %d get %s ",i,myRetnWeather.getWeatherResult->string[i]);
  28.          }
  29.     }
  30.     soap_destroy(&soap);
  31.     soap_end(&soap);
  32.     soap_done(&soap);
  33.     return 0;
  34. }


6.makefile
#CROSS_COMPILER=

CC:=$(CROSS_COMPILER)gcc
GCC:=$(CROSS_COMPILER)g++

all:
    $(CC) -o weather weather.c soapC.c soapClient.c stdsoap2.c

clean:
    rm -f weather


7.执行
$make

$./weather
is 0   get 直辖市 北京
is 1   get 北京
is 2   get 792
is 3   get 2016/08/01 10:31:54
is 4   get 今日天气实况:气温:28℃;风向/风力:北风 2级;湿度:73%
is 5   get 紫外线强度:弱。空气质量:较差。
is 6   get 紫外线指数:弱,辐射较弱,涂擦SPF12-15、PA+护肤品。
感冒指数:少发,感冒机率较低,避免长期处于空调屋中。
穿衣指数:热,适合穿T恤、短薄外套等夏季服装。
洗车指数:较适宜,无雨且风力较小,易保持清洁度。
运动指数:较适宜,较适宜进行各种户内外运动。
空气污染指数:较差,气象条件较不利于空气污染物扩散。。

is 7   get 8月1日 阴
is 8   get 24℃/31℃
is 9   get 无持续风向微风
is 10   get 2.gif
is 11   get 2.gif
is 12   get 8月2日 晴转多云
is 13   get 24℃/33℃
is 14   get 无持续风向微风
is 15   get 0.gif
is 16   get 1.gif
is 17   get 8月3日 晴转多云
is 18   get 25℃/33℃
is 19   get 无持续风向微风
is 20   get 0.gif
is 21   get 1.gif
is 22   get 8月4日 多云转晴
is 23   get 24℃/31℃
is 24   get 无持续风向微风
is 25   get 1.gif
is 26   get 0.gif
is 27   get 8月5日 晴转多云
is 28   get 25℃/31℃
is 29   get 无持续风向微风
is 30   get 0.gif
is 31   get 1.gif
欢迎关注并加入物联网行业联盟,积累行业人脉和资源。