Initial plugin "Hue Plugin" for bridging project.
[platform/upstream/iotivity.git] / bridging / plugins / hue_plugin / hue_file.cpp
1 //******************************************************************
2 //
3 // Copyright 2017 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 //
21
22
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <string>
28 #include <string.h>
29 #include "oic_string.h"
30 #include "mpmErrorCode.h"
31 #include "hue_file.h"
32 #include "cJSON.h"
33 #include <pthread.h>
34 #include <map>
35 #include "logger.h"
36
37 #define TAG "HUE_FILE"
38
39 /**
40  * Parse the authorized bridge array
41  * @param[in] fileBuffer
42  * @param[in] fileBufferSize
43  * @return true if parsed else false
44  */
45 static bool parseAuthorizedBridgeArray(char *fileBuffer, uint32_t fileBufferSize);
46
47 /**
48  * Parse each authorized bridge to get client and Mac address
49  * @param [in] object
50  * @return true if parsed else false
51  */
52 static bool parseAuthorizedBridge(cJSON *object);
53
54 std::map<std::string, std::string> file_map;
55
56 bool readAuthorizedBridgeFile()
57 {
58     bool parsedOk = false;
59     long lSize;
60     char *buffer = NULL;
61     size_t fileResult;
62     FILE *pFile = NULL;
63
64     pFile = fopen(HUE_AUTHORIZATION_FILE, "r");
65     if (pFile == NULL)
66     {
67         OIC_LOG_V(INFO, TAG, "File %s not present",
68                   HUE_AUTHORIZATION_FILE );
69     }
70     else
71     {
72         OIC_LOG_V(INFO, TAG, "Reading auth file @  %s",
73                   HUE_AUTHORIZATION_FILE);
74         // obtain file size:
75         fseek(pFile, 0, SEEK_END);
76         lSize = ftell(pFile);
77         rewind(pFile);
78
79         // allocate memory to contain the whole file:
80         buffer = (char *) malloc(sizeof(char) * (lSize + 1));
81         if ((buffer != NULL) && (lSize > 1))
82         {
83             // copy the file into the buffer:
84             fileResult = fread(buffer, 1, lSize, pFile);
85             if (fileResult == (size_t) lSize)
86             {
87                 buffer[lSize] = '\0';
88                 OIC_LOG_V(INFO, TAG, "Auth file contents = \n%s\n", buffer);
89                 parsedOk = parseAuthorizedBridgeArray(buffer, lSize);
90             }
91         }
92     }
93     if (NULL != buffer)
94     {
95         free(buffer);
96     }
97     if (NULL != pFile)
98     {
99         fclose(pFile);
100     }
101     return (parsedOk);
102 }
103
104 bool findAuthorizedBridge(const char *macAddrString, const char *clientID, hueFile &bridgeCtx)
105 {
106     if ((macAddrString != NULL) && (clientID == NULL))
107     {
108         if (file_map.find(macAddrString) != file_map.end())
109         {
110             std::string clientid = file_map[macAddrString];
111             OICStrcpy(bridgeCtx.clientID, MAX_STRING - 1, clientid.c_str());
112             return true;
113         }
114     }
115     else if ((macAddrString == NULL) && (clientID != NULL))
116     {
117         if (file_map.find(clientID) != file_map.end())
118         {
119             std::string macAddress = file_map[clientID];
120             OICStrcpy(bridgeCtx.macAddrString, MAX_STRING - 1, macAddress.c_str());
121             return true;
122         }
123     }
124     else if ((macAddrString != NULL) && (clientID != NULL))
125     {
126         OICStrcpy(bridgeCtx.macAddrString, MAX_STRING - 1, macAddrString);
127         OICStrcpy(bridgeCtx.clientID, MAX_STRING - 1, clientID);
128         return true;
129     }
130     else
131     {
132         OIC_LOG(ERROR, TAG, "Both mac and client id is NULL");
133         return false;
134     }
135     OIC_LOG(ERROR, TAG, "Bridge is not Authorized...........");
136     return false;
137 }
138
139 static bool parseAuthorizedBridgeArray(char *fileBuffer, uint32_t fileBufferSize)
140 {
141     bool parsedOk = false;
142     cJSON *object = NULL;
143     cJSON *array = NULL;
144     int32_t numBridges = 0;
145     int32_t index;
146
147     if ((fileBuffer != NULL) && (fileBufferSize > 0))
148     {
149         array = cJSON_Parse((char *) fileBuffer);
150         if (array != NULL)
151         {
152             numBridges = cJSON_GetArraySize(array);
153             parsedOk = true;
154             for (index = 0; index < numBridges; index++)
155             {
156                 object = cJSON_GetArrayItem(array, index);
157                 parsedOk = parseAuthorizedBridge(object);
158                 if (parsedOk == false)
159                 {
160                     OIC_LOG(ERROR, TAG, "Parsing one of the bridge lines in file failed");
161                 }
162             }
163             cJSON_Delete(array);
164         }
165         else
166         {
167             OIC_LOG(ERROR, TAG, "array returned from call to cJSON_Parse is NULL.");
168         }
169     }
170     return (parsedOk);
171 }
172
173 bool addAuthorizedBridge(const char *mac, const char *clientId)
174 {
175
176     if (mac == NULL || clientId == NULL)
177     {
178         OIC_LOG(ERROR, TAG, "Failed to add the bridge Details to the authorization list");
179         return false;
180     }
181     file_map[mac] = clientId;
182
183     return true;
184 }
185
186 static bool parseAuthorizedBridge(cJSON *object)
187 {
188     char *macAddrString = NULL;
189     char *clientID = NULL;
190
191     if (object != NULL)
192     {
193         if (cJSON_GetObjectItem(object, "id") != NULL)
194         {
195             macAddrString = cJSON_GetObjectItem(object, "id")->valuestring;
196         }
197         if (cJSON_GetObjectItem(object, "username") != NULL)
198         {
199             clientID = cJSON_GetObjectItem(object, "username")->valuestring;
200         }
201
202         addAuthorizedBridge(macAddrString, clientID);
203
204     }
205     return (true);
206 }
207
208 bool collectAuthorizedClients(const char *macAddrString, char *clientArray, uint32_t *numClients)
209 {
210     if ((macAddrString == NULL))
211     {
212         OIC_LOG_V(ERROR, TAG, "Mac id is NULL");
213         return false;
214     }
215     for (std::map<std::string, std::string>::iterator  itr = file_map.begin(); itr != file_map.end();
216          ++itr)
217     {
218         if (strcmp(macAddrString, (*itr).first.c_str()) == 0)
219         {
220             std::string clientID = (*itr).second;
221             if (!clientID.empty())
222             {
223                 OICStrcpy(clientArray, MAX_STRING, clientID.c_str());
224             }
225             *numClients = 1;
226             return true;
227         }
228     }
229     return (false);
230 }
231
232 void clearBridgeDetails()
233 {
234     file_map.clear();
235 }
236