lib: remove unused libnvfru
[unleashed.git] / usr / src / lib / libfru / libfru / Ancestor.cc
blob25032bb14d80efb95d17bcc70382558ad17d16f7
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <stdlib.h>
30 #include <string.h>
32 #include "Ancestor.h"
34 /* ========================================================================= */
35 /* Ancestor object definitions. */
37 Ancestor::Ancestor(Str field, fru_tag_t t, const fru_regdef_t *d)
38 : field_name(field),
39 tag(t),
40 def(d),
41 numInstances(0),
42 numBufs(1),
43 next(NULL)
45 offsets = (uint32_t *)malloc(sizeof (uint32_t)
46 * ANCESTOR_INST_BUF_SIZE);
47 paths = (char **)malloc(sizeof (char *)
48 * ANCESTOR_INST_BUF_SIZE);
51 Ancestor::~Ancestor()
53 free(offsets);
54 if (paths != NULL) {
55 for (int i = 0; i < numInstances; i++) {
56 free(paths[i]);
59 free(paths);
60 delete next;
64 void
65 Ancestor::print(void)
67 fprintf(stderr, "Ancestor Information\n");
68 fprintf(stderr, "Tag Name: %s\n", def->name);
69 fprintf(stderr, "Tag Type: %s\n",
70 get_tagtype_str(get_tag_type(&tag)));
71 fprintf(stderr, "Num instances: %d\n", numInstances);
72 fprintf(stderr, " offsets:\n");
73 for (int i = 0; i < numInstances; i++) {
74 fprintf(stderr, " %d\n", offsets[i]);
77 if (next != NULL) {
78 next->print();
83 void
84 Ancestor::addInstance(const char *path, uint32_t offset)
86 if (numInstances >= ANCESTOR_INST_BUF_SIZE) {
87 numBufs++;
88 offsets = (uint32_t *)realloc(offsets,
89 (sizeof (uint32_t) *
90 (ANCESTOR_INST_BUF_SIZE * numBufs)));
91 paths = (char **)realloc(offsets,
92 (sizeof (char *) *
93 (ANCESTOR_INST_BUF_SIZE * numBufs)));
95 offsets[numInstances] = offset;
96 paths[numInstances++] = strdup(path);
99 Str
100 Ancestor::getFieldName(void)
102 return (field_name);
105 fru_tag_t
106 Ancestor::getTag(void)
108 return (tag);
111 const fru_regdef_t *
112 Ancestor::getDef(void)
114 return (def);
118 Ancestor::getNumInstances(void)
120 return (numInstances);
123 uint32_t
124 Ancestor::getInstOffset(int num)
126 if (num < numInstances)
127 return (offsets[num]);
128 else
129 return (offsets[numInstances]);
132 const char *
133 Ancestor::getPath(int num)
135 if (num < numInstances)
136 return (paths[num]);
137 else
138 return (paths[numInstances]);
142 Ancestor *
143 Ancestor::listTaggedAncestors(char *element)
145 Ancestor *rc = NULL;
146 fru_regdef_t *def = NULL;
147 int i = 0;
149 unsigned int number = 0;
150 char **data_elems = fru_reg_list_entries(&number);
152 if (data_elems == NULL) {
153 return (NULL);
156 // look through all the elements.
157 for (i = 0; i < number; i++) {
158 def = (fru_regdef_t *)
159 fru_reg_lookup_def_by_name(data_elems[i]);
160 Ancestor *ant = createTaggedAncestor(def, element);
161 if (ant != NULL) {
162 if (rc == NULL) {
163 rc = ant;
164 } else {
165 Ancestor *tmp = rc;
166 while (tmp->next != NULL) {
167 tmp = tmp->next;
169 tmp->next = ant;
174 for (i = 0; i < number; i++) {
175 free(data_elems[i]);
177 free(data_elems);
179 return (rc);
182 Ancestor *
183 Ancestor::createTaggedAncestor(const fru_regdef_t *def, Str element)
185 // ancestors have to be tagged.
186 if (def->tagType == FRU_X)
187 return (NULL);
189 fru_tag_t tag;
190 mk_tag(def->tagType, def->tagDense, def->payloadLen, &tag);
191 Ancestor *rc = new Ancestor(element, tag, def);
193 if (element.compare(def->name) == 0) {
194 rc->addInstance("", 0);
195 return (rc);
198 int found = 0;
199 if (def->dataType == FDTYPE_Record) {
200 uint32_t offset = 0;
201 for (int i = 0; i < def->enumCount; i++) {
202 const fru_regdef_t *tmp
203 = fru_reg_lookup_def_by_name
204 ((char *)def->enumTable[i].text);
205 Str path = "/";
206 path << def->name;
207 int f = definitionContains(tmp, def, element,
208 offset, rc, path);
209 if (f == 1) found = 1; // found needs to latch at one.
210 offset += tmp->payloadLen;
214 if (!found) {
215 delete rc;
216 return (NULL);
219 return (rc);
223 Ancestor::definitionContains(const fru_regdef_t *def,
224 const fru_regdef_t *parent_def,
225 Str element,
226 uint32_t offset,
227 Ancestor *ant,
228 Str path)
230 if (element.compare(def->name) == 0) {
231 if (parent_def->iterationType != FRU_NOT_ITERATED) {
232 offset += 4;
233 for (int i = 0; i < parent_def->iterationCount; i++) {
234 Str tmp = path;
235 tmp << "[" << i << "]/";
236 ant->addInstance(tmp.peak(), offset);
237 offset += (parent_def->payloadLen - 4) /
238 parent_def->iterationCount;
240 } else {
241 path << "/";
242 ant->addInstance(path.peak(), offset);
244 return (1);
247 int found = 0;
248 if (def->dataType == FDTYPE_Record) {
249 for (int i = 0; i < def->enumCount; i++) {
250 const fru_regdef_t *tmp
251 = fru_reg_lookup_def_by_name
252 ((char *)def->enumTable[i].text);
253 Str newPath = path;
254 newPath << "/" << def->name;
255 int f = definitionContains(tmp, def, element,
256 offset, ant, newPath);
257 if (f == 1) found = 1; // found needs to latch at one.
258 offset += tmp->payloadLen;
262 return (found);