Commit 6f8f1498 authored by jdelange's avatar jdelange

* include/po_hi_storage.h

   src/po_hi_storage.c
   - Implement the packet store function.



git-svn-id: https://tecsw.estec.esa.int/svn/taste/trunk/po-hi-c@3515 129961e7-ef38-4bb5-a8f7-c9a525a55882
parent 4e80f972
......@@ -15,6 +15,19 @@
#include <deployment.h>
#include <request.h>
#include <deployment.h>
/* included files from the generated code */
#include <po_hi_config.h>
#include <po_hi_common.h>
#include <po_hi_returns.h>
#include <po_hi_monitor.h>
#include <po_hi_task.h>
#include <po_hi_debug.h>
#include <po_hi_protected.h>
#include <po_hi_utils.h>
#ifndef __PO_HI_STORAGE_FILENAME_MAXLENGTH
#define __PO_HI_STORAGE_FILENAME_MAXLENGTH 100
#endif
......@@ -50,11 +63,20 @@ typedef struct
char filename[__PO_HI_STORAGE_DIRECTORY_MAXFILES][__PO_HI_STORAGE_FILENAME_MAXLENGTH];
} __po_hi_storage_dir_t;
typedef __po_hi_request_t __po_hi_storage_packet_t;
#ifndef __PO_HI_STORAGE_PACKET_SIZE
#define __PO_HI_STORAGE_PACKET_SIZE sizeof(__po_hi_request_t __po_hi_storage_packet_t)
#endif
typedef __po_hi_uint8_t __po_hi_storage_packet_t;
typedef struct
{
__po_hi_storage_packet_t packets[__PO_HI_STORAGE_MAX_PACKETS];
__po_hi_uint8_t packets[__PO_HI_STORAGE_PACKET_SIZE * __PO_HI_STORAGE_MAX_PACKETS]; /*< packets contained int he store (structured bytes) >*/
int n_packets; /*< amount of packets stored in the store >*/
int capacity; /*< actual size of the store, meaning the number of packets it can store >*/
int read_off; /*< read offset in the buffer in terms of number of packets >*/
int write_off; /*< write offset in the buffer in terns of number of packets >*/
__po_hi_mutex_t mutex; /*< ensure buffer protection from concurrent accesses >*/
} __po_hi_storage_packet_store_t;
......@@ -415,6 +437,7 @@ int __po_hi_storage_get_cdir (__po_hi_storage_dir_t* current_directory);
* Upon success, the function returns __PO_HI_SUCCESS.
* It returns the following potential values:
* - __PO_HI_SUCCESS : successful operation
* - __PO_HI_ERROR_MUTEX_INIT : error when trying to instantiate locking-related resources
* - __PO_HI_TOOMANY : too many store already created
*/
int __po_hi_storage_packet_store_new (__po_hi_storage_packet_store_t* store);
......@@ -487,6 +510,10 @@ int __po_hi_storage_packet_store_read (__po_hi_storage_packet_store_t* store, __
* - __PO_HI_SUCCESS : successful operation
* - __PO_HI_TOOMANY : the store contains already too many
* packets, cannot write more packets.
* - __PO_HI_ERROR_MUTEX_LOCK : error when trying to acquire the mutex
* associated with the packet store.
* - __PO_HI_ERROR_MUTEX_UNLOCK : error when trying to release the mutex
* associated with the packet store.
* - __PO_HI_INVALID : invalid packet store or packet to write
* (invalid address)
*/
......
......@@ -8,6 +8,8 @@
* Copyright (C) 2011, European Space Agency.
*/
#include <string.h>
#include <po_hi_debug.h>
#include <po_hi_storage.h>
#include <po_hi_returns.h>
......@@ -121,7 +123,15 @@ int __po_hi_storage_get_cdir (__po_hi_storage_dir_t* current_directory)
int __po_hi_storage_packet_store_new (__po_hi_storage_packet_store_t* store)
{
return __PO_HI_NOTIMPLEMENTED;
if (__po_hi_mutex_init (&store->mutex, __PO_HI_MUTEX_REGULAR, 0) != __PO_HI_SUCCESS)
{
return __PO_HI_ERROR_MUTEX_CREATE;
}
store->capacity = __PO_HI_STORAGE_MAX_PACKETS;
store->n_packets = 0;
store->read_off = 0;
store->write_off = 0;
return __PO_HI_SUCCESS;
}
int __po_hi_storage_packet_store_new_from_file (__po_hi_storage_packet_store_t* store, __po_hi_storage_file_t* file)
......@@ -136,22 +146,155 @@ int __po_hi_storage_packet_store_write_to_file (__po_hi_storage_packet_store_t*
int __po_hi_storage_packet_store_read (__po_hi_storage_packet_store_t* store, __po_hi_storage_packet_t* packet)
{
return __PO_HI_NOTIMPLEMENTED;
int retcode = __PO_HI_ERROR_UNKNOWN;
if (store == NULL)
{
__DEBUGMSG ("[STORAGE] __po_hi_storage_packet_store_read: store is NULL\n");
return __PO_HI_INVALID;
}
if (packet == NULL)
{
__DEBUGMSG ("[STORAGE] __po_hi_storage_packet_store_read: packet is NULL\n");
return __PO_HI_INVALID;
}
if (__po_hi_mutex_lock (&store->mutex) != __PO_HI_SUCCESS)
{
return __PO_HI_ERROR_MUTEX_LOCK;
}
if (store->n_packets < 1)
{
retcode = __PO_HI_UNAVAILABLE;
}
else
{
memcpy (packet, &(store->packets[store->read_off * __PO_HI_STORAGE_PACKET_SIZE]), __PO_HI_STORAGE_PACKET_SIZE);
store->read_off = (store->read_off + 1 % store->capacity);
store->n_packets = store->n_packets - 1;
retcode = __PO_HI_SUCCESS;
}
if (__po_hi_mutex_unlock (&store->mutex) != __PO_HI_SUCCESS)
{
return __PO_HI_ERROR_MUTEX_UNLOCK;
}
return retcode;
}
int __po_hi_storage_packet_store_write (__po_hi_storage_packet_store_t* store, __po_hi_storage_packet_t* packet)
{
return __PO_HI_NOTIMPLEMENTED;
int retcode = __PO_HI_ERROR_UNKNOWN;
if (store == NULL)
{
__DEBUGMSG ("[STORAGE] __po_hi_storage_packet_store_write: store is NULL\n");
return __PO_HI_INVALID;
}
if (packet == NULL)
{
__DEBUGMSG ("[STORAGE] __po_hi_storage_packet_store_write: packet is NULL\n");
return __PO_HI_INVALID;
}
if (__po_hi_mutex_lock (&store->mutex) != __PO_HI_SUCCESS)
{
__DEBUGMSG ("[STORAGE] __po_hi_storage_packet_store_write: cannot lock mutex\n");
return __PO_HI_ERROR_MUTEX_LOCK;
}
if (store->n_packets >= store->capacity)
{
__DEBUGMSG ("[STORAGE] __po_hi_storage_packet_store_write: store is full\n");
retcode = __PO_HI_TOOMANY;
}
else
{
memcpy (&(store->packets[store->write_off * __PO_HI_STORAGE_PACKET_SIZE]), packet, __PO_HI_STORAGE_PACKET_SIZE);
store->write_off = (store->write_off + 1 % store->capacity);
store->n_packets = store->n_packets + 1;
retcode = __PO_HI_SUCCESS;
}
if (__po_hi_mutex_unlock (&store->mutex) != __PO_HI_SUCCESS)
{
__DEBUGMSG ("[STORAGE] __po_hi_storage_packet_store_write: cannot unlock mutex\n");
return __PO_HI_ERROR_MUTEX_UNLOCK;
}
return retcode;
}
int __po_hi_storage_packet_store_free (__po_hi_storage_packet_store_t* store, int n_free)
{
return __PO_HI_NOTIMPLEMENTED;
int retcode = __PO_HI_ERROR_UNKNOWN;
if (store == NULL)
{
return __PO_HI_INVALID;
}
if (n_free == 0)
{
return __PO_HI_INVALID;
}
if (__po_hi_mutex_lock (&store->mutex) != __PO_HI_SUCCESS)
{
return __PO_HI_ERROR_MUTEX_LOCK;
}
if (store->n_packets < n_free)
{
retcode = __PO_HI_UNAVAILABLE;
}
else
{
store->read_off = (store->read_off + n_free % store->capacity);
store->n_packets = store->n_packets - n_free;
retcode = __PO_HI_SUCCESS;
}
if (__po_hi_mutex_unlock (&store->mutex) != __PO_HI_SUCCESS)
{
return __PO_HI_ERROR_MUTEX_UNLOCK;
}
return retcode;
}
int __po_hi_storage_packet_store_status (__po_hi_storage_packet_store_t* store, __po_hi_storage_packet_store_status_t* status)
{
return __PO_HI_NOTIMPLEMENTED;
if (store == NULL)
{
return __PO_HI_INVALID;
}
if (status == NULL)
{
return __PO_HI_INVALID;
}
if (__po_hi_mutex_lock (&store->mutex) != __PO_HI_SUCCESS)
{
return __PO_HI_ERROR_MUTEX_LOCK;
}
status->n_packets = store->n_packets;
status->n_avail = store->capacity - store->n_packets;
status->full = (store->n_packets == store->capacity) ? 1 : 0;
if (__po_hi_mutex_unlock (&store->mutex) != __PO_HI_SUCCESS)
{
return __PO_HI_ERROR_MUTEX_UNLOCK;
}
return __PO_HI_SUCCESS;
}
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