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: Wed Dec 15 11:23: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>
27 #include <linux/module.h>
29 #include <net/irda/irda.h>
30 #include <net/irda/irias_object.h>
32 hashbin_t
*irias_objects
;
35 * Used when a missing value needs to be returned
37 struct ias_value irias_missing
= { IAS_MISSING
, 0, 0, 0, {0}};
40 * Function strndup (str, max)
42 * My own kernel version of strndup!
44 * Faster, check boundary... Jean II
46 static char *strndup(char *str
, int max
)
54 /* Check length, truncate */
59 /* Allocate new string */
60 new_str
= kmalloc(len
+ 1, GFP_ATOMIC
);
61 if (new_str
== NULL
) {
62 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__
);
66 /* Copy and truncate */
67 memcpy(new_str
, str
, len
);
74 * Function ias_new_object (name, id)
76 * Create a new IAS object
79 struct ias_object
*irias_new_object( char *name
, int id
)
81 struct ias_object
*obj
;
83 IRDA_DEBUG( 4, "%s()\n", __FUNCTION__
);
85 obj
= kmalloc(sizeof(struct ias_object
), GFP_ATOMIC
);
87 IRDA_WARNING("%s(), Unable to allocate object!\n",
91 memset(obj
, 0, sizeof( struct ias_object
));
93 obj
->magic
= IAS_OBJECT_MAGIC
;
94 obj
->name
= strndup(name
, IAS_MAX_CLASSNAME
);
97 /* Locking notes : the attrib spinlock has lower precendence
98 * than the objects spinlock. Never grap the objects spinlock
99 * while holding any attrib spinlock (risk of deadlock). Jean II */
100 obj
->attribs
= hashbin_new(HB_LOCK
);
102 if (obj
->attribs
== NULL
) {
103 IRDA_WARNING("%s(), Unable to allocate attribs!\n",
111 EXPORT_SYMBOL(irias_new_object
);
114 * Function irias_delete_attrib (attrib)
116 * Delete given attribute and deallocate all its memory
119 static void __irias_delete_attrib(struct ias_attrib
*attrib
)
121 IRDA_ASSERT(attrib
!= NULL
, return;);
122 IRDA_ASSERT(attrib
->magic
== IAS_ATTRIB_MAGIC
, return;);
126 irias_delete_value(attrib
->value
);
127 attrib
->magic
= ~IAS_ATTRIB_MAGIC
;
132 void __irias_delete_object(struct ias_object
*obj
)
134 IRDA_ASSERT(obj
!= NULL
, return;);
135 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
139 hashbin_delete(obj
->attribs
, (FREE_FUNC
) __irias_delete_attrib
);
141 obj
->magic
= ~IAS_OBJECT_MAGIC
;
147 * Function irias_delete_object (obj)
149 * Remove object from hashbin and deallocate all attributes associated with
150 * with this object and the object itself
153 int irias_delete_object(struct ias_object
*obj
)
155 struct ias_object
*node
;
157 IRDA_ASSERT(obj
!= NULL
, return -1;);
158 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return -1;);
160 /* Remove from list */
161 node
= hashbin_remove_this(irias_objects
, (irda_queue_t
*) obj
);
163 IRDA_DEBUG( 0, "%s(), object already removed!\n",
167 __irias_delete_object(obj
);
171 EXPORT_SYMBOL(irias_delete_object
);
174 * Function irias_delete_attrib (obj)
176 * Remove attribute from hashbin and, if it was the last attribute of
177 * the object, remove the object as well.
180 int irias_delete_attrib(struct ias_object
*obj
, struct ias_attrib
*attrib
,
183 struct ias_attrib
*node
;
185 IRDA_ASSERT(obj
!= NULL
, return -1;);
186 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return -1;);
187 IRDA_ASSERT(attrib
!= NULL
, return -1;);
189 /* Remove attribute from object */
190 node
= hashbin_remove_this(obj
->attribs
, (irda_queue_t
*) attrib
);
192 return 0; /* Already removed or non-existent */
194 /* Deallocate attribute */
195 __irias_delete_attrib(node
);
197 /* Check if object has still some attributes, destroy it if none.
198 * At first glance, this look dangerous, as the kernel reference
199 * various IAS objects. However, we only use this function on
200 * user attributes, not kernel attributes, so there is no risk
201 * of deleting a kernel object this way. Jean II */
202 node
= (struct ias_attrib
*) hashbin_get_first(obj
->attribs
);
203 if (cleanobject
&& !node
)
204 irias_delete_object(obj
);
210 * Function irias_insert_object (obj)
212 * Insert an object into the LM-IAS database
215 void irias_insert_object(struct ias_object
*obj
)
217 IRDA_ASSERT(obj
!= NULL
, return;);
218 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
220 hashbin_insert(irias_objects
, (irda_queue_t
*) obj
, 0, obj
->name
);
222 EXPORT_SYMBOL(irias_insert_object
);
225 * Function irias_find_object (name)
227 * Find object with given name
230 struct ias_object
*irias_find_object(char *name
)
232 IRDA_ASSERT(name
!= NULL
, return NULL
;);
234 /* Unsafe (locking), object might change */
235 return hashbin_lock_find(irias_objects
, 0, name
);
237 EXPORT_SYMBOL(irias_find_object
);
240 * Function irias_find_attrib (obj, name)
242 * Find named attribute in object
245 struct ias_attrib
*irias_find_attrib(struct ias_object
*obj
, char *name
)
247 struct ias_attrib
*attrib
;
249 IRDA_ASSERT(obj
!= NULL
, return NULL
;);
250 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return NULL
;);
251 IRDA_ASSERT(name
!= NULL
, return NULL
;);
253 attrib
= hashbin_lock_find(obj
->attribs
, 0, name
);
257 /* Unsafe (locking), attrib might change */
262 * Function irias_add_attribute (obj, attrib)
264 * Add attribute to object
267 static void irias_add_attrib(struct ias_object
*obj
, struct ias_attrib
*attrib
,
270 IRDA_ASSERT(obj
!= NULL
, return;);
271 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
273 IRDA_ASSERT(attrib
!= NULL
, return;);
274 IRDA_ASSERT(attrib
->magic
== IAS_ATTRIB_MAGIC
, return;);
276 /* Set if attrib is owned by kernel or user space */
277 attrib
->value
->owner
= owner
;
279 hashbin_insert(obj
->attribs
, (irda_queue_t
*) attrib
, 0, attrib
->name
);
283 * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
285 * Change the value of an objects attribute.
288 int irias_object_change_attribute(char *obj_name
, char *attrib_name
,
289 struct ias_value
*new_value
)
291 struct ias_object
*obj
;
292 struct ias_attrib
*attrib
;
296 obj
= hashbin_lock_find(irias_objects
, 0, obj_name
);
298 IRDA_WARNING("%s: Unable to find object: %s\n", __FUNCTION__
,
303 /* Slightly unsafe (obj might get removed under us) */
304 spin_lock_irqsave(&obj
->attribs
->hb_spinlock
, flags
);
307 attrib
= hashbin_find(obj
->attribs
, 0, attrib_name
);
308 if (attrib
== NULL
) {
309 IRDA_WARNING("%s: Unable to find attribute: %s\n",
310 __FUNCTION__
, attrib_name
);
311 spin_unlock_irqrestore(&obj
->attribs
->hb_spinlock
, flags
);
315 if ( attrib
->value
->type
!= new_value
->type
) {
316 IRDA_DEBUG( 0, "%s(), changing value type not allowed!\n",
318 spin_unlock_irqrestore(&obj
->attribs
->hb_spinlock
, flags
);
322 /* Delete old value */
323 irias_delete_value(attrib
->value
);
325 /* Insert new value */
326 attrib
->value
= new_value
;
329 spin_unlock_irqrestore(&obj
->attribs
->hb_spinlock
, flags
);
332 EXPORT_SYMBOL(irias_object_change_attribute
);
335 * Function irias_object_add_integer_attrib (obj, name, value)
337 * Add an integer attribute to an LM-IAS object
340 void irias_add_integer_attrib(struct ias_object
*obj
, char *name
, int value
,
343 struct ias_attrib
*attrib
;
345 IRDA_ASSERT(obj
!= NULL
, return;);
346 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
347 IRDA_ASSERT(name
!= NULL
, return;);
349 attrib
= kmalloc(sizeof(struct ias_attrib
), GFP_ATOMIC
);
350 if (attrib
== NULL
) {
351 IRDA_WARNING("%s: Unable to allocate attribute!\n",
355 memset(attrib
, 0, sizeof( struct ias_attrib
));
357 attrib
->magic
= IAS_ATTRIB_MAGIC
;
358 attrib
->name
= strndup(name
, IAS_MAX_ATTRIBNAME
);
361 attrib
->value
= irias_new_integer_value(value
);
363 irias_add_attrib(obj
, attrib
, owner
);
365 EXPORT_SYMBOL(irias_add_integer_attrib
);
368 * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
370 * Add a octet sequence attribute to an LM-IAS object
374 void irias_add_octseq_attrib(struct ias_object
*obj
, char *name
, __u8
*octets
,
377 struct ias_attrib
*attrib
;
379 IRDA_ASSERT(obj
!= NULL
, return;);
380 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
382 IRDA_ASSERT(name
!= NULL
, return;);
383 IRDA_ASSERT(octets
!= NULL
, return;);
385 attrib
= kmalloc(sizeof(struct ias_attrib
), GFP_ATOMIC
);
386 if (attrib
== NULL
) {
387 IRDA_WARNING("%s: Unable to allocate attribute!\n",
391 memset(attrib
, 0, sizeof( struct ias_attrib
));
393 attrib
->magic
= IAS_ATTRIB_MAGIC
;
394 attrib
->name
= strndup(name
, IAS_MAX_ATTRIBNAME
);
396 attrib
->value
= irias_new_octseq_value( octets
, len
);
398 irias_add_attrib(obj
, attrib
, owner
);
400 EXPORT_SYMBOL(irias_add_octseq_attrib
);
403 * Function irias_object_add_string_attrib (obj, string)
405 * Add a string attribute to an LM-IAS object
408 void irias_add_string_attrib(struct ias_object
*obj
, char *name
, char *value
,
411 struct ias_attrib
*attrib
;
413 IRDA_ASSERT(obj
!= NULL
, return;);
414 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
416 IRDA_ASSERT(name
!= NULL
, return;);
417 IRDA_ASSERT(value
!= NULL
, return;);
419 attrib
= kmalloc(sizeof( struct ias_attrib
), GFP_ATOMIC
);
420 if (attrib
== NULL
) {
421 IRDA_WARNING("%s: Unable to allocate attribute!\n",
425 memset(attrib
, 0, sizeof( struct ias_attrib
));
427 attrib
->magic
= IAS_ATTRIB_MAGIC
;
428 attrib
->name
= strndup(name
, IAS_MAX_ATTRIBNAME
);
430 attrib
->value
= irias_new_string_value(value
);
432 irias_add_attrib(obj
, attrib
, owner
);
434 EXPORT_SYMBOL(irias_add_string_attrib
);
437 * Function irias_new_integer_value (integer)
439 * Create new IAS integer value
442 struct ias_value
*irias_new_integer_value(int integer
)
444 struct ias_value
*value
;
446 value
= kmalloc(sizeof(struct ias_value
), GFP_ATOMIC
);
448 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__
);
451 memset(value
, 0, sizeof(struct ias_value
));
453 value
->type
= IAS_INTEGER
;
455 value
->t
.integer
= integer
;
459 EXPORT_SYMBOL(irias_new_integer_value
);
462 * Function irias_new_string_value (string)
464 * Create new IAS string value
466 * Per IrLMP 1.1, 4.3.3.2, strings are up to 256 chars - Jean II
468 struct ias_value
*irias_new_string_value(char *string
)
470 struct ias_value
*value
;
472 value
= kmalloc(sizeof(struct ias_value
), GFP_ATOMIC
);
474 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__
);
477 memset( value
, 0, sizeof( struct ias_value
));
479 value
->type
= IAS_STRING
;
480 value
->charset
= CS_ASCII
;
481 value
->t
.string
= strndup(string
, IAS_MAX_STRING
);
482 value
->len
= strlen(value
->t
.string
);
488 * Function irias_new_octseq_value (octets, len)
490 * Create new IAS octet-sequence value
492 * Per IrLMP 1.1, 4.3.3.2, octet-sequence are up to 1024 bytes - Jean II
494 struct ias_value
*irias_new_octseq_value(__u8
*octseq
, int len
)
496 struct ias_value
*value
;
498 value
= kmalloc(sizeof(struct ias_value
), GFP_ATOMIC
);
500 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__
);
503 memset(value
, 0, sizeof(struct ias_value
));
505 value
->type
= IAS_OCT_SEQ
;
507 if(len
> IAS_MAX_OCTET_STRING
)
508 len
= IAS_MAX_OCTET_STRING
;
511 value
->t
.oct_seq
= kmalloc(len
, GFP_ATOMIC
);
512 if (value
->t
.oct_seq
== NULL
){
513 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__
);
517 memcpy(value
->t
.oct_seq
, octseq
, len
);
521 struct ias_value
*irias_new_missing_value(void)
523 struct ias_value
*value
;
525 value
= kmalloc(sizeof(struct ias_value
), GFP_ATOMIC
);
527 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__
);
530 memset(value
, 0, sizeof(struct ias_value
));
532 value
->type
= IAS_MISSING
;
539 * Function irias_delete_value (value)
544 void irias_delete_value(struct ias_value
*value
)
546 IRDA_DEBUG(4, "%s()\n", __FUNCTION__
);
548 IRDA_ASSERT(value
!= NULL
, return;);
550 switch (value
->type
) {
551 case IAS_INTEGER
: /* Fallthrough */
553 /* No need to deallocate */
556 /* Deallocate string */
557 kfree(value
->t
.string
);
560 /* Deallocate byte stream */
561 kfree(value
->t
.oct_seq
);
564 IRDA_DEBUG(0, "%s(), Unknown value type!\n", __FUNCTION__
);
569 EXPORT_SYMBOL(irias_delete_value
);