robostep8th / Mbed 2 deprecated harurobo_PS3_com7

Dependencies:   mbed

Committer:
la00noix
Date:
Thu Mar 07 03:01:12 2019 +0000
Revision:
2:817bcbc3431e
Parent:
0:798b73e805d9
a

Who changed what in which revision?

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