gv_fsm 0.2.9 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 19016ba84adc54b46d0c52e6dffcd4024f3626a32ada32830cdf3a8e094a3a35
4
- data.tar.gz: 32f84664395d90c58da54bf5b8add1a8a06bd0f0901ffd1947b9f81e713df26e
3
+ metadata.gz: 16cf9c4cad02807aa41c4229aceea78be6f407c28f2a25a8b40e4395c6c7d494
4
+ data.tar.gz: 21f75946fb7d626d97cb87bcc44108e7fd3c01d0f8c388c08f0f3a65fb8f65c3
5
5
  SHA512:
6
- metadata.gz: 5896400ccad2e6a1091f20aa32d3739bbe45c3b87fe5ed4501a4254235e3acf6a0d09561789d53686939fafa5d298de84567475a6bcca23b8c0b5375242f4f19
7
- data.tar.gz: fdc81c9081b5c83e4842ce864fb4b5babcdfde5cc03936c3cad0eb64f8a078af517b3da50fea7bc1f1a8027c36b2678a16d3d537118cf1280e7b994b405a24a6
6
+ metadata.gz: 0eb8b28821342aaea705c4f4fceb94f5a689579934ce8da6f6e6dba06a3e902de73da42fa95019fa10a5a8dd457f7fe54a23946b5ce437d298cf88358a92c184
7
+ data.tar.gz: 885046a66dd9f4ac5b996ca0b28bad123b6ca21bf3983690556064f9b70ce9b2b546c0305e3148bc0ffb4692fe22b3aa0adc55dec8918d8e3b85180a902ca783
data/bin/gv_fsm CHANGED
@@ -48,6 +48,10 @@ op = OptionParser.new do |parser|
48
48
  sm.syslog = false
49
49
  end
50
50
 
51
+ parser.on("-k", "--sigint STATE", "Install SIGINT handler that points to STATE") do |state|
52
+ sm.sigint = state
53
+ end
54
+
51
55
  parser.on("-h", "--help", "Prints this help") do
52
56
  puts parser
53
57
  exit
@@ -73,6 +77,11 @@ unless sm.parse(ARGV[0]) then
73
77
  exit 3
74
78
  end
75
79
 
80
+ if sm.ino && sm.sigint then
81
+ STDERR.puts "ERROR: signal handler is not supported on Arduino!\n\n"
82
+ exit 4
83
+ end
84
+
76
85
  puts "Parsed #{sm.dotfile}"
77
86
  top = sm.topology
78
87
  puts "Graph topology:"
@@ -87,11 +96,18 @@ end
87
96
  puts "Generating C functions for states: #{sm.states_list.join(", ")}."
88
97
  puts " for transition: #{sm.transition_functions_list.join(", ")}."
89
98
 
99
+ if (sm.sigint) then
100
+ puts "Installed signal handler for SIGINT in state #{top[:sources][0]}:\n stable states have emergency transition to state #{sm.sigint}"
101
+ unless top[:sinks].include? sm.sigint then
102
+ puts "WARNING: the state #{sm.sigint} is not a source, please check topology"
103
+ end
104
+ end
90
105
 
91
106
  if options[:header] then
92
107
  name = sm.generate_h
93
108
  puts "Generated header #{name}"
94
109
  end
110
+
95
111
  if options[:source] then
96
112
  name = sm.generate_c
97
113
  puts "Generated source #{name}"
data/lib/gv_fsm.rb CHANGED
@@ -10,7 +10,7 @@ require File.expand_path("../version.rb", __FILE__)
10
10
  module GV_FSM
11
11
  class FSM
12
12
  attr_reader :states, :transitions, :dotfile, :prefix, :error
13
- attr_accessor :project_name, :description, :cname, :syslog, :ino
13
+ attr_accessor :project_name, :description, :cname, :syslog, :ino, :sigint
14
14
  include GV_FSM::Templates
15
15
 
16
16
  def initialize(filename = nil)
@@ -20,6 +20,7 @@ module GV_FSM
20
20
  @error = nil
21
21
  @matrix = nil
22
22
  @nodemap = {}
23
+ @sigint = nil
23
24
  parse(filename) if filename
24
25
  end
25
26
 
@@ -48,7 +49,7 @@ module GV_FSM
48
49
  return nil
49
50
  end
50
51
  @matrix = Matrix.zero(n, n)
51
- @description = g.name
52
+ @description = g.name unless @description
52
53
  i = 0
53
54
  g.each_node do |id|
54
55
  n = g.get_node(id)
@@ -85,6 +86,10 @@ module GV_FSM
85
86
  @error = "Parsing error"
86
87
  return nil
87
88
  end
