You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

405 lines
12 KiB

5 years ago
  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. /*
  76. System.loadLibrary( "rxtxSerial" );
  77. */
  78. }
  79. CommPortIdentifier ( String pn, CommPort cp, int pt, CommDriver driver)
  80. {
  81. PortName = pn;
  82. commport = cp;
  83. PortType = pt;
  84. next = null;
  85. RXTXDriver = driver;
  86. }
  87. /*------------------------------------------------------------------------------
  88. addPortName()
  89. accept: Name of the port s, Port type,
  90. reverence to RXTXCommDriver.
  91. perform: place a new CommPortIdentifier in the linked list
  92. return: none.
  93. exceptions: none.
  94. comments:
  95. ------------------------------------------------------------------------------*/
  96. public static void addPortName(String s, int type, CommDriver c)
  97. {
  98. if(debug) System.out.println("CommPortIdentifier:addPortName("+s+")");
  99. AddIdentifierToList(new CommPortIdentifier(s, null, type, c));
  100. }
  101. /*------------------------------------------------------------------------------
  102. AddIdentifierToList()
  103. accept: The cpi to add to the list.
  104. perform:
  105. return:
  106. exceptions:
  107. comments:
  108. ------------------------------------------------------------------------------*/
  109. private static void AddIdentifierToList( CommPortIdentifier cpi)
  110. {
  111. if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList()");
  112. synchronized (Sync)
  113. {
  114. if (CommPortIndex == null)
  115. {
  116. CommPortIndex = cpi;
  117. if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList() null");
  118. }
  119. else
  120. {
  121. CommPortIdentifier index = CommPortIndex;
  122. while (index.next != null)
  123. {
  124. index = index.next;
  125. if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList() index.next");
  126. }
  127. index.next = cpi;
  128. }
  129. }
  130. }
  131. /*------------------------------------------------------------------------------
  132. addPortOwnershipListener()
  133. accept:
  134. perform:
  135. return:
  136. exceptions:
  137. comments:
  138. ------------------------------------------------------------------------------*/
  139. public void addPortOwnershipListener(CommPortOwnershipListener c)
  140. {
  141. if(debug) System.out.println("CommPortIdentifier:addPortOwnershipListener()");
  142. /* is the Vector instantiated? */
  143. if( ownershipListener == null )
  144. {
  145. ownershipListener = new Vector();
  146. }
  147. /* is the ownership listener already in the list? */
  148. if ( ownershipListener.contains(c) == false)
  149. {
  150. ownershipListener.addElement(c);
  151. }
  152. }
  153. /*------------------------------------------------------------------------------
  154. getCurrentOwner()
  155. accept:
  156. perform:
  157. return:
  158. exceptions:
  159. comments:
  160. ------------------------------------------------------------------------------*/
  161. public String getCurrentOwner()
  162. {
  163. if(debug) System.out.println("CommPortIdentifier:getCurrentOwner()");
  164. return( Owner );
  165. }
  166. /*------------------------------------------------------------------------------
  167. getName()
  168. accept:
  169. perform:
  170. return:
  171. exceptions:
  172. comments:
  173. ------------------------------------------------------------------------------*/
  174. public String getName()
  175. {
  176. if(debug) System.out.println("CommPortIdentifier:getName()");
  177. return( PortName );
  178. }
  179. /*------------------------------------------------------------------------------
  180. getPortIdentifier()
  181. accept:
  182. perform:
  183. return:
  184. exceptions:
  185. comments:
  186. ------------------------------------------------------------------------------*/
  187. static public CommPortIdentifier getPortIdentifier(String s) throws NoSuchPortException
  188. {
  189. if(debug) System.out.println("CommPortIdentifier:getPortIdentifier(" + s +")");
  190. CommPortIdentifier index = CommPortIndex;
  191. synchronized (Sync)
  192. {
  193. while (index != null)
  194. {
  195. if (index.PortName.equals(s)) break;
  196. index = index.next;
  197. }
  198. }
  199. if (index != null) return index;
  200. else
  201. {
  202. if ( debug )
  203. System.out.println("not found!" + s);
  204. throw new NoSuchPortException();
  205. }
  206. }
  207. /*------------------------------------------------------------------------------
  208. getPortIdentifier()
  209. accept:
  210. perform:
  211. return:
  212. exceptions:
  213. comments:
  214. ------------------------------------------------------------------------------*/
  215. static public CommPortIdentifier getPortIdentifier(CommPort p)
  216. throws NoSuchPortException
  217. {
  218. if(debug) System.out.println("CommPortIdentifier:getPortIdentifier(CommPort)");
  219. CommPortIdentifier c = CommPortIndex;
  220. synchronized( Sync )
  221. {
  222. while ( c != null && c.commport != p )
  223. c = c.next;
  224. }
  225. if ( c != null )
  226. return (c);
  227. if ( debug )
  228. System.out.println("not found!" + p.getName());
  229. throw new NoSuchPortException();
  230. }
  231. /*------------------------------------------------------------------------------
  232. getPortIdentifiers()
  233. accept:
  234. perform:
  235. return:
  236. exceptions:
  237. comments:
  238. ------------------------------------------------------------------------------*/
  239. static public Enumeration getPortIdentifiers()
  240. {
  241. if(debug) System.out.println("static CommPortIdentifier:getPortIdentifiers()");
  242. return new CommPortEnumerator();
  243. }
  244. /*------------------------------------------------------------------------------
  245. getPortType()
  246. accept:
  247. perform:
  248. return:
  249. exceptions:
  250. comments:
  251. ------------------------------------------------------------------------------*/
  252. public int getPortType()
  253. {
  254. if(debug) System.out.println("CommPortIdentifier:getPortType()");
  255. return( PortType );
  256. }
  257. /*------------------------------------------------------------------------------
  258. isCurrentlyOwned()
  259. accept:
  260. perform:
  261. return:
  262. exceptions:
  263. comments:
  264. ------------------------------------------------------------------------------*/
  265. public synchronized boolean isCurrentlyOwned()
  266. {
  267. if(debug) System.out.println("CommPortIdentifier:isCurrentlyOwned()");
  268. return(!Available);
  269. }
  270. /*------------------------------------------------------------------------------
  271. open()
  272. accept:
  273. perform:
  274. return:
  275. exceptions:
  276. comments:
  277. ------------------------------------------------------------------------------*/
  278. public synchronized CommPort open(FileDescriptor f) throws UnsupportedCommOperationException
  279. {
  280. if(debug) System.out.println("CommPortIdentifier:open(FileDescriptor)");
  281. throw new UnsupportedCommOperationException();
  282. }
  283. private native String native_psmisc_report_owner(String PortName);
  284. /*------------------------------------------------------------------------------
  285. open()
  286. accept: application makeing the call and milliseconds to block
  287. during open.
  288. perform: open the port if possible
  289. return: CommPort if successfull
  290. exceptions: PortInUseException if in use.
  291. comments:
  292. ------------------------------------------------------------------------------*/
  293. private boolean HideOwnerEvents;
  294. public synchronized CommPort open(String TheOwner, int i)
  295. throws gnu.io.PortInUseException
  296. {
  297. if(debug) System.out.println("CommPortIdentifier:open("+TheOwner + ", " +i+")");
  298. if (Available == false)
  299. {
  300. synchronized (Sync)
  301. {
  302. fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED);
  303. try
  304. {
  305. wait(i);
  306. }
  307. catch ( InterruptedException e ) { }
  308. }
  309. }
  310. if (Available == false)
  311. {
  312. throw new gnu.io.PortInUseException(getCurrentOwner());
  313. }
  314. if(commport == null)
  315. {
  316. commport = RXTXDriver.getCommPort(PortName,PortType);
  317. }
  318. if(commport != null)
  319. {
  320. Owner = TheOwner;
  321. Available=false;
  322. fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNED);
  323. return commport;
  324. }
  325. else
  326. {
  327. throw new gnu.io.PortInUseException(
  328. native_psmisc_report_owner(PortName));
  329. }
  330. }
  331. /*------------------------------------------------------------------------------
  332. removePortOwnership()
  333. accept:
  334. perform:
  335. return:
  336. exceptions:
  337. comments:
  338. ------------------------------------------------------------------------------*/
  339. public void removePortOwnershipListener(CommPortOwnershipListener c)
  340. {
  341. if(debug) System.out.println("CommPortIdentifier:removePortOwnershipListener()");
  342. /* why is this called twice? */
  343. if(ownershipListener != null)
  344. ownershipListener.removeElement(c);
  345. }
  346. /*------------------------------------------------------------------------------
  347. internalClosePort()
  348. accept: None
  349. perform: clean up the Ownership information and send the event
  350. return: None
  351. exceptions: None
  352. comments: None
  353. ------------------------------------------------------------------------------*/
  354. synchronized void internalClosePort()
  355. {
  356. if(debug) System.out.println("CommPortIdentifier:internalClosePort()");
  357. Owner = null;
  358. Available = true;
  359. commport = null;
  360. /* this tosses null pointer?? */
  361. notifyAll();
  362. fireOwnershipEvent(CommPortOwnershipListener.PORT_UNOWNED);
  363. }
  364. /*------------------------------------------------------------------------------
  365. fireOwnershipEvent()
  366. accept:
  367. perform:
  368. return:
  369. exceptions:
  370. comments:
  371. ------------------------------------------------------------------------------*/
  372. void fireOwnershipEvent(int eventType)
  373. {
  374. if(debug) System.out.println("CommPortIdentifier:fireOwnershipEvent( " + eventType + " )");
  375. if (ownershipListener != null)
  376. {
  377. CommPortOwnershipListener c;
  378. for ( Enumeration e = ownershipListener.elements();
  379. e.hasMoreElements();
  380. c.ownershipChange(eventType))
  381. c = (CommPortOwnershipListener) e.nextElement();
  382. }
  383. }
  384. }