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