Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
nonoo committed May 28, 2015
0 parents commit 6e70019
Show file tree
Hide file tree
Showing 46 changed files with 2,069 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
Makefile.config.inc
*-build-*
*.o
*.a
*.dep
*.log
*.cfg
1 change: 1 addition & 0 deletions Makefile
@@ -0,0 +1 @@
include make/Makefile.subdir.inc
6 changes: 6 additions & 0 deletions Makefile.defconfig.inc
@@ -0,0 +1,6 @@
SRCTOPDIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))

# If Makefile.config.inc exists, we include it.
ifeq ($(wildcard $(SRCTOPDIR)/Makefile.config.inc),$(SRCTOPDIR)/Makefile.config.inc)
include $(SRCTOPDIR)/Makefile.config.inc
endif
1 change: 1 addition & 0 deletions build/Makefile
@@ -0,0 +1 @@
include ../make/Makefile.subdir.inc
20 changes: 20 additions & 0 deletions build/node/Makefile
@@ -0,0 +1,20 @@
NPROCS := $(shell nproc)

all:
@echo running $(NPROCS) jobs...
@colormake -j$(NPROCS) -f Makefile.goals
@echo
@echo Success!

install:
@echo running $(NPROCS) jobs...
@colormake -j$(NPROCS) -f Makefile.goals
@colormake -f Makefile.goals install
@echo
@echo Success!

%:
@echo running $(NPROCS) jobs...
@colormake -j$(NPROCS) -f Makefile.goals $@
@echo
@echo Success!
10 changes: 10 additions & 0 deletions build/node/Makefile.goals
@@ -0,0 +1,10 @@
include ../../Makefile.defconfig.inc
export CFLAGS := $(CFLAGS) -DNODE_BUILD
include ../../make/Makefile.target.inc

# Define the lib subdirs which are needed for this build.
LIBS := $(LIBS) base config daemon comm
PREBUILTLIBS := pcap

include ../../make/Makefile.build.inc
include ../../make/Makefile.common.inc
103 changes: 103 additions & 0 deletions build/node/node.c
@@ -0,0 +1,103 @@
#include <config/defaults.h>

#include <libs/base/base.h>
#include <libs/base/log.h>
#include <libs/daemon/console.h>
#include <libs/daemon/daemon.h>
#include <libs/config/config.h>
#include <libs/comm/comm.h>

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

static char *node_configfilename = CONFIGFILENAME;
static flag_t node_daemonize = 1;
static flag_t node_consoleclient = 0;
static char *node_directory = NULL;

static void node_printversion(void) {
console_log("node v%u.%u.%u-a%u ", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, APPID);
console_log(__TIME__ " " __DATE__ " " GITHASH "\n");
}

static void node_processcommandline(int argc, char **argv) {
int c;

while ((c = getopt(argc, argv, "hvfd:s:rc:i")) != -1) {
switch (c) {
case '?': // Unknown option
case 'h':
node_printversion();
console_log("usage:\n");
console_log(" -h - this help\n");
console_log(" -v - version\n");
console_log(" -f - run in foreground\n");
console_log(" -c [file] - use given config file\n");
console_log(" -r - connect console to background process (implies -f)\n");
console_log(" -d [dir] - change current directory on startup\n");
exit(0);
case 'v':
node_printversion();
exit(0);
case 'f':
node_daemonize = 0;
break;
case 'c':
node_configfilename = optarg;
break;
case 'r':
node_daemonize = 0;
node_consoleclient = 1;
break;
case 'd':
node_directory = optarg;
break;
default:
exit(1);
}
}
}

int main(int argc, char *argv[]) {
node_processcommandline(argc, argv);

// Changing the current working directory
if (!daemon_changecwd(node_directory))
return 1;

config_init(node_configfilename);
switch (daemon_init(node_daemonize, node_consoleclient)) {
case DAEMON_INIT_RESULT_FORKED_PARENTEXIT:
return 0;
case DAEMON_INIT_RESULT_FORK_ERROR:
case DAEMON_INIT_RESULT_CONSOLECLIENT_ERROR:
return 1;
default:
break;
}
if (!daemon_is_consoleclient()) {
base_init();
comm_init();
}

console_log("\n");
node_printversion();
console_log("*** ready.\n");

while (daemon_process()) {
if (!daemon_is_consoleclient()) {
base_process();
comm_process();
}
}

if (!daemon_is_consoleclient()) {
comm_deinit();
base_deinit();
}
daemon_deinit();
config_deinit();

return 0;
}
9 changes: 9 additions & 0 deletions config/app/default.h
@@ -0,0 +1,9 @@
#ifndef APP_H_
#define APP_H_

#define APPID APPID_DEFAULT

#define CONFIGFILENAME "default.cfg"
#define CONSOLE_INPUTBUFFERSIZE 255

#endif
1 change: 1 addition & 0 deletions config/appids.h
@@ -0,0 +1 @@
#define APPID_DEFAULT 0
12 changes: 12 additions & 0 deletions config/defaults.h
@@ -0,0 +1,12 @@
#ifndef DEFAULTS_H_
#define DEFAULTS_H_

#define VERSION_MAJOR 1
#define VERSION_MINOR 0
#define VERSION_PATCH 0

#include "appids.h"
// Including the app specific configuration file
#include APPCONFIGFILE

#endif /* ifdef DEFAULTS_H_ */
1 change: 1 addition & 0 deletions libs/Makefile
@@ -0,0 +1 @@
include ../make/Makefile.subdir.inc
1 change: 1 addition & 0 deletions libs/base/Makefile
@@ -0,0 +1 @@
include ../../make/Makefile.libs.inc
55 changes: 55 additions & 0 deletions libs/base/base.c
@@ -0,0 +1,55 @@
#include <config/defaults.h>

