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