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
, size_t 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
= kzalloc(sizeof(struct ias_object
), GFP_ATOMIC
);
87 IRDA_WARNING("%s(), Unable to allocate object!\n",
92 obj
->magic
= IAS_OBJECT_MAGIC
;
93 obj
->name
= strndup(name
, IAS_MAX_CLASSNAME
);
95 IRDA_WARNING("%s(), Unable to allocate name!\n",
102 /* Locking notes : the attrib spinlock has lower precendence
103 * than the objects spinlock. Never grap the objects spinlock
104 * while holding any attrib spinlock (risk of deadlock). Jean II */
105 obj
->attribs
= hashbin_new(HB_LOCK
);
107 if (obj
->attribs
== NULL
) {
108 IRDA_WARNING("%s(), Unable to allocate attribs!\n",
117 EXPORT_SYMBOL(irias_new_object
);
120 * Function irias_delete_attrib (attrib)
122 * Delete given attribute and deallocate all its memory
125 static void __irias_delete_attrib(struct ias_attrib
*attrib
)
127 IRDA_ASSERT(attrib
!= NULL
, return;);
128 IRDA_ASSERT(attrib
->magic
== IAS_ATTRIB_MAGIC
, return;);
132 irias_delete_value(attrib
->value
);
133 attrib
->magic
= ~IAS_ATTRIB_MAGIC
;
138 void __irias_delete_object(struct ias_object
*obj
)
140 IRDA_ASSERT(obj
!= NULL
, return;);
141 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
145 hashbin_delete(obj
->attribs
, (FREE_FUNC
) __irias_delete_attrib
);
147 obj
->magic
= ~IAS_OBJECT_MAGIC
;
153 * Function irias_delete_object (obj)
155 * Remove object from hashbin and deallocate all attributes associated with
156 * with this object and the object itself
159 int irias_delete_object(struct ias_object
*obj
)
161 struct ias_object
*node
;
163 IRDA_ASSERT(obj
!= NULL
, return -1;);
164 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return -1;);
166 /* Remove from list */
167 node
= hashbin_remove_this(irias_objects
, (irda_queue_t
*) obj
);
169 IRDA_DEBUG( 0, "%s(), object already removed!\n",
173 __irias_delete_object(obj
);
177 EXPORT_SYMBOL(irias_delete_object
);
180 * Function irias_delete_attrib (obj)
182 * Remove attribute from hashbin and, if it was the last attribute of
183 * the object, remove the object as well.
186 int irias_delete_attrib(struct ias_object
*obj
, struct ias_attrib
*attrib
,
189 struct ias_attrib
*node
;
191 IRDA_ASSERT(obj
!= NULL
, return -1;);
192 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return -1;);
193 IRDA_ASSERT(attrib
!= NULL
, return -1;);
195 /* Remove attribute from object */
196 node
= hashbin_remove_this(obj
->attribs
, (irda_queue_t
*) attrib
);
198 return 0; /* Already removed or non-existent */
200 /* Deallocate attribute */
201 __irias_delete_attrib(node
);
203 /* Check if object has still some attributes, destroy it if none.
204 * At first glance, this look dangerous, as the kernel reference
205 * various IAS objects. However, we only use this function on
206 * user attributes, not kernel attributes, so there is no risk
207 * of deleting a kernel object this way. Jean II */
208 node
= (struct ias_attrib
*) hashbin_get_first(obj
->attribs
);
209 if (cleanobject
&& !node
)
210 irias_delete_object(obj
);
216 * Function irias_insert_object (obj)
218 * Insert an object into the LM-IAS database
221 void irias_insert_object(struct ias_object
*obj
)
223 IRDA_ASSERT(obj
!= NULL
, return;);
224 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
226 hashbin_insert(irias_objects
, (irda_queue_t
*) obj
, 0, obj
->name
);
228 EXPORT_SYMBOL(irias_insert_object
);
231 * Function irias_find_object (name)
233 * Find object with given name
236 struct ias_object
*irias_find_object(char *name
)
238 IRDA_ASSERT(name
!= NULL
, return NULL
;);
240 /* Unsafe (locking), object might change */
241 return hashbin_lock_find(irias_objects
, 0, name
);
243 EXPORT_SYMBOL(irias_find_object
);
246 * Function irias_find_attrib (obj, name)
248 * Find named attribute in object
251 struct ias_attrib
*irias_find_attrib(struct ias_object
*obj
, char *name
)
253 struct ias_attrib
*attrib
;
255 IRDA_ASSERT(obj
!= NULL
, return NULL
;);
256 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return NULL
;);
257 IRDA_ASSERT(name
!= NULL
, return NULL
;);
259 attrib
= hashbin_lock_find(obj
->attribs
, 0, name
);
263 /* Unsafe (locking), attrib might change */
268 * Function irias_add_attribute (obj, attrib)
270 * Add attribute to object
273 static void irias_add_attrib(struct ias_object
*obj
, struct ias_attrib
*attrib
,
276 IRDA_ASSERT(obj
!= NULL
, return;);
277 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
279 IRDA_ASSERT(attrib
!= NULL
, return;);
280 IRDA_ASSERT(attrib
->magic
== IAS_ATTRIB_MAGIC
, return;);
282 /* Set if attrib is owned by kernel or user space */
283 attrib
->value
->owner
= owner
;
285 hashbin_insert(obj
->attribs
, (irda_queue_t
*) attrib
, 0, attrib
->name
);
289 * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
291 * Change the value of an objects attribute.
294 int irias_object_change_attribute(char *obj_name
, char *attrib_name
,
295 struct ias_value
*new_value
)
297 struct ias_object
*obj
;
298 struct ias_attrib
*attrib
;
302 obj
= hashbin_lock_find(irias_objects
, 0, obj_name
);
304 IRDA_WARNING("%s: Unable to find object: %s\n", __FUNCTION__
,
309 /* Slightly unsafe (obj might get removed under us) */
310 spin_lock_irqsave(&obj
->attribs
->hb_spinlock
, flags
);
313 attrib
= hashbin_find(obj
->attribs
, 0, attrib_name
);
314 if (attrib
== NULL
) {
315 IRDA_WARNING("%s: Unable to find attribute: %s\n",
316 __FUNCTION__
, attrib_name
);
317 spin_unlock_irqrestore(&obj
->attribs
->hb_spinlock
, flags
);
321 if ( attrib
->value
->type
!= new_value
->type
) {
322 IRDA_DEBUG( 0, "%s(), changing value type not allowed!\n",
324 spin_unlock_irqrestore(&obj
->attribs
->hb_spinlock
, flags
);
328 /* Delete old value */
329 irias_delete_value(attrib
->value
);
331 /* Insert new value */
332 attrib
->value
= new_value
;
335 spin_unlock_irqrestore(&obj
->attribs
->hb_spinlock
, flags
);
338 EXPORT_SYMBOL(irias_object_change_attribute
);
341 * Function irias_object_add_integer_attrib (obj, name, value)
343 * Add an integer attribute to an LM-IAS object
346 void irias_add_integer_attrib(struct ias_object
*obj
, char *name
, int value
,
349 struct ias_attrib
*attrib
;
351 IRDA_ASSERT(obj
!= NULL
, return;);
352 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
353 IRDA_ASSERT(name
!= NULL
, return;);
355 attrib
= kzalloc(sizeof(struct ias_attrib
), GFP_ATOMIC
);
356 if (attrib
== NULL
) {
357 IRDA_WARNING("%s: Unable to allocate attribute!\n",
362 attrib
->magic
= IAS_ATTRIB_MAGIC
;
363 attrib
->name
= strndup(name
, IAS_MAX_ATTRIBNAME
);
366 attrib
->value
= irias_new_integer_value(value
);
367 if (!attrib
->name
|| !attrib
->value
) {
368 IRDA_WARNING("%s: Unable to allocate attribute!\n",
371 irias_delete_value(attrib
->value
);
377 irias_add_attrib(obj
, attrib
, owner
);
379 EXPORT_SYMBOL(irias_add_integer_attrib
);
382 * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
384 * Add a octet sequence attribute to an LM-IAS object
388 void irias_add_octseq_attrib(struct ias_object
*obj
, char *name
, __u8
*octets
,
391 struct ias_attrib
*attrib
;
393 IRDA_ASSERT(obj
!= NULL
, return;);
394 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
396 IRDA_ASSERT(name
!= NULL
, return;);
397 IRDA_ASSERT(octets
!= NULL
, return;);
399 attrib
= kzalloc(sizeof(struct ias_attrib
), GFP_ATOMIC
);
400 if (attrib
== NULL
) {
401 IRDA_WARNING("%s: Unable to allocate attribute!\n",
406 attrib
->magic
= IAS_ATTRIB_MAGIC
;
407 attrib
->name
= strndup(name
, IAS_MAX_ATTRIBNAME
);
409 attrib
->value
= irias_new_octseq_value( octets
, len
);
410 if (!attrib
->name
|| !attrib
->value
) {
411 IRDA_WARNING("%s: Unable to allocate attribute!\n",
414 irias_delete_value(attrib
->value
);
420 irias_add_attrib(obj
, attrib
, owner
);
422 EXPORT_SYMBOL(irias_add_octseq_attrib
);
425 * Function irias_object_add_string_attrib (obj, string)
427 * Add a string attribute to an LM-IAS object
430 void irias_add_string_attrib(struct ias_object
*obj
, char *name
, char *value
,
433 struct ias_attrib
*attrib
;
435 IRDA_ASSERT(obj
!= NULL
, return;);
436 IRDA_ASSERT(obj
->magic
== IAS_OBJECT_MAGIC
, return;);
438 IRDA_ASSERT(name
!= NULL
, return;);
439 IRDA_ASSERT(value
!= NULL
, return;);
441 attrib
= kzalloc(sizeof( struct ias_attrib
), GFP_ATOMIC
);
442 if (attrib
== NULL
) {
443 IRDA_WARNING("%s: Unable to allocate attribute!\n",
448 attrib
->magic
= IAS_ATTRIB_MAGIC
;
449 attrib
->name
= strndup(name
, IAS_MAX_ATTRIBNAME
);
451 attrib
->value
= irias_new_string_value(value
);
452 if (!attrib
->name
|| !attrib
->value
) {
453 IRDA_WARNING("%s: Unable to allocate attribute!\n",
456 irias_delete_value(attrib
->value
);
462 irias_add_attrib(obj
, attrib
, owner
);
464 EXPORT_SYMBOL(irias_add_string_attrib
);
467 * Function irias_new_integer_value (integer)
469 * Create new IAS integer value
472 struct ias_value
*irias_new_integer_value(int integer
)
474 struct ias_value
*value
;
476 value
= kzalloc(sizeof(struct ias_value
), GFP_ATOMIC
);
478 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__
);
482 value
->type
= IAS_INTEGER
;
484 value
->t
.integer
= integer
;
488 EXPORT_SYMBOL(irias_new_integer_value
);
491 * Function irias_new_string_value (string)
493 * Create new IAS string value
495 * Per IrLMP 1.1, 4.3.3.2, strings are up to 256 chars - Jean II
497 struct ias_value
*irias_new_string_value(char *string
)
499 struct ias_value
*value
;
501 value
= kzalloc(sizeof(struct ias_value
), GFP_ATOMIC
);
503 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__
);
507 value
->type
= IAS_STRING
;
508 value
->charset
= CS_ASCII
;
509 value
->t
.string
= strndup(string
, IAS_MAX_STRING
);
510 if (!value
->t
.string
) {
511 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__
);
516 value
->len
= strlen(value
->t
.string
);
522 * Function irias_new_octseq_value (octets, len)
524 * Create new IAS octet-sequence value
526 * Per IrLMP 1.1, 4.3.3.2, octet-sequence are up to 1024 bytes - Jean II
528 struct ias_value
*irias_new_octseq_value(__u8
*octseq
, int len
)
530 struct ias_value
*value
;
532 value
= kzalloc(sizeof(struct ias_value
), GFP_ATOMIC
);
534 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__
);
538 value
->type
= IAS_OCT_SEQ
;
540 if(len
> IAS_MAX_OCTET_STRING
)
541 len
= IAS_MAX_OCTET_STRING
;
544 value
->t
.oct_seq
= kmemdup(octseq
, len
, GFP_ATOMIC
);
545 if (value
->t
.oct_seq
== NULL
){
546 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__
);
553 struct ias_value
*irias_new_missing_value(void)
555 struct ias_value
*value
;
557 value
= kzalloc(sizeof(struct ias_value
), GFP_ATOMIC
);
559 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__
);
563 value
->type
= IAS_MISSING
;
569 * Function irias_delete_value (value)
574 void irias_delete_value(struct ias_value
*value
)
576 IRDA_DEBUG(4, "%s()\n", __FUNCTION__
);
578 IRDA_ASSERT(value
!= NULL
, return;);
580 switch (value
->type
) {
581 case IAS_INTEGER
: /* Fallthrough */
583 /* No need to deallocate */
586 /* Deallocate string */
587 kfree(value
->t
.string
);
590 /* Deallocate byte stream */
591 kfree(value
->t
.oct_seq
);
594 IRDA_DEBUG(0, "%s(), Unknown value type!\n", __FUNCTION__
);
599 EXPORT_SYMBOL(irias_delete_value
);