FuJuntang
2022-07-06 e5cff5a3ef373a5090f45cd1dfb0b85d9c851d5d
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <signal.h>
 
#include "proto_comm.h"
#include "proto_dbg.h"
 
int timer_init(timer_t *timer_index, void (*timer_handler)(union sigval para)) {
  struct sigevent event;
  timer_t timer;
  int ret; 
 
  memset(&event, 0x00, sizeof(event));
  event.sigev_value.sival_int = ENABLE;
  event.sigev_value.sival_ptr = NULL;
  event.sigev_notify = SIGEV_THREAD;
  event.sigev_notify_function = timer_handler;
  
  ret = timer_create(CLOCK_REALTIME, &event, &timer);
  if (ret) {
    return -1;
  }
 
  if (timer_index != NULL) {
    *timer_index = timer;
  }
 
  return 0;
}
 
int timer_start(timer_t timer_index, int sec) {
  int ret; 
  struct itimerspec ts;
 
  ts.it_interval.tv_sec = 0; 
  ts.it_interval.tv_nsec = 0; 
  ts.it_value.tv_sec = sec; 
  ts.it_value.tv_nsec = 0; 
  ret = timer_settime(timer_index, 0, &ts, NULL);
  if (ret) {
    return -1;
  }
 
  return ret; 
}
 
int timer_stop(timer_t timer_index) {
 
  int ret = timer_start(timer_index, 0);
 
  return ret;
}
 
int timer_destroy(timer_t timer_index) {
  int ret;
 
  ret = timer_delete(timer_index);
 
  return ret;
}
 
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_ASSERT(NULL != (soap = soap_new()));
 
  soap_set_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;
}
 
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;
}
 
void proto_init_ProbeType(struct soap *soap, struct wsdd__ProbeType *probe)
{
    struct wsdd__ScopesType *scope = NULL;
 
    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 bridge_uri(char *src_uri, const char *username, const 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;    //check buf size with character ¡®:¡¯ and ¡®@'
    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)) {
        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;
}