~phablet-team/aethercast/fix-for-microsoft-dongle

« back to all changes in this revision

Viewing changes to src/mcs/networkutils.cpp

  • Committer: Tarmac
  • Author(s): Simon Fels
  • Date: 2016-03-04 14:14:09 UTC
  • mfrom: (119.4.12 ac-networking-fixes)
  • Revision ID: tarmac-20160304141409-xe8khdgb43nha1fn
Multiple changes for Networking support:

* Add a DisconnectAll dbus method to allow the UI to easily disconnect
  any connected device. Just for the purpose of a demo.
* Return proper errors when scanning fails.
* Enable miracast mode for the WiFi driver if available.
* Set GO intent by default to 7. Should be higher if we're a sink.
* Print frequencies consider for GO negotiation
* Several minor fixes
* Enable unit tests again.

Approved by PS Jenkins bot, Thomas Voß.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 */
17
17
 
18
18
#include <sys/ioctl.h>
19
 
#include <net/if.h>
20
19
#include <asm/types.h>
21
 
#include <linux/netlink.h>
22
 
#include <linux/rtnetlink.h>
23
20
#include <sys/socket.h>
24
21
#include <netinet/in.h>
25
22
#include <arpa/inet.h>
26
23
#include <unistd.h>
27
24
#include <errno.h>
28
25
#include <memory.h>
 
26
#include <stdlib.h>
 
27
#include <errno.h>
 
28
 
 
29
#include <linux/netlink.h>
 
30
#include <linux/rtnetlink.h>
 
31
 
 
32
// Hacks necessary to be able to include wireless.h
 
33
#ifndef __user
 
34
#define __user
 
35
#endif
 
36
 
 
37
#include <linux/if.h>
 
38
#include <linux/wireless.h>
29
39
 
30
40
#include <glib.h>
31
41
 
33
43
#include "networkutils.h"
34
44
#include "logger.h"
35
45
 
 
46
namespace {
 
47
static constexpr size_t kDriverCommandReplySize{1024};
 
48
 
36
49
#define NLMSG_TAIL(nmsg)                                \
37
50
    ((struct rtattr *) (((uint8_t*) (nmsg)) +   \
38
51
    NLMSG_ALIGN((nmsg)->nlmsg_len)))
56
69
 
57
70
    return 0;
58
71
}
 
72
}
59
73
 
60
74
namespace mcs {
61
75
int NetworkUtils::ModifyInterfaceAddress(int cmd, int flags,
259
273
        available = (int64_t) nbytes;
260
274
    return available;
261
275
}
 
276
 
 
277
typedef struct {
 
278
#ifdef SUPPORT_64BIT
 
279
    u64 bufaddr;
 
280
#else
 
281
    char *bufaddr;
 
282
#endif
 
283
    int used_len;
 
284
    int total_len;
 
285
} android_wifi_priv_cmd;
 
286
 
 
287
int NetworkUtils::SendDriverPrivateCommand(const std::string &ifname, const std::string &cmd) {
 
288
    struct ifreq ifr;
 
289
    android_wifi_priv_cmd priv_cmd;
 
290
    char buf[kDriverCommandReplySize];
 
291
    size_t buf_len = kDriverCommandReplySize;
 
292
 
 
293
    ::memset(buf, 0, sizeof(buf));
 
294
    ::memcpy(buf, cmd.c_str(), cmd.length() + 1);
 
295
    ::memset(&ifr, 0, sizeof(ifr));
 
296
    ::memset(&priv_cmd, 0, sizeof(priv_cmd));
 
297
 
 
298
    ::strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ);
 
299
 
 
300
#ifdef SUPPORT_64BIT
 
301
    priv_cmd.bufaddr = (u64)(uintptr_t) buf;
 
302
#else
 
303
    priv_cmd.bufaddr = buf;
 
304
#endif
 
305
    priv_cmd.used_len = buf_len;
 
306
    priv_cmd.total_len = buf_len;
 
307
    ifr.ifr_data = &priv_cmd;
 
308
 
 
309
    const int s = ::socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
 
310
    if (s < 0)
 
311
        return -EIO;
 
312
 
 
313
    const int ret = ::ioctl(s, SIOCDEVPRIVATE + 1, &ifr);
 
314
    ::close(s);
 
315
    return ret;
 
316
}
 
317
 
262
318
} // namespace mcs