Dependents:   Test_Wiimote

Committer:
bediyap
Date:
Sat Dec 17 06:59:19 2011 +0000
Revision:
3:37e5ebd509ea
Parent:
0:f6f434d9a03a
disable serial
todo:
auto detect wii disconnection due to battery failure ...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bediyap 0:f6f434d9a03a 1 /*
bediyap 0:f6f434d9a03a 2 Copyright (c) 2010 Peter Barrett
bediyap 0:f6f434d9a03a 3
bediyap 0:f6f434d9a03a 4 Permission is hereby granted, free of charge, to any person obtaining a copy
bediyap 0:f6f434d9a03a 5 of this software and associated documentation files (the "Software"), to deal
bediyap 0:f6f434d9a03a 6 in the Software without restriction, including without limitation the rights
bediyap 0:f6f434d9a03a 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
bediyap 0:f6f434d9a03a 8 copies of the Software, and to permit persons to whom the Software is
bediyap 0:f6f434d9a03a 9 furnished to do so, subject to the following conditions:
bediyap 0:f6f434d9a03a 10
bediyap 0:f6f434d9a03a 11 The above copyright notice and this permission notice shall be included in
bediyap 0:f6f434d9a03a 12 all copies or substantial portions of the Software.
bediyap 0:f6f434d9a03a 13
bediyap 0:f6f434d9a03a 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
bediyap 0:f6f434d9a03a 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
bediyap 0:f6f434d9a03a 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
bediyap 0:f6f434d9a03a 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
bediyap 0:f6f434d9a03a 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
bediyap 0:f6f434d9a03a 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
bediyap 0:f6f434d9a03a 20 THE SOFTWARE.
bediyap 0:f6f434d9a03a 21 */
bediyap 0:f6f434d9a03a 22
bediyap 0:f6f434d9a03a 23 #include <stdio.h>
bediyap 0:f6f434d9a03a 24 #include <stdlib.h>
bediyap 0:f6f434d9a03a 25 #include <stdio.h>
bediyap 0:f6f434d9a03a 26 #include <string.h>
bediyap 0:f6f434d9a03a 27
bediyap 0:f6f434d9a03a 28 #include "Utils.h"
bediyap 0:f6f434d9a03a 29 #include "Socket.h"
bediyap 0:f6f434d9a03a 30
bediyap 0:f6f434d9a03a 31 #define MAX_SOCKET_HANDLERS 3
bediyap 0:f6f434d9a03a 32 #define MAX_SOCKETS 16
bediyap 0:f6f434d9a03a 33
bediyap 0:f6f434d9a03a 34 class SocketInternalPad
bediyap 0:f6f434d9a03a 35 {
bediyap 0:f6f434d9a03a 36 public:
bediyap 0:f6f434d9a03a 37 SocketInternal si;
bediyap 0:f6f434d9a03a 38 u8 pad[8];
bediyap 0:f6f434d9a03a 39 };
bediyap 0:f6f434d9a03a 40
bediyap 0:f6f434d9a03a 41 class SocketManager
bediyap 0:f6f434d9a03a 42 {
bediyap 0:f6f434d9a03a 43 SocketHandler* _handlers[MAX_SOCKET_HANDLERS];
bediyap 0:f6f434d9a03a 44 SocketInternalPad _sockets[MAX_SOCKETS];
bediyap 0:f6f434d9a03a 45
bediyap 0:f6f434d9a03a 46 public:
bediyap 0:f6f434d9a03a 47 SocketManager()
bediyap 0:f6f434d9a03a 48 {
bediyap 0:f6f434d9a03a 49 memset(_handlers,0,sizeof(_handlers));
bediyap 0:f6f434d9a03a 50 memset(_sockets,0,sizeof(_sockets));
bediyap 0:f6f434d9a03a 51 }
bediyap 0:f6f434d9a03a 52
bediyap 0:f6f434d9a03a 53 SocketHandler* GetHandler(int type)
bediyap 0:f6f434d9a03a 54 {
bediyap 0:f6f434d9a03a 55 if (type < 1 || type > MAX_SOCKET_HANDLERS)
bediyap 0:f6f434d9a03a 56 return 0;
bediyap 0:f6f434d9a03a 57 return _handlers[type - 1];
bediyap 0:f6f434d9a03a 58 }
bediyap 0:f6f434d9a03a 59
bediyap 0:f6f434d9a03a 60 SocketInternal* GetInternal(int s)
bediyap 0:f6f434d9a03a 61 {
bediyap 0:f6f434d9a03a 62 if (s < 1 || s > MAX_SOCKETS)
bediyap 0:f6f434d9a03a 63 return 0;
bediyap 0:f6f434d9a03a 64 return &_sockets[s - 1].si;
bediyap 0:f6f434d9a03a 65 }
bediyap 0:f6f434d9a03a 66
bediyap 0:f6f434d9a03a 67 int RegisterSocketHandler(int type, SocketHandler* handler)
bediyap 0:f6f434d9a03a 68 {
bediyap 0:f6f434d9a03a 69 if (type < 1 || type > MAX_SOCKET_HANDLERS)
bediyap 0:f6f434d9a03a 70 return ERR_SOCKET_TYPE_NOT_FOUND;
bediyap 0:f6f434d9a03a 71 _handlers[type - 1] = handler;
bediyap 0:f6f434d9a03a 72 return 0;
bediyap 0:f6f434d9a03a 73 }
bediyap 0:f6f434d9a03a 74
bediyap 0:f6f434d9a03a 75 int Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData)
bediyap 0:f6f434d9a03a 76 {
bediyap 0:f6f434d9a03a 77 SocketHandler* h = GetHandler(type);
bediyap 0:f6f434d9a03a 78 if (!h)
bediyap 0:f6f434d9a03a 79 return ERR_SOCKET_TYPE_NOT_FOUND;
bediyap 0:f6f434d9a03a 80
bediyap 0:f6f434d9a03a 81 for (int i = 0; i < MAX_SOCKETS; i++)
bediyap 0:f6f434d9a03a 82 {
bediyap 0:f6f434d9a03a 83 SocketInternal* si = (SocketInternal*)(_sockets+i);
bediyap 0:f6f434d9a03a 84 if (si->ID == 0)
bediyap 0:f6f434d9a03a 85 {
bediyap 0:f6f434d9a03a 86 si->ID = i+1;
bediyap 0:f6f434d9a03a 87 si->Type = type;
bediyap 0:f6f434d9a03a 88 si->Callback = callback;
bediyap 0:f6f434d9a03a 89 si->userData = userData;
bediyap 0:f6f434d9a03a 90 return h->Open(si,addr);
bediyap 0:f6f434d9a03a 91 }
bediyap 0:f6f434d9a03a 92 }
bediyap 0:f6f434d9a03a 93 return ERR_SOCKET_NONE_LEFT;
bediyap 0:f6f434d9a03a 94 }
bediyap 0:f6f434d9a03a 95
bediyap 0:f6f434d9a03a 96 int Send(int socket, const u8* data, int len)
bediyap 0:f6f434d9a03a 97 {
bediyap 0:f6f434d9a03a 98 SocketInternal* si = GetInternal(socket);
bediyap 0:f6f434d9a03a 99 if (!si || si->ID != socket)
bediyap 0:f6f434d9a03a 100 return ERR_SOCKET_NOT_FOUND;
bediyap 0:f6f434d9a03a 101 return GetHandler(si->Type)->Send(si,data,len);
bediyap 0:f6f434d9a03a 102 }
bediyap 0:f6f434d9a03a 103
bediyap 0:f6f434d9a03a 104 int Close(int socket)
bediyap 0:f6f434d9a03a 105 {
bediyap 0:f6f434d9a03a 106 SocketInternal* si = GetInternal(socket);
bediyap 0:f6f434d9a03a 107 if (!si || si->ID != socket)
bediyap 0:f6f434d9a03a 108 return ERR_SOCKET_NOT_FOUND;
bediyap 0:f6f434d9a03a 109 si->ID = 0;
bediyap 0:f6f434d9a03a 110 return GetHandler(si->Type)->Close(si);
bediyap 0:f6f434d9a03a 111 }
bediyap 0:f6f434d9a03a 112 };
bediyap 0:f6f434d9a03a 113
bediyap 0:f6f434d9a03a 114 SocketManager gSocketManager;
bediyap 0:f6f434d9a03a 115
bediyap 0:f6f434d9a03a 116 int Socket_Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData)
bediyap 0:f6f434d9a03a 117 {
bediyap 0:f6f434d9a03a 118 return gSocketManager.Open(type,addr,callback,userData);
bediyap 0:f6f434d9a03a 119 }
bediyap 0:f6f434d9a03a 120
bediyap 0:f6f434d9a03a 121 int Socket_Send(int socket, const u8* data, int len)
bediyap 0:f6f434d9a03a 122 {
bediyap 0:f6f434d9a03a 123 return gSocketManager.Send(socket,data,len);
bediyap 0:f6f434d9a03a 124 }
bediyap 0:f6f434d9a03a 125
bediyap 0:f6f434d9a03a 126 int Socket_Close(int socket)
bediyap 0:f6f434d9a03a 127 {
bediyap 0:f6f434d9a03a 128 return gSocketManager.Close(socket);
bediyap 0:f6f434d9a03a 129 }
bediyap 0:f6f434d9a03a 130
bediyap 0:f6f434d9a03a 131 int RegisterSocketHandler(int type, SocketHandler* handler)
bediyap 0:f6f434d9a03a 132 {
bediyap 0:f6f434d9a03a 133 return gSocketManager.RegisterSocketHandler(type,handler);
bediyap 0:f6f434d9a03a 134 }
bediyap 0:f6f434d9a03a 135
bediyap 0:f6f434d9a03a 136 SocketInternal* GetSocketInternal(int socket)
bediyap 0:f6f434d9a03a 137 {
bediyap 0:f6f434d9a03a 138 return gSocketManager.GetInternal(socket);
bediyap 0:f6f434d9a03a 139 }
bediyap 0:f6f434d9a03a 140