aboutsummaryrefslogtreecommitdiff
path: root/n35d/n35d.c
blob: e9e42552d8736495c64f45df53fe3ae69a53b9d0 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/select.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/ioctl.h>

#include <linux/input.h>
#include <linux/watchdog.h>
#include <linux/apm_bios.h>

struct event {
	struct timeval time;
	unsigned short type;
	unsigned short code;
	unsigned int value;
};

#define eventDevice "/dev/input/event1"
#define apmDevice "/dev/apm_bios"

sig_atomic_t quit = 0;

/*	write string to sysfs file
 */
static int writeStr (const char * const file, const char * const s, const size_t slen) {
	int fd;
	
	if ((fd = open (file, O_WRONLY)) == -1) {
		perror ("open");
		return -1;
	}
	if (write (fd, s, slen) == -1) {
		perror ("write");
	}
	close (fd);
}

/*	write 1 or 0 to sysfs file
 */
static int writeToggleStr (const char * const file, const int enable) {
	const char *s = "0";
	const size_t slen = 1;

	if (enable) {
		s = "1";
	}
	writeStr (file, s, slen);
}

/*	suspend to ram
 */
static void suspend (int apmfd) {
	static const char * const s = "mem";
	const size_t slen = sizeof (s)-1;
	
	if (ioctl (apmfd, APM_IOC_SUSPEND, NULL) == -1) {
		perror ("ioctl suspend");
	}
}

/*	reboot system
 */
static void reboot () {
	system ("reboot");
}

/*	en/disable gps unit
 */
static void toggleGps (int enable) {
	writeToggleStr ("/sys/devices/platform/n35-gps/gps_power", enable);
}

/*	en/disable blue led
 */
static void toggleBlueLed (int enable) {
	writeToggleStr ("/sys/devices/platform/s3c24xx_led.1/leds/blue_led/brightness", enable);
}

/*	en/disable red warning led
 */
static void toggleWarningLed (int enable) {
	writeToggleStr ("/sys/devices/platform/s3c24xx_led.2/leds/warning_led/brightness", enable);
}

void handleSignal (int signum) {
	switch (signum) {
		case SIGTERM:
		case SIGINT:
			quit = 1;
			break;
	}
}

int main (int argc, char **argv) {
	int eventfd, watchdogfd, apmfd, maxfd;
	int blueBlink = 0, suspended = 0;
	enum {BLUE_OFF = 0, BLUE_ON = 1} blinkState = BLUE_OFF;
	struct sigaction sact;

	/* FIXME: check initial gps antenna state (how?) */

	memset (&sact, 0, sizeof (sact));
	sact.sa_handler = handleSignal;
	sigaction (SIGTERM, &sact, NULL);
	sigaction (SIGINT, &sact, NULL);

	if ((eventfd = open (eventDevice, O_RDONLY)) == -1) {
		perror ("open event");
		return 1;
	}

	if ((apmfd = open (apmDevice, O_RDWR)) == -1) {
		perror ("open apm");
		return 1;
	}

	maxfd = eventfd > apmfd ? eventfd : apmfd;
	++maxfd;

#if 0
	if ((watchdogfd = open ("/dev/watchdog", O_WRONLY)) == -1) {
		perror ("open watchdog");
		return -1;
	}

	int timeout;
	ioctl(watchdogfd, WDIOC_GETTIMEOUT, &timeout);
    printf("The timeout was is %d seconds\n", timeout);
	/* we’re not dead and purposefully closed the device */
	write (watchdogfd, "V", 1);
	close (watchdogfd);
#endif

	while (!quit) {
		fd_set rfds;
		struct timeval tv;
		int ret;

		FD_ZERO(&rfds);
		FD_SET(eventfd, &rfds);
		FD_SET(apmfd, &rfds);

		if (blueBlink) {
			if (blinkState == BLUE_ON) {
				tv.tv_sec = 0;
				tv.tv_usec = 100000;
			} else {
				tv.tv_sec = 1;
				tv.tv_usec = 0;
			}
		}

		ret = select (maxfd, &rfds, NULL, NULL, blueBlink ? &tv : NULL);
		if (ret == -1) {
			perror ("select");
			break;
		} else if (ret > 0) {
			if (FD_ISSET (eventfd, &rfds)) {
				/* handle key press events */
				struct event ev;

				if (read (eventfd, &ev, sizeof (ev)) == -1) {
					perror ("read");
				}
				printf ("time: %u sec %u usec, type %x, code %x, value %x\n",
						ev.time.tv_sec, ev.time.tv_usec, ev.type, ev.code,
						ev.value);
				if (ev.type == EV_KEY) {
					switch (ev.code) {
						case KEY_POWER:
							/* suspend */
							if (ev.value == 1) {
								if (suspended > 0) {
									/* prevent wakeup keypress from suspending
									 * again */
									--suspended;
								} else {
									/* make sure device does not wake ap again */
									usleep (100000);
									suspended = 1;
									suspend (apmfd);
								}
							}
							break;

						case KEY_POWER2:
							/* reboot */
							if (ev.value == 1) {
								reboot ();
							}
							break;

						case SW_RADIO:
							/* toggle gps power */
							toggleGps ((ev.value == 1));
							blueBlink = (ev.value == 1);
							toggleBlueLed (0);
							break;
					}
				}
			} else if (FD_ISSET (apmfd, &rfds)) {
				/* handle apm events */
				apm_event_t ev;

				if (read (apmfd, &ev, sizeof (ev)) == -1) {
					perror ("read");
					break;
				}
				printf ("got apm event %x\n", ev);
				switch (ev) {
					case APM_NORMAL_RESUME:
						/* device resumed from suspend */
						break;

#if 0
					/* this is done by the hardware (8% left) */
					case APM_LOW_BATTERY:
						toggleWarningLed (1);
						break;
#endif

					/* the apm driver is unable to send these events atm */
					case APM_POWER_STATUS_CHANGE:
						/* ? */
						break;
				}
			}
		}
		
		if (blueBlink) {
			blinkState = !blinkState;
			toggleBlueLed (blinkState);
		}
	}

	/* make sure the windows are closed before leaving */
	toggleGps (0);
	toggleBlueLed (0);
	toggleWarningLed (0);

	close (eventfd);
	close (apmfd);

	return 0;
}