-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgps_library.c
1348 lines (1134 loc) · 26.8 KB
/
gps_library.c
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* This file is part of gps-proxy.
*
* Copyright (C) 2012 Alexander Tarasikov <[email protected]>
*
* gps-proxy is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gps-proxy is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gps-proxy. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <cutils/sockets.h>
/* ANDROID-specific headers */
#define LOG_TAG "[GPS-PROXY-LIB] "
#include <utils/Log.h>
/* ANDROID libhardware headers */
#include <hardware/gps.h>
#include <stc_rpc.h>
#include <stc_log.h>
#include "gps-rpc.h"
/******************************************************************************
* Global Library State
*****************************************************************************/
static int client_fd = -1;
static pthread_mutex_t gps_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t gps_cond = PTHREAD_COND_INITIALIZER;
static GpsXtraCallbacks *xtraCallbacks = NULL;
static AGpsCallbacks *aGpsCallbacks = NULL;
static GpsCallbacks *gpsCallbacks = NULL;
static GpsNiCallbacks *niCallbacks = NULL;
static AGpsRilCallbacks *rilCallbacks = NULL;
static rpc_t *gps_rpc = NULL;
static pthread_t gps_rpc_thread;
static pthread_t gps_cb_thread;
static pthread_t ni_cb_thread;
static pthread_t agps_cb_thread;
static pthread_t xtra_cb_thread;
static pthread_t ril_cb_thread;
enum {
READ_END = 0,
WRITE_END = 1,
};
static int pipe_gps[2] = {-1, -1};
static int pipe_ni[2] = {-1, -1};
static int pipe_agps[2] = {-1, -1};
static int pipe_xtra[2] = {-1, -1};
static int pipe_ril[2] = {-1, -1};
#define CHECK_CLOSE(fd) \
do {\
if (fd >= 0) {\
close(fd); \
fd = -1;\
}\
} while (0)
/******************************************************************************
* RPC Socket Interface
*****************************************************************************/
static void gps_cb_thread_func(void* unused) {
while (pipe_gps[0] >= 0) {
struct rpc_request_hdr_t hdr;
memset(&hdr, 0, sizeof(hdr));
if (read(pipe_gps[READ_END], &hdr, sizeof(hdr)) != sizeof(hdr)) {
RPC_ERROR("failed to read request header");
break;
}
char *buf = hdr.buffer;
size_t idx = 0;
RPC_DEBUG("%s: request code %d", __func__, hdr.code);
switch (hdr.code) {
case GPS_LOC_CB:
if (gpsCallbacks && gpsCallbacks->location_cb) {
GpsLocation location;
memset(&location, 0, sizeof(location));
RPC_UNPACK(buf, idx, location);
gpsCallbacks->location_cb(&location);
}
else {
RPC_ERROR("gpsCallbacks == NULL");
}
break;
case GPS_STATUS_CB:
if (gpsCallbacks && gpsCallbacks->status_cb) {
GpsStatus status;
memset(&status, 0, sizeof(status));
RPC_UNPACK(buf, idx, status);
gpsCallbacks->status_cb(&status);
}
else {
RPC_ERROR("gpsCallbacks == NULL");
}
break;
case GPS_SV_STATUS_CB:
if (gpsCallbacks && gpsCallbacks->sv_status_cb) {
GpsSvStatus status;
memset(&status, 0, sizeof(status));
RPC_UNPACK(buf, idx, status);
gpsCallbacks->sv_status_cb(&status);
}
else {
RPC_ERROR("gpsCallbacks == NULL");
}
break;
case GPS_NMEA_CB:
if (gpsCallbacks && gpsCallbacks->nmea_cb) {
char nmea[RPC_PAYLOAD_MAX] = {};
GpsUtcTime timestamp;
int length;
RPC_UNPACK(buf, idx, timestamp);
RPC_UNPACK(buf, idx, length);
RPC_UNPACK_RAW(buf, idx, nmea, length);
gpsCallbacks->nmea_cb(timestamp,
nmea, length);
}
else {
RPC_ERROR("gpsCallbacks == NULL");
}
break;
case GPS_SET_CAPABILITIES_CB:
if (gpsCallbacks && gpsCallbacks->set_capabilities_cb) {
uint32_t caps = 0;
RPC_UNPACK(buf, idx, caps);
RPC_DEBUG("SET_CAPABILITIEST %x", caps);
gpsCallbacks->set_capabilities_cb(caps);
}
else {
RPC_ERROR("gpsCallbacks == NULL");
}
break;
case GPS_ACQUIRE_LOCK_CB:
if (gpsCallbacks && gpsCallbacks->acquire_wakelock_cb) {
gpsCallbacks->acquire_wakelock_cb();
}
else {
RPC_ERROR("gpsCallbacks == NULL");
}
break;
case GPS_RELEASE_LOCK_CB:
if (gpsCallbacks && gpsCallbacks->release_wakelock_cb) {
gpsCallbacks->release_wakelock_cb();
}
else {
RPC_ERROR("gpsCallbacks == NULL");
}
break;
case GPS_REQUEST_UTC_TIME_CB:
if (gpsCallbacks && gpsCallbacks->request_utc_time_cb) {
gpsCallbacks->request_utc_time_cb();
}
else {
RPC_ERROR("gpsCallbacks == NULL");
}
break;
}
fail:
continue;
}
}
static void agps_cb_thread_func(void* unused) {
LOG_ENTRY;
while (pipe_agps[0] >= 0) {
struct rpc_request_hdr_t hdr;
memset(&hdr, 0, sizeof(hdr));
if (read(pipe_agps[READ_END], &hdr, sizeof(hdr)) != sizeof(hdr)) {
RPC_ERROR("failed to read request header");
break;
}
char *buf = hdr.buffer;
size_t idx = 0;
RPC_DEBUG("%s: request code %d", __func__, hdr.code);
switch (hdr.code) {
case AGPS_STATUS_CB:
if (aGpsCallbacks && aGpsCallbacks->status_cb) {
AGpsStatus status;
memset(&status, 0, sizeof(status));
RPC_UNPACK(buf, idx, status);
aGpsCallbacks->status_cb(&status);
}
else {
RPC_ERROR("aGpsCallbacks == NULL");
}
break;
}
fail:
continue;
}
LOG_EXIT;
}
static void ni_cb_thread_func(void* unused) {
LOG_ENTRY;
while (pipe_ni[0] >= 0) {
struct rpc_request_hdr_t hdr;
memset(&hdr, 0, sizeof(hdr));
if (read(pipe_ni[READ_END], &hdr, sizeof(hdr)) != sizeof(hdr)) {
RPC_ERROR("failed to read request header");
break;
}
char *buf = hdr.buffer;
size_t idx = 0;
RPC_DEBUG("%s: request code %d", __func__, hdr.code);
switch (hdr.code) {
case NI_NOTIFY_CB:
if (niCallbacks && niCallbacks->notify_cb) {
GpsNiNotification nfy;
memset(&nfy, 0, sizeof(nfy));
RPC_UNPACK(buf, idx, nfy);
niCallbacks->notify_cb(&nfy);
}
else {
RPC_ERROR("niCallbacks == NULL");
}
break;
}
fail:
continue;
}
LOG_EXIT;
}
static void xtra_cb_thread_func(void* unused) {
LOG_ENTRY;
while (pipe_xtra[0] >= 0) {
struct rpc_request_hdr_t hdr;
memset(&hdr, 0, sizeof(hdr));
if (read(pipe_xtra[READ_END], &hdr, sizeof(hdr)) != sizeof(hdr)) {
RPC_ERROR("failed to read request header");
break;
}
char *buf = hdr.buffer;
size_t idx = 0;
RPC_DEBUG("%s: request code %d", __func__, hdr.code);
switch (hdr.code) {
case XTRA_REQUEST_CB:
if (xtraCallbacks && xtraCallbacks->download_request_cb) {
xtraCallbacks->download_request_cb();
}
else {
RPC_ERROR("xtraCallbacks == NULL");
}
break;
}
fail:
continue;
}
LOG_EXIT;
}
static void ril_cb_thread_func(void* unused) {
LOG_ENTRY;
while (pipe_ril[0] >= 0) {
struct rpc_request_hdr_t hdr;
memset(&hdr, 0, sizeof(hdr));
if (read(pipe_ril[READ_END], &hdr, sizeof(hdr)) != sizeof(hdr)) {
RPC_ERROR("failed to read request header");
break;
}
char *buf = hdr.buffer;
size_t idx = 0;
RPC_DEBUG("%s: request code %d", __func__, hdr.code);
switch (hdr.code) {
case RIL_SET_ID_CB:
if (rilCallbacks && rilCallbacks->request_setid) {
uint32_t flags;
RPC_UNPACK(buf, idx, flags);
rilCallbacks->request_setid(flags);
}
else {
RPC_ERROR("rilCallbacks == NULL");
}
break;
case RIL_REF_LOC_CB:
if (rilCallbacks && rilCallbacks->request_refloc) {
uint32_t flags;
RPC_UNPACK(buf, idx, flags);
rilCallbacks->request_refloc(flags);
}
else {
RPC_ERROR("rilCallbacks == NULL");
}
break;
}
fail:
continue;
}
LOG_EXIT;
}
static int gps_rpc_handler(rpc_request_hdr_t *hdr, rpc_reply_t *reply) {
LOG_ENTRY;
int rc = -1;
if (!hdr) {
RPC_ERROR("hdr is NULL");
goto fail;
}
if (!reply) {
RPC_ERROR("reply is NULL");
goto fail;
}
rc = 0;
RPC_INFO("rpc handler code %x : %s", hdr->code, gps_rpc_to_s(hdr->code));
reply->code = hdr->code;
char *buf = hdr->buffer;
size_t idx = 0;
switch (hdr->code) {
case GPS_LOC_CB:
case GPS_STATUS_CB:
case GPS_SV_STATUS_CB:
case GPS_NMEA_CB:
case GPS_SET_CAPABILITIES_CB:
case GPS_ACQUIRE_LOCK_CB:
case GPS_RELEASE_LOCK_CB:
case GPS_REQUEST_UTC_TIME_CB:
if (gpsCallbacks) {
write(pipe_gps[WRITE_END], hdr, sizeof(rpc_request_hdr_t));
}
else {
rc = -1;
RPC_ERROR("gpsCallbacks == NULL");
}
break;
case AGPS_STATUS_CB:
if (aGpsCallbacks) {
write(pipe_agps[WRITE_END], hdr, sizeof(rpc_request_hdr_t));
}
else {
rc = -1;
RPC_ERROR("aGpsCallbacks == NULL");
}
break;
case NI_NOTIFY_CB:
if (niCallbacks) {
write(pipe_ni[WRITE_END], hdr, sizeof(rpc_request_hdr_t));
}
else {
rc = -1;
RPC_ERROR("niCallbacks == NULL");
}
break;
case XTRA_REQUEST_CB:
if (xtraCallbacks) {
write(pipe_xtra[WRITE_END], hdr, sizeof(rpc_request_hdr_t));
}
else {
rc = -1;
RPC_ERROR("xtraCallbacks == NULL");
}
break;
case RIL_SET_ID_CB:
case RIL_REF_LOC_CB:
if (rilCallbacks) {
write(pipe_ril[WRITE_END], hdr, sizeof(rpc_request_hdr_t));
}
else {
rc = -1;
RPC_ERROR("rilCallbacks == NULL");
}
break;
case AGPS_CREATE_THREAD_CB:
if (aGpsCallbacks && aGpsCallbacks->create_thread_cb) {
agps_cb_thread =
aGpsCallbacks->create_thread_cb("agps",
agps_cb_thread_func, NULL);
}
else {
rc = -1;
RPC_ERROR("aGpsCallbacks == NULL");
}
break;
case NI_CREATE_THREAD_CB:
if (niCallbacks && niCallbacks->create_thread_cb) {
ni_cb_thread = niCallbacks->create_thread_cb("ni",
ni_cb_thread_func, NULL);
}
else {
rc = -1;
RPC_ERROR("niCallbacks == NULL");
}
break;
case GPS_CREATE_THREAD_CB:
if (gpsCallbacks && gpsCallbacks->create_thread_cb) {
gps_cb_thread = gpsCallbacks->create_thread_cb("gps",
gps_cb_thread_func, NULL);
}
else {
rc = -1;
RPC_ERROR("gpsCallbacks == NULL");
}
break;
case XTRA_CREATE_THREAD_CB:
if (xtraCallbacks && xtraCallbacks->create_thread_cb) {
xtra_cb_thread = xtraCallbacks->create_thread_cb("xtra",
xtra_cb_thread_func, NULL);
}
else {
rc = -1;
RPC_ERROR("xtraCallbacks == NULL");
}
break;
case RIL_CREATE_THREAD_CB:
if (rilCallbacks && rilCallbacks->create_thread_cb) {
ril_cb_thread = rilCallbacks->create_thread_cb("ril",
ril_cb_thread_func, NULL);
}
else {
rc = -1;
RPC_ERROR("rilCallbacks == NULL");
}
break;
default:
RPC_ERROR("unknown code %x", hdr->code);
break;
}
fail:
LOG_EXIT;
return 0;
}
static int rpc_call_result(rpc_t *rpc, rpc_request_t *req)
{
LOG_ENTRY;
int rc = -1;
if (!rpc) {
RPC_ERROR("rpc is NULL");
goto fail;
}
if (!req) {
RPC_ERROR("request is NULL");
goto fail;
}
rc = rpc_call(rpc, req);
if (rc < 0) {
RPC_ERROR("rpc_call failed %d", rc);
goto fail;
}
RPC_DEBUG("%s: rpc_call done", __func__);
size_t idx = 0;
RPC_UNPACK(req->reply.buffer, idx, rc);
fail:
LOG_EXIT;
return rc;
}
/******************************************************************************
* RPC Transport Setup
*****************************************************************************/
static void close_pipes(void) {
CHECK_CLOSE(pipe_gps[READ_END]);
CHECK_CLOSE(pipe_gps[WRITE_END]);
CHECK_CLOSE(pipe_agps[READ_END]);
CHECK_CLOSE(pipe_agps[WRITE_END]);
CHECK_CLOSE(pipe_ni[READ_END]);
CHECK_CLOSE(pipe_ni[WRITE_END]);
CHECK_CLOSE(pipe_xtra[READ_END]);
CHECK_CLOSE(pipe_xtra[WRITE_END]);
CHECK_CLOSE(pipe_ril[READ_END]);
CHECK_CLOSE(pipe_ril[WRITE_END]);
}
static void gps_proxy_cleanup(void) {
LOG_ENTRY;
pthread_mutex_lock(&gps_mutex);
xtraCallbacks = NULL;
aGpsCallbacks = NULL;
gpsCallbacks = NULL;
niCallbacks = NULL;
rilCallbacks = NULL;
pthread_mutex_unlock(&gps_mutex);
LOG_EXIT;
}
static int start_rpc(int fd) {
LOG_ENTRY;
struct rpc *rpc = NULL;
rpc = rpc_alloc();
if (!rpc) {
RPC_ERROR("out of memory");
goto fail;
}
if (rpc_init(fd, gps_rpc_handler, rpc)) {
RPC_ERROR("failed to init RPC");
goto fail;
}
if (rpc_start(rpc)) {
RPC_ERROR("failed to start RPC");
goto fail;
}
gps_rpc = rpc;
LOG_EXIT;
return 0;
fail:
if (rpc) {
rpc_free(rpc);
}
LOG_EXIT;
return -1;
}
static int gps_proxy_socket_open(void) {
int rc;
int retry = GPS_SOCKET_RETRY_COUNT;
int fd = -1;
LOG_ENTRY;
while (retry--) {
fd = socket_local_client(
GPS_RPC_SOCKET_NAME,
ANDROID_SOCKET_NAMESPACE_ABSTRACT,
SOCK_STREAM);
if (fd >= 0) {
break;
}
else {
RPC_ERROR("%s: fd %d, errno %d, err %s",
__func__, fd, errno, strerror(errno));
}
usleep(500);
}
LOG_EXIT;
return fd;
}
static void* gps_client(void* unused) {
int client_fd = -1;
LOG_ENTRY;
client_fd = gps_proxy_socket_open();
if (client_fd < 0) {
RPC_ERROR("failed to open the socket");
goto fail;
}
if (start_rpc(client_fd)) {
RPC_ERROR("failed to connect to the RPC server");
goto fail;
}
goto done;
fail:
if (client_fd >= 0) {
close(client_fd);
}
done:
pthread_mutex_lock(&gps_mutex);
pthread_cond_broadcast(&gps_cond);
pthread_mutex_unlock(&gps_mutex);
LOG_EXIT;
return NULL;
}
static int start_gps_client(void) {
LOG_ENTRY;
int rc = -1;
if (pipe(pipe_gps) < 0) {
RPC_ERROR("failed to create GPS pipe");
goto fail;
}
if (pipe(pipe_ni) < 0) {
RPC_ERROR("fail to create NI pipe");
goto fail;
}
if (pipe(pipe_agps) < 0) {
RPC_ERROR("failed to create AGPS pipe");
goto fail;
}
if (pipe(pipe_xtra) < 0) {
RPC_ERROR("failed to create XTRA pipe");
goto fail;
}
if (pipe(pipe_ril) < 0) {
RPC_ERROR("fail to create RIL pipe");
goto fail;
}
pthread_create(&gps_rpc_thread, NULL, gps_client, NULL);
pthread_mutex_lock(&gps_mutex);
pthread_cond_wait(&gps_cond, &gps_mutex);
pthread_mutex_unlock(&gps_mutex);
if (!gps_rpc) {
goto fail;
}
rc = 0;
goto done;
fail:
close_pipes();
done:
LOG_EXIT;
return rc;
}
/******************************************************************************
* XTRA Interface
*****************************************************************************/
static int gps_xtra_init(GpsXtraCallbacks *callbacks) {
LOG_ENTRY;
int rc = -1;
if (!callbacks) {
RPC_ERROR("%s: callbacks is NULL", __func__);
goto fail;
}
pthread_mutex_lock(&gps_mutex);
xtraCallbacks = callbacks;
pthread_mutex_unlock(&gps_mutex);
struct rpc_request_t req = {
.header = {
.code = GPS_PROXY_XTRA_INIT,
},
};
rc = rpc_call_result(gps_rpc, &req);
fail:
LOG_EXIT;
return rc;
}
static int inject_xtra_data(char *data, int length) {
LOG_ENTRY;
int rc = -1;
struct rpc_request_t req = {
.header = {
.code = GPS_PROXY_XTRA_INJECT_XTRA_DATA,
},
};
if (!data || !length) {
RPC_ERROR("%s: data is NULL", __func__);
goto fail;
}
char *buf = req.header.buffer;
size_t idx = 0;
memset(req.header.buffer, 0, RPC_PAYLOAD_MAX);
RPC_PACK(buf, idx, length);
RPC_PACK_RAW(buf, idx, data, length);
req.header.buffer[RPC_PAYLOAD_MAX - 1] = '\0';
rc = rpc_call_result(gps_rpc, &req);
fail:
LOG_EXIT;
return rc;
}
static GpsXtraInterface sGpsXtraInterface = {
.size = sizeof(GpsXtraInterface),
.init = gps_xtra_init,
.inject_xtra_data = inject_xtra_data,
};
/******************************************************************************
* AGPS Interface
*****************************************************************************/
static void agps_init(AGpsCallbacks *callbacks) {
LOG_ENTRY;
if (!callbacks) {
RPC_ERROR("%s: callbacks is NULL", __func__);
}
pthread_mutex_lock(&gps_mutex);
aGpsCallbacks = callbacks;
pthread_mutex_unlock(&gps_mutex);
struct rpc_request_t req = {
.header = {
.code = GPS_PROXY_AGPS_INIT,
},
};
rpc_call(gps_rpc, &req);
fail:
LOG_EXIT;
return;
}
static int agps_data_conn_open(const char *apn) {
LOG_ENTRY;
struct rpc_request_t req = {
.header = {
.code = GPS_PROXY_AGPS_DATA_CONN_OPEN,
},
};
int rc = -1;
char *buf = req.header.buffer;
size_t idx = 0;
if (!apn) {
RPC_DEBUG("%s: apn is NULL", __func__);
goto fail;
}
memset(req.header.buffer, 0, RPC_PAYLOAD_MAX);
RPC_PACK_S(buf, idx, apn);
req.header.buffer[RPC_PAYLOAD_MAX - 1] = '\0';
rc = rpc_call_result(gps_rpc, &req);
fail:
LOG_EXIT;
return rc;
}
static int agps_data_conn_closed(void) {
LOG_ENTRY;
struct rpc_request_t req = {
.header = {
.code = GPS_PROXY_AGPS_DATA_CONN_CLOSED,
},
};
int rc = rpc_call_result(gps_rpc, &req);
LOG_EXIT;
return rc;
}
static int agps_data_conn_failed(void) {
LOG_ENTRY;
struct rpc_request_t req = {
.header = {
.code = GPS_PROXY_AGPS_DATA_CONN_FAILED,
},
};
int rc = rpc_call_result(gps_rpc, &req);
LOG_EXIT;
return rc;
}
static int agps_set_server(AGpsType type, const char *hostname, int port) {
LOG_ENTRY;
struct rpc_request_t req = {
.header = {
.code = GPS_PROXY_AGPS_AGPS_SET_SERVER,
},
};
int rc = -1;
char *buf = req.header.buffer;
size_t idx = 0;
if (!hostname) {
RPC_ERROR("%s: hostname is NULL", __func__);
goto fail;
}
memset(req.header.buffer, 0, RPC_PAYLOAD_MAX);
RPC_PACK(buf, idx, type);
RPC_PACK(buf, idx, port);
RPC_PACK_S(buf, idx, hostname);
req.header.buffer[RPC_PAYLOAD_MAX - 1] = '\0';
rc = rpc_call_result(gps_rpc, &req);
fail:
LOG_EXIT;
return rc;
}
static AGpsInterface sAGpsInterface = {
.size = sizeof(AGpsInterface),
.init = agps_init,
.data_conn_open = agps_data_conn_open,
.data_conn_closed = agps_data_conn_closed,
.data_conn_failed = agps_data_conn_failed,
.set_server = agps_set_server,
};
/******************************************************************************
* NI Interface
*****************************************************************************/
static void ni_init(GpsNiCallbacks *callbacks) {
LOG_ENTRY;
if (!callbacks) {
RPC_ERROR("%s: callbacks is NULL", __func__);
goto fail;
}
niCallbacks = callbacks;
struct rpc_request_t req = {
.header = {
.code = GPS_PROXY_NI_INIT,
},
};
rpc_call(gps_rpc, &req);
fail:
LOG_EXIT;
}
static void ni_respond(int notif_id, GpsUserResponseType user_response) {
LOG_ENTRY;
struct rpc_request_t req = {
.header = {
.code = GPS_PROXY_NI_RESPOND,
},
};
char *buf = req.header.buffer;
size_t idx = 0;
RPC_PACK(buf, idx, notif_id);
RPC_PACK(buf, idx, user_response);
rpc_call(gps_rpc, &req);
fail:
LOG_EXIT;
return;
}
static const GpsNiInterface sGpsNiInterface = {
.size = sizeof(GpsNiInterface),
.init = ni_init,
.respond = ni_respond,
};
/******************************************************************************
* RIL Interface
*****************************************************************************/
static void ril_init(AGpsRilCallbacks *callbacks) {
LOG_ENTRY;
if (!callbacks) {
RPC_ERROR("%s: callbacks is NULL", __func__);
goto fail;
}
pthread_mutex_lock(&gps_mutex);
rilCallbacks = callbacks;
pthread_mutex_unlock(&gps_mutex);
struct rpc_request_t req = {
.header = {
.code = RIL_INIT,
}
};
rpc_call(gps_rpc, &req);
fail:
LOG_EXIT;
}
static void ril_set_ref_location(const AGpsRefLocation *agps_reflocation,
size_t sz_struct)
{
LOG_ENTRY;
if (!agps_reflocation || !sz_struct) {
RPC_ERROR("%s: agps_reflocation is NULL", __func__);
goto fail;
}
struct rpc_request_t req = {
.header = {
.code = RIL_SET_REF_LOC,
}
};
char *buf = req.header.buffer;
size_t idx = 0;
memset(req.header.buffer, 0, RPC_PAYLOAD_MAX);
RPC_PACK(buf, idx, sz_struct);
RPC_PACK_RAW(buf, idx, agps_reflocation, sz_struct);
rpc_call(gps_rpc, &req);
fail:
LOG_EXIT;
}
static void ril_set_set_id(AGpsSetIDType type, const char *setid) {
LOG_ENTRY;
if (!setid) {
RPC_ERROR("%s: setid is NULL", __func__);
goto fail;
}
struct rpc_request_t req = {
.header = {
.code = RIL_SET_SET_ID,
}
};
char *buf = req.header.buffer;
size_t idx = 0;
memset(req.header.buffer, 0, RPC_PAYLOAD_MAX);
RPC_PACK(buf, idx, type);
RPC_PACK_S(buf, idx, setid);
req.header.buffer[RPC_PAYLOAD_MAX - 1] = '\0';
rpc_call(gps_rpc, &req);
fail:
LOG_EXIT;
}
static void ril_ni_message(uint8_t *msg, size_t len) {
LOG_ENTRY;