gpspacket.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Foreign function interface binding for the packet module.
  3. *
  4. * This file is Copyright 2010 by the GPSD project
  5. * SPDX-License-Identifier: BSD-2-clause
  6. *
  7. */
  8. #include "../include/gpsd_config.h" /* must be before all includes */
  9. #include <assert.h>
  10. #include <ctype.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <sys/types.h>
  14. #include <unistd.h>
  15. #include "../include/gpsd.h"
  16. #include "../include/strfuncs.h"
  17. #define LOG_SHOUT 0
  18. struct gps_lexer_t *ffi_Lexer_init(void); // For FFI Python interface.
  19. void gpsd_vlog(const struct gpsd_errout_t*, const int, char*,
  20. size_t, const char*, va_list);
  21. static void visibilize(char *outbuf, size_t outlen, const char *inbuf,
  22. size_t inlen) {
  23. const char *sp;
  24. outbuf[0] = '\0';
  25. for (sp = inbuf; sp < inbuf + inlen && strlen(outbuf) + 6 < outlen; sp++)
  26. if (isprint((unsigned char)*sp) || (sp[0] == '\n' && sp[1] == '\0') ||
  27. (sp[0] == '\r' && sp[2] == '\0'))
  28. (void)snprintf(outbuf + strlen(outbuf), 2, "%c", *sp);
  29. else
  30. (void)snprintf(outbuf + strlen(outbuf), 6, "\\x%02x",
  31. 0x00ff & (unsigned)*sp);
  32. }
  33. /* assemble msg in vprintf(3) style, use errout hook or syslog for delivery */
  34. void gpsd_vlog(const struct gpsd_errout_t *errout, const int errlevel,
  35. char *outbuf, size_t outlen, const char *fmt, va_list ap)
  36. {
  37. if (errout->debug >= errlevel) {
  38. char buf[BUFSIZ];
  39. char *err_str;
  40. switch (errlevel) {
  41. case LOG_ERROR:
  42. err_str = "ERROR: ";
  43. break;
  44. case LOG_SHOUT:
  45. err_str = "SHOUT: ";
  46. break;
  47. case LOG_WARN:
  48. err_str = "WARN: ";
  49. break;
  50. case LOG_CLIENT:
  51. err_str = "CLIENT: ";
  52. break;
  53. case LOG_INF:
  54. err_str = "INFO: ";
  55. break;
  56. case LOG_DATA:
  57. err_str = "DATA: ";
  58. break;
  59. case LOG_PROG:
  60. err_str = "PROG: ";
  61. break;
  62. case LOG_IO:
  63. err_str = "IO: ";
  64. break;
  65. case LOG_SPIN:
  66. err_str = "SPIN: ";
  67. break;
  68. case LOG_RAW:
  69. err_str = "RAW: ";
  70. break;
  71. default:
  72. err_str = "UNK: ";
  73. }
  74. assert(errout->label != NULL);
  75. (void)strlcpy(buf, errout->label, sizeof(buf));
  76. (void)strlcat(buf, ":", sizeof(buf));
  77. (void)strlcat(buf, err_str, sizeof(buf));
  78. str_vappendf(buf, sizeof(buf), fmt, ap);
  79. visibilize(outbuf, outlen, buf, strlen(buf));
  80. if (getpid() == getsid(getpid()))
  81. syslog((errlevel <= LOG_SHOUT) ? LOG_ERR : LOG_NOTICE, "%s", outbuf);
  82. else if (errout->report != NULL)
  83. errout->report(outbuf);
  84. else
  85. (void)fputs(outbuf, stderr);
  86. }
  87. }
  88. /* assemble msg in printf(3) style, use errout hook or syslog for delivery */
  89. void gpsd_log(const int errlevel, const struct gpsd_errout_t *errout,
  90. const char *fmt, ...)
  91. {
  92. char buf[BUFSIZ];
  93. va_list ap;
  94. // cppcheck wants is to check for NULL == errout. Should never happen?
  95. if (NULL == errout || errout->debug < errlevel) {
  96. /* nothing to do, get out */
  97. return;
  98. }
  99. buf[0] = '\0';
  100. va_start(ap, fmt);
  101. gpsd_vlog(errout, errlevel, buf, sizeof(buf), fmt, ap);
  102. va_end(ap);
  103. }
  104. static void basic_report(const char *buf) {
  105. (void)fputs(buf, stderr);
  106. }
  107. void errout_reset(struct gpsd_errout_t *errout) {
  108. errout->debug = LOG_SHOUT;
  109. errout->report = basic_report;
  110. }
  111. size_t fvi_size_lexer = sizeof(struct gps_lexer_t);
  112. size_t fvi_size_buffer = (MAX_PACKET_LENGTH * 2) + 1;
  113. struct gps_lexer_t *ffi_Lexer_init() {
  114. struct gps_lexer_t *result;
  115. result = calloc(1, fvi_size_lexer);
  116. if (result == NULL) {
  117. return NULL;
  118. }
  119. packet_reset(result);
  120. return result;
  121. }
  122. // vim: set expandtab shiftwidth=4