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);
96 /* Locking notes : the attrib spinlock has lower precendence
97 * than the objects spinlock. Never grap the objects spinlock
98 * while holding any attrib spinlock (risk of deadlock). Jean II */
99 obj->attribs = hashbin_new(HB_LOCK);
101 if (obj->attribs == NULL) {
102 IRDA_WARNING("%s(), Unable to allocate attribs!\n",
110 EXPORT_SYMBOL(irias_new_object);
113 * Function irias_delete_attrib (attrib)
115 * Delete given attribute and deallocate all its memory
118 static void __irias_delete_attrib(struct ias_attrib *attrib)
120 IRDA_ASSERT(attrib != NULL, return;);
121 IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
125 irias_delete_value(attrib->value);
126 attrib->magic = ~IAS_ATTRIB_MAGIC;
131 void __irias_delete_object(struct ias_object *obj)
133 IRDA_ASSERT(obj != NULL, return;);
134 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
138 hashbin_delete(obj->attribs, (FREE_FUNC) __irias_delete_attrib);
140 obj->magic = ~IAS_OBJECT_MAGIC;
146 * Function irias_delete_object (obj)
148 * Remove object from hashbin and deallocate all attributes associated with
149 * with this object and the object itself
152 int irias_delete_object(struct ias_object *obj)
154 struct ias_object *node;
156 IRDA_ASSERT(obj != NULL, return -1;);
157 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
159 /* Remove from list */
160 node = hashbin_remove_this(irias_objects, (irda_queue_t *) obj);
162 IRDA_DEBUG( 0, "%s(), object already removed!\n",
166 __irias_delete_object(obj);
170 EXPORT_SYMBOL(irias_delete_object);
173 * Function irias_delete_attrib (obj)
175 * Remove attribute from hashbin and, if it was the last attribute of
176 * the object, remove the object as well.
179 int irias_delete_attrib(struct ias_object *obj, struct ias_attrib *attrib,
182 struct ias_attrib *node;
184 IRDA_ASSERT(obj != NULL, return -1;);
185 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
186 IRDA_ASSERT(attrib != NULL, return -1;);
188 /* Remove attribute from object */
189 node = hashbin_remove_this(obj->attribs, (irda_queue_t *) attrib);
191 return 0; /* Already removed or non-existent */
193 /* Deallocate attribute */
194 __irias_delete_attrib(node);
196 /* Check if object has still some attributes, destroy it if none.
197 * At first glance, this look dangerous, as the kernel reference
198 * various IAS objects. However, we only use this function on
199 * user attributes, not kernel attributes, so there is no risk
200 * of deleting a kernel object this way. Jean II */
201 node = (struct ias_attrib *) hashbin_get_first(obj->attribs);
202 if (cleanobject && !node)
203 irias_delete_object(obj);
209 * Function irias_insert_object (obj)
211 * Insert an object into the LM-IAS database
214 void irias_insert_object(struct ias_object *obj)
216 IRDA_ASSERT(obj != NULL, return;);
217 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
219 hashbin_insert(irias_objects, (irda_queue_t *) obj, 0, obj->name);
221 EXPORT_SYMBOL(irias_insert_object);
224 * Function irias_find_object (name)
226 * Find object with given name
229 struct ias_object *irias_find_object(char *name)
231 IRDA_ASSERT(name != NULL, return NULL;);
233 /* Unsafe (locking), object might change */
234 return hashbin_lock_find(irias_objects, 0, name);
236 EXPORT_SYMBOL(irias_find_object);
239 * Function irias_find_attrib (obj, name)
241 * Find named attribute in object
244 struct ias_attrib *irias_find_attrib(struct ias_object *obj, char *name)
246 struct ias_attrib *attrib;
248 IRDA_ASSERT(obj != NULL, return NULL;);
249 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return NULL;);
250 IRDA_ASSERT(name != NULL, return NULL;);
252 attrib = hashbin_lock_find(obj->attribs, 0, name);
256 /* Unsafe (locking), attrib might change */
261 * Function irias_add_attribute (obj, attrib)
263 * Add attribute to object
266 static void irias_add_attrib(struct ias_object *obj, struct ias_attrib *attrib,
269 IRDA_ASSERT(obj != NULL, return;);
270 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
272 IRDA_ASSERT(attrib != NULL, return;);
273 IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
275 /* Set if attrib is owned by kernel or user space */
276 attrib->value->owner = owner;
278 hashbin_insert(obj->attribs, (irda_queue_t *) attrib, 0, attrib->name);
282 * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
284 * Change the value of an objects attribute.
287 int irias_object_change_attribute(char *obj_name, char *attrib_name,
288 struct ias_value *new_value)
290 struct ias_object *obj;
291 struct ias_attrib *attrib;
295 obj = hashbin_lock_find(irias_objects, 0, obj_name);
297 IRDA_WARNING("%s: Unable to find object: %s\n", __FUNCTION__,
302 /* Slightly unsafe (obj might get removed under us) */
303 spin_lock_irqsave(&obj->attribs->hb_spinlock, flags);
306 attrib = hashbin_find(obj->attribs, 0, attrib_name);
307 if (attrib == NULL) {
308 IRDA_WARNING("%s: Unable to find attribute: %s\n",
309 __FUNCTION__, attrib_name);
310 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
314 if ( attrib->value->type != new_value->type) {
315 IRDA_DEBUG( 0, "%s(), changing value type not allowed!\n",
317 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
321 /* Delete old value */
322 irias_delete_value(attrib->value);
324 /* Insert new value */
325 attrib->value = new_value;
328 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
331 EXPORT_SYMBOL(irias_object_change_attribute);
334 * Function irias_object_add_integer_attrib (obj, name, value)
336 * Add an integer attribute to an LM-IAS object
339 void irias_add_integer_attrib(struct ias_object *obj, char *name, int value,
342 struct ias_attrib *attrib;
344 IRDA_ASSERT(obj != NULL, return;);
345 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
346 IRDA_ASSERT(name != NULL, return;);
348 attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
349 if (attrib == NULL) {
350 IRDA_WARNING("%s: Unable to allocate attribute!\n",
355 attrib->magic = IAS_ATTRIB_MAGIC;
356 attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
359 attrib->value = irias_new_integer_value(value);
361 irias_add_attrib(obj, attrib, owner);
363 EXPORT_SYMBOL(irias_add_integer_attrib);
366 * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
368 * Add a octet sequence attribute to an LM-IAS object
372 void irias_add_octseq_attrib(struct ias_object *obj, char *name, __u8 *octets,
375 struct ias_attrib *attrib;
377 IRDA_ASSERT(obj != NULL, return;);
378 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
380 IRDA_ASSERT(name != NULL, return;);
381 IRDA_ASSERT(octets != NULL, return;);
383 attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
384 if (attrib == NULL) {
385 IRDA_WARNING("%s: Unable to allocate attribute!\n",
390 attrib->magic = IAS_ATTRIB_MAGIC;
391 attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
393 attrib->value = irias_new_octseq_value( octets, len);
395 irias_add_attrib(obj, attrib, owner);
397 EXPORT_SYMBOL(irias_add_octseq_attrib);
400 * Function irias_object_add_string_attrib (obj, string)
402 * Add a string attribute to an LM-IAS object
405 void irias_add_string_attrib(struct ias_object *obj, char *name, char *value,
408 struct ias_attrib *attrib;
410 IRDA_ASSERT(obj != NULL, return;);
411 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
413 IRDA_ASSERT(name != NULL, return;);
414 IRDA_ASSERT(value != NULL, return;);
416 attrib = kzalloc(sizeof( struct ias_attrib), GFP_ATOMIC);
417 if (attrib == NULL) {
418 IRDA_WARNING("%s: Unable to allocate attribute!\n",
423 attrib->magic = IAS_ATTRIB_MAGIC;
424 attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
426 attrib->value = irias_new_string_value(value);
428 irias_add_attrib(obj, attrib, owner);
430 EXPORT_SYMBOL(irias_add_string_attrib);
433 * Function irias_new_integer_value (integer)
435 * Create new IAS integer value
438 struct ias_value *irias_new_integer_value(int integer)
440 struct ias_value *value;
442 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
444 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
448 value->type = IAS_INTEGER;
450 value->t.integer = integer;
454 EXPORT_SYMBOL(irias_new_integer_value);
457 * Function irias_new_string_value (string)
459 * Create new IAS string value
461 * Per IrLMP 1.1, 4.3.3.2, strings are up to 256 chars - Jean II
463 struct ias_value *irias_new_string_value(char *string)
465 struct ias_value *value;
467 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
469 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
473 value->type = IAS_STRING;
474 value->charset = CS_ASCII;
475 value->t.string = strndup(string, IAS_MAX_STRING);
476 value->len = strlen(value->t.string);
482 * Function irias_new_octseq_value (octets, len)
484 * Create new IAS octet-sequence value
486 * Per IrLMP 1.1, 4.3.3.2, octet-sequence are up to 1024 bytes - Jean II
488 struct ias_value *irias_new_octseq_value(__u8 *octseq , int len)
490 struct ias_value *value;
492 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
494 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
498 value->type = IAS_OCT_SEQ;
500 if(len > IAS_MAX_OCTET_STRING)
501 len = IAS_MAX_OCTET_STRING;
504 value->t.oct_seq = kmalloc(len, GFP_ATOMIC);
505 if (value->t.oct_seq == NULL){
506 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
510 memcpy(value->t.oct_seq, octseq , len);
514 struct ias_value *irias_new_missing_value(void)
516 struct ias_value *value;
518 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
520 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
524 value->type = IAS_MISSING;
531 * Function irias_delete_value (value)
536 void irias_delete_value(struct ias_value *value)
538 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
540 IRDA_ASSERT(value != NULL, return;);
542 switch (value->type) {
543 case IAS_INTEGER: /* Fallthrough */
545 /* No need to deallocate */
548 /* Deallocate string */
549 kfree(value->t.string);
552 /* Deallocate byte stream */
553 kfree(value->t.oct_seq);
556 IRDA_DEBUG(0, "%s(), Unknown value type!\n", __FUNCTION__);
561 EXPORT_SYMBOL(irias_delete_value);