Commit 4ac38223 authored by Ivan Tyagov's avatar Ivan Tyagov

Each coupler is now identified by a 12 bytest long UUID.

parent e9c18213
...@@ -26,28 +26,48 @@ ...@@ -26,28 +26,48 @@
#include "common.h" #include "common.h"
#include "mod_io_i2c.h" #include "mod_io_i2c.h"
#include "mod_io_opc_ua.h" #include "mod_io_opc_ua.h"
#include <time.h>
// The default port of OPC-UA server // The default port of OPC-UA server
const int DEFAULT_OPC_UA_PORT = 4840; const int DEFAULT_OPC_UA_PORT = 4840;
const int DEFAULT_MODE = 0; const int DEFAULT_MODE = 0;
char *randomString(size_t length)
{
static char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
char *randomString = NULL;
if (length) {
randomString = malloc(sizeof(char) * (length +1));
if (randomString) {
for (int n = 0;n < length;n++) {
int key = rand() % (int)(sizeof(charset) -1);
randomString[n] = charset[key];
}
randomString[length] = '\0';
}
}
return randomString;
}
// CLI arguments handling // CLI arguments handling
const char *argp_program_version = "OSIE OPC-UA coupler 0.0.1"; const char *argp_program_version = "OSIE OPC-UA coupler 0.0.1";
const char *argp_program_bug_address = "ivan@nexedi.com"; const char *argp_program_bug_address = "ivan@nexedi.com";
static char doc[] = "OPC-UA server which controls MOD-IO's relays' state over OPC-UA protocol."; static char doc[] = "OPC-UA server which controls MOD-IO's relays' state over OPC-UA protocol.";
static char args_doc[] = "..."; static char args_doc[] = "...";
static struct argp_option options[] = { static struct argp_option options[] = {
{"port", 'p', "4840", 0, "Port to bind to."}, {"port", 'p', "4840", 0, "Port to bind to."},
{"device", 'd', "/dev/i2c-1", 0, "Linux block device path."}, {"device", 'd', "/dev/i2c-1", 0, "Linux block device path."},
{"slave-address-list", 's', "0x58", 0, "Comma separated list of slave I2C addresses."}, {"slave-address-list", 's', "0x58", 0, "Comma separated list of slave I2C addresses."},
{"mode", 'm', "0", 0, "Set different modes of operation of coupler. Default (0) is set attached \ {"mode", 'm', "0", 0, "Set different modes of operation of coupler. Default (0) is set attached \
I2C's state state. Virtual (1) which does NOT set any I2C slaves' state."}, I2C's state state. Virtual (1) which does NOT set any I2C slaves' state."},
{"username", 'u', "", 0, "Username."}, {"username", 'u', "", 0, "Username."},
{"password", 'w', "", 0, "Password."}, {"password", 'w', "", 0, "Password."},
{"key", 'k', "", 0, "x509 key."}, {"key", 'k', "", 0, "x509 key."},
{"certificate", 'c', "", 0, "X509 certificate."}, {"certificate", 'c', "", 0, "X509 certificate."},
{0} {"uuid", 'i', "", 0, "UUID of coupler"},
{0}
}; };
struct arguments struct arguments
...@@ -60,41 +80,45 @@ struct arguments ...@@ -60,41 +80,45 @@ struct arguments
char *password; char *password;
char *key; char *key;
char *certificate; char *certificate;
char *uuid;
}; };
static error_t parse_opt(int key, char *arg, struct argp_state *state) static error_t parse_opt(int key, char *arg, struct argp_state *state)
{ {
struct arguments *arguments = state->input; struct arguments *arguments = state->input;
switch (key) { switch (key) {
case 'p': case 'p':
arguments->port = arg ? atoi (arg) : DEFAULT_OPC_UA_PORT; arguments->port = arg ? atoi (arg) : DEFAULT_OPC_UA_PORT;
break;
case 'd':
arguments->device = arg;
break;
case 's':
arguments->slave_address_list = arg;
break; break;
case 'd': case 'm':
arguments->device = arg; arguments->mode = arg ? atoi (arg) : DEFAULT_MODE;
break; break;
case 's': case 'u':
arguments->slave_address_list = arg; arguments->username = arg;
break; break;
case 'm': case 'w':
arguments->mode = arg ? atoi (arg) : DEFAULT_MODE; arguments->password = arg;
break; break;
case 'u': case 'c':
arguments->username = arg; arguments->certificate = arg;
break; break;
case 'w': case 'k':
arguments->password = arg; arguments->key = arg;
break; break;
case 'c': case 'i':
arguments->certificate = arg; arguments->uuid = arg;
break; break;
case 'k': case ARGP_KEY_ARG:
arguments->key = arg;
break;
case ARGP_KEY_ARG:
return 0; return 0;
default: default:
return ARGP_ERR_UNKNOWN; return ARGP_ERR_UNKNOWN;
} }
return 0; return 0;
} }
...@@ -125,13 +149,22 @@ int main(int argc, char **argv) ...@@ -125,13 +149,22 @@ int main(int argc, char **argv)
arguments.password = ""; arguments.password = "";
arguments.key = ""; arguments.key = "";
arguments.certificate = ""; arguments.certificate = "";
arguments.uuid = "";
argp_parse(&argp, argc, argv, 0, 0, &arguments); argp_parse(&argp, argc, argv, 0, 0, &arguments);
// if no uuid define, generate a random one
if (!strlen(arguments.uuid)) {
arguments.uuid = randomString(12);
}
printf("Mode=%d\n", arguments.mode); printf("Mode=%d\n", arguments.mode);
printf("Listening port=%d\n", arguments.port); printf("Listening port=%d\n", arguments.port);
printf("Block device=%s\n", arguments.device); printf("Block device=%s\n", arguments.device);
printf("Slave address list=%s\n", arguments.slave_address_list); printf("Slave address list=%s\n", arguments.slave_address_list);
printf("key=%s\n", arguments.key); printf("key=%s\n", arguments.key);
printf("certificate=%s\n", arguments.certificate); printf("certificate=%s\n", arguments.certificate);
printf("UUID=%s\n", arguments.uuid);
// transfer to global variables (CLI input) // transfer to global variables (CLI input)
I2C_VIRTUAL_MODE = arguments.mode; I2C_VIRTUAL_MODE = arguments.mode;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment