• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/prctl.h>
21 #include <termios.h>
22 
23 #include "at_call.h"
24 #include "at_data.h"
25 #include "at_modem.h"
26 #include "at_network.h"
27 #include "at_sim.h"
28 #include "at_sms.h"
29 #include "at_support.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 "const.telephony.ril.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     .CloseUnFinishedUssd = ReqCloseUnFinishedUssd,
95     .SetVonrSwitch = ReqSetVonrSwitch,
96 };
97 
98 static const HRilSimReq g_simReqOps = {
99     .GetSimStatus = ReqGetSimStatus,
100     .GetSimImsi = ReqGetSimImsi,
101     .GetSimIO = ReqGetSimIO,
102     .GetSimLockStatus = ReqGetSimLockStatus,
103     .SetSimLock = ReqSetSimLock,
104     .ChangeSimPassword = ReqChangeSimPassword,
105     .UnlockPin = ReqUnlockPin,
106     .UnlockPuk = ReqUnlockPuk,
107     .UnlockPin2 = ReqUnlockPin2,
108     .UnlockPuk2 = ReqUnlockPuk2,
109     .SetActiveSim = ReqSetActiveSim,
110     .SimStkSendTerminalResponse = ReqSimStkSendTerminalResponse,
111     .SimStkSendEnvelope = ReqSimStkSendEnvelope,
112     .SimStkSendCallSetupRequestResult = ReqSimStkSendCallSetupRequestResult,
113     .SimStkIsReady = ReqSimStkIsReady,
114     .GetRadioProtocol = ReqGetRadioProtocol,
115     .SetRadioProtocol = ReqSetRadioProtocol,
116     .SimOpenLogicalChannel = ReqSimOpenLogicalChannel,
117     .SimCloseLogicalChannel = ReqSimCloseLogicalChannel,
118     .SimTransmitApduLogicalChannel = ReqSimTransmitApduLogicalChannel,
119     .SimAuthentication = ReqSimAuthentication,
120     .UnlockSimLock = ReqUnlockSimLock,
121 };
122 
123 static const HRilSmsReq g_smsReqOps = {
124     .SendGsmSms = ReqSendGsmSms,
125     .SendSmsAck = ReqSendSmsAck,
126     .SendCdmaSms = ReqSendCdmaSms,
127     .SendCdmaAck = ReqSendCdmaSmsAck,
128     .AddSimMessage = ReqWriteSimMessage,
129     .DelSimMessage = ReqDelSimMessage,
130     .UpdateSimMessage = ReqWriteSimMessage,
131     .SetSmscAddr = ReqSetSmscAddr,
132     .GetSmscAddr = ReqGetSmscAddr,
133     .SetCBConfig = ReqSetCBConfig,
134     .GetCBConfig = ReqGetCBConfig,
135     .GetCdmaCBConfig = ReqGetCdmaCBConfig,
136     .SetCdmaCBConfig = ReqSetCdmaCBConfig,
137     .AddCdmaSimMessage = ReqAddCdmaSimMessage,
138     .DelCdmaSimMessage = ReqDelCdmaSimMessage,
139     .UpdateCdmaSimMessage = ReqUpdateCdmaSimMessage,
140 };
141 
142 static const HRilNetworkReq g_networkReqOps = {
143     .GetSignalStrength = ReqGetSignalStrength,
144     .GetCsRegStatus = ReqGetCsRegStatus,
145     .GetPsRegStatus = ReqGetPsRegStatus,
146     .GetOperatorInfo = ReqGetOperatorInfo,
147     .GetNetworkSearchInformation = ReqGetNetworkSearchInformation,
148     .GetNetworkSelectionMode = ReqGetNetworkSelectionMode,
149     .SetNetworkSelectionMode = ReqSetNetworkSelectionMode,
150     .SetPreferredNetwork = ReqSetPreferredNetwork,
151     .GetPreferredNetwork = ReqGetPreferredNetwork,
152     .GetNeighboringCellInfoList = ReqGetNeighboringCellInfoList,
153     .GetCurrentCellInfo = ReqGetCurrentCellInfo,
154     .GetPhysicalChannelConfig = ReqGetPhysicalChannelConfig,
155     .SetLocateUpdates = ReqSetLocateUpdates,
156     .SetNotificationFilter = ReqSetNotificationFilter,
157     .SetDeviceState = ReqSetDeviceState,
158 };
159 
160 static const HRilDataReq g_dataReqOps = {
161     .SetInitApnInfo = ReqSetInitApnInfo,
162     .ActivatePdpContext = ReqActivatePdpContext,
163     .DeactivatePdpContext = ReqDeactivatePdpContext,
164     .GetPdpContextList = ReqGetPdpContextList,
165     .GetLinkBandwidthInfo = ReqGetLinkBandwidthInfo,
166     .SetLinkBandwidthReportingRule = ReqSetLinkBandwidthReportingRule,
167     .SetDataPermitted = ReqSetDataPermitted,
168     .GetLinkCapability = ReqGetLinkCapability,
169 };
170 
171 static const HRilModemReq g_modemReqOps = {
172     .SetRadioState = ReqSetRadioState,
173     .GetRadioState = ReqGetRadioState,
174     .GetImei = ReqGetImei,
175     .GetMeid = ReqGetMeid,
176     .GetVoiceRadioTechnology = ReqGetVoiceRadioTechnology,
177 };
178 
179 HRilOps g_hrilOps = {
180     .callOps = &g_callReqOps,
181     .simOps = &g_simReqOps,
182     .smsOps = &g_smsReqOps,
183     .networkOps = &g_networkReqOps,
184     .dataOps = &g_dataReqOps,
185     .modemOps = &g_modemReqOps,
186 };
187 
GetRadioState(void)188 HRilRadioState GetRadioState(void)
189 {
190     return g_radioState;
191 }
192 
SetRadioState(HRilRadioState newState,int32_t rst)193 int32_t SetRadioState(HRilRadioState newState, int32_t rst)
194 {
195     char cmd[MAX_CMD_LENGTH] = {0};
196     ResponseInfo *pResponse = NULL;
197     HRilRadioState oldState;
198     const int32_t timeOut = 10000;
199     (void)memset_s(&oldState, sizeof(HRilRadioState), 0, sizeof(HRilRadioState));
200     struct ReportInfo reportInfo;
201     (void)memset_s(&reportInfo, sizeof(struct ReportInfo), 0, sizeof(struct ReportInfo));
202     if (g_atStatus > 0) {
203         pthread_cond_signal(&g_statusCond);
204         return -1;
205     }
206 
207     pthread_mutex_lock(&g_statusMutex);
208     oldState = g_radioState;
209     if (oldState == newState) {
210         TELEPHONY_LOGE("now then is same state");
211         pthread_mutex_unlock(&g_statusMutex);
212         return HRIL_ERR_REPEAT_STATUS;
213     }
214     g_radioState = newState;
215     pthread_cond_broadcast(&g_statusCond);
216     pthread_mutex_unlock(&g_statusMutex);
217 
218     if (oldState != g_radioState) {
219         (void)sprintf_s(cmd, MAX_CMD_LENGTH, "AT+CFUN=%u,%d", newState, rst);
220         int32_t err = SendCommandLock(cmd, NULL, timeOut, &pResponse);
221         if (err != 0 || !pResponse->success) {
222             TELEPHONY_LOGE("AT+CFUN send failed");
223             FreeResponseInfo(pResponse);
224             return -1;
225         }
226     }
227 
228     FreeResponseInfo(pResponse);
229     reportInfo.notifyId = HNOTI_MODEM_RADIO_STATE_UPDATED;
230     reportInfo.type = HRIL_NOTIFICATION;
231     reportInfo.error = HRIL_ERR_SUCCESS;
232     OnModemReport(GetSlotId(NULL), reportInfo, (const uint8_t *)&g_radioState, sizeof(HRilRadioState));
233     return 0;
234 }
235 
AtOnUnusual(void)236 static void AtOnUnusual(void)
237 {
238     ATCloseReadLoop();
239     g_atStatus = 1;
240     g_fd = -1;
241     int32_t err = SetRadioState(HRIL_RADIO_POWER_STATE_OFF, 0);
242     if (err == -1) {
243         TELEPHONY_LOGE("RadioState set failed");
244     }
245 }
246 
WaitAtClose(void)247 static void WaitAtClose(void)
248 {
249     pthread_mutex_lock(&g_statusMutex);
250 
251     while (g_atStatus == 0) {
252         pthread_cond_wait(&g_statusCond, &g_statusMutex);
253     }
254 
255     pthread_mutex_unlock(&g_statusMutex);
256 }
257 
ModemInit(void)258 static int32_t ModemInit(void)
259 {
260     ResponseInfo *pResponse = NULL;
261     int32_t err = SendCommandLock("ATE0Q0V1", NULL, 0, &pResponse);
262     if (err != 0 || !pResponse->success) {
263         TELEPHONY_LOGE("ATE0Q0V1 send failed");
264     }
265     FreeResponseInfo(pResponse);
266     /* Network registration events */
267     err = SendCommandLock("AT+CREG=2", NULL, 0, &pResponse);
268     if (err != 0 || !pResponse->success) {
269         SendCommandLock("AT+CREG=2", NULL, 0, &pResponse);
270     }
271     FreeResponseInfo(pResponse);
272 
273     /* GPRS registration events */
274     err = SendCommandLock("AT+CGREG=2", NULL, 0, &pResponse);
275     if (err != 0 || !pResponse->success) {
276         SendCommandLock("AT+CGREG=2", NULL, 0, &pResponse);
277     }
278     FreeResponseInfo(pResponse);
279     /* Enable the extended format of incoming calls */
280     SendCommandLock("AT+CRC=1", NULL, 0, NULL);
281     /* Set the SMS service type to Phase 2+ version */
282     SendCommandLock("AT+CSMS=1", NULL, 0, NULL);
283     /* Set the new SMS reporting method to +CMTI */
284     SendCommandLock("AT+CNMI=1,2,0,1,1", NULL, 0, NULL);
285     /* Enable active reporting of (U)SIM status */
286     SendCommandLock("AT^SIMST=1", NULL, 0, NULL);
287     /* Disabled  auto-answer */
288     SendCommandLock("ATS0=0", NULL, 0, NULL);
289     /* Extended errors */
290     SendCommandLock("AT+CMEE=1", NULL, 0, NULL);
291     /* Set to signal  reporting */
292     SendCommandLock("AT^HCSQ=3,10", NULL, 0, NULL);
293     SendCommandLock("AT^CURCEX=2,F7FFFFFFFFFFFF", NULL, 0, NULL);
294     /* IMS registration events */
295     SendCommandLock("AT+CIREG=2", NULL, 0, NULL);
296     /*  Call Waiting notifications */
297     SendCommandLock("AT+CCWA=1", NULL, 0, NULL);
298     /* Disabled muted */
299     SendCommandLock("AT+CMUT=0", NULL, 0, NULL);
300     /* Enabled CSSU unsolicited supp service notifications */
301     SendCommandLock("AT+CSSN=0,1", NULL, 0, NULL);
302     /* Set SMS PDU mode */
303     SendCommandLock("AT+CMGF=0", NULL, 0, NULL);
304     /* Set UNICODE character */
305     SendCommandLock("AT+CSCS=\"IRA\"", NULL, 0, NULL);
306     /* Set sms memory */
307     SendCommandLock("AT+CPMS=\"SM\",\"SM\",\"ME\"", NULL, 0, NULL);
308     /* Set to open network time reporting */
309     SendCommandLock("AT^TIME=1", NULL, 0, NULL);
310     /* Set to open network time zone reporting */
311     SendCommandLock("AT+CTZR=1", NULL, 0, NULL);
312     /* Enabled SRVCC status to report actively: This command complies with the 3GPP TS 27.007 protocol. */
313     SendCommandLock("AT+CIREP=1", NULL, 0, NULL);
314 
315     err = SetRadioState(HRIL_RADIO_POWER_STATE_ON, 0);
316     if (err == -1) {
317         TELEPHONY_LOGE("RadioState set failed");
318         struct ReportInfo reportInfo = { 0 };
319         reportInfo.notifyId = HNOTI_MODEM_RADIO_STATE_UPDATED;
320         reportInfo.type = HRIL_NOTIFICATION;
321         reportInfo.error = HRIL_ERR_SUCCESS;
322         OnModemReport(GetSlotId(NULL), reportInfo, (const uint8_t *)&g_radioState, sizeof(HRilRadioState));
323     }
324     TELEPHONY_LOGI("ModemInit finish radioState %{public}d", g_radioState);
325     return err;
326 }
327 
EventListeners(void)328 static void EventListeners(void)
329 {
330     prctl(PR_SET_NAME, "hril_event_listeners");
331     int32_t waitNextTryTime = SLEEP_TIME;
332     const char *devicePath = DEVICE_PATH;
333     char atTtyPath[PARAMETER_SIZE] = {0};
334 
335     usleep(DELAY_WAIT_MS); // Prevent slow loading of system properties.
336     if (GetParameter(AT_TTY_PATH, "", atTtyPath, PARAMETER_SIZE) > 0) {
337         devicePath = atTtyPath;
338     }
339 
340     TELEPHONY_LOGI("opening AT interface %{public}s", devicePath);
341     AtSetOnUnusual(AtOnUnusual);
342     while (TRUE) {
343         while (g_fd < 0) {
344             if (devicePath != NULL) {
345                 g_fd = open(devicePath, O_RDWR);
346             }
347             if (g_fd < 0) {
348                 TELEPHONY_LOGE("ril vendorlib,opening AT interface. retrying...");
349                 sleep(waitNextTryTime);
350             } else if (!memcmp(devicePath, DEVICE_PATH_DEFAULT, sizeof(DEVICE_PATH_DEFAULT) - 1)) {
351                 struct termios ios;
352                 tcgetattr(g_fd, &ios);
353                 ios.c_lflag = 0;
354                 tcsetattr(g_fd, TCSANOW, &ios);
355             }
356         }
357         g_atStatus = 0;
358         int32_t ret = ATStartReadLoop(g_fd, OnNotifyOps);
359         if (ret < 0) {
360             TELEPHONY_LOGE("AtRead error %d\n", ret);
361             return;
362         }
363         ModemInit();
364         sleep(1);
365         WaitAtClose();
366     }
367 }
368 
RilInitOps(const struct HRilReport * reportOps)369 const HRilOps *RilInitOps(const struct HRilReport *reportOps)
370 {
371     pthread_attr_t attr;
372     SetReportOps(reportOps);
373     pthread_attr_init(&attr);
374     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
375     int32_t ret = pthread_create(&g_eventListeners, &attr, (void *(*)(void *))EventListeners, NULL);
376     if (ret < 0) {
377         TELEPHONY_LOGE("EventListeners create failed %d \n", ret);
378     }
379     return &g_hrilOps;
380 }
381