Skip to content

Commit

Permalink
Rename Bacnet module namespace as BACNet and improve style
Browse files Browse the repository at this point in the history
  • Loading branch information
abelino committed Sep 19, 2024
1 parent 797e765 commit fcbd082
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 154 deletions.
9 changes: 7 additions & 2 deletions lib/bacnet.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
defmodule Bacnet do
defmodule BACNet do
@moduledoc """
BACNet client.
"""

require Logger

@spec add_device(device :: term) :: :ok | {:error, term}
defmodule Device do
@typedoc "Placeholder"
@type t :: term
end

@spec add_device(device :: Device.t) :: :ok | {:error, term}
def add_device(device) do
GenServer.call({:global, :bacnetd}, {:add_device, device})
end
Expand Down
32 changes: 13 additions & 19 deletions lib/bacnet/application.ex
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
defmodule Bacnet.Application do
defmodule BACNet.Application do
@moduledoc false

use Application

@impl true
def start(_type, _args) do
children = children()
opts = [strategy: :one_for_one, name: Bacnet.Supervisor]

Supervisor.start_link(children, opts)
end

defp children() do
[
{MuonTrap.Daemon, [bacnetd_exec(), bacnetd_args(), []]}
bacnetd_exe = "#{:code.priv_dir(:bacnet)}/bacnetd"
bacnetd_args = [
"--cookie", Node.get_cookie |> to_string,
"--nodename", Node.self |> to_string,
]
end

defp bacnetd_exec do
"#{:code.priv_dir(:bacnet)}/bacnetd"
end

defp bacnetd_args do
[
"--cookie", Node.get_cookie() |> to_string(),
"--nodename", Node.self() |> to_string(),
children = [
{MuonTrap.Daemon, [bacnetd_exe, bacnetd_args, []]}
]

Supervisor.start_link(
children,
strategy: :one_for_one,
name: BACNet.Supervisor
)
end
end
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Bacnet.MixProject do
defmodule BACNet.MixProject do
use Mix.Project

def project do
Expand Down Expand Up @@ -33,7 +33,7 @@ defmodule Bacnet.MixProject do
def application do
[
extra_applications: [:logger],
mod: {Bacnet.Application, []}
mod: {BACNet.Application, []}
]
end

Expand Down
2 changes: 1 addition & 1 deletion spec/bacnet_spec.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Bacnet.Spec do
defmodule BACNet.Spec do
use ESpec

specify do: expect true |> to(eq true)
Expand Down
25 changes: 13 additions & 12 deletions src/arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@

static const struct option options[] = {
{"nodename", required_argument, 0, 'n'},
{"cookie", required_argument, 0, 'c'},
{"cookie", required_argument, 0, 'c'},
{0, 0, 0, 0},
};

void args_parse(arg_t *args, int argc, char **argv)
{
int opt;
int option_index = 0;
int index = 0;

while ((opt = getopt_long(argc, argv, "n:c:", options, &option_index))
!= -1) {
while ((opt = getopt_long(argc, argv, "n:c:", options, &index)) != -1) {
switch (opt) {
case 'n':
strncpy(args->nodename, optarg, sizeof(args->nodename));
break;
case 'c':
strncpy(args->cookie, optarg, sizeof(args->cookie));
break;
default:
break;
case 'n':
strncpy(args->nodename, optarg, sizeof(args->nodename));
break;

case 'c':
strncpy(args->cookie, optarg, sizeof(args->cookie));
break;

default:
break;
}
}
}
76 changes: 38 additions & 38 deletions src/ei_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,29 @@
#include "ei_client.h"

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

struct ei_client {
bool ready;
pthread_mutex_t lock;
bool ready;
pthread_mutex_t lock;
struct ei_cnode_s cnode;
uint32_t creation;
int port;
int fd;
char nodename[MAXNODELEN + 1];
char cookie[MAXATOMLEN + 1];
uint32_t creation;
int port;
int fd;
char nodename[MAXNODELEN + 1];
char cookie[MAXATOMLEN + 1];
};

static struct ei_client client = { 0 };
static void ei_free();

bool ei_client_config(const char *nodename, const char *cookie)
{
if (client.ready) {
if (client.ready)
return true;
}

if (atexit(ei_free) != 0) {
if (atexit(ei_free) != 0)
return false;
}

strncpy(client.nodename, nodename, sizeof(client.nodename));
strncpy(client.cookie, cookie, sizeof(client.cookie));
Expand All @@ -42,79 +39,82 @@ bool ei_client_config(const char *nodename, const char *cookie)
pthread_mutex_init(&client.lock, NULL);
client.creation = 0;

if (ei_connect_init(&client.cnode, CNODE_NAME, cookie, client.creation) < 0) {
if (ei_connect_init(&client.cnode, CNODE_NAME, cookie, client.creation) < 0)
return false;
}

client.creation++;

client.fd = ei_connect(&client.cnode, client.nodename);
if (client.fd < 0) {
if (client.fd < 0)
return false;
}

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

client.ready = true;

return true;
}

bool ei_client_send(char *process_name, ei_x_buff *msg)
bool ei_client_send(char *process_name, ei_x_buff *message)
{
if (!client.ready) {
if (!client.ready)
return false;
}

pthread_mutex_lock(&client.lock);
int ret = ei_reg_send(&client.cnode, client.fd, process_name, msg->buff,
msg->index);
int ret = ei_reg_send(
&client.cnode,
client.fd,
process_name,
message->buff,
message->index
);
pthread_mutex_unlock(&client.lock);

return ret == 0 ? true : false;
}

bool ei_client_send_to(erlang_pid *pid, ei_x_buff *msg)
bool ei_client_send_to(erlang_pid *pid, ei_x_buff *message)
{
if (!client.ready) {
if (!client.ready)
return false;
}

pthread_mutex_lock(&client.lock);
int ret = ei_send(client.fd, pid, msg->buff, msg->index);
int ret = ei_send(client.fd, pid, message->buff, message->index);
pthread_mutex_unlock(&client.lock);

return ret == 0 ? true : false;
}

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

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

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

return true;
return ret == ERL_ERROR ? false : true;
}

static void ei_free()
{
if (client.fd != NULL) {
if (client.fd > 0)
ei_close_connection(client.fd);
}
}
8 changes: 4 additions & 4 deletions src/ei_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <ei.h>

bool ei_client_config(const char *nodename, const char *cookie);
bool ei_client_send(char *process_name, ei_x_buff *msg);
bool ei_client_send_to(erlang_pid *pid, 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);
bool ei_client_send(char *process_name, ei_x_buff *message);
bool ei_client_send_to(erlang_pid *pid, ei_x_buff *messageg);
bool ei_client_call(char *module, char *func, ei_x_buff *message, ei_x_buff *out);
bool ei_client_recv(erlang_msg *meta, ei_x_buff *message);

#endif /* EI_CLIENT_H */
36 changes: 14 additions & 22 deletions src/ei_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "ei_client.h"
#include "ei_log.h"

#define MAX_LOG_LEN 1024
#define MAX_LOG_LENGTH 1024

static const char *level_to_str(log_level_t level);

Expand All @@ -13,8 +13,8 @@ void ei_log(log_level_t level, const char *format, ...)
va_list vargs;
va_start(vargs, format);

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

ei_x_buff out;
ei_x_new(&out);
Expand All @@ -23,11 +23,11 @@ void ei_log(log_level_t level, const char *format, ...)
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_binary(&args, log_buffer, log_length);
ei_x_encode_empty_list(&args);

if (!ei_client_call("Elixir.Bacnet", "ei_log", &args, &out)) {
char new_format[MAX_LOG_LEN];
if (!ei_client_call("Elixir.BACNet", "ei_log", &args, &out)) {
char new_format[MAX_LOG_LENGTH];
snprintf(new_format, sizeof(new_format), "%s\n", format);
vprintf(new_format, vargs);
}
Expand All @@ -40,21 +40,13 @@ void ei_log(log_level_t level, const char *format, ...)
static const char *level_to_str(log_level_t level)
{
switch (level) {
case EMERGENCY:
return "emergency";
case ALERT:
return "alert";
case CRITICAL:
return "critical";
case ERROR:
return "error";
case WARNING:
return "warning";
case NOTICE:
return "notice";
case INFO:
return "info";
case DEBUG:
return "debug";
case EMERGENCY: return "emergency";
case ALERT: return "alert";
case CRITICAL: return "critical";
case ERROR: return "error";
case WARNING: return "warning";
case NOTICE: return "notice";
case INFO: return "info";
case DEBUG: return "debug";
}
}
16 changes: 8 additions & 8 deletions src/ei_log.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#ifndef EI_LOG_H
#define EI_LOG_H

#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__)
#define LOG_EMERGENCY(format, ...) ei_log(EMERGENCY, format, ##__VA_ARGS__)
#define LOG_ALERT(format, ...) ei_log(ALERT, format, ##__VA_ARGS__)
#define LOG_CRITICAL(format, ...) ei_log(CRITICAL, format, ##__VA_ARGS__)
#define LOG_ERROR(format, ...) ei_log(ERROR, format, ##__VA_ARGS__)
#define LOG_WARNING(format, ...) ei_log(WARNING, format, ##__VA_ARGS__)
#define LOG_NOTICE(format, ...) ei_log(NOTICE, format, ##__VA_ARGS__)
#define LOG_INFO(format, ...) ei_log(INFO, format, ##__VA_ARGS__)
#define LOG_DEBUG(format, ...) ei_log(DEBUG, format, ##__VA_ARGS__)

typedef enum {
EMERGENCY,
Expand Down
Loading

0 comments on commit fcbd082

Please sign in to comment.