summaryrefslogtreecommitdiffstats
path: root/tinySIP/src/dialogs/tsip_dialog_invite.hold.c
blob: e0558fcc2b697ea1a351f9c6fcf6c625b2c36c12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as publishd by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO 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 DOUBANGO.
*
*/

/**@file tsip_dialog_invite.hold.c
 * @brief Communication Hold (3GPP TS 24.610)
 * The Communication Hold supplementary service enables a user to suspend the reception of media stream(s) of an established IP multimedia session,
 * and resume the media stream(s) at a later time.
 *
 * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
 *

 */
#include "tinysip/dialogs/tsip_dialog_invite.h"

#include "tinysip/dialogs/tsip_dialog_invite.common.h"

#include "tsk_debug.h"

/* ======================== transitions ======================== */
static int x0100_Connected_2_Holding_X_oHold(va_list *app);
static int x0101_Holding_2_Connected_X_ixxx(va_list *app);
static int x0102_Connected_2_Resuming_X_oResume(va_list *app);
static int x0103_Resuming_2_Connected_X_ixxx(va_list *app);

static int x0150_Any_2_Any_X_i422(va_list *app);

/* ======================== conds ======================== */
static tsk_bool_t _fsm_cond_is_resp2INVITEorUPDATE(tsip_dialog_invite_t* self, tsip_message_t* message)
{
    return (TSIP_RESPONSE_IS_TO_INVITE(message) || TSIP_RESPONSE_IS_TO_UPDATE(message));
}

/* ======================== external functions ======================== */
extern int send_INVITEorUPDATE(tsip_dialog_invite_t *self, tsk_bool_t is_INVITE, tsk_bool_t force_sdp);
extern int tsip_dialog_invite_process_ro(tsip_dialog_invite_t *self, const tsip_message_t* message);
extern int send_ACK(tsip_dialog_invite_t *self, const tsip_response_t* r2xxINVITE);

int tsip_dialog_invite_hold_init(tsip_dialog_invite_t *self)
{
    tsk_fsm_set(TSIP_DIALOG_GET_FSM(self),

                /*=======================
                * === Hold ===
                */
                // Connected -> (send HOLD) -> Holding
                TSK_FSM_ADD_ALWAYS(_fsm_state_Connected, _fsm_action_oHold, _fsm_state_Holding, x0100_Connected_2_Holding_X_oHold, "x0100_Connected_2_Holding_X_oHold"),
                // Holding -> (i2xx) -> Connected
                TSK_FSM_ADD(_fsm_state_Holding, _fsm_action_i2xx, _fsm_cond_is_resp2INVITEorUPDATE, _fsm_state_Connected, x0101_Holding_2_Connected_X_ixxx, "x0101_Holding_2_Connected_X_ixxx"),
                // Holding -> (i300-699) -> Connected
                TSK_FSM_ADD(_fsm_state_Holding, _fsm_action_i300_to_i699, _fsm_cond_is_resp2INVITEorUPDATE, _fsm_state_Connected, x0101_Holding_2_Connected_X_ixxx, "x0101_Holding_2_Connected_X_ixxx"),

                /*=======================
                * === Resume ===
                */
                // Connected -> (send RESUME) -> Resuming
                TSK_FSM_ADD_ALWAYS(_fsm_state_Connected, _fsm_action_oResume, _fsm_state_Resuming, x0102_Connected_2_Resuming_X_oResume, "x0102_Connected_2_Resuming_X_oResume"),
                // Resuming -> (i2xx) -> Connected
                TSK_FSM_ADD(_fsm_state_Resuming, _fsm_action_i2xx, _fsm_cond_is_resp2INVITEorUPDATE, _fsm_state_Connected, x0103_Resuming_2_Connected_X_ixxx, "x0103_Resuming_2_Connected_X_ixxx"),
                // Resuming -> (i300-699) -> Connected
                TSK_FSM_ADD(_fsm_state_Resuming, _fsm_action_i300_to_i699, _fsm_cond_is_resp2INVITEorUPDATE, _fsm_state_Connected, x0103_Resuming_2_Connected_X_ixxx, "x0103_Resuming_2_Connected_X_ixxx"),

                TSK_FSM_ADD_NULL());

    return 0;
}



//--------------------------------------------------------
//				== STATE MACHINE BEGIN ==
//--------------------------------------------------------

// Connected -> (send HOLD) -> Holding
int x0100_Connected_2_Holding_X_oHold(va_list *app)
{
    int ret;
    tsip_dialog_invite_t *self;
    const tsip_action_t* action;

    self = va_arg(*app, tsip_dialog_invite_t *);
    va_arg(*app, const tsip_message_t *);
    action = va_arg(*app, const tsip_action_t *);

    if(!self->msession_mgr) {
        TSK_DEBUG_WARN("Media Session manager is Null");
        return 0;
    }

    /* put on hold */
    ret = tmedia_session_mgr_hold(self->msession_mgr, action->media.type);

    /* send the request */
    if((ret = send_INVITE(self, tsk_false))) {
        // FIXME: signal error without breaking the state machine
    }

    return 0;
}

