OEBInterfaces.Mod 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. MODULE OEBInterfaces;
  2. (**
  3. AUTHOR Timothée Martiel, 05/2016
  4. PURPOSE Abstract communication interfaces for the OEB host framework
  5. *)
  6. IMPORT Modules, Kernel, KernelLog, Plugins, Strings;
  7. CONST
  8. (* Events *)
  9. EventEnter * = 0;
  10. EventLeave * = 1;
  11. (* Interface State *)
  12. StateInit * = 0; (** Interface ready to work *)
  13. StateActive * = 1; (** Interface running *)
  14. StateStopped * = 2; (** Interface paused *)
  15. StateDead * = 3; (** Interface down *)
  16. StateDie * = 4; (** Request interface shutdown *)
  17. (* Session State *)
  18. SessionOpen * = 0;
  19. SessionWaiting * = 1;
  20. SessionClosed * = 2;
  21. SessionError * = 3;
  22. CR * = 0DX;
  23. LF * = 0AX;
  24. (* Broadcast messages *)
  25. BcastHeader = "INFO: *"; (** String prepended to all broadcast messages, identifying them *)
  26. BcastEntering = "*Enter"; (** Message announcing availability of bootloader on that communication channel *)
  27. BcastLeaving = "*Leave"; (** Message announcing that the bootloader leaves this channel *)
  28. CommandTimeout = 15000;
  29. Module = "OEB Interfaces";
  30. Trace * = TRUE;
  31. TYPE
  32. (** Interface: abstract communication layer over one or several physical channel. *)
  33. Interface * = OBJECT
  34. VAR
  35. name -, (** Name of the interface instance *)
  36. type -: ARRAY 128 OF CHAR; (** Description of the kind of interface *)
  37. state: LONGINT; (** State *)
  38. handle: Handler; (** Event handler *)
  39. sessions -: Session; (** List of sessions managed by this interface *)
  40. next *: Interface;
  41. (** Initializes an interface with a name and a description *)
  42. PROCEDURE & InitInterface * (CONST name, type: ARRAY 128 OF CHAR);
  43. BEGIN
  44. COPY(name, SELF.name);
  45. COPY(type, SELF.type)
  46. END InitInterface;
  47. (** Send a command to a session. The session must be managed by the interface. *)
  48. PROCEDURE Send * (session: Session; CONST cmd: ARRAY OF CHAR): BOOLEAN;
  49. BEGIN
  50. HALT(506); (*! ABSTRACT *)
  51. END Send;
  52. (**
  53. Receive a message on a session. This is called by Update and returns a received message and
  54. the matching session. A new session must be created if msg is a broadcast enter message.
  55. *)
  56. PROCEDURE Receive * (VAR session: Session; VAR msg: ARRAY OF CHAR): BOOLEAN;
  57. BEGIN
  58. HALT(506); (*! ABSTRACT *)
  59. END Receive;
  60. (** Set the event handler for the interface *)
  61. PROCEDURE SetHandler * (handler: Handler);
  62. BEGIN {EXCLUSIVE}
  63. handle := handler
  64. END SetHandler;
  65. (** Start listening on the interface *)
  66. PROCEDURE Start *;
  67. BEGIN {EXCLUSIVE}
  68. IF Trace THEN EnterTrace; KernelLog.String("starting"); ExitTrace END;
  69. IF state # StateDead THEN state := StateActive END
  70. END Start;
  71. (** Pauses listening on the interface *)
  72. PROCEDURE Stop *;
  73. BEGIN {EXCLUSIVE}
  74. IF Trace THEN EnterTrace; KernelLog.String("stopping"); ExitTrace END;
  75. IF state # StateDead THEN state := StateStopped END
  76. END Stop;
  77. (** Definitively stops an interface *)
  78. PROCEDURE Kill *;
  79. BEGIN {EXCLUSIVE}
  80. IF Trace THEN EnterTrace; KernelLog.String("killing"); ExitTrace END;
  81. state := StateDie;
  82. AWAIT(state = StateDead)
  83. END Kill;
  84. (** Returns the current state *)
  85. PROCEDURE State * (): LONGINT;
  86. BEGIN {EXCLUSIVE}
  87. RETURN state
  88. END State;
  89. (**
  90. Execute a command 'cmd' for a session. Gives back a message in 'reply'. Returns TRUE if the command succeeded
  91. (including verifying reply), FALSE otherwise.
  92. Must not be called from the interface's process.
  93. *)
  94. PROCEDURE Command * (session: Session; CONST cmd: ARRAY OF CHAR; VAR reply: ARRAY OF CHAR): BOOLEAN;
  95. BEGIN
  96. IF Trace THEN EnterTrace; KernelLog.String("Sending command: "); KernelLog.String(cmd); ExitTrace END;
  97. BEGIN {EXCLUSIVE}
  98. IF state # StateActive THEN
  99. reply := "ERR: interface stopped";
  100. RETURN FALSE
  101. END
  102. END;
  103. IF session.State() # SessionOpen THEN RETURN FALSE END;
  104. IF ~Send(session, cmd) THEN
  105. IF Trace THEN EnterTrace; KernelLog.String("Command '"); KernelLog.String(cmd); KernelLog.String("' failed"); ExitTrace END;
  106. session.Kill;
  107. RETURN FALSE
  108. END;
  109. Kernel.SetTimer(session.timer, CommandTimeout);
  110. session.Wait;
  111. session.AwaitState({SessionOpen, SessionError, SessionClosed});
  112. CASE session.State() OF
  113. SessionOpen:
  114. (* We got the reply for the command *)
  115. COPY(session.msg, reply);
  116. RETURN Check(cmd, reply)
  117. |SessionClosed:
  118. (* Session was closed *)
  119. reply := 'Session closed';
  120. RETURN FALSE
  121. |SessionError:
  122. (* Error occured *)
  123. reply := 'Session error';
  124. RETURN FALSE
  125. END
  126. END Command;
  127. PROCEDURE Enumerate * (enumerator: Enumerator);
  128. VAR
  129. session: Session;
  130. BEGIN {EXCLUSIVE}
  131. session := sessions;
  132. WHILE session # NIL DO
  133. enumerator(session);
  134. session := session.next
  135. END
  136. END Enumerate;
  137. (** Dispatches messages received by the interface. *)
  138. PROCEDURE Update *;
  139. VAR
  140. s, session: Session;
  141. msg: ARRAY 128 OF CHAR;
  142. BEGIN
  143. IF ~Receive(session, msg) THEN Kill END;
  144. IF session = NIL THEN RETURN END;
  145. IF Strings.Match(BcastHeader, msg) THEN
  146. (* Broadcast message *)
  147. IF Strings.Match(BcastEntering, msg) THEN
  148. BEGIN {EXCLUSIVE}
  149. session.next := sessions;
  150. sessions := session
  151. END;
  152. session.Open;
  153. IF handle # NIL THEN handle(EventEnter, SELF, session) END
  154. ELSIF Strings.Match(BcastLeaving, msg) THEN
  155. BEGIN {EXCLUSIVE}
  156. IF session = sessions THEN
  157. sessions := sessions.next
  158. ELSIF sessions # NIL THEN
  159. s := sessions;
  160. WHILE s.next # session DO s := s.next END;
  161. s.next := session.next
  162. END
  163. END;
  164. session.Close;
  165. IF handle # NIL THEN handle(EventLeave, SELF, session) END
  166. END
  167. ELSIF session.State() = SessionWaiting THEN
  168. (* Session was waiting for message *)
  169. IF Kernel.Expired(session.timer) THEN
  170. session.Kill
  171. ELSE
  172. COPY(msg, session.msg);
  173. session.Open
  174. END
  175. ELSIF (session.State() = SessionOpen) & (msg = "OK: nop") THEN
  176. (* Received reply for new session *)
  177. BEGIN {EXCLUSIVE}
  178. session.next := sessions;
  179. sessions := session
  180. END;
  181. session.Open;
  182. IF handle # NIL THEN handle(EventEnter, SELF, session) END
  183. ELSE
  184. (* Session was not waiting for message *)
  185. Kill
  186. END
  187. END Update;
  188. PROCEDURE Check (CONST cmd, reply: ARRAY OF CHAR): BOOLEAN;
  189. VAR
  190. expected: ARRAY 128 OF CHAR;
  191. BEGIN
  192. expected := "OK: ";
  193. Strings.Append(expected, cmd);
  194. RETURN ((cmd = "netinfo") & Strings.Match("OK:*", reply)) OR (Strings.Match(expected, reply))
  195. END Check;
  196. PROCEDURE EnterTrace *;
  197. BEGIN
  198. IF Trace THEN
  199. KernelLog.Enter;
  200. KernelLog.String(Module);
  201. KernelLog.String(" ");
  202. KernelLog.String(name);
  203. KernelLog.String(" ");
  204. KernelLog.String("(");
  205. KernelLog.String(type);
  206. KernelLog.String(")");
  207. KernelLog.String(": ")
  208. END
  209. END EnterTrace;
  210. PROCEDURE ExitTrace *;
  211. BEGIN
  212. KernelLog.Exit
  213. END ExitTrace;
  214. BEGIN {ACTIVE}
  215. BEGIN {EXCLUSIVE} AWAIT(state # StateInit) END;
  216. LOOP
  217. BEGIN {EXCLUSIVE}
  218. AWAIT((state = StateActive) OR (state = StateDie));
  219. IF state = StateDie THEN EXIT END
  220. END;
  221. Update
  222. END;
  223. BEGIN {EXCLUSIVE}
  224. state := StateDead
  225. END;
  226. FINALLY
  227. BEGIN {EXCLUSIVE}
  228. state := StateDead
  229. END
  230. END Interface;
  231. (** Bootloader instance to deploy on *)
  232. Session * = OBJECT
  233. VAR
  234. msg: ARRAY 1024 OF CHAR;
  235. name -: ARRAY 128 OF CHAR;
  236. timer: Kernel.MilliTimer;
  237. state: LONGINT;
  238. guid *: LONGINT;
  239. next -: Session;
  240. PROCEDURE & InitSession * (CONST name: ARRAY OF CHAR);
  241. BEGIN
  242. COPY(name, SELF.name)
  243. END InitSession;
  244. PROCEDURE State * (): LONGINT;
  245. BEGIN {EXCLUSIVE}
  246. RETURN state
  247. END State;
  248. PROCEDURE AwaitState * (state: SET);
  249. BEGIN {EXCLUSIVE}
  250. AWAIT(SELF.state IN state)
  251. END AwaitState;
  252. PROCEDURE Open *;
  253. BEGIN {EXCLUSIVE}
  254. IF state # SessionError THEN state := SessionOpen END
  255. END Open;
  256. PROCEDURE Wait *;
  257. BEGIN {EXCLUSIVE}
  258. IF state = SessionOpen THEN state := SessionWaiting END
  259. END Wait;
  260. PROCEDURE Close *;
  261. BEGIN {EXCLUSIVE}
  262. IF state # SessionError THEN state := SessionClosed END
  263. END Close;
  264. PROCEDURE Kill *;
  265. BEGIN {EXCLUSIVE}
  266. state := SessionError
  267. END Kill;
  268. END Session;
  269. Factory * = OBJECT (Plugins.Plugin)
  270. PROCEDURE NewInterface * (CONST param: ARRAY OF CHAR): Interface;
  271. BEGIN
  272. HALT(555) (* ABSTRACT *)
  273. END NewInterface;
  274. END Factory;
  275. (** Session start and stop event handler *)
  276. Handler * = PROCEDURE {DELEGATE} (event: LONGINT; interface: Interface; session: Session);
  277. Enumerator * = PROCEDURE {DELEGATE} (session: Session);
  278. VAR
  279. factories: Plugins.Registry;
  280. PROCEDURE NewInterface * (CONST type, param: ARRAY OF CHAR): Interface;
  281. VAR
  282. factory: Factory;
  283. m: Modules.Module;
  284. name, msg: ARRAY 128 OF CHAR;
  285. res: LONGINT;
  286. BEGIN
  287. name := "OEB";
  288. Strings.Append(name, type);
  289. Strings.Append(name, "Interfaces");
  290. m := Modules.ThisModule(name, res, msg);
  291. IF m = NIL THEN
  292. COPY(type, name);
  293. Strings.Append(name, "Interfaces");
  294. m := Modules.ThisModule(name, res, msg)
  295. END;
  296. factory := factories.Get(type)(Factory);
  297. RETURN factory.NewInterface(param)
  298. END NewInterface;
  299. PROCEDURE Register * (CONST type: ARRAY OF CHAR; factory: Factory);
  300. VAR
  301. ignore: LONGINT;
  302. BEGIN
  303. factory.SetName(type);
  304. factories.Add(factory, ignore)
  305. END Register;
  306. PROCEDURE Unregister * (factory: Factory);
  307. BEGIN
  308. factories.Remove(factory)
  309. END Unregister;
  310. PROCEDURE Cleanup;
  311. BEGIN
  312. Plugins.main.Remove(factories)
  313. END Cleanup;
  314. BEGIN
  315. NEW(factories, "OEB Itf", "OEB Interface Factories");
  316. Modules.InstallTermHandler(Cleanup)
  317. END OEBInterfaces.