managerimpl.h 29.9 KB
Newer Older
jpbl's avatar
jpbl committed
1 2 3 4
/*
 *  Copyright (C) 2004-2005 Savoir-Faire Linux inc.
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
 *  Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com>
5
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
6
 *  Author: Guillaume Carmel-Archambault <guillaume.carmel-archambault@savoirfairelinux.com>
jpbl's avatar
jpbl committed
7 8 9
 *
 *  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
10
 *  the Free Software Foundation; either version 3 of the License, or
jpbl's avatar
jpbl committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *  (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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef __MANAGER_H__
#define __MANAGER_H__

#include <string>
#include <vector>
yanmorin's avatar
yanmorin committed
28
#include <set>
29
#include <map>
jpbl's avatar
jpbl committed
30
#include <cc++/thread.h>
31
#include "dbus/dbusmanager.h"
jpbl's avatar
jpbl committed
32 33 34 35

#include "stund/stun.h"
#include "observer.h"
#include "config/config.h"
yanmorin's avatar
yanmorin committed
36

37
#include "account.h"
yanmorin's avatar
yanmorin committed
38
#include "call.h"
jpbl's avatar
jpbl committed
39 40 41 42 43 44 45 46 47 48 49

#include "audio/tonelist.h" // for Tone::TONEID declaration
#include "audio/audiofile.h"
#include "audio/dtmf.h"
#include "audio/codecDescriptor.h"

class AudioLayer;
class CodecDescriptor;
class GuiFramework;
class TelephoneTone;
class VoIPLink;
50
class UserAgent;
51

jpbl's avatar
jpbl committed
52 53 54 55
#ifdef USE_ZEROCONF
class DNSService;
#endif

56
/** Define a type for a AccountMap container */
yanmorin's avatar
yanmorin committed
57
typedef std::map<AccountID, Account*> AccountMap;
58 59

/** Define a type for a CallID to AccountID Map inside ManagerImpl */
yanmorin's avatar
yanmorin committed
60
typedef std::map<CallID, AccountID> CallAccountMap;
61

62
/** Define a type for CallID vector (waiting list, incoming not answered) */
yanmorin's avatar
yanmorin committed
63 64
typedef std::set<CallID> CallIDSet;

65
/** To send multiple string */
jpbl's avatar
jpbl committed
66 67
typedef std::list<std::string> TokenList;

68
/** Manager (controller) of sflphone daemon */
jpbl's avatar
jpbl committed
69
class ManagerImpl {
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
  public:
    ManagerImpl (void);
    ~ManagerImpl (void);

    /**
     * Initialisation of thread (sound) and map.
     * Init a new VoIPLink, audio codec and audio driver
     */
    void init (void);

    /**
     * Terminate all thread (sound, link) and unload AccountMap
     */
    void terminate (void);

    /**
     * Set user interface manager.
     * @param man The DBUS interface implementation
     */
    void setDBusManager (DBusManagerImpl* man) { _dbus = man; }

    /**
     * Accessor to audiodriver.
     * it's multi-thread and use mutex internally
     * @return AudioLayer*  The audio layer object
     */
    AudioLayer* getAudioDriver(void) const { return _audiodriver; }

    /**
     * Get a descriptor map of codec available
     * @return CodecDescriptor  The internal codec map
     */
    CodecDescriptor& getCodecDescriptorMap(void) {return _codecDescriptorMap;}

    /**
     * Functions which occur with a user's action
     * Place a new call
     * @param accountId	The account to make tha call with
     * @param id  The call identifier
     * @param to  The recipient of the call
     * @return bool true on success
     *		  false otherwise
     */
    bool outgoingCall(const AccountID& accountId, const CallID& id, const std::string& to);

    /**
     * Functions which occur with a user's action
     * Answer the call
     * @param id  The call identifier
     */
    bool answerCall(const CallID& id);

    /**
     * Functions which occur with a user's action
     * Hangup the call
     * @param id  The call identifier
     */
    bool hangupCall(const CallID& id);

    /**
     * Functions which occur with a user's action
     * Cancel the call
     * @param id  The call identifier
     */
    bool cancelCall(const CallID& id);

    /**
     * Functions which occur with a user's action
     * Put the call on hold
     * @param id  The call identifier
     */
    bool onHoldCall(const CallID& id);

