]> git.keenfalcon.ru Git - ZR10.git/commitdiff
* no glib!
authorLelik P. Korchagin <lelik@korchagins.ru>
Mon, 16 Jan 2023 14:34:50 +0000 (17:34 +0300)
committerLelik P. Korchagin <lelik@korchagins.ru>
Mon, 16 Jan 2023 14:34:50 +0000 (17:34 +0300)
Makefile
main.c

index 67eb4f55a24fbb2a684ef8ce8d2ef8ca6d752de6..8c22fe8381b7be4b4d7b054a2777bdd40bc04007 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -5,14 +5,12 @@ else
 CC=gcc
 endif
 
-LDFLAGS= -ggdb         $(shell pkg-config --libs glib-2.0) -lrt 
+LDFLAGS= -ggdb
 
 CPPFLAGS=-Wall \
        -Wno-address-of-packed-member \
        -D_GNU_SOURCE \
-       -ggdb \
-       -pthread \
-       $(shell pkg-config --cflags glib-2.0)
+       -ggdb
 
 all:   zr10
 
diff --git a/main.c b/main.c
index e945e255f1dc37afdb385d4c1524e20c119ad39f..2beeadea01acb42d42a278c29306cb5630d840f4 100644 (file)
--- a/main.c
+++ b/main.c
@@ -10,7 +10,6 @@
 # include <sys/socket.h>
 # include <netinet/in.h>
 # include <unistd.h>
-# include <glib-unix.h>
 
 uint16_t crc16_xmodem(void* data, size_t len)
 {
@@ -55,7 +54,7 @@ uint16_t crc16_xmodem(void* data, size_t len)
         for (int i = 0; i < len; i++) {
                 crc = (crc << 8) ^ crcs[((crc >> 8) ^ ((uint8_t*)data)[i]) & 0x00FF];
         }
-       return crc;
+        return crc;
 }
 
 struct zr10
@@ -90,126 +89,99 @@ int zr10_command(uint8_t *buffer, uint8_t cmd, uint8_t *data, size_t len)
         return sizeof(zr10) + len + sizeof(crc);
 }
 
