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.
Fork of GMMP by
GMMP.cpp
00001 /** GMMP 공통 정의 00002 * @file GMMP.cpp 00003 * @date 2015/07/20 00004 * @version 0.0.1.0 00005 **/ 00006 00007 #ifndef GMMP_h 00008 #define GMMP_h 00009 00010 /* 00011 #include <stdio.h> 00012 #include <stdlib.h> 00013 #include <string.h> 00014 #include <time.h> 00015 #include <pthread.h> 00016 #include <unistd.h> 00017 #include <stdarg.h> 00018 */ 00019 00020 //#include <inttypes.h> 00021 //#include <Time.h> 00022 //#include <Arduino.h> 00023 00024 #define USE_SNIC_WIFI //Murata Type YD Wi-Fi 00025 //#define USE_WIZNET_W5500 00026 00027 #if defined(TARGET_LPC1768) 00028 #include "EthernetInterface.h" 00029 #elif defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_L152RE) 00030 00031 #ifdef USE_SNIC_WIFI 00032 #include "SNIC_WifiInterface.h" 00033 #endif//USE_SNIC_WIFI 00034 00035 #ifdef USE_WIZNET_W5500 00036 #include "WIZnetInterface.h" 00037 #endif//USE_WIZNET_W5500 00038 #endif 00039 00040 #include "NTPClient.h" 00041 00042 #include "GMMP.h " 00043 00044 byte g_serverIp[LEN_IP] ; ///< 서버 IP 정보를 저장한다. 00045 int g_nServerPort = 0; ///< 서버 Port 정보를 저장한다. 00046 00047 char g_szAuthID[LEN_AUTH_ID];///< OMP Portal을 통해 사전에 등록된 M2M GW 의 Serial Number 저장 변수, 자동화를 위해 사용된다. 00048 00049 char g_szAuthKey[LEN_AUTH_KEY]; ///< 등록 절차 시 OMP에서 할당 받은 AuthKey 저장 변수, 자동화를 위해 사용된다. 00050 00051 char g_szDomainCode[LEN_DOMAIN_CODE]; ///< OMP Portal을 통해 사전에 등록된 서비스 영역별 구분 코드 저장 변수, 자동화를 위해 사용된다. 00052 00053 char g_szGWID[LEN_GW_ID]; ///< OMP에서 할당 받은 Gateway의 ID 문자열 저장 변수, 자동화를 위해 사용된다. 00054 00055 /** 00056 * @brief OMP서버로 부터 수신한 패킷을 제공할 콜백함수 포인트 구조체 00057 * @param pstGMMPHeader GMMP Header의 구조체 포인트 00058 * @param pstBody GMMP Body의 구조체 포인트 00059 * @return 성공:0, 실패: 1이상 , 에러코드 참조 00060 */ 00061 typedef int (*callback_Reg)(GMMPHeader* pstGMMPHeader, void* pstBody); 00062 00063 callback_Reg g_CallFunctionRegRecv = NULL; ///< OMP서버로 부터 수신한 패킷을 제공할 콜백함수 포인트 변수. 00064 //callback_Reg g_CallHeartbeatRegRecv = NULL; ///< OMP서버로 부터 수신한 패킷 중 Heartbeat 패킷을 제공할 콜백함수 포인트 변수. (TCP Always On mode에서만 사용된다.) 00065 00066 void Uninitialize() 00067 { 00068 InitMemory(); 00069 CloseSocket(); 00070 } 00071 00072 00073 00074 int Initialize(byte* serverIp, 00075 const int nPort, 00076 const char* pszDomainCode, 00077 const char* pszGWAuthID, 00078 byte* mac) 00079 { 00080 debugln("Initialize()"); 00081 00082 InitMemory(); 00083 00084 //delay(2000); 00085 #if defined(TARGET_LPC1768) 00086 EthernetInterface eth; 00087 #elif defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_L152RE) 00088 00089 #ifdef USE_SNIC_WIFI 00090 C_SNIC_WifiInterface wifi( D8, D2, NC, NC, D3); 00091 00092 #define MBED_AP_SSID "tide855" //"tide867" 00093 /** Securiry Options 00094 e_SEC_OPEN = 0x00, //Open 00095 e_SEC_WEP = 0x01, // WEP 00096 e_SEC_WPA_TKIP = 0x02, // WPA-PSK(TKIP) 00097 e_SEC_WPA2_AES = 0x04, // WPA2-PSK(AES) 00098 e_SEC_WPA2_MIXED = 0x06, // WPA2-PSK(TKIP/AES) 00099 e_SEC_WPA_AES = 0x07 // WPA-PSK(AES) **/ 00100 #define MBED_AP_SECURITY_TYPE e_SEC_WPA2_AES//e_SEC_OPEN 00101 #define MBED_AP_SECUTIRY_KEY "tidetide" 00102 00103 #endif//USE_SNIC_WIFI 00104 00105 #ifdef USE_WIZNET_W5500 00106 //unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x12,0x34,0x56}; 00107 SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk 00108 WIZnetInterface ethernet(&spi, PB_6, PA_9);//scs(PB_6), nRESET(PA_9); // reset pin is dummy, don't affect any pin of WIZ550io 00109 #endif//USE_WIZNET_W5500 00110 00111 #endif 00112 //NTPClient ntp; 00113 00114 infoln("Getting IP..."); 00115 00116 #if defined(TARGET_LPC1768) 00117 eth.init(); //Use DHCP 00118 eth.connect(); 00119 wait(0.5); 00120 printf("IP Address is %s\r\n", eth.getIPAddress()); 00121 #elif defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_L152RE) 00122 00123 #ifdef USE_SNIC_WIFI 00124 wifi.init(); 00125 00126 wait(0.5); 00127 int s = wifi.disconnect(); 00128 if( s != 0 ) { 00129 return -1; 00130 } 00131 00132 wait(0.3); 00133 // Connect AP 00134 wifi.connect( MBED_AP_SSID 00135 , strlen(MBED_AP_SSID) 00136 , MBED_AP_SECURITY_TYPE 00137 , MBED_AP_SECUTIRY_KEY 00138 , strlen(MBED_AP_SECUTIRY_KEY) ); 00139 wait(0.5); 00140 wifi.setIPConfig( true ); //Use DHCP 00141 wait(0.5); 00142 00143 tagWIFI_STATUS_T wifi_status; 00144 if( wifi.getWifiStatus(&wifi_status) ) printf("wifi_status error!\r\n"); 00145 00146 printf("MAC Address is %02x:%02x:%02x:%02x:%02x:%02x\r\n", 00147 wifi_status.mac_address[0], wifi_status.mac_address[1], wifi_status.mac_address[2], 00148 wifi_status.mac_address[3], wifi_status.mac_address[4], wifi_status.mac_address[5]); 00149 printf("IP Address is %s\r\n", wifi.getIPAddress()); 00150 #endif//USE_SNIC_WIFI 00151 00152 #ifdef USE_WIZNET_W5500 00153 //mbed_mac_address((char *)MAC_Addr); //Use mbed mac addres 00154 00155 int ret = ethernet.init(mac); 00156 printf("SPI Initialized \r\n"); 00157 wait(1); // 1 second for stable state 00158 00159 printf("W5500 Networking Started \r\n"); 00160 wait(1); // 1 second for stable state 00161 00162 if (!ret) { 00163 printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress()); 00164 ret = ethernet.connect(); 00165 if (!ret) { 00166 printf("IP: %s, MASK: %s, GW: %s\r\n", 00167 ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway()); 00168 } else { 00169 printf("Error ethernet.connect() - ret = %d\r\n", ret); 00170 //exit(0); 00171 } 00172 } else { 00173 printf("Error ethernet.init() - ret = %d\r\n", ret); 00174 //exit(0); 00175 } 00176 00177 #endif//USE_WIZNET_W5500 00178 #endif 00179 00180 00181 NTPClient ntp; 00182 printf("Trying to update time...\r\n"); 00183 //if (ntp.setTime("0.pool.ntp.org") == 0) 00184 // if (ntp.setTime("time-a.timefreq.bldrdoc.gov") == 0) 00185 // if (ntp.setTime("kr.pool.ntp.org") == 0) 00186 // if (ntp.setTime("112.220.115.166") == 0) 00187 if (ntp.setTime("211.233.40.78") == 0) //MURATA UDP does not support DNS resolver... 00188 { 00189 printf("Set time successfully\r\n"); 00190 time_t ctTime; 00191 ctTime = time(NULL); 00192 // printf("Time is set to (UTC): %s\r\n", ctime(&ctTime)); 00193 ctTime += 32400; // GMT+9/Seoul 00194 printf("Time is set to (GMT+9): %s\r\n", ctime(&ctTime)); 00195 } 00196 else 00197 { 00198 printf("Error\r\n"); 00199 return SERVER_CONNECT_ERROR; 00200 } 00201 00202 00203 if(SetServerInfo(serverIp, nPort, pszGWAuthID, pszDomainCode) != 0) 00204 { 00205 return SERVER_CONNECT_ERROR; 00206 } 00207 SetTID (0); 00208 00209 return GMMP_SUCCESS; 00210 } 00211 00212 void SetAuthKey(const char* pszAuthKey) 00213 { 00214 memcpy(g_szAuthKey, pszAuthKey, strlen(pszAuthKey)); 00215 } 00216 00217 void SetGWID(const char* pszGWID) 00218 { 00219 if(pszGWID != NULL) 00220 { 00221 memcpy(g_szGWID, pszGWID, strlen(pszGWID)); 00222 } 00223 } 00224 00225 char* GetGWID () 00226 { 00227 return g_szGWID; 00228 } 00229 00230 int SetServerInfo(byte* serverIp, int nPort, const char* pszAuthID, const char* pszDoamainCode) 00231 { 00232 /* 00233 if(pszAuthID == NULL || strlen(pszAuthID) > LEN_AUTH_ID 00234 || pszDoamainCode == NULL || strlen(pszDoamainCode) > LEN_DOMAIN_CODE) 00235 { 00236 return LIB_PARAM_ERROR; 00237 } 00238 */ 00239 00240 memcpy(g_serverIp, serverIp, LEN_IP); 00241 memcpy(g_szAuthID, pszAuthID, strlen(pszAuthID)); 00242 memcpy(g_szDomainCode, pszDoamainCode, strlen(pszDoamainCode)); 00243 00244 g_nServerPort = nPort; 00245 00246 int nRet = SetIntiSocket (); 00247 if(nRet != GMMP_SUCCESS) 00248 { 00249 return nRet; 00250 } 00251 00252 return GMMP_SUCCESS; 00253 } 00254 00255 void SetCallFunction(int (* pCallFunctionName)(GMMPHeader* pstGMMPHeader, void* pstBody)) 00256 { 00257 g_CallFunctionRegRecv = (callback_Reg)pCallFunctionName; 00258 } 00259 00260 int GO_Reg(const char* pszGWID, 00261 const char* pszManufactureID) 00262 { 00263 debugln("GO_Reg()"); 00264 SetTID (GetTID ()+1); 00265 00266 int nRet = GMMP_SetReg(g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszManufactureID); 00267 debugln("GO_SetReg() Done"); 00268 00269 return nRet; 00270 } 00271 00272 int GO_DeReg(const char* pszGWID, const char* pszDeviceID) 00273 { 00274 SetTID (GetTID ()+1); 00275 00276 int nRet = GMMP_SetDeReg (g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszDeviceID); 00277 00278 return nRet; 00279 } 00280 00281 int GO_Profile (const char* pszGWID, 00282 const char* pszDeviceID, 00283 const long nTID /*=0*/) 00284 { 00285 if(nTID == 0) { 00286 SetTID (GetTID ()+1); 00287 } else { 00288 SetTID (nTID); 00289 } 00290 00291 int nRet = GMMP_SetProfile (g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszDeviceID); 00292 00293 return nRet; 00294 } 00295 00296 int GO_Delivery (const char* pszGWID, 00297 const char* pszDeviceID, 00298 const char cReportType, 00299 const char cMediaType, 00300 const char* pszMessageBody) 00301 { 00302 SetTID (GetTID ()+1); 00303 00304 int nRet = GMMP_SetDelivery (g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszDeviceID, cReportType, cMediaType, pszMessageBody, 1, 1); 00305 00306 return nRet; 00307 } 00308 00309 int GO_Control (const char* pszGWID, 00310 const char* pszDeviceID, 00311 long nTID, 00312 const char cControlType, 00313 const char cResultCode) 00314 { 00315 SetTID (nTID); 00316 00317 return GMMP_SetControl (g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszDeviceID, cControlType, cResultCode); 00318 } 00319 00320 int GO_Notifi (const char* pszGWID, 00321 const char* pszDeviceID, 00322 const char cControlType, 00323 const char cResultCode, 00324 const char* pszMessageBody, 00325 const int nMessageBodySize) 00326 { 00327 int nRet = GMMP_SetNotifi (g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszDeviceID, cControlType, cResultCode, pszMessageBody, nMessageBodySize); 00328 00329 return nRet; 00330 } 00331 00332 int GO_HB (const char* pszGWID) 00333 { 00334 int nRet = GMMP_SetHB (g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID); 00335 00336 return nRet; 00337 } 00338 00339 int OG_Reg_Recv(GMMPHeader* pstGMMPHeader, stGwRegistrationRspHdr* pstGWBody, stDeviceRegistrationRspHdr* pstDeviceBody) 00340 { 00341 int nRet = 0; 00342 00343 if(g_CallFunctionRegRecv != NULL) 00344 { 00345 if(pstGWBody != NULL) 00346 { 00347 nRet= (*g_CallFunctionRegRecv)(pstGMMPHeader, pstGWBody); 00348 00349 if(nRet != GMMP_SUCCESS) 00350 { 00351 return nRet; 00352 } 00353 } 00354 else 00355 { 00356 nRet = (*g_CallFunctionRegRecv)(pstGMMPHeader, pstDeviceBody); 00357 00358 if(nRet != GMMP_SUCCESS) 00359 { 00360 return nRet; 00361 } 00362 } 00363 } 00364 00365 return GMMP_SUCCESS; 00366 } 00367 00368 int OG_DeReg_Recv(GMMPHeader* pstGMMPHeader, stGwDeRegistrationRspHdr* pstGWBody, stDeviceDeRegistrationRspHdr* pstDeviceBody) 00369 { 00370 if(g_CallFunctionRegRecv != NULL) 00371 { 00372 if(pstGWBody != NULL) 00373 { 00374 return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstGWBody); 00375 } 00376 else 00377 { 00378 return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstDeviceBody); 00379 } 00380 } 00381 00382 return GMMP_SUCCESS; 00383 } 00384 00385 int OG_Profile_Recv(GMMPHeader* pstGMMPHeader, stProfileRspHdr* pstBody) 00386 { 00387 if(g_CallFunctionRegRecv != NULL) 00388 { 00389 return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstBody); 00390 } 00391 00392 return GMMP_SUCCESS; 00393 } 00394 00395 int OG_Delivery_Recv(GMMPHeader* pstGMMPHeader, stPacketDeliveryRspHdr* pstBody) 00396 { 00397 if(g_CallFunctionRegRecv != NULL) 00398 { 00399 return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstBody); 00400 } 00401 00402 return GMMP_SUCCESS; 00403 } 00404 00405 int OG_Ctrl_Recv(GMMPHeader* pstGMMPHeader, stControlReqHdr* pstBody) 00406 { 00407 if(g_CallFunctionRegRecv != NULL) 00408 { 00409 return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstBody); 00410 } 00411 00412 return GMMP_SUCCESS; 00413 } 00414 00415 int OG_HB_Recv(GMMPHeader* pstGMMPHeader, stHeartBeatMsgRspHdr* pstBody) 00416 { 00417 if(g_CallFunctionRegRecv != NULL) 00418 { 00419 return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstBody); 00420 } 00421 00422 return GMMP_SUCCESS; 00423 } 00424 00425 int OG_Notifi_Recv(GMMPHeader* pstGMMPHeader, stNotificationRspHdr* pstBody) 00426 { 00427 if(g_CallFunctionRegRecv != NULL) 00428 { 00429 return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstBody); 00430 } 00431 00432 return GMMP_SUCCESS; 00433 } 00434 00435 int GetReadData(GMMPHeader* pstGMMPHeader, void** pBody) 00436 { 00437 int nHeaderCound = 0; 00438 int nRet = 0; 00439 00440 while(true) 00441 { 00442 nRet = GMMP_Read (pstGMMPHeader, pBody); 00443 if(nRet == GMMP_HEADER_SIZE_ERROR) { 00444 if(nHeaderCound > 3) { 00445 break; 00446 } 00447 nHeaderCound++; 00448 continue; 00449 } 00450 else if(nRet == GMMP_SUCCESS) { 00451 break; 00452 } 00453 else { 00454 return nRet; 00455 } 00456 } 00457 00458 GMMP_Recv (pstGMMPHeader, *pBody); 00459 00460 return nRet; 00461 } 00462 00463 long Char2int(void* pBuffer, int nSize) 00464 { 00465 if(nSize != sizeof(long)) { 00466 return 0; 00467 } 00468 00469 long nInt = 0; 00470 memcpy(&nInt, pBuffer, sizeof(long)); 00471 return btoli(nInt); 00472 } 00473 00474 int Char2short(void* pBuffer, short nSize) 00475 { 00476 if(nSize != sizeof(short)) { 00477 return 0; 00478 } 00479 00480 short nShort = 0; 00481 memcpy(&nShort, pBuffer, sizeof(short)); 00482 return btols(nShort); 00483 } 00484 00485 void InitMemory() 00486 { 00487 memset(g_szDomainCode, 0 , sizeof(g_szDomainCode)); //patch 00488 memset(g_szAuthID, 0 , sizeof(g_szAuthID)); 00489 memset(g_szAuthKey, 0 , sizeof(g_szAuthKey)); 00490 memset(g_szGWID, 0 , sizeof(g_szGWID)); 00491 00492 g_nServerPort = 0; 00493 00494 g_CallFunctionRegRecv = NULL; 00495 } 00496 00497 #endif
Generated on Wed Jul 13 2022 20:31:57 by
1.7.2
