![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This program uses code taken from another program called BlueUSB
Socket.h@0:31713f62f35b, 2011-12-10 (annotated)
- Committer:
- madcowswe
- Date:
- Sat Dec 10 18:45:31 2011 +0000
- Revision:
- 0:31713f62f35b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
madcowswe | 0:31713f62f35b | 1 | /* |
madcowswe | 0:31713f62f35b | 2 | Copyright (c) 2010 Peter Barrett |
madcowswe | 0:31713f62f35b | 3 | |
madcowswe | 0:31713f62f35b | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
madcowswe | 0:31713f62f35b | 5 | of this software and associated documentation files (the "Software"), to deal |
madcowswe | 0:31713f62f35b | 6 | in the Software without restriction, including without limitation the rights |
madcowswe | 0:31713f62f35b | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
madcowswe | 0:31713f62f35b | 8 | copies of the Software, and to permit persons to whom the Software is |
madcowswe | 0:31713f62f35b | 9 | furnished to do so, subject to the following conditions: |
madcowswe | 0:31713f62f35b | 10 | |
madcowswe | 0:31713f62f35b | 11 | The above copyright notice and this permission notice shall be included in |
madcowswe | 0:31713f62f35b | 12 | all copies or substantial portions of the Software. |
madcowswe | 0:31713f62f35b | 13 | |
madcowswe | 0:31713f62f35b | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
madcowswe | 0:31713f62f35b | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
madcowswe | 0:31713f62f35b | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
madcowswe | 0:31713f62f35b | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
madcowswe | 0:31713f62f35b | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
madcowswe | 0:31713f62f35b | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
madcowswe | 0:31713f62f35b | 20 | THE SOFTWARE. |
madcowswe | 0:31713f62f35b | 21 | */ |
madcowswe | 0:31713f62f35b | 22 | |
madcowswe | 0:31713f62f35b | 23 | #ifndef SOCKET_H_INCLUDED |
madcowswe | 0:31713f62f35b | 24 | #define SOCKET_H_INCLUDED |
madcowswe | 0:31713f62f35b | 25 | |
madcowswe | 0:31713f62f35b | 26 | #define SOCKET_HCI 1 |
madcowswe | 0:31713f62f35b | 27 | #define SOCKET_L2CAP 2 |
madcowswe | 0:31713f62f35b | 28 | #define SOCKET_RFCOM 3 |
madcowswe | 0:31713f62f35b | 29 | |
madcowswe | 0:31713f62f35b | 30 | typedef struct |
madcowswe | 0:31713f62f35b | 31 | { |
madcowswe | 0:31713f62f35b | 32 | u8 AddressSpecific[0]; // BDADDR,psm etc |
madcowswe | 0:31713f62f35b | 33 | } SocketAddrHdr; |
madcowswe | 0:31713f62f35b | 34 | |
madcowswe | 0:31713f62f35b | 35 | enum SocketState |
madcowswe | 0:31713f62f35b | 36 | { |
madcowswe | 0:31713f62f35b | 37 | SocketState_Unknown, |
madcowswe | 0:31713f62f35b | 38 | SocketState_Opening, |
madcowswe | 0:31713f62f35b | 39 | SocketState_Open, |
madcowswe | 0:31713f62f35b | 40 | SocketState_Closing, |
madcowswe | 0:31713f62f35b | 41 | SocketState_Closed |
madcowswe | 0:31713f62f35b | 42 | }; |
madcowswe | 0:31713f62f35b | 43 | |
madcowswe | 0:31713f62f35b | 44 | typedef void (*SocketCallback)(int socket, SocketState state, const u8* data, int len, void* userData); |
madcowswe | 0:31713f62f35b | 45 | |
madcowswe | 0:31713f62f35b | 46 | int Socket_Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData); // Open a socket |
madcowswe | 0:31713f62f35b | 47 | int Socket_Send(int socket, const u8* data, int len); |
madcowswe | 0:31713f62f35b | 48 | int Socket_State(int socket); |
madcowswe | 0:31713f62f35b | 49 | int Socket_Close(int socket); |
madcowswe | 0:31713f62f35b | 50 | |
madcowswe | 0:31713f62f35b | 51 | //=========================================================================== |
madcowswe | 0:31713f62f35b | 52 | //=========================================================================== |
madcowswe | 0:31713f62f35b | 53 | // Don't need to look at or use anything below this line: |
madcowswe | 0:31713f62f35b | 54 | // Internal representation of socket |
madcowswe | 0:31713f62f35b | 55 | |
madcowswe | 0:31713f62f35b | 56 | class SocketHandler; |
madcowswe | 0:31713f62f35b | 57 | class SocketInternal |
madcowswe | 0:31713f62f35b | 58 | { |
madcowswe | 0:31713f62f35b | 59 | public: |
madcowswe | 0:31713f62f35b | 60 | |
madcowswe | 0:31713f62f35b | 61 | u8 ID; |
madcowswe | 0:31713f62f35b | 62 | u8 State; |
madcowswe | 0:31713f62f35b | 63 | u8 Type; |
madcowswe | 0:31713f62f35b | 64 | u8 B0; |
madcowswe | 0:31713f62f35b | 65 | SocketCallback Callback; |
madcowswe | 0:31713f62f35b | 66 | void* userData; |
madcowswe | 0:31713f62f35b | 67 | u8 Data[0]; // Extra socket data starts here |
madcowswe | 0:31713f62f35b | 68 | |
madcowswe | 0:31713f62f35b | 69 | void Recv(const u8* data, int len) |
madcowswe | 0:31713f62f35b | 70 | { |
madcowswe | 0:31713f62f35b | 71 | Callback(ID,(SocketState)State,data,len,userData); |
madcowswe | 0:31713f62f35b | 72 | } |
madcowswe | 0:31713f62f35b | 73 | |
madcowswe | 0:31713f62f35b | 74 | void SetState(SocketState state) |
madcowswe | 0:31713f62f35b | 75 | { |
madcowswe | 0:31713f62f35b | 76 | State = state; |
madcowswe | 0:31713f62f35b | 77 | Callback(ID,(SocketState)State,0,0,userData); |
madcowswe | 0:31713f62f35b | 78 | } |
madcowswe | 0:31713f62f35b | 79 | }; |
madcowswe | 0:31713f62f35b | 80 | |
madcowswe | 0:31713f62f35b | 81 | class SocketHandler |
madcowswe | 0:31713f62f35b | 82 | { |
madcowswe | 0:31713f62f35b | 83 | public: |
madcowswe | 0:31713f62f35b | 84 | virtual int Open(SocketInternal* sock, SocketAddrHdr* addr) = 0; |
madcowswe | 0:31713f62f35b | 85 | virtual int Send(SocketInternal* sock, const u8* data, int len) = 0; |
madcowswe | 0:31713f62f35b | 86 | virtual int Close(SocketInternal* sock) = 0; |
madcowswe | 0:31713f62f35b | 87 | }; |
madcowswe | 0:31713f62f35b | 88 | |
madcowswe | 0:31713f62f35b | 89 | int RegisterSocketHandler(int type, SocketHandler* handler); |
madcowswe | 0:31713f62f35b | 90 | SocketInternal* GetSocketInternal(int socket); |
madcowswe | 0:31713f62f35b | 91 | |
madcowswe | 0:31713f62f35b | 92 | #define ERR_SOCKET_TYPE_NOT_FOUND -200 |
madcowswe | 0:31713f62f35b | 93 | #define ERR_SOCKET_NOT_FOUND -201 |
madcowswe | 0:31713f62f35b | 94 | #define ERR_SOCKET_NONE_LEFT -202 |
madcowswe | 0:31713f62f35b | 95 | |
madcowswe | 0:31713f62f35b | 96 | #endif // SOCKET_H_INCLUDED |