devicelist.c

Go to the documentation of this file.
00001 /*
00002  * File Name: devicelist.c
00003  */
00004 
00005 /*
00006  * This file is part of sysd.
00007  *
00008  * sysd is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * sysd is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  *
00026  * Parts used from halevt, Copyright (C) 2007  Patrice Dumas <pertusus at free dot fr>
00027  */
00028 
00029 //----------------------------------------------------------------------------
00030 // Include Files
00031 //----------------------------------------------------------------------------
00032 
00033 // system include files, between < >
00034 #include <glib.h>
00035 #include <hal/libhal.h>
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 
00040 // ereader include files, between < >
00041 
00042 // local include files, between " "
00043 #include "devicelist.h"
00044 #include "hal.h"
00045 #include "log.h"
00046 
00047 //----------------------------------------------------------------------------
00048 // Type Declarations
00049 //----------------------------------------------------------------------------
00050 
00051 
00052 //----------------------------------------------------------------------------
00053 // Constants
00054 //----------------------------------------------------------------------------
00055 
00056 
00057 //----------------------------------------------------------------------------
00058 // Static Variables
00059 //----------------------------------------------------------------------------
00060 
00061 static hal_device *hal_device_root;
00062 
00063 
00064 //============================================================================
00065 // Local Function Definitions
00066 //============================================================================
00067 
00068 
00069 //============================================================================
00070 // Functions Implementation
00071 //============================================================================
00072 
00073 void hal_free_property_value(char **values)
00074 {
00075     LOGPRINTF("entry");
00076 
00077     char **cur_value = values;
00078     
00079     while ((*cur_value) != NULL)
00080     {
00081         g_free(*cur_value);
00082         cur_value++;
00083     }
00084     g_free(values);
00085     values = NULL;
00086 }
00087 
00088 
00089 void hal_free_device_property(hal_device_property *property)
00090 {
00091     LOGPRINTF("entry");
00092 
00093     g_free(property->key);
00094     property->key = NULL;
00095     hal_free_property_value(property->values);
00096 }
00097 
00098 
00099 void hal_free_device(hal_device *device)
00100 {
00101     LOGPRINTF("entry");
00102 
00103     hal_device_property *property = device->properties;
00104     hal_device_property *previous_property;
00105 
00106     g_free(device->udi);
00107     
00108     while (property != NULL)
00109     {
00110         previous_property = property;
00111         property = property->next;
00112         g_free(previous_property);
00113     }
00114     
00115     g_free(device);
00116     device = NULL;
00117 }
00118 
00119 
00120 hal_device_property *hal_new_device_property(char *key, char **values)
00121 {
00122 //    LOGPRINTF("entry: key %s", key);
00123 
00124     hal_device_property *new_property = g_try_malloc0(sizeof(hal_device_property));
00125     if (new_property == NULL) 
00126     { 
00127         return NULL; 
00128     }
00129     new_property->values = values;
00130     new_property->key = key;
00131     return new_property;
00132 }
00133 
00134 
00135 hal_device *hal_device_list_add_device(LibHalContext *ctx, const char *udi)
00136 {
00137     LOGPRINTF("entry: udi %s", udi);
00138 
00139     DBusError dbus_error;
00140     LibHalPropertySet *pset = NULL;
00141     LibHalPropertySetIterator it;
00142     hal_device *device = NULL;
00143     char *key = NULL;
00144     char **value;
00145     hal_device_property *new_property = NULL;
00146 
00147     dbus_error_init(&dbus_error);
00148     
00149     device = g_try_malloc0(sizeof(hal_device));
00150     if (device == NULL) 
00151     { 
00152         goto oom;
00153     }
00154     device->properties = NULL;
00155 
00156     pset = libhal_device_get_all_properties(ctx, udi, &dbus_error);
00157     hal_check_dbus_error(&dbus_error);
00158     if (pset == NULL)
00159     {
00160         goto oom;
00161     }
00162 
00163     for (libhal_psi_init(&it, pset); libhal_psi_has_more(&it); libhal_psi_next(&it))
00164     {
00165         LibHalPropertyType type = libhal_psi_get_type(&it);
00166         key = g_strdup(libhal_psi_get_key(&it));
00167         if (key == NULL) 
00168         { 
00169             goto oom; 
00170         }
00171         
00172         value = hal_get_iterator_value(type, &it);
00173         if (value == NULL) 
00174         { 
00175             g_free(key);
00176             goto oom; 
00177         }
00178 
00179         new_property = hal_new_device_property(key, value);
00180         if (new_property == NULL) 
00181         { 
00182             hal_free_property_value(value);
00183             g_free(key);
00184             goto oom; 
00185         }
00186         new_property->next = device->properties;
00187         device->properties = new_property;
00188     }
00189     
00190     device->next = hal_device_root;
00191     device->udi = g_strdup(udi);
00192     if (device->udi == NULL) 
00193     { 
00194         goto oom;
00195     }
00196     hal_device_root = device;
00197     
00198     libhal_free_property_set(pset); 
00199     
00200     return device;
00201 
00202 oom:
00203     LOGPRINTF("No property found for %s (or oom)", udi);
00204 
00205     if (device) 
00206         hal_free_device(device);
00207     if (pset) 
00208         libhal_free_property_set(pset); 
00209     return NULL;
00210 }
00211 
00212 
00213 gboolean hal_device_list_remove_device(const char *udi)
00214 {
00215     LOGPRINTF("entry: udi %s", udi);
00216 
00217     g_return_val_if_fail(udi!=NULL, FALSE);
00218 
00219     hal_device *previous = hal_device_root;
00220     hal_device *device = hal_device_root;
00221 
00222     // no device 
00223     if (device == NULL) 
00224     {
00225         return FALSE; 
00226     }
00227 
00228     LOGPRINTF("Removing %s", udi);
00229 
00230     // device is the first one 
00231     if (device->udi && (strcmp(device->udi, udi)==0))
00232     {
00233         hal_device_root = device->next;
00234         hal_free_device(device);
00235         return TRUE;
00236     }
00237 
00238     while (device != NULL)
00239     {
00240         if (device->udi && (strcmp(device->udi, udi)==0))
00241         {
00242             previous->next = device->next;
00243             hal_free_device(device);
00244             return TRUE;
00245         }
00246         previous = device;
00247         device = device->next;
00248     }
00249     return FALSE;
00250 }
00251 
00252 
00253 hal_device *hal_device_list_find_device(const char *udi)
00254 {
00255     LOGPRINTF("entry: udi %s", udi);
00256     
00257     g_return_val_if_fail(udi!=NULL, NULL);
00258 
00259     hal_device *device = hal_device_root;
00260     while (device != NULL)
00261     {
00262         if (device->udi && (strcmp(device->udi, udi)==0)) 
00263         { 
00264             return device; 
00265         }
00266         device = device->next;
00267     }
00268     return NULL;
00269 }
00270 
00271 
00272 hal_device_property *hal_device_list_get_property(const char *key, const hal_device *device)
00273 {
00274 //    LOGPRINTF("entry: key %s", key);
00275     
00276     g_return_val_if_fail(key!=NULL, NULL);
00277     
00278     if (device == NULL)
00279     {
00280         return NULL;
00281     }
00282 
00283     hal_device_property *property = device->properties;
00284     while (property != NULL)
00285     {
00286         if (property->key && (strcmp(property->key, key)==0))
00287         {
00288             LOGPRINTF("entry found: key %s, property %p, %p, %s", key, property->values, *property->values, *property->values);
00289             return property;
00290         }
00291         property = property->next;
00292     }
00293     return NULL;
00294 }
00295 
00296 
00297 hal_device *hal_device_list_get_root()
00298 {
00299     return hal_device_root;
00300 }
00301 
00302  
00303 gboolean hal_device_list_set_property(const char *udi, const char *key)
00304 {
00305     LOGPRINTF("entry: udi %s", udi);
00306 
00307     hal_device *device = hal_device_list_find_device(udi);
00308     hal_device_property *property;
00309 
00310     char **values;
00311 
00312     if (device == NULL) 
00313     {
00314         return FALSE; 
00315     }
00316 
00317     values = hal_property_value(key, udi, NULL);
00318     if (values == NULL) 
00319     {
00320         return FALSE; 
00321     }
00322 
00323     property = hal_device_list_get_property(key, device);
00324     if (property != NULL)
00325     {
00326         hal_free_property_value(property->values);
00327         property->values = values;
00328     }
00329     else
00330     {
00331         char *new_key = g_strdup(key);
00332         if (new_key == NULL)
00333         {
00334             hal_free_property_value(values);
00335             return FALSE; 
00336         }
00337         property = hal_new_device_property(new_key, values);
00338         if (property == NULL) 
00339         {
00340             hal_free_property_value(values);
00341             g_free(new_key);
00342             return FALSE; 
00343         }
00344         property->next = device->properties;
00345         device->properties = property;
00346     }
00347     return TRUE;
00348 }
00349 
00350 
00351 gboolean hal_device_list_remove_property(const char *udi, const char *key)
00352 {
00353     LOGPRINTF("entry: udi %s", udi);
00354 
00355     hal_device *device = hal_device_list_find_device(udi);
00356     if (device == NULL || device->properties == NULL) 
00357     { 
00358         return FALSE; 
00359     }
00360 
00361     hal_device_property *property = device->properties;
00362     // first property 
00363     if (property->key && (strcmp(property->key, key)==0))
00364     {
00365         device->properties = property->next;
00366         hal_free_device_property(property);
00367     }
00368     
00369     hal_device_property *previous = property;
00370 
00371     while (property != NULL)
00372     {
00373         if (property->key && (strcmp(property->key, key)==0))
00374         {
00375             previous->next = property->next;
00376             hal_free_device_property(property);
00377             return TRUE;
00378         }
00379         previous = property;
00380         property = property->next;
00381     }
00382     return FALSE;
00383 }
00384 
00385 
00386 void hal_print_all_devices()
00387 {
00388     hal_device *device = hal_device_root;
00389     while (device != NULL)
00390     {
00391         hal_device_print(device->udi);
00392         device = device->next;
00393     }
00394 }
00395 
00396 
00397 int hal_count_devices()
00398 {
00399     int nr = 0;
00400     hal_device *device = hal_device_root;
00401     while (device != NULL)
00402     {
00403         nr++;
00404         device = device->next;
00405     }
00406     return nr;
00407 }
00408 
Generated by  doxygen 1.6.2-20100208