blob: c74e9521cff99c973bf66e0a1b00c1c7565ec109 [file] [log] [blame]
San Mehat168415b2009-05-06 11:14:21 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include <errno.h>
17
18#include <sys/types.h>
19#include <sys/socket.h>
Nick Kralevich6bc08282011-04-18 15:55:59 -070020#include <linux/netlink.h>
San Mehat3d407292009-05-07 08:49:30 -070021#include <string.h>
San Mehat168415b2009-05-06 11:14:21 -070022
23#define LOG_TAG "NetlinkListener"
24#include <cutils/log.h>
Nick Kralevich531312e2011-04-29 16:49:22 -070025#include <cutils/uevent.h>
San Mehat168415b2009-05-06 11:14:21 -070026
27#include <sysutils/NetlinkListener.h>
28#include <sysutils/NetlinkEvent.h>
29
Mike J. Chenec16b9d2011-06-23 14:55:28 -070030NetlinkListener::NetlinkListener(int socket, int format) :
San Mehat168415b2009-05-06 11:14:21 -070031 SocketListener(socket, false) {
Mike J. Chenec16b9d2011-06-23 14:55:28 -070032 mFormat = format;
San Mehat168415b2009-05-06 11:14:21 -070033}
34
San Mehatfa644ff2009-05-08 11:15:53 -070035bool NetlinkListener::onDataAvailable(SocketClient *cli)
San Mehat168415b2009-05-06 11:14:21 -070036{
San Mehatfa644ff2009-05-08 11:15:53 -070037 int socket = cli->getSocket();
Nick Kralevich6bc08282011-04-18 15:55:59 -070038 ssize_t count;
San Mehat168415b2009-05-06 11:14:21 -070039
Nick Kralevich57de8b82011-05-11 14:58:24 -070040 count = TEMP_FAILURE_RETRY(uevent_kernel_multicast_recv(socket, mBuffer, sizeof(mBuffer)));
David 'Digit' Turnerb59539d2011-01-17 02:04:37 +010041 if (count < 0) {
Nick Kralevich6bc08282011-04-18 15:55:59 -070042 SLOGE("recvmsg failed (%s)", strerror(errno));
43 return false;
44 }
45
San Mehat168415b2009-05-06 11:14:21 -070046 NetlinkEvent *evt = new NetlinkEvent();
Mike J. Chenec16b9d2011-06-23 14:55:28 -070047 int err = evt->decode(mBuffer, count, mFormat);
48
49 if (!err) {
San Mehat7e8529a2010-03-25 09:31:42 -070050 SLOGE("Error decoding NetlinkEvent");
Mike J. Chenec16b9d2011-06-23 14:55:28 -070051 } else {
52 onEvent(evt);
San Mehat168415b2009-05-06 11:14:21 -070053 }
54
San Mehat168415b2009-05-06 11:14:21 -070055 delete evt;
56 return true;
57}