![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
主導機 mbed用のプログラムです
Fork of F3RC_syudou_master by
Socket.cpp@0:736c76a75def, 2015-04-30 (annotated)
- Committer:
- hirokimineshita
- Date:
- Thu Apr 30 05:59:05 2015 +0000
- Revision:
- 0:736c76a75def
- Child:
- 16:b232fd9ee9c2
2015/04/30 PM 2 upload
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 | #include <stdio.h> |
hirokimineshita | 0:736c76a75def | 28 | #include <stdlib.h> |
hirokimineshita | 0:736c76a75def | 29 | #include <stdio.h> |
hirokimineshita | 0:736c76a75def | 30 | #include <string.h> |
hirokimineshita | 0:736c76a75def | 31 | |
hirokimineshita | 0:736c76a75def | 32 | #include "Utils.h" |
hirokimineshita | 0:736c76a75def | 33 | #include "Socket.h" |
hirokimineshita | 0:736c76a75def | 34 | |
hirokimineshita | 0:736c76a75def | 35 | #define MAX_SOCKET_HANDLERS 3 |
hirokimineshita | 0:736c76a75def | 36 | #define MAX_SOCKETS 16 |
hirokimineshita | 0:736c76a75def | 37 | #define MAX_LISTEN 8 |
hirokimineshita | 0:736c76a75def | 38 | |
hirokimineshita | 0:736c76a75def | 39 | class SocketInternalPad |
hirokimineshita | 0:736c76a75def | 40 | { |
hirokimineshita | 0:736c76a75def | 41 | public: |
hirokimineshita | 0:736c76a75def | 42 | SocketInternal si; |
hirokimineshita | 0:736c76a75def | 43 | u8 pad[8]; |
hirokimineshita | 0:736c76a75def | 44 | }; |
hirokimineshita | 0:736c76a75def | 45 | |
hirokimineshita | 0:736c76a75def | 46 | |
hirokimineshita | 0:736c76a75def | 47 | |
hirokimineshita | 0:736c76a75def | 48 | |
hirokimineshita | 0:736c76a75def | 49 | class SocketManager |
hirokimineshita | 0:736c76a75def | 50 | { |
hirokimineshita | 0:736c76a75def | 51 | SocketHandler* _handlers[MAX_SOCKET_HANDLERS]; |
hirokimineshita | 0:736c76a75def | 52 | SocketInternalPad _sockets[MAX_SOCKETS]; |
hirokimineshita | 0:736c76a75def | 53 | SocketListener _listeners[MAX_LISTEN]; |
hirokimineshita | 0:736c76a75def | 54 | |
hirokimineshita | 0:736c76a75def | 55 | public: |
hirokimineshita | 0:736c76a75def | 56 | SocketManager() |
hirokimineshita | 0:736c76a75def | 57 | { |
hirokimineshita | 0:736c76a75def | 58 | memset(_handlers,0,sizeof(_handlers)); |
hirokimineshita | 0:736c76a75def | 59 | memset(_sockets,0,sizeof(_sockets)); |
hirokimineshita | 0:736c76a75def | 60 | memset(_listeners,0,sizeof(_listeners)); |
hirokimineshita | 0:736c76a75def | 61 | } |
hirokimineshita | 0:736c76a75def | 62 | |
hirokimineshita | 0:736c76a75def | 63 | SocketHandler* GetHandler(int type) |
hirokimineshita | 0:736c76a75def | 64 | { |
hirokimineshita | 0:736c76a75def | 65 | if (type < 1 || type > MAX_SOCKET_HANDLERS) |
hirokimineshita | 0:736c76a75def | 66 | return 0; |
hirokimineshita | 0:736c76a75def | 67 | return _handlers[type - 1]; |
hirokimineshita | 0:736c76a75def | 68 | } |
hirokimineshita | 0:736c76a75def | 69 | |
hirokimineshita | 0:736c76a75def | 70 | SocketInternal* GetInternal(int s) |
hirokimineshita | 0:736c76a75def | 71 | { |
hirokimineshita | 0:736c76a75def | 72 | if (s < 1 || s > MAX_SOCKETS) |
hirokimineshita | 0:736c76a75def | 73 | return 0; |
hirokimineshita | 0:736c76a75def | 74 | return &_sockets[s - 1].si; |
hirokimineshita | 0:736c76a75def | 75 | } |
hirokimineshita | 0:736c76a75def | 76 | |
hirokimineshita | 0:736c76a75def | 77 | int RegisterSocketHandler(int type, SocketHandler* handler) |
hirokimineshita | 0:736c76a75def | 78 | { |
hirokimineshita | 0:736c76a75def | 79 | if (type < 1 || type > MAX_SOCKET_HANDLERS) |
hirokimineshita | 0:736c76a75def | 80 | return ERR_SOCKET_TYPE_NOT_FOUND; |
hirokimineshita | 0:736c76a75def | 81 | _handlers[type - 1] = handler; |
hirokimineshita | 0:736c76a75def | 82 | return 0; |
hirokimineshita | 0:736c76a75def | 83 | } |
hirokimineshita | 0:736c76a75def | 84 | |
hirokimineshita | 0:736c76a75def | 85 | int Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData) |
hirokimineshita | 0:736c76a75def | 86 | { |
hirokimineshita | 0:736c76a75def | 87 | SocketHandler* h = GetHandler(type); |
hirokimineshita | 0:736c76a75def | 88 | if (!h) |
hirokimineshita | 0:736c76a75def | 89 | return ERR_SOCKET_TYPE_NOT_FOUND; |
hirokimineshita | 0:736c76a75def | 90 | |
hirokimineshita | 0:736c76a75def | 91 | for (int i = 0; i < MAX_SOCKETS; i++) |
hirokimineshita | 0:736c76a75def | 92 | { |
hirokimineshita | 0:736c76a75def | 93 | SocketInternal* si = (SocketInternal*)(_sockets+i); |
hirokimineshita | 0:736c76a75def | 94 | if (si->ID == 0) |
hirokimineshita | 0:736c76a75def | 95 | { |
hirokimineshita | 0:736c76a75def | 96 | //printf("Call to Socket Manager Open \r\n"); |
hirokimineshita | 0:736c76a75def | 97 | si->ID = i+1; |
hirokimineshita | 0:736c76a75def | 98 | si->Type = type; |
hirokimineshita | 0:736c76a75def | 99 | si->Callback = callback; |
hirokimineshita | 0:736c76a75def | 100 | si->userData = userData; |
hirokimineshita | 0:736c76a75def | 101 | return h->Open(si,addr); |
hirokimineshita | 0:736c76a75def | 102 | } |
hirokimineshita | 0:736c76a75def | 103 | } |
hirokimineshita | 0:736c76a75def | 104 | return ERR_SOCKET_NONE_LEFT; |
hirokimineshita | 0:736c76a75def | 105 | } |
hirokimineshita | 0:736c76a75def | 106 | |
hirokimineshita | 0:736c76a75def | 107 | SocketInternal* Create(int type, SocketAddrHdr* addr, int port) |
hirokimineshita | 0:736c76a75def | 108 | { |
hirokimineshita | 0:736c76a75def | 109 | SocketInternal* si; |
hirokimineshita | 0:736c76a75def | 110 | SocketListener* li; |
hirokimineshita | 0:736c76a75def | 111 | SocketHandler* h = GetHandler(type); |
hirokimineshita | 0:736c76a75def | 112 | if (!h) |
hirokimineshita | 0:736c76a75def | 113 | return 0; |
hirokimineshita | 0:736c76a75def | 114 | |
hirokimineshita | 0:736c76a75def | 115 | for (int i = 0; i < MAX_SOCKETS; i++) |
hirokimineshita | 0:736c76a75def | 116 | { |
hirokimineshita | 0:736c76a75def | 117 | si = (SocketInternal*)(_sockets+i); |
hirokimineshita | 0:736c76a75def | 118 | if (si->ID == 0) |
hirokimineshita | 0:736c76a75def | 119 | { |
hirokimineshita | 0:736c76a75def | 120 | si->ID = i+1; |
hirokimineshita | 0:736c76a75def | 121 | si->State = SocketState_Listen; |
hirokimineshita | 0:736c76a75def | 122 | si->Type = type; |
hirokimineshita | 0:736c76a75def | 123 | si->port = port; |
hirokimineshita | 0:736c76a75def | 124 | for (int i = 0; i < MAX_LISTEN; i++){ |
hirokimineshita | 0:736c76a75def | 125 | li = (SocketListener*)(_listeners+i); |
hirokimineshita | 0:736c76a75def | 126 | if (( li->Type == si->Type )&& (li->port == si->port)) { |
hirokimineshita | 0:736c76a75def | 127 | si->Callback = li->Callback; |
hirokimineshita | 0:736c76a75def | 128 | si->userData = li->userData; |
hirokimineshita | 0:736c76a75def | 129 | h->Create(si,addr); |
hirokimineshita | 0:736c76a75def | 130 | return si; |
hirokimineshita | 0:736c76a75def | 131 | } |
hirokimineshita | 0:736c76a75def | 132 | |
hirokimineshita | 0:736c76a75def | 133 | } |
hirokimineshita | 0:736c76a75def | 134 | } |
hirokimineshita | 0:736c76a75def | 135 | } |
hirokimineshita | 0:736c76a75def | 136 | |
hirokimineshita | 0:736c76a75def | 137 | } |
hirokimineshita | 0:736c76a75def | 138 | |
hirokimineshita | 0:736c76a75def | 139 | |
hirokimineshita | 0:736c76a75def | 140 | int Listen(int type, int port, SocketCallback callback,void* userData) |
hirokimineshita | 0:736c76a75def | 141 | { |
hirokimineshita | 0:736c76a75def | 142 | SocketListener* li; |
hirokimineshita | 0:736c76a75def | 143 | SocketHandler* h = GetHandler(type); |
hirokimineshita | 0:736c76a75def | 144 | if (!h) return ERR_SOCKET_TYPE_NOT_FOUND; |
hirokimineshita | 0:736c76a75def | 145 | |
hirokimineshita | 0:736c76a75def | 146 | //printf("Call to Socket Manager Listen \r\n"); |
hirokimineshita | 0:736c76a75def | 147 | for (int i = 0; i < MAX_LISTEN; i++) |
hirokimineshita | 0:736c76a75def | 148 | { |
hirokimineshita | 0:736c76a75def | 149 | li = (SocketListener*)(_listeners+i); |
hirokimineshita | 0:736c76a75def | 150 | if (( li->Type == type )&& (li->port == port)) { |
hirokimineshita | 0:736c76a75def | 151 | //printf("Port %d is already in use\r\n",port); |
hirokimineshita | 0:736c76a75def | 152 | return ERR_SOCKET_IN_USE; //in use |
hirokimineshita | 0:736c76a75def | 153 | } |
hirokimineshita | 0:736c76a75def | 154 | } |
hirokimineshita | 0:736c76a75def | 155 | |
hirokimineshita | 0:736c76a75def | 156 | for (int i = 0; i < MAX_LISTEN; i++) |
hirokimineshita | 0:736c76a75def | 157 | { |
hirokimineshita | 0:736c76a75def | 158 | li = (SocketListener*)(_listeners+i); |
hirokimineshita | 0:736c76a75def | 159 | if (( li->Type == 0 )&& (li->port == 0)) { |
hirokimineshita | 0:736c76a75def | 160 | li->ID = i+1; |
hirokimineshita | 0:736c76a75def | 161 | li->Type = type; |
hirokimineshita | 0:736c76a75def | 162 | li->port = port; |
hirokimineshita | 0:736c76a75def | 163 | li->Callback = callback; |
hirokimineshita | 0:736c76a75def | 164 | li->userData = userData; |
hirokimineshita | 0:736c76a75def | 165 | //printf("Listening on port %d \r\n",port); |
hirokimineshita | 0:736c76a75def | 166 | return 0; |
hirokimineshita | 0:736c76a75def | 167 | } |
hirokimineshita | 0:736c76a75def | 168 | } |
hirokimineshita | 0:736c76a75def | 169 | //printf("Max listen ports reached\r\n",port); |
hirokimineshita | 0:736c76a75def | 170 | return ERR_SOCKET_NONE_LEFT; |
hirokimineshita | 0:736c76a75def | 171 | } |
hirokimineshita | 0:736c76a75def | 172 | |
hirokimineshita | 0:736c76a75def | 173 | int InUse(int type, int port) |
hirokimineshita | 0:736c76a75def | 174 | { |
hirokimineshita | 0:736c76a75def | 175 | SocketListener* li; |
hirokimineshita | 0:736c76a75def | 176 | SocketHandler* h = GetHandler(type); |
hirokimineshita | 0:736c76a75def | 177 | if (!h) return ERR_SOCKET_TYPE_NOT_FOUND; |
hirokimineshita | 0:736c76a75def | 178 | for (int i = 0; i < MAX_LISTEN; i++) |
hirokimineshita | 0:736c76a75def | 179 | { |
hirokimineshita | 0:736c76a75def | 180 | li = (SocketListener*)(_listeners+i); |
hirokimineshita | 0:736c76a75def | 181 | if (( li->Type == type )&& (li->port == port)) { |
hirokimineshita | 0:736c76a75def | 182 | |
hirokimineshita | 0:736c76a75def | 183 | //printf("Listen check on port %d OK\r\n",port); |
hirokimineshita | 0:736c76a75def | 184 | return 0; |
hirokimineshita | 0:736c76a75def | 185 | } |
hirokimineshita | 0:736c76a75def | 186 | } |
hirokimineshita | 0:736c76a75def | 187 | //printf("Listen check on port %d NOK\r\n",port); |
hirokimineshita | 0:736c76a75def | 188 | return ERR_SOCKET_NONE_LEFT; |
hirokimineshita | 0:736c76a75def | 189 | } |
hirokimineshita | 0:736c76a75def | 190 | |
hirokimineshita | 0:736c76a75def | 191 | |
hirokimineshita | 0:736c76a75def | 192 | int Accept(int socket, SocketCallback callback, void* userData) |
hirokimineshita | 0:736c76a75def | 193 | { |
hirokimineshita | 0:736c76a75def | 194 | SocketInternal* si = GetInternal(socket); |
hirokimineshita | 0:736c76a75def | 195 | if (!si || si->ID != socket) |
hirokimineshita | 0:736c76a75def | 196 | return ERR_SOCKET_NOT_FOUND; |
hirokimineshita | 0:736c76a75def | 197 | |
hirokimineshita | 0:736c76a75def | 198 | si->Callback = callback; |
hirokimineshita | 0:736c76a75def | 199 | si->userData = userData; |
hirokimineshita | 0:736c76a75def | 200 | |
hirokimineshita | 0:736c76a75def | 201 | //printf("Call to Socket Manager Accept \r\n"); |
hirokimineshita | 0:736c76a75def | 202 | return 0; |
hirokimineshita | 0:736c76a75def | 203 | |
hirokimineshita | 0:736c76a75def | 204 | } |
hirokimineshita | 0:736c76a75def | 205 | |
hirokimineshita | 0:736c76a75def | 206 | int Send(int socket, const u8* data, int len) |
hirokimineshita | 0:736c76a75def | 207 | { |
hirokimineshita | 0:736c76a75def | 208 | //printf("Call to Socket Manager Send \r\n"); |
hirokimineshita | 0:736c76a75def | 209 | SocketInternal* si = GetInternal(socket); |
hirokimineshita | 0:736c76a75def | 210 | //printf("socket = %d si->ID = %d si->Type = %d \r\n", socket, si->ID, si->Type); |
hirokimineshita | 0:736c76a75def | 211 | if (!si || si->ID != socket){ |
hirokimineshita | 0:736c76a75def | 212 | //printf("send: socket not found \r\n"); |
hirokimineshita | 0:736c76a75def | 213 | return ERR_SOCKET_NOT_FOUND; |
hirokimineshita | 0:736c76a75def | 214 | } |
hirokimineshita | 0:736c76a75def | 215 | //printf("Calling l2cap send \r\n"); |
hirokimineshita | 0:736c76a75def | 216 | |
hirokimineshita | 0:736c76a75def | 217 | SocketHandler* h = GetHandler(si->Type); |
hirokimineshita | 0:736c76a75def | 218 | if (!h) { |
hirokimineshita | 0:736c76a75def | 219 | //printf("Send: no socket type found \r\n"); |
hirokimineshita | 0:736c76a75def | 220 | return ERR_SOCKET_TYPE_NOT_FOUND; |
hirokimineshita | 0:736c76a75def | 221 | } |
hirokimineshita | 0:736c76a75def | 222 | return h->Send(si,data,len); |
hirokimineshita | 0:736c76a75def | 223 | |
hirokimineshita | 0:736c76a75def | 224 | } |
hirokimineshita | 0:736c76a75def | 225 | |
hirokimineshita | 0:736c76a75def | 226 | int Close(int socket) |
hirokimineshita | 0:736c76a75def | 227 | { |
hirokimineshita | 0:736c76a75def | 228 | SocketInternal* si = GetInternal(socket); |
hirokimineshita | 0:736c76a75def | 229 | if (!si || si->ID != socket) |
hirokimineshita | 0:736c76a75def | 230 | return ERR_SOCKET_NOT_FOUND; |
hirokimineshita | 0:736c76a75def | 231 | si->ID = 0; |
hirokimineshita | 0:736c76a75def | 232 | return GetHandler(si->Type)->Close(si); |
hirokimineshita | 0:736c76a75def | 233 | } |
hirokimineshita | 0:736c76a75def | 234 | }; |
hirokimineshita | 0:736c76a75def | 235 | |
hirokimineshita | 0:736c76a75def | 236 | SocketManager gSocketManager; |
hirokimineshita | 0:736c76a75def | 237 | |
hirokimineshita | 0:736c76a75def | 238 | int Socket_Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData) |
hirokimineshita | 0:736c76a75def | 239 | { |
hirokimineshita | 0:736c76a75def | 240 | //printf("Call to Socket Open \r\n"); |
hirokimineshita | 0:736c76a75def | 241 | return gSocketManager.Open(type,addr,callback,userData); |
hirokimineshita | 0:736c76a75def | 242 | } |
hirokimineshita | 0:736c76a75def | 243 | |
hirokimineshita | 0:736c76a75def | 244 | SocketInternal* Socket_Create(int type, SocketAddrHdr* addr, int port) |
hirokimineshita | 0:736c76a75def | 245 | { |
hirokimineshita | 0:736c76a75def | 246 | return gSocketManager.Create(type, addr, port); |
hirokimineshita | 0:736c76a75def | 247 | } |
hirokimineshita | 0:736c76a75def | 248 | |
hirokimineshita | 0:736c76a75def | 249 | int Socket_Send(int socket, const u8* data, int len) |
hirokimineshita | 0:736c76a75def | 250 | { |
hirokimineshita | 0:736c76a75def | 251 | //printf("Call to Socket_Send \r\n"); |
hirokimineshita | 0:736c76a75def | 252 | return gSocketManager.Send(socket,data,len); |
hirokimineshita | 0:736c76a75def | 253 | } |
hirokimineshita | 0:736c76a75def | 254 | |
hirokimineshita | 0:736c76a75def | 255 | int Socket_Close(int socket) |
hirokimineshita | 0:736c76a75def | 256 | { |
hirokimineshita | 0:736c76a75def | 257 | return gSocketManager.Close(socket); |
hirokimineshita | 0:736c76a75def | 258 | } |
hirokimineshita | 0:736c76a75def | 259 | |
hirokimineshita | 0:736c76a75def | 260 | int Socket_Listen(int type, int port,SocketCallback callback, void* userData) |
hirokimineshita | 0:736c76a75def | 261 | { |
hirokimineshita | 0:736c76a75def | 262 | //printf("Call to Socket_Listen \r\n"); |
hirokimineshita | 0:736c76a75def | 263 | return gSocketManager.Listen(type, port,callback,userData); |
hirokimineshita | 0:736c76a75def | 264 | } |
hirokimineshita | 0:736c76a75def | 265 | |
hirokimineshita | 0:736c76a75def | 266 | int Socket_InUse(int type, int port) |
hirokimineshita | 0:736c76a75def | 267 | { |
hirokimineshita | 0:736c76a75def | 268 | //printf("Call to Socket_InUse \r\n"); |
hirokimineshita | 0:736c76a75def | 269 | return gSocketManager.InUse(type, port); |
hirokimineshita | 0:736c76a75def | 270 | } |
hirokimineshita | 0:736c76a75def | 271 | |
hirokimineshita | 0:736c76a75def | 272 | int Socket_Accept(int socket, SocketCallback callback, void* userData) |
hirokimineshita | 0:736c76a75def | 273 | { |
hirokimineshita | 0:736c76a75def | 274 | //printf("Call to Socket_Accept \r\n"); |
hirokimineshita | 0:736c76a75def | 275 | return gSocketManager.Accept(socket, callback, userData); |
hirokimineshita | 0:736c76a75def | 276 | } |
hirokimineshita | 0:736c76a75def | 277 | |
hirokimineshita | 0:736c76a75def | 278 | int RegisterSocketHandler(int type, SocketHandler* handler) |
hirokimineshita | 0:736c76a75def | 279 | { |
hirokimineshita | 0:736c76a75def | 280 | return gSocketManager.RegisterSocketHandler(type,handler); |
hirokimineshita | 0:736c76a75def | 281 | } |
hirokimineshita | 0:736c76a75def | 282 | |
hirokimineshita | 0:736c76a75def | 283 | SocketInternal* GetSocketInternal(int socket) |
hirokimineshita | 0:736c76a75def | 284 | { |
hirokimineshita | 0:736c76a75def | 285 | return gSocketManager.GetInternal(socket); |
hirokimineshita | 0:736c76a75def | 286 | } |
hirokimineshita | 0:736c76a75def | 287 |