/*===========================================================================

FUNCTION loc_api_glue_init

DESCRIPTION
   Initiates the RPC client

RETURN VALUE
   1 for success
   0 for failure
   
===========================================================================*/
int loc_api_glue_init(void)
{
    if (loc_api_clnt == NULL)
    {
        /* Print msg */
        ALOGD("Trying to create RPC client...\n");
        loc_api_clnt = clnt_create(NULL, LOC_APIPROG, /*LOC_APIVERS*/ 0x00010000, NULL);
        ALOGD("Created loc_api_clnt ---- %x\n", (unsigned int)loc_api_clnt);

        if (loc_api_clnt == NULL)
        {
            fprintf(stderr, "Error: cannot create RPC client.\n");
            return 0;
        }

        /* Init RPC callbacks */
        int rc = loc_apicb_app_init();
        if (rc >= 0)
        {
            ALOGD("Loc API callback initialized.\n");
        } else {
            fprintf(stderr, "Loc API callback initialization failed.\n");
            return 0;
        }
    }

    return 1;
}
/*===========================================================================

FUNCTION loc_api_glue_init

DESCRIPTION
   Initiates the RPC client

RETURN VALUE
   1 for success
   0 for failure

===========================================================================*/
int loc_api_glue_init(void)
{
   if (loc_api_clnt == NULL)
   {
      /* Initialize data */
      int i;
      int pid = getpid();
      for (i = 0; i < LOC_API_CB_MAX_CLIENTS; i++)
      {
          loc_glue_callback_table[i].cb_id = i | (pid << 16);
          loc_glue_callback_table[i].cb_func = NULL;
          loc_glue_callback_table[i].handle = -1;
          loc_glue_callback_table[i].rpc_cb = NULL;
          loc_glue_callback_table[i].user = NULL;
      }

      /* Print msg */
      LOC_LOGV("Trying to create RPC client...\n");
      loc_api_clnt = clnt_create(NULL, LOC_APIPROG, LOC_APIVERS, NULL);
      LOC_LOGV("Created loc_api_clnt ---- %x\n", (unsigned int)loc_api_clnt);

      if (loc_api_clnt == NULL)
      {
         LOC_LOGE("Error: cannot create RPC client.\n");
         return 0;
      }

      /* Init RPC callbacks */
      loc_api_sync_call_init();

      int rc = loc_apicb_app_init();
      if (rc >= 0)
      {
         LOC_LOGD("Loc API RPC client initialized.\n");
         clnt_register_reset_notification_cb(loc_api_clnt, loc_api_glue_rpc_cb);
      }
      else {
         LOC_LOGE("Loc API callback initialization failed.\n");
         return 0;
      }
   }

   return 1;
}