release tizen_2.0 beta
[framework/telephony/tel-plugin-vmodem.git] / src / desc-vmodem.c
1 /*
2  * tel-plugin-vmodem
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Junhwan An <jh48.an@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <pthread.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <time.h>
27
28 #include <glib.h>
29
30 #include <tcore.h>
31 #include <plugin.h>
32
33 #include <tcore.h>
34 #include <server.h>
35 #include <plugin.h>
36 #include <user_request.h>
37 #include <hal.h>
38
39 #include "vdpram.h"
40
41 struct custom_data {
42         int vdpram_fd;
43         guint watch_id_vdpram;
44 };
45
46 static TReturn hal_power(TcoreHal *hal, gboolean flag)
47 {
48         struct custom_data *user_data;
49
50         user_data = tcore_hal_ref_user_data(hal);
51         if (!user_data)
52                 return TCORE_RETURN_FAILURE;
53
54         /* power on */
55         if (flag == TRUE) {
56                 if (FALSE == vdpram_poweron(user_data->vdpram_fd)) {
57                         err("vdpram_poweron failed");
58                         return TCORE_RETURN_FAILURE;
59                 }
60                 tcore_hal_set_power_state(hal, TRUE);
61         }
62         /* power off */
63         else {
64                 if (FALSE == vdpram_poweroff(user_data->vdpram_fd)) {
65                         err("vdpram_poweroff failed");
66                         return TCORE_RETURN_FAILURE;
67                 }
68                 tcore_hal_set_power_state(hal, FALSE);
69         }
70
71         return TCORE_RETURN_SUCCESS;
72 }
73
74
75 static TReturn hal_send(TcoreHal *hal, unsigned int data_len, void *data)
76 {
77         int ret;
78         struct custom_data *user_data;
79
80         if (tcore_hal_get_power_state(hal) == FALSE)
81                 return TCORE_RETURN_FAILURE;
82
83         user_data = tcore_hal_ref_user_data(hal);
84         if (!user_data)
85                 return TCORE_RETURN_FAILURE;
86
87         ret = vdpram_tty_write(user_data->vdpram_fd, data, data_len);
88         if(ret < 0)     {
89                 err("vdpram_tty_write failed");
90                 return TCORE_RETURN_FAILURE;
91         }
92         else {
93                 dbg("vdpram_tty_write success ret=%d (fd=%d, len=%d)", ret, user_data->vdpram_fd, data_len);
94                 return TCORE_RETURN_SUCCESS;
95         }
96 }
97
98
99 static struct tcore_hal_operations hops =
100 {
101         .power = hal_power,
102         .send = hal_send,
103 };
104
105 static gboolean on_recv_vdpram_message(GIOChannel *channel, GIOCondition condition, gpointer data)
106 {
107         TcoreHal *hal = data;
108         struct custom_data *custom;
109
110         #define BUF_LEN_MAX 512
111         char buf[BUF_LEN_MAX];
112         int n = 0;
113
114         custom = tcore_hal_ref_user_data(hal);
115         memset(buf, 0, BUF_LEN_MAX);
116         n = vdpram_tty_read(custom->vdpram_fd, buf, BUF_LEN_MAX);
117         if (n < 0) {
118                 err("tty_read error. return_valute = %d", n);
119                 return TRUE;
120         }
121
122         dbg("vdpram recv (ret = %d)", n);
123         tcore_hal_emit_recv_callback(hal, n, buf);
124
125         return TRUE;
126 }
127
128 static guint register_gio_watch(TcoreHal *h, int fd, void *callback)
129 {
130         GIOChannel *channel = NULL;
131         guint source;
132
133         if (fd < 0 || !callback)
134                 return 0;
135
136         channel = g_io_channel_unix_new(fd);
137         source = g_io_add_watch(channel, G_IO_IN, (GIOFunc) callback, h);
138         g_io_channel_unref(channel);
139         channel = NULL;
140
141         return source;
142 }
143
144
145 /*static int power_tx_pwr_on_exec(int nFd)
146 {
147          Not implement yet
148         return 0;
149 }*/
150
151 static gboolean on_load()
152 {
153         dbg("i'm load!");
154
155         return TRUE;
156 }
157
158 static gboolean on_init(TcorePlugin *plugin)
159 {
160         TcoreHal *hal;
161         struct custom_data *data;
162
163         if (!plugin)
164                 return FALSE;
165
166         dbg("i'm init!");
167
168         /*
169          * Phonet init
170          */
171         data = calloc(sizeof(struct custom_data), 1);
172         memset(data, 0, sizeof(struct custom_data));
173
174         data->vdpram_fd = vdpram_open();
175
176         /*
177          * HAL init
178          */
179         hal = tcore_hal_new(plugin, "vmodem", &hops, TCORE_HAL_MODE_CUSTOM);
180         tcore_hal_link_user_data(hal, data);
181
182         data->watch_id_vdpram= register_gio_watch(hal, data->vdpram_fd, on_recv_vdpram_message);
183
184         dbg("vdpram_fd = %d, watch_id_vdpram=%d ", data->vdpram_fd, data->watch_id_vdpram);
185
186         if (!vdpram_poweron(data->vdpram_fd))
187                 err("vdpram_poweron Failed");
188
189 //      power_tx_pwr_on_exec(data->vdpram_fd);
190
191         return TRUE;
192 }
193
194 static void on_unload(TcorePlugin *plugin)
195 {
196         if (!plugin)
197                 return;
198
199         dbg("i'm unload");
200 }
201
202 struct tcore_plugin_define_desc plugin_define_desc =
203 {
204         .name = "VMODEM",
205         .priority = TCORE_PLUGIN_PRIORITY_HIGH,
206         .version = 1,
207         .load = on_load,
208         .init = on_init,
209         .unload = on_unload
210 };