upload tizen1.0 source
[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         TcorePlugin *plugin = data;
108         TcoreHal *hal;
109         struct custom_data *custom;
110
111         #define BUF_LEN_MAX 512
112         char buf[BUF_LEN_MAX];
113         int n = 0;
114
115         hal = tcore_plugin_ref_hal(plugin);
116         custom = tcore_hal_ref_user_data(hal);
117         memset(buf, 0, BUF_LEN_MAX);
118         n = vdpram_tty_read(custom->vdpram_fd, buf, BUF_LEN_MAX);
119         if (n < 0) {
120                 err("tty_read error. return_valute = %d", n);
121                 return TRUE;
122         }
123
124         dbg("vdpram recv (ret = %d)", n);
125         tcore_hal_emit_recv_callback(hal, n, buf);
126
127         return TRUE;
128 }
129
130 static guint register_gio_watch(TcorePlugin *plugin, int fd, void *callback)
131 {
132         GIOChannel *channel = NULL;
133         guint source;
134
135         if (fd < 0 || !callback)
136                 return 0;
137
138         channel = g_io_channel_unix_new(fd);
139         source = g_io_add_watch(channel, G_IO_IN, (GIOFunc) callback, plugin);
140         g_io_channel_unref(channel);
141         channel = NULL;
142
143         return source;
144 }
145
146
147 /*static int power_tx_pwr_on_exec(int nFd)
148 {
149          Not implement yet
150         return 0;
151 }*/
152
153 static gboolean on_load()
154 {
155         dbg("i'm load!");
156
157         return TRUE;
158 }
159
160 static gboolean on_init(TcorePlugin *plugin)
161 {
162         TcoreHal *hal;
163         struct custom_data *data;
164
165         if (!plugin)
166                 return FALSE;
167
168         dbg("i'm init!");
169
170         /*
171          * Phonet init
172          */
173         data = calloc(sizeof(struct custom_data), 1);
174         memset(data, 0, sizeof(struct custom_data));
175
176         data->vdpram_fd = vdpram_open();
177
178         /*
179          * HAL init
180          */
181         hal = tcore_hal_new(plugin, "vmodem", &hops);
182         tcore_hal_link_user_data(hal, data);
183
184         data->watch_id_vdpram= register_gio_watch(plugin, data->vdpram_fd, on_recv_vdpram_message);
185
186         dbg("vdpram_fd = %d, watch_id_vdpram=%d ", data->vdpram_fd, data->watch_id_vdpram);
187
188         if (!vdpram_poweron(data->vdpram_fd))
189                 err("vdpram_poweron Failed");
190
191 //      power_tx_pwr_on_exec(data->vdpram_fd);
192
193         return TRUE;
194 }
195
196 static void on_unload(TcorePlugin *plugin)
197 {
198         if (!plugin)
199                 return;
200
201         dbg("i'm unload");
202 }
203
204 struct tcore_plugin_define_desc plugin_define_desc =
205 {
206         .name = "VMODEM",
207         .priority = TCORE_PLUGIN_PRIORITY_HIGH,
208         .version = 1,
209         .load = on_load,
210         .init = on_init,
211         .unload = on_unload
212 };