/src/rxtx-2.1-7r2/src/CommPortIdentifier.java

https://bitbucket.org/joschka_thurner/tc65sh-autodetect · Java · 413 lines · 234 code · 21 blank · 158 comment · 55 complexity · 913f24b8ee2ec34c3e35fe7df23544e2 MD5 · raw file

  1. /*-------------------------------------------------------------------------
  2. | rxtx is a native interface to serial ports in java.
  3. | Copyright 1997-2004 by Trent Jarvi taj@www.linux.org.uk
  4. |
  5. | This library is free software; you can redistribute it and/or
  6. | modify it under the terms of the GNU Library General Public
  7. | License as published by the Free Software Foundation; either
  8. | version 2 of the License, or (at your option) any later version.
  9. |
  10. | This library is distributed in the hope that it will be useful,
  11. | but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. | Library General Public License for more details.
  14. |
  15. | You should have received a copy of the GNU Library General Public
  16. | License along with this library; if not, write to the Free
  17. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. --------------------------------------------------------------------------*/
  19. package gnu.io;
  20. import java.io.FileDescriptor;
  21. import java.util.Vector;
  22. import java.util.Enumeration;
  23. /**
  24. * @author Trent Jarvi
  25. * @version %I%, %G%
  26. * @since JDK1.0
  27. */
  28. public class CommPortIdentifier extends Object /* extends Vector? */
  29. {
  30. public static final int PORT_SERIAL = 1; // rs232 Port
  31. public static final int PORT_PARALLEL = 2; // Parallel Port
  32. public static final int PORT_I2C = 3; // i2c Port
  33. public static final int PORT_RS485 = 4; // rs485 Port
  34. public static final int PORT_RAW = 5; // Raw Port
  35. private String PortName;
  36. private boolean Available = true;
  37. private String Owner;
  38. private CommPort commport;
  39. private CommDriver RXTXDriver;
  40. static CommPortIdentifier CommPortIndex;
  41. CommPortIdentifier next;
  42. private int PortType;
  43. private final static boolean debug = false;
  44. static Object Sync;
  45. Vector ownershipListener;
  46. /*------------------------------------------------------------------------------
  47. static {} aka initialization
  48. accept: -
  49. perform: load the rxtx driver
  50. return: -
  51. exceptions: Throwable
  52. comments: static block to initialize the class
  53. ------------------------------------------------------------------------------*/
  54. // initialization only done once....
  55. static
  56. {
  57. if(debug) System.out.println("CommPortIdentifier:static initialization()");
  58. Sync = new Object();
  59. try
  60. {
  61. CommDriver RXTXDriver = (CommDriver) Class.forName("gnu.io.RXTXCommDriver").newInstance();
  62. RXTXDriver.initialize();
  63. }
  64. catch (Throwable e)
  65. {
  66. System.err.println(e + " thrown while loading " + "gnu.io.RXTXCommDriver");
  67. }
  68. String OS;
  69. OS = System.getProperty("os.name");
  70. if(OS.toLowerCase().indexOf("linux") == -1)
  71. {
  72. if (debug)
  73. System.out.println("Have not implemented native_psmisc_report_owner(PortName)); in CommPortIdentifier");
  74. }
  75. System.loadLibrary( "rxtxSerial" );
  76. }
  77. CommPortIdentifier ( String pn, CommPort cp, int pt, CommDriver driver)
  78. {
  79. PortName = pn;
  80. commport = cp;
  81. PortType = pt;
  82. next = null;
  83. RXTXDriver = driver;
  84. }
  85. /*------------------------------------------------------------------------------
  86. addPortName()
  87. accept: Name of the port s, Port type,
  88. reverence to RXTXCommDriver.
  89. perform: place a new CommPortIdentifier in the linked list
  90. return: none.
  91. exceptions: none.
  92. comments:
  93. ------------------------------------------------------------------------------*/
  94. public static void addPortName(String s, int type, CommDriver c)
  95. {
  96. if(debug) System.out.println("CommPortIdentifier:addPortName("+s+")");
  97. AddIdentifierToList(new CommPortIdentifier(s, null, type, c));
  98. }
  99. /*------------------------------------------------------------------------------
  100. AddIdentifierToList()
  101. accept: The cpi to add to the list.
  102. perform:
  103. return:
  104. exceptions:
  105. comments:
  106. ------------------------------------------------------------------------------*/
  107. private static void AddIdentifierToList( CommPortIdentifier cpi)
  108. {
  109. if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList()");
  110. synchronized (Sync)
  111. {
  112. if (CommPortIndex == null)
  113. {
  114. CommPortIndex = cpi;
  115. if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList() null");
  116. }
  117. else
  118. {
  119. CommPortIdentifier index = CommPortIndex;
  120. while (index.next != null)
  121. {
  122. index = index.next;
  123. if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList() index.next");
  124. }
  125. index.next = cpi;
  126. }
  127. }
  128. }
  129. /*------------------------------------------------------------------------------
  130. addPortOwnershipListener()
  131. accept:
  132. perform:
  133. return:
  134. exceptions:
  135. comments:
  136. ------------------------------------------------------------------------------*/
  137. public void addPortOwnershipListener(CommPortOwnershipListener c)
  138. {
  139. if(debug) System.out.println("CommPortIdentifier:addPortOwnershipListener()");
  140. /* is the Vector instantiated? */
  141. if( ownershipListener == null )
  142. {
  143. ownershipListener = new Vector();
  144. }
  145. /* is the ownership listener already in the list? */
  146. if ( ownershipListener.contains(c) == false)
  147. {
  148. ownershipListener.addElement(c);
  149. }
  150. }
  151. /*------------------------------------------------------------------------------
  152. getCurrentOwner()
  153. accept:
  154. perform:
  155. return:
  156. exceptions:
  157. comments:
  158. ------------------------------------------------------------------------------*/
  159. public String getCurrentOwner()
  160. {
  161. if(debug) System.out.println("CommPortIdentifier:getCurrentOwner()");
  162. return( Owner );
  163. }
  164. /*------------------------------------------------------------------------------
  165. getName()
  166. accept:
  167. perform:
  168. return:
  169. exceptions:
  170. comments:
  171. ------------------------------------------------------------------------------*/
  172. public String getName()
  173. {
  174. if(debug) System.out.println("CommPortIdentifier:getName()");
  175. return( PortName );
  176. }
  177. /*------------------------------------------------------------------------------
  178. getPortIdentifier()
  179. accept:
  180. perform:
  181. return:
  182. exceptions:
  183. comments:
  184. ------------------------------------------------------------------------------*/
  185. static public CommPortIdentifier getPortIdentifier(String s) throws NoSuchPortException
  186. {
  187. if(debug) System.out.println("CommPortIdentifier:getPortIdentifier(" + s +")");
  188. CommPortIdentifier index = CommPortIndex;
  189. synchronized (Sync)
  190. {
  191. while (index != null)
  192. {
  193. if (index.PortName.equals(s)) break;
  194. index = index.next;
  195. }
  196. }
  197. if (index != null) return index;
  198. else
  199. {
  200. if ( debug )
  201. System.out.println("not found!" + s);
  202. throw new NoSuchPortException();
  203. }
  204. }
  205. /*------------------------------------------------------------------------------
  206. getPortIdentifier()
  207. accept:
  208. perform:
  209. return:
  210. exceptions:
  211. comments:
  212. ------------------------------------------------------------------------------*/
  213. static public CommPortIdentifier getPortIdentifier(CommPort p)
  214. throws NoSuchPortException
  215. {
  216. if(debug) System.out.println("CommPortIdentifier:getPortIdentifier(CommPort)");
  217. CommPortIdentifier c = CommPortIndex;
  218. synchronized( Sync )
  219. {
  220. while ( c != null && c.commport != p )
  221. c = c.next;
  222. }
  223. if ( c != null )
  224. return (c);
  225. if ( debug )
  226. System.out.println("not found!" + p.getName());
  227. throw new NoSuchPortException();
  228. }
  229. /*------------------------------------------------------------------------------
  230. getPortIdentifiers()
  231. accept:
  232. perform:
  233. return:
  234. exceptions:
  235. comments:
  236. ------------------------------------------------------------------------------*/
  237. static public Enumeration getPortIdentifiers()
  238. {
  239. if(debug) System.out.println("static CommPortIdentifier:getPortIdentifiers()");
  240. CommPortIndex = null;
  241. try
  242. {
  243. CommDriver RXTXDriver = (CommDriver) Class.forName("gnu.io.RXTXCommDriver").newInstance();
  244. RXTXDriver.initialize();
  245. }
  246. catch (Throwable e)
  247. {
  248. System.err.println(e + " thrown while loading " + "gnu.io.RXTXCommDriver");
  249. }
  250. return new CommPortEnumerator();
  251. }
  252. /*------------------------------------------------------------------------------
  253. getPortType()
  254. accept:
  255. perform:
  256. return:
  257. exceptions:
  258. comments:
  259. ------------------------------------------------------------------------------*/
  260. public int getPortType()
  261. {
  262. if(debug) System.out.println("CommPortIdentifier:getPortType()");
  263. return( PortType );
  264. }
  265. /*------------------------------------------------------------------------------
  266. isCurrentlyOwned()
  267. accept:
  268. perform:
  269. return:
  270. exceptions:
  271. comments:
  272. ------------------------------------------------------------------------------*/
  273. public synchronized boolean isCurrentlyOwned()
  274. {
  275. if(debug) System.out.println("CommPortIdentifier:isCurrentlyOwned()");
  276. return(!Available);
  277. }
  278. /*------------------------------------------------------------------------------
  279. open()
  280. accept:
  281. perform:
  282. return:
  283. exceptions:
  284. comments:
  285. ------------------------------------------------------------------------------*/
  286. public synchronized CommPort open(FileDescriptor f) throws UnsupportedCommOperationException
  287. {
  288. if(debug) System.out.println("CommPortIdentifier:open(FileDescriptor)");
  289. throw new UnsupportedCommOperationException();
  290. }
  291. private native String native_psmisc_report_owner(String PortName);
  292. /*------------------------------------------------------------------------------
  293. open()
  294. accept: application makeing the call and milliseconds to block
  295. during open.
  296. perform: open the port if possible
  297. return: CommPort if successfull
  298. exceptions: PortInUseException if in use.
  299. comments:
  300. ------------------------------------------------------------------------------*/
  301. private boolean HideOwnerEvents;
  302. public synchronized CommPort open(String TheOwner, int i)
  303. throws gnu.io.PortInUseException
  304. {
  305. if(debug) System.out.println("CommPortIdentifier:open("+TheOwner + ", " +i+")");
  306. if (Available == false)
  307. {
  308. synchronized (Sync)
  309. {
  310. fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED);
  311. try
  312. {
  313. wait(i);
  314. }
  315. catch ( InterruptedException e ) { }
  316. }
  317. }
  318. if (Available == false)
  319. {
  320. throw new gnu.io.PortInUseException(getCurrentOwner());
  321. }
  322. if(commport == null)
  323. {
  324. commport = RXTXDriver.getCommPort(PortName,PortType);
  325. }
  326. if(commport != null)
  327. {
  328. Owner = TheOwner;
  329. Available=false;
  330. fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNED);
  331. return commport;
  332. }
  333. else
  334. {
  335. throw new gnu.io.PortInUseException(
  336. native_psmisc_report_owner(PortName));
  337. }
  338. }
  339. /*------------------------------------------------------------------------------
  340. removePortOwnership()
  341. accept:
  342. perform:
  343. return:
  344. exceptions:
  345. comments:
  346. ------------------------------------------------------------------------------*/
  347. public void removePortOwnershipListener(CommPortOwnershipListener c)
  348. {
  349. if(debug) System.out.println("CommPortIdentifier:removePortOwnershipListener()");
  350. /* why is this called twice? */
  351. if(ownershipListener != null)
  352. ownershipListener.removeElement(c);
  353. }
  354. /*------------------------------------------------------------------------------
  355. internalClosePort()
  356. accept: None
  357. perform: clean up the Ownership information and send the event
  358. return: None
  359. exceptions: None
  360. comments: None
  361. ------------------------------------------------------------------------------*/
  362. synchronized void internalClosePort()
  363. {
  364. if(debug) System.out.println("CommPortIdentifier:internalClosePort()");
  365. Owner = null;
  366. Available = true;
  367. commport = null;
  368. /* this tosses null pointer?? */
  369. notifyAll();
  370. fireOwnershipEvent(CommPortOwnershipListener.PORT_UNOWNED);
  371. }
  372. /*------------------------------------------------------------------------------
  373. fireOwnershipEvent()
  374. accept:
  375. perform:
  376. return:
  377. exceptions:
  378. comments:
  379. ------------------------------------------------------------------------------*/
  380. void fireOwnershipEvent(int eventType)
  381. {
  382. if(debug) System.out.println("CommPortIdentifier:fireOwnershipEvent( " + eventType + " )");
  383. if (ownershipListener != null)
  384. {
  385. CommPortOwnershipListener c;
  386. for ( Enumeration e = ownershipListener.elements();
  387. e.hasMoreElements();
  388. c.ownershipChange(eventType))
  389. c = (CommPortOwnershipListener) e.nextElement();
  390. }
  391. }
  392. }