sippresence.h 7 KB
Newer Older
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
/*
 *  Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
 *
 *  Author: Patrick Keroulas  <patrick.keroulas@savoirfairelinux.com>
 *
 *  This program 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.
 *
 *  This program 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 program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, 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, Savoir-Faire Linux Inc.
 *  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 SIPPRESENCE_H
#define SIPPRESENCE_H

#include <string>
#include <list>

38
#include "noncopyable.h"
39 40 41 42 43 44
#include "pjsip/sip_types.h"
#include "pjsip/sip_msg.h"
#include "pjsip/sip_multipart.h"
#include "pjsip-simple/publish.h"
#include "pjsip-simple/presence.h"
#include "pjsip-simple/rpid.h"
45
#include <pj/pool.h>
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

struct pres_msg_data
{
    /**
     * Additional message headers as linked list. Application can add
     * headers to the list by creating the header, either from the heap/pool
     * or from temporary local variable, and add the header using
     * linked list operation. See pjsip_apps.c for some sample codes.
     */
    pjsip_hdr	hdr_list;

    /**
     * MIME type of optional message body.
     */
    pj_str_t	content_type;

    /**
     * Optional message body to be added to the message, only when the
     * message doesn't have a body.
     */
    pj_str_t	msg_body;

    /**
     * Content type of the multipart body. If application wants to send
     * multipart message bodies, it puts the parts in \a parts and set
     * the content type in \a multipart_ctype. If the message already
     * contains a body, the body will be added to the multipart bodies.
     */
    pjsip_media_type  multipart_ctype;

    /**
     * List of multipart parts. If application wants to send multipart
     * message bodies, it puts the parts in \a parts and set the content
     * type in \a multipart_ctype. If the message already contains a body,
     * the body will be added to the multipart bodies.
     */
    pjsip_multipart_part multipart_parts;
};


86
//extern void pres_process_msg_data(pjsip_tx_data *tdata, const pres_msg_data *msg_data);
87 88 89


class SIPAccount;
90 91
class PresSubClient;
class PresSubServer;
92 93 94 95 96
/**
 * TODO Clean this:
 */
struct pj_caching_pool;

97 98 99 100 101 102 103 104 105

/**
 * @file sippresence.h
 * @brief A SIP Presence manages buddy subscription in both PBX and IP2IP contexts.
 */

class SIPPresence {

public:
106

107 108 109 110 111 112 113 114 115 116
    /**
     * Constructor
     * @param acc the associated sipaccount
     */
    SIPPresence(SIPAccount * acc);
    /**
     * Destructor
     */
    ~SIPPresence();

117 118 119
    /**
     * Return associated sipaccount
     */
120
    SIPAccount * getAccount() const;
121 122 123 124 125 126 127
    /**
     * Return presence data.
     */
    pjsip_pres_status * getStatus();
    /**
     * Return presence module ID which is actually the same as the VOIP link
     */
128
    int getModId() const;
129 130 131
    /**
     *  Return a pool for generic functions.
     */
132
    pj_pool_t*  getPool() const;
133
    /**
134 135
     * Activate the module (PUBLISH/SUBSCRIBE)
     */
136
    void enable(bool flag);
137
     /**
138 139 140
     * Fill xml document, the header and the body
     */
    void fillDoc(pjsip_tx_data *tdata, const pres_msg_data *msg_data);
141 142 143 144
    /**
     * Modify the presence data
     * @param status is basically "open" or "close"
     */
145
    void updateStatus(bool status, const std::string &note);
146 147 148 149
    /**
     * Send the presence data in a PUBLISH to the PBX or in a NOTIFY
     * to a remote subscriber (IP2IP)
     */
150
    void sendPresence(bool status, const std::string &note);
151 152 153 154
    /**
     * Send a signal to the client on DBus. The signal contain the status
     * of a remote user.
     */
155
    void reportPresSubClientNotification(const std::string& uri, pjsip_pres_status * status);
156 157 158 159
    /**
     * Send a SUBSCRIBE request to PBX/IP2IP
     * @param buddyUri  Remote user that we want to subscribe
     */
160
    void subscribeClient(const std::string& uri, bool flag);
161 162
    /**
     * Add a buddy in the buddy list.
163
     * @param b     PresSubClient pointer
164
     */
165
    void addPresSubClient(PresSubClient *b);
166 167
    /**
     * Remove a buddy from the list.
168
     * @param b     PresSubClient pointer
169
     */
170
    void removePresSubClient(PresSubClient *b);
171

172 173 174 175 176
    /**
     * IP2IP context.
     * Report new Subscription to the client, waiting for approval.
     * @param s     PresenceSubcription pointer.
     */
177
    void reportnewServerSubscriptionRequest(PresSubServer *s);
178 179 180
     /**
     * IP2IP context.
     * Process new subscription based on client decision.
181 182
     * @param flag     client decision.
     * @param uri       uri of the remote subscriber
183
     */
184
    void approvePresSubServer(const std::string& uri, bool flag);
185
    /**
186 187 188
     * IP2IP context.
     * Add a server associated to a subscriber in the list.
     * @param s     PresenceSubcription pointer.
189
     */
190
    void addPresSubServer(PresSubServer *s);
191
    /**
192 193 194
     * IP2IP context.
     * Remove a server associated to a subscriber from the list.
     * @param s     PresenceSubcription pointer.
195
     */
196
    void removePresSubServer(PresSubServer *s);
197 198 199 200
    /**
     * IP2IP context.
     * Iterate through the subscriber list and send NOTIFY to each.
     */
201
    void notifyPresSubServer();
202

203
    bool isEnabled() const {
Tristan Matthews's avatar
Tristan Matthews committed
204
        return enabled_;
205
    }
206

207 208 209 210
    const std::list< PresSubClient *> getClientSubscriptions() {
        return pres_sub_client_list_;
    }

211 212 213
    void lock();
    void unlock();

214 215
    pjsip_publishc  *publish_sess;  /**< Client publication session.*/
private:
216
    pjsip_pres_status pres_status_data_; /**< Presence Data.*/
Tristan Matthews's avatar
Tristan Matthews committed
217 218

    pj_bool_t enabled_; /**< Allow for status publish,*/
219
    NON_COPYABLE(SIPPresence);
220

221
    SIPAccount * acc_; /**<  Associated SIP account. */
222 223
    std::list< PresSubServer *> pres_sub_server_list_; /**< Subscribers list.*/
    std::list< PresSubClient *> pres_sub_client_list_; /**< Subcribed buddy list.*/
224

225 226 227
    pj_mutex_t	*mutex_;	    /**< Mutex protection for this data	*/
    unsigned	mutex_nesting_level_; /**< Mutex nesting level.	*/
    pj_thread_t	*mutex_owner_; /**< Mutex owner.			*/
228
    pj_caching_pool     cp_;	    /**< Global pool factory.		*/
229 230
    pj_pool_t	*pool_;	    /**< pjsua's private pool.		*/

231

232 233 234
};

#endif