Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

irias_object.c

Go to the documentation of this file.
00001 /*********************************************************************
00002  *                
00003  * Filename:      irias_object.c
00004  * Version:       0.3
00005  * Description:   IAS object database and functions
00006  * Status:        Experimental.
00007  * Author:        Dag Brattli <dagb@cs.uit.no>
00008  * Created at:    Thu Oct  1 22:50:04 1998
00009  * Modified at:   Wed Dec 15 11:23:16 1999
00010  * Modified by:   Dag Brattli <dagb@cs.uit.no>
00011  * 
00012  *     Copyright (c) 1998-1999 Dag Brattli, All Rights Reserved.
00013  *      
00014  *     This program is free software; you can redistribute it and/or 
00015  *     modify it under the terms of the GNU General Public License as 
00016  *     published by the Free Software Foundation; either version 2 of 
00017  *     the License, or (at your option) any later version.
00018  *  
00019  *     Neither Dag Brattli nor University of Tromsų admit liability nor
00020  *     provide warranty for any of this software. This material is 
00021  *     provided "AS-IS" and at no charge.
00022  *     
00023  ********************************************************************/
00024 
00025 #include <linux/string.h>
00026 #include <linux/socket.h>
00027 
00028 #include <net/irda/irda.h>
00029 #include <net/irda/irmod.h>
00030 #include <net/irda/irias_object.h>
00031 
00032 hashbin_t *objects = NULL;
00033 
00034 /*
00035  *  Used when a missing value needs to be returned
00036  */
00037 struct ias_value missing = { IAS_MISSING, 0, 0, {0}};
00038 
00039 /*
00040  * Function strdup (str)
00041  *
00042  *    My own kernel version of strdup!
00043  *
00044  */
00045 char *strdup(char *str)
00046 {
00047         char *new_str;
00048         
00049         if (str == NULL)
00050                 return NULL;
00051 
00052         ASSERT(strlen( str) < 64, return NULL;);
00053         
00054         new_str = kmalloc(strlen(str)+1, GFP_ATOMIC);
00055         if (new_str == NULL)
00056                 return NULL;
00057         
00058         strcpy(new_str, str);
00059         
00060         return new_str;
00061 }
00062 
00063 /*
00064  * Function ias_new_object (name, id)
00065  *
00066  *    Create a new IAS object
00067  *
00068  */
00069 struct ias_object *irias_new_object( char *name, int id)
00070 {
00071         struct ias_object *obj;
00072         
00073         IRDA_DEBUG( 4, __FUNCTION__ "()\n");
00074 
00075         obj = (struct ias_object *) kmalloc(sizeof(struct ias_object), 
00076                                             GFP_ATOMIC);
00077         if (obj == NULL) {
00078                 IRDA_DEBUG(0, __FUNCTION__ "(), Unable to allocate object!\n");
00079                 return NULL;
00080         }
00081         memset(obj, 0, sizeof( struct ias_object));
00082 
00083         obj->magic = IAS_OBJECT_MAGIC;
00084         obj->name = strdup( name);
00085         obj->id = id;
00086 
00087         obj->attribs = hashbin_new(HB_LOCAL);
00088         
00089         return obj;
00090 }
00091 
00092 /*
00093  * Function irias_delete_attrib (attrib)
00094  *
00095  *    Delete given attribute and deallocate all its memory
00096  *
00097  */
00098 void __irias_delete_attrib(struct ias_attrib *attrib)
00099 {
00100         ASSERT(attrib != NULL, return;);
00101         ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
00102 
00103         if (attrib->name)
00104                 kfree(attrib->name);
00105 
00106         irias_delete_value(attrib->value);
00107         attrib->magic = ~IAS_ATTRIB_MAGIC;
00108         
00109         kfree(attrib);
00110 }
00111 
00112 void __irias_delete_object(struct ias_object *obj)
00113 {
00114         ASSERT(obj != NULL, return;);
00115         ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
00116 
00117         if (obj->name)
00118                 kfree(obj->name);
00119         
00120         hashbin_delete(obj->attribs, (FREE_FUNC) __irias_delete_attrib);
00121         
00122         obj->magic = ~IAS_OBJECT_MAGIC;
00123         
00124         kfree(obj);
00125 }
00126 
00127 /*
00128  * Function irias_delete_object (obj)
00129  *
00130  *    Remove object from hashbin and deallocate all attributes assosiated with
00131  *    with this object and the object itself
00132  *
00133  */
00134 int irias_delete_object(struct ias_object *obj) 
00135 {
00136         struct ias_object *node;
00137 
00138         ASSERT(obj != NULL, return -1;);
00139         ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
00140 
00141         node = hashbin_remove(objects, 0, obj->name);
00142         if (!node)
00143                 return 0; /* Already removed */
00144 
00145         __irias_delete_object(node);
00146 
00147         return 0;
00148 }
00149 
00150 /*
00151  * Function irias_insert_object (obj)
00152  *
00153  *    Insert an object into the LM-IAS database
00154  *
00155  */
00156 void irias_insert_object(struct ias_object *obj)
00157 {
00158         ASSERT(obj != NULL, return;);
00159         ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
00160         
00161         hashbin_insert(objects, (queue_t *) obj, 0, obj->name);
00162 }
00163 
00164 /*
00165  * Function irias_find_object (name)
00166  *
00167  *    Find object with given name
00168  *
00169  */
00170 struct ias_object *irias_find_object(char *name)
00171 {
00172         ASSERT(name != NULL, return NULL;);
00173 
00174         return hashbin_find(objects, 0, name);
00175 }
00176 
00177 /*
00178  * Function irias_find_attrib (obj, name)
00179  *
00180  *    Find named attribute in object
00181  *
00182  */
00183 struct ias_attrib *irias_find_attrib(struct ias_object *obj, char *name)
00184 {
00185         struct ias_attrib *attrib;
00186 
00187         ASSERT(obj != NULL, return NULL;);
00188         ASSERT(obj->magic == IAS_OBJECT_MAGIC, return NULL;);
00189         ASSERT(name != NULL, return NULL;);
00190 
00191         attrib = hashbin_find(obj->attribs, 0, name);
00192         if (attrib == NULL)
00193                 return NULL;
00194 
00195         return attrib;
00196 }
00197 
00198 /*
00199  * Function irias_add_attribute (obj, attrib)
00200  *
00201  *    Add attribute to object
00202  *
00203  */
00204 void irias_add_attrib( struct ias_object *obj, struct ias_attrib *attrib)
00205 {
00206         ASSERT(obj != NULL, return;);
00207         ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
00208         
00209         ASSERT(attrib != NULL, return;);
00210         ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
00211 
00212         hashbin_insert(obj->attribs, (queue_t *) attrib, 0, attrib->name);
00213 }
00214 
00215 /*
00216  * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
00217  *
00218  *    Change the value of an objects attribute.
00219  *
00220  */
00221 int irias_object_change_attribute(char *obj_name, char *attrib_name, 
00222                                   struct ias_value *new_value) 
00223 {
00224         struct ias_object *obj;
00225         struct ias_attrib *attrib;
00226 
00227         /* Find object */
00228         obj = hashbin_find(objects, 0, obj_name);
00229         if (obj == NULL) {
00230                 WARNING(__FUNCTION__ "(), Unable to find object: %s\n",
00231                         obj_name);
00232                 return -1;
00233         }
00234 
00235         /* Find attribute */
00236         attrib = hashbin_find(obj->attribs, 0, attrib_name);
00237         if (attrib == NULL) {
00238                 WARNING(__FUNCTION__ "(), Unable to find attribute: %s\n",
00239                         attrib_name);
00240                 return -1;
00241         }
00242         
00243         if ( attrib->value->type != new_value->type) {
00244                 IRDA_DEBUG( 0, __FUNCTION__ 
00245                        "(), changing value type not allowed!\n");
00246                 return -1;
00247         }
00248 
00249         /* Delete old value */
00250         irias_delete_value(attrib->value);
00251         
00252         /* Insert new value */
00253         attrib->value = new_value;
00254 
00255         /* Success */
00256         return 0;
00257 }
00258 
00259 /*
00260  * Function irias_object_add_integer_attrib (obj, name, value)
00261  *
00262  *    Add an integer attribute to an LM-IAS object
00263  *
00264  */
00265 void irias_add_integer_attrib(struct ias_object *obj, char *name, int value)
00266 {
00267         struct ias_attrib *attrib;
00268 
00269         ASSERT(obj != NULL, return;);
00270         ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
00271         ASSERT(name != NULL, return;);
00272         
00273         attrib = (struct ias_attrib *) kmalloc(sizeof(struct ias_attrib), 
00274                                                GFP_ATOMIC);
00275         if (attrib == NULL) {
00276                 WARNING(__FUNCTION__ "(), Unable to allocate attribute!\n");
00277                 return;
00278         }
00279         memset(attrib, 0, sizeof( struct ias_attrib));
00280 
00281         attrib->magic = IAS_ATTRIB_MAGIC;
00282         attrib->name = strdup(name);
00283 
00284         /* Insert value */
00285         attrib->value = irias_new_integer_value(value);
00286         
00287         irias_add_attrib(obj, attrib);
00288 }
00289 
00290  /*
00291  * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
00292  *
00293  *    Add a octet sequence attribute to an LM-IAS object
00294  *
00295  */
00296 
00297 void irias_add_octseq_attrib(struct ias_object *obj, char *name, __u8 *octets,
00298                              int len)
00299 {
00300         struct ias_attrib *attrib;
00301         
00302         ASSERT(obj != NULL, return;);
00303         ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
00304         
00305         ASSERT(name != NULL, return;);
00306         ASSERT(octets != NULL, return;);
00307         
00308         attrib = (struct ias_attrib *) kmalloc(sizeof(struct ias_attrib), 
00309                                                GFP_ATOMIC);
00310         if (attrib == NULL) {
00311                 WARNING(__FUNCTION__ 
00312                         "(), Unable to allocate attribute!\n");
00313                 return;
00314         }
00315         memset(attrib, 0, sizeof( struct ias_attrib));
00316         
00317         attrib->magic = IAS_ATTRIB_MAGIC;
00318         attrib->name = strdup( name);
00319         
00320         attrib->value = irias_new_octseq_value( octets, len);
00321         
00322         irias_add_attrib(obj, attrib);
00323 }
00324 
00325 /*
00326  * Function irias_object_add_string_attrib (obj, string)
00327  *
00328  *    Add a string attribute to an LM-IAS object
00329  *
00330  */
00331 void irias_add_string_attrib(struct ias_object *obj, char *name, char *value)
00332 {
00333         struct ias_attrib *attrib;
00334 
00335         ASSERT(obj != NULL, return;);
00336         ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
00337 
00338         ASSERT(name != NULL, return;);
00339         ASSERT(value != NULL, return;);
00340         
00341         attrib = (struct ias_attrib *) kmalloc(sizeof( struct ias_attrib), 
00342                                                GFP_ATOMIC);
00343         if (attrib == NULL) {
00344                 WARNING(__FUNCTION__ "(), Unable to allocate attribute!\n");
00345                 return;
00346         }
00347         memset(attrib, 0, sizeof( struct ias_attrib));
00348 
00349         attrib->magic = IAS_ATTRIB_MAGIC;
00350         attrib->name = strdup(name);
00351 
00352         attrib->value = irias_new_string_value(value);
00353 
00354         irias_add_attrib(obj, attrib);
00355 }
00356 
00357 /*
00358  * Function irias_new_integer_value (integer)
00359  *
00360  *    Create new IAS integer value
00361  *
00362  */
00363 struct ias_value *irias_new_integer_value(int integer)
00364 {
00365         struct ias_value *value;
00366 
00367         value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
00368         if (value == NULL) {
00369                 WARNING(__FUNCTION__ "(), Unable to kmalloc!\n");
00370                 return NULL;
00371         }
00372         memset(value, 0, sizeof(struct ias_value));
00373 
00374         value->type = IAS_INTEGER;
00375         value->len = 4;
00376         value->t.integer = integer;
00377 
00378         return value;
00379 }
00380 
00381 /*
00382  * Function irias_new_string_value (string)
00383  *
00384  *    Create new IAS string value
00385  *
00386  */
00387 struct ias_value *irias_new_string_value(char *string)
00388 {
00389         struct ias_value *value;
00390 
00391         value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
00392         if (value == NULL) {
00393                 WARNING(__FUNCTION__ "(), Unable to kmalloc!\n");
00394                 return NULL;
00395         }
00396         memset( value, 0, sizeof( struct ias_value));
00397 
00398         value->type = IAS_STRING;
00399         value->charset = CS_ASCII;
00400         value->len = strlen(string);
00401         value->t.string = strdup(string);
00402 
00403         return value;
00404 }
00405 
00406 
00407 /*
00408  * Function irias_new_octseq_value (octets, len)
00409  *
00410  *    Create new IAS octet-sequence value
00411  *
00412  */
00413 struct ias_value *irias_new_octseq_value(__u8 *octseq , int len)
00414 {
00415         struct ias_value *value;
00416 
00417         value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
00418         if (value == NULL) {
00419                 WARNING(__FUNCTION__ "(), Unable to kmalloc!\n");
00420                 return NULL;
00421         }
00422         memset(value, 0, sizeof(struct ias_value));
00423 
00424         value->type = IAS_OCT_SEQ;
00425         value->len = len;
00426 
00427         value->t.oct_seq = kmalloc(len, GFP_ATOMIC);
00428         if (value->t.oct_seq == NULL){
00429                 WARNING(__FUNCTION__"(), Unable to kmalloc!\n");
00430                 return NULL;
00431         }
00432         memcpy(value->t.oct_seq, octseq , len);
00433         return value;
00434 }
00435 
00436 struct ias_value *irias_new_missing_value(void)
00437 {
00438         struct ias_value *value;
00439 
00440         value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
00441         if (value == NULL) {
00442                 WARNING(__FUNCTION__ "(), Unable to kmalloc!\n");
00443                 return NULL;
00444         }
00445         memset(value, 0, sizeof(struct ias_value));
00446 
00447         value->type = IAS_MISSING;
00448         value->len = 0;
00449 
00450         return value;
00451 }
00452 
00453 /*
00454  * Function irias_delete_value (value)
00455  *
00456  *    Delete IAS value
00457  *
00458  */
00459 void irias_delete_value(struct ias_value *value)
00460 {
00461         IRDA_DEBUG(4, __FUNCTION__ "()\n");
00462 
00463         ASSERT(value != NULL, return;);
00464 
00465         switch (value->type) {
00466         case IAS_INTEGER: /* Fallthrough */
00467         case IAS_MISSING:
00468                 /* No need to deallocate */
00469                 break;
00470         case IAS_STRING:
00471                 /* If string, deallocate string */
00472                 if (value->t.string != NULL)
00473                         kfree(value->t.string);
00474                 break;
00475         case IAS_OCT_SEQ:
00476                 /* If byte stream, deallocate byte stream */
00477                  if (value->t.oct_seq != NULL)
00478                          kfree(value->t.oct_seq);
00479                  break;
00480         default:
00481                 IRDA_DEBUG(0, __FUNCTION__ "(), Unknown value type!\n");
00482                 break;
00483         }
00484         kfree(value);
00485 }
00486 
00487 
00488