Device: Added AT handlers
[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         char *data = NULL;
435         int length = 0;
436         int rc;
437         int i, j;
438
439         // Return error after 5 consecutive poll/read failures
440         for(i=5 ; i > 0 ; i--) {
441                 while(1) {
442                         rc = ril_device_transport_recv_poll(ril_device_p);
443                         if(rc < 0) {
444                                 LOGE("Polling from transport recv failed!");
445                                 break;
446                         }
447
448                         length = rc;
449
450                         rc = ril_device_transport_recv(ril_device_p, &data, length);
451                         if(rc <= 0 || data == NULL) {
452                                 LOGE("Reading from transport recv failed!");
453                                 break;
454                         }
455
456                         length = rc;
457
458                         // Read works now
459                         if(i != 5)
460                                  i = 5;
461
462                         responses_count = at_responses_process(&responses, data, length);
463                         responses_queued = 0;
464
465                         // Log response
466                         RIL_LOG_LOCK();
467                         ril_data_log(data, length);
468                         RIL_LOG_UNLOCK();
469
470                         // Good way to give the recv loop some sleep: call at_send_expect_status
471                         for(j=0 ; j < responses_count ; j++) {
472                                 // Log response
473                                 RIL_LOG_LOCK();
474                                 ril_recv_log(responses[j]);
475                                 RIL_LOG_UNLOCK();
476
477                                 // Handle sync
478                                 rc = at_sync_response_dequeue(responses[j]);
479                                 if(rc < 0) {
480                                         rc = at_response_queue(responses[j]);
481                                         if(rc < 0) {
482                                                 LOGE("Failed to queue response, clearing!");
483                                                 at_response_free(responses[j]);
484                                         } else {
485                                                 responses_queued = 1;
486                                         }
487                                 }
488                         }
489
490                         // Unlock the queue after adding all the requests to the queue (if any)
491                         if(responses_queued)
492                                 at_responses_queue_unlock();
493
494                         free(data);
495                 }
496
497                 // When poll/read failed, close and reopen the device
498                 ril_device_transport_close(ril_device_p);
499                 usleep(500);
500                 ril_device_transport_open(ril_device_p);
501
502                 RIL_DEVICE_UNLOCK(ril_device_p);
503         }
504
505         return -1;
506 }
507
508 void *ril_device_transport_recv_thread(void *data)
509 {
510         struct ril_device *ril_device_p = (struct ril_device *) data;
511         int rc;
512         int i;
513
514         for(i = 5 ; i > 0 ; i--) {
515                 rc = ril_device_transport_recv_loop(ril_device_p);
516                 if(rc < 0) {
517                         LOGE("Recv loop failed too many times, restarting...");
518
519                         rc = ril_device_deinit(ril_device_p);
520                         if(rc < 0)
521                                 break;
522
523                         usleep(500);
524
525                         rc = ril_device_init(ril_device_p);
526                         if(rc < 0)
527                                 break;
528                 }
529         }
530
531         LOGE("Recv thread failed too many times, stopping it all...");
532         ril_device_deinit(ril_device_p);
533
534         return NULL;
535 }
536
537 int ril_device_transport_recv_thread_start(struct ril_device *ril_device_p)
538 {
539         pthread_attr_t attr;
540         int rc;
541
542         if(ril_device_p->handlers == NULL) {
543                 LOGE("Missing device handlers!");
544                 return -1;
545         }
546
547         if(ril_device_p->handlers->transport == NULL) {
548                 LOGE("Missing device transport handlers!");
549                 return -1;
550         }
551
552         pthread_attr_init(&attr);
553         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
554
555         rc = pthread_create(&(ril_device_p->handlers->transport->recv_thread), &attr, ril_device_transport_recv_thread, (void *) ril_device_p);
556         if(rc != 0) {
557                 LOGE("Creating transport recv thread failed!");
558                 return -1;
559         }
560
561         return 0;
562 }
563
564 /*
565  * AT
566  */
567
568 int ril_device_at_power_on(struct ril_device *ril_device_p)
569 {
570         int rc;
571
572         if(ril_device_p->handlers == NULL) {
573                 LOGE("Missing device handlers!");
574                 return -1;
575         }
576
577         // Missing AT handlers is not fatal
578         if(ril_device_p->handlers->at == NULL) {
579                 LOGE("Missing device AT handlers!");
580                 return 0;
581         }
582
583         if(ril_device_p->handlers->at->power_on == NULL) {
584                 LOGE("Missing device AT power on handler!");
585                 return 0;
586         }
587
588         rc = ril_device_p->handlers->at->power_on(ril_device_p->handlers->at->sdata);
589         return rc;
590 }
591
592 int ril_device_at_power_off(struct ril_device *ril_device_p)
593 {
594         int rc;
595
596         if(ril_device_p->handlers == NULL) {
597                 LOGE("Missing device handlers!");
598                 return -1;
599         }
600
601         // Missing AT handlers is not fatal
602         if(ril_device_p->handlers->at == NULL) {
603                 LOGE("Missing device AT handlers!");
604                 return 0;
605         }
606
607         if(ril_device_p->handlers->at->power_off == NULL) {
608                 LOGE("Missing device AT power off handler!");
609                 return 0;
610         }
611
612         rc = ril_device_p->handlers->at->power_off(ril_device_p->handlers->at->sdata);
613         return rc;
614 }
615
616 int ril_device_at_setup(struct ril_device *ril_device_p)
617 {
618         int status;
619         int rc;
620
621         // Builtin
622
623         // Echo enabled, send results, verbose enabled
624         at_send_expect_status("ATE1Q0V1", NULL);
625
626         // Extended errors
627         at_send_expect_status("AT+CMEE=1", NULL);
628
629         // Detailed rings, service reporting
630         at_send_expect_status("AT+CRC=1;+CR=1", NULL);
631
632         // SMS PDU mode
633         at_send_expect_status("AT+CMGF=0", NULL);
634
635         // Network registration notifications
636         status = at_send_expect_status("AT+CREG=2", NULL);
637         if(at_status_error(status)) {
638                 LOGD("Modem doesn't support AT+CREG=2");
639                 at_send_expect_status("AT+CREG=1", NULL);
640         }
641
642         // Handler
643
644         if(ril_device_p->handlers == NULL) {
645                 LOGE("Missing device handlers!");
646                 return -1;
647         }
648
649         // Missing AT handlers is not fatal
650         if(ril_device_p->handlers->at == NULL) {
651                 LOGE("Missing device AT handlers!");
652                 return 0;
653         }
654
655         if(ril_device_p->handlers->at->setup == NULL) {
656                 LOGE("Missing device AT setup handler!");
657                 return 0;
658         }
659
660         rc = ril_device_p->handlers->at->setup(ril_device_p->handlers->at->sdata);
661         return rc;
662 }