-struct application
-{
-        GMainLoop      *loop;
-        gboolean         done;
-};
-
-static gboolean stop(gpointer data)
-{
-        struct application *app  = (struct application *)data;
-        g_main_loop_quit(app->loop);
-        return app->done = TRUE;
-}
-
-extern gboolean got_ack(GIOChannel *channel, GIOCondition cond, gpointer data)
-{
-        struct application *app  = (struct application *)data;
-        int udp = g_io_channel_unix_get_fd(channel);
-        struct sockaddr_in from;
-        socklen_t from_len = sizeof(from);
-        uint8_t ack[2048];
-
-        int n = recvfrom(udp, ack, sizeof(ack), MSG_WAITALL, (struct sockaddr *)&from, &from_len);
-        if (n > 0) {
-                printf("%-15s: ", inet_ntoa(from.sin_addr));
-                for (int i = 0; i < n; i++) {
-                        printf(" %02x", ack[i]);
-                }
-                printf("\n");
-        } else {
-                perror("recv");
-        }
-
-        g_main_loop_quit(app->loop);
-        return app->done = TRUE;
-}
-
 # define PORT 37260
 
 int main(int argc, char *argv[])
 {
-        struct application app;
-        uint8_t buffer[128];
-        int udp, on = 1;
+        int udp, n, on = 1;
         struct sockaddr_in addr;
+        socklen_t addr_len = sizeof(addr);
 
         uint8_t command = 0;
-        int8_t data[256];
+        int8_t command_data[64];
         int len = 0;
 
-        if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-                perror("socket");
-                return 1;
-        }
-
-        if (setsockopt(udp, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
-                perror("setsockopt");
-                return 1;
-        }
-
-        memset(&addr, 0, sizeof(addr));
-        addr.sin_family = AF_INET;
-        addr.sin_addr.s_addr = htonl(INADDR_ANY);
-        addr.sin_port = htons(0);
-
-        bind(udp, (struct sockaddr *)&addr, sizeof(addr));
-
-        g_unix_signal_add (SIGINT,  stop, &app);
-        g_unix_signal_add (SIGHUP,  stop, &app);
-        g_unix_signal_add (SIGTERM, stop, &app);
-
-        GIOChannel *ch = g_io_channel_unix_new(udp);
-        g_io_add_watch(ch, G_IO_IN, got_ack, &app);
+        uint8_t buffer[64];
 
         if (argc == 2 && argv[1][0] == '.') {
                 command = 0x08;
-                data[0] = 1;
+                command_data[0] = 1;
+                len = 1;
+        } else if (argc == 2 && argv[1][0] == 'f') {
+                command = 0x04;
+                command_data[0] = 1;
                 len = 1;
         } else {
                 command = 0x07;
                 len = 2;
 
                 if (argc > 2) {
-                        data[1] = atoi(argv[2]);
-                        if (data[1] > 100) {
-                                data[1] = 100;
-                        } else if (data[1] < -100) {
-                                data[1] = -100;
+                        command_data[1] = atoi(argv[2]);
+                        if (command_data[1] > 100) {
+                                command_data[1] = 100;
+                        } else if (command_data[1] < -100) {
+                                command_data[1] = -100;
                         }
                 }
 
                 if (argc > 1) {
-                        data[0] = atoi(argv[1]);
-                        if (data[0] > 100) {
-                                data[0] = 100;
-                        } else if (data[0] < -100) {
-                                data[0] = -100;
+                        command_data[0] = atoi(argv[1]);
+                        if (command_data[0] > 100) {
+                                command_data[0] = 100;
+                        } else if (command_data[0] < -100) {
+                                command_data[0] = -100;
                         }
                 }
         }
 
-        addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
-        addr.sin_port = htons(PORT);
-
-        int n = zr10_command(buffer, command, (uint8_t*)data, len);
-        printf("%-15s: ", inet_ntoa(addr.sin_addr));
+        n = zr10_command(buffer, command, (uint8_t*)command_data, len);
+        printf("%15s %5d", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
         for (int i = 0; i < n; i++) {
                 printf(" %02x", buffer[i]);
         }
         printf("\n");
 
-        if (sendto(udp, buffer, n, MSG_CONFIRM, (const struct sockaddr *)&addr, sizeof(addr)) < 0) {
-                perror("send");
+        memset(&addr, 0, sizeof(addr));
+        addr.sin_family = AF_INET;
+        addr.sin_addr.s_addr = htonl(INADDR_ANY);
+        addr.sin_port = htons(0);
+
+        udp = socket(AF_INET, SOCK_DGRAM, 0);
+        if (udp < 0) {
+                perror("socket");
+                return 1;
         }
 
-# if 1
-        app.loop = g_main_loop_new(NULL, FALSE);
-        g_main_loop_run(app.loop);
-        g_main_loop_unref(app.loop);
-# endif
+        if (setsockopt(udp, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
+                perror("so_broadcast");
+                return 1;
+        }
+
+        if (bind(udp, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+                perror("bind");
+                return 1;
+        }
+
+        addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
+        addr.sin_port = htons(PORT);
+
+        n = sendto(udp, buffer, n, 0, (const struct sockaddr *)&addr, sizeof(addr));
+        if (n < 0) {
+                perror("sendto");
+                return 1;
+        }
+
+        n = recvfrom(udp, buffer, sizeof(buffer), 0, (struct sockaddr *)&addr, &addr_len);
+        if (n < 0) {
+                perror("recvfrom");
+                return 1;
+        }
+
+        printf("%15s %5d", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
+        for (int i = 0; i < n; i++) {
+                printf(" %02x", buffer[i]);
+        }
+        printf("\n");
 
-        close(udp);
         return 0;
 }