1 /*********************************************************************
3 * Filename: irias_object.c
5 * Description: IAS object database and functions
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Thu Oct 1 22:50:04 1998
9 * Modified at: Sat Oct 9 17:11:16 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
12 * Copyright (c) 1998-1999 Dag Brattli, All Rights Reserved.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * Neither Dag Brattli nor University of Tromsø admit liability nor
20 * provide warranty for any of this software. This material is
21 * provided "AS-IS" and at no charge.
23 ********************************************************************/
25 #include <linux/string.h>
26 #include <linux/socket.h>
28 #include <net/irda/irda.h>
29 #include <net/irda/irmod.h>
30 #include <net/irda/irias_object.h>
32 hashbin_t
*objects
= NULL
;
35 * Used when a missing value needs to be returned
37 struct ias_value missing
= { IAS_MISSING
, 0, 0, {0}};
40 * Function strdup (str)
42 * My own kernel version of strdup!
45 char *strdup(char *str
)
52 ASSERT(strlen( str
) < 64, return NULL
;);
54 new_str
= kmalloc(strlen(str
)+1, GFP_ATOMIC
);
64 * Function ias_new_object (name, id)
66 * Create a new IAS object
69 struct ias_object
*irias_new_object( char *name
, int id
)
71 struct ias_object
*obj
;
73 IRDA_DEBUG( 4, __FUNCTION__
"()\n");
75 obj
= (struct ias_object
*) kmalloc( sizeof( struct ias_object
),
78 IRDA_DEBUG(0, __FUNCTION__
"(), Unable to allocate object!\n");
81 memset(obj
, 0, sizeof( struct ias_object
));
83 obj
->magic
= IAS_OBJECT_MAGIC
;
84 obj
->name
= strdup( name
);
87 obj
->attribs
= hashbin_new(HB_LOCAL
);
93 * Function irias_delete_attrib (attrib)
95 * Delete given attribute and deallocate all its memory
98 void __irias_delete_attrib(struct ias_attrib
*attrib
)
100 ASSERT(attrib
!= NULL
, return;);
101 ASSERT(attrib
->magic
== IAS_ATTRIB_MAGIC
, return;);
106 irias_delete_value(attrib
->value
);
107 attrib
->magic
= ~IAS_ATTRIB_MAGIC
;
112 void __irias_delete_object(struct ias_object
*obj
)
114 ASSERT(obj
!= NULL
, return;);
115 ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
120 hashbin_delete(obj
->attribs
, (FREE_FUNC
) __irias_delete_attrib
);
122 obj
->magic
= ~IAS_OBJECT_MAGIC
;
128 * Function irias_delete_object (obj)
130 * Remove object from hashbin and deallocate all attributes assosiated with
131 * with this object and the object itself
134 int irias_delete_object(struct ias_object
*obj
)
136 struct ias_object
*node
;
138 ASSERT(obj
!= NULL
, return -1;);
139 ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return -1;);
141 node
= hashbin_remove(objects
, 0, obj
->name
);
143 return 0; /* Already removed */
145 __irias_delete_object(node
);
151 * Function irias_insert_object (obj)
153 * Insert an object into the LM-IAS database
156 void irias_insert_object(struct ias_object
*obj
)
158 ASSERT(obj
!= NULL
, return;);
159 ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
161 hashbin_insert(objects
, (queue_t
*) obj
, 0, obj
->name
);
165 * Function irias_find_object (name)
167 * Find object with given name
170 struct ias_object
*irias_find_object(char *name
)
172 ASSERT(name
!= NULL
, return NULL
;);
174 return hashbin_find(objects
, 0, name
);
178 * Function irias_find_attrib (obj, name)
180 * Find named attribute in object
183 struct ias_attrib
*irias_find_attrib(struct ias_object
*obj
, char *name
)
185 struct ias_attrib
*attrib
;
187 ASSERT(obj
!= NULL
, return NULL
;);
188 ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return NULL
;);
189 ASSERT(name
!= NULL
, return NULL
;);
191 attrib
= hashbin_find(obj
->attribs
, 0, name
);
199 * Function irias_add_attribute (obj, attrib)
201 * Add attribute to object
204 void irias_add_attrib( struct ias_object
*obj
, struct ias_attrib
*attrib
)
206 ASSERT(obj
!= NULL
, return;);
207 ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
209 ASSERT(attrib
!= NULL
, return;);
210 ASSERT(attrib
->magic
== IAS_ATTRIB_MAGIC
, return;);
212 hashbin_insert(obj
->attribs
, (queue_t
*) attrib
, 0, attrib
->name
);
216 * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
218 * Change the value of an objects attribute.
221 int irias_object_change_attribute(char *obj_name
, char *attrib_name
,
222 struct ias_value
*new_value
)
224 struct ias_object
*obj
;
225 struct ias_attrib
*attrib
;
228 obj
= hashbin_find(objects
, 0, obj_name
);
230 WARNING(__FUNCTION__
"(), Unable to find object: %s\n",
236 attrib
= hashbin_find(obj
->attribs
, 0, attrib_name
);
237 if (attrib
== NULL
) {
238 WARNING(__FUNCTION__
"(), Unable to find attribute: %s\n",
243 if ( attrib
->value
->type
!= new_value
->type
) {
244 IRDA_DEBUG( 0, __FUNCTION__
245 "(), changing value type not allowed!\n");
249 /* Delete old value */
250 irias_delete_value(attrib
->value
);
252 /* Insert new value */
253 attrib
->value
= new_value
;
260 * Function irias_object_add_integer_attrib (obj, name, value)
262 * Add an integer attribute to an LM-IAS object
265 void irias_add_integer_attrib(struct ias_object
*obj
, char *name
, int value
)
267 struct ias_attrib
*attrib
;
269 ASSERT(obj
!= NULL
, return;);
270 ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
271 ASSERT(name
!= NULL
, return;);
273 attrib
= (struct ias_attrib
*) kmalloc(sizeof(struct ias_attrib
),
275 if (attrib
== NULL
) {
276 WARNING(__FUNCTION__
"(), Unable to allocate attribute!\n");
279 memset(attrib
, 0, sizeof( struct ias_attrib
));
281 attrib
->magic
= IAS_ATTRIB_MAGIC
;
282 attrib
->name
= strdup(name
);
285 attrib
->value
= irias_new_integer_value(value
);
287 irias_add_attrib(obj
, attrib
);
291 * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
293 * Add a octet sequence attribute to an LM-IAS object
297 void irias_add_octseq_attrib(struct ias_object
*obj
, char *name
, __u8
*octets
,
300 struct ias_attrib
*attrib
;
302 ASSERT(obj
!= NULL
, return;);
303 ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
305 ASSERT(name
!= NULL
, return;);
306 ASSERT(octets
!= NULL
, return;);
308 attrib
= (struct ias_attrib
*) kmalloc(sizeof(struct ias_attrib
),
310 if (attrib
== NULL
) {
312 "(), Unable to allocate attribute!\n");
315 memset(attrib
, 0, sizeof( struct ias_attrib
));
317 attrib
->magic
= IAS_ATTRIB_MAGIC
;
318 attrib
->name
= strdup( name
);
320 attrib
->value
= irias_new_octseq_value( octets
, len
);
322 irias_add_attrib(obj
, attrib
);
326 * Function irias_object_add_string_attrib (obj, string)
328 * Add a string attribute to an LM-IAS object
331 void irias_add_string_attrib(struct ias_object
*obj
, char *name
, char *value
)
333 struct ias_attrib
*attrib
;
335 ASSERT(obj
!= NULL
, return;);
336 ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
338 ASSERT(name
!= NULL
, return;);
339 ASSERT(value
!= NULL
, return;);
341 attrib
= (struct ias_attrib
*) kmalloc(sizeof( struct ias_attrib
),
343 if (attrib
== NULL
) {
344 WARNING(__FUNCTION__
"(), Unable to allocate attribute!\n");
347 memset(attrib
, 0, sizeof( struct ias_attrib
));
349 attrib
->magic
= IAS_ATTRIB_MAGIC
;
350 attrib
->name
= strdup(name
);
352 attrib
->value
= irias_new_string_value(value
);
354 irias_add_attrib(obj
, attrib
);
358 * Function irias_new_integer_value (integer)
360 * Create new IAS integer value
363 struct ias_value
*irias_new_integer_value(int integer
)
365 struct ias_value
*value
;
367 value
= kmalloc(sizeof( struct ias_value
), GFP_ATOMIC
);
369 WARNING(__FUNCTION__
"(), Unable to kmalloc!\n");
372 memset(value
, 0, sizeof(struct ias_value
));
374 value
->type
= IAS_INTEGER
;
376 value
->t
.integer
= integer
;
382 * Function irias_new_string_value (string)
384 * Create new IAS string value
387 struct ias_value
*irias_new_string_value(char *string
)
389 struct ias_value
*value
;
391 value
= kmalloc(sizeof(struct ias_value
), GFP_ATOMIC
);
393 WARNING(__FUNCTION__
"(), Unable to kmalloc!\n");
396 memset( value
, 0, sizeof( struct ias_value
));
398 value
->type
= IAS_STRING
;
399 value
->charset
= CS_ASCII
;
400 value
->len
= strlen(string
);
401 value
->t
.string
= strdup(string
);
408 * Function irias_new_octseq_value (octets, len)
410 * Create new IAS octet-sequence value
413 struct ias_value
*irias_new_octseq_value(__u8
*octseq
, int len
)
415 struct ias_value
*value
;
417 value
= kmalloc(sizeof(struct ias_value
), GFP_ATOMIC
);
419 WARNING(__FUNCTION__
"(), Unable to kmalloc!\n");
422 memset(value
, 0, sizeof( struct ias_value
));
424 value
->type
= IAS_OCT_SEQ
;
427 value
->t
.oct_seq
= kmalloc(len
, GFP_ATOMIC
);
428 if (value
->t
.oct_seq
== NULL
){
429 WARNING(__FUNCTION__
"(), Unable to kmalloc!\n");
432 memcpy(value
->t
.oct_seq
, octseq
, len
);
437 * Function irias_delete_value (value)
442 void irias_delete_value(struct ias_value
*value
)
444 IRDA_DEBUG(4, __FUNCTION__
"()\n");
446 ASSERT(value
!= NULL
, return;);
448 switch (value
->type
) {
449 case IAS_INTEGER
: /* Fallthrough */
451 /* No need to deallocate */
454 /* If string, deallocate string */
455 if (value
->t
.string
!= NULL
)
456 kfree(value
->t
.string
);
459 /* If byte stream, deallocate byte stream */
460 if (value
->t
.oct_seq
!= NULL
)
461 kfree(value
->t
.oct_seq
);
464 IRDA_DEBUG(0, __FUNCTION__
"(), Unknown value type!\n");