Move set audio path api here
[adaptation/intel_mfld/tel-plugin-mfld-blackbay.git] / src / desc_imc_pr3.c
1 /*
2  *
3  *  tel-plugin-imc-pr3
4  *
5  *  Copyright (C) 2012  Intel Corporation. All rights reserved.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28
29 #include <glib.h>
30
31 #include <tcore.h>
32 #include <plugin.h>
33
34 #include <user_request.h>
35 #include <hal.h>
36
37 #include "tty_imc.h"
38 #include "pr3_audio.h"
39
40 #define BUF_LEN_MAX 512
41
42 struct custom_data {
43         int tty_fd;
44         guint watch_id_tty;
45 };
46
47 static TReturn hal_power(TcoreHal *hal, gboolean flag)
48 {
49         struct custom_data *user_data;
50
51         user_data = tcore_hal_ref_user_data(hal);
52         if (!user_data)
53                 return TCORE_RETURN_FAILURE;
54
55         /* power on */
56         if (flag == TRUE) {
57                 dbg("Phone Power On success (fd:%d)", user_data->tty_fd);
58
59                 tcore_hal_set_power_state(hal, TRUE);
60         /* power off */
61         } else {
62                 dbg("Phone Power Off success.");
63
64                 tcore_hal_set_power_state(hal, FALSE);
65         }
66
67         return TCORE_RETURN_SUCCESS;
68 }
69
70
71 static TReturn hal_send(TcoreHal *hal, unsigned int data_len, void *data)
72 {
73         int ret;
74         struct custom_data *user_data;
75
76         if (tcore_hal_get_power_state(hal) == FALSE)
77                 return TCORE_RETURN_FAILURE;
78
79         user_data = tcore_hal_ref_user_data(hal);
80         if (!user_data)
81                 return TCORE_RETURN_FAILURE;
82
83         ret = tty_write(user_data->tty_fd, data, data_len);
84         if(ret < 0) {
85                 err("tty_write failed");
86
87                 return TCORE_RETURN_FAILURE;
88         }
89
90         dbg("tty_write success ret=%d (fd=%d, len=%d)", ret, user_data->tty_fd, data_len);
91
92         return TCORE_RETURN_SUCCESS;
93 }
94
95
96 static struct tcore_hal_operations hops =
97 {
98         .power = hal_power,
99         .send = hal_send,
100         .set_sound_path = pr3_audio_set_sound_path,
101 };
102
103 static gboolean on_recv_tty_message(GIOChannel *channel, GIOCondition condition, gpointer data)
104 {
105         TcoreHal *hal = data;
106         struct custom_data *custom;
107         char buf[BUF_LEN_MAX];
108         int n = 0;
109
110         custom = tcore_hal_ref_user_data(hal);
111         memset(buf, 0, BUF_LEN_MAX);
112         n = tty_read(custom->tty_fd, buf, BUF_LEN_MAX);
113         if (n < 0) {
114                 err("tty_read error. return_valute = %d", n);
115                 return TRUE;
116         }
117
118         dbg("tty recv (ret = %d)", n);
119         tcore_hal_emit_recv_callback(hal, n, buf);
120
121         tcore_hal_dispatch_response_data(hal, 0, n, buf);
122
123         return TRUE;
124 }
125
126 static guint register_gio_watch(TcoreHal *h, int fd, void *callback)
127 {
128         GIOChannel *channel = NULL;
129         guint source;
130
131         if (fd < 0 || !callback)
132                 return 0;
133
134         channel = g_io_channel_unix_new(fd);
135         source = g_io_add_watch(channel, G_IO_IN, (GIOFunc) callback, h);
136         g_io_channel_unref(channel);
137         channel = NULL;
138
139         return source;
140 }
141
142 static gboolean on_load()
143 {
144         dbg("I'm load!");
145
146         return TRUE;
147 }
148
149 static gboolean on_init(TcorePlugin *plugin)
150 {
151         TcoreHal *hal;
152         struct custom_data *data;
153
154         if (!plugin)
155                 return FALSE;
156
157         dbg("I'm init!");
158
159         /* Phonet init */
160         data = calloc(sizeof(struct custom_data), 1);
161         memset(data, 0, sizeof(struct custom_data));
162
163         data->tty_fd = tty_open();
164
165         /* HAL init */
166         hal = tcore_hal_new(plugin, "imc-pr3", &hops, TCORE_HAL_MODE_AT);
167         tcore_hal_link_user_data(hal, data);
168
169         data->watch_id_tty= register_gio_watch(hal, data->tty_fd, on_recv_tty_message);
170
171         dbg("tty_fd = %d, watch_id_tty=%d ", data->tty_fd, data->watch_id_tty);
172
173         if (pr3_audio_init() != TRUE)
174                 err("Error in audio initialization")
175
176
177         return TRUE;
178 }
179
180 static void on_unload(TcorePlugin *plugin)
181 {
182         if (!plugin)
183                 return;
184
185         if (pr3_audio_unload() != TRUE)
186                 err("Error in audio unload")
187
188         dbg("I'm unload");
189 }
190
191 struct tcore_plugin_define_desc plugin_define_desc =
192 {
193         .name = "IMC-PR3",
194         .priority = TCORE_PLUGIN_PRIORITY_HIGH,
195         .version = 1,
196         .load = on_load,
197         .init = on_init,
198         .unload = on_unload
199 };