sippresence.h 7.52 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
struct pres_msg_data {
48 49 50 51 52 53
    /**
     * 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.
     */
54
    pjsip_hdr    hdr_list;
55 56 57 58

    /**
     * MIME type of optional message body.
     */
59
    pj_str_t    content_type;
60 61 62 63 64

    /**
     * Optional message body to be added to the message, only when the
     * message doesn't have a body.
     */
65
    pj_str_t    msg_body;
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

    /**
     * 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;
};


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


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

96 97 98 99 100 101 102 103

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

class SIPPresence {

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
    public:

        /**
         * Constructor
         * @param acc the associated sipaccount
         */
        SIPPresence(SIPAccount * acc);
        /**
         * Destructor
         */
        ~SIPPresence();

        /**
         * Return associated sipaccount
         */
        SIPAccount * getAccount() const;
        /**
         * Return presence data.
         */
        pjsip_pres_status * getStatus();
        /**
         * Return presence module ID which is actually the same as the VOIP link
         */
        int getModId() const;
        /**
         *  Return a pool for generic functions.
         */
        pj_pool_t*  getPool() const;
        /**
         * Activate the module (PUBLISH/SUBSCRIBE)
         */
        void enable(bool flag);
        /**
        * Fill xml document, the header and the body
        */
        void fillDoc(pjsip_tx_data *tdata, const pres_msg_data *msg_data);
        /**
         * Modify the presence data
         * @param status is basically "open" or "close"
         */
        void updateStatus(bool status, const std::string &note);
        /**
         * Send the presence data in a PUBLISH to the PBX or in a NOTIFY
         * to a remote subscriber (IP2IP)
         */
        void sendPresence(bool status, const std::string &note);
        /**
         * Send a signal to the client on DBus. The signal contain the status
         * of a remote user.
         */
        void reportPresSubClientNotification(const std::string& uri, pjsip_pres_status * status);
        /**
         * Send a SUBSCRIBE request to PBX/IP2IP
         * @param buddyUri  Remote user that we want to subscribe
         */
        void subscribeClient(const std::string& uri, bool flag);
        /**
         * Add a buddy in the buddy list.
         * @param b     PresSubClient pointer
         */
        void addPresSubClient(PresSubClient *b);
        /**
         * Remove a buddy from the list.
         * @param b     PresSubClient pointer
         */
        void removePresSubClient(PresSubClient *b);

        /**
         * IP2IP context.
         * Report new Subscription to the client, waiting for approval.
         * @param s     PresenceSubcription pointer.
         */
        void reportnewServerSubscriptionRequest(PresSubServer *s);
        /**
        * IP2IP context.
        * Process new subscription based on client decision.
        * @param flag     client decision.
        * @param uri       uri of the remote subscriber
        */
        void approvePresSubServer(const std::string& uri, bool flag);
        /**
         * IP2IP context.
         * Add a server associated to a subscriber in the list.
         * @param s     PresenceSubcription pointer.
         */
        void addPresSubServer(PresSubServer *s);
        /**
         * IP2IP context.
         * Remove a server associated to a subscriber from the list.
         * @param s     PresenceSubcription pointer.
         */
        void removePresSubServer(PresSubServer *s);
        /**
         * IP2IP context.
         * Iterate through the subscriber list and send NOTIFY to each.
         */
        void notifyPresSubServer();

        bool isEnabled() const {
            return enabled_;
        }

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

        void lock();
        void unlock();

    private:
        NON_COPYABLE(SIPPresence);

        static pj_status_t pres_publish(SIPPresence *pres);
        static void pres_publish_cb(struct pjsip_publishc_cbparam *param);
        static pj_status_t pres_send_publish(SIPPresence *pres, pj_bool_t active);

        pjsip_publishc  *publish_sess_;  /**< Client publication session.*/
        pjsip_pres_status pres_status_data_; /**< Presence Data.*/

        pj_bool_t enabled_; /**< Allow for status publish,*/

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

        pj_mutex_t      *mutex_;
        unsigned         mutex_nesting_level_;
        pj_thread_t     *mutex_owner_;
        pj_caching_pool  cp_;
        pj_pool_t       *pool_;
234 235 236
};

#endif