[9610] wlbt: Fix warnings for ERD 9610 Android P Project.
[GitHub/LineageOS/android_hardware_samsung_slsi_scsc_wifibt_wifi_hal.git] / common.cpp
1
2 #include <stdint.h>
3 #include <fcntl.h>
4 #include <sys/socket.h>
5 #include <netlink/genl/genl.h>
6 #include <netlink/genl/family.h>
7 #include <netlink/genl/ctrl.h>
8 #include <linux/rtnetlink.h>
9 #include <netpacket/packet.h>
10 #include <linux/filter.h>
11 #include <linux/errqueue.h>
12
13 #include <linux/pkt_sched.h>
14 #include <netlink/object-api.h>
15 #include <netlink/netlink.h>
16 #include <netlink/socket.h>
17 #include <netlink/handlers.h>
18
19 #include "wifi_hal.h"
20 #include "common.h"
21 #include "cpp_bindings.h"
22
23 interface_info *getIfaceInfo(wifi_interface_handle handle)
24 {
25 return (interface_info *)handle;
26 }
27
28 wifi_handle getWifiHandle(wifi_interface_handle handle)
29 {
30 return getIfaceInfo(handle)->handle;
31 }
32
33 hal_info *getHalInfo(wifi_handle handle)
34 {
35 return (hal_info *)handle;
36 }
37
38 hal_info *getHalInfo(wifi_interface_handle handle)
39 {
40 return getHalInfo(getWifiHandle(handle));
41 }
42
43 wifi_handle getWifiHandle(hal_info *info)
44 {
45 return (wifi_handle)info;
46 }
47
48 wifi_interface_handle getIfaceHandle(interface_info *info)
49 {
50 return (wifi_interface_handle)info;
51 }
52
53 wifi_error wifi_register_handler(wifi_handle handle, int cmd, nl_recvmsg_msg_cb_t func, void *arg)
54 {
55 hal_info *info = (hal_info *)handle;
56
57 /* TODO: check for multiple handlers? */
58 pthread_mutex_lock(&info->cb_lock);
59
60 wifi_error result = WIFI_ERROR_OUT_OF_MEMORY;
61
62 if (info->num_event_cb < info->alloc_event_cb) {
63 info->event_cb[info->num_event_cb].nl_cmd = cmd;
64 info->event_cb[info->num_event_cb].vendor_id = 0;
65 info->event_cb[info->num_event_cb].vendor_subcmd = 0;
66 info->event_cb[info->num_event_cb].cb_func = func;
67 info->event_cb[info->num_event_cb].cb_arg = arg;
68 /*
69 ALOGI("Successfully added event handler %p:%p for command %d at %d",
70 arg, func, cmd, info->num_event_cb);*/
71 info->num_event_cb++;
72 result = WIFI_SUCCESS;
73 }
74
75 pthread_mutex_unlock(&info->cb_lock);
76 return result;
77 }
78
79 wifi_error wifi_register_vendor_handler(wifi_handle handle,
80 uint32_t id, int subcmd, nl_recvmsg_msg_cb_t func, void *arg)
81 {
82 hal_info *info = (hal_info *)handle;
83
84 //ALOGD("GSCAN register handle wifi_register_vendor_handler %p", handle);
85 /* TODO: check for multiple handlers? */
86 pthread_mutex_lock(&info->cb_lock);
87 //ALOGI("Added event handler %p", info);
88
89 wifi_error result = WIFI_ERROR_OUT_OF_MEMORY;
90
91 // ALOGD("register_vendor_handler: handle = %p", handle);
92 if (info->num_event_cb < info->alloc_event_cb) {
93 info->event_cb[info->num_event_cb].nl_cmd = NL80211_CMD_VENDOR;
94 info->event_cb[info->num_event_cb].vendor_id = id;
95 info->event_cb[info->num_event_cb].vendor_subcmd = subcmd;
96 info->event_cb[info->num_event_cb].cb_func = func;
97 info->event_cb[info->num_event_cb].cb_arg = arg;
98 /*
99 ALOGI("Added event handler %p:%p for vendor 0x%0x and subcmd 0x%0x at %d",
100 arg, func, id, subcmd, info->num_event_cb);*/
101 info->num_event_cb++;
102 result = WIFI_SUCCESS;
103 }
104
105 pthread_mutex_unlock(&info->cb_lock);
106 return result;
107 }
108
109 void wifi_unregister_handler(wifi_handle handle, int cmd)
110 {
111 hal_info *info = (hal_info *)handle;
112
113 if (cmd == NL80211_CMD_VENDOR) {
114 ALOGE("Must use wifi_unregister_vendor_handler to remove vendor handlers");
115 return;
116 }
117
118 pthread_mutex_lock(&info->cb_lock);
119
120 for (int i = 0; i < info->num_event_cb; i++) {
121 if (info->event_cb[i].nl_cmd == cmd) {
122 /*
123 ALOGI("Successfully removed event handler %p:%p for cmd = 0x%0x from %d",
124 info->event_cb[i].cb_arg, info->event_cb[i].cb_func, cmd, i);*/
125
126 memmove(&info->event_cb[i], &info->event_cb[i+1],
127 (info->num_event_cb - i - 1) * sizeof(cb_info));
128 info->num_event_cb--;
129 break;
130 }
131 }
132
133 pthread_mutex_unlock(&info->cb_lock);
134 }
135
136 void wifi_unregister_vendor_handler(wifi_handle handle, uint32_t id, int subcmd)
137 {
138 hal_info *info = (hal_info *)handle;
139
140 pthread_mutex_lock(&info->cb_lock);
141
142 for (int i = 0; i < info->num_event_cb; i++) {
143
144 if (info->event_cb[i].nl_cmd == NL80211_CMD_VENDOR
145 && info->event_cb[i].vendor_id == id
146 && info->event_cb[i].vendor_subcmd == subcmd) {
147 /*
148 ALOGI("Successfully removed event handler %p:%p for vendor 0x%0x, subcmd 0x%0x from %d",
149 info->event_cb[i].cb_arg, info->event_cb[i].cb_func, id, subcmd, i);*/
150 memmove(&info->event_cb[i], &info->event_cb[i+1],
151 (info->num_event_cb - i - 1) * sizeof(cb_info));
152 info->num_event_cb--;
153 break;
154 }
155 }
156
157 pthread_mutex_unlock(&info->cb_lock);
158 }
159
160
161 wifi_error wifi_register_cmd(wifi_handle handle, int id, WifiCommand *cmd)
162 {
163 hal_info *info = (hal_info *)handle;
164
165 //ALOGD("registering command %d", id);
166
167 wifi_error result = WIFI_ERROR_OUT_OF_MEMORY;
168
169 if (info->num_cmd < info->alloc_cmd) {
170 info->cmd[info->num_cmd].id = id;
171 info->cmd[info->num_cmd].cmd = cmd;
172 //ALOGI("Successfully added command %d: %p at %d", id, cmd, info->num_cmd);
173 info->num_cmd++;
174 result = WIFI_SUCCESS;
175 }
176
177 return result;
178 }
179
180 WifiCommand *wifi_unregister_cmd(wifi_handle handle, int id)
181 {
182 hal_info *info = (hal_info *)handle;
183
184 //ALOGD("un-registering command %d", id);
185
186 WifiCommand *cmd = NULL;
187
188 for (int i = 0; i < info->num_cmd; i++) {
189 if (info->cmd[i].id == id) {
190 cmd = info->cmd[i].cmd;
191 memmove(&info->cmd[i], &info->cmd[i+1], (info->num_cmd - i) * sizeof(cmd_info));
192 info->num_cmd--;
193 //ALOGI("Successfully removed command %d: %p from %d", id, cmd, i);
194 break;
195 }
196 }
197
198 return cmd;
199 }
200
201 WifiCommand *wifi_get_cmd(wifi_handle handle, int id)
202 {
203 hal_info *info = (hal_info *)handle;
204
205 WifiCommand *cmd = NULL;
206
207 for (int i = 0; i < info->num_cmd; i++) {
208 if (info->cmd[i].id == id) {
209 cmd = info->cmd[i].cmd;
210 break;
211 }
212 }
213
214 return cmd;
215 }
216
217 void wifi_unregister_cmd(wifi_handle handle, WifiCommand *cmd)
218 {
219 hal_info *info = (hal_info *)handle;
220
221 for (int i = 0; i < info->num_cmd; i++) {
222 if (info->cmd[i].cmd == cmd) {
223 memmove(&info->cmd[i], &info->cmd[i+1], (info->num_cmd - i) * sizeof(cmd_info));
224 info->num_cmd--;
225 //ALOGI("Successfully removed command %d: %p from %d", id, cmd, i);
226 break;
227 }
228 }
229 }
230
231 wifi_error wifi_cancel_cmd(wifi_request_id id, wifi_interface_handle iface)
232 {
233 wifi_handle handle = getWifiHandle(iface);
234
235 WifiCommand *cmd = wifi_unregister_cmd(handle, id);
236 //ALOGD("Cancel WifiCommand = %p", cmd);
237 if (cmd) {
238 cmd->cancel();
239 cmd->releaseRef();
240 return WIFI_SUCCESS;
241 }
242
243 return WIFI_ERROR_INVALID_ARGS;
244 }