• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_cj_socket_spp_ffi"
17 #endif
18 
19 #include "bluetooth_spp_ffi.h"
20 
21 #include "bluetooth_spp_server_impl.h"
22 #include "bluetooth_spp_utils.h"
23 
24 namespace OHOS {
25 namespace Bluetooth {
26 extern "C" {
parseCSppOptions(CSppOption options)27 static SppOption parseCSppOptions(CSppOption options)
28 {
29     SppOption ret = { .uuid_ = options.uuid, .secure_ = options.secure, .type_ = BtSocketType(options.type) };
30     return ret;
31 }
32 
FfiBluetoothSocketSppListen(char * name,CSppOption options,int32_t * errCode)33 int32_t FfiBluetoothSocketSppListen(char* name, CSppOption options, int32_t* errCode)
34 {
35     return SppServerImpl::SppListen(name, parseCSppOptions(options), errCode);
36 }
37 
FfiBluetoothSocketSppAccept(int32_t serverSocket,int32_t * errCode)38 int32_t FfiBluetoothSocketSppAccept(int32_t serverSocket, int32_t* errCode)
39 {
40     return SppServerImpl::SppAccept(serverSocket, errCode);
41 }
42 
FfiBluetoothSocketSppConnect(char * deviceId,CSppOption options,int32_t * errCode)43 int32_t FfiBluetoothSocketSppConnect(char* deviceId, CSppOption options, int32_t* errCode)
44 {
45     return SppClientImpl::SppConnect(deviceId, parseCSppOptions(options), errCode);
46 }
47 
FfiBluetoothSocketGetDeviceId(int32_t clientSocket,int32_t * errCode)48 char* FfiBluetoothSocketGetDeviceId(int32_t clientSocket, int32_t* errCode)
49 {
50     std::string deviceId = SppClientImpl::GetDeviceId(clientSocket, errCode);
51     if (*errCode != SUCCESS_CODE) {
52         return nullptr;
53     }
54     return MallocCString(deviceId);
55 }
56 
FfiBluetoothSocketSppCloseServerSocket(int32_t socket,int32_t * errCode)57 void FfiBluetoothSocketSppCloseServerSocket(int32_t socket, int32_t* errCode)
58 {
59     *errCode = SppServerImpl::SppCloseServerSocket(socket);
60 }
61 
FfiBluetoothSocketSppCloseClientSocket(int32_t socket,int32_t * errCode)62 void FfiBluetoothSocketSppCloseClientSocket(int32_t socket, int32_t* errCode)
63 {
64     *errCode = SppClientImpl::SppCloseClientSocket(socket);
65 }
66 
FfiBluetoothSocketSppWrite(int32_t clientSocket,CArrUI8 data,int32_t * errCode)67 void FfiBluetoothSocketSppWrite(int32_t clientSocket, CArrUI8 data, int32_t* errCode)
68 {
69     *errCode = SppClientImpl::SppWrite(clientSocket, data);
70 }
71 
FfiBluetoothSocketOn(char * type,int32_t clientSocket,int64_t cbId,int32_t * errCode)72 void FfiBluetoothSocketOn(char* type, int32_t clientSocket, int64_t cbId, int32_t* errCode)
73 {
74     *errCode = SppServerImpl::RegisterSocketObserver(type, clientSocket, cbId);
75 }
76 
FfiBluetoothSocketOff(char * type,int32_t clientSocket,int64_t cbId,int32_t * errCode)77 void FfiBluetoothSocketOff(char* type, int32_t clientSocket, int64_t cbId, int32_t* errCode)
78 {
79     *errCode = SppServerImpl::DeRegisterSocketObserver(type, clientSocket, cbId);
80 }
81 }
82 } // namespace Bluetooth
83 } // namespace OHOS