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