1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 
5 #ifndef __SPP_API_H__
6 #define __SPP_API_H__
7 
8 #include "sdp_api.h"
9 #include "cmsis_os.h"
10 #include "cqueue.h"
11 #include "rfcomm_api.h"
12 
13 #define SPP_DEVICE_NUM      4
14 #define SPP_SERVICE_NUM     4
15 
16 typedef uint16_t spp_event_t;
17 #define BTIF_SPP_EVENT_REMDEV_CONNECTED         0
18 #define BTIF_SPP_EVENT_REMDEV_DISCONNECTED      1
19 #define BTIF_SPP_EVENT_DATA_SENT                2
20 #define BTIF_SPP_EVENT_REMDEV_DISCONNECTED_IND  3
21 #define BTIF_SPP_EVENT_REMDEV_CONNECTED_IND     4
22 
23 typedef uint8_t spp_port_t;
24 
25 #define BTIF_SPP_SERVER_PORT             0x01
26 #define BTIF_SPP_CLIENT_PORT             0x02
27 
28 #define SPP_RX_SIGNAL_ID                 0x04
29 
30 struct spp_device;
31 
32 struct spp_tx_done {
33     uint8_t         *tx_buf;
34     uint16_t        tx_data_length;
35 };
36 
37 struct spp_client {
38     btif_remote_device_t *remDev;
39     uint8_t                  serverId;
40     btif_sdp_query_token_t   *sdpToken;
41     uint8_t* rfcommServiceSearchRequestPtr;
42     uint8_t rfcommServiceSearchRequestLen;
43 };
44 
45 struct spp_service {
46     struct rf_service   rf_service;
47     const uint8_t       *name;
48     uint16_t            nameLen;
49     uint16_t            numPorts;
50     btif_sdp_record_t   *sdpRecord;
51     uint8_t             service_used_flag;
52 };
53 
54 struct spp_callback_parms {
55     spp_event_t event;
56     int status;
57     union {
58         void           *other;
59         btif_remote_device_t *remDev;
60     } p;
61 };
62 
63 typedef void (*spp_callback_t)(struct spp_device *locDev,
64                             struct spp_callback_parms *Info);
65 
66 typedef int (*spp_handle_data_event_func_t)(void *pDev, uint8_t process, uint8_t *pData, uint16_t dataLen);
67 
68 struct _spp_dev {
69     union {
70         struct spp_client   client;
71         struct spp_service  *sppService;
72     } type;
73 };
74 
75 struct spp_device {
76     spp_port_t      portType;
77     uint32_t        app_id;
78     osThreadId      reader_thread_id;
79     osMutexId       mutex_id;
80     CQueue          rx_queue;
81     uint8_t         *rx_buffer;
82     uint32_t        rx_buffer_size;
83     uint32_t        tx_packet_num;
84     void            *priv;
85     spp_callback_t  spp_callback;
86     spp_handle_data_event_func_t spp_handle_data_event_func;
87     uint8_t         serialNumber;
88     uint8_t         spp_connected_flag;
89     uint8_t         sppUsedFlag;
90     uint8_t         close_pending;
91     struct _spp_dev sppDev;
92     void            *_channel;
93 };
94 
95 #define container_of(ptr, type, member) ({                  \
96     const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
97     (type *)( (char *)__mptr - offsetof(type,member) );})
98 
99 #ifdef __cplusplus
100 extern "C" {
101 #endif
102 
103 bt_status_t btif_spp_init_device(struct spp_device *dev, uint16_t numPackets, osMutexId mid);
104 bt_status_t btif_spp_init_rx_buf(struct spp_device *dev, uint8_t *rx_buf, uint32_t size);
105 bool btif_sppos_is_txpacket_available(struct spp_device *dev);
106 bt_status_t btif_spp_open(struct spp_device *dev,
107                         btif_remote_device_t  *btDevice,
108                         spp_callback_t callback);
109 bt_status_t btif_spp_open_client(struct spp_device *dev,
110                         bt_bdaddr_t *bdaddr,
111                         spp_callback_t callback);
112 bt_status_t btif_spp_open_server(struct spp_device *dev,
113                         spp_callback_t callback);
114 bt_status_t btif_spp_disconnect(struct spp_device *dev);
115 bt_status_t btif_spp_close(struct spp_device *dev);
116 bt_status_t btif_spp_read(struct spp_device *dev, char *buffer, uint16_t *nBytes);
117 bt_status_t btif_spp_write(struct spp_device *dev, char *buffer, uint16_t *nBytes);
118 bt_status_t btif_spp_service_setup(struct spp_device *dev,
119                                 struct spp_service *service,
120                                 btif_sdp_record_t *record);
121 bt_status_t btif_ccmp_open(struct spp_device *dev,
122                         btif_remote_device_t  *btDevice,
123                         spp_callback_t callback,
124                         uint8_t port);
125 struct spp_device *btif_create_spp_device(void);
126 void btif_destroy_spp_device(struct spp_device *dev_t);
127 struct spp_device *btif_spp_get_device(uint32_t app_id);
128 struct spp_service *btif_create_spp_service(void);
129 void btif_destroy_spp_service(struct spp_service *spp_service_p);
130 uint32_t btif_spp_get_app_id(struct spp_device *dev);
131 uint32_t btif_spp_get_rfcomm_handle(struct spp_device *dev);
132 uint8_t btif_spp_get_server_channel(struct spp_device *dev);
133 const char *btif_spp_event2str(spp_event_t event);
134 
135 typedef void (*btif_bt_spp_app_callback_func)(void* spp_devi, void* spp_para);
136 void btif_register_bt_spp_callback_func(btif_bt_spp_app_callback_func func);
137 uint32_t btif_spp_profile_save_ctx(uint32_t app_id,btif_remote_device_t *rem_dev, uint8_t *buf, uint32_t buf_len);
138 uint32_t btif_spp_profile_restore_ctx(uint8_t *buf, uint32_t buf_len);
139 
140 /*---------------------------------------------------------------------------
141  * rfcomm channel number
142  *      should be from 1 to 30
143  */
144 enum RFCOMM_CHANNEL_NUM_ {
145     RFCOMM_CHANNEL_1 = 10,
146     RFCOMM_CHANNEL_2,
147     RFCOMM_CHANNEL_3,
148     RFCOMM_CHANNEL_4,
149     RFCOMM_CHANNEL_5,
150     RFCOMM_CHANNEL_6,
151     RFCOMM_CHANNEL_7,
152     RFCOMM_CHANNEL_8,
153     RFCOMM_CHANNEL_9,
154     RFCOMM_CHANNEL_10,
155 };
156 
157 #ifdef __cplusplus
158 }
159 #endif
160 
161 #endif /* __SPP_API_H__ */
162