GE-115 Emulator
An Emulator of the General Electrics GE-115 computer
console_socket.c
Go to the documentation of this file.
1 #include <sys/socket.h>
2 #include <sys/un.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <string.h>
6 #include <stdlib.h>
7 
8 #include "console_socket.h"
9 #include "ge.h"
10 #include "console.h"
11 #include "log.h"
12 
13 static const char socket_path[] = "/tmp/gemu.console";
14 static int console_socket_fd = -1;
15 
16 static int console_socket_init(struct ge *ge, void *ctx)
17 {
18  int sd;
19  struct sockaddr_un sock;
20  (void)ge;
21  (void)ctx;
22  unlink(socket_path);
23  memset(&sock, 0, sizeof(sock));
24  sock.sun_family = AF_UNIX;
25  strcpy(sock.sun_path, socket_path);
26  sd = socket(AF_UNIX, SOCK_DGRAM, 0);
27  if (sd < 0)
28  return sd;
29  fcntl(sd, F_SETFL, O_NONBLOCK);
30  if (bind(sd, (struct sockaddr *)&sock, sizeof(sock)) != 0) {
31  close(sd);
32  return -1;
33  }
34 
35  console_socket_fd = sd;
36  return 0;
37 }
38 
39 
40 static int console_socket_check(struct ge *ge, void *ctx)
41 {
42  char buf[1024];
43  struct sockaddr_un dst;
44  ssize_t ret;
45  socklen_t socket_size = sizeof(struct sockaddr_un);
46  struct ge_console console;
47 
48  (void)ctx;
49 
50  if (console_socket_fd < 0) {
51  return -1;
52  }
53 
54  ge_fill_console_data(ge, &console);
55 
56  ret = recvfrom(console_socket_fd, buf, 1024, 0,
57  (struct sockaddr *)&dst, &socket_size);
58  if (ret > 0) {
59  ge_log(LOG_DEBUG, "doing check\n");
60  sendto(console_socket_fd, (unsigned char *)(&console),
61  sizeof(struct ge_console), 0, (struct sockaddr *)&dst,
62  socket_size);
63  }
64 
65  return 0;
66 }
67 
68 static struct ge_peri console_socket = {
70  .on_pulse = console_socket_check,
71 };
72 
74 {
76 }
void ge_fill_console_data(struct ge *ge, struct ge_console *console)
Definition: console.c:7
static int console_socket_fd
static int console_socket_check(struct ge *ge, void *ctx)
static const char socket_path[]
static int console_socket_init(struct ge *ge, void *ctx)
static struct ge_peri console_socket
int console_socket_register(struct ge *ge)
int ge_register_peri(struct ge *ge, struct ge_peri *p)
Definition: peripherical.c:55
void ge_log(ge_log_type type, const char *format,...)
Log message.
Definition: log.c:31
@ LOG_DEBUG
General detailed debug information.
Definition: log.h:19
Definition: ge.h:606
int(* init)(struct ge *, void *)
Definition: ge.h:608
The entire state of the emulated system, including registers, memory, peripherals and timings.
Definition: ge.h:94