Skip to content

Commit

Permalink
Make bacnet network interface configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
abelino committed Sep 26, 2024
1 parent 820e31f commit de1e6e4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
23 changes: 21 additions & 2 deletions lib/bacnet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,28 @@ defmodule BACNet do
end

@impl GenServer
def init(_args) do
def init(args) do
bacnetd_exe = "#{:code.priv_dir(:bacnet)}/bacnetd"
port = Port.open({:spawn, bacnetd_exe}, [:binary, :use_stdio, {:packet, 4}, {:env, [{~c"BACNET_IFACE", ~c"enp129s0f0np0"}]}])

env =
case args[:network_interface] do
nil ->
[]

network_interface ->
[{~c"BACNET_IFACE", to_charlist(network_interface)}]
end

port = Port.open(
{:spawn, bacnetd_exe},
[
:binary,
:use_stdio,
{:packet, 4},
{:env, env},
]
)

state = %State{port: port}

{:ok, state}
Expand Down
10 changes: 8 additions & 2 deletions src/bacnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ void handle_bacnet_request(char *buffer, int *index, ei_x_buff *reply)
case REQUEST_ANALOG_INPUT_SET_PRESENT_VALUE: {
routed_analog_input_present_value_t *request = &data->routed_analog_input_present_value;

Get_Routed_Device_Object(request->device_instance_number);
uint32_t device_index =
Routed_Device_Instance_To_Index(request->device_instance_number);

Get_Routed_Device_Object(device_index);
Routed_Analog_Input_Present_Value_Set(request->object_instance_number, request->value);
Get_Routed_Device_Object(0);
ei_x_encode_atom(reply, "ok");
Expand All @@ -231,7 +234,10 @@ void handle_bacnet_request(char *buffer, int *index, ei_x_buff *reply)
case REQUEST_MULTISTATE_INPUT_SET_PRESENT_VALUE: {
routed_multistate_input_present_value_t *request = &data->routed_multistate_input_present_value;

Get_Routed_Device_Object(request->device_instance_number);
uint32_t device_index =
Routed_Device_Instance_To_Index(request->device_instance_number);

Get_Routed_Device_Object(device_index);
Routed_Multistate_Input_Present_Value_Set(request->object_instance_number, request->value);
Get_Routed_Device_Object(0);
ei_x_encode_atom(reply, "ok");
Expand Down
16 changes: 8 additions & 8 deletions src/bacnet_enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,14 +427,14 @@ const enum_tuple_t BACNET_UNIT_ATOMS[] = {
{NULL, -1},
};

const char *BACNET_DEVICE_STATUS_ATOMS[] = {
"operational",
"operational_read_only",
"download_required",
"download_in_progress",
"non_operational",
"backup_in_progress",
"max",
const enum_tuple_t BACNET_DEVICE_STATUS_ATOMS[] = {
{"operational", 0},
{"operational_read_only", 1},
{"download_required", 2},
{"download_in_progress", 3},
{"non_operational", 4},
{"backup_in_progress", 5},
{"max", 6},
};

#endif /* BACNET_ENUM_H */
23 changes: 2 additions & 21 deletions src/bacnet_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,6 @@ static int find_enum_value(const enum_tuple_t *enum_tuples, const char *atom)
return -1;
}

static int find_string(const char *haystack[], char *needle)
{
for (size_t index = 0; index < sizeof(*haystack); index++) {
if (strcmp(haystack[index], needle) == 0) {
return index;
}
}

return -1;
}

static int
decode_bacnet_request_type(
char *buffer,
Expand All @@ -100,14 +89,6 @@ decode_bacnet_request_type(
return -1;
}

static int
decode_bacnet_object_instance_number(char *buffer,
int *index,
object_instanct_number_t *data)
{
return ei_decode_ulong(buffer, index, &data->value);
}

static int
decode_bacnet_object_name(char *buffer, int *index, object_name_t *data)
{
Expand Down Expand Up @@ -150,7 +131,7 @@ decode_bacnet_system_status(char *buffer,
if (ei_decode_atom(buffer, index, atom) == -1)
return -1;

int status = find_string(BACNET_DEVICE_STATUS_ATOMS, atom);
int status = find_enum_value(BACNET_DEVICE_STATUS_ATOMS, atom);
if (status == -1)
return -1;

Expand Down Expand Up @@ -373,7 +354,7 @@ decode_bacnet_request_data(
{
switch(type) {
case REQUEST_DEVICE_SET_OBJECT_INSTANCE_NUMBER:
return decode_bacnet_object_instance_number(buffer, index, &data->instance_number);
return ei_decode_ulong(buffer, index, &data->instance_number.value);

case REQUEST_DEVICE_SET_OBJECT_NAME:
return decode_bacnet_object_name(buffer, index, &data->name);
Expand Down

0 comments on commit de1e6e4

Please sign in to comment.