blob: 3366babe7af17358f2a08611c32017a452816f53 [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;
40 LOGE("Failed to register property (%s)", strerror(errno));
41 pthread_mutex_unlock(&mLock);
42 return -1;
43 }
44 }
45 mPropertyPairs->push_back(new PropertyPair(name, pp));
46 pthread_mutex_unlock(&mLock);
47 return 0;
48}
49
50int PropertyManager::unregisterProperty(const char *name) {
51 PropertyPairCollection::iterator it;
52
53// LOGD("unregisterProperty(%s)", name);
54 pthread_mutex_lock(&mLock);
55 for (it = mPropertyPairs->begin(); it != mPropertyPairs->end(); ++it) {
56 if (!strcmp(name, (*it)->getName())) {
57 delete ((*it));
58 mPropertyPairs->erase(it);
59 pthread_mutex_unlock(&mLock);
60 return 0;
61 }
62 }
63 pthread_mutex_unlock(&mLock);
64 errno = ENOENT;
65 return -1;
66}
67
68/*
69 * IPropertyManager methods
70 */
71
72int PropertyManager::set(const char *name, const char *value) {
73 PropertyPairCollection::iterator it;
74
75 pthread_mutex_lock(&mLock);
76 for (it = mPropertyPairs->begin(); it != mPropertyPairs->end(); ++it) {
77 if (!strcmp(name, (*it)->getName())) {
78 pthread_mutex_unlock(&mLock);
79 return (*it)->getProvider()->set(name, value);
80 }
81 }
82 pthread_mutex_unlock(&mLock);
83 errno = ENOENT;
84 return -1;
85}
86
87const char *PropertyManager::get(const char *name, char *buffer, size_t max) {
88 PropertyPairCollection::iterator it;
89
90 memset(buffer, 0, max);
91 pthread_mutex_lock(&mLock);
92 for (it = mPropertyPairs->begin(); it != mPropertyPairs->end(); ++it) {
93 if (!strcmp(name, (*it)->getName())) {
94 pthread_mutex_unlock(&mLock);
95 return (*it)->getProvider()->get(name, buffer, max);
96 }
97 }
98 pthread_mutex_unlock(&mLock);
99 errno = ENOENT;
100 return NULL;
101}
102
103android::List<char *> *PropertyManager::createPropertyList() {
104 android::List<char *> *c = new android::List<char *>();
105
106 PropertyPairCollection::iterator it;
107
108 pthread_mutex_lock(&mLock);
109 for (it = mPropertyPairs->begin(); it != mPropertyPairs->end(); ++it)
110 c->push_back(strdup((*it)->getName()));
111 pthread_mutex_unlock(&mLock);
112 return c;
113}
114
115PropertyPair::PropertyPair(const char *name, IPropertyProvider *pp) {
116 mName = strdup(name);
117 mPp = pp;
118}
119
120PropertyPair::~PropertyPair() {
121 free(mName);
122}