San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 1 | /* |
| 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> |
San Mehat | 3d40729 | 2009-05-07 08:49:30 -0700 | [diff] [blame] | 20 | #include <string.h> |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 21 | |
| 22 | #define LOG_TAG "NetlinkListener" |
| 23 | #include <cutils/log.h> |
| 24 | |
| 25 | #include <sysutils/NetlinkListener.h> |
| 26 | #include <sysutils/NetlinkEvent.h> |
| 27 | |
| 28 | NetlinkListener::NetlinkListener(int socket) : |
| 29 | SocketListener(socket, false) { |
| 30 | } |
| 31 | |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 32 | bool NetlinkListener::onDataAvailable(SocketClient *cli) |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 33 | { |
San Mehat | fa644ff | 2009-05-08 11:15:53 -0700 | [diff] [blame] | 34 | int socket = cli->getSocket(); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 35 | int count; |
| 36 | |
David 'Digit' Turner | b59539d | 2011-01-17 02:04:37 +0100 | [diff] [blame^] | 37 | count = TEMP_FAILURE_RETRY(recv(socket, mBuffer, sizeof(mBuffer), 0)); |
| 38 | if (count < 0) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 39 | SLOGE("recv failed (%s)", strerror(errno)); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 40 | return false; |
| 41 | } |
| 42 | |
| 43 | NetlinkEvent *evt = new NetlinkEvent(); |
| 44 | if (!evt->decode(mBuffer, count)) { |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame] | 45 | SLOGE("Error decoding NetlinkEvent"); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 46 | goto out; |
| 47 | } |
| 48 | |
San Mehat | ebfe3db | 2009-10-10 17:35:13 -0700 | [diff] [blame] | 49 | onEvent(evt); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 50 | out: |
| 51 | delete evt; |
| 52 | return true; |
| 53 | } |