Import 2.3.16
[davej-history.git] / net / irda / irias_object.c
blob8621c43b6c2b22e22d7abd8e98335ebf978383d7
1 /*********************************************************************
2 *
3 * Filename: irias_object.c
4 * Version: 0.3
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: Mon Jun 21 16:11:13 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/irda.h>
29 #include <net/irda/irda.h>
30 #include <net/irda/irmod.h>
31 #include <net/irda/irias_object.h>
33 hashbin_t *objects = NULL;
36 * Used when a missing value needs to be returned
38 struct ias_value missing = { IAS_MISSING, 0, 0, {0}};
41 * Function strdup (str)
43 * My own kernel version of strdup!
46 char *strdup(char *str)
48 char *new_str;
50 if (str == NULL)
51 return NULL;
53 ASSERT(strlen( str) < 64, return NULL;);
55 new_str = kmalloc(strlen(str)+1, GFP_ATOMIC);
56 if (new_str == NULL)
57 return NULL;
59 strcpy(new_str, str);
61 return new_str;
65 * Function ias_new_object (name, id)
67 * Create a new IAS object
70 struct ias_object *irias_new_object( char *name, int id)
72 struct ias_object *obj;
74 DEBUG( 4, __FUNCTION__ "()\n");
76 obj = (struct ias_object *) kmalloc( sizeof( struct ias_object),
77 GFP_ATOMIC);
78 if (obj == NULL) {
79 DEBUG(0, __FUNCTION__ "(), Unable to allocate object!\n");
80 return NULL;
82 memset(obj, 0, sizeof( struct ias_object));
84 obj->magic = IAS_OBJECT_MAGIC;
85 obj->name = strdup( name);
86 obj->id = id;
88 obj->attribs = hashbin_new(HB_LOCAL);
90 return obj;
94 * Function irias_delete_attrib (attrib)
96 * Delete given attribute and deallocate all its memory
99 void __irias_delete_attrib(struct ias_attrib *attrib)
101 ASSERT(attrib != NULL, return;);
102 ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
104 if (attrib->name)
105 kfree(attrib->name);
107 irias_delete_value(attrib->value);
108 attrib->magic = ~IAS_ATTRIB_MAGIC;
110 kfree(attrib);
113 void __irias_delete_object(struct ias_object *obj)
115 ASSERT(obj != NULL, return;);
116 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
118 if (obj->name)
119 kfree(obj->name);
121 hashbin_delete(obj->attribs, (FREE_FUNC) __irias_delete_attrib);
123 obj->magic = ~IAS_OBJECT_MAGIC;
125 kfree(obj);
129 * Function irias_delete_object (obj)
131 * Remove object from hashbin and deallocate all attributes assosiated with
132 * with this object and the object itself
135 int irias_delete_object(struct ias_object *obj)
137 struct ias_object *node;
139 ASSERT(obj != NULL, return -1;);
140 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
142 node = hashbin_remove(objects, 0, obj->name);
143 if (!node)
144 return 0; /* Already removed */
146 __irias_delete_object(node);
148 return 0;
152 * Function irias_insert_object (obj)
154 * Insert an object into the LM-IAS database
157 void irias_insert_object(struct ias_object *obj)
159 DEBUG(4, __FUNCTION__ "()\n");
161 ASSERT(obj != NULL, return;);
162 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
164 hashbin_insert(objects, (QUEUE *) obj, 0, obj->name);
168 * Function irias_find_object (name)
170 * Find object with given name
173 struct ias_object *irias_find_object(char *name)
175 ASSERT(name != NULL, return NULL;);
177 return hashbin_find(objects, 0, name);
181 * Function irias_find_attrib (obj, name)
183 * Find named attribute in object
186 struct ias_attrib *irias_find_attrib( struct ias_object *obj, char *name)
188 struct ias_attrib *attrib;
190 DEBUG( 4, __FUNCTION__ "()\n");
192 ASSERT( obj != NULL, return NULL;);
193 ASSERT( obj->magic == IAS_OBJECT_MAGIC, return NULL;);
194 ASSERT( name != NULL, return NULL;);
196 attrib = hashbin_find( obj->attribs, 0, name);
197 if ( attrib == NULL)
198 return NULL;
200 return attrib;
204 * Function irias_add_attribute (obj, attrib)
206 * Add attribute to object
209 void irias_add_attrib( struct ias_object *obj, struct ias_attrib *attrib)
211 DEBUG( 4, __FUNCTION__ "()\n");
213 ASSERT( obj != NULL, return;);
214 ASSERT( obj->magic == IAS_OBJECT_MAGIC, return;);
216 ASSERT( attrib != NULL, return;);
217 ASSERT( attrib->magic == IAS_ATTRIB_MAGIC, return;);
219 hashbin_insert( obj->attribs, (QUEUE *) attrib, 0, attrib->name);
223 * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
225 * Change the value of an objects attribute.
228 int irias_object_change_attribute( char *obj_name, char *attrib_name,
229 struct ias_value *new_value)
231 struct ias_object *obj;
232 struct ias_attrib *attrib;
234 /* Find object */
235 obj = hashbin_find( objects, 0, obj_name);
236 if ( obj == NULL) {
237 DEBUG( 0, __FUNCTION__ "(), Unable to find object: %s\n",
238 obj_name);
239 return -1;
242 /* Find attribute */
243 attrib = hashbin_find( obj->attribs, 0, attrib_name);
244 if ( attrib == NULL) {
245 DEBUG( 0, __FUNCTION__ "(), Unable to find attribute: %s\n",
246 attrib_name);
247 return -1;
250 if ( attrib->value->type != new_value->type) {
251 DEBUG( 0, __FUNCTION__
252 "(), changing value type not allowed!\n");
253 return -1;
256 /* Delete old value */
257 irias_delete_value( attrib->value);
259 /* Insert new value */
260 attrib->value = new_value;
262 /* Success */
263 return 0;
267 * Function irias_object_add_integer_attrib (obj, name, value)
269 * Add an integer attribute to an LM-IAS object
272 void irias_add_integer_attrib( struct ias_object *obj, char *name, int value)
274 struct ias_attrib *attrib;
276 ASSERT( obj != NULL, return;);
277 ASSERT( obj->magic == IAS_OBJECT_MAGIC, return;);
278 ASSERT( name != NULL, return;);
280 attrib = (struct ias_attrib *) kmalloc( sizeof( struct ias_attrib),
281 GFP_ATOMIC);
282 if ( attrib == NULL) {
283 DEBUG( 0, __FUNCTION__
284 "(), Unable to allocate attribute!\n");
285 return;
287 memset( attrib, 0, sizeof( struct ias_attrib));
289 attrib->magic = IAS_ATTRIB_MAGIC;
290 attrib->name = strdup( name);
291 /* attrib->attr = NULL; */
293 /* Insert value */
294 attrib->value = irias_new_integer_value( value);
296 irias_add_attrib( obj, attrib);
300 * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
302 * Add a octet sequence attribute to an LM-IAS object
306 void irias_add_octseq_attrib( struct ias_object *obj, char *name,
307 __u8 *octets, int len)
309 struct ias_attrib *attrib;
311 ASSERT( obj != NULL, return;);
312 ASSERT( obj->magic == IAS_OBJECT_MAGIC, return;);
314 ASSERT( name != NULL, return;);
315 ASSERT( octets != NULL, return;);
316 ASSERT( len < 55, return;); /* FIXME: must be 1024, but... */
318 attrib = (struct ias_attrib *) kmalloc( sizeof( struct ias_attrib),
319 GFP_ATOMIC);
320 if ( attrib == NULL) {
321 DEBUG( 0, __FUNCTION__
322 "(), Unable to allocate attribute!\n");
323 return;
325 memset( attrib, 0, sizeof( struct ias_attrib));
327 attrib->magic = IAS_ATTRIB_MAGIC;
328 attrib->name = strdup( name);
330 attrib->value = irias_new_octseq_value( octets, len);
332 irias_add_attrib( obj, attrib);
336 * Function irias_object_add_string_attrib (obj, string)
338 * Add a string attribute to an LM-IAS object
341 void irias_add_string_attrib( struct ias_object *obj, char *name, char *value)
343 struct ias_attrib *attrib;
345 ASSERT( obj != NULL, return;);
346 ASSERT( obj->magic == IAS_OBJECT_MAGIC, return;);
348 ASSERT( name != NULL, return;);
349 ASSERT( value != NULL, return;);
351 attrib = (struct ias_attrib *) kmalloc( sizeof( struct ias_attrib),
352 GFP_ATOMIC);
353 if ( attrib == NULL) {
354 DEBUG( 0, __FUNCTION__
355 "(), Unable to allocate attribute!\n");
356 return;
358 memset( attrib, 0, sizeof( struct ias_attrib));
360 attrib->magic = IAS_ATTRIB_MAGIC;
361 attrib->name = strdup( name);
363 attrib->value = irias_new_string_value( value);
365 irias_add_attrib( obj, attrib);
369 * Function irias_new_integer_value (integer)
371 * Create new IAS integer value
374 struct ias_value *irias_new_integer_value( int integer)
376 struct ias_value *value;
378 value = kmalloc( sizeof( struct ias_value), GFP_ATOMIC);
379 if ( value == NULL) {
380 DEBUG( 0, __FUNCTION__ "(), Unable to kmalloc!\n");
381 return NULL;
383 memset( value, 0, sizeof( struct ias_value));
385 value->type = IAS_INTEGER;
386 value->len = 4;
387 value->t.integer = integer;
389 return value;
393 * Function irias_new_string_value (string)
395 * Create new IAS string value
398 struct ias_value *irias_new_string_value( char *string)
400 struct ias_value *value;
402 value = kmalloc( sizeof( struct ias_value), GFP_ATOMIC);
403 if ( value == NULL) {
404 DEBUG( 0, __FUNCTION__ "(), Unable to kmalloc!\n");
405 return NULL;
407 memset( value, 0, sizeof( struct ias_value));
409 value->type = IAS_STRING;
410 value->charset = CS_ASCII;
411 value->len = strlen( string);
412 value->t.string = strdup( string);
414 return value;
419 * Function irias_new_octseq_value (octets, len)
421 * Create new IAS octet-sequence value
424 struct ias_value *irias_new_octseq_value( __u8 *octseq , int len)
426 struct ias_value *value;
428 ASSERT(len <= 55, return NULL;); /*FIXME: must be 1024, but.....*/
430 value = kmalloc( sizeof( struct ias_value), GFP_ATOMIC);
431 if ( value == NULL) {
432 DEBUG( 0, __FUNCTION__ "(), Unable to kmalloc!\n");
433 return NULL;
435 memset( value, 0, sizeof( struct ias_value));
437 value->type = IAS_OCT_SEQ;
438 value->len = len;
440 value->t.oct_seq = kmalloc(len, GFP_ATOMIC);
441 if( value->t.oct_seq == NULL){
442 DEBUG(0, __FUNCTION__"(), Unable to kmalloc!\n");
443 return NULL;
445 memcpy(value->t.oct_seq, octseq , len);
446 return value;
450 * Function irias_delete_value (value)
452 * Delete IAS value
455 void irias_delete_value( struct ias_value *value)
457 DEBUG( 4, __FUNCTION__ "()\n");
459 ASSERT( value != NULL, return;);
461 switch( value->type) {
462 case IAS_INTEGER: /* Fallthrough */
463 case IAS_MISSING:
464 /* No need to deallocate */
465 break;
466 case IAS_STRING:
467 /* If string, deallocate string */
468 if ( value->t.string != NULL)
469 kfree( value->t.string);
470 break;
471 case IAS_OCT_SEQ:
472 /* If byte stream, deallocate byte stream */
473 if ( value->t.oct_seq != NULL)
474 kfree(value->t.oct_seq);
475 break;
476 default:
477 DEBUG( 0, __FUNCTION__ "(), Unknown value type!\n");
478 break;
480 kfree( value);