tsip_action.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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_action.h
  23. * @brief SIP action.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. * @date Created: Sat Nov 8 16:54:58 2009 mdiop
  28. */
  29. #include "tinysip/tsip_action.h"
  30. #include "tsk_string.h"
  31. #include "tsk_memory.h"
  32. #include "tsk_debug.h"
  33. /* Local functions */
  34. tsip_action_t* _tsip_action_create(tsip_action_type_t type, va_list* app);
  35. int _tsip_action_set(tsip_action_handle_t* self, va_list* app);
  36. /**@defgroup tsip_action_group SIP action (Sending/Receiving Requests)
  37. */
  38. /**@ingroup tsip_action_group
  39. * Creates new SIP action handle.
  40. * @param type The type of the action to create.
  41. * @param ... Any TSIP_ACTION_SET_*() macros. e.g. @ref TSIP_ACTION_SET_HEADER(). MUST always ends with @ref TSIP_ACTION_SET_NULL().
  42. * @retval A valid SIP handle if succeed and Null otherwise.
  43. *
  44. * @code
  45. tsip_action_handle_t* handle;
  46. handle = tsip_action_create(tsip_atype_config,
  47. TSIP_ACTION_SET_HEADER("User-Agent", "IM-client/OMA1.0 doubango/v1.0.0"),
  48. TSIP_ACTION_SET_HEADER("Supported", "precondition"),
  49. TSIP_ACTION_SET_PAYLOAD("my payload", strlen("my payload")),
  50. TSIP_ACTION_SET_NULL());
  51. // This action handle could be used to configure an outgoing request
  52. // by using @ref TSIP_ACTION_SET_CONFIG() like this:
  53. // tsip_action_PUBLISH(session,
  54. // TSIP_ACTION_SET_CONFIG(handle),
  55. // TSIP_ACTION_SET_NULL());
  56. //
  57. // in this case only the initial outgoing PUBLISH will have these headers and this
  58. // payload
  59. //
  60. //
  61. // To destroy the handle
  62. TSK_OBJECT_SAFE_FREE(handle);
  63. * @endcode
  64. */
  65. tsip_action_handle_t* tsip_action_create(tsip_action_type_t type, ...)
  66. {
  67. va_list ap;
  68. tsip_action_t* handle;
  69. va_start(ap, type);
  70. handle = _tsip_action_create(type, &ap);
  71. va_end(ap);
  72. return handle;
  73. }
  74. /**@ingroup tsip_action_group
  75. * Configures a SIP action handle.
  76. * @param self A pointer to the action to configure.
  77. * @param ... Any TSIP_ACTION_SET_*() macros. e.g. @ref TSIP_ACTION_SET_HEADER(). MUST always ends with @ref TSIP_ACTION_SET_NULL().
  78. * @retval Zero if succeed and non-zero error code otherwise.
  79. *
  80. * @code
  81. int ret = tsip_action_set(handle,
  82. TSIP_ACTION_SET_HEADER("User-Agent", "IM-client/OMA1.0 doubango/v1.0.0"),
  83. TSIP_ACTION_SET_HEADER("Supported", "precondition"),
  84. TSIP_ACTION_SET_PAYLOAD("my payload", strlen("my payload")),
  85. TSIP_ACTION_SET_NULL());
  86. //... whatever
  87. // To destroy the handle
  88. TSK_OBJECT_SAFE_FREE(handle);
  89. * @endcode
  90. */
  91. int tsip_action_set(tsip_action_handle_t* self, ...)
  92. {
  93. int ret;
  94. va_list ap;
  95. va_start(ap, self);
  96. ret = _tsip_action_set(self, &ap);
  97. va_end(ap);
  98. return ret;
  99. }
  100. /** internal fuction used to config a SIP action */
  101. int _tsip_action_set(tsip_action_handle_t* self, va_list* app)
  102. {
  103. tsip_action_param_type_t curr;
  104. tsip_action_t* action = self;
  105. if(!action) { /* Nothing to do */
  106. return 0;
  107. }
  108. while((curr = va_arg(*app, tsip_action_param_type_t)) != aptype_null) {
  109. switch(curr) {
  110. case aptype_header: {
  111. /* (const char*)NAME_STR, (const char*)VALUE_STR */
  112. const char* name = va_arg(*app, const char *);
  113. const char* value = va_arg(*app, const char *);
  114. tsk_params_add_param(&action->headers, name, value);
  115. break;
  116. }
  117. case aptype_config: {
  118. /* (const tsip_action_handle_t*)ACTION_CONFIG_HANDLE */
  119. const tsip_action_t* handle = va_arg(*app, const tsip_action_handle_t *);
  120. if(handle && handle->type == tsip_atype_config) {
  121. /* Copy headers */
  122. if(!TSK_LIST_IS_EMPTY(handle->headers)) {
  123. tsk_list_pushback_list(action->headers, handle->headers);
  124. }
  125. /* Copy payload */
  126. if(handle->payload && handle->payload->data && handle->payload->size) {
  127. TSK_OBJECT_SAFE_FREE(action->payload);
  128. action->payload = tsk_buffer_create(handle->payload->data, handle->payload->size);
  129. }
  130. /* Copy resp line */
  131. action->line_resp.code = handle->line_resp.code;
  132. tsk_strupdate(&action->line_resp.phrase, handle->line_resp.phrase);
  133. /* Copy media type */
  134. action->media.type = handle->media.type;
  135. /* Copy media params */
  136. if(!TSK_LIST_IS_EMPTY(handle->media.params)) {
  137. if(!action->media.params) {
  138. action->media.params = tmedia_params_create();
  139. }
  140. tsk_list_pushback_list(action->media.params, handle->media.params);
  141. }
  142. }
  143. else if(handle) { /* Only invalid type should cause error */
  144. TSK_DEBUG_ERROR("Invalid action configuration handle.");
  145. return -2;
  146. }
  147. break;
  148. }
  149. case aptype_payload: {
  150. /* (const void*)PAY_PTR, (tsk_size_t)PAY_SIZE */
  151. const void* payload = va_arg(*app, const void *);
  152. tsk_size_t size = va_arg(*app, tsk_size_t);
  153. if(payload && size) {
  154. TSK_OBJECT_SAFE_FREE(action->payload);
  155. action->payload = tsk_buffer_create(payload, size);
  156. }
  157. break;
  158. }
  159. case aptype_resp_line: {
  160. /* (int32_t)CODE_INT, (const char*)PHRASE_STR */
  161. int32_t code = va_arg(*app, int32_t);
  162. const char* phrase = va_arg(*app, const void *);
  163. action->line_resp.code = (short)code;
  164. tsk_strupdate(&action->line_resp.phrase, phrase);
  165. break;
  166. }
  167. case aptype_media_type: {
  168. /* (enum tmedia_type_e)TYPE_ENUM */
  169. action->media.type = va_arg(*app, tmedia_type_t);
  170. break;
  171. }
  172. case aptype_media: {
  173. /* ... */
  174. tmedia_params_L_t* params;
  175. if((params = tmedia_params_create_2(app))) {
  176. if(action->media.params) {
  177. tsk_list_pushback_list(action->media.params, params);
  178. }
  179. else {
  180. action->media.params = tsk_object_ref(params);
  181. }
  182. TSK_OBJECT_SAFE_FREE(params);
  183. }
  184. break;
  185. }
  186. default: {
  187. /* va_list will be unsafe ==> exit */
  188. TSK_DEBUG_ERROR("NOT SUPPORTED.");
  189. return -3;
  190. }
  191. } /* switch */
  192. } /* while */
  193. return 0;
  194. }
  195. /** internal function used to create new SIP action */
  196. tsip_action_t* _tsip_action_create(tsip_action_type_t type, va_list* app)
  197. {
  198. tsip_action_t* action = tsk_null;
  199. /* create the action */
  200. if(!(action = tsk_object_new(tsip_action_def_t))) {
  201. TSK_DEBUG_ERROR("Failed to create new SIP action.");
  202. return tsk_null;
  203. }
  204. else {
  205. action->type = type;
  206. }
  207. /* configure the action */
  208. if(_tsip_action_set(action, app)) {
  209. TSK_DEBUG_ERROR("Invalid parameter");
  210. TSK_OBJECT_SAFE_FREE(action);
  211. }
  212. return action;
  213. }
  214. //=================================================================================================
  215. // SIP action object definition
  216. //
  217. static tsk_object_t* tsip_action_ctor(tsk_object_t * self, va_list * app)
  218. {
  219. tsip_action_t *action = self;
  220. if(action) {
  221. action->headers = tsk_list_create();
  222. action->media.type = tmedia_none;
  223. }
  224. return self;
  225. }
  226. static tsk_object_t* tsip_action_dtor(tsk_object_t * self)
  227. {
  228. tsip_action_t *action = self;
  229. if(action) {
  230. TSK_OBJECT_SAFE_FREE(action->headers);
  231. TSK_OBJECT_SAFE_FREE(action->payload);
  232. TSK_OBJECT_SAFE_FREE(action->media.params);
  233. TSK_FREE(action->line_resp.phrase);
  234. TSK_FREE(action->ect.to);
  235. }
  236. return self;
  237. }
  238. static const tsk_object_def_t tsip_action_def_s = {
  239. sizeof(tsip_action_t),
  240. tsip_action_ctor,
  241. tsip_action_dtor,
  242. tsk_null,
  243. };
  244. const tsk_object_def_t *tsip_action_def_t = &tsip_action_def_s;