summaryrefslogtreecommitdiff
path: root/loc_api/libloc_api/loc_eng_ioctl.cpp
blob: dc7b2c11eaae0223916212517992452be0b337de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* Copyright (c) 2009,2011 The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * 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.
 *     * Neither the name of The Linux Foundation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER 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.
 *
 */

#define LOG_NDDEBUG 0

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <ctype.h>
#include <math.h>
#include <pthread.h>

#include <rpc/rpc.h>
#include <loc_api_rpc_glue.h>

#include <hardware/gps.h>

#include <loc_eng.h>

#define LOG_TAG "lib_locapi"
#include <utils/Log.h>

// comment this out to enable logging
// #undef LOGD
// #define LOGD(...) {}

// Function declarations
static boolean loc_eng_ioctl_setup_cb(
    rpc_loc_client_handle_type    handle,
    rpc_loc_ioctl_e_type          ioctl_type
);

static boolean loc_eng_ioctl_wait_cb(
    int                            timeout_msec,  // Timeout in this number of msec
    rpc_loc_ioctl_callback_s_type *cb_data_ptr    // Output parameter for IOCTL calls
);

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

FUNCTION    loc_eng_ioctl

DESCRIPTION
   This function calls loc_ioctl and waits for the callback result before
   returning back to the user.

DEPENDENCIES
   N/A

RETURN VALUE
   TRUE                 if successful
   FALSE                if failed

SIDE EFFECTS
   N/A

===========================================================================*/
boolean loc_eng_ioctl(
    rpc_loc_client_handle_type           handle,
    rpc_loc_ioctl_e_type                 ioctl_type,
    rpc_loc_ioctl_data_u_type*           ioctl_data_ptr,
    uint32                               timeout_msec,
    rpc_loc_ioctl_callback_s_type       *cb_data_ptr
    )
{
    boolean                    ret_val;
    int                        rpc_ret_val;
    loc_eng_ioctl_data_s_type *ioctl_cb_data_ptr;

    LOGV ("loc_eng_ioctl: client = %d, ioctl_type = %d, cb_data =0x%x\n", (int32) handle, ioctl_type, (uint32) cb_data_ptr);

    ioctl_cb_data_ptr = &(loc_eng_data.ioctl_data);
    // Select the callback we are waiting for
    ret_val = loc_eng_ioctl_setup_cb (handle, ioctl_type);

    if (ret_val == TRUE) 
    {
        rpc_ret_val =  loc_ioctl (handle,
                                    ioctl_type,
                                    ioctl_data_ptr);

        LOGV ("loc_eng_ioctl: loc_ioctl returned %d \n", rpc_ret_val);

        if (rpc_ret_val == RPC_LOC_API_SUCCESS)
        {
            // Wait for the callback of loc_ioctl
            ret_val = loc_eng_ioctl_wait_cb (timeout_msec, cb_data_ptr);
        }
        else
        {
            ret_val = FALSE;
        }
    }

    // Reset the state when we are done
    pthread_mutex_lock(&ioctl_cb_data_ptr->cb_data_mutex);
    ioctl_cb_data_ptr->cb_is_selected = FALSE;
    ioctl_cb_data_ptr->cb_is_waiting  = FALSE;
    ioctl_cb_data_ptr->cb_has_arrived = FALSE;
    pthread_mutex_unlock(&ioctl_cb_data_ptr->cb_data_mutex);

    return ret_val;
}


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

FUNCTION    loc_eng_ioctl_setup_cb

DESCRIPTION
   Selects which callback is going to be waited for

DEPENDENCIES
   N/A

RETURN VALUE
   TRUE                 if successful
   FALSE                if failed

SIDE EFFECTS
   N/A

===========================================================================*/
static boolean loc_eng_ioctl_setup_cb(
    rpc_loc_client_handle_type    handle,
    rpc_loc_ioctl_e_type          ioctl_type
    )
{
    boolean ret_val;
    loc_eng_ioctl_data_s_type *ioctl_cb_data_ptr;

    ioctl_cb_data_ptr = &(loc_eng_data.ioctl_data);

    pthread_mutex_lock(&ioctl_cb_data_ptr->cb_data_mutex);
    if (ioctl_cb_data_ptr->cb_is_selected == TRUE)
    {
        LOGD ("loc_eng_ioctl_setup_cb: ERROR, another ioctl in progress \n");
        ret_val = FALSE;
    }
    else
    {
        ioctl_cb_data_ptr->cb_is_selected = TRUE;
        ioctl_cb_data_ptr->cb_is_waiting  = FALSE;
        ioctl_cb_data_ptr->cb_has_arrived = FALSE;
        ioctl_cb_data_ptr->client_handle  = handle;
        ioctl_cb_data_ptr->ioctl_type     = ioctl_type;
        memset (&(ioctl_cb_data_ptr->cb_payload), 0, sizeof (rpc_loc_ioctl_callback_s_type));
        ret_val = TRUE;
    }
    pthread_mutex_unlock(&ioctl_cb_data_ptr->cb_data_mutex);

    return ret_val;
}

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

FUNCTION    loc_eng_ioctl_wait_cb

DESCRIPTION
   Waits for a selected callback. The wait expires in timeout_msec.

   If the function is called before an existing wait has finished, it will
   immediately return EBUSY.

DEPENDENCIES
   N/A

RETURN VALUE
   TRUE                 if successful
   FALSE                if failed

SIDE EFFECTS
   N/A

