fujuntang
2022-04-28 51ccd8155f029d66366d0df8e2baf886ffd50000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
 
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "proto_comm.h"
#include "proto_dbg.h"
 
void soap_perror(struct soap *soap, const char *str)
{
    if (NULL == str) {
        SOAP_DBGERR("[soap] error: %d, %s, %s\n", soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
    } else {
        SOAP_DBGERR("[soap] %s error: %d, %s, %s\n", str, soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
    }
    return;
}
 
void* proto_soap_malloc(struct soap *soap, unsigned int n)
{
    void *p = NULL;
 
    if (n > 0) {
        p = soap_malloc(soap, n);
        SOAP_ASSERT(NULL != p);
        memset(p, 0x00 ,n);
    }
    return p;
}
 
struct soap *proto_soap_new(int timeout)
{
    struct soap *soap = NULL;                                                   // soap»·¾³±äÁ¿
 
    SOAP_ASSERT(NULL != (soap = soap_new()));
 
    soap_set_namespaces(soap, namespaces);                                      // ÉèÖÃsoapµÄnamespaces
    soap->recv_timeout    = timeout;                                            
    soap->send_timeout    = timeout;
    soap->connect_timeout = timeout;
 
#if defined(__linux__) || defined(__linux)                                     
    soap->socket_flags = MSG_NOSIGNAL;                                          
#endif
 
    soap_set_mode(soap, SOAP_C_UTFSTRING);                                    
 
    return soap;
}
 
void proto_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_done(soap);                                                            // Reset, close communications, and remove callbacks
    soap_free(soap);                                                            // Reset and deallocate the context created with soap_new or soap_copy
}
 
int proto_SetAuthInfo(struct soap *soap, const char *username, const char *password)
{
    int result = 0;
 
    SOAP_ASSERT(NULL != username);
    SOAP_ASSERT(NULL != password);
 
    result = soap_wsse_add_UsernameTokenDigest(soap, NULL, username, password);
    SOAP_CHECK_ERROR(result, soap, "add_UsernameTokenDigest");
 
EXIT:
 
    return result;
}
 
float para_check(float data)
{
  float ret;
 
  if (data > PARA_MAX) {
    ret = PARA_MAX;
  } else if (data < PARA_MIN) {
    ret = PARA_MIN;
  } else {
    ret = data;
  }
 
  return ret;
}
 
/************************************************************************
  *³õʼ»¯soapÃèÊöÏûϢͷ
 
  *ÔÚ±¾º¯ÊýÄÚ²¿Í¨¹ýproto_soap_malloc·ÖÅäµÄÄڴ棬½«ÔÚproto_soap_deleteÖб»ÊÍ·Å
************************************************************************/
void proto_init_header(struct soap *soap)
{
    struct SOAP_ENV__Header *header = NULL;
 
    SOAP_ASSERT(NULL != soap);
 
    header = (struct SOAP_ENV__Header *)proto_soap_malloc(soap, sizeof(struct SOAP_ENV__Header));
    soap_default_SOAP_ENV__Header(soap, header);
    header->wsa__MessageID = (char*)soap_wsa_rand_uuid(soap);
    header->wsa__MessageID = (char *)soap_strdup(soap, soap_rand_uuid(soap, "urn:uuid:"));
    header->wsa__To        = (char*)proto_soap_malloc(soap, strlen(SOAP_TO) + 1);
    header->wsa__Action    = (char*)proto_soap_malloc(soap, strlen(SOAP_ACTION) + 1);
    strcpy(header->wsa__To, SOAP_TO);
    strcpy(header->wsa__Action, SOAP_ACTION);
    soap->header = header;
 
    return;
}
 
/************************************************************************
  *³õʼ»¯Ì½²âÉ豸µÄ·¶Î§ºÍÀàÐÍ
 
  *ÔÚ±¾º¯ÊýÄÚ²¿Í¨¹ýproto_soap_malloc·ÖÅäµÄÄڴ棬½«ÔÚproto_soap_deleteÖб»ÊÍ·Å
************************************************************************/
void proto_init_ProbeType(struct soap *soap, struct wsdd__ProbeType *probe)
{
    struct wsdd__ScopesType *scope = NULL;                                      // ÓÃÓÚÃèÊö²éÕÒÄÄÀàµÄWeb·þÎñ
 
    SOAP_ASSERT(NULL != soap);
    SOAP_ASSERT(NULL != probe);
 
    scope = (struct wsdd__ScopesType *)proto_soap_malloc(soap, sizeof(struct wsdd__ScopesType));
    soap_default_wsdd__ScopesType(soap, scope);                                 // ÉèÖÃѰÕÒÉ豸µÄ·¶Î§
    scope->__item = (char*)proto_soap_malloc(soap, strlen(SOAP_ITEM) + 1);
    strcpy(scope->__item, SOAP_ITEM);
 
    memset(probe, 0x00, sizeof(struct wsdd__ProbeType));
    soap_default_wsdd__ProbeType(soap, probe);
    probe->Scopes = scope;
    probe->Types  = (char*)proto_soap_malloc(soap, strlen(SOAP_TYPES) + 1);     // ÉèÖÃѰÕÒÉ豸µÄÀàÐÍ
    strcpy(probe->Types, SOAP_TYPES);
 
    return;
}
 
/************************************************************************
  *rtsp://100.100.100.140:554/av0_0
  *rtsp://username:password@100.100.100.140:554/av0_0
************************************************************************/
int make_uri_withauth(char *src_uri, char *username, char *password, char *dest_uri, unsigned int size_dest_uri)
{
    int result = 0;
    unsigned int needBufSize = 0;
 
    SOAP_ASSERT(NULL != src_uri);
    SOAP_ASSERT(NULL != username);
    SOAP_ASSERT(NULL != password);
    SOAP_ASSERT(NULL != dest_uri);
    memset(dest_uri, 0x00, size_dest_uri);
 
    needBufSize = strlen(src_uri) + strlen(username) + strlen(password) + 3;    // ¼ì²é»º´æÊÇ·ñ×ã¹»£¬°üÀ¨¡®:¡¯ºÍ¡®@¡¯ºÍ×Ö·û´®½áÊø·û
    if (size_dest_uri < needBufSize) {
        SOAP_DBGERR("dest uri buf size is not enough.\n");
        result = -1;
        goto EXIT;
    }
 
    if (0 == strlen(username) && 0 == strlen(password)) {                       // Éú³ÉеÄuriµØÖ·
        strcpy(dest_uri, src_uri);
    } else {
        char *p = strstr(src_uri, "//");
        if (NULL == p) {
            SOAP_DBGERR("can't found '//', src uri is: %s.\n", src_uri);
            result = -1;
            goto EXIT;
        }
        p += 2;
 
        memcpy(dest_uri, src_uri, p - src_uri);
        sprintf(dest_uri + strlen(dest_uri), "%s:%s@", username, password);
        strcat(dest_uri, p);
    }
 
EXIT:
 
    return result;
}