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; |
| 31 | } |
| 32 | |
| 33 | NetlinkEvent::~NetlinkEvent() { |
| 34 | int i; |
| 35 | if (mPath) |
| 36 | free(mPath); |
| 37 | if (mSubsystem) |
| 38 | free(mSubsystem); |
| 39 | for (i = 0; i < NL_PARAMS_MAX; i++) { |
| 40 | if (!mParams[i]) |
| 41 | break; |
| 42 | free(mParams[i]); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | bool NetlinkEvent::decode(char *buffer, int size) { |
| 47 | char *s = buffer; |
| 48 | char *end; |
| 49 | int param_idx = 0; |
| 50 | int i; |
| 51 | int first = 1; |
| 52 | |
| 53 | end = s + size; |
| 54 | while (s < end) { |
| 55 | if (first) { |
| 56 | char *p; |
| 57 | for (p = s; *p != '@'; p++); |
| 58 | p++; |
| 59 | mPath = strdup(p); |
| 60 | first = 0; |
| 61 | } else { |
| 62 | if (!strncmp(s, "ACTION=", strlen("ACTION="))) { |
| 63 | char *a = s + strlen("ACTION="); |
| 64 | if (!strcmp(a, "add")) |
| 65 | mAction = NlActionAdd; |
| 66 | else if (!strcmp(a, "remove")) |
| 67 | mAction = NlActionRemove; |
| 68 | else if (!strcmp(a, "change")) |
| 69 | mAction = NlActionChange; |
San Mehat | 03f0d27 | 2009-05-26 15:18:25 -0700 | [diff] [blame^] | 70 | } else if (!strncmp(s, "SEQNUM=", strlen("SEQNUM="))) |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 71 | mSeq = atoi(s + strlen("SEQNUM=")); |
San Mehat | 03f0d27 | 2009-05-26 15:18:25 -0700 | [diff] [blame^] | 72 | else if (!strncmp(s, "SUBSYSTEM=", strlen("SUBSYSTEM="))) |
San Mehat | 168415b | 2009-05-06 11:14:21 -0700 | [diff] [blame] | 73 | mSubsystem = strdup(s + strlen("SUBSYSTEM=")); |
| 74 | else |
| 75 | mParams[param_idx++] = strdup(s); |
| 76 | } |
| 77 | s+= strlen(s) + 1; |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | const char *NetlinkEvent::findParam(const char *paramName) { |
| 83 | int i; |
| 84 | |
| 85 | for (i = 0; i < NL_PARAMS_MAX; i++) { |
| 86 | if (!mParams[i]) |
| 87 | break; |
| 88 | if (!strncmp(mParams[i], paramName, strlen(paramName))) |
| 89 | return &mParams[i][strlen(paramName) + 1]; |
| 90 | } |
| 91 | |
| 92 | LOGE("NetlinkEvent::FindParam(): Parameter '%s' not found", paramName); |
| 93 | return NULL; |
| 94 | } |