sipbuddy.h 6.26 KB
Newer Older
Eloi Bail's avatar
Eloi Bail committed
1
2
3
/*
 *  Copyright (C) 2012, 2013 LOTES TM LLC
 *  Author : Andrey Loukhnov <aol.nnov@gmail.com>
4
 *  Author : Patrick Keroulas <patrick.keroulas@savoirfairelinux.com>
Eloi Bail's avatar
Eloi Bail committed
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
 *  This file is a part of pult5-voip
 *
 *  pult5-voip 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.
 *
 *  pult5-voip 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 this programm. If not, see <http://www.gnu.org/licenses/>.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify pult5-voip, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, LOTES-TM LLC
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */

#ifndef SIPBUDDY_H
33
#define    SIPBUDDY_H
Eloi Bail's avatar
Eloi Bail committed
34
35
36
37
38
39
40
41
42
43
44
45

#include <pjsip-simple/presence.h>
#include <pj/timer.h>
#include <pj/pool.h>
#include <string>

#include <pjsip-simple/evsub.h>
#include <pjsip-simple/evsub_msg.h>
#include <pjsip/sip_endpoint.h>
#include <pjsip/sip_transport.h>


46
class SIPAccount;
Eloi Bail's avatar
Eloi Bail committed
47

48
49
50
/**
 * Transaction functions of event subscription client side.
 */
51
52
static void buddy_evsub_on_state(pjsip_evsub *sub, pjsip_event *event);
static void buddy_evsub_on_tsx_state(pjsip_evsub *sub,
53
54
                                     pjsip_transaction *tsx,
                                     pjsip_event *event);
55
static void buddy_evsub_on_rx_notify(pjsip_evsub *sub,
56
57
58
59
60
                                     pjsip_rx_data *rdata,
                                     int *p_st_code,
                                     pj_str_t **p_st_text,
                                     pjsip_hdr *res_hdr,
                                     pjsip_msg_body **p_body);
Eloi Bail's avatar
Eloi Bail committed
61
62
static void buddy_timer_cb(pj_timer_heap_t *th, pj_timer_entry *entry);

63

Eloi Bail's avatar
Eloi Bail committed
64
class SIPBuddy {
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
    public:
        /**
         * Constructor
         * @param uri   SIP uri of remote user that we want to subscribe,
         */
        SIPBuddy(const std::string &uri, SIPAccount *acc);
        /**
         * Destructor.
         * Process the the unsubscription before the destruction.
         */
        ~SIPBuddy();
        /**
         * Compare with another buddy's uris.
         * @param b     Other buddy pointer
         */
        bool match(SIPBuddy *b);
        /**
         * The PBX must approve the subrciption before the buddy is added in the buddy list.
         */
        void accept();
        /**
         * Send a SUBCRIBE to the PXB or directly to a buddy in the IP2IP context.
         */
        bool subscribe();
        /**
         * Send a SUBCRIBE to the PXB or directly to a buddy in the IP2IP context but
         * the 0s timeout make the dialog expire immediatly.
         */
        bool unsubscribe();
        /**
         * Return  the monitor variable.
         */
        bool isSubscribed();
        /**
         * Return the buddy URI
         */
        std::string getURI();
Eloi Bail's avatar
Eloi Bail committed
103

104

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        friend void buddy_evsub_on_state(pjsip_evsub *sub, pjsip_event *event);
        friend void buddy_evsub_on_tsx_state(pjsip_evsub *sub,
                                             pjsip_transaction *tsx,
                                             pjsip_event *event);
        friend void buddy_evsub_on_rx_notify(pjsip_evsub *sub,
                                             pjsip_rx_data *rdata,
                                             int *p_st_code,
                                             pj_str_t **p_st_text,
                                             pjsip_hdr *res_hdr,
                                             pjsip_msg_body **p_body);
        friend void buddy_timer_cb(pj_timer_heap_t *th, pj_timer_entry *entry);
        void incLock() {
            lock_count++;
        }
        void decLock() {
            lock_count--;
        }
Eloi Bail's avatar
Eloi Bail committed
122

123
    private:
Eloi Bail's avatar
Eloi Bail committed
124

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
        /**
         * Plan a retry or a renew a subscription.
         * @param reschedule    Allow for reschedule.
         * @param msec          Delay value in milliseconds.
         */
        void rescheduleTimer(bool reschedule, unsigned msec);
        /**
         * Callback after a presence notification was received.
         * Tranfert info to the SIP account.
         */
        pj_status_t reportPresence();
        /**
         * Process the un/subscribe request transmission.
         */
        pj_status_t updateSubscription();
        /*
         * Compare the reason of a transaction end with the given string.
         */
        bool isTermReason(std::string);
        /**
         * return the code after a transaction is terminated.
         */
        unsigned getTermCode();
Eloi Bail's avatar
Eloi Bail committed
148

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
        SIPAccount          *acc;       /**< Associated SIP account pointer */
        pj_str_t         uri;        /**< Buddy URI.            */
        pj_str_t         contact;   /**< Contact learned from subscrp.    */
        pj_str_t         display;   /**< Buddy display name.        */
        pjsip_dialog    *dlg;        /**< The underlying dialog.        */
        pj_bool_t         monitor;   /**< Should we monitor?        */
        pj_str_t         name;        /**< Buddy name.            */
        pj_caching_pool      cp_;
        pj_pool_t        *pool;        /**< Pool for this buddy.        */
        pjsip_pres_status     status;    /**< Buddy presence status.        */
        pjsip_evsub        *sub;        /**< Buddy presence subscription    */
        unsigned         term_code; /**< Subscription termination code    */
        pj_str_t         term_reason;/**< Subscription termination reason */
        pj_timer_entry     timer;        /**< Resubscription timer        */
        void        *user_data; /**< Application data. */
        int lock_count;
Eloi Bail's avatar
Eloi Bail committed
165
166
};

167
#endif    /* SIPBUDDY_H */