Remove pointless variable initialization
[framework/connectivity/connman.git] / src / rfkill.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <stdint.h>
32
33 #include "connman.h"
34
35 enum rfkill_type {
36         RFKILL_TYPE_ALL = 0,
37         RFKILL_TYPE_WLAN,
38         RFKILL_TYPE_BLUETOOTH,
39         RFKILL_TYPE_UWB,
40         RFKILL_TYPE_WIMAX,
41         RFKILL_TYPE_WWAN,
42         RFKILL_TYPE_GPS,
43         RFKILL_TYPE_FM,
44 };
45
46 enum rfkill_operation {
47         RFKILL_OP_ADD = 0,
48         RFKILL_OP_DEL,
49         RFKILL_OP_CHANGE,
50         RFKILL_OP_CHANGE_ALL,
51 };
52
53 struct rfkill_event {
54         uint32_t idx;
55         uint8_t  type;
56         uint8_t  op;
57         uint8_t  soft;
58         uint8_t  hard;
59 };
60
61 static enum connman_service_type convert_type(uint8_t type)
62 {
63         switch (type) {
64         case RFKILL_TYPE_WLAN:
65                 return CONNMAN_SERVICE_TYPE_WIFI;
66         case RFKILL_TYPE_BLUETOOTH:
67                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
68         case RFKILL_TYPE_WIMAX:
69                 return CONNMAN_SERVICE_TYPE_WIMAX;
70         case RFKILL_TYPE_WWAN:
71                 return CONNMAN_SERVICE_TYPE_CELLULAR;
72         }
73
74         return CONNMAN_SERVICE_TYPE_UNKNOWN;
75 }
76
77 static GIOStatus rfkill_process(GIOChannel *chan)
78 {
79         unsigned char buf[32];
80         struct rfkill_event *event = (void *) buf;
81         enum connman_service_type type;
82         gsize len;
83         GIOStatus status;
84
85         DBG("");
86
87         memset(buf, 0, sizeof(buf));
88
89         status = g_io_channel_read_chars(chan, (gchar *) buf,
90                         sizeof(struct rfkill_event), &len, NULL);
91
92         if (status != G_IO_STATUS_NORMAL)
93                 return status;
94
95         if (len != sizeof(struct rfkill_event))
96                 return status;
97
98         DBG("idx %u type %u op %u soft %u hard %u", event->idx,
99                                                 event->type, event->op,
100                                                 event->soft, event->hard);
101
102         switch (event->op) {
103         case RFKILL_OP_ADD:
104                 type = convert_type(event->type);
105                 __connman_technology_add_rfkill(event->idx, type,
106                                                 event->soft, event->hard);
107                 break;
108         case RFKILL_OP_DEL:
109                 __connman_technology_remove_rfkill(event->idx);
110                 break;
111         case RFKILL_OP_CHANGE:
112                 __connman_technology_update_rfkill(event->idx, event->soft,
113                                                                 event->hard);
114                 break;
115         default:
116                 break;
117         }
118
119         return status;
120 }
121
122 static gboolean rfkill_event(GIOChannel *chan,
123                                 GIOCondition cond, gpointer data)
124 {
125         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
126                 return FALSE;
127
128         if (rfkill_process(chan) == G_IO_STATUS_ERROR)
129                 return FALSE;
130
131         return TRUE;
132 }
133
134 static GIOChannel *channel = NULL;
135
136 int __connman_rfkill_init(void)
137 {
138         GIOFlags flags;
139         int fd;
140
141         DBG("");
142
143         fd = open("/dev/rfkill", O_RDWR);
144         if (fd < 0) {
145                 connman_error("Failed to open RFKILL control device");
146                 return -EIO;
147         }
148
149         channel = g_io_channel_unix_new(fd);
150         g_io_channel_set_close_on_unref(channel, TRUE);
151
152         flags = g_io_channel_get_flags(channel);
153         flags |= G_IO_FLAG_NONBLOCK;
154         g_io_channel_set_flags(channel, flags, NULL);
155
156         /* Process current RFKILL events sent on device open */
157         while (rfkill_process(channel) == G_IO_STATUS_NORMAL);
158
159         g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
160                                                         rfkill_event, NULL);
161
162         return 0;
163 }
164
165 void __connman_rfkill_cleanup(void)
166 {
167         DBG("");
168
169         if (channel == NULL)
170                 return;
171
172         g_io_channel_shutdown(channel, TRUE, NULL);
173         g_io_channel_unref(channel);
174
175         channel = NULL;
176 }