diff options
| author | Electric-Blue <Electric-Blue@users.noreply.github.com> | 2016-01-26 02:03:48 +0300 |
|---|---|---|
| committer | Electric-Blue <Electric-Blue@users.noreply.github.com> | 2016-01-26 02:03:48 +0300 |
| commit | 0c4a1cc1c2083a871562364de5df4f9d88ec321e (patch) | |
| tree | ddb8428845b663c078359eda4ec81bdcc157414f /nimbluez/bluetoothnet.nim | |
| parent | 27a7a2a18a6db8b67cbe0e86c37cb174de2a1295 (diff) | |
| download | NimBluez-0c4a1cc1c2083a871562364de5df4f9d88ec321e.tar.gz NimBluez-0c4a1cc1c2083a871562364de5df4f9d88ec321e.zip | |
First version.
Diffstat (limited to 'nimbluez/bluetoothnet.nim')
| -rw-r--r-- | nimbluez/bluetoothnet.nim | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/nimbluez/bluetoothnet.nim b/nimbluez/bluetoothnet.nim new file mode 100644 index 0000000..ac794fa --- /dev/null +++ b/nimbluez/bluetoothnet.nim @@ -0,0 +1,69 @@ +# Copyright (c) 2016, Maxim V. Abramov +# All rights reserved. +# Look at license.txt for more info. + +## This module implements a high-level cross-platform sockets interface for +## Bluetooth. +## This module is just a draft yet. + +import os +import bluetoothnativesockets + +proc bindAddr*(socket: SocketHandle, port = RfcommPort(0), address = ""): cint = + ## Binds a Bluetooth RFCOMM socket. + var name = getRfcommAddr(port, address) + result = bindAddr(socket, + cast[ptr SockAddr](addr(name)), + sizeof(name).SockLen) + + +proc bindAddr*(socket: SocketHandle, port: L2capPort, address = ""): cint = + ## Binds a Bluetooth L2CAP socket. + var name = getL2capAddr(port, address) + result = bindAddr(socket, + cast[ptr SockAddr](addr(name)), + sizeof(name).SockLen) + + +proc acceptRfcommAddr*(server: SocketHandle, + address: var string): SocketHandle = + ## Enables incoming connection attempts on a Bluetooth RFCOMM socket. + var sockAddr = getRfcommAddr() + var addrLen = sizeof(sockAddr).SockLen + result = accept(server, cast[ptr SockAddr](addr(sockAddr)), addr(addrLen)) + address = getAddrString(sockAddr) + + +proc acceptL2capAddr*(server: SocketHandle, address: var string): SocketHandle = + ## Enables incoming connection attempts on a Bluetooth L2CAP socket. + var sockAddr = getL2capAddr() + var addrLen = sizeof(sockAddr).SockLen + result = accept(server, cast[ptr SockAddr](addr(sockAddr)), addr(addrLen)) + address = getAddrString(sockAddr) + + +proc connect*(socket: SocketHandle, port: RfcommPort, address: string): cint = + ## Connects to a target Bluetooth device, using a previously created Bluetooth RFCOMM socket. + var name = getRfcommAddr(port, address) + result = connect(socket, cast[ptr SockAddr](addr(name)), sizeof(name).SockLen) + + +proc connect*(socket: SocketHandle, port: L2capPort, address: string): cint = + ## Connects to a target Bluetooth device, using a previously created Bluetooth L2CAP socket. + var name = getL2capAddr(port, address) + result = connect(socket, cast[ptr SockAddr](addr(name)), sizeof(name).SockLen) + + +proc send*(socket: SocketHandle, message: string): cint = + ## Sends data on a connected socket. + result = send(socket, cstring(message), cint(message.len), cint(0)).cint + + +proc recv*(socket: SocketHandle): string = + ## Receives data from a connected socket. + result = "" + result.setLen(1000) + let recvLen = recv(socket, cstring(result), cint(result.len), cint(0)) + if recvLen < 0'i32: + raiseOSError(osLastError()) + result.setLen(recvLen) |
