/doubango/tinySIP/src/api/tsip_api_subscribe.c

https://gitlab.com/iwan.aucamp/doubango · C · 160 lines · 93 code · 34 blank · 33 comment · 11 complexity · cb7c3e58ce2ed87f4a133475663a6b7d MD5 · raw file

  1. /*
  2. * Copyright (C) 2010-2011 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * 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
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. /**@file tsip_api_subscribe.c
  23. * @brief Public subscription (SUBSCRIBE) functions.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/api/tsip_api_subscribe.h"
  29. #include "tinysip/dialogs/tsip_dialog_layer.h"
  30. #include "tinysip/dialogs/tsip_dialog_subscribe.h"
  31. #include "tinysip/tsip_action.h"
  32. #include "tsip.h"
  33. #include "tsk_runnable.h"
  34. #include "tsk_debug.h"
  35. #define TSIP_SUBSCRIBE_EVENT_CREATE( type) tsk_object_new(tsip_subscribe_event_def_t, type)
  36. extern tsip_action_t* _tsip_action_create(tsip_action_type_t type, va_list* app);
  37. int tsip_subscribe_event_signal(tsip_subscribe_event_type_t type, tsip_ssession_t* ss, short status_code, const char *phrase, const tsip_message_t* sipmessage)
  38. {
  39. tsip_subscribe_event_t* sipevent = TSIP_SUBSCRIBE_EVENT_CREATE(type);
  40. tsip_event_init(TSIP_EVENT(sipevent), ss, status_code, phrase, sipmessage, tsip_event_subscribe);
  41. TSK_RUNNABLE_ENQUEUE_OBJECT(TSK_RUNNABLE(TSIP_SSESSION(ss)->stack), sipevent);
  42. return 0;
  43. }
  44. int tsip_api_subscribe_send_subscribe(const tsip_ssession_handle_t *ss, ...)
  45. {
  46. const tsip_ssession_t* _ss;
  47. va_list ap;
  48. tsip_action_t* action;
  49. tsip_dialog_t* dialog;
  50. int ret = -1;
  51. if(!(_ss = ss) || !_ss->stack){
  52. TSK_DEBUG_ERROR("Invalid parameter.");
  53. return ret;
  54. }
  55. /* Checks if the stack has been started */
  56. if(!TSK_RUNNABLE(_ss->stack)->started){
  57. TSK_DEBUG_ERROR("Stack not started.");
  58. return -2;
  59. }
  60. va_start(ap, ss);
  61. if((action = _tsip_action_create(tsip_atype_subscribe, &ap))){
  62. if(!(dialog = tsip_dialog_layer_find_by_ss(_ss->stack->layer_dialog, ss))){
  63. dialog = tsip_dialog_layer_new(_ss->stack->layer_dialog, tsip_dialog_SUBSCRIBE, ss);
  64. }
  65. ret = tsip_dialog_fsm_act(dialog, action->type, tsk_null, action);
  66. tsk_object_unref(dialog);
  67. TSK_OBJECT_SAFE_FREE(action);
  68. }
  69. va_end(ap);
  70. return ret;
  71. }
  72. int tsip_api_subscribe_send_unsubscribe(const tsip_ssession_handle_t *ss, ...)
  73. {
  74. const tsip_ssession_t* _ss;
  75. va_list ap;
  76. tsip_action_t* action;
  77. int ret = -1;
  78. if(!(_ss = ss) || !_ss->stack){
  79. TSK_DEBUG_ERROR("Invalid parameter.");
  80. return ret;
  81. }
  82. /* Checks if the stack is running */
  83. if(!TSK_RUNNABLE(_ss->stack)->running){
  84. TSK_DEBUG_ERROR("Stack not running.");
  85. return -2;
  86. }
  87. va_start(ap, ss);
  88. if((action = _tsip_action_create(tsip_atype_unsubscribe, &ap))){
  89. ret = tsip_ssession_handle(ss, action);
  90. TSK_OBJECT_SAFE_FREE(action);
  91. }
  92. va_end(ap);
  93. return 0;
  94. }
  95. //========================================================
  96. // SIP SUBSCRIBE event object definition
  97. //
  98. static tsk_object_t* tsip_subscribe_event_ctor(tsk_object_t * self, va_list * app)
  99. {
  100. tsip_subscribe_event_t *sipevent = self;
  101. if(sipevent){
  102. sipevent->type = va_arg(*app, tsip_subscribe_event_type_t);
  103. }
  104. return self;
  105. }
  106. static tsk_object_t* tsip_subscribe_event_dtor(tsk_object_t * self)
  107. {
  108. tsip_subscribe_event_t *sipevent = self;
  109. if(sipevent){
  110. tsip_event_deinit(TSIP_EVENT(sipevent));
  111. }
  112. return self;
  113. }
  114. static int tsip_subscribe_event_cmp(const tsk_object_t *obj1, const tsk_object_t *obj2)
  115. {
  116. return -1;
  117. }
  118. static const tsk_object_def_t tsip_subscribe_event_def_s =
  119. {
  120. sizeof(tsip_subscribe_event_t),
  121. tsip_subscribe_event_ctor,
  122. tsip_subscribe_event_dtor,
  123. tsip_subscribe_event_cmp,
  124. };
  125. const tsk_object_def_t *tsip_subscribe_event_def_t = &tsip_subscribe_event_def_s;