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 <stdlib.h> |
San Mehat | 3d40729 | 2009-05-07 08:49:30 -0700 | [diff] [blame] | 17 | #include <string.h> |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 18 | |
| 19 | #define LOG_TAG "NetlinkEvent" |
| 20 | #include <cutils/log.h> |
| 21 | |
| 22 | #include <sysutils/NetlinkEvent.h> |
| 23 | |
| 24 | const int NetlinkEvent::NlActionUnknown = 0; |
| 25 | const int NetlinkEvent::NlActionAdd = 1; |
| 26 | const int NetlinkEvent::NlActionRemove = 2; |
| 27 | const int NetlinkEvent::NlActionChange = 3; |
| 28 | |
| 29 | NetlinkEvent::NetlinkEvent() { |
| 30 | mAction = NlActionUnknown; |
San Mehat | ebfe3db | 2009-10-10 17:35:13 -0700 | [diff] [blame] | 31 | memset(mParams, 0, sizeof(mParams)); |
| 32 | mPath = NULL; |
| 33 | mSubsystem = NULL; |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | NetlinkEvent::~NetlinkEvent() { |
| 37 | int i; |
| 38 | if (mPath) |
| 39 | free(mPath); |
| 40 | if (mSubsystem) |
| 41 | free(mSubsystem); |
| 42 | for (i = 0; i < NL_PARAMS_MAX; i++) { |
| 43 | if (!mParams[i]) |
| 44 | break; |
| 45 | free(mParams[i]); |
| 46 | } |
| 47 | } |
| 48 | |
San Mehat | d674413 | 2009-12-24 07:17:09 -0800 | [diff] [blame] | 49 | void NetlinkEvent::dump() { |
| 50 | int i; |
| 51 | |
| 52 | for (i = 0; i < NL_PARAMS_MAX; i++) { |
| 53 | if (!mParams[i]) |
| 54 | break; |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame^] | 55 | SLOGD("NL param '%s'\n", mParams[i]); |
San Mehat | d674413 | 2009-12-24 07:17:09 -0800 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 59 | bool NetlinkEvent::decode(char *buffer, int size) { |
| 60 | char *s = buffer; |
| 61 | char *end; |
| 62 | int param_idx = 0; |
| 63 | int i; |
| 64 | int first = 1; |
| 65 | |
| 66 | end = s + size; |
| 67 | while (s < end) { |
| 68 | if (first) { |
| 69 | char *p; |
| 70 | for (p = s; *p != '@'; p++); |
| 71 | p++; |
| 72 | mPath = strdup(p); |
| 73 | first = 0; |
| 74 | } else { |
| 75 | if (!strncmp(s, "ACTION=", strlen("ACTION="))) { |
| 76 | char *a = s + strlen("ACTION="); |
| 77 | if (!strcmp(a, "add")) |
| 78 | mAction = NlActionAdd; |
| 79 | else if (!strcmp(a, "remove")) |
| 80 | mAction = NlActionRemove; |
| 81 | else if (!strcmp(a, "change")) |
| 82 | mAction = NlActionChange; |
San Mehat | 03f0d27 | 2009-05-26 15:18:25 -0700 | [diff] [blame] | 83 | } else if (!strncmp(s, "SEQNUM=", strlen("SEQNUM="))) |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 84 | mSeq = atoi(s + strlen("SEQNUM=")); |
San Mehat | 03f0d27 | 2009-05-26 15:18:25 -0700 | [diff] [blame] | 85 | else if (!strncmp(s, "SUBSYSTEM=", strlen("SUBSYSTEM="))) |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 86 | mSubsystem = strdup(s + strlen("SUBSYSTEM=")); |
| 87 | else |
| 88 | mParams[param_idx++] = strdup(s); |
| 89 | } |
| 90 | s+= strlen(s) + 1; |
| 91 | } |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | const char *NetlinkEvent::findParam(const char *paramName) { |
| 96 | int i; |
| 97 | |
| 98 | for (i = 0; i < NL_PARAMS_MAX; i++) { |
| 99 | if (!mParams[i]) |
| 100 | break; |
| 101 | if (!strncmp(mParams[i], paramName, strlen(paramName))) |
| 102 | return &mParams[i][strlen(paramName) + 1]; |
| 103 | } |
| 104 | |
San Mehat | 7e8529a | 2010-03-25 09:31:42 -0700 | [diff] [blame^] | 105 | SLOGE("NetlinkEvent::FindParam(): Parameter '%s' not found", paramName); |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 106 | return NULL; |
| 107 | } |