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