Modified for W5500 Ethernet initialize

Fork of GMMP_mbed by SKTelecom_ThingPlug

Committer:
hkjung
Date:
Wed Aug 12 02:13:34 2015 +0000
Revision:
3:6b4536e1962f
Parent:
0:7e575e5f88ec
Modified for W5500 Ethernet initialize

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 {
lesmin 0:7e575e5f88ec 19 if (g_socket <= 0) return;
lesmin 0:7e575e5f88ec 20
lesmin 0:7e575e5f88ec 21 g_socket = -1;
lesmin 0:7e575e5f88ec 22
lesmin 0:7e575e5f88ec 23 return;
lesmin 0:7e575e5f88ec 24 }
lesmin 0:7e575e5f88ec 25
lesmin 0:7e575e5f88ec 26 int CheckSocket()
lesmin 0:7e575e5f88ec 27 {
lesmin 0:7e575e5f88ec 28 return 0;
lesmin 0:7e575e5f88ec 29 }
lesmin 0:7e575e5f88ec 30
lesmin 0:7e575e5f88ec 31 int Connect()
hkjung 3:6b4536e1962f 32 {
hkjung 3:6b4536e1962f 33 int ret = GMMP_SUCCESS;
lesmin 0:7e575e5f88ec 34
lesmin 0:7e575e5f88ec 35 //delay(2000);
lesmin 0:7e575e5f88ec 36 infoln("Connecting...");
lesmin 0:7e575e5f88ec 37 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 38
lesmin 0:7e575e5f88ec 39 char szServerIp[24];
lesmin 0:7e575e5f88ec 40 sprintf(szServerIp, "%d.%d.%d.%d", g_serverIp[0], g_serverIp[1], g_serverIp[2], g_serverIp[3]);
lesmin 0:7e575e5f88ec 41
lesmin 0:7e575e5f88ec 42 if(g_pClient == NULL) {
lesmin 0:7e575e5f88ec 43 ERR("pClient is NULL!!!");
lesmin 0:7e575e5f88ec 44 return LIB_PARAM_ERROR;
lesmin 0:7e575e5f88ec 45 }
lesmin 0:7e575e5f88ec 46
lesmin 0:7e575e5f88ec 47 // if (client.connect((char *)szServerIp, g_nServerPort)) {
hkjung 3:6b4536e1962f 48 /*
hkjung 3:6b4536e1962f 49 if (g_pClient->connect((char *)szServerIp, g_nServerPort)) {
hkjung 3:6b4536e1962f 50 infoln("Connected!!");
hkjung 3:6b4536e1962f 51 } else {
hkjung 3:6b4536e1962f 52 infoln("connection failed!");
hkjung 3:6b4536e1962f 53 ret = SERVER_CONNECT_ERROR;
hkjung 3:6b4536e1962f 54 }
hkjung 3:6b4536e1962f 55 */
hkjung 3:6b4536e1962f 56 int retry = 30;
hkjung 3:6b4536e1962f 57 do
hkjung 3:6b4536e1962f 58 {
hkjung 3:6b4536e1962f 59 if(retry <= 0) {
hkjung 3:6b4536e1962f 60 ret = SERVER_CONNECT_ERROR;
hkjung 3:6b4536e1962f 61 break;
hkjung 3:6b4536e1962f 62 }
hkjung 3:6b4536e1962f 63
hkjung 3:6b4536e1962f 64 infoln("Trying to connect to Server...");
hkjung 3:6b4536e1962f 65 if (g_pClient->connect((char *)szServerIp, g_nServerPort)) {
hkjung 3:6b4536e1962f 66 infoln("Connected!");
hkjung 3:6b4536e1962f 67 break;
hkjung 3:6b4536e1962f 68 }
hkjung 3:6b4536e1962f 69
hkjung 3:6b4536e1962f 70 infoln("Connection failed!");
hkjung 3:6b4536e1962f 71 } while(retry--);
lesmin 0:7e575e5f88ec 72
lesmin 0:7e575e5f88ec 73 return ret;
lesmin 0:7e575e5f88ec 74 }
lesmin 0:7e575e5f88ec 75
lesmin 0:7e575e5f88ec 76 int WriteTCP(char* pBuf, int nLen)
lesmin 0:7e575e5f88ec 77 {
lesmin 0:7e575e5f88ec 78 DBG("WriteTCP(): %d", nLen);
lesmin 0:7e575e5f88ec 79
lesmin 0:7e575e5f88ec 80 if (pBuf == NULL || nLen <= 0) return LIB_PARAM_ERROR;
lesmin 0:7e575e5f88ec 81
lesmin 0:7e575e5f88ec 82 if(g_pClient == NULL) {
lesmin 0:7e575e5f88ec 83 ERR("pClient is NULL!!!");
lesmin 0:7e575e5f88ec 84 return LIB_PARAM_ERROR;
lesmin 0:7e575e5f88ec 85 }
lesmin 0:7e575e5f88ec 86 // client.write(pBuf, nLen);
hkjung 3:6b4536e1962f 87 g_pClient->write(pBuf, nLen);
lesmin 0:7e575e5f88ec 88
lesmin 0:7e575e5f88ec 89 return GMMP_SUCCESS;
lesmin 0:7e575e5f88ec 90 }
lesmin 0:7e575e5f88ec 91
lesmin 0:7e575e5f88ec 92 int ReadTCP(char* pBuf, const int nMaxlen)
lesmin 0:7e575e5f88ec 93 {
lesmin 0:7e575e5f88ec 94 return ReadTCP2(pBuf, nMaxlen, 1);
lesmin 0:7e575e5f88ec 95 }
lesmin 0:7e575e5f88ec 96
lesmin 0:7e575e5f88ec 97 int ReadTCPAsync(char* pBuf, const int nMaxlen);
lesmin 0:7e575e5f88ec 98
lesmin 0:7e575e5f88ec 99 int ReadTCP2(char* pBuf, const int nMaxlen, byte blocking)
lesmin 0:7e575e5f88ec 100 {
lesmin 0:7e575e5f88ec 101 //blocking = 0;
lesmin 0:7e575e5f88ec 102
lesmin 0:7e575e5f88ec 103 if (!blocking) {
lesmin 0:7e575e5f88ec 104 return ReadTCPAsync(pBuf, nMaxlen);
lesmin 0:7e575e5f88ec 105 }
lesmin 0:7e575e5f88ec 106
lesmin 0:7e575e5f88ec 107 if (pBuf == NULL || nMaxlen <= 0) return LIB_PARAM_ERROR;
lesmin 0:7e575e5f88ec 108
lesmin 0:7e575e5f88ec 109 if(g_pClient == NULL) {
lesmin 0:7e575e5f88ec 110 ERR("pClient is NULL!!!");
lesmin 0:7e575e5f88ec 111 return LIB_PARAM_ERROR;
lesmin 0:7e575e5f88ec 112 }
lesmin 0:7e575e5f88ec 113
lesmin 0:7e575e5f88ec 114 int idx = 0;
lesmin 0:7e575e5f88ec 115 memset(pBuf, 0, nMaxlen);
lesmin 0:7e575e5f88ec 116
lesmin 0:7e575e5f88ec 117 while(idx < nMaxlen)
lesmin 0:7e575e5f88ec 118 {
lesmin 0:7e575e5f88ec 119 // if (client.available()) {
lesmin 0:7e575e5f88ec 120 if (g_pClient->available()) {
lesmin 0:7e575e5f88ec 121 // pBuf[idx] = client.read();
lesmin 0:7e575e5f88ec 122 pBuf[idx] = g_pClient->read();
lesmin 0:7e575e5f88ec 123 DBG("%d", idx);
lesmin 0:7e575e5f88ec 124 DBG(" %c",(byte)pBuf[idx]);
lesmin 0:7e575e5f88ec 125 idx++;
lesmin 0:7e575e5f88ec 126 } else {
lesmin 0:7e575e5f88ec 127 //debugln("E_WOULDBLOCK");
lesmin 0:7e575e5f88ec 128 return E_WOULDBLOCK;
lesmin 0:7e575e5f88ec 129 }
lesmin 0:7e575e5f88ec 130 }
lesmin 0:7e575e5f88ec 131
lesmin 0:7e575e5f88ec 132 return GMMP_SUCCESS;
lesmin 0:7e575e5f88ec 133 }
lesmin 0:7e575e5f88ec 134
lesmin 0:7e575e5f88ec 135 int ReadTCPAsync(char* pBuf, const int nMaxlen)
lesmin 0:7e575e5f88ec 136 {
lesmin 0:7e575e5f88ec 137 if (pBuf == NULL || nMaxlen <= 0) return LIB_PARAM_ERROR;
lesmin 0:7e575e5f88ec 138
lesmin 0:7e575e5f88ec 139 if(g_pClient == NULL) {
lesmin 0:7e575e5f88ec 140 ERR("pClient is NULL!!!");
lesmin 0:7e575e5f88ec 141 return LIB_PARAM_ERROR;
lesmin 0:7e575e5f88ec 142 }
lesmin 0:7e575e5f88ec 143
lesmin 0:7e575e5f88ec 144 int idx = 0;
lesmin 0:7e575e5f88ec 145 char val = -1;
lesmin 0:7e575e5f88ec 146
lesmin 0:7e575e5f88ec 147 memset(pBuf, 0, nMaxlen);
lesmin 0:7e575e5f88ec 148
lesmin 0:7e575e5f88ec 149 while(idx < nMaxlen)
lesmin 0:7e575e5f88ec 150 {
lesmin 0:7e575e5f88ec 151 // val = client.read();
lesmin 0:7e575e5f88ec 152 val = g_pClient->read();
lesmin 0:7e575e5f88ec 153
lesmin 0:7e575e5f88ec 154 if (val != -1) {
lesmin 0:7e575e5f88ec 155 pBuf[idx++] = (byte)val;
lesmin 0:7e575e5f88ec 156
lesmin 0:7e575e5f88ec 157 /*
lesmin 0:7e575e5f88ec 158 pBuf[idx] = (byte)val;
lesmin 0:7e575e5f88ec 159 debug(idx);
lesmin 0:7e575e5f88ec 160 debug(" ");
lesmin 0:7e575e5f88ec 161 debugln((byte)pBuf[idx]);
lesmin 0:7e575e5f88ec 162 idx++;
lesmin 0:7e575e5f88ec 163 */
lesmin 0:7e575e5f88ec 164 } else {
lesmin 0:7e575e5f88ec 165 return E_WOULDBLOCK;
lesmin 0:7e575e5f88ec 166 }
lesmin 0:7e575e5f88ec 167 }
lesmin 0:7e575e5f88ec 168
lesmin 0:7e575e5f88ec 169 return GMMP_SUCCESS;
lesmin 0:7e575e5f88ec 170 }