Skip to content

Commit 22a6391

Browse files
committed
SIYI_tools: added compression code
1 parent 1943072 commit 22a6391

File tree

3 files changed

+110
-15
lines changed

3 files changed

+110
-15
lines changed

SIYI_tools/thermal_socket/Makefile

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
CFLAGS = -static
1+
CFLAGS = -static -Wall -Wextra -g3
22

33
CC=arm-linux-gnueabihf-gcc
4+
#CC=gcc
5+
LIBS = -lz
6+
7+
all: thermal_socket
48

59
thermal_socket: thermal_socket.c
6-
$(CC) $(CFLAGS) -o thermal_socket thermal_socket.c
10+
$(CC) $(CFLAGS) -o thermal_socket thermal_socket.c $(LIBS)
11+
12+
clean:
13+
/bin/rm -f thermal_socket

SIYI_tools/thermal_socket/fetch_thermal.py

+33-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
import threading
1515
import datetime
1616
import math
17+
import zlib
1718

1819
from MAVProxy.modules.lib.mp_image import MPImage
1920

2021
TCP_THERMAL=("192.168.144.25", 7345)
22+
#TCP_THERMAL=("127.0.0.1", 7345)
2123

2224
last_data = None
2325
WIN_NAME = "Thermal"
@@ -72,6 +74,18 @@ def display_file(fname, data):
7274
cv2.setMouseCallback(WIN_NAME, click_callback)
7375
update_title()
7476

77+
import zlib
78+
79+
def decompress_zlib_buffer(compressed_buffer):
80+
try:
81+
# Decompress the buffer using zlib
82+
uncompressed_buffer = zlib.decompress(compressed_buffer)
83+
return uncompressed_buffer
84+
except zlib.error as e:
85+
# Handle any errors during decompression
86+
print(f"Decompression error: {e}")
87+
return None
88+
7589
def fetch_latest():
7690
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7791
tcp.connect(TCP_THERMAL)
@@ -84,15 +98,30 @@ def fetch_latest():
8498
buf += b
8599

86100
print(len(buf))
87-
if len(buf) != 128 + 8 + EXPECTED_DATA_SIZE:
101+
header_len = 128 + 12
102+
if len(buf) < header_len:
103+
print("Invalid data size %u" % len(buf))
88104
return None
89105

90106
print("Got %u bytes" % len(buf))
107+
91108
fname = buf[:128].decode("utf-8").strip('\x00')
92-
tstamp, = struct.unpack("<d", buf[128:128+8])
109+
compressed_size,tstamp = struct.unpack("<Id", buf[128:128+12])
110+
111+
compressed_data = buf[header_len:]
112+
113+
if compressed_size != len(compressed_data):
114+
print("Invalid compressed_size %u expected %u" % (compressed_size, len(compressed_data)))
115+
return None
116+
117+
uncompressed_data = decompress_zlib_buffer(compressed_data)
118+
119+
print("uncompressed_size=%u" % len(uncompressed_data))
120+
if len(uncompressed_data) != EXPECTED_DATA_SIZE:
121+
print("Bad uncompressed length %u" % len(uncompressed_data))
122+
93123
print(fname, tstamp)
94-
data = buf[128+8:]
95-
return fname, tstamp, data
124+
return fname, tstamp, uncompressed_data
96125

97126
def save_file(fname, tstamp, data):
98127
fname = os.path.basename(fname)[:-4]

SIYI_tools/thermal_socket/thermal_socket.c

+68-9
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,26 @@
1818
#include <stdlib.h>
1919
#include <sys/wait.h>
2020
#include <dirent.h>
21+
#include <zlib.h>
2122

2223
#define LISTEN_PORT 7345
2324
#define THERMAL_DIR "/mnt/DCIM/102SIYI_TEM"
24-
#define EXPECTED_SIZE 655360
25+
//#define THERMAL_DIR "/mnt/Photo/B"
26+
//#define THERMAL_DIR "test_images"
2527

