Migrating from C style to C++ style of coding in nest plugin
authorvijendrx <vijendrax.kumar@intel.com>
Wed, 1 Mar 2017 10:42:36 +0000 (16:12 +0530)
committerTodd Malsbary <todd.malsbary@intel.com>
Thu, 27 Apr 2017 16:55:54 +0000 (16:55 +0000)
Replacing fgets/fputs with fstream equivalents and using std::string
in nest_plugin, wherever applicable.

Change-Id: I5c54556876cc09d1816672a1c30d641856b1c1c7
Signed-off-by: vijendrx <vijendrax.kumar@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/17579
Reviewed-by: Joseph Morrow <joseph.l.morrow@intel.com>
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Todd Malsbary <todd.malsbary@intel.com>
(cherry picked from commit f3016c9416863700ab1c161b104ac46005d07296)
Reviewed-on: https://gerrit.iotivity.org/gerrit/19169
Reviewed-by: Uze Choi <uzchoi@samsung.com>
bridging/plugins/nest_plugin/SConscript
bridging/plugins/nest_plugin/nestResource.cpp [moved from bridging/plugins/nest_plugin/nest_resource.cpp with 92% similarity]
bridging/plugins/nest_plugin/nest_objects/nest.cpp
bridging/plugins/nest_plugin/nest_objects/nest.h
bridging/plugins/nest_plugin/nest_objects/nestDefs.h [moved from bridging/plugins/nest_plugin/nest_objects/nest_defs.h with 100% similarity]
bridging/plugins/nest_plugin/nest_objects/nestThermostat.cpp [moved from bridging/plugins/nest_plugin/nest_objects/nest_thermostat.cpp with 98% similarity]
bridging/plugins/nest_plugin/nest_objects/nestThermostat.h [moved from bridging/plugins/nest_plugin/nest_objects/nest_thermostat.h with 98% similarity]

