#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;
|
}
|
|
|