    /**
     * Functions which occur with a user's action
     * Put the call off hold
     * @param id  The call identifier
     */
    bool offHoldCall(const CallID& id);

    /**
     * Functions which occur with a user's action
     * Transfer the call
     * @param id  The call identifier
     * @param to  The recipient of the transfer
     */
    bool transferCall(const CallID& id, const std::string& to);

    /**
     * Functions which occur with a user's action
     * Refuse the call
     * @param id  The call identifier
     */
    bool refuseCall(const CallID& id);

    /** 
     * Save config to file 
     * @return true on success
     *	    false otherwise
     */
    bool saveConfig (void);

    /**
     * Send registration to all enabled accounts
174 175
     * @return 0 on registration success
     *          1 otherelse
176
     */
177
    int initRegisterAccounts();
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 253 254 255 256 257 258 259 260 261 262 263

    /**
     * @return true if we tried to register once
     */
    bool _hasTriedToRegister;

    /**
     * Undocumented
     */
    bool sendTextMessage(const AccountID& accountId, const std::string& to, const std::string& message);

    /**
     * Handle choice of the DTMF-send-way
     * @param   id: callid of the line.
     * @param   code: pressed key.
     */
    bool sendDtmf(const CallID& id, char code);

    /**
     * Play the dtmf-associated sound
     * @param code  The pressed key
     * @param isTalking	In conversation or not. Useful to know whether or not the sound streams are started
     */
    bool playDtmf(char code, bool isTalking);

    /**
     * Play a ringtone
     * @return bool True on success
     *	      false otherwise
     */
    bool playTone ();

    /**
     * Play a special ringtone ( BUSY ) if there's at least one message on the voice mail 
     * @return bool True on success
     *	      false otherwise
     */
    bool playToneWithMessage ();

    /**
     * Acts on the audio streams and audio files
     * @param stopAudio	Tells whether or not to stop the streams
     */
    void stopTone(bool stopAudio);

    /**
     * When receiving a new incoming call, add it to the callaccount map
     * and notify user
     * @param call A call pointer
     * @param accountId an account id
     * @return bool True if the call was added correctly
     */
    bool incomingCall(Call* call, const AccountID& accountId);

    /**
     * Notify the user that the recipient of the call has answered and the put the
     * call in Current state
     * @param id  The call identifier
     */
    void peerAnsweredCall(const CallID& id);

    /**
     * Rings back because the outgoing call is ringing and the put the
     * call in Ringing state
     * @param id  The call identifier
     */
    void peerRingingCall(const CallID& id);

    /**
     * Put the call in Hungup state, remove the call from the list
     * @param id  The call identifier
     */
    void peerHungupCall(const CallID& id);

    /**
     * Notify the client with an incoming message
     * @param accountId	The account identifier
     * @param message The content of the message
     */
    void incomingMessage(const AccountID& accountId, const std::string& message);

    /**
     * Notify the user he has voice mails
     * @param accountId	  The account identifier
     * @param nb_msg The number of messages
     */
264
    void startVoiceMessageNotification(const AccountID& accountId, int nb_msg);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

    /** 
     * Notify the user that registration succeeded  
     * @param accountId	  The account identifier
     */
    void registrationSucceed(const AccountID& accountId);

    /** 
     * Notify the user that unregistration succeeded  
     * @param accountId	  The account identifier
     */
    void unregistrationSucceed(const AccountID& accountId);

    /** 
     * Notify the user that registration failed  
     * @param accountId	  The account identifier
     */
    void registrationFailed(const AccountID& accountId);

    /** 
     * Notify the user that registration is trying  
     * @param accountId	  The account identifier
     */
    void registrationTrying(const AccountID& accountId);

    /**
     * ConfigurationManager - Send registration request
     * @param accountId The account to register/unregister 
     * @param expire The flag for the type of registration
     *		 0 for unregistration request
     *		 1 for registration request
     */
297
    void sendRegister( const ::std::string& accountId , const int32_t& expire );
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

    bool getZeroconf(const std::string& sequenceId);
    bool attachZeroconfEvents(const std::string& sequenceId, Pattern::Observer& observer);
    bool detachZeroconfEvents(Pattern::Observer& observer);
    bool getCallStatus(const std::string& sequenceId);

    /** 
     * Get account list 
     * @return std::vector<std::string> A list of accoundIDs
     */
    std::vector< std::string >  getAccountList();

