Commit 343b9c9b authored by Mikhail Karpenko's avatar Mikhail Karpenko

Move linked list functions to a separate file

parent 1438b135
...@@ -17,9 +17,8 @@ PROGS = camogm ...@@ -17,9 +17,8 @@ PROGS = camogm
PHPFILES = camogmstate.php PHPFILES = camogmstate.php
SRCS = camogm.c camogm_ogm.c camogm_jpeg.c camogm_mov.c camogm_kml.c camogm_read.c SRCS = camogm.c camogm_ogm.c camogm_jpeg.c camogm_mov.c camogm_kml.c camogm_read.c index_list.c
OBJS = camogm.o camogm_ogm.o camogm_jpeg.o camogm_mov.o camogm_kml.o camogm_read.o index_list.o
OBJS = camogm.o camogm_ogm.o camogm_jpeg.o camogm_mov.o camogm_kml.o camogm_read.o
CFLAGS += -Wall -I$(ELPHEL_KERNEL_DIR)/include/elphel CFLAGS += -Wall -I$(ELPHEL_KERNEL_DIR)/include/elphel
#CFLAGS += -Wall -I$(INCDIR) -I$(ELPHEL_KERNEL_DIR)/include/elphel #CFLAGS += -Wall -I$(INCDIR) -I$(ELPHEL_KERNEL_DIR)/include/elphel
......
...@@ -47,11 +47,14 @@ ...@@ -47,11 +47,14 @@
#include <sys/stat.h> #include <sys/stat.h>
#include "camogm_read.h" #include "camogm_read.h"
#include "index_list.h"
/** @brief Offset in Exif where TIFF header starts */ /** @brief Offset in Exif where TIFF header starts */
#define TIFF_HDR_OFFSET 12 #define TIFF_HDR_OFFSET 12
/** @brief The date and time format of Exif field */ /** @brief The date and time format of Exif field */
#define EXIF_DATE_TIME_FORMAT "%Y:%m:%d %H:%M:%S" #define EXIF_DATE_TIME_FORMAT "%Y:%m:%d %H:%M:%S"
/** @brief The format string used for file parameters reporting. Time and port number are extracted from Exif */
#define INDEX_FORMAT_STR "port_number=%d;unix_time=%ld;usec_time=%06u;offset=0x%010llx;file_size=%u\n"
/** @brief The delimiters used to separate several commands in one command string sent over socket */ /** @brief The delimiters used to separate several commands in one command string sent over socket */
#define CMD_DELIMITER "/?" #define CMD_DELIMITER "/?"
/** @brief The length of a buffer for command string */ /** @brief The length of a buffer for command string */
...@@ -60,8 +63,6 @@ ...@@ -60,8 +63,6 @@
#define SMALL_BUFF_LEN 32 #define SMALL_BUFF_LEN 32
/** @brief 64 bit mask to align offsets to 4 kb page boundary */ /** @brief 64 bit mask to align offsets to 4 kb page boundary */
#define PAGE_BOUNDARY_MASK 0xffffffffffffe000 #define PAGE_BOUNDARY_MASK 0xffffffffffffe000
/** @brief The format string used for file parameters reporting. Time and port number are extracted from Exif */
#define INDEX_FORMAT_STR "port_number=%d;unix_time=%ld;usec_time=%06u;offset=0x%010llx;file_size=%u\n"
/** @brief The size of read buffer in bytes. The data will be read from disk in blocks of this size */ /** @brief The size of read buffer in bytes. The data will be read from disk in blocks of this size */
#define PHY_BLK_SZ 4096 #define PHY_BLK_SZ 4096
/** @brief Include or exclude file start and stop markers from resulting file. This must be set to 1 for JPEG files */ /** @brief Include or exclude file start and stop markers from resulting file. This must be set to 1 for JPEG files */
...@@ -247,128 +248,6 @@ void dump_index_dir(const struct disk_idir *idir) ...@@ -247,128 +248,6 @@ void dump_index_dir(const struct disk_idir *idir)
} }
} }
/**
* @brief Create a new node in a linked list of disk indexes
* @param[in,out] index pointer to a newly allocated structure
* @return 0 in case a new disk index structure was successfully created and -1 otherwise
*/
int create_node(struct disk_index **index)
{
if (*index != NULL)
return -1;
*index = malloc(sizeof(struct disk_index));
if (*index != NULL) {
memset(*index, 0, sizeof(struct disk_index));
return 0;
} else {
return -1;
}
}
/**
* @brief Add a new index node to disk index directory
* @param[in,out] idir pointer to disk index directory
* @param[in] index pointer to index node to be added to disk index directory
* @return The number of entries in disk index directory
*/
int add_node(struct disk_idir *idir, struct disk_index *index)
{
if (idir->head == NULL && idir->tail == NULL) {
idir->head = index;
idir->tail = index;
idir->size = 1;
} else {
index->prev = idir->tail;
idir->tail->next = index;
idir->tail = index;
idir->size++;
}
return idir->size;
}
/**
* @brief Find index node by its start offset
* @param[in] idir pointer to disk index directory
* @param[in] offset the offset of the file which should be found
* @return pointer to disk index node or NULL if the corresponding file was not found
*/
struct disk_index *find_by_offset(const struct disk_idir *idir, uint64_t offset)
{
struct disk_index *index = idir->head;
while (index != NULL) {
if (index->f_offset == offset)
break;
index = index->next;
}
return index;
}
/**
* @brief Remove a single index node from disk index directory
* @param[in,out] idir pointer to disk index directory
* @param[in] node pointer to the index node which should be removed
* @return The number of entries in disk index directory
*/
int remove_node(struct disk_idir *idir, struct disk_index *node)
{
if (node == NULL)
return -1;
if (node == idir->head) {
idir->head = node->next;
idir->head->prev = NULL;
} else if (node == idir->tail) {
idir->tail = node->prev;
idir->tail->next = NULL;
} else {
struct disk_index *ind = idir->head;
while (ind != NULL) {
if (ind == node) {
ind->prev->next = ind->next;
ind->next->prev = ind->prev;
break;
}
ind = ind->next;
}
}
free(node);
node = NULL;
idir->size--;
return idir->size;
}
/**
* Remove all entries from disk index directory an free memory
* @param[in] idir pointer to disk index directory
* @return 0 in case the directory was successfully deleted and -1 if the directory was empty
*/
int delete_idir(struct disk_idir *idir)
{
struct disk_index *curr_ind;
struct disk_index *next_ind;
if (idir == NULL || idir->head == NULL)
return -1;
curr_ind = idir->head;
next_ind = curr_ind->next;
while (curr_ind != NULL) {
free(curr_ind);
curr_ind = next_ind;
if (curr_ind != NULL)
next_ind = curr_ind->next;
}
idir->head = idir->tail = NULL;
idir->size = 0;
return 0;
}
/** /**
* @brief Find pattern in a data buffer * @brief Find pattern in a data buffer
* *
......
...@@ -19,52 +19,6 @@ ...@@ -19,52 +19,6 @@
#include "camogm.h" #include "camogm.h"
/**
* @struct disk_index
* @brief Contains a single entry into disk index directory. Each node in
* the disk index directory corresponds to a file in the raw device buffer and
* hold its starting offset, sensor port number, time stamp and file size.
* @var disk_index::next
* Pointer to the next index node
* @var disk_index::prev
* Pointer to the previous disk index node
* @var disk_index::rawtime
* Time stamp in UNIX format
* @var disk_index::usec
* The microsecond part of the time stamp
* @var disk_index::port
* The sensor port number this frame was captured from
* @var disk_index::f_size
* File size in bytes
* @var disk_index::f_offset
* The offset of the file start in the raw device buffer (in bytes)
*/
struct disk_index {
struct disk_index *next;
struct disk_index *prev;
time_t rawtime;
unsigned int usec;
uint32_t port;
size_t f_size;
uint64_t f_offset;
};
/**
* @struct disk_idir
* @brief Contains pointers to disk index directory
* @var disk_idir::head
* Pointer to the first node of disk index directory
* @var disk_idir::tail
* Pointer to the last node of disk index directory
* @var disk_idir::size
* The number of nodes in disk index directory
*/
struct disk_idir {
struct disk_index *head;
struct disk_index *tail;
size_t size;
};
/** /**
* @struct range * @struct range
* @brief Container for offsets in raw device buffer * @brief Container for offsets in raw device buffer
......
/** @file index_list.c
* @brief Provides data structures and functions for working with disk index directory
* @copyright Copyright (C) 2016 Elphel, Inc.
*
* <b>License:</b>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include "index_list.h"
/**
* @brief Create a new node in a linked list of disk indexes
* @param[in,out] index pointer to a newly allocated structure
* @return 0 in case a new disk index structure was successfully created and -1 otherwise
*/
int create_node(struct disk_index **index)
{
if (*index != NULL)
return -1;
*index = malloc(sizeof(struct disk_index));
if (*index != NULL) {
memset(*index, 0, sizeof(struct disk_index));
return 0;
} else {
return -1;
}
}
/**
* @brief Add a new index node to disk index directory
* @param[in,out] idir pointer to disk index directory
* @param[in] index pointer to index node to be added to disk index directory
* @return The number of entries in disk index directory
*/
int add_node(struct disk_idir *idir, struct disk_index *index)
{
if (idir->head == NULL && idir->tail == NULL) {
idir->head = index;
idir->tail = index;
idir->size = 1;
} else {
index->prev = idir->tail;
idir->tail->next = index;
idir->tail = index;
idir->size++;
}
return idir->size;
}
/**
* @brief Find index node by its start offset
* @param[in] idir pointer to disk index directory
* @param[in] offset the offset of the file which should be found
* @return pointer to disk index node or NULL if the corresponding file was not found
*/
struct disk_index *find_by_offset(const struct disk_idir *idir, uint64_t offset)
{
struct disk_index *index = idir->head;
while (index != NULL) {
if (index->f_offset == offset)
break;
index = index->next;
}
return index;
}
/**
* @brief Remove a single index node from disk index directory
* @param[in,out] idir pointer to disk index directory
* @param[in] node pointer to the index node which should be removed
* @return The number of entries in disk index directory
*/
int remove_node(struct disk_idir *idir, struct disk_index *node)
{
if (node == NULL)
return -1;
if (node == idir->head) {
idir->head = node->next;
idir->head->prev = NULL;
} else if (node == idir->tail) {
idir->tail = node->prev;
idir->tail->next = NULL;
} else {
struct disk_index *ind = idir->head;
while (ind != NULL) {
if (ind == node) {
ind->prev->next = ind->next;
ind->next->prev = ind->prev;
break;
}
ind = ind->next;
}
}
free(node);
node = NULL;
idir->size--;
return idir->size;
}
/**
* Remove all entries from disk index directory an free memory
* @param[in] idir pointer to disk index directory
* @return 0 in case the directory was successfully deleted and -1 if the directory was empty
*/
int delete_idir(struct disk_idir *idir)
{
struct disk_index *curr_ind;
struct disk_index *next_ind;
if (idir == NULL || idir->head == NULL)
return -1;
curr_ind = idir->head;
next_ind = curr_ind->next;
while (curr_ind != NULL) {
free(curr_ind);
curr_ind = next_ind;
if (curr_ind != NULL)
next_ind = curr_ind->next;
}
idir->head = idir->tail = NULL;
idir->size = 0;
return 0;
}
/** @file index_list.h
* @brief Provides data structures and functions for working with disk index directory
* @copyright Copyright (C) 2016 Elphel, Inc.
*
* <b>License:</b>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _INDEX_LIST_H
#define _INDEX_LIST_H
#include <stdint.h>
/**
* @struct disk_index
* @brief Contains a single entry into disk index directory. Each node in
* the disk index directory corresponds to a file in the raw device buffer and
* hold its starting offset, sensor port number, time stamp and file size.
* @var disk_index::next
* Pointer to the next index node
* @var disk_index::prev
* Pointer to the previous disk index node
* @var disk_index::rawtime
* Time stamp in UNIX format
* @var disk_index::usec
* The microsecond part of the time stamp
* @var disk_index::port
* The sensor port number this frame was captured from
* @var disk_index::f_size
* File size in bytes
* @var disk_index::f_offset
* The offset of the file start in the raw device buffer (in bytes)
*/
struct disk_index {
struct disk_index *next;
struct disk_index *prev;
time_t rawtime;
unsigned int usec;
uint32_t port;
size_t f_size;
uint64_t f_offset;
};
/**
* @struct disk_idir
* @brief Contains pointers to disk index directory
* @var disk_idir::head
* Pointer to the first node of disk index directory
* @var disk_idir::tail
* Pointer to the last node of disk index directory
* @var disk_idir::size
* The number of nodes in disk index directory
*/
struct disk_idir {
struct disk_index *head;
struct disk_index *tail;
size_t size;
};
void dump_index_dir(const struct disk_idir *idir);
int create_node(struct disk_index **index);
int add_node(struct disk_idir *idir, struct disk_index *index);
struct disk_index *find_by_offset(const struct disk_idir *idir, uint64_t offset);
int remove_node(struct disk_idir *idir, struct disk_index *node);
int delete_idir(struct disk_idir *idir);
#endif /* _INDEX_LIST_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment