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