- pre1:
[davej-history.git] / net / irda / irias_object.c
blob17ad3801de515625c61e634a87f0a27a71ab7ef6
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: 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}};
40 * Function strdup (str)
42 * My own kernel version of strdup!
45 char *strdup(char *str)
47 char *new_str;
49 if (str == NULL)
50 return NULL;
52 ASSERT(strlen( str) < 64, return NULL;);
54 new_str = kmalloc(strlen(str)+1, GFP_ATOMIC);
55 if (new_str == NULL)
56 return NULL;
58 strcpy(new_str, str);
60 return new_str;
64 * Function ias_new_object (name, id)
66 * Create a new IAS object
69 struct ias_object *irias_new_object( char *name, int id)
71 struct ias_object *obj;
73 IRDA_DEBUG( 4, __FUNCTION__ "()\n");
75 obj = (struct ias_object *) kmalloc(sizeof(struct ias_object),
76 GFP_ATOMIC);
77 if (obj == NULL) {
78 IRDA_DEBUG(0, __FUNCTION__ "(), Unable to allocate object!\n");
79 return NULL;
81 memset(obj, 0, sizeof( struct ias_object));
83 obj->magic = IAS_OBJECT_MAGIC;
84 obj->name = strdup( name);
85 obj->id = id;
87 obj->attribs = hashbin_new(HB_LOCAL);
89 return obj;
93 * Function irias_delete_attrib (attrib)
95 * Delete given attribute and deallocate all its memory
98 void __irias_delete_attrib(struct ias_attrib *attrib)
100 ASSERT(attrib != NULL, return;);
101 ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
103 if (attrib->name)
104 kfree(attrib->name);
106 irias_delete_value(attrib->value);
107 attrib->magic = ~IAS_ATTRIB_MAGIC;
109 kfree(attrib);
112 void __irias_delete_object(struct ias_object *obj)
114 ASSERT(obj != NULL, return;);
115 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
117 if (obj->name)
118 kfree(obj->name);
120 hashbin_delete(obj->attribs, (FREE_FUNC) __irias_delete_attrib);
122 obj->magic = ~IAS_OBJECT_MAGIC;
124 kfree(obj);
128 * Function irias_delete_object (obj)
130 * Remove object from hashbin and deallocate all attributes assosiated with
131 * with this object and the object itself
134 int irias_delete_object(struct ias_object *obj)
136 struct ias_object *node;
138 ASSERT(obj != NULL, return -1;);
139 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
141 node = hashbin_remove(objects, 0, obj->name);
142 if (!node)
143 return 0; /* Already removed */
145 __irias_delete_object(node);
147 return 0;
151 * Function irias_insert_object (obj)
153 * Insert an object into the LM-IAS database
156 void irias_insert_object(struct ias_object *obj)
158 ASSERT(obj != NULL, return;);
159 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
161 hashbin_insert(objects, (queue_t *) obj, 0, obj->name);
165 * Function irias_find_object (name)
167 * Find object with given name
170 struct ias_object *irias_find_object(char *name)
172 ASSERT(name != NULL, return NULL;);
174 return hashbin_find(objects, 0, name);
178 * Function irias_find_attrib (obj, name)
180 * Find named attribute in object
183 struct ias_attrib *irias_find_attrib(struct ias_object *obj, char *name)
185 struct ias_attrib *attrib;
187 ASSERT(obj != NULL, return NULL;);
188 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return NULL;);
189 ASSERT(name != NULL, return NULL;);
191 attrib = hashbin_find(obj->attribs, 0, name);
192 if (attrib == NULL)
193 return NULL;
195 return attrib;
199 * Function irias_add_attribute (obj, attrib)
201 * Add attribute to object
204 void irias_add_attrib( struct ias_object *obj, struct ias_attrib *attrib)
206 ASSERT(obj != NULL, return;);
207 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
209 ASSERT(attrib != NULL, return;);
210 ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
212 hashbin_insert(obj->attribs, (queue_t *) attrib, 0, attrib->name);
216 * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
218 * Change the value of an objects attribute.
221 int irias_object_change_attribute(char *obj_name, char *attrib_name,
222 struct ias_value *new_value)
224 struct ias_object *obj;
225 struct ias_attrib *attrib;
227 /* Find object */
228 obj = hashbin_find(objects, 0, obj_name);
229 if (obj == NULL) {
230 WARNING(__FUNCTION__ "(), Unable to find object: %s\n",
231 obj_name);
232 return -1;
235 /* Find attribute */
236 attrib = hashbin_find(obj->attribs, 0, attrib_name);
237 if (attrib == NULL) {
238 WARNING(__FUNCTION__ "(), Unable to find attribute: %s\n",
239 attrib_name);
240 return -1;
243 if ( attrib->value->type != new_value->type) {
244 IRDA_DEBUG( 0, __FUNCTION__
245 "(), changing value type not allowed!\n");
246 return -1;
249 /* Delete old value */
250 irias_delete_value(attrib->value);
252 /* Insert new value */
253 attrib->value = new_value;
255 /* Success */
256 return 0;
260 * Function irias_object_add_integer_attrib (obj, name, value)
262 * Add an integer attribute to an LM-IAS object
265 void irias_add_integer_attrib(struct ias_object *obj, char *name, int value)
267 struct ias_attrib *attrib;
269 ASSERT(obj != NULL, return;);
270 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
271 ASSERT(name != NULL, return;);
273 attrib = (struct ias_attrib *) kmalloc(sizeof(struct ias_attrib),
274 GFP_ATOMIC);
275 if (attrib == NULL) {
276 WARNING(__FUNCTION__ "(), Unable to allocate attribute!\n");
277 return;
279 memset(attrib, 0, sizeof( struct ias_attrib));
281 attrib->magic = IAS_ATTRIB_MAGIC;
282 attrib->name = strdup(name);
284 /* Insert value */
285 attrib->value = irias_new_integer_value(value);
287 irias_add_attrib(obj, attrib);
291 * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
293 * Add a octet sequence attribute to an LM-IAS object
297 void irias_add_octseq_attrib(struct ias_object *obj, char *name, __u8 *octets,
298 int len)
300 struct ias_attrib *attrib;
302 ASSERT(obj != NULL, return;);
303 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
305 ASSERT(name != NULL, return;);
306 ASSERT(octets != NULL, return;);
308 attrib = (struct ias_attrib *) kmalloc(sizeof(struct ias_attrib),
309 GFP_ATOMIC);
310 if (attrib == NULL) {
311 WARNING(__FUNCTION__
312 "(), Unable to allocate attribute!\n");
313 return;
315 memset(attrib, 0, sizeof( struct ias_attrib));
317 attrib->magic = IAS_ATTRIB_MAGIC;
318 attrib->name = strdup( name);
320 attrib->value = irias_new_octseq_value( octets, len);
322 irias_add_attrib(obj, attrib);
326 * Function irias_object_add_string_attrib (obj, string)
328 * Add a string attribute to an LM-IAS object
331 void irias_add_string_attrib(struct ias_object *obj, char *name, char *value)
333 struct ias_attrib *attrib;
335 ASSERT(obj != NULL, return;);
336 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
338 ASSERT(name != NULL, return;);
339 ASSERT(value != NULL, return;);
341 attrib = (struct ias_attrib *) kmalloc(sizeof( struct ias_attrib),
342 GFP_ATOMIC);
343 if (attrib == NULL) {
344 WARNING(__FUNCTION__ "(), Unable to allocate attribute!\n");
345 return;
347 memset(attrib, 0, sizeof( struct ias_attrib));
349 attrib->magic = IAS_ATTRIB_MAGIC;
350 attrib->name = strdup(name);
352 attrib->value = irias_new_string_value(value);
354 irias_add_attrib(obj, attrib);
358 * Function irias_new_integer_value (integer)
360 * Create new IAS integer value
363 struct ias_value *irias_new_integer_value(int integer)
365 struct ias_value *value;
367 value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
368 if (value == NULL) {
369 WARNING(__FUNCTION__ "(), Unable to kmalloc!\n");
370 return NULL;
372 memset(value, 0, sizeof(struct ias_value));
374 value->type = IAS_INTEGER;
375 value->len = 4;
376 value->t.integer = integer;
378 return value;
382 * Function irias_new_string_value (string)
384 * Create new IAS string value
387 struct ias_value *irias_new_string_value(char *string)
389 struct ias_value *value;
391 value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
392 if (value == NULL) {
393 WARNING(__FUNCTION__ "(), Unable to kmalloc!\n");
394 return NULL;
396 memset( value, 0, sizeof( struct ias_value));
398 value->type = IAS_STRING;
399 value->charset = CS_ASCII;
400 value->len = strlen(string);
401 value->t.string = strdup(string);
403 return value;
408 * Function irias_new_octseq_value (octets, len)
410 * Create new IAS octet-sequence value
413 struct ias_value *irias_new_octseq_value(__u8 *octseq , int len)
415 struct ias_value *value;
417 value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
418 if (value == NULL) {
419 WARNING(__FUNCTION__ "(), Unable to kmalloc!\n");
420 return NULL;
422 memset(value, 0, sizeof(struct ias_value));
424 value->type = IAS_OCT_SEQ;
425 value->len = len;
427 value->t.oct_seq = kmalloc(len, GFP_ATOMIC);
428 if (value->t.oct_seq == NULL){
429 WARNING(__FUNCTION__"(), Unable to kmalloc!\n");
430 return NULL;
432 memcpy(value->t.oct_seq, octseq , len);
433 return value;
436 struct ias_value *irias_new_missing_value(void)
438 struct ias_value *value;
440 value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
441 if (value == NULL) {
442 WARNING(__FUNCTION__ "(), Unable to kmalloc!\n");
443 return NULL;
445 memset(value, 0, sizeof(struct ias_value));
447 value->type = IAS_MISSING;
448 value->len = 0;
450 return value;
454 * Function irias_delete_value (value)
456 * Delete IAS value
459 void irias_delete_value(struct ias_value *value)
461 IRDA_DEBUG(4, __FUNCTION__ "()\n");
463 ASSERT(value != NULL, return;);
465 switch (value->type) {
466 case IAS_INTEGER: /* Fallthrough */
467 case IAS_MISSING:
468 /* No need to deallocate */
469 break;
470 case IAS_STRING:
471 /* If string, deallocate string */
472 if (value->t.string != NULL)
473 kfree(value->t.string);
474 break;
475 case IAS_OCT_SEQ:
476 /* If byte stream, deallocate byte stream */
477 if (value->t.oct_seq != NULL)
478 kfree(value->t.oct_seq);
479 break;
480 default:
481 IRDA_DEBUG(0, __FUNCTION__ "(), Unknown value type!\n");
482 break;
484 kfree(value);