San Mehat | c4a895b | 2009-06-23 21:10:57 -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 | |
| 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 | |
| 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 | |
| 37 | StringProperty::StringProperty(const char *name, bool ro, int elements) : |
| 38 | Property(name, ro, Property::Type_STRING, elements) { |
| 39 | } |
| 40 | int StringProperty::set(int idx, int value) { |
| 41 | LOGE("Integer 'set' called on string property!"); |
| 42 | errno = EINVAL; |
| 43 | return -1; |
| 44 | } |
| 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 | } |
| 50 | int StringProperty::get(int idx, int *buffer) { |
| 51 | LOGE("Integer 'get' called on string property!"); |
| 52 | errno = EINVAL; |
| 53 | return -1; |
| 54 | } |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 88 | IntegerProperty::IntegerProperty(const char *name, bool ro, int elements) : |
| 89 | Property(name, ro, Property::Type_INTEGER, elements) { |
| 90 | } |
| 91 | |
| 92 | int IntegerProperty::set(int idx, const char *value) { |
| 93 | LOGE("String 'set' called on integer property!"); |
| 94 | errno = EINVAL; |
| 95 | return -1; |
| 96 | } |
| 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 | } |
| 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 | } |
| 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 | |
| 113 | IntegerPropertyHelper::IntegerPropertyHelper(const char *name, bool ro, |
| 114 | int *buffer) : |
| 115 | IntegerProperty(name, ro, 1) { |
| 116 | mBuffer = buffer; |
| 117 | } |
| 118 | |
| 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 | |
| 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 | |
| 139 | IPV4AddressProperty::IPV4AddressProperty(const char *name, bool ro, int elements) : |
| 140 | Property(name, ro, Property::Type_IPV4, elements) { |
| 141 | } |
| 142 | |
| 143 | int IPV4AddressProperty::set(int idx, const char *value) { |
| 144 | LOGE("String 'set' called on ipv4 property!"); |
| 145 | errno = EINVAL; |
| 146 | return -1; |
| 147 | } |
| 148 | int IPV4AddressProperty::set(int idx, int value) { |
| 149 | LOGE("Integer 'set' called on ipv4 property!"); |
| 150 | errno = EINVAL; |
| 151 | return -1; |
| 152 | } |
| 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 | } |
| 158 | int IPV4AddressProperty::get(int idx, int *buffer) { |
| 159 | LOGE("Integer 'get' called on ipv4 property!"); |
| 160 | errno = EINVAL; |
| 161 | return -1; |
| 162 | } |
| 163 | |
| 164 | IPV4AddressPropertyHelper::IPV4AddressPropertyHelper(const char *name, bool ro, |
| 165 | struct in_addr *buffer) : |
| 166 | IPV4AddressProperty(name, ro, 1) { |
| 167 | mBuffer = buffer; |
| 168 | } |
| 169 | |
| 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 | |
| 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 | |
| 190 | PropertyNamespace::PropertyNamespace(const char *name) { |
| 191 | mName = strdup(name); |
| 192 | mProperties = new PropertyCollection(); |
| 193 | } |
| 194 | |
| 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 | } |