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 02:13:34 2015 +0000
Revision:
3:6b4536e1962f
Parent:
2:0c4e3edf2918
Child:
4:1e04850ce835
Modified for W5500 Ethernet initialize

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