Skip to content

Commit

Permalink
Status LED: Show on when client is connected, off otherwise.
Browse files Browse the repository at this point in the history
  • Loading branch information
banksy-git committed Feb 9, 2021
1 parent f2fe24e commit 8579039
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gateway/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CC=../rtl819x/toolchain/rsdk-4.4.7-4181-EB-2.6.30-0.9.30-m32u-140129/bin/mips-linux-gcc
CC=/mnt/tmp/opt/realtek/rtl819x/toolchain/rsdk-4.4.7-4181-EB-2.6.30-0.9.30-m32u-140129/bin/mips-linux-gcc

build:
$(CC) -static main.c -o serialgateway
Expand Down
19 changes: 19 additions & 0 deletions gateway/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
Expand All @@ -32,6 +33,16 @@ static int _buf[BUF_SIZE];
#define LOG_DEBUG(format, ...)
#endif

static void _set_status_led(bool is_on)
{
int fd = open("/proc/led1", O_WRONLY);
if (fd < 0) {
return;
}
write(fd, (is_on) ? "1\n" : "0\n", 2);
close(fd);
}

static void _error_exit(const char* msg)
{
perror(msg);
Expand Down Expand Up @@ -84,6 +95,7 @@ static int _open_serial_port()
static void _close_connectionfd()
{
if (_connection_fd >= 0) {
_set_status_led(0);
fprintf(stderr, "Closing existing connection\n");
shutdown(_connection_fd, SHUT_RDWR);
close(_connection_fd);
Expand Down Expand Up @@ -111,6 +123,7 @@ int main()
_error_exit("setsockopt(SO_REUSEADDR) failed");
}


name.sin_family = AF_INET;
name.sin_port = htons (8888);
name.sin_addr.s_addr = htonl (INADDR_ANY);
Expand Down Expand Up @@ -144,9 +157,15 @@ int main()
continue;
}
_close_connectionfd();
_set_status_led(1);
fprintf (stderr,
"Connect from host %s\n",
inet_ntoa (clientname.sin_addr));
int enable = 1;
if (setsockopt(new, SOL_SOCKET, SO_KEEPALIVE,
&enable, sizeof(enable)) < 0) {
fprintf (stderr, "Failed to set SO_KEEPALIVE");
}
FD_SET (new, &_master_read_set);
_connection_fd = new;

Expand Down

0 comments on commit 8579039

Please sign in to comment.