1 /* //device/system/rild/rild.c
2 **
3 ** Copyright 2006 The Android Open Source Project
4 **
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"
29 #include <log/log.h>
30 #include <cutils/properties.h>
31 #include <cutils/sockets.h>
32 #include <sys/capability.h>
33 #include <sys/prctl.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <libril/ril_ex.h>
37
38 #if defined(PRODUCT_COMPATIBLE_PROPERTY)
39 #define LIB_PATH_PROPERTY "vendor.rild.libpath"
40 #define LIB_ARGS_PROPERTY "vendor.rild.libargs"
41 #else
42 #define LIB_PATH_PROPERTY "rild.libpath"
43 #define LIB_ARGS_PROPERTY "rild.libargs"
44 #endif
45 #define MAX_LIB_ARGS 16
46
usage(const char * argv0)47 static void usage(const char *argv0) {
48 fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0);
49 exit(EXIT_FAILURE);
50 }
51
52 extern char ril_service_name_base[MAX_SERVICE_NAME_LENGTH];
53 extern char ril_service_name[MAX_SERVICE_NAME_LENGTH];
54
55 extern void RIL_register (const RIL_RadioFunctions *callbacks);
56 extern void rilc_thread_pool ();
57
58 extern void RIL_register_socket (const RIL_RadioFunctions *(*rilUimInit)
59 (const struct RIL_Env *, int, char **), RIL_SOCKET_TYPE socketType, int argc, char **argv);
60
61 extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e,
62 void *response, size_t responselen);
63
64 extern void RIL_onRequestAck(RIL_Token t);
65
66 #if defined(ANDROID_MULTI_SIM)
67 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
68 size_t datalen, RIL_SOCKET_ID socket_id);
69 #else
70 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
71 size_t datalen);
72 #endif
73
74 extern void RIL_requestTimedCallback (RIL_TimedCallback callback,
75 void *param, const struct timeval *relativeTime);
76
77
78 static struct RIL_Env s_rilEnv = {
79 RIL_onRequestComplete,
80 RIL_onUnsolicitedResponse,
81 RIL_requestTimedCallback,
82 RIL_onRequestAck
83 };
84
85 extern void RIL_startEventLoop();
86
make_argv(char * args,char ** argv)87 static int make_argv(char * args, char ** argv) {
88 // 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
main(int argc,char ** argv)101 int main(int argc, char **argv) {
102 // vendor ril lib path either passed in as -l parameter, or read from rild.libpath property
103 const char *rilLibPath = NULL;
104 // ril arguments either passed in as -- parameter, or read from rild.libargs property
105 char **rilArgv;
106 // handle for vendor ril lib
107 void *dlHandle;
108 // Pointer to ril init function in vendor ril
109 const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
110 // Pointer to sap init function in vendor ril
111 const RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **);
112 const char *err_str = NULL;
113
114 // functions returned by ril init function in vendor ril
115 const RIL_RadioFunctions *funcs;
116 // lib path from rild.libpath property (if it's read)
117 char libPath[PROPERTY_VALUE_MAX];
118 // flat to indicate if -- parameters are present
119 unsigned char hasLibArgs = 0;
120
121 int i;
122 // ril/socket id received as -c parameter, otherwise set to 0
123 const char *clientId = NULL;
124
125 RLOGD("**RIL Daemon Started**");
126 RLOGD("**RILd param count=%d**", argc);
127
128 umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
129 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;
137 } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) {
138 clientId = argv[i+1];
139 i += 2;
140 } else {
141 usage(argv[0]);
142 }
143 }
144
145 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)) {
152 snprintf(ril_service_name, sizeof(ril_service_name), "%s%s", ril_service_name_base,
153 clientId);
154 }
155
156 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
166 // force to use libcuttlefish-ril-2.so
167 rilLibPath = "libcuttlefish-ril-2.so";
168
169 dlHandle = dlopen(rilLibPath, RTLD_NOW);
170
171 if (dlHandle == NULL) {
172 RLOGE("dlopen failed: %s", dlerror());
173 exit(EXIT_FAILURE);
174 }
175
176 RLOGI("dlopen good: %s", rilLibPath);
177
178 RIL_startEventLoop();
179
180 rilInit =
181 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
182 dlsym(dlHandle, "RIL_Init");
183
184 if (rilInit == NULL) {
185 RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
186 exit(EXIT_FAILURE);
187 }
188
189 dlerror(); // Clear any previous dlerror
190 rilUimInit =
191 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
192 dlsym(dlHandle, "RIL_SAP_Init");
193 err_str = dlerror();
194 if (err_str) {
195 RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str);
196 } else if (!rilUimInit) {
197 RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath);
198 }
199
200 if (hasLibArgs) {
201 rilArgv = argv + i - 1;
202 argc = argc -i + 1;
203 } else {
204 static char * newArgv[MAX_LIB_ARGS];
205 static char args[PROPERTY_VALUE_MAX];
206 rilArgv = newArgv;
207 property_get(LIB_ARGS_PROPERTY, args, "");
208 argc = make_argv(args, rilArgv);
209 }
210
211 rilArgv[argc++] = "-c";
212 rilArgv[argc++] = (char*)clientId;
213 RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]);
214
215 // Make sure there's a reasonable argv[0]
216 rilArgv[0] = argv[0];
217
218 funcs = rilInit(&s_rilEnv, argc, rilArgv);
219 RLOGD("RIL_Init rilInit completed");
220
221 RIL_register(funcs);
222
223 RLOGD("RIL_Init RIL_register completed");
224
225 if (rilUimInit) {
226 RLOGD("RIL_register_socket started");
227 RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv);
228 }
229
230 RLOGD("RIL_register_socket completed");
231
232 rilc_thread_pool();
233
234 done:
235 RLOGD("RIL_Init starting sleep loop");
236 while (true) {
237 sleep(UINT32_MAX);
238 }
239 }
240