#include "base.h"

#include <libs/daemon/console.h>

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>

volatile base_flags_t base_flags;
base_id_t base_id;

void base_getorigid(base_id_t *id) {
memset((void *)id, 0, sizeof(base_id_t));
gethostname((char *)id, sizeof(base_id_t));
}

// Converts hex char pairs to bytes, storing the result in the input buffer.
// Returns the length of the successfully converted byte array.
uint8_t base_hexdatatodata(char *hexdata) {
uint8_t i, length;
char hexnum[3];
char *endptr;

if (!hexdata)
return 0;

length = strlen(hexdata)/2;
hexnum[2] = 0;
for (i = 0; i < length; i++) {
hexnum[0] = hexdata[i*2];
hexnum[1] = hexdata[i*2+1];
errno = 0;
hexdata[i] = (uint8_t)strtol(hexnum, &endptr, 16);
if (*endptr != 0 || errno != 0)
break;
}
return i;
}

void base_process(void) {
}

void base_init(void) {
console_log("base: init\n");

base_getorigid(&base_id);
}

void base_deinit(void) {
console_log("base: deinit\n");
}
12 changes: 12 additions & 0 deletions libs/base/base.h
@@ -0,0 +1,12 @@
#ifndef BASE_H_
#define BASE_H_

#include <stdint.h>

uint8_t base_hexdatatodata(char *hexdata);
void base_process(void);

void base_init(void);
void base_deinit(void);

#endif
59 changes: 59 additions & 0 deletions libs/base/command.c
@@ -0,0 +1,59 @@
#include <config/defaults.h>

#include "command.h"
#include "log.h"
#include "types.h"
#include "base.h"

#include <libs/daemon/console.h>
#include <libs/config/config.h>

#include <string.h>
#include <errno.h>
#include <ctype.h>

void command_process(char *input_buffer) {
extern loglevel_t loglevel;
extern base_flags_t base_flags;

char *tok = strtok(input_buffer, " ");

if (tok == NULL)
return;

if (strcmp(tok, "help") == 0 || strcmp(tok, "h") == 0) {
console_log(" ver - version\n");
console_log(" log (loglevel) - get/set loglevel\n");
console_log(" exit - exits the application\n");
return;
}

if (strcmp(tok, "ver") == 0) {
log_ver();
return;
}

if (strcmp(tok, "log") == 0) {
tok = strtok(NULL, " ");
if (tok != NULL) {
if (strcmp(tok, "off") == 0 || tok[0] == '0') {
if (loglevel.raw == 0)
memset((void *)&loglevel.raw, 0xff, sizeof(loglevel.raw));
else
loglevel.raw = 0;;
} else if (strcmp(tok, "debug") == 0)
loglevel.flags.debug = !loglevel.flags.debug;

config_set_loglevel(&loglevel);
}
log_loglevel(&loglevel);
return;
}

if (strcmp(tok, "exit") == 0) {
base_flags.sigexit = 1;
return;
}

console_log("error: unknown command, see help, or go get a beer.\n");
}
6 changes: 6 additions & 0 deletions libs/base/command.h
@@ -0,0 +1,6 @@
#ifndef COMMAND_H_
#define COMMAND_H_

void command_process(char *input_buffer);

#endif
30 changes: 30 additions & 0 deletions libs/base/log.c
@@ -0,0 +1,30 @@
#include <config/defaults.h>

#include "log.h"

void log_ver(void) {
console_log("ver: v%u.%u.%u-a%u", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, APPID);
console_log(" " __TIME__ " " __DATE__ " " GITHASH "\n");
}

void log_loglevel(loglevel_t *loglevel) {
console_log("loglevel:\n");

console_log(" debug ");
if (loglevel->flags.debug)
console_log("on\n");
else
console_log("off\n");
}

void log_cmdmissingparam(void) {
console_log("missing parameter(s), see help.\n");
}

void log_cmdinvalidparam(void) {
console_log("invalid parameter(s), see help.\n");
}

void log_daemon_initconsoleserverfailed(void) {
console_log("daemon error: failed to initialize console server\n");
}
12 changes: 12 additions & 0 deletions libs/base/log.h
@@ -0,0 +1,12 @@
#ifndef LOG_H_
#define LOG_H_

#include <libs/daemon/console.h>

void log_ver(void);
void log_loglevel(loglevel_t *loglevel);
void log_cmdmissingparam(void);
void log_cmdinvalidparam(void);
void log_daemon_initconsoleserverfailed(void);

#endif
35 changes: 35 additions & 0 deletions libs/base/types.h
@@ -0,0 +1,35 @@
#ifndef TYPES_H_
#define TYPES_H_

#include <config/defaults.h>

#include <stdint.h>

#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif

#define BE16_TO_CPU(x) Swap16(x)
#define BE32_TO_CPU(x) Swap32(x)
#define CPU_TO_BE16(x) Swap16(x)
#define CPU_TO_BE32(x) Swap32(x)

#define Swap16(u16) ((uint16_t)(((uint16_t)(u16) >> 8) |\
((uint16_t)(u16) << 8)))

#define Swap32(u32) ((uint32_t)(((uint32_t)Swap16((uint32_t)(u32) >> 16)) |\
((uint32_t)Swap16((uint32_t)(u32)) << 16)))

#define ASSERT(x) static uint8_t __attribute__((unused)) assert_var[(x) ? 1 : -1]

typedef char base_id_t[16]; // This have to fit into one EEPROM page (16 bytes by default).
typedef uint8_t flag_t;

typedef struct __attribute__((packed)) {
uint8_t sigexit : 1;
} base_flags_t;

#endif

0 comments on commit 6e70019

Please sign in to comment.