Configuration via command line

This commit is contained in:
Thomas Kolb 2018-06-23 19:42:19 +02:00
parent 8790cf6a5e
commit 3afbd516b4

View file

@ -4,6 +4,8 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include "sk6812.h" #include "sk6812.h"
#include "logger.h" #include "logger.h"
@ -12,17 +14,78 @@
#define PORT 2703 #define PORT 2703
#define NUM_MODULES 300 #define NUM_MODULES 60
#define GPIO_IDX 961 #define GPIO_IDX 960
#define BASE_ADDR ((void*)0x40010000U) #define BASE_ADDR ((void*)0x40000000U)
int main(void) struct CmdLineArgs {
uint16_t port;
void *base_addr;
uint32_t gpio_idx;
uint32_t num_modules;
};
int parse_args(int argc, char **argv, struct CmdLineArgs *args)
{
int opt;
unsigned long tmp_ul;
unsigned long long tmp_ull;
while((opt = getopt(argc, argv, "p:a:g:n:")) != -1) {
switch(opt) {
case 'p': // port
tmp_ul = strtoul(optarg, NULL, 0);
if(tmp_ul < 1 || tmp_ul > 65535) {
LOG(LVL_ERR, "Port number is out of range [1..65535]: %ul", tmp_ul);
return -1;
}
args->port = (uint16_t)tmp_ul;
break;
case 'a': // base address
tmp_ull = strtoull(optarg, NULL, 0);
args->base_addr = (void*)tmp_ull;
break;
case 'g': // gpio
args->gpio_idx = strtoul(optarg, NULL, 0);
break;
case 'n': // number of modules
args->num_modules = strtoul(optarg, NULL, 0);
break;
default:
return -1;
}
}
return 0;
}
int main(int argc, char **argv)
{ {
struct sk6812_ctx ctx; struct sk6812_ctx ctx;
// initialize logger struct CmdLineArgs args;
// initialize logger
logger_init(); logger_init();
// default arguments
args.port = PORT;
args.base_addr = BASE_ADDR;
args.gpio_idx = GPIO_IDX;
args.num_modules = NUM_MODULES;
// parse command line arguments
if(parse_args(argc, argv, &args) == -1) {
LOG(LVL_FATAL, "Error while parsing command line arguments!\n\n"
"Usage: %s [-p port] [-a base_address] [-g gpio_index] [-n number_of_modules]\n\n", argv[0]);
return 1;
}
// initialise the UDP server // initialise the UDP server
if(udpproto_init(PORT) == -1) { if(udpproto_init(PORT) == -1) {
LOG(LVL_FATAL, "Could not initialize the UDP server."); LOG(LVL_FATAL, "Could not initialize the UDP server.");