3/5

Dependencies:   mbed

Committer:
yuki0701
Date:
Tue Mar 05 04:30:35 2019 +0000
Revision:
1:530908de68c6
Parent:
0:a56be39653d0
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
la00noix 0:a56be39653d0 1 /*
la00noix 0:a56be39653d0 2 Copyright (c) 2010 Peter Barrett
la00noix 0:a56be39653d0 3
la00noix 0:a56be39653d0 4 Permission is hereby granted, free of charge, to any person obtaining a copy
la00noix 0:a56be39653d0 5 of this software and associated documentation files (the "Software"), to deal
la00noix 0:a56be39653d0 6 in the Software without restriction, including without limitation the rights
la00noix 0:a56be39653d0 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
la00noix 0:a56be39653d0 8 copies of the Software, and to permit persons to whom the Software is
la00noix 0:a56be39653d0 9 furnished to do so, subject to the following conditions:
la00noix 0:a56be39653d0 10
la00noix 0:a56be39653d0 11 The above copyright notice and this permission notice shall be included in
la00noix 0:a56be39653d0 12 all copies or substantial portions of the Software.
la00noix 0:a56be39653d0 13
la00noix 0:a56be39653d0 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
la00noix 0:a56be39653d0 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
la00noix 0:a56be39653d0 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
la00noix 0:a56be39653d0 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
la00noix 0:a56be39653d0 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
la00noix 0:a56be39653d0 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
la00noix 0:a56be39653d0 20 THE SOFTWARE.
la00noix 0:a56be39653d0 21 */
la00noix 0:a56be39653d0 22
la00noix 0:a56be39653d0 23 /*
la00noix 0:a56be39653d0 24 Tue Apr 26 2011 Bart Janssens: added a socket listener
la00noix 0:a56be39653d0 25 */
la00noix 0:a56be39653d0 26
la00noix 0:a56be39653d0 27 #include <stdio.h>
la00noix 0:a56be39653d0 28 #include <stdlib.h>
la00noix 0:a56be39653d0 29 #include <stdio.h>
la00noix 0:a56be39653d0 30 #include <string.h>
la00noix 0:a56be39653d0 31
la00noix 0:a56be39653d0 32 #include "Utils.h"
la00noix 0:a56be39653d0 33 #include "Socket.h"
la00noix 0:a56be39653d0 34
la00noix 0:a56be39653d0 35 #define MAX_SOCKET_HANDLERS 3
la00noix 0:a56be39653d0 36 #define MAX_SOCKETS 16
la00noix 0:a56be39653d0 37 #define MAX_LISTEN 8
la00noix 0:a56be39653d0 38
la00noix 0:a56be39653d0 39 class SocketInternalPad
la00noix 0:a56be39653d0 40 {
la00noix 0:a56be39653d0 41 public:
la00noix 0:a56be39653d0 42 SocketInternal si;
la00noix 0:a56be39653d0 43 u8 pad[8];
la00noix 0:a56be39653d0 44 };
la00noix 0:a56be39653d0 45
la00noix 0:a56be39653d0 46
la00noix 0:a56be39653d0 47
la00noix 0:a56be39653d0 48
la00noix 0:a56be39653d0 49 class SocketManager
la00noix 0:a56be39653d0 50 {
la00noix 0:a56be39653d0 51 SocketHandler* _handlers[MAX_SOCKET_HANDLERS];
la00noix 0:a56be39653d0 52 SocketInternalPad _sockets[MAX_SOCKETS];
la00noix 0:a56be39653d0 53 SocketListener _listeners[MAX_LISTEN];
la00noix 0:a56be39653d0 54
la00noix 0:a56be39653d0 55 public:
la00noix 0:a56be39653d0 56 SocketManager()
la00noix 0:a56be39653d0 57 {
la00noix 0:a56be39653d0 58 memset(_handlers,0,sizeof(_handlers));
la00noix 0:a56be39653d0 59 memset(_sockets,0,sizeof(_sockets));
la00noix 0:a56be39653d0 60 memset(_listeners,0,sizeof(_listeners));
la00noix 0:a56be39653d0 61 }
la00noix 0:a56be39653d0 62
la00noix 0:a56be39653d0 63 SocketHandler* GetHandler(int type)
la00noix 0:a56be39653d0 64 {
la00noix 0:a56be39653d0 65 if (type < 1 || type > MAX_SOCKET_HANDLERS)
la00noix 0:a56be39653d0 66 return 0;
la00noix 0:a56be39653d0 67 return _handlers[type - 1];
la00noix 0:a56be39653d0 68 }
la00noix 0:a56be39653d0 69
la00noix 0:a56be39653d0 70 SocketInternal* GetInternal(int s)
la00noix 0:a56be39653d0 71 {
la00noix 0:a56be39653d0 72 if (s < 1 || s > MAX_SOCKETS)
la00noix 0:a56be39653d0 73 return 0;
la00noix 0:a56be39653d0 74 return &_sockets[s - 1].si;
la00noix 0:a56be39653d0 75 }
la00noix 0:a56be39653d0 76
la00noix 0:a56be39653d0 77 int RegisterSocketHandler(int type, SocketHandler* handler)
la00noix 0:a56be39653d0 78 {
la00noix 0:a56be39653d0 79 if (type < 1 || type > MAX_SOCKET_HANDLERS)
la00noix 0:a56be39653d0 80 return ERR_SOCKET_TYPE_NOT_FOUND;
la00noix 0:a56be39653d0 81 _handlers[type - 1] = handler;
la00noix 0:a56be39653d0 82 return 0;
la00noix 0:a56be39653d0 83 }
la00noix 0:a56be39653d0 84
la00noix 0:a56be39653d0 85 int Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData)
la00noix 0:a56be39653d0 86 {
la00noix 0:a56be39653d0 87 SocketHandler* h = GetHandler(type);
la00noix 0:a56be39653d0 88 if (!h)
la00noix 0:a56be39653d0 89 return ERR_SOCKET_TYPE_NOT_FOUND;
la00noix 0:a56be39653d0 90
la00noix 0:a56be39653d0 91 for (int i = 0; i < MAX_SOCKETS; i++)
la00noix 0:a56be39653d0 92 {
la00noix 0:a56be39653d0 93 SocketInternal* si = (SocketInternal*)(_sockets+i);
la00noix 0:a56be39653d0 94 if (si->ID == 0)
la00noix 0:a56be39653d0 95 {
la00noix 0:a56be39653d0 96 //printf("Call to Socket Manager Open \r\n");
la00noix 0:a56be39653d0 97 si->ID = i+1;
la00noix 0:a56be39653d0 98 si->Type = type;
la00noix 0:a56be39653d0 99 si->Callback = callback;
la00noix 0:a56be39653d0 100 si->userData = userData;
la00noix 0:a56be39653d0 101 return h->Open(si,addr);
la00noix 0:a56be39653d0 102 }
la00noix 0:a56be39653d0 103 }
la00noix 0:a56be39653d0 104 return ERR_SOCKET_NONE_LEFT;
la00noix 0:a56be39653d0 105 }
la00noix 0:a56be39653d0 106
la00noix 0:a56be39653d0 107 SocketInternal* Create(int type, SocketAddrHdr* addr, int port)
la00noix 0:a56be39653d0 108 {
la00noix 0:a56be39653d0 109 SocketInternal* si;
la00noix 0:a56be39653d0 110 SocketListener* li;
la00noix 0:a56be39653d0 111 SocketHandler* h = GetHandler(type);
la00noix 0:a56be39653d0 112 if (!h)
la00noix 0:a56be39653d0 113 return 0;
la00noix 0:a56be39653d0 114
la00noix 0:a56be39653d0 115 for (int i = 0; i < MAX_SOCKETS; i++)
la00noix 0:a56be39653d0 116 {
la00noix 0:a56be39653d0 117 si = (SocketInternal*)(_sockets+i);
la00noix 0:a56be39653d0 118 if (si->ID == 0)
la00noix 0:a56be39653d0 119 {
la00noix 0:a56be39653d0 120 si->ID = i+1;
la00noix 0:a56be39653d0 121 si->State = SocketState_Listen;
la00noix 0:a56be39653d0 122 si->Type = type;
la00noix 0:a56be39653d0 123 si->port = port;
la00noix 0:a56be39653d0 124 for (int i = 0; i < MAX_LISTEN; i++){
la00noix 0:a56be39653d0 125 li = (SocketListener*)(_listeners+i);
la00noix 0:a56be39653d0 126 if (( li->Type == si->Type )&& (li->port == si->port)) {
la00noix 0:a56be39653d0 127 si->Callback = li->Callback;
la00noix 0:a56be39653d0 128 si->userData = li->userData;
la00noix 0:a56be39653d0 129 h->Create(si,addr);
la00noix 0:a56be39653d0 130 return si;
la00noix 0:a56be39653d0 131 }
la00noix 0:a56be39653d0 132
la00noix 0:a56be39653d0 133 }
la00noix 0:a56be39653d0 134 }
la00noix 0:a56be39653d0 135 }
la00noix 0:a56be39653d0 136
la00noix 0:a56be39653d0 137 }
la00noix 0:a56be39653d0 138
la00noix 0:a56be39653d0 139
la00noix 0:a56be39653d0 140 int Listen(int type, int port, SocketCallback callback,void* userData)
la00noix 0:a56be39653d0 141 {
la00noix 0:a56be39653d0 142 SocketListener* li;
la00noix 0:a56be39653d0 143 SocketHandler* h = GetHandler(type);
la00noix 0:a56be39653d0 144 if (!h) return ERR_SOCKET_TYPE_NOT_FOUND;
la00noix 0:a56be39653d0 145
la00noix 0:a56be39653d0 146 //printf("Call to Socket Manager Listen \r\n");
la00noix 0:a56be39653d0 147 for (int i = 0; i < MAX_LISTEN; i++)
la00noix 0:a56be39653d0 148 {
la00noix 0:a56be39653d0 149 li = (SocketListener*)(_listeners+i);
la00noix 0:a56be39653d0 150 if (( li->Type == type )&& (li->port == port)) {
la00noix 0:a56be39653d0 151 //printf("Port %d is already in use\r\n",port);
la00noix 0:a56be39653d0 152 return ERR_SOCKET_IN_USE; //in use
la00noix 0:a56be39653d0 153 }
la00noix 0:a56be39653d0 154 }
la00noix 0:a56be39653d0 155
la00noix 0:a56be39653d0 156 for (int i = 0; i < MAX_LISTEN; i++)
la00noix 0:a56be39653d0 157 {
la00noix 0:a56be39653d0 158 li = (SocketListener*)(_listeners+i);
la00noix 0:a56be39653d0 159 if (( li->Type == 0 )&& (li->port == 0)) {
la00noix 0:a56be39653d0 160 li->ID = i+1;
la00noix 0:a56be39653d0 161 li->Type = type;
la00noix 0:a56be39653d0 162 li->port = port;
la00noix 0:a56be39653d0 163 li->Callback = callback;
la00noix 0:a56be39653d0 164 li->userData = userData;
la00noix 0:a56be39653d0 165 //printf("Listening on port %d \r\n",port);
la00noix 0:a56be39653d0 166 return 0;
la00noix 0:a56be39653d0 167 }
la00noix 0:a56be39653d0 168 }
la00noix 0:a56be39653d0 169 //printf("Max listen ports reached\r\n",port);
la00noix 0:a56be39653d0 170 return ERR_SOCKET_NONE_LEFT;
la00noix 0:a56be39653d0 171 }
la00noix 0:a56be39653d0 172
la00noix 0:a56be39653d0 173 int InUse(int type, int port)
la00noix 0:a56be39653d0 174 {
la00noix 0:a56be39653d0 175 SocketListener* li;
la00noix 0:a56be39653d0 176 SocketHandler* h = GetHandler(type);
la00noix 0:a56be39653d0 177 if (!h) return ERR_SOCKET_TYPE_NOT_FOUND;
la00noix 0:a56be39653d0 178 for (int i = 0; i < MAX_LISTEN; i++)
la00noix 0:a56be39653d0 179 {
la00noix 0:a56be39653d0 180 li = (SocketListener*)(_listeners+i);
la00noix 0:a56be39653d0 181 if (( li->Type == type )&& (li->port == port)) {
la00noix 0:a56be39653d0 182
la00noix 0:a56be39653d0 183 //printf("Listen check on port %d OK\r\n",port);
la00noix 0:a56be39653d0 184 return 0;
la00noix 0:a56be39653d0 185 }
la00noix 0:a56be39653d0 186 }
la00noix 0:a56be39653d0 187 //printf("Listen check on port %d NOK\r\n",port);
la00noix 0:a56be39653d0 188 return ERR_SOCKET_NONE_LEFT;
la00noix 0:a56be39653d0 189 }
la00noix 0:a56be39653d0 190
la00noix 0:a56be39653d0 191
la00noix 0:a56be39653d0 192 int Accept(int socket, SocketCallback callback, void* userData)
la00noix 0:a56be39653d0 193 {
la00noix 0:a56be39653d0 194 SocketInternal* si = GetInternal(socket);
la00noix 0:a56be39653d0 195 if (!si || si->ID != socket)
la00noix 0:a56be39653d0 196 return ERR_SOCKET_NOT_FOUND;
la00noix 0:a56be39653d0 197
la00noix 0:a56be39653d0 198 si->Callback = callback;
la00noix 0:a56be39653d0 199 si->userData = userData;
la00noix 0:a56be39653d0 200
la00noix 0:a56be39653d0 201 //printf("Call to Socket Manager Accept \r\n");
la00noix 0:a56be39653d0 202 return 0;
la00noix 0:a56be39653d0 203
la00noix 0:a56be39653d0 204 }
la00noix 0:a56be39653d0 205
la00noix 0:a56be39653d0 206 int Send(int socket, const u8* data, int len)
la00noix 0:a56be39653d0 207 {
la00noix 0:a56be39653d0 208 //printf("Call to Socket Manager Send \r\n");
la00noix 0:a56be39653d0 209 SocketInternal* si = GetInternal(socket);
la00noix 0:a56be39653d0 210 //printf("socket = %d si->ID = %d si->Type = %d \r\n", socket, si->ID, si->Type);
la00noix 0:a56be39653d0 211 if (!si || si->ID != socket){
la00noix 0:a56be39653d0 212 //printf("send: socket not found \r\n");
la00noix 0:a56be39653d0 213 return ERR_SOCKET_NOT_FOUND;
la00noix 0:a56be39653d0 214 }
la00noix 0:a56be39653d0 215 //printf("Calling l2cap send \r\n");
la00noix 0:a56be39653d0 216
la00noix 0:a56be39653d0 217 SocketHandler* h = GetHandler(si->Type);
la00noix 0:a56be39653d0 218 if (!h) {
la00noix 0:a56be39653d0 219 //printf("Send: no socket type found \r\n");
la00noix 0:a56be39653d0 220 return ERR_SOCKET_TYPE_NOT_FOUND;
la00noix 0:a56be39653d0 221 }
la00noix 0:a56be39653d0 222 return h->Send(si,data,len);
la00noix 0:a56be39653d0 223
la00noix 0:a56be39653d0 224 }
la00noix 0:a56be39653d0 225
la00noix 0:a56be39653d0 226 int Close(int socket)
la00noix 0:a56be39653d0 227 {
la00noix 0:a56be39653d0 228 SocketInternal* si = GetInternal(socket);
la00noix 0:a56be39653d0 229 if (!si || si->ID != socket)
la00noix 0:a56be39653d0 230 return ERR_SOCKET_NOT_FOUND;
la00noix 0:a56be39653d0 231 si->ID = 0;
la00noix 0:a56be39653d0 232 return GetHandler(si->Type)->Close(si);
la00noix 0:a56be39653d0 233 }
la00noix 0:a56be39653d0 234 };
la00noix 0:a56be39653d0 235
la00noix 0:a56be39653d0 236 SocketManager gSocketManager;
la00noix 0:a56be39653d0 237
la00noix 0:a56be39653d0 238 int Socket_Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData)
la00noix 0:a56be39653d0 239 {
la00noix 0:a56be39653d0 240 //printf("Call to Socket Open \r\n");
la00noix 0:a56be39653d0 241 return gSocketManager.Open(type,addr,callback,userData);
la00noix 0:a56be39653d0 242 }
la00noix 0:a56be39653d0 243
la00noix 0:a56be39653d0 244 SocketInternal* Socket_Create(int type, SocketAddrHdr* addr, int port)
la00noix 0:a56be39653d0 245 {
la00noix 0:a56be39653d0 246 return gSocketManager.Create(type, addr, port);
la00noix 0:a56be39653d0 247 }
la00noix 0:a56be39653d0 248
la00noix 0:a56be39653d0 249 int Socket_Send(int socket, const u8* data, int len)
la00noix 0:a56be39653d0 250 {
la00noix 0:a56be39653d0 251 //printf("Call to Socket_Send \r\n");
la00noix 0:a56be39653d0 252 return gSocketManager.Send(socket,data,len);
la00noix 0:a56be39653d0 253 }
la00noix 0:a56be39653d0 254
la00noix 0:a56be39653d0 255 int Socket_Close(int socket)
la00noix 0:a56be39653d0 256 {
la00noix 0:a56be39653d0 257 return gSocketManager.Close(socket);
la00noix 0:a56be39653d0 258 }
la00noix 0:a56be39653d0 259
la00noix 0:a56be39653d0 260 int Socket_Listen(int type, int port,SocketCallback callback, void* userData)
la00noix 0:a56be39653d0 261 {
la00noix 0:a56be39653d0 262 //printf("Call to Socket_Listen \r\n");
la00noix 0:a56be39653d0 263 return gSocketManager.Listen(type, port,callback,userData);
la00noix 0:a56be39653d0 264 }
la00noix 0:a56be39653d0 265
la00noix 0:a56be39653d0 266 int Socket_InUse(int type, int port)
la00noix 0:a56be39653d0 267 {
la00noix 0:a56be39653d0 268 //printf("Call to Socket_InUse \r\n");
la00noix 0:a56be39653d0 269 return gSocketManager.InUse(type, port);
la00noix 0:a56be39653d0 270 }
la00noix 0:a56be39653d0 271
la00noix 0:a56be39653d0 272 int Socket_Accept(int socket, SocketCallback callback, void* userData)
la00noix 0:a56be39653d0 273 {
la00noix 0:a56be39653d0 274 //printf("Call to Socket_Accept \r\n");
la00noix 0:a56be39653d0 275 return gSocketManager.Accept(socket, callback, userData);
la00noix 0:a56be39653d0 276 }
la00noix 0:a56be39653d0 277
la00noix 0:a56be39653d0 278 int RegisterSocketHandler(int type, SocketHandler* handler)
la00noix 0:a56be39653d0 279 {
la00noix 0:a56be39653d0 280 return gSocketManager.RegisterSocketHandler(type,handler);
la00noix 0:a56be39653d0 281 }
la00noix 0:a56be39653d0 282
la00noix 0:a56be39653d0 283 SocketInternal* GetSocketInternal(int socket)
la00noix 0:a56be39653d0 284 {
la00noix 0:a56be39653d0 285 return gSocketManager.GetInternal(socket);
la00noix 0:a56be39653d0 286 }
la00noix 0:a56be39653d0 287