    /**
     * Retrieve details about a given account
     * @param accountID	  The account identifier
     * @return std::map< std::string, std::string > The account details
     */
    std::map< std::string, std::string > getAccountDetails(const AccountID& accountID);

    /**
     * Save the details of an existing account, given the account ID
     * This will load the configuration map with the given data.
     * It will also register/unregister links where the 'Enabled' switched.
     * @param accountID	  The account identifier
     * @param details	  The account parameters
     */
324 325
    void setAccountDetails( const ::std::string& accountID, 
	const std::map< ::std::string, ::std::string >& details );
326 327 328 329 330

    /**
     * Add a new account, and give it a new account ID automatically
     * @param details The new account parameters
     */
331
    void addAccount(const std::map< ::std::string, ::std::string >& details);
332 333 334 335 336 337 338 339 340 341 342 343

    /**
     * Delete an existing account, unregister VoIPLink associated, and
     * purge from configuration.
     * @param accountID	The account unique ID
     */
    void removeAccount(const AccountID& accountID);

    /**
     * Get the list of codecs we supports, not ordered
     * @return The list of the codecs
     */  
344
    std::vector< ::std::string > getCodecList( void );
345 346 347 348 349 350 351

    /**
     * Get the info about one codec
     * Name / Clock rate / bitrate / bandwidth
     * @param payload The payload of the codec
     * @return std::vector<::DBus::string> The information
     */
352
    std::vector< ::std::string > getCodecDetails( const int32_t& payload);
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472

    /**
     * Get a list of supported input audio plugin
     * @return std::vector<std::string> List of names
     */
    std::vector< std::string> getInputAudioPluginList(void);

    /**
     * Get a list of supported output audio plugin
     * @return std::vector<std::string> List of names
     */
    std::vector< std::string> getOutputAudioPluginList(void);

    /**
     * Set input audio plugin 
     * @param audioPlugin The audio plugin
     */
    void setInputAudioPlugin(const std::string& audioPlugin);

    /**
     * Set output audio plugin 
     * @param audioPlugin The audio plugin
     */
    void setOutputAudioPlugin(const std::string& audioPlugin);

    /**
     * Get list of supported audio output device
     * @return std::vector<std::string> A list of the audio devices supporting playback
     */
    std::vector<std::string> getAudioOutputDeviceList(void);

    /**
     * Set audio output device
     * @param index The index of the soundcard
     */
    void setAudioOutputDevice(const int index);

    /**
     * Get list of supported audio input device
     * @return std::vector<std::string> A list of the audio devices supporting capture
     */
    std::vector<std::string> getAudioInputDeviceList(void);

    /**
     * Set audio input device
     * @param index The index of the soundcard
     */
    void setAudioInputDevice(const int index);

    /**
     * Get string array representing integer indexes of output and input device
     * @return std::vector<std::string> A list of the current audio devices
     */
    std::vector<std::string> getCurrentAudioDevicesIndex();

    /**
     * Get index of an audio device
     * @param name The string description of an audio device
     * @return int  His index
     */
    int getAudioDeviceIndex( const std::string name );

    /*
     * Get current alsa plugin
     * @return std::string  The Alsa plugin
     */
    std::string getCurrentAudioOutputPlugin( void ); 

    /**
     * Convert a list of payload in a special format, readable by the server.
     * Required format: payloads separated with one slash.
     * @return std::string The serializabled string
     */
    std::string serialize(std::vector<std::string> v);

    /**
     * Tells if IAX2 support is enabled
     * @return int 1 if IAX2 is enabled
     *	       0 otherwise
     */
    int isIax2Enabled( void ); 

    /**
     * Ringtone option.
     * If ringtone is enabled, ringtone on incoming call use custom choice. If not, only standart tone. 
     * @return int	1 if enabled
     *	        0 otherwise
     */
    int isRingtoneEnabled( void ); 

    /**
     * Set the ringtone option 
     * Inverse current value
     */
    void ringtoneEnabled( void ); 

    /**
     * Get the ringtone 
     * @return gchar* The file name selected as a ringtone
     */
    std::string getRingtoneChoice( void );

    /**
     * Set a ringtone
     * @param tone The file name of the ringtone
     */
    void setRingtoneChoice( const std::string& );

