summaryrefslogtreecommitdiffstats
path: root/Replicant_contributors_meeting_27_28_July_2019_Paris_France/modems/Replicant_and_modems_Introduction.tex
blob: a7006f04926b3ed5110ed38b424c05e31b43fd66 (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
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
\documentclass{beamer}
\usepackage[english]{babel}
\usepackage{color}
\usepackage{graphicx}
\usepackage{ifthen}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{pdfpages}

\lstdefinestyle{terminal}{
  backgroundcolor=\color{black},
  basicstyle=\scriptsize\color{green},
}

%% Based on:
%% https://tex.stackexchange.com/questions/136900/insert-a-full-page-image
\newcommand{\pictureframe}[1] {
  {
    \begin{frame}
      \noindent
      \resizebox{\textwidth}{\textheight}
                {\includegraphics{#1}}
                \hspace*{-\textwidth}
    \end{frame}
  }
}

\usetheme{Singapore}

\title{Modems support in Replicant}
\author{Denis 'GNUtoo' Carikli}
\date{\today}

\begin{document}

\maketitle

%% TODO: Using \itemize{} fails to compile
%% TODO: convert \center frames to chapter title

\begin{frame}
  \center{In this presentation:}
  \begin{itemize}
  \item The hardware
  \item Android Reference implementation (More simple)
  \item Questions and/or Pause
  \item Replicant's Samsung IPC implementation (More complex)
  \end{itemize}
\end{frame}

\begin{frame}
  \center{The hardware}
\end{frame}

\pictureframe{../../external_resources/replicant_website/images/freedom-privacy-security-issues/hardware.png}
\pictureframe{../../external_resources/replicant_website/images/freedom-privacy-security-issues/good-modem-isolation.png}

\pictureframe{output/example_modem.png}

\begin{frame}
  \center{Why do we look at android reference implementation first?}
  \begin{itemize}
  \item The hardware is more simple
  \item The protocol is way easier to understand
  \item Has good enough documentation (standard, reference implentation)
  \item It also explains why we got protocols like samsung-ipc
  \item Relevant for devices with free software bootloaders
  \end{itemize}
\end{frame}

\begin{frame}
  \center{Example: Openmoko}
  \begin{itemize}
  \item Simple
  \item But very strongly outdated
  \end{itemize}
\end{frame}

\pictureframe{output/serial_modem_hardware.png}

\begin{frame}
  \center{AT commands}
  \begin{itemize}
    \item The good parts:
      \begin{itemize}
      \item Standard (ETSI GSM 07.07 / 3GPP TS 27.007)
      \item Publically available, no registration
      \item pdf versions: git://git.osmocom.org/3gpp-etsi-pdf-links.git
      \end{itemize}
      \item The bad parts: More on that later...
  \end{itemize}
\end{frame}

\begin{frame}
  \center{Examples based on the specification (07.07)}
\end{frame}

\begin{lstlisting}[style=terminal]
  # use verbose error values, report registration
  > AT+CMEE=2;+CREG=1
  < OK
  > AT
  < OK
  > AT+CFUN=1
  < OK
\end{lstlisting}

\begin{frame}
  \center{More complex example}
\end{frame}

\begin{lstlisting}[style=terminal]
  # use verbose error values, report registration
  > AT+CMEE=2;+CREG=1
  < OK
  > AT+CFUN=1
  < +CME ERROR: SIM PIN required
  > AT+CPIN="1234"
  < +CME ERROR: incorrect password (user entered wrong PIN)
  > AT+CPIN="4321"
  < OK
  # Automatic registration to an operator
  # +COPS: <mode>[,<format>,<oper>]
  # mode 0: automatic (<oper> field is ignored)
  # format 0: long format alphanumeric <oper>
  > AT+COPS=0,0
  < OK
  < +CREG: 1
  > AT+COPS?
  < +COPS: 0,0,"SFR"
  > OK
\end{lstlisting}

\begin{frame}
  \center{AT commands: The bad parts:}
  \begin{itemize}
  \item Syncronous
  \item difficult to write parsers (need to keep state)
  \item Slow
  \item Vendors extensions
  \end{itemize}
\end{frame}

\begin{frame}
  \center{More realistic example}
\end{frame}

\begin{frame}
  \center{Standard not respected}
\end{frame}

\begin{lstlisting}[style=terminal]
  # 0707: +CSQ: <rssi>,<ber>
  # MDM6200/6600: +CSQ: <N>
  # 0707: 31 = -51dbm
  # MDM6200/6600: 31 = -75dbm
  > +CSQ: 31
  < +CRING: VOICE/06050403002
\end{lstlisting}

\begin{frame}
  \center{Vendor specific commands}
\end{frame}

\begin{lstlisting}[style=terminal]
  # Enable noise cancelation on the OpenMoko
  > AT%N0105
  < OK
\end{lstlisting}

\begin{frame}
  \center{Example of issues}
  \begin{itemize}
  \item What if the answer doesn't come back?
  \item What if there is some noise on the serial port?
  \item What if I need to run another commands while waiting for the answer of the previous one?
  \item More modern modems with AT protocol (Example: GTA04):
    \begin{itemize}
    \item Modem connected over USB
    \item Serveral "virtual serial" interfaces
    \end{itemize}
    \end{itemize}
\end{frame}

\begin{frame}
  \center{The bad parts:}
  \begin{itemize}
  \item $\rightarrow$This lead modem and device vendors to make their own protocol.
  \item $\rightarrow$Very similar issues with GPS (NMEA not meant for
    GPS $\rightarrow$ custom protocols by modem chip vendors (UBlox, SIRF, etc)).
  \end{itemize}
\end{frame}

\begin{frame}
  \center{AT commands are still in use:}
  \begin{itemize}
  \item GTA04
  \item Optimus black
  \item Usually (also) available on modems available at low quantity orders.
  \item $\rightarrow$Often in use on devices made for the free software community .
  \item Sometimes vendor documentation is even publically available for specific modems.
  \end{itemize}
\end{frame}

\begin{frame}
  \center{Handling AT command set in Android}
\end{frame}

\pictureframe{output/serial_modem_software.png}

\begin{frame}
  \center{rild}
\end{frame}

\begin{frame}
  \center{finding libril implementation: hardware/ril/rild/rild.c}
\end{frame}

\lstset{language=C}
\begin{lstlisting}
  int main(int argc, char **argv) {
    // vendor ril lib path either passed in as
    // -l parameter, or read from rild.libpath
    // property
    const char *rilLibPath = NULL;
    // ril arguments either passed in
    // as -- parameter, or read from rild.libargs
    // property
    ...
  }
\end{lstlisting}

\begin{frame}
\center{finding libril implementation}
\end{frame}

\begin{lstlisting}[style=terminal]
  $ cd device/samsung/i9300
  $ git grep rild
  system.prop:rild.libpath=/system/lib/hw/libsamsung-ril.so
  system.prop:rild.libargs=-d /dev/ttyS0
\end{lstlisting}

\begin{frame}
  \center{In the code: hardware/ril/rild/rild.c}
\end{frame}

\lstset{language=C}
\begin{lstlisting}
int main(int argc, char **argv) {
  ...
  const RIL_RadioFunctions *(*rilInit)(
        const struct RIL_Env *, int, char **);
  ...
  dlHandle = dlopen(rilLibPath, RTLD_NOW);
  ...
  RIL_startEventLoop();
  ...
  rilInit = (const RIL_RadioFunctions *(*)(
           const struct RIL_Env *, int, char **))
           dlsym(dlHandle, "RIL_Init");
  funcs = rilInit(&s_rilEnv, argc, rilArgv);
  ...
  RIL_register(funcs);
  ...
}
\end{lstlisting}

\begin{frame}
  \center{reference-ril}
  \begin{itemize}
  \item Implements libril, like libsamsung-ril
  \item Same git repository than rild
  \item AT commands, very basic (only one channel)
  \item Beware of CaMeL Case Code and \_ mix (RIL\_Init, not RIL\_init)
  \end{itemize}
\end{frame}

\lstset{language=C}
\begin{lstlisting}
const RIL_RadioFunctions *RIL_Init(
                     const struct RIL_Env *env,
                     int argc, char **argv) {
  while ( -1 != (opt = getopt(argc, argv,
                                "p:d:s:c:"))) {
    ...
    switch (opt) {
      ...
      case 'd':
      s_device_path = optarg;
      RLOGI("Opening tty device %s\n",
            s_device_path);
      break;
      ...
    }
  } ...
}
\end{lstlisting}

\lstset{language=C}
\begin{lstlisting}
  static const RIL_RadioFunctions s_callbacks = {
    RIL_VERSION,
    onRequest,
    currentState,
    onSupports,
    onCancel,
    getVersion
  };
  ...

  const RIL_RadioFunctions *RIL_Init(
                       const struct RIL_Env *env,
                       int argc, char **argv) {
    ...
    return &s_callbacks;
  }
\end{lstlisting}

\begin{frame}
  \center{Calling}
\end{frame}

\lstset{language=C}
\begin{lstlisting}
  static void onRequest (int request, void *data,
  size_t datalen, RIL_Token t) {
    ...
    switch (request) {
      ...
      case RIL_REQUEST_DIAL:
      requestDial(data, datalen, t);
      break;
      case ...
    }
\end{lstlisting}

\begin{frame}
  \center{requestDial}
\end{frame}

\lstset{language=C}
\begin{lstlisting}
  static void requestDial(void *data,
  size_t datalen __unused, RIL_Token t) {
    ...
    ret = at_send_command(cmd, NULL);

    free(cmd);
    ...
    RIL_onRequestComplete(t, RIL_E_SUCCESS,
    NULL, 0);
  }
\end{lstlisting}

\begin{frame}
  \center{Incomming call}
\end{frame}

\begin{frame}
  \center{onUnsolicited}
\end{frame}
\lstset{language=C}
\begin{lstlisting}
   static void onUnsolicited (const char *s,
                             const char *sms_pdu) {
   ...
   if (strStartsWith(s,"+CRING:")
   || strStartsWith(s,"RING")
   || strStartsWith(s,"NO CARRIER")
   || strStartsWith(s,"+CCWA") ) {
     RIL_onUnsolicitedResponse (
     RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
     NULL, 0);
     ...
   }
   ...
 }
\end{lstlisting}

\begin{frame}
  \center{RIL\_Init}
\end{frame}
\lstset{language=C}
\begin{lstlisting}
  const RIL_RadioFunctions *RIL_Init(
  const struct RIL_Env *env,
  int argc, char **argv) {
    ...
    ret = pthread_create(&s_tid_mainloop, &attr,
    mainLoop, NULL);
    ...
  }
\end{lstlisting}

\begin{frame}
  \center{MainLoop}
\end{frame}
\lstset{language=C}
\begin{lstlisting}
  static void * mainLoop(void *param __unused) {
    ...
    ret = at_open(fd, onUnsolicited);
    ...
  }
\end{lstlisting}

\begin{frame}
  \center{at\_open}
\end{frame}
\lstset{language=C}
\begin{lstlisting}
  static ATUnsolHandler s_unsolHandler;
  ...

  int at_open(int fd, ATUnsolHandler h)
  {
    ...
    s_unsolHandler = h;
    ...
    ret = pthread_create(&s_tid_reader, &attr,
                               readerLoop, &attr);
    ...
  }
\end{lstlisting}

\begin{frame}
  \center{readerLoop}
\end{frame}

\lstset{language=C}
\begin{lstlisting}

static void *readerLoop(void *arg __unused)
{
  for (;;) {
    ...
    processLine(line);
    ...
  }
  ...
}
\end{lstlisting}

\begin{frame}
  \center{processLine}
\end{frame}
\lstset{language=C}
\begin{lstlisting}
  static void processLine(const char *line) {
  pthread_mutex_lock(&s_commandmutex);
  if (sp_response == NULL) {
    /* no command pending */
    handleUnsolicited(line);
  } ...
}
\end{lstlisting}

\lstset{language=C}
\begin{lstlisting}
  static void handleUnsolicited(const char *line)
  {
    if (s_unsolHandler != NULL) {
      s_unsolHandler(line, NULL);
    }
  }
\end{lstlisting}

\begin{frame}
  \center{Other protocols}
\end{frame}

\begin{frame}
  \center{Other protocols: status}
  \begin{itemize}
  \item "samsung-ipc"
    \begin{itemize}
    \item Implemented in libsamsung-ipc
    \item Usable on Android and GNU/Linux
    \item Incomplete
    \item No wireshark dissectror
    \item No upstream Linux driver yet
    \end{itemize}
  \item QMI
    \begin{itemize}
      \item Implemented in libqmi, other?
      \item Usable with upstream Linux and in GNU/Linux
    \end{itemize}
  \item ISI (Nokia: N900, N9, etc)
    \begin{itemize}
      \item Implemented in Ofono, Freesmartphone.org, other?
      \item Wireshark dissector
      \item Upstream Linux drivers
    \end{itemize}
  \item "Palm Pre"
    \begin{itemize}
    \item Implemented in msmcomm
    \item Was usable in GNU/Linux through Freesmartphone.org
    \item Code lost? Michael Lauer should push it again soon.
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}
  \center{Other protocols: characteristics}
  \begin{itemize}
  \item Like a network protocol
    \begin{itemize}
    \item sequence number to match query and response
    \item Asyncronous
    \end{itemize}
  \item Free software implementation not always available
  \item Or incomplete (samsung-ipc)
  \end{itemize}
\end{frame}

\begin{frame}
  \center{Use a supported protocol}
  \begin{itemize}
  \item Example with the Palm pre
    \begin{itemize}
    \item Had an unknown protocol that was used by the nonfree default implementation
    \item Also had AT commands
    \item People implemented the AT commands
    \item No ring indication if my memory is correct
    \item At the end they implemented the unknown vendor protocol in msmcommd
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}
\center{Questions?}
  \begin{itemize}
  \item Next part is about Samsung IPC
  \item Increasing level of complexity
  \item Increasing level of complexity
  \item $\rightarrow$ Questions on the first part before continuing.
  \end{itemize}
\end{frame}

\begin{frame}
  Licenses:
  \begin{itemize}
  \item Attribution-ShareAlike 3.0 Unported for the two images that comes from
    the Freedom Privacy and Security page.
  \item \url{https://creativecommons.org/licenses/by-sa/4.0/} for
    the rest.
  \end{itemize}
\end{frame}

\begin{thebibliography}{99}
\end{thebibliography}

\end{document}