Modified for W5500 Ethernet initialize Added the Ethernet interface re-initialize code Added the connection retry

Dependents:   ThingPlug_Ethernet_Example

Fork of GMMP_mbed by Eric Jung

Committer:
hkjung
Date:
Wed Aug 12 08:58:58 2015 +0000
Revision:
5:9bbb6933559b
Parent:
4:1e04850ce835
Ethernet interface reinitialize code added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lesmin 0:7e575e5f88ec 1 /** TCP Socket 통신을 위한 모듈
lesmin 0:7e575e5f88ec 2 * @file Network.cpp
lesmin 0:7e575e5f88ec 3 * @date 2015/07/20
lesmin 0:7e575e5f88ec 4 * @version 0.0.1.0
lesmin 0:7e575e5f88ec 5 **/
lesmin 0:7e575e5f88ec 6
lesmin 0:7e575e5f88ec 7 #include "Client.h"
lesmin 0:7e575e5f88ec 8
lesmin 0:7e575e5f88ec 9 #include "Network.h"
lesmin 0:7e575e5f88ec 10 #include "GMMP_Operation.h"
lesmin 0:7e575e5f88ec 11
lesmin 0:7e575e5f88ec 12 int g_socket = -1;
lesmin 0:7e575e5f88ec 13
lesmin 0:7e575e5f88ec 14 //Client client;
lesmin 0:7e575e5f88ec 15 extern Client *g_pClient; //lesmin
lesmin 0:7e575e5f88ec 16
lesmin 0:7e575e5f88ec 17 void CloseSocket()
lesmin 0:7e575e5f88ec 18 {
hkjung 4:1e04850ce835 19 if (g_socket <= 0) return;
lesmin 0:7e575e5f88ec 20 g_socket = -1;
lesmin 0:7e575e5f88ec 21
lesmin 0:7e575e5f88ec 22 return;
lesmin 0:7e575e5f88ec 23 }
lesmin 0:7e575e5f88ec 24
lesmin 0:7e575e5f88ec 25 int CheckSocket()
lesmin 0:7e575e5f88ec 26 {
lesmin 0:7e575e5f88ec 27 return 0;
lesmin 0:7e575e5f88ec 28 }
lesmin 0:7e575e5f88ec 29
lesmin 0:7e575e5f88ec 30 int Connect()
hkjung 3:6b4536e1962f 31 {
hkjung 3:6b4536e1962f 32 int ret = GMMP_SUCCESS;
lesmin 0:7e575e5f88ec 33
lesmin 0:7e575e5f88ec 34 //delay(2000);
lesmin 0:7e575e5f88ec 35 infoln("Connecting...");
lesmin 0:7e575e5f88ec 36 INFO("server ip: %d.%d.%d.%d:%d", g_serverIp[0], g_serverIp[1], g_serverIp[2], g_serverIp[3], g_nServerPort);
lesmin 0:7e575e5f88ec 37
lesmin 0:7e575e5f88ec 38 char szServerIp[24];
lesmin 0:7e575e5f88ec 39 sprintf(szServerIp, "%d.%d.%d.%d", g_serverIp[0], g_serverIp[1], g_serverIp[2], g_serverIp[3]);
lesmin 0:7e575e5f88ec 40
lesmin 0:7e575e5f88ec 41 if(g_pClient == NULL) {
lesmin 0:7e575e5f88ec 42 ERR("pClient is NULL!!!");
lesmin 0:7e575e5f88ec 43 return LIB_PARAM_ERROR;
lesmin 0:7e575e5f88ec 44 }
hkjung 4:1e04850ce835 45
lesmin 0:7e575e5f88ec 46 // if (client.connect((char *)szServerIp, g_nServerPort)) {
hkjung 3:6b4536e1962f 47 /*
hkjung 3:6b4536e1962f 48 if (g_pClient->connect((char *)szServerIp, g_nServerPort)) {
hkjung 3:6b4536e1962f 49 infoln("Connected!!");
hkjung 3:6b4536e1962f 50 } else {
hkjung 3:6b4536e1962f 51 infoln("connection failed!");
hkjung 3:6b4536e1962f 52 ret = SERVER_CONNECT_ERROR;
hkjung 3:6b4536e1962f 53 }
hkjung 3:6b4536e1962f 54 */
hkjung 3:6b4536e1962f 55 int retry = 30;
hkjung 3:6b4536e1962f 56 do
hkjung 3:6b4536e1962f 57 {
hkjung 3:6b4536e1962f 58 if(retry <= 0) {
hkjung 3:6b4536e1962f 59 ret = SERVER_CONNECT_ERROR;
hkjung 3:6b4536e1962f 60 break;
hkjung 3:6b4536e1962f 61 }
hkjung 3:6b4536e1962f 62
hkjung 3:6b4536e1962f 63 infoln("Trying to connect to Server...");
hkjung 3:6b4536e1962f 64 if (g_pClient->connect((char *)szServerIp, g_nServerPort)) {
hkjung 3:6b4536e1962f 65 infoln("Connected!");
hkjung 3:6b4536e1962f 66 break;
hkjung 3:6b4536e1962f 67 }
hkjung 3:6b4536e1962f 68
hkjung 3:6b4536e1962f 69 infoln("Connection failed!");
hkjung 3:6b4536e1962f 70 } while(retry--);
lesmin 0:7e575e5f88ec 71
hkjung 4:1e04850ce835 72 // ## 20150812 debug
hkjung 4:1e04850ce835 73 //if(g_pClient->connected())
hkjung 4:1e04850ce835 74 //{
hkjung 4:1e04850ce835 75 // printf("Debug >> Send Hello\r\n");
hkjung 4:1e04850ce835 76 // g_pClient->write("hello", 6); // data sending success
hkjung 4:1e04850ce835 77 //}
hkjung 4:1e04850ce835 78
lesmin 0:7e575e5f88ec 79 return ret;
lesmin 0:7e575e5f88ec 80 }
lesmin 0:7e575e5f88ec 81
lesmin 0:7e575e5f88ec 82 int WriteTCP(char* pBuf, int nLen)
hkjung 4:1e04850ce835 83 {
lesmin 0:7e575e5f88ec 84 DBG("WriteTCP(): %d", nLen);
hkjung 4:1e04850ce835 85 INFO("Connection Status = %s", g_pClient->connected() ? "Connected":"Disconnected");
lesmin 0:7e575e5f88ec 86
lesmin 0:7e575e5f88ec 87 if (pBuf == NULL || nLen <= 0) return LIB_PARAM_ERROR;
lesmin 0:7e575e5f88ec 88
lesmin 0:7e575e5f88ec 89 if(g_pClient == NULL) {
lesmin 0:7e575e5f88ec 90 ERR("pClient is NULL!!!");
lesmin 0:7e575e5f88ec 91 return LIB_PARAM_ERROR;
lesmin 0:7e575e5f88ec 92 }
lesmin 0:7e575e5f88ec 93 // client.write(pBuf, nLen);
hkjung 3:6b4536e1962f 94 g_pClient->write(pBuf, nLen);
lesmin 0:7e575e5f88ec 95
lesmin 0:7e575e5f88ec 96 return GMMP_SUCCESS;
lesmin 0:7e575e5f88ec 97 }
lesmin 0:7e575e5f88ec 98
lesmin 0:7e575e5f88ec 99 int ReadTCP(char* pBuf, const int nMaxlen)
lesmin 0:7e575e5f88ec 100 {
lesmin 0:7e575e5f88ec 101 return ReadTCP2(pBuf, nMaxlen, 1);
lesmin 0:7e575e5f88ec 102 }
lesmin 0:7e575e5f88ec 103
lesmin 0:7e575e5f88ec 104 int ReadTCPAsync(char* pBuf, const int nMaxlen);
lesmin 0:7e575e5f88ec 105
lesmin 0:7e575e5f88ec 106 int ReadTCP2(char* pBuf, const int nMaxlen, byte blocking)
lesmin 0:7e575e5f88ec 107 {
lesmin 0:7e575e5f88ec 108 //blocking = 0;
lesmin 0:7e575e5f88ec 109
lesmin 0:7e575e5f88ec 110 if (!blocking) {
lesmin 0:7e575e5f88ec 111 return ReadTCPAsync(pBuf, nMaxlen);
lesmin 0:7e575e5f88ec 112 }
lesmin 0:7e575e5f88ec 113
lesmin 0:7e575e5f88ec 114 if (pBuf == NULL || nMaxlen <= 0) return LIB_PARAM_ERROR;
lesmin 0:7e575e5f88ec 115
lesmin 0:7e575e5f88ec 116 if(g_pClient == NULL) {
lesmin 0:7e575e5f88ec 117 ERR("pClient is NULL!!!");
lesmin 0:7e575e5f88ec 118 return LIB_PARAM_ERROR;
lesmin 0:7e575e5f88ec 119 }
lesmin 0:7e575e5f88ec 120
lesmin 0:7e575e5f88ec 121 int idx = 0;
lesmin 0:7e575e5f88ec 122 memset(pBuf, 0, nMaxlen);
lesmin 0:7e575e5f88ec 123
lesmin 0:7e575e5f88ec 124 while(idx < nMaxlen)
lesmin 0:7e575e5f88ec 125 {
lesmin 0:7e575e5f88ec 126 // if (client.available()) {
lesmin 0:7e575e5f88ec 127 if (g_pClient->available()) {
lesmin 0:7e575e5f88ec 128 // pBuf[idx] = client.read();
lesmin 0:7e575e5f88ec 129 pBuf[idx] = g_pClient->read();
lesmin 0:7e575e5f88ec 130 DBG("%d", idx);
lesmin 0:7e575e5f88ec 131 DBG(" %c",(byte)pBuf[idx]);
lesmin 0:7e575e5f88ec 132 idx++;
lesmin 0:7e575e5f88ec 133 } else {
lesmin 0:7e575e5f88ec 134 //debugln("E_WOULDBLOCK");
lesmin 0:7e575e5f88ec 135 return E_WOULDBLOCK;
lesmin 0:7e575e5f88ec 136 }
lesmin 0:7e575e5f88ec 137 }
lesmin 0:7e575e5f88ec 138
lesmin 0:7e575e5f88ec 139 return GMMP_SUCCESS;
lesmin 0:7e575e5f88ec 140 }
lesmin 0:7e575e5f88ec 141
lesmin 0:7e575e5f88ec 142 int ReadTCPAsync(char* pBuf, const int nMaxlen)
lesmin 0:7e575e5f88ec 143 {
lesmin 0:7e575e5f88ec 144 if (pBuf == NULL || nMaxlen <= 0) return LIB_PARAM_ERROR;
lesmin 0:7e575e5f88ec 145
lesmin 0:7e575e5f88ec 146 if(g_pClient == NULL) {
lesmin 0:7e575e5f88ec 147 ERR("pClient is NULL!!!");
lesmin 0:7e575e5f88ec 148 return LIB_PARAM_ERROR;
lesmin 0:7e575e5f88ec 149 }
lesmin 0:7e575e5f88ec 150
lesmin 0:7e575e5f88ec 151 int idx = 0;
lesmin 0:7e575e5f88ec 152 char val = -1;
lesmin 0:7e575e5f88ec 153
lesmin 0:7e575e5f88ec 154 memset(pBuf, 0, nMaxlen);
lesmin 0:7e575e5f88ec 155
lesmin 0:7e575e5f88ec 156 while(idx < nMaxlen)
lesmin 0:7e575e5f88ec 157 {
lesmin 0:7e575e5f88ec 158 // val = client.read();
lesmin 0:7e575e5f88ec 159 val = g_pClient->read();
lesmin 0:7e575e5f88ec 160
lesmin 0:7e575e5f88ec 161 if (val != -1) {
lesmin 0:7e575e5f88ec 162 pBuf[idx++] = (byte)val;
lesmin 0:7e575e5f88ec 163
lesmin 0:7e575e5f88ec 164 /*
lesmin 0:7e575e5f88ec 165 pBuf[idx] = (byte)val;
lesmin 0:7e575e5f88ec 166 debug(idx);
lesmin 0:7e575e5f88ec 167 debug(" ");
lesmin 0:7e575e5f88ec 168 debugln((byte)pBuf[idx]);
lesmin 0:7e575e5f88ec 169 idx++;
lesmin 0:7e575e5f88ec 170 */
lesmin 0:7e575e5f88ec 171 } else {
lesmin 0:7e575e5f88ec 172 return E_WOULDBLOCK;
lesmin 0:7e575e5f88ec 173 }
lesmin 0:7e575e5f88ec 174 }
lesmin 0:7e575e5f88ec 175
lesmin 0:7e575e5f88ec 176 return GMMP_SUCCESS;
lesmin 0:7e575e5f88ec 177 }