1 /*
2 * Copyright (C) 2021-2022 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 "vendor_adapter.h"
17
18 #include <fcntl.h>
19 #include <stddef.h>
20 #include <termios.h>
21
22 #include "at_call.h"
23 #include "at_data.h"
24 #include "at_modem.h"
25 #include "at_network.h"
26 #include "at_sim.h"
27 #include "at_sms.h"
28 #include "at_support.h"
29 #include "hril.h"
30 #include "hril_notification.h"
31 #include "hril_public_struct.h"
32 #include "hril_vendor_call_defs.h"
33 #include "hril_vendor_data_defs.h"
34 #include "hril_vendor_modem_defs.h"
35 #include "hril_vendor_network_defs.h"
36 #include "hril_vendor_sim_defs.h"
37 #include "hril_vendor_sms_defs.h"
38 #include "parameter.h"
39 #include "pthread.h"
40 #include "sched.h"
41 #include "securec.h"
42 #include "telephony_log_c.h"
43 #include "unistd.h"
44 #include "vendor_report.h"
45 #include "vendor_util.h"
46
47 #define DEVICE_PATH "/dev/ttyUSB0"
48 #define DEVICE_PATH_DEFAULT "/dev/ttyUSB"
49
50 #define AT_TTY_PATH "persist.sys.radio.attty.path"
51
52 static HRilRadioState g_radioState = HRIL_RADIO_POWER_STATE_UNAVAILABLE;
53 static pthread_mutex_t g_statusMutex = PTHREAD_MUTEX_INITIALIZER;
54 static pthread_cond_t g_statusCond = PTHREAD_COND_INITIALIZER;
55 static pthread_t g_eventListeners;
56 static int32_t g_fd = -1;
57 static int32_t g_atStatus = 0;
58
59 static const HRilCallReq g_callReqOps = {
60 .GetCallList = ReqGetCallList,
61 .Dial = ReqDial,
62 .Hangup = ReqHangup,
63 .Reject = ReqReject,
64 .Answer = ReqAnswer,
65 .GetClip = ReqGetClip,
66 .SetClip = ReqSetClip,
67 .HoldCall = ReqHoldCall,
68 .UnHoldCall = ReqUnHoldCall,
69 .SwitchCall = ReqSwitchCall,
70 .CombineConference = ReqCombineConference,
71 .SeparateConference = ReqSeparateConference,
72 .CallSupplement = ReqCallSupplement,
73 .GetCallWaiting = ReqGetCallWaiting,
74 .SetCallWaiting = ReqSetCallWaiting,
75 .GetCallTransferInfo = ReqGetCallTransferInfo,
76 .SetCallTransferInfo = ReqSetCallTransferInfo,
77 .GetCallRestriction = ReqGetCallRestriction,
78 .SetCallRestriction = ReqSetCallRestriction,
79 .GetClir = ReqGetClir,
80 .SetClir = ReqSetClir,
81 .StartDtmf = ReqStartDtmf,
82 .SendDtmf = ReqSendDtmf,
83 .StopDtmf = ReqStopDtmf,
84 .GetCallPreferenceMode = ReqGetCallPreferenceMode,
85 .SetCallPreferenceMode = ReqSetCallPreferenceMode,
86 .SetUssd = ReqSetUssd,
87 .GetUssd = ReqGetUssd,
88 .GetMute = ReqGetMute,
89 .SetMute = ReqSetMute,
90 .GetEmergencyCallList = ReqGetEmergencyCallList,
91 .GetCallFailReason = ReqGetCallFailReason,
92 .SetEmergencyCallList = ReqSetEmergencyCallList,
93 .SetBarringPassword = ReqSetBarringPassword,
94 };
95
96 static const HRilSimReq g_simReqOps = {
97 .GetSimStatus = ReqGetSimStatus,
98 .GetSimImsi = ReqGetSimImsi,
99 .GetSimIO = ReqGetSimIO,
100 .GetSimLockStatus = ReqGetSimLockStatus,
101 .SetSimLock = ReqSetSimLock,
102 .ChangeSimPassword = ReqChangeSimPassword,
103 .UnlockPin = ReqUnlockPin,
104 .UnlockPuk = ReqUnlockPuk,
105 .UnlockPin2 = ReqUnlockPin2,
106 .UnlockPuk2 = ReqUnlockPuk2,
107 .SetActiveSim = ReqSetActiveSim,
108 .SimStkSendTerminalResponse = ReqSimStkSendTerminalResponse,
109 .SimStkSendEnvelope = ReqSimStkSendEnvelope,
110 .SimStkSendCallSetupRequestResult = ReqSimStkSendCallSetupRequestResult,
111 .SimStkIsReady = ReqSimStkIsReady,
112 .GetRadioProtocol = ReqGetRadioProtocol,
113 .SetRadioProtocol = ReqSetRadioProtocol,
114 .SimOpenLogicalChannel = ReqSimOpenLogicalChannel,
115 .SimCloseLogicalChannel = ReqSimCloseLogicalChannel,
116 .SimTransmitApduLogicalChannel = ReqSimTransmitApduLogicalChannel,
117 .UnlockSimLock = ReqUnlockSimLock,
118 };
119
120 static const HRilSmsReq g_smsReqOps = {
121 .SendGsmSms = ReqSendGsmSms,
122 .SendSmsAck = ReqSendSmsAck,
123 .SendCdmaSms = ReqSendCdmaSms,
124 .SendCdmaAck = ReqSendCdmaSmsAck,
125 .AddSimMessage = ReqWriteSimMessage,
126 .DelSimMessage = ReqDelSimMessage,
127 .UpdateSimMessage = ReqWriteSimMessage,
128 .SetSmscAddr = ReqSetSmscAddr,
129 .GetSmscAddr = ReqGetSmscAddr,
130 .SetCBConfig = ReqSetCBConfig,
131 .GetCBConfig = ReqGetCBConfig,
132 .GetCdmaCBConfig = ReqGetCdmaCBConfig,
133 .SetCdmaCBConfig = ReqSetCdmaCBConfig,
134 .AddCdmaSimMessage = ReqAddCdmaSimMessage,
135 .DelCdmaSimMessage = ReqDelCdmaSimMessage,
136 .UpdateCdmaSimMessage = ReqUpdateCdmaSimMessage,
137 };
138
139 static const HRilNetworkReq g_networkReqOps = {
140 .GetSignalStrength = ReqGetSignalStrength,
141 .GetCsRegStatus = ReqGetCsRegStatus,
142 .GetPsRegStatus = ReqGetPsRegStatus,
143 .GetOperatorInfo = ReqGetOperatorInfo,
144 .GetNetworkSearchInformation = ReqGetNetworkSearchInformation,
145 .GetNetworkSelectionMode = ReqGetNetworkSelectionMode,
146 .SetNetworkSelectionMode = ReqSetNetworkSelectionMode,
147 .SetPreferredNetwork = ReqSetPreferredNetwork,
148 .GetPreferredNetwork = ReqGetPreferredNetwork,
149 .GetNeighboringCellInfoList = ReqGetNeighboringCellInfoList,
150 .GetCurrentCellInfo = ReqGetCurrentCellInfo,
151 .GetPhysicalChannelConfig = ReqGetPhysicalChannelConfig,
152 .SetLocateUpdates = ReqSetLocateUpdates,
153 .SetNotificationFilter = ReqSetNotificationFilter,
154 .SetDeviceState = ReqSetDeviceState,
155 };
156
157 static const HRilDataReq g_dataReqOps = {
158 .SetInitApnInfo = ReqSetInitApnInfo,
159 .ActivatePdpContext = ReqActivatePdpContext,
160 .DeactivatePdpContext = ReqDeactivatePdpContext,
161 .GetPdpContextList = ReqGetPdpContextList,
162 .GetLinkBandwidthInfo = ReqGetLinkBandwidthInfo,
163 .SetLinkBandwidthReportingRule = ReqSetLinkBandwidthReportingRule,
164 .SetDataPermitted = ReqSetDataPermitted,
165 };
166
167 static const HRilModemReq g_modemReqOps = {
168 .SetRadioState = ReqSetRadioState,
169 .GetRadioState = ReqGetRadioState,
170 .GetImei = ReqGetImei,
171 .GetMeid = ReqGetMeid,
172 .GetVoiceRadioTechnology = ReqGetVoiceRadioTechnology,
173 };
174
175 HRilOps g_hrilOps = {
176 .callOps = &g_callReqOps,
177 .simOps = &g_simReqOps,
178 .smsOps = &g_smsReqOps,
179 .networkOps = &g_networkReqOps,
180 .dataOps = &g_dataReqOps,
181 .modemOps = &g_modemReqOps,
182 };
183
GetRadioState(void)184 HRilRadioState GetRadioState(void)
185 {
186 return g_radioState;
187 }
188
SetRadioState(HRilRadioState newState,int32_t rst)189 int32_t SetRadioState(HRilRadioState newState, int32_t rst)
190 {
191 char cmd[MAX_CMD_LENGTH] = {0};
192 ResponseInfo *pResponse = NULL;
193 HRilRadioState oldState;
194 const int32_t timeOut = 10000;
195 (void)memset_s(&oldState, sizeof(HRilRadioState), 0, sizeof(HRilRadioState));
196 struct ReportInfo reportInfo;
197 (void)memset_s(&reportInfo, sizeof(struct ReportInfo), 0, sizeof(struct ReportInfo));
198 if (g_atStatus > 0) {
199 pthread_cond_signal(&g_statusCond);
200 return -1;
201 }
202
203 pthread_mutex_lock(&g_statusMutex);
204 oldState = g_radioState;
205 if (oldState == newState) {
206 TELEPHONY_LOGE("now then is same state");
207 pthread_mutex_unlock(&g_statusMutex);
208 return HRIL_ERR_REPEAT_STATUS;
209 }
210 g_radioState = newState;
211 pthread_cond_broadcast(&g_statusCond);
212 pthread_mutex_unlock(&g_statusMutex);
213
214 if (oldState != g_radioState) {
215 (void)sprintf_s(cmd, MAX_CMD_LENGTH, "AT+CFUN=%u,%d", newState, rst);
216 int32_t err = SendCommandLock(cmd, NULL, timeOut, &pResponse);
217 if (err != 0 || !pResponse->success) {
218 TELEPHONY_LOGE("AT+CFUN send failed");
219 FreeResponseInfo(pResponse);
220 return -1;
221 }
222 }
223
224 FreeResponseInfo(pResponse);
225 reportInfo.notifyId = HNOTI_MODEM_RADIO_STATE_UPDATED;
226 reportInfo.type = HRIL_NOTIFICATION;
227 reportInfo.error = HRIL_ERR_SUCCESS;
228 OnModemReport(GetSlotId(NULL), reportInfo, (const uint8_t *)&g_radioState, sizeof(HRilRadioState));
229 return 0;
230 }
231
AtOnUnusual(void)232 static void AtOnUnusual(void)
233 {
234 ATCloseReadLoop();
235 g_atStatus = 1;
236 g_fd = -1;
237 int32_t err = SetRadioState(HRIL_RADIO_POWER_STATE_OFF, 0);
238 if (err == -1) {
239 TELEPHONY_LOGE("RadioState set failed");
240 }
241 }
242
WaitAtClose(void)243 static void WaitAtClose(void)
244 {
245 pthread_mutex_lock(&g_statusMutex);
246
247 while (g_atStatus == 0) {
248 pthread_cond_wait(&g_statusCond, &g_statusMutex);
249 }
250
251 pthread_mutex_unlock(&g_statusMutex);
252 }
253
ModemInit(void)254 static int32_t ModemInit(void)
255 {
256 ResponseInfo *pResponse = NULL;
257 int32_t err = SendCommandLock("ATE0Q0V1", NULL, 0, &pResponse);
258 if (err != 0 || !pResponse->success) {
259 TELEPHONY_LOGE("ATE0Q0V1 send failed");
260 }
261 FreeResponseInfo(pResponse);
262 /* Network registration events */
263 err = SendCommandLock("AT+CREG=2", NULL, 0, &pResponse);
264 if (err != 0 || !pResponse->success) {
265 SendCommandLock("AT+CREG=2", NULL, 0, &pResponse);
266 }
267 FreeResponseInfo(pResponse);
268
269 /* GPRS registration events */
270 err = SendCommandLock("AT+CGREG=2", NULL, 0, &pResponse);
271 if (err != 0 || !pResponse->success) {
272 SendCommandLock("AT+CGREG=2", NULL, 0, &pResponse);
273 }
274 FreeResponseInfo(pResponse);
275 /* Enable the extended format of incoming calls */
276 SendCommandLock("AT+CRC=1", NULL, 0, NULL);
277 /* Set the SMS service type to Phase 2+ version */
278 SendCommandLock("AT+CSMS=1", NULL, 0, NULL);
279 /* Set the new SMS reporting method to +CMTI */
280 SendCommandLock("AT+CNMI=1,2,0,1,1", NULL, 0, NULL);
281 /* Enable active reporting of (U)SIM status */
282 SendCommandLock("AT^SIMST=1", NULL, 0, NULL);
283 /* Disabled auto-answer */
284 SendCommandLock("ATS0=0", NULL, 0, NULL);
285 /* Extended errors */
286 SendCommandLock("AT+CMEE=1", NULL, 0, NULL);
287 /* Set to signal reporting */
288 SendCommandLock("AT^HCSQ=3,10", NULL, 0, NULL);
289 SendCommandLock("AT^CURCEX=2,F7FFFFFFFFFFFF", NULL, 0, NULL);
290 /* IMS registration events */
291 SendCommandLock("AT+CIREG=2", NULL, 0, NULL);
292 /* Call Waiting notifications */
293 SendCommandLock("AT+CCWA=1", NULL, 0, NULL);
294 /* Disabled muted */
295 SendCommandLock("AT+CMUT=0", NULL, 0, NULL);
296 /* Enabled CSSU unsolicited supp service notifications */
297 SendCommandLock("AT+CSSN=0,1", NULL, 0, NULL);
298 /* Set SMS PDU mode */
299 SendCommandLock("AT+CMGF=0", NULL, 0, NULL);
300 /* Set UNICODE character */
301 SendCommandLock("AT+CSCS=\"IRA\"", NULL, 0, NULL);
302 /* Set sms memory */
303 SendCommandLock("AT+CPMS=\"SM\",\"SM\",\"ME\"", NULL, 0, NULL);
304 /* Set to open network time reporting */
305 SendCommandLock("AT^TIME=1", NULL, 0, NULL);
306 /* Set to open network time zone reporting */
307 SendCommandLock("AT+CTZR=1", NULL, 0, NULL);
308 /* Enabled SRVCC status to report actively: This command complies with the 3GPP TS 27.007 protocol. */
309 SendCommandLock("AT+CIREP=1", NULL, 0, NULL);
310
311 err = SetRadioState(HRIL_RADIO_POWER_STATE_ON, 0);
312 if (err == -1) {
313 TELEPHONY_LOGE("RadioState set failed");
314 struct ReportInfo reportInfo = { 0 };
315 reportInfo.notifyId = HNOTI_MODEM_RADIO_STATE_UPDATED;
316 reportInfo.type = HRIL_NOTIFICATION;
317 reportInfo.error = HRIL_ERR_SUCCESS;
318 OnModemReport(GetSlotId(NULL), reportInfo, (const uint8_t *)&g_radioState, sizeof(HRilRadioState));
319 }
320 TELEPHONY_LOGI("ModemInit finish radioState %{public}d", g_radioState);
321 return err;
322 }
323
EventListeners(void)324 static void EventListeners(void)
325 {
326 int32_t waitNextTryTime = SLEEP_TIME;
327 const char *devicePath = DEVICE_PATH;
328 char atTtyPath[PARAMETER_SIZE] = {0};
329
330 usleep(DELAY_WAIT_MS); // Prevent slow loading of system properties.
331 if (GetParameter(AT_TTY_PATH, "", atTtyPath, PARAMETER_SIZE) > 0) {
332 devicePath = atTtyPath;
333 }
334
335 TELEPHONY_LOGI("opening AT interface %{public}s", devicePath);
336 AtSetOnUnusual(AtOnUnusual);
337 while (TRUE) {
338 while (g_fd < 0) {
339 if (devicePath != NULL) {
340 g_fd = open(devicePath, O_RDWR);
341 }
342 if (g_fd < 0) {
343 TELEPHONY_LOGE("ril vendorlib,opening AT interface. retrying...");
344 sleep(waitNextTryTime);
345 } else if (!memcmp(devicePath, DEVICE_PATH_DEFAULT, sizeof(DEVICE_PATH_DEFAULT) - 1)) {
346 struct termios ios;
347 tcgetattr(g_fd, &ios);
348 ios.c_lflag = 0;
349 tcsetattr(g_fd, TCSANOW, &ios);
350 }
351 }
352 g_atStatus = 0;
353 int32_t ret = ATStartReadLoop(g_fd, OnNotifyOps);
354 if (ret < 0) {
355 TELEPHONY_LOGE("AtRead error %d\n", ret);
356 return;
357 }
358 ModemInit();
359 sleep(1);
360 WaitAtClose();
361 }
362 }
363
RilInitOps(const struct HRilReport * reportOps)364 const HRilOps *RilInitOps(const struct HRilReport *reportOps)
365 {
366 pthread_attr_t attr;
367 SetReportOps(reportOps);
368 pthread_attr_init(&attr);
369 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
370 int32_t ret = pthread_create(&g_eventListeners, &attr, (void *(*)(void *))EventListeners, NULL);
371 if (ret < 0) {
372 TELEPHONY_LOGE("EventListeners create failed %d \n", ret);
373 }
374 return &g_hrilOps;
375 }
376