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