libgpsmm.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2005 Alfredo Pironti
  3. *
  4. * This file is Copyright 2005 by the GPSD project
  5. * SPDX-License-Identifier: BSD-2-clause
  6. */
  7. #include "../include/gpsd_config.h" // must be before all includes
  8. #include <assert.h> // for assert()
  9. #include <cstdlib>
  10. #include "../include/libgpsmm.h"
  11. struct gps_data_t* gpsmm::gps_inner_open(const char *host, const char *port)
  12. {
  13. if (0 != (gps_open(host, port, gps_state()))) {
  14. to_user = NULL;
  15. return NULL;
  16. }
  17. // else, connection successfully opened
  18. to_user = new struct gps_data_t;
  19. // prevent CWE-690 warning: dereference of possibly-NULL pinter
  20. assert(NULL != to_user);
  21. return backup(); // we return the backup of our internal structure
  22. }
  23. struct gps_data_t* gpsmm::stream(int flags)
  24. {
  25. if (NULL == to_user) {
  26. return NULL;
  27. }
  28. if (-1 == gps_stream(gps_state(),flags, NULL)) {
  29. return NULL;
  30. }
  31. // else
  32. return backup();
  33. }
  34. struct gps_data_t* gpsmm::send(const char *request)
  35. {
  36. if (-1 == gps_send(gps_state(),request)) {
  37. return NULL;
  38. }
  39. // else
  40. return backup();
  41. }
  42. struct gps_data_t* gpsmm::read(void)
  43. {
  44. if (0 >= gps_read(gps_state(), NULL, 0)) {
  45. // we return null if there was a read() error, or
  46. // if no data is ready in POLL_NOBLOCK (default) mode, or
  47. // if the connection is closed by gpsd
  48. return NULL;
  49. }
  50. // else
  51. return backup();
  52. }
  53. bool gpsmm::waiting(int timeout)
  54. {
  55. return gps_waiting(gps_state(), timeout);
  56. }
  57. const char *gpsmm::data(void)
  58. {
  59. return gps_data(gps_state());
  60. }
  61. // cppcheck-suppress unusedFunction
  62. void gpsmm::clear_fix(void)
  63. {
  64. gps_clear_fix(&(gps_state()->fix));
  65. }
  66. // cppcheck-suppress unusedFunction
  67. void gpsmm::enable_debug(int level, FILE *fp)
  68. {
  69. gps_enable_debug(level, fp);
  70. }
  71. // cppcheck-suppress unusedFunction
  72. bool gpsmm::is_open(void)
  73. {
  74. return to_user != NULL;
  75. }
  76. gpsmm::~gpsmm()
  77. {
  78. if (NULL != to_user) {
  79. (void)gps_close(gps_state());
  80. delete to_user;
  81. }
  82. }
  83. // vim: set expandtab shiftwidth=4