BlackOneとAndroidの連携デモプログラム AndroidAccessoryを改造してBlackOneとAndroidが連携できるようにしました。 サポートしているのは、デモアプリの ”Buttons” B1-SW1, B2-SW2, B3-SW3 ”LED2” RGB-LED のみです。 LCDに表示するイメージをマイクロSDカードに入れてLCDのソケットに挿入しておく必要があります。 イメージは、320X240ドットで”\Image”という名前のフォルダの直下に”10.jpg”という名前で入れてください。

Dependencies:   TextLCD mbed

Committer:
techand
Date:
Fri Dec 23 04:33:33 2011 +0000
Revision:
0:7b556109fd46

        

Who changed what in which revision?

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