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