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>
28 #include <net/irda/irda.h>
29 #include <net/irda/irmod.h>
30 #include <net/irda/irias_object.h>
32 hashbin_t *objects = NULL;
35 * Used when a missing value needs to be returned
37 struct ias_value 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 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 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 = (struct ias_object *) kmalloc(sizeof(struct ias_object),
88 IRDA_DEBUG(0, "%s(), Unable to allocate object!\n", __FUNCTION__);
91 memset(obj, 0, sizeof( struct ias_object));
93 obj->magic = IAS_OBJECT_MAGIC;
94 obj->name = strndup(name, IAS_MAX_CLASSNAME);
97 obj->attribs = hashbin_new(HB_LOCAL);
103 * Function irias_delete_attrib (attrib)
105 * Delete given attribute and deallocate all its memory
108 void __irias_delete_attrib(struct ias_attrib *attrib)
110 ASSERT(attrib != NULL, return;);
111 ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
116 irias_delete_value(attrib->value);
117 attrib->magic = ~IAS_ATTRIB_MAGIC;
122 void __irias_delete_object(struct ias_object *obj)
124 ASSERT(obj != NULL, return;);
125 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
130 hashbin_delete(obj->attribs, (FREE_FUNC) __irias_delete_attrib);
132 obj->magic = ~IAS_OBJECT_MAGIC;
138 * Function irias_delete_object (obj)
140 * Remove object from hashbin and deallocate all attributes assosiated with
141 * with this object and the object itself
144 int irias_delete_object(struct ias_object *obj)
146 struct ias_object *node;
148 ASSERT(obj != NULL, return -1;);
149 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
151 node = hashbin_remove(objects, 0, obj->name);
153 return 0; /* Already removed */
155 __irias_delete_object(node);
161 * Function irias_delete_attrib (obj)
163 * Remove attribute from hashbin and, if it was the last attribute of
164 * the object, remove the object as well.
167 int irias_delete_attrib(struct ias_object *obj, struct ias_attrib *attrib)
169 struct ias_attrib *node;
171 ASSERT(obj != NULL, return -1;);
172 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
173 ASSERT(attrib != NULL, return -1;);
175 /* Remove attribute from object */
176 node = hashbin_remove(obj->attribs, 0, attrib->name);
178 return 0; /* Already removed or non-existent */
180 /* Deallocate attribute */
181 __irias_delete_attrib(node);
183 /* Check if object has still some attributes */
184 node = (struct ias_attrib *) hashbin_get_first(obj->attribs);
186 irias_delete_object(obj);
192 * Function irias_insert_object (obj)
194 * Insert an object into the LM-IAS database
197 void irias_insert_object(struct ias_object *obj)
199 ASSERT(obj != NULL, return;);
200 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
202 hashbin_insert(objects, (irda_queue_t *) obj, 0, obj->name);
206 * Function irias_find_object (name)
208 * Find object with given name
211 struct ias_object *irias_find_object(char *name)
213 ASSERT(name != NULL, return NULL;);
215 return hashbin_find(objects, 0, name);
219 * Function irias_find_attrib (obj, name)
221 * Find named attribute in object
224 struct ias_attrib *irias_find_attrib(struct ias_object *obj, char *name)
226 struct ias_attrib *attrib;
228 ASSERT(obj != NULL, return NULL;);
229 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return NULL;);
230 ASSERT(name != NULL, return NULL;);
232 attrib = hashbin_find(obj->attribs, 0, name);
240 * Function irias_add_attribute (obj, attrib)
242 * Add attribute to object
245 void irias_add_attrib( struct ias_object *obj, struct ias_attrib *attrib,
248 ASSERT(obj != NULL, return;);
249 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
251 ASSERT(attrib != NULL, return;);
252 ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
254 /* Set if attrib is owned by kernel or user space */
255 attrib->value->owner = owner;
257 hashbin_insert(obj->attribs, (irda_queue_t *) attrib, 0, attrib->name);
261 * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
263 * Change the value of an objects attribute.
266 int irias_object_change_attribute(char *obj_name, char *attrib_name,
267 struct ias_value *new_value)
269 struct ias_object *obj;
270 struct ias_attrib *attrib;
273 obj = hashbin_find(objects, 0, obj_name);
275 WARNING("%s(), Unable to find object: %s\n", __FUNCTION__,
281 attrib = hashbin_find(obj->attribs, 0, attrib_name);
282 if (attrib == NULL) {
283 WARNING("%s(), Unable to find attribute: %s\n", __FUNCTION__,
288 if ( attrib->value->type != new_value->type) {
289 IRDA_DEBUG( 0, "%s(), changing value type not allowed!\n", __FUNCTION__);
293 /* Delete old value */
294 irias_delete_value(attrib->value);
296 /* Insert new value */
297 attrib->value = new_value;
304 * Function irias_object_add_integer_attrib (obj, name, value)
306 * Add an integer attribute to an LM-IAS object
309 void irias_add_integer_attrib(struct ias_object *obj, char *name, int value,
312 struct ias_attrib *attrib;
314 ASSERT(obj != NULL, return;);
315 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
316 ASSERT(name != NULL, return;);
318 attrib = (struct ias_attrib *) kmalloc(sizeof(struct ias_attrib),
320 if (attrib == NULL) {
321 WARNING("%s(), Unable to allocate attribute!\n", __FUNCTION__);
324 memset(attrib, 0, sizeof( struct ias_attrib));
326 attrib->magic = IAS_ATTRIB_MAGIC;
327 attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
330 attrib->value = irias_new_integer_value(value);
332 irias_add_attrib(obj, attrib, owner);
336 * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
338 * Add a octet sequence attribute to an LM-IAS object
342 void irias_add_octseq_attrib(struct ias_object *obj, char *name, __u8 *octets,
345 struct ias_attrib *attrib;
347 ASSERT(obj != NULL, return;);
348 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
350 ASSERT(name != NULL, return;);
351 ASSERT(octets != NULL, return;);
353 attrib = (struct ias_attrib *) kmalloc(sizeof(struct ias_attrib),
355 if (attrib == NULL) {
356 WARNING("%s(), Unable to allocate attribute!\n", __FUNCTION__);
359 memset(attrib, 0, sizeof( struct ias_attrib));
361 attrib->magic = IAS_ATTRIB_MAGIC;
362 attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
364 attrib->value = irias_new_octseq_value( octets, len);
366 irias_add_attrib(obj, attrib, owner);
370 * Function irias_object_add_string_attrib (obj, string)
372 * Add a string attribute to an LM-IAS object
375 void irias_add_string_attrib(struct ias_object *obj, char *name, char *value,
378 struct ias_attrib *attrib;
380 ASSERT(obj != NULL, return;);
381 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
383 ASSERT(name != NULL, return;);
384 ASSERT(value != NULL, return;);
386 attrib = (struct ias_attrib *) kmalloc(sizeof( struct ias_attrib),
388 if (attrib == NULL) {
389 WARNING("%s(), Unable to allocate attribute!\n", __FUNCTION__);
392 memset(attrib, 0, sizeof( struct ias_attrib));
394 attrib->magic = IAS_ATTRIB_MAGIC;
395 attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
397 attrib->value = irias_new_string_value(value);
399 irias_add_attrib(obj, attrib, owner);
403 * Function irias_new_integer_value (integer)
405 * Create new IAS integer value
408 struct ias_value *irias_new_integer_value(int integer)
410 struct ias_value *value;
412 value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
414 WARNING("%s(), Unable to kmalloc!\n", __FUNCTION__);
417 memset(value, 0, sizeof(struct ias_value));
419 value->type = IAS_INTEGER;
421 value->t.integer = integer;
427 * Function irias_new_string_value (string)
429 * Create new IAS string value
431 * Per IrLMP 1.1, 4.3.3.2, strings are up to 256 chars - Jean II
433 struct ias_value *irias_new_string_value(char *string)
435 struct ias_value *value;
437 value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
439 WARNING("%s(), Unable to kmalloc!\n", __FUNCTION__);
442 memset( value, 0, sizeof( struct ias_value));
444 value->type = IAS_STRING;
445 value->charset = CS_ASCII;
446 value->t.string = strndup(string, IAS_MAX_STRING);
447 value->len = strlen(value->t.string);
454 * Function irias_new_octseq_value (octets, len)
456 * Create new IAS octet-sequence value
458 * Per IrLMP 1.1, 4.3.3.2, octet-sequence are up to 1024 bytes - Jean II
460 struct ias_value *irias_new_octseq_value(__u8 *octseq , int len)
462 struct ias_value *value;
464 value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
466 WARNING("%s(), Unable to kmalloc!\n", __FUNCTION__);
469 memset(value, 0, sizeof(struct ias_value));
471 value->type = IAS_OCT_SEQ;
473 if(len > IAS_MAX_OCTET_STRING)
474 len = IAS_MAX_OCTET_STRING;
477 value->t.oct_seq = kmalloc(len, GFP_ATOMIC);
478 if (value->t.oct_seq == NULL){
479 WARNING("%s(), Unable to kmalloc!\n", __FUNCTION__);
483 memcpy(value->t.oct_seq, octseq , len);
487 struct ias_value *irias_new_missing_value(void)
489 struct ias_value *value;
491 value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
493 WARNING("%s(), Unable to kmalloc!\n", __FUNCTION__);
496 memset(value, 0, sizeof(struct ias_value));
498 value->type = IAS_MISSING;
505 * Function irias_delete_value (value)
510 void irias_delete_value(struct ias_value *value)
512 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
514 ASSERT(value != NULL, return;);
516 switch (value->type) {
517 case IAS_INTEGER: /* Fallthrough */
519 /* No need to deallocate */
522 /* If string, deallocate string */
523 if (value->t.string != NULL)
524 kfree(value->t.string);
527 /* If byte stream, deallocate byte stream */
528 if (value->t.oct_seq != NULL)
529 kfree(value->t.oct_seq);
532 IRDA_DEBUG(0, "%s(), Unknown value type!\n", __FUNCTION__);