1 //******************************************************************
3 // Copyright 2017 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
29 #include "oic_string.h"
30 #include "mpmErrorCode.h"
37 #define TAG "HUE_FILE"
40 * Parse the authorized bridge array
41 * @param[in] fileBuffer
42 * @param[in] fileBufferSize
43 * @return true if parsed else false
45 static bool parseAuthorizedBridgeArray(char *fileBuffer, uint32_t fileBufferSize);
48 * Parse each authorized bridge to get client and Mac address
50 * @return true if parsed else false
52 static bool parseAuthorizedBridge(cJSON *object);
54 std::map<std::string, std::string> file_map;
56 bool readAuthorizedBridgeFile()
58 bool parsedOk = false;
64 pFile = fopen(HUE_AUTHORIZATION_FILE, "r");
67 OIC_LOG_V(INFO, TAG, "File %s not present",
68 HUE_AUTHORIZATION_FILE );
72 OIC_LOG_V(INFO, TAG, "Reading auth file @ %s",
73 HUE_AUTHORIZATION_FILE);
75 fseek(pFile, 0, SEEK_END);
79 // allocate memory to contain the whole file:
80 buffer = (char *) malloc(sizeof(char) * (lSize + 1));
81 if ((buffer != NULL) && (lSize > 1))
83 // copy the file into the buffer:
84 fileResult = fread(buffer, 1, lSize, pFile);
85 if (fileResult == (size_t) lSize)
88 OIC_LOG_V(INFO, TAG, "Auth file contents = \n%s\n", buffer);
89 parsedOk = parseAuthorizedBridgeArray(buffer, lSize);
104 bool findAuthorizedBridge(const char *macAddrString, const char *clientID, hueFile &bridgeCtx)
106 if ((macAddrString != NULL) && (clientID == NULL))
108 if (file_map.find(macAddrString) != file_map.end())
110 std::string clientid = file_map[macAddrString];
111 OICStrcpy(bridgeCtx.clientID, MAX_STRING - 1, clientid.c_str());
115 else if ((macAddrString == NULL) && (clientID != NULL))
117 if (file_map.find(clientID) != file_map.end())
119 std::string macAddress = file_map[clientID];
120 OICStrcpy(bridgeCtx.macAddrString, MAX_STRING - 1, macAddress.c_str());
124 else if ((macAddrString != NULL) && (clientID != NULL))
126 OICStrcpy(bridgeCtx.macAddrString, MAX_STRING - 1, macAddrString);
127 OICStrcpy(bridgeCtx.clientID, MAX_STRING - 1, clientID);
132 OIC_LOG(ERROR, TAG, "Both mac and client id is NULL");
135 OIC_LOG(ERROR, TAG, "Bridge is not Authorized...........");
139 static bool parseAuthorizedBridgeArray(char *fileBuffer, uint32_t fileBufferSize)
141 bool parsedOk = false;
142 cJSON *object = NULL;
144 int32_t numBridges = 0;
147 if ((fileBuffer != NULL) && (fileBufferSize > 0))
149 array = cJSON_Parse((char *) fileBuffer);
152 numBridges = cJSON_GetArraySize(array);
154 for (index = 0; index < numBridges; index++)
156 object = cJSON_GetArrayItem(array, index);
157 parsedOk = parseAuthorizedBridge(object);
158 if (parsedOk == false)
160 OIC_LOG(ERROR, TAG, "Parsing one of the bridge lines in file failed");
167 OIC_LOG(ERROR, TAG, "array returned from call to cJSON_Parse is NULL.");
173 bool addAuthorizedBridge(const char *mac, const char *clientId)
176 if (mac == NULL || clientId == NULL)
178 OIC_LOG(ERROR, TAG, "Failed to add the bridge Details to the authorization list");
181 file_map[mac] = clientId;
186 static bool parseAuthorizedBridge(cJSON *object)
188 char *macAddrString = NULL;
189 char *clientID = NULL;
193 if (cJSON_GetObjectItem(object, "id") != NULL)
195 macAddrString = cJSON_GetObjectItem(object, "id")->valuestring;
197 if (cJSON_GetObjectItem(object, "username") != NULL)
199 clientID = cJSON_GetObjectItem(object, "username")->valuestring;
202 addAuthorizedBridge(macAddrString, clientID);
208 bool collectAuthorizedClients(const char *macAddrString, char *clientArray, uint32_t *numClients)
210 if ((macAddrString == NULL))
212 OIC_LOG_V(ERROR, TAG, "Mac id is NULL");
215 for (std::map<std::string, std::string>::iterator itr = file_map.begin(); itr != file_map.end();
218 if (strcmp(macAddrString, (*itr).first.c_str()) == 0)
220 std::string clientID = (*itr).second;
221 if (!clientID.empty())
223 OICStrcpy(clientArray, MAX_STRING, clientID.c_str());
232 void clearBridgeDetails()