Liblinphone  3.12.0
Basic buddy status notification

This program is a very simple usage example of liblinphone, demonstrating how to initiate SIP subscriptions and receive notifications from a sip uri identity passed from the command line.
Argument must be like sip:jehan.nosp@m.@sip.nosp@m..linp.nosp@m.hone.nosp@m..org .
ex budy_list sip:jehan.nosp@m.@sip.nosp@m..linp.nosp@m.hone.nosp@m..org
Subscription is cleared on SIGINT

/*
buddy_status
Copyright (C) 2010 Belledonne Communications SARL
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 2
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.
*/
#include "linphone/core.h"
#include <signal.h>
static bool_t running=TRUE;
static void stop(int signum){
running=FALSE;
}
static void notify_presence_recv_updated (LinphoneCore *lc, LinphoneFriend *friend) {
const LinphoneAddress* friend_address = linphone_friend_get_address(friend);
if (friend_address != NULL) {
char *activity_str = linphone_presence_activity_to_string(activity);
char *str = linphone_address_as_string (friend_address);
printf("New state state [%s] for user id [%s] \n"
,activity_str
,str);
ms_free(str);
}
}
static void new_subscription_requested (LinphoneCore *lc, LinphoneFriend *friend, const char* url) {
const LinphoneAddress* friend_address = linphone_friend_get_address(friend);
if (friend_address != NULL) {
char *str = linphone_address_as_string (friend_address);
printf(" [%s] wants to see your status, accepting\n", str);
ms_free(str);
}
linphone_friend_edit(friend); /* start editing friend */
linphone_friend_set_inc_subscribe_policy(friend,LinphoneSPAccept); /* Accept incoming subscription request for this friend*/
linphone_friend_done(friend); /*commit change*/
linphone_core_add_friend(lc,friend); /* add this new friend to the buddy list*/
}
static void registration_state_changed(struct _LinphoneCore *lc, LinphoneProxyConfig *cfg, LinphoneRegistrationState cstate, const char *message){
printf("New registration state %s for user id [%s] at proxy [%s]"
,linphone_proxy_config_get_addr(cfg));
}
int main(int argc, char *argv[]){
LinphoneCoreVTable vtable={0};
char* dest_friend=NULL;
char* identity=NULL;
char* password=NULL;
LinphoneFriend* my_friend=NULL;
/* takes sip uri identity from the command line arguments */
if (argc>1){
dest_friend=argv[1];
}
/* takes sip uri identity from the command line arguments */
if (argc>2){
identity=argv[2];
}
/* takes password from the command line arguments */
if (argc>3){
password=argv[3];
}
signal(SIGINT,stop);
//#define DEBUG
#ifdef DEBUG
linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
#endif
/*
Fill the LinphoneCoreVTable with application callbacks.
All are optional. Here we only use the both notify_presence_received and new_subscription_requested callbacks
in order to get notifications about friend status.
*/
vtable.notify_presence_received=notify_presence_recv_updated;
vtable.new_subscription_requested=new_subscription_requested;
vtable.registration_state_changed=registration_state_changed; /*just in case sip proxy is used*/
/*
Instantiate a LinphoneCore object given the LinphoneCoreVTable
*/
lc=linphone_core_new(&vtable,NULL,NULL,NULL);
/*sip proxy might be requested*/
if (identity != NULL) {
/*create proxy config*/
/*parse identity*/
if (from==NULL){
printf("%s not a valid sip uri, must be like sip:toto@sip.linphone.org \n",identity);
goto end;
}
if (password!=NULL){
info=linphone_auth_info_new(linphone_address_get_username(from),NULL,password,NULL,NULL,NULL); /*create authentication structure from identity*/
linphone_core_add_auth_info(lc,info); /*add authentication info to LinphoneCore*/
}
// configure proxy entries
linphone_proxy_config_set_identity(proxy_cfg,identity); /*set identity with user name and domain*/
linphone_proxy_config_set_server_addr(proxy_cfg,linphone_address_get_domain(from)); /* we assume domain = proxy server address*/
linphone_proxy_config_enable_register(proxy_cfg,TRUE); /*activate registration for this proxy config*/
linphone_proxy_config_enable_publish(proxy_cfg,TRUE); /* enable presence satus publication for this proxy*/
linphone_address_unref(from); /*release resource*/
linphone_core_add_proxy_config(lc,proxy_cfg); /*add proxy config to linphone core*/
linphone_core_set_default_proxy(lc,proxy_cfg); /*set to default proxy*/
/* Loop until registration status is available */
do {
linphone_core_iterate(lc); /* first iterate initiates registration */
ms_usleep(100000);
}
}
if (dest_friend) {
my_friend = linphone_core_create_friend_with_address(lc, dest_friend); /*creates friend object from dest*/
if (my_friend == NULL) {
printf("bad destination uri for friend [%s]\n",dest_friend);
goto end;
}
linphone_friend_enable_subscribes(my_friend,TRUE); /*configure this friend to emit SUBSCRIBE message after being added to LinphoneCore*/
linphone_friend_set_inc_subscribe_policy(my_friend,LinphoneSPAccept); /* Accept incoming subscription request for this friend*/
linphone_core_add_friend(lc,my_friend); /* add my friend to the buddy list, initiate SUBSCRIBE message*/
}
/*set my status to online*/
/* main loop for receiving notifications and doing background linphone core work: */
while(running){
linphone_core_iterate(lc); /* first iterate initiates subscription */
ms_usleep(50000);
}
/* change my presence status to offline*/
linphone_core_iterate(lc); /* just to make sure new status is initiate message is issued */
linphone_friend_edit(my_friend); /* start editing friend */
linphone_friend_enable_subscribes(my_friend,FALSE); /*disable subscription for this friend*/
linphone_friend_done(my_friend); /*commit changes triggering an UNSUBSCRIBE message*/
linphone_core_iterate(lc); /* just to make sure unsubscribe message is issued */
end:
printf("Shutting down...\n");
printf("Exited\n");
return 0;
}
LinphoneRegistrationState
enum _LinphoneRegistrationState LinphoneRegistrationState
LinphoneRegistrationState describes proxy registration states.
linphone_friend_edit
void linphone_friend_edit(LinphoneFriend *fr)
Starts editing a friend configuration.
linphone_registration_state_to_string
const char * linphone_registration_state_to_string(LinphoneRegistrationState cs)
Human readable version of the LinphoneRegistrationState.
linphone_core_set_default_proxy
#define linphone_core_set_default_proxy(lc, config)
Definition: core.h:1960
LinphoneCore
struct _LinphoneCore LinphoneCore
Linphone core main object created by function linphone_core_new() .
Definition: types.h:472
linphone_proxy_config_set_server_addr
LinphoneStatus linphone_proxy_config_set_server_addr(LinphoneProxyConfig *cfg, const char *server_addr)
Sets the proxy address.
_LinphoneCoreVTable::new_subscription_requested
LinphoneCoreNewSubscriptionRequestedCb new_subscription_requested
Notify about pending presence subscription request.
Definition: core.h:186
linphone_friend_get_presence_model
const LinphonePresenceModel * linphone_friend_get_presence_model(const LinphoneFriend *lf)
Get the presence model of a friend.
linphone_friend_enable_subscribes
LinphoneStatus linphone_friend_enable_subscribes(LinphoneFriend *fr, bool_t val)
Configure LinphoneFriend to subscribe to presence information.
linphone_address_get_username
const char * linphone_address_get_username(const LinphoneAddress *u)
Returns the username.
linphone_address_as_string
char * linphone_address_as_string(const LinphoneAddress *u)
Returns the address as a string.
LinphonePresenceModel
struct _LinphonePresenceModel LinphonePresenceModel
Presence model type holding information about the presence of a person.
Definition: types.h:847
linphone_address_unref
void linphone_address_unref(LinphoneAddress *addr)
Decrement reference count of LinphoneAddress object.
linphone_friend_set_inc_subscribe_policy
LinphoneStatus linphone_friend_set_inc_subscribe_policy(LinphoneFriend *fr, LinphoneSubscribePolicy pol)
Configure incoming subscription policy for this friend.
LinphonePresenceBasicStatusClosed
This value means that the associated contact element, if any, is unable to accept communication.
Definition: types.h:840
linphone_core_enable_logs
LINPHONE_DEPRECATED void linphone_core_enable_logs(FILE *file)
Enable logs in supplied FILE*.
LinphonePresenceActivity
struct _LinphonePresenceActivity LinphonePresenceActivity
Presence activity type holding information about a presence activity.
Definition: types.h:733
LinphonePresenceBasicStatusOpen
This value means that the associated contact element, if any, is ready to accept communication.
Definition: types.h:837
linphone_presence_model_set_basic_status
LinphoneStatus linphone_presence_model_set_basic_status(LinphonePresenceModel *model, LinphonePresenceBasicStatus basic_status)
Sets the basic status of a presence model.
linphone_presence_activity_to_string
char * linphone_presence_activity_to_string(const LinphonePresenceActivity *activity)
Gets the string representation of a presence activity.
LinphoneProxyConfig
struct _LinphoneProxyConfig LinphoneProxyConfig
The LinphoneProxyConfig object represents a proxy configuration to be used by the LinphoneCore object...
Definition: types.h:940
linphone_friend_done
void linphone_friend_done(LinphoneFriend *fr)
Commits modification made to the friend configuration.
linphone_proxy_config_get_state
LinphoneRegistrationState linphone_proxy_config_get_state(const LinphoneProxyConfig *cfg)
Get the registration state of the given proxy config.
LinphoneAuthInfo
struct _LinphoneAuthInfo LinphoneAuthInfo
Object holding authentication information.
Definition: types.h:231
linphone_proxy_config_enable_publish
void linphone_proxy_config_enable_publish(LinphoneProxyConfig *cfg, bool_t val)
Indicates either or not, PUBLISH must be issued for this LinphoneProxyConfig .
linphone_proxy_config_get_identity
const LINPHONE_DEPRECATED char * linphone_proxy_config_get_identity(const LinphoneProxyConfig *cfg)
linphone_proxy_config_enable_register
void linphone_proxy_config_enable_register(LinphoneProxyConfig *cfg, bool_t val)
Indicates either or not, REGISTRATION must be issued for this LinphoneProxyConfig .
linphone_core_add_auth_info
void linphone_core_add_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info)
Adds authentication information to the LinphoneCore.
linphone_core_iterate
void linphone_core_iterate(LinphoneCore *lc)
Main loop function.
LinphoneRegistrationProgress
Registration is in progress.
Definition: types.h:997
linphone_core_set_presence_model
void linphone_core_set_presence_model(LinphoneCore *lc, LinphonePresenceModel *presence)
Set my presence model.
linphone_presence_model_get_activity
LinphonePresenceActivity * linphone_presence_model_get_activity(const LinphonePresenceModel *model)
Gets the first activity of a presence model (there is usually only one).
linphone_address_new
LinphoneAddress * linphone_address_new(const char *addr)
Constructs a LinphoneAddress object by parsing the user supplied address, given as a string.
linphone_core_create_friend_with_address
LinphoneFriend * linphone_core_create_friend_with_address(LinphoneCore *lc, const char *address)
Create a LinphoneFriend from the given address.
linphone_core_new
LINPHONE_DEPRECATED LinphoneCore * linphone_core_new(const LinphoneCoreVTable *vtable, const char *config_path, const char *factory_config_path, void *userdata)
Instanciates a LinphoneCore object.
linphone_auth_info_new
LinphoneAuthInfo * linphone_auth_info_new(const char *username, const char *userid, const char *passwd, const char *ha1, const char *realm, const char *domain)
Creates a LinphoneAuthInfo object with supplied information.
linphone_presence_model_unref
LinphonePresenceModel * linphone_presence_model_unref(LinphonePresenceModel *model)
Decrease the reference count of the LinphonePresenceModel object and destroy it if it reaches 0.
linphone_proxy_config_new
LINPHONE_DEPRECATED LinphoneProxyConfig * linphone_proxy_config_new(void)
Creates an empty proxy config.
linphone_proxy_config_set_identity
LINPHONE_DEPRECATED LinphoneStatus linphone_proxy_config_set_identity(LinphoneProxyConfig *cfg, const char *identity)
LinphoneAddress
struct SalAddress LinphoneAddress
Object that represents a SIP address.
Definition: types.h:186
_LinphoneCoreVTable::notify_presence_received
LinphoneCoreNotifyPresenceReceivedCb notify_presence_received
Notify received presence events.
Definition: core.h:184
LinphoneFriend
struct _LinphoneFriend LinphoneFriend
Represents a buddy, all presence actions like subscription and status change notification are perform...
Definition: types.h:539
linphone_address_get_domain
const char * linphone_address_get_domain(const LinphoneAddress *u)
Returns the domain name.
LinphoneSPAccept
Automatically accepts a subscription request.
Definition: types.h:1059
linphone_core_add_friend
void linphone_core_add_friend(LinphoneCore *lc, LinphoneFriend *fr)
Add a friend to the current buddy list, if subscription attribute is set, a SIP SUBSCRIBE message is...
linphone_presence_model_new
LinphonePresenceModel * linphone_presence_model_new(void)
Creates a default presence model.
_LinphoneCoreVTable
This structure holds all callbacks that the application should implement.
Definition: core.h:180
linphone_core_add_proxy_config
LinphoneStatus linphone_core_add_proxy_config(LinphoneCore *lc, LinphoneProxyConfig *config)
Add a proxy configuration.
_LinphoneCoreVTable::registration_state_changed
LinphoneCoreRegistrationStateChangedCb registration_state_changed
Notifies registration state changes.
Definition: core.h:182
linphone_core_destroy
LINPHONE_DEPRECATED void linphone_core_destroy(LinphoneCore *lc)
Destroys a LinphoneCore.
linphone_friend_get_address
const LinphoneAddress * linphone_friend_get_address(const LinphoneFriend *lf)
Get address of this friend.