    /**
     * Tells if the user wants to display the dialpad or not 
     * @return int 1 if dialpad has to be displayed
     *	       0 otherwise
     */
    int getDialpad( void );

    /**
     * Set the dialpad visible or not
     */
    void setDialpad( void );

473 474 475 476 477 478 479 480 481 482 483 484
    /**
     * Tells if the user wants to display the volume controls or not 
     * @return int 1 if the controls have to be displayed
     *	       0 otherwise
     */
    int getVolumeControls( void );

    /**
     * Set the volume controls ( mic and speaker ) visible or not
     */
    void setVolumeControls( void );

485 486 487 488
    /**
     * Set the maximum number of calls to keep in the history
     * @param calls The number of calls
     */
489
    void setMaxCalls( const int& calls );
490 491 492 493 494

    /**
     * Get the maximum number of calls to keep in the history
     * @return double The number of calls
     */
495
    int getMaxCalls( void );
496

497 498 499 500 501 502 503 504 505 506 507 508 509 510
    /**
     * Configure the start-up option
     * @return int	1 if SFLphone should start in the system tray
     *	        0 otherwise
     */
    int isStartHidden( void );

    /**
     * Configure the start-up option
     * At startup, SFLphone can be displayed or start hidden in the system tray
     */
    void startHidden( void );

    /**
511
     * Configure the popup behaviour
512 513 514 515 516 517 518 519 520 521 522 523
     * @return int	1 if it should popup on incoming calls
     *		0 if it should never popups  
     */
    int popupMode( void );

    /**
     * Configure the popup behaviour 
     * When SFLphone is in the system tray, you can configure when it popups
     * Never or only on incoming calls
     */
    void switchPopupMode( void );

524 525 526 527 528 529 530 531 532 533
    /**
     * Determine whether or not the search bar (history) should be displayed
     */
    int getSearchbar( void );

    /**
     * Configure the search bar behaviour
     */
    void setSearchbar( void );

534 535 536 537 538 539 540 541 542
    /**
     * Set the desktop notification level
     */
    void setNotify( void );

    /**
     * Get the desktop notification level
     * @return int The notification level
     */
543
    int32_t getNotify( void );
544 545 546 547 548 549 550

    /**
     * Set the desktop mail notification level
     */
    void setMailNotify( void );

    /**
551 552 553 554 555
     * Get the audio manager
     * @return int The audio manager
     *		    0 ALSA
     *		    1 PULSEAUDIO
     */
556
    int32_t getAudioManager( void );
557 558 559 560

    /**
     * Set the audio manager
     */
561
    void setAudioManager( const int32_t& api );
562 563

    void switchAudioManager( void );
564 565
    
    void setPulseAppVolumeControl( void ); 
566
    int32_t getPulseAppVolumeControl( void ); 
567

568
    /**
569 570 571
     * Get the desktop mail notification level
     * @return int The mail notification level
     */
572
    int32_t getMailNotify( void );
573

574 575 576 577 578
    /**
     * Get the expire value for SIP registration, set in the user configuration file
     */
    int getRegistrationExpireValue( void );

579 580 581 582 583 584 585 586 587
    /**
     * Retrieve the formatted list of codecs payload in the user config file and
     * load in the active list of codecs
     * @return std::vector<std::string>	  The vector containing the active codecs
     */
    std::vector<std::string> retrieveActiveCodecs( void );

    /**
     * Get the list of the active codecs
588
     * @return std::vector< ::std::string >  The list of active codecs
589
     */  
590
    std::vector< ::std::string > getActiveCodecList( void ); 
591 592 593 594 595

    /**
     * Set the list of the active codecs
     * @param list  The new list of active codecs
     */  
596
    void setActiveCodecList( const std::vector< ::std::string >& list);
597 598 599 600 601 602

    /*
     * Notify the client that an error occured
     * @param errCode The error code. Could be: ALSA_CAPTURE_ERROR
     *					       ALSA_PLAYBACK_ERROR
     */
603
    void notifyErrClient( const int32_t& errCode );
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786

    /**
     * Retrieve in the configuration tree the value of a parameter in a specific section
     * @param section	The section to look in
     * @param name	The name of the parameter you want to get
     * @param arg	Undocumented
     * @return bool	true on success
     *			false otherwise
     */
    bool getConfig(const std::string& section, const std::string& name, TokenList& arg);
    
