Bluetooth support for MBED with $2 Bluetooth dongles. Includes a USB host and built in support for bluetooth HID devices such as mice, keyboards and wii controllers.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Socket.h Source File

Socket.h

00001 /*
00002 Copyright (c) 2010 Peter Barrett
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013 
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE.
00021 */
00022 
00023 #ifndef SOCKET_H_INCLUDED
00024 #define SOCKET_H_INCLUDED
00025 
00026 #define SOCKET_HCI      1
00027 #define SOCKET_L2CAP    2
00028 #define SOCKET_RFCOM    3
00029 
00030 typedef struct
00031 {
00032     u8  AddressSpecific[0]; // BDADDR,psm etc
00033 } SocketAddrHdr;
00034 
00035 enum SocketState
00036 {
00037     SocketState_Unknown,
00038     SocketState_Opening,
00039     SocketState_Open,
00040     SocketState_Closing,
00041     SocketState_Closed
00042 };
00043 
00044 typedef void (*SocketCallback)(int socket, SocketState state, const u8* data, int len, void* userData);
00045 
00046 int Socket_Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData);   // Open a socket
00047 int Socket_Send(int socket, const u8* data, int len);
00048 int Socket_State(int socket);
00049 int Socket_Close(int socket);
00050 
00051 //===========================================================================
00052 //===========================================================================
00053 //  Don't need to look at or use anything below this line:
00054 //  Internal representation of socket
00055 
00056 class SocketHandler;
00057 class SocketInternal
00058 {
00059     public:
00060 
00061     u8 ID;
00062     u8 State;
00063     u8 Type;
00064     u8 B0;
00065     SocketCallback Callback;
00066     void* userData;
00067     u8  Data[0];    // Extra socket data starts here
00068 
00069     void Recv(const u8* data, int len)
00070     {
00071         Callback(ID,(SocketState)State,data,len,userData);
00072     }
00073 
00074     void SetState(SocketState state)
00075     {
00076         State = state;
00077         Callback(ID,(SocketState)State,0,0,userData);
00078     }
00079 };
00080 
00081 class SocketHandler
00082 {
00083     public:
00084     virtual int Open(SocketInternal* sock, SocketAddrHdr* addr) = 0;
00085     virtual int Send(SocketInternal* sock, const u8* data, int len) = 0;
00086     virtual int Close(SocketInternal* sock) = 0;
00087 };
00088 
00089 int RegisterSocketHandler(int type, SocketHandler* handler);
00090 SocketInternal* GetSocketInternal(int socket);
00091 
00092 #define ERR_SOCKET_TYPE_NOT_FOUND -200
00093 #define ERR_SOCKET_NOT_FOUND -201
00094 #define ERR_SOCKET_NONE_LEFT -202
00095 
00096 #endif // SOCKET_H_INCLUDED