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