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