180 likes | 300 Views
Embedded Systems Software Training Center. BluRapport SDK. Agenda. Introduce to Low Level socket based layer of RXBT. Questions. What difference between protocol and profile? Why we need to use profiles?. Socket based layer. Sockets overview
E N D
Embedded Systems Software Training Center BluRapport SDK
Agenda • Introduce toLow Level socket based layer of RXBT
Questions • What difference between protocol and profile? • Why we need to use profiles?
Socket based layer Sockets overview A RXBT socket is an endpoint of an inter-process communication flow across a Bluetooth network. Most communication between devices is based on the Bluetooth protocol stack. A socket API is an application programming interface (API), provided by RXBT SDK, that allows application programs to control and use bluetoothsockets. A socket address is the combination of an BDA address and type of protocol, much like one end of a telephone connection is the combination of a phone number and a particular extension. Based on this address, RXBT sockets deliver incoming data packets to the appropriate application process or thread Communicating local and remote sockets are called socket pairs. Each socket pair is described by a unique 6-tuple consisting of source and destination BDA addresses and port numbers, i.e. of local and remote socket addresses.
Socket SDK • Main Functions that are used to work with Low Level socket API: • rx_socket_trx_socket(rx_socktype_ttype, rx_sockproto_t protocol)- Create new socket • type - socket type (either stream or seqpacket) • protocol - Bluetooth protocol (it can be: RX_PF_HCI, RX_PF_L2CAP, RX_PF_RFCOMM, RX_PF_OBEX, RX_PF_SDP and etc.) • returns socket ID (>= 0) if ok, error code (< 0) in case of error. • rx_ret_trx_close(rx_socket_tsock) – Close socket • sock - socket created by rx_socket() call • typedefstructrx_sockaddr_s (more information about type of addresses and type of protocols see in rx_sdk.h) • { • rx_sockproto_t proto; • union __protocol_u • { • rx_sockaddr_hci_thci; • rx_sockaddr_l2cap_t l2cap; • rx_sockaddr_rfcomm_trfcomm; • rx_sockaddr_sdp_tsdp; • rx_sockaddr_sco_tsco; • rx_sockaddr_bnep_tbnep; • } protocol; • } • rx_sockaddr_t;
Socket SDK • Main Functions that are used to work with Low Level socket API: • rx_ret_trx_bind(rx_socket_tsock, rx_sockaddr_t *my_addr)- Bind local socket to some address • sock - socket to bind • my_addr – address • NOTE: • Bind local socket to some address. For different socket types different address components are used. In general, BDA will be ignored but PSM or channel # will be taken into account. If PSM or channel # is busy, RET_ALREADY_EXISTS is returned. It is strictly necessary to call rx_bind before rx_listen, but not before rx_connect.
Socket SDK • Main Functions that are used to work with Low Level socket API: • rx_ret_trx_listen(rx_socket_tsock, int backlog)- Register socket as waiting for connections. • sock - socket to listen on. • backlog - incoming connections queue size (currently ignored) • NOTE: • The socket must not be used for other i/o before. • rx_bindmust be called before rx_listen. • It is impossible to call rx_listen on HCI socket.
Socket SDK • Main Functions that are used to work with Low Level socket API: • rx_socket_trx_accept(rx_socket_tsock, rx_sockaddr_t *addr)- Accept incoming connection. • sock - socket listening on. • addr- peer's address • Returns new socket ID (>= 0) or error code (< 0). • NOTE: • Block if no ready incoming connections on this socket. • rx_listenmust be run once before rx_accept. • It is impossible to call rx_accept on HCI socket.
Socket SDK • Main Functions that are used to work with Low Level socket API: • rx_ret_trx_connect(rx_socket_tsock, rx_sockaddr_t *dest_addr)- Connect to the destination. • sock - socket to use • dest_addr- destination address (BDA and PSM or channel # must be filled) • NOTE: • Connect to the peer, blocking until connection complete. • It is impossible to call rx_connect on HCI socket.
Socket SDK • Main Functions that are used to work with Low Level socket API: • rx_ret_trx_read(rx_socket_tsock, void *buf, unsignedlen)- Read data from the peer. • sock - socket ID • buf- user's buffer • len- buffer size • Rreturnsnumber of bytes read (> 0), or 0 in case of connection break, or error code (< 0) • NOTE: • Blocking if no data ready. • If socket is in STREAM mode, can aither break or glue packets. • If socket is in SEQPACKET mode, return exactly packet size. If buffer length is less then packet size, packet rest will be returned on the next rx_read or rx_recvcall. • rx_readand rx_recv calls can be mixed, but only one thread can call rx_read/rx_recv at the same time. • It is impossible to call rx_read on HCI socket.
Socket SDK • Main Functions that are used to work with Low Level socket API: • rx_ret_trx_write(rx_socket_tsock, void *buf, unsignedlen)- Send data to the peer. • sock - socket ID • buf- user buffer • len- data length • Returns number of bytes written (> 0), or 0 in case of connection break, or error code (< 0) • NOTE: • Try to send as much data as possible, but does not guarantee that all data will be send.
Socket SDK • Main Functions that are used to work with Low Level socket API: • rx_ret_trx_getsockopt(rx_socket_tsock, int level, intoptname, void *optval, unsigned *optlen)- Get either protocol or socket-level option. • sock - socket ID • [in] level - RX_SOL_SOCKET or RX_SOL_PROTO • [in] optname - either option from rx_sockopt_name_t or protocol-specific option. • [out] optval - user buffer; • [in,out] optlen - in: *optlen - buffer length; out: real number of bytes read. • NOTE: • This call can't block. • This call can't be called at the same time with rx_read call.
Socket SDK • Main Functions that are used to work with Low Level socket API: • rx_ret_trx_setsockopt(rx_socket_tsock, int level, intoptname, void *optval, unsigned optlen)- Set either protocol or socket-level option. • sock - socket ID • level - RX_SOL_SOCKET or RX_SOL_PROTO • optname- either option from rx_sockopt_name_tor protocol-specific option. • optval- user buffer; • optlen- optlen - buffer length. • NOTE: • This call can't block. • This call can't be called at the same time with rx_write call.
Socket SDK • Main Functions that are used to work with Low Level socket API: • rx_ret_trx_getsockname(rx_socket_tsock, rx_sockaddr_t *addr)- Get local socket address. • sock - socket ID • addr- address • NOTE: • Valid after rx_bind(), rx_listen(), rx_connect() calls. • Never blocks. • rx_ret_trx_getpeername(rx_socket_tsock, rx_sockaddr_t *addr)- Get remote address. • sock - socket ID • addr - address • NOTE: • Valid after rx_accept(), rx_connect() calls. • Never blocks.
Socket SDK • Main Functions that are used to work with Low Level socket API: • rx_ret_trx_ioctl(rx_socket_tsock, int name, • void *arg_val, unsigned arg_size, • void *ret_val, unsigned ret_buf_size, unsigned *ret_len)- Do protocol-specific action. • sock - socket ID • name - protocol-specific call ID • arg_val- IN argument value • arg_size- IN argument size • ret_val- result value • ret_buf_size- result buffer size • ret_len- real result length • Returns a Error code • NOTE: • This is low-level function used to to some protocol-specific thing. • It can block. • Only one rx_ioctl can be callse on the same socket an the same time..
Socket SDK Main Functions that are used to work with Low Level socket API: Example of using IOCTL rx_ret_trx_rfcomm_sent_fcoff(rx_socket_tsock, rx_bool_tfcoff) { rx_ret_t ret = RET_OK; if (fcoff) { ret = rx_ioctl(sock, RFCOMM_IOCTL_SENT_FCOFF, 0,0,0,0,0); } else { ret = rx_ioctl(sock, RFCOMM_IOCTL_SENT_FCON, 0,0,0,0,0); } return ret; }
Socket SDK Main Functions that are used to work with Low Level socket API: Example of using IOCTL rx_ret_trx_rfcomm_sent_test(rx_socket_tsock) { rx_ret_t ret = RET_OK; ret = rx_ioctl(sock, RFCOMM_IOCTL_SENT_TEST, 0,0, 0,0,0); return ret; }