Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of 2016_Spring_kaida by
Diff: Socket.cpp
- Revision:
- 15:5e973e6c058c
- Parent:
- 0:736c76a75def
--- a/Socket.cpp Mon Jun 06 12:22:38 2016 +0000
+++ b/Socket.cpp Thu Jun 09 02:53:48 2016 +0000
@@ -20,7 +20,7 @@
THE SOFTWARE.
*/
-/*
+/*
Tue Apr 26 2011 Bart Janssens: added a socket listener
*/
@@ -38,13 +38,13 @@
class SocketInternalPad
{
- public:
+public:
SocketInternal si;
u8 pad[8];
};
-
+
class SocketManager
{
@@ -52,47 +52,40 @@
SocketInternalPad _sockets[MAX_SOCKETS];
SocketListener _listeners[MAX_LISTEN];
- public:
- SocketManager()
- {
+public:
+ SocketManager() {
memset(_handlers,0,sizeof(_handlers));
memset(_sockets,0,sizeof(_sockets));
memset(_listeners,0,sizeof(_listeners));
}
- SocketHandler* GetHandler(int type)
- {
+ SocketHandler* GetHandler(int type) {
if (type < 1 || type > MAX_SOCKET_HANDLERS)
return 0;
return _handlers[type - 1];
}
- SocketInternal* GetInternal(int s)
- {
+ SocketInternal* GetInternal(int s) {
if (s < 1 || s > MAX_SOCKETS)
return 0;
return &_sockets[s - 1].si;
}
- int RegisterSocketHandler(int type, SocketHandler* handler)
- {
- if (type < 1 || type > MAX_SOCKET_HANDLERS)
+ int RegisterSocketHandler(int type, SocketHandler* handler) {
+ if (type < 1 || type > MAX_SOCKET_HANDLERS)
return ERR_SOCKET_TYPE_NOT_FOUND;
_handlers[type - 1] = handler;
return 0;
}
- int Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData)
- {
+ int Open(int type, SocketAddrHdr* addr, SocketCallback callback, void* userData) {
SocketHandler* h = GetHandler(type);
if (!h)
return ERR_SOCKET_TYPE_NOT_FOUND;
-
- for (int i = 0; i < MAX_SOCKETS; i++)
- {
+
+ for (int i = 0; i < MAX_SOCKETS; i++) {
SocketInternal* si = (SocketInternal*)(_sockets+i);
- if (si->ID == 0)
- {
+ if (si->ID == 0) {
//printf("Call to Socket Manager Open \r\n");
si->ID = i+1;
si->Type = type;
@@ -103,25 +96,22 @@
}
return ERR_SOCKET_NONE_LEFT;
}
-
- SocketInternal* Create(int type, SocketAddrHdr* addr, int port)
- {
+
+ SocketInternal* Create(int type, SocketAddrHdr* addr, int port) {
SocketInternal* si;
SocketListener* li;
SocketHandler* h = GetHandler(type);
if (!h)
return 0;
-
- for (int i = 0; i < MAX_SOCKETS; i++)
- {
+
+ for (int i = 0; i < MAX_SOCKETS; i++) {
si = (SocketInternal*)(_sockets+i);
- if (si->ID == 0)
- {
+ if (si->ID == 0) {
si->ID = i+1;
si->State = SocketState_Listen;
si->Type = type;
si->port = port;
- for (int i = 0; i < MAX_LISTEN; i++){
+ for (int i = 0; i < MAX_LISTEN; i++) {
li = (SocketListener*)(_listeners+i);
if (( li->Type == si->Type )&& (li->port == si->port)) {
si->Callback = li->Callback;
@@ -131,30 +121,27 @@
}
}
- }
+ }
}
}
-
-
- int Listen(int type, int port, SocketCallback callback,void* userData)
- {
+
+
+ int Listen(int type, int port, SocketCallback callback,void* userData) {
SocketListener* li;
SocketHandler* h = GetHandler(type);
if (!h) return ERR_SOCKET_TYPE_NOT_FOUND;
-
+
//printf("Call to Socket Manager Listen \r\n");
- for (int i = 0; i < MAX_LISTEN; i++)
- {
+ for (int i = 0; i < MAX_LISTEN; i++) {
li = (SocketListener*)(_listeners+i);
if (( li->Type == type )&& (li->port == port)) {
- //printf("Port %d is already in use\r\n",port);
- return ERR_SOCKET_IN_USE; //in use
+ //printf("Port %d is already in use\r\n",port);
+ return ERR_SOCKET_IN_USE; //in use
}
}
-
- for (int i = 0; i < MAX_LISTEN; i++)
- {
+
+ for (int i = 0; i < MAX_LISTEN; i++) {
li = (SocketListener*)(_listeners+i);
if (( li->Type == 0 )&& (li->port == 0)) {
li->ID = i+1;
@@ -167,16 +154,14 @@
}
}
//printf("Max listen ports reached\r\n",port);
- return ERR_SOCKET_NONE_LEFT;
+ return ERR_SOCKET_NONE_LEFT;
}
-
- int InUse(int type, int port)
- {
+
+ int InUse(int type, int port) {
SocketListener* li;
SocketHandler* h = GetHandler(type);
if (!h) return ERR_SOCKET_TYPE_NOT_FOUND;
- for (int i = 0; i < MAX_LISTEN; i++)
- {
+ for (int i = 0; i < MAX_LISTEN; i++) {
li = (SocketListener*)(_listeners+i);
if (( li->Type == type )&& (li->port == port)) {
@@ -185,46 +170,43 @@
}
}
//printf("Listen check on port %d NOK\r\n",port);
- return ERR_SOCKET_NONE_LEFT;
- }
-
+ return ERR_SOCKET_NONE_LEFT;
+ }
- int Accept(int socket, SocketCallback callback, void* userData)
- {
+
+ int Accept(int socket, SocketCallback callback, void* userData) {
SocketInternal* si = GetInternal(socket);
if (!si || si->ID != socket)
return ERR_SOCKET_NOT_FOUND;
-
+
si->Callback = callback;
si->userData = userData;
-
- //printf("Call to Socket Manager Accept \r\n");
+
+ //printf("Call to Socket Manager Accept \r\n");
return 0;
-
+
}
- int Send(int socket, const u8* data, int len)
- {
- //printf("Call to Socket Manager Send \r\n");
+ int Send(int socket, const u8* data, int len) {
+ //printf("Call to Socket Manager Send \r\n");
SocketInternal* si = GetInternal(socket);
- //printf("socket = %d si->ID = %d si->Type = %d \r\n", socket, si->ID, si->Type);
- if (!si || si->ID != socket){
+ //printf("socket = %d si->ID = %d si->Type = %d \r\n", socket, si->ID, si->Type);
+ if (!si || si->ID != socket) {
//printf("send: socket not found \r\n");
return ERR_SOCKET_NOT_FOUND;
}
//printf("Calling l2cap send \r\n");
-
+
SocketHandler* h = GetHandler(si->Type);
if (!h) {
- //printf("Send: no socket type found \r\n");
- return ERR_SOCKET_TYPE_NOT_FOUND;
+ //printf("Send: no socket type found \r\n");
+ return ERR_SOCKET_TYPE_NOT_FOUND;
}
return h->Send(si,data,len);
-
+
}
- int Close(int socket)
- {
+ int Close(int socket) {
SocketInternal* si = GetInternal(socket);
if (!si || si->ID != socket)
return ERR_SOCKET_NOT_FOUND;
