Introduction of the new SAP-UIM socket

Added new socket connection for communication between the SAP module
in Telephony and User Identity Module with DSDA support.

New classes added are:

RilSocket
---------
Base class(virtual) for any type of socket. Has listen
and commands callbacks similar to that of the existing
telephony->rild socket.

RilSapSocket
------------
Derived class for socket for communication between BT SAP
and the sim module in the modem. It initialises
socket, calls Sap_Uim_Init to get the handles and
starts the socket thread.

rilSocketQueue
--------------
Queue for managing socket requests.

Change-Id: I8828173941d6ae76f1f9cc0d567efaf41a77d175
diff --git a/rild/rild.c b/rild/rild.c
index 8e479dc..268170e 100644
--- a/rild/rild.c
+++ b/rild/rild.c
@@ -31,6 +31,7 @@
 #include <cutils/sockets.h>
 #include <sys/capability.h>
 #include <sys/prctl.h>
+#include <libril/ril_ex.h>
 
 #include <private/android_filesystem_config.h>
 #include "hardware/qemu_pipe.h"
@@ -39,8 +40,7 @@
 #define LIB_ARGS_PROPERTY   "rild.libargs"
 #define MAX_LIB_ARGS        16
 
-static void usage(const char *argv0)
-{
+static void usage(const char *argv0) {
     fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0);
     exit(EXIT_FAILURE);
 }
@@ -49,20 +49,23 @@
 
 extern void RIL_register (const RIL_RadioFunctions *callbacks);
 
+extern void RIL_register_socket (RIL_RadioFunctions *(*rilUimInit)
+        (const struct RIL_Env *, int, char **), RIL_SOCKET_TYPE socketType, int argc, char **argv);
+
 extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e,
-                           void *response, size_t responselen);
+        void *response, size_t responselen);
 
 
 #if defined(ANDROID_MULTI_SIM)
 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
-                                size_t datalen, RIL_SOCKET_ID socket_id);
+        size_t datalen, RIL_SOCKET_ID socket_id);
 #else
 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
-                                size_t datalen);
+        size_t datalen);
 #endif
 
 extern void RIL_requestTimedCallback (RIL_TimedCallback callback,
-                               void *param, const struct timeval *relativeTime);
+        void *param, const struct timeval *relativeTime);
 
 
 static struct RIL_Env s_rilEnv = {
@@ -73,8 +76,7 @@
 
 extern void RIL_startEventLoop();
 
-static int make_argv(char * args, char ** argv)
-{
+static int make_argv(char * args, char ** argv) {
     // Note: reserve argv[0]
     int count = 1;
     char * tok;
@@ -116,12 +118,14 @@
     }
 }
 
-int main(int argc, char **argv)
-{
+int main(int argc, char **argv) {
     const char * rilLibPath = NULL;
     char **rilArgv;
     void *dlHandle;
     const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
+    const RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **);
+    char *err_str = NULL;
+
     const RIL_RadioFunctions *funcs;
     char libPath[PROPERTY_VALUE_MAX];
     unsigned char hasLibArgs = 0;
@@ -286,13 +290,26 @@
 
     RIL_startEventLoop();
 
-    rilInit = (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))dlsym(dlHandle, "RIL_Init");
+    rilInit =
+        (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
+        dlsym(dlHandle, "RIL_Init");
 
     if (rilInit == NULL) {
         RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
         exit(EXIT_FAILURE);
     }
 
+    dlerror(); // Clear any previous dlerror
+    rilUimInit =
+        (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
+        dlsym(dlHandle, "RIL_SAP_Init");
+    err_str = dlerror();
+    if (err_str) {
+        RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str);
+    } else if (!rilUimInit) {
+        RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath);
+    }
+
     if (hasLibArgs) {
         rilArgv = argv + i - 1;
         argc = argc -i + 1;
@@ -318,6 +335,13 @@
 
     RLOGD("RIL_Init RIL_register completed");
 
+    if (rilUimInit) {
+        RLOGD("RIL_register_socket started");
+        RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv);
+    }
+
+    RLOGD("RIL_register_socket completed");
+
 done:
 
     RLOGD("RIL_Init starting sleep loop");