NetlinkEvent.cpp revision ebfe3db361c51d9d99bf6cfd495bd16bdf815e1f
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>
17#include <string.h>
18
19#define LOG_TAG "NetlinkEvent"
20#include <cutils/log.h>
21
22#include <sysutils/NetlinkEvent.h>
23
24const int NetlinkEvent::NlActionUnknown = 0;
25const int NetlinkEvent::NlActionAdd = 1;
26const int NetlinkEvent::NlActionRemove = 2;
27const int NetlinkEvent::NlActionChange = 3;
28
29NetlinkEvent::NetlinkEvent() {
30    mAction = NlActionUnknown;
31    memset(mParams, 0, sizeof(mParams));
32    mPath = NULL;
33    mSubsystem = NULL;
34}
35
36NetlinkEvent::~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
49bool NetlinkEvent::decode(char *buffer, int size) {
50    char *s = buffer;
51    char *end;
52    int param_idx = 0;
53    int i;
54    int first = 1;
55
56    end = s + size;
57    while (s < end) {
58        if (first) {
59            char *p;
60            for (p = s; *p != '@'; p++);
61            p++;
62            mPath = strdup(p);
63            first = 0;
64        } else {
65            if (!strncmp(s, "ACTION=", strlen("ACTION="))) {
66                char *a = s + strlen("ACTION=");
67                if (!strcmp(a, "add"))
68                    mAction = NlActionAdd;
69                else if (!strcmp(a, "remove"))
70                    mAction = NlActionRemove;
71                else if (!strcmp(a, "change"))
72                    mAction = NlActionChange;
73            } else if (!strncmp(s, "SEQNUM=", strlen("SEQNUM=")))
74                mSeq = atoi(s + strlen("SEQNUM="));
75            else if (!strncmp(s, "SUBSYSTEM=", strlen("SUBSYSTEM=")))
76                mSubsystem = strdup(s + strlen("SUBSYSTEM="));
77            else
78                mParams[param_idx++] = strdup(s);
79        }
80        s+= strlen(s) + 1;
81    }
82    return true;
83}
84
85const char *NetlinkEvent::findParam(const char *paramName) {
86    int i;
87
88    for (i = 0; i < NL_PARAMS_MAX; i++) {
89        if (!mParams[i])
90            break;
91        if (!strncmp(mParams[i], paramName, strlen(paramName)))
92            return &mParams[i][strlen(paramName) + 1];
93    }
94
95    LOGE("NetlinkEvent::FindParam(): Parameter '%s' not found", paramName);
96    return NULL;
97}
98