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