84bb331a477d676c3db883d4cb28bce692560949
1 /**
2 * @file rm_nameserver.c
3 *
4 * @brief
5 * This is the 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_loc.h>
50 #include <ti/drv/rm/include/rm_nameserverloc.h>
52 /* AVL BBST includes */
53 #include <ti/drv/rm/include/tree.h>
55 /* RM OSAL layer */
56 #include <rm_osal.h>
58 /**********************************************************************
59 ************** Red-Black BBST Tree NameServer Functions **************
60 **********************************************************************/
62 Rm_NameServerNode *Rm_newNameServerNode(char *name, uint32_t resourceBase,
63 uint32_t resourceLength)
64 {
65 Rm_NameServerNode *newNode = NULL;
67 newNode = Rm_osalMalloc(sizeof(Rm_NameServerNode));
69 /* Populate the node */
70 strcpy(newNode->name, name);
71 newNode->base = resourceBase;
72 newNode->length = resourceLength;
74 return(newNode);
75 }
77 void Rm_freeNameServerNode(Rm_NameServerNode *node)
78 {
79 /* Free the memory associated with the tree node. */
80 Rm_osalFree((void *)node, sizeof(Rm_NameServerNode));
81 }
83 /* Prototype for tree node comparison function
84 * element1 < element2 --> return < 0
85 * element1 = element2 --> return 0
86 * element1 > element2 --> return > 0 */
87 int Rm_NameServerNodeCompare(Rm_NameServerNode *element1, Rm_NameServerNode *element2)
88 {
89 return(strcmp(element1->name, element2->name));
90 }
92 /* Generate the red-black tree manipulation functions */
93 RB_GENERATE(_Rm_NameServerTree, _Rm_NameServerNode, linkage, Rm_NameServerNodeCompare);
95 /**********************************************************************
96 ********************** Internal Functions ****************************
97 **********************************************************************/
99 int32_t Rm_nsInit(Rm_Inst *rmInst)
100 {
101 Rm_NameServerTree *rootEntry = NULL;
103 /* Create the NameServer root entry and initialize it */
104 rootEntry = Rm_osalMalloc(sizeof(Rm_NameServerTree));
105 RB_INIT(rootEntry);
107 rmInst->nameServer = (void *)rootEntry;
109 return(RM_NS_ACTION_APPROVED);
110 }
112 int32_t Rm_nsAddObject(Rm_Inst *rmInst, Rm_ResourceInfo *resourceInfo)
113 {
114 Rm_NameServerNode *newNode = NULL;
115 Rm_NameServerNode *collidingNode = NULL;
117 newNode = Rm_newNameServerNode(resourceInfo->nsName, resourceInfo->base,
118 resourceInfo->length);
120 /* Try to insert the new name */
121 collidingNode = RB_INSERT(_Rm_NameServerTree, rmInst->nameServer, newNode);
123 if (collidingNode)
124 {
125 Rm_freeNameServerNode(newNode);
126 return(RM_NS_ERROR_NAME_ALREADY_EXISTS);
127 }
129 return(RM_NS_ACTION_APPROVED);
130 }
132 void Rm_nsPrintObjects(Rm_Inst *rmInst)
133 {
134 Rm_NameServerNode *node;
136 RB_FOREACH(node, _Rm_NameServerTree, rmInst->nameServer)
137 {
138 Rm_osalLog("Name: %s base: %d length: %d\n", node->name, node->base, node->length);
139 }
140 }
142 int32_t Rm_nsFindObject(Rm_Inst *rmInst, Rm_ResourceInfo *resourceInfo)
143 {
144 Rm_NameServerNode findNode;
145 Rm_NameServerNode *matchingNode = NULL;
147 /* Copy the name to find into the find node structure */
148 strcpy(findNode.name, resourceInfo->nsName);
150 matchingNode = RB_FIND(_Rm_NameServerTree, rmInst->nameServer, &findNode);
152 if (matchingNode)
153 {
154 /* Copy the name's resource information */
155 resourceInfo->base = matchingNode->base;
156 resourceInfo->length = matchingNode->length;
157 }
158 else
159 {
160 return(RM_NS_ERROR_NAME_DOES_NOT_EXIST);
161 }
163 /* STUB APPROVED FOR NOW */
164 return(RM_NS_ACTION_APPROVED);
165 }
167 int32_t Rm_nsDeleteObject(Rm_Inst *rmInst, Rm_ResourceInfo *resourceInfo)
168 {
169 Rm_NameServerNode findNode;
170 Rm_NameServerNode *matchingNode = NULL;
172 /* Copy the name to find into the find node structure */
173 strcpy(findNode.name, resourceInfo->nsName);
175 matchingNode = RB_FIND(_Rm_NameServerTree, rmInst->nameServer, &findNode);
177 if (matchingNode)
178 {
179 /* Remove the name from the NameServer */
180 RB_REMOVE(_Rm_NameServerTree, rmInst->nameServer, matchingNode);
181 Rm_freeNameServerNode(matchingNode);
182 }
183 else
184 {
185 return(RM_NS_ERROR_NAME_DOES_NOT_EXIST);
186 }
188 /* STUB APPROVED FOR NOW */
189 return(RM_NS_ACTION_APPROVED);
190 }