Dependencies:   mbed

Committer:
abe00makoto
Date:
Thu May 26 19:39:37 2011 +0000
Revision:
1:237cfff63ef8
Parent:
0:e939856c1939

        

Who changed what in which revision?

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