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