Skip to content

Commit

Permalink
Register c-node globally and remove no-longer needed services
Browse files Browse the repository at this point in the history
  • Loading branch information
abelino committed Aug 31, 2024
1 parent be91bc0 commit 5fd91c6
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 167 deletions.
17 changes: 17 additions & 0 deletions lib/bacnet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,21 @@ defmodule Bacnet do
@moduledoc """
BACNet client.
"""

use GenServer

require Logger

@spec start_link(any) :: GenServer.on_start()
def start_link(_opts) do
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
end

@impl GenServer
def init(_) do
{:ok, nil}
end

@doc false
defdelegate ei_log(level, term), to: Logger, as: :log
end
3 changes: 1 addition & 2 deletions lib/bacnet/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ defmodule Bacnet.Application do

defp children() do
[
{Bacnet.Logger, nil},
{Bacnet.Heartbeat, nil},
{Bacnet, nil},
{MuonTrap.Daemon, [bacnetd_exec(), bacnetd_args(), []]}
]
end
Expand Down
26 changes: 0 additions & 26 deletions lib/bacnet/heartbeat.ex

This file was deleted.

40 changes: 0 additions & 40 deletions lib/bacnet/logger.ex

This file was deleted.

29 changes: 28 additions & 1 deletion src/ei_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "ei_client.h"

#define OTP_COMPAT_VER 25
#define OTP_COMPAT_VER 24
#define HEARTBEAT_PROCESS "Elixir.Bacnet.Heartbeat"
#define CNODE_NAME "bacnetd"

Expand Down Expand Up @@ -53,6 +53,11 @@ bool ei_client_config(const char *nodename, const char *cookie)
return false;
}

erlang_pid *pid = ei_self(&client.cnode);
if (ei_global_register(client.fd, CNODE_NAME, pid) == -1) {
return false;
}

client.ready = true;

return true;
Expand All @@ -72,6 +77,28 @@ bool ei_client_send(char *process_name, ei_x_buff *msg)
return ret == 0 ? true : false;
}

bool ei_client_call(char *module, char *func, ei_x_buff *args, ei_x_buff *out)
{
pthread_mutex_lock(&client.lock);
int ret = ei_rpc(&client.cnode, client.fd, module, func, args->buff,
args->index, out);
pthread_mutex_unlock(&client.lock);

return ret == -1 ? false : true;
}

bool ei_client_recv(erlang_msg *meta, ei_x_buff *msg)
{
pthread_mutex_lock(&client.lock);
int ret = ei_xreceive_msg(client.fd, meta, msg);
pthread_mutex_unlock(&client.lock);
if (ret == ERL_ERROR) {
return false;
}

return true;
}

