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