diff options
Diffstat (limited to 'toxav/msi.c')
-rwxr-xr-x | toxav/msi.c | 156 |
1 files changed, 68 insertions, 88 deletions
diff --git a/toxav/msi.c b/toxav/msi.c index 8d4e6964..c2d50302 100755 --- a/toxav/msi.c +++ b/toxav/msi.c | |||
@@ -122,7 +122,7 @@ static struct _Callbacks { | |||
122 | void* data; | 122 | void* data; |
123 | } callbacks[11] = {0}; | 123 | } callbacks[11] = {0}; |
124 | 124 | ||
125 | inline__ void invoke_callback(uint32_t call_index, MSICallbackID id) | 125 | inline__ void invoke_callback(int32_t call_index, MSICallbackID id) |
126 | { | 126 | { |
127 | /*if ( callbacks[id].function ) event.rise ( callbacks[id].function, callbacks[id].data );*/ | 127 | /*if ( callbacks[id].function ) event.rise ( callbacks[id].function, callbacks[id].data );*/ |
128 | if ( callbacks[id].function ) { | 128 | if ( callbacks[id].function ) { |
@@ -209,18 +209,7 @@ static inline__ const uint8_t *stringify_response ( MSIResponse response ) | |||
209 | } | 209 | } |
210 | 210 | ||
211 | 211 | ||
212 | #define ON_HEADER(iterator, header, descriptor, size_const) \ | 212 | |
213 | ( memcmp(iterator, descriptor, size_const) == 0){ /* Okay */ \ | ||
214 | iterator += size_const; /* Set iterator at begining of value part */ \ | ||
215 | if ( *iterator != value_byte ) { assert(0); return -1; }\ | ||
216 | iterator ++;\ | ||
217 | uint16_t _value_size = (uint16_t) *(iterator ) << 8 | \ | ||
218 | (uint16_t) *(iterator + 1); \ | ||
219 | header.header_value = calloc(sizeof(uint8_t), _value_size); \ | ||
220 | header.size = _value_size; \ | ||
221 | memcpy(header.header_value, iterator + 2, _value_size);\ | ||
222 | iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte */ \ | ||
223 | } | ||
224 | 213 | ||
225 | /** | 214 | /** |
226 | * @brief Parse raw 'data' received from socket into MSIMessage struct. | 215 | * @brief Parse raw 'data' received from socket into MSIMessage struct. |
@@ -235,6 +224,19 @@ static inline__ const uint8_t *stringify_response ( MSIResponse response ) | |||
235 | */ | 224 | */ |
236 | int parse_raw_data ( MSIMessage *msg, const uint8_t *data, uint16_t length ) | 225 | int parse_raw_data ( MSIMessage *msg, const uint8_t *data, uint16_t length ) |
237 | { | 226 | { |
227 | |||
228 | #define ON_HEADER(iterator, header, descriptor, size_const) \ | ||
229 | ( memcmp(iterator, descriptor, size_const) == 0){ /* Okay */ \ | ||
230 | iterator += size_const; /* Set iterator at begining of value part */ \ | ||
231 | if ( *iterator != value_byte ) { assert(0); return -1; }\ | ||
232 | iterator ++;\ | ||
233 | uint16_t _value_size = (uint16_t) *(iterator ) << 8 | \ | ||
234 | (uint16_t) *(iterator + 1); \ | ||
235 | header.header_value = calloc(sizeof(uint8_t), _value_size); \ | ||
236 | header.size = _value_size; \ | ||
237 | memcpy(header.header_value, iterator + 2, _value_size);\ | ||
238 | iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte */ } | ||
239 | |||
238 | if ( msg == NULL ) { | 240 | if ( msg == NULL ) { |
239 | LOGGER_ERROR("Could not parse message: no storage!"); | 241 | LOGGER_ERROR("Could not parse message: no storage!"); |
240 | } | 242 | } |
@@ -537,10 +539,6 @@ uint8_t *append_header_to_string ( | |||
537 | } | 539 | } |
538 | 540 | ||
539 | 541 | ||
540 | #define CLEAN_ASSIGN(added, var, field, header)\ | ||
541 | if ( header.header_value ) { var = append_header_to_string(var, (const uint8_t*)field, header.header_value, header.size, &added); } | ||
542 | |||
543 | |||
544 | /** | 542 | /** |
545 | * @brief Convert MSIMessage struct to _sendable_ string. | 543 | * @brief Convert MSIMessage struct to _sendable_ string. |
546 | * | 544 | * |
@@ -550,6 +548,9 @@ if ( header.header_value ) { var = append_header_to_string(var, (const uint8_t*) | |||
550 | */ | 548 | */ |
551 | uint16_t message_to_send ( MSIMessage *msg, uint8_t *dest ) | 549 | uint16_t message_to_send ( MSIMessage *msg, uint8_t *dest ) |
552 | { | 550 | { |
551 | #define CLEAN_ASSIGN(added, var, field, header)\ | ||
552 | if ( header.header_value ) { var = append_header_to_string(var, (const uint8_t*)field, header.header_value, header.size, &added); } | ||
553 | |||
553 | if (msg == NULL) { | 554 | if (msg == NULL) { |
554 | LOGGER_ERROR("Empty message!"); | 555 | LOGGER_ERROR("Empty message!"); |
555 | return 0; | 556 | return 0; |
@@ -857,35 +858,6 @@ int has_call_error ( MSISession *session, MSICall* call, MSIMessage *msg ) | |||
857 | 858 | ||
858 | 859 | ||
859 | /** | 860 | /** |
860 | * @brief Function called at request timeout. If not called in thread it might cause trouble | ||
861 | * | ||
862 | * @param arg Control session | ||
863 | * @return void* | ||
864 | */ | ||
865 | void *handle_timeout ( void *arg ) | ||
866 | { | ||
867 | /* TODO: Cancel might not arrive there; set up | ||
868 | * timers on these cancels and terminate call on | ||
869 | * their timeout | ||
870 | */ | ||
871 | MSICall *_call = arg; | ||
872 | |||
873 | LOGGER_DEBUG("[Call: %s] Request timed out!", _call->id); | ||
874 | |||
875 | invoke_callback(_call->call_idx, MSI_OnRequestTimeout); | ||
876 | |||
877 | if ( _call && _call->session ) { | ||
878 | |||
879 | /* TODO: Cancel all? */ | ||
880 | /* uint16_t _it = 0; | ||
881 | for ( ; _it < _session->call->peer_count; _it++ ) */ | ||
882 | msi_cancel ( _call->session, _call->call_idx, _call->peers [0], "Request timed out" ); | ||
883 | } | ||
884 | |||
885 | pthread_exit(NULL); | ||
886 | } | ||
887 | |||
888 | /** | ||
889 | * @brief Add peer to peer list. | 861 | * @brief Add peer to peer list. |
890 | * | 862 | * |
891 | * @param call What call. | 863 | * @param call What call. |
@@ -903,6 +875,8 @@ void add_peer( MSICall *call, int peer_id ) | |||
903 | } | 875 | } |
904 | 876 | ||
905 | call->peers[call->peer_count - 1] = peer_id; | 877 | call->peers[call->peer_count - 1] = peer_id; |
878 | |||
879 | LOGGER_DEBUG("Added peer: %d", peer_id); | ||
906 | } | 880 | } |
907 | 881 | ||
908 | 882 | ||
@@ -922,7 +896,7 @@ MSICall *init_call ( MSISession *session, int peers, int ringing_timeout ) | |||
922 | return NULL; | 896 | return NULL; |
923 | } | 897 | } |
924 | 898 | ||
925 | uint32_t _call_idx = 0; | 899 | int32_t _call_idx = 0; |
926 | for (; _call_idx < session->max_calls; _call_idx ++) { | 900 | for (; _call_idx < session->max_calls; _call_idx ++) { |
927 | if ( !session->calls[_call_idx] ) { | 901 | if ( !session->calls[_call_idx] ) { |
928 | session->calls[_call_idx] = calloc ( sizeof ( MSICall ), 1 ); | 902 | session->calls[_call_idx] = calloc ( sizeof ( MSICall ), 1 ); |
@@ -1015,6 +989,37 @@ int terminate_call ( MSISession *session, MSICall *call ) | |||
1015 | } | 989 | } |
1016 | 990 | ||
1017 | 991 | ||
992 | /** | ||
993 | * @brief Function called at request timeout. If not called in thread it might cause trouble | ||
994 | * | ||
995 | * @param arg Control session | ||
996 | * @return void* | ||
997 | */ | ||
998 | void *handle_timeout ( void *arg ) | ||
999 | { | ||
1000 | /* TODO: Cancel might not arrive there; set up | ||
1001 | * timers on these cancels and terminate call on | ||
1002 | * their timeout | ||
1003 | */ | ||
1004 | MSICall *_call = arg; | ||
1005 | |||
1006 | LOGGER_DEBUG("[Call: %s] Request timed out!", _call->id); | ||
1007 | |||
1008 | invoke_callback(_call->call_idx, MSI_OnRequestTimeout); | ||
1009 | |||
1010 | if ( _call && _call->session ) { | ||
1011 | |||
1012 | /* TODO: Cancel all? */ | ||
1013 | /* uint16_t _it = 0; | ||
1014 | * for ( ; _it < _session->call->peer_count; _it++ ) */ | ||
1015 | msi_cancel ( _call->session, _call->call_idx, _call->peers [0], "Request timed out" ); | ||
1016 | terminate_call(_call->session, _call); | ||
1017 | } | ||
1018 | |||
1019 | pthread_exit(NULL); | ||
1020 | } | ||
1021 | |||
1022 | |||
1018 | /********** Request handlers **********/ | 1023 | /********** Request handlers **********/ |
1019 | int handle_recv_invite ( MSISession *session, MSICall* call, MSIMessage *msg ) | 1024 | int handle_recv_invite ( MSISession *session, MSICall* call, MSIMessage *msg ) |
1020 | { | 1025 | { |
@@ -1072,7 +1077,7 @@ int handle_recv_invite ( MSISession *session, MSICall* call, MSIMessage *msg ) | |||
1072 | } | 1077 | } |
1073 | int handle_recv_start ( MSISession *session, MSICall* call, MSIMessage *msg ) | 1078 | int handle_recv_start ( MSISession *session, MSICall* call, MSIMessage *msg ) |
1074 | { | 1079 | { |
1075 | LOGGER_DEBUG("Session: %p Handling 'start' on call: %s", session, call->id ); | 1080 | LOGGER_DEBUG("Session: %p Handling 'start' on call: %s, friend id: %d", session, call->id, msg->friend_id ); |
1076 | 1081 | ||
1077 | if ( has_call_error ( session, call, msg ) == 0 ) | 1082 | if ( has_call_error ( session, call, msg ) == 0 ) |
1078 | return -1; | 1083 | return -1; |
@@ -1125,10 +1130,10 @@ int handle_recv_cancel ( MSISession *session, MSICall* call, MSIMessage *msg ) | |||
1125 | return 0; | 1130 | return 0; |
1126 | 1131 | ||
1127 | /* Act as end message */ | 1132 | /* Act as end message */ |
1128 | 1133 | /* | |
1129 | MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) ); | 1134 | MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) ); |
1130 | send_message ( session, call, _msg_ending, msg->friend_id ); | 1135 | send_message ( session, call, _msg_ending, msg->friend_id ); |
1131 | free_message ( _msg_ending ); | 1136 | free_message ( _msg_ending );*/ |
1132 | 1137 | ||
1133 | invoke_callback(call->call_idx, MSI_OnCancel); | 1138 | invoke_callback(call->call_idx, MSI_OnCancel); |
1134 | 1139 | ||
@@ -1383,31 +1388,6 @@ void msi_handle_packet ( Messenger *messenger, int source, uint8_t *data, uint16 | |||
1383 | } | 1388 | } |
1384 | 1389 | ||
1385 | 1390 | ||
1386 | /******************************************************************************************************************** | ||
1387 | * ******************************************************************************************************************* | ||
1388 | ******************************************************************************************************************** | ||
1389 | ******************************************************************************************************************** | ||
1390 | ******************************************************************************************************************** | ||
1391 | * | ||
1392 | * | ||
1393 | * | ||
1394 | * PUBLIC API FUNCTIONS IMPLEMENTATIONS | ||
1395 | * | ||
1396 | * | ||
1397 | * | ||
1398 | ******************************************************************************************************************** | ||
1399 | ******************************************************************************************************************** | ||
1400 | ******************************************************************************************************************** | ||
1401 | ******************************************************************************************************************** | ||
1402 | ********************************************************************************************************************/ | ||
1403 | |||
1404 | |||
1405 | |||
1406 | |||
1407 | |||
1408 | |||
1409 | |||
1410 | |||
1411 | /** | 1391 | /** |
1412 | * @brief Callback setter. | 1392 | * @brief Callback setter. |
1413 | * | 1393 | * |
@@ -1430,7 +1410,7 @@ void msi_register_callback ( MSICallback callback, MSICallbackID id, void* userd | |||
1430 | * @return MSISession* The created session. | 1410 | * @return MSISession* The created session. |
1431 | * @retval NULL Error occured. | 1411 | * @retval NULL Error occured. |
1432 | */ | 1412 | */ |
1433 | MSISession *msi_init_session ( Messenger* messenger, uint32_t max_calls ) | 1413 | MSISession *msi_init_session ( Messenger* messenger, int32_t max_calls ) |
1434 | { | 1414 | { |
1435 | if (messenger == NULL) { | 1415 | if (messenger == NULL) { |
1436 | LOGGER_ERROR("Could not init session on empty messenger!"); | 1416 | LOGGER_ERROR("Could not init session on empty messenger!"); |
@@ -1507,13 +1487,13 @@ int msi_terminate_session ( MSISession *session ) | |||
1507 | * @param friend_id The friend. | 1487 | * @param friend_id The friend. |
1508 | * @return int | 1488 | * @return int |
1509 | */ | 1489 | */ |
1510 | int msi_invite ( MSISession* session, uint32_t* call_index, MSICallType call_type, uint32_t rngsec, uint32_t friend_id ) | 1490 | int msi_invite ( MSISession* session, int32_t* call_index, MSICallType call_type, uint32_t rngsec, uint32_t friend_id ) |
1511 | { | 1491 | { |
1512 | LOGGER_DEBUG("Session: %p Inviting friend: %u", session, friend_id); | 1492 | LOGGER_DEBUG("Session: %p Inviting friend: %u", session, friend_id); |
1513 | 1493 | ||
1514 | MSIMessage *_msg_invite = msi_new_message ( TYPE_REQUEST, stringify_request ( invite ) ); | 1494 | MSIMessage *_msg_invite = msi_new_message ( TYPE_REQUEST, stringify_request ( invite ) ); |
1515 | 1495 | ||
1516 | MSICall* _call = init_call ( session, 1, rngsec ); /* Just one for now */ | 1496 | MSICall* _call = init_call ( session, 1, rngsec ); /* Just one peer for now */ |
1517 | if ( !_call ) return -1; /* Cannot handle more calls */ | 1497 | if ( !_call ) return -1; /* Cannot handle more calls */ |
1518 | 1498 | ||
1519 | *call_index = _call->call_idx; | 1499 | *call_index = _call->call_idx; |
@@ -1553,11 +1533,11 @@ int msi_invite ( MSISession* session, uint32_t* call_index, MSICallType call_typ | |||
1553 | * @retval -1 Error occured. | 1533 | * @retval -1 Error occured. |
1554 | * @retval 0 Success. | 1534 | * @retval 0 Success. |
1555 | */ | 1535 | */ |
1556 | int msi_hangup ( MSISession* session, uint32_t call_index ) | 1536 | int msi_hangup ( MSISession* session, int32_t call_index ) |
1557 | { | 1537 | { |
1558 | LOGGER_DEBUG("Session: %p Hanging up call: %u", session, call_index); | 1538 | LOGGER_DEBUG("Session: %p Hanging up call: %u", session, call_index); |
1559 | 1539 | ||
1560 | if ( call_index >= session->max_calls || !session->calls[call_index] ) { | 1540 | if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) { |
1561 | LOGGER_ERROR("Invalid call index!"); | 1541 | LOGGER_ERROR("Invalid call index!"); |
1562 | return -1; | 1542 | return -1; |
1563 | } | 1543 | } |
@@ -1592,11 +1572,11 @@ int msi_hangup ( MSISession* session, uint32_t call_index ) | |||
1592 | * @param call_type Answer with Audio or Video(both). | 1572 | * @param call_type Answer with Audio or Video(both). |
1593 | * @return int | 1573 | * @return int |
1594 | */ | 1574 | */ |
1595 | int msi_answer ( MSISession* session, uint32_t call_index, MSICallType call_type ) | 1575 | int msi_answer ( MSISession* session, int32_t call_index, MSICallType call_type ) |
1596 | { | 1576 | { |
1597 | LOGGER_DEBUG("Session: %p Answering call: %u", session, call_index); | 1577 | LOGGER_DEBUG("Session: %p Answering call: %u", session, call_index); |
1598 | 1578 | ||
1599 | if ( call_index >= session->max_calls || !session->calls[call_index] ){ | 1579 | if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ){ |
1600 | LOGGER_ERROR("Invalid call index!"); | 1580 | LOGGER_ERROR("Invalid call index!"); |
1601 | return -1; | 1581 | return -1; |
1602 | } | 1582 | } |
@@ -1641,11 +1621,11 @@ int msi_answer ( MSISession* session, uint32_t call_index, MSICallType call_type | |||
1641 | * @param reason Set optional reason header. Pass NULL if none. | 1621 | * @param reason Set optional reason header. Pass NULL if none. |
1642 | * @return int | 1622 | * @return int |
1643 | */ | 1623 | */ |
1644 | int msi_cancel ( MSISession *session, uint32_t call_index, uint32_t peer, const char *reason ) | 1624 | int msi_cancel ( MSISession* session, int32_t call_index, uint32_t peer, const char* reason ) |
1645 | { | 1625 | { |
1646 | LOGGER_DEBUG("Session: %p Canceling call: %u; reason:", session, call_index, reason? reason : "Unknown"); | 1626 | LOGGER_DEBUG("Session: %p Canceling call: %u; reason:", session, call_index, reason? reason : "Unknown"); |
1647 | 1627 | ||
1648 | if ( call_index >= session->max_calls || !session->calls[call_index] ){ | 1628 | if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ){ |
1649 | LOGGER_ERROR("Invalid call index!"); | 1629 | LOGGER_ERROR("Invalid call index!"); |
1650 | return -1; | 1630 | return -1; |
1651 | } | 1631 | } |
@@ -1670,11 +1650,11 @@ int msi_cancel ( MSISession *session, uint32_t call_index, uint32_t peer, const | |||
1670 | * @param call_id To which call is this action handled. | 1650 | * @param call_id To which call is this action handled. |
1671 | * @return int | 1651 | * @return int |
1672 | */ | 1652 | */ |
1673 | int msi_reject ( MSISession *session, uint32_t call_index, const uint8_t *reason ) | 1653 | int msi_reject ( MSISession* session, int32_t call_index, const uint8_t* reason ) |
1674 | { | 1654 | { |
1675 | LOGGER_DEBUG("Session: %p Rejecting call: %u; reason:", session, call_index, reason? (char*)reason : "Unknown"); | 1655 | LOGGER_DEBUG("Session: %p Rejecting call: %u; reason:", session, call_index, reason? (char*)reason : "Unknown"); |
1676 | 1656 | ||
1677 | if ( call_index >= session->max_calls || !session->calls[call_index] ){ | 1657 | if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ){ |
1678 | LOGGER_ERROR("Invalid call index!"); | 1658 | LOGGER_ERROR("Invalid call index!"); |
1679 | return -1; | 1659 | return -1; |
1680 | } | 1660 | } |
@@ -1699,11 +1679,11 @@ int msi_reject ( MSISession *session, uint32_t call_index, const uint8_t *reason | |||
1699 | * @param call_id To which call is this action handled. | 1679 | * @param call_id To which call is this action handled. |
1700 | * @return int | 1680 | * @return int |
1701 | */ | 1681 | */ |
1702 | int msi_stopcall ( MSISession *session, uint32_t call_index ) | 1682 | int msi_stopcall ( MSISession* session, int32_t call_index ) |
1703 | { | 1683 | { |
1704 | LOGGER_DEBUG("Session: %p Stopping call index: %u", session, call_index); | 1684 | LOGGER_DEBUG("Session: %p Stopping call index: %u", session, call_index); |
1705 | 1685 | ||
1706 | if ( call_index >= session->max_calls || !session->calls[call_index] ) | 1686 | if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) |
1707 | return -1; | 1687 | return -1; |
1708 | 1688 | ||
1709 | /* just terminate it */ | 1689 | /* just terminate it */ |