blob: c554a9fe1ded7a8dfd0e10839473d02f709fcfbe [file] [log] [blame]
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001/* //device/system/rild/rild.c
2**
Etan Cohend3652192014-06-20 08:28:44 -07003** Copyright 2006 The Android Open Source Project
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08004**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <dlfcn.h>
21#include <string.h>
22#include <stdint.h>
23#include <unistd.h>
24#include <fcntl.h>
25#include <errno.h>
26
27#include <telephony/ril.h>
28#define LOG_TAG "RILD"
Dan Willemsen950d8302017-04-03 21:58:13 -070029#include <log/log.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080030#include <cutils/properties.h>
31#include <cutils/sockets.h>
Nick Kralevichfe4e1ec2013-02-28 14:16:41 -080032#include <sys/capability.h>
Elliott Hughes5d0cb9e2014-07-18 17:55:52 -070033#include <sys/prctl.h>
Andreas Gampe8ded9962015-04-06 12:20:24 -070034#include <sys/stat.h>
35#include <sys/types.h>
Dheeraj Shetty27976c42014-07-02 21:27:57 +020036#include <libril/ril_ex.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080037
Jaekyun Seok1b4e6372018-02-26 13:30:06 +090038#if defined(PRODUCT_COMPATIBLE_PROPERTY)
39#define LIB_PATH_PROPERTY "vendor.rild.libpath"
40#define LIB_ARGS_PROPERTY "vendor.rild.libargs"
41#else
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080042#define LIB_PATH_PROPERTY "rild.libpath"
43#define LIB_ARGS_PROPERTY "rild.libargs"
Jaekyun Seok1b4e6372018-02-26 13:30:06 +090044#endif
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080045#define MAX_LIB_ARGS 16
46
Dheeraj Shetty27976c42014-07-02 21:27:57 +020047static void usage(const char *argv0) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080048 fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0);
Elliott Hughese2a70cf2013-11-20 13:51:45 -080049 exit(EXIT_FAILURE);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080050}
51
Amit Mahajan7955c432017-03-28 11:17:55 -070052extern char ril_service_name_base[MAX_SERVICE_NAME_LENGTH];
53extern char ril_service_name[MAX_SERVICE_NAME_LENGTH];
Etan Cohend3652192014-06-20 08:28:44 -070054
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080055extern void RIL_register (const RIL_RadioFunctions *callbacks);
Amit Mahajan18fe36b2016-08-25 11:19:21 -070056extern void rilc_thread_pool ();
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080057
Nathan Harold14e42662017-07-31 19:44:46 -070058extern void RIL_register_socket (const RIL_RadioFunctions *(*rilUimInit)
Dheeraj Shetty27976c42014-07-02 21:27:57 +020059 (const struct RIL_Env *, int, char **), RIL_SOCKET_TYPE socketType, int argc, char **argv);
60
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080061extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020062 void *response, size_t responselen);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080063
Sanket Padawe6ff9a872016-01-27 15:09:12 -080064extern void RIL_onRequestAck(RIL_Token t);
65
Etan Cohend3652192014-06-20 08:28:44 -070066#if defined(ANDROID_MULTI_SIM)
67extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020068 size_t datalen, RIL_SOCKET_ID socket_id);
Etan Cohend3652192014-06-20 08:28:44 -070069#else
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080070extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020071 size_t datalen);
Etan Cohend3652192014-06-20 08:28:44 -070072#endif
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080073
Wink Saville2932f312010-10-07 16:35:15 -070074extern void RIL_requestTimedCallback (RIL_TimedCallback callback,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020075 void *param, const struct timeval *relativeTime);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080076
77
78static struct RIL_Env s_rilEnv = {
79 RIL_onRequestComplete,
80 RIL_onUnsolicitedResponse,
Sanket Padawe6ff9a872016-01-27 15:09:12 -080081 RIL_requestTimedCallback,
82 RIL_onRequestAck
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080083};
84
85extern void RIL_startEventLoop();
86
Dheeraj Shetty27976c42014-07-02 21:27:57 +020087static int make_argv(char * args, char ** argv) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080088 // Note: reserve argv[0]
89 int count = 1;
90 char * tok;
91 char * s = args;
92
93 while ((tok = strtok(s, " \0"))) {
94 argv[count] = tok;
95 s = NULL;
96 count++;
97 }
98 return count;
99}
100
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200101int main(int argc, char **argv) {
Amit Mahajan18fe36b2016-08-25 11:19:21 -0700102 // vendor ril lib path either passed in as -l parameter, or read from rild.libpath property
Jorge Lucangeli Obeseaa7b3c2016-04-18 17:58:04 -0700103 const char *rilLibPath = NULL;
Amit Mahajan18fe36b2016-08-25 11:19:21 -0700104 // ril arguments either passed in as -- parameter, or read from rild.libargs property
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800105 char **rilArgv;
Amit Mahajan18fe36b2016-08-25 11:19:21 -0700106 // handle for vendor ril lib
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800107 void *dlHandle;
Amit Mahajan18fe36b2016-08-25 11:19:21 -0700108 // Pointer to ril init function in vendor ril
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800109 const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
Amit Mahajan18fe36b2016-08-25 11:19:21 -0700110 // Pointer to sap init function in vendor ril
Nathan Harold14e42662017-07-31 19:44:46 -0700111 const RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **);
Jorge Lucangeli Obeseaa7b3c2016-04-18 17:58:04 -0700112 const char *err_str = NULL;
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200113
Amit Mahajan18fe36b2016-08-25 11:19:21 -0700114 // functions returned by ril init function in vendor ril
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800115 const RIL_RadioFunctions *funcs;
Amit Mahajan18fe36b2016-08-25 11:19:21 -0700116 // lib path from rild.libpath property (if it's read)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800117 char libPath[PROPERTY_VALUE_MAX];
Amit Mahajan18fe36b2016-08-25 11:19:21 -0700118 // flat to indicate if -- parameters are present
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800119 unsigned char hasLibArgs = 0;
120
121 int i;
Amit Mahajan18fe36b2016-08-25 11:19:21 -0700122 // ril/socket id received as -c parameter, otherwise set to 0
Etan Cohend3652192014-06-20 08:28:44 -0700123 const char *clientId = NULL;
Amit Mahajan18fe36b2016-08-25 11:19:21 -0700124
Etan Cohend3652192014-06-20 08:28:44 -0700125 RLOGD("**RIL Daemon Started**");
126 RLOGD("**RILd param count=%d**", argc);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800127
Nick Kralevichaea7d5b2010-12-10 15:53:02 -0800128 umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800129 for (i = 1; i < argc ;) {
130 if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) {
131 rilLibPath = argv[i + 1];
132 i += 2;
133 } else if (0 == strcmp(argv[i], "--")) {
134 i++;
135 hasLibArgs = 1;
136 break;
Etan Cohend3652192014-06-20 08:28:44 -0700137 } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) {
138 clientId = argv[i+1];
139 i += 2;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800140 } else {
141 usage(argv[0]);
142 }
143 }
144
Etan Cohend3652192014-06-20 08:28:44 -0700145 if (clientId == NULL) {
146 clientId = "0";
147 } else if (atoi(clientId) >= MAX_RILDS) {
148 RLOGE("Max Number of rild's supported is: %d", MAX_RILDS);
149 exit(0);
150 }
151 if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) {
Elliott Hughesef75a2a2018-01-19 15:53:14 -0800152 snprintf(ril_service_name, sizeof(ril_service_name), "%s%s", ril_service_name_base,
153 clientId);
Etan Cohend3652192014-06-20 08:28:44 -0700154 }
155
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800156 if (rilLibPath == NULL) {
157 if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) {
158 // No lib sepcified on the command line, and nothing set in props.
159 // Assume "no-ril" case.
160 goto done;
161 } else {
162 rilLibPath = libPath;
163 }
164 }
165
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800166 dlHandle = dlopen(rilLibPath, RTLD_NOW);
167
168 if (dlHandle == NULL) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800169 RLOGE("dlopen failed: %s", dlerror());
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800170 exit(EXIT_FAILURE);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800171 }
172
173 RIL_startEventLoop();
174
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200175 rilInit =
176 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
177 dlsym(dlHandle, "RIL_Init");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800178
179 if (rilInit == NULL) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800180 RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800181 exit(EXIT_FAILURE);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800182 }
183
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200184 dlerror(); // Clear any previous dlerror
185 rilUimInit =
Nathan Harold14e42662017-07-31 19:44:46 -0700186 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200187 dlsym(dlHandle, "RIL_SAP_Init");
188 err_str = dlerror();
189 if (err_str) {
190 RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str);
191 } else if (!rilUimInit) {
192 RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath);
193 }
194
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800195 if (hasLibArgs) {
196 rilArgv = argv + i - 1;
197 argc = argc -i + 1;
198 } else {
199 static char * newArgv[MAX_LIB_ARGS];
200 static char args[PROPERTY_VALUE_MAX];
201 rilArgv = newArgv;
202 property_get(LIB_ARGS_PROPERTY, args, "");
203 argc = make_argv(args, rilArgv);
204 }
205
Etan Cohend3652192014-06-20 08:28:44 -0700206 rilArgv[argc++] = "-c";
Jorge Lucangeli Obeseaa7b3c2016-04-18 17:58:04 -0700207 rilArgv[argc++] = (char*)clientId;
Etan Cohend3652192014-06-20 08:28:44 -0700208 RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]);
209
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800210 // Make sure there's a reasonable argv[0]
211 rilArgv[0] = argv[0];
212
213 funcs = rilInit(&s_rilEnv, argc, rilArgv);
Etan Cohend3652192014-06-20 08:28:44 -0700214 RLOGD("RIL_Init rilInit completed");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800215
216 RIL_register(funcs);
217
Etan Cohend3652192014-06-20 08:28:44 -0700218 RLOGD("RIL_Init RIL_register completed");
219
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200220 if (rilUimInit) {
221 RLOGD("RIL_register_socket started");
222 RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv);
223 }
224
225 RLOGD("RIL_register_socket completed");
226
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800227done:
228
Amit Mahajan18fe36b2016-08-25 11:19:21 -0700229 rilc_thread_pool();
230
Etan Cohend3652192014-06-20 08:28:44 -0700231 RLOGD("RIL_Init starting sleep loop");
Elliott Hughes164d2052013-11-20 14:53:08 -0800232 while (true) {
233 sleep(UINT32_MAX);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800234 }
235}