2 * This file is part of hayes-ril.
4 * Copyright (C) 2012 Paul Kocialkowski <contact@paulk.fr>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <utils/Log.h>
21 #include <telephony/ril.h>
23 #include <hayes-ril.h>
25 #define RIL_VERSION_STRING "Hayes RIL"
27 struct ril_device *ril_device;
28 struct ril_globals ril_globals;
30 struct ril_dispatch_unsol ril_dispatch_unsol[] = {
31 { "AT+CPIN", at_cpin }
34 void ril_on_request(int request, void *data, size_t length, RIL_Token t)
38 case RIL_REQUEST_RADIO_POWER:
39 ril_request_radio_power(t, data, length);
42 case RIL_REQUEST_GET_CURRENT_CALLS:
43 ril_request_get_current_calls(t, data, length);
46 LOGE("Request not implemented: %d\n", request);
47 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
52 const char *ril_get_version(void)
54 return RIL_VERSION_STRING;
57 RIL_RadioState ril_on_state_request(void)
59 LOGD("State request");
60 return ril_globals.radio_state;
63 int ril_on_supports(int requestCode)
68 void ril_on_cancel(RIL_Token t)
73 static const RIL_RadioFunctions ril_ops = {
82 void *ril_dispatch(void *data)
84 struct at_response *response = NULL;
88 for(i = 5 ; i > 0 ; i--) {
90 response = at_response_dequeue();
94 if(response->command == NULL) {
95 at_response_free(response);
103 rc = at_response_expect_to_func(response);
105 for(j=0 ; j < ril_globals.dispatch_unsol_count ; j++) {
106 if(at_commands_compare(response->command, ril_globals.dispatch_unsol[j].command) && ril_globals.dispatch_unsol[j].func != NULL) {
107 ril_globals.dispatch_unsol[j].func(response);
112 at_response_free(response);
115 LOGE("RIL dispatch failed, retrying!");
118 LOGE("RIL dispatch failed too many times, aborting");
123 int ril_dispatch_thread_start(void)
128 pthread_attr_init(&attr);
129 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
131 rc = pthread_create(&ril_globals.dispatch_thread, &attr, ril_dispatch, NULL);
133 LOGE("Creating dispatch thread failed!");
140 void ril_globals_init(void)
142 memset(&ril_globals, 0, sizeof(struct ril_globals));
143 ril_globals.dispatch_unsol = &ril_dispatch_unsol;
144 ril_globals.dispatch_unsol_count = sizeof(ril_dispatch_unsol) / sizeof(struct ril_dispatch_unsol);
146 LOGE("Registered %d unsol requests handlers", ril_globals.dispatch_unsol_count);
148 ril_globals.radio_state = RADIO_STATE_OFF;
150 at_responses_handling_init();
153 const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
158 ril_device_register(&ril_device);
160 LOGD("Starting %s for device: %s", RIL_VERSION_STRING, ril_device->name);
162 rc = ril_device_init(ril_device);
164 LOGE("Failed to init device!");
165 ril_device_deinit(ril_device);
170 rc = ril_device_recv_thread_start(ril_device);
172 LOGE("Failed to start device thread!");
173 ril_device_deinit(ril_device);
178 rc = ril_dispatch_thread_start();
180 LOGE("Failed to start dispatch thread!");
181 ril_device_deinit(ril_device);
186 LOGD("Initialization complete");