Added pre-main capabilities, cleaned up services and NameServer modules
[keystone-rtos/rm-lld.git] / src / rm_nameserver.c
1 /**
2  *   @file  rm_nameserver.c
3  *
4  *   @brief   
5  *      Resource Manager NameServer source.
6  *
7  *  \par
8  *  ============================================================================
9  *  @n   (C) Copyright 2012, Texas Instruments, Inc.
10  * 
11  *  Redistribution and use in source and binary forms, with or without 
12  *  modification, are permitted provided that the following conditions 
13  *  are met:
14  *
15  *    Redistributions of source code must retain the above copyright 
16  *    notice, this list of conditions and the following disclaimer.
17  *
18  *    Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the 
20  *    documentation and/or other materials provided with the   
21  *    distribution.
22  *
23  *    Neither the name of Texas Instruments Incorporated nor the names of
24  *    its contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
28  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
29  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
31  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
32  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
33  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
36  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
37  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  *
39  *  \par
40 */
42 /* RM Types */
43 #include <ti/drv/rm/rm_types.h>
45 /* RM external API includes */
46 #include <ti/drv/rm/rm_services.h>
48 /* RM internal API includes */
49 #include <ti/drv/rm/include/rm_nameserverloc.h>
51 /* AVL BBST includes */
52 #include <ti/drv/rm/include/tree.h>
54 /* RM OSAL layer */
55 #include <rm_osal.h>
57 /**********************************************************************
58  ********************* NameServer Tree Functions **********************
59  **********************************************************************/
61 Rm_NameServerNode *Rm_nameServerNewNode(Rm_NameServerNodeCfg *nodeCfg)
62 {
63     Rm_NameServerNode *newNode = NULL;
65     newNode = Rm_osalMalloc(sizeof(Rm_NameServerNode));
66     if (newNode) {
67         strcpy(newNode->objName, nodeCfg->objName);
68         strcpy(newNode->resourceName, nodeCfg->resourceName); 
69         newNode->resourceBase = nodeCfg->resourceBase;
70         newNode->resourceLength = nodeCfg->resourceLength;
71     }
72     
73     return(newNode);
74 }
76 void Rm_nameServerFreeNode(Rm_NameServerNode *node)
77 {
78     if (node) {
79         Rm_osalFree((void *)node, sizeof(Rm_NameServerNode));
80     }
81 }
83 int Rm_nameServerNodeCompare(Rm_NameServerNode *node1, Rm_NameServerNode *node2)
84 {
85     return(strcmp(node1->objName, node2->objName));
86 }
88 /* Generate the NameServer tree manipulation functions */
89 RB_GENERATE(_Rm_NameServerTree, _Rm_NameServerNode, linkage, Rm_nameServerNodeCompare);
91 /**********************************************************************
92  ******************* Internal NameServer APIs *************************
93  **********************************************************************/
95 int32_t Rm_nameServerAddObject(Rm_NameServerObjCfg *objCfg)
96 {
97     Rm_NameServerNode    *newNode = NULL;
98     int32_t               retVal = RM_SERVICE_APPROVED_AND_COMPLETED;
99     
100     if (newNode = Rm_nameServerNewNode(&objCfg->nodeCfg)) {
101         if (RB_INSERT(_Rm_NameServerTree, objCfg->nameServerTree, newNode)) {
102             /* Collision */
103             Rm_nameServerFreeNode(newNode);
104             retVal = RM_SERVICE_DENIED_NAMESERVER_ERROR_NAME_ALREADY_EXISTS;
105         }
106     }
107     else {
108         retVal = RM_SERVICE_ERROR_NAMESERVER_OBJECT_ADD_FAILED;
109     }
110     
111     return(retVal);
114 int32_t Rm_nameServerFindObject(Rm_NameServerObjCfg *objCfg)
116     Rm_NameServerNode  findNode;
117     Rm_NameServerNode *matchingNode;
118     int32_t            retVal = RM_SERVICE_ERROR_NAMESERVER_ERROR_NAME_DOES_NOT_EXIST;
120     memset((void *)&findNode, 0, sizeof(Rm_NameServerNode));
121     strcpy(findNode.objName, objCfg->nodeCfg.objName);
122     
123     if (matchingNode = RB_FIND(_Rm_NameServerTree, objCfg->nameServerTree, &findNode)) {
124         /* Found in NameServer */
125         strcpy(objCfg->nodeCfg.resourceName, matchingNode->resourceName);
126         objCfg->nodeCfg.resourceBase = matchingNode->resourceBase;
127         objCfg->nodeCfg.resourceLength = matchingNode->resourceLength;
129         retVal = RM_SERVICE_PROCESSING;
130     }    
131     return(retVal);
134 int32_t Rm_nameServerDeleteObject(Rm_NameServerObjCfg *objCfg)
135 {   
136     Rm_NameServerNode  findNode;
137     Rm_NameServerNode *matchingNode;
138     int32_t            retVal = RM_SERVICE_ERROR_NAMESERVER_ERROR_NAME_DOES_NOT_EXIST;    
140     memset((void *)&findNode, 0, sizeof(Rm_NameServerNode));
141     strcpy(findNode.objName, objCfg->nodeCfg.objName);
142     
143     if (matchingNode = RB_FIND(_Rm_NameServerTree, objCfg->nameServerTree, &findNode)) {
144         /* Remove from NameServer */
145         RB_REMOVE(_Rm_NameServerTree, objCfg->nameServerTree, matchingNode);
146         Rm_nameServerFreeNode(matchingNode);
147         
148         retVal = RM_SERVICE_APPROVED_AND_COMPLETED;
149     }    
150     return(retVal);
153 void Rm_nameServerPrintObjects(Rm_NameServerTree *nameServerTree)
155     Rm_NameServerNode *node;
157     RB_FOREACH(node, _Rm_NameServerTree, nameServerTree) {               
158         Rm_osalLog("Name: %s resourceName: %s base: %d length: %d\n", node->objName, node->resourceName,
159                                                                       node->resourceBase, node->resourceLength);
160     }
163 Rm_NameServerTree *Rm_nameServerInit(void)
165     Rm_NameServerTree *rootEntry = NULL;
167     rootEntry = Rm_osalMalloc(sizeof(Rm_NameServerTree));
168     RB_INIT(rootEntry);
170     return(rootEntry);