| 1 | /*------------------------------------------------------------------------- |
| 2 | | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface. |
| 3 | | RXTX is a native interface to serial ports in java. |
| 4 | | Copyright 2002-2007 by Trent Jarvi [email protected] and others who |
| 5 | | actually wrote it. See individual source files for more information. |
| 6 | | |
| 7 | | A copy of the LGPL v 2.1 may be found at |
| 8 | | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is |
| 9 | | here for your convenience. |
| 10 | | |
| 11 | | This library is free software; you can redistribute it and/or |
| 12 | | modify it under the terms of the GNU Lesser General Public |
| 13 | | License as published by the Free Software Foundation; either |
| 14 | | version 2.1 of the License, or (at your option) any later version. |
| 15 | | |
| 16 | | This library is distributed in the hope that it will be useful, |
| 17 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | | Lesser General Public License for more details. |
| 20 | | |
| 21 | | An executable that contains no derivative of any portion of RXTX, but |
| 22 | | is designed to work with RXTX by being dynamically linked with it, |
| 23 | | is considered a "work that uses the Library" subject to the terms and |
| 24 | | conditions of the GNU Lesser General Public License. |
| 25 | | |
| 26 | | The following has been added to the RXTX License to remove |
| 27 | | any confusion about linking to RXTX. We want to allow in part what |
| 28 | | section 5, paragraph 2 of the LGPL does not permit in the special |
| 29 | | case of linking over a controlled interface. The intent is to add a |
| 30 | | Java Specification Request or standards body defined interface in the |
| 31 | | future as another exception but one is not currently available. |
| 32 | | |
| 33 | | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface |
| 34 | | |
| 35 | | As a special exception, the copyright holders of RXTX give you |
| 36 | | permission to link RXTX with independent modules that communicate with |
| 37 | | RXTX solely through the Sun Microsytems CommAPI interface version 2, |
| 38 | | regardless of the license terms of these independent modules, and to copy |
| 39 | | and distribute the resulting combined work under terms of your choice, |
| 40 | | provided that every copy of the combined work is accompanied by a complete |
| 41 | | copy of the source code of RXTX (the version of RXTX used to produce the |
| 42 | | combined work), being distributed under the terms of the GNU Lesser General |
| 43 | | Public License plus this exception. An independent module is a |
| 44 | | module which is not derived from or based on RXTX. |
| 45 | | |
| 46 | | Note that people who make modified versions of RXTX are not obligated |
| 47 | | to grant this special exception for their modified versions; it is |
| 48 | | their choice whether to do so. The GNU Lesser General Public License |
| 49 | | gives permission to release a modified version without this exception; this |
| 50 | | exception also makes it possible to release a modified version which |
| 51 | | carries forward this exception. |
| 52 | | |
| 53 | | You should have received a copy of the GNU Lesser General Public |
| 54 | | License along with this library; if not, write to the Free |
| 55 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 56 | | All trademarks belong to their respective owners. |
| 57 | --------------------------------------------------------------------------*/ |
| 58 | package gnu.io; |
| 59 | |
| 60 | import java.io.RandomAccessFile; |
| 61 | |
| 62 | public class Zystem |
| 63 | { |
| 64 | public static final int SILENT_MODE = 0; |
| 65 | public static final int FILE_MODE = 1; |
| 66 | public static final int NET_MODE = 2; |
| 67 | public static final int MEX_MODE = 3; |
| 68 | public static final int PRINT_MODE = 4; |
| 69 | public static final int J2EE_MSG_MODE = 5; |
| 70 | public static final int J2SE_LOG_MODE = 6; |
| 71 | |
| 72 | static int mode; |
| 73 | |
| 74 | static |
| 75 | { |
| 76 | /* |
| 77 | The rxtxZystem library uses Python code and is not |
| 78 | included with RXTX. A seperate library will be released |
| 79 | to avoid potential license conflicts. |
| 80 | |
| 81 | Trent Jarvi [email protected] |
| 82 | */ |
| 83 | |
| 84 | //System.loadLibrary( "rxtxZystem" ); |
| 85 | mode = SILENT_MODE; |
| 86 | } |
| 87 | |
| 88 | private static String target; |
| 89 | |
| 90 | public Zystem( int m ) throws UnSupportedLoggerException |
| 91 | { |
| 92 | mode = m; |
| 93 | startLogger( "asdf" ); |
| 94 | } |
| 95 | /** |
| 96 | * Constructor. |
| 97 | * Mode is taken from the java system property "gnu.io.log.mode". The available values are :<ul> |
| 98 | * <li> SILENT_MODE No logging |
| 99 | * <li> FILE_MODE log to file |
| 100 | * <li> NET_MODE |
| 101 | * <li> MEX_MODE |
| 102 | * <li> PRINT_MODE |
| 103 | * <li> J2EE_MSG_MODE |
| 104 | * <li> J2SE_LOG_MODE log to java.util.logging |
| 105 | * </ul> |
| 106 | */ |
| 107 | public Zystem () throws UnSupportedLoggerException |
| 108 | { |
| 109 | String s = System.getProperty ("gnu.io.log.mode"); |
| 110 | if (s != null) |
| 111 | { |
| 112 | if ("SILENT_MODE".equals (s)) |
| 113 | { |
| 114 | mode = SILENT_MODE; |
| 115 | } |
| 116 | else if ("FILE_MODE".equals (s)) |
| 117 | { |
| 118 | mode = FILE_MODE; |
| 119 | } |
| 120 | else if ("NET_MODE".equals (s)) |
| 121 | { |
| 122 | mode = NET_MODE; |
| 123 | } |
| 124 | else if ("MEX_MODE".equals (s)) |
| 125 | { |
| 126 | mode = MEX_MODE; |
| 127 | } |
| 128 | else if ("PRINT_MODE".equals (s)) |
| 129 | { |
| 130 | mode = PRINT_MODE; |
| 131 | } |
| 132 | else if ("J2EE_MSG_MODE".equals (s)) |
| 133 | { |
| 134 | mode = J2EE_MSG_MODE; |
| 135 | } |
| 136 | else if ("J2SE_LOG_MODE".equals (s)) |
| 137 | { |
| 138 | mode = J2SE_LOG_MODE; |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | try |
| 143 | { |
| 144 | mode = Integer.parseInt (s); |
| 145 | } |
| 146 | catch (NumberFormatException e) |
| 147 | { |
| 148 | mode = SILENT_MODE; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | mode = SILENT_MODE; |
| 155 | } |
| 156 | startLogger ("asdf"); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | public void startLogger( ) throws UnSupportedLoggerException |
| 161 | { |
| 162 | if ( mode == SILENT_MODE || mode == PRINT_MODE ) |
| 163 | { |
| 164 | //nativeNetInit( ); |
| 165 | return; |
| 166 | } |
| 167 | throw new UnSupportedLoggerException( "Target Not Allowed" ); |
| 168 | } |
| 169 | |
| 170 | /* accept the host or file to log to. */ |
| 171 | |
| 172 | public void startLogger( String t ) throws UnSupportedLoggerException |
| 173 | { |
| 174 | target = t; |
| 175 | /* |
| 176 | if ( mode == NET_MODE ) |
| 177 | { |
| 178 | nativeNetInit( ); |
| 179 | } |
| 180 | if ( nativeInit( ) ) |
| 181 | { |
| 182 | throw new UnSupportedLoggerException( |
| 183 | "Port initializion failed" ); |
| 184 | } |
| 185 | */ |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | public void finalize() |
| 190 | { |
| 191 | /* |
| 192 | if ( mode == NET_MODE ) |
| 193 | { |
| 194 | nativeNetFinalize( ); |
| 195 | } |
| 196 | nativeFinalize(); |
| 197 | */ |
| 198 | mode = SILENT_MODE; |
| 199 | target = null; |
| 200 | } |
| 201 | |
| 202 | public void filewrite( String s ) |
| 203 | { |
| 204 | try { |
| 205 | RandomAccessFile w = |
| 206 | new RandomAccessFile( target, "rw" ); |
| 207 | w.seek( w.length() ); |
| 208 | w.writeBytes( s ); |
| 209 | w.close(); |
| 210 | } catch ( Exception e ) { |
| 211 | System.out.println("Debug output file write failed"); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | public boolean report( String s) |
| 216 | { |
| 217 | if ( mode == NET_MODE ) |
| 218 | { |
| 219 | // return( nativeNetReportln( s ) ); |
| 220 | } |
| 221 | else if ( mode == PRINT_MODE ) |
| 222 | { |
| 223 | System.out.println( s ); |
| 224 | return( true ); |
| 225 | } |
| 226 | else if ( mode == MEX_MODE ) |
| 227 | { |
| 228 | // return( nativeMexReport( s ) ); |
| 229 | } |
| 230 | else if ( mode == SILENT_MODE ) |
| 231 | { |
| 232 | return( true ); |
| 233 | } |
| 234 | else if ( mode == FILE_MODE ) |
| 235 | { |
| 236 | filewrite( s ); |
| 237 | } |
| 238 | else if ( mode == J2EE_MSG_MODE ) |
| 239 | { |
| 240 | return( false ); |
| 241 | } |
| 242 | else if (mode == J2SE_LOG_MODE) |
| 243 | { |
| 244 | java.util.logging.Logger.getLogger ("gnu.io").fine (s); |
| 245 | return (true); |
| 246 | } |
| 247 | return( false ); |
| 248 | } |
| 249 | |
| 250 | public boolean reportln( ) |
| 251 | { |
| 252 | boolean b; |
| 253 | if ( mode == NET_MODE ) |
| 254 | { |
| 255 | // b= nativeNetReportln( "\n" ); |
| 256 | // return(b); |
| 257 | } |
| 258 | else if ( mode == PRINT_MODE ) |
| 259 | { |
| 260 | System.out.println( ); |
| 261 | return( true ); |
| 262 | } |
| 263 | else if ( mode == MEX_MODE ) |
| 264 | { |
| 265 | // b = nativeMexReportln( "\n" ); |
| 266 | // return(b); |
| 267 | } |
| 268 | else if ( mode == SILENT_MODE ) |
| 269 | { |
| 270 | return( true ); |
| 271 | } |
| 272 | else if ( mode == FILE_MODE ) |
| 273 | { |
| 274 | filewrite( "\n" ); |
| 275 | } |
| 276 | else if ( mode == J2EE_MSG_MODE ) |
| 277 | { |
| 278 | return( false ); |
| 279 | } |
| 280 | return( false ); |
| 281 | } |
| 282 | |
| 283 | public boolean reportln( String s) |
| 284 | { |
| 285 | boolean b; |
| 286 | if ( mode == NET_MODE ) |
| 287 | { |
| 288 | // b= nativeNetReportln( s + "\n" ); |
| 289 | // return(b); |
| 290 | } |
| 291 | else if ( mode == PRINT_MODE ) |
| 292 | { |
| 293 | System.out.println( s ); |
| 294 | return( true ); |
| 295 | } |
| 296 | else if ( mode == MEX_MODE ) |
| 297 | { |
| 298 | // b = nativeMexReportln( s + "\n" ); |
| 299 | // return(b); |
| 300 | } |
| 301 | else if ( mode == SILENT_MODE ) |
| 302 | { |
| 303 | return( true ); |
| 304 | } |
| 305 | else if ( mode == FILE_MODE ) |
| 306 | { |
| 307 | filewrite( s + "\n" ); |
| 308 | } |
| 309 | else if ( mode == J2EE_MSG_MODE ) |
| 310 | { |
| 311 | return( false ); |
| 312 | } |
| 313 | else if (mode == J2SE_LOG_MODE) |
| 314 | { |
| 315 | return (true); |
| 316 | } |
| 317 | return( false ); |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | private native boolean nativeInit( ); |
| 322 | private native void nativeFinalize(); |
| 323 | */ |
| 324 | |
| 325 | /* open and close the socket */ |
| 326 | /* |
| 327 | private native boolean nativeNetInit( ); |
| 328 | private native void nativeNetFinalize(); |
| 329 | */ |
| 330 | |
| 331 | /* dumping to a remote machine */ |
| 332 | /* |
| 333 | public native boolean nativeNetReport( String s ); |
| 334 | public native boolean nativeNetReportln( String s ); |
| 335 | */ |
| 336 | |
| 337 | /* specific to Matlab */ |
| 338 | /* |
| 339 | public native boolean nativeMexReport( String s ); |
| 340 | public native boolean nativeMexReportln( String s ); |
| 341 | */ |
| 342 | } |