]> Pileus Git - ~andy/csm213a-hw/blob - syc/src/com/example/sycapp/SerialPort.java
Add Android app from Yue
[~andy/csm213a-hw] / syc / src / com / example / sycapp / SerialPort.java
1 /*
2  * Copyright 2009 Cedric Priscal
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. 
15  */
16
17 package com.example.sycapp;
18
19 import java.io.File;
20 import java.io.FileDescriptor;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26
27 import android.util.Log;
28
29 public class SerialPort {
30
31         private static final String TAG = "SerialPort";
32
33         /*
34          * Do not remove or rename the field mFd: it is used by native method close();
35          */
36         private FileDescriptor mFd;
37         private FileInputStream mFileInputStream;
38         private FileOutputStream mFileOutputStream;
39
40         public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException {
41
42                 /* Check access permission */
43                 if (!device.canRead() || !device.canWrite()) {
44                         try {
45                                 /* Missing read/write permission, trying to chmod the file */
46                                 Process su;
47                                 su = Runtime.getRuntime().exec("/system/bin/su");
48                                 String cmd = "chmod 666 " + device.getAbsolutePath() + "\n"
49                                                 + "exit\n";
50                                 su.getOutputStream().write(cmd.getBytes());
51                                 if ((su.waitFor() != 0) || !device.canRead()
52                                                 || !device.canWrite()) {
53                                         throw new SecurityException();
54                                 }
55                         } catch (Exception e) {
56                                 e.printStackTrace();
57                                 throw new SecurityException();
58                         }
59                 }
60
61                 mFd = open(device.getAbsolutePath(), baudrate, flags);
62                 if (mFd == null) {
63                         Log.e(TAG, "native open returns null");
64                         throw new IOException();
65                 }
66                 mFileInputStream = new FileInputStream(mFd);
67                 mFileOutputStream = new FileOutputStream(mFd);
68         }
69
70         // Getters and setters
71         public InputStream getInputStream() {
72                 return mFileInputStream;
73         }
74
75         public OutputStream getOutputStream() {
76                 return mFileOutputStream;
77         }
78
79         // JNI
80         private native static FileDescriptor open(String path, int baudrate, int flags);
81         public native void close();
82         static {
83                 System.loadLibrary("serial_port");
84         }
85 }