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+ // }
0 commit comments