Merge test folder of test branch
[iotivity.git] / test / src / automation / robot / device_lib / src / org / iotivity / test / rfw / common / devicecontroller / android / AndroidNetworkManager.java
1 /******************************************************************\r
2  *\r
3  * Copyright 2018 Samsung Electronics All Rights Reserved.\r
4  *\r
5  *\r
6  *\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  *\r
20  ******************************************************************/\r
21 \r
22 package org.iotivity.test.rfw.common.devicecontroller.android;\r
23 \r
24 import java.io.BufferedReader;\r
25 import java.io.File;\r
26 import java.io.IOException;\r
27 import java.io.InputStreamReader;\r
28 import java.util.ArrayList;\r
29 import java.util.logging.Logger;\r
30 \r
31 import org.iotivity.test.rfw.common.devicecontroller.interfaces.INetworkController;\r
32 import org.iotivity.test.rfw.common.util.IoTivityLogger;\r
33 import org.iotivity.test.rfw.common.util.SystemUtil;\r
34 \r
35 import org.iotivity.test.rfw.common.devicecontroller.android.AndroidCommand;\r
36 \r
37 class AndroidNetworkManager implements INetworkController {\r
38 \r
39     private Logger logger  = IoTivityLogger.GetLogger();\r
40     private String sdkPath = "";\r
41 \r
42     @Override\r
43     public boolean wifiOn() {\r
44 \r
45         String[] command = { AndroidCommand.ADB, AndroidCommand.SHELL,\r
46                 AndroidCommand.ROOT, AndroidCommand.CLEAN,\r
47                 AndroidCommand.WIFI_SERVICE_ENABLE };\r
48 \r
49         try {\r
50 \r
51             BufferedReader in = getProcessBuilderReader(command);\r
52 \r
53             SystemUtil.sleep(1000);\r
54 \r
55             String line = null;\r
56             while ((line = in.readLine()) != null) {\r
57                 if (line.contains("fail"))\r
58                     return false;\r
59             }\r
60         } catch (IOException e) {\r
61             e.printStackTrace();\r
62         }\r
63 \r
64         return true;\r
65     }\r
66 \r
67     @Override\r
68     public boolean wifiOff() {\r
69 \r
70         String[] command = { AndroidCommand.ADB, AndroidCommand.SHELL,\r
71                 AndroidCommand.ROOT, AndroidCommand.CLEAN,\r
72                 AndroidCommand.WIFI_SERVICE_DISABLE };\r
73 \r
74         try {\r
75 \r
76             BufferedReader in = getProcessBuilderReader(command);\r
77 \r
78             SystemUtil.sleep(1000);\r
79 \r
80             String line = null;\r
81             while ((line = in.readLine()) != null) {\r
82                 if (line.contains("fail"))\r
83                     return false;\r
84             }\r
85         } catch (IOException e) {\r
86             e.printStackTrace();\r
87         }\r
88         return true;\r
89     }\r
90 \r
91     @Override\r
92     public boolean blueToothOn() {\r
93 \r
94         try {\r
95             Process process = Runtime.getRuntime()\r
96                     .exec(AndroidCommand.BLUETOOTH_SERVICE_ENABLE);\r
97             BufferedReader in = new BufferedReader(\r
98                     new InputStreamReader(process.getInputStream()));\r
99 \r
100             SystemUtil.sleep(1000);\r
101 \r
102             String line = null;\r
103             while ((line = in.readLine()) != null) {\r
104                 if (line.contains("fail"))\r
105                     return false;\r
106             }\r
107         } catch (IOException e) {\r
108             e.printStackTrace();\r
109         }\r
110 \r
111         return true;\r
112 \r
113     }\r
114 \r
115     @Override\r
116     public boolean blueToothOff() {\r
117 \r
118         String[] command = { AndroidCommand.ADB, AndroidCommand.SHELL,\r
119                 AndroidCommand.ROOT, AndroidCommand.CLEAN,\r
120                 AndroidCommand.BLUETOOTH_SERVICE_DISABLE };\r
121 \r
122         try {\r
123 \r
124             BufferedReader in = getProcessBuilderReader(command);\r
125 \r
126             SystemUtil.sleep(1000);\r
127 \r
128             String line = null;\r
129             while ((line = in.readLine()) != null) {\r
130                 if (line.contains("fail"))\r
131                     return false;\r
132             }\r
133         } catch (IOException e) {\r
134             e.printStackTrace();\r
135         }\r
136 \r
137         return true;\r
138     }\r
139 \r
140     @Override\r
141     public String getIPAddress() {\r
142 \r
143         String line = null;\r
144         String ret = null;\r
145         try {\r
146             Process process = Runtime.getRuntime()\r
147                     .exec(AndroidCommand.GET_IP_CMD);\r
148             BufferedReader in = new BufferedReader(\r
149                     new InputStreamReader(process.getInputStream()));\r
150 \r
151             SystemUtil.sleep(1000);\r
152 \r
153             while ((line = in.readLine()) != null) {\r
154                 logger.info(line + "\n");\r
155                 if (line.contains("fail"))\r
156                     return null;\r
157                 else if (line.contains("wlan0")) {\r
158                     if (line.contains("inet")) {\r
159                         int start = line.indexOf("inet") + "inet".length();\r
160                         int end = line.indexOf("/");\r
161                         ret = line.substring(start, end);\r
162                         return ret;\r
163                     }\r
164                 }\r
165             }\r
166         } catch (IOException e) {\r
167             e.printStackTrace();\r
168         }\r
169 \r
170         return ret;\r
171     }\r
172 \r
173     @Override\r
174     public String getBluetoothMacAddress() {\r
175         String line = null;\r
176         try {\r
177 \r
178             Process process = Runtime.getRuntime()\r
179                     .exec(AndroidCommand.GET_MAC_ADDRESS);\r
180             BufferedReader in = new BufferedReader(\r
181                     new InputStreamReader(process.getInputStream()));\r
182 \r
183             SystemUtil.sleep(1000);\r
184             while ((line = in.readLine()) != null) {\r
185                 if (line.contains("fail"))\r
186                     return null;\r
187                 else\r
188                     return line;\r
189             }\r
190         } catch (IOException e) {\r
191             e.printStackTrace();\r
192         }\r
193 \r
194         return line;\r
195     }\r
196 \r
197     @Override\r
198     public ArrayList<String> getNetworkInformations() {\r
199         return null;\r
200     }\r
201 \r
202     @Override\r
203     public boolean setSDKPath(String sdkPath) {\r
204         if (sdkPath != null) {\r
205             this.sdkPath = sdkPath;\r
206             return true;\r
207         } else\r
208             return false;\r
209     }\r
210 \r
211     public BufferedReader getProcessBuilderReader(String[] command) {\r
212         ProcessBuilder probuilder = null;\r
213         Process process = null;\r
214         try {\r
215             probuilder = new ProcessBuilder(command);\r
216 \r
217             // You can set up your work directory\r
218             probuilder.directory(new File(sdkPath));\r
219             process = probuilder.start();\r
220 \r
221         } catch (Exception e) {\r
222             e.printStackTrace();\r
223         }\r
224 \r
225         return new BufferedReader(\r
226                 new InputStreamReader(process.getInputStream()));\r
227     }\r
228 \r
229 }