001package jmri.jmrix.openlcb; 002 003import java.net.InetAddress; 004import java.net.NetworkInterface; 005import java.net.SocketException; 006import java.nio.charset.StandardCharsets; 007import java.util.ArrayList; 008import java.util.List; 009import java.util.Random; 010import java.util.ResourceBundle; 011 012import jmri.ClockControl; 013import jmri.GlobalProgrammerManager; 014import jmri.InstanceManager; 015import jmri.jmrix.can.CanListener; 016import jmri.jmrix.can.CanMessage; 017import jmri.jmrix.can.CanReply; 018import jmri.jmrix.can.CanSystemConnectionMemo; 019import jmri.jmrix.can.TrafficController; 020import jmri.profile.ProfileManager; 021import jmri.util.ThreadingUtil; 022 023import org.openlcb.*; 024import org.openlcb.can.AliasMap; 025import org.openlcb.can.CanInterface; 026import org.openlcb.can.MessageBuilder; 027import org.openlcb.can.OpenLcbCanFrame; 028import org.openlcb.implementations.DatagramService; 029import org.openlcb.implementations.MemoryConfigurationService; 030import org.openlcb.protocols.TimeProtocol; 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033 034/** 035 * Does configuration for OpenLCB communications implementations. 036 * 037 * @author Bob Jacobsen Copyright (C) 2010 038 */ 039public class OlcbConfigurationManager extends jmri.jmrix.can.ConfigurationManager { 040 041 // Constants for the protocol options keys. These option keys are used to save configuration 042 // in the profile.xml and set on a per-connection basis in the connection preferences. 043 044 // Protocol key for node identification 045 public static final String OPT_PROTOCOL_IDENT = "Ident"; 046 047 // Option key for Node ID 048 public static final String OPT_IDENT_NODEID = "NodeId"; 049 // Option key for User Name, used for the Simple Node Ident Protocol 050 public static final String OPT_IDENT_USERNAME = "UserName"; 051 // Option key for User Description, used for the Simple Node Ident Protocol 052 public static final String OPT_IDENT_DESCRIPTION = "UserDescription"; 053 054 // Protocol key for fast clock 055 public static final String OPT_PROTOCOL_FASTCLOCK = "FastClock"; 056 057 // Option key for fast clock mode 058 public static final String OPT_FASTCLOCK_ENABLE = "EnableMode"; 059 // Option value for setting fast clock to disabled. 060 public static final String OPT_FASTCLOCK_ENABLE_OFF = "disabled"; 061 // Option value for setting fast clock to clock generator/producer/master. 062 public static final String OPT_FASTCLOCK_ENABLE_GENERATOR = "generator"; 063 // Option value for setting fast clock to clock consumer/slave. 064 public static final String OPT_FASTCLOCK_ENABLE_CONSUMER = "consumer"; 065 066 // Option key for setting the clock identifier. 067 public static final String OPT_FASTCLOCK_ID = "ClockId"; 068 // Option value for using the well-known clock id "default clock" 069 public static final String OPT_FASTCLOCK_ID_DEFAULT = "default"; 070 // Option value for using the well-known clock id "default real-time clock" 071 public static final String OPT_FASTCLOCK_ID_DEFAULT_RT = "realtime"; 072 // Option value for using the well-known clock id "alternate clock 1" 073 public static final String OPT_FASTCLOCK_ID_ALT_1 = "alt1"; 074 // Option value for using the well-known clock id "alternate clock 2" 075 public static final String OPT_FASTCLOCK_ID_ALT_2 = "alt2"; 076 // Option value for using a custom clock ID 077 public static final String OPT_FASTCLOCK_ID_CUSTOM = "custom"; 078 079 // Option key for setting the clock identifier to a custom value. Must set ClockId==custom in 080 // order to be in effect. The custom clock id is in node ID format. 081 public static final String OPT_FASTCLOCK_CUSTOM_ID = "ClockCustomId"; 082 083 public OlcbConfigurationManager(CanSystemConnectionMemo memo) { 084 super(memo); 085 086 InstanceManager.store(cf = new jmri.jmrix.openlcb.swing.OpenLcbComponentFactory(adapterMemo), 087 jmri.jmrix.swing.ComponentFactory.class); 088 InstanceManager.store(this, OlcbConfigurationManager.class); 089 } 090 091 final jmri.jmrix.swing.ComponentFactory cf; 092 093 private void initializeFastClock() { 094 boolean isMaster; 095 String enableOption = adapterMemo.getProtocolOption(OPT_PROTOCOL_FASTCLOCK, OPT_FASTCLOCK_ENABLE); 096 if (OPT_FASTCLOCK_ENABLE_GENERATOR.equals(enableOption)) { 097 isMaster = true; 098 } else if (OPT_FASTCLOCK_ENABLE_CONSUMER.equals(enableOption)) { 099 isMaster = false; 100 } else { 101 // no clock needed. 102 return; 103 } 104 105 NodeID clockId = null; 106 String clockIdSetting = adapterMemo.getProtocolOption(OPT_PROTOCOL_FASTCLOCK, OPT_FASTCLOCK_ID); 107 if (OPT_FASTCLOCK_ID_DEFAULT.equals(clockIdSetting)) { 108 clockId = TimeProtocol.DEFAULT_CLOCK; 109 } else if (OPT_FASTCLOCK_ID_DEFAULT_RT.equals(clockIdSetting)) { 110 clockId = TimeProtocol.DEFAULT_RT_CLOCK; 111 } else if (OPT_FASTCLOCK_ID_ALT_1.equals(clockIdSetting)) { 112 clockId = TimeProtocol.ALT_CLOCK_1; 113 } else if (OPT_FASTCLOCK_ID_ALT_2.equals(clockIdSetting)) { 114 clockId = TimeProtocol.ALT_CLOCK_2; 115 } else if (OPT_FASTCLOCK_ID_CUSTOM.equals(clockIdSetting)) { 116 String customId = adapterMemo.getProtocolOption(OPT_PROTOCOL_FASTCLOCK, OPT_FASTCLOCK_CUSTOM_ID); 117 if (customId == null || customId.isEmpty()) { 118 log.error("OpenLCB clock initialize: User selected custom clock, but did not provide a Custom Clock ID. Using default clock."); 119 } else { 120 try { 121 clockId = new NodeID(customId); 122 } catch (IllegalArgumentException e) { 123 log.error("OpenLCB clock initialize: Custom Clock ID '{}' is in illegal format. Use dotted hex notation like 05.01.01.01.DD.EE", customId); 124 } 125 } 126 } 127 if (clockId == null) { 128 clockId = TimeProtocol.DEFAULT_CLOCK; 129 } 130 log.debug("Creating olcb clock with id {} is_master {}", clockId, isMaster); 131 clockControl = new OlcbClockControl(getInterface(), clockId, isMaster); 132 InstanceManager.setDefault(ClockControl.class, clockControl); 133 } 134 135 @Override 136 public void configureManagers() { 137 138 // create our NodeID 139 getOurNodeID(); 140 141 // do the connections 142 tc = adapterMemo.getTrafficController(); 143 144 olcbCanInterface = createOlcbCanInterface(nodeID, tc); 145 146 // create JMRI objects 147 InstanceManager.setSensorManager( 148 getSensorManager()); 149 150 InstanceManager.setTurnoutManager( 151 getTurnoutManager()); 152 153 InstanceManager.setThrottleManager( 154 getThrottleManager()); 155 156 InstanceManager.setReporterManager( 157 getReporterManager()); 158 159 InstanceManager.setLightManager( 160 getLightManager() 161 ); 162 163 InstanceManager.store(getCommandStation(), jmri.CommandStation.class); 164 165 if (getProgrammerManager().isAddressedModePossible()) { 166 InstanceManager.store(getProgrammerManager(), jmri.AddressedProgrammerManager.class); 167 } 168 if (getProgrammerManager().isGlobalProgrammerAvailable()) { 169 jmri.InstanceManager.store(getProgrammerManager(), GlobalProgrammerManager.class); 170 } 171 172 // start alias acquisition 173 new StartUpHandler().start(); 174 175 OlcbInterface iface = getInterface(); 176 loaderClient = new LoaderClient(iface.getOutputConnection(), 177 iface.getMemoryConfigurationService(), 178 iface.getDatagramService()); 179 iface.registerMessageListener(loaderClient); 180 181 iface.registerMessageListener(new SimpleNodeIdentInfoHandler()); 182 iface.registerMessageListener(new PipRequestHandler()); 183 184 initializeFastClock(); 185 186 aliasMap = new AliasMap(); 187 tc.addCanListener(new CanListener() { 188 @Override 189 public void message(CanMessage m) { 190 if (!m.isExtended() || m.isRtr()) { 191 return; 192 } 193 aliasMap.processFrame(convertFromCan(m)); 194 } 195 196 @Override 197 public void reply(CanReply m) { 198 if (!m.isExtended() || m.isRtr()) { 199 return; 200 } 201 aliasMap.processFrame(convertFromCan(m)); 202 } 203 }); 204 messageBuilder = new MessageBuilder(aliasMap); 205 } 206 207 CanInterface olcbCanInterface; 208 TrafficController tc; 209 NodeID nodeID; 210 LoaderClient loaderClient; 211 OlcbClockControl clockControl; 212 OlcbEventNameStore olcbEventNameStore = new OlcbEventNameStore(); 213 214 OlcbInterface getInterface() { 215 return olcbCanInterface.getInterface(); 216 } 217 218 // internal to OpenLCB library, should not be exposed 219 AliasMap aliasMap; 220 // internal to OpenLCB library, should not be exposed 221 MessageBuilder messageBuilder; 222 223 /** 224 * Check if a type of manager is provided by this manager. 225 * 226 * @param type the class of manager to check 227 * @return true if the type of manager is provided; false otherwise 228 */ 229 @Override 230 public boolean provides(Class<?> type) { 231 if (adapterMemo.getDisabled()) { 232 return false; 233 } 234 if (type.equals(jmri.ThrottleManager.class)) { 235 return true; 236 } 237 if (type.equals(jmri.SensorManager.class)) { 238 return true; 239 } 240 if (type.equals(jmri.TurnoutManager.class)) { 241 return true; 242 } 243 if (type.equals(jmri.ReporterManager.class)) { 244 return true; 245 } 246 if (type.equals(jmri.LightManager.class)) { 247 return true; 248 } 249 if (type.equals(jmri.GlobalProgrammerManager.class)) { 250 return true; 251 } 252 if (type.equals(jmri.AddressedProgrammerManager.class)) { 253 return true; 254 } 255 if (type.equals(jmri.CommandStation.class)) { 256 return true; 257 } 258 if (type.equals(AliasMap.class)) { 259 return true; 260 } 261 if (type.equals(MessageBuilder.class)) { 262 return true; 263 } 264 if (type.equals(MimicNodeStore.class)) { 265 return true; 266 } 267 if (type.equals(Connection.class)) { 268 return true; 269 } 270 if (type.equals(MemoryConfigurationService.class)) { 271 return true; 272 } 273 if (type.equals(DatagramService.class)) { 274 return true; 275 } 276 if (type.equals(NodeID.class)) { 277 return true; 278 } 279 if (type.equals(OlcbInterface.class)) { 280 return true; 281 } 282 if (type.equals(CanInterface.class)) { 283 return true; 284 } 285 if (type.equals(ClockControl.class)) { 286 return clockControl != null; 287 } 288 if (type.equals(OlcbEventNameStore.class)) { 289 return true; 290 } 291 return false; // nothing, by default 292 } 293 294 @SuppressWarnings("unchecked") 295 @Override 296 public <T> T get(Class<?> T) { 297 if (adapterMemo.getDisabled()) { 298 return null; 299 } 300 if (T.equals(jmri.ThrottleManager.class)) { 301 return (T) getThrottleManager(); 302 } 303 if (T.equals(jmri.SensorManager.class)) { 304 return (T) getSensorManager(); 305 } 306 if (T.equals(jmri.TurnoutManager.class)) { 307 return (T) getTurnoutManager(); 308 } 309 if (T.equals(jmri.LightManager.class)) { 310 return (T) getLightManager(); 311 } 312 if (T.equals(jmri.ReporterManager.class)) { 313 return (T) getReporterManager(); 314 } 315 if (T.equals(jmri.GlobalProgrammerManager.class)) { 316 return (T) getProgrammerManager(); 317 } 318 if (T.equals(jmri.AddressedProgrammerManager.class)) { 319 return (T) getProgrammerManager(); 320 } 321 if (T.equals(jmri.CommandStation.class)) { 322 return (T) getCommandStation(); 323 } 324 if (T.equals(AliasMap.class)) { 325 return (T) aliasMap; 326 } 327 if (T.equals(MessageBuilder.class)) { 328 return (T) messageBuilder; 329 } 330 if (T.equals(MimicNodeStore.class)) { 331 return (T) getInterface().getNodeStore(); 332 } 333 if (T.equals(Connection.class)) { 334 return (T) getInterface().getOutputConnection(); 335 } 336 if (T.equals(MemoryConfigurationService.class)) { 337 return (T) getInterface().getMemoryConfigurationService(); 338 } 339 if (T.equals(DatagramService.class)) { 340 return (T) getInterface().getDatagramService(); 341 } 342 if (T.equals(LoaderClient.class)) { 343 return (T) loaderClient; 344 } 345 if (T.equals(NodeID.class)) { 346 return (T) nodeID; 347 } 348 if (T.equals(OlcbInterface.class)) { 349 return (T) getInterface(); 350 } 351 if (T.equals(CanInterface.class)) { 352 return (T) olcbCanInterface; 353 } 354 if (T.equals(ClockControl.class)) { 355 return (T) clockControl; 356 } 357 if (T.equals(OlcbEventNameStore.class)) { 358 return (T) olcbEventNameStore; 359 } 360 return null; // nothing, by default 361 } 362 363 protected OlcbProgrammerManager programmerManager; 364 365 public OlcbProgrammerManager getProgrammerManager() { 366 if (adapterMemo.getDisabled()) { 367 return null; 368 } 369 if (programmerManager == null) { 370 programmerManager = new OlcbProgrammerManager(adapterMemo); 371 } 372 return programmerManager; 373 } 374 375 protected OlcbThrottleManager throttleManager; 376 377 public OlcbThrottleManager getThrottleManager() { 378 if (adapterMemo.getDisabled()) { 379 return null; 380 } 381 if (throttleManager == null) { 382 throttleManager = new OlcbThrottleManager(adapterMemo); 383 } 384 return throttleManager; 385 } 386 387 protected OlcbTurnoutManager turnoutManager; 388 389 public OlcbTurnoutManager getTurnoutManager() { 390 if (adapterMemo.getDisabled()) { 391 return null; 392 } 393 if (turnoutManager == null) { 394 turnoutManager = new OlcbTurnoutManager(adapterMemo); 395 } 396 return turnoutManager; 397 } 398 399 protected OlcbSensorManager sensorManager; 400 401 public OlcbSensorManager getSensorManager() { 402 if (adapterMemo.getDisabled()) { 403 return null; 404 } 405 if (sensorManager == null) { 406 sensorManager = new OlcbSensorManager(adapterMemo); 407 } 408 return sensorManager; 409 } 410 411 protected OlcbReporterManager reporterManager; 412 413 public OlcbReporterManager getReporterManager() { 414 if (adapterMemo.getDisabled()) { 415 return null; 416 } 417 if (reporterManager == null) { 418 reporterManager = new OlcbReporterManager(adapterMemo); 419 } 420 return reporterManager; 421 } 422 423 protected OlcbCommandStation commandStation; 424 425 public OlcbCommandStation getCommandStation() { 426 if (adapterMemo.getDisabled()) { 427 return null; 428 } 429 if (commandStation == null) { 430 commandStation = new OlcbCommandStation(adapterMemo); 431 } 432 return commandStation; 433 } 434 435 @Override 436 public void dispose() { 437 if (turnoutManager != null) { 438 InstanceManager.deregister(turnoutManager, jmri.jmrix.openlcb.OlcbTurnoutManager.class); 439 } 440 if (sensorManager != null) { 441 InstanceManager.deregister(sensorManager, jmri.jmrix.openlcb.OlcbSensorManager.class); 442 } 443 if (lightManager != null) { 444 InstanceManager.deregister(lightManager, jmri.jmrix.openlcb.OlcbLightManager.class); 445 } 446 if (cf != null) { 447 InstanceManager.deregister(cf, jmri.jmrix.swing.ComponentFactory.class); 448 } 449 InstanceManager.deregister(this, OlcbConfigurationManager.class); 450 451 if (clockControl != null) { 452 clockControl.dispose(); 453 InstanceManager.deregister(clockControl, ClockControl.class); 454 } 455 } 456 457 protected OlcbLightManager lightManager; 458 459 public OlcbLightManager getLightManager() { 460 if (adapterMemo.getDisabled()) { 461 return null; 462 } 463 if (lightManager == null) { 464 lightManager = new OlcbLightManager(adapterMemo); 465 } 466 return lightManager; 467 } 468 469 class SimpleNodeIdentInfoHandler extends MessageDecoder { 470 /** 471 * Helper function to add a string value to the sequence of bytes to send for SNIP 472 * response content. 473 * 474 * @param addString string to render into byte stream 475 * @param contents represents the byte stream that will be sent. 476 * @param maxlength maximum number of characters to include, not counting terminating null 477 */ 478 private void addStringPart(String addString, List<Byte> contents, int maxlength) { 479 if (addString != null && !addString.isEmpty()) { 480 String value = addString.substring(0,Math.min(maxlength, addString.length())); 481 byte[] bb = value.getBytes(StandardCharsets.UTF_8); 482 for (byte b : bb) { 483 contents.add(b); 484 } 485 } 486 // terminating null byte. 487 contents.add((byte)0); 488 } 489 490 SimpleNodeIdentInfoHandler() { 491 List<Byte> l = new ArrayList<>(256); 492 493 l.add((byte)4); // version byte 494 addStringPart("JMRI", l, 40); // mfg field; 40 char limit in Standard, not counting final null 495 addStringPart(jmri.Application.getApplicationName(), l, 40); // model 496 String name = ProfileManager.getDefault().getActiveProfileName(); 497 if (name != null) { 498 addStringPart(name, l, 20); // hardware version 499 } else { 500 addStringPart("", l, 20); // hardware version 501 } 502 addStringPart(jmri.Version.name(), l, 20); // software version 503 504 l.add((byte)2); // version byte 505 addStringPart(adapterMemo.getProtocolOption(OPT_PROTOCOL_IDENT, OPT_IDENT_USERNAME), l, 62); 506 addStringPart(adapterMemo.getProtocolOption(OPT_PROTOCOL_IDENT, OPT_IDENT_DESCRIPTION), l, 63); 507 508 content = new byte[l.size()]; 509 for (int i = 0; i < l.size(); ++i) { 510 content[i] = l.get(i); 511 } 512 } 513 private final byte[] content; 514 515 @Override 516 public void handleSimpleNodeIdentInfoRequest(SimpleNodeIdentInfoRequestMessage msg, 517 Connection sender) { 518 if (msg.getDestNodeID().equals(nodeID)) { 519 // Sending a SNIP reply to the bus crashes the library up to 0.7.7. 520 if (msg.getSourceNodeID().equals(nodeID) || Version.libVersionAtLeast(0, 7, 8)) { 521 getInterface().getOutputConnection().put(new SimpleNodeIdentInfoReplyMessage(nodeID, msg.getSourceNodeID(), content), this); 522 } 523 } 524 } 525 } 526 527 class PipRequestHandler extends MessageDecoder { 528 529 @Override 530 public void handleProtocolIdentificationRequest(ProtocolIdentificationRequestMessage msg, Connection sender) { 531 long flags = 0x00041000000000L; // PC, SNIP protocols 532 // only reply if for us 533 if (msg.getDestNodeID() == nodeID) { 534 getInterface().getOutputConnection().put(new ProtocolIdentificationReplyMessage(nodeID, msg.getSourceNodeID(), flags), this); 535 } 536 } 537 538 } 539 540 @Override 541 protected ResourceBundle getActionModelResourceBundle() { 542 return ResourceBundle.getBundle("jmri.jmrix.openlcb.OlcbActionListBundle"); 543 } 544 545 /** 546 * Create a node ID in the JMRI range from one byte of IP address, and 2 547 * bytes of PID. That changes each time, which isn't perhaps what's wanted. 548 */ 549 protected void getOurNodeID() { 550 try { 551 String userOption = adapterMemo.getProtocolOption(OPT_PROTOCOL_IDENT, OPT_IDENT_NODEID); 552 if (userOption != null && !userOption.isEmpty()) { 553 try { 554 nodeID = new NodeID(userOption); 555 log.trace("getOurNodeID sets known option Node ID: {}", nodeID); 556 return; 557 } catch (IllegalArgumentException e) { 558 log.error("User configured a node ID protocol option which is in invalid format ({}). Expected dotted hex notation like 02.01.12.FF.EE.DD", userOption); 559 } 560 } 561 List<NodeID> previous = InstanceManager.getList(NodeID.class); 562 if (!previous.isEmpty()) { 563 nodeID = previous.get(0); 564 log.trace("getOurNodeID sets known instance Node ID: {}", nodeID); 565 return; 566 } 567 568 long pid = getProcessId(1); 569 log.trace("Process ID: {}", pid); 570 571 // get first network interface internet address 572 // almost certainly the wrong approach, isn't likely to 573 // find real IP address for coms, but it gets some entropy. 574 InetAddress address = null; 575 try { 576 NetworkInterface n = NetworkInterface.getNetworkInterfaces().nextElement(); 577 if (n != null) { 578 address = n.getInetAddresses().nextElement(); 579 } 580 log.debug("InetAddress: {}", address); 581 } catch (SocketException | java.util.NoSuchElementException e) { 582 // SocketException is part of the getNetworkInterfaces specification. 583 // java.util.NoSuchElementException seen on some Windows machines 584 // for unknown reasons. We provide a short error message in that case. 585 log.warn("Can't get IP address to make NodeID. You should set a NodeID in the Connection preferences."); 586 } 587 588 int b2 = 0; 589 if (address != null) { 590 b2 = address.getAddress()[0]; 591 } else { 592 b2 = (byte)(RANDOM.nextInt(255) & 0xFF); // & 0xFF not strictly necessary, but makes SpotBugs happy 593 log.trace("Used random value {} for address byte", b2); 594 } 595 596 // store new NodeID 597 nodeID = new NodeID(new byte[]{2, 1, 18, (byte) (b2 & 0xFF), (byte) ((pid >> 8) & 0xFF), (byte) (pid & 0xFF)}); 598 log.debug("getOurNodeID sets new Node ID: {}", nodeID); 599 600 } catch (Exception e) { 601 // We catch Exception here, instead of within the NetworkInterface lookup, because 602 // we want to know which kind of exceptions we're seeing. If/when this gets reported, 603 // generalize the catch statement above. 604 log.error("Unexpected Exception while processing Node ID definition. Please report this to the JMRI developers", e); 605 byte b2 = (byte)(RANDOM.nextInt(255) & 0xFF); // & 0xFF not strictly necessary, but makes SpotBugs happy 606 byte b1 = (byte)(RANDOM.nextInt(255) & 0xFF); 607 byte b0 = (byte)(RANDOM.nextInt(255) & 0xFF); 608 nodeID = new NodeID(new byte[]{2, 1, 18, b2, b1, b0}); 609 log.debug("Setting random Node ID: {}", nodeID); 610 } 611 } 612 613 private static final Random RANDOM = new Random(); 614 615 protected long getProcessId(final long fallback) { 616 // Note: may fail in some JVM implementations 617 // therefore fallback has to be provided 618 619 // something like '<pid>@<hostname>', at least in SUN / Oracle JVMs 620 final String jvmName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName(); 621 final int index = jvmName.indexOf('@'); 622 623 if (index < 1) { 624 // part before '@' empty (index = 0) / '@' not found (index = -1) 625 return fallback; 626 } 627 628 try { 629 return Long.parseLong(jvmName.substring(0, index)); 630 } catch (NumberFormatException e) { 631 // ignore 632 } 633 return fallback; 634 } 635 636 public static CanInterface createOlcbCanInterface(NodeID nodeID, TrafficController tc) { 637 final CanInterface olcbIf = new CanInterface(nodeID, frame -> tc.sendCanMessage(convertToCan(frame), null)); 638 tc.addCanListener(new CanListener() { 639 @Override 640 public void message(CanMessage m) { 641 // ignored -- loopback is handled by the olcbInterface. 642 } 643 644 @Override 645 public void reply(CanReply m) { 646 if (!m.isExtended() || m.isRtr()) { 647 return; 648 } 649 olcbIf.frameInput().send(convertFromCan(m)); 650 } 651 }); 652 olcbIf.getInterface().setLoopbackThread((Runnable r)->ThreadingUtil.runOnLayout(r::run)); 653 return olcbIf; 654 } 655 656 static jmri.jmrix.can.CanMessage convertToCan(org.openlcb.can.CanFrame f) { 657 jmri.jmrix.can.CanMessage fout = new jmri.jmrix.can.CanMessage(f.getData(), f.getHeader()); 658 fout.setExtended(true); 659 return fout; 660 } 661 662 static OpenLcbCanFrame convertFromCan(jmri.jmrix.can.CanFrame message) { 663 OpenLcbCanFrame fin = new OpenLcbCanFrame(0); 664 fin.setHeader(message.getHeader()); 665 if (message.getNumDataElements() == 0) { 666 return fin; 667 } 668 byte[] data = new byte[message.getNumDataElements()]; 669 for (int i = 0; i < data.length; ++i) { 670 data[i] = (byte) (message.getElement(i) & 0xff); 671 } 672 fin.setData(data); 673 return fin; 674 } 675 676 /** 677 * State machine to handle startup 678 */ 679 class StartUpHandler { 680 681 javax.swing.Timer timer; 682 683 static final int START_DELAY = 2500; 684 685 void start() { 686 log.debug("StartUpHandler starts up"); 687 // wait geological time for adapter startup 688 timer = new javax.swing.Timer(START_DELAY, new javax.swing.AbstractAction() { 689 690 @Override 691 public void actionPerformed(java.awt.event.ActionEvent e) { 692 Thread t = jmri.util.ThreadingUtil.newThread( 693 () -> { 694 // N.B. during JUnit testing, the following call tends to hang 695 // on semaphore acquisition in org.openlcb.can.CanInterface.initialize() 696 // near line 109 in openlcb lib 0.7.22, which leaves 697 // the thread hanging around forever. 698 olcbCanInterface.initialize(); 699 }, 700 "olcbCanInterface.initialize"); 701 t.start(); 702 } 703 }); 704 timer.setRepeats(false); 705 timer.start(); 706 } 707 } 708 709 private final static Logger log = LoggerFactory.getLogger(OlcbConfigurationManager.class); 710}