2014-07-21 07:10:57 +08:00
|
|
|
/** msi.c
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* Copyright (C) 2013 Tox project All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Tox.
|
|
|
|
*
|
|
|
|
* Tox is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tox is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif /* HAVE_CONFIG_H */
|
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
#include "../toxcore/logger.h"
|
2014-05-31 23:27:22 +08:00
|
|
|
#include "../toxcore/util.h"
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-02-10 06:06:44 +08:00
|
|
|
#include "msi.h"
|
2014-03-23 02:33:56 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2014-06-23 06:22:01 +08:00
|
|
|
#include <stdbool.h>
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
#define MSI_MAXMSG_SIZE 256
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
/* Define default timeout for a request.
|
|
|
|
* There is no behavior specified by the msi on what will
|
|
|
|
* client do on timeout, but to call timeout callback.
|
|
|
|
*/
|
|
|
|
#define m_deftout 10000 /* in milliseconds */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Protocol:
|
|
|
|
*
|
2014-07-21 07:10:57 +08:00
|
|
|
* |id [1 byte]| |size [1 byte]| |data [$size bytes]| |...{repeat}| |0 {end byte}|
|
2014-01-25 08:32:33 +08:00
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
typedef enum {
|
|
|
|
IDRequest = 1,
|
|
|
|
IDResponse,
|
|
|
|
IDReason,
|
|
|
|
IDCallType,
|
|
|
|
IDCallId,
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
} MSIHeaderID;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
typedef enum {
|
|
|
|
TypeRequest,
|
|
|
|
TypeResponse,
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
} MSIMessageType;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
invite,
|
|
|
|
start,
|
|
|
|
cancel,
|
|
|
|
reject,
|
|
|
|
end,
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
} MSIRequest;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
ringing,
|
|
|
|
starting,
|
|
|
|
ending,
|
|
|
|
error
|
|
|
|
|
|
|
|
} MSIResponse;
|
|
|
|
|
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
#define GENERIC_HEADER(header, val_type) \
|
2014-01-25 08:32:33 +08:00
|
|
|
typedef struct _MSIHeader##header { \
|
2014-07-21 07:10:57 +08:00
|
|
|
val_type value; \
|
|
|
|
_Bool exists; \
|
2014-01-25 08:32:33 +08:00
|
|
|
} MSIHeader##header;
|
|
|
|
|
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
GENERIC_HEADER ( Request, MSIRequest )
|
|
|
|
GENERIC_HEADER ( Response, MSIResponse )
|
|
|
|
GENERIC_HEADER ( CallType, MSICallType )
|
|
|
|
GENERIC_HEADER ( CallId, MSICallIDType )
|
|
|
|
GENERIC_HEADER ( Reason, MSIReasonStrType )
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief This is the message structure. It contains all of the headers and
|
|
|
|
* destination/source of the message stored in friend_id.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
*/
|
|
|
|
typedef struct _MSIMessage {
|
|
|
|
|
|
|
|
MSIHeaderRequest request;
|
|
|
|
MSIHeaderResponse response;
|
|
|
|
MSIHeaderCallType calltype;
|
|
|
|
MSIHeaderReason reason;
|
|
|
|
MSIHeaderCallId callid;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
int friend_id;
|
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
} MSIMessage;
|
|
|
|
|
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
inline__ void invoke_callback(MSISession *session, int32_t call_index, MSICallbackID id)
|
2014-03-12 05:59:20 +08:00
|
|
|
{
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( session->callbacks[id].function ) {
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_DEBUG("Invoking callback function: %d", id);
|
2014-07-21 07:10:57 +08:00
|
|
|
session->callbacks[id].function ( session->agent_handler, call_index, session->callbacks[id].data );
|
2014-04-28 01:21:26 +08:00
|
|
|
}
|
2014-03-12 05:59:20 +08:00
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Parse raw 'data' received from socket into MSIMessage struct.
|
|
|
|
* Every message has to have end value of 'end_byte' or _undefined_ behavior
|
|
|
|
* occures. The best practice is to check the end of the message at the handle_packet.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param msg Container.
|
|
|
|
* @param data The data.
|
|
|
|
* @return int
|
2014-04-17 00:08:44 +08:00
|
|
|
* @retval -1 Error occurred.
|
2014-01-25 08:32:33 +08:00
|
|
|
* @retval 0 Success.
|
|
|
|
*/
|
2014-07-20 10:08:05 +08:00
|
|
|
static int parse_raw_data ( MSIMessage *msg, const uint8_t *data, uint16_t length )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
#define FAIL_CONSTRAINT(constraint, wanted) if ((constraint -= wanted) < 1) { LOGGER_ERROR("Read over length!"); return -1; }
|
|
|
|
#define FAIL_SIZE(byte, valid) if ( byte != valid ) { LOGGER_ERROR("Invalid data size!"); return -1; }
|
|
|
|
#define FAIL_LIMITS(byte, low, high) if ( byte < low || byte > high ) { LOGGER_ERROR("Invalid data!"); return -1; }
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
if ( msg == NULL ) {
|
|
|
|
LOGGER_ERROR("Could not parse message: no storage!");
|
2014-06-01 03:02:50 +08:00
|
|
|
return -1;
|
2014-04-28 01:21:26 +08:00
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( data[length - 1] ) { /* End byte must have value 0 */
|
|
|
|
LOGGER_ERROR("Invalid end byte");
|
2014-02-07 07:10:55 +08:00
|
|
|
return -1;
|
2014-07-21 07:10:57 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
const uint8_t *it = data;
|
|
|
|
int size_constraint = length;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
while ( *it ) {/* until end byte is hit */
|
|
|
|
switch (*it) {
|
2014-07-22 23:20:55 +08:00
|
|
|
case IDRequest:
|
|
|
|
FAIL_CONSTRAINT(size_constraint, 3);
|
|
|
|
FAIL_SIZE(it[1], 1);
|
|
|
|
FAIL_LIMITS(it[2], invite, end);
|
|
|
|
msg->request.value = it[2];
|
|
|
|
it += 3;
|
|
|
|
msg->request.exists = 1;
|
2014-02-17 09:01:30 +08:00
|
|
|
break;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
case IDResponse:
|
|
|
|
FAIL_CONSTRAINT(size_constraint, 3);
|
|
|
|
FAIL_SIZE(it[1], 1);
|
|
|
|
FAIL_LIMITS(it[2], ringing, error);
|
|
|
|
msg->response.value = it[2];
|
|
|
|
it += 3;
|
|
|
|
msg->response.exists = 1;
|
2014-02-17 09:01:30 +08:00
|
|
|
break;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
case IDCallType:
|
|
|
|
FAIL_CONSTRAINT(size_constraint, 3);
|
|
|
|
FAIL_SIZE(it[1], 1);
|
|
|
|
FAIL_LIMITS(it[2], type_audio, type_video);
|
|
|
|
msg->calltype.value = it[2];
|
|
|
|
it += 3;
|
|
|
|
msg->calltype.exists = 1;
|
2014-02-17 09:01:30 +08:00
|
|
|
break;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
case IDCallId:
|
|
|
|
FAIL_CONSTRAINT(size_constraint, sizeof(MSICallIDType) + 2);
|
|
|
|
FAIL_SIZE(it[1], sizeof(MSICallIDType));
|
|
|
|
memcpy(msg->callid.value, it + 2, sizeof(MSICallIDType));
|
|
|
|
it += sizeof(MSICallIDType) + 2;
|
|
|
|
msg->callid.exists = 1;
|
2014-02-17 09:01:30 +08:00
|
|
|
break;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
case IDReason:
|
|
|
|
FAIL_CONSTRAINT(size_constraint, sizeof(MSIReasonStrType) + 2);
|
|
|
|
FAIL_SIZE(it[1], sizeof(MSIReasonStrType));
|
|
|
|
memcpy(msg->reason.value, it + 2, sizeof(MSIReasonStrType));
|
|
|
|
it += sizeof(MSIReasonStrType) + 2;
|
|
|
|
msg->reason.exists = 1;
|
2014-02-17 09:01:30 +08:00
|
|
|
break;
|
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
default:
|
|
|
|
LOGGER_ERROR("Invalid id byte");
|
|
|
|
return -1;
|
|
|
|
break;
|
2014-04-28 01:21:26 +08:00
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-17 09:01:30 +08:00
|
|
|
* @brief Create the message.
|
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param type Request or response.
|
|
|
|
* @param type_id Type of request/response.
|
|
|
|
* @return MSIMessage* Created message.
|
2014-04-17 00:08:44 +08:00
|
|
|
* @retval NULL Error occurred.
|
2014-01-25 08:32:33 +08:00
|
|
|
*/
|
2014-07-21 07:10:57 +08:00
|
|
|
MSIMessage *msi_new_message ( MSIMessageType type, const uint8_t type_value )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-07-21 07:10:57 +08:00
|
|
|
MSIMessage *retu = calloc ( sizeof ( MSIMessage ), 1 );
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( retu == NULL ) {
|
2014-06-28 10:13:38 +08:00
|
|
|
LOGGER_WARNING("Allocation failed! Program might misbehave!");
|
2014-04-28 01:21:26 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( type == TypeRequest ) {
|
|
|
|
retu->request.exists = 1;
|
|
|
|
retu->request.value = type_value;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
} else {
|
2014-07-21 07:10:57 +08:00
|
|
|
retu->response.exists = 1;
|
|
|
|
retu->response.value = type_value;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
return retu;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Parse data from handle_packet.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param data The data.
|
|
|
|
* @return MSIMessage* Parsed message.
|
2014-04-17 00:08:44 +08:00
|
|
|
* @retval NULL Error occurred.
|
2014-01-25 08:32:33 +08:00
|
|
|
*/
|
2014-07-21 07:10:57 +08:00
|
|
|
MSIMessage *parse_recv ( const uint8_t *data, uint16_t length )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-04-28 01:21:26 +08:00
|
|
|
if ( data == NULL ) {
|
|
|
|
LOGGER_WARNING("Tried to parse empty message!");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
MSIMessage *retu = calloc ( sizeof ( MSIMessage ), 1 );
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( retu == NULL ) {
|
2014-06-28 10:13:38 +08:00
|
|
|
LOGGER_WARNING("Allocation failed! Program might misbehave!");
|
2014-04-28 01:21:26 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( parse_raw_data ( retu, data, length ) == -1 ) {
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
free ( retu );
|
2014-01-25 08:32:33 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
return retu;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Speaks for it self.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param dest Container.
|
|
|
|
* @param header_field Field.
|
|
|
|
* @param header_value Field value.
|
|
|
|
* @param value_len Length of field value.
|
|
|
|
* @param length Pointer to container length.
|
|
|
|
* @return uint8_t* Iterated container.
|
|
|
|
*/
|
2014-07-21 07:10:57 +08:00
|
|
|
uint8_t *format_output ( uint8_t *dest, MSIHeaderID id, const void *value, uint8_t value_len, uint16_t *length )
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2014-04-28 01:21:26 +08:00
|
|
|
if ( dest == NULL ) {
|
|
|
|
LOGGER_ERROR("No destination space!");
|
2014-06-28 10:13:38 +08:00
|
|
|
return NULL;
|
2014-04-28 01:21:26 +08:00
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if (value == NULL || value_len == 0) {
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_ERROR("Empty header value");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
*dest = id;
|
|
|
|
dest ++;
|
|
|
|
*dest = value_len;
|
|
|
|
dest ++;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
memcpy(dest, value, value_len);
|
2014-06-15 22:36:57 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
*length += (2 + value_len);
|
2014-06-15 22:36:57 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
return dest + value_len; /* Set to next position ready to be written */
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-21 07:10:57 +08:00
|
|
|
* @brief Parse MSIMessage to send.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param msg The message.
|
|
|
|
* @param dest Destination.
|
2014-07-21 07:10:57 +08:00
|
|
|
* @return uint16_t Its final size.
|
2014-01-25 08:32:33 +08:00
|
|
|
*/
|
2014-07-21 07:10:57 +08:00
|
|
|
uint16_t parse_send ( MSIMessage *msg, uint8_t *dest )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-04-28 01:21:26 +08:00
|
|
|
if (msg == NULL) {
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_ERROR("No message!");
|
2014-04-28 01:21:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
if (dest == NULL ) {
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_ERROR("No destination!");
|
2014-04-28 01:21:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
uint8_t *it = dest;
|
|
|
|
uint16_t size = 0;
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if (msg->request.exists) {
|
|
|
|
uint8_t cast = msg->request.value;
|
|
|
|
it = format_output(it, IDRequest, &cast, 1, &size);
|
|
|
|
}
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if (msg->response.exists) {
|
|
|
|
uint8_t cast = msg->response.value;
|
|
|
|
it = format_output(it, IDResponse, &cast, 1, &size);
|
|
|
|
}
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if (msg->calltype.exists) {
|
|
|
|
uint8_t cast = msg->calltype.value;
|
|
|
|
it = format_output(it, IDCallType, &cast, 1, &size);
|
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if (msg->callid.exists) {
|
|
|
|
it = format_output(it, IDCallId, &msg->callid.value, sizeof(msg->callid.value), &size);
|
|
|
|
}
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if (msg->reason.exists) {
|
|
|
|
it = format_output(it, IDReason, &msg->reason.value, sizeof(msg->reason.value), &size);
|
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
*it = 0;
|
|
|
|
size ++;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
return size;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
void msi_msg_set_calltype ( MSIMessage *msg, const MSICallType value )
|
|
|
|
{
|
|
|
|
if ( !msg ) return;
|
|
|
|
|
|
|
|
msg->calltype.exists = 1;
|
|
|
|
msg->calltype.value = value;
|
2014-07-21 07:10:57 +08:00
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
void msi_msg_set_reason ( MSIMessage *msg, const MSIReasonStrType value )
|
|
|
|
{
|
|
|
|
if ( !msg ) return;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
msg->reason.exists = 1;
|
|
|
|
memcpy(msg->reason.value, value, sizeof(MSIReasonStrType));
|
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
void msi_msg_set_callid ( MSIMessage *msg, const MSICallIDType value )
|
|
|
|
{
|
|
|
|
if ( !msg ) return;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
msg->callid.exists = 1;
|
2014-07-21 07:10:57 +08:00
|
|
|
memcpy(msg->callid.value, value, sizeof(MSICallIDType));
|
|
|
|
}
|
2014-07-03 22:58:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct _Timer {
|
2014-07-09 02:39:23 +08:00
|
|
|
void *(*func)(void *);
|
2014-07-08 11:19:56 +08:00
|
|
|
void *func_arg1;
|
|
|
|
int func_arg2;
|
2014-07-03 22:58:00 +08:00
|
|
|
uint64_t timeout;
|
|
|
|
size_t idx;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
} Timer;
|
|
|
|
|
|
|
|
typedef struct _TimerHandler {
|
|
|
|
Timer **timers;
|
|
|
|
pthread_mutex_t mutex;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
size_t max_capacity;
|
|
|
|
size_t size;
|
|
|
|
uint64_t resolution;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
_Bool running;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
} TimerHandler;
|
|
|
|
|
2014-07-09 02:39:23 +08:00
|
|
|
struct timer_function_args {
|
|
|
|
void *arg1;
|
|
|
|
int arg2;
|
|
|
|
};
|
2014-07-03 22:58:00 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Allocate timer in array
|
2014-07-03 23:13:11 +08:00
|
|
|
*
|
2014-07-03 22:58:00 +08:00
|
|
|
* @param timers_container Handler
|
|
|
|
* @param func Function to be executed
|
|
|
|
* @param arg Its args
|
|
|
|
* @param timeout Timeout in ms
|
|
|
|
* @return int
|
|
|
|
*/
|
2014-07-20 10:08:05 +08:00
|
|
|
static int timer_alloc ( TimerHandler *timers_container, void *(func)(void *), void *arg1, int arg2, unsigned timeout)
|
2014-07-03 22:58:00 +08:00
|
|
|
{
|
2014-07-09 02:39:23 +08:00
|
|
|
static int timer_id;
|
2014-07-03 22:58:00 +08:00
|
|
|
pthread_mutex_lock(&timers_container->mutex);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
int i = 0;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
for (; i < timers_container->max_capacity && timers_container->timers[i]; i ++);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
|
|
|
if (i == timers_container->max_capacity) {
|
2014-07-03 22:58:00 +08:00
|
|
|
LOGGER_WARNING("Maximum capacity reached!");
|
|
|
|
pthread_mutex_unlock(&timers_container->mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-07-03 23:13:11 +08:00
|
|
|
|
|
|
|
Timer *timer = timers_container->timers[i] = calloc(sizeof(Timer), 1);
|
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
if (timer == NULL) {
|
|
|
|
LOGGER_ERROR("Failed to allocate timer!");
|
|
|
|
pthread_mutex_unlock(&timers_container->mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
timers_container->size ++;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
timer->func = func;
|
2014-07-08 11:19:56 +08:00
|
|
|
timer->func_arg1 = arg1;
|
|
|
|
timer->func_arg2 = arg2;
|
2014-07-03 22:58:00 +08:00
|
|
|
timer->timeout = timeout + current_time_monotonic(); /* In ms */
|
2014-07-09 02:39:23 +08:00
|
|
|
++timer_id;
|
|
|
|
timer->idx = timer_id;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
/* reorder */
|
|
|
|
if (i) {
|
|
|
|
int j = i - 1;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
for (; j >= 0 && timeout < timers_container->timers[j]->timeout; j--) {
|
2014-07-03 23:13:11 +08:00
|
|
|
Timer *tmp = timers_container->timers[j];
|
2014-07-03 22:58:00 +08:00
|
|
|
timers_container->timers[j] = timer;
|
2014-07-03 23:13:11 +08:00
|
|
|
timers_container->timers[j + 1] = tmp;
|
2014-07-03 22:58:00 +08:00
|
|
|
}
|
|
|
|
}
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
pthread_mutex_unlock(&timers_container->mutex);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
LOGGER_DEBUG("Allocated timer index: %d timeout: %d, current size: %d", i, timeout, timers_container->size);
|
2014-07-09 02:39:23 +08:00
|
|
|
return timer->idx;
|
2014-07-03 22:58:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Remove timer from array
|
2014-07-03 23:13:11 +08:00
|
|
|
*
|
2014-07-03 22:58:00 +08:00
|
|
|
* @param timers_container handler
|
2014-07-09 02:39:23 +08:00
|
|
|
* @param idx timer id
|
|
|
|
* @param lock_mutex (does the mutex need to be locked)
|
2014-07-03 22:58:00 +08:00
|
|
|
* @return int
|
|
|
|
*/
|
2014-07-20 10:08:05 +08:00
|
|
|
static int timer_release ( TimerHandler *timers_container, int idx , int lock_mutex)
|
2014-07-03 22:58:00 +08:00
|
|
|
{
|
2014-07-09 00:04:13 +08:00
|
|
|
if (lock_mutex)
|
|
|
|
pthread_mutex_lock(&timers_container->mutex);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
Timer **timed_events = timers_container->timers;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-09 02:39:23 +08:00
|
|
|
int i, res = -1;
|
|
|
|
|
|
|
|
for (i = 0; i < timers_container->max_capacity; ++i) {
|
|
|
|
if (timed_events[i] && timed_events[i]->idx == idx) {
|
|
|
|
res = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res == -1) {
|
|
|
|
LOGGER_WARNING("No event with id: %d", idx);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-09 00:04:13 +08:00
|
|
|
if (lock_mutex) pthread_mutex_unlock(&timers_container->mutex);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-09 02:39:23 +08:00
|
|
|
free(timed_events[res]);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-09 02:39:23 +08:00
|
|
|
timed_events[res] = NULL;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-09 02:39:23 +08:00
|
|
|
i = res + 1;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
for (; i < timers_container->max_capacity && timed_events[i]; i ++) {
|
2014-07-03 23:13:11 +08:00
|
|
|
timed_events[i - 1] = timed_events[i];
|
2014-07-03 22:58:00 +08:00
|
|
|
timed_events[i] = NULL;
|
|
|
|
}
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
timers_container->size--;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-21 10:10:05 +08:00
|
|
|
LOGGER_DEBUG("Popped id: %d, current size: %d ", idx, timers_container->size);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-09 00:04:13 +08:00
|
|
|
if (lock_mutex) pthread_mutex_unlock(&timers_container->mutex);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Main poll for timer execution
|
2014-07-03 23:13:11 +08:00
|
|
|
*
|
2014-07-03 22:58:00 +08:00
|
|
|
* @param arg ...
|
|
|
|
* @return void*
|
|
|
|
*/
|
2014-07-20 10:08:05 +08:00
|
|
|
static void *timer_poll( void *arg )
|
2014-07-03 22:58:00 +08:00
|
|
|
{
|
|
|
|
TimerHandler *handler = arg;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
while ( handler->running ) {
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
pthread_mutex_lock(&handler->mutex);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
if ( handler->running ) {
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
uint64_t time = current_time_monotonic();
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
while ( handler->timers[0] && handler->timers[0]->timeout < time ) {
|
2014-07-21 07:10:57 +08:00
|
|
|
pthread_t tid;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-09 02:39:23 +08:00
|
|
|
struct timer_function_args *args = malloc(sizeof(struct timer_function_args));
|
|
|
|
args->arg1 = handler->timers[0]->func_arg1;
|
|
|
|
args->arg2 = handler->timers[0]->func_arg2;
|
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( 0 != pthread_create(&tid, NULL, handler->timers[0]->func, args) ||
|
2014-07-22 23:20:55 +08:00
|
|
|
0 != pthread_detach(tid) ) {
|
2014-07-09 02:39:23 +08:00
|
|
|
LOGGER_ERROR("Failed to execute timer at: %d!", handler->timers[0]->timeout);
|
|
|
|
free(args);
|
|
|
|
} else {
|
|
|
|
LOGGER_DEBUG("Exectued timer assigned at: %d", handler->timers[0]->timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
timer_release(handler, handler->timers[0]->idx, 0);
|
2014-07-03 22:58:00 +08:00
|
|
|
}
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
}
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
pthread_mutex_unlock(&handler->mutex);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
usleep(handler->resolution);
|
|
|
|
}
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Start timer poll and return handler
|
2014-07-03 23:13:11 +08:00
|
|
|
*
|
2014-07-03 22:58:00 +08:00
|
|
|
* @param max_capacity capacity
|
|
|
|
* @param resolution ...
|
|
|
|
* @return TimerHandler*
|
|
|
|
*/
|
2014-07-20 10:08:05 +08:00
|
|
|
static TimerHandler *timer_init_session (int max_capacity, int resolution)
|
2014-07-03 22:58:00 +08:00
|
|
|
{
|
2014-07-03 23:13:11 +08:00
|
|
|
TimerHandler *handler = calloc(1, sizeof(TimerHandler));
|
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
if (handler == NULL) {
|
|
|
|
LOGGER_ERROR("Failed to allocate memory, program might misbehave!");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-07-03 23:13:11 +08:00
|
|
|
|
|
|
|
handler->timers = calloc(max_capacity, sizeof(Timer *));
|
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
if (handler->timers == NULL) {
|
|
|
|
LOGGER_ERROR("Failed to allocate %d timed events!", max_capacity);
|
|
|
|
free(handler);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
handler->max_capacity = max_capacity;
|
|
|
|
handler->running = 1;
|
|
|
|
handler->resolution = resolution;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
pthread_mutex_init(&handler->mutex, NULL);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
pthread_t _tid;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
|
|
|
if ( 0 != pthread_create(&_tid, NULL, timer_poll, handler) || 0 != pthread_detach(_tid) ) {
|
2014-07-03 22:58:00 +08:00
|
|
|
LOGGER_ERROR("Failed to start timer poll thread!");
|
|
|
|
free(handler->timers);
|
|
|
|
free(handler);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Terminate timer session
|
2014-07-03 23:13:11 +08:00
|
|
|
*
|
2014-07-03 22:58:00 +08:00
|
|
|
* @param handler The timer handler
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-07-20 10:08:05 +08:00
|
|
|
static void timer_terminate_session(TimerHandler *handler)
|
2014-07-03 23:13:11 +08:00
|
|
|
{
|
2014-07-03 22:58:00 +08:00
|
|
|
pthread_mutex_lock(&handler->mutex);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
handler->running = 0;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
pthread_mutex_unlock(&handler->mutex);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
int i = 0;
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
for (; i < handler->max_capacity; i ++)
|
|
|
|
free(handler->timers[i]);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
free(handler->timers);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
pthread_mutex_destroy( &handler->mutex );
|
|
|
|
}
|
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/**
|
|
|
|
* @brief Generate _random_ alphanumerical string.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param str Destination.
|
|
|
|
* @param size Size of string.
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-07-20 10:08:05 +08:00
|
|
|
static void t_randomstr ( uint8_t *str, uint32_t size )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-04-28 01:21:26 +08:00
|
|
|
if (str == NULL) {
|
|
|
|
LOGGER_DEBUG("Empty destination!");
|
|
|
|
return;
|
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
static const uint8_t _bytes[] =
|
|
|
|
"0123456789"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
2014-06-28 10:13:38 +08:00
|
|
|
uint32_t _it = 0;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
for ( ; _it < size; _it++ ) {
|
2014-02-03 03:43:16 +08:00
|
|
|
str[_it] = _bytes[ random_int() % 61 ];
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
2014-06-23 06:22:01 +08:00
|
|
|
error_none,
|
|
|
|
error_deadcall, /* has call id but it's from old call */
|
2014-01-25 08:32:33 +08:00
|
|
|
error_id_mismatch, /* non-existing call */
|
|
|
|
|
|
|
|
error_no_callid, /* not having call id */
|
|
|
|
error_no_call, /* no call in session */
|
|
|
|
error_no_crypto_key, /* no crypto key */
|
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
error_busy
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
} MSICallError; /* Error codes */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Stringify error code.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param error_code The code.
|
|
|
|
* @return const uint8_t* The string.
|
|
|
|
*/
|
2014-03-12 05:59:20 +08:00
|
|
|
static inline__ const uint8_t *stringify_error ( MSICallError error_code )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
|
|
|
static const uint8_t *strings[] = {
|
2014-06-15 22:36:57 +08:00
|
|
|
( uint8_t *) "",
|
|
|
|
( uint8_t *) "Using dead call",
|
|
|
|
( uint8_t *) "Call id not set to any call",
|
|
|
|
( uint8_t *) "Call id not available",
|
|
|
|
( uint8_t *) "No active call in session",
|
|
|
|
( uint8_t *) "No Crypto-key set",
|
|
|
|
( uint8_t *) "Callee busy"
|
2014-01-25 08:32:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return strings[error_code];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Speaks for it self.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session Control session.
|
|
|
|
* @param msg The message.
|
|
|
|
* @param to Where to.
|
|
|
|
* @return int
|
2014-04-17 00:08:44 +08:00
|
|
|
* @retval -1 Error occurred.
|
2014-01-25 08:32:33 +08:00
|
|
|
* @retval 0 Success.
|
|
|
|
*/
|
2014-07-20 10:08:05 +08:00
|
|
|
static int send_message ( MSISession *session, MSICall *call, MSIMessage *msg, uint32_t to )
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2014-07-21 07:10:57 +08:00
|
|
|
msi_msg_set_callid ( msg, call->id );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
uint8_t msg_string_final [MSI_MAXMSG_SIZE];
|
|
|
|
uint16_t length = parse_send ( msg, msg_string_final );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if (!length) {
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_WARNING("Parsing message failed; nothing sent!");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( m_msi_packet(session->messenger_handle, to, msg_string_final, length) ) {
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_DEBUG("Sent message");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
return -1;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
inline__ int send_reponse ( MSISession *session, MSICall *call, MSIResponse response, uint32_t to )
|
|
|
|
{
|
|
|
|
MSIMessage *msg = msi_new_message ( TypeResponse, response );
|
2014-07-22 23:24:47 +08:00
|
|
|
int ret = send_message ( session, call, msg, to );
|
2014-07-21 07:10:57 +08:00
|
|
|
free ( msg );
|
2014-07-22 23:24:47 +08:00
|
|
|
return ret;
|
2014-07-21 07:10:57 +08:00
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-03-23 02:33:56 +08:00
|
|
|
/**
|
|
|
|
* @brief Determine 'bigger' call id
|
2014-05-26 00:27:48 +08:00
|
|
|
*
|
2014-03-23 02:33:56 +08:00
|
|
|
* @param first duh
|
|
|
|
* @param second duh
|
|
|
|
* @return int
|
|
|
|
* @retval 0 it's first
|
|
|
|
* @retval 1 it's second
|
|
|
|
*/
|
2014-07-20 10:08:05 +08:00
|
|
|
static int call_id_bigger( const uint8_t *first, const uint8_t *second)
|
2014-03-23 02:33:56 +08:00
|
|
|
{
|
2014-07-21 07:10:57 +08:00
|
|
|
return (memcmp(first, second, sizeof(MSICallIDType)) < 0);
|
2014-03-23 02:33:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/**
|
|
|
|
* @brief Speaks for it self.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session Control session.
|
|
|
|
* @param msg The message.
|
|
|
|
* @param peer_id The peer.
|
2014-07-21 10:10:05 +08:00
|
|
|
* @return -1, 0
|
2014-01-25 08:32:33 +08:00
|
|
|
*/
|
2014-07-21 10:10:05 +08:00
|
|
|
static int flush_peer_type ( MSICall *call, MSIMessage *msg, int peer_id )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-07-22 23:20:55 +08:00
|
|
|
if ( msg->calltype.exists ) {
|
2014-07-21 07:10:57 +08:00
|
|
|
call->type_peer[peer_id] = msg->calltype.value;
|
|
|
|
return 0;
|
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_WARNING("No call type header!");
|
|
|
|
return -1;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
|
2014-07-20 10:08:05 +08:00
|
|
|
static int terminate_call ( MSISession *session, MSICall *call );
|
|
|
|
|
|
|
|
static void handle_remote_connection_change(Messenger *messenger, int friend_num, uint8_t status, void *session_p)
|
2014-02-16 03:44:33 +08:00
|
|
|
{
|
2014-02-17 09:01:30 +08:00
|
|
|
MSISession *session = session_p;
|
|
|
|
|
|
|
|
switch ( status ) {
|
|
|
|
case 0: { /* Went offline */
|
2014-05-26 00:27:48 +08:00
|
|
|
uint32_t j = 0;
|
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
for ( ; j < session->max_calls; j ++ ) {
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
if ( !session->calls[j] ) continue;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-02-16 03:44:33 +08:00
|
|
|
int i = 0;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
for ( ; i < session->calls[j]->peer_count; i ++ )
|
|
|
|
if ( session->calls[j]->peers[i] == friend_num ) {
|
2014-07-21 07:10:57 +08:00
|
|
|
invoke_callback(session, j, MSI_OnPeerTimeout);
|
2014-07-11 06:20:07 +08:00
|
|
|
terminate_call(session, session->calls[j]);
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_DEBUG("Remote: %d timed out!", friend_num);
|
|
|
|
return; /* TODO: On group calls change behaviour */
|
2014-02-16 03:44:33 +08:00
|
|
|
}
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2014-02-16 03:44:33 +08:00
|
|
|
}
|
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-20 10:08:05 +08:00
|
|
|
static MSICall *find_call ( MSISession *session, uint8_t *call_id )
|
2014-04-28 01:21:26 +08:00
|
|
|
{
|
|
|
|
if ( call_id == NULL ) return NULL;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
uint32_t i = 0;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
|
|
|
for (; i < session->max_calls; i ++ )
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( session->calls[i] && memcmp(session->calls[i]->id, call_id, sizeof(session->calls[i]->id)) == 0 ) {
|
2014-04-28 01:21:26 +08:00
|
|
|
return session->calls[i];
|
2014-05-24 22:02:01 +08:00
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/**
|
|
|
|
* @brief Sends error response to peer.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session The session.
|
|
|
|
* @param errid The id.
|
|
|
|
* @param to Where to?
|
|
|
|
* @return int
|
2014-06-23 06:22:01 +08:00
|
|
|
* @retval -1/0 It's usually always success.
|
2014-01-25 08:32:33 +08:00
|
|
|
*/
|
2014-07-20 10:08:05 +08:00
|
|
|
static int send_error ( MSISession *session, MSICall *call, MSICallError errid, uint32_t to )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-05-31 06:29:43 +08:00
|
|
|
if (!call) {
|
|
|
|
LOGGER_WARNING("Cannot handle error on 'null' call");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_DEBUG("Sending error: %d on call: %s", errid, call->id);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
MSIMessage *msg_error = msi_new_message ( TypeResponse, error );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
msi_msg_set_reason ( msg_error, stringify_error(errid) );
|
|
|
|
send_message ( session, call, msg_error, to );
|
|
|
|
free ( msg_error );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Add peer to peer list.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param call What call.
|
|
|
|
* @param peer_id Its id.
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-07-20 10:08:05 +08:00
|
|
|
static void add_peer( MSICall *call, int peer_id )
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2014-06-29 10:29:39 +08:00
|
|
|
uint32_t *peers = !call->peers ? peers = calloc(sizeof(uint32_t), 1) :
|
|
|
|
realloc( call->peers, sizeof(uint32_t) * call->peer_count);
|
|
|
|
|
2014-06-28 10:13:38 +08:00
|
|
|
if (!peers) {
|
|
|
|
LOGGER_WARNING("Allocation failed! Program might misbehave!");
|
|
|
|
return;
|
2014-02-17 09:01:30 +08:00
|
|
|
}
|
|
|
|
|
2014-06-28 10:13:38 +08:00
|
|
|
call->peer_count ++;
|
|
|
|
call->peers = peers;
|
2014-01-25 08:32:33 +08:00
|
|
|
call->peers[call->peer_count - 1] = peer_id;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-17 01:56:40 +08:00
|
|
|
LOGGER_DEBUG("Added peer: %d", peer_id);
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Speaks for it self.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session Control session.
|
|
|
|
* @param peers Amount of peers. (Currently it only supports 1)
|
|
|
|
* @param ringing_timeout Ringing timeout.
|
|
|
|
* @return MSICall* The created call.
|
|
|
|
*/
|
2014-07-20 10:08:05 +08:00
|
|
|
static MSICall *init_call ( MSISession *session, int peers, int ringing_timeout )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
if (peers == 0) {
|
|
|
|
LOGGER_ERROR("No peers!");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
int32_t call_idx = 0;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
for (; call_idx < session->max_calls; call_idx ++) {
|
|
|
|
if ( !session->calls[call_idx] ) {
|
2014-06-29 10:29:39 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
if (!(session->calls[call_idx] = calloc ( sizeof ( MSICall ), 1 ))) {
|
2014-06-28 10:13:38 +08:00
|
|
|
LOGGER_WARNING("Allocation failed! Program might misbehave!");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-06-29 10:29:39 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
if ( call_idx == session->max_calls ) {
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_WARNING("Reached maximum amount of calls!");
|
2014-05-03 07:46:03 +08:00
|
|
|
return NULL;
|
2014-04-28 01:21:26 +08:00
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
MSICall *call = session->calls[call_idx];
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
call->call_idx = call_idx;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
if ( !(call->type_peer = calloc ( sizeof ( MSICallType ), peers )) ) {
|
2014-06-28 10:13:38 +08:00
|
|
|
LOGGER_WARNING("Allocation failed! Program might misbehave!");
|
2014-07-05 21:11:25 +08:00
|
|
|
free(call);
|
2014-04-28 01:21:26 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
call->session = session;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
/*_call->_participant_count = _peers;*/
|
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
call->request_timer_id = 0;
|
|
|
|
call->ringing_timer_id = 0;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
call->ringing_tout_ms = ringing_timeout;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_init ( &call->mutex, NULL );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
LOGGER_DEBUG("Started new call with index: %u", call_idx);
|
|
|
|
return call;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Terminate the call.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session Control session.
|
|
|
|
* @return int
|
2014-04-17 00:08:44 +08:00
|
|
|
* @retval -1 Error occurred.
|
2014-01-25 08:32:33 +08:00
|
|
|
* @retval 0 Success.
|
|
|
|
*/
|
2014-07-20 10:08:05 +08:00
|
|
|
static int terminate_call ( MSISession *session, MSICall *call )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-05-26 00:27:48 +08:00
|
|
|
if ( !call ) {
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_WARNING("Tried to terminate non-existing call!");
|
2014-01-25 08:32:33 +08:00
|
|
|
return -1;
|
2014-04-28 01:21:26 +08:00
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-05-24 22:02:01 +08:00
|
|
|
LOGGER_DEBUG("Terminated call id: %d", call->call_idx);
|
2014-01-25 08:32:33 +08:00
|
|
|
/* Check event loop and cancel timed events if there are any
|
2014-02-16 03:44:33 +08:00
|
|
|
* NOTE: This has to be done before possibly
|
2014-01-25 08:32:33 +08:00
|
|
|
* locking the mutex the second time
|
|
|
|
*/
|
2014-07-09 00:04:13 +08:00
|
|
|
timer_release ( session->timer_handler, call->request_timer_id, 1);
|
|
|
|
timer_release ( session->timer_handler, call->ringing_timer_id, 1);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
/* Get a handle */
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_lock ( &call->mutex );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-05-26 00:27:48 +08:00
|
|
|
session->calls[call->call_idx] = NULL;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
free ( call->type_peer );
|
|
|
|
free ( call->peers);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
/* Release handle */
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock ( &call->mutex );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_destroy ( &call->mutex );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
free ( call );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-17 01:56:40 +08:00
|
|
|
/**
|
|
|
|
* @brief Function called at request timeout. If not called in thread it might cause trouble
|
|
|
|
*
|
|
|
|
* @param arg Control session
|
|
|
|
* @return void*
|
|
|
|
*/
|
2014-07-20 10:08:05 +08:00
|
|
|
static void *handle_timeout ( void *arg )
|
2014-05-17 01:56:40 +08:00
|
|
|
{
|
|
|
|
/* TODO: Cancel might not arrive there; set up
|
|
|
|
* timers on these cancels and terminate call on
|
|
|
|
* their timeout
|
|
|
|
*/
|
2014-07-09 02:39:23 +08:00
|
|
|
struct timer_function_args *args = arg;
|
|
|
|
int call_index = args->arg2;
|
|
|
|
MSISession *session = args->arg1;
|
2014-07-21 07:10:57 +08:00
|
|
|
MSICall *call = session->calls[call_index];
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if (call) {
|
|
|
|
LOGGER_DEBUG("[Call: %d] Request timed out!", call->call_idx);
|
2014-06-19 07:44:49 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
invoke_callback(session, call_index, MSI_OnRequestTimeout);
|
2014-06-19 03:33:11 +08:00
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( call && call->session ) {
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-17 01:56:40 +08:00
|
|
|
/* TODO: Cancel all? */
|
2014-05-26 00:27:48 +08:00
|
|
|
/* uint16_t _it = 0;
|
2014-05-17 01:56:40 +08:00
|
|
|
* for ( ; _it < _session->call->peer_count; _it++ ) */
|
2014-07-21 07:10:57 +08:00
|
|
|
msi_cancel ( call->session, call->call_idx, call->peers [0], "Request timed out" );
|
|
|
|
/*terminate_call(call->session, call);*/
|
2014-05-17 01:56:40 +08:00
|
|
|
}
|
2014-07-09 02:39:23 +08:00
|
|
|
|
|
|
|
free(arg);
|
|
|
|
pthread_exit(NULL);
|
2014-05-17 01:56:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/********** Request handlers **********/
|
2014-07-20 10:08:05 +08:00
|
|
|
static int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Handling 'invite' on call: %d", session, call ? call->call_idx : -1);
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if (!msg->calltype.exists) {/**/
|
|
|
|
LOGGER_WARNING("Peer sent invalid call type!");
|
2014-07-22 23:20:55 +08:00
|
|
|
send_error ( session, call, error_no_callid, msg->friend_id );
|
2014-07-21 07:10:57 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
if ( call ) {
|
2014-05-26 00:27:48 +08:00
|
|
|
if ( call->peers[0] == msg->friend_id ) {
|
2014-07-21 07:10:57 +08:00
|
|
|
if (call->state == call_inviting) {
|
|
|
|
/* The glare case. A calls B when at the same time
|
|
|
|
* B calls A. Who has advantage is set bey calculating
|
|
|
|
* 'bigger' Call id and then that call id is being used in
|
|
|
|
* future. User with 'bigger' Call id has the advantage
|
|
|
|
* as in he will wait the response from the other.
|
|
|
|
*/
|
|
|
|
LOGGER_DEBUG("Glare case; Peer: %d", call->peers[0]);
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( call_id_bigger (call->id, msg->callid.value) == 1 ) { /* Peer has advantage */
|
|
|
|
|
|
|
|
/* Terminate call; peer will timeout(call) if call initialization fails */
|
|
|
|
terminate_call(session, call);
|
|
|
|
|
|
|
|
call = init_call ( session, 1, 0 );
|
|
|
|
|
|
|
|
if ( !call ) {
|
|
|
|
pthread_mutex_unlock(&session->mutex);
|
|
|
|
LOGGER_ERROR("Starting call");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
} else {
|
|
|
|
pthread_mutex_unlock(&session->mutex);
|
|
|
|
return 0; /* Wait for ringing from peer */
|
|
|
|
}
|
|
|
|
} else if (call->state == call_active) {
|
|
|
|
/* Request for media change; call callback and send starting response */
|
|
|
|
if (flush_peer_type(call, msg, 0) != 0) { /**/
|
|
|
|
LOGGER_WARNING("Peer sent invalid call type!");
|
2014-07-22 23:20:55 +08:00
|
|
|
send_error ( session, call, error_no_callid, msg->friend_id );
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-05-31 06:29:43 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_DEBUG("Set new call type: %s", call->type_peer[0] == type_audio ? "audio" : "video");
|
|
|
|
send_reponse(session, call, starting, msg->friend_id);
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-07-21 07:10:57 +08:00
|
|
|
invoke_callback(session, call->call_idx, MSI_OnMediaChange);
|
|
|
|
return 1;
|
2014-03-23 02:33:56 +08:00
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
} else {
|
2014-06-23 06:22:01 +08:00
|
|
|
send_error ( session, call, error_busy, msg->friend_id ); /* TODO: Ugh*/
|
|
|
|
terminate_call(session, call);
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-03-23 02:33:56 +08:00
|
|
|
return 0;
|
2014-05-26 00:27:48 +08:00
|
|
|
}
|
2014-05-31 07:11:28 +08:00
|
|
|
} else {
|
2014-05-31 06:29:43 +08:00
|
|
|
call = init_call ( session, 1, 0 );
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-05-31 06:29:43 +08:00
|
|
|
if ( !call ) {
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-05-31 06:29:43 +08:00
|
|
|
LOGGER_ERROR("Starting call");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( !msg->callid.exists ) {
|
2014-06-23 06:22:01 +08:00
|
|
|
send_error ( session, call, error_no_callid, msg->friend_id );
|
|
|
|
terminate_call(session, call);
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-01-25 08:32:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
memcpy ( call->id, msg->callid.value, sizeof(msg->callid.value) );
|
2014-05-31 06:29:43 +08:00
|
|
|
call->state = call_starting;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-31 06:29:43 +08:00
|
|
|
add_peer( call, msg->friend_id);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-31 06:29:43 +08:00
|
|
|
flush_peer_type ( call, msg, 0 );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
send_reponse(session, call, ringing, msg->friend_id);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
invoke_callback(session, call->call_idx, MSI_OnInvite);
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2014-07-20 10:08:05 +08:00
|
|
|
|
|
|
|
static int handle_recv_start ( MSISession *session, MSICall *call, MSIMessage *msg )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-06-23 06:22:01 +08:00
|
|
|
if ( !call ) {
|
|
|
|
LOGGER_WARNING("Session: %p Handling 'start' on no call");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-23 07:54:40 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Handling 'start' on call: %d, friend id: %d", session, call->call_idx, msg->friend_id );
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
call->state = call_active;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
invoke_callback(session, call->call_idx, MSI_OnStart);
|
2014-01-25 08:32:33 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2014-07-20 10:08:05 +08:00
|
|
|
|
|
|
|
static int handle_recv_reject ( MSISession *session, MSICall *call, MSIMessage *msg )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-06-23 06:22:01 +08:00
|
|
|
if ( !call ) {
|
|
|
|
LOGGER_WARNING("Session: %p Handling 'start' on no call");
|
2014-01-25 08:32:33 +08:00
|
|
|
return 0;
|
2014-05-30 05:57:58 +08:00
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Handling 'reject' on call: %s", session, call->call_idx);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
invoke_callback(session, call->call_idx, MSI_OnReject);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
send_reponse(session, call, ending, msg->friend_id);
|
|
|
|
terminate_call(session, call);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2014-07-20 10:08:05 +08:00
|
|
|
|
|
|
|
static int handle_recv_cancel ( MSISession *session, MSICall *call, MSIMessage *msg )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-06-23 06:22:01 +08:00
|
|
|
if ( !call ) {
|
|
|
|
LOGGER_WARNING("Session: %p Handling 'start' on no call");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-23 07:54:40 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Handling 'cancel' on call: %s", session, call->call_idx);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
invoke_callback(session, call->call_idx, MSI_OnCancel);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
terminate_call ( session, call );
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2014-07-20 10:08:05 +08:00
|
|
|
|
|
|
|
static int handle_recv_end ( MSISession *session, MSICall *call, MSIMessage *msg )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-06-23 06:22:01 +08:00
|
|
|
if ( !call ) {
|
|
|
|
LOGGER_WARNING("Session: %p Handling 'start' on no call");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-23 07:54:40 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Handling 'end' on call: %d", session, call->call_idx);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
invoke_callback(session, call->call_idx, MSI_OnEnd);
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
send_reponse(session, call, ending, msg->friend_id);
|
|
|
|
terminate_call ( session, call );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********** Response handlers **********/
|
2014-07-20 10:08:05 +08:00
|
|
|
static int handle_recv_ringing ( MSISession *session, MSICall *call, MSIMessage *msg )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-06-23 06:22:01 +08:00
|
|
|
if ( !call ) {
|
|
|
|
LOGGER_WARNING("Session: %p Handling 'start' on no call");
|
2014-01-25 08:32:33 +08:00
|
|
|
return 0;
|
2014-05-30 05:57:58 +08:00
|
|
|
}
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-06-27 05:42:37 +08:00
|
|
|
if ( call->ringing_timer_id ) {
|
|
|
|
LOGGER_WARNING("Call already ringing");
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-06-27 05:42:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Handling 'ringing' on call: %d", session, call->call_idx );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-08 11:19:56 +08:00
|
|
|
call->ringing_timer_id = timer_alloc ( session->timer_handler, handle_timeout, session, call->call_idx,
|
|
|
|
call->ringing_tout_ms );
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
invoke_callback(session, call->call_idx, MSI_OnRinging);
|
2014-01-25 08:32:33 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2014-07-20 10:08:05 +08:00
|
|
|
static int handle_recv_starting ( MSISession *session, MSICall *call, MSIMessage *msg )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-06-23 06:22:01 +08:00
|
|
|
if ( !call ) {
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_WARNING("Session: %p Handling 'starting' on non-existing call");
|
2014-01-25 08:32:33 +08:00
|
|
|
return 0;
|
2014-05-30 05:57:58 +08:00
|
|
|
}
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( call->state == call_active ) { /* Change media */
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Changing media on call: %d", session, call->call_idx );
|
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
invoke_callback(session, call->call_idx, MSI_OnMediaChange);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
} else if ( call->state == call_inviting ) {
|
|
|
|
LOGGER_DEBUG("Session: %p Handling 'starting' on call: %d", session, call->call_idx );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
call->state = call_active;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
MSIMessage *msg_start = msi_new_message ( TypeRequest, start );
|
|
|
|
send_message ( session, call, msg_start, msg->friend_id );
|
|
|
|
free ( msg_start );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
flush_peer_type ( call, msg, 0 );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
/* This is here in case of glare */
|
|
|
|
timer_release ( session->timer_handler, call->ringing_timer_id, 1 );
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
invoke_callback(session, call->call_idx, MSI_OnStarting);
|
2014-07-22 23:20:55 +08:00
|
|
|
} else {
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_ERROR("Invalid call state");
|
|
|
|
terminate_call(session, call );
|
|
|
|
pthread_mutex_unlock(&session->mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2014-07-20 10:08:05 +08:00
|
|
|
static int handle_recv_ending ( MSISession *session, MSICall *call, MSIMessage *msg )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-06-23 06:22:01 +08:00
|
|
|
if ( !call ) {
|
|
|
|
LOGGER_WARNING("Session: %p Handling 'start' on no call");
|
2014-05-30 05:57:58 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Handling 'ending' on call: %d", session, call->call_idx );
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
invoke_callback(session, call->call_idx, MSI_OnEnding);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-03-07 10:13:04 +08:00
|
|
|
/* Terminate call */
|
2014-07-21 07:10:57 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-04-28 01:21:26 +08:00
|
|
|
terminate_call ( session, call );
|
2014-07-21 07:10:57 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2014-07-20 10:08:05 +08:00
|
|
|
static int handle_recv_error ( MSISession *session, MSICall *call, MSIMessage *msg )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-05-26 00:27:48 +08:00
|
|
|
if ( !call ) {
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_WARNING("Handling 'error' on non-existing call!");
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-04-28 01:21:26 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Handling 'error' on call: %d", session, call->call_idx );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
invoke_callback(session, call->call_idx, MSI_OnEnding);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/* Handle error accordingly */
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( msg->reason.exists ) {
|
|
|
|
/* TODO */
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
terminate_call ( session, call );
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-05-31 07:11:28 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
/**
|
|
|
|
* @brief BASIC call flow:
|
|
|
|
*
|
|
|
|
* ALICE BOB
|
|
|
|
* | invite --> |
|
|
|
|
* | |
|
|
|
|
* | <-- ringing |
|
|
|
|
* | |
|
|
|
|
* | <-- starting |
|
|
|
|
* | |
|
|
|
|
* | start --> |
|
|
|
|
* | |
|
|
|
|
* | <-- MEDIA TRANS --> |
|
|
|
|
* | |
|
|
|
|
* | end --> |
|
|
|
|
* | |
|
|
|
|
* | <-- ending |
|
|
|
|
*
|
|
|
|
* Alice calls Bob by sending invite packet.
|
|
|
|
* Bob recvs the packet and sends an ringing packet;
|
|
|
|
* which notifies Alice that her invite is acknowledged.
|
|
|
|
* Ringing screen shown on both sides.
|
|
|
|
* Bob accepts the invite for a call by sending starting packet.
|
|
|
|
* Alice recvs the starting packet and sends the started packet to
|
|
|
|
* inform Bob that she recved the starting packet.
|
|
|
|
* Now the media transmission is established ( i.e. RTP transmission ).
|
|
|
|
* Alice hangs up and sends end packet.
|
|
|
|
* Bob recves the end packet and sends ending packet
|
|
|
|
* as the acknowledgement that the call is ending.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2014-07-20 10:08:05 +08:00
|
|
|
static void msi_handle_packet ( Messenger *messenger, int source, const uint8_t *data, uint16_t length, void *object )
|
2014-02-01 19:52:48 +08:00
|
|
|
{
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_DEBUG("Got msi message");
|
2014-02-01 19:52:48 +08:00
|
|
|
/* Unused */
|
|
|
|
(void)messenger;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
MSISession *session = object;
|
|
|
|
MSIMessage *msg;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-26 00:27:48 +08:00
|
|
|
if ( !length ) {
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_WARNING("Lenght param negative");
|
|
|
|
return;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
msg = parse_recv ( data, length );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
if ( !msg ) {
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_WARNING("Error parsing message");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
LOGGER_DEBUG("Successfully parsed message");
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
msg->friend_id = source;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
/* Find what call */
|
2014-07-21 07:10:57 +08:00
|
|
|
MSICall *call = msg->callid.exists ? find_call(session, msg->callid.value ) : NULL;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
/* Now handle message */
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( msg->request.exists ) { /* Handle request */
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
switch (msg->request.value) {
|
|
|
|
case invite:
|
|
|
|
handle_recv_invite ( session, call, msg );
|
|
|
|
break;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
case start:
|
|
|
|
handle_recv_start ( session, call, msg );
|
|
|
|
break;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
case cancel:
|
|
|
|
handle_recv_cancel ( session, call, msg );
|
|
|
|
break;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
case reject:
|
|
|
|
handle_recv_reject ( session, call, msg );
|
|
|
|
break;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
case end:
|
|
|
|
handle_recv_end ( session, call, msg );
|
|
|
|
break;
|
2014-02-01 19:52:48 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
} else if ( msg->response.exists ) { /* Handle response */
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-31 06:29:43 +08:00
|
|
|
/* Got response so cancel timer */
|
2014-07-09 00:04:13 +08:00
|
|
|
if ( call ) timer_release ( session->timer_handler, call->request_timer_id, 1 );
|
2014-05-31 06:29:43 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
switch (msg->response.value) {
|
|
|
|
case ringing:
|
|
|
|
handle_recv_ringing ( session, call, msg );
|
|
|
|
break;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
case starting:
|
|
|
|
handle_recv_starting ( session, call, msg );
|
|
|
|
break;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
case ending:
|
|
|
|
handle_recv_ending ( session, call, msg );
|
|
|
|
break;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-22 23:20:55 +08:00
|
|
|
case error:
|
|
|
|
handle_recv_error ( session, call, msg );
|
|
|
|
break;
|
2014-04-28 01:21:26 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
} else {
|
|
|
|
LOGGER_WARNING("Invalid message: no resp nor requ headers");
|
2014-02-01 19:52:48 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
free ( msg );
|
2014-02-01 19:52:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/**
|
|
|
|
* @brief Callback setter.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param callback The callback.
|
|
|
|
* @param id The id.
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-07-21 07:10:57 +08:00
|
|
|
void msi_register_callback ( MSISession *session, MSICallbackType callback, MSICallbackID id, void *userdata )
|
2014-01-25 08:32:33 +08:00
|
|
|
{
|
2014-07-21 07:10:57 +08:00
|
|
|
session->callbacks[id].function = callback;
|
|
|
|
session->callbacks[id].data = userdata;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Start the control session.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param messenger Tox* object.
|
2014-04-28 01:21:26 +08:00
|
|
|
* @param max_calls Amount of calls possible
|
2014-01-25 08:32:33 +08:00
|
|
|
* @return MSISession* The created session.
|
2014-04-17 00:08:44 +08:00
|
|
|
* @retval NULL Error occurred.
|
2014-01-25 08:32:33 +08:00
|
|
|
*/
|
2014-05-26 00:27:48 +08:00
|
|
|
MSISession *msi_init_session ( Messenger *messenger, int32_t max_calls )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-04-28 01:21:26 +08:00
|
|
|
if (messenger == NULL) {
|
|
|
|
LOGGER_ERROR("Could not init session on empty messenger!");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-03 23:13:11 +08:00
|
|
|
TimerHandler *handler = timer_init_session(max_calls * 10, 10000);
|
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
if ( !max_calls || !handler ) {
|
|
|
|
LOGGER_WARNING("Invalid max call treshold or timer handler initialization failed!");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
MSISession *retu = calloc ( sizeof ( MSISession ), 1 );
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
if (retu == NULL) {
|
2014-06-28 10:13:38 +08:00
|
|
|
LOGGER_ERROR("Allocation failed! Program might misbehave!");
|
2014-05-26 00:27:48 +08:00
|
|
|
return NULL;
|
2014-04-28 01:21:26 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
retu->messenger_handle = messenger;
|
|
|
|
retu->agent_handler = NULL;
|
|
|
|
retu->timer_handler = handler;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
if (!(retu->calls = calloc( sizeof (MSICall *), max_calls ))) {
|
2014-06-28 10:13:38 +08:00
|
|
|
LOGGER_ERROR("Allocation failed! Program might misbehave!");
|
2014-07-05 21:11:25 +08:00
|
|
|
free(retu);
|
2014-06-28 10:13:38 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-06-29 10:29:39 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
retu->max_calls = max_calls;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
retu->frequ = 10000; /* default value? */
|
|
|
|
retu->call_timeout = 30000; /* default value? */
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
m_callback_msi_packet(messenger, msi_handle_packet, retu );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-02-16 03:44:33 +08:00
|
|
|
/* This is called when remote terminates session */
|
2014-07-05 21:11:25 +08:00
|
|
|
m_callback_connectionstatus_internal_av(messenger, handle_remote_connection_change, retu);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_init(&retu->mutex, NULL);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
LOGGER_DEBUG("New msi session: %p max calls: %u", retu, max_calls);
|
|
|
|
return retu;
|
2014-01-25 08:32:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Terminate control session.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session The session
|
|
|
|
* @return int
|
|
|
|
*/
|
2014-02-17 09:01:30 +08:00
|
|
|
int msi_terminate_session ( MSISession *session )
|
|
|
|
{
|
2014-04-28 01:21:26 +08:00
|
|
|
if (session == NULL) {
|
|
|
|
LOGGER_ERROR("Tried to terminate non-existing session");
|
|
|
|
return -1;
|
2014-03-23 02:33:56 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-02-17 09:01:30 +08:00
|
|
|
m_callback_msi_packet((struct Messenger *) session->messenger_handle, NULL, NULL);
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
int _status = 0;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
/* If have calls, cancel them */
|
|
|
|
uint32_t idx = 0;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
|
|
|
for (; idx < session->max_calls; idx ++) if ( session->calls[idx] ) {
|
|
|
|
/* Cancel all? */
|
|
|
|
uint16_t _it = 0;
|
|
|
|
/*for ( ; _it < session->calls[idx]->peer_count; _it++ )
|
|
|
|
* FIXME: will not work on multiple peers, must cancel call for all peers
|
|
|
|
*/
|
|
|
|
msi_cancel ( session, idx, session->calls[idx]->peers [_it], "MSI session terminated!" );
|
|
|
|
}
|
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
timer_terminate_session(session->timer_handler);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_destroy(&session->mutex);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_DEBUG("Terminated session: %p", session);
|
2014-01-25 08:32:33 +08:00
|
|
|
free ( session );
|
|
|
|
return _status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Send invite request to friend_id.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session Control session.
|
|
|
|
* @param call_type Type of the call. Audio or Video(both audio and video)
|
|
|
|
* @param rngsec Ringing timeout.
|
|
|
|
* @param friend_id The friend.
|
|
|
|
* @return int
|
|
|
|
*/
|
2014-05-26 00:27:48 +08:00
|
|
|
int msi_invite ( MSISession *session, int32_t *call_index, MSICallType call_type, uint32_t rngsec, uint32_t friend_id )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Inviting friend: %u", session, friend_id);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
int i = 0;
|
2014-07-22 23:20:55 +08:00
|
|
|
|
|
|
|
for (; i < session->max_calls; i ++)
|
2014-07-21 07:10:57 +08:00
|
|
|
if (session->calls[i] && session->calls[i]->peers[0] == friend_id) {
|
|
|
|
LOGGER_ERROR("Already in a call with friend %d", friend_id);
|
|
|
|
pthread_mutex_unlock(&session->mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-07-22 23:20:55 +08:00
|
|
|
|
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
MSICall *call = init_call ( session, 1, rngsec ); /* Just one peer for now */
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( !call ) {
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-05-31 06:29:43 +08:00
|
|
|
LOGGER_ERROR("Cannot handle more calls");
|
|
|
|
return -1;
|
2014-05-24 22:02:01 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
*call_index = call->call_idx;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
t_randomstr ( call->id, sizeof(call->id) );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
add_peer ( call, friend_id );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
call->type_local = call_type;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
MSIMessage *msg_invite = msi_new_message ( TypeRequest, invite );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
msi_msg_set_calltype(msg_invite, call_type);
|
|
|
|
send_message ( session, call, msg_invite, friend_id );
|
|
|
|
free( msg_invite );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
call->state = call_inviting;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
call->request_timer_id = timer_alloc ( session->timer_handler, handle_timeout, session, call->call_idx, m_deftout );
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_DEBUG("Invite sent");
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Hangup active call.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session Control session.
|
2014-04-28 01:21:26 +08:00
|
|
|
* @param call_id To which call is this action handled.
|
2014-02-17 09:01:30 +08:00
|
|
|
* @return int
|
2014-04-17 00:08:44 +08:00
|
|
|
* @retval -1 Error occurred.
|
2014-01-25 08:32:33 +08:00
|
|
|
* @retval 0 Success.
|
|
|
|
*/
|
2014-05-26 00:27:48 +08:00
|
|
|
int msi_hangup ( MSISession *session, int32_t call_index )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Hanging up call: %u", session, call_index);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-17 01:56:40 +08:00
|
|
|
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_ERROR("Invalid call index!");
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-04-28 01:21:26 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( session->calls[call_index]->state != call_active ) {
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_ERROR("No call with such index or call is not active!");
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-01-25 08:32:33 +08:00
|
|
|
return -1;
|
2014-04-28 01:21:26 +08:00
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
MSIMessage *msg_end = msi_new_message ( TypeRequest, end );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
/* hangup for each peer */
|
2014-07-21 07:10:57 +08:00
|
|
|
int it = 0;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
for ( ; it < session->calls[call_index]->peer_count; it ++ )
|
|
|
|
send_message ( session, session->calls[call_index], msg_end, session->calls[call_index]->peers[it] );
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-06-27 05:05:13 +08:00
|
|
|
session->calls[call_index]->state = call_hanged_up;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
free ( msg_end );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-03 23:13:11 +08:00
|
|
|
session->calls[call_index]->request_timer_id =
|
2014-07-08 11:19:56 +08:00
|
|
|
timer_alloc ( session->timer_handler, handle_timeout, session, call_index, m_deftout );
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-01-25 08:32:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Answer active call request.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session Control session.
|
2014-04-28 01:21:26 +08:00
|
|
|
* @param call_id To which call is this action handled.
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param call_type Answer with Audio or Video(both).
|
|
|
|
* @return int
|
|
|
|
*/
|
2014-05-26 00:27:48 +08:00
|
|
|
int msi_answer ( MSISession *session, int32_t call_index, MSICallType call_type )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Answering call: %u", session, call_index);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
|
|
|
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_ERROR("Invalid call index!");
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-04-28 01:21:26 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
MSIMessage *msg_starting = msi_new_message ( TypeResponse, starting );
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
session->calls[call_index]->type_local = call_type;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
msi_msg_set_calltype(msg_starting, call_type);
|
2014-07-24 01:24:51 +08:00
|
|
|
|
|
|
|
send_message ( session, session->calls[call_index], msg_starting, session->calls[call_index]->peers[0] );
|
2014-07-21 07:10:57 +08:00
|
|
|
free ( msg_starting );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
session->calls[call_index]->state = call_active;
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-01-25 08:32:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Cancel request.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session Control session.
|
2014-04-28 01:21:26 +08:00
|
|
|
* @param call_id To which call is this action handled.
|
2014-02-10 06:06:44 +08:00
|
|
|
* @param reason Set optional reason header. Pass NULL if none.
|
2014-01-25 08:32:33 +08:00
|
|
|
* @return int
|
|
|
|
*/
|
2014-05-26 00:27:48 +08:00
|
|
|
int msi_cancel ( MSISession *session, int32_t call_index, uint32_t peer, const char *reason )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Canceling call: %u; reason: %s", session, call_index, reason ? reason : "Unknown");
|
2014-05-26 00:27:48 +08:00
|
|
|
|
|
|
|
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_ERROR("Invalid call index!");
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-04-28 01:21:26 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
MSIMessage *msg_cancel = msi_new_message ( TypeRequest, cancel );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-22 04:11:59 +08:00
|
|
|
/* FIXME */
|
2014-07-22 23:20:55 +08:00
|
|
|
#if 0
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( reason && strlen(reason) < sizeof(MSIReasonStrType) ) {
|
2014-07-22 04:11:59 +08:00
|
|
|
MSIReasonStrType reason_cast;
|
|
|
|
memset(reason_cast, '\0', sizeof(MSIReasonStrType));
|
2014-07-21 07:10:57 +08:00
|
|
|
memcpy(reason_cast, reason, strlen(reason));
|
|
|
|
msi_msg_set_reason(msg_cancel, reason_cast);
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 10:10:05 +08:00
|
|
|
#endif
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
send_message ( session, session->calls[call_index], msg_cancel, peer );
|
|
|
|
free ( msg_cancel );
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-24 22:02:01 +08:00
|
|
|
terminate_call ( session, session->calls[call_index] );
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Reject request.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session Control session.
|
2014-04-28 01:21:26 +08:00
|
|
|
* @param call_id To which call is this action handled.
|
2014-01-25 08:32:33 +08:00
|
|
|
* @return int
|
|
|
|
*/
|
2014-07-21 07:10:57 +08:00
|
|
|
int msi_reject ( MSISession *session, int32_t call_index, const char *reason )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-07-22 04:11:59 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Rejecting call: %u; reason: %s", session, call_index, reason ? reason : "Unknown");
|
2014-05-26 00:27:48 +08:00
|
|
|
|
|
|
|
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
|
2014-04-28 01:21:26 +08:00
|
|
|
LOGGER_ERROR("Invalid call index!");
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-04-28 01:21:26 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
MSIMessage *msg_reject = msi_new_message ( TypeRequest, reject );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-22 04:11:59 +08:00
|
|
|
/* FIXME */
|
|
|
|
#if 0
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-22 04:11:59 +08:00
|
|
|
if ( reason && strlen(reason) < sizeof(MSIReasonStrType) ) {
|
|
|
|
MSIReasonStrType reason_cast;
|
|
|
|
memset(reason_cast, '\0', sizeof(MSIReasonStrType));
|
2014-07-21 07:10:57 +08:00
|
|
|
memcpy(reason_cast, reason, strlen(reason));
|
|
|
|
msi_msg_set_reason(msg_reject, reason_cast);
|
|
|
|
}
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-22 04:11:59 +08:00
|
|
|
#endif
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
send_message ( session, session->calls[call_index], msg_reject,
|
2014-05-26 00:27:48 +08:00
|
|
|
session->calls[call_index]->peers[session->calls[call_index]->peer_count - 1] );
|
2014-07-21 07:10:57 +08:00
|
|
|
free ( msg_reject );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-06-27 05:52:42 +08:00
|
|
|
session->calls[call_index]->state = call_hanged_up;
|
2014-07-03 23:13:11 +08:00
|
|
|
session->calls[call_index]->request_timer_id =
|
2014-07-08 11:19:56 +08:00
|
|
|
timer_alloc ( session->timer_handler, handle_timeout, session, call_index, m_deftout );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-01-25 08:32:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
/**
|
|
|
|
* @brief Send invite request to friend_id.
|
|
|
|
*
|
|
|
|
* @param session Control session.
|
|
|
|
* @param call_index Call index.
|
|
|
|
* @param call_type Type of the call. Audio or Video(both audio and video)
|
|
|
|
* @param rngsec Ringing timeout.
|
|
|
|
* @param friend_id The friend.
|
|
|
|
* @return int
|
|
|
|
*/
|
2014-07-22 23:20:55 +08:00
|
|
|
int msi_change_type(MSISession *session, int32_t call_index, MSICallType call_type)
|
2014-07-21 07:10:57 +08:00
|
|
|
{
|
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_DEBUG("Changing media on call: %d", call_index);
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
|
|
|
|
LOGGER_ERROR("Invalid call index!");
|
|
|
|
pthread_mutex_unlock(&session->mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
MSICall *call = session->calls[call_index];
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( call->state != call_active ) {
|
|
|
|
LOGGER_ERROR("Call is not active!");
|
|
|
|
pthread_mutex_unlock(&session->mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
if ( call->type_local == call_type ) {
|
|
|
|
LOGGER_ERROR("Call is already set to the requested type!");
|
|
|
|
pthread_mutex_unlock(&session->mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
call->type_local = call_type;
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
MSIMessage *msg_invite = msi_new_message ( TypeRequest, invite );
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
msi_msg_set_calltype ( msg_invite, call_type );
|
|
|
|
send_message ( session, call, msg_invite, call->peers[0] );
|
|
|
|
free ( msg_invite );
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-21 07:10:57 +08:00
|
|
|
LOGGER_DEBUG("Request for media change sent");
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-07-22 23:20:55 +08:00
|
|
|
|
2014-01-25 08:32:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Terminate the current call.
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-01-25 08:32:33 +08:00
|
|
|
* @param session Control session.
|
2014-04-28 01:21:26 +08:00
|
|
|
* @param call_id To which call is this action handled.
|
2014-01-25 08:32:33 +08:00
|
|
|
* @return int
|
|
|
|
*/
|
2014-05-26 00:27:48 +08:00
|
|
|
int msi_stopcall ( MSISession *session, int32_t call_index )
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_lock(&session->mutex);
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_DEBUG("Session: %p Stopping call index: %u", session, call_index);
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-05-24 22:02:01 +08:00
|
|
|
if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-01-25 08:32:33 +08:00
|
|
|
return -1;
|
2014-05-24 22:02:01 +08:00
|
|
|
}
|
2014-01-25 08:32:33 +08:00
|
|
|
|
|
|
|
/* just terminate it */
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
terminate_call ( session, session->calls[call_index] );
|
2014-01-25 08:32:33 +08:00
|
|
|
|
2014-07-05 23:36:12 +08:00
|
|
|
pthread_mutex_unlock(&session->mutex);
|
2014-01-25 08:32:33 +08:00
|
|
|
return 0;
|
2014-04-17 00:08:44 +08:00
|
|
|
}
|