blob: 17fb519398862274d321497183c8e31d5094b166 [file] [log] [blame]
San Mehatdc266072009-05-06 11:16:52 -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 */
San Mehat3c5a6f02009-05-22 15:36:13 -070016
San Mehatdc266072009-05-06 11:16:52 -070017#include <stdio.h>
San Mehat48765672009-05-20 15:28:43 -070018#include <stdlib.h>
San Mehatdc266072009-05-06 11:16:52 -070019#include <string.h>
20#include <fcntl.h>
21#include <unistd.h>
22#include <malloc.h>
23#include <errno.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <unistd.h>
27
28#define LOG_TAG "Controller"
29
30#include <cutils/log.h>
31
32#include "Controller.h"
33
34extern "C" int init_module(void *, unsigned int, const char *);
35extern "C" int delete_module(const char *, unsigned int);
36
San Mehat3aff2d12009-06-15 14:10:44 -070037Controller::Controller(const char *name, PropertyManager *propMngr,
38 IControllerHandler *handlers) {
San Mehat3c5a6f02009-05-22 15:36:13 -070039 mPropMngr = propMngr;
40 mName = strdup(name);
San Mehat3aff2d12009-06-15 14:10:44 -070041 mHandlers = handlers;
San Mehat3c5a6f02009-05-22 15:36:13 -070042 mBoundInterface = NULL;
43}
San Mehat48765672009-05-20 15:28:43 -070044
San Mehat3c5a6f02009-05-22 15:36:13 -070045Controller::~Controller() {
46 if (mBoundInterface)
47 free(mBoundInterface);
48 if (mName)
49 free(mName);
San Mehatdc266072009-05-06 11:16:52 -070050}
51
52int Controller::start() {
53 return 0;
54}
55
56int Controller::stop() {
57 return 0;
58}
59
San Mehat3c5a6f02009-05-22 15:36:13 -070060int Controller::set(const char *name, const char *value) {
San Mehat48765672009-05-20 15:28:43 -070061 errno = ENOENT;
62 return -1;
63}
64
San Mehat3c5a6f02009-05-22 15:36:13 -070065const char *Controller::get(const char *name, char *buffer, size_t maxsize) {
San Mehat48765672009-05-20 15:28:43 -070066 errno = ENOENT;
67 return NULL;
68}
69
San Mehatdc266072009-05-06 11:16:52 -070070int Controller::loadKernelModule(char *modpath, const char *args) {
71 void *module;
72 unsigned int size;
73
San Mehatdc266072009-05-06 11:16:52 -070074 module = loadFile(modpath, &size);
75 if (!module) {
76 errno = -EIO;
77 return -1;
78 }
79
80 int rc = init_module(module, size, args);
81 free (module);
82 return rc;
83}
84
85int Controller::unloadKernelModule(const char *modtag) {
86 int rc = -1;
87 int retries = 10;
88
San Mehatdc266072009-05-06 11:16:52 -070089 while (retries--) {
90 rc = delete_module(modtag, O_NONBLOCK | O_EXCL);
91 if (rc < 0 && errno == EAGAIN)
92 usleep(1000*500);
93 else
94 break;
95 }
96
97 if (rc != 0) {
San Mehat3c5a6f02009-05-22 15:36:13 -070098 LOGW("Unable to unload kernel driver '%s' (%s)", modtag,
San Mehatdc266072009-05-06 11:16:52 -070099 strerror(errno));
100 }
101 return rc;
102}
103
104bool Controller::isKernelModuleLoaded(const char *modtag) {
105 FILE *fp = fopen("/proc/modules", "r");
106
107 if (!fp) {
108 LOGE("Unable to open /proc/modules (%s)", strerror(errno));
109 return false;
110 }
111
112 char line[255];
113 while(fgets(line, sizeof(line), fp)) {
114 char *endTag = strchr(line, ' ');
115
116 if (!endTag) {
117 LOGW("Unable to find tag for line '%s'", line);
118 continue;
119 }
120 if (!strncmp(line, modtag, (endTag - line))) {
121 fclose(fp);
122 return true;
123 }
124 }
125
126 fclose(fp);
127 return false;
128}
129
San Mehatdc266072009-05-06 11:16:52 -0700130void *Controller::loadFile(char *filename, unsigned int *_size)
131{
132 int ret, fd;
133 struct stat sb;
134 ssize_t size;
135 void *buffer = NULL;
136
137 /* open the file */
138 fd = open(filename, O_RDONLY);
139 if (fd < 0)
140 return NULL;
141
142 /* find out how big it is */
143 if (fstat(fd, &sb) < 0)
144 goto bail;
145 size = sb.st_size;
146
147 /* allocate memory for it to be read into */
148 buffer = malloc(size);
149 if (!buffer)
150 goto bail;
151
152 /* slurp it into our buffer */
153 ret = read(fd, buffer, size);
154 if (ret != size)
155 goto bail;
156
157 /* let the caller know how big it is */
158 *_size = size;
159
160bail:
161 close(fd);
162 return buffer;
163}
San Mehat3c5a6f02009-05-22 15:36:13 -0700164
165int Controller::bindInterface(const char *ifname) {
166 mBoundInterface = strdup(ifname);
167 LOGD("Controller %s bound to %s", mName, ifname);
168 return 0;
169}
170
171int Controller::unbindInterface(const char *ifname) {
172 free(mBoundInterface);
173 mBoundInterface = NULL;
174 LOGD("Controller %s unbound from %s", mName, ifname);
175 return 0;
176}