    /**
     * Change a specific value in the configuration tree.
     * This value will then be saved in the user config file sflphonedrc
     * @param section	The section name 
     * @param name	The parameter name
     * @param value	The new string value 
     * @return bool	true on success
     *		      false otherwise
     */
    bool setConfig(const std::string& section, const std::string& name, const std::string& value);
    
    /**
     * Change a specific value in the configuration tree.
     * This value will then be saved in the user config file sflphonedrc
     * @param section	The section name 
     * @param name	The parameter name
     * @param value	The new int value 
     * @return bool	true on success
     *		      false otherwise
     */
    bool setConfig(const std::string& section, const std::string& name, int value);
    
    /** 
     * Get a int from the configuration tree
     * Throw an Conf::ConfigTreeItemException if not found
     * @param section The section name to look in
     * @param name    The parameter name
     * @return int    The int value
     */
    int getConfigInt(const std::string& section, const std::string& name);

    /** 
     * Get a string from the configuration tree
     * Throw an Conf::ConfigTreeItemException if not found
     * @param section The section name to look in
     * @param name    The parameter name
     * @return sdt::string    The string value
     */
    std::string getConfigString(const std::string& section, const std::string& name);

    /**
     * Retrieve the soundcards index in the user config file and try to open audio devices
     * with a specific alsa plugin.
     * Set the audio layer sample rate
     */
    void selectAudioDriver(void);

    /**
     * Handle audio sounds heard by a caller while they wait for their 
     * connection to a called party to be completed.
     */
    void ringback ();

    /**
     * Handle played music when an incoming call occurs
     */
    void ringtone ();

    /**
     * Handle played music when a congestion occurs
     */
    void congestion ();
    
    /**
     * Handle played sound when a call can not be conpleted because of a busy recipient
     */
    void callBusy(const CallID& id);

    /**
     * Handle played sound when a failure occurs
     */
    void callFailure(const CallID& id);

    /** 
     * Retrieve the current telephone tone
     * @return AudioLoop*   The audio tone or 0 if no tone (init before calling this function) 
     */
    AudioLoop* getTelephoneTone();
    
    /** 
     * Retrieve the current telephone file
     * @return AudioLoop* The audio file or 0 if the wav is stopped 
     */
    AudioLoop* getTelephoneFile();

    /**
     * @return true is there is one or many incoming call waiting
     * new call, not anwsered or refused
     */
    bool incomingCallWaiting(void);

    /**
     * Notification of incoming call when you are already busy
     */
    void notificationIncomingCall(void);

    /*
     * Inline functions to manage speaker volume control
     * Read by main thread and AudioLayer thread
     * Write by main thread only
     * @return unsigned short	The volume value
     */
    unsigned short getSpkrVolume(void) { return _spkr_volume; }
    
    /*
     * Inline functions to manage speaker volume control
     * Read by main thread and AudioLayer thread
     * Write by main thread only
     * @param spkr_vol	The volume value
     */
    void setSpkrVolume(unsigned short spkr_vol) {  _spkr_volume = spkr_vol; }
    
    /*
     * Inline functions to manage mic volume control
     * Read by main thread and AudioLayer thread
     * Write by main thread only
     * @return unsigned short	The volume value
     */
    unsigned short getMicVolume(void) {  return _mic_volume;  }
    
    /*
     * Inline functions to manage mic volume control
     * Read by main thread and AudioLayer thread
     * Write by main thread only
     * @param mic_vol	The volume value
     */
    void setMicVolume(unsigned short mic_vol) {    _mic_volume = mic_vol;   }

    // Manage information about firewall
    
    /*
     * Get information about firewall 
     * @param  stunSvrAddr: stun server
     * @param  port         port number to open to test the connection
     * @return true if the connection is successful
     */
    bool getStunInfo(StunAddress4& stunSvrAddr, int port);

    /*
     * Inline functions to manage firewall settings
     * @return int The firewall port
     */
    inline int getFirewallPort(void) 		{ return _firewallPort; }
    
    /*
     * Inline functions to manage firewall settings
     * @param port The firewall port
     */
    inline void setFirewallPort(int port) 	{ _firewallPort = port; }
    
    /*
     * Inline functions to manage firewall settings
     * @return std::string The firewall address
     */
    inline std::string getFirewallAddress (void) 	{ return _firewallAddr; }

