blob: 6faf9b8302d4c25c047e7e476dd020842bd05b33 [file] [log] [blame]
San Mehat3c5a6f02009-05-22 15:36:13 -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
17#define LOG_TAG "PropertyManager"
18
19#include <cutils/log.h>
20
21#include "PropertyManager.h"
22
23PropertyManager::PropertyManager() {
24 mPropertyPairs = new PropertyPairCollection();
25 pthread_mutex_init(&mLock, NULL);
26}
27
28PropertyManager::~PropertyManager() {
29 delete mPropertyPairs;
30}
31
32int PropertyManager::registerProperty(const char *name, IPropertyProvider *pp) {
33 PropertyPairCollection::iterator it;
34
35// LOGD("registerProperty(%s)", name);
36 pthread_mutex_lock(&mLock);
37 for (it = mPropertyPairs->begin(); it != mPropertyPairs->end(); ++it) {
38 if (!strcmp(name, (*it)->getName())) {
39 errno = EADDRINUSE;
San Mehat3aff2d12009-06-15 14:10:44 -070040 LOGE("Failed to register property %s (%s)",
41 name, strerror(errno));
San Mehat3c5a6f02009-05-22 15:36:13 -070042 pthread_mutex_unlock(&mLock);
43 return -1;
44 }
45 }
46 mPropertyPairs->push_back(new PropertyPair(name, pp));
47 pthread_mutex_unlock(&mLock);
48 return 0;
49}
50
51int PropertyManager::unregisterProperty(const char *name) {
52 PropertyPairCollection::iterator it;
53
54// LOGD("unregisterProperty(%s)", name);
55 pthread_mutex_lock(&mLock);
56 for (it = mPropertyPairs->begin(); it != mPropertyPairs->end(); ++it) {
57 if (!strcmp(name, (*it)->getName())) {
58 delete ((*it));
59 mPropertyPairs->erase(it);
60 pthread_mutex_unlock(&mLock);
61 return 0;
62 }
63 }
64 pthread_mutex_unlock(&mLock);
65 errno = ENOENT;
66 return -1;
67}
68
69/*
70 * IPropertyManager methods
71 */
72
73int PropertyManager::set(const char *name, const char *value) {
74 PropertyPairCollection::iterator it;
75
76 pthread_mutex_lock(&mLock);
77 for (it = mPropertyPairs->begin(); it != mPropertyPairs->end(); ++it) {
78 if (!strcmp(name, (*it)->getName())) {
79 pthread_mutex_unlock(&mLock);
80 return (*it)->getProvider()->set(name, value);
81 }
82 }
83 pthread_mutex_unlock(&mLock);
84 errno = ENOENT;
85 return -1;
86}
87
88const char *PropertyManager::get(const char *name, char *buffer, size_t max) {
89 PropertyPairCollection::iterator it;
90
91 memset(buffer, 0, max);
92 pthread_mutex_lock(&mLock);
93 for (it = mPropertyPairs->begin(); it != mPropertyPairs->end(); ++it) {
94 if (!strcmp(name, (*it)->getName())) {
95 pthread_mutex_unlock(&mLock);
96 return (*it)->getProvider()->get(name, buffer, max);
97 }
98 }
99 pthread_mutex_unlock(&mLock);
100 errno = ENOENT;
101 return NULL;
102}
103
104android::List<char *> *PropertyManager::createPropertyList() {
105 android::List<char *> *c = new android::List<char *>();
106
107 PropertyPairCollection::iterator it;
108
109 pthread_mutex_lock(&mLock);
110 for (it = mPropertyPairs->begin(); it != mPropertyPairs->end(); ++it)
111 c->push_back(strdup((*it)->getName()));
112 pthread_mutex_unlock(&mLock);
113 return c;
114}
115
116PropertyPair::PropertyPair(const char *name, IPropertyProvider *pp) {
117 mName = strdup(name);
118 mPp = pp;
119}
120
121PropertyPair::~PropertyPair() {
122 free(mName);
123}