• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef WIFI_CPP_BINDINGS_H
17 #define WIFI_CPP_BINDINGS_H
18 
19 #include "wifi_hal.h"
20 #include "common.h"
21 #include "sync.h"
22 #include "securec.h"
23 
24 class WifiEvent {
25     static const unsigned nL80211AttrMaxInternal = 256;
26 public:
WifiEvent(nl_msg * msg)27     explicit WifiEvent(nl_msg *msg) : mMsg(msg), mHeader(nullptr)
28     {
29         if (memset_s(mAttributes, sizeof(mAttributes), 0, sizeof(mAttributes)) != EOK) {
30             HDF_LOGI("mAttributes memset error");
31             return;
32         }
33     }
~WifiEvent()34     ~WifiEvent() {}
35 
36     void Log();
37 
38     int Parse();
39 
header()40     genlmsghdr *header()
41     {
42         return mHeader;
43     }
44 
GetCmd()45     int GetCmd()
46     {
47         return mHeader->cmd;
48     }
49 
GetVendorId()50     int GetVendorId()
51     {
52         return GetU32(NL80211_ATTR_VENDOR_ID);
53     }
54 
GetVendorSubcmd()55     int GetVendorSubcmd()
56     {
57         return GetU32(NL80211_ATTR_VENDOR_SUBCMD);
58     }
59 
GetVendorData()60     void *GetVendorData()
61     {
62         return GetData(NL80211_ATTR_VENDOR_DATA);
63     }
64 
GetVendorDataLen()65     int GetVendorDataLen()
66     {
67         return GetLen(NL80211_ATTR_VENDOR_DATA);
68     }
69 
Attributes()70     nlattr **Attributes()
71     {
72         return mAttributes;
73     }
74 
GetAttribute(int attribute)75     nlattr *GetAttribute(int attribute)
76     {
77         return mAttributes[attribute];
78     }
79 
GetU8(int attribute)80     uint8_t GetU8(int attribute)
81     {
82         return mAttributes[attribute] ? nla_get_u8(mAttributes[attribute]) : 0;
83     }
84 
GetU16(int attribute)85     uint16_t GetU16(int attribute)
86     {
87         return mAttributes[attribute] ? nla_get_u16(mAttributes[attribute]) : 0;
88     }
89 
GetU32(int attribute)90     uint32_t GetU32(int attribute)
91     {
92         return mAttributes[attribute] ? nla_get_u32(mAttributes[attribute]) : 0;
93     }
94 
GetU64(int attribute)95     uint64_t GetU64(int attribute)
96     {
97         return mAttributes[attribute] ? nla_get_u64(mAttributes[attribute]) : 0;
98     }
99 
GetLen(int attribute)100     int GetLen(int attribute)
101     {
102         return mAttributes[attribute] ? nla_len(mAttributes[attribute]) : 0;
103     }
104 
GetData(int attribute)105     void *GetData(int attribute)
106     {
107         return mAttributes[attribute] ? nla_data(mAttributes[attribute]) : NULL;
108     }
109 
GetString(int attribute)110     void *GetString(int attribute)
111     {
112         return mAttributes[attribute] ? nla_get_string(mAttributes[attribute]) : NULL;
113     }
114 private:
115     WifiEvent(const WifiEvent&); // hide copy constructor to prevent copies
116     WifiEvent& operator = (const WifiEvent&);
117 
118 private:
119     struct nl_msg *mMsg;
120     struct genlmsghdr *mHeader;
121     struct nlattr *mAttributes[nL80211AttrMaxInternal + 1];
122 };
123 
124 class NlIterator {
125     struct nlattr *pos;
126     int rem;
127 public:
NlIterator(struct nlattr * attr)128     explicit NlIterator(struct nlattr *attr)
129     {
130         pos = reinterpret_cast<struct nlattr *>(nla_data(attr));
131         rem = nla_len(attr);
132     }
HasNext()133     bool HasNext()
134     {
135         return nla_ok(pos, rem);
136     }
Next()137     void Next()
138     {
139         pos = reinterpret_cast<struct nlattr *>(nla_next(pos, &(rem)));
140     }
Get()141     struct nlattr *Get()
142     {
143         return pos;
144     }
GetType()145     uint16_t GetType()
146     {
147         return nla_type(pos);
148     }
GetU8()149     uint8_t GetU8()
150     {
151         return nla_get_u8(pos);
152     }
GetU16()153     uint16_t GetU16()
154     {
155         return nla_get_u16(pos);
156     }
GetU32()157     uint32_t GetU32()
158     {
159         return nla_get_u32(pos);
160     }
GetU64()161     uint64_t GetU64()
162     {
163         return nla_get_u64(pos);
164     }
GetData()165     void* GetData()
166     {
167         return nla_data(pos);
168     }
GetLen()169     int GetLen()
170     {
171         return nla_len(pos);
172     }
GetString()173     void* GetString()
174     {
175         return nla_get_string(pos);
176     }
177 private:
178     NlIterator(const NlIterator&);    // hide copy constructor to prevent copies
179     NlIterator& operator = (const NlIterator&);
180 };
181 
182 class WifiRequest {
183 public:
WifiRequest(int family)184     explicit WifiRequest(int family)
185     {
186         mMsg = nullptr;
187         mFamily = family;
188         mIface = -1;
189     }
190 
WifiRequest(int family,int iface)191     WifiRequest(int family, int iface)
192     {
193         mMsg = nullptr;
194         mFamily = family;
195         mIface = iface;
196     }
197 
~WifiRequest()198     ~WifiRequest()
199     {
200         Destroy();
201     }
202 
Destroy()203     void Destroy()
204     {
205         if (mMsg) {
206             nlmsg_free(mMsg);
207             mMsg = nullptr;
208         }
209     }
210 
GetMessage()211     nl_msg *GetMessage()
212     {
213         return mMsg;
214     }
215 
216     /* Command assembly helpers */
217     int Create(int family, uint8_t cmd, int flags, int hdrlen);
Create(uint8_t cmd)218     int Create(uint8_t cmd)
219     {
220         return Create(mFamily, cmd, 0, 0);
221     }
222 
223     int Create(uint32_t id, int subcmd);
224 
Put(int attribute,void * ptr,unsigned len)225     int Put(int attribute, void *ptr, unsigned len)
226     {
227         return nla_put(mMsg, attribute, len, ptr);
228     }
PutS8(int attribute,int8_t value)229     int PutS8(int attribute, int8_t value)
230     {
231         return nla_put(mMsg, attribute, sizeof(value), &value);
232     }
PutU8(int attribute,uint8_t value)233     int PutU8(int attribute, uint8_t value)
234     {
235         return nla_put(mMsg, attribute, sizeof(value), &value);
236     }
PutU16(int attribute,uint16_t value)237     int PutU16(int attribute, uint16_t value)
238     {
239         return nla_put(mMsg, attribute, sizeof(value), &value);
240     }
PutU32(int attribute,uint32_t value)241     int PutU32(int attribute, uint32_t value)
242     {
243         return nla_put(mMsg, attribute, sizeof(value), &value);
244     }
PutU64(int attribute,uint64_t value)245     int PutU64(int attribute, uint64_t value)
246     {
247         return nla_put(mMsg, attribute, sizeof(value), &value);
248     }
PutString(int attribute,const char * value)249     int PutString(int attribute, const char *value)
250     {
251         return nla_put(mMsg, attribute, strlen(value) + 1, value);
252     }
PutAddr(int attribute,macAddr value)253     int PutAddr(int attribute, macAddr value)
254     {
255         return nla_put(mMsg, attribute, sizeof(macAddr), value);
256     }
PutFlag(int attribute)257     int PutFlag(int attribute)
258     {
259         return nla_put_flag(mMsg, attribute);
260     }
AttrStart(int attribute)261     struct nlattr *AttrStart(int attribute)
262     {
263         return nla_nest_start(mMsg, attribute);
264     }
AttrEnd(struct nlattr * attr)265     void AttrEnd(struct nlattr *attr)
266     {
267         nla_nest_end(mMsg, attr);
268     }
269 
SetIfaceId(int ifindex)270     int SetIfaceId(int ifindex)
271     {
272         return PutU32(NL80211_ATTR_IFINDEX, ifindex);
273     }
274 
275 private:
276     WifiRequest(const WifiRequest&);        // hide copy constructor to prevent copies
277     WifiRequest& operator = (const WifiRequest&);
278 private:
279     int mFamily;
280     int mIface;
281     struct nl_msg *mMsg;
282 };
283 
284 class WifiCommand {
285 public:
WifiCommand(const char * type,wifiHandle handle,int id)286     WifiCommand(const char *type, wifiHandle handle, int id)
287         : mType(type), mMsg(GetHalInfo(handle)->nl80211FamilyId), mId(id), mRefs(1)
288     {
289         mIfaceInfo = nullptr;
290         mInfo = GetHalInfo(handle);
291     }
292 
WifiCommand(const char * type,wifiInterfaceHandle iface,int id)293     WifiCommand(const char *type, wifiInterfaceHandle iface, int id)
294         : mType(type), mMsg(GetHalInfo(iface)->nl80211FamilyId, GetIfaceInfo(iface)->id),
295           mId(id), mRefs(1)
296     {
297         mIfaceInfo = GetIfaceInfo(iface);
298         mInfo = GetHalInfo(iface);
299     }
300 
~WifiCommand()301     virtual ~WifiCommand() {}
302 
Id()303     int Id()
304     {
305         return mId;
306     }
307 
GetType()308     const char *GetType()
309     {
310         return mType;
311     }
312 
AddRef()313     virtual void AddRef()
314     {
315         int refs = __sync_add_and_fetch(&mRefs, 1);
316         HDF_LOGD("AddRef: WifiCommand %p has %d references", this, refs);
317     }
318 
ReleaseRef()319     virtual void ReleaseRef()
320     {
321         int refs = __sync_sub_and_fetch(&mRefs, 1);
322         if (refs == 0) {
323             delete this;
324         } else {
325         }
326     }
327 
Create()328     virtual int Create()
329     {
330         HDF_LOGD("WifiCommand %p can't be created", this);
331         return HAL_NOT_SUPPORTED;
332     }
333 
Cancel()334     virtual int Cancel()
335     {
336         return HAL_NOT_SUPPORTED;
337     }
338 
339     int RequestResponse();
340     int RequestEvent(int cmd);
341     int RequestVendorEvent(uint32_t id, int subcmd);
342     int RequestResponse(WifiRequest& request);
343 
344 protected:
WifiHandle()345     wifiHandle WifiHandle()
346     {
347         return GetWifiHandle(mInfo);
348     }
349 
IfaceHandle()350     wifiInterfaceHandle IfaceHandle()
351     {
352         return GetIfaceHandle(mIfaceInfo);
353     }
354 
FamilyId()355     int FamilyId()
356     {
357         return mInfo->nl80211FamilyId;
358     }
359 
IfaceId()360     int IfaceId()
361     {
362         return mIfaceInfo->id;
363     }
364 
HandleResponse(WifiEvent & reply)365     virtual int HandleResponse(WifiEvent& reply)
366     {
367         HDF_LOGI("skipping a response");
368         return NL_SKIP;
369     }
370 
HandleEvent(WifiEvent & event)371     virtual int HandleEvent(WifiEvent& event)
372     {
373         HDF_LOGI("skipping an event %{public}d", event.GetCmd());
374         return NL_SKIP;
375     }
376 
RegisterHandler(int cmd)377     int RegisterHandler(int cmd)
378     {
379         return WifiRegisterHandler(WifiHandle(), cmd, &EventHandler, this);
380     }
381 
UnregisterHandler(int cmd)382     void UnregisterHandler(int cmd)
383     {
384         WifiUnregisterHandler(WifiHandle(), cmd);
385     }
386 
RegisterVendorHandler(uint32_t id,int subcmd)387     int RegisterVendorHandler(uint32_t id, int subcmd)
388     {
389         return WifiRegisterVendorHandler(WifiHandle(), id, subcmd, &EventHandler, this);
390     }
391 
UnregisterVendorHandlerWithoutLock(uint32_t id,int subcmd)392     void UnregisterVendorHandlerWithoutLock(uint32_t id, int subcmd)
393     {
394         WifiUnregisterVendorHandlerWithoutLock(WifiHandle(), id, subcmd);
395     }
396 
UnregisterVendorHandler(uint32_t id,int subcmd)397     void UnregisterVendorHandler(uint32_t id, int subcmd)
398     {
399         WifiUnregisterVendorHandler(WifiHandle(), id, subcmd);
400     }
401 
402 protected:
403     const char *mType;
404     HalInfo *mInfo;
405     WifiRequest mMsg;
406     Condition mCondition;
407     int mId;
408     InterfaceInfo *mIfaceInfo;
409     int mRefs;
410 
411 private:
412     WifiCommand(const WifiCommand&);           // hide copy constructor to prevent copies
413     WifiCommand& operator = (const WifiCommand&);
414 
415     static int ResponseHandler(struct nl_msg *msg, void *arg);
416 
417     static int EventHandler(struct nl_msg *msg, void *arg);
418 
419     static int ValidHandler(struct nl_msg *msg, void *arg);
420 
421     static int AckHandler(struct nl_msg *msg, void *arg);
422 
423     static int FinishHandler(struct nl_msg *msg, void *arg);
424 
425     static int ErrorHandler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg);
426 };
427 
428 /* nl message processing macros (required to pass C++ type checks) */
429 
430 #define FOR_EACH_ATTR(pos, nla, rem) \
431     for ((pos) = reinterpret_cast<nlattr *>(nla_data(nla)), (rem) = nla_len(nla); \
432         nla_ok(pos, rem); \
433         pos = reinterpret_cast<nlattr *>(nla_next(pos, &(rem))))
434 
435 extern void InitResponseLock(void);
436 extern void DestroyResponseLock(void);
437 
438 #endif