Commit eb2f643e authored by Andrey Filippov's avatar Andrey Filippov
Browse files

making resilient against restarting camogm and incrementing state file name

parent 997be073
Loading
Loading
Loading
Loading
+21 −12
Original line number Diff line number Diff line
@@ -44,7 +44,7 @@
#endif

/** @brief Default debug level */
#define DEFAULT_DEBUG_LVL         6
#define DEFAULT_DEBUG_LVL         0
/** @brief Default poll() timeout in ms*/
#define DEFAULT_POLL_TIMEOUT      1000
/** @brief JPEG trailer size in bytes */
@@ -125,7 +125,7 @@ int debug_level;
FILE* debug_file;

void camogm_init(camogm_state *state, char *pipe_name, uint16_t port_num);
int camogm_start(camogm_state *state);
int camogm_start(camogm_state *state, bool restart);
int camogm_stop(camogm_state *state);
void camogm_reset(camogm_state *state);
int camogm_debug(camogm_state *state, const char *fname);
@@ -340,7 +340,7 @@ int camogm_debug_level(int d)
 * @param[in]   state   a pointer to a structure containing current state
 * @return      0 if the recording finished successfully and negative error code otherwise
 */
int camogm_start(camogm_state *state)
int camogm_start(camogm_state *state, bool restart)
{
	int timestamp_start;
	int rslt;
@@ -357,10 +357,15 @@ int camogm_start(camogm_state *state)
		return CAMOGM_FRAME_OTHER;
	}
	if (state->rawdev_op){
		copy_state_file(state); // copy camogm.disk to named state file, fill be used when downloading data as file_start parameter
		if (!restart) {
			copy_state_file(state); // copy camogm.disk to named state file, it will be used when downloading data as file_start parameter
			D1(fprintf(debug_file, "Starting raw data recording, marked %s, state->path='%s'\n", get_state_path(state), state->path));
			D1(syslog (LOG_INFO,    "Starting raw data recording, marked %s", get_state_path(state)));
			next_state_path(state); // increment path so next time will use different one.
		} else {
			D1(fprintf(debug_file, "Re-starting raw data recording, using the same state file, text will be %s, state->path='%s'\n", get_state_path(state), state->path));
			D1(syslog (LOG_INFO,   "Re-starting raw data recording, next will be %s", get_state_path(state)));
		}
	} else {
		D1(fprintf(debug_file, "Starting file system recording to %s\n", state->path));
		D1(syslog (LOG_INFO, "Starting file system recording to %s", state->path));
@@ -1538,10 +1543,13 @@ int parse_cmd(camogm_state *state, FILE* npipe)
	}
	// now cmd is trimmed, arg is NULL or a pointer to trimmed command arguments
	if (strcmp(cmd, "start") == 0) {
		if (state->prog_state != STATE_STOPPED) {
			return -1; // can not start if it is already running
		}
		check_compressors(state);
		get_disk_info(state);
		D6(fprintf(debug_file, "camogm_start() from %s:%d: @ %07d\n", __FILE__, __LINE__,get_fpga_usec(state->fd_fparmsall[0], 0)));
		camogm_start(state);
		camogm_start(state, false);
		return 1;
	} else if (strcmp(cmd, "reset") == 0) { // will reset pointer to the last acquired frame (if any)
		camogm_reset(state);
@@ -1874,7 +1882,7 @@ int listener_loop(camogm_state *state)
				state->prog_state = STATE_RESTARTING;
				D6(fprintf(debug_file, "camogm_start() from %s:%d: @ %07d\n", \
				__FILE__, __LINE__,get_fpga_usec(state->fd_fparmsall[0], 0)));
				camogm_start(state);
				camogm_start(state, true);
				break;
			case  CAMOGM_FRAME_FILE_ERR:    // error with file I/O
			case  CAMOGM_FRAME_OTHER:       // other errors
@@ -1915,7 +1923,7 @@ int listener_loop(camogm_state *state)

			// retry starting
			D2(fprintf(debug_file, "camogm_start() from %s:%d: @ %07d\n", __FILE__, __LINE__,get_fpga_usec(state->fd_fparmsall[0], 0)));
			switch ((rslt = -camogm_start(state))) {
			switch ((rslt = -camogm_start(state, true))) {
			case 0:
				break;                      // file started OK, nothing to do
			case CAMOGM_TOO_EARLY:
@@ -1963,6 +1971,7 @@ int listener_loop(camogm_state *state)
//		} // if pfd.revents & POLLIN
#endif
	} // while (process)
	syslog (LOG_DEBUG, "%s:line %d : Exiting camogm's listener loop @ %07d\n", __FILE__, __LINE__, get_fpga_usec(sstate.fd_fparmsall[0], 0));
	D2(fprintf(debug_file, "End of while (process) from %s:%d: @ %07d\n", \
	__FILE__, __LINE__,get_fpga_usec(state->fd_fparmsall[0], 0)));
	// normally, we should not be here
@@ -2353,7 +2362,7 @@ int open_files(camogm_state *state)
			clean_up(state);
			return -3;
		}
		syslog (LOG_INFO, "open_files(): ccam_dma_buf[%d] = %p", port, ccam_dma_buf[port]);
		D1(syslog (LOG_INFO, "open_files(): ccam_dma_buf[%d] = %p", port, ccam_dma_buf[port]));


		// now open/mmap file to read sensor/compressor parameters (currently - just free memory in circbuf and compressor state)
@@ -2459,7 +2468,7 @@ int main(int argc, char *argv[])
			__FILE__, __LINE__, ret, get_fpga_usec(sstate.fd_fparmsall[0], 0)));

	ret = listener_loop(&sstate);

	syslog (LOG_INFO, "%s:line %d : Exiting camogm @ %07d\n", __FILE__, __LINE__, get_fpga_usec(sstate.fd_fparmsall[0], 0));
	return ret;
}