• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <iostream>
17 #include <unistd.h>
18 #include <map>
19 #include <string>
20 
21 #include "telephony_types.h"
22 
23 #include "cellular_data_client.h"
24 
25 namespace OHOS {
26 namespace Telephony {
27 class CellularDataCodeTest {
28     using Fun = void (*)();
29 public:
30     CellularDataCodeTest() = default;
31 
32     ~CellularDataCodeTest() = default;
33 
IsCellularDataEnabledTest()34     static void IsCellularDataEnabledTest()
35     {
36         int32_t result = CellularDataClient::GetInstance().IsCellularDataEnabled();
37         std::cout << "TelephonyTestService Remote IsCellularDataEnabled result [" << result << "]" << std::endl;
38     }
39 
EnableCellularDataTest()40     static void EnableCellularDataTest()
41     {
42         const int32_t maxTestCount = 61;
43         const uint32_t spaceTime = 1000 * 100 * 2.6;
44         const int32_t useMaxType = 1;
45         const int32_t evenNumber = 2;
46         int32_t type = 0;
47         std::cout << "please input whether enable: enable/1,disable/0 " << std::endl;
48         std::cin >> type;
49         int32_t result = 0;
50         if (type > useMaxType) {
51             for (int32_t i = 1; i < maxTestCount; i++) {
52                 bool dataEnable = i % evenNumber;
53                 if (dataEnable) {
54                     std::cout << "enable====: " << dataEnable << std::endl;
55                     CellularDataClient::GetInstance().EnableCellularData(dataEnable);
56                     usleep(spaceTime);
57                     result = CellularDataClient::GetInstance().GetCellularDataState();
58                     std::cout << "Remote GetCellularDataState result [" << result << "]" << std::endl;
59                 } else {
60                     std::cout << "enable false====: " << dataEnable << std::endl;
61                     CellularDataClient::GetInstance().EnableCellularData(dataEnable);
62                     usleep(spaceTime);
63                     result = CellularDataClient::GetInstance().GetCellularDataState();
64                     std::cout << "Remote GetCellularDataState result [" << result << "]" << std::endl;
65                 }
66                 std::cout << "i := " << i << std::endl;
67             }
68             return;
69         }
70         bool enable = (type > 0);
71         result = CellularDataClient::GetInstance().EnableCellularData(enable);
72         std::cout << "Remote EnableCellularData " << enable <<" result [" << result << "]" << std::endl;
73     }
74 
GetCellularDataStateTest()75     static void GetCellularDataStateTest()
76     {
77         int32_t result = CellularDataClient::GetInstance().GetCellularDataState();
78         std::cout << "Remote GetCellularDataState result [" << result << "]" << std::endl;
79     }
80 
IsCellularDataRoamingEnabledTest()81     static void IsCellularDataRoamingEnabledTest()
82     {
83         int32_t slotId = DEFAULT_SIM_SLOT_ID;
84         std::cout << "please input parameter int slotId" << std::endl;
85         std::cin >> slotId;
86         int32_t result = CellularDataClient::GetInstance().IsCellularDataRoamingEnabled(slotId);
87         std::cout << "Remote IsCellularDataRoamingEnabled result [" << result << "]" << std::endl;
88     }
89 
EnableCellularDataRoamingTest()90     static void EnableCellularDataRoamingTest()
91     {
92         int32_t slotId = DEFAULT_SIM_SLOT_ID;
93         int32_t type = 0;
94         std::cout << "please input parameter int slotId " << std::endl;
95         std::cin >> slotId;
96         std::cout << "please input parameter roaming enable: enable/1,disable/0" << std::endl;
97         std::cin >> type;
98         bool enable = (type > 0);
99         int32_t result = CellularDataClient::GetInstance().EnableCellularDataRoaming(slotId, enable);
100         std::cout << "Remote GetOperatorName " << enable << " result [" << result << "]" << std::endl;
101     }
102 
HandleApnChangedTest()103     static void HandleApnChangedTest()
104     {
105         int32_t slotId = DEFAULT_SIM_SLOT_ID;
106         sptr<ICellularDataManager> proxy = CellularDataClient::GetInstance().GetProxy();
107         if (proxy != nullptr) {
108             int32_t result = proxy->HandleApnChanged(slotId);
109             std::cout << "TelephonyTestService Remote result [" << result << "]" << std::endl;
110         } else {
111             std::cout << "TelephonyTestService Remote is null" << std::endl;
112         }
113     }
114 
GetDefaultSlotId()115     static void GetDefaultSlotId()
116     {
117         sptr<ICellularDataManager> proxy = CellularDataClient::GetInstance().GetProxy();
118         if (proxy != nullptr) {
119             int32_t result = proxy->GetDefaultCellularDataSlotId();
120             std::cout << "get default slot id is := " << result << std::endl;
121         } else {
122             std::cout << "get default slot id fail" << std::endl;
123         }
124     }
125 
SetDefaultSlotId()126     static void SetDefaultSlotId()
127     {
128         int32_t slotId = DEFAULT_SIM_SLOT_ID;
129         std::cout << "please input parameter int slot" << std::endl;
130         std::cin >> slotId;
131         sptr<ICellularDataManager> proxy = CellularDataClient::GetInstance().GetProxy();
132         if (proxy != nullptr) {
133             int32_t result = proxy->SetDefaultCellularDataSlotId(slotId);
134             std::cout << "TelephonyTestService SetDefaultSlotId is " << result << std::endl;
135         } else {
136             std::cout << "TelephonyTestService SetDefaultSlotId is null" << std::endl;
137         }
138     }
139 
GetDataFlowType()140     static void GetDataFlowType()
141     {
142         sptr<ICellularDataManager> proxy = CellularDataClient::GetInstance().GetProxy();
143         if (proxy != nullptr) {
144             int32_t result = proxy->GetCellularDataFlowType();
145             std::cout << "get CellDataFlowType is := " << result << std::endl;
146         } else {
147             std::cout << "get CellDataFlowType is fail" << std::endl;
148         }
149     }
150 
TestDataPackageUp()151     static void TestDataPackageUp()
152     {
153         system("rm /data/iface_tx_p");
154         system("touch /data/iface_rx_p");
155         if (access("/data/iface_tx_p", F_OK) == 0) {
156             std::cout << "Please manually delete iface_tx_p file " << std::endl;
157             return;
158         }
159         if (access("/data/iface_tx_p", F_OK) != 0) {
160             std::cout << "Please manually touch iface_tx_p file" << std::endl;
161         }
162     }
163 
TestDataPackageDown()164     static void TestDataPackageDown()
165     {
166         system("rm /data/iface_rx_p");
167         system("touch /data/iface_tx_p");
168         if (access("/data/iface_rx_p", F_OK) == 0) {
169             std::cout << "Please manually delete iface_rx_p file " << std::endl;
170             return;
171         }
172         if (access("/data/iface_tx_p", F_OK) != 0) {
173             std::cout << "Please manually touch iface_tx_p file" << std::endl;
174         }
175     }
176 
TestDataPackageUpDown()177     static void TestDataPackageUpDown()
178     {
179         system("rm /data/iface_tx_p");
180         system("rm /data/iface_rx_p");
181         if (access("/data/iface_tx_p", F_OK) == 0 || access("/data/iface_rx_p", F_OK) == 0) {
182             std::cout << "Please manually delete iface_rx_p and iface_tx_p file " << std::endl;
183             return;
184         }
185     }
186 
DataPackageTest(const int32_t testId)187     static bool DataPackageTest(const int32_t testId)
188     {
189         std::map<int32_t, Fun> testFunMap {
190             {TEST_DATA_PACKAGE_UP, &CellularDataCodeTest::TestDataPackageUp},
191             {TEST_DATA_PACKAGE_DOWN, &CellularDataCodeTest::TestDataPackageDown},
192             {TEST_DATA_PACKAGE_UP_DOWN, &CellularDataCodeTest::TestDataPackageUpDown},
193 
194         };
195         std::map<int32_t, Fun>::iterator it = testFunMap.find(testId);
196         if (it != testFunMap.end()) {
197             (*(it->second))();
198         } else if (TEST_DATA_PACKAGE_EXIT == testId) {
199             std::cout << "exit..." << std::endl;
200             return false;
201         } else {
202             std::cout << "please input correct number..." << std::endl;
203         }
204         return true;
205     }
206 
TouchOrRmDataPackage()207     static void TouchOrRmDataPackage()
208     {
209         int32_t input = 0;
210         while (true) {
211             std::cout << "\n-----------start--------------\nusage:please input a cmd num:\n"
212                 "0:testDataPackageUp\n"
213                 "1:testDataPackageDown\n"
214                 "2:testDataPackageUpDown\n"
215                 "100:exit\n"
216             << std::endl;
217             std::cin >> input;
218             std::cout << "inputCMD is [" << input << "]" << std::endl;
219             if (!DataPackageTest(input)) {
220                 break;
221             }
222         }
223     }
224 
UnitTest(const int32_t testId) const225     bool UnitTest(const int32_t testId) const
226     {
227         std::map<int32_t, Fun> testFunMap {
228             {IS_CELLULAR_DATA_ENABLED_TEST, &IsCellularDataEnabledTest},
229             {ENABLE_CELLULAR_DATA_TEST, &EnableCellularDataTest},
230             {GET_CELLULAR_DATA_STATE_TEST, &GetCellularDataStateTest},
231             {IS_DATA_ROAMING_ENABLED_TEST, &IsCellularDataRoamingEnabledTest},
232             {ENABLE_DATA_ROAMING_TEST, &EnableCellularDataRoamingTest},
233             {APN_CHANGED_TEST, &HandleApnChangedTest},
234             {GET_DEFAULT_SLOT_ID, &GetDefaultSlotId},
235             {SET_DEFAULT_SLOT_ID, &SetDefaultSlotId},
236             {GET_DATA_FLOW_TYPE, &GetDataFlowType},
237             {TOUCH_OR_RM_DATA_PACKAGE, &TouchOrRmDataPackage},
238         };
239         std::map<int32_t, Fun>::iterator it = testFunMap.find(testId);
240         if (it != testFunMap.end()) {
241             (*(it->second))();
242         } else if (EXIT_CELLULAR_DATA_TEST == testId) {
243             std::cout << "exit..." << std::endl;
244             return false;
245         } else {
246             std::cout << "please input correct number..." << std::endl;
247         }
248         return true;
249     }
250 
251 private:
252     const int32_t IS_CELLULAR_DATA_ENABLED_TEST = 0;
253     const int32_t ENABLE_CELLULAR_DATA_TEST = 1;
254     const int32_t GET_CELLULAR_DATA_STATE_TEST = 2;
255     const int32_t IS_DATA_ROAMING_ENABLED_TEST = 3;
256     const int32_t ENABLE_DATA_ROAMING_TEST = 4;
257     const int32_t APN_CHANGED_TEST = 5;
258     const int32_t GET_DEFAULT_SLOT_ID = 6;
259     const int32_t SET_DEFAULT_SLOT_ID = 7;
260     const int32_t GET_DATA_FLOW_TYPE = 8;
261     const int32_t TOUCH_OR_RM_DATA_PACKAGE = 9;
262     const int32_t EXIT_CELLULAR_DATA_TEST = 1000;
263     static const int32_t TEST_DATA_PACKAGE_UP = 0;
264     static const int32_t TEST_DATA_PACKAGE_DOWN = 1;
265     static const int32_t TEST_DATA_PACKAGE_UP_DOWN = 2;
266     static const int32_t TEST_DATA_PACKAGE_EXIT = 100;
267 };
268 } // namespace Telephony
269 } // namespace OHOS
270 
main()271 int main()
272 {
273     OHOS::Telephony::CellularDataClient &cellularDataClient = OHOS::Telephony::CellularDataClient::GetInstance();
274     if (cellularDataClient.GetProxy() == nullptr) {
275         std::cout << "\n--- telephonyService == nullptr\n" << std::endl;
276         return 0;
277     }
278     int32_t inputCMD = 0;
279     OHOS::Telephony::CellularDataCodeTest cellularDataCodeTest;
280     while (true) {
281         std::cout << "\n-----------start--------------\nusage:please input a cmd num:\n"
282             "0:IsCellularDataEnabledTest\n"
283             "1:EnableCellularDataTest\n"
284             "2:GetCellularDataStateTest\n"
285             "3:IsCellularDataRoamingEnabledTest\n"
286             "4:EnableCellularDataRoamingTest\n"
287             "5:ApnChangedTest\n"
288             "6:GetDefaultSlotId\n"
289             "7:SetDefaultSlotId\n"
290             "8:GetCellularDataFlowType\n"
291             "9:TouchOrRmDataPackage\n"
292             "1000:exit\n"
293             << std::endl;
294         std::cin >> inputCMD;
295         std::cout << "inputCMD is [" << inputCMD << "]" << std::endl;
296         if (!cellularDataCodeTest.UnitTest(inputCMD)) {
297             break;
298         }
299     }
300     return 0;
301 }