• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 /**
17  * @addtogroup Bluetooth
18  * @{
19  *
20  * @brief Defines map mns object.
21  *
22  */
23 
24 /**
25  * @file map_mce_mns_server.cpp
26  *
27  * @brief map mns source file .
28  *
29  */
30 
31 #include "map_mce_mns_server.h"
32 #include "btm.h"
33 #include "log.h"
34 #include "map_mce_instance_stm.h"
35 #include "rfcomm.h"
36 #include "sdp.h"
37 
38 namespace OHOS {
39 namespace bluetooth {
MapMceMnsServer(MapMceService & service,MasInstanceConfig config)40 MapMceMnsServer::MapMceMnsServer(MapMceService &service, MasInstanceConfig config)
41     : mceService_(service), mnsConfig_(config)
42 {
43     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
44     rfcommNo_ = 0;
45     sdpHandle_ = 0;
46     obexServer_ = nullptr;
47     mceMnsObexObserver_ = nullptr;
48     goepL2capPsm_ = MCE_MNS_GOEP_L2CAP_PSM_VALUE;
49     mnsStatus_ = MAP_MCE_STATE_SHUTDOWN;
50 }
51 
~MapMceMnsServer()52 MapMceMnsServer::~MapMceMnsServer()
53 {
54     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
55     mnsStatus_ = MapMceServiceStateType::MAP_MCE_STATE_SHUTDOWN;
56     mceMnsObexObserver_ = nullptr;
57 }
58 
StartUp(void)59 int MapMceMnsServer::StartUp(void)
60 {
61     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
62     int ret = CreateMasSdpRecord();
63     if (ret != BT_SUCCESS) {
64         LOG_ERROR("%{public}s CreateMasSdpRecord error!", __PRETTY_FUNCTION__);
65         return RET_NO_SUPPORT;
66     }
67     ret = RegisterServiceSecurity();
68     if (ret != BT_SUCCESS) {
69         DestroyMasSdpRecord();
70         LOG_ERROR("%{public}s RegisterServiceSecurity error!", __PRETTY_FUNCTION__);
71         return RET_NO_SUPPORT;
72     }
73     BTM_AddLocalL2capPsmForLogging(BTM_HCI_LOG_FILTER_MODULE_MAP, goepL2capPsm_);
74     BTM_AddLocalRfcommScnForLogging(BTM_HCI_LOG_FILTER_MODULE_MAP, rfcommNo_);
75     ObexServerConfig obexSrvConfig;
76     obexSrvConfig.useRfcomm_ = true;
77     obexSrvConfig.rfcommScn_ = rfcommNo_;
78     obexSrvConfig.rfcommMtu_ = mnsConfig_.rfcommMtu;
79 #ifdef MCE_DISABLE_L2CAP
80     obexSrvConfig.useL2cap_ = false;
81 #else
82     obexSrvConfig.useL2cap_ = true;
83 #endif
84     obexSrvConfig.l2capPsm_ = goepL2capPsm_;
85     obexSrvConfig.l2capMtu_ = mnsConfig_.l2capMtu;
86     obexSrvConfig.isSupportSrm_ = true;
87     obexSrvConfig.isSupportReliableSession_ = false;
88     mceMnsObexObserver_ = std::make_unique<MceMnsObexServerObserver>(*this);
89     obexServer_ = std::make_unique<ObexMpServer>(
90         MCE_MNS_SERVICE_NAME, obexSrvConfig, *mceMnsObexObserver_, *mceService_.GetDispatcher());
91     ret = obexServer_->Startup();
92     mnsStatus_ = MapMceServiceStateType::MAP_MCE_STATE_STARTUP;
93     if (ret != BT_SUCCESS) {
94         ret = RET_BAD_STATUS;
95         mnsStatus_ = MapMceServiceStateType::MAP_MCE_STATE_SHUTDOWN;
96         DestroyMasSdpRecord();
97         DeregisterServiceSecurity();
98         BTM_RemoveLocalL2capPsmForLogging(BTM_HCI_LOG_FILTER_MODULE_MAP, goepL2capPsm_);
99         BTM_RemoveLocalRfcommScnChannelForLogging(BTM_HCI_LOG_FILTER_MODULE_MAP, rfcommNo_);
100         LOG_ERROR("%{public}s obex server startup error!", __PRETTY_FUNCTION__);
101     }
102     return ret;
103 }
104 
ShutDown(void)105 void MapMceMnsServer::ShutDown(void)
106 {
107     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
108     if (mnsStatus_ == MapMceServiceStateType::MAP_MCE_STATE_STARTUP) {
109         obexServer_->Shutdown();
110         DeregisterServiceSecurity();
111         DestroyMasSdpRecord();
112         BTM_RemoveLocalL2capPsmForLogging(BTM_HCI_LOG_FILTER_MODULE_MAP, goepL2capPsm_);
113         BTM_RemoveLocalRfcommScnChannelForLogging(BTM_HCI_LOG_FILTER_MODULE_MAP, rfcommNo_);
114         mnsStatus_ = MapMceServiceStateType::MAP_MCE_STATE_SHUTDOWN;
115     }
116 }
117 
AddServiceClassIdList(void)118 int MapMceMnsServer::AddServiceClassIdList(void)
119 {
120     LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
121 
122     BtUuid btUuid;
123     btUuid.type = BT_UUID_16;
124     btUuid.uuid16 = MCE_MNS_SERVICE_CLASS_UUID;
125     int ret = SDP_AddServiceClassIdList(sdpHandle_, &btUuid, MCE_MNS_SERVICE_CLASS_ID_NUMBER);
126     if (ret != BT_SUCCESS) {
127         LOG_ERROR("mce mns Call SDP_AddServiceClassIdList Error");
128         return RET_NO_SUPPORT;
129     }
130     return BT_SUCCESS;
131 }
132 
AddProtocolDescriptorList(void)133 int MapMceMnsServer::AddProtocolDescriptorList(void)
134 {
135     int index = 0;
136     rfcommNo_ = RFCOMM_AssignServerNum();
137     SdpProtocolDescriptor descriptor[MCE_MNS_PROTOCOL_DESCRIPTOR_NUMBER];
138     descriptor[index].protocolUuid.type = BT_UUID_16;
139     descriptor[index].protocolUuid.uuid16 = UUID_PROTOCOL_L2CAP;
140     descriptor[index].parameterNumber = 0;
141     index++;
142     descriptor[index].protocolUuid.type = BT_UUID_16;
143     descriptor[index].protocolUuid.uuid16 = UUID_PROTOCOL_RFCOMM;
144     descriptor[index].parameterNumber = MCE_MNS_PROTOCOL_DESCRIPTOR_PARAMETER_NUMBER;
145     descriptor[index].parameter[0].type = SDP_TYPE_UINT_8;
146     descriptor[index].parameter[0].value = rfcommNo_;
147     index++;
148     descriptor[index].protocolUuid.type = BT_UUID_16;
149     descriptor[index].protocolUuid.uuid16 = UUID_PROTOCOL_OBEX;
150     descriptor[index].parameterNumber = 0;
151 
152     int ret = SDP_AddProtocolDescriptorList(sdpHandle_, descriptor, MCE_MNS_PROTOCOL_DESCRIPTOR_NUMBER);
153     if (ret != BT_SUCCESS) {
154         LOG_ERROR("mce mns Call SDP_AddProtocolDescriptorList Error");
155         return RET_NO_SUPPORT;
156     }
157     LOG_INFO("%{public}s execute,rfcomm = %{public}d", __PRETTY_FUNCTION__, int(rfcommNo_));
158     return BT_SUCCESS;
159 }
160 
AddServiceName(void)161 int MapMceMnsServer::AddServiceName(void)
162 {
163     LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
164     int ret = SDP_AddServiceName(
165         sdpHandle_, SDP_ATTRIBUTE_PRIMARY_LANGUAGE_BASE, MCE_MNS_SERVICE_NAME.c_str(), MCE_MNS_SERVICE_NAME.size());
166     if (ret != BT_SUCCESS) {
167         LOG_ERROR("mce mns Call SDP_AddServiceName Error");
168         return RET_NO_SUPPORT;
169     }
170     return BT_SUCCESS;
171 }
172 
AddBluetoothProfileDescriptorList(void)173 int MapMceMnsServer::AddBluetoothProfileDescriptorList(void)
174 {
175     LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
176 
177     SdpProfileDescriptor profileDescriptor;
178     profileDescriptor.profileUuid.type = BT_UUID_16;
179     profileDescriptor.profileUuid.uuid16 = MCE_MNS_PROFILE_DESCRIPTOR_UUID;
180     profileDescriptor.versionNumber = MCE_MNS_PROFILE_VERSION;
181 
182     int ret = SDP_AddBluetoothProfileDescriptorList(sdpHandle_, &profileDescriptor, MCE_MNS_PROFILE_DESCRIPTOR_NUMBER);
183     if (ret != BT_SUCCESS) {
184         LOG_ERROR("mce mns Call SDP_AddBluetoothProfileDescriptorList Error");
185         return RET_NO_SUPPORT;
186     }
187     return BT_SUCCESS;
188 }
189 
AddAttribute(void)190 int MapMceMnsServer::AddAttribute(void)
191 {
192     LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
193 
194     int ret = SDP_AddAttribute(sdpHandle_,
195         MCE_MNS_GOEP_L2CAP_PSM_ATTRIBUTE_ID,
196         SdpDataType::SDP_TYPE_UINT_16,
197         (uint8_t *)&goepL2capPsm_,
198         sizeof(uint16_t));
199     if (ret != BT_SUCCESS) {
200         LOG_ERROR("map mce mns Call SDP_AddAttribute Error");
201         return RET_NO_SUPPORT;
202     }
203     ret = SDP_AddAttribute(sdpHandle_,
204         MCE_MNS_SUPPORTED_FEATURES_ATTRIBUTE_ID,
205         SdpDataType::SDP_TYPE_UINT_32,
206         (uint8_t *)&MCE_MNS_SUPPORTED_FEATURES_V14,
207         sizeof(uint32_t));
208     if (ret != BT_SUCCESS) {
209         LOG_ERROR("map mce mns Call SDP_AddAttribute Error");
210         return RET_NO_SUPPORT;
211     }
212     return BT_SUCCESS;
213 }
214 
RegisterServiceRecord(void)215 int MapMceMnsServer::RegisterServiceRecord(void)
216 {
217     LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
218 
219     int ret = SDP_RegisterServiceRecord(sdpHandle_);
220     if (ret != BT_SUCCESS) {
221         LOG_ERROR("mce mns Call SDP_RegisterServiceRecord Error");
222         return RET_NO_SUPPORT;
223     }
224     return BT_SUCCESS;
225 }
226 
CreateMasSdpRecord(void)227 int MapMceMnsServer::CreateMasSdpRecord(void)
228 {
229     LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
230 
231     int retVal = BT_SUCCESS;
232     sdpHandle_ = SDP_CreateServiceRecord();
233     retVal |= AddServiceClassIdList();
234     retVal |= AddProtocolDescriptorList();
235     retVal |= AddServiceName();
236     retVal |= AddBluetoothProfileDescriptorList();
237     retVal |= AddAttribute();
238     if (retVal == BT_SUCCESS) {
239         retVal = RegisterServiceRecord();
240     } else {
241         retVal = RET_NO_SUPPORT;
242     }
243     return retVal;
244 }
245 
DestroyMasSdpRecord(void)246 void MapMceMnsServer::DestroyMasSdpRecord(void)
247 {
248     LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
249 
250     if (SDP_DeregisterServiceRecord(sdpHandle_)) {
251         LOG_ERROR("Call SDP_DeregisterServiceRecord Error");
252     }
253     if (RFCOMM_FreeServerNum(rfcommNo_)) {
254         LOG_ERROR("Call RFCOMM_FreeServerNum Error");
255     }
256     if (SDP_DestroyServiceRecord(sdpHandle_)) {
257         LOG_ERROR("Call SDP_DestroyServiceRecord Error");
258     }
259 }
260 
RegisterServiceSecurity(void) const261 int MapMceMnsServer::RegisterServiceSecurity(void) const
262 {
263     LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
264 
265     // register for l2cap
266     GapSecChannel secChannel;
267     secChannel.l2capPsm = goepL2capPsm_;
268     GapServiceSecurityInfo serviceInfo;
269     serviceInfo.direction = INCOMING;
270     serviceInfo.serviceId = GAP_Service(MAP_CLIENT_ID_START + MAP_MAX_COUNT);
271     serviceInfo.protocolId = GAP_SecMultiplexingProtocol::SEC_PROTOCOL_L2CAP;
272     serviceInfo.channelId = secChannel;
273 
274     int ret = GAPIF_RegisterServiceSecurity(nullptr,
275         &serviceInfo,
276         GAP_SEC_IN_AUTHENTICATION | GAP_SEC_IN_ENCRYPTION | GAP_SEC_OUT_AUTHENTICATION | GAP_SEC_OUT_ENCRYPTION);
277     if (ret != BT_SUCCESS) {
278         LOG_ERROR("map mce mns Call GAP_RegisterServiceSecurity l2cap Error");
279         return RET_NO_SUPPORT;
280     }
281     // register for rfcomm
282     secChannel.rfcommChannel = rfcommNo_;
283     serviceInfo.channelId = secChannel;
284     serviceInfo.protocolId = GAP_SecMultiplexingProtocol::SEC_PROTOCOL_RFCOMM;
285 
286     ret = GAPIF_RegisterServiceSecurity(nullptr,
287         &serviceInfo,
288         GAP_SEC_IN_AUTHENTICATION | GAP_SEC_IN_ENCRYPTION | GAP_SEC_OUT_AUTHENTICATION | GAP_SEC_OUT_ENCRYPTION);
289     if (ret != BT_SUCCESS) {
290         LOG_ERROR("map mce mns Call GAP_RegisterServiceSecurity Error rfcomm");
291         return RET_NO_SUPPORT;
292     }
293     return BT_SUCCESS;
294 }
295 
DeregisterServiceSecurity(void) const296 void MapMceMnsServer::DeregisterServiceSecurity(void) const
297 {
298     LOG_INFO("%{public}s Enter", __PRETTY_FUNCTION__);
299 
300     // deregister for l2cap
301     GapSecChannel secChannel;
302     secChannel.l2capPsm = goepL2capPsm_;
303 
304     GapServiceSecurityInfo serviceInfo;
305     serviceInfo.direction = INCOMING;
306     serviceInfo.serviceId = GAP_Service(MAP_CLIENT_ID_START + MAP_MAX_COUNT),
307     serviceInfo.protocolId = GAP_SecMultiplexingProtocol::SEC_PROTOCOL_L2CAP;
308     serviceInfo.channelId = secChannel;
309 
310     int ret = GAPIF_DeregisterServiceSecurity(nullptr, &serviceInfo);
311     if (ret != BT_SUCCESS) {
312         LOG_ERROR("MAP mce mns Call GAP_UnregisterServiceSecurity l2cap Error");
313     }
314     // deregister for rfcomm
315     secChannel.rfcommChannel = rfcommNo_;
316     serviceInfo.channelId = secChannel;
317     serviceInfo.protocolId = GAP_SecMultiplexingProtocol::SEC_PROTOCOL_RFCOMM;
318     ret = GAPIF_DeregisterServiceSecurity(nullptr, &serviceInfo);
319     if (ret != BT_SUCCESS) {
320         LOG_ERROR("MAP mce mns Call GAPIF_UnregisterServiceSecurity rfcomm Error");
321     }
322 }
323 
~MceMnsObexServerObserver()324 MapMceMnsServer::MceMnsObexServerObserver::~MceMnsObexServerObserver()
325 {
326     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
327 }
328 
OnTransportConnect(ObexIncomingConnect & incomingConnect)329 void MapMceMnsServer::MceMnsObexServerObserver::OnTransportConnect(ObexIncomingConnect &incomingConnect)
330 {
331     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
332     if (mceMnsServer_.mnsStatus_ == MapMceServiceStateType::MAP_MCE_STATE_STARTUP) {
333         incomingConnect.GetRemoteAddress().GetAddress();
334         utility::Message outMsg(MSG_MCEDEVICE_MNS_INFO_TRANSPORT_INCOMING);
335         outMsg.arg2_ = (void *)(&incomingConnect);
336         mceMnsServer_.mceService_.MnsPostMessage(outMsg, incomingConnect.GetRemoteAddress().GetAddress());
337     } else {
338         incomingConnect.RejectConnection();
339     }
340 }
341 
OnTransportDisconnected(const std::string & btAddr)342 void MapMceMnsServer::MceMnsObexServerObserver::OnTransportDisconnected(const std::string &btAddr)
343 {
344     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
345     utility::Message outMsg(MSG_MCEDEVICE_MNS_INFO_TRANSPORT_DISCONNECTED);
346     mceMnsServer_.mceService_.MnsPostMessage(outMsg, btAddr);
347 }
348 
OnTransportError(const std::string & btAddr,int errCd,const std::string & msg)349 void MapMceMnsServer::MceMnsObexServerObserver::OnTransportError(
350     const std::string &btAddr, int errCd, const std::string &msg)
351 {
352     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
353     utility::Message outMsg(MSG_MCEDEVICE_MNS_INFO_TRANSPORT_ERROR);
354     outMsg.arg1_ = errCd;
355     mceMnsServer_.mceService_.MnsPostMessage(outMsg, btAddr);
356 }
357 
OnConnect(ObexServerSession & session,const ObexHeader & req)358 void MapMceMnsServer::MceMnsObexServerObserver::OnConnect(ObexServerSession &session, const ObexHeader &req)
359 {
360     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
361     std::string btDevice = session.GetRemoteAddr().GetAddress();
362     utility::Message outMsg(MSG_MCEDEVICE_MNS_INFO_CONNECT);
363     outMsg.arg2_ = (void *)(&session);
364     mceMnsServer_.mceService_.MnsPostMessageWithHeader(outMsg, btDevice, req);
365 }
366 
OnDisconnect(ObexServerSession & session,const ObexHeader & req)367 void MapMceMnsServer::MceMnsObexServerObserver::OnDisconnect(ObexServerSession &session, const ObexHeader &req)
368 {
369     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
370     std::string btDevice = session.GetRemoteAddr().GetAddress();
371     utility::Message outMsg(MSG_MCEDEVICE_MNS_INFO_DISCONNECT);
372     outMsg.arg2_ = (void *)(&session);
373     mceMnsServer_.mceService_.MnsPostMessageWithHeader(outMsg, btDevice, req);
374 }
375 
OnPut(ObexServerSession & session,const ObexHeader & req)376 void MapMceMnsServer::MceMnsObexServerObserver::OnPut(ObexServerSession &session, const ObexHeader &req)
377 {
378     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
379     std::string btDevice = session.GetRemoteAddr().GetAddress();
380     utility::Message outMsg(MSG_MCEDEVICE_MNS_INFO_PUT);
381     mceMnsServer_.mceService_.MnsPostMessageWithHeader(outMsg, btDevice, req);
382 }
383 
OnBusy(ObexServerSession & session,bool isBusy) const384 void MapMceMnsServer::MceMnsObexServerObserver::OnBusy(ObexServerSession &session, bool isBusy) const
385 {
386     LOG_INFO("%{public}s execute", __PRETTY_FUNCTION__);
387     std::string btDevice = session.GetRemoteAddr().GetAddress();
388     utility::Message outMsg(MSG_MCEDEVICE_MNS_INFO_BUSY_STATUS_CHANGE);
389     outMsg.arg1_ = int(isBusy);
390     outMsg.arg2_ = (void *)(&session);
391     mceMnsServer_.mceService_.MnsPostMessage(outMsg, btDevice);
392 }
393 }  // namespace bluetooth
394 }  // namespace OHOS
395