Skip to content

Commit

Permalink
more flexibel nmt slave config, add mode = automatic to restart nodes…
Browse files Browse the repository at this point in the history
… that are rebooting, also start automatic node when node starts
  • Loading branch information
tonyrog committed Aug 13, 2015
1 parent 380adc6 commit 7966144
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 40 deletions.
11 changes: 6 additions & 5 deletions include/canopen.hrl
Expand Up @@ -254,10 +254,9 @@

%%
-define(COBID_TO_CANID(ID),
if ?is_cobid_extended((ID)) ->
((ID) band ?COBID_ENTRY_ID_MASK) bor ?CAN_EFF_FLAG;
true ->
((ID) band ?CAN_SFF_MASK)
case ?is_cobid_extended((ID)) of
true -> ((ID) band ?COBID_ENTRY_ID_MASK) bor ?CAN_EFF_FLAG;
false -> ((ID) band ?CAN_SFF_MASK)
end).

-define(CANID_TO_COBID(ID),
Expand Down Expand Up @@ -418,6 +417,7 @@
-type uint32() :: non_neg_integer().

-type nmt_role() :: slave | master | autonomous.
-type nmt_conf() :: default | undefined | string().
-type nid_type() :: nodeid | xnodeid.

%% SDO configuration parameters
Expand Down Expand Up @@ -460,7 +460,7 @@
last ::uint1(), %% Flag indicating if last segment is received
node_pid :: pid(), %% Pid of the node
client :: term(), %% Delayed gen_server:reply caller
ctx :: record(sdo_ctx), %% general parameters
ctx :: #sdo_ctx{}, %% general parameters
buf :: term(), %% Data buffer
mref :: term() %% Ref to application
}).
Expand Down Expand Up @@ -530,6 +530,7 @@
toggle = 0, %% Node guard toggle for nodeid
xtoggle = 0, %% Node guard toggle for xnodeid
nmt_role = autonomous, %% NMT role (master/slav/autonomous)
nmt_conf = default, %% where to read nmt slave config
node_guard_timer, %% Node guard supervision of master
node_guard_error = false, %% Lost contact with NMT master
node_life_time = 0, %% Node guard supervision of master
Expand Down
10 changes: 10 additions & 0 deletions priv/nmt.conf
@@ -0,0 +1,10 @@
%%
%% {{nodeid,N}, [{mode, automatic | manual } |
%% {guard_time, GT} |
%% {life_factor, LF} |
%% {heartbeat_time, Time} ]}
%% N must be a number between 1..126
%% Both GT and LF must be set to activate node guarding
%% mode = automatice only works in supervision = none
%%
{{nodeid,10}, [{mode, automatic}] }.
10 changes: 10 additions & 0 deletions src/co_api.erl
Expand Up @@ -140,6 +140,7 @@
{product, integer()} |
{revision, integer()} |
{nmt_role, nmt_role()} |
{nmt_conf, nmt_conf()} |
{supervision, supervision()} |
{inact_timeout, timeout() } |
{dict, obj_dict()} |
Expand Down Expand Up @@ -247,6 +248,15 @@ verify_option(nmt_role, NewValue)
ok;
verify_option(nmt_role, _NewValue) ->
{error, "Option nmt_role can only be set to slave/master/autonomous."};
verify_option(nmt_conf, NewValue)
when NewValue =:= default;
NewValue =:= undefined;
NewValue =:= "";
is_list(NewValue) ->
ok;
verify_option(nmt_conf, _NewValue) ->
{error, "Option nmt_conf can only be set to default/file-name."};

verify_option(supervision, NewValue)
when NewValue == node_guard;
NewValue == heartbeat;
Expand Down
90 changes: 88 additions & 2 deletions src/co_lib.erl
Expand Up @@ -68,8 +68,8 @@
%% Utilities
-export([utc_time/0,
sec/0,
debug/1]).

debug/1,
text_expand/2]).

%%--------------------------------------------------------------------
%% @doc
Expand Down Expand Up @@ -1245,3 +1245,89 @@ debug(true) ->
ale:trace(on, self(), debug);
debug(false) ->
ale:trace(off, self(), debug).

%%
%% Utility to exand environment "variables" in unicode text
%% variables are written as ${var} where var is a encoded atom
%% operating system enviroment is accessed through $(VAR)
%% and application library dir $/app/
%%
text_expand(Text, Env) when is_list(Text) ->
%% assume unicode character list!
text_expand_(Text, [], Env);
text_expand(Text, Env) when is_binary(Text) ->
%% assume utf8 encoded data!
text_expand_(unicode:characters_to_list(Text), [], Env).

text_expand_([$$,${|Text], Acc, Env) ->
text_expand_collect_(Text, [], [${,$$], env, Acc, Env);
text_expand_([$$,$(|Text], Acc, Env) ->
text_expand_collect_(Text, [], [$(,$$], shell, Acc, Env);
text_expand_([$$,$/|Text], Acc, Env) ->
text_expand_collect_(Text, [], [$/,$$], lib, Acc, Env);
text_expand_([$\\,C|Text], Acc, Env) ->
text_expand_(Text, [C|Acc], Env);
text_expand_([C|Text], Acc, Env) ->
text_expand_(Text, [C|Acc], Env);
text_expand_([], Acc, _Env) ->
lists:reverse(Acc).


text_expand_collect_([$)|Text], Var, _Pre, shell, Acc, Env) ->
case os:getenv(rev_variable(Var)) of
false ->
text_expand_(Text, Acc, Env);
Value ->
Acc1 = lists:reverse(Value, Acc),
text_expand_(Text, Acc1, Env)
end;
text_expand_collect_([$/|Text], Var, _Pre, lib, Acc, Env) ->
try erlang:list_to_existing_atom(rev_variable(Var)) of
App ->
case code:lib_dir(App) of
{error,_} ->
text_expand_(Text, Acc, Env);
Value ->
Acc1 = lists:reverse(Value, Acc),
text_expand_(Text, Acc1, Env)
end
catch
error:_ ->
text_expand_(Text, Acc, Env)
end;
text_expand_collect_([$}|Text], Var, _Pre, env, Acc, Env) ->
try erlang:list_to_existing_atom(rev_variable(Var)) of
Key ->
case lists:keyfind(Key, 1, Env) of
false ->
text_expand_(Text, Acc, Env);
{_,Val} ->
Value = lists:flatten(io_lib:format("~w", [Val])),
Acc1 = lists:reverse(Value, Acc),
text_expand_(Text, Acc1, Env)
end
catch
error:_ ->
text_expand_(Text, Acc, Env)
end;
text_expand_collect_([C|Text], Var, Pre, Shell, Acc, Env) ->
if C >= $a, C =< $z;
C >= $A, C =< $Z;
C >= $0, C =< $9;
C == $_; C == $@;
C == $\s; C == $\t -> %% space and tab allowed in begining and end
text_expand_collect_(Text, [C|Var], Pre, Shell, Acc, Env);
true ->
%% char not allowed in variable named
text_expand_(Text, [C | Var ++ Pre ++ Acc], Env)
end;
text_expand_collect_([], Var, Pre, _Shell, Acc, Env) ->
text_expand_([], Var ++ Pre ++ Acc, Env).

rev_variable(Var) ->
trim_hd(lists:reverse(trim_hd(Var))).

trim_hd([$\s|Cs]) -> trim_hd(Cs);
trim_hd([$\t|Cs]) -> trim_hd(Cs);
trim_hd(Cs) -> Cs.

0 comments on commit 7966144

Please sign in to comment.