1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _CHRE_WWAN_H_
18 #define _CHRE_WWAN_H_
19 
20 /**
21  * @file
22  * Wireless Wide Area Network (WWAN, i.e. mobile/cellular network) API relevant
23  * for querying cell tower identity and associated information that can be
24  * useful in determining location.
25  *
26  * Based on Android N RIL definitions (located at this path as of the time of
27  * this comment: hardware/ril/include/telephony/ril.h), version 12. Updated
28  * based on Android radio HAL definition (hardware/interfaces/radio) for more
29  * recent Android builds. Refer to those files and associated documentation for
30  * further details.
31  *
32  * In general, the parts of this API that are taken from the RIL follow the
33  * field naming conventions established in that interface rather than the CHRE
34  * API conventions, in order to avoid confusion and enable code re-use where
35  * applicable. Note that structure names include the chreWwan* prefix rather
36  * than RIL_*, but field names are the same. If necessary to enable code
37  * sharing, it is recommended to create typedefs that map from the CHRE
38  * structures to the associated RIL type names, for example "typedef struct
39  * chreWwanCellIdentityGsm RIL_CellIdentityGsm_v12", etc.
40  */
41 
42 #include <chre/common.h>
43 
44 #include <stdbool.h>
45 #include <stdint.h>
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 /**
52  * The set of flags returned by chreWwanGetCapabilities().
53  * @defgroup CHRE_WWAN_CAPABILITIES
54  * @{
55  */
56 
57 //! No WWAN APIs are supported
58 #define CHRE_WWAN_CAPABILITIES_NONE  UINT32_C(0)
59 
60 //! Current cell information can be queried via chreWwanGetCellInfoAsync()
61 #define CHRE_WWAN_GET_CELL_INFO      UINT32_C(1 << 0)
62 
63 /** @} */
64 
65 /**
66  * Produce an event ID in the block of IDs reserved for WWAN
67  * @param offset  Index into WWAN event ID block; valid range [0,15]
68  */
69 #define CHRE_WWAN_EVENT_ID(offset)  (CHRE_EVENT_WWAN_FIRST_EVENT + (offset))
70 
71 /**
72  * nanoappHandleEvent argument: struct chreWwanCellInfoResult
73  *
74  * Provides the result of an asynchronous request for cell info sent via
75  * chreWwanGetCellInfoAsync().
76  */
77 #define CHRE_EVENT_WWAN_CELL_INFO_RESULT  CHRE_WWAN_EVENT_ID(0)
78 
79 // NOTE: Do not add new events with ID > 15; only values 0-15 are reserved
80 // (see chre/event.h)
81 
82 /**
83  * The current version of struct chreWwanCellInfoResult associated with this
84  * API definition.
85  */
86 #define CHRE_WWAN_CELL_INFO_RESULT_VERSION  UINT8_C(1)
87 
88 //! Reference: RIL_CellIdentityGsm_v12
89 struct chreWwanCellIdentityGsm {
90     //! 3-digit Mobile Country Code, 0..999, INT32_MAX if unknown
91     int32_t mcc;
92 
93     //! 2 or 3-digit Mobile Network Code, 0..999, INT32_MAX if unknown
94     int32_t mnc;
95 
96     //! 16-bit Location Area Code, 0..65535, INT32_MAX if unknown
97     int32_t lac;
98 
99     //! 16-bit GSM Cell Identity described in TS 27.007, 0..65535,
100     //! INT32_MAX if unknown
101     int32_t cid;
102 
103     //! 16-bit GSM Absolute RF channel number, INT32_MAX if unknown
104     int32_t arfcn;
105 
106     //! 6-bit Base Station Identity Code, UINT8_MAX if unknown
107     uint8_t bsic;
108 
109     //! Reserved for future use; must be set to 0
110     uint8_t reserved[3];
111 };
112 
113 //! Reference: RIL_CellIdentityWcdma_v12
114 struct chreWwanCellIdentityWcdma {
115     //! 3-digit Mobile Country Code, 0..999, INT32_MAX if unknown
116     int32_t mcc;
117 
118     //! 2 or 3-digit Mobile Network Code, 0..999, INT32_MAX if unknown
119     int32_t mnc;
120 
121     //! 16-bit Location Area Code, 0..65535, INT32_MAX if unknown
122     int32_t lac;
123 
124     //! 28-bit UMTS Cell Identity described in TS 25.331, 0..268435455,
125     //! INT32_MAX if unknown
126     int32_t cid;
127 
128     //! 9-bit UMTS Primary Scrambling Code described in TS 25.331, 0..511,
129     //! INT32_MAX if unknown
130     int32_t psc;
131 
132     //! 16-bit UMTS Absolute RF Channel Number, INT32_MAX if unknown
133     int32_t uarfcn;
134 };
135 
136 //! Reference: RIL_CellIdentityCdma
137 struct chreWwanCellIdentityCdma {
138     //! Network Id 0..65535, INT32_MAX if unknown
139     int32_t networkId;
140 
141     //! CDMA System Id 0..32767, INT32_MAX if unknown
142     int32_t systemId;
143 
144     //! Base Station Id 0..65535, INT32_MAX if unknown
145     int32_t basestationId;
146 
147     //! Longitude is a decimal number as specified in 3GPP2 C.S0005-A v6.0.
148     //! It is represented in units of 0.25 seconds and ranges from -2592000
149     //! to 2592000, both values inclusive (corresponding to a range of -180
150     //! to +180 degrees). INT32_MAX if unknown
151     int32_t longitude;
152 
153     //! Latitude is a decimal number as specified in 3GPP2 C.S0005-A v6.0.
154     //! It is represented in units of 0.25 seconds and ranges from -1296000
155     //! to 1296000, both values inclusive (corresponding to a range of -90
156     //! to +90 degrees). INT32_MAX if unknown
157     int32_t latitude;
158 };
159 
160 //! Reference: RIL_CellIdentityLte_v12
161 struct chreWwanCellIdentityLte {
162     //! 3-digit Mobile Country Code, 0..999, INT32_MAX if unknown
163     int32_t mcc;
164 
165     //! 2 or 3-digit Mobile Network Code, 0..999, INT32_MAX if unknown
166     int32_t mnc;
167 
168     //! 28-bit Cell Identity described in TS ???, INT32_MAX if unknown
169     int32_t ci;
170 
171     //! physical cell id 0..503, INT32_MAX if unknown
172     int32_t pci;
173 
174     //! 16-bit tracking area code, INT32_MAX if unknown
175     int32_t tac;
176 
177     //! 18-bit LTE Absolute RF Channel Number, INT32_MAX if unknown
178     int32_t earfcn;
179 };
180 
181 //! Reference: RIL_CellIdentityTdscdma
182 struct chreWwanCellIdentityTdscdma {
183     //! 3-digit Mobile Country Code, 0..999, INT32_MAX if unknown
184     int32_t mcc;
185 
186     //! 2 or 3-digit Mobile Network Code, 0..999, INT32_MAX if unknown
187     int32_t mnc;
188 
189     //! 16-bit Location Area Code, 0..65535, INT32_MAX if unknown
190     int32_t lac;
191 
192     //! 28-bit UMTS Cell Identity described in TS 25.331, 0..268435455,
193     //! INT32_MAX if unknown
194     int32_t cid;
195 
196     //! 8-bit Cell Parameters ID described in TS 25.331, 0..127, INT32_MAX if
197     //! unknown
198     int32_t cpid;
199 };
200 
201 //! Reference: android.hardware.radio@1.4 CellIdentityNr
202 //! @since v1.4
203 struct chreWwanCellIdentityNr {
204     //! 3-digit Mobile Country Code, in range [0, 999]. This value must be valid
205     //! for registered or camped cells. INT32_MAX means invalid/unreported.
206     int32_t mcc;
207 
208     //! 2 or 3-digit Mobile Network Code, in range [0, 999]. This value must be
209     //! valid for registered or camped cells. INT32_MAX means
210     //! invalid/unreported.
211     int32_t mnc;
212 
213     //! NR Cell Identity in range [0, 68719476735] (36 bits), which
214     //! unambiguously identifies a cell within a public land mobile network
215     //! (PLMN). This value must be valid for registered or camped cells.
216     //! Reference: TS 38.413 section 9.3.1.7.
217     //!
218     //! Note: for backward compatibility reasons, the nominally int64_t nci is
219     //! split into two uint32_t values, with nci0 being the least significant 4
220     //! bytes. If chreWwanUnpackNrNci returns INT64_MAX, it means nci is
221     //! invalid/unreported.
222     //!
223     //! Users are recommended to use the helper accessor chreWwanUnpackNrNci to
224     //! access the nci field.
225     //!
226     //! @see chreWwanUnpackNrNci
227     uint32_t nci0;
228     uint32_t nci1;
229 
230     //! Physical cell id in range [0, 1007]. This value must be valid.
231     //! Reference: TS 38.331 section 6.3.2.
232     int32_t pci;
233 
234     //! 24-bit tracking area code in range [0, 16777215]. INT32_MAX means
235     //! invalid/unreported.
236     //! Reference: TS 38.413 section 9.3.3.10 and TS 29.571 section 5.4.2.
237     int32_t tac;
238 
239     //! NR Absolute Radio Frequency Channel Number, in range [0, 3279165]. This
240     //! value must be valid.
241     //! Reference: TS 38.101-1 section 5.4.2.1 and TS 38.101-2 section 5.4.2.1.
242     int32_t nrarfcn;
243 };
244 
245 //! Reference: RIL_GSM_SignalStrength_v12
246 struct chreWwanSignalStrengthGsm {
247     //! Valid values are (0-31, 99) as defined in TS 27.007 8.5
248     //! INT32_MAX means invalid/unreported.
249     int32_t signalStrength;
250 
251     //! bit error rate (0-7, 99) as defined in TS 27.007 8.5
252     //! INT32_MAX means invalid/unreported.
253     int32_t bitErrorRate;
254 
255     //! Timing Advance in bit periods. 1 bit period = 48.13 us.
256     //! INT32_MAX means invalid/unreported.
257     int32_t timingAdvance;
258 };
259 
260 //! Reference: RIL_SignalStrengthWcdma
261 struct chreWwanSignalStrengthWcdma {
262     //! Valid values are (0-31, 99) as defined in TS 27.007 8.5
263     //! INT32_MAX means invalid/unreported.
264     int32_t signalStrength;
265 
266     //! bit error rate (0-7, 99) as defined in TS 27.007 8.5
267     //! INT32_MAX means invalid/unreported.
268     int32_t bitErrorRate;
269 };
270 
271 //! Reference: RIL_CDMA_SignalStrength
272 struct chreWwanSignalStrengthCdma {
273     //! Valid values are positive integers.  This value is the actual RSSI value
274     //! multiplied by -1.  Example: If the actual RSSI is -75, then this
275     //! response value will be 75.
276     //! INT32_MAX means invalid/unreported.
277     int32_t dbm;
278 
279     //! Valid values are positive integers.  This value is the actual Ec/Io
280     //! multiplied by -10.  Example: If the actual Ec/Io is -12.5 dB, then this
281     //! response value will be 125.
282     //! INT32_MAX means invalid/unreported.
283     int32_t ecio;
284 };
285 
286 //! Reference: RIL_EVDO_SignalStrength
287 struct chreWwanSignalStrengthEvdo {
288     //! Valid values are positive integers.  This value is the actual RSSI value
289     //! multiplied by -1.  Example: If the actual RSSI is -75, then this
290     //! response value will be 75.
291     //! INT32_MAX means invalid/unreported.
292     int32_t dbm;
293 
294     //! Valid values are positive integers.  This value is the actual Ec/Io
295     //! multiplied by -10.  Example: If the actual Ec/Io is -12.5 dB, then this
296     //! response value will be 125.
297     //! INT32_MAX means invalid/unreported.
298     int32_t ecio;
299 
300     //! Valid values are 0-8.  8 is the highest signal to noise ratio.
301     //! INT32_MAX means invalid/unreported.
302     int32_t signalNoiseRatio;
303 };
304 
305 //! Reference: RIL_LTE_SignalStrength_v8
306 struct chreWwanSignalStrengthLte {
307     //! Valid values are (0-31, 99) as defined in TS 27.007 8.5
308     int32_t signalStrength;
309 
310     //! The current Reference Signal Receive Power in dBm multiplied by -1.
311     //! Range: 44 to 140 dBm
312     //! INT32_MAX means invalid/unreported.
313     //! Reference: 3GPP TS 36.133 9.1.4
314     int32_t rsrp;
315 
316     //! The current Reference Signal Receive Quality in dB multiplied by -1.
317     //! Range: 3 to 20 dB.
318     //! INT32_MAX means invalid/unreported.
319     //! Reference: 3GPP TS 36.133 9.1.7
320     int32_t rsrq;
321 
322     //! The current reference signal signal-to-noise ratio in 0.1 dB units.
323     //! Range: -200 to +300 (-200 = -20.0 dB, +300 = 30dB).
324     //! INT32_MAX means invalid/unreported.
325     //! Reference: 3GPP TS 36.101 8.1.1
326     int32_t rssnr;
327 
328     //! The current Channel Quality Indicator.
329     //! Range: 0 to 15.
330     //! INT32_MAX means invalid/unreported.
331     //! Reference: 3GPP TS 36.101 9.2, 9.3, A.4
332     int32_t cqi;
333 
334     //! timing advance in micro seconds for a one way trip from cell to device.
335     //! Approximate distance can be calculated using 300m/us * timingAdvance.
336     //! Range: 0 to 0x7FFFFFFE
337     //! INT32_MAX means invalid/unreported.
338     //! Reference: 3GPP 36.321 section 6.1.3.5
339     //! also: http://www.cellular-planningoptimization.com/2010/02/timing-advance-with-calculation.html
340     int32_t timingAdvance;
341 };
342 
343 //! Reference: RIL_TD_SCDMA_SignalStrength
344 struct chreWwanSignalStrengthTdscdma {
345     //! The Received Signal Code Power in dBm multiplied by -1.
346     //! Range : 25 to 120
347     //! INT32_MAX means invalid/unreported.
348     //! Reference: 3GPP TS 25.123, section 9.1.1.1
349     int32_t rscp;
350 };
351 
352 //! Reference: android.hardware.radio@1.4 NrSignalStrength
353 //! @since v1.4
354 struct chreWwanSignalStrengthNr {
355     //! SS (second synchronization) reference signal received power in dBm
356     //! multiplied by -1.
357     //! Range [44, 140], INT32_MAX means invalid/unreported.
358     //! Reference: TS 38.215 section 5.1.1 and TS 38.133 section 10.1.6.
359     int32_t ssRsrp;
360 
361     //! SS reference signal received quality in 0.5 dB units.
362     //! Range [-86, 41] with -86 = -43.0 dB and 41 = 20.5 dB.
363     //! INT32_MAX means invalid/unreported.
364     //! Reference: TS 38.215 section 5.1.3 and TS 38.133 section 10.1.11.1.
365     int32_t ssRsrq;
366 
367     //! SS signal-to-noise and interference ratio in 0.5 dB units.
368     //! Range [-46, 81] with -46 = -23.0 dB and 81 = 40.5 dB.
369     //! INT32_MAX means invalid/unreported.
370     //! Reference: TS 38.215 section 5.1.5 and TS 38.133 section 10.1.16.1.
371     int32_t ssSinr;
372 
373     //! CSI reference signal received power in dBm multiplied by -1.
374     //! Range [44, 140], INT32_MAX means invalid/unreported.
375     //! Reference: TS 38.215 section 5.1.2 and TS 38.133 section 10.1.6.
376     int32_t csiRsrp;
377 
378     //! CSI reference signal received quality in 0.5 dB units.
379     //! Range [-86, 41] with -86 = -43.0 dB and 41 = 20.5 dB.
380     //! INT32_MAX means invalid/unreported.
381     //! Reference: TS 38.215 section 5.1.4 and TS 38.133 section 10.1.11.1.
382     int32_t csiRsrq;
383 
384     //! CSI signal-to-noise and interference ratio in 0.5 dB units.
385     //! Range [-46, 81] with -46 = -23.0 dB and 81 = 40.5 dB.
386     //! INT32_MAX means invalid/unreported.
387     //! Reference: TS 38.215 section 5.1.6 and TS 38.133 section 10.1.16.1.
388     int32_t csiSinr;
389 };
390 
391 //! Reference: RIL_CellInfoGsm_v12
392 struct chreWwanCellInfoGsm {
393     struct chreWwanCellIdentityGsm    cellIdentityGsm;
394     struct chreWwanSignalStrengthGsm  signalStrengthGsm;
395 };
396 
397 //! Reference: RIL_CellInfoWcdma_v12
398 struct chreWwanCellInfoWcdma {
399     struct chreWwanCellIdentityWcdma    cellIdentityWcdma;
400     struct chreWwanSignalStrengthWcdma  signalStrengthWcdma;
401 };
402 
403 //! Reference: RIL_CellInfoCdma
404 struct chreWwanCellInfoCdma {
405     struct chreWwanCellIdentityCdma    cellIdentityCdma;
406     struct chreWwanSignalStrengthCdma  signalStrengthCdma;
407     struct chreWwanSignalStrengthEvdo  signalStrengthEvdo;
408 };
409 
410 //! Reference: RIL_CellInfoLte_v12
411 struct chreWwanCellInfoLte {
412     struct chreWwanCellIdentityLte    cellIdentityLte;
413     struct chreWwanSignalStrengthLte  signalStrengthLte;
414 };
415 
416 //! Reference: RIL_CellInfoTdscdma
417 struct chreWwanCellInfoTdscdma {
418     struct chreWwanCellIdentityTdscdma    cellIdentityTdscdma;
419     struct chreWwanSignalStrengthTdscdma  signalStrengthTdscdma;
420 };
421 
422 //! Reference: android.hardware.radio@1.4 CellInfoNr
423 //! @since v1.4
424 struct chreWwanCellInfoNr {
425     struct chreWwanCellIdentityNr    cellIdentityNr;
426     struct chreWwanSignalStrengthNr  signalStrengthNr;
427 };
428 
429 //! Reference: RIL_CellInfoType
430 //! All other values are reserved and should be ignored by nanoapps.
431 enum chreWwanCellInfoType {
432     CHRE_WWAN_CELL_INFO_TYPE_GSM      = 1,
433     CHRE_WWAN_CELL_INFO_TYPE_CDMA     = 2,
434     CHRE_WWAN_CELL_INFO_TYPE_LTE      = 3,
435     CHRE_WWAN_CELL_INFO_TYPE_WCDMA    = 4,
436     CHRE_WWAN_CELL_INFO_TYPE_TD_SCDMA = 5,
437     CHRE_WWAN_CELL_INFO_TYPE_NR       = 6,  //! @since v1.4
438 };
439 
440 //! Reference: RIL_TimeStampType
441 enum chreWwanCellTimeStampType {
442     CHRE_WWAN_CELL_TIMESTAMP_TYPE_UNKNOWN  = 0,
443     CHRE_WWAN_CELL_TIMESTAMP_TYPE_ANTENNA  = 1,
444     CHRE_WWAN_CELL_TIMESTAMP_TYPE_MODEM    = 2,
445     CHRE_WWAN_CELL_TIMESTAMP_TYPE_OEM_RIL  = 3,
446     CHRE_WWAN_CELL_TIMESTAMP_TYPE_JAVA_RIL = 4,
447 };
448 
449 //! Reference: RIL_CellInfo_v12
450 struct chreWwanCellInfo {
451     //! Timestamp in nanoseconds; must be in the same time base as chreGetTime()
452     uint64_t timeStamp;
453 
454     //! A value from enum {@link #CellInfoType} indicating the radio access
455     //! technology of the cell, and which field in union CellInfo can be used
456     //! to retrieve additional information
457     uint8_t cellInfoType;
458 
459     //! A value from enum {@link #CellTimeStampType} that identifies the source
460     //! of the value in timeStamp. This is typically set to
461     //! CHRE_WWAN_CELL_TIMESTAMP_TYPE_OEM_RIL, and indicates the time given by
462     //! chreGetTime() that an intermediate module received the data from the
463     //! modem and forwarded it to the requesting CHRE client.
464     uint8_t timeStampType;
465 
466     //! !0 if this cell is registered, 0 if not registered
467     uint8_t registered;
468 
469     //! Reserved for future use; must be set to 0
470     uint8_t reserved;
471 
472     //! The value in cellInfoType indicates which field in this union is valid
473     union chreWwanCellInfoPerRat {
474         struct chreWwanCellInfoGsm     gsm;
475         struct chreWwanCellInfoCdma    cdma;
476         struct chreWwanCellInfoLte     lte;
477         struct chreWwanCellInfoWcdma   wcdma;
478         struct chreWwanCellInfoTdscdma tdscdma;
479         struct chreWwanCellInfoNr      nr;  //! @since v1.4
480     } CellInfo;
481 };
482 
483 /**
484  * Data structure provided with events of type CHRE_EVENT_WWAN_CELL_INFO_RESULT.
485  */
486 struct chreWwanCellInfoResult {
487     //! Indicates the version of the structure, for compatibility purposes.
488     //! Clients do not normally need to worry about this field; the CHRE
489     //! implementation guarantees that the client only receives the structure
490     //! version it expects.
491     uint8_t version;
492 
493     //! Populated with a value from enum {@link #chreError}, indicating whether
494     //! the request failed, and if so, provides the cause of the failure
495     uint8_t errorCode;
496 
497     //! The number of valid entries in cells[]
498     uint8_t cellInfoCount;
499 
500     //! Reserved for future use; must be set to 0
501     uint8_t reserved;
502 
503     //! Set to the cookie parameter given to chreWwanGetCellInfoAsync()
504     const void *cookie;
505 
506     //! Pointer to an array of cellInfoCount elements containing information
507     //! about serving and neighbor cells
508     const struct chreWwanCellInfo *cells;
509 };
510 
511 
512 /**
513  * Retrieves a set of flags indicating the WWAN features supported by the
514  * current CHRE implementation. The value returned by this function must be
515  * consistent for the entire duration of the Nanoapp's execution.
516  *
517  * The client must allow for more flags to be set in this response than it knows
518  * about, for example if the implementation supports a newer version of the API
519  * than the client was compiled against.
520  *
521  * @return A bitmask with zero or more CHRE_WWAN_CAPABILITIES_* flags set
522  *
523  * @since v1.1
524  */
525 uint32_t chreWwanGetCapabilities(void);
526 
527 /**
528  * Query information about the current serving cell and its neighbors. This does
529  * not perform a network scan, but should return state from the current network
530  * registration data stored in the cellular modem. This is effectively the same
531  * as a request for RIL_REQUEST_GET_CELL_INFO_LIST in the RIL.
532  *
533  * The requested cellular information is returned asynchronously via
534  * CHRE_EVENT_WWAN_CELL_INFO_RESULT. The implementation must send this event,
535  * either with successful data or an error status, within
536  * CHRE_ASYNC_RESULT_TIMEOUT_NS.
537  *
538  * If the airplane mode setting is enabled at the Android level, the CHRE
539  * implementation is expected to return a successful asynchronous result with an
540  * empty cell info list.
541  *
542  * @param cookie An opaque value that will be included in the chreAsyncResult
543  *        sent in relation to this request.
544  *
545  * @return true if the request was accepted for processing, false otherwise
546  *
547  * @since v1.1
548  */
549 bool chreWwanGetCellInfoAsync(const void *cookie);
550 
551 /**
552  * Helper accessor for nci in the chreWwanCellIdentityNr struct.
553  *
554  * @return nci or INT64_MAX if invalid/unreported.
555  *
556  * @see chreWwanCellIdentityNr
557  *
558  * @since v1.4
559  */
chreWwanUnpackNrNci(const struct chreWwanCellIdentityNr * nrCellId)560 static inline int64_t chreWwanUnpackNrNci(
561     const struct chreWwanCellIdentityNr *nrCellId) {
562   return (int64_t) (((uint64_t) nrCellId->nci1 << 32) | nrCellId->nci0);
563 }
564 
565 #ifdef __cplusplus
566 }
567 #endif
568 
569 #endif  /* _CHRE_WWAN_H_ */
570