SipphoneClient.cpp
6.55 KB
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
230
231
232
233
234
235
236
237
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* CC/LICENSE
*
* 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 "CCBar.h"
#include "SipphoneClient.h"
const int LOCAL_SIP_PORT = 6060;
const int LOG_LEVEL = 3;
const bool LOG_APPEND = true;
void call_state_callback(int account_id, int call_id, const char *srcnum, const char *dstnum, PHONELIB_CALL_STATE cstate, int sip_response_code, const char *sip_response_desc)
{
assert(Tool::p_Bar != NULL);
CCCBar *p_Bar = static_cast<CCCBar *>(Tool::p_Bar);
p_Bar->GetSipphoneClient().process_call_state(account_id, call_id, srcnum, dstnum, cstate, sip_response_code, sip_response_desc);
}
void reg_state_callback(int account_id, PHONELIB_REG_STATE state, int code, const char *statusText)
{
assert(Tool::p_Bar != NULL);
CCCBar *p_Bar = static_cast<CCCBar *>(Tool::p_Bar);
p_Bar->GetSipphoneClient().process_reg_state(account_id, state, code, statusText);
}
SipphoneClient::SipphoneClient(void) : m_account_id(-1), m_call_id(-1), m_is_initial(false), m_is_register(false), m_is_talking(false)
{
m_Logger.Initialize(50000, 50, "C:\\Log\\CCAgentBar\\", "SIP", phonelib_get_version_string());
m_Logger.UnInitialize();
}
SipphoneClient::~SipphoneClient(void)
{
}
void SipphoneClient::process_reg_state(int account_id, PHONELIB_REG_STATE state, int sip_response_code, const char *sip_response_desc)
{
assert(Tool::p_Bar != NULL);
Tool::m_Logger.WriteLog(Level_High, "[SipphoneClient-process_reg_state] account_id:%d state:%d sip_response_code:%d sip_response_desc:%s",
account_id, state, sip_response_code, sip_response_desc);
}
void SipphoneClient::process_call_state(int account_id, int call_id, const char *srcnum, const char *dstnum, PHONELIB_CALL_STATE state, int sip_response_code, const char *sip_response_desc)
{
assert(Tool::p_Bar != NULL);
Tool::m_Logger.WriteLog(Level_High, "[SipphoneClient-process_call_state] account_id:%d call_id:%d srcnum:%s dstnum:%s state:%d sip_response_code:%d sip_response_desc:%s",
account_id, call_id, srcnum, dstnum, state, sip_response_code, sip_response_desc);
switch(state)
{
case PHONELIB_CALL_STATE_TERMINATED:
{
m_is_talking = false;
break;
}
case PHONELIB_CALL_STATE_INCOMING:
{
if(m_is_talking)
{
phonelib_drop_call(call_id);
}
else
{
m_is_talking = true;
m_call_id = call_id;
}
break;
}
default:
break;
}
}
int SipphoneClient::SipInitial()
{
LONG ret = AGENTBARERROR_SUCCESS;
Tool::m_Logger.WriteLog(Level_High, "[SipphoneClient-SipInitial] void");
if(!m_is_initial)
{
ret = phonelib_init(LOCAL_SIP_PORT, m_Logger.GetFullFileName().c_str(), LOG_LEVEL, LOG_APPEND);
if(ret == AGENTBARERROR_SUCCESS)
{
m_is_initial = true;
phonelib_set_call_state_callback(call_state_callback);
phonelib_set_reg_state_callback(reg_state_callback);
}
else
{
ret = AGENTBARERROR_SOFTPHONE_ERROR;
}
Tool::m_Logger.WriteLog("SipphoneClient", "SipInitial", "phonelib_init", ret);
}
return ret;
}
int SipphoneClient::SipUnInitial()
{
LONG ret = AGENTBARERROR_SUCCESS;
Tool::m_Logger.WriteLog(Level_High, "[SipphoneClient-SipUnInitial] void");
if(m_is_initial)
{
ret = phonelib_destroy();
if(ret == AGENTBARERROR_SUCCESS)
{
m_is_register = false; // 在phonelib_destroy()里,会自动将sip账号给remove掉,因此将此变量设置成false, 则不会再继续调用addaccount
m_is_initial = false;
}
else
{
ret = AGENTBARERROR_SOFTPHONE_ERROR;
}
Tool::m_Logger.WriteLog("SipphoneClient", "SipUnInitial", "phonelib_destroy", ret);
}
return ret;
}
int SipphoneClient::SipRegister(const char *server, int server_port, const char *sip_account, const char *sip_password, int timeout)
{
LONG ret = AGENTBARERROR_SUCCESS;
Tool::m_Logger.WriteLog(Level_High, "[SipphoneClient-SipRegister] server:%s server_port:%d sip_account:%s sip_password:%s timeout:%d", server, server_port, sip_account, sip_password, timeout);
if(m_is_register)
{
ret = phonelib_remove_account(m_account_id);
if(ret == AGENTBARERROR_SUCCESS)
{
m_is_register = false;
}
else
{
ret = AGENTBARERROR_SOFTPHONE_ERROR;
}
Tool::m_Logger.WriteLog("SipphoneClient", "SipRegister", "phonelib_remove_account", ret);
}
if(!m_is_register)
{
ret = phonelib_add_account(server, server_port, sip_account, sip_password, timeout, &m_account_id);
if(ret == AGENTBARERROR_SUCCESS)
{
m_is_register = true;
}
else
{
ret = AGENTBARERROR_SOFTPHONE_ERROR;
}
Tool::m_Logger.WriteLog("SipphoneClient", "SipRegister", "phonelib_add_account", ret);
Tool::m_Logger.WriteLog(Level_High, "[SipphoneClient-SipRegister] [out] m_account_id:%d", m_account_id);
}
return ret;
}
int SipphoneClient::SipUnRegister()
{
LONG ret = AGENTBARERROR_SUCCESS;
Tool::m_Logger.WriteLog(Level_High, "[SipphoneClient-SipUnRegister] void");
if(m_is_register)
{
ret = phonelib_remove_account(m_account_id);
if(ret == AGENTBARERROR_SUCCESS)
{
m_is_register = false;
}
else
{
ret = AGENTBARERROR_SOFTPHONE_ERROR;
}
Tool::m_Logger.WriteLog("SipphoneClient", "SipUnRegister", "phonelib_remove_account", ret);
}
return ret;
}
int SipphoneClient::SipMakecall(const char *dest)
{
return AGENTBARERROR_SUCCESS;
}
int SipphoneClient::SipAnswercall()
{
LONG ret = AGENTBARERROR_SUCCESS;
Tool::m_Logger.WriteLog(Level_High, "[SipphoneClient-SipAnswercall] void");
if(m_is_talking)
{
ret = phonelib_answer_call(m_call_id);
if(ret != AGENTBARERROR_SUCCESS)
{
ret = AGENTBARERROR_SOFTPHONE_ERROR;
}
Tool::m_Logger.WriteLog("SipphoneClient", "SipAnswercall", "phonelib_answer_call", ret);
}
return ret;
}
int SipphoneClient::SipHangupcall()
{
LONG ret = AGENTBARERROR_SUCCESS;
Tool::m_Logger.WriteLog(Level_High, "[SipphoneClient-SipHangupcall] void");
if(m_is_talking)
{
ret = phonelib_drop_call(m_call_id);
if(ret != AGENTBARERROR_SUCCESS)
{
ret = AGENTBARERROR_SOFTPHONE_ERROR;
}
Tool::m_Logger.WriteLog("SipphoneClient", "SipHangupcall", "phonelib_drop_call", ret);
}
return ret;
}
int SipphoneClient::SipHoldcall()
{
return AGENTBARERROR_SUCCESS;
}
int SipphoneClient::SipUnholdcall()
{
return AGENTBARERROR_SUCCESS;
}
int SipphoneClient::SipSenddtmf(const char *dtmf)
{
return AGENTBARERROR_SUCCESS;
}