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