libgpsmm.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (C) 2005 Alfredo Pironti
  3. *
  4. * SPDX-License-Identifier: BSD-2-clause
  5. */
  6. #include "gpsd_config.h" /* must be before all includes */
  7. #include <cstdlib>
  8. #include "libgpsmm.h"
  9. struct gps_data_t* gpsmm::gps_inner_open(const char *host, const char *port)
  10. {
  11. const bool err = (gps_open(host, port, gps_state()) != 0);
  12. if ( err ) {
  13. to_user = NULL;
  14. return NULL;
  15. }
  16. else { // connection successfully opened
  17. to_user= new struct gps_data_t;
  18. return backup(); //we return the backup of our internal structure
  19. }
  20. }
  21. struct gps_data_t* gpsmm::stream(int flags)
  22. {
  23. if (to_user == NULL)
  24. return NULL;
  25. else if (gps_stream(gps_state(),flags, NULL)==-1) {
  26. return NULL;
  27. }
  28. else {
  29. return backup();
  30. }
  31. }
  32. struct gps_data_t* gpsmm::send(const char *request)
  33. {
  34. if (gps_send(gps_state(),request)==-1) {
  35. return NULL;
  36. }
  37. else {
  38. return backup();
  39. }
  40. }
  41. struct gps_data_t* gpsmm::read(void)
  42. {
  43. if (gps_read(gps_state(), NULL, 0)<=0) {
  44. // we return null if there was a read() error, if no
  45. // data was ready in POLL_NOBLOCK mode, or if the
  46. // connection is closed by gpsd
  47. return NULL;
  48. }
  49. else {
  50. return backup();
  51. }
  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. #ifdef CLIENTDEBUG_ENABLE
  70. gps_enable_debug(level, fp);
  71. #endif /* CLIENTDEBUG_ENABLE */
  72. }
  73. // cppcheck-suppress unusedFunction
  74. bool gpsmm::is_open(void)
  75. {
  76. return to_user != NULL;
  77. }
  78. gpsmm::~gpsmm()
  79. {
  80. if ( to_user != NULL ) {
  81. (void)gps_close(gps_state());
  82. delete to_user;
  83. }
  84. }