    /**
     * If you are behind a NAT, you have to use STUN server, specified in 
     * STUN configuration(you can change this one by default) to give you an 
     * public IP address and assign a port number.
     * Note: Set firewall port/address retreive
     * @param svr   Server on which to send request
     * @param port  On which port we want to listen to
     * @return true if we are behind a NAT (without error)
     */
    bool behindNat(const std::string& svr, int port);

    /**
     * Init default values for the different fields in the config file.
     * Fills the local _config (Conf::ConfigTree) with the default contents.
     * Called in main.cpp, just before Manager::init().
     */
787
    void initConfigFile ( bool load_user_value = true );
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814

    /**
     * Tell if the setup was already loaded
     * @return bool True if yes
     *		  false otherwise
     */
    bool hasLoadedSetup() { return _setupLoaded; }

    /** 
     * Return a new random callid that is not present in the list
     * @return CallID A brand new callid
     */
    CallID getNewCallID();

    /**
     * Get the current call id
     * @return CallID	The call id or ""
     */
    const CallID& getCurrentCallId();

    /**
     * Check if a call is the current one
     * @param callId the new callid
     * @return bool   True if the id is the current call
     */
    bool isCurrentCall(const CallID& callId);

815
    /**
816 817 818
     * Restart PJSIP
     * @param void
     * @return void
819
     */
820
    void restartPjsip();
821

822 823 824 825 826 827 828 829
    int getSipPort();
    
    void setSipPort(int port);
    
    void unregisterCurSIPAccounts();
    
    void registerCurSIPAccounts();
    
830
  private:
Yun Liu's avatar
Yun Liu committed
831
    
832 833 834 835 836 837 838 839
    /**
     * Check if a process is running with the system command
     *
     * @return 0 on success
     *          1 otherelse
     */
    int app_is_running(std::string process);

840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
    /**
     * Create .PROGNAME directory in home user and create 
     * configuration tree from the settings file if this file exists.
     *
     * @return	0 if creating file failed
     *			1 if config-file exists
     *			2 if file doesn't exist yet.
     */
    int createSettingsPath (void);

    /*
     * Initialize audiocodec with config setting
     */
    void initAudioCodec(void);

    /*
     * Initialize audiodriver
     */
    void initAudioDriver(void);

    /*
     * Initialize zeroconf module and scanning
     */
    void initZeroconf(void);

    /*
     * Init the volume for speakers/micro from 0 to 100 value
     */ 
    void initVolume();

    /**
     * Tell if there is a current call processed
     * @return bool True if there is a current call
     */
    bool hasCurrentCall();

    /**
     * Switch of current call id
     * @param id The new callid
     */
    void switchCall(const CallID& id);
    
    /*
     * Play one tone
     * @return false if the driver is uninitialize
     */
    bool playATone(Tone::TONEID toneId);

    /** The configuration tree. It contains accounts parameters, general user settings ,audio settings, ... */
    Conf::ConfigTree _config;

    /** Current Call ID */
    CallID _currentCallId2;

    /** Protected current call access */
    ost::Mutex _currentCallMutex;

    /** Vector of CodecDescriptor */
    CodecDescriptor* _codecBuilder;

    /** Audio layer */
    AudioLayer* _audiodriver;

    // Main thread

    DTMF* _dtmfKey;

    // map of codec (for configlist request)
    CodecDescriptor _codecDescriptorMap;

    /////////////////////
    // Protected by Mutex
    /////////////////////
    ost::Mutex _toneMutex;
    TelephoneTone* _telephoneTone;
    AudioFile _audiofile;

    // To handle volume control
    short _spkr_volume;
    short _mic_volume;
    // End of sound variable


    // Multithread variable (protected by _mutex)
    // 
    /** Mutex to protect access to code section */
    ost::Mutex _mutex;

    // Multithread variable (non protected)
    DBusManagerImpl * _dbus;

    /** Waiting Call Vectors */
    CallIDSet _waitingCall;

    /** Protect waiting call list, access by many voip/audio threads */
    ost::Mutex _waitingCallMutex;
    
    /** Number of waiting call, synchronize with waitingcall callidvector */
    unsigned int _nbIncomingWaitingCall;
    
