Device: Added AT handlers
authorPaul Kocialkowski <contact@paulk.fr>
Thu, 30 Aug 2012 17:22:50 +0000 (19:22 +0200)
committerPaul Kocialkowski <contact@paulk.fr>
Thu, 30 Aug 2012 17:22:50 +0000 (19:22 +0200)
Signed-off-by: Paul Kocialkowski <contact@paulk.fr>
device.c
hayes-ril.c
hayes-ril.h
power.c

index 9872ab8..4e3ddbb 100644 (file)
--- a/device.c
+++ b/device.c
@@ -58,6 +58,19 @@ int ril_device_init(struct ril_device *ril_device_p)
        return 0;
 }
 
+int ril_device_setup(struct ril_device *ril_device_p)
+{
+       int rc;
+
+       rc = ril_device_at_setup(ril_device_p);
+       if(rc < 0) {
+               LOGE("Failed to setup device!");
+               return -1;
+       }
+
+       return 0;
+}
+
 int ril_device_deinit(struct ril_device *ril_device_p)
 {
        ril_device_transport_close(ril_device_p);
@@ -115,6 +128,24 @@ int ril_device_data_create(struct ril_device *ril_device_p)
        LOGD("Creating mutex for transport handlers...");
        pthread_mutex_init(&(ril_device_p->handlers->transport->mutex), NULL);
 
+       // Missing AT handlers is not fatal
+       if(ril_device_p->handlers->at == NULL) {
+               LOGE("Missing device AT handlers!");
+               return 0;
+       }
+
+       if(ril_device_p->handlers->at->sdata_create == NULL) {
+               LOGE("Missing device AT data handlers!");
+               return 0;
+       }
+
+       LOGD("Creating data for AT handlers...");
+       ril_device_p->handlers->at->sdata_create(&ril_device_p->handlers->at->sdata);
+       if(rc < 0) {
+               LOGE("Creating data for AT handlers failed!");
+               return -1;
+       }
+
        return 0;
 }
 
@@ -164,6 +195,24 @@ int ril_device_data_destroy(struct ril_device *ril_device_p)
        LOGD("Destroying mutex for transport handlers...");
        pthread_mutex_destroy(&(ril_device_p->handlers->transport->mutex));
 
+       // Missing AT handlers is not fatal
+       if(ril_device_p->handlers->at == NULL) {
+               LOGE("Missing device AT handlers!");
+               return 0;
+       }
+
+       if(ril_device_p->handlers->at->sdata_create == NULL) {
+               LOGE("Missing device AT data handlers!");
+               return 0;
+       }
+
+       LOGD("Creating data for AT handlers...");
+       ril_device_p->handlers->at->sdata_destroy(ril_device_p->handlers->at->sdata);
+       if(rc < 0) {
+               LOGE("Destroying data for AT handlers failed!");
+               return -1;
+       }
+
        return 0;
 }
 
@@ -511,3 +560,103 @@ int ril_device_transport_recv_thread_start(struct ril_device *ril_device_p)
 
        return 0;
 }
+
+/*
+ * AT
+ */
+
+int ril_device_at_power_on(struct ril_device *ril_device_p)
+{
+       int rc;
+
+       if(ril_device_p->handlers == NULL) {
+               LOGE("Missing device handlers!");
+               return -1;
+       }
+
+       // Missing AT handlers is not fatal
+       if(ril_device_p->handlers->at == NULL) {
+               LOGE("Missing device AT handlers!");
+               return 0;
+       }
+
+       if(ril_device_p->handlers->at->power_on == NULL) {
+               LOGE("Missing device AT power on handler!");
+               return 0;
+       }
+
+       rc = ril_device_p->handlers->at->power_on(ril_device_p->handlers->at->sdata);
+       return rc;
+}
+
+int ril_device_at_power_off(struct ril_device *ril_device_p)
+{
+       int rc;
+
+       if(ril_device_p->handlers == NULL) {
+               LOGE("Missing device handlers!");
+               return -1;
+       }
+
+       // Missing AT handlers is not fatal
+       if(ril_device_p->handlers->at == NULL) {
+               LOGE("Missing device AT handlers!");
+               return 0;
+       }
+
+       if(ril_device_p->handlers->at->power_off == NULL) {
+               LOGE("Missing device AT power off handler!");
+               return 0;
+       }
+
+       rc = ril_device_p->handlers->at->power_off(ril_device_p->handlers->at->sdata);
+       return rc;
+}
+
+int ril_device_at_setup(struct ril_device *ril_device_p)
+{
+       int status;
+       int rc;
+
+       // Builtin
+
+       // Echo enabled, send results, verbose enabled
+       at_send_expect_status("ATE1Q0V1", NULL);
+
+       // Extended errors
+       at_send_expect_status("AT+CMEE=1", NULL);
+
+       // Detailed rings, service reporting
+       at_send_expect_status("AT+CRC=1;+CR=1", NULL);
+
+       // SMS PDU mode
+       at_send_expect_status("AT+CMGF=0", NULL);
+
+       // Network registration notifications
+       status = at_send_expect_status("AT+CREG=2", NULL);
+       if(at_status_error(status)) {
+               LOGD("Modem doesn't support AT+CREG=2");
+               at_send_expect_status("AT+CREG=1", NULL);
+       }
+
+       // Handler
+
+       if(ril_device_p->handlers == NULL) {
+               LOGE("Missing device handlers!");
+               return -1;
+       }
+
+       // Missing AT handlers is not fatal
+       if(ril_device_p->handlers->at == NULL) {
+               LOGE("Missing device AT handlers!");
+               return 0;
+       }
+
+       if(ril_device_p->handlers->at->setup == NULL) {
+               LOGE("Missing device AT setup handler!");
+               return 0;
+       }
+
+       rc = ril_device_p->handlers->at->setup(ril_device_p->handlers->at->sdata);
+       return rc;
+}
index 84ac480..d7b2eef 100644 (file)
@@ -225,6 +225,14 @@ const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **a
                return NULL;
        }
 
