first commit
This commit is contained in:
342
GB28181Device/sip/sip_cm.cpp
Normal file
342
GB28181Device/sip/sip_cm.cpp
Normal file
@@ -0,0 +1,342 @@
|
||||
/***************************************************************************************
|
||||
*
|
||||
* IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
*
|
||||
* By downloading, copying, installing or using the software you agree to this license.
|
||||
* If you do not agree to this license, do not download, install,
|
||||
* copy or use the software.
|
||||
*
|
||||
* Copyright (C) 2014-2022, Happytimesoft Corporation, all rights reserved.
|
||||
*
|
||||
* Redistribution and use in binary forms, with or without modification, are permitted.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the License.
|
||||
*
|
||||
****************************************************************************************/
|
||||
|
||||
#include "sys_inc.h"
|
||||
#include "hsip.h"
|
||||
#include "sua.h"
|
||||
#include "sip_parse.h"
|
||||
#include "sip_rx.h"
|
||||
#include "sip_tx.h"
|
||||
#include "sip_cm.h"
|
||||
#include "sip_reg.h"
|
||||
#include "sip_pstate.h"
|
||||
|
||||
/***************************************************************************************/
|
||||
extern HSIP_CLASS hsip;
|
||||
extern HSIP_USER g_user;
|
||||
|
||||
/***************************************************************************************/
|
||||
void cm_set_local_ip(uint32 ip)
|
||||
{
|
||||
struct in_addr addr;
|
||||
addr.s_addr = hsip.local_ip = ip;
|
||||
strcpy(hsip.local_ipstr, inet_ntoa(addr));
|
||||
}
|
||||
|
||||
void cm_server_set(const char * sip, uint16 sport, const char * sid, const char * sdomain)
|
||||
{
|
||||
HSIP_USER * p_user = &g_user;
|
||||
|
||||
uint32 srv_addr = get_address_by_name(sip);
|
||||
|
||||
struct in_addr in;
|
||||
in.s_addr = srv_addr;
|
||||
|
||||
strcpy(p_user->user_domain, sdomain);
|
||||
|
||||
strcpy(p_user->server_ipstr, inet_ntoa(in));
|
||||
p_user->server_port = sport;
|
||||
|
||||
strcpy(p_user->server_id, sid);
|
||||
sprintf(p_user->server_sip_addr, "sip:%s@%s", sid, sdomain);
|
||||
}
|
||||
|
||||
void cm_acct_set(const char * acct, const char * pwd, int is_tcp)
|
||||
{
|
||||
HSIP_USER * p_user = &g_user;
|
||||
|
||||
strcpy(p_user->user_name, acct);
|
||||
strcpy(p_user->user_auth_info.auth_name, acct);
|
||||
strcpy(p_user->user_auth_passwd, pwd);
|
||||
|
||||
p_user->usrf_tcp_sip = is_tcp;
|
||||
p_user->usrf_tcp_rtp = 0;
|
||||
|
||||
p_user->user_disp_name[0] = '\0';
|
||||
strcpy(p_user->user_agent_desc, hsip.ver);
|
||||
|
||||
sprintf(p_user->user_sip_addr, "sip:%s@%s", acct, p_user->user_domain);
|
||||
|
||||
strcpy(p_user->user_ip, hsip.local_ipstr);
|
||||
strcpy(p_user->user_real_ip, hsip.local_ipstr);
|
||||
|
||||
p_user->expires_time = hsip.user_expires_sec;
|
||||
p_user->user_crpty_mode = HCRPTY_NULL;
|
||||
|
||||
log_print(HT_LOG_INFO, "%s\r\n", p_user->user_sip_addr);
|
||||
}
|
||||
|
||||
void cm_name_set(char * p_disp_name)
|
||||
{
|
||||
HSIP_USER * p_user = &g_user;
|
||||
|
||||
if (p_disp_name && p_disp_name[0])
|
||||
{
|
||||
strncpy(p_user->user_disp_name, p_disp_name, sizeof(p_user->user_disp_name)-1);
|
||||
}
|
||||
}
|
||||
|
||||
BOOL cm_name_get(char * p_disp_name, int max_len)
|
||||
{
|
||||
HSIP_USER * p_user = &g_user;
|
||||
|
||||
if (p_disp_name)
|
||||
{
|
||||
strncpy(p_disp_name, p_user->user_disp_name, max_len-1);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
BOOL cm_start_reg()
|
||||
{
|
||||
HSIP_USER * p_user = &g_user;
|
||||
|
||||
if (!sip_user_socket_init(p_user))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
log_print(HT_LOG_INFO, "%s, sip_user_socket_init finish\r\n", __FUNCTION__);
|
||||
|
||||
HSIP_MSG * tx_msg = sip_build_register_normal_msg(p_user, inet_addr(p_user->server_ipstr), htons(p_user->server_port));
|
||||
if (NULL == tx_msg)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
log_print(HT_LOG_INFO, "%s, sip_build_register_normal_msg finish\r\n", __FUNCTION__);
|
||||
|
||||
user_tx_free_msg(p_user, tx_msg);
|
||||
|
||||
log_print(HT_LOG_INFO, "%s, user_tx_free_msg finish\r\n", __FUNCTION__);
|
||||
|
||||
// 已经发起认证过程
|
||||
p_user->user_state = HSIP_AUTH_INIT;
|
||||
|
||||
hsip.sys_init_flag = 2;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/***************************************************************************************
|
||||
外部普通接口函数
|
||||
***************************************************************************************/
|
||||
|
||||
char * cm_get_self_acct(uint32 index)
|
||||
{
|
||||
HSIP_USER * p_user = &g_user;
|
||||
|
||||
if (index == 0)
|
||||
{
|
||||
return p_user->user_name;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
char * cm_get_req_acct(void * sua)
|
||||
{
|
||||
SUA * p_sua = (SUA *)sua;
|
||||
if (p_sua == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return p_sua->req_name;
|
||||
}
|
||||
|
||||
char * cm_get_remote_acct(void * sua)
|
||||
{
|
||||
SUA * p_sua = (SUA *)sua;
|
||||
if (p_sua == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return p_sua->remote_name;
|
||||
}
|
||||
|
||||
BOOL cm_set_remote_acct(void * sua, const char * p_tel)
|
||||
{
|
||||
SUA * p_sua = (SUA *)sua;
|
||||
if (p_sua == NULL)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (pps_safe_node(hsip.sua_fl, p_sua) == FALSE)
|
||||
{
|
||||
log_print(HT_LOG_ERR, "%s, param p_sua[%p] err!!!\r\n", __FUNCTION__, p_sua);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
const char * ptr = strchr(p_tel, '@');
|
||||
if (ptr)
|
||||
{
|
||||
const char * user_name = ptr;
|
||||
int name_len = ptr - p_tel;
|
||||
|
||||
const char * user_domain = ptr + 1;
|
||||
|
||||
memcpy(p_sua->remote_name, user_name, name_len);
|
||||
p_sua->remote_name[name_len] = '\0';
|
||||
strncpy(p_sua->remote_domain, user_domain, sizeof(p_sua->remote_domain) - 1);
|
||||
snprintf(p_sua->remote_sip_address, sizeof(p_sua->remote_sip_address)-1, "sip:%s", p_tel);
|
||||
}
|
||||
else
|
||||
{
|
||||
strncpy(p_sua->remote_name, p_tel, sizeof(p_sua->remote_name) - 1);
|
||||
snprintf(p_sua->remote_sip_address, sizeof(p_sua->remote_sip_address)-1, "sip:%s@%s", p_sua->remote_name, p_sua->remote_domain);
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void cm_set_ntf_func(SIPNTFC p_func, void * p_userdata)
|
||||
{
|
||||
sys_os_mutex_enter(hsip.mutex_cb);
|
||||
hsip.ext_ntf_func = p_func;
|
||||
hsip.user_data = p_userdata;
|
||||
sys_os_mutex_leave(hsip.mutex_cb);
|
||||
}
|
||||
|
||||
char * cm_get_sname(void * sua)
|
||||
{
|
||||
SUA * p_sua = (SUA *)sua;
|
||||
if (p_sua == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (pps_safe_node(hsip.sua_fl, p_sua) == FALSE)
|
||||
{
|
||||
log_print(HT_LOG_ERR, "%s, param sua[%p] err!!!\r\n", __FUNCTION__, p_sua);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return p_sua->s_name;
|
||||
}
|
||||
|
||||
BOOL cm_set_sname(void * sua, const char * p_sname)
|
||||
{
|
||||
SUA * p_sua = (SUA *)sua;
|
||||
if (p_sua == NULL)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
strncpy(p_sua->s_name, p_sname, sizeof(p_sua->s_name)-1);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int cm_get_call_state(void * sua)
|
||||
{
|
||||
SUA * p_sua = (SUA *)sua;
|
||||
if (p_sua == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (p_sua->call_state == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else if (p_sua->call_state < SUA_CS_ESTABLISHED)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if (p_sua->call_state == SUA_CS_ESTABLISHED)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
else if (p_sua->call_state > SUA_CS_ESTABLISHED)
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
|
||||
BOOL cm_sip_cmd(void * sua, PUACMD ua_cmd)
|
||||
{
|
||||
SUA * p_sua = (SUA *)sua;
|
||||
if (p_sua == NULL)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return sip_isw_cmd(ua_cmd, p_sua);
|
||||
}
|
||||
|
||||
|
||||
void cm_clear_sua_cap(void * sua, const char * cap_name)
|
||||
{
|
||||
SUA * p_sua = (SUA *)sua;
|
||||
if (p_sua == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (pps_safe_node(hsip.sua_fl, p_sua) == FALSE)
|
||||
{
|
||||
log_print(HT_LOG_ERR, "%s, param p_sua[%p] err!!!\r\n", __FUNCTION__, p_sua);
|
||||
return;
|
||||
}
|
||||
|
||||
sua_clear_self_sdp(p_sua, cap_name);
|
||||
}
|
||||
|
||||
int cm_add_sua_cap(void * sua, const char * cap_type, int pt, const char * encoder, int hz, int chn, int direction)
|
||||
{
|
||||
SUA * p_sua = (SUA *)sua;
|
||||
if (p_sua == NULL || pt < 0 || pt >= 128)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (pps_safe_node(hsip.sua_fl, p_sua) == FALSE)
|
||||
{
|
||||
log_print(HT_LOG_ERR, "%s, param p_sua[%p] err!!!\r\n", __FUNCTION__, p_sua);
|
||||
return -1;
|
||||
}
|
||||
|
||||
SDPINFO sdp;
|
||||
memset(&sdp, 0, sizeof(SDPINFO));
|
||||
|
||||
sdp.mtype = (strcasecmp(cap_type, "audio") == 0) ? SDP_MEDIA_AUDIO : SDP_MEDIA_VIDEO;
|
||||
strncpy(sdp.encoder, encoder, sizeof(sdp.encoder));
|
||||
sdp.hz = hz;
|
||||
sdp.chn = chn;
|
||||
sdp.pt = pt;
|
||||
sdp.sendrecv = direction;
|
||||
|
||||
return sua_add_self_sdp(p_sua, &sdp);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user