summaryrefslogtreecommitdiff
path: root/libgpsmm.cpp
blob: c2d9eac5e5fc1cf13e802ae986a9c91c844eb086 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright (C) 2005 Alfredo Pironti
 *
 * This software is distributed under a BSD-style license. See the
 * file "COPYING" in the top-level directory of the disribution for details.
 *
 */
#include <cstdlib>

#include "gpsd_config.h"
#include "libgpsmm.h"


gpsmm::gpsmm() :
		#if GPSD_API_MAJOR_VERSION < 5
			_gps_data(0),
		#endif
			to_user(0)
	{
	}


struct gps_data_t* gpsmm::open(void) {
	return open("localhost",DEFAULT_GPSD_PORT);
}

struct gps_data_t* gpsmm::open(const char *host, const char *port) {
	#if GPSD_API_MAJOR_VERSION < 5
		gps_data = gps_open( host, port );
		const bool err = (gps_data==NULL); //connection not opened
	#else
		const bool err = (gps_open(host, port, gps_data()) != 0);
	#endif
	if ( err ) {
		return NULL;
	}
	else { //connection succesfully opened
		to_user= new struct gps_data_t;
		return backup(); //we return the backup of our internal structure
	}
}

struct gps_data_t* gpsmm::stream(int flags) {
  if (gps_stream(gps_data(),flags, NULL)==-1) {
		return NULL;
	}
	else {
		return backup();
	}
}

struct gps_data_t* gpsmm::send(const char *request) {
	if (gps_send(gps_data(),request)==-1) {
		return NULL;
	}
	else {
		return backup();
	}
}

struct gps_data_t* gpsmm::read(void) {
	if (gps_read(gps_data())<=0) {
		// we return null if there was a read() error, if no data was ready in
		// POLL_NOBLOCK mode, or if the connection is closed by gpsd
		return NULL;
	}
	else {
		return backup();
	}
}

int gpsmm::close(void) {
	return gps_close(gps_data());
}

bool gpsmm::waiting(void) {
	return gps_waiting(gps_data());
}

void gpsmm::clear_fix(void) {
	gps_clear_fix(&(gps_data()->fix));
}

void gpsmm::enable_debug(int level, FILE *fp) {
#ifdef CLIENTDEBUG_ENABLE
	gps_enable_debug(level, fp);
#endif /* CLIENTDEBUG_ENABLE */
}

gpsmm::~gpsmm() {
	if ( to_user != NULL ) {
		gps_close(gps_data());
		delete to_user;
	}
}