1/*
2 * Copyright (C) 2014 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#include "wifi_hal.h"
18
19#ifndef __WIFI_HAL_COMMON_H__
20#define __WIFI_HAL_COMMON_H__
21
22#ifndef LOG_TAG
23#define LOG_TAG  "WifiHAL"
24#endif
25
26#include <stdint.h>
27#include <fcntl.h>
28#include <inttypes.h>
29#include <sys/socket.h>
30#include <netlink/genl/genl.h>
31#include <netlink/genl/family.h>
32#include <netlink/genl/ctrl.h>
33#include <linux/rtnetlink.h>
34#include <netpacket/packet.h>
35#include <linux/filter.h>
36#include <linux/errqueue.h>
37
38#include <linux/pkt_sched.h>
39#include <netlink/object-api.h>
40#include <netlink/netlink.h>
41#include <netlink/socket.h>
42#include <netlink-types.h>
43
44#include "nl80211_copy.h"
45
46#include <utils/Log.h>
47#include "rb_wrapper.h"
48#include "pkt_stats.h"
49#include "wifihal_internal.h"
50
51#define SOCKET_BUFFER_SIZE      (32768U)
52#define RECV_BUF_SIZE           (4096)
53#define DEFAULT_EVENT_CB_SIZE   (64)
54#define DEFAULT_CMD_SIZE        (64)
55#define NUM_RING_BUFS           5
56
57#define MAC_ADDR_ARRAY(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
58#define MAC_ADDR_STR "%02x:%02x:%02x:%02x:%02x:%02x"
59#define BIT(x) (1 << (x))
60
61typedef int16_t s16;
62typedef int32_t s32;
63typedef int64_t s64;
64
65typedef void (*wifi_internal_event_handler) (wifi_handle handle, int events);
66
67class WifiCommand;
68
69typedef struct {
70    int nl_cmd;
71    uint32_t vendor_id;
72    int vendor_subcmd;
73    nl_recvmsg_msg_cb_t cb_func;
74    void *cb_arg;
75} cb_info;
76
77typedef struct {
78    wifi_request_id id;
79    WifiCommand *cmd;
80} cmd_info;
81
82typedef struct {
83    wifi_handle handle;                             // handle to wifi data
84    char name[IFNAMSIZ+1];                          // interface name + trailing null
85    int  id;                                        // id to use when talking to driver
86} interface_info;
87
88struct gscan_event_handlers_s;
89struct rssi_monitor_event_handler_s;
90
91typedef struct hal_info_s {
92
93    struct nl_sock *cmd_sock;                       // command socket object
94    struct nl_sock *event_sock;                     // event socket object
95    struct nl_sock *user_sock;                      // user socket object
96    int nl80211_family_id;                          // family id for 80211 driver
97
98    bool in_event_loop;                             // Indicates that event loop is active
99    bool clean_up;                                  // Indication to clean up the socket
100
101    wifi_internal_event_handler event_handler;      // default event handler
102    wifi_cleaned_up_handler cleaned_up_handler;     // socket cleaned up handler
103
104    cb_info *event_cb;                              // event callbacks
105    int num_event_cb;                               // number of event callbacks
106    int alloc_event_cb;                             // number of allocated callback objects
107    pthread_mutex_t cb_lock;                        // mutex for the event_cb access
108
109    cmd_info *cmd;                                  // Outstanding commands
110    int num_cmd;                                    // number of commands
111    int alloc_cmd;                                  // number of commands allocated
112
113    interface_info **interfaces;                    // array of interfaces
114    int num_interfaces;                             // number of interfaces
115
116    feature_set supported_feature_set;
117    // add other details
118    int user_sock_arg;
119    struct rb_info rb_infos[NUM_RING_BUFS];
120    void (*on_ring_buffer_data) (char *ring_name, char *buffer, int buffer_size,
121          wifi_ring_buffer_status *status);
122    void (*on_alert) (wifi_request_id id, char *buffer, int buffer_size, int err_code);
123    struct pkt_stats_s *pkt_stats;
124
125    /* socket pair used to exit from blocking poll*/
126    int exit_sockets[2];
127    u32 rx_buf_size_allocated;
128    u32 rx_buf_size_occupied;
129    wifi_ring_buffer_entry *rx_aggr_pkts;
130    rx_aggr_stats aggr_stats;
131    u32 prev_seq_no;
132    // pointer to structure having various gscan_event_handlers
133    struct gscan_event_handlers_s *gscan_handlers;
134    /* mutex for the log_handler access*/
135    pthread_mutex_t lh_lock;
136    /* mutex for the alert_handler access*/
137    pthread_mutex_t ah_lock;
138    u32 firmware_bus_max_size;
139    bool fate_monitoring_enabled;
140    packet_fate_monitor_info *pkt_fate_stats;
141    /* mutex for the packet fate stats shared resource protection */
142    pthread_mutex_t pkt_fate_stats_lock;
143    struct rssi_monitor_event_handler_s *rssi_handlers;
144} hal_info;
145
146wifi_error wifi_register_handler(wifi_handle handle, int cmd, nl_recvmsg_msg_cb_t func, void *arg);
147wifi_error wifi_register_vendor_handler(wifi_handle handle,
148            uint32_t id, int subcmd, nl_recvmsg_msg_cb_t func, void *arg);
149
150void wifi_unregister_handler(wifi_handle handle, int cmd);
151void wifi_unregister_vendor_handler(wifi_handle handle, uint32_t id, int subcmd);
152
153wifi_error wifi_register_cmd(wifi_handle handle, int id, WifiCommand *cmd);
154WifiCommand *wifi_unregister_cmd(wifi_handle handle, int id);
155void wifi_unregister_cmd(wifi_handle handle, WifiCommand *cmd);
156
157interface_info *getIfaceInfo(wifi_interface_handle);
158wifi_handle getWifiHandle(wifi_interface_handle handle);
159hal_info *getHalInfo(wifi_handle handle);
160hal_info *getHalInfo(wifi_interface_handle handle);
161wifi_handle getWifiHandle(hal_info *info);
162wifi_interface_handle getIfaceHandle(interface_info *info);
163wifi_error initializeGscanHandlers(hal_info *info);
164wifi_error cleanupGscanHandlers(hal_info *info);
165wifi_error initializeRSSIMonitorHandler(hal_info *info);
166wifi_error cleanupRSSIMonitorHandler(hal_info *info);
167
168lowi_cb_table_t *getLowiCallbackTable(u32 requested_lowi_capabilities);
169
170wifi_error wifi_start_sending_offloaded_packet(wifi_request_id id,
171        wifi_interface_handle iface, u8 *ip_packet, u16 ip_packet_len,
172        u8 *src_mac_addr, u8 *dst_mac_addr, u32 period_msec);
173wifi_error wifi_stop_sending_offloaded_packet(wifi_request_id id,
174        wifi_interface_handle iface);
175wifi_error wifi_start_rssi_monitoring(wifi_request_id id, wifi_interface_handle
176        iface, s8 max_rssi, s8 min_rssi, wifi_rssi_event_handler eh);
177wifi_error wifi_stop_rssi_monitoring(wifi_request_id id, wifi_interface_handle iface);
178// some common macros
179
180#define min(x, y)       ((x) < (y) ? (x) : (y))
181#define max(x, y)       ((x) > (y) ? (x) : (y))
182
183#define REQUEST_ID_MAX 1000
184#define get_requestid() ((arc4random()%REQUEST_ID_MAX) + 1)
185#define WAIT_TIME_FOR_SET_REG_DOMAIN 50000
186
187#ifdef __cplusplus
188extern "C"
189{
190#endif /* __cplusplus */
191void hexdump(void *bytes, u16 len);
192u8 get_rssi(u8 rssi_wo_noise_floor);
193#ifdef __cplusplus
194}
195#endif /* __cplusplus */
196
197#endif
198
199