Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /** |
michael@0 | 6 | * Abstraction on top of the wifi support from libhardware_legacy that we |
michael@0 | 7 | * use to talk to the wpa_supplicant. |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | #ifndef WifiUtils_h |
michael@0 | 11 | #define WifiUtils_h |
michael@0 | 12 | |
michael@0 | 13 | #include "nsString.h" |
michael@0 | 14 | #include "nsAutoPtr.h" |
michael@0 | 15 | #include "mozilla/dom/WifiOptionsBinding.h" |
michael@0 | 16 | #include "mozilla/dom/network/NetUtils.h" |
michael@0 | 17 | #include "nsCxPusher.h" |
michael@0 | 18 | |
michael@0 | 19 | // Needed to add a copy constructor to WifiCommandOptions. |
michael@0 | 20 | struct CommandOptions |
michael@0 | 21 | { |
michael@0 | 22 | public: |
michael@0 | 23 | CommandOptions(const CommandOptions& aOther) { |
michael@0 | 24 | mId = aOther.mId; |
michael@0 | 25 | mCmd = aOther.mCmd; |
michael@0 | 26 | mRequest = aOther.mRequest; |
michael@0 | 27 | mIfname = aOther.mIfname; |
michael@0 | 28 | mRoute = aOther.mRoute; |
michael@0 | 29 | mIpaddr = aOther.mIpaddr; |
michael@0 | 30 | mMask = aOther.mMask; |
michael@0 | 31 | mGateway = aOther.mGateway; |
michael@0 | 32 | mDns1 = aOther.mDns1; |
michael@0 | 33 | mDns2 = aOther.mDns2; |
michael@0 | 34 | mKey = aOther.mKey; |
michael@0 | 35 | mValue = aOther.mValue; |
michael@0 | 36 | mDefaultValue = aOther.mDefaultValue; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | CommandOptions(const mozilla::dom::WifiCommandOptions& aOther) { |
michael@0 | 40 | |
michael@0 | 41 | #define COPY_OPT_FIELD(prop, defaultValue) \ |
michael@0 | 42 | if (aOther.prop.WasPassed()) { \ |
michael@0 | 43 | prop = aOther.prop.Value(); \ |
michael@0 | 44 | } else { \ |
michael@0 | 45 | prop = defaultValue; \ |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | #define COPY_FIELD(prop) prop = aOther.prop; |
michael@0 | 49 | COPY_FIELD(mId) |
michael@0 | 50 | COPY_FIELD(mCmd) |
michael@0 | 51 | COPY_OPT_FIELD(mRequest, EmptyString()) |
michael@0 | 52 | COPY_OPT_FIELD(mIfname, EmptyString()) |
michael@0 | 53 | COPY_OPT_FIELD(mIpaddr, 0) |
michael@0 | 54 | COPY_OPT_FIELD(mRoute, 0) |
michael@0 | 55 | COPY_OPT_FIELD(mMask, 0) |
michael@0 | 56 | COPY_OPT_FIELD(mGateway, 0) |
michael@0 | 57 | COPY_OPT_FIELD(mDns1, 0) |
michael@0 | 58 | COPY_OPT_FIELD(mDns2, 0) |
michael@0 | 59 | COPY_OPT_FIELD(mKey, EmptyString()) |
michael@0 | 60 | COPY_OPT_FIELD(mValue, EmptyString()) |
michael@0 | 61 | COPY_OPT_FIELD(mDefaultValue, EmptyString()) |
michael@0 | 62 | |
michael@0 | 63 | #undef COPY_OPT_FIELD |
michael@0 | 64 | #undef COPY_FIELD |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | // All the fields, not Optional<> anymore to get copy constructors. |
michael@0 | 68 | nsString mCmd; |
michael@0 | 69 | nsString mDefaultValue; |
michael@0 | 70 | int32_t mDns1; |
michael@0 | 71 | int32_t mDns2; |
michael@0 | 72 | int32_t mGateway; |
michael@0 | 73 | int32_t mId; |
michael@0 | 74 | nsString mIfname; |
michael@0 | 75 | int32_t mIpaddr; |
michael@0 | 76 | nsString mKey; |
michael@0 | 77 | int32_t mMask; |
michael@0 | 78 | nsString mRequest; |
michael@0 | 79 | int32_t mRoute; |
michael@0 | 80 | nsString mValue; |
michael@0 | 81 | }; |
michael@0 | 82 | |
michael@0 | 83 | // Abstract class that exposes libhardware_legacy functions we need for |
michael@0 | 84 | // wifi management. |
michael@0 | 85 | // We use the ICS signatures here since they are likely more future-proof. |
michael@0 | 86 | class WpaSupplicantImpl |
michael@0 | 87 | { |
michael@0 | 88 | public: |
michael@0 | 89 | // Suppress warning from nsAutoPtr |
michael@0 | 90 | virtual ~WpaSupplicantImpl() {} |
michael@0 | 91 | |
michael@0 | 92 | virtual int32_t |
michael@0 | 93 | do_wifi_wait_for_event(const char *iface, char *buf, size_t len) = 0; // KK == ICS != JB |
michael@0 | 94 | |
michael@0 | 95 | virtual int32_t |
michael@0 | 96 | do_wifi_command(const char* iface, const char* cmd, char* buff, size_t* len) = 0; // KK == ICS != JB |
michael@0 | 97 | |
michael@0 | 98 | virtual int32_t |
michael@0 | 99 | do_wifi_load_driver() = 0; |
michael@0 | 100 | |
michael@0 | 101 | virtual int32_t |
michael@0 | 102 | do_wifi_unload_driver() = 0; |
michael@0 | 103 | |
michael@0 | 104 | virtual int32_t |
michael@0 | 105 | do_wifi_start_supplicant(int32_t) = 0; // ICS != JB == KK |
michael@0 | 106 | |
michael@0 | 107 | virtual int32_t |
michael@0 | 108 | do_wifi_stop_supplicant(int32_t) = 0; //ICS != JB == KK |
michael@0 | 109 | |
michael@0 | 110 | virtual int32_t |
michael@0 | 111 | do_wifi_connect_to_supplicant(const char* iface) = 0; // KK == ICS != JB |
michael@0 | 112 | |
michael@0 | 113 | virtual void |
michael@0 | 114 | do_wifi_close_supplicant_connection(const char* iface) = 0; // KK == ICS != JB |
michael@0 | 115 | }; |
michael@0 | 116 | |
michael@0 | 117 | // Concrete class to use to access the wpa supplicant. |
michael@0 | 118 | class WpaSupplicant MOZ_FINAL |
michael@0 | 119 | { |
michael@0 | 120 | public: |
michael@0 | 121 | WpaSupplicant(); |
michael@0 | 122 | |
michael@0 | 123 | // Use nsCString as the type of aInterface to guarantee it's |
michael@0 | 124 | // null-terminated so that we can pass it to c API without |
michael@0 | 125 | // conversion |
michael@0 | 126 | void WaitForEvent(nsAString& aEvent, const nsCString& aInterface); |
michael@0 | 127 | bool ExecuteCommand(CommandOptions aOptions, |
michael@0 | 128 | mozilla::dom::WifiResultOptions& result, |
michael@0 | 129 | const nsCString& aInterface); |
michael@0 | 130 | |
michael@0 | 131 | private: |
michael@0 | 132 | nsAutoPtr<WpaSupplicantImpl> mImpl; |
michael@0 | 133 | nsAutoPtr<NetUtils> mNetUtils; |
michael@0 | 134 | |
michael@0 | 135 | protected: |
michael@0 | 136 | void CheckBuffer(char* buffer, int32_t length, nsAString& aEvent); |
michael@0 | 137 | uint32_t MakeMask(uint32_t len); |
michael@0 | 138 | }; |
michael@0 | 139 | |
michael@0 | 140 | #endif // WifiUtils_h |