1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/wifi/WifiUtils.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,140 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +/** 1.9 + * Abstraction on top of the wifi support from libhardware_legacy that we 1.10 + * use to talk to the wpa_supplicant. 1.11 + */ 1.12 + 1.13 +#ifndef WifiUtils_h 1.14 +#define WifiUtils_h 1.15 + 1.16 +#include "nsString.h" 1.17 +#include "nsAutoPtr.h" 1.18 +#include "mozilla/dom/WifiOptionsBinding.h" 1.19 +#include "mozilla/dom/network/NetUtils.h" 1.20 +#include "nsCxPusher.h" 1.21 + 1.22 +// Needed to add a copy constructor to WifiCommandOptions. 1.23 +struct CommandOptions 1.24 +{ 1.25 +public: 1.26 + CommandOptions(const CommandOptions& aOther) { 1.27 + mId = aOther.mId; 1.28 + mCmd = aOther.mCmd; 1.29 + mRequest = aOther.mRequest; 1.30 + mIfname = aOther.mIfname; 1.31 + mRoute = aOther.mRoute; 1.32 + mIpaddr = aOther.mIpaddr; 1.33 + mMask = aOther.mMask; 1.34 + mGateway = aOther.mGateway; 1.35 + mDns1 = aOther.mDns1; 1.36 + mDns2 = aOther.mDns2; 1.37 + mKey = aOther.mKey; 1.38 + mValue = aOther.mValue; 1.39 + mDefaultValue = aOther.mDefaultValue; 1.40 + } 1.41 + 1.42 + CommandOptions(const mozilla::dom::WifiCommandOptions& aOther) { 1.43 + 1.44 +#define COPY_OPT_FIELD(prop, defaultValue) \ 1.45 + if (aOther.prop.WasPassed()) { \ 1.46 + prop = aOther.prop.Value(); \ 1.47 + } else { \ 1.48 + prop = defaultValue; \ 1.49 + } 1.50 + 1.51 +#define COPY_FIELD(prop) prop = aOther.prop; 1.52 + COPY_FIELD(mId) 1.53 + COPY_FIELD(mCmd) 1.54 + COPY_OPT_FIELD(mRequest, EmptyString()) 1.55 + COPY_OPT_FIELD(mIfname, EmptyString()) 1.56 + COPY_OPT_FIELD(mIpaddr, 0) 1.57 + COPY_OPT_FIELD(mRoute, 0) 1.58 + COPY_OPT_FIELD(mMask, 0) 1.59 + COPY_OPT_FIELD(mGateway, 0) 1.60 + COPY_OPT_FIELD(mDns1, 0) 1.61 + COPY_OPT_FIELD(mDns2, 0) 1.62 + COPY_OPT_FIELD(mKey, EmptyString()) 1.63 + COPY_OPT_FIELD(mValue, EmptyString()) 1.64 + COPY_OPT_FIELD(mDefaultValue, EmptyString()) 1.65 + 1.66 +#undef COPY_OPT_FIELD 1.67 +#undef COPY_FIELD 1.68 + } 1.69 + 1.70 + // All the fields, not Optional<> anymore to get copy constructors. 1.71 + nsString mCmd; 1.72 + nsString mDefaultValue; 1.73 + int32_t mDns1; 1.74 + int32_t mDns2; 1.75 + int32_t mGateway; 1.76 + int32_t mId; 1.77 + nsString mIfname; 1.78 + int32_t mIpaddr; 1.79 + nsString mKey; 1.80 + int32_t mMask; 1.81 + nsString mRequest; 1.82 + int32_t mRoute; 1.83 + nsString mValue; 1.84 +}; 1.85 + 1.86 +// Abstract class that exposes libhardware_legacy functions we need for 1.87 +// wifi management. 1.88 +// We use the ICS signatures here since they are likely more future-proof. 1.89 +class WpaSupplicantImpl 1.90 +{ 1.91 +public: 1.92 + // Suppress warning from nsAutoPtr 1.93 + virtual ~WpaSupplicantImpl() {} 1.94 + 1.95 + virtual int32_t 1.96 + do_wifi_wait_for_event(const char *iface, char *buf, size_t len) = 0; // KK == ICS != JB 1.97 + 1.98 + virtual int32_t 1.99 + do_wifi_command(const char* iface, const char* cmd, char* buff, size_t* len) = 0; // KK == ICS != JB 1.100 + 1.101 + virtual int32_t 1.102 + do_wifi_load_driver() = 0; 1.103 + 1.104 + virtual int32_t 1.105 + do_wifi_unload_driver() = 0; 1.106 + 1.107 + virtual int32_t 1.108 + do_wifi_start_supplicant(int32_t) = 0; // ICS != JB == KK 1.109 + 1.110 + virtual int32_t 1.111 + do_wifi_stop_supplicant(int32_t) = 0; //ICS != JB == KK 1.112 + 1.113 + virtual int32_t 1.114 + do_wifi_connect_to_supplicant(const char* iface) = 0; // KK == ICS != JB 1.115 + 1.116 + virtual void 1.117 + do_wifi_close_supplicant_connection(const char* iface) = 0; // KK == ICS != JB 1.118 +}; 1.119 + 1.120 +// Concrete class to use to access the wpa supplicant. 1.121 +class WpaSupplicant MOZ_FINAL 1.122 +{ 1.123 +public: 1.124 + WpaSupplicant(); 1.125 + 1.126 + // Use nsCString as the type of aInterface to guarantee it's 1.127 + // null-terminated so that we can pass it to c API without 1.128 + // conversion 1.129 + void WaitForEvent(nsAString& aEvent, const nsCString& aInterface); 1.130 + bool ExecuteCommand(CommandOptions aOptions, 1.131 + mozilla::dom::WifiResultOptions& result, 1.132 + const nsCString& aInterface); 1.133 + 1.134 +private: 1.135 + nsAutoPtr<WpaSupplicantImpl> mImpl; 1.136 + nsAutoPtr<NetUtils> mNetUtils; 1.137 + 1.138 +protected: 1.139 + void CheckBuffer(char* buffer, int32_t length, nsAString& aEvent); 1.140 + uint32_t MakeMask(uint32_t len); 1.141 +}; 1.142 + 1.143 +#endif // WifiUtils_h