errno.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <pjsip-simple/errno.h>
  20. #include <pj/string.h>
  21. /* PJSIP-SIMPLE's own error codes/messages
  22. * MUST KEEP THIS ARRAY SORTED!!
  23. * Message must be limited to 64 chars!
  24. */
  25. #if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING != 0)
  26. static const struct
  27. {
  28. int code;
  29. const char *msg;
  30. } err_str[] =
  31. {
  32. /* Event errors */
  33. { PJSIP_SIMPLE_ENOPKG, "No SIP event package with the specified name" },
  34. { PJSIP_SIMPLE_EPKGEXISTS, "SIP event package already exist" },
  35. /* Presence errors */
  36. { PJSIP_SIMPLE_ENOTSUBSCRIBE, "Expecting SUBSCRIBE request" },
  37. { PJSIP_SIMPLE_ENOPRESENCE, "No presence associated with the subscription" },
  38. { PJSIP_SIMPLE_ENOPRESENCEINFO, "No presence info in the server subscription" },
  39. { PJSIP_SIMPLE_EBADCONTENT, "Bad Content-Type for presence" },
  40. { PJSIP_SIMPLE_EBADPIDF, "Bad PIDF content for presence" },
  41. { PJSIP_SIMPLE_EBADXPIDF, "Bad XPIDF content for presence" },
  42. { PJSIP_SIMPLE_EBADRPID, "Invalid or bad RPID document"},
  43. /* isComposing errors. */
  44. { PJSIP_SIMPLE_EBADISCOMPOSE, "Bad isComposing indication/XML message" },
  45. };
  46. #endif /* PJ_HAS_ERROR_STRING */
  47. /*
  48. * pjsipsimple_strerror()
  49. */
  50. PJ_DEF(pj_str_t) pjsipsimple_strerror( pj_status_t statcode,
  51. char *buf, pj_size_t bufsize )
  52. {
  53. pj_str_t errstr;
  54. #if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING != 0)
  55. if (statcode >= PJSIP_SIMPLE_ERRNO_START &&
  56. statcode < PJSIP_SIMPLE_ERRNO_START + PJ_ERRNO_SPACE_SIZE)
  57. {
  58. /* Find the error in the table.
  59. * Use binary search!
  60. */
  61. int first = 0;
  62. int n = PJ_ARRAY_SIZE(err_str);
  63. while (n > 0) {
  64. int half = n/2;
  65. int mid = first + half;
  66. if (err_str[mid].code < statcode) {
  67. first = mid+1;
  68. n -= (half+1);
  69. } else if (err_str[mid].code > statcode) {
  70. n = half;
  71. } else {
  72. first = mid;
  73. break;
  74. }
  75. }
  76. if (PJ_ARRAY_SIZE(err_str) && err_str[first].code == statcode) {
  77. pj_str_t msg;
  78. msg.ptr = (char*)err_str[first].msg;
  79. msg.slen = pj_ansi_strlen(err_str[first].msg);
  80. errstr.ptr = buf;
  81. pj_strncpy_with_null(&errstr, &msg, bufsize);
  82. return errstr;
  83. }
  84. }
  85. #endif /* PJ_HAS_ERROR_STRING */
  86. /* Error not found. */
  87. errstr.ptr = buf;
  88. errstr.slen = pj_ansi_snprintf(buf, bufsize,
  89. "Unknown pjsip-simple error %d",
  90. statcode);
  91. if (errstr.slen < 1 || errstr.slen >= (pj_ssize_t)bufsize)
  92. errstr.slen = bufsize - 1;
  93. return errstr;
  94. }