Commit 6c7542b2 authored by Guillaume Roguez's avatar Guillaume Roguez

utils: better sip error msg handling

This patch changes sip_utils API as follow:

- sip_strerror() outputs a std::string representing
  the given PJSIP error code.
- sip_printerror() acts like the old sip_strerror() version.

Refs #77903

Change-Id: Icc5b84a088f5533e6e64fd30bd95d3f382af86b8
parent b264aa7b
......@@ -185,11 +185,9 @@ IceTransport::onComplete(pj_ice_strans* ice_st, pj_ice_strans_op op,
const bool done = status == PJ_SUCCESS;
RING_DBG("ICE %s with %s", opname, done?"success":"error");
if (!done) {
char errmsg[PJ_ERR_MSG_SIZE];
pj_strerror(status, errmsg, sizeof(errmsg));
RING_ERR("ICE %s failed: %s", opname, errmsg);
}
if (!done)
RING_ERR("ICE %s failed: %s", opname,
sip_utils::sip_strerror(status).c_str());
{
std::lock_guard<std::mutex> lk(iceMutex_);
......@@ -254,8 +252,7 @@ IceTransport::setInitiatorSession()
if (isInitialized()) {
auto status = pj_ice_strans_change_role(icest_.get(), PJ_ICE_SESS_ROLE_CONTROLLING);
if (status != PJ_SUCCESS) {
RING_ERR("ICE role change failed");
sip_utils::sip_strerror(status);
RING_ERR("ICE role change failed: %s", sip_utils::sip_strerror(status).c_str());
return false;
}
return true;
......@@ -271,8 +268,7 @@ IceTransport::setSlaveSession()
if (isInitialized()) {
auto status = pj_ice_strans_change_role(icest_.get(), PJ_ICE_SESS_ROLE_CONTROLLED);
if (status != PJ_SUCCESS) {
RING_ERR("ICE role change failed");
sip_utils::sip_strerror(status);
RING_ERR("ICE role change failed: %s", sip_utils::sip_strerror(status).c_str());
return false;
}
return true;
......@@ -311,8 +307,7 @@ IceTransport::start(const Attribute& rem_attrs,
rem_candidates.size(),
rem_candidates.data());
if (status != PJ_SUCCESS) {
RING_ERR("ICE start failed");
sip_utils::sip_strerror(status);
RING_ERR("ICE start failed: %s", sip_utils::sip_strerror(status).c_str());
return false;
}
return true;
......@@ -381,8 +376,7 @@ IceTransport::stop()
auto status = pj_ice_strans_stop_ice(icest_.get());
if (status != PJ_SUCCESS) {
RING_ERR("ICE start failed");
sip_utils::sip_strerror(status);
RING_ERR("ICE start failed: %s", sip_utils::sip_strerror(status).c_str());
return false;
}
......@@ -523,12 +517,12 @@ IceTransport::addCandidate(int comp_id, const IpAddr& localAddr,
NULL);
if (ret != PJ_SUCCESS) {
RING_ERR("fail to add candidate for comp_id=%d : %s : %s", comp_id
RING_ERR("failed to add candidate for comp_id=%d : %s : %s", comp_id
, localAddr.toString().c_str()
, publicAddr.toString().c_str());
sip_utils::sip_strerror(ret);
sip_utils::sip_printerror(ret);
} else {
RING_DBG("success to add candidate for comp_id=%d : %s : %s", comp_id
RING_DBG("succeed to add candidate for comp_id=%d : %s : %s", comp_id
, localAddr.toString().c_str()
, publicAddr.toString().c_str());
}
......@@ -708,8 +702,7 @@ IceTransport::send(int comp_id, const unsigned char* buf, size_t len)
}
auto status = pj_ice_strans_sendto(icest_.get(), comp_id+1, buf, len, remote.pjPtr(), remote.getLength());
if (status != PJ_SUCCESS) {
sip_utils::sip_strerror(status);
RING_ERR("send failed");
RING_ERR("ice send failed: %s", sip_utils::sip_strerror(status).c_str());
return -1;
}
return len;
......@@ -846,11 +839,8 @@ handleIOEvents(pj_ice_strans_cfg& cfg, unsigned max_msec)
// error
if (n_events < 0) {
const auto err = pj_get_os_error();
char err_msg[128];
pj_strerror(err, err_msg, sizeof(err_msg));
err_msg[sizeof(err_msg)-1] = '\0';
// Kept as debug as some errors are "normal" in regular context
RING_DBG("IceIOQueue: error %d - %s", err, err_msg);
RING_DBG("IceIOQueue: error %d - %s", err, sip_utils::sip_strerror(err).c_str());
std::this_thread::sleep_for(std::chrono::milliseconds(PJ_TIME_VAL_MSEC(timeout)));
return;
}
......
......@@ -65,8 +65,8 @@ ip_utils::getAddrList(const std::string &name, pj_uint16_t family)
pj_cstr(&pjname, name.c_str());
auto status = pj_getaddrinfo(family, &pjname, &addr_num, res);
if (status != PJ_SUCCESS) {
RING_ERR("Error resolving %s :", name.c_str());
sip_utils::sip_strerror(status);
RING_ERR("Error resolving %s : %s", name.c_str(),
sip_utils::sip_strerror(status).c_str());
return ipList;
}
......
......@@ -204,13 +204,20 @@ addContactHeader(const pj_str_t *contact_str, pjsip_tx_data *tdata)
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*) contact);
}
void
sip_strerror(pj_status_t code)
sip_printerror(pj_status_t code)
{
char err_msg[PJ_ERR_MSG_SIZE];
pj_strerror(code, err_msg, sizeof err_msg);
RING_ERR("%d: %s", code, err_msg);
}
std::string
sip_strerror(pj_status_t code)
{
char err_msg[PJ_ERR_MSG_SIZE];
pj_strerror(code, err_msg, sizeof err_msg);
return std::string{err_msg};
}
}} // namespace ring::sip_utils
......@@ -79,7 +79,8 @@ std::string getHostFromUri(const std::string& sipUri);
void addContactHeader(const pj_str_t *contactStr, pjsip_tx_data *tdata);
void sip_strerror(pj_status_t code);
void sip_printerror(pj_status_t code);
std::string sip_strerror(pj_status_t code);
}} // namespace ring::sip_utils
......
......@@ -270,12 +270,9 @@ SIPAccount::onTransportStateChanged(pjsip_transport_state state, const pjsip_tra
RING_DBG("Transport state changed to %s for account %s !", SipTransport::stateToStr(state), accountID_.c_str());
if (!SipTransport::isAlive(transport_, state)) {
if (info) {
char err_msg[128];
err_msg[0] = '\0';
pj_str_t descr = pj_strerror(info->status, err_msg, sizeof(err_msg));
transportStatus_ = info->status;
transportError_ = std::string(descr.ptr, descr.slen);
RING_ERR("Transport disconnected: %.*s", descr.slen, descr.ptr);
transportError_ = sip_utils::sip_strerror(info->status);
RING_ERR("Transport disconnected: %s", transportError_.c_str());
}
else {
// This is already the generic error used by pjsip.
......@@ -993,7 +990,7 @@ SIPAccount::sendRegister()
//RING_DBG("pjsip_regc_init from:%s, srv:%s, contact:%s", from.c_str(), srvUri.c_str(), std::string(pj_strbuf(&pjContact), pj_strlen(&pjContact)).c_str());
if ((status = pjsip_regc_init(regc, &pjSrv, &pjFrom, &pjFrom, 1, &pjContact, getRegistrationExpire())) != PJ_SUCCESS) {
sip_utils::sip_strerror(status);
sip_utils::sip_printerror(status);
throw VoipLinkException("Unable to initialize account registration structure");
}
......@@ -1032,7 +1029,7 @@ SIPAccount::sendRegister()
// pjsip_regc_send increment the transport ref count by one,
if ((status = pjsip_regc_send(regc, tdata)) != PJ_SUCCESS) {
sip_utils::sip_strerror(status);
sip_utils::sip_printerror(status);
throw VoipLinkException("Unable to send account registration request");
}
......@@ -1147,7 +1144,7 @@ SIPAccount::sendUnregister()
pj_status_t status;
if ((status = pjsip_regc_send(regc, tdata)) != PJ_SUCCESS) {
sip_utils::sip_strerror(status);
sip_utils::sip_printerror(status);
throw VoipLinkException("Unable to send request to unregister sip account");
}
......@@ -2153,10 +2150,7 @@ SIPAccount::sendTextMessage(const std::string& to, const std::string& msg)
pj_status_t status = pjsip_endpt_create_request(link_->getEndpoint(), &msg_method,
&pjTo, &pjFrom, &pjTo, NULL, NULL, -1, NULL, &tdata);
if (status != PJ_SUCCESS) {
char err_msg[128];
err_msg[0] = '\0';
pj_str_t descr = pj_strerror(status, err_msg, sizeof(err_msg));
RING_ERR("Unable to create request: %.*s", descr.slen, descr.ptr);
RING_ERR("Unable to create request: %s", sip_utils::sip_strerror(status).c_str());
return;
}
......@@ -2181,10 +2175,7 @@ SIPAccount::sendTextMessage(const std::string& to, const std::string& msg)
status = pjsip_endpt_send_request(link_->getEndpoint(), tdata, -1, nullptr, nullptr);
if (status != PJ_SUCCESS) {
char err_msg[128];
err_msg[0] = '\0';
pj_str_t descr = pj_strerror(status, err_msg, sizeof(err_msg));
RING_ERR("Unable to send request: %.*s", descr.slen, descr.ptr);
RING_ERR("Unable to send request: %s", sip_utils::sip_strerror(status).c_str());
return;
}
}
......
......@@ -670,7 +670,7 @@ SIPCall::peerHungup()
inv->mod_data[getSIPVoIPLink()->getModId()] = NULL;
} else {
inv.reset();
sip_utils::sip_strerror(ret);
sip_utils::sip_printerror(ret);
}
}
......
......@@ -352,7 +352,7 @@ SipTransportBroker::createUdpTransport(const SipTransportDescr& d)
RING_ERR("UDP IPv%s Transport did not start on %s",
listeningAddress.isIpv4() ? "4" : "6",
listeningAddress.toString(true).c_str());
sip_utils::sip_strerror(status);
sip_utils::sip_printerror(status);
return nullptr;
}
......@@ -388,8 +388,7 @@ SipTransportBroker::getTlsListener(const SipTransportDescr& d, const pjsip_tls_s
pjsip_tpfactory *listener = nullptr;
const pj_status_t status = pjsip_tls_transport_start2(endpt_, settings, listeningAddress.pjPtr(), nullptr, 1, &listener);
if (status != PJ_SUCCESS) {
RING_ERR("TLS listener did not start");
sip_utils::sip_strerror(status);
RING_ERR("TLS listener did not start: %s", sip_utils::sip_strerror(status).c_str());
return nullptr;
}
return std::make_shared<TlsListener>(listener);
......@@ -423,8 +422,7 @@ SipTransportBroker::getTlsTransport(const std::shared_ptr<TlsListener>& l, const
&transport);
if (!transport || status != PJ_SUCCESS) {
RING_ERR("Could not get new TLS transport");
sip_utils::sip_strerror(status);
RING_ERR("Could not get new TLS transport: %s", sip_utils::sip_strerror(status).c_str());
return nullptr;
}
auto ret = std::make_shared<SipTransport>(transport, l);
......
......@@ -528,10 +528,8 @@ SIPVoIPLink::SIPVoIPLink()
auto status = pjsip_tpmgr_set_state_cb(pjsip_endpt_get_tpmgr(endpt_),
tp_state_callback);
if (status != PJ_SUCCESS) {
RING_ERR("Can't set transport callback");
sip_utils::sip_strerror(status);
}
if (status != PJ_SUCCESS)
RING_ERR("Can't set transport callback: %s", sip_utils::sip_strerror(status).c_str());
if (!ip_utils::getLocalAddr())
throw VoipLinkException("UserAgent: Unable to determine network capabilities");
......@@ -699,7 +697,7 @@ SIPVoIPLink::handleEvents()
static const pj_time_val timeout = {0, 0}; // polling
auto ret = pjsip_endpt_handle_events(endpt_, &timeout);
if (ret != PJ_SUCCESS)
sip_utils::sip_strerror(ret);
sip_utils::sip_printerror(ret);
#ifdef RING_VIDEO
dequeKeyframeRequests();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment