SKTelecom_ThingPlug / GMMP_mbed

Dependents:   ThingPlug_WiFi_Example_2 ThingPlug_Ethernet_Example ThingPlug_Ethernet_Example_temp_V2

Fork of GMMP_2 by Sangmin Lee

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GMMP.cpp Source File

GMMP.cpp

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