    /**
     * Add incoming callid to the waiting list
     * @param id CallID to add
     */
    void addWaitingCall(const CallID& id);

    /**
     * Remove incoming callid to the waiting list
     * @param id CallID to remove
     */
    void removeWaitingCall(const CallID& id);

    /**
     * Tell if a call is waiting and should be remove
     * @param id CallID to test
     * @return bool True if the call is waiting
     */
    bool isWaitingCall(const CallID& id);

    /**
     * Path of the ConfigFile 
     */
    std::string 	_path;
    int _exist;
    int _setupLoaded;

    // To handle firewall
    int _firewallPort;
    std::string _firewallAddr;

    // tell if we have zeroconf is enabled
    int _hasZeroconf;
jpbl's avatar
jpbl committed
972 973

#ifdef USE_ZEROCONF
974 975 976
    // DNSService contain every zeroconf services
    //  configuration detected on the network
    DNSService *_DNSService;
jpbl's avatar
jpbl committed
977 978
#endif

979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
    /** Map to associate a CallID to the good account */
    CallAccountMap _callAccountMap;
    
    /** Mutex to lock the call account map (main thread + voiplink thread) */
    ost::Mutex _callAccountMapMutex;

    /** Associate a new CallID to a AccountID
     * Protected by mutex
     * @param callID the new CallID not in the list yet
     * @param accountID the known accountID present in accountMap
     * @return bool True if the new association is create
     */
    bool associateCallToAccount(const CallID& callID, const AccountID& accountID);

    /** Remove a CallID/AccountID association
     * Protected by mutex
     * @param callID the CallID to remove
     * @return bool True if association is removed
     */
    bool removeCallAccount(const CallID& callID);

    /** Contains a list of account (sip, aix, etc) and their respective voiplink/calls */
    AccountMap _accountMap;

    /**
     * Load the account from configuration
     * @return short Number of account
     */
    short loadAccountMap();

    /**
     * Unload the account (delete them)
     */
    void unloadAccountMap();

    /**
     * Tell if an account exists
     * @param accountID account ID check
     * @return bool True if the account exists
     *		  false otherwise
     */
    bool accountExists(const AccountID& accountID);

1022
public:
1023 1024 1025 1026 1027 1028 1029
    /**
     * Get an account pointer
     * @param accountID account ID to get
     * @return Account*	 The account pointer or 0
     */
    Account* getAccount(const AccountID& accountID);

1030 1031 1032 1033 1034 1035 1036
    /** Return the AccountID from a CallID
     * Protected by mutex
     * @param callID the CallID in the list
     * @return AccountID  The accountID associated or "" if the callID is not found
     */
    AccountID getAccountFromCall(const CallID& callID);

1037 1038 1039 1040 1041 1042 1043
    /**
     * Get the voip link from the account pointer
     * @param accountID	  Account ID to get
     * @return VoIPLink*   The voip link from the account pointer or 0
     */
    VoIPLink* getAccountLink(const AccountID& accountID);

1044 1045
    AccountID getAccountIdFromNameAndServer(const std::string& userName, const std::string& server);

1046
private:
Yun Liu's avatar
Yun Liu committed
1047 1048 1049 1050 1051 1052 1053

    // Copy Constructor
    ManagerImpl(const ManagerImpl& rh);

    // Assignment Operator
    ManagerImpl& operator=( const ManagerImpl& rh);

1054
    /**
1055
     * The UserAgent provides sip operation facilities for all sip accounts
1056
     */
1057
    UserAgent *_userAgent;
1058

1059 1060
    /** Whether the _UserAgent has been initialized */
    bool _userAgentInitlized;
1061 1062
    
    bool _sipThreadStop;
1063

1064 1065 1066 1067
#ifdef TEST
    bool testCallAccountMap();
    bool testAccountMap();
#endif
1068

1069
    friend class ConfigurationTest;
1070

1071 1072 1073 1074
public:
    /**
     * Retuun the instance of sip manager
     */
1075
    UserAgent *getUserAgent();
1076 1077 1078 1079
    
    void setSipThreadStatus(bool status) {_sipThreadStop = status;}
    
    bool getSipThreadStatus() {return _sipThreadStop;}
jpbl's avatar
jpbl committed
1080 1081
};

Emmanuel Milou's avatar
Emmanuel Milou committed
1082
#endif // __MANAGER_H__