Liblinphone 5.4.23
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.

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

/*
* Copyright (c) 2010-2022 Belledonne Communications SARL.
*
* This file is part of Liblinphone
* (see https://gitlab.linphone.org/BC/public/liblinphone).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#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 */
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 account_registration_state_changed(struct _LinphoneCore *lc,
LinphoneAccount *account,
const char *message) {
printf("New registration state %s for user id [%s] at proxy [%s]", linphone_registration_state_to_string(cstate),
linphone_account_params_get_identity(account_params), linphone_account_params_get_addr(account_params));
}
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_LOGS
#ifdef DEBUG_LOGS
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.account_registration_state_changed = account_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 account parameters*/
/*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_account_params_set_identity_address(params, from); /*set identity with user name and domain*/
params, linphone_address_get_domain(from)); /* we assume domain = proxy server address*/
linphone_account_params_enable_register(params, TRUE); /*activate registration for this account*/
linphone_account_params_enable_publish(params, TRUE); /* enable presence satus publication for this proxy*/
linphone_address_unref(from); /*release resource*/
/*create account*/
linphone_core_add_account(lc, account); /*add account to linphone core*/
linphone_core_set_default_account(lc, account); /*set to default account*/
linphone_account_params_unref(params); /*release resource*/
/* Loop until registration status is available */
do {
linphone_core_iterate(lc); /* first iterate initiates registration */
ms_usleep(100000);
} while (running && linphone_account_get_state(account) == LinphoneRegistrationProgress);
}
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;
}
my_friend, TRUE); /*configure this friend to emit SUBSCRIBE message after being added to LinphoneCore*/
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;
}
void linphone_account_params_enable_publish(LinphoneAccountParams *params, bool_t enable)
Indicates either or not, PUBLISH must be issued for this LinphoneAccountParams.
void linphone_core_set_default_account(LinphoneCore *core, LinphoneAccount *account)
Sets the default account.
struct _LinphoneAccount LinphoneAccount
Object that represents a Linphone Account.
Definition c-types.h:96
struct _LinphoneAccountParams LinphoneAccountParams
Object that is used to set the different parameters of a LinphoneAccount.
Definition c-types.h:105
LinphoneAccountParams * linphone_account_params_new(LinphoneCore *lc, bool_t use_default_values)
Create a new LinphoneAccountParams object.
LinphoneAccount * linphone_core_create_account(LinphoneCore *core, LinphoneAccountParams *params)
Creates an account using given parameters, see linphone_core_create_account_params().
MS2_DEPRECATED LinphoneStatus linphone_account_params_set_server_addr(LinphoneAccountParams *params, const char *server_address)
Sets the proxy address.
LinphoneStatus linphone_core_add_account(LinphoneCore *core, LinphoneAccount *account)
Adds an account.
LinphoneRegistrationState linphone_account_get_state(const LinphoneAccount *account)
Get the registration state of the given account.
LinphoneStatus linphone_account_params_set_identity_address(LinphoneAccountParams *params, const LinphoneAddress *identity)
Sets the user identity as a SIP address.
void linphone_account_params_unref(LinphoneAccountParams *params)
Release a LinphoneAccountParams.
const LinphoneAccountParams * linphone_account_get_params(const LinphoneAccount *account)
Get the LinphoneAccountParams as read-only object.
void linphone_account_params_enable_register(LinphoneAccountParams *params, bool_t enable)
Indicates either or not, REGISTRATION must be issued for this LinphoneAccountParams.
MS2_DEPRECATED const char * linphone_account_params_get_identity(const LinphoneAccountParams *params)
Get the identity of the account params.
void linphone_core_add_auth_info(LinphoneCore *core, const LinphoneAuthInfo *info)
Adds authentication information to the LinphoneCore.
struct _LinphoneAuthInfo LinphoneAuthInfo
Object holding authentication information.
Definition c-types.h:84
LinphoneFriend * linphone_core_create_friend_with_address(LinphoneCore *core, const char *address)
Creates a LinphoneFriend from the given address.
const LinphoneAddress * linphone_friend_get_address(const LinphoneFriend *linphone_friend)
Get address of this friend.
struct _LinphoneFriend LinphoneFriend
This object is used to store a SIP address.
Definition types.h:314
void linphone_presence_model_unref(LinphonePresenceModel *model)
Decrease the reference count of the LinphonePresenceModel object and destroy it if it reaches 0.
LinphonePresenceActivity * linphone_presence_model_get_activity(const LinphonePresenceModel *model)
Gets the first activity of a presence model (there is usually only one).
LinphoneStatus linphone_presence_model_set_basic_status(LinphonePresenceModel *model, LinphonePresenceBasicStatus basic_status)
Sets the basic status of a presence model.
LinphoneStatus linphone_friend_set_inc_subscribe_policy(LinphoneFriend *linphone_friend, LinphoneSubscribePolicy policy)
Configure incoming subscription policy for this friend.
LinphoneStatus linphone_friend_enable_subscribes(LinphoneFriend *linphone_friend, bool_t enable)
Configure LinphoneFriend to subscribe to presence information.
void linphone_friend_done(LinphoneFriend *linphone_friend)
Commits modification made to the friend configuration.
LinphonePresenceModel * linphone_presence_model_new(void)
Creates a default presence model.
char * linphone_presence_activity_to_string(const LinphonePresenceActivity *activity)
Gets the string representation of a presence activity.
void linphone_friend_edit(LinphoneFriend *linphone_friend)
Starts editing a friend configuration.
void linphone_core_set_presence_model(LinphoneCore *core, LinphonePresenceModel *presence)
Sets my presence model.
const LinphonePresenceModel * linphone_friend_get_presence_model(const LinphoneFriend *linphone_friend)
Get the presence model of a friend.
struct _LinphonePresenceModel LinphonePresenceModel
Presence model type holding information about the presence of a person.
Definition types.h:995
struct _LinphonePresenceActivity LinphonePresenceActivity
Presence activity type holding information about a presence activity.
Definition types.h:881
@ LinphonePresenceBasicStatusClosed
This value means that the associated contact element, if any, is unable to accept communication.
Definition types.h:988
@ LinphonePresenceBasicStatusOpen
This value means that the associated contact element, if any, is ready to accept communication.
Definition types.h:985
@ LinphoneSPAccept
Automatically accepts a subscription request.
Definition types.h:1180
void linphone_core_iterate(LinphoneCore *core)
Main loop integration.
MS2_DEPRECATED LinphoneCore * linphone_core_new(const LinphoneCoreVTable *vtable, const char *config_path, const char *factory_config_path, void *userdata)
Instanciates a LinphoneCore object.
struct _LinphoneCoreVTable LinphoneCoreVTable
This structure holds all callbacks that the application should implement.
struct _LinphoneCore LinphoneCore
Main object to instanciate and on which to keep a reference.
Definition types.h:485
MS2_DEPRECATED void linphone_core_destroy(LinphoneCore *core)
Destroys a LinphoneCore.
char * linphone_address_as_string(const LinphoneAddress *address)
Returns the address as a string.
struct _LinphoneAddress LinphoneAddress
Object that represents a parsed SIP address.
Definition c-types.h:156
const char * linphone_address_get_username(const LinphoneAddress *address)
Returns the username.
LinphoneAddress * linphone_address_new(const char *address)
Constructs a LinphoneAddress object by parsing the user supplied address, given as a string.
void linphone_address_unref(LinphoneAddress *address)
Decrement reference count of LinphoneAddress object.
const char * linphone_address_get_domain(const LinphoneAddress *address)
Returns the domain name.
const char * linphone_registration_state_to_string(LinphoneRegistrationState state)
Human readable version of the LinphoneRegistrationState.
enum _LinphoneRegistrationState LinphoneRegistrationState
Describes proxy registration states.
@ LinphoneRegistrationProgress
Registration is in progress.
Definition types.h:455
LinphoneCoreNewSubscriptionRequestedCb new_subscription_requested
Notify about pending presence subscription request.
Definition core.h:206
LinphoneCoreNotifyPresenceReceivedCb notify_presence_received
Notify received presence events.
Definition core.h:202