index 92a2cdd..5a60e7c 100644 (file)
@@ -88,9 +88,9 @@ nest_env.PrependUnique(LIBS = ['m',
 # Source files and Target(s)
 ######################################################################
 nest_src = [
-         os.path.join(bridging_path, 'plugins', 'nest_plugin', 'nest_resource.cpp'),
+         os.path.join(bridging_path, 'plugins', 'nest_plugin', 'nestResource.cpp'),
          os.path.join(bridging_path, 'plugins', 'nest_plugin', 'nest_objects', 'nest.cpp'),
-         os.path.join(bridging_path, 'plugins', 'nest_plugin', 'nest_objects', 'nest_thermostat.cpp'),
+         os.path.join(bridging_path, 'plugins', 'nest_plugin', 'nest_objects', 'nestThermostat.cpp'),
          ]
 
 nest_env.AppendUnique(NEST_SRC = nest_src)
@@ -102,65 +102,48 @@ OCEntityHandlerResult resourceEntityHandlerCb(OCEntityHandlerFlag flag,
 MPMResult loadNestAuthConfig(std::string filename, std::string &pincode,
                              std::string &accessToken)
 {
-    FILE *fp = fopen(filename.c_str(), "r");
-    char str[MPM_MAX_LENGTH_1024];
     MPMResult result = MPM_RESULT_INTERNAL_ERROR;
+    ifstream tokenFile(filename);
 
-    if (NULL != fp)
+    if (!tokenFile.is_open())
     {
-        if (fgets(str, MPM_MAX_LENGTH_1024, fp) == NULL)
+        OIC_LOG_V(ERROR, TAG, "Could not open %s.\n", filename.c_str());
+        return result;
+    }
+    else
+    {
+        if (!getline(tokenFile, pincode))
         {
-            OIC_LOG_V(ERROR, TAG, "fgets failed on %s", filename.c_str());
+            OIC_LOG(ERROR, TAG, "Could not read pincode");
             goto CLEANUP;
         }
-        // @todo the logic below needs to be fixed.
-        // The intention here was to remove the last character which was new line.
-        // Current logic doesn't cover edge case like empty string and etc.
-        str[strlen(str) - 1] = '\0';
-        pincode = std::string(str);
-
-        if (fgets(str, MPM_MAX_LENGTH_1024, fp) == NULL)
+        if (!getline(tokenFile, accessToken))
         {
-            OIC_LOG_V(ERROR, TAG, "fgets failed on %s", filename.c_str());
+            OIC_LOG(ERROR, TAG, "Could not read access token");
             goto CLEANUP;
         }
-        str[strlen(str) - 1] = '\0';
-        accessToken = std::string(str);
-
-        if (fgets(str, MPM_MAX_LENGTH_1024, fp) == NULL)
+        if (!getline(tokenFile, nest_client_id))
         {
-            OIC_LOG_V(ERROR, TAG, "fgets failed on %s", filename.c_str());
+            OIC_LOG(ERROR, TAG, "Could not read client id");
             goto CLEANUP;
         }
-        str[strlen(str) - 1] = '\0';
-        nest_client_id = std::string(str);
-
-        if (fgets(str, MPM_MAX_LENGTH_1024, fp) == NULL)
+        if (!getline(tokenFile, nest_client_secret))
         {
-            OIC_LOG_V(ERROR, TAG, "fgets failed on %s", filename.c_str());
+            OIC_LOG(ERROR, TAG, "Could not read client secret");
             goto CLEANUP;
         }
-        str[strlen(str) - 1] = '\0';
-        nest_client_secret = std::string(str);
 
         result = MPM_RESULT_OK;
     }
-    else
-    {
-        OIC_LOG_V(ERROR, TAG, "Could not open %s.\n", filename.c_str());
-    }
 
 CLEANUP:
-    if (fp != NULL)
-    {
-        fclose(fp);
-    }
+    tokenFile.close();
     return result;
 }
 
 Nest::ACCESS_TOKEN populateAccessTokenFromFile(std::string accessToken)
 {
-    Nest::ACCESS_TOKEN aTok(accessToken.c_str());
+    Nest::ACCESS_TOKEN aTok(accessToken);
 
     return aTok;
 }
@@ -187,22 +170,20 @@ MPMResult checkValidityOfExistingToken(Nest::ACCESS_TOKEN aTok)
 
 void updateNestTokenFile(std::string filename, std::string pincode, std::string accessToken)
 {
-    FILE *fp = fopen(filename.c_str(), "w");
-    if (fp == NULL)
+    ofstream tokenFile;
+    tokenFile.open(filename.c_str());
+    if (tokenFile.is_open())
     {
-        OIC_LOG(ERROR, TAG, "Failed to open nest.cnf file");
-        return;
+        tokenFile << pincode << std::endl;
+        tokenFile << accessToken << std::endl;
+        tokenFile << nest_client_id << std::endl;
+        tokenFile << nest_client_secret << std::endl;
+        tokenFile.close();
+    }
+    else
+    {
+        OIC_LOG(ERROR, TAG, "Failed to open nest token file");
     }
-    fputs(pincode.c_str(), fp);
-    fputs("\n", fp);
-    fputs(accessToken.c_str(), fp);
-    fputs("\n", fp);
-    fputs(nest_client_id.c_str(), fp);
-    fputs("\n", fp);
-    fputs(nest_client_secret.c_str(), fp);
-    fputs("\n", fp);
-
-    fclose(fp);
 }
 
 MPMResult refreshAccessToken(std::string filename, std::string pincode)
@@ -332,7 +313,7 @@ bool isSecureEnvironmentSet()
 {
     char *non_secure_env = getenv("NONSECURE");
 
-    if (non_secure_env && (strcmp(non_secure_env, "true") == 0))
+    if (non_secure_env && !strcmp(non_secure_env, "true"))
     {
         OIC_LOG(INFO, TAG, "Creating NON SECURE resources");
         return false;
@@ -420,7 +401,7 @@ MPMResult  deleteOCFResource(const std::string &uri)
     return MPM_RESULT_OK;
 }
 
-void createPayloadForMetadata(MPMResourceList **list, const char *uri)
+void createPayloadForMetadata(MPMResourceList **list, const std::string &uri)
 {
     MPMResourceList *tempPtr = NULL;
     tempPtr = (MPMResourceList *) OICCalloc(1, sizeof(MPMResourceList));
@@ -431,7 +412,7 @@ void createPayloadForMetadata(MPMResourceList **list, const char *uri)
     }
 
     OICStrcpy(tempPtr->rt, MPM_MAX_LENGTH_64, NEST_THERMOSTAT_RT.c_str());
-    OICStrcpy(tempPtr->href, MPM_MAX_URI_LEN, uri);
+    OICStrcpy(tempPtr->href, MPM_MAX_URI_LEN, uri.c_str());
     OICStrcpy(tempPtr->interfaces, MPM_MAX_LENGTH_64, NEST_THERMOSTAT_IF.c_str());
     tempPtr->bitmap = BM;
     tempPtr->next = *list;
@@ -490,7 +471,7 @@ MPMResult pluginAdd(MPMPluginCtx *, MPMPipeMessage *message)
     createOCFResource(uri);
     addedThermostats[uri] = uriToNestThermostatMap[uri];
 
-    createPayloadForMetadata(&list, uri.c_str());
+    createPayloadForMetadata(&list, uri);
 
     NestThermostat::THERMOSTAT thermostat;
     addedThermostats[uri]->get(thermostat);
index be945bd..780d6e9 100644 (file)
@@ -23,7 +23,7 @@
 #include <iostream>
 #include "nest.h"
 #include "curlClient.h"
-#include "nest_defs.h"
+#include "nestDefs.h"
 #include "rapidjson.h"
 #include "document.h"
 #include "stringbuffer.h"
@@ -48,13 +48,13 @@ void Nest::setAccessToken(const ACCESS_TOKEN &token)
     m_accessToken = token;
 }
 
-Nest::AWAY_MODE Nest::getAwayMode(const char *value) const
+Nest::AWAY_MODE Nest::getAwayMode(const std::string &value) const
 {
-    if (!strncmp(value, NEST_HOME_TAG, strlen(NEST_HOME_TAG)))
+    if (NEST_HOME_TAG == value)
     {
         return eAWHome;
     }
-    else if (!strncmp(value, NEST_AWAY_TAG, strlen(NEST_AWAY_TAG)))
+    else if (NEST_AWAY_TAG == value)
     {
         return eAWAway;
     }
index 642d663..b0e024f 100644 (file)
@@ -27,8 +27,8 @@
 #include <stdint.h>
 #include "oic_malloc.h"
 #include "oic_string.h"
-#include "nest_defs.h"
-#include "nest_thermostat.h"
+#include "nestDefs.h"
+#include "nestThermostat.h"
 #include <string.h>
 
 using namespace std;
@@ -68,9 +68,9 @@ class Nest
                 grantTime = 0;
             }
 
-            _ACCESS_TOKEN(const char *a_token)
+            _ACCESS_TOKEN(const std::string &a_token)
             {
-                OICStrcpy(accessToken, NEST_ACCESS_TOKEN_LEN - 1, a_token);
+                OICStrcpy(accessToken, NEST_ACCESS_TOKEN_LEN - 1, a_token.c_str());
                 memset(expires, 0, NEST_ACCESS_TOKEN_EXPIRY);
                 acquiredTime = 0;
                 grantTime = 0;
@@ -166,7 +166,7 @@ class Nest
 
         MPMResult parseStructureJsonResponse(std::string &json, META_INFO &meta);
 
-        AWAY_MODE getAwayMode(const char *value) const;
+        AWAY_MODE getAwayMode(const std::string &value) const;
 
         ACCESS_TOKEN m_accessToken;
         META_INFO m_metaInfo;
@@ -20,8 +20,8 @@
 
 #include <stdio.h>
 #include <string>
-#include "nest_thermostat.h"
-#include "nest_defs.h"
+#include "nestThermostat.h"
+#include "nestDefs.h"
 #include "rapidjson.h"
 #include "document.h"
 #include "JsonHelper.h"
@@ -35,7 +35,8 @@
 using namespace OC::Bridging;
 
 NestThermostat::NestThermostat(const std::string &token, uint16_t hum, uint32_t hvac,
-                               uint16_t temp, uint32_t scale, const char *devId) : m_token(token)
+                               uint16_t temp, uint32_t scale, const std::string &devId)
+                               : m_token(token)
 {
     m_thermostat.humidity = hum;
     m_thermostat.targetTempF = temp;
@@ -38,7 +38,7 @@ class NestThermostat
         typedef std::vector<NestThermostat> devices;
 
         NestThermostat(const std::string &token, uint16_t hum, uint32_t hvac,
-                       uint16_t temp, uint32_t scale, const char *devId);
+                       uint16_t temp, uint32_t scale, const std::string &devId);
 
         NestThermostat(const std::string &token, const std::string &jsonThermostat);