+       rc = ril_device_setup(ril_device);
+       if(rc < 0) {
+               LOGE("Failed to setup device!");
+               ril_device_deinit(ril_device);
+
+               return NULL;
+       }
+
        LOGD("Initialization complete");
 
        return &ril_ops;
index 8b4a1a2..61a1cec 100644 (file)
@@ -74,9 +74,20 @@ struct ril_device_transport_handlers {
        pthread_mutex_t mutex;
 };
 
+struct ril_device_at_handlers {
+       void *sdata;
+       int (*sdata_create)(void **sdata);
+       int (*sdata_destroy)(void *sdata);
+
+       int (*power_on)(void *sdata);
+       int (*power_off)(void *sdata);
+       int (*setup)(void *sdata);
+};
+
 struct ril_device_handlers {
        struct ril_device_power_handlers *power;
        struct ril_device_transport_handlers *transport;
+       struct ril_device_at_handlers *at;
 };
 
 struct ril_device {
@@ -120,13 +131,15 @@ struct ril_globals {
 // Device
 void ril_device_register(struct ril_device **ril_device_p);
 int ril_device_init(struct ril_device *ril_device_p);
+int ril_device_setup(struct ril_device *ril_device_p);
 int ril_device_deinit(struct ril_device *ril_device_p);
-
 int ril_device_data_create(struct ril_device *ril_device_p);
 int ril_device_data_destroy(struct ril_device *ril_device_p);
+
 int ril_device_power_on(struct ril_device *ril_device_p);
 int ril_device_power_off(struct ril_device *ril_device_p);
 int ril_device_power_boot(struct ril_device *ril_device_p);
+
 int ril_device_transport_open(struct ril_device *ril_device_p);
 int ril_device_transport_close(struct ril_device *ril_device_p);
 int ril_device_transport_send(struct ril_device *ril_device_p, void *data, int length);
@@ -134,6 +147,10 @@ int ril_device_transport_recv(struct ril_device *ril_device_p, void **data, int
 int ril_device_transport_recv_poll(struct ril_device *ril_device_p);
 int ril_device_transport_recv_thread_start(struct ril_device *ril_device_p);
 
+int ril_device_at_power_on(struct ril_device *ril_device_p);
+int ril_device_at_power_off(struct ril_device *ril_device_p);
+int ril_device_at_setup(struct ril_device *ril_device_p);
+
 // Utils
 char *ril_status_string(int status);
 void ril_recv_log(struct at_response *response);
diff --git a/power.c b/power.c
index 97d576b..50ab7a9 100644 (file)
--- a/power.c
+++ b/power.c
@@ -62,34 +62,19 @@ int at_cfun_disable_expect(struct at_response *response, void *data, RIL_Token t
 
 void ril_power_on(void)
 {
-       int status;
-
        LOGD("Powering on");
 
-       // Echo enabled, send results, verbose enabled
-       at_send_expect_status("ATE1Q0V1", NULL);
-
-       // Extended errors
-       at_send_expect_status("AT+CMEE=1", NULL);
-
-       // Detailed rings, service reporting
-       at_send_expect_status("AT+CRC=1;+CR=1", NULL);
-
-       // SMS PDU mode
-       at_send_expect_status("AT+CMGF=0", NULL);
-
-       // Network registration notifications
-       status = at_send_expect_status("AT+CREG=2", NULL);
-       if(at_status_error(status)) {
-               LOGD("Modem doesn't support AT+CREG=2");
-               at_send_expect_status("AT+CREG=1", NULL);
-       }
-
        at_send_expect_to_func("AT+CFUN=1", NULL, NULL, 0x0000, at_cfun_enable_expect);
+
+       ril_device_at_power_on(ril_device);
 }
 
 void ril_power_off(void)
 {
+       LOGD("Powering off");
+
+       ril_device_at_power_off(ril_device);
+
        at_send_expect_to_func("AT+CFUN=0", NULL, NULL, 0x0000, at_cfun_disable_expect);
 }