XOOMの動作状況を聞き処理を変えてみました。 USBケーブルを抜いた際に処理を終了するようにしました。

Dependencies:   mbed

Committer:
abe00makoto
Date:
Fri May 27 18:51:15 2011 +0000
Revision:
3:432e5675d240
Parent:
0:9fb6c423e32c
nexus one support
maybe support add XOOM ,nexus S

Who changed what in which revision?

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