Simon Ford / Mbed 2 deprecated WiiRacing

Dependencies:   mbed m3pi ID12RFIDIRQ

Committer:
simon
Date:
Mon Apr 11 23:03:22 2011 +0000
Revision:
0:e2ef12467862

        

Who changed what in which revision?

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