local fix version of myBlueUSB (http://mbed.org/users/networker/code/myBlueUSB/). - merge deleted files which are required to compile. - enable echo back of received data via RFCOMM.

Dependencies:   AvailableMemory FatFileSystem mbed myUSBHost

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 #define SOCKET_SDP      4
00030 
00031 #define SO_RECBUF   1
00032 #define SO_SNDBUF   2
00033 #define NOPROTOOPT  -100
00034 
00035 typedef struct
00036 {
00037     u8  AddressSpecific[0]; // BDADDR,psm etc
00038 } SocketAddrHdr;
00039 
00040 enum SocketState
00041 {
00042     SocketState_Unknown,
00043     SocketState_Opening,
00044     SocketState_Open,
00045     SocketState_Closing,
00046     SocketState_Closed,
00047     SocketState_Listen,
00048     SocketState_Accepting,
00049     SocketState_L2CAP_WaitConnect = 8,
00050     SocketState_L2CAP_WaitConnectRsp,
00051     SocketState_L2CAP_WaitDisconnect,
00052     SocketState_L2CAP_Config_wait = 16,
00053     SocketState_L2CAP_Config_wait_send,
00054     SocketState_L2CAP_Config_wait_req,
00055     SocketState_L2CAP_Config_wait_rsp,
00056     SocketState_L2CAP_Config_wait_reqrsp  
00057 };
00058 
00059 typedef void (*SocketCallback)(int socket, SocketState state, const u8* data, int len, void* userData);
00060 
00061 int Socket_Create(int type, SocketCallback callback, void* userData);   // Allocate a socket
00062 int Socket_Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData);   // Open a socket
00063 int Socket_Listen(int type, int channel, SocketCallback callback, void* userData);   // Open a socket passive
00064 int Socket_Accept(int type, int scid, int rxid, SocketCallback callback, void* userData);   // Open a socket for an incoming connection
00065 int Socket_SetOpt(int socket, int so, int* data, int len);
00066 int Socket_GetOpt(int socket, int so, int* data, int len);
00067 int Socket_Send(int socket, const u8* data, int len);
00068 int Socket_State(int socket);
00069 int Socket_Close(int socket);
00070 
00071 //===========================================================================
00072 //===========================================================================
00073 //  Don't need to look at or use anything below this line:
00074 //  Internal representation of socket
00075 
00076 class SocketHandler;
00077 class SocketInternal
00078 {
00079     public:
00080 
00081     u8 ID;
00082     u8 State;
00083     u8 Type;
00084     u8 B0;
00085     SocketCallback Callback;
00086     void* userData;
00087     u8  Data[0];    // Extra socket data starts here
00088 
00089     void Recv(const u8* data, int len)
00090     {
00091         Callback(ID,(SocketState)State,data,len,userData);
00092     }
00093 
00094     void SetState(SocketState state)
00095     {
00096         State = state;
00097         Callback(ID,(SocketState)State,0,0,userData);
00098         if (state == SocketState_Closed) {
00099           printf("Socket %d has been freed\n", ID);
00100           ID = 0;
00101         }
00102     }
00103 };
00104 
00105 class SocketHandler
00106 {
00107     public:
00108     virtual int Create(SocketInternal* sock) { printf("SocketHandler::Create: not implemented for %s\n", Name()); return sock->ID;}
00109     virtual int Open(SocketInternal* sock, SocketAddrHdr* addr) = 0;
00110     virtual int Send(SocketInternal* sock, const u8* data, int len) = 0;
00111     virtual int Close(SocketInternal* sock) = 0;
00112     virtual int Listen(SocketInternal* sock, int channel) { printf("SocketHandler::Listen: not implemented for %s\n", Name());return 0;}    
00113     virtual int Accept(SocketInternal* sock, int scid, int rxid) { printf("SocketHandler::Accept: not implemented for %s\n", Name());return 0;}
00114     virtual int SetOpt(SocketInternal* sock, int so, int* data, int len) { printf("SocketHandler::SetOpt: not implemented for %s\n", Name());return 0;}
00115     virtual int GetOpt(SocketInternal* sock, int so, int* data, int len) { printf("SocketHandler::GetOpt: not implemented for %s\n", Name());return 0;}
00116     virtual char* Name() { return "Base_SocketHandler";}
00117 };
00118 
00119 int RegisterSocketHandler(int type, SocketHandler* handler);
00120 SocketInternal* GetSocketInternal(int socket);
00121 
00122 #define ERR_SOCKET_TYPE_NOT_FOUND -200
00123 #define ERR_SOCKET_NOT_FOUND -201
00124 #define ERR_SOCKET_NONE_LEFT -202
00125 #define ERR_SOCKET_CANT_LISTEN -203
00126 
00127 #endif // SOCKET_H_INCLUDED