Removes nfc_get_default_device() function.

The nfc_get_default_device() is not needed anymore since the first element from returned nfc_connstring list of nfc_list_devices() function.
Plus, nfc_open() can be use with NULL nfc_connstring which will select automatically the first available device (ie. the default one).
This commit is contained in:
Romuald Conty 2012-12-04 12:17:56 +01:00
parent 03e5611d14
commit dc949c257e
4 changed files with 15 additions and 38 deletions

3
NEWS
View file

@ -21,6 +21,9 @@ API Changes:
creation, etc.) creation, etc.)
* Functions * Functions
- Remove nfc_get_default_device() function: the default device is now the
first in nfc_list_devices() or could be open using NULL connstring with
nfc_open() function.
- New enum-to-string converter functions str_nfc_modulation_type() and - New enum-to-string converter functions str_nfc_modulation_type() and
str_nfc_baud_rate() str_nfc_baud_rate()
- New str_nfc_target() to convert nfc_target struct into allocated string - New str_nfc_target() to convert nfc_target struct into allocated string

View file

@ -67,7 +67,6 @@ extern "C" {
NFC_EXPORT void nfc_exit(nfc_context *context); NFC_EXPORT void nfc_exit(nfc_context *context);
/* NFC Device/Hardware manipulation */ /* NFC Device/Hardware manipulation */
NFC_EXPORT bool nfc_get_default_device(nfc_connstring *connstring);
NFC_EXPORT nfc_device *nfc_open(nfc_context *context, const nfc_connstring connstring); NFC_EXPORT nfc_device *nfc_open(nfc_context *context, const nfc_connstring connstring);
NFC_EXPORT void nfc_close(nfc_device *pnd); NFC_EXPORT void nfc_close(nfc_device *pnd);
NFC_EXPORT int nfc_abort_command(nfc_device *pnd); NFC_EXPORT int nfc_abort_command(nfc_device *pnd);

View file

@ -80,12 +80,20 @@ nfc_context_new(void)
} }
res->user_defined_device_count = 0; res->user_defined_device_count = 0;
// Load user defined device from environment variable at first
char *envvar = getenv("LIBNFC_DEFAULT_DEVICE");
if (envvar) {
strcpy(res->user_defined_devices[0].name, "user defined default device");
strcpy(res->user_defined_devices[0].connstring, envvar);
res->user_defined_device_count++;
}
// Load options from configuration file (ie. /etc/nfc/libnfc.conf) // Load options from configuration file (ie. /etc/nfc/libnfc.conf)
conf_load(res); conf_load(res);
// Environment variables // Environment variables
// Load "intrusive scan" option // Load "intrusive scan" option
char *envvar = getenv("LIBNFC_INTRUSIVE_SCAN"); envvar = getenv("LIBNFC_INTRUSIVE_SCAN");
string_as_boolean(envvar, &(res->allow_intrusive_scan)); string_as_boolean(envvar, &(res->allow_intrusive_scan));
// log level // log level

View file

@ -132,46 +132,13 @@ nfc_exit(nfc_context *context)
nfc_context_free(context); nfc_context_free(context);
} }
/** @ingroup dev
* @brief Get the defaut NFC device
* @param connstring \a nfc_connstring pointer where the default connection string will be stored
* @return \e true on success
*
* This function fill \e connstring with the LIBNFC_DEFAULT_DEVICE environment variable content
* if is set otherwise it will search for the first available device, and fill
* \e connstring with the corresponding \a nfc_connstring value.
*
* This function returns true when LIBNFC_DEFAULT_DEVICE is set or an available device is found.
*
* @note The \e connstring content can be invalid if LIBNFC_DEFAULT_DEVICE is
* set with incorrect value.
*/
bool
nfc_get_default_device(nfc_connstring *connstring)
{
char *env_default_connstring = getenv("LIBNFC_DEFAULT_DEVICE");
if (NULL == env_default_connstring) {
// LIBNFC_DEFAULT_DEVICE is not set, we fallback on probing for the first available device
nfc_connstring listed_cs[1];
size_t device_found = nfc_list_devices(NULL, listed_cs, 1);
if (device_found) {
strncpy(*connstring, listed_cs[0], sizeof(nfc_connstring));
} else {
return false;
}
} else {
strncpy(*connstring, env_default_connstring, sizeof(nfc_connstring));
}
return true;
}
/** @ingroup dev /** @ingroup dev
* @brief Open a NFC device * @brief Open a NFC device
* @param context The context to operate on, or NULL for the default context. * @param context The context to operate on.
* @param connstring The device connection string if specific device is wanted, \c NULL otherwise * @param connstring The device connection string if specific device is wanted, \c NULL otherwise
* @return Returns pointer to a \a nfc_device struct if successfull; otherwise returns \c NULL value. * @return Returns pointer to a \a nfc_device struct if successfull; otherwise returns \c NULL value.
* *
* If \e connstring is \c NULL, the \a nfc_get_default_device() function is used. * If \e connstring is \c NULL, the first available device from \a nfc_list_devices function is used.
* *
* If \e connstring is set, this function will try to claim the right device using information provided by \e connstring. * If \e connstring is set, this function will try to claim the right device using information provided by \e connstring.
* *
@ -189,7 +156,7 @@ nfc_open(nfc_context *context, const nfc_connstring connstring)
nfc_connstring ncs; nfc_connstring ncs;
if (connstring == NULL) { if (connstring == NULL) {
if (!nfc_get_default_device(&ncs)) { if (!nfc_list_devices(context, &ncs, 1)) {
return NULL; return NULL;
} }
} else { } else {