[go: up one dir, main page]

File: test-gps

package info (click to toggle)
altos 1.9.21-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 83,636 kB
  • sloc: ansic: 119,717; java: 42,907; makefile: 8,459; sh: 4,995; xml: 2,154; pascal: 2,008
file content (103 lines) | stat: -rwxr-xr-x 1,820 bytes parent folder | download | duplicates (7)
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
96
97
98
99
100
101
102
103
#!/usr/bin/nickle

import File;

string timed_read(file f, int timeout) {
	thread reader = fork func() {
		try {
			return fgets(f);
		} catch Thread::signal(int i) {
			return "";
		}
	}();

	thread killer = fork func() {
		try {
			sleep (timeout);
			Thread::send_signal(reader, 1);
		} catch Thread::signal(int i) {
			return;
		}
	}();

	poly v = Thread::join(reader);
	Thread::send_signal(killer, 1);
	Thread::join(killer);
	if (is_string(v))
		return v;
	return "";
}

void flush_input(file f) {
	for (;;) {
		string s = timed_read(f, 200);
		if (s == "")
			break;
	}
}

string[*] gps(file f) {
	string[...] x = {};

	flush_input(f);
	fprintf (f, "g\nv\n");
	flush(f);
	for (;;) {
		string l = timed_read(f, 1000);
		if (l == "") {
			File::fprintf(stderr, "Read timedout\n");
			exit(1);
		}
		x[dim(x)] = l;
		if (String::index(l, "software-version") == 0)
			break;
	}
	return x;
}

string[*] find_gps(string[*] s, string match) {
	for (int i = 0; i < dim(s); i++)
		if (String::index(s[i], match) >= 0)
			return String::wordsplit(s[i], " ");
	return (string[*]) {};
}

bool
do_gps(file f) {

	string[*] i = gps(f);
	string[*] flags = find_gps(i, "Flags:");
	string[*] sats = find_gps(i, "Sats:");

	int actual_flags = string_to_integer(flags[1]);
	int actual_sats = string_to_integer(sats[1]);

	while ((actual_flags & (1 << 4)) == 0) {
		printf("Flags: %s\n", flags[1]);
		printf("Sats: %s\n", sats[1]);

		sleep(1000);
		i = gps(f);
		flags = find_gps(i, "Flags:");
		sats = find_gps(i, "Sats:");

		actual_flags = string_to_integer(flags[1]);
	}

	printf("Flags: %s\n", flags[1]);
	printf("Sats: %s\n", sats[1]);
	printf("GPS locked\n");
	return true;
}

void main () {
	string	name = argv[1];
	file	f = open(name, "r+");
	bool ret = true;

	if (!do_gps(f))
		ret = false;
	exit (ret? 0 : 1);
}

main();