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