89
+ if (self.sigint && !states.find {|s| s[:id] == self.sigint}) then
90
+ @error = "Missing SIGINT state #{self.sigint}"
91
+ return nil
92
+ end
88
93
  return graph
89
94
  end
90
95
 
data/lib/templates.rb CHANGED
@@ -23,8 +23,8 @@ module GV_FSM
23
23
 
24
24
  HH =<<~EOH
25
25
  <% if !@ino then %>
26
- #ifndef <%= @cname.upcase %>_H
27
- #define <%= @cname.upcase %>_H
26
+ #ifndef <%= File::basename(@cname).upcase %>_H
27
+ #define <%= File::basename(@cname).upcase %>_H
28
28
  #include <stdlib.h>
29
29
  <% else %>
30
30
  #include <arduino.h>
@@ -92,7 +92,7 @@ module GV_FSM
92
92
  <% end %>
93
93
 
94
94
  // state manager
95
- <%= @prefix %>state_t <%= @prefix %>run_state(<%= @prefix %>state_t cur_state, state_data_t *data);
95
+ <%= @prefix %>state_t <%= @prefix %>run_state(<%= @prefix %>state_t cur_state, <%= @prefix %>state_data_t *data);
96
96
 
97
97
  <% if !@ino then %>
98
98
  #endif
@@ -108,8 +108,21 @@ module GV_FSM
108
108
  <% else %>
109
109
  <% if @syslog then log = :ino end %>
110
110
  <% end %>
111
- #include "<%= @cname %>.h"
111
+ #include "<%= File::basename(@cname) %>.h"
112
112
 
113
+ <% if sigint then %>// Install signal handler:
114
+ // SIGINT requests a transition to state <%= self.sigint %>
115
+ #include <signal.h>
116
+ static int _exit_request = 0;
117
+ static void signal_handler(int signal) {
118
+ if (signal == SIGINT) {
119
+ _exit_request = 1;<% if log == :syslog then %>
120
+ syslog(LOG_WARNING, "[FSM] SIGINT transition to <%= sigint %>");<% elsif log == :ino then %>
121
+ Serial.println("[FSM] SIGINT transition to <%= sigint %>");<% end %>
122
+ }
123
+ }
124
+
125
+ <% end %>
113
126
  <% placeholder = "Your Code Here" %>
114
127
  // SEARCH FOR <%= placeholder %> FOR CODE INSERTION POINTS!
115
128
 
@@ -153,6 +166,7 @@ module GV_FSM
153
166
  // |_| \\__,_|_| |_|\\___|\\__|_|\\___/|_| |_|___/
154
167
  //
155
168
  <% dest = destinations.dup %>
169
+ <% topo = self.topology %>
156
170
  <% @states.each do |s| %>
157
171
  <% stable = true if dest[s[:id]].include? s[:id] %>
158
172
  <% dest[s[:id]].map! {|n| (@prefix+"STATE_"+n).upcase} %>
@@ -161,9 +175,13 @@ module GV_FSM
161
175
  end %>
162
176
  // Function to be executed in state <%= s[:id] %>
163
177
  // valid return states: <%= dest[s[:id]].join(", ") %>
178
+ <% if sigint && stable && topo[:sources][0] != s[:id] then %>
179
+ // SIGINT triggers an emergency transition to <%= self.sigint %>
180
+ <% end %>
164
181
  <%= @prefix %>state_t <%= s[:function] %>(<%= @prefix %>state_data_t *data) {
165
182
  <%= @prefix %>state_t next_state = <%= dest[s[:id]].first %>;
166
-
183
+ <% if sigint && topo[:sources][0] == s[:id] then %>signal(SIGINT, signal_handler);
184
+ <% end %>
167
185
  <% if log == :syslog then %>
168
186
  syslog(LOG_INFO, "[FSM] In state <%= s[:id] %>");
169
187
  <% elsif log == :ino then %>
@@ -186,6 +204,10 @@ module GV_FSM
186
204
  <% end %>
187
205
  next_state = <%= @prefix.upcase %>NO_CHANGE;
188
206
  }
207
+ <% if sigint && stable && topo[:sources][0] != s[:id] then %>
208
+ // SIGINT transition override
209
+ if (_exit_request) next_state = <%= (@prefix+"STATE_"+self.sigint ).upcase %>;
210
+ <% end %>
189
211
  return next_state;
190
212
  }
191
213
 
@@ -282,4 +304,4 @@ module GV_FSM
282
304
  <% end %>
283
305
  EOC
284
306
  end
285
- end
307
+ end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module GV_FSM
2
- VERSION = "0.2.9"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gv_fsm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.9
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paolo Bosetti
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-12 00:00:00.000000000 Z
11
+ date: 2022-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-graphviz