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 /** GMMP 공통 정의
lesmin 0:7e575e5f88ec 2 * @file GMMP.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 #ifndef GMMP_h
lesmin 0:7e575e5f88ec 8 #define GMMP_h
lesmin 0:7e575e5f88ec 9
lesmin 0:7e575e5f88ec 10 /*
lesmin 0:7e575e5f88ec 11 #include <stdio.h>
lesmin 0:7e575e5f88ec 12 #include <stdlib.h>
lesmin 0:7e575e5f88ec 13 #include <string.h>
lesmin 0:7e575e5f88ec 14 #include <time.h>
lesmin 0:7e575e5f88ec 15 #include <pthread.h>
lesmin 0:7e575e5f88ec 16 #include <unistd.h>
lesmin 0:7e575e5f88ec 17 #include <stdarg.h>
lesmin 0:7e575e5f88ec 18 */
lesmin 0:7e575e5f88ec 19
lesmin 0:7e575e5f88ec 20 //#include <inttypes.h>
lesmin 0:7e575e5f88ec 21 //#include <Time.h>
lesmin 0:7e575e5f88ec 22 //#include <Arduino.h>
lesmin 0:7e575e5f88ec 23
lesmin 0:7e575e5f88ec 24 #include "config.h"
lesmin 0:7e575e5f88ec 25
lesmin 1:29d7f5c53126 26 #include "Client.h"
lesmin 0:7e575e5f88ec 27
lesmin 0:7e575e5f88ec 28 /** moved to config.h
lesmin 0:7e575e5f88ec 29 //#define USE_SNIC_WIFI //Murata Type YD Wi-Fi
lesmin 0:7e575e5f88ec 30 //#define USE_WIZNET_W5500
lesmin 0:7e575e5f88ec 31 **/
lesmin 0:7e575e5f88ec 32
hkjung 3:6b4536e1962f 33 // Added the code for LPC1768 platform
hkjung 3:6b4536e1962f 34 #if defined(TARGET_LPC1768) || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_L152RE)
lesmin 0:7e575e5f88ec 35
lesmin 0:7e575e5f88ec 36 #ifdef USE_SNIC_WIFI
lesmin 0:7e575e5f88ec 37 #include "SNIC_WifiInterface.h"
lesmin 0:7e575e5f88ec 38 C_SNIC_WifiInterface wifi( D8, D2, NC, NC, D3);
lesmin 0:7e575e5f88ec 39
lesmin 0:7e575e5f88ec 40 /** moved to config.h
lesmin 0:7e575e5f88ec 41 // #define MBED_AP_SSID "FON"//"TIDE867"//"tide855"
lesmin 0:7e575e5f88ec 42 // /** Securiry Options
lesmin 0:7e575e5f88ec 43 // e_SEC_OPEN = 0x00, //Open
lesmin 0:7e575e5f88ec 44 // e_SEC_WEP = 0x01, // WEP
lesmin 0:7e575e5f88ec 45 // e_SEC_WPA_TKIP = 0x02, // WPA-PSK(TKIP)
lesmin 0:7e575e5f88ec 46 // e_SEC_WPA2_AES = 0x04, // WPA2-PSK(AES)
lesmin 0:7e575e5f88ec 47 // e_SEC_WPA2_MIXED = 0x06, // WPA2-PSK(TKIP/AES)
lesmin 0:7e575e5f88ec 48 // e_SEC_WPA_AES = 0x07 // WPA-PSK(AES) **/
lesmin 0:7e575e5f88ec 49 // #define MBED_AP_SECURITY_TYPE e_SEC_OPEN//e_SEC_WPA2_AES//e_SEC_OPEN
lesmin 0:7e575e5f88ec 50 // #define MBED_AP_SECUTIRY_KEY ""//"tidetide"
lesmin 0:7e575e5f88ec 51 // **/
lesmin 0:7e575e5f88ec 52
lesmin 0:7e575e5f88ec 53 #endif//USE_SNIC_WIFI
lesmin 0:7e575e5f88ec 54
lesmin 0:7e575e5f88ec 55 #ifdef USE_WIZNET_W5500
hkjung 4:1e04850ce835 56 #include "WIZnetInterface.h"
hkjung 5:9bbb6933559b 57 //#include "EthernetInterface.h" // WIZnet Ethernet library replacement needed (W5100/W5200/W5500 -> W5500/W7500)
hkjung 4:1e04850ce835 58
hkjung 4:1e04850ce835 59 #if defined(TARGET_LPC1768)
hkjung 4:1e04850ce835 60 SPI spi(p5, p6, p7); // mosi, miso, sclk
hkjung 4:1e04850ce835 61 WIZnetInterface ethernet(&spi, p8, p11);
hkjung 5:9bbb6933559b 62 #elif defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_L152RE)
hkjung 4:1e04850ce835 63 SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk
hkjung 5:9bbb6933559b 64 WIZnetInterface ethernet(&spi, PB_6, PA_9);//scs(PB_6), nRESET(PA_9); // reset pin is dummy, don't affect any pin of WIZ550io
hkjung 4:1e04850ce835 65 #else
hkjung 4:1e04850ce835 66 // Ethernet interface initialization code for additional platforms will be added later.
hkjung 4:1e04850ce835 67 #endif
lesmin 0:7e575e5f88ec 68 #endif//USE_WIZNET_W5500
hkjung 5:9bbb6933559b 69 #elif defined(TARGET_WIZwiki_W7500) // WIZnet Ethernet library replacement needed (W5100/W5200/W5500 -> W5500/W7500)
hkjung 4:1e04850ce835 70 #include "EthernetInterface.h"
hkjung 4:1e04850ce835 71 EthernetInterface ethernet;
lesmin 0:7e575e5f88ec 72 #endif
lesmin 0:7e575e5f88ec 73
lesmin 0:7e575e5f88ec 74 #include "NTPClient.h"
lesmin 0:7e575e5f88ec 75
lesmin 0:7e575e5f88ec 76 #include "GMMP.h"
lesmin 0:7e575e5f88ec 77
lesmin 0:7e575e5f88ec 78 byte g_serverIp[LEN_IP] ; ///< 서버 IP 정보를 저장한다.
lesmin 0:7e575e5f88ec 79 int g_nServerPort = 0; ///< 서버 Port 정보를 저장한다.
lesmin 0:7e575e5f88ec 80
lesmin 0:7e575e5f88ec 81 char g_szAuthID[LEN_AUTH_ID];///< OMP Portal을 통해 사전에 등록된 M2M GW 의 Serial Number 저장 변수, 자동화를 위해 사용된다.
lesmin 0:7e575e5f88ec 82
lesmin 0:7e575e5f88ec 83 char g_szAuthKey[LEN_AUTH_KEY]; ///< 등록 절차 시 OMP에서 할당 받은 AuthKey 저장 변수, 자동화를 위해 사용된다.
lesmin 0:7e575e5f88ec 84
lesmin 0:7e575e5f88ec 85 char g_szDomainCode[LEN_DOMAIN_CODE]; ///< OMP Portal을 통해 사전에 등록된 서비스 영역별 구분 코드 저장 변수, 자동화를 위해 사용된다.
lesmin 0:7e575e5f88ec 86
lesmin 0:7e575e5f88ec 87 char g_szGWID[LEN_GW_ID]; ///< OMP에서 할당 받은 Gateway의 ID 문자열 저장 변수, 자동화를 위해 사용된다.
lesmin 0:7e575e5f88ec 88
lesmin 1:29d7f5c53126 89 Client *g_pClient = NULL;
lesmin 1:29d7f5c53126 90
lesmin 0:7e575e5f88ec 91 /**
lesmin 0:7e575e5f88ec 92 * @brief OMP서버로 부터 수신한 패킷을 제공할 콜백함수 포인트 구조체
lesmin 0:7e575e5f88ec 93 * @param pstGMMPHeader GMMP Header의 구조체 포인트
lesmin 0:7e575e5f88ec 94 * @param pstBody GMMP Body의 구조체 포인트
lesmin 0:7e575e5f88ec 95 * @return 성공:0, 실패: 1이상 , 에러코드 참조
lesmin 0:7e575e5f88ec 96 */
lesmin 0:7e575e5f88ec 97 typedef int (*callback_Reg)(GMMPHeader* pstGMMPHeader, void* pstBody);
lesmin 0:7e575e5f88ec 98
lesmin 0:7e575e5f88ec 99 callback_Reg g_CallFunctionRegRecv = NULL; ///< OMP서버로 부터 수신한 패킷을 제공할 콜백함수 포인트 변수.
lesmin 0:7e575e5f88ec 100 //callback_Reg g_CallHeartbeatRegRecv = NULL; ///< OMP서버로 부터 수신한 패킷 중 Heartbeat 패킷을 제공할 콜백함수 포인트 변수. (TCP Always On mode에서만 사용된다.)
lesmin 0:7e575e5f88ec 101
lesmin 0:7e575e5f88ec 102 void Uninitialize()
lesmin 0:7e575e5f88ec 103 {
lesmin 0:7e575e5f88ec 104 InitMemory();
lesmin 0:7e575e5f88ec 105 CloseSocket();
lesmin 0:7e575e5f88ec 106 }
lesmin 0:7e575e5f88ec 107
lesmin 0:7e575e5f88ec 108 int Initialize(byte* serverIp,
lesmin 0:7e575e5f88ec 109 const int nPort,
lesmin 0:7e575e5f88ec 110 const char* pszDomainCode,
lesmin 0:7e575e5f88ec 111 const char* pszGWAuthID,
lesmin 0:7e575e5f88ec 112 byte* mac)
lesmin 0:7e575e5f88ec 113 {
lesmin 0:7e575e5f88ec 114 debugln("Initialize()");
lesmin 0:7e575e5f88ec 115
lesmin 0:7e575e5f88ec 116 InitMemory();
lesmin 0:7e575e5f88ec 117
hkjung 3:6b4536e1962f 118 //delay(2000);
hkjung 3:6b4536e1962f 119 //NTPClient ntp;
hkjung 3:6b4536e1962f 120
hkjung 3:6b4536e1962f 121 infoln("Getting IP...");
lesmin 0:7e575e5f88ec 122
lesmin 0:7e575e5f88ec 123 #ifdef USE_SNIC_WIFI
lesmin 0:7e575e5f88ec 124 wifi.init();
lesmin 0:7e575e5f88ec 125
lesmin 0:7e575e5f88ec 126 wait(0.5);
lesmin 0:7e575e5f88ec 127 int s = wifi.disconnect();
lesmin 0:7e575e5f88ec 128 if( s != 0 ) {
lesmin 0:7e575e5f88ec 129 return -1;
lesmin 0:7e575e5f88ec 130 }
lesmin 0:7e575e5f88ec 131
lesmin 0:7e575e5f88ec 132 wait(0.3);
lesmin 0:7e575e5f88ec 133 // Connect AP
lesmin 0:7e575e5f88ec 134 wifi.connect( MBED_AP_SSID
lesmin 0:7e575e5f88ec 135 , strlen(MBED_AP_SSID)
lesmin 0:7e575e5f88ec 136 , MBED_AP_SECURITY_TYPE
lesmin 0:7e575e5f88ec 137 , MBED_AP_SECUTIRY_KEY
lesmin 0:7e575e5f88ec 138 , strlen(MBED_AP_SECUTIRY_KEY) );
lesmin 0:7e575e5f88ec 139 wait(0.5);
lesmin 0:7e575e5f88ec 140 wifi.setIPConfig( true ); //Use DHCP
lesmin 0:7e575e5f88ec 141 wait(0.5);
lesmin 0:7e575e5f88ec 142
lesmin 0:7e575e5f88ec 143 tagWIFI_STATUS_T wifi_status;
lesmin 0:7e575e5f88ec 144 if( wifi.getWifiStatus(&wifi_status) ) printf("wifi_status error!\r\n");
lesmin 0:7e575e5f88ec 145
lesmin 0:7e575e5f88ec 146 printf("MAC Address is %02x:%02x:%02x:%02x:%02x:%02x\r\n",
lesmin 0:7e575e5f88ec 147 wifi_status.mac_address[0], wifi_status.mac_address[1], wifi_status.mac_address[2],
lesmin 0:7e575e5f88ec 148 wifi_status.mac_address[3], wifi_status.mac_address[4], wifi_status.mac_address[5]);
lesmin 0:7e575e5f88ec 149 printf("IP Address is %s\r\n", wifi.getIPAddress());
lesmin 0:7e575e5f88ec 150 #endif//USE_SNIC_WIFI
lesmin 0:7e575e5f88ec 151
hkjung 5:9bbb6933559b 152 #ifdef USE_WIZNET_W5500
lesmin 0:7e575e5f88ec 153 //mbed_mac_address((char *)MAC_Addr); //Use mbed mac addres
hkjung 3:6b4536e1962f 154
hkjung 5:9bbb6933559b 155 #if defined(TARGET_NUCLEO_F411RE) // For SPI Test
hkjung 3:6b4536e1962f 156 //spi.format(8,3); // Setup: bit data, high steady state clock, 2nd edge capture
hkjung 3:6b4536e1962f 157 //spi.frequency(25000000); // SPI Clock; 25MHz (default: 1MHz)
hkjung 4:1e04850ce835 158 //spi.frequency(12000000); // SPI Clock; 12.5MHz (default: 1MHz)
hkjung 3:6b4536e1962f 159 #endif
hkjung 3:6b4536e1962f 160
lesmin 0:7e575e5f88ec 161 printf("input MAC Address is %02x:%02x:%02x:%02x:%02x:%02x\r\n",
lesmin 0:7e575e5f88ec 162 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
lesmin 0:7e575e5f88ec 163
lesmin 0:7e575e5f88ec 164 int ret = ethernet.init(mac);
lesmin 0:7e575e5f88ec 165 //printf("SPI Initialized \r\n");
lesmin 0:7e575e5f88ec 166 //wait(1); // 1 second for stable state
lesmin 0:7e575e5f88ec 167
lesmin 0:7e575e5f88ec 168 printf("W5500 Networking Started \r\n");
lesmin 0:7e575e5f88ec 169 //wait(1); // 1 second for stable state
lesmin 0:7e575e5f88ec 170
hkjung 4:1e04850ce835 171 if (!ret) {
hkjung 4:1e04850ce835 172 ret = ethernet.connect();
lesmin 0:7e575e5f88ec 173 if (!ret) {
hkjung 4:1e04850ce835 174 printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
lesmin 0:7e575e5f88ec 175 printf("IP: %s, MASK: %s, GW: %s\r\n",
lesmin 0:7e575e5f88ec 176 ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
lesmin 0:7e575e5f88ec 177 } else {
lesmin 0:7e575e5f88ec 178 printf("Error ethernet.connect() - ret = %d\r\n", ret);
lesmin 0:7e575e5f88ec 179 //exit(0);
hkjung 4:1e04850ce835 180
hkjung 4:1e04850ce835 181 // ## 20150812 Eric added, Ethernet interface re-initialize code
hkjung 4:1e04850ce835 182 ///////////////////////////////////////////////////////////////////////////
hkjung 4:1e04850ce835 183 do{
hkjung 4:1e04850ce835 184 //printf("Ethernet Interface Re-initialize\r\n");
hkjung 4:1e04850ce835 185 #if defined(TARGET_LPC1768)
hkjung 4:1e04850ce835 186 SPI spi(p5, p6, p7); // mosi, miso, sclk
hkjung 4:1e04850ce835 187 #elif defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_L152RE)
hkjung 4:1e04850ce835 188 SPI spi(PA_7, PA_6, PA_5);
hkjung 4:1e04850ce835 189 #endif
hkjung 4:1e04850ce835 190 //wait(0.5);
hkjung 4:1e04850ce835 191
hkjung 4:1e04850ce835 192 ethernet.init(mac);
hkjung 4:1e04850ce835 193 ret = ethernet.connect();
hkjung 4:1e04850ce835 194 printf("Re-initialized, MAC: %s\r\n", ethernet.getMACAddress());
hkjung 4:1e04850ce835 195 printf("IP: %s, MASK: %s, GW: %s\r\n",
hkjung 4:1e04850ce835 196 ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
hkjung 4:1e04850ce835 197 } while(ret);
hkjung 4:1e04850ce835 198 ///////////////////////////////////////////////////////////////////////////
lesmin 0:7e575e5f88ec 199 }
lesmin 0:7e575e5f88ec 200 } else {
lesmin 0:7e575e5f88ec 201 printf("Error ethernet.init() - ret = %d\r\n", ret);
lesmin 0:7e575e5f88ec 202 //exit(0);
lesmin 0:7e575e5f88ec 203 }
lesmin 0:7e575e5f88ec 204
lesmin 0:7e575e5f88ec 205 #endif//USE_WIZNET_W5500
lesmin 0:7e575e5f88ec 206
lesmin 0:7e575e5f88ec 207 NTPClient ntp;
lesmin 0:7e575e5f88ec 208 printf("Trying to update time...\r\n");
lesmin 0:7e575e5f88ec 209 //if (ntp.setTime("0.pool.ntp.org") == 0)
lesmin 0:7e575e5f88ec 210 // if (ntp.setTime("time-a.timefreq.bldrdoc.gov") == 0)
lesmin 0:7e575e5f88ec 211 // if (ntp.setTime("kr.pool.ntp.org") == 0)
lesmin 0:7e575e5f88ec 212 // if (ntp.setTime("112.220.115.166") == 0)
lesmin 0:7e575e5f88ec 213 if (ntp.setTime("211.233.40.78") == 0) //MURATA UDP does not support DNS resolver...
lesmin 0:7e575e5f88ec 214 {
lesmin 0:7e575e5f88ec 215 printf("Set time successfully\r\n");
lesmin 0:7e575e5f88ec 216 time_t ctTime;
lesmin 0:7e575e5f88ec 217 ctTime = time(NULL);
lesmin 0:7e575e5f88ec 218 // printf("Time is set to (UTC): %s\r\n", ctime(&ctTime));
lesmin 0:7e575e5f88ec 219 ctTime += 32400; // GMT+9/Seoul
lesmin 0:7e575e5f88ec 220 printf("Time is set to (GMT+9): %s\r\n", ctime(&ctTime));
lesmin 0:7e575e5f88ec 221 }
lesmin 0:7e575e5f88ec 222 else
lesmin 0:7e575e5f88ec 223 {
lesmin 0:7e575e5f88ec 224 printf("Error\r\n");
lesmin 0:7e575e5f88ec 225 }
hkjung 3:6b4536e1962f 226
lesmin 0:7e575e5f88ec 227 g_pClient = new Client; //lesmin
lesmin 0:7e575e5f88ec 228
lesmin 0:7e575e5f88ec 229 if(SetServerInfo(serverIp, nPort, pszGWAuthID, pszDomainCode) != 0)
lesmin 0:7e575e5f88ec 230 {
lesmin 0:7e575e5f88ec 231 return LIB_PARAM_ERROR;
hkjung 4:1e04850ce835 232 }
hkjung 3:6b4536e1962f 233
lesmin 0:7e575e5f88ec 234 SetTID(0);
lesmin 0:7e575e5f88ec 235
lesmin 0:7e575e5f88ec 236 return GMMP_SUCCESS;
lesmin 0:7e575e5f88ec 237 }
lesmin 0:7e575e5f88ec 238
lesmin 0:7e575e5f88ec 239 void SetAuthKey(const char* pszAuthKey)
lesmin 0:7e575e5f88ec 240 {
lesmin 0:7e575e5f88ec 241 memcpy(g_szAuthKey, pszAuthKey, strlen(pszAuthKey));
lesmin 0:7e575e5f88ec 242 }
lesmin 0:7e575e5f88ec 243
lesmin 0:7e575e5f88ec 244 void SetGWID(const char* pszGWID)
lesmin 0:7e575e5f88ec 245 {
lesmin 0:7e575e5f88ec 246 if(pszGWID != NULL)
lesmin 0:7e575e5f88ec 247 {
lesmin 0:7e575e5f88ec 248 memcpy(g_szGWID, pszGWID, strlen(pszGWID));
lesmin 0:7e575e5f88ec 249 }
lesmin 0:7e575e5f88ec 250 }
lesmin 0:7e575e5f88ec 251
lesmin 0:7e575e5f88ec 252 char* GetGWID()
lesmin 0:7e575e5f88ec 253 {
lesmin 0:7e575e5f88ec 254 return g_szGWID;
lesmin 0:7e575e5f88ec 255 }
lesmin 0:7e575e5f88ec 256
lesmin 0:7e575e5f88ec 257 int SetServerInfo(byte* serverIp, int nPort, const char* pszAuthID, const char* pszDoamainCode)
lesmin 0:7e575e5f88ec 258 {
lesmin 0:7e575e5f88ec 259 /*
lesmin 0:7e575e5f88ec 260 if(pszAuthID == NULL || strlen(pszAuthID) > LEN_AUTH_ID
lesmin 0:7e575e5f88ec 261 || pszDoamainCode == NULL || strlen(pszDoamainCode) > LEN_DOMAIN_CODE)
lesmin 0:7e575e5f88ec 262 {
lesmin 0:7e575e5f88ec 263 return LIB_PARAM_ERROR;
lesmin 0:7e575e5f88ec 264 }
lesmin 0:7e575e5f88ec 265 */
lesmin 0:7e575e5f88ec 266
lesmin 0:7e575e5f88ec 267 memcpy(g_serverIp, serverIp, LEN_IP);
lesmin 0:7e575e5f88ec 268 memcpy(g_szAuthID, pszAuthID, strlen(pszAuthID));
lesmin 0:7e575e5f88ec 269 memcpy(g_szDomainCode, pszDoamainCode, strlen(pszDoamainCode));
lesmin 0:7e575e5f88ec 270
lesmin 0:7e575e5f88ec 271 g_nServerPort = nPort;
lesmin 0:7e575e5f88ec 272
lesmin 0:7e575e5f88ec 273 int nRet = SetIntiSocket();
lesmin 0:7e575e5f88ec 274 if(nRet != GMMP_SUCCESS)
lesmin 0:7e575e5f88ec 275 {
lesmin 0:7e575e5f88ec 276 return nRet;
lesmin 0:7e575e5f88ec 277 }
lesmin 0:7e575e5f88ec 278
lesmin 0:7e575e5f88ec 279 return GMMP_SUCCESS;
lesmin 0:7e575e5f88ec 280 }
lesmin 0:7e575e5f88ec 281
lesmin 0:7e575e5f88ec 282 void SetCallFunction(int (* pCallFunctionName)(GMMPHeader* pstGMMPHeader, void* pstBody))
lesmin 0:7e575e5f88ec 283 {
lesmin 0:7e575e5f88ec 284 g_CallFunctionRegRecv = (callback_Reg)pCallFunctionName;
lesmin 0:7e575e5f88ec 285 }
lesmin 0:7e575e5f88ec 286
lesmin 0:7e575e5f88ec 287 int GO_Reg(const char* pszGWID,
lesmin 0:7e575e5f88ec 288 const char* pszManufactureID)
hkjung 4:1e04850ce835 289 {
hkjung 4:1e04850ce835 290 //CloseSocket();
hkjung 4:1e04850ce835 291 //SetIntiSocket();
hkjung 4:1e04850ce835 292
lesmin 0:7e575e5f88ec 293 debugln("GO_Reg()");
lesmin 0:7e575e5f88ec 294 SetTID(GetTID()+1);
hkjung 4:1e04850ce835 295
lesmin 0:7e575e5f88ec 296 int nRet = GMMP_SetReg(g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszManufactureID);
lesmin 0:7e575e5f88ec 297 debugln("GO_SetReg() Done");
lesmin 0:7e575e5f88ec 298
lesmin 0:7e575e5f88ec 299 return nRet;
lesmin 0:7e575e5f88ec 300 }
lesmin 0:7e575e5f88ec 301
lesmin 0:7e575e5f88ec 302 int GO_DeReg(const char* pszGWID, const char* pszDeviceID)
lesmin 0:7e575e5f88ec 303 {
lesmin 0:7e575e5f88ec 304 SetTID(GetTID()+1);
lesmin 0:7e575e5f88ec 305
lesmin 0:7e575e5f88ec 306 int nRet = GMMP_SetDeReg(g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszDeviceID);
lesmin 0:7e575e5f88ec 307
lesmin 0:7e575e5f88ec 308 return nRet;
lesmin 0:7e575e5f88ec 309 }
lesmin 0:7e575e5f88ec 310
lesmin 0:7e575e5f88ec 311 int GO_Profile(const char* pszGWID,
lesmin 0:7e575e5f88ec 312 const char* pszDeviceID,
lesmin 0:7e575e5f88ec 313 const long nTID /*=0*/)
lesmin 0:7e575e5f88ec 314 {
lesmin 0:7e575e5f88ec 315 if(nTID == 0) {
lesmin 0:7e575e5f88ec 316 SetTID(GetTID()+1);
lesmin 0:7e575e5f88ec 317 } else {
lesmin 0:7e575e5f88ec 318 SetTID(nTID);
lesmin 0:7e575e5f88ec 319 }
lesmin 0:7e575e5f88ec 320
lesmin 0:7e575e5f88ec 321 int nRet = GMMP_SetProfile(g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszDeviceID);
lesmin 0:7e575e5f88ec 322
lesmin 0:7e575e5f88ec 323 return nRet;
lesmin 0:7e575e5f88ec 324 }
lesmin 0:7e575e5f88ec 325
lesmin 0:7e575e5f88ec 326 int GO_Delivery(const char* pszGWID,
lesmin 0:7e575e5f88ec 327 const char* pszDeviceID,
lesmin 0:7e575e5f88ec 328 const char cReportType,
lesmin 0:7e575e5f88ec 329 const char cMediaType,
lesmin 0:7e575e5f88ec 330 const char* pszMessageBody)
lesmin 0:7e575e5f88ec 331 {
lesmin 0:7e575e5f88ec 332 SetTID(GetTID()+1);
lesmin 0:7e575e5f88ec 333
lesmin 0:7e575e5f88ec 334 int nRet = GMMP_SetDelivery(g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszDeviceID, cReportType, cMediaType, pszMessageBody, 1, 1);
lesmin 0:7e575e5f88ec 335
lesmin 0:7e575e5f88ec 336 return nRet;
lesmin 0:7e575e5f88ec 337 }
lesmin 0:7e575e5f88ec 338
lesmin 0:7e575e5f88ec 339 int GO_Control(const char* pszGWID,
lesmin 0:7e575e5f88ec 340 const char* pszDeviceID,
lesmin 0:7e575e5f88ec 341 long nTID,
lesmin 0:7e575e5f88ec 342 const char cControlType,
lesmin 0:7e575e5f88ec 343 const char cResultCode)
lesmin 0:7e575e5f88ec 344 {
lesmin 0:7e575e5f88ec 345 SetTID(nTID);
lesmin 0:7e575e5f88ec 346
lesmin 0:7e575e5f88ec 347 return GMMP_SetControl(g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszDeviceID, cControlType, cResultCode);
lesmin 0:7e575e5f88ec 348 }
lesmin 0:7e575e5f88ec 349
lesmin 0:7e575e5f88ec 350 int GO_Notifi(const char* pszGWID,
lesmin 0:7e575e5f88ec 351 const char* pszDeviceID,
lesmin 0:7e575e5f88ec 352 const char cControlType,
lesmin 0:7e575e5f88ec 353 const char cResultCode,
lesmin 0:7e575e5f88ec 354 const char* pszMessageBody,
lesmin 0:7e575e5f88ec 355 const int nMessageBodySize)
lesmin 0:7e575e5f88ec 356 {
lesmin 0:7e575e5f88ec 357 int nRet = GMMP_SetNotifi(g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszDeviceID, cControlType, cResultCode, pszMessageBody, nMessageBodySize);
lesmin 0:7e575e5f88ec 358
lesmin 0:7e575e5f88ec 359 return nRet;
lesmin 0:7e575e5f88ec 360 }
lesmin 0:7e575e5f88ec 361
lesmin 0:7e575e5f88ec 362 int GO_HB(const char* pszGWID)
lesmin 0:7e575e5f88ec 363 {
lesmin 0:7e575e5f88ec 364 int nRet = GMMP_SetHB(g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID);
lesmin 0:7e575e5f88ec 365
lesmin 0:7e575e5f88ec 366 return nRet;
lesmin 0:7e575e5f88ec 367 }
lesmin 0:7e575e5f88ec 368
lesmin 0:7e575e5f88ec 369 int OG_Reg_Recv(GMMPHeader* pstGMMPHeader, stGwRegistrationRspHdr* pstGWBody, stDeviceRegistrationRspHdr* pstDeviceBody)
lesmin 0:7e575e5f88ec 370 {
lesmin 0:7e575e5f88ec 371 int nRet = 0;
lesmin 0:7e575e5f88ec 372
lesmin 0:7e575e5f88ec 373 if(g_CallFunctionRegRecv != NULL)
lesmin 0:7e575e5f88ec 374 {
lesmin 0:7e575e5f88ec 375 if(pstGWBody != NULL)
lesmin 0:7e575e5f88ec 376 {
lesmin 0:7e575e5f88ec 377 nRet= (*g_CallFunctionRegRecv)(pstGMMPHeader, pstGWBody);
lesmin 0:7e575e5f88ec 378
lesmin 0:7e575e5f88ec 379 if(nRet != GMMP_SUCCESS)
lesmin 0:7e575e5f88ec 380 {
lesmin 0:7e575e5f88ec 381 return nRet;
lesmin 0:7e575e5f88ec 382 }
lesmin 0:7e575e5f88ec 383 }
lesmin 0:7e575e5f88ec 384 else
lesmin 0:7e575e5f88ec 385 {
lesmin 0:7e575e5f88ec 386 nRet = (*g_CallFunctionRegRecv)(pstGMMPHeader, pstDeviceBody);
lesmin 0:7e575e5f88ec 387
lesmin 0:7e575e5f88ec 388 if(nRet != GMMP_SUCCESS)
lesmin 0:7e575e5f88ec 389 {
lesmin 0:7e575e5f88ec 390 return nRet;
lesmin 0:7e575e5f88ec 391 }
lesmin 0:7e575e5f88ec 392 }
lesmin 0:7e575e5f88ec 393 }
lesmin 0:7e575e5f88ec 394
lesmin 0:7e575e5f88ec 395 return GMMP_SUCCESS;
lesmin 0:7e575e5f88ec 396 }
lesmin 0:7e575e5f88ec 397
lesmin 0:7e575e5f88ec 398 int OG_DeReg_Recv(GMMPHeader* pstGMMPHeader, stGwDeRegistrationRspHdr* pstGWBody, stDeviceDeRegistrationRspHdr* pstDeviceBody)
lesmin 0:7e575e5f88ec 399 {
lesmin 0:7e575e5f88ec 400 if(g_CallFunctionRegRecv != NULL)
lesmin 0:7e575e5f88ec 401 {
lesmin 0:7e575e5f88ec 402 if(pstGWBody != NULL)
lesmin 0:7e575e5f88ec 403 {
lesmin 0:7e575e5f88ec 404 return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstGWBody);
lesmin 0:7e575e5f88ec 405 }
lesmin 0:7e575e5f88ec 406 else
lesmin 0:7e575e5f88ec 407 {
lesmin 0:7e575e5f88ec 408 return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstDeviceBody);
lesmin 0:7e575e5f88ec 409 }
lesmin 0:7e575e5f88ec 410 }
lesmin 0:7e575e5f88ec 411
lesmin 0:7e575e5f88ec 412 return GMMP_SUCCESS;
lesmin 0:7e575e5f88ec 413 }
lesmin 0:7e575e5f88ec 414
lesmin 0:7e575e5f88ec 415 int OG_Profile_Recv(GMMPHeader* pstGMMPHeader, stProfileRspHdr* pstBody)
lesmin 0:7e575e5f88ec 416 {
lesmin 0:7e575e5f88ec 417 if(g_CallFunctionRegRecv != NULL)
lesmin 0:7e575e5f88ec 418 {
lesmin 0:7e575e5f88ec 419 return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstBody);
lesmin 0:7e575e5f88ec 420 }
lesmin 0:7e575e5f88ec 421
lesmin 0:7e575e5f88ec 422 return GMMP_SUCCESS;
lesmin 0:7e575e5f88ec 423 }
lesmin 0:7e575e5f88ec 424
lesmin 0:7e575e5f88ec 425 int OG_Delivery_Recv(GMMPHeader* pstGMMPHeader, stPacketDeliveryRspHdr* pstBody)
lesmin 0:7e575e5f88ec 426 {
lesmin 0:7e575e5f88ec 427 if(g_CallFunctionRegRecv != NULL)
lesmin 0:7e575e5f88ec 428 {
lesmin 0:7e575e5f88ec 429 return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstBody);
lesmin 0:7e575e5f88ec 430 }
lesmin 0:7e575e5f88ec 431
lesmin 0:7e575e5f88ec 432 return GMMP_SUCCESS;
lesmin 0:7e575e5f88ec 433 }
lesmin 0:7e575e5f88ec 434
lesmin 0:7e575e5f88ec 435 int OG_Ctrl_Recv(GMMPHeader* pstGMMPHeader, stControlReqHdr* pstBody)
lesmin 0:7e575e5f88ec 436 {
lesmin 0:7e575e5f88ec 437 if(g_CallFunctionRegRecv != NULL)
lesmin 0:7e575e5f88ec 438 {
lesmin 0:7e575e5f88ec 439 return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstBody);
lesmin 0:7e575e5f88ec 440 }
lesmin 0:7e575e5f88ec 441
lesmin 0:7e575e5f88ec 442 return GMMP_SUCCESS;
lesmin 0:7e575e5f88ec 443 }
lesmin 0:7e575e5f88ec 444
lesmin 0:7e575e5f88ec 445 int OG_HB_Recv(GMMPHeader* pstGMMPHeader, stHeartBeatMsgRspHdr* pstBody)
lesmin 0:7e575e5f88ec 446 {
lesmin 0:7e575e5f88ec 447 if(g_CallFunctionRegRecv != NULL)
lesmin 0:7e575e5f88ec 448 {
lesmin 0:7e575e5f88ec 449 return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstBody);
lesmin 0:7e575e5f88ec 450 }
lesmin 0:7e575e5f88ec 451
lesmin 0:7e575e5f88ec 452 return GMMP_SUCCESS;
lesmin 0:7e575e5f88ec 453 }
lesmin 0:7e575e5f88ec 454
lesmin 0:7e575e5f88ec 455 int OG_Notifi_Recv(GMMPHeader* pstGMMPHeader, stNotificationRspHdr* pstBody)
lesmin 0:7e575e5f88ec 456 {
lesmin 0:7e575e5f88ec 457 if(g_CallFunctionRegRecv != NULL)
lesmin 0:7e575e5f88ec 458 {
lesmin 0:7e575e5f88ec 459 return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstBody);
lesmin 0:7e575e5f88ec 460 }
lesmin 0:7e575e5f88ec 461
lesmin 0:7e575e5f88ec 462 return GMMP_SUCCESS;
lesmin 0:7e575e5f88ec 463 }
lesmin 0:7e575e5f88ec 464
lesmin 0:7e575e5f88ec 465 int GetReadData(GMMPHeader* pstGMMPHeader, void** pBody)
lesmin 0:7e575e5f88ec 466 {
lesmin 0:7e575e5f88ec 467 int nHeaderCound = 0;
lesmin 0:7e575e5f88ec 468 int nRet = 0;
lesmin 0:7e575e5f88ec 469
lesmin 0:7e575e5f88ec 470 while(true)
lesmin 0:7e575e5f88ec 471 {
lesmin 0:7e575e5f88ec 472 nRet = GMMP_Read(pstGMMPHeader, pBody);
lesmin 0:7e575e5f88ec 473 if(nRet == GMMP_HEADER_SIZE_ERROR) {
lesmin 0:7e575e5f88ec 474 if(nHeaderCound > 3) {
lesmin 0:7e575e5f88ec 475 break;
lesmin 0:7e575e5f88ec 476 }
lesmin 0:7e575e5f88ec 477 nHeaderCound++;
lesmin 0:7e575e5f88ec 478 continue;
lesmin 0:7e575e5f88ec 479 }
lesmin 0:7e575e5f88ec 480 else if(nRet == GMMP_SUCCESS) {
lesmin 0:7e575e5f88ec 481 break;
lesmin 0:7e575e5f88ec 482 }
lesmin 0:7e575e5f88ec 483 else {
lesmin 0:7e575e5f88ec 484 return nRet;
lesmin 0:7e575e5f88ec 485 }
lesmin 0:7e575e5f88ec 486 }
lesmin 0:7e575e5f88ec 487
lesmin 0:7e575e5f88ec 488 GMMP_Recv(pstGMMPHeader, *pBody);
lesmin 0:7e575e5f88ec 489
lesmin 0:7e575e5f88ec 490 return nRet;
lesmin 0:7e575e5f88ec 491 }
lesmin 0:7e575e5f88ec 492
lesmin 0:7e575e5f88ec 493 long Char2int(void* pBuffer, int nSize)
lesmin 0:7e575e5f88ec 494 {
lesmin 0:7e575e5f88ec 495 if(nSize != sizeof(long)) {
lesmin 0:7e575e5f88ec 496 return 0;
lesmin 0:7e575e5f88ec 497 }
lesmin 0:7e575e5f88ec 498
lesmin 0:7e575e5f88ec 499 long nInt = 0;
lesmin 0:7e575e5f88ec 500 memcpy(&nInt, pBuffer, sizeof(long));
lesmin 0:7e575e5f88ec 501 return btoli(nInt);
lesmin 0:7e575e5f88ec 502 }
lesmin 0:7e575e5f88ec 503
lesmin 0:7e575e5f88ec 504 int Char2short(void* pBuffer, short nSize)
lesmin 0:7e575e5f88ec 505 {
lesmin 0:7e575e5f88ec 506 if(nSize != sizeof(short)) {
lesmin 0:7e575e5f88ec 507 return 0;
lesmin 0:7e575e5f88ec 508 }
lesmin 0:7e575e5f88ec 509
lesmin 0:7e575e5f88ec 510 short nShort = 0;
lesmin 0:7e575e5f88ec 511 memcpy(&nShort, pBuffer, sizeof(short));
lesmin 0:7e575e5f88ec 512 return btols(nShort);
lesmin 0:7e575e5f88ec 513 }
lesmin 0:7e575e5f88ec 514
lesmin 0:7e575e5f88ec 515 void InitMemory()
lesmin 0:7e575e5f88ec 516 {
lesmin 0:7e575e5f88ec 517 memset(g_szDomainCode, 0 , sizeof(g_szAuthID));
lesmin 0:7e575e5f88ec 518 memset(g_szAuthID, 0 , sizeof(g_szAuthID));
lesmin 0:7e575e5f88ec 519 memset(g_szAuthKey, 0 , sizeof(g_szAuthKey));
lesmin 0:7e575e5f88ec 520 memset(g_szGWID, 0 , sizeof(g_szGWID));
lesmin 0:7e575e5f88ec 521
lesmin 0:7e575e5f88ec 522 g_nServerPort = 0;
lesmin 0:7e575e5f88ec 523
lesmin 0:7e575e5f88ec 524 g_CallFunctionRegRecv = NULL;
lesmin 0:7e575e5f88ec 525 }
lesmin 0:7e575e5f88ec 526
lesmin 0:7e575e5f88ec 527 #endif