1 /*
2 * Copyright (C) 2021-2022 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 <sys/socket.h>
17 #include <string>
18 #include <unistd.h>
19 #include "bluetooth_log.h"
20 #include "bluetooth_host.h"
21 #include "bluetooth_host_proxy.h"
22 #include "bluetooth_socket_proxy.h"
23 #include "bluetooth_socket.h"
24 #include "hisysevent.h"
25 #include "ipc_skeleton.h"
26 #include "iservice_registry.h"
27 #include "securec.h"
28 #include "system_ability_definition.h"
29 #include "raw_address.h"
30
31 namespace OHOS {
32 namespace Bluetooth {
33 const int LENGTH = 18;
34 struct SppClientSocket::impl {
35 impl(const BluetoothRemoteDevice &addr, UUID uuid, SppSocketType type, bool auth);
36 impl(int fd, std::string address);
~implOHOS::Bluetooth::SppClientSocket::impl37 ~impl()
38 {
39 if (!proxy_) {
40 HILOGI("failed: no proxy_");
41 return;
42 }
43
44 proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
45
46 if (fd_ > 0) {
47 shutdown(fd_, SHUT_RD);
48 shutdown(fd_, SHUT_WR);
49 HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::BLUETOOTH, "SPP_CONNECT_STATE",
50 HiviewDFX::HiSysEvent::EventType::STATISTIC, "ACTION", "close", "ID", fd_, "ADDRESS", "empty",
51 "PID", IPCSkeleton::GetCallingPid(), "UID", IPCSkeleton::GetCallingUid());
52 HILOGI("fd closed, fd_: %{pubilc}d", fd_);
53 close(fd_);
54 fd_ = -1;
55 }
56 }
57
CloseOHOS::Bluetooth::SppClientSocket::impl58 void Close()
59 {
60 HILOGI("starts");
61 if (socketStatus_ == SOCKET_CLOSED) {
62 HILOGW("The socketStatus_ is already SOCKET_CLOSED");
63 return;
64 } else {
65 socketStatus_ = SOCKET_CLOSED;
66 if (fd_ > 0) {
67 shutdown(fd_, SHUT_RD);
68 shutdown(fd_, SHUT_WR);
69 HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::BLUETOOTH, "SPP_CONNECT_STATE",
70 HiviewDFX::HiSysEvent::EventType::STATISTIC, "ACTION", "close", "ID", fd_, "ADDRESS", "empty",
71 "PID", IPCSkeleton::GetCallingPid(), "UID", IPCSkeleton::GetCallingUid());
72 HILOGI("fd closed, fd_: %{pubilc}d", fd_);
73 close(fd_);
74 fd_ = -1;
75 } else {
76 HILOGE("socket not created");
77 return;
78 }
79 }
80 }
81
RecvSocketSignalOHOS::Bluetooth::SppClientSocket::impl82 bool RecvSocketSignal()
83 {
84 uint8_t recvStateBuf[1];
85 #ifdef DARWIN_PLATFORM
86 int recvBufSize = recv(fd_, recvStateBuf, sizeof(recvStateBuf), 0);
87 #else
88 int recvBufSize = recv(fd_, recvStateBuf, sizeof(recvStateBuf), MSG_WAITALL);
89 #endif
90 if (recvBufSize <= 0) {
91 HILOGE("service closed");
92 return false;
93 }
94 bool state = recvStateBuf[0];
95
96 uint8_t buf[6] = {0}; // addr buffer len
97 #ifdef DARWIN_PLATFORM
98 int recvAddrSize = recv(fd_, buf, sizeof(buf), 0);
99 #else
100 int recvAddrSize = recv(fd_, buf, sizeof(buf), MSG_WAITALL);
101 #endif
102 if (recvAddrSize <= 0) {
103 HILOGE("service closed");
104 return false;
105 }
106 char token[LENGTH] = {0};
107 (void)sprintf_s(token,
108 sizeof(token), "%02X:%02X:%02X:%02X:%02X:%02X", buf[0x05], buf[0x04], buf[0x03], buf[0x02], buf[0x01], buf[0x00]);
109 BluetoothRawAddress rawAddr {
110 token
111 };
112 std::string address = rawAddr.GetAddress().c_str();
113 return state;
114 }
115
getSecurityFlagsOHOS::Bluetooth::SppClientSocket::impl116 int getSecurityFlags()
117 {
118 int flags = 0;
119 if (auth_) {
120 flags |= FLAG_AUTH;
121 flags |= FLAG_ENCRYPT;
122 }
123 return flags;
124 }
125
GetInputStreamOHOS::Bluetooth::SppClientSocket::impl126 InputStream &GetInputStream()
127 {
128 HILOGI("starts");
129 if (inputStream_ == nullptr) {
130 HILOGE("inputStream is NULL, failed. please Connect");
131 }
132 return *inputStream_;
133 }
134
GetOutputStreamOHOS::Bluetooth::SppClientSocket::impl135 OutputStream &GetOutputStream()
136 {
137 HILOGI("starts");
138 if (outputStream_ == nullptr) {
139 HILOGE("outputStream is NULL, failed. please Connect");
140 }
141 return *outputStream_;
142 }
143
GetRemoteDeviceOHOS::Bluetooth::SppClientSocket::impl144 BluetoothRemoteDevice &GetRemoteDevice()
145 {
146 HILOGI("starts");
147 return remoteDevice_;
148 }
149
IsConnectedOHOS::Bluetooth::SppClientSocket::impl150 bool IsConnected()
151 {
152 HILOGI("starts");
153 return socketStatus_ == SOCKET_CONNECTED;
154 }
155
156 class BluetoothClientSocketDeathRecipient;
157 sptr<BluetoothClientSocketDeathRecipient> deathRecipient_;
158
159 sptr<IBluetoothSocket> proxy_;
160 std::unique_ptr<InputStream> inputStream_ {
161 nullptr
162 };
163 std::unique_ptr<OutputStream> outputStream_ {
164 nullptr
165 };
166 BluetoothRemoteDevice remoteDevice_;
167 UUID uuid_;
168 SppSocketType type_;
169 std::string address_;
170 int fd_;
171 bool auth_;
172 int socketStatus_;
173 };
174
175 class SppClientSocket::impl::BluetoothClientSocketDeathRecipient final : public IRemoteObject::DeathRecipient {
176 public:
BluetoothClientSocketDeathRecipient(SppClientSocket::impl & host)177 BluetoothClientSocketDeathRecipient(SppClientSocket::impl &host) : host_(host) {};
178 ~BluetoothClientSocketDeathRecipient() final = default;
179 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothClientSocketDeathRecipient);
180
OnRemoteDied(const wptr<IRemoteObject> & remote)181 void OnRemoteDied(const wptr<IRemoteObject> &remote) final
182 {
183 HILOGI("starts");
184 host_.proxy_->AsObject()->RemoveDeathRecipient(host_.deathRecipient_);
185 host_.proxy_ = nullptr;
186 }
187
188 private:
189 SppClientSocket::impl &host_;
190 };
191
impl(const BluetoothRemoteDevice & addr,UUID uuid,SppSocketType type,bool auth)192 SppClientSocket::impl::impl(const BluetoothRemoteDevice &addr, UUID uuid, SppSocketType type, bool auth)
193 : inputStream_(nullptr),
194 outputStream_(nullptr),
195 remoteDevice_(addr),
196 uuid_(uuid),
197 type_(type),
198 fd_(-1),
199 auth_(auth),
200 socketStatus_(SOCKET_INIT)
201 {
202 HILOGI("starts");
203 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
204 sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
205
206 if (!hostRemote) {
207 HILOGI("failed: no hostRemote");
208 return;
209 }
210
211 sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
212 sptr<IRemoteObject> remote = hostProxy->GetProfile(PROFILE_SPP);
213
214 if (!remote) {
215 HILOGI("failed: no remote");
216 return;
217 }
218 HILOGI("remote obtained");
219
220 proxy_ = iface_cast<IBluetoothSocket>(remote);
221
222 deathRecipient_ = new BluetoothClientSocketDeathRecipient(*this);
223
224 if (!proxy_) {
225 HILOGE("proxy_ is nullptr");
226 return;
227 }
228
229 proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
230 }
231
impl(int fd,std::string address)232 SppClientSocket::impl::impl(int fd, std::string address)
233 : inputStream_(std::make_unique<InputStream>(fd)),
234 outputStream_(std::make_unique<OutputStream>(fd)),
235 remoteDevice_(BluetoothRemoteDevice(address, 0)),
236 type_(TYPE_RFCOMM),
237 address_(address),
238 fd_(fd),
239 auth_(false),
240 socketStatus_(SOCKET_CONNECTED)
241 {
242 HILOGI("starts");
243 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
244 sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
245
246 if (!hostRemote) {
247 HILOGI("failed: no hostRemote");
248 return;
249 }
250
251 sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
252 sptr<IRemoteObject> remote = hostProxy->GetProfile(PROFILE_SPP);
253
254 if (!remote) {
255 HILOGI("failed: no remote");
256 return;
257 }
258 HILOGI("remote obtained");
259
260 proxy_ = iface_cast<IBluetoothSocket>(remote);
261
262 deathRecipient_ = new BluetoothClientSocketDeathRecipient(*this);
263
264 if (!proxy_) {
265 HILOGE("proxy_ is nullptr");
266 return;
267 }
268
269 proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
270 }
271
SppClientSocket(const BluetoothRemoteDevice & bda,UUID uuid,SppSocketType type,bool auth)272 SppClientSocket::SppClientSocket(const BluetoothRemoteDevice &bda, UUID uuid, SppSocketType type, bool auth)
273 : pimpl(new SppClientSocket::impl(bda, uuid, type, auth))
274 {}
275
SppClientSocket(int fd,std::string address)276 SppClientSocket::SppClientSocket(int fd, std::string address) : pimpl(new SppClientSocket::impl(fd, address))
277 {}
278
~SppClientSocket()279 SppClientSocket::~SppClientSocket()
280 {}
281
Connect()282 int SppClientSocket::Connect()
283 {
284 HILOGI("starts");
285 if (!IS_BT_ENABLED()) {
286 HILOGI("BR is not TURN_ON");
287 return BtStatus::BT_FAILURE;
288 }
289 pimpl->address_ = pimpl->remoteDevice_.GetDeviceAddr();
290
291 std::string tempAddress = pimpl->address_;
292 if (!tempAddress.size()) {
293 return BtStatus::BT_FAILURE;
294 }
295 if (pimpl->socketStatus_ == SOCKET_CLOSED) {
296 HILOGE("socket closed");
297 return BtStatus::BT_FAILURE;
298 }
299
300 bluetooth::Uuid serverUuid = bluetooth::Uuid::ConvertFrom128Bits(pimpl->uuid_.ConvertTo128Bits());
301
302 if (!pimpl->proxy_) {
303 HILOGE("proxy_ is nullptr");
304 return BtStatus::BT_FAILURE;
305 }
306
307 pimpl->fd_ =
308 pimpl->proxy_->Connect(tempAddress, serverUuid, (int32_t)pimpl->getSecurityFlags(), (int32_t)pimpl->type_);
309
310 HILOGI("fd_: %{public}d", pimpl->fd_);
311 if (pimpl->fd_ == -1) {
312 HILOGE("connect failed!");
313 return BtStatus::BT_FAILURE;
314 }
315
316 bool recvret = pimpl->RecvSocketSignal();
317 HILOGI("recvret: %{public}d", recvret);
318 pimpl->inputStream_ = std::make_unique<InputStream>(pimpl->fd_);
319 pimpl->outputStream_ = std::make_unique<OutputStream>(pimpl->fd_);
320
321 if (!recvret) {
322 HILOGE("connect failed!");
323 return BtStatus::BT_FAILURE;
324 }
325 pimpl->socketStatus_ = SOCKET_CONNECTED;
326 HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::BLUETOOTH, "SPP_CONNECT_STATE",
327 HiviewDFX::HiSysEvent::EventType::STATISTIC, "ACTION", "connect", "ID", pimpl->fd_, "ADDRESS", tempAddress,
328 "PID", IPCSkeleton::GetCallingPid(), "UID", IPCSkeleton::GetCallingUid());
329 return BtStatus::BT_SUCC;
330 }
331
Close()332 void SppClientSocket::Close()
333 {
334 HILOGI("starts");
335 return pimpl->Close();
336 }
337
GetInputStream()338 InputStream &SppClientSocket::GetInputStream()
339 {
340 HILOGI("starts");
341 return pimpl->GetInputStream();
342 }
343
GetOutputStream()344 OutputStream &SppClientSocket::GetOutputStream()
345 {
346 HILOGI("starts");
347 return pimpl->GetOutputStream();
348 }
349
GetRemoteDevice()350 BluetoothRemoteDevice &SppClientSocket::GetRemoteDevice()
351 {
352 HILOGI("starts");
353 return pimpl->GetRemoteDevice();
354 }
355
IsConnected() const356 bool SppClientSocket::IsConnected() const
357 {
358 HILOGI("starts");
359 return pimpl->IsConnected();
360 }
361
362 struct SppServerSocket::impl {
363 impl(const std::string &name, UUID uuid, SppSocketType type, bool encrypt);
~implOHOS::Bluetooth::SppServerSocket::impl364 ~impl()
365 {
366 if (!proxy_) {
367 HILOGE("proxy_ is nullptr");
368 return;
369 }
370
371 proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
372
373 if (fd_ > 0) {
374 shutdown(fd_, SHUT_RD);
375 shutdown(fd_, SHUT_WR);
376 close(fd_);
377 HILOGI("fd closed, fd_: %{public}d", fd_);
378 fd_ = -1;
379 }
380 }
381
ListenOHOS::Bluetooth::SppServerSocket::impl382 int Listen()
383 {
384 HILOGI("starts");
385 if (!IS_BT_ENABLED()) {
386 HILOGI("BR is not TURN_ON");
387 return BtStatus::BT_FAILURE;
388 }
389 if (socketStatus_ == SOCKET_CLOSED) {
390 HILOGE("failed, socketStatus_ is SOCKET_CLOSED");
391 return BtStatus::BT_FAILURE;
392 }
393
394 bluetooth::Uuid serverUuid = bluetooth::Uuid::ConvertFrom128Bits(uuid_.ConvertTo128Bits());
395
396 if (!proxy_) {
397 HILOGE("failed, proxy_ is nullptr");
398 socketStatus_ = SOCKET_CLOSED;
399 return BtStatus::BT_FAILURE;
400 }
401
402 fd_ = proxy_->Listen(name_, serverUuid, (int32_t)getSecurityFlags(), (int32_t)type_);
403 if (fd_ == BT_INVALID_SOCKET_FD) {
404 HILOGE("listen socket failed, fd_ is -1");
405 socketStatus_ = SOCKET_CLOSED;
406 return BtStatus::BT_FAILURE;
407 }
408
409 if (socketStatus_ == SOCKET_INIT) {
410 socketStatus_ = SOCKET_LISTENING;
411 } else {
412 HILOGE("failed, socketStatus_: %{public}d is not SOCKET_INIT", socketStatus_);
413 close(fd_);
414 socketStatus_ = SOCKET_CLOSED;
415 return BtStatus::BT_FAILURE;
416 }
417
418 return BtStatus::BT_SUCC;
419 }
420
getSecurityFlagsOHOS::Bluetooth::SppServerSocket::impl421 int getSecurityFlags()
422 {
423 int flags = 0;
424 if (encrypt_) {
425 flags |= FLAG_AUTH;
426 flags |= FLAG_ENCRYPT;
427 }
428 return flags;
429 }
430
AcceptOHOS::Bluetooth::SppServerSocket::impl431 std::unique_ptr<SppClientSocket> Accept(int timeout)
432 {
433 HILOGI("starts");
434 if (socketStatus_ != SOCKET_LISTENING) {
435 HILOGE("socket is not in listen state");
436 return nullptr;
437 }
438 struct timeval time = {timeout, 0};
439 setsockopt(fd_, SOL_SOCKET, SO_SNDTIMEO, (const char *)&time, sizeof(time));
440 setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, (const char *)&time, sizeof(time));
441
442 acceptFd_ = RecvSocketFd();
443 HILOGE("RecvSocketFd acceptFd: %{public}d", acceptFd_);
444 if (acceptFd_ <= 0) {
445 return nullptr;
446 }
447 if (timeout > 0) {
448 time = {0, 0};
449 setsockopt(fd_, SOL_SOCKET, SO_SNDTIMEO, (const char *)&time, sizeof(time));
450 setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, (const char *)&time, sizeof(time));
451 }
452
453 std::unique_ptr<SppClientSocket> clientSocket = std::make_unique<SppClientSocket>(acceptFd_, acceptAddress_);
454
455 HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::BLUETOOTH, "SPP_CONNECT_STATE",
456 HiviewDFX::HiSysEvent::EventType::STATISTIC, "ACTION", "connect", "ID", acceptFd_, "ADDRESS",
457 acceptAddress_, "PID", IPCSkeleton::GetCallingPid(), "UID", IPCSkeleton::GetCallingUid());
458
459 return clientSocket;
460 }
461
RecvSocketFdOHOS::Bluetooth::SppServerSocket::impl462 int RecvSocketFd()
463 {
464 HILOGI("starts");
465 int rv = 0;
466 int cfd = -1;
467 int clientFd = -1;
468 char ccmsg[CMSG_SPACE(sizeof(cfd))];
469 char buffer[10];
470 struct iovec io = {.iov_base = buffer, .iov_len = sizeof(buffer)};
471 struct msghdr msg;
472 (void)memset_s(&msg, sizeof(msg), 0, sizeof(msg));
473 msg.msg_control = ccmsg;
474 msg.msg_controllen = sizeof(ccmsg);
475 msg.msg_iov = &io;
476 msg.msg_iovlen = 1;
477
478 #ifdef DARWIN_PLATFORM
479 rv = recvmsg(fd_, &msg, 0);
480 #else
481 rv = recvmsg(fd_, &msg, MSG_NOSIGNAL);
482 #endif
483 if (rv == -1) {
484 HILOGE("[sock] recvmsg error %{public}d, fd: %{public}d", errno, fd_);
485 return BtStatus::BT_FAILURE;
486 }
487 struct cmsghdr *cmptr = CMSG_FIRSTHDR(&msg);
488 if ((cmptr != NULL) && (cmptr->cmsg_len == CMSG_LEN(sizeof(int)))) {
489 if (cmptr->cmsg_level != SOL_SOCKET || cmptr->cmsg_type != SCM_RIGHTS) {
490 HILOGE("[sock] control level: %{public}d", cmptr->cmsg_level);
491 HILOGE("[sock] control type: %{public}d", cmptr->cmsg_type);
492 return BtStatus::BT_FAILURE;
493 }
494 clientFd = *((int *)CMSG_DATA(cmptr));
495 } else {
496 return BtStatus::BT_FAILURE;
497 }
498 uint8_t recvBuf[rv];
499 (void)memset_s(&recvBuf, sizeof(recvBuf), 0, sizeof(recvBuf));
500 if (memcpy_s(recvBuf, sizeof(recvBuf), (uint8_t *)msg.msg_iov[0].iov_base, rv) != EOK) {
501 HILOGE("[sock] RecvSocketFd, recvBuf memcpy_s fail");
502 return BtStatus::BT_FAILURE;
503 }
504
505 uint8_t buf[6] = {0};
506 if (memcpy_s(buf, sizeof(buf), &recvBuf[1], sizeof(buf)) != EOK) {
507 HILOGE("[sock] RecvSocketFd, buf memcpy_s fail");
508 return BtStatus::BT_FAILURE;
509 }
510
511 char token[LENGTH] = {0};
512 (void)sprintf_s(token,
513 sizeof(token), "%02X:%02X:%02X:%02X:%02X:%02X", buf[0x05], buf[0x04], buf[0x03], buf[0x02], buf[0x01], buf[0x00]);
514 BluetoothRawAddress rawAddr {token};
515 acceptAddress_ = rawAddr.GetAddress().c_str();
516 return clientFd;
517 }
518
CloseOHOS::Bluetooth::SppServerSocket::impl519 void Close()
520 {
521 HILOGI("starts");
522 if (socketStatus_ == SOCKET_CLOSED) {
523 HILOGW("The socketStatus_ is already SOCKET_CLOSED");
524 return;
525 } else {
526 socketStatus_ = SOCKET_CLOSED;
527 if (fd_ > 0) {
528 shutdown(fd_, SHUT_RD);
529 shutdown(fd_, SHUT_WR);
530 close(fd_);
531 HILOGI("fd closed, fd_: %{public}d", fd_);
532 fd_ = -1;
533 return;
534 } else {
535 HILOGE("socket not created");
536 return;
537 }
538 }
539 }
540
GetStringTagOHOS::Bluetooth::SppServerSocket::impl541 const std::string &GetStringTag()
542 {
543 HILOGI("starts");
544 if (socketStatus_ == SOCKET_CLOSED) {
545 HILOGE("socketStatus_ is SOCKET_CLOSED");
546 socketServiceType_ = "";
547 } else {
548 socketServiceType_ = "ServerSocket: Type: TYPE_RFCOMM";
549 socketServiceType_.append(" ServerName: ").append(name_);
550 }
551 return socketServiceType_;
552 }
553
554 class BluetoothServerSocketDeathRecipient;
555 sptr<BluetoothServerSocketDeathRecipient> deathRecipient_;
556
557 sptr<IBluetoothSocket> proxy_;
558 UUID uuid_;
559 SppSocketType type_;
560 bool encrypt_;
561 int fd_;
562 int socketStatus_;
563 std::string name_ {
564 ""
565 };
566 int acceptFd_;
567 std::string acceptAddress_;
568 std::string socketServiceType_ {
569 ""
570 };
571 };
572
573 class SppServerSocket::impl::BluetoothServerSocketDeathRecipient final : public IRemoteObject::DeathRecipient {
574 public:
BluetoothServerSocketDeathRecipient(SppServerSocket::impl & host)575 BluetoothServerSocketDeathRecipient(SppServerSocket::impl &host) : host_(host) {};
576 ~BluetoothServerSocketDeathRecipient() final = default;
577 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothServerSocketDeathRecipient);
578
OnRemoteDied(const wptr<IRemoteObject> & remote)579 void OnRemoteDied(const wptr<IRemoteObject> &remote) final
580 {
581 HILOGI("starts");
582 host_.proxy_ = nullptr;
583 }
584
585 private:
586 SppServerSocket::impl &host_;
587 };
588
impl(const std::string & name,UUID uuid,SppSocketType type,bool encrypt)589 SppServerSocket::impl::impl(const std::string &name, UUID uuid, SppSocketType type, bool encrypt)
590 : uuid_(uuid), type_(type), encrypt_(encrypt), fd_(-1), socketStatus_(SOCKET_INIT), name_(name)
591 {
592 HILOGI("(4 parameters) starts");
593 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
594 sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
595
596 if (!hostRemote) {
597 HILOGI("failed: no hostRemote");
598 return;
599 }
600 sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
601 sptr<IRemoteObject> remote = hostProxy->GetProfile(PROFILE_SPP);
602
603 if (!remote) {
604 HILOGE("failed: no remote");
605 return;
606 }
607 HILOGI("remote obtained");
608
609 proxy_ = iface_cast<IBluetoothSocket>(remote);
610 if (proxy_ == nullptr) {
611 return;
612 }
613 deathRecipient_ = new BluetoothServerSocketDeathRecipient(*this);
614 proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
615 }
616
SppServerSocket(const std::string & name,UUID uuid,SppSocketType type,bool encrypt)617 SppServerSocket::SppServerSocket(const std::string &name, UUID uuid, SppSocketType type, bool encrypt)
618 : pimpl(new SppServerSocket::impl(name, uuid, type, encrypt))
619 {
620 HILOGI("(4 parameters) starts");
621 int ret = pimpl->Listen();
622 if (ret < 0) {
623 HILOGE("bind listen failed!");
624 }
625 }
626
~SppServerSocket()627 SppServerSocket::~SppServerSocket()
628 {}
629
Accept(int timeout)630 std::unique_ptr<SppClientSocket> SppServerSocket::Accept(int timeout)
631 {
632 HILOGI("starts");
633 return pimpl->Accept(timeout);
634 }
635
Close()636 void SppServerSocket::Close()
637 {
638 HILOGI("starts");
639 return pimpl->Close();
640 }
641
GetStringTag()642 const std::string &SppServerSocket::GetStringTag()
643 {
644 HILOGI("starts");
645 return pimpl->GetStringTag();
646 }
647
BuildInsecureRfcommDataSocketByServiceRecord(const BluetoothRemoteDevice & device,const UUID & uuid)648 SppClientSocket *SocketFactory::BuildInsecureRfcommDataSocketByServiceRecord(
649 const BluetoothRemoteDevice &device, const UUID &uuid)
650 {
651 HILOGI("starts");
652 if (device.IsValidBluetoothRemoteDevice()) {
653 return new SppClientSocket(device, uuid, TYPE_RFCOMM, false);
654 } else {
655 HILOGE("[sock] Device is not valid.");
656 return nullptr;
657 }
658 }
659
BuildRfcommDataSocketByServiceRecord(const BluetoothRemoteDevice & device,const UUID & uuid)660 SppClientSocket *SocketFactory::BuildRfcommDataSocketByServiceRecord(
661 const BluetoothRemoteDevice &device, const UUID &uuid)
662 {
663 HILOGI("starts");
664 if (device.IsValidBluetoothRemoteDevice()) {
665 return new SppClientSocket(device, uuid, TYPE_RFCOMM, true);
666 } else {
667 HILOGE("[sock] Device is not valid.");
668 return nullptr;
669 }
670 }
671
DataListenInsecureRfcommByServiceRecord(const std::string & name,const UUID & uuid)672 SppServerSocket *SocketFactory::DataListenInsecureRfcommByServiceRecord(const std::string &name, const UUID &uuid)
673 {
674 HILOGI("starts");
675 return new SppServerSocket(name, uuid, TYPE_RFCOMM, false);
676 }
677
DataListenRfcommByServiceRecord(const std::string & name,const UUID & uuid)678 SppServerSocket *SocketFactory::DataListenRfcommByServiceRecord(const std::string &name, const UUID &uuid)
679 {
680 HILOGI("starts");
681 return new SppServerSocket(name, uuid, TYPE_RFCOMM, true);
682 }
683 } // namespace Bluetooth
684 } // namespace OHOS