Eric Jung / GMMP_mbed_Ethernet_Reinit

Dependents:   ThingPlug_Ethernet_Example

Fork of GMMP_mbed by Eric Jung

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 // 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     //#include "EthernetInterface.h" // WIZnet Ethernet library replacement needed (W5100/W5200/W5500 -> W5500/W7500)
00058         
00059     #if defined(TARGET_LPC1768) 
00060         SPI spi(p5, p6, p7); // mosi, miso, sclk
00061         WIZnetInterface ethernet(&spi, p8, p11);
00062     #elif defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_L152RE)     
00063         SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk                  
00064         WIZnetInterface ethernet(&spi, PB_6, PA_9);//scs(PB_6), nRESET(PA_9); // reset pin is dummy, don't affect any pin of WIZ550io                                   
00065     #else
00066         // Ethernet interface initialization code for additional platforms will be added later.
00067     #endif  
00068     #endif//USE_WIZNET_W5500
00069 #elif defined(TARGET_WIZwiki_W7500) // WIZnet Ethernet library replacement needed (W5100/W5200/W5500 -> W5500/W7500)
00070     #include "EthernetInterface.h" 
00071     EthernetInterface ethernet; 
00072 #endif
00073 
00074 #include "NTPClient.h"
00075 
00076 #include "GMMP.h"
00077 
00078 byte g_serverIp[LEN_IP] ; ///< 서버 IP 정보를 저장한다.
00079 int  g_nServerPort = 0; ///<  서버 Port 정보를 저장한다.
00080 
00081 char g_szAuthID[LEN_AUTH_ID];///< OMP Portal을 통해 사전에 등록된 M2M GW 의 Serial Number 저장 변수, 자동화를 위해 사용된다.
00082 
00083 char g_szAuthKey[LEN_AUTH_KEY]; ///< 등록 절차 시 OMP에서 할당 받은 AuthKey 저장 변수, 자동화를 위해 사용된다.
00084 
00085 char g_szDomainCode[LEN_DOMAIN_CODE]; ///< OMP Portal을 통해 사전에 등록된 서비스 영역별 구분 코드 저장 변수, 자동화를 위해 사용된다.
00086 
00087 char g_szGWID[LEN_GW_ID]; ///< OMP에서 할당 받은 Gateway의 ID 문자열 저장 변수, 자동화를 위해 사용된다.
00088 
00089 Client *g_pClient = NULL;
00090 
00091 /**
00092  * @brief OMP서버로 부터 수신한 패킷을 제공할 콜백함수 포인트 구조체
00093  * @param pstGMMPHeader GMMP Header의 구조체 포인트
00094  * @param pstBody GMMP Body의 구조체 포인트
00095  * @return 성공:0, 실패: 1이상 , 에러코드 참조
00096  */
00097 typedef int (*callback_Reg)(GMMPHeader* pstGMMPHeader, void* pstBody);
00098 
00099 callback_Reg g_CallFunctionRegRecv = NULL; ///< OMP서버로 부터 수신한 패킷을 제공할 콜백함수 포인트 변수.
00100 //callback_Reg g_CallHeartbeatRegRecv = NULL; ///< OMP서버로 부터 수신한 패킷 중 Heartbeat 패킷을 제공할 콜백함수 포인트 변수. (TCP Always On mode에서만 사용된다.)
00101 
00102 void Uninitialize()
00103 {
00104     InitMemory();
00105     CloseSocket();
00106 }
00107 
00108 int Initialize(byte* serverIp,
00109         const int nPort,
00110         const char* pszDomainCode,
00111         const char* pszGWAuthID,
00112         byte* mac)
00113 {
00114   debugln("Initialize()");
00115 
00116     InitMemory();
00117 
00118     //delay(2000);
00119     //NTPClient ntp;
00120     
00121     infoln("Getting IP...");
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         #if defined(TARGET_NUCLEO_F411RE) // For SPI Test
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         #endif
00160             
00161     printf("input MAC Address is %02x:%02x:%02x:%02x:%02x:%02x\r\n",
00162         mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
00163     
00164     int ret = ethernet.init(mac);
00165     //printf("SPI Initialized \r\n");
00166     //wait(1); // 1 second for stable state
00167     
00168     printf("W5500 Networking Started \r\n");
00169     //wait(1); // 1 second for stable state
00170 
00171     if (!ret) {        
00172         ret = ethernet.connect();    
00173         if (!ret) {
00174             printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
00175             printf("IP: %s, MASK: %s, GW: %s\r\n",
00176                       ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
00177         } else {
00178             printf("Error ethernet.connect() - ret = %d\r\n", ret);
00179             //exit(0);
00180             
00181             // ## 20150812 Eric added, Ethernet interface re-initialize code
00182             ///////////////////////////////////////////////////////////////////////////
00183             do{             
00184                 //printf("Ethernet Interface Re-initialize\r\n");               
00185                 #if defined(TARGET_LPC1768) 
00186                 SPI spi(p5, p6, p7); // mosi, miso, sclk        
00187                 #elif defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_L152RE)
00188                 SPI spi(PA_7, PA_6, PA_5);
00189                 #endif
00190                 //wait(0.5);
00191                 
00192                 ethernet.init(mac);
00193                 ret = ethernet.connect();
00194                 printf("Re-initialized, MAC: %s\r\n", ethernet.getMACAddress());            
00195                 printf("IP: %s, MASK: %s, GW: %s\r\n",
00196                           ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
00197             } while(ret);
00198             ///////////////////////////////////////////////////////////////////////////
00199         }
00200     } else {
00201         printf("Error ethernet.init() - ret = %d\r\n", ret);
00202         //exit(0);
00203     }
00204 
00205     #endif//USE_WIZNET_W5500
00206     
00207     NTPClient ntp;
00208     printf("Trying to update time...\r\n");
00209     //if (ntp.setTime("0.pool.ntp.org") == 0)
00210 //  if (ntp.setTime("time-a.timefreq.bldrdoc.gov") == 0)
00211 //  if (ntp.setTime("kr.pool.ntp.org") == 0)
00212 //  if (ntp.setTime("112.220.115.166") == 0)
00213     if (ntp.setTime("211.233.40.78") == 0) //MURATA UDP does not support DNS resolver...
00214     {
00215       printf("Set time successfully\r\n");
00216       time_t ctTime;
00217       ctTime = time(NULL);
00218 //    printf("Time is set to (UTC): %s\r\n", ctime(&ctTime));
00219       ctTime += 32400; // GMT+9/Seoul
00220       printf("Time is set to (GMT+9): %s\r\n", ctime(&ctTime));
00221     }
00222     else
00223     {
00224       printf("Error\r\n");
00225     } 
00226         
00227     g_pClient = new Client; //lesmin
00228 
00229     if(SetServerInfo(serverIp, nPort, pszGWAuthID, pszDomainCode) != 0)
00230     {
00231         return LIB_PARAM_ERROR;
00232     }   
00233 
00234     SetTID(0);
00235 
00236     return GMMP_SUCCESS;
00237 }
00238 
00239 void SetAuthKey(const char* pszAuthKey)
00240 {
00241     memcpy(g_szAuthKey, pszAuthKey, strlen(pszAuthKey));
00242 }
00243 
00244 void SetGWID(const char* pszGWID)
00245 {
00246     if(pszGWID != NULL)
00247     {
00248         memcpy(g_szGWID, pszGWID, strlen(pszGWID));
00249     }
00250 }
00251 
00252 char* GetGWID()
00253 {
00254     return g_szGWID;
00255 }
00256 
00257 int SetServerInfo(byte* serverIp, int nPort, const char* pszAuthID, const char* pszDoamainCode)
00258 {
00259   /*
00260     if(pszAuthID == NULL || strlen(pszAuthID) > LEN_AUTH_ID
00261             || pszDoamainCode == NULL || strlen(pszDoamainCode) > LEN_DOMAIN_CODE)
00262     {
00263         return LIB_PARAM_ERROR;
00264     }
00265   */
00266 
00267     memcpy(g_serverIp, serverIp, LEN_IP);
00268     memcpy(g_szAuthID, pszAuthID, strlen(pszAuthID));
00269     memcpy(g_szDomainCode, pszDoamainCode, strlen(pszDoamainCode));
00270 
00271     g_nServerPort = nPort;
00272 
00273     int nRet = SetIntiSocket();
00274     if(nRet != GMMP_SUCCESS)
00275     {
00276         return nRet;
00277     }
00278 
00279     return GMMP_SUCCESS;
00280 }
00281 
00282 void SetCallFunction(int (* pCallFunctionName)(GMMPHeader* pstGMMPHeader, void* pstBody))
00283 {
00284     g_CallFunctionRegRecv = (callback_Reg)pCallFunctionName;
00285 }
00286 
00287 int GO_Reg(const char* pszGWID,
00288         const char* pszManufactureID)
00289 {  
00290   //CloseSocket();
00291   //SetIntiSocket();
00292   
00293   debugln("GO_Reg()");
00294     SetTID(GetTID()+1);
00295     
00296     int nRet = GMMP_SetReg(g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszManufactureID);
00297   debugln("GO_SetReg() Done");
00298 
00299     return nRet;
00300 }
00301 
00302 int GO_DeReg(const char* pszGWID, const char* pszDeviceID)
00303 {
00304     SetTID(GetTID()+1);
00305 
00306     int nRet = GMMP_SetDeReg(g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszDeviceID);
00307 
00308     return nRet;
00309 }
00310 
00311 int GO_Profile(const char* pszGWID,
00312         const char* pszDeviceID,
00313         const long nTID /*=0*/)
00314 {
00315     if(nTID == 0) {
00316         SetTID(GetTID()+1);
00317     } else {
00318         SetTID(nTID);
00319     }
00320 
00321     int nRet = GMMP_SetProfile(g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszDeviceID);
00322 
00323   return nRet;
00324 }
00325 
00326 int GO_Delivery(const char* pszGWID,
00327         const char* pszDeviceID,
00328         const char cReportType,
00329         const char cMediaType,
00330         const char* pszMessageBody)
00331 {
00332     SetTID(GetTID()+1);
00333 
00334     int nRet = GMMP_SetDelivery(g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszDeviceID, cReportType, cMediaType, pszMessageBody, 1, 1);
00335 
00336     return nRet;
00337 }
00338 
00339 int GO_Control(const char* pszGWID,
00340         const char* pszDeviceID,
00341         long nTID,
00342         const char cControlType,
00343         const char cResultCode)
00344 {
00345     SetTID(nTID);
00346 
00347     return GMMP_SetControl(g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszDeviceID, cControlType, cResultCode);
00348 }
00349 
00350 int GO_Notifi(const char* pszGWID,
00351         const char* pszDeviceID,
00352         const char cControlType,
00353         const char cResultCode,
00354         const char* pszMessageBody,
00355         const int nMessageBodySize)
00356 {
00357     int nRet =  GMMP_SetNotifi(g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID, pszDeviceID, cControlType, cResultCode, pszMessageBody, nMessageBodySize);
00358 
00359     return nRet;
00360 }
00361 
00362 int GO_HB(const char* pszGWID)
00363 {
00364     int nRet =  GMMP_SetHB(g_szAuthID, g_szAuthKey, g_szDomainCode, pszGWID);
00365 
00366     return nRet;
00367 }
00368 
00369 int OG_Reg_Recv(GMMPHeader* pstGMMPHeader, stGwRegistrationRspHdr* pstGWBody, stDeviceRegistrationRspHdr* pstDeviceBody)
00370 {
00371     int nRet = 0;
00372 
00373     if(g_CallFunctionRegRecv != NULL)
00374     {
00375         if(pstGWBody != NULL)
00376         {
00377             nRet= (*g_CallFunctionRegRecv)(pstGMMPHeader, pstGWBody);
00378 
00379             if(nRet != GMMP_SUCCESS)
00380             {
00381                 return nRet;
00382             }
00383         }
00384         else
00385         {
00386             nRet = (*g_CallFunctionRegRecv)(pstGMMPHeader, pstDeviceBody);
00387 
00388             if(nRet != GMMP_SUCCESS)
00389             {
00390                 return nRet;
00391             }
00392         }
00393     }
00394 
00395     return GMMP_SUCCESS;
00396 }
00397 
00398 int OG_DeReg_Recv(GMMPHeader* pstGMMPHeader, stGwDeRegistrationRspHdr* pstGWBody, stDeviceDeRegistrationRspHdr* pstDeviceBody)
00399 {
00400     if(g_CallFunctionRegRecv != NULL)
00401     {
00402         if(pstGWBody != NULL)
00403         {
00404             return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstGWBody);
00405         }
00406         else
00407         {
00408             return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstDeviceBody);
00409         }
00410     }
00411 
00412     return GMMP_SUCCESS;
00413 }
00414 
00415 int OG_Profile_Recv(GMMPHeader* pstGMMPHeader, stProfileRspHdr* 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_Delivery_Recv(GMMPHeader* pstGMMPHeader, stPacketDeliveryRspHdr* pstBody)
00426 {
00427     if(g_CallFunctionRegRecv != NULL)
00428     {
00429         return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstBody);
00430     }
00431 
00432     return GMMP_SUCCESS;
00433 }
00434 
00435 int OG_Ctrl_Recv(GMMPHeader* pstGMMPHeader, stControlReqHdr* pstBody)
00436 {
00437     if(g_CallFunctionRegRecv != NULL)
00438     {
00439         return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstBody);
00440     }
00441 
00442     return GMMP_SUCCESS;
00443 }
00444 
00445 int OG_HB_Recv(GMMPHeader* pstGMMPHeader, stHeartBeatMsgRspHdr* pstBody)
00446 {
00447     if(g_CallFunctionRegRecv != NULL)
00448     {
00449         return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstBody);
00450     }
00451 
00452     return GMMP_SUCCESS;
00453 }
00454 
00455 int OG_Notifi_Recv(GMMPHeader* pstGMMPHeader, stNotificationRspHdr* pstBody)
00456 {
00457     if(g_CallFunctionRegRecv != NULL)
00458     {
00459         return (*g_CallFunctionRegRecv)(pstGMMPHeader, pstBody);
00460     }
00461 
00462     return GMMP_SUCCESS;
00463 }
00464 
00465 int GetReadData(GMMPHeader* pstGMMPHeader, void** pBody)
00466 {
00467     int nHeaderCound = 0;
00468     int nRet = 0;
00469   
00470     while(true)
00471     {
00472         nRet = GMMP_Read(pstGMMPHeader, pBody);
00473         if(nRet == GMMP_HEADER_SIZE_ERROR) {
00474             if(nHeaderCound > 3) {
00475                 break;
00476             }
00477             nHeaderCound++;
00478             continue;
00479         }
00480         else if(nRet == GMMP_SUCCESS) {
00481             break;
00482         }
00483         else {
00484             return nRet;
00485         }
00486     }
00487 
00488     GMMP_Recv(pstGMMPHeader, *pBody);
00489 
00490     return nRet;
00491 }
00492 
00493 long Char2int(void* pBuffer, int nSize)
00494 {
00495     if(nSize != sizeof(long)) {
00496         return 0;
00497     }
00498 
00499     long nInt = 0;
00500     memcpy(&nInt, pBuffer, sizeof(long));
00501     return btoli(nInt);
00502 }
00503 
00504 int Char2short(void* pBuffer, short nSize)
00505 {
00506     if(nSize != sizeof(short)) {
00507         return 0;
00508     }
00509 
00510     short nShort = 0;
00511     memcpy(&nShort, pBuffer, sizeof(short));
00512     return btols(nShort);
00513 }
00514 
00515 void InitMemory()
00516 {
00517     memset(g_szDomainCode, 0 , sizeof(g_szAuthID));
00518     memset(g_szAuthID, 0 , sizeof(g_szAuthID));
00519     memset(g_szAuthKey, 0 , sizeof(g_szAuthKey));
00520     memset(g_szGWID, 0 , sizeof(g_szGWID));
00521 
00522     g_nServerPort = 0;
00523 
00524     g_CallFunctionRegRecv = NULL;
00525 }
00526 
00527 #endif