Skip to content

Commit c20eada

Browse files
committed
fix: debug acq_mode to data product parsing function in pff.h
1 parent 0c55a14 commit c20eada

File tree

5 files changed

+110
-163
lines changed

5 files changed

+110
-163
lines changed

net_thread.c

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727

2828
static int group_ph_frames;
29-
static module_snapshot_buffer_t *snapshot_buffers;
29+
static module_snapshot_buffer *snapshot_buffers;
3030

3131
// Initialization function for Hashpipe.
3232
// This function is called once when the thread is created
@@ -119,7 +119,7 @@ static int init(hashpipe_thread_args_t *args)
119119

120120
// Initialize snapshot buffers
121121
snapshot_buffers = NULL;
122-
init_module_snapshot_buffers(module_config, snapshot_buffers);
122+
init_module_snapshot_buffers(module_config, &snapshot_buffers);
123123
if (snapshot_buffers == NULL) {
124124
hashpipe_error("net_thread", "Failed to initialize snapshot buffers\n");
125125
pthread_exit(NULL);
@@ -250,15 +250,6 @@ static void *run(hashpipe_thread_args_t *args)
250250
{
251251
hashpipe_pktsock_release_frame(p_frame);
252252
}
253-
// let's create snapshot files here
254-
// char ssmovie[128];
255-
// char ssph[128];
256-
// snprintf(ssmovie, sizeof(ssmovie), "%s/module_0/obs_snapshot/start_0.img16.seqno_0.pff", ssdir);
257-
// snprintf(ssph, sizeof(ssph), "%s/module_0/obs_snapshot/start_0.ph256.seqno_0.pff", ssdir);
258-
// hashpipe_info(__FUNCTION__, "Movie snapshot: %s", ssmovie);
259-
// hashpipe_info(__FUNCTION__, "PH snapshot: %s", ssph);
260-
// FILE *mov16_fp = fopen(ssmovie, "w");
261-
// FILE *ph_fp = fopen(ssph, "w");
262253

263254
// track last successful grpc snapshot send time
264255
struct timeval last_idle_check_time;
@@ -371,57 +362,15 @@ static void *run(hashpipe_thread_args_t *args)
371362
}
372363

373364
// Fetch the snapshot buffer for the current module
374-
module_snapshot_buffer_t *snapshot_buffer = get_snapshot_buffer(blockHeader->pkt_head[i].mod_num, snapshot_buffers);
365+
module_snapshot_buffer *snapshot_buffer = get_snapshot_buffer(blockHeader->pkt_head[i].mod_num, snapshot_buffers);
375366
if (snapshot_buffer) {
376367
snapshot_buffer->update_snapshot(
377368
&blockHeader->pkt_head[i],
378369
pkt_data + BYTE_PKT_HEADER,
379370
&nowTime,
380-
ssint
371+
ssint,
372+
group_ph_frames
381373
);
382-
// PACKET_HEADER pkt_head = blockHeader->pkt_head[i];
383-
384-
// // char acq_mode = pkt_head.acq_mode;
385-
// // DATA_PRODUCT dp = acq_mode_to_dp(acq_mode, group_ph_frames);
386-
// if (dp == DP_PH_256_IMG || dp == DP_PH_1024_IMG)
387-
// {
388-
// snapshot_buffer->update_snapshot(&blockHeader->pkt_head[i], pkt_data + BYTE_PKT_HEADER);
389-
// // for PH snapshots
390-
// tdiff = timeval_diff(&lastPHTime, &nowTime);
391-
// if (tdiff > ssint * 1000)
392-
// {
393-
// // WritePHSnapshots(ph_fp, &blockHeader->pkt_head[i], pkt_data + BYTE_PKT_HEADER);
394-
// WritePHSnapshotsToUds(DP_PH_256_IMG, &blockHeader->pkt_head[i], pkt_data + BYTE_PKT_HEADER);
395-
// lastPHTime.tv_sec = nowTime.tv_sec;
396-
// lastPHTime.tv_usec = nowTime.tv_usec;
397-
// }
398-
// }
399-
// else if (dp == DP_BIT16_IMG)
400-
// {
401-
// // if we get four packets from four different quabos,
402-
// // imgfull will be 0xf.
403-
// // then we will write the data into the snapshot file.
404-
// quabo_num = blockHeader->pkt_head[i].quabo_num;
405-
// imgfull |= 1 << quabo_num;
406-
// // TODO: group the mov images?
407-
// quabo16_to_module16_copy(pkt_data + BYTE_PKT_HEADER, quabo_num, oimgbuf);
408-
// memcpy(imgbuf + quabo_num * 512, oimgbuf, 512);
409-
// memcpy(&imgheader[quabo_num], &blockHeader->pkt_head[i], sizeof(PACKET_HEADER));
410-
// if (imgfull == 0xf)
411-
// {
412-
// imgfull = 0;
413-
// // for Img16 snapshots
414-
// tdiff = timeval_diff(&lastImg16Time, &nowTime);
415-
// if (tdiff > ssint * 1000)
416-
// {
417-
// // DATA_PRODUCT img_dp = (imgheader[0].acq_mode == 0x03) ? DP_BIT8_IMG : DP_BIT16_IMG;
418-
// // WriteImgSnapshots(mov16_fp, imgheader, imgbuf);
419-
// WriteImgSnapshotsToUds(dp, imgheader, imgbuf);
420-
// lastImg16Time.tv_sec = nowTime.tv_sec;
421-
// lastImg16Time.tv_usec = nowTime.tv_usec;
422-
// }
423-
// }
424-
// }
425374
}
426375

