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>
11 *
12 * Copyright (c) 1998-1999 Dag Brattli, All Rights Reserved.
13 *
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.
18 *
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.
22 *
23 ********************************************************************/
24
25 #include <linux/string.h>
26 #include <linux/socket.h>
27
28 #include <net/irda/irda.h>
29 #include <net/irda/irmod.h>
30 #include <net/irda/irias_object.h>
31
32 hashbin_t *objects = NULL;
33
34 /*
35 * Used when a missing value needs to be returned
36 */
37 struct ias_value missing = { IAS_MISSING, 0, 0, 0};
38
39 /*
40 * Function strdup (str)
41 *
42 * My own kernel version of strdup!
43 *
44 */
45 char *strdup(char *str)
46 {
47 char *new_str;
48
49 if (str == NULL)
50 return NULL;
51
52 ASSERT(strlen( str) < 64, return NULL;);
53
54 new_str = kmalloc(strlen(str)+1, GFP_ATOMIC);
55 if (new_str == NULL)
56 return NULL;
57
58 strcpy(new_str, str);
59
60 return new_str;
61 }
62
63 /*
64 * Function ias_new_object (name, id)
65 *
66 * Create a new IAS object
67 *
68 */
69 struct ias_object *irias_new_object( char *name, int id)
70 {
71 struct ias_object *obj;
72
73 IRDA_DEBUG( 4, __FUNCTION__ "()\n");
74
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;
80 }
81 memset(obj, 0, sizeof( struct ias_object));
82
83 obj->magic = IAS_OBJECT_MAGIC;
84 obj->name = strdup( name);
85 obj->id = id;
86
87 obj->attribs = hashbin_new(HB_LOCAL);
88
89 return obj;
90 }
91
92 /*
93 * Function irias_delete_attrib (attrib)
94 *
95 * Delete given attribute and deallocate all its memory
96 *
97 */
98 void __irias_delete_attrib(struct ias_attrib *attrib)
99 {
100 ASSERT(attrib != NULL, return;);
101 ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
102
103 if (attrib->name)
104 kfree(attrib->name);
105
106 irias_delete_value(attrib->value);
107 attrib->magic = ~IAS_ATTRIB_MAGIC;
108
109 kfree(attrib);
110 }
111
112 void __irias_delete_object(struct ias_object *obj)
113 {
114 ASSERT(obj != NULL, return;);
115 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
116
117 if (obj->name)
118 kfree(obj->name);
119
120 hashbin_delete(obj->attribs, (FREE_FUNC) __irias_delete_attrib);
121
122 obj->magic = ~IAS_OBJECT_MAGIC;
123
124 kfree(obj);
125 }
126
127 /*
128 * Function irias_delete_object (obj)
129 *
130 * Remove object from hashbin and deallocate all attributes assosiated with
131 * with this object and the object itself
132 *
133 */
134 int irias_delete_object(struct ias_object *obj)
135 {
136 struct ias_object *node;
137
138 ASSERT(obj != NULL, return -1;);
139 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
140
141 node = hashbin_remove(objects, 0, obj->name);
142 if (!node)
143 return 0; /* Already removed */
144
145 __irias_delete_object(node);
146
147 return 0;
148 }
149
150 /*
151 * Function irias_delete_attrib (obj)
152 *
153 * Remove attribute from hashbin and, if it was the last attribute of
154 * the object, remove the object as well.
155 *
156 */
157 int irias_delete_attrib(struct ias_object *obj, struct ias_attrib *attrib)
158 {
159 struct ias_attrib *node;
160
161 ASSERT(obj != NULL, return -1;);
162 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
163 ASSERT(attrib != NULL, return -1;);
164
165 /* Remove atribute from object */
166 node = hashbin_remove(obj->attribs, 0, attrib->name);
167 if (!node)
168 return 0; /* Already removed or non-existent */
169
170 /* Deallocate attribute */
171 __irias_delete_attrib(node);
172
173 /* Check if object has still some attributes */
174 node = (struct ias_attrib *) hashbin_get_first(obj->attribs);
175 if (!node)
176 irias_delete_object(obj);
177
178 return 0;
179 }
180
181 /*
182 * Function irias_insert_object (obj)
183 *
184 * Insert an object into the LM-IAS database
185 *
186 */
187 void irias_insert_object(struct ias_object *obj)
188 {
189 ASSERT(obj != NULL, return;);
190 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
191
192 hashbin_insert(objects, (irda_queue_t *) obj, 0, obj->name);
193 }
194
195 /*
196 * Function irias_find_object (name)
197 *
198 * Find object with given name
199 *
200 */
201 struct ias_object *irias_find_object(char *name)
202 {
203 ASSERT(name != NULL, return NULL;);
204
205 return hashbin_find(objects, 0, name);
206 }
207
208 /*
209 * Function irias_find_attrib (obj, name)
210 *
211 * Find named attribute in object
212 *
213 */
214 struct ias_attrib *irias_find_attrib(struct ias_object *obj, char *name)
215 {
216 struct ias_attrib *attrib;
217
218 ASSERT(obj != NULL, return NULL;);
219 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return NULL;);
220 ASSERT(name != NULL, return NULL;);
221
222 attrib = hashbin_find(obj->attribs, 0, name);
223 if (attrib == NULL)
224 return NULL;
225
226 return attrib;
227 }
228
229 /*
230 * Function irias_add_attribute (obj, attrib)
231 *
232 * Add attribute to object
233 *
234 */
235 void irias_add_attrib( struct ias_object *obj, struct ias_attrib *attrib,
236 int owner)
237 {
238 ASSERT(obj != NULL, return;);
239 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
240
241 ASSERT(attrib != NULL, return;);
242 ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
243
244 /* Set if attrib is owned by kernel or user space */
245 attrib->value->owner = owner;
246
247 hashbin_insert(obj->attribs, (irda_queue_t *) attrib, 0, attrib->name);
248 }
249
250 /*
251 * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
252 *
253 * Change the value of an objects attribute.
254 *
255 */
256 int irias_object_change_attribute(char *obj_name, char *attrib_name,
257 struct ias_value *new_value)
258 {
259 struct ias_object *obj;
260 struct ias_attrib *attrib;
261
262 /* Find object */
263 obj = hashbin_find(objects, 0, obj_name);
264 if (obj == NULL) {
265 WARNING(__FUNCTION__ "(), Unable to find object: %s\n",
266 obj_name);
267 return -1;
268 }
269
270 /* Find attribute */
271 attrib = hashbin_find(obj->attribs, 0, attrib_name);
272 if (attrib == NULL) {
273 WARNING(__FUNCTION__ "(), Unable to find attribute: %s\n",
274 attrib_name);
275 return -1;
276 }
277
278 if ( attrib->value->type != new_value->type) {
279 IRDA_DEBUG( 0, __FUNCTION__
280 "(), changing value type not allowed!\n");
281 return -1;
282 }
283
284 /* Delete old value */
285 irias_delete_value(attrib->value);
286
287 /* Insert new value */
288 attrib->value = new_value;
289
290 /* Success */
291 return 0;
292 }
293
294 /*
295 * Function irias_object_add_integer_attrib (obj, name, value)
296 *
297 * Add an integer attribute to an LM-IAS object
298 *
299 */
300 void irias_add_integer_attrib(struct ias_object *obj, char *name, int value,
301 int owner)
302 {
303 struct ias_attrib *attrib;
304
305 ASSERT(obj != NULL, return;);
306 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
307 ASSERT(name != NULL, return;);
308
309 attrib = (struct ias_attrib *) kmalloc(sizeof(struct ias_attrib),
310 GFP_ATOMIC);
311 if (attrib == NULL) {
312 WARNING(__FUNCTION__ "(), Unable to allocate attribute!\n");
313 return;
314 }
315 memset(attrib, 0, sizeof( struct ias_attrib));
316
317 attrib->magic = IAS_ATTRIB_MAGIC;
318 attrib->name = strdup(name);
319
320 /* Insert value */
321 attrib->value = irias_new_integer_value(value);
322
323 irias_add_attrib(obj, attrib, owner);
324 }
325
326 /*
327 * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
328 *
329 * Add a octet sequence attribute to an LM-IAS object
330 *
331 */
332
333 void irias_add_octseq_attrib(struct ias_object *obj, char *name, __u8 *octets,
334 int len, int owner)
335 {
336 struct ias_attrib *attrib;
337
338 ASSERT(obj != NULL, return;);
339 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
340
341 ASSERT(name != NULL, return;);
342 ASSERT(octets != NULL, return;);
343
344 attrib = (struct ias_attrib *) kmalloc(sizeof(struct ias_attrib),
345 GFP_ATOMIC);
346 if (attrib == NULL) {
347 WARNING(__FUNCTION__
348 "(), Unable to allocate attribute!\n");
349 return;
350 }
351 memset(attrib, 0, sizeof( struct ias_attrib));
352
353 attrib->magic = IAS_ATTRIB_MAGIC;
354 attrib->name = strdup( name);
355
356 attrib->value = irias_new_octseq_value( octets, len);
357
358 irias_add_attrib(obj, attrib, owner);
359 }
360
361 /*
362 * Function irias_object_add_string_attrib (obj, string)
363 *
364 * Add a string attribute to an LM-IAS object
365 *
366 */
367 void irias_add_string_attrib(struct ias_object *obj, char *name, char *value,
368 int owner)
369 {
370 struct ias_attrib *attrib;
371
372 ASSERT(obj != NULL, return;);
373 ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
374
375 ASSERT(name != NULL, return;);
376 ASSERT(value != NULL, return;);
377
378 attrib = (struct ias_attrib *) kmalloc(sizeof( struct ias_attrib),
379 GFP_ATOMIC);
380 if (attrib == NULL) {
381 WARNING(__FUNCTION__ "(), Unable to allocate attribute!\n");
382 return;
383 }
384 memset(attrib, 0, sizeof( struct ias_attrib));
385
386 attrib->magic = IAS_ATTRIB_MAGIC;
387 attrib->name = strdup(name);
388
389 attrib->value = irias_new_string_value(value);
390
391 irias_add_attrib(obj, attrib, owner);
392 }
393
394 /*
395 * Function irias_new_integer_value (integer)
396 *
397 * Create new IAS integer value
398 *
399 */
400 struct ias_value *irias_new_integer_value(int integer)
401 {
402 struct ias_value *value;
403
404 value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
405 if (value == NULL) {
406 WARNING(__FUNCTION__ "(), Unable to kmalloc!\n");
407 return NULL;
408 }
409 memset(value, 0, sizeof(struct ias_value));
410
411 value->type = IAS_INTEGER;
412 value->len = 4;
413 value->t.integer = integer;
414
415 return value;
416 }
417
418 /*
419 * Function irias_new_string_value (string)
420 *
421 * Create new IAS string value
422 *
423 */
424 struct ias_value *irias_new_string_value(char *string)
425 {
426 struct ias_value *value;
427
428 value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
429 if (value == NULL) {
430 WARNING(__FUNCTION__ "(), Unable to kmalloc!\n");
431 return NULL;
432 }
433 memset( value, 0, sizeof( struct ias_value));
434
435 value->type = IAS_STRING;
436 value->charset = CS_ASCII;
437 value->len = strlen(string);
438 value->t.string = strdup(string);
439
440 return value;
441 }
442
443
444 /*
445 * Function irias_new_octseq_value (octets, len)
446 *
447 * Create new IAS octet-sequence value
448 *
449 */
450 struct ias_value *irias_new_octseq_value(__u8 *octseq , int len)
451 {
452 struct ias_value *value;
453
454 value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
455 if (value == NULL) {
456 WARNING(__FUNCTION__ "(), Unable to kmalloc!\n");
457 return NULL;
458 }
459 memset(value, 0, sizeof(struct ias_value));
460
461 value->type = IAS_OCT_SEQ;
462 value->len = len;
463
464 value->t.oct_seq = kmalloc(len, GFP_ATOMIC);
465 if (value->t.oct_seq == NULL){
466 WARNING(__FUNCTION__"(), Unable to kmalloc!\n");
467 return NULL;
468 }
469 memcpy(value->t.oct_seq, octseq , len);
470 return value;
471 }
472
473 struct ias_value *irias_new_missing_value(void)
474 {
475 struct ias_value *value;
476
477 value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
478 if (value == NULL) {
479 WARNING(__FUNCTION__ "(), Unable to kmalloc!\n");
480 return NULL;
481 }
482 memset(value, 0, sizeof(struct ias_value));
483
484 value->type = IAS_MISSING;
485 value->len = 0;
486
487 return value;
488 }
489
490 /*
491 * Function irias_delete_value (value)
492 *
493 * Delete IAS value
494 *
495 */
496 void irias_delete_value(struct ias_value *value)
497 {
498 IRDA_DEBUG(4, __FUNCTION__ "()\n");
499
500 ASSERT(value != NULL, return;);
501
502 switch (value->type) {
503 case IAS_INTEGER: /* Fallthrough */
504 case IAS_MISSING:
505 /* No need to deallocate */
506 break;
507 case IAS_STRING:
508 /* If string, deallocate string */
509 if (value->t.string != NULL)
510 kfree(value->t.string);
511 break;
512 case IAS_OCT_SEQ:
513 /* If byte stream, deallocate byte stream */
514 if (value->t.oct_seq != NULL)
515 kfree(value->t.oct_seq);
516 break;
517 default:
518 IRDA_DEBUG(0, __FUNCTION__ "(), Unknown value type!\n");
519 break;
520 }
521 kfree(value);
522 }
523
524
525
526
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.