-
Notifications
You must be signed in to change notification settings - Fork 33
/
pisetclock.c
264 lines (207 loc) · 5.29 KB
/
pisetclock.c
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* Copyright (c) 2022 Niklas Ekström
*/
#include <exec/types.h>
#include <exec/ports.h>
#include <exec/tasks.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <devices/timer.h>
#include <proto/alib.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "../a314device/a314.h"
#define PICMD_SERVICE_NAME "picmd"
struct StartMsgHeader
{
UWORD length;
short rows;
short cols;
UBYTE component_count;
UBYTE arg_count;
};
static const char picmd_args[] = "\x04" "date" "\x05" "+%s%z";
#define ARGS_LEN (sizeof(picmd_args) - 1)
#define START_MSG_LEN (sizeof(struct StartMsgHeader) + ARGS_LEN)
struct MsgPort *sync_mp;
struct MsgPort *async_mp;
struct A314_IORequest *read_ior;
struct A314_IORequest *sync_ior;
struct timerequest *tr;
ULONG socket;
UBYTE arbuf[256];
BOOL pending_a314_read = FALSE;
BOOL stream_closed = FALSE;
void start_a314_cmd(struct MsgPort *reply_port, struct A314_IORequest *ior, UWORD cmd, char *buffer, int length)
{
ior->a314_Request.io_Message.mn_ReplyPort = reply_port;
ior->a314_Request.io_Command = cmd;
ior->a314_Request.io_Error = 0;
ior->a314_Socket = socket;
ior->a314_Buffer = buffer;
ior->a314_Length = length;
SendIO((struct IORequest *)ior);
}
BYTE a314_connect(char *name)
{
socket = time(NULL);
start_a314_cmd(sync_mp, sync_ior, A314_CONNECT, name, strlen(name));
Wait(1L << sync_mp->mp_SigBit);
GetMsg(sync_mp);
return sync_ior->a314_Request.io_Error;
}
BYTE a314_write(char *buffer, int length)
{
start_a314_cmd(sync_mp, sync_ior, A314_WRITE, buffer, length);
Wait(1L << sync_mp->mp_SigBit);
GetMsg(sync_mp);
return sync_ior->a314_Request.io_Error;
}
BYTE a314_eos()
{
start_a314_cmd(sync_mp, sync_ior, A314_EOS, NULL, 0);
Wait(1L << sync_mp->mp_SigBit);
GetMsg(sync_mp);
return sync_ior->a314_Request.io_Error;
}
BYTE a314_reset()
{
start_a314_cmd(sync_mp, sync_ior, A314_RESET, NULL, 0);
Wait(1L << sync_mp->mp_SigBit);
GetMsg(sync_mp);
return sync_ior->a314_Request.io_Error;
}
void start_a314_read()
{
start_a314_cmd(async_mp, read_ior, A314_READ, arbuf, 255);
pending_a314_read = TRUE;
}
void handle_read_data()
{
static BOOL timestamp_handled = FALSE;
if (timestamp_handled)
return;
UBYTE *p = read_ior->a314_Buffer;
int len = read_ior->a314_Length;
unsigned long time_zone = 0;
for (int i = 0; i < len; i++)
{
if (p[i] == '+' || p[i] == '-')
{
unsigned long hours = ((p[i+1] - '0') * 10) + (p[i+2] - '0');
unsigned long minutes = ((p[i+3] - '0') * 10) + (p[i+4] - '0');
time_zone = (hours * 60 + minutes) * 60;
if (p[i] == '-')
time_zone = -time_zone;
p[i] = 0;
break;
}
}
unsigned long ts = strtoul(p, NULL, 10);
ts -= 252460800; // Adjust base from 1970-01-01 to 1978-01-01.
ts += time_zone;
tr->tr_time.tv_secs = ts;
tr->tr_node.io_Command = TR_SETSYSTIME;
DoIO((struct IORequest *)tr);
timestamp_handled = TRUE;
}
void handle_a314_read_completed()
{
pending_a314_read = FALSE;
if (stream_closed)
return;
int res = read_ior->a314_Request.io_Error;
if (res == A314_READ_OK)
{
handle_read_data();
start_a314_read();
}
else if (res == A314_READ_EOS)
{
a314_eos();
stream_closed = TRUE;
}
else if (res == A314_READ_RESET)
{
stream_closed = TRUE;
}
}
int main(int argc, char **argv)
{
sync_mp = CreatePort(NULL, 0);
async_mp = CreatePort(NULL, 0);
sync_ior = (struct A314_IORequest *)CreateExtIO(sync_mp, sizeof(struct A314_IORequest));
read_ior = (struct A314_IORequest *)CreateExtIO(async_mp, sizeof(struct A314_IORequest));
tr = (struct timerequest *)CreateExtIO(sync_mp, sizeof(struct timerequest));
if (!sync_mp || !async_mp || !sync_ior || !read_ior || !tr)
{
printf("Unable to allocate enough memory\n");
goto fail1;
}
if (OpenDevice(TIMERNAME, 0, (struct IORequest *)tr, 0))
{
printf("Unable to open " TIMERNAME "\n");
goto fail1;
}
if (OpenDevice(A314_NAME, 0, (struct IORequest *)sync_ior, 0))
{
printf("Unable to open " A314_NAME "\n");
goto fail2;
}
memcpy(read_ior, sync_ior, sizeof(struct A314_IORequest));
if (a314_connect(PICMD_SERVICE_NAME) != A314_CONNECT_OK)
{
printf("Unable to connect to " PICMD_SERVICE_NAME " service\n");
goto fail3;
}
char start_msg[START_MSG_LEN];
struct StartMsgHeader *hdr = (struct StartMsgHeader *)start_msg;
hdr->length = START_MSG_LEN;
hdr->rows = 25;
hdr->cols = 80;
hdr->component_count = 0;
hdr->arg_count = 2;
memcpy(hdr + 1, picmd_args, ARGS_LEN);
a314_write(start_msg, START_MSG_LEN);
start_a314_read();
ULONG portsig = 1L << async_mp->mp_SigBit;
BOOL did_reset = FALSE;
while (TRUE)
{
ULONG signal = Wait(portsig | SIGBREAKF_CTRL_C);
if (signal & portsig)
{
struct Message *msg;
while (msg = GetMsg(async_mp))
handle_a314_read_completed();
}
if ((signal & SIGBREAKF_CTRL_C) && !did_reset)
{
a314_reset();
did_reset = TRUE;
}
if (stream_closed && !pending_a314_read)
break;
}
fail3:
CloseDevice((struct IORequest *)sync_ior);
fail2:
CloseDevice((struct IORequest *)tr);
fail1:
if (tr)
DeleteExtIO((struct IORequest *)tr);
if (read_ior)
DeleteExtIO((struct IORequest *)read_ior);
if (sync_ior)
DeleteExtIO((struct IORequest *)sync_ior);
if (async_mp)
DeletePort(async_mp);
if (sync_mp)
DeletePort(sync_mp);
return 0;
}