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