26-
struct header {
28+
#define EXPECTED_SIZE (640 * 512 * 2)
29+
30+
#define FORK_PER_CONNECTION 1
31+
32+
#define PACKED __attribute__((__packed__))
33+
34+
struct PACKED header {
2735
char fname[128];
36+
uint32_t compressed_size;
2837
double timestamp;
2938
};
3039

31-
static int open_socket_in(int type, int port)
40+
static int open_socket_in(int port)
3241
{
3342
struct sockaddr_in sock;
3443
int res;
@@ -99,6 +108,46 @@ char *find_latest_file(int fd, struct stat *st_latest)
99108
return newest_fname;
100109
}
101110

111+
// Function to handle errors
112+
static void zlib_error(const char *msg) {
113+
perror(msg);
114+
exit(1);
115+
}
116+
117+
/*
118+
compress a buffer, returning the compressed size
119+
exit via zlib_error() on any error
120+
*/
121+
static uint32_t compress_buffer(uint8_t *in_buffer, uint32_t in_size, uint8_t *out_buffer, uint32_t out_buf_size, uint8_t compression_level)
122+
{
123+
z_stream strm;
124+
memset(&strm, 0, sizeof(strm)); // Initialize the z_stream structure
125+
126+
// Initialize the zlib stream for compression
127+
if (deflateInit(&strm, compression_level) != Z_OK) {
128+
zlib_error("deflateInit failed");
129+
}
130+
131+
// Set up the zlib stream for compression
132+
strm.avail_in = in_size;
133+
strm.next_in = in_buffer;
134+
135+
strm.avail_out = out_buf_size;
136+
strm.next_out = out_buffer;
137+
138+
// Compress with zlib
139+
int ret = deflate(&strm, Z_FINISH);
140+
if (ret == Z_STREAM_ERROR) {
141+
deflateEnd(&strm);
142+
zlib_error("deflate failed");
143+
}
144+
145+
// Clean up the zlib stream
146+
deflateEnd(&strm);
147+
148+
// Calculate how many bytes were compressed into out_buffer
149+
return out_buf_size - strm.avail_out;
150+
}
102151

103152

104153
static void serve_connection(int fd)
@@ -129,25 +178,31 @@ static void serve_connection(int fd)
129178
if (read(dfd, buf, sizeof(buf)) != sizeof(buf)) {
130179
return;
131180
}
181+
uint8_t compressed_buf[EXPECTED_SIZE*2];
182+
h.compressed_size = compress_buffer(buf, sizeof(buf), compressed_buf, sizeof(compressed_buf), 1);
183+
if (h.compressed_size == 0) {
184+
printf("compression failed %s\n", h.fname);
185+
exit(1);
186+
}
132187

133188
write(fd, &h, sizeof(h));
134-
write(fd, buf, sizeof(buf));
189+
write(fd, compressed_buf, h.compressed_size);
135190

136-
printf("Sent %s\n", h.fname);
191+
printf("Sent %s compression %.1f%% compressed_size=%u\n", h.fname, (100.0 * h.compressed_size) / sizeof(buf), (unsigned)h.compressed_size);
137192
}
138193

139194
static void listener(void)
140195
{
141196
int sock;
142197

143-
sock = open_socket_in(SOCK_STREAM, LISTEN_PORT);
198+
sock = open_socket_in(LISTEN_PORT);
144199

145-
if (listen(sock, 20) == -1) {
200+
if (listen(sock, 200) == -1) {
146201
fprintf(stderr,"listen failed\n");
147202
exit(1);
148203
}
149204

150-
printf("waiting for connections\n");
205+
printf("waiting for connections on port %u\n", (unsigned)LISTEN_PORT);
151206

152207
while (1) {
153208
struct sockaddr addr;
@@ -159,16 +214,20 @@ static void listener(void)
159214
fd = accept(sock, &addr, &in_addrlen);
160215

161216
if (fd != -1) {
217+
#if FORK_PER_CONNECTION
162218
if (fork() == 0) {
163219
serve_connection(fd);
164220
exit(0);
165221
}
222+
#else
223+
serve_connection(fd);
224+
#endif
166225
close(fd);
167226
}
168227
}
169228
}
170229

171-
int main(int argc, const char *argv[])
230+
int main(void)
172231
{
173232
listener();
174233
return 0;

0 commit comments

Comments
 (0)