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: Mon Jun 21 16:11:13 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>
27 #include <linux/irda.h>
29 #include <net/irda/irda.h>
30 #include <net/irda/irmod.h>
31 #include <net/irda/irias_object.h>
33 hashbin_t
*objects
= NULL
;
36 * Used when a missing value needs to be returned
38 struct ias_value missing
= { IAS_MISSING
, 0, 0, {0}};
41 * Function strdup (str)
43 * My own kernel version of strdup!
46 char *strdup(char *str
)
53 ASSERT(strlen( str
) < 64, return NULL
;);
55 new_str
= kmalloc(strlen(str
)+1, GFP_ATOMIC
);
65 * Function ias_new_object (name, id)
67 * Create a new IAS object
70 struct ias_object
*irias_new_object( char *name
, int id
)
72 struct ias_object
*obj
;
74 DEBUG( 4, __FUNCTION__
"()\n");
76 obj
= (struct ias_object
*) kmalloc( sizeof( struct ias_object
),
79 DEBUG(0, __FUNCTION__
"(), Unable to allocate object!\n");
82 memset(obj
, 0, sizeof( struct ias_object
));
84 obj
->magic
= IAS_OBJECT_MAGIC
;
85 obj
->name
= strdup( name
);
88 obj
->attribs
= hashbin_new(HB_LOCAL
);
94 * Function irias_delete_attrib (attrib)
96 * Delete given attribute and deallocate all its memory
99 void __irias_delete_attrib(struct ias_attrib
*attrib
)
101 ASSERT(attrib
!= NULL
, return;);
102 ASSERT(attrib
->magic
== IAS_ATTRIB_MAGIC
, return;);
107 irias_delete_value(attrib
->value
);
108 attrib
->magic
= ~IAS_ATTRIB_MAGIC
;
113 void __irias_delete_object(struct ias_object
*obj
)
115 ASSERT(obj
!= NULL
, return;);
116 ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
121 hashbin_delete(obj
->attribs
, (FREE_FUNC
) __irias_delete_attrib
);
123 obj
->magic
= ~IAS_OBJECT_MAGIC
;
129 * Function irias_delete_object (obj)
131 * Remove object from hashbin and deallocate all attributes assosiated with
132 * with this object and the object itself
135 int irias_delete_object(struct ias_object
*obj
)
137 struct ias_object
*node
;
139 ASSERT(obj
!= NULL
, return -1;);
140 ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return -1;);
142 node
= hashbin_remove(objects
, 0, obj
->name
);
144 return 0; /* Already removed */
146 __irias_delete_object(node
);
152 * Function irias_insert_object (obj)
154 * Insert an object into the LM-IAS database
157 void irias_insert_object(struct ias_object
*obj
)
159 DEBUG(4, __FUNCTION__
"()\n");
161 ASSERT(obj
!= NULL
, return;);
162 ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
164 hashbin_insert(objects
, (QUEUE
*) obj
, 0, obj
->name
);
168 * Function irias_find_object (name)
170 * Find object with given name
173 struct ias_object
*irias_find_object(char *name
)
175 ASSERT(name
!= NULL
, return NULL
;);
177 return hashbin_find(objects
, 0, name
);
181 * Function irias_find_attrib (obj, name)
183 * Find named attribute in object
186 struct ias_attrib
*irias_find_attrib( struct ias_object
*obj
, char *name
)
188 struct ias_attrib
*attrib
;
190 DEBUG( 4, __FUNCTION__
"()\n");
192 ASSERT( obj
!= NULL
, return NULL
;);
193 ASSERT( obj
->magic
== IAS_OBJECT_MAGIC
, return NULL
;);
194 ASSERT( name
!= NULL
, return NULL
;);
196 attrib
= hashbin_find( obj
->attribs
, 0, name
);
204 * Function irias_add_attribute (obj, attrib)
206 * Add attribute to object
209 void irias_add_attrib( struct ias_object
*obj
, struct ias_attrib
*attrib
)
211 DEBUG( 4, __FUNCTION__
"()\n");
213 ASSERT( obj
!= NULL
, return;);
214 ASSERT( obj
->magic
== IAS_OBJECT_MAGIC
, return;);
216 ASSERT( attrib
!= NULL
, return;);
217 ASSERT( attrib
->magic
== IAS_ATTRIB_MAGIC
, return;);
219 hashbin_insert( obj
->attribs
, (QUEUE
*) attrib
, 0, attrib
->name
);
223 * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
225 * Change the value of an objects attribute.
228 int irias_object_change_attribute( char *obj_name
, char *attrib_name
,
229 struct ias_value
*new_value
)
231 struct ias_object
*obj
;
232 struct ias_attrib
*attrib
;
235 obj
= hashbin_find( objects
, 0, obj_name
);
237 DEBUG( 0, __FUNCTION__
"(), Unable to find object: %s\n",
243 attrib
= hashbin_find( obj
->attribs
, 0, attrib_name
);
244 if ( attrib
== NULL
) {
245 DEBUG( 0, __FUNCTION__
"(), Unable to find attribute: %s\n",
250 if ( attrib
->value
->type
!= new_value
->type
) {
251 DEBUG( 0, __FUNCTION__
252 "(), changing value type not allowed!\n");
256 /* Delete old value */
257 irias_delete_value( attrib
->value
);
259 /* Insert new value */
260 attrib
->value
= new_value
;
267 * Function irias_object_add_integer_attrib (obj, name, value)
269 * Add an integer attribute to an LM-IAS object
272 void irias_add_integer_attrib( struct ias_object
*obj
, char *name
, int value
)
274 struct ias_attrib
*attrib
;
276 ASSERT( obj
!= NULL
, return;);
277 ASSERT( obj
->magic
== IAS_OBJECT_MAGIC
, return;);
278 ASSERT( name
!= NULL
, return;);
280 attrib
= (struct ias_attrib
*) kmalloc( sizeof( struct ias_attrib
),
282 if ( attrib
== NULL
) {
283 DEBUG( 0, __FUNCTION__
284 "(), Unable to allocate attribute!\n");
287 memset( attrib
, 0, sizeof( struct ias_attrib
));
289 attrib
->magic
= IAS_ATTRIB_MAGIC
;
290 attrib
->name
= strdup( name
);
291 /* attrib->attr = NULL; */
294 attrib
->value
= irias_new_integer_value( value
);
296 irias_add_attrib( obj
, attrib
);
300 * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
302 * Add a octet sequence attribute to an LM-IAS object
306 void irias_add_octseq_attrib( struct ias_object
*obj
, char *name
,
307 __u8
*octets
, int len
)
309 struct ias_attrib
*attrib
;
311 ASSERT( obj
!= NULL
, return;);
312 ASSERT( obj
->magic
== IAS_OBJECT_MAGIC
, return;);
314 ASSERT( name
!= NULL
, return;);
315 ASSERT( octets
!= NULL
, return;);
316 ASSERT( len
< 55, return;); /* FIXME: must be 1024, but... */
318 attrib
= (struct ias_attrib
*) kmalloc( sizeof( struct ias_attrib
),
320 if ( attrib
== NULL
) {
321 DEBUG( 0, __FUNCTION__
322 "(), Unable to allocate attribute!\n");
325 memset( attrib
, 0, sizeof( struct ias_attrib
));
327 attrib
->magic
= IAS_ATTRIB_MAGIC
;
328 attrib
->name
= strdup( name
);
330 attrib
->value
= irias_new_octseq_value( octets
, len
);
332 irias_add_attrib( obj
, attrib
);
336 * Function irias_object_add_string_attrib (obj, string)
338 * Add a string attribute to an LM-IAS object
341 void irias_add_string_attrib( struct ias_object
*obj
, char *name
, char *value
)
343 struct ias_attrib
*attrib
;
345 ASSERT( obj
!= NULL
, return;);
346 ASSERT( obj
->magic
== IAS_OBJECT_MAGIC
, return;);
348 ASSERT( name
!= NULL
, return;);
349 ASSERT( value
!= NULL
, return;);
351 attrib
= (struct ias_attrib
*) kmalloc( sizeof( struct ias_attrib
),
353 if ( attrib
== NULL
) {
354 DEBUG( 0, __FUNCTION__
355 "(), Unable to allocate attribute!\n");
358 memset( attrib
, 0, sizeof( struct ias_attrib
));
360 attrib
->magic
= IAS_ATTRIB_MAGIC
;
361 attrib
->name
= strdup( name
);
363 attrib
->value
= irias_new_string_value( value
);
365 irias_add_attrib( obj
, attrib
);
369 * Function irias_new_integer_value (integer)
371 * Create new IAS integer value
374 struct ias_value
*irias_new_integer_value( int integer
)
376 struct ias_value
*value
;
378 value
= kmalloc( sizeof( struct ias_value
), GFP_ATOMIC
);
379 if ( value
== NULL
) {
380 DEBUG( 0, __FUNCTION__
"(), Unable to kmalloc!\n");
383 memset( value
, 0, sizeof( struct ias_value
));
385 value
->type
= IAS_INTEGER
;
387 value
->t
.integer
= integer
;
393 * Function irias_new_string_value (string)
395 * Create new IAS string value
398 struct ias_value
*irias_new_string_value( char *string
)
400 struct ias_value
*value
;
402 value
= kmalloc( sizeof( struct ias_value
), GFP_ATOMIC
);
403 if ( value
== NULL
) {
404 DEBUG( 0, __FUNCTION__
"(), Unable to kmalloc!\n");
407 memset( value
, 0, sizeof( struct ias_value
));
409 value
->type
= IAS_STRING
;
410 value
->charset
= CS_ASCII
;
411 value
->len
= strlen( string
);
412 value
->t
.string
= strdup( string
);
419 * Function irias_new_octseq_value (octets, len)
421 * Create new IAS octet-sequence value
424 struct ias_value
*irias_new_octseq_value( __u8
*octseq
, int len
)
426 struct ias_value
*value
;
428 ASSERT(len
<= 55, return NULL
;); /*FIXME: must be 1024, but.....*/
430 value
= kmalloc( sizeof( struct ias_value
), GFP_ATOMIC
);
431 if ( value
== NULL
) {
432 DEBUG( 0, __FUNCTION__
"(), Unable to kmalloc!\n");
435 memset( value
, 0, sizeof( struct ias_value
));
437 value
->type
= IAS_OCT_SEQ
;
440 value
->t
.oct_seq
= kmalloc(len
, GFP_ATOMIC
);
441 if( value
->t
.oct_seq
== NULL
){
442 DEBUG(0, __FUNCTION__
"(), Unable to kmalloc!\n");
445 memcpy(value
->t
.oct_seq
, octseq
, len
);
450 * Function irias_delete_value (value)
455 void irias_delete_value( struct ias_value
*value
)
457 DEBUG( 4, __FUNCTION__
"()\n");
459 ASSERT( value
!= NULL
, return;);
461 switch( value
->type
) {
462 case IAS_INTEGER
: /* Fallthrough */
464 /* No need to deallocate */
467 /* If string, deallocate string */
468 if ( value
->t
.string
!= NULL
)
469 kfree( value
->t
.string
);
472 /* If byte stream, deallocate byte stream */
473 if ( value
->t
.oct_seq
!= NULL
)
474 kfree(value
->t
.oct_seq
);
477 DEBUG( 0, __FUNCTION__
"(), Unknown value type!\n");