1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/target/hal/Hal.cc
14 #undef ZYPP_BASE_LOGGER_LOGGROUP
15 #define ZYPP_BASE_LOGGER_LOGGROUP "HAL"
16 #include "zypp/base/Logger.h"
18 #include "zypp/target/hal/Hal.h"
22 #include <hal/libhal.h>
29 ///////////////////////////////////////////////////////////////////
31 { /////////////////////////////////////////////////////////////////
32 ///////////////////////////////////////////////////////////////////
34 { /////////////////////////////////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////
37 { /////////////////////////////////////////////////////////////////
40 ///////////////////////////////////////////////////////////////////
42 // CLASS NAME : Hal::Impl
44 /** Hal implementation. */
48 * pointer to dbus connection
50 DBusConnection *_connection;
53 * pointer to complete hal context
55 LibHalContext *_context;
58 * report HAL error and reset the error condition
61 report_error (const std::string & reason, DBusError & error) const
63 ERR << reason << ": " << string (error.name) << ": " << string(error.message);
64 dbus_error_init(&error);
72 dbus_error_init (&error);
74 _connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error); // get shared connection to DBUS 'socket'
75 //_connection = dbus_bus_get_private (DBUS_BUS_SYSTEM, &error); // get private connection DBUS 'socket'
77 _context = libhal_ctx_new (); // create empty HAL context
79 if (libhal_ctx_set_dbus_connection (_context, _connection)) { // connect to HAL daemon via DBUS
80 if (libhal_ctx_init (_context, &error)) { // fill HAL context
83 report_error ("libhal_ctx_init", error);
86 report_error ("libhal_ctx_set_dbus_connection", error);
88 libhal_ctx_free (_context); // clean up
91 report_error ("libhal_ctx_new: Can't create libhal context", error);
93 // dbus_connection_close (_connection); // call only if dbus_bus_get_private was used
94 dbus_connection_unref (_connection);
97 report_error ("dbus_bus_get", error);
106 libhal_ctx_free (_context);
109 // dbus_connection_close (_connection); // call only if dbus_bus_get_private was used
110 dbus_connection_unref (_connection);
115 * query for HAL capability present
118 bool query( const std::string & cap_r ) const
119 { return query( cap_r, Rel::ANY, std::string() ); }
122 * query for HAL capability having a specific value
124 bool query( const std::string & cap_r,
126 const std::string & val_r ) const
129 dbus_error_init (&error);
131 // ask HAL which devices provide the needed capability
136 char **device_names = libhal_find_device_by_capability (_context, cap_r.c_str(), &device_count, &error);
138 if (device_names == NULL) {
139 report_error ("libhal_find_device_by_capability", error);
142 else if (device_count > 0) {
144 #if 0 // once we get a capabilities value from HAL, we can compare it ...
147 int cmp = (lhs != rhs) ? ((lhs < rhs) ? -1 : 1) : 0;
149 switch ( relation.inSwitch() )
176 // We shouldn't get here.
177 INT << "Unknown relational opertor '" << relation << "' treated as 'NONE'" << endl;
187 if (device_names != NULL)
188 libhal_free_string_array (device_names);
194 /** Offer default Impl. */
195 static shared_ptr<Impl> nullimpl()
197 static shared_ptr<Impl> _nullimpl( new Impl );
201 }; // struct Hal::Impl
207 bool query( const std::string & cap_r ) const
208 { return query( cap_r, Rel::ANY, std::string() ); }
209 bool query( const std::string & cap_r,
211 const std::string & val_r ) const
213 /** Offer default Impl. */
214 static shared_ptr<Impl> nullimpl()
216 static shared_ptr<Impl> _nullimpl( new Impl );
222 ///////////////////////////////////////////////////////////////////
224 /** \relates Hal::Impl Stream output
225 * And maybe std::ostream & operator<< Hal::Impl below too.
226 * return libhal version or something like that.
228 inline std::ostream & operator<<( std::ostream & str, const Hal::Impl & obj )
230 return str << "Hal::Impl";
233 ///////////////////////////////////////////////////////////////////
237 ///////////////////////////////////////////////////////////////////
239 ///////////////////////////////////////////////////////////////////
241 // METHOD NAME : Hal::Hal
242 // METHOD TYPE : Ctor
245 : _pimpl( Impl::nullimpl() )
248 ///////////////////////////////////////////////////////////////////
250 // METHOD NAME : Hal::~Hal
251 // METHOD TYPE : Dtor
256 ///////////////////////////////////////////////////////////////////
258 // METHOD NAME : Hal::instance
259 // METHOD TYPE : Hal &
261 Hal & Hal::instance()
263 static Hal _singleton;
267 ///////////////////////////////////////////////////////////////////
268 // Foreward to implenemtation
269 ///////////////////////////////////////////////////////////////////
271 bool Hal::query( const std::string & cap_r ) const
272 { return _pimpl->query( cap_r ); }
274 bool Hal::query( const std::string & cap_r,
276 const std::string & val_r ) const
277 { return _pimpl->query( cap_r, op_r, val_r ); }
279 /******************************************************************
281 ** FUNCTION NAME : operator<<
282 ** FUNCTION TYPE : std::ostream &
284 std::ostream & operator<<( std::ostream & str, const Hal & obj )
286 return str << *obj._pimpl;
289 /////////////////////////////////////////////////////////////////
291 ///////////////////////////////////////////////////////////////////
292 /////////////////////////////////////////////////////////////////
293 } // namespace target
294 ///////////////////////////////////////////////////////////////////
295 /////////////////////////////////////////////////////////////////
297 ///////////////////////////////////////////////////////////////////