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
17 #include <stdlib.h>
18 #include <errno.h>
19 #include <strings.h>
20 #include <netinet/in.h>
21
22 #define LOG_TAG "Property"
23
24 #include <cutils/log.h>
25
26 #include "Property.h"
27
Property(const char * name,bool readOnly,int type,int numElements)28 Property::Property(const char *name, bool readOnly,
29 int type, int numElements) :
30 mName(name), mReadOnly(readOnly), mType(type),
31 mNumElements(numElements) {
32 if (index(name, '.')) {
33 LOGW("Property name %s violates namespace rules", name);
34 }
35 }
36
StringProperty(const char * name,bool ro,int elements)37 StringProperty::StringProperty(const char *name, bool ro, int elements) :
38 Property(name, ro, Property::Type_STRING, elements) {
39 }
set(int idx,int value)40 int StringProperty::set(int idx, int value) {
41 LOGE("Integer 'set' called on string property!");
42 errno = EINVAL;
43 return -1;
44 }
set(int idx,struct in_addr * value)45 int StringProperty::set(int idx, struct in_addr *value) {
46 LOGE("IpAddr 'set' called on string property!");
47 errno = EINVAL;
48 return -1;
49 }
get(int idx,int * buffer)50 int StringProperty::get(int idx, int *buffer) {
51 LOGE("Integer 'get' called on string property!");
52 errno = EINVAL;
53 return -1;
54 }
get(int idx,struct in_addr * buffer)55 int StringProperty::get(int idx, struct in_addr *buffer) {
56 LOGE("IpAddr 'get' called on string property!");
57 errno = EINVAL;
58 return -1;
59 }
60
StringPropertyHelper(const char * name,bool ro,char * buffer,size_t max)61 StringPropertyHelper::StringPropertyHelper(const char *name, bool ro,
62 char *buffer, size_t max) :
63 StringProperty(name, ro, 1) {
64 mBuffer = buffer;
65 mMax = max;
66 }
67
set(int idx,const char * value)68 int StringPropertyHelper::set(int idx, const char *value) {
69 if (idx != 0) {
70 LOGW("Attempt to use array index on StringPropertyHelper::set");
71 errno = EINVAL;
72 return -1;
73 }
74 strncpy(mBuffer, value, mMax);
75 return 0;
76 }
77
get(int idx,char * buffer,size_t max)78 int StringPropertyHelper::get(int idx, char *buffer, size_t max) {
79 if (idx != 0) {
80 LOGW("Attempt to use array index on StringPropertyHelper::get");
81 errno = EINVAL;
82 return -1;
83 }
84 strncpy(buffer, mBuffer, max);
85 return 0;
86 }
87
IntegerProperty(const char * name,bool ro,int elements)88 IntegerProperty::IntegerProperty(const char *name, bool ro, int elements) :
89 Property(name, ro, Property::Type_INTEGER, elements) {
90 }
91
set(int idx,const char * value)92 int IntegerProperty::set(int idx, const char *value) {
93 LOGE("String 'set' called on integer property!");
94 errno = EINVAL;
95 return -1;
96 }
set(int idx,struct in_addr * value)97 int IntegerProperty::set(int idx, struct in_addr *value) {
98 LOGE("IpAddr 'set' called on integer property!");
99 errno = EINVAL;
100 return -1;
101 }
get(int idx,char * buffer,size_t max)102 int IntegerProperty::get(int idx, char *buffer, size_t max) {
103 LOGE("String 'get' called on integer property!");
104 errno = EINVAL;
105 return -1;
106 }
get(int idx,struct in_addr * buffer)107 int IntegerProperty::get(int idx, struct in_addr *buffer) {
108 LOGE("IpAddr 'get' called on integer property!");
109 errno = EINVAL;
110 return -1;
111 }
112
IntegerPropertyHelper(const char * name,bool ro,int * buffer)113 IntegerPropertyHelper::IntegerPropertyHelper(const char *name, bool ro,
114 int *buffer) :
115 IntegerProperty(name, ro, 1) {
116 mBuffer = buffer;
117 }
118
set(int idx,int value)119 int IntegerPropertyHelper::set(int idx, int value) {
120 if (idx != 0) {
121 LOGW("Attempt to use array index on IntegerPropertyHelper::set");
122 errno = EINVAL;
123 return -1;
124 }
125 *mBuffer = value;
126 return 0;
127 }
128
get(int idx,int * buffer)129 int IntegerPropertyHelper::get(int idx, int *buffer) {
130 if (idx != 0) {
131 LOGW("Attempt to use array index on IntegerPropertyHelper::get");
132 errno = EINVAL;
133 return -1;
134 }
135 *buffer = *mBuffer;
136 return 0;
137 }
138
IPV4AddressProperty(const char * name,bool ro,int elements)139 IPV4AddressProperty::IPV4AddressProperty(const char *name, bool ro, int elements) :
140 Property(name, ro, Property::Type_IPV4, elements) {
141 }
142
set(int idx,const char * value)143 int IPV4AddressProperty::set(int idx, const char *value) {
144 LOGE("String 'set' called on ipv4 property!");
145 errno = EINVAL;
146 return -1;
147 }
set(int idx,int value)148 int IPV4AddressProperty::set(int idx, int value) {
149 LOGE("Integer 'set' called on ipv4 property!");
150 errno = EINVAL;
151 return -1;
152 }
get(int idx,char * buffer,size_t max)153 int IPV4AddressProperty::get(int idx, char *buffer, size_t max) {
154 LOGE("String 'get' called on ipv4 property!");
155 errno = EINVAL;
156 return -1;
157 }
get(int idx,int * buffer)158 int IPV4AddressProperty::get(int idx, int *buffer) {
159 LOGE("Integer 'get' called on ipv4 property!");
160 errno = EINVAL;
161 return -1;
162 }
163
IPV4AddressPropertyHelper(const char * name,bool ro,struct in_addr * buffer)164 IPV4AddressPropertyHelper::IPV4AddressPropertyHelper(const char *name, bool ro,
165 struct in_addr *buffer) :
166 IPV4AddressProperty(name, ro, 1) {
167 mBuffer = buffer;
168 }
169
set(int idx,struct in_addr * value)170 int IPV4AddressPropertyHelper::set(int idx, struct in_addr *value) {
171 if (idx != 0) {
172 LOGW("Attempt to use array index on IPV4AddressPropertyHelper::set");
173 errno = EINVAL;
174 return -1;
175 }
176 memcpy(mBuffer, value, sizeof(struct in_addr));
177 return 0;
178 }
179
get(int idx,struct in_addr * buffer)180 int IPV4AddressPropertyHelper::get(int idx, struct in_addr *buffer) {
181 if (idx != 0) {
182 LOGW("Attempt to use array index on IPV4AddressPropertyHelper::get");
183 errno = EINVAL;
184 return -1;
185 }
186 memcpy(buffer, mBuffer, sizeof(struct in_addr));
187 return 0;
188 }
189
PropertyNamespace(const char * name)190 PropertyNamespace::PropertyNamespace(const char *name) {
191 mName = strdup(name);
192 mProperties = new PropertyCollection();
193 }
194
~PropertyNamespace()195 PropertyNamespace::~PropertyNamespace() {
196 PropertyCollection::iterator it;
197 for (it = mProperties->begin(); it != mProperties->end();) {
198 delete (*it);
199 it = mProperties->erase(it);
200 }
201 delete mProperties;
202 free(mName);
203 }
204