3/5

Dependencies:   mbed

Committer:
yuki0701
Date:
Tue Mar 05 04:30:35 2019 +0000
Revision:
1:530908de68c6
Parent:
0:a56be39653d0
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
la00noix 0:a56be39653d0 1 /*
la00noix 0:a56be39653d0 2 Copyright (c) 2010 Peter Barrett
la00noix 0:a56be39653d0 3
la00noix 0:a56be39653d0 4 Permission is hereby granted, free of charge, to any person obtaining a copy
la00noix 0:a56be39653d0 5 of this software and associated documentation files (the "Software"), to deal
la00noix 0:a56be39653d0 6 in the Software without restriction, including without limitation the rights
la00noix 0:a56be39653d0 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
la00noix 0:a56be39653d0 8 copies of the Software, and to permit persons to whom the Software is
la00noix 0:a56be39653d0 9 furnished to do so, subject to the following conditions:
la00noix 0:a56be39653d0 10
la00noix 0:a56be39653d0 11 The above copyright notice and this permission notice shall be included in
la00noix 0:a56be39653d0 12 all copies or substantial portions of the Software.
la00noix 0:a56be39653d0 13
la00noix 0:a56be39653d0 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
la00noix 0:a56be39653d0 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
la00noix 0:a56be39653d0 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
la00noix 0:a56be39653d0 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
la00noix 0:a56be39653d0 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
la00noix 0:a56be39653d0 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
la00noix 0:a56be39653d0 20 THE SOFTWARE.
la00noix 0:a56be39653d0 21 */
la00noix 0:a56be39653d0 22
la00noix 0:a56be39653d0 23 /*
la00noix 0:a56be39653d0 24 Tue Apr 26 2011 Bart Janssens: added a socket listener
la00noix 0:a56be39653d0 25 */
la00noix 0:a56be39653d0 26
la00noix 0:a56be39653d0 27 #ifndef SOCKET_H_INCLUDED
la00noix 0:a56be39653d0 28 #define SOCKET_H_INCLUDED
la00noix 0:a56be39653d0 29
la00noix 0:a56be39653d0 30 #define SOCKET_HCI 1
la00noix 0:a56be39653d0 31 #define SOCKET_L2CAP 2
la00noix 0:a56be39653d0 32 #define SOCKET_RFCOM 3
la00noix 0:a56be39653d0 33
la00noix 0:a56be39653d0 34 typedef struct
la00noix 0:a56be39653d0 35 {
la00noix 0:a56be39653d0 36 u8 AddressSpecific[0]; // BDADDR,psm etc
la00noix 0:a56be39653d0 37 } SocketAddrHdr;
la00noix 0:a56be39653d0 38
la00noix 0:a56be39653d0 39
la00noix 0:a56be39653d0 40
la00noix 0:a56be39653d0 41 enum SocketState
la00noix 0:a56be39653d0 42 {
la00noix 0:a56be39653d0 43 SocketState_Unknown,
la00noix 0:a56be39653d0 44 SocketState_Opening,
la00noix 0:a56be39653d0 45 SocketState_Open,
la00noix 0:a56be39653d0 46 SocketState_Closing,
la00noix 0:a56be39653d0 47 SocketState_Closed,
la00noix 0:a56be39653d0 48 SocketState_Listen,
la00noix 0:a56be39653d0 49 SocketState_Accept
la00noix 0:a56be39653d0 50 };
la00noix 0:a56be39653d0 51
la00noix 0:a56be39653d0 52 typedef void (*SocketCallback)(int socket, SocketState state, const u8* data, int len, void* userData);
la00noix 0:a56be39653d0 53
la00noix 0:a56be39653d0 54 class SocketListener
la00noix 0:a56be39653d0 55 {
la00noix 0:a56be39653d0 56 public:
la00noix 0:a56be39653d0 57 int ID;
la00noix 0:a56be39653d0 58 int Type;
la00noix 0:a56be39653d0 59 int port;
la00noix 0:a56be39653d0 60 SocketCallback Callback;
la00noix 0:a56be39653d0 61 void* userData;
la00noix 0:a56be39653d0 62 };
la00noix 0:a56be39653d0 63
la00noix 0:a56be39653d0 64 //===========================================================================
la00noix 0:a56be39653d0 65 //===========================================================================
la00noix 0:a56be39653d0 66 // Don't need to look at or use anything below this line:
la00noix 0:a56be39653d0 67 // Internal representation of socket
la00noix 0:a56be39653d0 68
la00noix 0:a56be39653d0 69 class SocketHandler;
la00noix 0:a56be39653d0 70 class SocketInternal
la00noix 0:a56be39653d0 71 {
la00noix 0:a56be39653d0 72 public:
la00noix 0:a56be39653d0 73
la00noix 0:a56be39653d0 74 u8 ID;
la00noix 0:a56be39653d0 75 u8 State;
la00noix 0:a56be39653d0 76 u8 Type;
la00noix 0:a56be39653d0 77 u8 B0;
la00noix 0:a56be39653d0 78 int port;
la00noix 0:a56be39653d0 79 SocketCallback Callback;
la00noix 0:a56be39653d0 80 void* userData;
la00noix 0:a56be39653d0 81 u8 Data[0]; // Extra socket data starts here
la00noix 0:a56be39653d0 82
la00noix 0:a56be39653d0 83 void Recv(const u8* data, int len)
la00noix 0:a56be39653d0 84 {
la00noix 0:a56be39653d0 85 Callback(ID,(SocketState)State,data,len,userData);
la00noix 0:a56be39653d0 86 }
la00noix 0:a56be39653d0 87
la00noix 0:a56be39653d0 88 void SetState(SocketState state)
la00noix 0:a56be39653d0 89 {
la00noix 0:a56be39653d0 90 State = state;
la00noix 0:a56be39653d0 91 Callback(ID,(SocketState)State,0,0,userData);
la00noix 0:a56be39653d0 92 }
la00noix 0:a56be39653d0 93 };
la00noix 0:a56be39653d0 94
la00noix 0:a56be39653d0 95 class SocketHandler
la00noix 0:a56be39653d0 96 {
la00noix 0:a56be39653d0 97 public:
la00noix 0:a56be39653d0 98 virtual int Open(SocketInternal* sock, SocketAddrHdr* addr) = 0;
la00noix 0:a56be39653d0 99 virtual int Create(SocketInternal* sock, SocketAddrHdr* addr) = 0;
la00noix 0:a56be39653d0 100 virtual int Send(SocketInternal* sock, const u8* data, int len) = 0;
la00noix 0:a56be39653d0 101 virtual int Close(SocketInternal* sock) = 0;
la00noix 0:a56be39653d0 102 virtual int Accept(SocketInternal* sock, SocketAddrHdr* addr) = 0;
la00noix 0:a56be39653d0 103 };
la00noix 0:a56be39653d0 104
la00noix 0:a56be39653d0 105 int RegisterSocketHandler(int type, SocketHandler* handler);
la00noix 0:a56be39653d0 106 SocketInternal* GetSocketInternal(int socket);
la00noix 0:a56be39653d0 107
la00noix 0:a56be39653d0 108 int Socket_Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData); // Open a socket
la00noix 0:a56be39653d0 109 int Socket_Listen(int type, int port,SocketCallback callback, void* userData);
la00noix 0:a56be39653d0 110 int Socket_InUse(int type, int port);
la00noix 0:a56be39653d0 111 int Socket_Accept(int socket, SocketCallback callback, void* userData);
la00noix 0:a56be39653d0 112 SocketInternal* Socket_Create(int type,SocketAddrHdr* addr, int port);
la00noix 0:a56be39653d0 113 int Socket_Send(int socket, const u8* data, int len);
la00noix 0:a56be39653d0 114 int Socket_State(int socket);
la00noix 0:a56be39653d0 115 int Socket_Close(int socket);
la00noix 0:a56be39653d0 116
la00noix 0:a56be39653d0 117 #define ERR_SOCKET_TYPE_NOT_FOUND -200
la00noix 0:a56be39653d0 118 #define ERR_SOCKET_NOT_FOUND -201
la00noix 0:a56be39653d0 119 #define ERR_SOCKET_NONE_LEFT -202
la00noix 0:a56be39653d0 120 #define ERR_SOCKET_IN_USE -203
la00noix 0:a56be39653d0 121
la00noix 0:a56be39653d0 122 #endif // SOCKET_H_INCLUDED