The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 "wifi" |
| 18 | |
| 19 | #include "jni.h" |
Elliott Hughes | f17b971 | 2011-04-12 16:12:09 -0700 | [diff] [blame] | 20 | #include <ScopedUtfChars.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include <utils/misc.h> |
| 22 | #include <android_runtime/AndroidRuntime.h> |
| 23 | #include <utils/Log.h> |
Zheng BaoZhong | 038e315 | 2009-10-10 08:07:44 -0400 | [diff] [blame] | 24 | #include <utils/String16.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
| 26 | #include "wifi.h" |
| 27 | |
| 28 | #define WIFI_PKG_NAME "android/net/wifi/WifiNative" |
Irfan Sheriff | f235c5a | 2010-10-21 16:44:48 -0700 | [diff] [blame] | 29 | #define BUF_SIZE 256 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
Irfan Sheriff | 618455f | 2011-11-18 14:33:09 -0800 | [diff] [blame] | 33 | static jint DBG = false; |
Mike Lockwood | a5ec95c | 2009-07-08 17:11:17 -0400 | [diff] [blame] | 34 | |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 35 | static int doCommand(const char *ifname, const char *cmd, char *replybuf, int replybuflen) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | { |
| 37 | size_t reply_len = replybuflen - 1; |
| 38 | |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 39 | if (::wifi_command(ifname, cmd, replybuf, &reply_len) != 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | return -1; |
| 41 | else { |
| 42 | // Strip off trailing newline |
| 43 | if (reply_len > 0 && replybuf[reply_len-1] == '\n') |
| 44 | replybuf[reply_len-1] = '\0'; |
| 45 | else |
| 46 | replybuf[reply_len] = '\0'; |
| 47 | return 0; |
| 48 | } |
| 49 | } |
| 50 | |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 51 | static jint doIntCommand(const char *ifname, const char* fmt, ...) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | { |
Elliott Hughes | f17b971 | 2011-04-12 16:12:09 -0700 | [diff] [blame] | 53 | char buf[BUF_SIZE]; |
| 54 | va_list args; |
| 55 | va_start(args, fmt); |
| 56 | int byteCount = vsnprintf(buf, sizeof(buf), fmt, args); |
| 57 | va_end(args); |
| 58 | if (byteCount < 0 || byteCount >= BUF_SIZE) { |
| 59 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | } |
Elliott Hughes | f17b971 | 2011-04-12 16:12:09 -0700 | [diff] [blame] | 61 | char reply[BUF_SIZE]; |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 62 | if (doCommand(ifname, buf, reply, sizeof(reply)) != 0) { |
Elliott Hughes | f17b971 | 2011-04-12 16:12:09 -0700 | [diff] [blame] | 63 | return -1; |
| 64 | } |
| 65 | return static_cast<jint>(atoi(reply)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 68 | static jboolean doBooleanCommand(const char *ifname, const char* expect, const char* fmt, ...) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | { |
Elliott Hughes | f17b971 | 2011-04-12 16:12:09 -0700 | [diff] [blame] | 70 | char buf[BUF_SIZE]; |
| 71 | va_list args; |
| 72 | va_start(args, fmt); |
| 73 | int byteCount = vsnprintf(buf, sizeof(buf), fmt, args); |
| 74 | va_end(args); |
| 75 | if (byteCount < 0 || byteCount >= BUF_SIZE) { |
| 76 | return JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | } |
Elliott Hughes | f17b971 | 2011-04-12 16:12:09 -0700 | [diff] [blame] | 78 | char reply[BUF_SIZE]; |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 79 | if (doCommand(ifname, buf, reply, sizeof(reply)) != 0) { |
Elliott Hughes | f17b971 | 2011-04-12 16:12:09 -0700 | [diff] [blame] | 80 | return JNI_FALSE; |
| 81 | } |
| 82 | return (strcmp(reply, expect) == 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // Send a command to the supplicant, and return the reply as a String |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 86 | static jstring doStringCommand(JNIEnv* env, const char *ifname, const char* fmt, ...) { |
Elliott Hughes | f17b971 | 2011-04-12 16:12:09 -0700 | [diff] [blame] | 87 | char buf[BUF_SIZE]; |
| 88 | va_list args; |
| 89 | va_start(args, fmt); |
| 90 | int byteCount = vsnprintf(buf, sizeof(buf), fmt, args); |
| 91 | va_end(args); |
| 92 | if (byteCount < 0 || byteCount >= BUF_SIZE) { |
| 93 | return NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | } |
Elliott Hughes | f17b971 | 2011-04-12 16:12:09 -0700 | [diff] [blame] | 95 | char reply[4096]; |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 96 | if (doCommand(ifname, buf, reply, sizeof(reply)) != 0) { |
Elliott Hughes | f17b971 | 2011-04-12 16:12:09 -0700 | [diff] [blame] | 97 | return NULL; |
| 98 | } |
| 99 | // TODO: why not just NewStringUTF? |
| 100 | String16 str((char *)reply); |
| 101 | return env->NewString((const jchar *)str.string(), str.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Elliott Hughes | f17b971 | 2011-04-12 16:12:09 -0700 | [diff] [blame] | 104 | static jboolean android_net_wifi_isDriverLoaded(JNIEnv* env, jobject) |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 105 | { |
| 106 | return (jboolean)(::is_wifi_driver_loaded() == 1); |
| 107 | } |
| 108 | |
Elliott Hughes | f17b971 | 2011-04-12 16:12:09 -0700 | [diff] [blame] | 109 | static jboolean android_net_wifi_loadDriver(JNIEnv* env, jobject) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | { |
| 111 | return (jboolean)(::wifi_load_driver() == 0); |
| 112 | } |
| 113 | |
Elliott Hughes | f17b971 | 2011-04-12 16:12:09 -0700 | [diff] [blame] | 114 | static jboolean android_net_wifi_unloadDriver(JNIEnv* env, jobject) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | { |
| 116 | return (jboolean)(::wifi_unload_driver() == 0); |
| 117 | } |
| 118 | |
Elliott Hughes | f17b971 | 2011-04-12 16:12:09 -0700 | [diff] [blame] | 119 | static jboolean android_net_wifi_startSupplicant(JNIEnv* env, jobject) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | { |
| 121 | return (jboolean)(::wifi_start_supplicant() == 0); |
| 122 | } |
| 123 | |
Irfan Sheriff | f42c39b | 2011-08-26 14:45:23 -0700 | [diff] [blame] | 124 | static jboolean android_net_wifi_startP2pSupplicant(JNIEnv* env, jobject) |
| 125 | { |
| 126 | return (jboolean)(::wifi_start_p2p_supplicant() == 0); |
| 127 | } |
| 128 | |
Elliott Hughes | f17b971 | 2011-04-12 16:12:09 -0700 | [diff] [blame] | 129 | static jboolean android_net_wifi_killSupplicant(JNIEnv* env, jobject) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | { |
| 131 | return (jboolean)(::wifi_stop_supplicant() == 0); |
| 132 | } |
| 133 | |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 134 | static jboolean android_net_wifi_connectToSupplicant(JNIEnv* env, jobject, jstring jIface) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | { |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 136 | ScopedUtfChars ifname(env, jIface); |
| 137 | return (jboolean)(::wifi_connect_to_supplicant(ifname.c_str()) == 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 140 | static void android_net_wifi_closeSupplicantConnection(JNIEnv* env, jobject, jstring jIface) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | { |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 142 | ScopedUtfChars ifname(env, jIface); |
| 143 | ::wifi_close_supplicant_connection(ifname.c_str()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 146 | static jstring android_net_wifi_waitForEvent(JNIEnv* env, jobject, jstring jIface) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | { |
Irfan Sheriff | f235c5a | 2010-10-21 16:44:48 -0700 | [diff] [blame] | 148 | char buf[BUF_SIZE]; |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 149 | ScopedUtfChars ifname(env, jIface); |
| 150 | int nread = ::wifi_wait_for_event(ifname.c_str(), buf, sizeof buf); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | if (nread > 0) { |
| 152 | return env->NewStringUTF(buf); |
| 153 | } else { |
Elliott Hughes | f17b971 | 2011-04-12 16:12:09 -0700 | [diff] [blame] | 154 | return NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 158 | static jboolean android_net_wifi_doBooleanCommand(JNIEnv* env, jobject, jstring jIface, |
| 159 | jstring jCommand) |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 160 | { |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 161 | ScopedUtfChars ifname(env, jIface); |
| 162 | ScopedUtfChars command(env, jCommand); |
| 163 | |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 164 | if (command.c_str() == NULL) { |
| 165 | return JNI_FALSE; |
| 166 | } |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 167 | if (DBG) ALOGD("doBoolean: %s", command.c_str()); |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 168 | return doBooleanCommand(ifname.c_str(), "OK", "%s", command.c_str()); |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 171 | static jint android_net_wifi_doIntCommand(JNIEnv* env, jobject, jstring jIface, |
| 172 | jstring jCommand) |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 173 | { |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 174 | ScopedUtfChars ifname(env, jIface); |
| 175 | ScopedUtfChars command(env, jCommand); |
| 176 | |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 177 | if (command.c_str() == NULL) { |
| 178 | return -1; |
| 179 | } |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 180 | if (DBG) ALOGD("doInt: %s", command.c_str()); |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 181 | return doIntCommand(ifname.c_str(), "%s", command.c_str()); |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 184 | static jstring android_net_wifi_doStringCommand(JNIEnv* env, jobject, jstring jIface, |
| 185 | jstring jCommand) |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 186 | { |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 187 | ScopedUtfChars ifname(env, jIface); |
| 188 | |
| 189 | ScopedUtfChars command(env, jCommand); |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 190 | if (command.c_str() == NULL) { |
| 191 | return NULL; |
| 192 | } |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 193 | if (DBG) ALOGD("doString: %s", command.c_str()); |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 194 | return doStringCommand(env, ifname.c_str(), "%s", command.c_str()); |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | |
| 198 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | // ---------------------------------------------------------------------------- |
| 200 | |
| 201 | /* |
| 202 | * JNI registration. |
| 203 | */ |
| 204 | static JNINativeMethod gWifiMethods[] = { |
| 205 | /* name, signature, funcPtr */ |
| 206 | |
| 207 | { "loadDriver", "()Z", (void *)android_net_wifi_loadDriver }, |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 208 | { "isDriverLoaded", "()Z", (void *)android_net_wifi_isDriverLoaded }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | { "unloadDriver", "()Z", (void *)android_net_wifi_unloadDriver }, |
| 210 | { "startSupplicant", "()Z", (void *)android_net_wifi_startSupplicant }, |
Irfan Sheriff | f42c39b | 2011-08-26 14:45:23 -0700 | [diff] [blame] | 211 | { "startP2pSupplicant", "()Z", (void *)android_net_wifi_startP2pSupplicant }, |
Irfan Sheriff | 96071a7 | 2010-12-14 11:29:23 -0800 | [diff] [blame] | 212 | { "killSupplicant", "()Z", (void *)android_net_wifi_killSupplicant }, |
Irfan Sheriff | 44b330d | 2011-12-28 13:00:28 -0800 | [diff] [blame] | 213 | { "connectToSupplicant", "(Ljava/lang/String;)Z", |
| 214 | (void *)android_net_wifi_connectToSupplicant }, |
| 215 | { "closeSupplicantConnection", "(Ljava/lang/String;)V", |
| 216 | (void *)android_net_wifi_closeSupplicantConnection }, |
| 217 | { "waitForEvent", "(Ljava/lang/String;)Ljava/lang/String;", |
| 218 | (void*) android_net_wifi_waitForEvent }, |
| 219 | { "doBooleanCommand", "(Ljava/lang/String;Ljava/lang/String;)Z", |
| 220 | (void*) android_net_wifi_doBooleanCommand }, |
| 221 | { "doIntCommand", "(Ljava/lang/String;Ljava/lang/String;)I", |
| 222 | (void*) android_net_wifi_doIntCommand }, |
| 223 | { "doStringCommand", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", |
| 224 | (void*) android_net_wifi_doStringCommand }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | int register_android_net_wifi_WifiManager(JNIEnv* env) |
| 228 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | return AndroidRuntime::registerNativeMethods(env, |
| 230 | WIFI_PKG_NAME, gWifiMethods, NELEM(gWifiMethods)); |
| 231 | } |
| 232 | |
| 233 | }; // namespace android |