michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * Abstraction on top of the wifi support from libhardware_legacy that we michael@0: * use to talk to the wpa_supplicant. michael@0: */ michael@0: michael@0: #ifndef WifiUtils_h michael@0: #define WifiUtils_h michael@0: michael@0: #include "nsString.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "mozilla/dom/WifiOptionsBinding.h" michael@0: #include "mozilla/dom/network/NetUtils.h" michael@0: #include "nsCxPusher.h" michael@0: michael@0: // Needed to add a copy constructor to WifiCommandOptions. michael@0: struct CommandOptions michael@0: { michael@0: public: michael@0: CommandOptions(const CommandOptions& aOther) { michael@0: mId = aOther.mId; michael@0: mCmd = aOther.mCmd; michael@0: mRequest = aOther.mRequest; michael@0: mIfname = aOther.mIfname; michael@0: mRoute = aOther.mRoute; michael@0: mIpaddr = aOther.mIpaddr; michael@0: mMask = aOther.mMask; michael@0: mGateway = aOther.mGateway; michael@0: mDns1 = aOther.mDns1; michael@0: mDns2 = aOther.mDns2; michael@0: mKey = aOther.mKey; michael@0: mValue = aOther.mValue; michael@0: mDefaultValue = aOther.mDefaultValue; michael@0: } michael@0: michael@0: CommandOptions(const mozilla::dom::WifiCommandOptions& aOther) { michael@0: michael@0: #define COPY_OPT_FIELD(prop, defaultValue) \ michael@0: if (aOther.prop.WasPassed()) { \ michael@0: prop = aOther.prop.Value(); \ michael@0: } else { \ michael@0: prop = defaultValue; \ michael@0: } michael@0: michael@0: #define COPY_FIELD(prop) prop = aOther.prop; michael@0: COPY_FIELD(mId) michael@0: COPY_FIELD(mCmd) michael@0: COPY_OPT_FIELD(mRequest, EmptyString()) michael@0: COPY_OPT_FIELD(mIfname, EmptyString()) michael@0: COPY_OPT_FIELD(mIpaddr, 0) michael@0: COPY_OPT_FIELD(mRoute, 0) michael@0: COPY_OPT_FIELD(mMask, 0) michael@0: COPY_OPT_FIELD(mGateway, 0) michael@0: COPY_OPT_FIELD(mDns1, 0) michael@0: COPY_OPT_FIELD(mDns2, 0) michael@0: COPY_OPT_FIELD(mKey, EmptyString()) michael@0: COPY_OPT_FIELD(mValue, EmptyString()) michael@0: COPY_OPT_FIELD(mDefaultValue, EmptyString()) michael@0: michael@0: #undef COPY_OPT_FIELD michael@0: #undef COPY_FIELD michael@0: } michael@0: michael@0: // All the fields, not Optional<> anymore to get copy constructors. michael@0: nsString mCmd; michael@0: nsString mDefaultValue; michael@0: int32_t mDns1; michael@0: int32_t mDns2; michael@0: int32_t mGateway; michael@0: int32_t mId; michael@0: nsString mIfname; michael@0: int32_t mIpaddr; michael@0: nsString mKey; michael@0: int32_t mMask; michael@0: nsString mRequest; michael@0: int32_t mRoute; michael@0: nsString mValue; michael@0: }; michael@0: michael@0: // Abstract class that exposes libhardware_legacy functions we need for michael@0: // wifi management. michael@0: // We use the ICS signatures here since they are likely more future-proof. michael@0: class WpaSupplicantImpl michael@0: { michael@0: public: michael@0: // Suppress warning from nsAutoPtr michael@0: virtual ~WpaSupplicantImpl() {} michael@0: michael@0: virtual int32_t michael@0: do_wifi_wait_for_event(const char *iface, char *buf, size_t len) = 0; // KK == ICS != JB michael@0: michael@0: virtual int32_t michael@0: do_wifi_command(const char* iface, const char* cmd, char* buff, size_t* len) = 0; // KK == ICS != JB michael@0: michael@0: virtual int32_t michael@0: do_wifi_load_driver() = 0; michael@0: michael@0: virtual int32_t michael@0: do_wifi_unload_driver() = 0; michael@0: michael@0: virtual int32_t michael@0: do_wifi_start_supplicant(int32_t) = 0; // ICS != JB == KK michael@0: michael@0: virtual int32_t michael@0: do_wifi_stop_supplicant(int32_t) = 0; //ICS != JB == KK michael@0: michael@0: virtual int32_t michael@0: do_wifi_connect_to_supplicant(const char* iface) = 0; // KK == ICS != JB michael@0: michael@0: virtual void michael@0: do_wifi_close_supplicant_connection(const char* iface) = 0; // KK == ICS != JB michael@0: }; michael@0: michael@0: // Concrete class to use to access the wpa supplicant. michael@0: class WpaSupplicant MOZ_FINAL michael@0: { michael@0: public: michael@0: WpaSupplicant(); michael@0: michael@0: // Use nsCString as the type of aInterface to guarantee it's michael@0: // null-terminated so that we can pass it to c API without michael@0: // conversion michael@0: void WaitForEvent(nsAString& aEvent, const nsCString& aInterface); michael@0: bool ExecuteCommand(CommandOptions aOptions, michael@0: mozilla::dom::WifiResultOptions& result, michael@0: const nsCString& aInterface); michael@0: michael@0: private: michael@0: nsAutoPtr mImpl; michael@0: nsAutoPtr mNetUtils; michael@0: michael@0: protected: michael@0: void CheckBuffer(char* buffer, int32_t length, nsAString& aEvent); michael@0: uint32_t MakeMask(uint32_t len); michael@0: }; michael@0: michael@0: #endif // WifiUtils_h