static void ei_free()
{
if (client.fd != NULL) {
Expand Down
4 changes: 2 additions & 2 deletions src/ei_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

#include <ei.h>

typedef void(msg_handler_cb_t)(erlang_msg *msg, ei_x_buff *data);

bool ei_client_config(const char *nodename, const char *cookie);
bool ei_client_send(char *process_name, ei_x_buff *msg);
bool ei_client_call(char *module, char *func, ei_x_buff *msg, ei_x_buff *out);
bool ei_client_recv(erlang_msg *meta, ei_x_buff *msg);

#endif /* EI_CLIENT_H */
66 changes: 0 additions & 66 deletions src/ei_heartbeat.c

This file was deleted.

6 changes: 0 additions & 6 deletions src/ei_heartbeat.h

This file was deleted.

29 changes: 17 additions & 12 deletions src/ei_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,31 @@ static const char *level_to_str(log_level_t level);

void ei_log(log_level_t level, const char *format, ...)
{
va_list args;
va_start(args, format);
va_list vargs;
va_start(vargs, format);

char log_buffer[MAX_LOG_LEN];
int log_len = snprintf(log_buffer, sizeof(log_buffer), format, args);
int log_len = vsnprintf(log_buffer, sizeof(log_buffer), format, vargs);

ei_x_buff msg;
ei_x_new_with_version(&msg);
ei_x_encode_tuple_header(&msg, 2);
ei_x_encode_atom(&msg, level_to_str(level));
ei_x_encode_binary(&msg, log_buffer, log_len);
ei_x_buff out;
ei_x_new(&out);

if (!ei_client_send("Elixir.Bacnet.Logger", &msg)) {
ei_x_buff args;
ei_x_new(&args);
ei_x_encode_list_header(&args, 2);
ei_x_encode_atom(&args, level_to_str(level));
ei_x_encode_binary(&args, log_buffer, log_len);
ei_x_encode_empty_list(&args);

if (!ei_client_call("Elixir.Bacnet", "ei_log", &args, &out)) {
char new_format[MAX_LOG_LEN];
snprintf(new_format, sizeof(new_format), "%s\n", format);
vprintf(new_format, args);
vprintf(new_format, vargs);
}

ei_x_free(&msg);
va_end(args);
ei_x_free(&out);
ei_x_free(&args);
va_end(vargs);
}

static const char *level_to_str(log_level_t level)
Expand Down
12 changes: 8 additions & 4 deletions src/ei_log.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#ifndef EI_LOG_H
#define EI_LOG_H

#define LOG_ERR(format, ...) ei_log(ERROR, format __VA_ARGS__)
#define LOG_WRN(format, ...) ei_log(WARNING, format __VA_ARGS__)
#define LOG_INF(format, ...) ei_log(INFO, format __VA_ARGS__)
#define LOG_DBG(format, ...) ei_log(DEBUG, format __VA_ARGS__)
#define LOG_EME(format, ...) ei_log(EMERGENCY, format, ##__VA_ARGS__)
#define LOG_ALE(format, ...) ei_log(ALERT, format, ##__VA_ARGS__)
#define LOG_CRT(format, ...) ei_log(CRITICAL, format, ##__VA_ARGS__)
#define LOG_ERR(format, ...) ei_log(ERROR, format, ##__VA_ARGS__)
#define LOG_WRN(format, ...) ei_log(WARNING, format, ##__VA_ARGS__)
#define LOG_NTC(format, ...) ei_log(NOTICE, format, ##__VA_ARGS__)
#define LOG_INF(format, ...) ei_log(INFO, format, ##__VA_ARGS__)
#define LOG_DBG(format, ...) ei_log(DEBUG, format, ##__VA_ARGS__)

typedef enum {
EMERGENCY,
Expand Down
46 changes: 38 additions & 8 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,60 @@
#include "arg.h"
#include "bacnet/datalink/dlenv.h"
#include "ei_client.h"
#include "ei_heartbeat.h"
#include "ei_log.h"

static arg_t args;
static void unimplemented(const char *msg_type);

int main(int argc, char **argv)
{
dlenv_init();

args_parse(&args, argc, argv);

if (!ei_client_config(args.nodename, args.cookie)) {
LOG_ERR("Failed to start ei client");
LOG_ERR("bacnetd: unable to establish connection");
return -1;
}

if (!ei_heartbeat_start()) {
LOG_ERR("Heartbeat failed to start");
return -1;
}
LOG_DBG("bacnetd: process started");

while (1) {
pause();
erlang_msg meta;
ei_x_buff msg;
ei_x_new(&msg);

if (!ei_client_recv(&meta, &msg)) {
goto cleanup;
}

int index = 0;
int version = 0;
int arity = 0;
char msg_type[MAXATOMLEN] = { 0 };

char *buf = msg.buff;
bool bad_msg = (false
|| ei_decode_version(buf, &index, &version)
|| ei_decode_tuple_header(buf, &index, &arity)
|| (arity < 2)
|| ei_decode_atom(buf, &index, msg_type));

if (bad_msg) {
goto cleanup;
}

if (strcmp(msg_type, "$gen_call") == 0) {
unimplemented("$gen_call");
}

cleanup:
ei_x_free(&msg);
}

return 0;
}

static void unimplemented(const char *msg_type)
{
LOG_WRN("bacnetd: %s unimplemented", msg_type);
}

0 comments on commit 5fd91c6

Please sign in to comment.