===========================================================================*/
boolean loc_eng_ioctl_wait_cb(
    int                            timeout_msec,  // Timeout in this number of msec
    rpc_loc_ioctl_callback_s_type *cb_data_ptr
    )
{
    boolean ret_val = FALSE; // the return value of this function
    int rc;                  // return code from pthread calls

    struct timeval present_time;
    struct timespec expire_time;
    loc_eng_ioctl_data_s_type *ioctl_cb_data_ptr;

    ioctl_cb_data_ptr = &(loc_eng_data.ioctl_data);

    pthread_mutex_lock(&ioctl_cb_data_ptr->cb_data_mutex);

    do {
        if (ioctl_cb_data_ptr->cb_is_selected == FALSE)
        {
            LOGD ("loc_eng_ioctl_wait_cb: ERROR called when cb_is_waiting is set to FALSE \n");
            ret_val = FALSE;
            break;
        }

        // Calculate absolute expire time
        gettimeofday(&present_time, NULL);
        expire_time.tv_sec  = present_time.tv_sec;
        expire_time.tv_sec  +=  timeout_msec / 1000;
        if ((present_time.tv_usec + timeout_msec) >= 1000)
        {
            expire_time.tv_sec += 1;
        }
        expire_time.tv_nsec = (present_time.tv_usec + timeout_msec) % 1000 * 1000;

        // Special case where callback is issued before loc_ioctl ever returns
        if (ioctl_cb_data_ptr->cb_has_arrived == TRUE)
        {
            LOGD ("loc_eng_ioctl_wait_cb: cb has arrived without waiting \n");
            ret_val = TRUE;
            break;
        }
   
        ioctl_cb_data_ptr->cb_is_waiting = TRUE;
        // Wait for the callback until timeout expires
        rc = pthread_cond_timedwait(&ioctl_cb_data_ptr->cb_arrived_cond,
                                    &ioctl_cb_data_ptr->cb_data_mutex,
                                    &expire_time);

        if (rc == 0)
        {
            ret_val = TRUE;
        }
        else
        {
            ret_val = FALSE;
        }

        LOGV ("loc_eng_ioctl_wait_cb: pthread_cond_timedwait returned %d\n", rc);

    } while (0);

    // Process the ioctl callback data when IOCTL is successful
    if (ret_val == TRUE)
    {
        ioctl_cb_data_ptr = &(loc_eng_data.ioctl_data);
        if (ioctl_cb_data_ptr->cb_payload.status == RPC_LOC_API_SUCCESS)
        {
            ret_val = TRUE;
            if (cb_data_ptr != NULL)
            {
                memcpy (cb_data_ptr,
                        &(ioctl_cb_data_ptr->cb_payload),
                        sizeof (rpc_loc_ioctl_callback_s_type));
            }
        }
        else
        {
            ret_val = FALSE;
        }
    }

    pthread_mutex_unlock(&ioctl_cb_data_ptr->cb_data_mutex);

    LOGV ("loc_eng_ioctl_wait_cb: returned %d\n", ret_val);
    return ret_val;
}

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

FUNCTION    loc_eng_ioctl_process_cb

DESCRIPTION
   This function process the IOCTL callback, parameter specifies the client 
   that receives the IOCTL callback.

DEPENDENCIES
   N/A

RETURN VALUE
   TRUE                 if successful
   FALSE                if failed

SIDE EFFECTS
   N/A

===========================================================================*/
boolean loc_eng_ioctl_process_cb (
    rpc_loc_client_handle_type           client_handle,
    const rpc_loc_ioctl_callback_s_type *cb_data_ptr
    )
{
    boolean ret_val = FALSE; // the return value of this function
    loc_eng_ioctl_data_s_type *ioctl_cb_data_ptr;
    ioctl_cb_data_ptr = &(loc_eng_data.ioctl_data);

    pthread_mutex_lock(&ioctl_cb_data_ptr->cb_data_mutex);
    if (client_handle != ioctl_cb_data_ptr->client_handle)
    {
        LOGD ("loc_eng_ioctl_process_cb: client handle mismatch, received = %d, expected = %d \n",
                (int32) client_handle, (int32) ioctl_cb_data_ptr->client_handle);
        ret_val = FALSE;
    }
    else if (cb_data_ptr->type != ioctl_cb_data_ptr->ioctl_type)
    {
        LOGD ("loc_eng_ioctl_process_cb: ioctl type mismatch, received = %d, expected = %d \n",
                 cb_data_ptr->type, ioctl_cb_data_ptr->ioctl_type);
        ret_val = FALSE;
    }
    else // both matches
    {
        memcpy (&(ioctl_cb_data_ptr->cb_payload),
                cb_data_ptr,
                sizeof (rpc_loc_ioctl_callback_s_type));

        ioctl_cb_data_ptr->cb_has_arrived = TRUE;

        LOGV ("loc_eng_ioctl_process_cb: callback arrived for client = %d, ioctl = %d, status = %d\n",
                (int32) ioctl_cb_data_ptr->client_handle, ioctl_cb_data_ptr->ioctl_type,
                (int32) ioctl_cb_data_ptr->cb_payload.status);

        ret_val = TRUE;
    }

    pthread_mutex_unlock(&ioctl_cb_data_ptr->cb_data_mutex);

    // Signal the waiting thread that callback has arrived
    if (ret_val == TRUE)
    {
        pthread_cond_signal (&ioctl_cb_data_ptr->cb_arrived_cond);
    }

    return ret_val;
}