summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ucs2-afb/ucs_binding.c52
1 files changed, 27 insertions, 25 deletions
diff --git a/ucs2-afb/ucs_binding.c b/ucs2-afb/ucs_binding.c
index c1a0a06..48a333e 100644
--- a/ucs2-afb/ucs_binding.c
+++ b/ucs2-afb/ucs_binding.c
@@ -446,40 +446,42 @@ PUBLIC void ucs2Init (struct afb_req request) {
sd_event_source *evtSource;
int err;
- // Read and parse XML file
+ /* Read and parse XML file */
ucsConfig = ParseFile (request);
if (NULL == ucsConfig) goto OnErrorExit;
- // Fulup->Thorsten BUG InitializeCdevs should fail when control does not exit
- if (!InitializeCdevs(&ucsContext)) {
- afb_req_fail_f (request, "devnit-error", "Fail to initialise device [rx=%s tx=%s]", CONTROL_CDEV_RX, CONTROL_CDEV_TX);
- goto OnErrorExit;
- }
+ /* When ucsContextS is set, do not initalize UNICENS, CDEVs or system hooks, just load new XML */
+ if (!ucsContextS)
+ {
+ if (!ucsContextS && !InitializeCdevs(&ucsContext)) {
+ afb_req_fail_f (request, "devnit-error", "Fail to initialise device [rx=%s tx=%s]", CONTROL_CDEV_RX, CONTROL_CDEV_TX);
+ goto OnErrorExit;
+ }
- // Initialise Unicens Config Data Structure
- UCSI_Init(&ucsContext.ucsiData, &ucsContext);
+ /* Initialise UNICENS Config Data Structure */
+ UCSI_Init(&ucsContext.ucsiData, &ucsContext);
- // Initialise Unicens with parsed config
- if (!UCSI_NewConfig(&ucsContext.ucsiData, ucsConfig)) {
- afb_req_fail_f (request, "UNICENS-init", "Fail to initialize Unicens");
- goto OnErrorExit;
- }
+ /* register aplayHandle file fd into binder mainloop */
+ err = sd_event_add_io(afb_daemon_get_event_loop(afbIface->daemon), &evtSource, ucsContext.rx.fileHandle, EPOLLIN, onReadCB, &ucsContext);
+ if (err < 0) {
+ afb_req_fail_f (request, "register-mainloop", "Cannot hook events to mainloop");
+ goto OnErrorExit;
+ }
- // register aplayHandle file fd into binder mainloop
- err = sd_event_add_io(afb_daemon_get_event_loop(afbIface->daemon), &evtSource, ucsContext.rx.fileHandle, EPOLLIN, onReadCB, &ucsContext);
- if (err < 0) {
- afb_req_fail_f (request, "register-mainloop", "Cannot hook events to mainloop");
- goto OnErrorExit;
+ /* init UNICENS Volume Library */
+ ucsContext.channels = UCSI_Vol_Init (&ucsContext.ucsiData, volumeCB);
+ if (!ucsContext.channels) {
+ afb_req_fail_f (request, "register-volume", "Could not enqueue new Unicens config");
+ goto OnErrorExit;
+ }
+ /* save this in a statical variable until ucs2vol move to C */
+ ucsContextS = &ucsContext;
}
-
- // init Unicens Volume Library
- ucsContext.channels = UCSI_Vol_Init (&ucsContext.ucsiData, volumeCB);
- if (!ucsContext.channels) {
- afb_req_fail_f (request, "register-volume", "Could not enqueue new Unicens config");
+ /* Initialise UNICENS with parsed config */
+ if (!UCSI_NewConfig(&ucsContext.ucsiData, ucsConfig)) {
+ afb_req_fail_f (request, "UNICENS-init", "Fail to initialize UNICENS");
goto OnErrorExit;
}
- // save this in a statical variable until ucs2vol move to C
- ucsContextS = &ucsContext;
afb_req_success(request,NULL,"UNICENS-active");
highlight .o { color: #f92672 } /* Operator */ .highlight .p { color: #f8f8f2 } /* Punctuation */ .highlight .ch { color: #75715e } /* Comment.Hashbang */ .highlight .cm { color: #75715e } /* Comment.Multiline */ .highlight .cp { color: #75715e } /* Comment.Preproc */ .highlight .cpf { color: #75715e } /* Comment.PreprocFile */ .highlight .c1 { color: #75715e } /* Comment.Single */ .highlight .cs { color: #75715e } /* Comment.Special */ .highlight .gd { color: #f92672 } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gi { color: #a6e22e } /* Generic.Inserted */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #75715e } /* Generic.Subheading */ .highlight .kc { color: #66d9ef } /* Keyword.Constant */ .highlight .kd { color: #66d9ef } /* Keyword.Declaration */ .highlight .kn { color: #f92672 } /* Keyword.Namespace */ .highlight .kp { color: #66d9ef } /* Keyword.Pseudo */ .highlight .kr { color: #66d9ef } /* Keyword.Reserved */ .highlight .kt { color: #66d9ef } /* Keyword.Type */ .highlight .ld { color: #e6db74 } /* Literal.Date */ .highlight .m { color: #ae81ff } /* Literal.Number */ .highlight .s { color: #e6db74 } /* Literal.String */ .highlight .na { color: #a6e22e } /* Name.Attribute */ .highlight .nb { color: #f8f8f2 } /* Name.Builtin */ .highlight .nc { color: #a6e22e } /* Name.Class */ .highlight .no { color: #66d9ef } /* Name.Constant */ .highlight .nd { color: #a6e22e } /* Name.Decorator */ .highlight .ni { color: #f8f8f2 } /* Name.Entity */ .highlight .ne { color: #a6e22e } /* Name.Exception */ .highlight .nf { color: #a6e22e } /* Name.Function */ .highlight .nl { color: #f8f8f2 } /* Name.Label */ .highlight .nn { color: #f8f8f2 } /* Name.Namespace */ .highlight .nx { color: #a6e22e } /* Name.Other */ .highlight .py { color: #f8f8f2 } /* Name.Property */ .highlight .nt { color: #f92672 } /* Name.Tag */ .highlight .nv { color: #f8f8f2 } /* Name.Variable */ .highlight .ow { color: #f92672 } /* Operator.Word */ .highlight .w { color: #f8f8f2 } /* Text.Whitespace */ .highlight .mb { color: #ae81ff } /* Literal.Number.Bin */ .highlight .mf { color: #ae81ff } /* Literal.Number.Float */ .highlight .mh { color: #ae81ff } /* Literal.Number.Hex */ .highlight .mi { color: #ae81ff } /* Literal.Number.Integer */ .highlight .mo { color: #ae81ff } /* Literal.Number.Oct */ .highlight .sa { color: #e6db74 } /* Literal.String.Affix */ .highlight .sb { color: #e6db74 } /* Literal.String.Backtick */ .highlight .sc { color: #e6db74 } /* Literal.String.Char */ .highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */ .highlight .sd { color: #e6db74 } /* Literal.String.Doc */ .highlight .s2 { color: #e6db74 } /* Literal.String.Double */ .highlight .se { color: #ae81ff } /* Literal.String.Escape */ .highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */ .highlight .si { color: #e6db74 } /* Literal.String.Interpol */ .highlight .sx { color: #e6db74 } /* Literal.String.Other */ .highlight .sr { color: #e6db74 } /* Literal.String.Regex */ .highlight .s1 { color: #e6db74 } /* Literal.String.Single */ .highlight .ss { color: #e6db74 } /* Literal.String.Symbol */ .highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #a6e22e } /* Name.Function.Magic */ .highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */ .highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */ .highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */ .highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */ .highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */ } @media (prefers-color-scheme: light) { .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
/*------------------------------------------------------------------------------------------------*/
/* UNICENS V2.1.0-3491                                                                            */
/* Copyright (c) 2017 Microchip Technology Germany II GmbH & Co. KG.                              */
/*                                                                                                */
/* 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 2 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/>.                          */
/*                                                                                                */
/* You may also obtain this software under a propriety license from Microchip.                    */
/* Please contact Microchip for further information.                                              */
/*------------------------------------------------------------------------------------------------*/

/*!
 * \file
 * \brief Implementation of the class CStaticMemoryManager.
 * \cond UCS_INTERNAL_DOC
 * \addtogroup G_UCS_SMM_CLASS
 * @{
 */

/*------------------------------------------------------------------------------------------------*/
/* Includes                                                                                       */
/*------------------------------------------------------------------------------------------------*/
#include "ucs_smm.h"
#include "ucs_misc.h"
#include "ucs_trace.h"

/*------------------------------------------------------------------------------------------------*/
/* Internal prototypes                                                                            */
/*------------------------------------------------------------------------------------------------*/
static Smm_Descriptor_t* Smm_GetTypeDescriptor(CStaticMemoryManager *self, Ams_MemUsage_t type);
static void* Smm_Allocate(void *self, uint16_t mem_size, Ams_MemUsage_t type, void** custom_info_pptr);
static void Smm_Free(void *self, void *mem_ptr, Ams_MemUsage_t type, void* custom_info_ptr);

/*------------------------------------------------------------------------------------------------*/
/* Implementation                                                                                 */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Constructor of the static memory manager
 *  \param self         The instance
 *  \param ucs_user_ptr User reference that needs to be passed in every callback function
 */
void Smm_Ctor(CStaticMemoryManager *self, void *ucs_user_ptr)
{
    uint8_t index;
    self->ucs_user_ptr = ucs_user_ptr;

    Dl_Ctor(&self->tx_object_descr.list, ucs_user_ptr);              /* initialize descriptor lists */
    Dl_Ctor(&self->rx_object_descr.list, ucs_user_ptr);
    Dl_Ctor(&self->tx_payload_descr.list, ucs_user_ptr);
    Dl_Ctor(&self->rx_payload_descr.list, ucs_user_ptr);
    Dl_Ctor(&self->null_descr.list, ucs_user_ptr);

    self->tx_object_descr.max_mem_size = AMSG_TX_OBJECT_SZ;         /* initialize descriptor memory sizes */
    self->rx_object_descr.max_mem_size = AMSG_RX_OBJECT_SZ;
    self->tx_payload_descr.max_mem_size = SMM_SIZE_TX_MSG;
    self->rx_payload_descr.max_mem_size = SMM_SIZE_RX_MSG;
    self->null_descr.max_mem_size = 0U;

    for (index = 0U; index < SMM_NUM_TX_MSGS; index++)              /* initialize Tx objects and payload */
    {                                                               /* CDlNode::data_ptr has to point to the memory */
        Dln_Ctor(&self->resources.tx_objects[index].node, &self->resources.tx_objects[index].object);
        Dl_InsertTail(&self->tx_object_descr.list, &self->resources.tx_objects[index].node);

        Dln_Ctor(&self->resources.tx_payload[index].node, &self->resources.tx_payload[index].data);
        Dl_InsertTail(&self->tx_payload_descr.list, &self->resources.tx_payload[index].node);
    }

    for (index = 0U; index < SMM_NUM_RX_MSGS; index++)              /* initialize Rx objects and payload */
    {                                                               /* CDlNode::data_ptr has to point to the memory */
        Dln_Ctor(&self->resources.rx_objects[index].node, &self->resources.rx_objects[index].object);
        Dl_InsertTail(&self->rx_object_descr.list, &self->resources.rx_objects[index].node);

        Dln_Ctor(&self->resources.rx_payload[index].node, &self->resources.rx_payload[index].data);
        Dl_InsertTail(&self->rx_payload_descr.list, &self->resources.rx_payload[index].node);
    }
}

/*! \brief  Load function of the static memory management plug-in.
 *  \param  self                The instance
 *  \param  allocator_ptr       Assignable interface for allocate and free functions
 *  \param  rx_def_payload_size The default Rx allocation size the AMS uses if TelId "4" is missing.
 *                              Just use for checks. Do not overrule.
 *  \return Returns \c UCS_RET_SUCCESS if the initialization succeeded, otherwise \c UCS_RET_ERR_PARAM.
 */
Ucs_Return_t Smm_LoadPlugin(CStaticMemoryManager *self, Ams_MemAllocator_t *allocator_ptr, uint16_t rx_def_payload_size)
{
    Ucs_Return_t ret = UCS_RET_SUCCESS;

    allocator_ptr->inst_ptr = self;             /* assign instance to allocator */
    allocator_ptr->alloc_fptr = &Smm_Allocate;  /* assign callback functions */
    allocator_ptr->free_fptr = &Smm_Free;

    if (rx_def_payload_size != SMM_SIZE_RX_MSG)
    {
        ret = UCS_RET_ERR_PARAM;
        TR_ERROR((self->ucs_user_ptr, "[SMM]", "SMM initialization failed: wrong configuration of rx_def_payload_size.", 0U));
    }

    return ret;
}

/*! \brief  Retrieves a descriptor for a memory type
 *  \param  self  The instance
 *  \param  type  Usage type of the requested memory
 *  \return Returns the respective descriptor for a memory type
 */
static Smm_Descriptor_t* Smm_GetTypeDescriptor(CStaticMemoryManager *self, Ams_MemUsage_t type)
{
    Smm_Descriptor_t* descr_ptr = NULL;

    switch (type)
    {
        case AMS_MU_RX_OBJECT:
            descr_ptr = &self->rx_object_descr;
            break;
        case AMS_MU_RX_PAYLOAD:
            descr_ptr = &self->rx_payload_descr;
            break;
        case AMS_MU_TX_OBJECT:
            descr_ptr = &self->tx_object_descr;
            break;
        case AMS_MU_TX_PAYLOAD:
            descr_ptr = &self->tx_payload_descr;
            break;
        default:
            TR_FAILED_ASSERT(self->ucs_user_ptr, "[SMM]");  /* requested memory for unknown type */
            descr_ptr = &self->null_descr;
            break;
    }

    return descr_ptr;
}

/*! \brief  Allocates memory of a certain type
 *  \param  self             The instance
 *  \param  mem_size         Size of the memory in bytes
 *  \param  type             The memory usage type
 *  \param  custom_info_pptr Reference to custom information
 *  \return Returns a reference to the allocated memory or \c NULL if the allocation is not possible
 */
static void* Smm_Allocate(void *self, uint16_t mem_size, Ams_MemUsage_t type, void** custom_info_pptr)
{
    CStaticMemoryManager *self_ = (CStaticMemoryManager*)self;
    void *mem_ptr = NULL;
    CDlNode *node_ptr = NULL;

    Smm_Descriptor_t* descr_ptr = Smm_GetTypeDescriptor(self_, type);

    if (mem_size <= descr_ptr->max_mem_size)
    {
        node_ptr = Dl_PopHead(&descr_ptr->list);    /* size is ok, retrieve a node from the list */
    }

    if (node_ptr != NULL)
    {
        mem_ptr = Dln_GetData(node_ptr);            /* retrieve reference of whole message object */
        *custom_info_pptr = node_ptr;
    }

    return mem_ptr;
}

/*! \brief  Frees memory of a certain type
 *  \param  self             The instance
 *  \param  mem_ptr          Reference to the memory chunk
 *  \param  type             The memory usage type
 *  \param  custom_info_ptr  Reference to custom information
 */
static void Smm_Free(void *self, void *mem_ptr, Ams_MemUsage_t type, void* custom_info_ptr)
{
    CStaticMemoryManager *self_ = (CStaticMemoryManager*)self;
    Smm_Descriptor_t* descr_ptr = Smm_GetTypeDescriptor(self_, type);

    Dl_InsertHead(&descr_ptr->list, (CDlNode*)custom_info_ptr);
    MISC_UNUSED(mem_ptr);
}

/*! \brief  Retrieves the current number of unused message objects.
 *  \param  self        The instance
 *  \param  rx_cnt_ptr  Application provided reference to write the current number of unused Rx message objects.
 *  \param  tx_cnt_ptr  Application provided reference to write the current number of unused Tx message objects.
 *  \return Returns \c UCS_RET_ERR_PARAM if \c NULL is provided otherwise \c UCS_RET_SUCCESS. 
 */
Ucs_Return_t Smm_GetFreeBufferCnt(CStaticMemoryManager *self, uint16_t *rx_cnt_ptr, uint16_t *tx_cnt_ptr)
{
    Ucs_Return_t ret = UCS_RET_SUCCESS;

    if ((tx_cnt_ptr != NULL) && (rx_cnt_ptr != NULL))
    {
        *rx_cnt_ptr = Dl_GetSize(&self->rx_object_descr.list);
        *tx_cnt_ptr = Dl_GetSize(&self->tx_object_descr.list);
    }
    else
    {
        ret = UCS_RET_ERR_PARAM;
    }

    return ret;
}


/*!
 * @}
 * \endcond
 */

/*------------------------------------------------------------------------------------------------*/
/* End of file                                                                                    */
/*------------------------------------------------------------------------------------------------*/