From d96688e27a0e7d8c06ac3e399718a217daf6994e Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Mon, 21 Jul 2008 18:12:51 +0000 Subject: Ticket #576: Added user data in pjsua account and buddy git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@2162 74dad513-b988-da41-8d7b-12977e46ad98 --- pjsip/include/pjsua-lib/pjsua.h | 69 ++++++++++++++++++++++++++++++++ pjsip/include/pjsua-lib/pjsua_internal.h | 1 + pjsip/src/pjsua-lib/pjsua_acc.c | 34 +++++++++++++++- pjsip/src/pjsua-lib/pjsua_pres.c | 68 +++++++++++++++++++++++++++++++ 4 files changed, 171 insertions(+), 1 deletion(-) diff --git a/pjsip/include/pjsua-lib/pjsua.h b/pjsip/include/pjsua-lib/pjsua.h index 326f5306..0d6322ea 100644 --- a/pjsip/include/pjsua-lib/pjsua.h +++ b/pjsip/include/pjsua-lib/pjsua.h @@ -1999,6 +1999,13 @@ PJ_DECL(pj_status_t) pjsua_transport_close( pjsua_transport_id id, */ typedef struct pjsua_acc_config { + /** + * Arbitrary user data to be associated with the newly created account. + * Application may set this later with #pjsua_acc_set_user_data() and + * retrieve it with #pjsua_acc_get_user_data(). + */ + void *user_data; + /** * Account priority, which is used to control the order of matching * incoming/outgoing requests. The higher the number means the higher @@ -2395,6 +2402,29 @@ PJ_DECL(pj_status_t) pjsua_acc_add_local(pjsua_transport_id tid, pj_bool_t is_default, pjsua_acc_id *p_acc_id); +/** + * Set arbitrary data to be associated with the account. + * + * @param acc_id The account ID. + * @param user_data User/application data. + * + * @return PJ_SUCCESS on success, or the appropriate error code. + */ +PJ_DECL(pj_status_t) pjsua_acc_set_user_data(pjsua_acc_id acc_id, + void *user_data); + + +/** + * Retrieve arbitrary data associated with the account. + * + * @param acc_id The account ID. + * + * @return The user data. In the case where the account ID is + * not valid, NULL is returned. + */ +PJ_DECL(void*) pjsua_acc_get_user_data(pjsua_acc_id acc_id); + + /** * Delete an account. This will unregister the account from the SIP server, * if necessary, and terminate server side presence subscriptions associated @@ -3359,6 +3389,12 @@ typedef struct pjsua_buddy_config */ pj_bool_t subscribe; + /** + * Specify arbitrary application data to be associated with with + * the buddy object. + */ + void *user_data; + } pjsua_buddy_config; @@ -3517,6 +3553,16 @@ PJ_DECL(pj_bool_t) pjsua_buddy_is_valid(pjsua_buddy_id buddy_id); PJ_DECL(pj_status_t) pjsua_enum_buddies(pjsua_buddy_id ids[], unsigned *count); +/** + * Find the buddy ID with the specified URI. + * + * @param uri The buddy URI. + * + * @return The buddy ID, or PJSUA_INVALID_ID if not found. + */ +PJ_DECL(pjsua_buddy_id) pjsua_buddy_find(const pj_str_t *uri); + + /** * Get detailed buddy info. * @@ -3534,6 +3580,29 @@ PJ_DECL(pj_status_t) pjsua_enum_buddies(pjsua_buddy_id ids[], PJ_DECL(pj_status_t) pjsua_buddy_get_info(pjsua_buddy_id buddy_id, pjsua_buddy_info *info); +/** + * Set the user data associated with the buddy object. + * + * @param buddy_id The buddy identification. + * @param user_data Arbitrary application data to be associated with + * the buddy object. + * + * @return PJ_SUCCESS on success, or the appropriate error code. + */ +PJ_DECL(pj_status_t) pjsua_buddy_set_user_data(pjsua_buddy_id buddy_id, + void *user_data); + + +/** + * Get the user data associated with the budy object. + * + * @param buddy_id The buddy identification. + * + * @return The application data. + */ +PJ_DECL(void*) pjsua_buddy_get_user_data(pjsua_buddy_id buddy_id); + + /** * Add new buddy to the buddy list. If presence subscription is enabled * for this buddy, this function will also start the presence subscription diff --git a/pjsip/include/pjsua-lib/pjsua_internal.h b/pjsip/include/pjsua-lib/pjsua_internal.h index 7245469c..7e913969 100644 --- a/pjsip/include/pjsua-lib/pjsua_internal.h +++ b/pjsip/include/pjsua-lib/pjsua_internal.h @@ -164,6 +164,7 @@ typedef struct pjsua_buddy { pj_pool_t *pool; /**< Pool for this buddy. */ unsigned index; /**< Buddy index. */ + void *user_data; /**< Application data. */ pj_str_t uri; /**< Buddy URI. */ pj_str_t contact; /**< Contact learned from subscrp. */ pj_str_t name; /**< Buddy name. */ diff --git a/pjsip/src/pjsua-lib/pjsua_acc.c b/pjsip/src/pjsua-lib/pjsua_acc.c index 892d63c4..a234964c 100644 --- a/pjsip/src/pjsua-lib/pjsua_acc.c +++ b/pjsip/src/pjsua-lib/pjsua_acc.c @@ -159,7 +159,6 @@ static pj_status_t initialize_acc(unsigned acc_id) sip_reg_uri = NULL; } - /* Save the user and domain part. These will be used when finding an * account for incoming requests. */ @@ -385,6 +384,39 @@ PJ_DEF(pj_status_t) pjsua_acc_add_local( pjsua_transport_id tid, } +/* + * Set arbitrary data to be associated with the account. + */ +PJ_DEF(pj_status_t) pjsua_acc_set_user_data(pjsua_acc_id acc_id, + void *user_data) +{ + PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), + PJ_EINVAL); + PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP); + + PJSUA_LOCK(); + + pjsua_var.acc[acc_id].cfg.user_data = user_data; + + PJSUA_UNLOCK(); + + return PJ_SUCCESS; +} + + +/* + * Retrieve arbitrary data associated with the account. + */ +PJ_DEF(void*) pjsua_acc_get_user_data(pjsua_acc_id acc_id) +{ + PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), + NULL); + PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, NULL); + + return pjsua_var.acc[acc_id].cfg.user_data; +} + + /* * Delete account. */ diff --git a/pjsip/src/pjsua-lib/pjsua_pres.c b/pjsip/src/pjsua-lib/pjsua_pres.c index 1bf23098..04632f41 100644 --- a/pjsip/src/pjsua-lib/pjsua_pres.c +++ b/pjsip/src/pjsua-lib/pjsua_pres.c @@ -69,6 +69,31 @@ PJ_DEF(unsigned) pjsua_get_buddy_count(void) } +/* + * Find buddy. + */ +PJ_DEF(pjsua_buddy_id) pjsua_buddy_find(const pj_str_t *uri_str) +{ + pj_str_t input; + pj_pool_t *pool; + pjsip_uri *uri; + pjsua_buddy_id buddy_id; + + pool = pjsua_pool_create("buddyfind", 512, 512); + pj_strdup_with_null(pool, &input, uri_str); + + uri = pjsip_parse_uri(pool, input.ptr, input.slen, 0); + if (!uri) + buddy_id = PJSUA_INVALID_ID; + else + buddy_id = pjsua_find_buddy(uri); + + pj_pool_release(pool); + + return buddy_id; +} + + /* * Check if buddy ID is valid. */ @@ -190,6 +215,46 @@ PJ_DEF(pj_status_t) pjsua_buddy_get_info( pjsua_buddy_id buddy_id, return PJ_SUCCESS; } +/* + * Set the user data associated with the buddy object. + */ +PJ_DEF(pj_status_t) pjsua_buddy_set_user_data( pjsua_buddy_id buddy_id, + void *user_data) +{ + PJ_ASSERT_RETURN(buddy_id>=0 && + buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy), + PJ_EINVAL); + + PJSUA_LOCK(); + + pjsua_var.buddy[buddy_id].user_data = user_data; + + PJSUA_UNLOCK(); + + return PJ_SUCCESS; +} + + +/* + * Get the user data associated with the budy object. + */ +PJ_DEF(void*) pjsua_buddy_get_user_data(pjsua_buddy_id buddy_id) +{ + void *user_data; + + PJ_ASSERT_RETURN(buddy_id>=0 && + buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy), + NULL); + + PJSUA_LOCK(); + + user_data = pjsua_var.buddy[buddy_id].user_data; + + PJSUA_UNLOCK(); + + return user_data; +} + /* * Reset buddy descriptor. @@ -289,6 +354,9 @@ PJ_DEF(pj_status_t) pjsua_buddy_add( const pjsua_buddy_config *cfg, if (pjsua_var.buddy[index].port == 0) pjsua_var.buddy[index].port = 5060; + /* Save user data */ + pjsua_var.buddy[index].user_data = (void*)cfg->user_data; + if (p_buddy_id) *p_buddy_id = index; -- cgit v1.2.3