Merge branch 'test_user_defined_device_optional'

* test_user_defined_device_optional:
  Fix double malloc
  Allow device.optional=true to tolerate missing device
This commit is contained in:
Philippe Teuwen 2013-01-20 15:36:29 +01:00
commit 532b30a2cd
4 changed files with 47 additions and 5 deletions

View file

@ -118,6 +118,16 @@ conf_keyvalue_context(void *data, const char *key, const char *value)
context->user_defined_device_count++;
}
strcpy(context->user_defined_devices[context->user_defined_device_count - 1].connstring, value);
} else if (strcmp(key, "device.optional") == 0) {
if ((context->user_defined_device_count == 0) || context->user_defined_devices[context->user_defined_device_count - 1].optional) {
if (context->user_defined_device_count >= MAX_USER_DEFINED_DEVICES) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Configuration exceeded maximum user-defined devices.");
return;
}
context->user_defined_device_count++;
}
if ((strcmp(value, "true") == 0) || (strcmp(value, "True") == 0) || (strcmp(value, "1") == 0)) //optional
context->user_defined_devices[context->user_defined_device_count - 1].optional = true;
} else {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO, "Unknown key in config line: %s = %s", key, value);
}

View file

@ -77,6 +77,7 @@ nfc_context_new(void)
for (int i = 0; i < MAX_USER_DEFINED_DEVICES; i++) {
strcpy(res->user_defined_devices[i].name, "");
strcpy(res->user_defined_devices[i].connstring, "");
res->user_defined_devices[i].optional = false;
}
res->user_defined_device_count = 0;

View file

@ -157,6 +157,7 @@ struct nfc_driver {
struct nfc_user_defined_device {
char name[DEVICE_NAME_LENGTH];
nfc_connstring connstring;
bool optional;
};
/**

View file

@ -185,7 +185,7 @@ nfc_open(nfc_context *context, const nfc_connstring connstring)
continue;
}
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Unable to open \"%s\".", ncs);
return pnd;
return NULL;
}
for (uint32_t i = 0; i > context->user_defined_device_count; i++) {
if (strcmp(ncs, context->user_defined_devices[i].connstring) == 0) {
@ -244,10 +244,40 @@ nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_
// Load manually configured devices (from config file and env variables)
// TODO From env var...
for (uint32_t i = 0; i < context->user_defined_device_count; i++) {
strcpy((char *)(connstrings + device_found), context->user_defined_devices[i].connstring);
device_found++;
if (device_found >= connstrings_len)
return device_found;
if (context->user_defined_devices[i].optional) {
// let's make sure the device exists
nfc_device *pnd = NULL;
char *env_log_level = getenv("LIBNFC_LOG_LEVEL");
char *old_env_log_level = NULL;
// do it silently
if (env_log_level) {
if ((old_env_log_level = malloc(strlen(env_log_level)+1)) == NULL)
exit(EXIT_FAILURE);
strcpy(old_env_log_level, env_log_level);
}
setenv("LIBNFC_LOG_LEVEL", "0", 1);
pnd = nfc_open(context, context->user_defined_devices[i].connstring);
if (old_env_log_level) {
setenv("LIBNFC_LOG_LEVEL", old_env_log_level, 1);
free(old_env_log_level);
} else {
unsetenv("LIBNFC_LOG_LEVEL");
}
if (pnd) {
nfc_close(pnd);
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "User device %s found", context->user_defined_devices[i].name);
strcpy((char *)(connstrings + device_found), context->user_defined_devices[i].connstring);
device_found ++;
if (device_found == connstrings_len)
break;
}
} else {
// manual choice is not marked as optional so let's take it blindly
strcpy((char *)(connstrings + device_found), context->user_defined_devices[i].connstring);
device_found++;
if (device_found >= connstrings_len)
return device_found;
}
}
// Device auto-detection