tsip_parser_header_Subscription_State.rl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_header_Subscription_State.c
  23. * @brief SIP Subscription_State header.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_Subscription_State.h"
  29. #include "tinysip/parsers/tsip_parser_uri.h"
  30. #include "tsk_debug.h"
  31. #include "tsk_memory.h"
  32. /***********************************
  33. * Ragel state machine.
  34. */
  35. %%{
  36. machine tsip_machine_parser_header_Subscription_State;
  37. # Includes
  38. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  39. action tag{
  40. tag_start = p;
  41. }
  42. action parse_state{
  43. TSK_PARSER_SET_STRING(hdr_Subscription_State->state);
  44. }
  45. action parse_reason{
  46. TSK_PARSER_SET_STRING(hdr_Subscription_State->reason);
  47. }
  48. action parse_expires{
  49. TSK_PARSER_SET_INTEGER(hdr_Subscription_State->expires);
  50. }
  51. action parse_retry_after{
  52. TSK_PARSER_SET_INTEGER(hdr_Subscription_State->retry_after);
  53. }
  54. action parse_param{
  55. TSK_PARSER_ADD_PARAM(TSIP_HEADER_PARAMS(hdr_Subscription_State));
  56. }
  57. action eob{
  58. }
  59. subexp_params = (( "reason"i EQUAL token>tag %parse_reason ) | ( "expires"i EQUAL delta_seconds>tag %parse_expires ) | ( "retry-after"i EQUAL delta_seconds>tag %parse_retry_after ))@1 | generic_param>tag %parse_param @0;
  60. Subscription_State = ( "Subscription-State"i ) HCOLON token>tag %parse_state ( SEMI subexp_params )*;
  61. # Entry point
  62. main := Subscription_State :>CRLF @eob;
  63. }%%
  64. tsip_header_Subscription_State_t* tsip_header_Subscription_State_create()
  65. {
  66. return tsk_object_new(tsip_header_Subscription_State_def_t);
  67. }
  68. int tsip_header_Subscription_State_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  69. {
  70. if(header){
  71. const tsip_header_Subscription_State_t *Subscription_State = (const tsip_header_Subscription_State_t *)header;
  72. int ret;
  73. ret = tsk_buffer_append_2(output, "%s%s%s",
  74. Subscription_State->state,
  75. Subscription_State->reason ? ";reason=" : "",
  76. Subscription_State->reason ? Subscription_State->reason : ""
  77. );
  78. if(!ret && Subscription_State->expires>=0){
  79. ret = tsk_buffer_append_2(output, ";expires=%d", Subscription_State->expires);
  80. }
  81. if(!ret && Subscription_State->retry_after>=0){
  82. ret = tsk_buffer_append_2(output, ";retry-after=%d", Subscription_State->retry_after);
  83. }
  84. return ret;
  85. }
  86. return -1;
  87. }
  88. tsip_header_Subscription_State_t *tsip_header_Subscription_State_parse(const char *data, tsk_size_t size)
  89. {
  90. int cs = 0;
  91. const char *p = data;
  92. const char *pe = p + size;
  93. const char *eof = pe;
  94. tsip_header_Subscription_State_t *hdr_Subscription_State = tsip_header_Subscription_State_create();
  95. const char *tag_start = tsk_null;
  96. %%write data;
  97. (void)(eof);
  98. (void)(tsip_machine_parser_header_Subscription_State_first_final);
  99. (void)(tsip_machine_parser_header_Subscription_State_error);
  100. (void)(tsip_machine_parser_header_Subscription_State_en_main);
  101. %%write init;
  102. %%write exec;
  103. if( cs < %%{ write first_final; }%% ){
  104. TSK_DEBUG_ERROR("Failed to parse 'Subscription-State' header.");
  105. TSK_OBJECT_SAFE_FREE(hdr_Subscription_State);
  106. }
  107. return hdr_Subscription_State;
  108. }
  109. //========================================================
  110. // Subscription_State header object definition
  111. //
  112. static tsk_object_t* tsip_header_Subscription_State_ctor(tsk_object_t *self, va_list * app)
  113. {
  114. tsip_header_Subscription_State_t *Subscription_State = self;
  115. if(Subscription_State){
  116. TSIP_HEADER(Subscription_State)->type = tsip_htype_Subscription_State;
  117. TSIP_HEADER(Subscription_State)->serialize = tsip_header_Subscription_State_serialize;
  118. Subscription_State->expires = -1;
  119. Subscription_State->retry_after = -1;
  120. }
  121. else{
  122. TSK_DEBUG_ERROR("Failed to create new Subscription_State header.");
  123. }
  124. return self;
  125. }
  126. static tsk_object_t* tsip_header_Subscription_State_dtor(tsk_object_t *self)
  127. {
  128. tsip_header_Subscription_State_t *Subscription_State = self;
  129. if(Subscription_State){
  130. TSK_FREE(Subscription_State->state);
  131. TSK_FREE(Subscription_State->reason);
  132. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(Subscription_State));
  133. }
  134. else{
  135. TSK_DEBUG_ERROR("Null Subscription_State header.");
  136. }
  137. return self;
  138. }
  139. static const tsk_object_def_t tsip_header_Subscription_State_def_s =
  140. {
  141. sizeof(tsip_header_Subscription_State_t),
  142. tsip_header_Subscription_State_ctor,
  143. tsip_header_Subscription_State_dtor,
  144. tsk_null
  145. };
  146. const tsk_object_def_t *tsip_header_Subscription_State_def_t = &tsip_header_Subscription_State_def_s;