AT: Better failure handling and removed wrong unlock
[hayes-ril.git] / device.c
1 /*
2  * This file is part of hayes-ril.
3  *
4  * Copyright (C) 2012 Paul Kocialkowski <contact@paulk.fr>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <pthread.h>
20
21 #define LOG_TAG "RIL-DEV"
22 #include <utils/Log.h>
23
24 #include <hayes-ril.h>
25
26 /*
27  * Global
28  */
29
30 int ril_device_init(struct ril_device *ril_device_p)
31 {
32         int rc;
33
34         rc = ril_device_data_create(ril_device_p);
35         if(rc < 0) {
36                 LOGE("Failed to create device data!");
37                 return -1;
38         }
39
40         rc = ril_device_power_on(ril_device_p);
41         if(rc < 0) {
42                 LOGE("Failed to power on device!");
43                 return -1;
44         }
45
46         rc = ril_device_power_boot(ril_device_p);
47         if(rc < 0) {
48                 LOGE("Failed to boot device!");
49                 return -1;
50         }
51
52         rc = ril_device_transport_open(ril_device_p);
53         if(rc < 0) {
54                 LOGE("Failed to open device!");
55                 return -1;
56         }
57
58         return 0;
59 }
60
61 int ril_device_setup(struct ril_device *ril_device_p)
62 {
63         int rc;
64
65         rc = ril_device_at_setup(ril_device_p);
66         if(rc < 0) {
67                 LOGE("Failed to setup device!");
68                 return -1;
69         }
70
71         return 0;
72 }
73
74 int ril_device_deinit(struct ril_device *ril_device_p)
75 {
76         ril_device_transport_close(ril_device_p);
77
78         ril_device_power_off(ril_device_p);
79
80         ril_device_data_destroy(ril_device_p);
81
82         return 0;
83 }
84
85 int ril_device_data_create(struct ril_device *ril_device_p)
86 {
87         int rc;
88
89         if(ril_device_p->handlers == NULL) {
90                 LOGE("Missing device handlers!");
91                 return -1;
92         }
93
94         if(ril_device_p->handlers->power == NULL) {
95                 LOGE("Missing device power handlers!");
96                 return -1;
97         }
98
99         if(ril_device_p->handlers->power->sdata_create == NULL) {
100                 LOGE("Missing device power data handlers!");
101                 return -1;
102         }
103
104         LOGD("Creating data for power handlers...");
105         rc = ril_device_p->handlers->power->sdata_create(&ril_device_p->handlers->power->sdata);
106         if(rc < 0) {
107                 LOGE("Creating data for power handlers failed!");
108                 return -1;
109         }
110
111         if(ril_device_p->handlers->transport == NULL) {
112                 LOGE("Missing device transport handlers!");
113                 return -1;
114         }
115
116         if(ril_device_p->handlers->transport->sdata_create == NULL) {
117                 LOGE("Missing device transport data handlers!");
118                 return -1;
119         }
120
121         LOGD("Creating data for transport handlers...");
122         ril_device_p->handlers->transport->sdata_create(&ril_device_p->handlers->transport->sdata);
123         if(rc < 0) {
124                 LOGE("Creating data for transport handlers failed!");
125                 return -1;
126         }
127
128         LOGD("Creating mutex for transport handlers...");
129         pthread_mutex_init(&(ril_device_p->handlers->transport->mutex), NULL);
130
131         // Missing AT handlers is not fatal
132         if(ril_device_p->handlers->at == NULL) {
133                 LOGE("Missing device AT handlers!");
134                 return 0;
135         }
136
137         if(ril_device_p->handlers->at->sdata_create == NULL) {
138                 LOGE("Missing device AT data handlers!");
139                 return 0;
140         }
141
142         LOGD("Creating data for AT handlers...");
143         ril_device_p->handlers->at->sdata_create(&ril_device_p->handlers->at->sdata);
144         if(rc < 0) {
145                 LOGE("Creating data for AT handlers failed!");
146                 return -1;
147         }
148
149         return 0;
150 }
151
152 int ril_device_data_destroy(struct ril_device *ril_device_p)
153 {
154         int rc;
155
156         if(ril_device_p->handlers == NULL) {
157                 LOGE("Missing device handlers!");
158                 return -1;
159         }
160
161         if(ril_device_p->handlers->power == NULL) {
162                 LOGE("Missing device power handlers!");
163                 return -1;
164         }
165
166         if(ril_device_p->handlers->power->sdata_destroy == NULL) {
167                 LOGE("Missing device power data handlers!");
168                 return -1;
169         }
170
171         LOGD("Destroying data for power handlers...");
172         rc = ril_device_p->handlers->power->sdata_destroy(ril_device_p->handlers->power->sdata);
173         if(rc < 0) {
174                 LOGE("Destroying data for power handlers failed!");
175                 return -1;
176         }
177
178         if(ril_device_p->handlers->transport == NULL) {
179                 LOGE("Missing device transport handlers!");
180                 return -1;
181         }
182
183         if(ril_device_p->handlers->transport->sdata_destroy == NULL) {
184                 LOGE("Missing device transport data handlers!");
185                 return -1;
186         }
187
188         LOGD("Destroying data for transport handlers...");
189         ril_device_p->handlers->transport->sdata_destroy(ril_device_p->handlers->transport->sdata);
190         if(rc < 0) {
191                 LOGE("Destroying data for transport handlers failed!");
192                 return -1;
193         }
194
195         LOGD("Destroying mutex for transport handlers...");
196         pthread_mutex_destroy(&(ril_device_p->handlers->transport->mutex));
197
198         // Missing AT handlers is not fatal
199         if(ril_device_p->handlers->at == NULL) {
200                 LOGE("Missing device AT handlers!");
201                 return 0;
202         }
203
204         if(ril_device_p->handlers->at->sdata_create == NULL) {
205                 LOGE("Missing device AT data handlers!");
206                 return 0;
207         }
208
209         LOGD("Creating data for AT handlers...");
210         ril_device_p->handlers->at->sdata_destroy(ril_device_p->handlers->at->sdata);
211         if(rc < 0) {
212                 LOGE("Destroying data for AT handlers failed!");
213                 return -1;
214         }
215
216         return 0;
217 }
218
219 /*
220  * Power
221  */
222
223 int ril_device_power_on(struct ril_device *ril_device_p)
224 {
225         int rc;
226
227         if(ril_device_p->handlers == NULL) {
228                 LOGE("Missing device handlers!");
229                 return -1;
230         }
231
232         if(ril_device_p->handlers->power == NULL) {
233                 LOGE("Missing device power handlers!");
234                 return -1;
235         }
236
237         if(ril_device_p->handlers->power->power_on == NULL) {
238                 LOGE("Missing device power on handler!");
239                 return -1;
240         }
241
242         LOGD("Powering modem on...");
243
244         rc = ril_device_p->handlers->power->power_on(ril_device_p->handlers->power->sdata);
245         return rc;
246 }
247
248 int ril_device_power_off(struct ril_device *ril_device_p)
249 {
250         int rc;
251
252         if(ril_device_p->handlers == NULL) {
253                 LOGE("Missing device handlers!");
254                 return -1;
255         }
256
257         if(ril_device_p->handlers->power == NULL) {
258                 LOGE("Missing device power handlers!");
259                 return -1;
260         }
261
262         if(ril_device_p->handlers->power->power_off == NULL) {
263                 LOGE("Missing device power off handler!");
264                 return -1;
265         }
266
267         LOGD("Powering modem off...");
268
269         rc = ril_device_p->handlers->power->power_off(ril_device_p->handlers->power->sdata);
270         return rc;
271 }
272
273 int ril_device_power_boot(struct ril_device *ril_device_p)
274 {
275         int rc;
276
277         if(ril_device_p->handlers == NULL) {
278                 LOGE("Missing device handlers!");
279                 return -1;
280         }
281
282         if(ril_device_p->handlers->power == NULL) {
283                 LOGE("Missing device power handlers!");
284                 return -1;
285         }
286
287         if(ril_device_p->handlers->power->boot == NULL) {
288                 LOGE("Missing device power boot handler!");
289                 return -1;
290         }
291
292         LOGD("Booting modem...");
293
294         rc = ril_device_p->handlers->power->boot(ril_device_p->handlers->power->sdata);
295         return rc;
296 }
297
298 /*
299  * Transport
300  */
301
302
303 int ril_device_transport_open(struct ril_device *ril_device_p)
304 {
305         int rc;
306
307         if(ril_device_p->handlers == NULL) {
308                 LOGE("Missing device handlers!");
309                 return -1;
310         }
311
312         if(ril_device_p->handlers->transport == NULL) {
313                 LOGE("Missing device transport handlers!");
314                 return -1;
315         }
316
317         if(ril_device_p->handlers->transport->open == NULL) {
318                 LOGE("Missing device transport open handler!");
319                 return -1;
320         }
321
322         LOGD("Opening modem...");
323
324         rc = ril_device_p->handlers->transport->open(ril_device_p->handlers->transport->sdata);
325         return rc;
326 }
327
328 int ril_device_transport_close(struct ril_device *ril_device_p)
329 {
330         int rc;
331
332         if(ril_device_p->handlers == NULL) {
333                 LOGE("Missing device handlers!");
334                 return -1;
335         }
336
337         if(ril_device_p->handlers->transport == NULL) {
338                 LOGE("Missing device transport handlers!");
339                 return -1;
340         }
341
342         if(ril_device_p->handlers->transport->close == NULL) {
343                 LOGE("Missing device transport close handler!");
344                 return -1;
345         }
346
347         LOGD("Closing modem...");
348
349         rc = ril_device_p->handlers->transport->close(ril_device_p->handlers->transport->sdata);
350         return rc;
351 }
352
353 int ril_device_transport_send(struct ril_device *ril_device_p, void *data, int length)
354 {
355         int rc;
356
357         if(ril_device_p->handlers == NULL) {
358                 LOGE("Missing device handlers!");
359                 return -1;
360         }
361
362         if(ril_device_p->handlers->transport == NULL) {
363                 LOGE("Missing device transport handlers!");
364                 return -1;
365         }
366
367         if(ril_device_p->handlers->transport->send == NULL) {
368                 LOGE("Missing device transport send handler!");
369                 return -1;
370         }
371
372         RIL_DEVICE_LOCK(ril_device_p);
373         rc = ril_device_p->handlers->transport->send(ril_device_p->handlers->transport->sdata, data, length);
374         RIL_DEVICE_UNLOCK(ril_device_p);
375
376         return rc;
377 }
378
379 int ril_device_transport_recv(struct ril_device *ril_device_p, void **data, int length)
380 {
381         int rc;
382
383         if(ril_device_p->handlers == NULL) {
384                 LOGE("Missing device handlers!");
385                 return -1;
386         }
387
388         if(ril_device_p->handlers->transport == NULL) {
389                 LOGE("Missing device transport handlers!");
390                 return -1;
391         }
392
393         if(ril_device_p->handlers->transport->recv == NULL) {
394                 LOGE("Missing device transport recv handler!");
395                 return -1;
396         }
397
398         RIL_DEVICE_LOCK(ril_device_p);
399         rc = ril_device_p->handlers->transport->recv(ril_device_p->handlers->transport->sdata, data, length);
400         RIL_DEVICE_UNLOCK(ril_device_p);
401
402         return rc;
403 }
404
405 int ril_device_transport_recv_poll(struct ril_device *ril_device_p)
406 {
407         int rc;
408
409         if(ril_device_p->handlers == NULL) {
410                 LOGE("Missing device handlers!");
411                 return -1;
412         }
413
414         if(ril_device_p->handlers->transport == NULL) {
415                 LOGE("Missing device transport handlers!");
416                 return -1;
417         }
418
419         if(ril_device_p->handlers->transport->recv_poll == NULL) {
420                 LOGE("Missing device transport recv poll handler!");
421                 return -1;
422         }
423
424         rc = ril_device_p->handlers->transport->recv_poll(ril_device_p->handlers->transport->sdata);
425         return rc;
426 }
427
428 int ril_device_transport_recv_loop(struct ril_device *ril_device_p)
429 {
430         struct at_response **responses = NULL;
431         int responses_count = 0;
432         int responses_queued = 0;
433
434         int failures = 0;
435         int run = 1;
436
437         char *data = NULL;
438         int length = 0;
439         int rc;
440         int i;
441
442 run_loop:
443         while(run) {
444                 rc = ril_device_transport_recv_poll(ril_device_p);
445                 if(rc < 0) {
446                         LOGE("Polling from transport recv failed!");
447                         break;
448                 }
449
450                 length = rc;
451
452                 rc = ril_device_transport_recv(ril_device_p, &data, length);
453                 if(rc <= 0 || data == NULL) {
454                         LOGE("Reading from transport recv failed!");
455                         break;
456                 }
457
458                 length = rc;
459
460                 // Read works now
461                 if(failures)
462                         failures = 0;
463
464                 responses_count = at_responses_process(&responses, data, length);
465                 responses_queued = 0;
466
467                 // Log response
468                 RIL_LOG_LOCK();
469                 ril_data_log(data, length);
470                 RIL_LOG_UNLOCK();
471
472                 // Good way to give the recv loop some sleep: call at_send_expect_status
473                 for(i=0 ; i < responses_count ; i++) {
474                         // Log response
475                         RIL_LOG_LOCK();
476                         ril_recv_log(responses[i]);
477                         RIL_LOG_UNLOCK();
478
479                         // Handle sync
480                         rc = at_sync_response_dequeue(responses[i]);
481                         if(rc < 0) {
482                                 rc = at_response_queue(responses[i]);
483                                 if(rc < 0) {
484                                         LOGE("Failed to queue response, clearing!");
485                                         at_response_free(responses[i]);
486                                 } else {
487                                         responses_queued = 1;
488                                 }
489                         }
490                 }
491
492                 // Unlock the queue after adding all the requests to the queue (if any)
493                 if(responses_queued)
494                         at_responses_queue_unlock();
495
496                 free(data);
497         }
498
499         LOGE("RIL device transport recv loop stopped!");
500         failures++;
501
502         // Freeze all the sent requests
503         at_freeze_start();
504
505         if(failures < 4) {
506                 LOGD("Reopening transport...");
507
508                 ril_device_transport_close(ril_device_p);
509         } else if(failures < 7) {
510                 LOGD("Powering off and on again...");
511
512                 ril_device_transport_close(ril_device_p);
513                 ril_device_power_off(ril_device_p);
514                 ril_device_power_on(ril_device_p);
515
516                 LOGD("Reopening transport...");
517         }
518
519         rc = ril_device_transport_open(ril_device_p);
520         if(rc < 0) {
521                 LOGE("Unable to reopen transport!");
522                 at_freeze_stop();
523                 run = 0;
524         } else {
525                 at_freeze_send();
526                 run = 1;
527         }
528
529         RIL_DEVICE_UNLOCK(ril_device_p);
530
531         goto run_loop;
532 }
533
534 void *ril_device_transport_recv_thread(void *data)
535 {
536         struct ril_device *ril_device_p = (struct ril_device *) data;
537         int rc;
538         int i;
539
540         rc = ril_device_transport_recv_loop(ril_device_p);
541         LOGE("RIL device transport recv thread returned with %d, aborting!", rc);
542
543         ril_device_deinit(ril_device_p);
544         at_freeze_set(AT_FREEZE_ON);
545
546         return NULL;
547 }
548
549 int ril_device_transport_recv_thread_start(struct ril_device *ril_device_p)
550 {
551         pthread_attr_t attr;
552         int rc;
553
554         if(ril_device_p->handlers == NULL) {
555                 LOGE("Missing device handlers!");
556                 return -1;
557         }
558
559         if(ril_device_p->handlers->transport == NULL) {
560                 LOGE("Missing device transport handlers!");
561                 return -1;
562         }
563
564         pthread_attr_init(&attr);
565         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
566
567         rc = pthread_create(&(ril_device_p->handlers->transport->recv_thread), &attr, ril_device_transport_recv_thread, (void *) ril_device_p);
568         if(rc != 0) {
569                 LOGE("Creating transport recv thread failed!");
570                 return -1;
571         }
572
573         return 0;
574 }
575
576 /*
577  * AT
578  */
579
580 int ril_device_at_power_on(struct ril_device *ril_device_p)
581 {
582         int rc;
583
584         if(ril_device_p->handlers == NULL) {
585                 LOGE("Missing device handlers!");
586                 return -1;
587         }
588
589         // Missing AT handlers is not fatal
590         if(ril_device_p->handlers->at == NULL) {
591                 LOGE("Missing device AT handlers!");
592                 return 0;
593         }
594
595         if(ril_device_p->handlers->at->power_on == NULL) {
596                 LOGE("Missing device AT power on handler!");
597                 return 0;
598         }
599
600         rc = ril_device_p->handlers->at->power_on(ril_device_p->handlers->at->sdata);
601         return rc;
602 }
603
604 int ril_device_at_power_off(struct ril_device *ril_device_p)
605 {
606         int rc;
607
608         if(ril_device_p->handlers == NULL) {
609                 LOGE("Missing device handlers!");
610                 return -1;
611         }
612
613         // Missing AT handlers is not fatal
614         if(ril_device_p->handlers->at == NULL) {
615                 LOGE("Missing device AT handlers!");
616                 return 0;
617         }
618
619         if(ril_device_p->handlers->at->power_off == NULL) {
620                 LOGE("Missing device AT power off handler!");
621                 return 0;
622         }
623
624         rc = ril_device_p->handlers->at->power_off(ril_device_p->handlers->at->sdata);
625         return rc;
626 }
627
628 int ril_device_at_setup(struct ril_device *ril_device_p)
629 {
630         int status;
631         int rc;
632
633         // Builtin
634
635         // Echo enabled, send results, verbose enabled
636         at_send_expect_status("ATE1Q0V1", NULL);
637
638         // Extended errors
639         at_send_expect_status("AT+CMEE=1", NULL);
640
641         // Detailed rings, service reporting
642         at_send_expect_status("AT+CRC=1;+CR=1", NULL);
643
644         // SMS PDU mode
645         at_send_expect_status("AT+CMGF=0", NULL);
646
647         // Network registration notifications
648         status = at_send_expect_status("AT+CREG=2", NULL);
649         if(at_status_error(status)) {
650                 LOGD("Modem doesn't support AT+CREG=2");
651                 at_send_expect_status("AT+CREG=1", NULL);
652         }
653
654         // Handler
655
656         if(ril_device_p->handlers == NULL) {
657                 LOGE("Missing device handlers!");
658                 return -1;
659         }
660
661         // Missing AT handlers is not fatal
662         if(ril_device_p->handlers->at == NULL) {
663                 LOGE("Missing device AT handlers!");
664                 return 0;
665         }
666
667         if(ril_device_p->handlers->at->setup == NULL) {
668                 LOGE("Missing device AT setup handler!");
669                 return 0;
670         }
671
672         rc = ril_device_p->handlers->at->setup(ril_device_p->handlers->at->sdata);
673         return rc;
674 }