• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 Huawei Device Co., Ltd.
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 #include "wifiaware.h"
17 #include <string.h>
18 #include "hal_wifiaware.h"
19 #include "wifi_device_util.h"
20 #include "wifi_hotspot.h"
21 
22 #define WIFIAWARE_TX_LOW_POWER (-61)
23 #define SHA256_BYTE_LEN 32
24 #define WIFIAWARE_MAX_POWER (-42)
25 #define WIFIAWARE_MIN_POWER (-70)
26 static signed char g_power = WIFIAWARE_TX_LOW_POWER;
27 
InitNAN(void)28 int InitNAN(void)
29 {
30     int ret;
31     char ifname[WIFI_IFNAME_MAX_SIZE + 1] = {0};
32 
33     if (IsHotspotActive() != WIFI_HOTSPOT_ACTIVE) {
34         return WIFIAWARE_FAIL;
35     }
36 
37     if (GetHotspotChannel() != WIFIAWARE_DEFAULT_CHANNEL) {
38         return WIFIAWARE_FAIL;
39     }
40 
41     ret = GetHotspotInterfaceName(ifname, sizeof(ifname));
42     if (ret != WIFI_SUCCESS) {
43         return WIFIAWARE_FAIL;
44     }
45 
46     if (HalWifiSdpInit(ifname) != 0) {
47         return WIFIAWARE_FAIL;
48     }
49 
50     return WIFIAWARE_SUCCESS;
51 }
52 
SubscribeService(const char * svcName,unsigned char localHandle,RecvCallback recvCB)53 int SubscribeService(const char* svcName, unsigned char localHandle, RecvCallback recvCB)
54 {
55     int svcNameLen;
56     unsigned char shaSvcName[SHA256_BYTE_LEN] = {0};
57 
58     if (svcName == 0 || recvCB == 0) {
59         return WIFIAWARE_FAIL;
60     }
61 
62     svcNameLen = strlen(svcName);
63     if (svcNameLen == 0) {
64         return WIFIAWARE_FAIL;
65     }
66 
67     if (HalCipherHashSha256(svcName, svcNameLen, shaSvcName, sizeof(shaSvcName)) != 0) {
68         return WIFIAWARE_FAIL;
69     }
70 
71     if (HalWifiSdpStartService((char*)shaSvcName, localHandle, (HalRecvCallback)recvCB, HAL_WIFI_SDP_SUBSCRIBE) != 0) {
72         return WIFIAWARE_FAIL;
73     }
74 
75     return WIFIAWARE_SUCCESS;
76 }
77 
SendData(unsigned char * macAddr,unsigned char peerHandle,unsigned char localHandle,unsigned char * msg,int len)78 int SendData(unsigned char* macAddr, unsigned char peerHandle, unsigned char localHandle,
79     unsigned char* msg, int len)
80 {
81     if (HalWifiSdpSend(macAddr, peerHandle, localHandle, msg, len) != 0) {
82         return WIFIAWARE_FAIL;
83     }
84 
85     return WIFIAWARE_SUCCESS;
86 }
87 
StopSubscribe(unsigned char localHandle)88 int StopSubscribe(unsigned char localHandle)
89 {
90     if (HalWifiSdpStopService(localHandle, HAL_WIFI_SDP_SUBSCRIBE) != 0) {
91         return WIFIAWARE_FAIL;
92     }
93 
94     return WIFIAWARE_SUCCESS;
95 }
96 
DeinitNAN(void)97 int DeinitNAN(void)
98 {
99     if (HalWifiSdpDeinit() != 0) {
100         return WIFIAWARE_FAIL;
101     }
102 
103     return WIFIAWARE_SUCCESS;
104 }
105 
SetLowPower(int powerSwitch)106 int SetLowPower(int powerSwitch)
107 {
108     int ret;
109     char ifname[WIFI_IFNAME_MAX_SIZE + 1] = {0};
110 
111     ret = GetHotspotInterfaceName(ifname, sizeof(ifname));
112     if (ret != WIFI_SUCCESS) {
113         return WIFIAWARE_FAIL;
114     }
115 
116     switch (powerSwitch) {
117         case WIFIAWARE_LOW_POWER_SWITCH_ON:
118             if (HalWifiSdpAdjustTxPower(ifname, g_power) != 0) {
119                 return WIFIAWARE_FAIL;
120             }
121             break;
122         case WIFIAWARE_LOW_POWER_SWITCH_OFF:
123             if (HalWifiSdpRestoreTxPower(ifname) != 0) {
124                 return WIFIAWARE_FAIL;
125             }
126             break;
127         default:
128             return WIFIAWARE_FAIL;
129     }
130 
131     return WIFIAWARE_SUCCESS;
132 }
133 
SetPower(signed char value)134 int SetPower(signed char value)
135 {
136     g_power = value;
137     return WIFIAWARE_SUCCESS;
138 }
139 
GetPower(void)140 char GetPower(void)
141 {
142     return g_power;
143 }
144 
NanBeaconSwitch(unsigned char enable)145 int NanBeaconSwitch(unsigned char enable)
146 {
147     char ifname[WIFI_IFNAME_MAX_SIZE + 1] = {0};
148 
149     if (GetHotspotInterfaceName(ifname, sizeof(ifname)) != WIFI_SUCCESS) {
150         return WIFIAWARE_FAIL;
151     }
152     if (HalWifiSdpBeaconSwitch(ifname, enable) != 0) {
153         return WIFIAWARE_FAIL;
154     }
155 
156     return WIFIAWARE_SUCCESS;
157 }
158 
NanSetRetryTimes(unsigned int retries)159 int NanSetRetryTimes(unsigned int retries)
160 {
161     if (HalWifiSdpSetRetryTimes(retries) != 0) {
162         return WIFIAWARE_FAIL;
163     }
164 
165     return WIFIAWARE_SUCCESS;
166 }
167 
NanGetSyncMode(void)168 int NanGetSyncMode(void)
169 {
170     return HalWifiSdpGetSyncMode();
171 }
172