qos_cb.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2007 SOMA Networks, Inc.
  5. * Written by Ovidiu Sas (osas)
  6. *
  7. * This file is part of Kamailio, a free SIP server.
  8. *
  9. * Kamailio is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * Kamailio is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * History:
  24. * --------
  25. * 2007-07-14 initial version (osas)
  26. */
  27. #include "../../mem/shm_mem.h"
  28. #include "../../dprint.h"
  29. #include "qos_ctx_helpers.h"
  30. #include "qos_cb.h"
  31. static struct qos_head_cbl* create_cbs = 0;
  32. static struct qos_cb_params params = {NULL, NULL, 0, NULL};
  33. int init_qos_callbacks(void)
  34. {
  35. create_cbs = (struct qos_head_cbl*)shm_malloc(sizeof(struct qos_head_cbl));
  36. if (create_cbs==0) {
  37. LM_ERR("no more shm mem\n");
  38. return -1;
  39. }
  40. create_cbs->first = 0;
  41. create_cbs->types = 0;
  42. return 0;
  43. }
  44. void destroy_qos_callbacks_list(struct qos_callback *cb)
  45. {
  46. struct qos_callback *cb_t;
  47. while(cb) {
  48. cb_t = cb;
  49. cb = cb->next;
  50. /* FIXME - what about parameters ? */
  51. LM_DBG("freeing cp=%p\n", cb_t);
  52. shm_free(cb_t);
  53. }
  54. }
  55. void destroy_qos_callbacks(void)
  56. {
  57. if (create_cbs==0)
  58. return;
  59. destroy_qos_callbacks_list(create_cbs->first);
  60. shm_free(create_cbs);
  61. create_cbs = 0;
  62. }
  63. int register_qoscb(qos_ctx_t *qos, int types, qos_cb f, void *param )
  64. {
  65. struct qos_callback *cb;
  66. LM_DBG("registering qos CB\n");
  67. if ( types&QOSCB_CREATED ) {
  68. if (types!=QOSCB_CREATED) {
  69. LM_CRIT("QOSCB_CREATED type must be register alone!\n");
  70. return -1;
  71. }
  72. } else {
  73. if (qos==0) {
  74. LM_CRIT("non-QOSCB_CREATED type "
  75. "must be register to a qos (qos missing)!\n");
  76. return -1;
  77. }
  78. }
  79. cb = (struct qos_callback*)shm_malloc(sizeof(struct qos_callback));
  80. if (cb==0) {
  81. LM_ERR("no more shm mem\n");
  82. return -1;
  83. }
  84. LM_DBG("cb=%p\n", cb);
  85. cb->types = types;
  86. cb->callback = f;
  87. cb->param = param;
  88. if ( types&QOSCB_CREATED ) {
  89. cb->next = create_cbs->first;
  90. create_cbs->first = cb;
  91. create_cbs->types |= types;
  92. } else {
  93. cb->next = qos->cbs.first;
  94. qos->cbs.first = cb;
  95. qos->cbs.types |= types;
  96. LM_DBG("qos=%p qos->cbs=%p types=%d\n",
  97. qos, &(qos->cbs), qos->cbs.types);
  98. }
  99. return 0;
  100. }
  101. void run_create_cbs(struct qos_ctx_st *qos, struct sip_msg *msg)
  102. {
  103. struct qos_callback *cb;
  104. if (create_cbs->first==0)
  105. return;
  106. params.msg = msg;
  107. /* avoid garbage due static structure */
  108. params.sdp = NULL;
  109. params.role = 0;
  110. params.param = NULL;
  111. for ( cb=create_cbs->first; cb; cb=cb->next) {
  112. LM_DBG("qos=%p\n",qos);
  113. params.param = &cb->param;
  114. cb->callback( qos, QOSCB_CREATED, &params );
  115. }
  116. return;
  117. }
  118. void run_qos_callbacks(int type, struct qos_ctx_st *qos,
  119. struct qos_sdp_st *sdp, unsigned int role,
  120. struct sip_msg *msg)
  121. {
  122. struct qos_callback *cb;
  123. if (qos == NULL)
  124. return;
  125. LM_DBG("qos=%p qos->cbs=%p, qos->cbs.types=%d\n",
  126. qos, &(qos->cbs), qos->cbs.types);
  127. if (qos->cbs.first==0 || ((qos->cbs.types)&type)==0 )
  128. return;
  129. params.sdp = sdp;
  130. params.role = role;
  131. params.msg = msg;
  132. LM_DBG("searching in %p\n", qos->cbs.first);
  133. for ( cb=qos->cbs.first; cb; cb=cb->next) {
  134. if ( (cb->types)&type ) {
  135. LM_DBG("qos=%p, type=%d\n", qos, type);
  136. params.param = &cb->param;
  137. cb->callback( qos, type, &params );
  138. }
  139. }
  140. return;
  141. }