forked from ferjm/b2g-js-ril
/
ril_worker.js
2541 lines (2297 loc) · 77.7 KB
/
ril_worker.js
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
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is RIL JS Worker.
*
* The Initial Developer of the Original Code is
* the Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Kyle Machulis <kyle@nonpolynomial.com>
* Philipp von Weitershausen <philipp@weitershausen.de>
* Fernando Jimenez <ferjmoreno@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/**
* This file implements the RIL worker thread. It communicates with
* the main thread to provide a high-level API to the phone's RIL
* stack, and with the RIL IPC thread to communicate with the RIL
* device itself. These communication channels use message events as
* known from Web Workers:
*
* - postMessage()/"message" events for main thread communication
*
* - postRILMessage()/"RILMessageEvent" events for RIL IPC thread
* communication.
*
* The three objects in this file represent individual parts of this
* communication chain:
*
* - RILMessageEvent -> Buf -> RIL -> Phone -> postMessage()
* - "message" event -> Phone -> RIL -> Buf -> postRILMessage()
*
* Note: The code below is purposely lean on abstractions to be as lean in
* terms of object allocations. As a result, it may look more like C than
* JavaScript, and that's intended.
*/
"use strict";
importScripts("ril_consts.js");
let DEBUG = false;
const INT32_MAX = 2147483647;
const UINT8_SIZE = 1;
const UINT16_SIZE = 2;
const UINT32_SIZE = 4;
const PARCEL_SIZE_SIZE = UINT32_SIZE;
/**
* This object contains helpers buffering incoming data & deconstructing it
* into parcels as well as buffering outgoing data & constructing parcels.
* For that it maintains two buffers and corresponding uint8 views, indexes.
*
* The incoming buffer is a circular buffer where we store incoming data.
* As soon as a complete parcel is received, it is processed right away, so
* the buffer only needs to be large enough to hold one parcel.
*
* The outgoing buffer is to prepare outgoing parcels. The index is reset
* every time a parcel is sent.
*/
let Buf = {
INCOMING_BUFFER_LENGTH: 1024,
OUTGOING_BUFFER_LENGTH: 1024,
init: function init() {
this.incomingBuffer = new ArrayBuffer(this.INCOMING_BUFFER_LENGTH);
this.outgoingBuffer = new ArrayBuffer(this.OUTGOING_BUFFER_LENGTH);
this.incomingBytes = new Uint8Array(this.incomingBuffer);
this.outgoingBytes = new Uint8Array(this.outgoingBuffer);
// Track where incoming data is read from and written to.
this.incomingWriteIndex = 0;
this.incomingReadIndex = 0;
// Leave room for the parcel size for outgoing parcels.
this.outgoingIndex = PARCEL_SIZE_SIZE;
// How many bytes we've read for this parcel so far.
this.readIncoming = 0;
// Size of the incoming parcel. If this is zero, we're expecting a new
// parcel.
this.currentParcelSize = 0;
// This gets incremented each time we send out a parcel.
this.token = 1;
// Maps tokens we send out with requests to the request type, so that
// when we get a response parcel back, we know what request it was for.
this.tokenRequestMap = {};
// This is the token of last solicited response.
this.lastSolicitedToken = 0;
},
/**
* Grow the incoming buffer.
*
* @param min_size
* Minimum new size. The actual new size will be the the smallest
* power of 2 that's larger than this number.
*/
growIncomingBuffer: function growIncomingBuffer(min_size) {
if (DEBUG) {
debug("Current buffer of " + this.INCOMING_BUFFER_LENGTH +
" can't handle incoming " + min_size + " bytes.");
}
let oldBytes = this.incomingBytes;
this.INCOMING_BUFFER_LENGTH =
2 << Math.floor(Math.log(min_size)/Math.log(2));
if (DEBUG) debug("New incoming buffer size: " + this.INCOMING_BUFFER_LENGTH);
this.incomingBuffer = new ArrayBuffer(this.INCOMING_BUFFER_LENGTH);
this.incomingBytes = new Uint8Array(this.incomingBuffer);
if (this.incomingReadIndex <= this.incomingWriteIndex) {
// Read and write index are in natural order, so we can just copy
// the old buffer over to the bigger one without having to worry
// about the indexes.
this.incomingBytes.set(oldBytes, 0);
} else {
// The write index has wrapped around but the read index hasn't yet.
// Write whatever the read index has left to read until it would
// circle around to the beginning of the new buffer, and the rest
// behind that.
let head = oldBytes.subarray(this.incomingReadIndex);
let tail = oldBytes.subarray(0, this.incomingReadIndex);
this.incomingBytes.set(head, 0);
this.incomingBytes.set(tail, head.length);
this.incomingReadIndex = 0;
this.incomingWriteIndex += head.length;
}
if (DEBUG) {
debug("New incoming buffer size is " + this.INCOMING_BUFFER_LENGTH);
}
},
/**
* Grow the outgoing buffer.
*
* @param min_size
* Minimum new size. The actual new size will be the the smallest
* power of 2 that's larger than this number.
*/
growOutgoingBuffer: function growOutgoingBuffer(min_size) {
if (DEBUG) {
debug("Current buffer of " + this.OUTGOING_BUFFER_LENGTH +
" is too small.");
}
let oldBytes = this.outgoingBytes;
this.OUTGOING_BUFFER_LENGTH =
2 << Math.floor(Math.log(min_size)/Math.log(2));
this.outgoingBuffer = new ArrayBuffer(this.OUTGOING_BUFFER_LENGTH);
this.outgoingBytes = new Uint8Array(this.outgoingBuffer);
this.outgoingBytes.set(oldBytes, 0);
if (DEBUG) {
debug("New outgoing buffer size is " + this.OUTGOING_BUFFER_LENGTH);
}
},
/**
* Functions for reading data from the incoming buffer.
*
* These are all little endian, apart from readParcelSize();
*/
readUint8: function readUint8() {
let value = this.incomingBytes[this.incomingReadIndex];
this.incomingReadIndex = (this.incomingReadIndex + 1) %
this.INCOMING_BUFFER_LENGTH;
return value;
},
readUint16: function readUint16() {
return this.readUint8() | this.readUint8() << 8;
},
readUint32: function readUint32() {
return this.readUint8() | this.readUint8() << 8 |
this.readUint8() << 16 | this.readUint8() << 24;
},
readUint32List: function readUint32List() {
let length = this.readUint32();
let ints = [];
for (let i = 0; i < length; i++) {
ints.push(this.readUint32());
}
return ints;
},
readString: function readString() {
let string_len = this.readUint32();
if (string_len < 0 || string_len >= INT32_MAX) {
return null;
}
let s = "";
for (let i = 0; i < string_len; i++) {
s += String.fromCharCode(this.readUint16());
}
// Strings are \0\0 delimited, but that isn't part of the length. And
// if the string length is even, the delimiter is two characters wide.
// It's insane, I know.
let delimiter = this.readUint16();
if (!(string_len & 1)) {
delimiter |= this.readUint16();
}
if (DEBUG) {
if (delimiter != 0) {
debug("Something's wrong, found string delimiter: " + delimiter);
}
}
return s;
},
readStringList: function readStringList() {
let num_strings = this.readUint32();
let strings = [];
for (let i = 0; i < num_strings; i++) {
strings.push(this.readString());
}
return strings;
},
readParcelSize: function readParcelSize() {
return this.readUint8() << 24 | this.readUint8() << 16 |
this.readUint8() << 8 | this.readUint8();
},
/**
* Functions for writing data to the outgoing buffer.
*/
writeUint8: function writeUint8(value) {
if (this.outgoingIndex >= this.OUTGOING_BUFFER_LENGTH) {
this.growOutgoingBuffer(this.outgoingIndex + 1);
}
this.outgoingBytes[this.outgoingIndex] = value;
this.outgoingIndex++;
},
writeUint16: function writeUint16(value) {
this.writeUint8(value & 0xff);
this.writeUint8((value >> 8) & 0xff);
},
writeUint32: function writeUint32(value) {
this.writeUint8(value & 0xff);
this.writeUint8((value >> 8) & 0xff);
this.writeUint8((value >> 16) & 0xff);
this.writeUint8((value >> 24) & 0xff);
},
writeString: function writeString(value) {
if (value == null) {
this.writeUint32(-1);
return;
}
this.writeUint32(value.length);
for (let i = 0; i < value.length; i++) {
this.writeUint16(value.charCodeAt(i));
}
// Strings are \0\0 delimited, but that isn't part of the length. And
// if the string length is even, the delimiter is two characters wide.
// It's insane, I know.
this.writeUint16(0);
if (!(value.length & 1)) {
this.writeUint16(0);
}
},
writeStringList: function writeStringList(strings) {
this.writeUint32(strings.length);
for (let i = 0; i < strings.length; i++) {
this.writeString(strings[i]);
}
},
writeParcelSize: function writeParcelSize(value) {
/**
* Parcel size will always be the first thing in the parcel byte
* array, but the last thing written. Store the current index off
* to a temporary to be reset after we write the size.
*/
let currentIndex = this.outgoingIndex;
this.outgoingIndex = 0;
this.writeUint8((value >> 24) & 0xff);
this.writeUint8((value >> 16) & 0xff);
this.writeUint8((value >> 8) & 0xff);
this.writeUint8(value & 0xff);
this.outgoingIndex = currentIndex;
},
/**
* Parcel management
*/
/**
* Write incoming data to the circular buffer.
*
* @param incoming
* Uint8Array containing the incoming data.
*/
writeToIncoming: function writeToIncoming(incoming) {
// We don't have to worry about the head catching the tail since
// we process any backlog in parcels immediately, before writing
// new data to the buffer. So the only edge case we need to handle
// is when the incoming data is larger than the buffer size.
if (incoming.length > this.INCOMING_BUFFER_LENGTH) {
this.growIncomingBuffer(incoming.length);
}
// We can let the typed arrays do the copying if the incoming data won't
// wrap around the edges of the circular buffer.
let remaining = this.INCOMING_BUFFER_LENGTH - this.incomingWriteIndex;
if (remaining >= incoming.length) {
this.incomingBytes.set(incoming, this.incomingWriteIndex);
} else {
// The incoming data would wrap around it.
let head = incoming.subarray(0, remaining);
let tail = incoming.subarray(remaining);
this.incomingBytes.set(head, this.incomingWriteIndex);
this.incomingBytes.set(tail, 0);
}
this.incomingWriteIndex = (this.incomingWriteIndex + incoming.length) %
this.INCOMING_BUFFER_LENGTH;
},
/**
* Process incoming data.
*
* @param incoming
* Uint8Array containing the incoming data.
*/
processIncoming: function processIncoming(incoming) {
if (DEBUG) {
debug("Received " + incoming.length + " bytes.");
debug("Already read " + this.readIncoming);
}
this.writeToIncoming(incoming);
this.readIncoming += incoming.length;
while (true) {
if (!this.currentParcelSize) {
// We're expecting a new parcel.
if (this.readIncoming < PARCEL_SIZE_SIZE) {
// We don't know how big the next parcel is going to be, need more
// data.
if (DEBUG) debug("Next parcel size unknown, going to sleep.");
return;
}
this.currentParcelSize = this.readParcelSize();
if (DEBUG) debug("New incoming parcel of size " +
this.currentParcelSize);
// The size itself is not included in the size.
this.readIncoming -= PARCEL_SIZE_SIZE;
}
if (this.readIncoming < this.currentParcelSize) {
// We haven't read enough yet in order to be able to process a parcel.
if (DEBUG) debug("Read " + this.readIncoming + ", but parcel size is "
+ this.currentParcelSize + ". Going to sleep.");
return;
}
// Alright, we have enough data to process at least one whole parcel.
// Let's do that.
let expectedAfterIndex = (this.incomingReadIndex + this.currentParcelSize)
% this.INCOMING_BUFFER_LENGTH;
if (DEBUG) {
let parcel;
if (expectedAfterIndex < this.incomingReadIndex) {
let head = this.incomingBytes.subarray(this.incomingReadIndex);
let tail = this.incomingBytes.subarray(0, expectedAfterIndex);
parcel = Array.slice(head).concat(Array.slice(tail));
} else {
parcel = Array.slice(this.incomingBytes.subarray(
this.incomingReadIndex, expectedAfterIndex));
}
debug("Parcel (size " + this.currentParcelSize + "): " + parcel);
}
if (DEBUG) debug("We have at least one complete parcel.");
try {
this.processParcel();
} catch (ex) {
if (DEBUG) debug("Parcel handling threw " + ex + "\n" + ex.stack);
}
// Ensure that the whole parcel was consumed.
if (this.incomingReadIndex != expectedAfterIndex) {
if (DEBUG) {
debug("Parcel handler didn't consume whole parcel, " +
Math.abs(expectedAfterIndex - this.incomingReadIndex) +
" bytes left over");
}
this.incomingReadIndex = expectedAfterIndex;
}
this.readIncoming -= this.currentParcelSize;
this.currentParcelSize = 0;
}
},
/**
* Process one parcel.
*/
processParcel: function processParcel() {
let response_type = this.readUint32();
let length = this.readIncoming - UINT32_SIZE;
let request_type;
if (response_type == RESPONSE_TYPE_SOLICITED) {
let token = this.readUint32();
let error = this.readUint32();
length -= 2 * UINT32_SIZE;
request_type = this.tokenRequestMap[token];
if (error) {
//TODO
if (DEBUG) {
debug("Received error " + error + " for solicited parcel type " +
request_type);
}
return;
}
if (DEBUG) {
debug("Solicited response for request type " + request_type +
", token " + token);
}
delete this.tokenRequestMap[token];
this.lastSolicitedToken = token;
} else if (response_type == RESPONSE_TYPE_UNSOLICITED) {
request_type = this.readUint32();
length -= UINT32_SIZE;
if (DEBUG) debug("Unsolicited response for request type " + request_type);
} else {
if (DEBUG) debug("Unknown response type: " + response_type);
return;
}
RIL.handleParcel(request_type, length);
},
/**
* Start a new outgoing parcel.
*
* @param type
* Integer specifying the request type.
*/
newParcel: function newParcel(type) {
// We're going to leave room for the parcel size at the beginning.
this.outgoingIndex = PARCEL_SIZE_SIZE;
this.writeUint32(type);
let token = this.token;
this.writeUint32(token);
this.tokenRequestMap[token] = type;
this.token++;
return token;
},
/**
* Communicate with the RIL IPC thread.
*/
sendParcel: function sendParcel() {
// Compute the size of the parcel and write it to the front of the parcel
// where we left room for it. Note that he parcel size does not include
// the size itself.
let parcelSize = this.outgoingIndex - PARCEL_SIZE_SIZE;
this.writeParcelSize(parcelSize);
// This assumes that postRILMessage will make a copy of the ArrayBufferView
// right away!
let parcel = this.outgoingBytes.subarray(0, this.outgoingIndex);
if (DEBUG) debug("Outgoing parcel: " + Array.slice(parcel));
postRILMessage(parcel);
this.outgoingIndex = PARCEL_SIZE_SIZE;
},
simpleRequest: function simpleRequest(type) {
this.newParcel(type);
this.sendParcel();
}
};
/**
* Provide a high-level API representing the RIL's capabilities. This is
* where parcels are sent and received from and translated into API calls.
* For the most part, this object is pretty boring as it simply translates
* between method calls and RIL parcels. Somebody's gotta do the job...
*/
let RIL = {
/**
* Retrieve the ICC's status.
*
* Response will call Phone.onICCStatus().
*/
getICCStatus: function getICCStatus() {
Buf.simpleRequest(REQUEST_GET_SIM_STATUS);
},
/**
* Enter a PIN to unlock the ICC.
*
* @param pin
* String containing the PIN.
*
* Response will call Phone.onEnterICCPIN().
*/
enterICCPIN: function enterICCPIN(pin) {
Buf.newParcel(REQUEST_ENTER_SIM_PIN);
Buf.writeUint32(1);
Buf.writeString(pin);
Buf.sendParcel();
},
/**
* Change the current ICC PIN number
*
* @param oldPin
* String containing the old PIN value
* @param newPin
* String containing the new PIN value
*
* Response will call Phone.onChangeICCPIN().
*/
changeICCPIN: function changeICCPIN(oldPin, newPin) {
Buf.newParcel(REQUEST_CHANGE_SIM_PIN);
Buf.writeUint32(2);
Buf.writeString(oldPin);
Buf.writeString(newPin);
Buf.sendParcel();
},
/**
* Supplies SIM PUK and a new PIN to unlock the ICC
*
* @param puk
* String containing the PUK value.
* @param newPin
* String containing the new PIN value.
*
* Response will call Phone.onEnterICCPUK().
*/
enterICCPUK: function enterICCPUK(puk, newPin) {
Buf.newParcel(REQUEST_ENTER_SIM_PUK);
Buf.writeUint32(2);
Buf.writeString(puk);
Buf.writeString(newPin);
Buf.sendParcel();
},
/**
* Request the phone's radio power to be switched on or off.
*
* @param on
* Boolean indicating the desired power state.
*/
setRadioPower: function setRadioPower(on) {
Buf.newParcel(REQUEST_RADIO_POWER);
Buf.writeUint32(1);
Buf.writeUint32(on ? 1 : 0);
Buf.sendParcel();
},
/**
* Set screen state.
*
* @param on
* Boolean indicating whether the screen should be on or off.
*/
setScreenState: function setScreenState(on) {
Buf.newParcel(REQUEST_SCREEN_STATE);
Buf.writeUint32(1);
Buf.writeUint32(on ? 1 : 0);
Buf.sendParcel();
},
getRegistrationState: function getRegistrationState() {
Buf.simpleRequest(REQUEST_REGISTRATION_STATE);
},
getGPRSRegistrationState: function getGPRSRegistrationState() {
Buf.simpleRequest(REQUEST_GPRS_REGISTRATION_STATE);
},
getOperator: function getOperator() {
Buf.simpleRequest(REQUEST_OPERATOR);
},
getNetworkSelectionMode: function getNetworkSelectionMode() {
Buf.simpleRequest(REQUEST_QUERY_NETWORK_SELECTION_MODE);
},
/**
* Get current calls.
*/
getCurrentCalls: function getCurrentCalls() {
Buf.simpleRequest(REQUEST_GET_CURRENT_CALLS);
},
/**
* Get the signal strength.
*/
getSignalStrength: function getSignalStrength() {
Buf.simpleRequest(REQUEST_SIGNAL_STRENGTH);
},
getIMEI: function getIMEI() {
Buf.simpleRequest(REQUEST_GET_IMEI);
},
getIMEISV: function getIMEISV() {
Buf.simpleRequest(REQUEST_GET_IMEISV);
},
getDeviceIdentity: function getDeviceIdentity() {
Buf.simpleRequest(REQUEST_GET_DEVICE_IDENTITY);
},
/**
* Dial the phone.
*
* @param address
* String containing the address (number) to dial.
* @param clirMode
* Integer doing something XXX TODO
* @param uusInfo
* Integer doing something XXX TODO
*/
dial: function dial(address, clirMode, uusInfo) {
let token = Buf.newParcel(REQUEST_DIAL);
Buf.writeString(address);
Buf.writeUint32(clirMode || 0);
Buf.writeUint32(uusInfo || 0);
// TODO Why do we need this extra 0? It was put it in to make this
// match the format of the binary message.
Buf.writeUint32(0);
Buf.sendParcel();
},
/**
* Hang up the phone.
*
* @param callIndex
* Call index (1-based) as reported by REQUEST_GET_CURRENT_CALLS.
*/
hangUp: function hangUp(callIndex) {
Buf.newParcel(REQUEST_HANGUP);
Buf.writeUint32(1);
Buf.writeUint32(callIndex);
Buf.sendParcel();
},
/**
* Mute or unmute the radio.
*
* @param mute
* Boolean to indicate whether to mute or unmute the radio.
*/
setMute: function setMute(mute) {
Buf.newParcel(REQUEST_SET_MUTE);
Buf.writeUint32(1);
Buf.writeUint32(mute ? 1 : 0);
Buf.sendParcel();
},
/**
* Answer an incoming call.
*/
answerCall: function answerCall() {
Buf.simpleRequest(REQUEST_ANSWER);
},
/**
* Reject an incoming call.
*/
rejectCall: function rejectCall() {
Buf.simpleRequest(REQUEST_UDUB);
},
/**
* Send an SMS.
*
* @param smscPDU
* String containing the SMSC PDU in hex format.
* @param address
* String containing the recipients address.
* @param body
* String containing the message body.
* @param dcs
* Data coding scheme. One of the PDU_DCS_MSG_CODING_*BITS_ALPHABET
* constants.
* @param bodyLengthInOctets
* Byte length of the message body when encoded with the given DCS.
*/
sendSMS: function sendSMS(smscPDU, address, body, dcs, bodyLengthInOctets) {
let token = Buf.newParcel(REQUEST_SEND_SMS);
//TODO we want to map token to the input values so that on the
// response from the RIL device we know which SMS request was successful
// or not. Maybe we should build that functionality into newParcel() and
// handle it within tokenRequestMap[].
Buf.writeUint32(2);
Buf.writeString(smscPDU);
GsmPDUHelper.writeMessage(address, body, dcs, bodyLengthInOctets);
Buf.sendParcel();
},
/**
* Acknowledge the receipt and handling of an SMS.
*
* @param success
* Boolean indicating whether the message was successfuly handled.
* @param cause
* SMS_* constant indicating the reason for unsuccessful handling.
*/
acknowledgeSMS: function acknowledgeSMS(success, cause) {
let token = Buf.newParcel(REQUEST_SMS_ACKNOWLEDGE);
Buf.writeUint32(2);
Buf.writeUint32(success ? 1 : 0);
Buf.writeUint32(cause);
Buf.sendParcel();
},
/**
* Start a DTMF Tone.
*
* @param dtmfChar
* DTMF signal to send, 0-9, *, +
*/
startTone: function startTone(dtmfChar) {
Buf.newParcel(REQUEST_DTMF_START);
Buf.writeString(dtmfChar);
Buf.sendParcel();
},
stopTone: function stopTone() {
Buf.simpleRequest(REQUEST_DTMF_STOP);
},
sendTone: function sendTone(dtmfChar) {
Buf.newParcel(REQUEST_DTMF);
Buf.writeString(dtmfChar);
Buf.sendParcel();
},
/**
* Get the Short Message Service Center address.
*/
getSMSCAddress: function getSMSCAddress() {
Buf.simpleRequest(REQUEST_GET_SMSC_ADDRESS);
},
/**
* Set the Short Message Service Center address.
*
* @param smsc
* Short Message Service Center address in PDU format.
*/
setSMSCAddress: function setSMSCAddress(smsc) {
Buf.newParcel(REQUEST_SET_SMSC_ADDRESS);
Buf.writeString(smsc);
Buf.sendParcel();
},
/**
* Setup a data call.
*
* @param radioTech
* Integer to indicate radio technology.
* DATACALL_RADIOTECHNOLOGY_CDMA => CDMA.
* DATACALL_RADIOTECHNOLOGY_GSM => GSM.
* @param apn
* String containing the name of the APN to connect to.
* @param user
* String containing the username for the APN.
* @param passwd
* String containing the password for the APN.
* @param chappap
* Integer containing CHAP/PAP auth type.
* DATACALL_AUTH_NONE => PAP and CHAP is never performed.
* DATACALL_AUTH_PAP => PAP may be performed.
* DATACALL_AUTH_CHAP => CHAP may be performed.
* DATACALL_AUTH_PAP_OR_CHAP => PAP / CHAP may be performed.
* @param pdptype
* String containing PDP type to request. ("IP", "IPV6", ...)
*/
setupDataCall: function (radioTech, apn, user, passwd, chappap, pdptype) {
let token = Buf.newParcel(REQUEST_SETUP_DATA_CALL);
Buf.writeUint32(7);
Buf.writeString(radioTech.toString());
Buf.writeString(DATACALL_PROFILE_DEFAULT.toString());
Buf.writeString(apn);
Buf.writeString(user);
Buf.writeString(passwd);
Buf.writeString(chappap.toString());
Buf.writeString(pdptype);
Buf.sendParcel();
return token;
},
/**
* Deactivate a data call.
*
* @param cid
* String containing CID.
* @param reason
* One of DATACALL_DEACTIVATE_* constants.
*/
deactivateDataCall: function (cid, reason) {
let token = Buf.newParcel(REQUEST_DEACTIVATE_DATA_CALL);
Buf.writeUint32(2);
Buf.writeString(cid);
Buf.writeString(reason);
Buf.sendParcel();
return token;
},
/**
* Get a list of data calls.
*/
getDataCallList: function getDataCallList() {
Buf.simpleRequest(REQUEST_DATA_CALL_LIST);
},
/**
* Get failure casue code for the most recently failed PDP context.
*/
getFailCauseCode: function getFailCauseCode() {
Buf.simpleRequest(REQUEST_LAST_CALL_FAIL_CAUSE);
},
/**
* Handle incoming requests from the RIL. We find the method that
* corresponds to the request type. Incidentally, the request type
* _is_ the method name, so that's easy.
*/
handleParcel: function handleParcel(request_type, length) {
let method = this[request_type];
if (typeof method == "function") {
if (DEBUG) debug("Handling parcel as " + method.name);
method.call(this, length);
}
}
};
RIL[REQUEST_GET_SIM_STATUS] = function REQUEST_GET_SIM_STATUS() {
let iccStatus = {
cardState: Buf.readUint32(), // CARD_STATE_*
universalPINState: Buf.readUint32(), // PINSTATE_*
gsmUmtsSubscriptionAppIndex: Buf.readUint32(),
setCdmaSubscriptionAppIndex: Buf.readUint32(),
apps: []
};
let apps_length = Buf.readUint32();
if (apps_length > CARD_MAX_APPS) {
apps_length = CARD_MAX_APPS;
}
for (let i = 0 ; i < apps_length ; i++) {
iccStatus.apps.push({
app_type: Buf.readUint32(), // APPTYPE_*
app_state: Buf.readUint32(), // CARD_APP_STATE_*
perso_substate: Buf.readUint32(), // PERSOSUBSTATE_*
aid: Buf.readString(),
app_label: Buf.readString(),
pin1_replaced: Buf.readUint32(),
pin1: Buf.readUint32(),
pin2: Buf.readUint32()
});
}
Phone.onICCStatus(iccStatus);
};
RIL[REQUEST_ENTER_SIM_PIN] = function REQUEST_ENTER_SIM_PIN() {
let response = Buf.readUint32List();
Phone.onEnterICCPIN(response);
};
RIL[REQUEST_ENTER_SIM_PUK] = function REQUEST_ENTER_SIM_PUK() {
let response = Buf.readUint32List();
Phone.onEnterICCPUK(response);
};
RIL[REQUEST_ENTER_SIM_PIN2] = null;
RIL[REQUEST_ENTER_SIM_PUK2] = null;
RIL[REQUEST_CHANGE_SIM_PIN] = function REQUEST_CHANGE_SIM_PIN() {
Phone.onChangeICCPIN();
};
RIL[REQUEST_CHANGE_SIM_PIN2] = null;
RIL[REQUEST_ENTER_NETWORK_DEPERSONALIZATION] = null;
RIL[REQUEST_GET_CURRENT_CALLS] = function REQUEST_GET_CURRENT_CALLS(length) {
let calls_length = 0;
// The RIL won't even send us the length integer if there are no active calls.
// So only read this integer if the parcel actually has it.
if (length) {
calls_length = Buf.readUint32();
}
if (!calls_length) {
Phone.onCurrentCalls(null);
return;
}
let calls = {};
for (let i = 0; i < calls_length; i++) {
let call = {
state: Buf.readUint32(), // CALL_STATE_*
callIndex: Buf.readUint32(), // GSM index (1-based)
toa: Buf.readUint32(),
isMpty: Boolean(Buf.readUint32()),
isMT: Boolean(Buf.readUint32()),
als: Buf.readUint32(),
isVoice: Boolean(Buf.readUint32()),
isVoicePrivacy: Boolean(Buf.readUint32()),
somethingOrOther: Buf.readUint32(), //XXX TODO whatziz? not in ril.h, but it's in the output...
number: Buf.readString(), //TODO munge with TOA
numberPresentation: Buf.readUint32(), // CALL_PRESENTATION_*
name: Buf.readString(),
namePresentation: Buf.readUint32(),
uusInfo: null
};
let uusInfoPresent = Buf.readUint32();
if (uusInfoPresent == 1) {
call.uusInfo = {
type: Buf.readUint32(),
dcs: Buf.readUint32(),
userData: null //XXX TODO byte array?!?
};
}
calls[call.callIndex] = call;
}
Phone.onCurrentCalls(calls);
};
RIL[REQUEST_DIAL] = function REQUEST_DIAL(length) {
Phone.onDial();
};
RIL[REQUEST_GET_IMSI] = function REQUEST_GET_IMSI(length) {
let imsi = Buf.readString();
Phone.onIMSI(imsi);
};
RIL[REQUEST_HANGUP] = function REQUEST_HANGUP(length) {
Phone.onHangUp();
};
RIL[REQUEST_HANGUP_WAITING_OR_BACKGROUND] = null;
RIL[REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND] = null;
RIL[REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE] = null;
RIL[REQUEST_SWITCH_HOLDING_AND_ACTIVE] = null;
RIL[REQUEST_CONFERENCE] = null;
RIL[REQUEST_UDUB] = function REQUEST_UDUB(length) {
Phone.onRejectCall();
};
RIL[REQUEST_LAST_CALL_FAIL_CAUSE] = null;
RIL[REQUEST_SIGNAL_STRENGTH] = function REQUEST_SIGNAL_STRENGTH() {
let strength = {
// Valid values are (0-31, 99) as defined in TS 27.007 8.5.
// For some reason we're getting int32s like [99, 4, 0, 0] and [99, 3, 0, 0]
// here, so let's strip of anything beyond the first byte.
gsmSignalStrength: Buf.readUint32() & 0xff,
// GSM bit error rate (0-7, 99) as defined in TS 27.007 8.5.
gsmBitErrorRate: Buf.readUint32(),
// The CDMA RSSI value.
cdmaDBM: Buf.readUint32(),
// The CDMA EC/IO.
cdmaECIO: Buf.readUint32(),
// The EVDO RSSI value.
evdoDBM: Buf.readUint32(),
// The EVDO EC/IO.
evdoECIO: Buf.readUint32(),
// Valid values are 0-8. 8 is the highest signal to noise ratio
evdoSNR: Buf.readUint32()
};
Phone.onSignalStrength(strength);
};