1b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/******************************************************************************
2b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood  @file:  loc_eng.cpp
3b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood  @brief:
4b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
5b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood  DESCRIPTION
6b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    This file defines the implemenation for GPS hardware abstraction layer.
7b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
8b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood  INITIALIZATION AND SEQUENCING REQUIREMENTS
9b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
10b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood  -----------------------------------------------------------------------------
11b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodCopyright (c) 2009, QUALCOMM USA, INC.
12b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
13b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodAll rights reserved.
14b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
15b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRedistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
16b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
17b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood�         Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
18b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
19b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood�         Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
20b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
21b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood�         Neither the name of the QUALCOMM USA, INC.  nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
22b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
23b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood  -----------------------------------------------------------------------------
25b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
26b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood******************************************************************************/
27b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
28b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*=====================================================================
29b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood$Header: $
30b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood$DateTime: $
31b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood$Author: $
32b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood======================================================================*/
33b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
34b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#define LOG_NDDEBUG 0
35b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
36b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#include <stdio.h>
37b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#include <stdlib.h>
38b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#include <unistd.h>
39b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#include <ctype.h>
40b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#include <math.h>
41b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#include <pthread.h>
42b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#include <arpa/inet.h>
43b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#include <netdb.h>
44b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
45b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#include <rpc/rpc.h>
46b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#include "loc_api_rpc_glue.h"
47b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#include "loc_apicb_appinit.h"
48b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
49b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#include <cutils/properties.h>
50b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#include <cutils/sched_policy.h>
51b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#include <utils/SystemClock.h>
52b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
53b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#include <loc_eng.h>
54b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#include <loc_eng_ni.h>
55b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
56b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#define LOG_TAG "lib_locapi"
57b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#include <utils/Log.h>
58b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
59b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood// comment this out to enable logging
60b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block// #undef ALOGD
61b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block// #define ALOGD(...) {}
62b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
63b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#define DEBUG_MOCK_NI 0
64b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
65b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood// Function declarations for sLocEngInterface
66b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int  loc_eng_init(GpsCallbacks* callbacks);
67b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int  loc_eng_start();
68b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int  loc_eng_stop();
693c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwoodstatic int  loc_eng_set_position_mode(GpsPositionMode mode, GpsPositionRecurrence recurrence,
703c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood            uint32_t min_interval, uint32_t preferred_accuracy, uint32_t preferred_time);
71b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_cleanup();
72b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int  loc_eng_inject_time(GpsUtcTime time, int64_t timeReference, int uncertainty);
73b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int  loc_eng_inject_location(double latitude, double longitude, float accuracy);
74b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_delete_aiding_data (GpsAidingData f);
75b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic const void* loc_eng_get_extension(const char* name);
76b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
77b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood// Function declarations for sLocEngAGpsInterface
78b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_agps_init(AGpsCallbacks* callbacks);
79b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int loc_eng_agps_data_conn_open(const char* apn);
80b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int loc_eng_agps_data_conn_closed();
81b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int loc_eng_agps_data_conn_failed();
82b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int loc_eng_agps_set_server(AGpsType type, const char* hostname, int port);
83b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
84b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
85b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int32 loc_event_cb (rpc_loc_client_handle_type client_handle,
86b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                           rpc_loc_event_mask_type loc_event,
87b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                           const rpc_loc_event_payload_u_type* loc_event_payload);
88b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_report_position (const rpc_loc_parsed_position_s_type *location_report_ptr);
89b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_report_sv (const rpc_loc_gnss_info_s_type *gnss_report_ptr);
90b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_report_status (const rpc_loc_status_event_s_type *status_report_ptr);
91b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_report_nmea (const rpc_loc_nmea_report_s_type *nmea_report_ptr);
92b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_process_conn_request (const rpc_loc_server_request_s_type *server_request_ptr);
93b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
945153d56321e20974d29c0649839d96e8351775b6Mike Lockwoodstatic void loc_eng_process_deferred_action (void* arg);
952c115de4096177477cf9495de9dd533267d0607eMike Lockwoodstatic void loc_eng_process_atl_deferred_action (int flags);
96b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_delete_aiding_data_deferred_action (void);
97b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
98b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int set_agps_server();
99b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
100b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood// Defines the GpsInterface in gps.h
101b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic const GpsInterface sLocEngInterface =
102b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
103300b715b0dc36ae82b442a201432a5f2065e664bMike Lockwood    sizeof(GpsInterface),
104b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_init,
105b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_start,
106b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_stop,
107b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_cleanup,
108b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_inject_time,
109b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_inject_location,
110b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_delete_aiding_data,
111b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_set_position_mode,
112b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_get_extension,
113b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood};
114b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
115b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic const AGpsInterface sLocEngAGpsInterface =
116b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
117300b715b0dc36ae82b442a201432a5f2065e664bMike Lockwood    sizeof(AGpsInterface),
118b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_agps_init,
119b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_agps_data_conn_open,
120b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_agps_data_conn_closed,
121b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_agps_data_conn_failed,
122b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_agps_set_server,
123b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood};
124b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
125b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood// Global data structure for location engine
126b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodloc_eng_data_s_type loc_eng_data;
127b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
128b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
129b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    gps_get_hardware_interface
130b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
131b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
132b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   Returns the GPS hardware interaface based on LOC API
133b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   if GPS is enabled.
134b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
135b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
136b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   None
137b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
138b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
139b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   0: success
140b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
141b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
142b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
143b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
144b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
145b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodconst GpsInterface* gps_get_hardware_interface ()
146b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
147b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    char propBuf[PROPERTY_VALUE_MAX];
148b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
149b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    // check to see if GPS should be disabled
150b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    property_get("gps.disable", propBuf, "");
151b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (propBuf[0] == '1')
152b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
153b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block        ALOGD("gps_get_interface returning NULL because gps.disable=1\n");
154b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        return NULL;
155b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
156b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
157b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    return &sLocEngInterface;
158b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
159b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
160b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
161b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    loc_eng_init
162b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
163b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
164b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   Initialize the location engine, this include setting up global datas
165b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   and registers location engien with loc api service.
166b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
167b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
168b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   None
169b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
170b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
171b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   0: success
172b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
173b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
174b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
175b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
176b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
1779391343952a0ab27a738f5b3de5c5525ecbc096dMike Lockwood
1789391343952a0ab27a738f5b3de5c5525ecbc096dMike Lockwood// fully shutting down the GPS is temporarily disabled to avoid intermittent BP crash
1799391343952a0ab27a738f5b3de5c5525ecbc096dMike Lockwood#define DISABLE_CLEANUP 1
1809391343952a0ab27a738f5b3de5c5525ecbc096dMike Lockwood
181b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int loc_eng_init(GpsCallbacks* callbacks)
182b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
1839391343952a0ab27a738f5b3de5c5525ecbc096dMike Lockwood#if DISABLE_CLEANUP
1849391343952a0ab27a738f5b3de5c5525ecbc096dMike Lockwood    if (loc_eng_data.deferred_action_thread) {
1859391343952a0ab27a738f5b3de5c5525ecbc096dMike Lockwood        // already initialized
1869391343952a0ab27a738f5b3de5c5525ecbc096dMike Lockwood        return 0;
1879391343952a0ab27a738f5b3de5c5525ecbc096dMike Lockwood    }
1889391343952a0ab27a738f5b3de5c5525ecbc096dMike Lockwood#endif
189b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    // Start the LOC api RPC service
190b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_api_glue_init ();
191b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1923c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood    callbacks->set_capabilities_cb(GPS_CAPABILITY_SCHEDULING | GPS_CAPABILITY_MSA | GPS_CAPABILITY_MSB);
1933c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood
194b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    memset (&loc_eng_data, 0, sizeof (loc_eng_data_s_type));
195b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
196b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    // LOC ENG module data initialization
1972c115de4096177477cf9495de9dd533267d0607eMike Lockwood    loc_eng_data.location_cb    = callbacks->location_cb;
1982c115de4096177477cf9495de9dd533267d0607eMike Lockwood    loc_eng_data.sv_status_cb   = callbacks->sv_status_cb;
1992c115de4096177477cf9495de9dd533267d0607eMike Lockwood    loc_eng_data.status_cb      = callbacks->status_cb;
2002c115de4096177477cf9495de9dd533267d0607eMike Lockwood    loc_eng_data.nmea_cb        = callbacks->nmea_cb;
2012c115de4096177477cf9495de9dd533267d0607eMike Lockwood    loc_eng_data.acquire_wakelock_cb = callbacks->acquire_wakelock_cb;
2022c115de4096177477cf9495de9dd533267d0607eMike Lockwood    loc_eng_data.release_wakelock_cb = callbacks->release_wakelock_cb;
203b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
204b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_event_mask_type event = RPC_LOC_EVENT_PARSED_POSITION_REPORT |
205b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                                    RPC_LOC_EVENT_SATELLITE_REPORT |
206b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                                    RPC_LOC_EVENT_LOCATION_SERVER_REQUEST |
207b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                                    RPC_LOC_EVENT_ASSISTANCE_DATA_REQUEST |
208b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                                    RPC_LOC_EVENT_IOCTL_REPORT |
209b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                                    RPC_LOC_EVENT_STATUS_REPORT |
210b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                                    RPC_LOC_EVENT_NMEA_POSITION_REPORT |
211b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                                    RPC_LOC_EVENT_NI_NOTIFY_VERIFY_REQUEST;
212b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
213b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_data.client_handle = loc_open (event, loc_event_cb);
214b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
215b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_mutex_init (&(loc_eng_data.deferred_action_mutex), NULL);
216b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_cond_init  (&(loc_eng_data.deferred_action_cond) , NULL);
217bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood     pthread_mutex_init (&(loc_eng_data.deferred_stop_mutex), NULL);
218bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood
21924d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    loc_eng_data.loc_event = 0;
2202c115de4096177477cf9495de9dd533267d0607eMike Lockwood    loc_eng_data.deferred_action_flags = 0;
221b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    memset (loc_eng_data.apn_name, 0, sizeof (loc_eng_data.apn_name));
222b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
223b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_data.aiding_data_for_deletion = 0;
224b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_data.engine_status = GPS_STATUS_NONE;
225b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
226b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    // XTRA module data initialization
227b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_data.xtra_module_data.download_request_cb = NULL;
228b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
229b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    // IOCTL module data initialization
230b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_data.ioctl_data.cb_is_selected  = FALSE;
231b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_data.ioctl_data.cb_is_waiting   = FALSE;
232b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_data.ioctl_data.client_handle   = RPC_LOC_CLIENT_HANDLE_INVALID;
233b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    memset (&(loc_eng_data.ioctl_data.cb_payload),
234b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            0,
235b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            sizeof (rpc_loc_ioctl_callback_s_type));
236b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
237b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_mutex_init (&(loc_eng_data.ioctl_data.cb_data_mutex), NULL);
238b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_cond_init(&loc_eng_data.ioctl_data.cb_arrived_cond, NULL);
239b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
2405153d56321e20974d29c0649839d96e8351775b6Mike Lockwood    loc_eng_data.deferred_action_thread = callbacks->create_thread_cb("loc_api",
2415153d56321e20974d29c0649839d96e8351775b6Mike Lockwood                                            loc_eng_process_deferred_action, NULL);
242b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
243b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD ("loc_eng_init called, client id = %d\n", (int32) loc_eng_data.client_handle);
244b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    return 0;
245b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
246b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
247b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
248b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    loc_eng_cleanup
249b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
250b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
251b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   Cleans location engine. The location client handle will be released.
252b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
253b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
254b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   None
255b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
256b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
257b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   None
258b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
259b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
260b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
261b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
262b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
263b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_cleanup()
264b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
2659391343952a0ab27a738f5b3de5c5525ecbc096dMike Lockwood#if DISABLE_CLEANUP
2669391343952a0ab27a738f5b3de5c5525ecbc096dMike Lockwood    return;
2679391343952a0ab27a738f5b3de5c5525ecbc096dMike Lockwood#else
268b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (loc_eng_data.deferred_action_thread)
269b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
270b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        /* Terminate deferred action working thread */
2712c115de4096177477cf9495de9dd533267d0607eMike Lockwood        pthread_mutex_lock(&loc_eng_data.deferred_action_mutex);
2722c115de4096177477cf9495de9dd533267d0607eMike Lockwood        /* hold a wake lock while events are pending for deferred_action_thread */
2732c115de4096177477cf9495de9dd533267d0607eMike Lockwood        loc_eng_data.acquire_wakelock_cb();
2742c115de4096177477cf9495de9dd533267d0607eMike Lockwood        loc_eng_data.deferred_action_flags |= DEFERRED_ACTION_QUIT;
2752c115de4096177477cf9495de9dd533267d0607eMike Lockwood        pthread_cond_signal(&loc_eng_data.deferred_action_cond);
2762c115de4096177477cf9495de9dd533267d0607eMike Lockwood        pthread_mutex_unlock(&loc_eng_data.deferred_action_mutex);
277b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
278b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        void* ignoredValue;
279b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        pthread_join(loc_eng_data.deferred_action_thread, &ignoredValue);
280b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        loc_eng_data.deferred_action_thread = NULL;
281b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
282b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
283b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    // clean up
284b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    (void) loc_close (loc_eng_data.client_handle);
285b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
286b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_mutex_destroy (&loc_eng_data.deferred_action_mutex);
287b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_cond_destroy  (&loc_eng_data.deferred_action_cond);
288b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
289b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_mutex_destroy (&loc_eng_data.ioctl_data.cb_data_mutex);
290b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_cond_destroy  (&loc_eng_data.ioctl_data.cb_arrived_cond);
291b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
292ae1989429bcdce118229b4b728daaed99b039e8fMike Lockwood// Do not call this as it can result in the ARM9 crashing if it sends events while we are disabled
293ae1989429bcdce118229b4b728daaed99b039e8fMike Lockwood//    loc_apicb_app_deinit();
2949391343952a0ab27a738f5b3de5c5525ecbc096dMike Lockwood#endif
295b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
296b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
297b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
298b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
299b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    loc_eng_start
300b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
301b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
302b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   Starts the tracking session
303b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
304b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
305b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   None
306b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
307b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
308b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   0: success
309b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
310b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
311b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
312b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
313b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
314b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int loc_eng_start()
315b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
316b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    int ret_val;
317b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD ("loc_eng_start\n");
318b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
319b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (loc_eng_data.position_mode != GPS_POSITION_MODE_STANDALONE &&
320b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            loc_eng_data.agps_server_host[0] != 0 &&
321b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            loc_eng_data.agps_server_port != 0) {
322b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        int result = set_agps_server();
323b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block        ALOGD ("set_agps_server returned = %d\n", result);
324b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
325b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
326b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    ret_val = loc_start_fix (loc_eng_data.client_handle);
327b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
328b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (ret_val != RPC_LOC_API_SUCCESS)
329b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
330b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block        ALOGD ("loc_eng_start returned error = %d\n", ret_val);
331b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
332b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
333b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    return 0;
334b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
335b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
336b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
337b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
338b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    loc_eng_stop
339b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
340b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
341b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   Stops the tracking session
342b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
343b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
344b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   None
345b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
346b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
347b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   0: success
348b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
349b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
350b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
351b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
352b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
353b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int loc_eng_stop()
354b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
355b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    int ret_val;
356b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
357b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD ("loc_eng_stop\n");
358b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
359bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood    pthread_mutex_lock(&(loc_eng_data.deferred_stop_mutex));
360bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood    // work around problem with loc_eng_stop when AGPS requests are pending
361bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood    // we defer stopping the engine until the AGPS request is done
362bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood    if (loc_eng_data.agps_request_pending)
363bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood    {
364bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood        loc_eng_data.stop_request_pending = true;
365b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block        ALOGD ("deferring stop until AGPS data call is finished\n");
366bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood        pthread_mutex_unlock(&(loc_eng_data.deferred_stop_mutex));
367bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood        return 0;
368bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood    }
369bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood    pthread_mutex_unlock(&(loc_eng_data.deferred_stop_mutex));
370bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood
371b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    ret_val = loc_stop_fix (loc_eng_data.client_handle);
372b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (ret_val != RPC_LOC_API_SUCCESS)
373b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
374b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block        ALOGD ("loc_eng_stop returned error = %d\n", ret_val);
375b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
376b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
377b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    return 0;
378b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
379b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
380b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int loc_eng_set_gps_lock(rpc_loc_lock_e_type lock_type)
381b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
382b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_ioctl_data_u_type    ioctl_data;
383b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    boolean                      ret_val;
384b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
385b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD ("loc_eng_set_gps_lock mode, client = %d, lock_type = %d\n",
386b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            (int32) loc_eng_data.client_handle, lock_type);
387b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
388b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    ioctl_data.rpc_loc_ioctl_data_u_type_u.engine_lock = lock_type;
389b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    ioctl_data.disc = RPC_LOC_IOCTL_SET_ENGINE_LOCK;
390b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
391b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    ret_val = loc_eng_ioctl (loc_eng_data.client_handle,
392b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                            RPC_LOC_IOCTL_SET_ENGINE_LOCK,
393b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                            &ioctl_data,
394b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                            LOC_IOCTL_DEFAULT_TIMEOUT,
395b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                            NULL /* No output information is expected*/);
396b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
397b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (ret_val != TRUE)
398b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
399b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block        ALOGD ("loc_eng_set_gps_lock mode failed\n");
400b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
401b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
402b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    return 0;
403b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
404b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
405b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
406b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    loc_eng_set_position_mode
407b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
408b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
4093c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood   Sets the mode and fix frequency for the tracking session.
410b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
411b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
412b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   None
413b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
414b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
415b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   0: success
416b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
417b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
418b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
419b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
420b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
4213c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwoodstatic int loc_eng_set_position_mode(GpsPositionMode mode, GpsPositionRecurrence recurrence,
4223c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood            uint32_t min_interval, uint32_t preferred_accuracy, uint32_t preferred_time)
423b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
424b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_ioctl_data_u_type    ioctl_data;
425b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_fix_criteria_s_type *fix_criteria_ptr;
426b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    boolean                      ret_val;
427b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
428b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD ("loc_eng_set_position mode, client = %d, interval = %d, mode = %d\n",
4293c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood            (int32) loc_eng_data.client_handle, min_interval, mode);
430b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
431b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_data.position_mode = mode;
432b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    ioctl_data.disc = RPC_LOC_IOCTL_SET_FIX_CRITERIA;
433b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
434b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    fix_criteria_ptr = &(ioctl_data.rpc_loc_ioctl_data_u_type_u.fix_criteria);
4353c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood    fix_criteria_ptr->valid_mask = RPC_LOC_FIX_CRIT_VALID_PREFERRED_OPERATION_MODE |
436b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                                   RPC_LOC_FIX_CRIT_VALID_RECURRENCE_TYPE;
437b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
4383c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood    switch (mode) {
4393c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood        case GPS_POSITION_MODE_MS_BASED:
4403c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood            fix_criteria_ptr->preferred_operation_mode = RPC_LOC_OPER_MODE_MSB;
4413c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood            break;
4423c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood        case GPS_POSITION_MODE_MS_ASSISTED:
4433c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood            fix_criteria_ptr->preferred_operation_mode = RPC_LOC_OPER_MODE_MSA;
4443c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood            break;
4453c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood        case GPS_POSITION_MODE_STANDALONE:
4463c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood        default:
4473c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood            fix_criteria_ptr->preferred_operation_mode = RPC_LOC_OPER_MODE_STANDALONE;
4483c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood            break;
449b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
4503c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood    if (min_interval > 0) {
4513c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood        fix_criteria_ptr->min_interval = min_interval;
4523c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood        fix_criteria_ptr->valid_mask |= RPC_LOC_FIX_CRIT_VALID_MIN_INTERVAL;
453b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
4543c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood    if (preferred_accuracy > 0) {
4553c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood        fix_criteria_ptr->preferred_accuracy = preferred_accuracy;
4563c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood        fix_criteria_ptr->valid_mask |= RPC_LOC_FIX_CRIT_VALID_PREFERRED_ACCURACY;
4573c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood    }
4583c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood    if (preferred_time > 0) {
4593c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood        fix_criteria_ptr->preferred_response_time = preferred_time;
4603c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood        fix_criteria_ptr->valid_mask |= RPC_LOC_FIX_CRIT_VALID_PREFERRED_RESPONSE_TIME;
4613c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood    }
4623c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood
4633c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood    switch (recurrence) {
4643c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood        case GPS_POSITION_RECURRENCE_SINGLE:
4653c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood            fix_criteria_ptr->recurrence_type = RPC_LOC_SINGLE_FIX;
4663c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood            break;
4673c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood        case GPS_POSITION_RECURRENCE_PERIODIC:
4683c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood        default:
4693c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood            fix_criteria_ptr->recurrence_type = RPC_LOC_PERIODIC_FIX;
4703c865ec2f9efbe5e21b55c1b43a2fbe9ffe1000aMike Lockwood            break;
471b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
472b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
473b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    ret_val = loc_eng_ioctl(loc_eng_data.client_handle,
474b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                            RPC_LOC_IOCTL_SET_FIX_CRITERIA,
475b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                            &ioctl_data,
476b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                            LOC_IOCTL_DEFAULT_TIMEOUT,
477b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                            NULL /* No output information is expected*/);
478b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
479b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (ret_val != TRUE)
480b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
481b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block        ALOGD ("loc_eng_set_position mode failed\n");
482b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
483b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
484b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    return 0;
485b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
486b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
487b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
488b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    loc_eng_inject_time
489b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
490b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
491b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   This is used by Java native function to do time injection.
492b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
493b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
494b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   None
495b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
496b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
497b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   RPC_LOC_API_SUCCESS
498b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
499b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
500b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
501b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
502b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
503b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int loc_eng_inject_time (GpsUtcTime time, int64_t timeReference, int uncertainty)
504b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
505b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_ioctl_data_u_type       ioctl_data;
506b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_assist_data_time_s_type *time_info_ptr;
507b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    boolean                          ret_val;
508b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
509b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD ("loc_eng_inject_time, uncertainty = %d\n", uncertainty);
510b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
511b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    ioctl_data.disc = RPC_LOC_IOCTL_INJECT_UTC_TIME;
512b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
513b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    time_info_ptr = &(ioctl_data.rpc_loc_ioctl_data_u_type_u.assistance_data_time);
514b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    time_info_ptr->time_utc = time;
515b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    time_info_ptr->time_utc += (int64_t)(android::elapsedRealtime() - timeReference);
516b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    time_info_ptr->uncertainty = uncertainty; // Uncertainty in ms
517b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
518b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    ret_val = loc_eng_ioctl (loc_eng_data.client_handle,
519b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                             RPC_LOC_IOCTL_INJECT_UTC_TIME,
520b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                             &ioctl_data,
521b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                             LOC_IOCTL_DEFAULT_TIMEOUT,
522b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                             NULL /* No output information is expected*/);
523b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
524b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (ret_val != TRUE)
525b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
526b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block        ALOGD ("loc_eng_inject_time failed\n");
527b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
528b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
529b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    return 0;
530b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
531b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
532b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int loc_eng_inject_location (double latitude, double longitude, float accuracy)
533b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
534b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    /* not yet implemented */
535b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    return 0;
536b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
537b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
538b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
539b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    loc_eng_delete_aiding_data
540b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
541b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
542b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   This is used by Java native function to delete the aiding data. The function
543b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   updates the global variable for the aiding data to be deleted. If the GPS
544b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   engine is off, the aiding data will be deleted. Otherwise, the actual action
545b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   will happen when gps engine is turned off.
546b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
547b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
548b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   Assumes the aiding data type specified in GpsAidingData matches with
549b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   LOC API specification.
550b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
551b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
552b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   RPC_LOC_API_SUCCESS
553b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
554b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
555b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
556b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
557b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
558b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_delete_aiding_data (GpsAidingData f)
559b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
560b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_mutex_lock(&(loc_eng_data.deferred_action_mutex));
561b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
562a4977cd631b6f4bc1ee74d0ed60977cb06d12e6fMike Lockwood    // Currently, LOC API only support deletion of all aiding data,
563a4977cd631b6f4bc1ee74d0ed60977cb06d12e6fMike Lockwood    if (f)
564b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        loc_eng_data.aiding_data_for_deletion = GPS_DELETE_ALL;
565b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
5666774225c7860662a75ff580ded6f23b50dd8f324Mike Lockwood    if ((loc_eng_data.engine_status != GPS_STATUS_SESSION_BEGIN) &&
567b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        (loc_eng_data.aiding_data_for_deletion != 0))
568b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
5692c115de4096177477cf9495de9dd533267d0607eMike Lockwood        /* hold a wake lock while events are pending for deferred_action_thread */
5702c115de4096177477cf9495de9dd533267d0607eMike Lockwood        loc_eng_data.acquire_wakelock_cb();
5712c115de4096177477cf9495de9dd533267d0607eMike Lockwood        loc_eng_data.deferred_action_flags |= DEFERRED_ACTION_DELETE_AIDING;
572b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        pthread_cond_signal(&(loc_eng_data.deferred_action_cond));
573b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
574b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        // In case gps engine is ON, the assistance data will be deleted when the engine is OFF
575b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
576b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
577b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_mutex_unlock(&(loc_eng_data.deferred_action_mutex));
578b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
579b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
580b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
581b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    loc_eng_get_extension
582b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
583b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
584b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   Get the gps extension to support XTRA.
585b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
586b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
587b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
588b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
589b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
590b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   The GPS extension interface.
591b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
592b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
593b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
594b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
595b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
596b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic const void* loc_eng_get_extension(const char* name)
597b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
598b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (strcmp(name, GPS_XTRA_INTERFACE) == 0)
599b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
600b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        return &sLocEngXTRAInterface;
601b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
602b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    else if (strcmp(name, AGPS_INTERFACE) == 0)
603b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
604b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        return &sLocEngAGpsInterface;
605b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
606b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    else if (strcmp(name, GPS_NI_INTERFACE) == 0)
607b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
608b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        return &sLocEngNiInterface;
609b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
610b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
611b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    return NULL;
612b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
613b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
614b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#if DEBUG_MOCK_NI == 1
615b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
616b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    mock_ni
617b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
618b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
619b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   DEBUG tool: simulate an NI request
620b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
621b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
622b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
623b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
624b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
625b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   None
626b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
627b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
628b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
629b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
630b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
631b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void* mock_ni(void* arg)
632b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
633b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    static int busy = 0;
634b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
635b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (busy) return NULL;
636b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
637b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    busy = 1;
638b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
639b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    sleep(5);
640b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
641b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_client_handle_type           client_handle;
642b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_event_mask_type              loc_event;
643b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_event_payload_u_type         payload;
644b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_ni_event_s_type             *ni_req;
645b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_ni_supl_notify_verify_req_s_type *supl_req;
646b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
647b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    client_handle = (rpc_loc_client_handle_type) arg;
648b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
649b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_event = RPC_LOC_EVENT_NI_NOTIFY_VERIFY_REQUEST;
650b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    payload.disc = loc_event;
651b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
652b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    ni_req = &payload.rpc_loc_event_payload_u_type_u.ni_request;
653b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    ni_req->event = RPC_LOC_NI_EVENT_SUPL_NOTIFY_VERIFY_REQ;
654b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    supl_req = &ni_req->payload.rpc_loc_ni_event_payload_u_type_u.supl_req;
655b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
656b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    // Encodings for Spirent Communications
657b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    char client_name[80]  = {0x53,0x78,0x5A,0x5E,0x76,0xD3,0x41,0xC3,0x77,
658b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            0xBB,0x5D,0x77,0xA7,0xC7,0x61,0x7A,0xFA,0xED,0x9E,0x03};
659b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    char requestor_id[80] = {0x53,0x78,0x5A,0x5E,0x76,0xD3,0x41,0xC3,0x77,
660b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            0xBB,0x5D,0x77,0xA7,0xC7,0x61,0x7A,0xFA,0xED,0x9E,0x03};
661b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
662b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    supl_req->flags = RPC_LOC_NI_CLIENT_NAME_PRESENT |
663b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                      RPC_LOC_NI_REQUESTOR_ID_PRESENT |
664b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                      RPC_LOC_NI_ENCODING_TYPE_PRESENT;
665b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
666b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    supl_req->datacoding_scheme = RPC_LOC_NI_SUPL_GSM_DEFAULT;
667b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
668b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    supl_req->client_name.data_coding_scheme = RPC_LOC_NI_SUPL_GSM_DEFAULT; // no coding
669b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    supl_req->client_name.client_name_string.client_name_string_len = strlen(client_name);
670b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    supl_req->client_name.client_name_string.client_name_string_val = client_name;
671b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    supl_req->client_name.string_len = strlen(client_name);
672b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
673b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    supl_req->requestor_id.data_coding_scheme = RPC_LOC_NI_SUPL_GSM_DEFAULT;
674b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    supl_req->requestor_id.requestor_id_string.requestor_id_string_len = strlen(requestor_id);
675b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    supl_req->requestor_id.requestor_id_string.requestor_id_string_val = requestor_id;
676b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    supl_req->requestor_id.string_len = strlen(requestor_id);
677b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
678b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    supl_req->notification_priv_type = RPC_LOC_NI_USER_NOTIFY_VERIFY_ALLOW_NO_RESP;
679b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    supl_req->user_response_timer = 10;
680b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
681b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_event_cb(client_handle, loc_event, &payload);
682b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
683b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    busy = 0;
684b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
685b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    return NULL;
686b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
687b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood#endif // DEBUG_MOCK_NI
688b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
689b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
690b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    loc_event_cb
691b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
692b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
693b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   This is the callback function registered by loc_open.
694b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
695b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
696b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
697b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
698b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
699b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   RPC_LOC_API_SUCCESS
700b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
701b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
702b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
703b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
704b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
705b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int32 loc_event_cb(
706b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_client_handle_type           client_handle,
707b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_event_mask_type              loc_event,
708b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    const rpc_loc_event_payload_u_type*  loc_event_payload
709b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    )
710b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
7117c2f7b69f91e660f872d0ca15ab14b32d23e53d4Steve Block    ALOGV ("loc_event_cb, client = %d, loc_event = 0x%x", (int32) client_handle, (uint32) loc_event);
712b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (client_handle == loc_eng_data.client_handle)
713b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
71424d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        pthread_mutex_lock(&loc_eng_data.deferred_action_mutex);
71524d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        loc_eng_data.loc_event = loc_event;
71624d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        memcpy(&loc_eng_data.loc_event_payload, loc_event_payload, sizeof(*loc_event_payload));
717b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
7182c115de4096177477cf9495de9dd533267d0607eMike Lockwood        /* hold a wake lock while events are pending for deferred_action_thread */
7192c115de4096177477cf9495de9dd533267d0607eMike Lockwood        loc_eng_data.acquire_wakelock_cb();
7202c115de4096177477cf9495de9dd533267d0607eMike Lockwood        loc_eng_data.deferred_action_flags |= DEFERRED_ACTION_EVENT;
7212c115de4096177477cf9495de9dd533267d0607eMike Lockwood        pthread_cond_signal(&loc_eng_data.deferred_action_cond);
7222c115de4096177477cf9495de9dd533267d0607eMike Lockwood        pthread_mutex_unlock(&loc_eng_data.deferred_action_mutex);
723b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
724b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    else
725b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
726b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block        ALOGD ("loc client mismatch: received = %d, expected = %d \n", (int32) client_handle, (int32) loc_eng_data.client_handle);
727b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
728b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
729b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    return RPC_LOC_API_SUCCESS;
730b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
731b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
732b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
733b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    loc_eng_report_position
734b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
735b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
736b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   Reports position information to the Java layer.
737b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
738b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
739b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
740b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
741b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
742b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
743b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
744b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
745b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
746b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
747b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
748b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_report_position (const rpc_loc_parsed_position_s_type *location_report_ptr)
749b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
750b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    GpsLocation location;
751b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
7527c2f7b69f91e660f872d0ca15ab14b32d23e53d4Steve Block    ALOGV ("loc_eng_report_position: location report, valid mask = 0x%x, sess status = %d\n",
753b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood         (uint32) location_report_ptr->valid_mask, location_report_ptr->session_status);
754b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
755300b715b0dc36ae82b442a201432a5f2065e664bMike Lockwood    memset (&location, 0, sizeof(location));
756300b715b0dc36ae82b442a201432a5f2065e664bMike Lockwood    location.size = sizeof(location);
757b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (location_report_ptr->valid_mask & RPC_LOC_POS_VALID_SESSION_STATUS)
758b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
759b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        // Not a position report, return
760b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        if (location_report_ptr->session_status == RPC_LOC_SESS_STATUS_SUCCESS)
761b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        {
762b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            if (location_report_ptr->valid_mask & RPC_LOC_POS_VALID_TIMESTAMP_UTC)
763b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            {
764b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                location.timestamp = location_report_ptr->timestamp_utc;
765b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            }
766b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
767b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            if ((location_report_ptr->valid_mask & RPC_LOC_POS_VALID_LATITUDE) &&
768b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                (location_report_ptr->valid_mask & RPC_LOC_POS_VALID_LONGITUDE))
769b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            {
770b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                location.flags    |= GPS_LOCATION_HAS_LAT_LONG;
771b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                location.latitude  = location_report_ptr->latitude;
772b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                location.longitude = location_report_ptr->longitude;
773b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            }
774b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
775b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            if (location_report_ptr->valid_mask &  RPC_LOC_POS_VALID_ALTITUDE_WRT_ELLIPSOID )
776b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            {
777b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                location.flags    |= GPS_LOCATION_HAS_ALTITUDE;
778b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                location.altitude = location_report_ptr->altitude_wrt_ellipsoid;
779b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            }
780b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
781b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            if ((location_report_ptr->valid_mask & RPC_LOC_POS_VALID_SPEED_HORIZONTAL) &&
782b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                (location_report_ptr->valid_mask & RPC_LOC_POS_VALID_SPEED_VERTICAL))
783b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            {
784b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                location.flags    |= GPS_LOCATION_HAS_SPEED;
785b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                location.speed = sqrt(location_report_ptr->speed_horizontal * location_report_ptr->speed_horizontal +
786b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                                     location_report_ptr->speed_vertical * location_report_ptr->speed_vertical);
787b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            }
788b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
789b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            if (location_report_ptr->valid_mask &  RPC_LOC_POS_VALID_HEADING)
790b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            {
791b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                location.flags    |= GPS_LOCATION_HAS_BEARING;
792b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                location.bearing = location_report_ptr->heading;
793b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            }
794b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
795b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            if (location_report_ptr->valid_mask & RPC_LOC_POS_VALID_HOR_UNC_CIRCULAR)
796b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            {
797b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                location.flags    |= GPS_LOCATION_HAS_ACCURACY;
798b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                location.accuracy = location_report_ptr->hor_unc_circular;
799b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            }
800b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
801b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            if (loc_eng_data.location_cb != NULL)
802b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            {
8037c2f7b69f91e660f872d0ca15ab14b32d23e53d4Steve Block                ALOGV ("loc_eng_report_position: fire callback\n");
804b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                loc_eng_data.location_cb (&location);
805b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            }
806b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        }
807b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        else
808b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        {
8097c2f7b69f91e660f872d0ca15ab14b32d23e53d4Steve Block            ALOGV ("loc_eng_report_position: ignore position report when session status = %d\n", location_report_ptr->session_status);
810b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        }
811b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
812b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    else
813b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
8147c2f7b69f91e660f872d0ca15ab14b32d23e53d4Steve Block        ALOGV ("loc_eng_report_position: ignore position report when session status is not set\n");
815b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
816b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
817b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
818b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
819b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    loc_eng_report_sv
820b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
821b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
822b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   Reports GPS satellite information to the Java layer.
823b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
824b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
825b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
826b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
827b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
828b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
829b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
830b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
831b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
832b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
833b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
834b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_report_sv (const rpc_loc_gnss_info_s_type *gnss_report_ptr)
835b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
836b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    GpsSvStatus     SvStatus;
837b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    int             num_svs_max, i;
838b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood	const rpc_loc_sv_info_s_type *sv_info_ptr;
839b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
8407c2f7b69f91e660f872d0ca15ab14b32d23e53d4Steve Block    ALOGV ("loc_eng_report_sv: valid_mask = 0x%x, num of sv = %d\n",
841b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            (uint32) gnss_report_ptr->valid_mask,
842b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            gnss_report_ptr->sv_count);
843b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
844b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    num_svs_max = 0;
845b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    memset (&SvStatus, 0, sizeof (GpsSvStatus));
846b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (gnss_report_ptr->valid_mask & RPC_LOC_GNSS_INFO_VALID_SV_COUNT)
847b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
848b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        num_svs_max = gnss_report_ptr->sv_count;
849b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        if (num_svs_max > GPS_MAX_SVS)
850b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        {
851b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            num_svs_max = GPS_MAX_SVS;
852b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        }
853b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
854b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
855b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (gnss_report_ptr->valid_mask & RPC_LOC_GNSS_INFO_VALID_SV_LIST)
856b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
857b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        SvStatus.num_svs = 0;
858b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
859b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        for (i = 0; i < num_svs_max; i++)
860b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        {
861b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            sv_info_ptr = &(gnss_report_ptr->sv_list.sv_list_val[i]);
862b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            if (sv_info_ptr->valid_mask & RPC_LOC_SV_INFO_VALID_SYSTEM)
863b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            {
864b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                if (sv_info_ptr->system == RPC_LOC_SV_SYSTEM_GPS)
865b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                {
866300b715b0dc36ae82b442a201432a5f2065e664bMike Lockwood                    SvStatus.sv_list[SvStatus.num_svs].size = sizeof(GpsSvStatus);
867b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                    SvStatus.sv_list[SvStatus.num_svs].prn = sv_info_ptr->prn;
868b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
869b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                    // We only have the data field to report gps eph and alm mask
870b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                    if ((sv_info_ptr->valid_mask & RPC_LOC_SV_INFO_VALID_HAS_EPH) &&
871b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                        (sv_info_ptr->has_eph == 1))
872b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                    {
873b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                        SvStatus.ephemeris_mask |= (1 << (sv_info_ptr->prn-1));
874b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                    }
875b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
876b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                    if ((sv_info_ptr->valid_mask & RPC_LOC_SV_INFO_VALID_HAS_ALM) &&
877b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                        (sv_info_ptr->has_alm == 1))
878b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                    {
879b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                        SvStatus.almanac_mask |= (1 << (sv_info_ptr->prn-1));
880b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                    }
881b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
882b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                    if ((sv_info_ptr->valid_mask & RPC_LOC_SV_INFO_VALID_PROCESS_STATUS) &&
883b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                        (sv_info_ptr->process_status == RPC_LOC_SV_STATUS_TRACK))
884b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                    {
885b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                        SvStatus.used_in_fix_mask |= (1 << (sv_info_ptr->prn-1));
886b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                    }
887b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                }
888b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                // SBAS: GPS RPN: 120-151,
889b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                // In exteneded measurement report, we follow nmea standard, which is from 33-64.
890b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                else if (sv_info_ptr->system == RPC_LOC_SV_SYSTEM_SBAS)
891b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                {
892b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                    SvStatus.sv_list[SvStatus.num_svs].prn = sv_info_ptr->prn + 33 - 120;
893b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                }
894b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                // Gloness: Slot id: 1-32
895b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                // In extended measurement report, we follow nmea standard, which is 65-96
896b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                else if (sv_info_ptr->system == RPC_LOC_SV_SYSTEM_GLONASS)
897b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                {
898b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                    SvStatus.sv_list[SvStatus.num_svs].prn = sv_info_ptr->prn + (65-1);
899b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                }
900b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                // Unsupported SV system
901b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                else
902b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                {
903b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                    continue;
904b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                }
905b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            }
906b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
907b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            if (sv_info_ptr->valid_mask & RPC_LOC_SV_INFO_VALID_SNR)
908b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            {
909b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                SvStatus.sv_list[SvStatus.num_svs].snr = sv_info_ptr->snr;
910b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            }
911b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
912b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            if (sv_info_ptr->valid_mask & RPC_LOC_SV_INFO_VALID_ELEVATION)
913b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            {
914b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                SvStatus.sv_list[SvStatus.num_svs].elevation = sv_info_ptr->elevation;
915b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            }
916b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
917b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            if (sv_info_ptr->valid_mask & RPC_LOC_SV_INFO_VALID_AZIMUTH)
918b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            {
919b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                SvStatus.sv_list[SvStatus.num_svs].azimuth = sv_info_ptr->azimuth;
920b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            }
921b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
922b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            SvStatus.num_svs++;
923b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        }
924b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
925b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
9267c2f7b69f91e660f872d0ca15ab14b32d23e53d4Steve Block    ALOGV ("num_svs = %d, eph mask = %d, alm mask = %d\n", SvStatus.num_svs, SvStatus.ephemeris_mask, SvStatus.almanac_mask );
927b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if ((SvStatus.num_svs != 0) && (loc_eng_data.sv_status_cb != NULL))
928b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
929b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        loc_eng_data.sv_status_cb(&SvStatus);
930b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
931b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
932b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
933b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
934b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    loc_eng_report_status
935b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
936b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
937b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   Reports GPS engine state to Java layer.
938b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
939b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
940b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
941b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
942b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
943b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
944b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
945b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
946b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
947b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
948b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
949b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_report_status (const rpc_loc_status_event_s_type *status_report_ptr)
950b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
951b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    GpsStatus status;
952b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
9537c2f7b69f91e660f872d0ca15ab14b32d23e53d4Steve Block    ALOGV ("loc_eng_report_status: event = %d\n", status_report_ptr->event);
954b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
955300b715b0dc36ae82b442a201432a5f2065e664bMike Lockwood    memset (&status, 0, sizeof(status));
956300b715b0dc36ae82b442a201432a5f2065e664bMike Lockwood    status.size = sizeof(status);
957b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    status.status = GPS_STATUS_NONE;
958b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (status_report_ptr->event == RPC_LOC_STATUS_EVENT_ENGINE_STATE)
959b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
960b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        if (status_report_ptr->payload.rpc_loc_status_event_payload_u_type_u.engine_state == RPC_LOC_ENGINE_STATE_ON)
961b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        {
96224d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood            // GPS_STATUS_SESSION_BEGIN implies GPS_STATUS_ENGINE_ON
963b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            status.status = GPS_STATUS_SESSION_BEGIN;
9645153d56321e20974d29c0649839d96e8351775b6Mike Lockwood            loc_eng_data.status_cb(&status);
965b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        }
966b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        else if (status_report_ptr->payload.rpc_loc_status_event_payload_u_type_u.engine_state == RPC_LOC_ENGINE_STATE_OFF)
967b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        {
96824d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood            // GPS_STATUS_SESSION_END implies GPS_STATUS_ENGINE_OFF
9696774225c7860662a75ff580ded6f23b50dd8f324Mike Lockwood            status.status = GPS_STATUS_ENGINE_OFF;
9705153d56321e20974d29c0649839d96e8351775b6Mike Lockwood            loc_eng_data.status_cb(&status);
971b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        }
972b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
973b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
9742c115de4096177477cf9495de9dd533267d0607eMike Lockwood    pthread_mutex_lock(&loc_eng_data.deferred_action_mutex);
975b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_data.engine_status = status.status;
976b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
977b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    // Wake up the thread for aiding data deletion.
9786774225c7860662a75ff580ded6f23b50dd8f324Mike Lockwood    if ((loc_eng_data.engine_status != GPS_STATUS_SESSION_BEGIN) &&
979b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        (loc_eng_data.aiding_data_for_deletion != 0))
980b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
9812c115de4096177477cf9495de9dd533267d0607eMike Lockwood        /* hold a wake lock while events are pending for deferred_action_thread */
9822c115de4096177477cf9495de9dd533267d0607eMike Lockwood        loc_eng_data.acquire_wakelock_cb();
9832c115de4096177477cf9495de9dd533267d0607eMike Lockwood        loc_eng_data.deferred_action_flags |= DEFERRED_ACTION_DELETE_AIDING;
984b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        pthread_cond_signal(&(loc_eng_data.deferred_action_cond));
985b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        // In case gps engine is ON, the assistance data will be deleted when the engine is OFF
986b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
987b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
9882c115de4096177477cf9495de9dd533267d0607eMike Lockwood    pthread_mutex_unlock(&loc_eng_data.deferred_action_mutex);
989b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
990b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
991b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_report_nmea (const rpc_loc_nmea_report_s_type *nmea_report_ptr)
992b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
993b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (loc_eng_data.nmea_cb != NULL)
994b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
995b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        struct timeval tv;
996b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
997b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        gettimeofday(&tv, (struct timezone *) NULL);
998b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        long long now = tv.tv_sec * 1000LL + tv.tv_usec / 1000;
999b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1000c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood#if (AMSS_VERSION==3200)
1001b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        loc_eng_data.nmea_cb(now, nmea_report_ptr->nmea_sentences.nmea_sentences_val,
1002b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                nmea_report_ptr->nmea_sentences.nmea_sentences_len);
1003c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood#else
1004c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood        loc_eng_data.nmea_cb(now, nmea_report_ptr->nmea_sentences, nmea_report_ptr->length);
1005c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood#endif
1006b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
1007b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
1008b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1009b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
1010b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    loc_eng_process_conn_request
1011b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1012b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
1013b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   Requests data connection to be brought up/tore down with the location server.
1014b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1015b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
1016b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
1017b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1018b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
1019b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
1020b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1021b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
1022b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
1023b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1024b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
1025b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_process_conn_request (const rpc_loc_server_request_s_type *server_request_ptr)
1026b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
1027b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD ("loc_event_cb: get loc event location server request, event = %d\n", server_request_ptr->event);
1028b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1029b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    // Signal DeferredActionThread to send the APN name
1030b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_mutex_lock(&loc_eng_data.deferred_action_mutex);
1031b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1032b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    // This implemenation is based on the fact that modem now at any time has only one data connection for AGPS at any given time
1033b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (server_request_ptr->event == RPC_LOC_SERVER_REQUEST_OPEN)
1034b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
1035b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        loc_eng_data.conn_handle = server_request_ptr->payload.rpc_loc_server_request_u_type_u.open_req.conn_handle;
1036b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        loc_eng_data.agps_status = GPS_REQUEST_AGPS_DATA_CONN;
1037bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood        loc_eng_data.agps_request_pending = true;
1038b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
1039b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    else
1040b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
1041b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        loc_eng_data.conn_handle = server_request_ptr->payload.rpc_loc_server_request_u_type_u.close_req.conn_handle;
1042b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        loc_eng_data.agps_status = GPS_RELEASE_AGPS_DATA_CONN;
1043bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood        loc_eng_data.agps_request_pending = false;
1044b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
1045b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
10462c115de4096177477cf9495de9dd533267d0607eMike Lockwood    /* hold a wake lock while events are pending for deferred_action_thread */
10472c115de4096177477cf9495de9dd533267d0607eMike Lockwood    loc_eng_data.acquire_wakelock_cb();
10482c115de4096177477cf9495de9dd533267d0607eMike Lockwood    loc_eng_data.deferred_action_flags |= DEFERRED_ACTION_AGPS_STATUS;
10492c115de4096177477cf9495de9dd533267d0607eMike Lockwood    pthread_cond_signal(&loc_eng_data.deferred_action_cond);
10502c115de4096177477cf9495de9dd533267d0607eMike Lockwood    pthread_mutex_unlock(&loc_eng_data.deferred_action_mutex);
1051b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
1052b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1053b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
1054b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    loc_eng_agps_init
1055b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1056b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
1057b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1058b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1059b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
1060b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   NONE
1061b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1062b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
1063b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   0
1064b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1065b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
1066b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
1067b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1068b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
1069b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_agps_init(AGpsCallbacks* callbacks)
1070b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
10717c2f7b69f91e660f872d0ca15ab14b32d23e53d4Steve Block    ALOGV("loc_eng_agps_init\n");
1072b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_data.agps_status_cb = callbacks->status_cb;
1073b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
1074b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1075b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int loc_eng_agps_data_conn_open(const char* apn)
1076b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
1077b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    int apn_len;
1078b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD("loc_eng_agps_data_conn_open: %s\n", apn);
1079b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1080b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_mutex_lock(&(loc_eng_data.deferred_action_mutex));
1081b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1082b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (apn != NULL)
1083b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
1084b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        apn_len = strlen (apn);
1085b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1086b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        if (apn_len >= sizeof(loc_eng_data.apn_name))
1087b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        {
1088b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block            ALOGD ("loc_eng_set_apn: error, apn name exceeds maximum lenght of 100 chars\n");
1089b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            apn_len = sizeof(loc_eng_data.apn_name) - 1;
1090b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        }
1091b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1092b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        memcpy (loc_eng_data.apn_name, apn, apn_len);
1093b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        loc_eng_data.apn_name[apn_len] = '\0';
1094b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
1095b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
10962c115de4096177477cf9495de9dd533267d0607eMike Lockwood    /* hold a wake lock while events are pending for deferred_action_thread */
10972c115de4096177477cf9495de9dd533267d0607eMike Lockwood    loc_eng_data.acquire_wakelock_cb();
10982c115de4096177477cf9495de9dd533267d0607eMike Lockwood    loc_eng_data.deferred_action_flags |= DEFERRED_ACTION_AGPS_DATA_SUCCESS;
1099b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_cond_signal(&(loc_eng_data.deferred_action_cond));
1100b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_mutex_unlock(&(loc_eng_data.deferred_action_mutex));
1101b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    return 0;
1102b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
1103b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1104b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int loc_eng_agps_data_conn_closed()
1105b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
1106b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD("loc_eng_agps_data_conn_closed\n");
1107b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_mutex_lock(&(loc_eng_data.deferred_action_mutex));
11082c115de4096177477cf9495de9dd533267d0607eMike Lockwood    /* hold a wake lock while events are pending for deferred_action_thread */
11092c115de4096177477cf9495de9dd533267d0607eMike Lockwood    loc_eng_data.acquire_wakelock_cb();
11102c115de4096177477cf9495de9dd533267d0607eMike Lockwood    loc_eng_data.deferred_action_flags |= DEFERRED_ACTION_AGPS_DATA_CLOSED;
1111b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_cond_signal(&(loc_eng_data.deferred_action_cond));
1112b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_mutex_unlock(&(loc_eng_data.deferred_action_mutex));
1113b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    return 0;
1114b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
1115b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1116b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int loc_eng_agps_data_conn_failed()
1117b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
1118b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD("loc_eng_agps_data_conn_failed\n");
1119b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1120b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_mutex_lock(&(loc_eng_data.deferred_action_mutex));
11212c115de4096177477cf9495de9dd533267d0607eMike Lockwood    /* hold a wake lock while events are pending for deferred_action_thread */
11222c115de4096177477cf9495de9dd533267d0607eMike Lockwood    loc_eng_data.acquire_wakelock_cb();
11232c115de4096177477cf9495de9dd533267d0607eMike Lockwood    loc_eng_data.deferred_action_flags |= DEFERRED_ACTION_AGPS_DATA_FAILED;
1124b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_cond_signal(&(loc_eng_data.deferred_action_cond));
1125b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    pthread_mutex_unlock(&(loc_eng_data.deferred_action_mutex));
1126b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    return 0;
1127b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
1128b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1129b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int set_agps_server()
1130b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
1131b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_ioctl_data_u_type       ioctl_data;
1132b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_server_info_s_type      *server_info_ptr;
1133b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    boolean                         ret_val;
1134b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    uint16                          port_temp;
1135b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    unsigned char                   *b_ptr;
1136b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1137b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (loc_eng_data.agps_server_host[0] == 0 || loc_eng_data.agps_server_port == 0)
1138b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        return -1;
1139b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1140b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (loc_eng_data.agps_server_address == 0) {
1141b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        struct hostent* he = gethostbyname(loc_eng_data.agps_server_host);
1142b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        if (he)
1143b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            loc_eng_data.agps_server_address = *(uint32_t *)he->h_addr_list[0];
1144b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
1145b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (loc_eng_data.agps_server_address == 0)
1146b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        return -1;
1147b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1148b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    b_ptr = (unsigned char*) (&loc_eng_data.agps_server_address);
1149c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood
1150c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood
1151c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood    server_info_ptr = &(ioctl_data.rpc_loc_ioctl_data_u_type_u.server_addr);
1152c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood    ioctl_data.disc = RPC_LOC_IOCTL_SET_UMTS_SLP_SERVER_ADDR;
1153c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood    server_info_ptr->addr_type = RPC_LOC_SERVER_ADDR_URL;
1154c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood    server_info_ptr->addr_info.disc =  RPC_LOC_SERVER_ADDR_URL;
1155c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood
1156c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood#if (AMSS_VERSION==3200)
1157c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood    char   url[24];
1158b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    memset(url, 0, sizeof(url));
1159b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    snprintf(url, sizeof(url) - 1, "%d.%d.%d.%d:%d",
1160b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            (*(b_ptr + 0)  & 0x000000ff), (*(b_ptr+1) & 0x000000ff),
1161b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            (*(b_ptr + 2)  & 0x000000ff), (*(b_ptr+3) & 0x000000ff),
1162b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            (loc_eng_data.agps_server_port & (0x0000ffff)));
1163b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1164b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    server_info_ptr->addr_info.rpc_loc_server_addr_u_type_u.url.addr.addr_val = url;
1165c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood    server_info_ptr->addr_info.rpc_loc_server_addr_u_type_u.url.addr.addr_len = strlen(url);
1166b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD ("set_agps_server, addr = %s\n", server_info_ptr->addr_info.rpc_loc_server_addr_u_type_u.url.addr.addr_val);
1167c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood#else
1168c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood    char* buf = server_info_ptr->addr_info.rpc_loc_server_addr_u_type_u.url.addr;
1169c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood    int buf_len = sizeof(server_info_ptr->addr_info.rpc_loc_server_addr_u_type_u.url.addr);
1170c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood    memset(buf, 0, buf_len);
1171c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood    snprintf(buf, buf_len - 1, "%d.%d.%d.%d:%d",
1172c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood            (*(b_ptr + 0)  & 0x000000ff), (*(b_ptr+1) & 0x000000ff),
1173c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood            (*(b_ptr + 2)  & 0x000000ff), (*(b_ptr+3) & 0x000000ff),
1174c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood            (loc_eng_data.agps_server_port & (0x0000ffff)));
1175c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood
1176c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood    server_info_ptr->addr_info.rpc_loc_server_addr_u_type_u.url.length = buf_len;
1177b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD ("set_agps_server, addr = %s\n", buf);
1178c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood#endif
1179b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1180b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    ret_val = loc_eng_ioctl (loc_eng_data.client_handle,
1181b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                            RPC_LOC_IOCTL_SET_UMTS_SLP_SERVER_ADDR,
1182b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                            &ioctl_data,
1183b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                            LOC_IOCTL_DEFAULT_TIMEOUT,
1184b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                            NULL /* No output information is expected*/);
1185b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1186b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (ret_val != TRUE)
1187b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
1188b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block        ALOGD ("set_agps_server failed\n");
1189b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        return -1;
1190b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
1191b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    else
1192b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
11937c2f7b69f91e660f872d0ca15ab14b32d23e53d4Steve Block        ALOGV ("set_agps_server successful\n");
1194b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        return 0;
1195b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
1196b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
1197b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1198b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic int loc_eng_agps_set_server(AGpsType type, const char* hostname, int port)
1199b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
1200b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD ("loc_eng_set_default_agps_server, type = %d, hostname = %s, port = %d\n", type, hostname, port);
1201b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1202b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    if (type != AGPS_TYPE_SUPL)
1203b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        return -1;
1204b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1205b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    strncpy(loc_eng_data.agps_server_host, hostname, sizeof(loc_eng_data.agps_server_host) - 1);
1206b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_data.agps_server_port = port;
1207b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    return 0;
1208b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
1209b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1210b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
1211b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    loc_eng_delete_aiding_data_deferred_action
1212b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1213b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
1214b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   This is used to remove the aiding data when GPS engine is off.
1215b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1216b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
1217b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   Assumes the aiding data type specified in GpsAidingData matches with
1218b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   LOC API specification.
1219b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1220b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
1221b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   RPC_LOC_API_SUCCESS
1222b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1223b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
1224b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
1225b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1226b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
1227b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwoodstatic void loc_eng_delete_aiding_data_deferred_action (void)
1228b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
1229b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    // Currently, we only support deletion of all aiding data,
1230b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    // since the Android defined aiding data mask matches with modem,
1231b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    // so just pass them down without any translation
1232b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_ioctl_data_u_type          ioctl_data;
1233b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_assist_data_delete_s_type  *assist_data_ptr;
1234b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    boolean                             ret_val;
1235b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1236b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    ioctl_data.disc = RPC_LOC_IOCTL_DELETE_ASSIST_DATA;
1237b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    assist_data_ptr = &(ioctl_data.rpc_loc_ioctl_data_u_type_u.assist_data_delete);
1238a4977cd631b6f4bc1ee74d0ed60977cb06d12e6fMike Lockwood    assist_data_ptr->type = loc_eng_data.aiding_data_for_deletion;
1239a4977cd631b6f4bc1ee74d0ed60977cb06d12e6fMike Lockwood    loc_eng_data.aiding_data_for_deletion = 0;
1240a4977cd631b6f4bc1ee74d0ed60977cb06d12e6fMike Lockwood
1241b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    memset (&(assist_data_ptr->reserved), 0, sizeof (assist_data_ptr->reserved));
1242b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1243b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    ret_val = loc_eng_ioctl (loc_eng_data.client_handle,
1244b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                             RPC_LOC_IOCTL_DELETE_ASSIST_DATA ,
1245b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                             &ioctl_data,
1246b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                             LOC_IOCTL_DEFAULT_TIMEOUT,
1247b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                             NULL);
1248b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1249b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD("loc_eng_ioctl for aiding data deletion returned %d, 1 for success\n", ret_val);
1250b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
1251b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1252b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
1253b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION    loc_eng_process_atl_deferred_action
1254b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1255b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
1256b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   This is used to inform the location engine of the processing status for
1257b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   data connection open/close request.
1258b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1259b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
1260b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   None
1261b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1262b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
1263b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   RPC_LOC_API_SUCCESS
1264b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1265b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
1266b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
1267b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1268b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
12692c115de4096177477cf9495de9dd533267d0607eMike Lockwoodstatic void loc_eng_process_atl_deferred_action (int flags)
1270b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
1271b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_server_open_status_s_type  *conn_open_status_ptr;
1272b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_server_close_status_s_type *conn_close_status_ptr;
1273b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    rpc_loc_ioctl_data_u_type           ioctl_data;
1274b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    boolean                             ret_val;
1275b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    int                                 agps_status = -1;
1276b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
12777c2f7b69f91e660f872d0ca15ab14b32d23e53d4Steve Block    ALOGV("loc_eng_process_atl_deferred_action, agps_status = %d\n", loc_eng_data.agps_status);
1278b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1279b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    memset (&ioctl_data, 0, sizeof (rpc_loc_ioctl_data_u_type));
1280b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
12812c115de4096177477cf9495de9dd533267d0607eMike Lockwood    if (flags & DEFERRED_ACTION_AGPS_DATA_CLOSED)
1282b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
1283b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        ioctl_data.disc = RPC_LOC_IOCTL_INFORM_SERVER_CLOSE_STATUS;
1284b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        conn_close_status_ptr = &(ioctl_data.rpc_loc_ioctl_data_u_type_u.conn_close_status);
1285b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        conn_close_status_ptr->conn_handle = loc_eng_data.conn_handle;
1286b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        conn_close_status_ptr->close_status = RPC_LOC_SERVER_CLOSE_SUCCESS;
1287b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
1288b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    else
1289b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
1290b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        ioctl_data.disc = RPC_LOC_IOCTL_INFORM_SERVER_OPEN_STATUS;
1291b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        conn_open_status_ptr = &ioctl_data.rpc_loc_ioctl_data_u_type_u.conn_open_status;
1292b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        conn_open_status_ptr->conn_handle = loc_eng_data.conn_handle;
12932c115de4096177477cf9495de9dd533267d0607eMike Lockwood        if (flags & DEFERRED_ACTION_AGPS_DATA_SUCCESS)
1294b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        {
1295b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            conn_open_status_ptr->open_status = RPC_LOC_SERVER_OPEN_SUCCESS;
1296b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            // Both buffer are of the same maximum size, and the source is null terminated
1297b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            // strcpy (&(ioctl_data.rpc_loc_ioctl_data_u_type_u.conn_open_status.apn_name), &(loc_eng_data.apn_name));
1298c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood#if (AMSS_VERSION==3200)
1299b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            conn_open_status_ptr->apn_name = loc_eng_data.apn_name;
1300c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood#else
1301c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood            memset(conn_open_status_ptr->apn_name, 0, sizeof(conn_open_status_ptr->apn_name));
1302c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood            strncpy(conn_open_status_ptr->apn_name, loc_eng_data.apn_name,
1303c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood                    sizeof(conn_open_status_ptr->apn_name) - 1);
1304c6419effb6a1da6dbc86432e66d64b5bfcacbad6Mike Lockwood#endif
1305b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            // Delay this so that PDSM ATL module will behave properly
1306b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            sleep (1);
1307b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block            ALOGD("loc_eng_ioctl for ATL with apn_name = %s\n", conn_open_status_ptr->apn_name);
1308b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        }
1309b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        else // data_connection_failed
1310b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        {
1311b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            conn_open_status_ptr->open_status = RPC_LOC_SERVER_OPEN_FAIL;
1312b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        }
1313b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        // Delay this so that PDSM ATL module will behave properly
1314b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        sleep (1);
1315b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
1316b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1317b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    ret_val = loc_eng_ioctl (loc_eng_data.client_handle,
1318b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                             ioctl_data.disc,
1319b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                             &ioctl_data,
1320b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                             LOC_IOCTL_DEFAULT_TIMEOUT,
1321b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood                             NULL);
1322b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1323b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD("loc_eng_ioctl for ATL returned %d (1 for success)\n", ret_val);
1324b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
1325b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1326b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood/*===========================================================================
132724d7556a88537061aaef7c58588813c0faf98c21Mike LockwoodFUNCTION    loc_eng_process_loc_event
132824d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
132924d7556a88537061aaef7c58588813c0faf98c21Mike LockwoodDESCRIPTION
133024d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood   This is used to process events received from the location engine.
133124d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
133224d7556a88537061aaef7c58588813c0faf98c21Mike LockwoodDEPENDENCIES
133324d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood   None
133424d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
133524d7556a88537061aaef7c58588813c0faf98c21Mike LockwoodRETURN VALUE
133624d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood   N/A
133724d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
133824d7556a88537061aaef7c58588813c0faf98c21Mike LockwoodSIDE EFFECTS
133924d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood   N/A
134024d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
134124d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood===========================================================================*/
134224d7556a88537061aaef7c58588813c0faf98c21Mike Lockwoodstatic void loc_eng_process_loc_event (rpc_loc_event_mask_type loc_event,
134324d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        rpc_loc_event_payload_u_type* loc_event_payload)
134424d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood{
134524d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    if (loc_event & RPC_LOC_EVENT_PARSED_POSITION_REPORT)
134624d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    {
134724d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        loc_eng_report_position (&(loc_event_payload->rpc_loc_event_payload_u_type_u.parsed_location_report));
134824d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    }
134924d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
135024d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    if (loc_event & RPC_LOC_EVENT_SATELLITE_REPORT)
135124d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    {
135224d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        loc_eng_report_sv (&(loc_event_payload->rpc_loc_event_payload_u_type_u.gnss_report));
135324d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    }
135424d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
135524d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    if (loc_event & RPC_LOC_EVENT_STATUS_REPORT)
135624d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    {
135724d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        loc_eng_report_status (&(loc_event_payload->rpc_loc_event_payload_u_type_u.status_report));
135824d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    }
135924d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
136024d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    if (loc_event & RPC_LOC_EVENT_NMEA_POSITION_REPORT)
136124d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    {
136224d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        loc_eng_report_nmea (&(loc_event_payload->rpc_loc_event_payload_u_type_u.nmea_report));
136324d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    }
136424d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
136524d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    // Android XTRA interface supports only XTRA download
136624d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    if (loc_event & RPC_LOC_EVENT_ASSISTANCE_DATA_REQUEST)
136724d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    {
136824d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        if (loc_event_payload->rpc_loc_event_payload_u_type_u.assist_data_request.event ==
136924d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood                RPC_LOC_ASSIST_DATA_PREDICTED_ORBITS_REQ)
137024d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        {
1371b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block            ALOGD ("loc_event_cb: xtra download requst");
137224d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
137324d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood            // Call Registered callback
137424d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood            if (loc_eng_data.xtra_module_data.download_request_cb != NULL)
137524d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood            {
137624d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood                loc_eng_data.xtra_module_data.download_request_cb ();
137724d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood            }
137824d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        }
137924d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    }
138024d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
138124d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    if (loc_event & RPC_LOC_EVENT_IOCTL_REPORT)
138224d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    {
138324d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        // Process the received RPC_LOC_EVENT_IOCTL_REPORT
138424d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        (void) loc_eng_ioctl_process_cb (loc_eng_data.client_handle,
138524d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood                                &(loc_event_payload->rpc_loc_event_payload_u_type_u.ioctl_report));
138624d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    }
138724d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
138824d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    if (loc_event & RPC_LOC_EVENT_LOCATION_SERVER_REQUEST)
138924d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    {
139024d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        loc_eng_process_conn_request (&(loc_event_payload->rpc_loc_event_payload_u_type_u.loc_server_request));
139124d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    }
139224d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
139324d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    loc_eng_ni_callback(loc_event, loc_event_payload);
139424d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
139524d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood#if DEBUG_MOCK_NI == 1
139624d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    // DEBUG only
139724d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    if ((loc_event & RPC_LOC_EVENT_STATUS_REPORT) &&
139824d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        loc_event_payload->rpc_loc_event_payload_u_type_u.status_report.
139924d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        payload.rpc_loc_status_event_payload_u_type_u.engine_state
140024d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        == RPC_LOC_ENGINE_STATE_OFF)
140124d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    {
140224d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        // Mock an NI request
140324d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        pthread_t th;
140424d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        pthread_create (&th, NULL, mock_ni, (void*) client_handle);
140524d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    }
140624d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood#endif /* DEBUG_MOCK_NI == 1 */
140724d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood}
140824d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
140924d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood/*===========================================================================
1410b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodFUNCTION loc_eng_process_deferred_action
1411b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1412b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDESCRIPTION
1413b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   Main routine for the thread to execute certain commands
1414b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   that are not safe to be done from within an RPC callback.
1415b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1416b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodDEPENDENCIES
1417b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   None
1418b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1419b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodRETURN VALUE
1420b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   None
1421b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1422b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike LockwoodSIDE EFFECTS
1423b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood   N/A
1424b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1425b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood===========================================================================*/
14265153d56321e20974d29c0649839d96e8351775b6Mike Lockwoodstatic void loc_eng_process_deferred_action (void* arg)
1427b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood{
142824d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    AGpsStatus      status;
1429300b715b0dc36ae82b442a201432a5f2065e664bMike Lockwood    status.size = sizeof(status);
143024d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood    status.type = AGPS_TYPE_SUPL;
143124d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
1432b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD("loc_eng_process_deferred_action started\n");
1433b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1434237c22f03d1800ec68b47e7df072efeab6c04199Mike Lockwood    // make sure we do not run in background scheduling group
1435237c22f03d1800ec68b47e7df072efeab6c04199Mike Lockwood    set_sched_policy(gettid(), SP_FOREGROUND);
1436b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1437237c22f03d1800ec68b47e7df072efeab6c04199Mike Lockwood    // disable the GPS lock
1438b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD("Setting GPS privacy lock to RPC_LOC_LOCK_NONE\n");
1439b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    loc_eng_set_gps_lock(RPC_LOC_LOCK_NONE);
1440b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
14412c115de4096177477cf9495de9dd533267d0607eMike Lockwood    while (1)
1442b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    {
144324d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        GpsAidingData   aiding_data_for_deletion;
144424d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        GpsStatusValue  engine_status;
144524d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
144624d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        rpc_loc_event_mask_type         loc_event;
144724d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        rpc_loc_event_payload_u_type    loc_event_payload;
144824d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
1449b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        // Wait until we are signalled to do a deferred action, or exit
1450b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        pthread_mutex_lock(&loc_eng_data.deferred_action_mutex);
14512c115de4096177477cf9495de9dd533267d0607eMike Lockwood
1452a162c4351926285892214b0726aaf07f0631dc72Mike Lockwood        // If we have an event we should process it immediately,
1453a162c4351926285892214b0726aaf07f0631dc72Mike Lockwood        // otherwise wait until we are signalled
1454a162c4351926285892214b0726aaf07f0631dc72Mike Lockwood        if (loc_eng_data.deferred_action_flags == 0) {
1455a162c4351926285892214b0726aaf07f0631dc72Mike Lockwood            // do not hold a wake lock while waiting for an event...
14562c115de4096177477cf9495de9dd533267d0607eMike Lockwood            loc_eng_data.release_wakelock_cb();
1457a162c4351926285892214b0726aaf07f0631dc72Mike Lockwood            pthread_cond_wait(&loc_eng_data.deferred_action_cond,
1458a162c4351926285892214b0726aaf07f0631dc72Mike Lockwood                                &loc_eng_data.deferred_action_mutex);
1459a162c4351926285892214b0726aaf07f0631dc72Mike Lockwood            // but after we are signalled reacquire the wake lock
1460a162c4351926285892214b0726aaf07f0631dc72Mike Lockwood            // until we are done processing the event.
1461a162c4351926285892214b0726aaf07f0631dc72Mike Lockwood            loc_eng_data.acquire_wakelock_cb();
1462a162c4351926285892214b0726aaf07f0631dc72Mike Lockwood        }
1463b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
14642c115de4096177477cf9495de9dd533267d0607eMike Lockwood        if (loc_eng_data.deferred_action_flags & DEFERRED_ACTION_QUIT)
1465b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        {
146624d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood            pthread_mutex_unlock(&loc_eng_data.deferred_action_mutex);
1467b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            break;
1468b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        }
1469b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
147024d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        // copy anything we need before releasing the mutex
147124d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        loc_event = loc_eng_data.loc_event;
147224d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        if (loc_event != 0) {
147324d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood            memcpy(&loc_event_payload, &loc_eng_data.loc_event_payload, sizeof(loc_event_payload));
147424d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood            loc_eng_data.loc_event = 0;
147524d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        }
147624d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
14772c115de4096177477cf9495de9dd533267d0607eMike Lockwood        int flags = loc_eng_data.deferred_action_flags;
14782c115de4096177477cf9495de9dd533267d0607eMike Lockwood        loc_eng_data.deferred_action_flags = 0;
147924d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        engine_status = loc_eng_data.agps_status;
148024d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        aiding_data_for_deletion = loc_eng_data.aiding_data_for_deletion;
148124d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        status.status = loc_eng_data.agps_status;
148224d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        loc_eng_data.agps_status = 0;
148324d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
148424d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        // perform all actions after releasing the mutex to avoid blocking RPCs from the ARM9
148524d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        pthread_mutex_unlock(&(loc_eng_data.deferred_action_mutex));
148624d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood
148724d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        if (loc_event != 0) {
148824d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood            loc_eng_process_loc_event(loc_event, &loc_event_payload);
148924d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        }
1490b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1491b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        // send_delete_aiding_data must be done when GPS engine is off
149224d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        if ((engine_status != GPS_STATUS_SESSION_BEGIN) && (aiding_data_for_deletion != 0))
1493b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        {
1494b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            loc_eng_delete_aiding_data_deferred_action ();
1495b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        }
1496b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
14972c115de4096177477cf9495de9dd533267d0607eMike Lockwood        if (flags & (DEFERRED_ACTION_AGPS_DATA_SUCCESS |
14982c115de4096177477cf9495de9dd533267d0607eMike Lockwood                     DEFERRED_ACTION_AGPS_DATA_CLOSED |
14992c115de4096177477cf9495de9dd533267d0607eMike Lockwood                     DEFERRED_ACTION_AGPS_DATA_FAILED))
1500b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        {
15012c115de4096177477cf9495de9dd533267d0607eMike Lockwood            loc_eng_process_atl_deferred_action(flags);
1502bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood
1503bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood            pthread_mutex_lock(&(loc_eng_data.deferred_stop_mutex));
1504bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood            // work around problem with loc_eng_stop when AGPS requests are pending
1505bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood            // we defer stopping the engine until the AGPS request is done
1506bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood            loc_eng_data.agps_request_pending = false;
1507bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood            if (loc_eng_data.stop_request_pending)
1508bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood            {
1509b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block                ALOGD ("handling deferred stop\n");
15100b602e40b8dc1f134ed94f7a781b1be2b88e760dMike Lockwood                if (loc_stop_fix(loc_eng_data.client_handle) != RPC_LOC_API_SUCCESS)
15110b602e40b8dc1f134ed94f7a781b1be2b88e760dMike Lockwood                {
1512b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block                    ALOGD ("loc_stop_fix failed!\n");
15130b602e40b8dc1f134ed94f7a781b1be2b88e760dMike Lockwood                }
1514bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood            }
1515bd3166c592f9f578040bb94bd7c68e4f01a0038dMike Lockwood            pthread_mutex_unlock(&(loc_eng_data.deferred_stop_mutex));
1516b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        }
1517b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
151824d7556a88537061aaef7c58588813c0faf98c21Mike Lockwood        if (status.status != 0 && loc_eng_data.agps_status_cb) {
1519b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood            loc_eng_data.agps_status_cb(&status);
1520b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood        }
1521b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood    }
1522b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood
1523237c22f03d1800ec68b47e7df072efeab6c04199Mike Lockwood    // reenable the GPS lock
1524b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD("Setting GPS privacy lock to RPC_LOC_LOCK_ALL\n");
1525237c22f03d1800ec68b47e7df072efeab6c04199Mike Lockwood    loc_eng_set_gps_lock(RPC_LOC_LOCK_ALL);
1526237c22f03d1800ec68b47e7df072efeab6c04199Mike Lockwood
1527b80e8ff4f8022a07a34ed2bc9395c73bbcc82759Steve Block    ALOGD("loc_eng_process_deferred_action thread exiting\n");
15282c115de4096177477cf9495de9dd533267d0607eMike Lockwood    loc_eng_data.release_wakelock_cb();
15295153d56321e20974d29c0649839d96e8351775b6Mike Lockwood
15305153d56321e20974d29c0649839d96e8351775b6Mike Lockwood    loc_eng_data.deferred_action_thread = 0;
1531b4c4f2016ae47fdd00e26b57d1766cf180d6f51aMike Lockwood}
1532300b715b0dc36ae82b442a201432a5f2065e664bMike Lockwood
1533300b715b0dc36ae82b442a201432a5f2065e664bMike Lockwood// for gps.c
1534300b715b0dc36ae82b442a201432a5f2065e664bMike Lockwoodextern "C" const GpsInterface* get_gps_interface()
1535300b715b0dc36ae82b442a201432a5f2065e664bMike Lockwood{
1536300b715b0dc36ae82b442a201432a5f2065e664bMike Lockwood    return &sLocEngInterface;
1537300b715b0dc36ae82b442a201432a5f2065e664bMike Lockwood}
1538