Enlightenment CVS committal Author : englebass Project : e17 Module : proto
Dir : e17/proto/e_dbus/src/lib/nm Modified Files: E_Nm.h Makefile.am e_nm_device.c e_nm_manager.c e_nm_private.h e_nm_util.c Added Files: e_nm_network.c Log Message: Add patch from Ross Vandegrift and convert nm to use E_DBus_Callback_Func =================================================================== RCS file: /cvs/e/e17/proto/e_dbus/src/lib/nm/E_Nm.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -3 -r1.1 -r1.2 --- E_Nm.h 21 Mar 2007 10:31:16 -0000 1.1 +++ E_Nm.h 27 Jul 2007 20:34:40 -0000 1.2 @@ -21,9 +21,30 @@ * For method calls, the return structs use the following naming convention: * E_NM_<Interface>_<Method_Call_Name>_Return */ -typedef void (*E_NM_Callback_Func) (void *user_data, void *method_return, DBusError *error); /* org.freedesktop.NetworkManager api */ -int e_nm_get_devices(E_NM_Context *ctx, E_NM_Callback_Func cb_func, void *data); -int e_nm_sleep(E_NM_Context *ctx, E_NM_Callback_Func cb_func, void *data, int do_sleep); +int e_nm_get_devices(E_NM_Context *ctx, E_DBus_Callback_Func cb_func, void *data); +int e_nm_sleep(E_NM_Context *ctx, E_DBus_Callback_Func cb_func, void *data, int do_sleep); + + +/* org.freedesktop.NetworkManager.Device api */ +int e_nm_device_get_name(E_NM_Context *ctx, const char *device, + E_DBus_Callback_Func cb_func, void *data); +int e_nm_device_get_type(E_NM_Context *ctx, const char *device, + E_DBus_Callback_Func cb_func, void *data); +int e_nm_device_get_hal_udi(E_NM_Context *ctx, const char *device, + E_DBus_Callback_Func cb_func, void *data); +int e_nm_device_get_ip4_address(E_NM_Context *ctx, const char *device, + E_DBus_Callback_Func cb_func, void *data); +int e_nm_device_get_link_active(E_NM_Context *ctx, const char *device, + E_DBus_Callback_Func cb_func, void *data); +int e_nm_device_wireless_get_strength(E_NM_Context *ctx, const char *device, + E_DBus_Callback_Func cb_func, void *data); +int e_nm_device_wireless_get_active_network(E_NM_Context *ctx, + const char *device, + E_DBus_Callback_Func cb_func, + void *data); +int e_nm_device_wireless_get_networks(E_NM_Context *ctx, const char *device, + E_DBus_Callback_Func cb_func, void *data); #endif + =================================================================== RCS file: /cvs/e/e17/proto/e_dbus/src/lib/nm/Makefile.am,v retrieving revision 1.5 retrieving revision 1.6 diff -u -3 -r1.5 -r1.6 --- Makefile.am 22 Mar 2007 07:47:37 -0000 1.5 +++ Makefile.am 27 Jul 2007 20:34:40 -0000 1.6 @@ -12,6 +12,7 @@ e_nm.c \ e_nm_manager.c \ e_nm_device.c \ +e_nm_network.c \ e_nm_util.c =================================================================== RCS file: /cvs/e/e17/proto/e_dbus/src/lib/nm/e_nm_device.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -3 -r1.2 -r1.3 --- e_nm_device.c 22 Mar 2007 19:52:59 -0000 1.2 +++ e_nm_device.c 27 Jul 2007 20:34:40 -0000 1.3 @@ -1,72 +1,159 @@ +/* + * This file defines functions that query each of the functions provided by + * the NetworkManager Device interface. + */ + #include "E_Nm.h" #include "e_nm_private.h" #include <Ecore_Data.h> -#define e_nm_device_call_new(path, member) dbus_message_new_method_call(E_NM_SERVICE, path, E_NM_INTERFACE_DEVICE, member) -#define e_nm_device_wired_call_new(path, member) dbus_message_new_method_call(E_NM_SERVICE, path, E_NM_INTERFACE_DEVICE_WIRED, member) -#define e_nm_device_wireless_call_new(path, member) dbus_message_new_method_call(E_NM_SERVICE, path, E_NM_INTERFACE_DEVICE_WIRELESS, member) +/** + * Get the system name of a NetworkManager device + * + * Returns an Ecore_List of dbus object paths for network devices. This list is + * of const char *, and is freed automatically after the callback returns. + * + * @param ctx an e_nm context + * @param device a NetworkManager device to communicate with + * @param cb a callback, used when the method returns (or an error is received) + * @param data user data to pass to the callback function + */ +int +e_nm_device_get_name(E_NM_Context *ctx, const char *device, + E_DBus_Callback_Func cb_func, void *data) +{ + return e_nm_get_from_device(ctx, device, cb_func, data, "getName", + DBUS_TYPE_STRING); +} +/** + * Return the type of a an NM device: + * + * 0: unknown + * 1: wired + * 2: wireless + * + * @param ctx an e_nm context + * @param device a NetworkManager device to communicate with + * @param cb a callback, used when the method returns (or an error is received) + * @param data user data to pass to the callback function + */ int -e_nm_device_deactivate(E_NM_Context *ctx, const char *device, E_NM_Callback_Func cb_func, void *data) +e_nm_device_get_type(E_NM_Context *ctx, const char *device, + E_DBus_Callback_Func cb_func, void *data) { - E_NM_Callback *cb; - DBusMessage *msg; - int ret; + return e_nm_get_from_device(ctx, device, cb_func, data, "getType", + DBUS_TYPE_INT32); +} - cb = e_nm_callback_new(cb_func, data); - msg = e_nm_device_call_new(device, "Deactivate"); - ret = e_dbus_message_send(ctx->conn, msg, cb_nm_generic, -1, cb) ? 1 : 0; - dbus_message_unref(msg); - return ret; + +/** + * Get the HAL UDI of a NetworkManager device + * + * @param ctx an e_nm context + * @param device a NetworkManager device to communicate with + * @param cb a callback, used when the method returns (or an error is received) + * @param data user data to pass to the callback function + */ +int +e_nm_device_get_hal_udi(E_NM_Context *ctx, const char *device, + E_DBus_Callback_Func cb_func, void *data) +{ + return e_nm_get_from_device(ctx, device, cb_func, data, "getHalUdi", + DBUS_TYPE_STRING); } + +/** + * Get the IPv4 address of a NetworkManager device + * + * @param ctx an e_nm context + * @param device a NetworkManager device to communicate with + * @param cb a callback, used when the method returns (or an error is received) + * @param data user data to pass to the callback function + */ int -e_nm_device_wired_activate(E_NM_Context *ctx, const char *device, E_NM_Callback_Func cb_func, void *data, char user_requested) +e_nm_device_get_ip4_address(E_NM_Context *ctx, const char *device, + E_DBus_Callback_Func cb_func, void *data) { - E_NM_Callback *cb; - DBusMessage *msg; - int ret; - dbus_bool_t val; + return e_nm_get_from_device(ctx, device, cb_func, data, "getIP4Address", + DBUS_TYPE_UINT32); +} - val = user_requested; - cb = e_nm_callback_new(cb_func, data); - msg = e_nm_device_wired_call_new(device, "Activate"); - dbus_message_append_args(msg, DBUS_TYPE_BOOLEAN, &val, DBUS_TYPE_INVALID); - ret = e_dbus_message_send(ctx->conn, msg, cb_nm_generic, -1, cb) ? 1 : 0; - dbus_message_unref(msg); - return ret; +/** + * Get the link status of a NetworkManager device + * + * @param ctx an e_nm context + * @param device a NetworkManager device to communicate with + * @param cb a callback, used when the method returns (or an error is received) + * @param data user data to pass to the callback function + */ +int +e_nm_device_get_link_active(E_NM_Context *ctx, const char *device, + E_DBus_Callback_Func cb_func, void *data) +{ + return e_nm_get_from_device(ctx, device, cb_func, data, "getLinkActive", + DBUS_TYPE_BOOLEAN); } + +/** + * Get the signal strength of a the wireless network that a NetworkManager + * device is connected to. + * + * @param ctx an e_nm context + * @param device a NetworkManager device to communicate with + * @param cb a callback, used when the method returns (or an error is received) + * @param data user data to pass to the callback function + */ int -e_nm_device_wireless_get_active_networks(E_NM_Context *ctx, const char *device, E_NM_Callback_Func cb_func, void *data) +e_nm_device_wireless_get_strength(E_NM_Context *ctx, const char *device, + E_DBus_Callback_Func cb_func, void *data) { - E_NM_Callback *cb; - DBusMessage *msg; - int ret; + return e_nm_get_from_device(ctx, device, cb_func, data, "getStrength", + DBUS_TYPE_INT32); +} - cb = e_nm_callback_new(cb_func, data); - msg = e_nm_device_wireless_call_new(device, "GetActiveNetworks"); - ret = e_dbus_message_send(ctx->conn, msg, cb_nm_string_list, -1, cb) ? 1 : 0; - dbus_message_unref(msg); - return ret; + +/** + * Find the NetworkManager device's currently associated wireless network + * + * @param ctx an e_nm context + * @param device a NetworkManager device to communicate with + * @param cb a callback, used when the method returns (or an error is received) + * @param data user data to pass to the callback function + */ +int +e_nm_device_wireless_get_active_network(E_NM_Context *ctx, const char *device, + E_DBus_Callback_Func cb_func, void *data) +{ + return e_nm_get_from_device(ctx, device, cb_func, data, "getActiveNetwork", + DBUS_TYPE_STRING); } + +/** + * Get the list of available wireless networks + * + * Returns an Ecore_List of wireless network names + * + * @param ctx an e_nm context + * @param device a NetworkManager device to communicate with + * @param cb a callback, used when the method returns (or an error is received) + * @param data user data to pass to the callback function + */ int -e_nm_device_wireless_activate(E_NM_Context *ctx, const char *device, E_NM_Callback_Func cb_func, void *data, const char *access_point, char user_requested) +e_nm_device_wireless_get_networks(E_NM_Context *ctx, const char *device, + E_DBus_Callback_Func cb_func, void *data) { - E_NM_Callback *cb; DBusMessage *msg; int ret; - dbus_bool_t val; - val = user_requested; - - cb = e_nm_callback_new(cb_func, data); - msg = e_nm_device_wireless_call_new(device, "Activate"); - dbus_message_append_args(msg, DBUS_TYPE_STRING, &access_point, DBUS_TYPE_BOOLEAN, &val, DBUS_TYPE_INVALID); - ret = e_dbus_message_send(ctx->conn, msg, cb_nm_generic, -1, cb) ? 1 : 0; + msg = e_nm_device_call_new(device, "getNetworks"); + ret = e_dbus_method_call_send(ctx->conn, msg, cb_nm_string_list, cb_func, -1, data) ? 1 : 0; dbus_message_unref(msg); return ret; } + =================================================================== RCS file: /cvs/e/e17/proto/e_dbus/src/lib/nm/e_nm_manager.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -3 -r1.2 -r1.3 --- e_nm_manager.c 22 Mar 2007 19:52:59 -0000 1.2 +++ e_nm_manager.c 27 Jul 2007 20:34:40 -0000 1.3 @@ -1,6 +1,6 @@ #include "E_Nm.h" #include "e_nm_private.h" -#define e_nm_manager_call_new(member) dbus_message_new_method_call(E_NM_SERVICE, E_NM_PATH_NETWORK_MANAGER, E_NM_INTERFACE_NETWORK_MANAGER, member) + /** * Get all network devices. @@ -9,46 +9,68 @@ * of const char *, and is freed automatically after the callback returns. * * @param ctx an e_nm context - * @param cb a callback to call when the method returns (or an error is received) + * @param cb a callback, used when the method returns (or an error is received) * @param data user data to pass to the callback function - */ + **/ int -e_nm_get_devices(E_NM_Context *ctx, E_NM_Callback_Func cb_func, void *data) +e_nm_get_devices(E_NM_Context *ctx, E_DBus_Callback_Func cb_func, void *data) { - E_NM_Callback *cb; DBusMessage *msg; int ret; - cb = e_nm_callback_new(cb_func, data); - msg = e_nm_manager_call_new("GetDevices"); + msg = e_nm_manager_call_new("getDevices"); - ret = e_dbus_message_send(ctx->conn, msg, cb_nm_string_list, -1, cb) ? 1 : 0; + ret = e_dbus_method_call_send(ctx->conn, msg, cb_nm_string_list, cb_func, -1, data) ? 1 : 0; dbus_message_unref(msg); return ret; } + /** - * Sleep or wake up network manager. - * - * The return_data of the callback will be NULL. + * Find the active device that NM has chosen + * + * Returns a single string containing the dbus path to the active device * * @param ctx an e_nm context - * @param cb a callback to call when the method returns (or an error is received) + * @param cb a callback, used when the method returns (or an error is received) * @param data user data to pass to the callback function - */ + **/ int -e_nm_sleep(E_NM_Context *ctx, E_NM_Callback_Func cb_func, void *data, int do_sleep) +e_nm_get_active_device(E_NM_Context *ctx, E_DBus_Callback_Func cb_func, + void *data) { - E_NM_Callback *cb; - DBusMessage *msg; - int ret; - dbus_bool_t var = do_sleep; + return e_nm_get_from_nm(ctx, cb_func, data, + "getActiveDevice", DBUS_TYPE_STRING); +} - cb = e_nm_callback_new(cb_func, data); - msg = e_nm_manager_call_new("Sleep"); - dbus_message_append_args(msg, DBUS_TYPE_BOOLEAN, &var, DBUS_TYPE_INVALID); - ret = e_dbus_message_send(ctx->conn, msg, cb_nm_generic, -1, cb) ? 1 : 0; - dbus_message_unref(msg); - return ret; +/** + * Query the current state of the network + * + * Returns a single string containing the status: + * + * "connecting": there is a pending network connection (waiting for a DHCP + * request to complete, waiting for an encryption + * key/passphrase, waiting for a wireless network, etc) + * + * "connected": there is an active network connection + * + * "scanning": there is no active network connection, but NetworkManager + * is looking for an access point to associate with + * + * "disconnected": there is no network connection + * + * + * + * @param ctx an e_nm context + * @param cb a callback, used when the method returns (or an error is received) + * @param data user data to pass to the callback function + **/ +int +e_nm_status(E_NM_Context *ctx, E_DBus_Callback_Func cb_func, + void *data) +{ + + return e_nm_get_from_nm(ctx, cb_func, data, + "status", DBUS_TYPE_STRING); } =================================================================== RCS file: /cvs/e/e17/proto/e_dbus/src/lib/nm/e_nm_private.h,v retrieving revision 1.2 retrieving revision 1.3 diff -u -3 -r1.2 -r1.3 --- e_nm_private.h 22 Mar 2007 01:44:13 -0000 1.2 +++ e_nm_private.h 27 Jul 2007 20:34:40 -0000 1.3 @@ -5,15 +5,14 @@ #define E_NM_INTERFACE_NETWORK_MANAGER "org.freedesktop.NetworkManager" #define E_NM_PATH_NETWORK_MANAGER "/org/freedesktop/NetworkManager" #define E_NM_INTERFACE_DEVICE "org.freedesktop.NetworkManager.Device" -#define E_NM_INTERFACE_DEVICE_WIRED "org.freedesktop.NetworkManager.Device.Wired" -#define E_NM_INTERFACE_DEVICE_WIRELESS "org.freedesktop.NetworkManager.Device.Wireless" -typedef struct E_NM_Callback E_NM_Callback; -struct E_NM_Callback -{ - E_NM_Callback_Func func; - void *user_data; -}; + +#define e_nm_manager_call_new(member) dbus_message_new_method_call(E_NM_SERVICE, E_NM_PATH_NETWORK_MANAGER, E_NM_INTERFACE_NETWORK_MANAGER, member) + +#define e_nm_device_call_new(path, member) dbus_message_new_method_call(E_NM_SERVICE, path, E_NM_INTERFACE_DEVICE, member) + +#define e_nm_network_call_new(member) dbus_message_new_method_call(E_NM_SERVICE, E_NM_PATH_NETWORK_MANAGER, E_NM_INTERFACE_NETWORK_MANAGER, member) + struct E_NM_Context { @@ -31,9 +30,16 @@ -E_NM_Callback * e_nm_callback_new(E_NM_Callback_Func cb_func, void *user_data); -void e_nm_callback_free(E_NM_Callback *callback); - -void cb_nm_generic(void *data, DBusMessage *msg, DBusError *err); -void cb_nm_string_list(void *data, DBusMessage *msg, DBusError *err); +int e_nm_get_from_nm(E_NM_Context *ctx, E_DBus_Callback_Func cb_func, void *data, + const char *method, int rettype); +int e_nm_get_from_device(E_NM_Context *ctx, const char *device, + E_DBus_Callback_Func cb_func, void *data, + const char *method, int rettype); + +void *cb_nm_generic(DBusMessage *msg, DBusError *err); +void *cb_nm_int32(DBusMessage *msg, DBusError *err); +void *cb_nm_uint32(DBusMessage *msg, DBusError *err); +void *cb_nm_string(DBusMessage *msg, DBusError *err); +void *cb_nm_boolean(DBusMessage *msg, DBusError *err); +void *cb_nm_string_list(DBusMessage *msg, DBusError *err); #endif =================================================================== RCS file: /cvs/e/e17/proto/e_dbus/src/lib/nm/e_nm_util.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -3 -r1.2 -r1.3 --- e_nm_util.c 22 Mar 2007 07:20:04 -0000 1.2 +++ e_nm_util.c 27 Jul 2007 20:34:40 -0000 1.3 @@ -1,54 +1,164 @@ #include "E_Nm.h" #include "e_nm_private.h" +#include "E_DBus.h" #include <string.h> #include <Ecore_Data.h> /** * @internal - * @brief Create a callback structure - * @param cb_func the callback function - * @param user_data data to pass to the callback + * @brief returns an e_dbus callback for a given dbus type + * @param rettype the return type we want to find a callback for + **/ +E_DBus_Unmarshal_Func +e_nm_callback_by_type(int rettype) +{ + switch (rettype) + { + case DBUS_TYPE_STRING: + return cb_nm_string; + + case DBUS_TYPE_INT32: + return cb_nm_int32; + + case DBUS_TYPE_UINT32: + return cb_nm_uint32; + + case DBUS_TYPE_BOOLEAN: + return cb_nm_boolean; + + default: + return cb_nm_generic; + } +} + +/** + * @internal + * @brief Send "get" messages to NetworkManager via e_dbus + * @param ctx an e_nm context + * @param cb a callback, used when the method returns (or an error is received) + * @param data user data to pass to the callback function + * @param method the name of the method that should be called + * @param rettype the type of the data that will be returned to the callback + **/ +int +e_nm_get_from_nm(E_NM_Context *ctx, E_DBus_Callback_Func cb_func, void *data, + const char *method, int rettype) +{ + DBusMessage *msg; + int ret; + + msg = e_nm_manager_call_new(method); + ret = e_dbus_method_call_send(ctx->conn, msg, e_nm_callback_by_type(rettype), + cb_func, -1, data) ? 1 : 0; + dbus_message_unref(msg); + return ret; +} + + +/** + * @internal + * @brief Send "get" messages to a Device via e_dbus + * @param ctx an e_nm context + * @param cb a callback, used when the method returns (or an error is received) + * @param data user data to pass to the callback function + * @param method the name of the method that should be called + * @param rettype the type of the data that will be returned to the callback + **/ +int +e_nm_get_from_device(E_NM_Context *ctx, const char *device, + E_DBus_Callback_Func cb_func, void *data, + const char *method, int rettype) +{ + DBusMessage *msg; + int ret; + + msg = e_nm_device_call_new(device, method); + ret = e_dbus_method_call_send(ctx->conn, msg, e_nm_callback_by_type(rettype), + cb_func, -1, data) ? 1 : 0; + dbus_message_unref(msg); + return ret; +} + +/** + * @internal + * @brief Generic callback for methods that return nothing + */ +void * +cb_nm_generic(DBusMessage *msg, DBusError *err) +{ + return NULL; +} + + +/** + * @internal + * @brief Callback for methods that return DBUS_TYPE_INT32 */ -E_NM_Callback * -e_nm_callback_new(E_NM_Callback_Func cb_func, void *user_data) +void * +cb_nm_int32(DBusMessage *msg, DBusError *err) { - E_NM_Callback *cb; + dbus_int32_t *i; - if (!cb_func) return NULL; + i = malloc(sizeof(dbus_int32_t)); + /* Actually emit the integer */ + dbus_message_get_args(msg, err, + DBUS_TYPE_INT32, i, + DBUS_TYPE_INVALID); - cb = calloc(1, sizeof(E_NM_Callback)); - if (!cb) return NULL; - cb->func = cb_func; - cb->user_data = user_data; - return cb; + return i; } /** * @internal - * @brief Free a callback structure - * @param callback the callback to free + * @brief Callback for methods that return DBUS_TYPE_UINT32 */ -void -e_nm_callback_free(E_NM_Callback *callback) +void * +cb_nm_uint32(DBusMessage *msg, DBusError *err) { - free(callback); + dbus_uint32_t *i; + + i = malloc(sizeof(dbus_uint32_t)); + /* Actually emit the unsigned integer */ + dbus_message_get_args(msg, err, + DBUS_TYPE_UINT32, i, + DBUS_TYPE_INVALID); + + return i; } /** * @internal - * @brief Generic callback for methods that return nothing + * @brief Callback for methods that return DBUS_TYPE_BOOLEAN */ -void -cb_nm_generic(void *data, DBusMessage *msg, DBusError *err) +void * +cb_nm_boolean(DBusMessage *msg, DBusError *err) { - E_NM_Callback *cb; + dbus_bool_t *i; - cb = data; - if (cb->func) - cb->func(cb->user_data, NULL, err); + i = malloc(sizeof(dbus_bool_t)); + /* Actually emit the unsigned integer */ + dbus_message_get_args(msg, err, + DBUS_TYPE_BOOLEAN, i, + DBUS_TYPE_INVALID); + + return i; +} - if (dbus_error_is_set(err)) dbus_error_free(err); - e_nm_callback_free(cb); +/** + * @internal + * @brief Callback for methods returning a single string + */ +void * +cb_nm_string(DBusMessage *msg, DBusError *err) +{ + char *str; + + /* Actually emit the string */ + dbus_message_get_args(msg, err, + DBUS_TYPE_STRING, &str, + DBUS_TYPE_INVALID); + + return str; } @@ -56,30 +166,17 @@ * @internal * @brief Callback for methods returning a list of strings or object paths */ -void -cb_nm_string_list(void *data, DBusMessage *msg, DBusError *err) +void * +cb_nm_string_list(DBusMessage *msg, DBusError *err) { - E_NM_Callback *cb; Ecore_List *devices; DBusMessageIter iter, sub; - cb = data; - if (!cb->func) - goto out; - - if (dbus_error_is_set(err)) - { - cb->func(cb->user_data, NULL, err); - dbus_error_free(err); - goto out; - } - - devices = ecore_list_new(); - dbus_message_iter_init(msg, &iter); if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY || - dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_OBJECT_PATH) goto out; + dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_OBJECT_PATH) return NULL; + devices = ecore_list_new(); dbus_message_iter_recurse(&iter, &sub); while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) { @@ -90,10 +187,6 @@ dbus_message_iter_next(&sub); } - cb->func(cb->user_data, devices, err); - ecore_list_destroy(devices); -out: - e_nm_callback_free(cb); - return; + return devices; } ------------------------------------------------------------------------- This SF.net email is sponsored by: Splunk Inc. Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/ _______________________________________________ enlightenment-cvs mailing list enlightenment-cvs@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/enlightenment-cvs