• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <guest/hals/ril/libril/ril.h>
28 
29 #define LOG_TAG "RILD"
30 #include <log/log.h>
31 #include <cutils/properties.h>
32 #include <cutils/sockets.h>
33 #include <sys/capability.h>
34 #include <sys/prctl.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <guest/hals/ril/libril/ril_ex.h>
38 
39 #define LIB_PATH_PROPERTY   "vendor.rild.libpath"
40 #define LIB_ARGS_PROPERTY   "vendor.rild.libargs"
41 #define MAX_LIB_ARGS        16
42 
usage(const char * argv0)43 static void usage(const char *argv0) {
44     fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0);
45     exit(EXIT_FAILURE);
46 }
47 
48 extern char ril_service_name_base[MAX_SERVICE_NAME_LENGTH];
49 extern char ril_service_name[MAX_SERVICE_NAME_LENGTH];
50 
51 extern void RIL_register (const RIL_RadioFunctions *callbacks);
52 extern void rilc_thread_pool ();
53 
54 extern void RIL_register_socket (const RIL_RadioFunctions *(*rilUimInit)
55         (const struct RIL_Env *, int, char **), RIL_SOCKET_TYPE socketType, int argc, char **argv);
56 
57 extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e,
58         void *response, size_t responselen);
59 
60 extern void RIL_onRequestAck(RIL_Token t);
61 
62 #if defined(ANDROID_MULTI_SIM)
63 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
64         size_t datalen, RIL_SOCKET_ID socket_id);
65 #else
66 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
67         size_t datalen);
68 #endif
69 
70 extern void RIL_requestTimedCallback (RIL_TimedCallback callback,
71         void *param, const struct timeval *relativeTime);
72 
73 
74 static struct RIL_Env s_rilEnv = {
75     RIL_onRequestComplete,
76     RIL_onUnsolicitedResponse,
77     RIL_requestTimedCallback,
78     RIL_onRequestAck
79 };
80 
81 extern void RIL_startEventLoop();
82 
make_argv(char * args,char ** argv)83 static int make_argv(char * args, char ** argv) {
84     // Note: reserve argv[0]
85     int count = 1;
86     char * tok;
87     char * s = args;
88 
89     while ((tok = strtok(s, " \0"))) {
90         argv[count] = tok;
91         s = NULL;
92         count++;
93     }
94     return count;
95 }
96 
main(int argc,char ** argv)97 int main(int argc, char **argv) {
98     // vendor ril lib path either passed in as -l parameter, or read from rild.libpath property
99     const char *rilLibPath = NULL;
100     // ril arguments either passed in as -- parameter, or read from rild.libargs property
101     char **rilArgv;
102     // handle for vendor ril lib
103     void *dlHandle;
104     // Pointer to ril init function in vendor ril
105     const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
106     // Pointer to sap init function in vendor ril
107     const RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **);
108     const char *err_str = NULL;
109 
110     // functions returned by ril init function in vendor ril
111     const RIL_RadioFunctions *funcs;
112     // lib path from rild.libpath property (if it's read)
113     char libPath[PROPERTY_VALUE_MAX];
114     // flat to indicate if -- parameters are present
115     unsigned char hasLibArgs = 0;
116 
117     int i;
118     // ril/socket id received as -c parameter, otherwise set to 0
119     const char *clientId = NULL;
120 
121     RLOGD("**RIL Daemon Started - Version 1.4**");
122     RLOGD("**RILd param count=%d**", argc);
123 
124     umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
125     for (i = 1; i < argc ;) {
126         if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) {
127             rilLibPath = argv[i + 1];
128             i += 2;
129         } else if (0 == strcmp(argv[i], "--")) {
130             i++;
131             hasLibArgs = 1;
132             break;
133         } else if (0 == strcmp(argv[i], "-c") &&  (argc - i > 1)) {
134             clientId = argv[i+1];
135             i += 2;
136         } else {
137             usage(argv[0]);
138         }
139     }
140 
141     if (clientId == NULL) {
142         clientId = "0";
143     } else if (atoi(clientId) >= MAX_RILDS) {
144         RLOGE("Max Number of rild's supported is: %d", MAX_RILDS);
145         exit(0);
146     }
147     if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) {
148         snprintf(ril_service_name, sizeof(ril_service_name), "%s%s", ril_service_name_base,
149                  clientId);
150     }
151 
152     if (rilLibPath == NULL) {
153         if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) {
154             // No lib sepcified on the command line, and nothing set in props.
155             // Assume "no-ril" case.
156             goto done;
157         } else {
158             rilLibPath = libPath;
159         }
160     }
161 
162     dlHandle = dlopen(rilLibPath, RTLD_NOW);
163 
164     if (dlHandle == NULL) {
165         RLOGE("dlopen failed: %s", dlerror());
166         exit(EXIT_FAILURE);
167     }
168 
169     RIL_startEventLoop();
170 
171     rilInit =
172         (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
173         dlsym(dlHandle, "RIL_Init");
174 
175     if (rilInit == NULL) {
176         RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
177         exit(EXIT_FAILURE);
178     }
179 
180     dlerror(); // Clear any previous dlerror
181     rilUimInit =
182         (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
183         dlsym(dlHandle, "RIL_SAP_Init");
184     err_str = dlerror();
185     if (err_str) {
186         RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str);
187     } else if (!rilUimInit) {
188         RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath);
189     }
190 
191     if (hasLibArgs) {
192         rilArgv = argv + i - 1;
193         argc = argc -i + 1;
194     } else {
195         static char * newArgv[MAX_LIB_ARGS];
196         static char args[PROPERTY_VALUE_MAX];
197         rilArgv = newArgv;
198         property_get(LIB_ARGS_PROPERTY, args, "");
199         argc = make_argv(args, rilArgv);
200     }
201 
202     rilArgv[argc++] = "-c";
203     rilArgv[argc++] = (char*)clientId;
204     RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]);
205 
206     // Make sure there's a reasonable argv[0]
207     rilArgv[0] = argv[0];
208 
209     funcs = rilInit(&s_rilEnv, argc, rilArgv);
210     RLOGD("RIL_Init rilInit completed");
211 
212     RLOGD("RIL_Init callback versions = %d", funcs->version);
213 
214     RIL_register(funcs);
215 
216     RLOGD("RIL_Init RIL_register completed");
217 
218     if (rilUimInit) {
219         RLOGD("RIL_register_socket started");
220         RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv);
221     }
222 
223     RLOGD("RIL_register_socket completed");
224 
225 done:
226 
227     rilc_thread_pool();
228 
229     RLOGD("RIL_Init starting sleep loop");
230     while (true) {
231         sleep(UINT32_MAX);
232     }
233 }
234