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