// Holding -> (ixxx) -> Connected
int x0101_Holding_2_Connected_X_ixxx(va_list *app)
{
    int ret;

    tsip_dialog_invite_t* self = va_arg(*app, tsip_dialog_invite_t *);
    const tsip_response_t* response = va_arg(*app, const tsip_response_t *);

    /* reset current action */
    tsip_dialog_set_curr_action(TSIP_DIALOG(self), tsk_null);

    /* Process remote offer */
    if((ret = tsip_dialog_invite_process_ro(self, response))) {
        /* Send error */
        return ret;
    }
    else if(TSIP_RESPONSE_IS_TO_INVITE(response)) {
        /* send ACK */
        ret = send_ACK(self, response);
    }

    /* alert the user */
    if(TSIP_RESPONSE_IS_2XX(response)) {
        TSIP_DIALOG_INVITE_SIGNAL(self, tsip_m_local_hold_ok,
                                  TSIP_RESPONSE_CODE(response), TSIP_RESPONSE_PHRASE(response), response);
        self->hold.local = tsk_true;
    }
    else {
        TSIP_DIALOG_INVITE_SIGNAL(self, tsip_m_local_hold_nok,
                                  TSIP_RESPONSE_CODE(response), TSIP_RESPONSE_PHRASE(response), response);
        self->hold.local = tsk_false;
    }

    return ret;
}

// Connected -> (send RESUME) -> Resuming
int x0102_Connected_2_Resuming_X_oResume(va_list *app)
{
    int ret;
    tsip_dialog_invite_t *self;
    const tsip_action_t* action;

    self = va_arg(*app, tsip_dialog_invite_t *);
    va_arg(*app, const tsip_message_t *);
    action = va_arg(*app, const tsip_action_t *);

    if(!self->msession_mgr) {
        TSK_DEBUG_WARN("Media Session manager is Null");
        return 0;
    }

    /* Resume both */
    ret = tmedia_session_mgr_resume(self->msession_mgr, action->media.type, tsk_true);
    ret = tmedia_session_mgr_resume(self->msession_mgr, action->media.type, tsk_false);

    /* send the request */
    if((ret = send_INVITE(self, tsk_false))) {
        // FIXME: signal error without breaking the state machine
    }

    return 0;
}

// Resuming -> (ixxx) -> Connected
int x0103_Resuming_2_Connected_X_ixxx(va_list *app)
{
    int ret;

    tsip_dialog_invite_t* self = va_arg(*app, tsip_dialog_invite_t *);
    const tsip_response_t* response = va_arg(*app, const tsip_response_t *);

    /* reset current action */
    tsip_dialog_set_curr_action(TSIP_DIALOG(self), tsk_null);

    /* Process remote offer */
    if((ret = tsip_dialog_invite_process_ro(self, response))) {
        /* Send error */
        return ret;
    }
    else if(TSIP_RESPONSE_IS_TO_INVITE(response)) {
        /* send ACK */
        ret = send_ACK(self, response);
    }

    /* alert the user */
    if(TSIP_RESPONSE_IS_2XX(response)) {
        TSIP_DIALOG_INVITE_SIGNAL(self, tsip_m_local_resume_ok,
                                  TSIP_RESPONSE_CODE(response), TSIP_RESPONSE_PHRASE(response), response);
        self->hold.local = tsk_false;
    }
    else {
        TSIP_DIALOG_INVITE_SIGNAL(self, tsip_m_local_resume_nok,
                                  TSIP_RESPONSE_CODE(response), TSIP_RESPONSE_PHRASE(response), response);
        self->hold.local = tsk_true;
    }

    return ret;
}

/* handle requests/responses (MUST be called after set_ro()) */
int tsip_dialog_invite_hold_handle(tsip_dialog_invite_t* self, const tsip_request_t* rINVITEorUPDATE)
{
    tsk_bool_t remote_hold, bodiless_invite;
    int ret = 0;

    if(!self || !rINVITEorUPDATE || !self->msession_mgr) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }

    remote_hold = tmedia_session_mgr_is_held(self->msession_mgr, self->msession_mgr->type, tsk_false);

    // resume the call if we receive bodiless INVITE
    bodiless_invite = !TSIP_MESSAGE_HAS_CONTENT(rINVITEorUPDATE) && TSIP_REQUEST_IS_INVITE(rINVITEorUPDATE);
    if(bodiless_invite && remote_hold) {
        // resume remote
        if((ret = tmedia_session_mgr_resume(self->msession_mgr, self->msession_mgr->type, tsk_false)) == 0) {
            remote_hold = tsk_false;
        }
    }

    if(ret == 0 && (remote_hold != self->hold.remote)) {
        self->hold.remote = remote_hold;
        TSIP_DIALOG_INVITE_SIGNAL(self, self->hold.remote ? tsip_m_remote_hold : tsip_m_remote_resume,
                                  tsip_event_code_dialog_request_incoming, "Hold/Resume state changed", rINVITEorUPDATE);
    }

    return ret;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//				== STATE MACHINE END ==
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
OpenPOWER on IntegriCloud