427376
// ==== End snapshot code ====

snapshot.c

Lines changed: 70 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@
1212
#include <sys/stat.h>
1313
#include <netinet/in.h>
1414
#include <poll.h>
15-
#include <sys/time.h>
1615

1716
#include "snapshot.h"
1817
#include "hashpipe.h"
19-
#include "pff.h"
20-
#include "databuf.h"
2118

22-
// get the time difference
23-
static uint64_t timeval_diff(struct timeval *start, struct timeval *end)
19+
// get the time difference in microseconds (usec)
20+
uint64_t timeval_diff(struct timeval *start, struct timeval *end)
2421
{
2522
struct timeval diff;
2623
diff.tv_sec = end->tv_sec - start->tv_sec;
@@ -34,8 +31,8 @@ static uint64_t timeval_diff(struct timeval *start, struct timeval *end)
3431
return diff.tv_sec * 1000000 + diff.tv_usec;
3532
}
3633

37-
module_snapshot_buffer_t* get_snapshot_buffer(uint16_t module_id, module_snapshot_buffer_t *snapshot_buffers) {
38-
module_snapshot_buffer_t *iter = snapshot_buffers;
34+
module_snapshot_buffer* get_snapshot_buffer(uint16_t module_id, module_snapshot_buffer *snapshot_buffers) {
35+
module_snapshot_buffer *iter = snapshot_buffers;
3936
while (iter != NULL) {
4037
if (iter->module_id == module_id) {
4138
return iter;
@@ -46,8 +43,8 @@ module_snapshot_buffer_t* get_snapshot_buffer(uint16_t module_id, module_snapsho
4643
}
4744

4845
/* Initialize snapshot buffers for each declared module id */
49-
void init_module_snapshot_buffers(char *module_config, module_snapshot_buffer_t *snapshot_buffers) {
50-
snapshot_buffers = NULL;
46+
void init_module_snapshot_buffers(char *module_config, module_snapshot_buffer **snapshot_buffers) {
47+
*snapshot_buffers = NULL;
5148

5249
char fbuf[100];
5350
signed char cbuf;
@@ -67,14 +64,16 @@ void init_module_snapshot_buffers(char *module_config, module_snapshot_buffer_t
6764
ungetc(cbuf, module_config_fp);
6865
if (cbuf != '#')
6966
{
70-
if (fscanf(module_config_fp, "%u\n", &module_id) == 1)
67+
if (fscanf(module_config_fp, "%hu\n", &module_id) == 1)
7168
{
72-
if (snapshot_buffers == NULL) {
73-
snapshot_buffers = new module_snapshot_buffer_t(module_id);
69+
if (*snapshot_buffers == NULL) {
70+
hashpipe_info(__FUNCTION__, "Creating snapshot buffer for module %hu\n", module_id);
71+
*snapshot_buffers = new module_snapshot_buffer(module_id);
7472
} else {
75-
module_snapshot_buffer_t *new_buffer = new module_snapshot_buffer_t(module_id);
76-
new_buffer->next = snapshot_buffers;
77-
snapshot_buffers = new_buffer;
73+
hashpipe_info(__FUNCTION__, "Creating snapshot buffer for module %hu\n", module_id);
74+
module_snapshot_buffer *new_buffer = new module_snapshot_buffer(module_id);
75+
new_buffer->next = *snapshot_buffers;
76+
*snapshot_buffers = new_buffer;
7877
}
7978
}
8079
}
@@ -158,62 +157,6 @@ static int sprint_img_snapshot_json(char* dest, size_t size, PACKET_HEADER* head
158157
return (p - dest);
159158
}
160159

161-
// =====================================================================
162-
// Filesystem snapshot functions (originally from net_thread.c)
163-
// =====================================================================
164-
165-
166-
// Writes a single-packet pulse-height snapshot to a file.
167-
void WritePHSnapshots(FILE *fp, PACKET_HEADER *header, uint8_t *data) {
168-
char json_buffer[1024];
169-
170-
// Create the JSON header in memory using the unified function
171-
if (sprint_ph_snapshot_json(json_buffer, sizeof(json_buffer), header) <= 0) {
172-
hashpipe_error(__FUNCTION__, "Failed to sprint PH snapshot JSON for filesystem");
173-
return;
174-
}
175-
176-
// Write the PFF frame to the file
177-
// move the pointer to the beginning,
178-
// as we only need one pkt in the snapshot file.
179-
fseek(fp, 0, SEEK_SET);
180-
pff_start_json(fp);
181-
fputs(json_buffer, fp); // Write the generated JSON string
182-
pff_end_json(fp);
183-
pff_write_image(fp, PIXELS_PER_IMAGE * 2, data);
184-
fflush(fp);
185-
if (ftruncate(fileno(fp), ftell(fp)) < 0) {
186-
hashpipe_error(__FUNCTION__, "Failed to truncate PH snapshot file");
187-
}
188-
fsync(fileno(fp));
189-
}
190-
191-
192-
// write data into img snapshot file
193-
void WriteImgSnapshots(FILE *fp, PACKET_HEADER *header, uint8_t *data) {
194-
char json_buffer[4096];
195-
196-
// Create the JSON header in memory using the unified function
197-
if (sprint_img_snapshot_json(json_buffer, sizeof(json_buffer), header) <= 0) {
198-
hashpipe_error(__FUNCTION__, "Failed to sprint Img snapshot JSON for filesystem");
199-
return;
200-
}
201-
202-
// Write the PFF frame to the file
203-
// move the pointer to the beginning,
204-
// as we only need one pkt in the snapshot file.
205-
fseek(fp, 0, SEEK_SET);
206-
pff_start_json(fp);
207-
fputs(json_buffer, fp); // Write the generated JSON string
208-
pff_end_json(fp);
209-
pff_write_image(fp, BYTES_PER_MODULE_FRAME, data);
210-
fflush(fp);
211-
if (ftruncate(fileno(fp), ftell(fp)) < 0) {
212-
hashpipe_error(__FUNCTION__, "Failed to truncate Img snapshot file");
213-
}
214-
fsync(fileno(fp));
215-
}
216-
217160

218161
// =====================================================================
219162
// UDS snapshot functions
@@ -418,3 +361,59 @@ void write_32x32_to_uds(DATA_PRODUCT dp, PACKET_HEADER *header, uint8_t *data) {
418361
hashpipe_error(__FUNCTION__, "Failed to sprint Img snapshot JSON for UDS");
419362
}
420363
}
364+
365+
// =====================================================================
366+
// Filesystem snapshot functions (originally from net_thread.c)
367+
// =====================================================================
368+
369+
370+
// // Writes a single-packet pulse-height snapshot to a file.
371+
// void WritePHSnapshots(FILE *fp, PACKET_HEADER *header, uint8_t *data) {
372+
// char json_buffer[1024];
373+
374+
// // Create the JSON header in memory using the unified function
375+
// if (sprint_ph_snapshot_json(json_buffer, sizeof(json_buffer), header) <= 0) {
376+
// hashpipe_error(__FUNCTION__, "Failed to sprint PH snapshot JSON for filesystem");
377+
// return;
378+
// }
379+
380+
// // Write the PFF frame to the file
381+
// // move the pointer to the beginning,
382+
// // as we only need one pkt in the snapshot file.
383+
// fseek(fp, 0, SEEK_SET);
384+
// pff_start_json(fp);
385+
// fputs(json_buffer, fp); // Write the generated JSON string
386+
// pff_end_json(fp);
387+
// pff_write_image(fp, PIXELS_PER_IMAGE * 2, data);
388+
// fflush(fp);
389+
// if (ftruncate(fileno(fp), ftell(fp)) < 0) {
390+
// hashpipe_error(__FUNCTION__, "Failed to truncate PH snapshot file");
391+
// }
392+
// fsync(fileno(fp));
393+
// }
394+
395+
396+
// // write data into img snapshot file
397+
// void WriteImgSnapshots(FILE *fp, PACKET_HEADER *header, uint8_t *data) {
398+
// char json_buffer[4096];
399+
400+
// // Create the JSON header in memory using the unified function
401+
// if (sprint_img_snapshot_json(json_buffer, sizeof(json_buffer), header) <= 0) {
402+
// hashpipe_error(__FUNCTION__, "Failed to sprint Img snapshot JSON for filesystem");
403+
// return;
404+
// }
405+
406+
// // Write the PFF frame to the file
407+
// // move the pointer to the beginning,
408+
// // as we only need one pkt in the snapshot file.
409+
// fseek(fp, 0, SEEK_SET);
410+
// pff_start_json(fp);
411+
// fputs(json_buffer, fp); // Write the generated JSON string
412+
// pff_end_json(fp);
413+
// pff_write_image(fp, BYTES_PER_MODULE_FRAME, data);
414+
// fflush(fp);
415+
// if (ftruncate(fileno(fp), ftell(fp)) < 0) {
416+
// hashpipe_error(__FUNCTION__, "Failed to truncate Img snapshot file");
417+
// }
418+
// fsync(fileno(fp));
419+
// }

snapshot.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#ifndef _SNAPSHOT_H_
22
#define _SNAPSHOT_H_
33

4-
#include <stdio.h>
54
#include <stdint.h>
5+
#include <sys/time.h>
6+
#include "image.h"
7+
#include <stdio.h>
68
#include "databuf.h"
79
#include "pff.h"
810

@@ -18,7 +20,7 @@ extern "C" {
1820
#define UDS_PATH_TEMPLATE "/tmp/hashpipe_grpc.dp_%s.sock"
1921

2022
// get the time difference
21-
static uint64_t timeval_diff(struct timeval *start, struct timeval *end);
23+
uint64_t timeval_diff(struct timeval *start, struct timeval *end);
2224

2325
// Structure representing a Unix Domain Socket connection.
2426
typedef struct uds_connection {
@@ -90,7 +92,7 @@ struct module_snapshot_buffer {
9092
uint16_t module_id;
9193
snapshot_t *snapshot_head;
9294
snapshot_t *snapshot_tail;
93-
module_snapshot_buffer_t *next;
95+
module_snapshot_buffer *next;
9496

9597
module_snapshot_buffer(uint16_t module_id) {
9698
this->module_id = module_id;
@@ -127,19 +129,17 @@ struct module_snapshot_buffer {
127129
return NULL; // Not found
128130
}
129131

130-
void update_snapshot(PACKET_HEADER *header, uint8_t *data, struct timeval *nowTime, int snapshot_interval_ms) {
132+
void update_snapshot(PACKET_HEADER *header, uint8_t *data, struct timeval *nowTime, int snapshot_interval_ms, int group_ph_frames) {
131133
char acq_mode = header->acq_mode;
132-
int quabo_num = header.quabo_num;
134+
int quabo_num = header->quabo_num;
133135
DATA_PRODUCT dp = acq_mode_to_dp(acq_mode, group_ph_frames);
134136
snapshot_t *s = this->get_snapshot(dp);
135137

136138
// If timestamp difference exceeds snapshot interval, write snapshot to the unix-domain socket for this data product
137139
bool write_snapshot = false;
138-
tdiff = timeval_diff(&s->last_update_time, &nowTime);
140+
uint64_t tdiff = timeval_diff(&s->last_update_time, nowTime);
139141
if ((tdiff > snapshot_interval_ms * 1000))
140142
{
141-
s->last_update_time.tv_sec = nowTime.tv_sec;
142-
s->last_update_time.tv_usec = nowTime.tv_usec;
143143
write_snapshot = true;
144144
}
145145

@@ -152,8 +152,10 @@ struct module_snapshot_buffer {
152152
if (write_snapshot) {
153153
write_16x16_to_uds(dp, header, s->data);
154154
// memset(s->headers, 0, sizeof(s->headers));
155-
memset(s->data, 0, sizeof(s->data));
155+
s->last_update_time.tv_sec = nowTime->tv_sec;
156+
s->last_update_time.tv_usec = nowTime->tv_usec;
156157
}
158+
memset(s->data, 0, sizeof(s->data));
157159
} else {
158160
// Require 4 quabo images
159161
s->quabo_bitmap |= 1 << quabo_num;
@@ -170,6 +172,8 @@ struct module_snapshot_buffer {
170172
if (s->quabo_bitmap == 0xf) {
171173
if (write_snapshot) {
172174
write_16x16_to_uds(dp, s->headers, s->data);
175+
s->last_update_time.tv_sec = nowTime->tv_sec;
176+
s->last_update_time.tv_usec = nowTime->tv_usec;
173177
}
174178
memset(s->headers, 0, sizeof(s->headers));
175179
memset(s->data, 0, sizeof(s->data));
@@ -186,11 +190,12 @@ struct module_snapshot_buffer {
186190
current = next;
187191
}
188192
}
189-
} module_snapshot_buffer_t;
193+
};
190194

191195

192-
void init_module_snapshot_buffers(char *module_config, module_snapshot_buffer_t *snapshot_buffers);
196+
void init_module_snapshot_buffers(char *module_config, module_snapshot_buffer **snapshot_buffers);
193197

198+
module_snapshot_buffer* get_snapshot_buffer(uint16_t module_id, module_snapshot_buffer *snapshot_buffers);
194199

195200

196201
#ifdef __cplusplus

util/pff.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ DATA_PRODUCT str_to_dp(const char* s) {
3030
return DP_NONE;
3131
}
3232

33+
DATA_PRODUCT acq_mode_to_dp(char acq_mode, int group_ph_frames) {
34+
switch (acq_mode) {
35+
case 0x01:
36+
if (group_ph_frames) {
37+
return DP_PH_1024_IMG; // 1024 pulse height
38+
}
39+
else {
40+
return DP_PH_256_IMG; // 256 pulse height
41+
}
42+
case 0x02: return DP_BIT16_IMG; // 16 bit imaging
43+
case 0x03: return DP_BIT16_IMG; // 16 bit imaging
44+
case 0x06: return DP_BIT8_IMG; // 8 bit imaging
45+
case 0x07: return DP_BIT8_IMG; // 8 bit imaging
46+
default: return DP_NONE;
47+
}
48+
}
49+
3350
void pff_start_json(FILE* f) {
3451
}
3552

0 commit comments

Comments
 (0)