blob: 29643dbcdf8d2dbd6f2b594425e73f16fb65fa50 [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 "stk.h"
36#include <vendor-ril/telephony/ril.h>
37#include <stdlib.h>
38#include <stdio.h>
39#include <cutils/jstring.h>
40#include <string.h>
41#include <time.h>
42
43#include "resp_timeout.h"
44
45#undef LOG_TAG
46#define LOG_TAG "DEMO_STK"
47#define INVALID -1;
48#define ACCEPT 1;
49#define REJECT 0;
50
51extern void ARspRequestWithArg(int request, const char* arg,RIL_SOCKET_ID socket_id);
52
53//RIL_REQUEST_STK_SEND_ENVELOPE_COMMAND
54int sendEnvelope(int argc, char **argv, RIL_SOCKET_ID socket_id, RequestInfo *pRI)
55{
56 android::Parcel p;
57 size_t pos = p.dataPosition();
58
59 //p.writeString(contents);
60 writeStringToParcel(p, (const char *)argv[1]);
61 p.setDataPosition(pos);
62
63 pRI->pCI->dispatchFunction(p, pRI);
64
65 return 0;
66}
67//RIL_REQUEST_STK_SEND_TERMINAL_RESPONSE
68int sendTerminalResponse(int argc, char **argv, RIL_SOCKET_ID socket_id, RequestInfo *pRI)
69{
70 android::Parcel p;
71 size_t pos = p.dataPosition();
72
73 //p.writeString(contents);
74 writeStringToParcel(p, (const char *)argv[1]);
75 p.setDataPosition(pos);
76
77 pRI->pCI->dispatchFunction(p, pRI);
78
79 return 0;
80}
81//RIL_REQUEST_STK_SEND_ENVELOPE_WITH_STATUS
82int sendEnvelopeWithStatus(int argc, char **argv, RIL_SOCKET_ID socket_id, RequestInfo *pRI)
83{
84 android::Parcel p;
85 size_t pos = p.dataPosition();
86
87 //p.writeString(contents);
88 writeStringToParcel(p, (const char *)argv[1]);
89 p.setDataPosition(pos);
90
91 pRI->pCI->dispatchFunction(p, pRI);
92
93 return 0;
94}
95
96//RIL_REQUEST_STK_HANDLE_CALL_SETUP_REQUESTED_FROM_SIM
97int handleCallSetupRequestFromSim(int argc, char **argv, RIL_SOCKET_ID socket_id, RequestInfo *pRI)
98{
99 if(argc != 2) {
100 free(pRI);
101 RLOGD("handleCallSetupRequestFromSim parameter lost");
102 return 0;
103 }
104 int reject = INVALID;
105 if(strcasecmp("yes", argv[1])){
106 reject = ACCEPT;
107 } else if (strcasecmp("no", argv[1])){
108 reject = REJECT;
109 } else {
110 free(pRI);
111 RLOGD("input: %s, parameter is wrong, please input again!", argv[1]);
112 return 0;
113 }
114 android::Parcel p;
115 size_t pos = p.dataPosition();
116 p.writeInt32(1);
117 p.writeInt32(reject);
118 p.setDataPosition(pos);
119
120 pRI->pCI->dispatchFunction(p, pRI);
121
122 clear_timeout(socket_id);
123 return 0;
124}
125
126//RIL_REQUEST_REPORT_STK_SERVICE_IS_RUNNING
127int reportStkServiceIsRunning(int argc, char **argv, RIL_SOCKET_ID socket_id, RequestInfo *pRI)
128{
129 android::Parcel p;
130
131 pRI->pCI->dispatchFunction(p, pRI);
132
133 return 0;
134}
135
136static int getStkCommandType(char *cmd) {
137 char temp[3] = {0};
138 int cmdType = 0;
139
140 strncpy(temp, cmd, 2);
141 cmdType = strtoul(temp, NULL, 16);
142 cmdType = 0xFF & cmdType;
143
144 return cmdType;
145}
146
147static void parseStkCmdType(char* cmd, int* cmdType) {
148 int cmd_len = strlen(cmd);
149 int typePos = 0;
150
151 if (cmd_len < 14) {
152 RLOGD("parseStkCmdType exception!");
153 return;
154 }
155
156 if(cmd[2] <= '7' ) {
157 typePos = 10;
158 } else {
159 typePos = 12;
160 }
161
162 // get command type
163 *cmdType = getStkCommandType(&cmd[typePos]);
164}
165
166static void parseStkCmdQualifier(char* cmd, int* cmdQual) {
167 int cmd_len = strlen(cmd);
168 int typePos = 0;
169
170 if (cmd_len < 14) {
171 RLOGD("parseStkCmdQualifier exception!");
172 return;
173 }
174
175 if(cmd[2] <= '7' ) {
176 typePos = 12;
177 } else {
178 typePos = 14;
179 }
180
181 // get command qualifier
182 *cmdQual = getStkCommandType(&cmd[typePos]);
183}
184
185static int getStkCommandNumber(char *cmd) {
186 char temp[3] = {0};
187 int cmdNum = 0;
188
189 strncpy(temp, cmd, 2);
190 cmdNum = strtoul(temp, NULL, 16);
191 cmdNum = 0xFF & cmdNum;
192
193 return cmdNum;
194}
195
196static void parseStkCmdNum(char* cmd, int* cmdNum) {
197 int cmd_len = strlen(cmd);
198 int typePos = 0;
199
200 if (cmd_len < 12) {
201 RLOGD("parseStkCmdNum exception!");
202 return;
203 }
204
205 if (cmd[2] <= '7') {
206 typePos = 8;
207 } else {
208 typePos = 10;
209 }
210
211 // check command num
212 *cmdNum = getStkCommandNumber(&cmd[typePos]);
213 RLOGD("parseStkCmdNum cmdNum:%d", *cmdNum);
214}
215
216static int numToBCD(int data) {
217 char string[20] = {0};
218 int low = 0;
219 int high = 0;
220 int result = -1;
221 char num_table[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
222
223 snprintf(string, 20, "%2d", data);
224 if(strlen(string)>2) {
225 RLOGD("[numToBCD]Out of range, data = %d, string = %s", data, string);
226 return -1;
227 }
228 for(int i=0; i<10; i++) {
229 if(num_table[i] == string[0])
230 low = i ;
231 if(num_table[i] == string[1])
232 high = i;
233 }
234 result = ((high<<4)&0xF0)+(low&0x0F);
235 RLOGD("[numToBCD]data=%d, result=%#x, string=%s, low=%d, high=%d", data, result, string, low, high);
236 return result;
237}
238
239static void handleProvideLocalInfo(char *cmd, int slot_id){
240 int cmdNum = -1;
241 time_t now_tmp;
242 struct tm *now;
243 int year = -1, year_bcd = -1;
244 int month = -1, month_bcd = -1;
245 int day = -1, day_bcd = -1;
246 int hour = -1, hour_bcd = -1;
247 int min = -1, min_bcd = -1;
248 int sec = -1, sec_bcd = -1;
249 char response[200]="";
250
251 parseStkCmdNum(cmd, &cmdNum);
252 time(&now_tmp);
253 now = localtime(&now_tmp);
254 year = now->tm_year + 1900-2000;
255 month = now->tm_mon+1;
256 day = now->tm_mday;
257 hour = now->tm_hour;
258 min = now->tm_min;
259 sec = now->tm_sec;
260 RLOGD("[handleProvideLocalInfo]Get time from system: %d/%d/%d, %d:%d:%d", year, month, day, hour, min, sec);
261 year_bcd = numToBCD(year);
262 month_bcd = numToBCD(month);
263 day_bcd = numToBCD(day);
264 hour_bcd = numToBCD(hour);
265 min_bcd = numToBCD(min);
266 sec_bcd = numToBCD(sec);
267 snprintf(response, 200, "8103%02X260382028281830100A607%02X%02X%02X%02X%02X%02XFF", cmdNum&0XFF, year_bcd&0XFF, month_bcd&0XFF,
268 day_bcd&0XFF, hour_bcd&0XFF, min_bcd&0XFF, sec_bcd&0XFF);
269 RLOGD("[handleProvideLocalInfo]Send RIL_REQUEST_STK_SEND_TERMINAL_RESPONSE, data=%s", response);
270 ARspRequestWithArg(RIL_REQUEST_STK_SEND_TERMINAL_RESPONSE, response, (RIL_SOCKET_ID)slot_id);
271}
272
273void handleStkCommand(char* response, int responselen, int slot_id) {
274 int cmdType = -1;
275 int cmdQual = -1;
276 int cmdNum = -1;
277
278 parseStkCmdType(response, &cmdType);
279 parseStkCmdQualifier(response, &cmdQual);
280 parseStkCmdNum(response, &cmdNum);
281 RLOGD("[handleStkCommand][slot%d]cmdType=%d, cmdQual=%d, cmdNum=%d", slot_id, cmdType, cmdQual, cmdNum);
282
283 switch (cmdType)
284 {
285 case CMD_OPEN_CHAN:
286 RLOGD("[URC][CAT][BIP][SIM%d]: the proactive command include open channel TLV, please choose yes or no", slot_id);
287 printf("[URC][CAT][BIP][SIM%d]: the proactive command include open channel TLV, please choose yes or no\n", slot_id+1);
288 setup_timeout(slot_id); //set up timeout one minutes
289 break;
290 case CMD_PROVIDE_LOCAL_INFO:
291 if (0x03 != cmdQual){
292 RLOGD("not request for data/time, qualifier = %#x", cmdQual);
293 return;
294 }
295 handleProvideLocalInfo(response, slot_id);
296 break;
297 default:
298 break;
299 }
300}