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}};
41 * Function ias_new_object (name, id)
43 * Create a new IAS object
46 struct ias_object *irias_new_object( char *name, int id)
48 struct ias_object *obj;
50 IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
52 obj = kzalloc(sizeof(struct ias_object), GFP_ATOMIC);
54 IRDA_WARNING("%s(), Unable to allocate object!\n",
59 obj->magic = IAS_OBJECT_MAGIC;
60 obj->name = kstrndup(name, IAS_MAX_CLASSNAME, GFP_ATOMIC);
62 IRDA_WARNING("%s(), Unable to allocate name!\n",
69 /* Locking notes : the attrib spinlock has lower precendence
70 * than the objects spinlock. Never grap the objects spinlock
71 * while holding any attrib spinlock (risk of deadlock). Jean II */
72 obj->attribs = hashbin_new(HB_LOCK);
74 if (obj->attribs == NULL) {
75 IRDA_WARNING("%s(), Unable to allocate attribs!\n",
84 EXPORT_SYMBOL(irias_new_object);
87 * Function irias_delete_attrib (attrib)
89 * Delete given attribute and deallocate all its memory
92 static void __irias_delete_attrib(struct ias_attrib *attrib)
94 IRDA_ASSERT(attrib != NULL, return;);
95 IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
99 irias_delete_value(attrib->value);
100 attrib->magic = ~IAS_ATTRIB_MAGIC;
105 void __irias_delete_object(struct ias_object *obj)
107 IRDA_ASSERT(obj != NULL, return;);
108 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
112 hashbin_delete(obj->attribs, (FREE_FUNC) __irias_delete_attrib);
114 obj->magic = ~IAS_OBJECT_MAGIC;
120 * Function irias_delete_object (obj)
122 * Remove object from hashbin and deallocate all attributes associated with
123 * with this object and the object itself
126 int irias_delete_object(struct ias_object *obj)
128 struct ias_object *node;
130 IRDA_ASSERT(obj != NULL, return -1;);
131 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
133 /* Remove from list */
134 node = hashbin_remove_this(irias_objects, (irda_queue_t *) obj);
136 IRDA_DEBUG( 0, "%s(), object already removed!\n",
140 __irias_delete_object(obj);
144 EXPORT_SYMBOL(irias_delete_object);
147 * Function irias_delete_attrib (obj)
149 * Remove attribute from hashbin and, if it was the last attribute of
150 * the object, remove the object as well.
153 int irias_delete_attrib(struct ias_object *obj, struct ias_attrib *attrib,
156 struct ias_attrib *node;
158 IRDA_ASSERT(obj != NULL, return -1;);
159 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
160 IRDA_ASSERT(attrib != NULL, return -1;);
162 /* Remove attribute from object */
163 node = hashbin_remove_this(obj->attribs, (irda_queue_t *) attrib);
165 return 0; /* Already removed or non-existent */
167 /* Deallocate attribute */
168 __irias_delete_attrib(node);
170 /* Check if object has still some attributes, destroy it if none.
171 * At first glance, this look dangerous, as the kernel reference
172 * various IAS objects. However, we only use this function on
173 * user attributes, not kernel attributes, so there is no risk
174 * of deleting a kernel object this way. Jean II */
175 node = (struct ias_attrib *) hashbin_get_first(obj->attribs);
176 if (cleanobject && !node)
177 irias_delete_object(obj);
183 * Function irias_insert_object (obj)
185 * Insert an object into the LM-IAS database
188 void irias_insert_object(struct ias_object *obj)
190 IRDA_ASSERT(obj != NULL, return;);
191 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
193 hashbin_insert(irias_objects, (irda_queue_t *) obj, 0, obj->name);
195 EXPORT_SYMBOL(irias_insert_object);
198 * Function irias_find_object (name)
200 * Find object with given name
203 struct ias_object *irias_find_object(char *name)
205 IRDA_ASSERT(name != NULL, return NULL;);
207 /* Unsafe (locking), object might change */
208 return hashbin_lock_find(irias_objects, 0, name);
210 EXPORT_SYMBOL(irias_find_object);
213 * Function irias_find_attrib (obj, name)
215 * Find named attribute in object
218 struct ias_attrib *irias_find_attrib(struct ias_object *obj, char *name)
220 struct ias_attrib *attrib;
222 IRDA_ASSERT(obj != NULL, return NULL;);
223 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return NULL;);
224 IRDA_ASSERT(name != NULL, return NULL;);
226 attrib = hashbin_lock_find(obj->attribs, 0, name);
230 /* Unsafe (locking), attrib might change */
235 * Function irias_add_attribute (obj, attrib)
237 * Add attribute to object
240 static void irias_add_attrib(struct ias_object *obj, struct ias_attrib *attrib,
243 IRDA_ASSERT(obj != NULL, return;);
244 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
246 IRDA_ASSERT(attrib != NULL, return;);
247 IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
249 /* Set if attrib is owned by kernel or user space */
250 attrib->value->owner = owner;
252 hashbin_insert(obj->attribs, (irda_queue_t *) attrib, 0, attrib->name);
256 * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
258 * Change the value of an objects attribute.
261 int irias_object_change_attribute(char *obj_name, char *attrib_name,
262 struct ias_value *new_value)
264 struct ias_object *obj;
265 struct ias_attrib *attrib;
269 obj = hashbin_lock_find(irias_objects, 0, obj_name);
271 IRDA_WARNING("%s: Unable to find object: %s\n", __FUNCTION__,
276 /* Slightly unsafe (obj might get removed under us) */
277 spin_lock_irqsave(&obj->attribs->hb_spinlock, flags);
280 attrib = hashbin_find(obj->attribs, 0, attrib_name);
281 if (attrib == NULL) {
282 IRDA_WARNING("%s: Unable to find attribute: %s\n",
283 __FUNCTION__, attrib_name);
284 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
288 if ( attrib->value->type != new_value->type) {
289 IRDA_DEBUG( 0, "%s(), changing value type not allowed!\n",
291 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
295 /* Delete old value */
296 irias_delete_value(attrib->value);
298 /* Insert new value */
299 attrib->value = new_value;
302 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
305 EXPORT_SYMBOL(irias_object_change_attribute);
308 * Function irias_object_add_integer_attrib (obj, name, value)
310 * Add an integer attribute to an LM-IAS object
313 void irias_add_integer_attrib(struct ias_object *obj, char *name, int value,
316 struct ias_attrib *attrib;
318 IRDA_ASSERT(obj != NULL, return;);
319 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
320 IRDA_ASSERT(name != NULL, return;);
322 attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
323 if (attrib == NULL) {
324 IRDA_WARNING("%s: Unable to allocate attribute!\n",
329 attrib->magic = IAS_ATTRIB_MAGIC;
330 attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC);
333 attrib->value = irias_new_integer_value(value);
334 if (!attrib->name || !attrib->value) {
335 IRDA_WARNING("%s: Unable to allocate attribute!\n",
338 irias_delete_value(attrib->value);
344 irias_add_attrib(obj, attrib, owner);
346 EXPORT_SYMBOL(irias_add_integer_attrib);
349 * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
351 * Add a octet sequence attribute to an LM-IAS object
355 void irias_add_octseq_attrib(struct ias_object *obj, char *name, __u8 *octets,
358 struct ias_attrib *attrib;
360 IRDA_ASSERT(obj != NULL, return;);
361 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
363 IRDA_ASSERT(name != NULL, return;);
364 IRDA_ASSERT(octets != NULL, return;);
366 attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
367 if (attrib == NULL) {
368 IRDA_WARNING("%s: Unable to allocate attribute!\n",
373 attrib->magic = IAS_ATTRIB_MAGIC;
374 attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC);
376 attrib->value = irias_new_octseq_value( octets, len);
377 if (!attrib->name || !attrib->value) {
378 IRDA_WARNING("%s: Unable to allocate attribute!\n",
381 irias_delete_value(attrib->value);
387 irias_add_attrib(obj, attrib, owner);
389 EXPORT_SYMBOL(irias_add_octseq_attrib);
392 * Function irias_object_add_string_attrib (obj, string)
394 * Add a string attribute to an LM-IAS object
397 void irias_add_string_attrib(struct ias_object *obj, char *name, char *value,
400 struct ias_attrib *attrib;
402 IRDA_ASSERT(obj != NULL, return;);
403 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
405 IRDA_ASSERT(name != NULL, return;);
406 IRDA_ASSERT(value != NULL, return;);
408 attrib = kzalloc(sizeof( struct ias_attrib), GFP_ATOMIC);
409 if (attrib == NULL) {
410 IRDA_WARNING("%s: Unable to allocate attribute!\n",
415 attrib->magic = IAS_ATTRIB_MAGIC;
416 attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC);
418 attrib->value = irias_new_string_value(value);
419 if (!attrib->name || !attrib->value) {
420 IRDA_WARNING("%s: Unable to allocate attribute!\n",
423 irias_delete_value(attrib->value);
429 irias_add_attrib(obj, attrib, owner);
431 EXPORT_SYMBOL(irias_add_string_attrib);
434 * Function irias_new_integer_value (integer)
436 * Create new IAS integer value
439 struct ias_value *irias_new_integer_value(int integer)
441 struct ias_value *value;
443 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
445 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
449 value->type = IAS_INTEGER;
451 value->t.integer = integer;
455 EXPORT_SYMBOL(irias_new_integer_value);
458 * Function irias_new_string_value (string)
460 * Create new IAS string value
462 * Per IrLMP 1.1, 4.3.3.2, strings are up to 256 chars - Jean II
464 struct ias_value *irias_new_string_value(char *string)
466 struct ias_value *value;
468 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
470 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
474 value->type = IAS_STRING;
475 value->charset = CS_ASCII;
476 value->t.string = kstrndup(string, IAS_MAX_STRING, GFP_ATOMIC);
477 if (!value->t.string) {
478 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
483 value->len = strlen(value->t.string);
489 * Function irias_new_octseq_value (octets, len)
491 * Create new IAS octet-sequence value
493 * Per IrLMP 1.1, 4.3.3.2, octet-sequence are up to 1024 bytes - Jean II
495 struct ias_value *irias_new_octseq_value(__u8 *octseq , int len)
497 struct ias_value *value;
499 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
501 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
505 value->type = IAS_OCT_SEQ;
507 if(len > IAS_MAX_OCTET_STRING)
508 len = IAS_MAX_OCTET_STRING;
511 value->t.oct_seq = kmemdup(octseq, len, GFP_ATOMIC);
512 if (value->t.oct_seq == NULL){
513 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
520 struct ias_value *irias_new_missing_value(void)
522 struct ias_value *value;
524 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
526 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
530 value->type = IAS_MISSING;
536 * Function irias_delete_value (value)
541 void irias_delete_value(struct ias_value *value)
543 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
545 IRDA_ASSERT(value != NULL, return;);
547 switch (value->type) {
548 case IAS_INTEGER: /* Fallthrough */
550 /* No need to deallocate */
553 /* Deallocate string */
554 kfree(value->t.string);
557 /* Deallocate byte stream */
558 kfree(value->t.oct_seq);
561 IRDA_DEBUG(0, "%s(), Unknown value type!\n", __FUNCTION__);
566 EXPORT_SYMBOL(irias_delete_value);