blob: 38b94971d4dcd5ae3d3d7d4c1e9d4fe27c80e71d [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/* Copyright Statement:
2 *
3 * This software/firmware and related documentation ("MediaTek Software") are
4 * protected under relevant copyright laws. The information contained herein
5 * is confidential and proprietary to MediaTek Inc. and/or its licensors.
6 * Without the prior written permission of MediaTek inc. and/or its licensors,
7 * any reproduction, modification, use or disclosure of MediaTek Software,
8 * and information contained herein, in whole or in part, shall be strictly prohibited.
9 */
10/* MediaTek Inc. (C) 2010. All rights reserved.
11 *
12 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
13 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
14 * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
15 * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
18 * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
19 * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
20 * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
21 * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
22 * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
23 * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
24 * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
25 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
26 * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
27 * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
28 * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
29 * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
30 *
31 * The following software/firmware and/or related documentation ("MediaTek Software")
32 * have been modified by MediaTek Inc. All revisions are subject to any receiver's
33 * applicable license agreements with MediaTek Inc.
34 */
35#include <vendor-ril/telephony/ril.h>
36#include <stdlib.h>
37#include <stdio.h>
38#include <cutils/jstring.h>
39#include <log/log.h>
40#include <unistd.h>
41#include <math.h>
42#include <string>
43#include <vector>
44
45#include "Phone_utils.h"
46#include "common.h"
47#include "em/em.h"
48#include "Radio_capability_switch_util.h"
49
50#if EM_MODE_SUPPORT
51
52#undef LOG_TAG
53#define LOG_TAG "EM_MODEMTEST"
54
55int mCurrentEmmodemtestFlag = -1; // at cmd handle flag
56
57const int MODEM_NONE = 0;
58const int MODEM_CTA = 1;
59const int MODEM_FTA = 2;
60const int MODEM_IOT = 3;
61const int MODEM_QUERY = 4;
62const int MODEM_OPERATOR = 5;
63const int MODEM_FACTORY = 6;
64const int MODEM_QUERY_CDMA = 7;
65const int MODEM_CDMA = 8;
66const int MODEM_QUERY_CLSC = 9;
67const int MODEM_CLSC = 10;
68
69int mCtaOption = 0;
70int mIotOption = 0;
71int mFtaOption = 0;
72int mOperatorOption = 0;
73int mFactoryOption = 0;
74int mCdmaOption = 0;
75
76int modem_id = -1;
77int modem_option_cnt = -1;
78int modem_option[8] = {0};
79
80static const int EVENT_QUERY_PREFERRED_TYPE_DONE = 1000;
81static const int EVENT_SET_PREFERRED_TYPE_DONE = 1001;
82static const int REBOOT_DIALOG = 2000;
83static const int NETWORK_TYPE = 3; //type 3 means GSM/WCDMA (auto mode) 0 means GSM/WCDMA (WCDMA preferred)
84
85static const int CMD_LENGTH = 6;
86static const int MODE_LENGTH = 3;
87
88//static const String PREFERENCE_GPRS = "com.mtk.GPRS";
89//static const String PREF_ATTACH_MODE = "ATTACH_MODE";
90//static const String PREF_ATTACH_MODE_SIM = "ATTACH_MODE_SIM";
91static const int ATTACH_MODE_ALWAYS = 1;
92static const int ATTACH_MODE_NOT_SPECIFY = -1;
93static const int DOCOMO_OPTION = 1 << 7;
94static const int SOFTBANK_OPTION = 1 << 8;
95//static const String PROP_TEST_CARD = "persist.sys.forcttestcard";
96//static const String PROP_TDD_TEST = "persist.sys.forcttddtest";
97
98static const int IPO_ENABLE = 1;
99static const int IPO_DISABLE = 0;
100
101static const int PCH_DATA_PREFER = 0;
102static const int PCH_CALL_PREFER = 1;
103
104static const int INDEX_SPIRENT = 1;
105static const int FLAG_UNLOCK = 0x200000;
106static const int FLAG_NOT_DETECT_CDMA_CARD = 0x100000;
107
108bool mModemFlag = false;
109int mCurrentMode = 0;
110int mCurrentCmdFlag = 0;
111
112void setCurrentTestFlag(int msg){
113 mCurrentEmmodemtestFlag = msg;
114 return ;
115}
116
117void sendATCommand_modemtest(const char *cmd,int msg)
118{
119 setCurrentTestFlag(msg);
120 emSendATCommand(cmd, Radio_capability_switch_util::get_main_capability_phone_id());
121 return ;
122}
123
124static void sendATCommandCdma(std::string str, int message) {
125 std::vector<std::string> cmdOri(3);
126 cmdOri[0] = "AT+ECTM=" + str;
127 cmdOri[1] = "";
128 cmdOri[2] = "DESTRILD:C2K";
129 std::vector<std::string> cmds = getCdmaCmdArr(cmdOri);
130 std::string cmd;
131 for (auto s : cmds) {
132 cmd += s;
133 }
134 sendATCommand_modemtest(cmd.c_str(), message);
135 sendATCommand_modemtest("AT+RFSSYNC", -1);
136}
137
138void setCdmaOption() {
139 if (modem_option[0] == INDEX_SPIRENT) {
140 sendATCommandCdma("\"SPIRENT\"", MODEM_CDMA);
141 } else {
142 sendATCommandCdma("\"NONE\"", MODEM_CDMA);
143 }
144}
145void sendATCommand_modemtest(const char *str,int flag,int msg)
146{
147 char cmd[32] = {0};
148 mCurrentCmdFlag = (mCurrentCmdFlag & 0xFF0000) | flag;
149 sprintf(cmd,"AT+EPCT=%s,%d",str,mCurrentCmdFlag);
150 setCurrentTestFlag(msg);
151 emSendATCommand(cmd,Radio_capability_switch_util::get_main_capability_phone_id());
152 return ;
153}
154
155int setPreferredNetworkType_modemtest(int type)
156{
157 RequestInfo *pRI_preferredNetType = creatRILInfoAndInit(RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE, OTHER,
158 (RIL_SOCKET_ID)Radio_capability_switch_util::get_main_capability_phone_id());
159 android::Parcel p;
160 size_t pos = p.dataPosition();
161 p.writeInt32(1);
162 p.writeInt32(type);
163 p.setDataPosition(pos);
164 pRI_preferredNetType->pCI->dispatchFunction(p, pRI_preferredNetType);
165 return 1;
166}
167void checkNetworkType() {
168 RLOGD("TcheckNetworkType");
169 RequestInfo *pRI_preferredNetType = creatRILInfoAndInit(RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE, OTHER,
170 (RIL_SOCKET_ID)Radio_capability_switch_util::get_main_capability_phone_id());
171 android::Parcel p;
172 pRI_preferredNetType->pCI->dispatchFunction(p, pRI_preferredNetType);
173}
174
175void handleQuery(char* data)
176{
177 if(strstr(data,"+EPCT:") != NULL){
178 char *p = strstr(data,",");
179 if(p != NULL){
180 char mode[3] = {0};
181 char flag[12] = {0};
182 strncpy(mode,p-1,1);
183 char *end = strstr(data,"OK");
184 if(end != NULL){
185 RLOGD("end %s ", end);
186 RLOGD("end len %d ", end - p -1);
187 strncpy(flag,p+1,end - p -1);
188 mCurrentCmdFlag = atoi(flag);
189 }
190 mCurrentMode = atoi(mode);
191 RLOGD("mCurrentMode %d mCurrentCmdFlag %d",mCurrentMode,mCurrentCmdFlag);
192 }
193 }
194 return;
195}
196const char *modem_test_cta_options[] = {
197 "Integrity Check",
198 "RLC TL1",
199 "K1297",
200 "SN Conflict",
201 "CF query",
202 "DLMN lock",
203 "Measurement open",
204 "Disable DPA",
205 "Intra CMR",
206};
207
208const char *modem_test_fta_options[] = {
209 "ANITE",
210 "CRTUG",
211 "CRTUW",
212 "ANRITSU",
213 "CMW500"
214};
215
216void setGprsTransferType(int type) {
217 int property = (type == PCH_DATA_PREFER ? 1 : 0);
218 RLOGD("Change persist.radio.gprs.prefer to %d" ,property);
219 //SystemProperties.set("persist.radio.gprs.prefer", property);
220 char cmd[32];
221 sprintf(cmd,"AT+EGTP=%d",type);
222 sendATCommand_modemtest(cmd,-1);
223 sprintf(cmd,"AT+EMPPCH=%d",type);
224 sendATCommand_modemtest(cmd,-1);
225 //sendATCMD
226}
227void attachOrDetachGprs() {
228 if ((mOperatorOption & DOCOMO_OPTION) != 0 || (mOperatorOption & SOFTBANK_OPTION) != 0) {
229 RLOGD("Attach GPRS for DoCoMo/Softband");
230 //SystemProperties.set("persist.radio.gprs.attach.type", "1");
231 char cmdStr[] = {"AT+EGTYPE=1,1"};
232 sendATCommand_modemtest(cmdStr,-1);
233 } else {
234 RLOGD("Dettach GPRS for DoCoMo/Softband");
235 //SystemProperties.set("persist.radio.gprs.attach.type", "0");
236 char cmdStr[] = {"AT+EGTYPE=0,1"};
237 sendATCommand_modemtest(cmdStr,-1);
238 }
239}
240void turnoffWCMAPreferred(){
241 if(mModemFlag){
242 setCurrentTestFlag(EVENT_SET_PREFERRED_TYPE_DONE);
243 setPreferredNetworkType_modemtest(NETWORK_TYPE);
244 }
245}
246int modemTestProcess(int id,int *whichButton, int ButtonCount)//from onCreate & onCreateDialog
247{
248 switch(id){
249 case REBOOT_DIALOG:
250 {
251 //hint do reboot
252 return 0;
253 }
254 case MODEM_NONE:
255 {
256 sendATCommand_modemtest("0", 0, MODEM_NONE);
257 if (mCurrentMode == MODEM_FTA) {
258 setGprsTransferType(PCH_CALL_PREFER);
259 }
260 break;
261 }
262 case MODEM_CTA:
263 {
264 mCtaOption = 0;
265 for(int i = 0; i < ButtonCount; i++){
266 mCtaOption += (1 << whichButton[i]);
267 }
268 turnoffWCMAPreferred();
269 sendATCommand_modemtest("1", mCtaOption, MODEM_CTA);
270 if (mCurrentMode == MODEM_FTA) {
271 setGprsTransferType(PCH_CALL_PREFER);
272 }
273 break;
274 }
275 case MODEM_FTA:
276 {
277 mFtaOption = 0;
278 for(int i = 0; i < ButtonCount; i++){
279 RLOGD("which button %d",whichButton[i]);
280 mFtaOption += (1 << whichButton[i]);
281 }
282 RLOGD("mFtaOption %x", mFtaOption);
283 turnoffWCMAPreferred();
284 sendATCommand_modemtest("2", mFtaOption, MODEM_FTA);
285 //enableIPO(false);
286 setGprsTransferType(PCH_DATA_PREFER);
287 break;
288 }
289 case MODEM_IOT:
290 {
291 mIotOption = 0;
292 for(int i = 0; i < ButtonCount; i++){
293 mIotOption += (1 << whichButton[i]);
294 }
295 sendATCommand_modemtest("3", mIotOption, MODEM_IOT);
296 if (mCurrentMode == MODEM_FTA) {
297 setGprsTransferType(PCH_CALL_PREFER);
298 }
299 break;
300 }
301 case MODEM_OPERATOR:
302 {
303 mOperatorOption = 0;
304 for(int i = 0; i < ButtonCount; i++){
305 mOperatorOption += (1 << whichButton[i]);
306 }
307 turnoffWCMAPreferred();
308 attachOrDetachGprs();
309 sendATCommand_modemtest("4", mOperatorOption, MODEM_OPERATOR);
310 if (mCurrentMode == MODEM_FTA) {
311 setGprsTransferType(PCH_CALL_PREFER);
312 }
313 break;
314 }
315 case MODEM_CDMA:
316 setCdmaOption();
317 break;
318 case MODEM_FACTORY:
319 {
320 turnoffWCMAPreferred();
321 sendATCommand_modemtest("5", 0, MODEM_FACTORY);
322 if (mCurrentMode == MODEM_FTA) {
323 setGprsTransferType(PCH_CALL_PREFER);
324 }
325 break;
326 }
327 }
328}
329
330const char * getToastString(int what) {
331 switch (what) {
332 case MODEM_NONE:
333 return "MODEM_NONE";
334 case MODEM_CTA:
335 return "MODEM_CTA";
336 case MODEM_FTA:
337 return "MODEM_FTA";
338 case MODEM_IOT:
339 return "MODEM_IOT";
340 case MODEM_OPERATOR:
341 return "MODEM_OPERATOR";
342 case MODEM_FACTORY:
343 return "MODEM_FACTORY";
344 case MODEM_CDMA:
345 return "MODEM_CDMA";
346 default:
347 return "";
348 }
349}
350
351void emModemtestAtCmdHandle(char*response, int responselen) {
352 switch (mCurrentEmmodemtestFlag) {
353 case MODEM_NONE:
354 case MODEM_CTA:
355 case MODEM_FTA:
356 case MODEM_IOT:
357 case MODEM_OPERATOR:
358 case MODEM_FACTORY:
359 case MODEM_CDMA:
360 {
361 if ((responselen > 0) && (response != NULL)) {
362 RLOGD("%s AT cmd success. %s\n",getToastString(mCurrentEmmodemtestFlag),response);
363 }
364 else {
365 RLOGD("%s AT cmd failed.\n",getToastString(mCurrentEmmodemtestFlag));
366 }
367 break;
368 }
369 case MODEM_QUERY:
370 {
371 if ((responselen > 0) && (response != NULL)) {
372 RLOGD("Query success. %s\n",response);
373 handleQuery(response);
374 }
375 else {
376 RLOGD("Query fail. ");
377 }
378 break;
379 }
380 case EVENT_QUERY_PREFERRED_TYPE_DONE:
381 {
382 if ((responselen > 0) && (response != NULL)) {
383 int type = -1;
384 //parse currect preferred type
385 type = atoi(response);
386 RLOGD("Get Preferred Type: %d ",type);
387 if (type == 0) {
388 mModemFlag = true;
389 } else {
390 mModemFlag = false;
391 }
392 }
393 else {
394 RLOGD("Query preferred type fail ");
395 }
396 break;
397 }
398 case EVENT_SET_PREFERRED_TYPE_DONE:
399 {
400 if ((responselen > 0) && (response != NULL)) {
401 RLOGD("Turn off WCDMA Preferred success\n");
402 }
403 else {
404 RLOGD("Turn off WCDMA Preferred Fail");
405 }
406 break;
407 }
408 default:
409 break;
410 }
411}
412
413void * emModemtestThread(void* arg)
414{
415 RLOGD("Modem Test: %s ",getToastString(modem_id));
416 modemTestProcess(modem_id,modem_option,modem_option_cnt);
417 android::unregisterNetwork();
418 android::emResultNotify(RET_STRING_MODEMTEST_SUCCESS);
419 pthread_exit(0);
420}
421
422int emModemtestStart(int argc, int multicnt,int *item)
423{
424 //item[0]: fta/cta/... item[1] count item[2]..item[count+1] which part
425 RLOGD("emModemtestStart called");
426 if(argc < 1)
427 {
428 RLOGD("emModemtestStart: please select id to test: \
429 ");
430 android::emResultNotify(RET_STRING_MODEMTEST_FAIL);
431 return -1;
432 }
433 int idmapping[7] = {MODEM_NONE,MODEM_CTA,MODEM_FTA,MODEM_IOT,MODEM_FACTORY,MODEM_FACTORY, MODEM_CDMA};
434 modem_id = idmapping[item[0]];
435 modem_option_cnt = multicnt;
436 modem_option[0] = item[1];
437
438 for(int i = 0; i < modem_option_cnt ; i++){
439 modem_option[i+1] = item[2+i];
440 }
441 modem_option_cnt+=1;
442
443 RLOGD("emModemtestStart modem_option_cnt %d",modem_option_cnt);
444 mCtaOption = 0;
445 mIotOption = 0;
446 mFtaOption = 0;
447 mOperatorOption = 0;
448 mFactoryOption = 0;
449 mCdmaOption = 0;
450 mCurrentEmmodemtestFlag = 0;
451 android::registerForATcmdResponse(emModemtestAtCmdHandle);
452
453 if(modem_id != MODEM_CDMA) {
454 setCurrentTestFlag(EVENT_QUERY_PREFERRED_TYPE_DONE);
455 checkNetworkType();
456 sendATCommand_modemtest("AT+EPCT?",MODEM_QUERY);
457 }
458
459 pthread_t emmodemtest_thread;
460 pthread_create(&emmodemtest_thread,NULL, emModemtestThread, NULL);
461 return (0);
462}
463#endif
464