Fix a condition to filter out a wrong cred ID
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / inc / ESRichCommon.h
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics 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 #ifndef ES_COMMON_RICH_H_
22 #define ES_COMMON_RICH_H_
23
24 #include <iostream>
25 #include <string>
26 #ifndef WITH_ARDUINO
27 #include <memory>
28 #include <vector>
29 #endif
30
31 #include "OCPlatform.h"
32 #include "ocstack.h"
33 #include "octypes.h"
34
35 #include "escommon.h"
36
37 using namespace OC;
38 using namespace std;
39
40 #ifndef WITH_ARDUINO
41 namespace OIC
42 {
43     namespace Service
44     {
45         /**
46          * @brief Properties of provisioning resource. It includes a provisioning status and last
47          *        error code.
48          */
49         class EnrolleeStatus
50         {
51         public:
52
53             /**
54              * Constructor
55              */
56             EnrolleeStatus(const OCRepresentation& rep)
57             {
58                 m_rep = rep;
59             }
60
61             EnrolleeStatus(const EnrolleeStatus& enrolleeStatus) :
62                 m_rep(enrolleeStatus.getRepresentation())
63             {
64             }
65
66             EnrolleeStatus(const EnrolleeStatus&& enrolleeStatus) :
67                 m_rep(std::move(enrolleeStatus.getRepresentation()))
68             {
69             }
70
71             /**
72              * Get a provisioning status property of Enrollee.
73              *
74              * @return a provisioning status property of Enrollee
75              */
76             ProvStatus getProvStatus()
77             {
78                 if(m_rep.hasAttribute(OC_RSRVD_ES_PROVSTATUS))
79                 {
80                     return static_cast<ProvStatus>(
81                                         m_rep.getValue<int>(OC_RSRVD_ES_PROVSTATUS));
82                 }
83                 return ES_STATE_INIT;
84             }
85
86             /**
87              * Get a last error code property of Enrollee.
88              *
89              * @return a last error code property of Enrollee.
90              */
91             ESErrorCode getLastErrCode()
92             {
93                 if(m_rep.hasAttribute(OC_RSRVD_ES_LAST_ERRORCODE))
94                 {
95                     return static_cast<ESErrorCode>(
96                                         m_rep.getValue<int>(OC_RSRVD_ES_LAST_ERRORCODE));
97                 }
98                 return ES_ERRCODE_NO_ERROR;
99             }
100
101             /**
102              * Get OCRepresentation object
103              *
104              * @return OCRepresentation object
105              */
106             const OCRepresentation& getRepresentation() const
107             {
108                 return m_rep;
109             }
110         protected:
111             OCRepresentation m_rep;
112         };
113
114         /**
115          * @brief Data class stored for Cloud server property provisioning
116          */
117         class CloudProp
118         {
119         public:
120
121             /**
122              * Constructor
123              */
124             CloudProp()
125             {
126                 m_cloudID = "";
127                 m_credID = 0;
128             }
129
130             CloudProp(const CloudProp& cloudProp) :
131                                             m_rep(cloudProp.toOCRepresentation()),
132                                             m_cloudID(cloudProp.getCloudID()),
133                                             m_credID(cloudProp.getCredID())
134             {
135             }
136
137             CloudProp(const CloudProp&& cloudProp) :
138                                             m_rep(std::move(cloudProp.toOCRepresentation())),
139                                             m_cloudID(cloudProp.getCloudID()),
140                                             m_credID(cloudProp.getCredID())
141             {
142             }
143
144             /**
145              * Constructor with OCRepresentation object. This is used for JNI communication.
146              */
147             CloudProp(const OCRepresentation &rep)
148             {
149                 m_rep = rep;
150                 m_cloudID = "";
151                 m_credID = 0;
152             }
153
154             /**
155              * Set CloudServer resource properties to be delivered to Enrollee
156              *
157              * @param authCode  Auth code issued by OAuth2.0-compatible account server
158              * @param authProvider Auth provider ID
159              * @param ciServer Cloud interface server URL which an Enrollee is going to registered
160              */
161             void setCloudProp(string authCode, string authProvider, string ciServer)
162             {
163                 m_rep.setValue(OC_RSRVD_ES_AUTHCODE, authCode);
164                 m_rep.setValue(OC_RSRVD_ES_AUTHPROVIDER, authProvider);
165                 m_rep.setValue(OC_RSRVD_ES_CISERVER, ciServer);
166             }
167
168             /**
169              * Set CloudServer's UUID
170              *
171              * @param cloudID Cloud Interface server's UUID
172              */
173             void setCloudID(string cloudID)
174             {
175                 m_cloudID = cloudID;
176             }
177
178             /**
179              * Set CloudServer's credential ID of certificate
180              *
181              * @param credID Cloud Interface server's credential ID of certificate
182              */
183             void setCredID(int credID)
184             {
185                 m_credID = credID;
186             }
187
188             /**
189              * Get an auth code to be delivered.
190              *
191              * @return an auth code to be delivered.
192              */
193             std::string getAuthCode() const
194             {
195                 if(m_rep.hasAttribute(OC_RSRVD_ES_AUTHCODE))
196                 {
197                     return m_rep.getValue<std::string>(OC_RSRVD_ES_AUTHCODE);
198                 }
199                 return std::string("");
200             }
201
202             /**
203              * Get an auth provider which issued an auth code
204              *
205              * @return an auth provider which issued an auth code
206              */
207             std::string getAuthProvider() const
208             {
209                 if(m_rep.hasAttribute(OC_RSRVD_ES_AUTHPROVIDER))
210                 {
211                     return m_rep.getValue<std::string>(OC_RSRVD_ES_AUTHPROVIDER);
212                 }
213                 return std::string("");
214             }
215
216             /**
217              * Get a CI server to be delivered
218              *
219              * @return a CI server to be delivered
220              */
221             std::string getCiServer() const
222             {
223                 if(m_rep.hasAttribute(OC_RSRVD_ES_CISERVER))
224                 {
225                     return m_rep.getValue<std::string>(OC_RSRVD_ES_CISERVER);
226                 }
227                 return std::string("");
228             }
229
230             /**
231              * Get a CI server's Uuid to be delivered
232              *
233              * @return a CI server's Uuid to be delivered
234              */
235             std::string getCloudID() const
236             {
237                 return m_cloudID;
238             }
239
240             /**
241              * Get a CI server's credential ID of certificate
242              *
243              * @return a CI server's credential ID of certificated
244              */
245             int getCredID() const
246             {
247                 return m_credID;
248             }
249
250             /**
251              * Get OCRepresentation object
252              *
253              * @return OCRepresentation object
254              */
255             const OCRepresentation &toOCRepresentation() const
256             {
257                 return m_rep;
258             }
259         protected:
260             OCRepresentation m_rep;
261             std::string m_cloudID;
262             int m_credID;
263         };
264
265         /**
266          * @brief Data class stored for Device property provisioning which includes a WiFi
267          *        and device configuration provisioning
268          */
269         class DeviceProp
270         {
271         public:
272
273             /**
274              * Constructor
275              */
276             DeviceProp()
277             {
278             }
279
280             DeviceProp(const DeviceProp& deviceProp) :
281                 m_rep(deviceProp.toOCRepresentation())
282             {
283             }
284
285             DeviceProp(const DeviceProp&& deviceProp) :
286                 m_rep(std::move(deviceProp.toOCRepresentation()))
287             {
288             }
289
290             /**
291              * Constructor with OCRepresentation object. This is used for JNI communication.
292              */
293             DeviceProp(const OCRepresentation &rep)
294             {
295                 m_rep = rep;
296             }
297
298             /**
299              * Set WiFi resource properties to be delivered to Enrollee
300              *
301              * @param ssid Ssid of the Enroller
302              * @param pwd Pwd of the Enrolle
303              * @param authtype Auth type of the Enroller
304              * @param enctype Encryption type of the Enroller
305              *
306              * @see WIFI_AUTHTYPE
307              * @see WIFI_ENCTYPE
308              */
309             void setWiFiProp(string ssid, string pwd, WIFI_AUTHTYPE authtype, WIFI_ENCTYPE enctype)
310             {
311                 m_rep.setValue(OC_RSRVD_ES_SSID, ssid);
312                 m_rep.setValue(OC_RSRVD_ES_CRED, pwd);
313                 m_rep.setValue(OC_RSRVD_ES_AUTHTYPE, authtype);
314                 m_rep.setValue(OC_RSRVD_ES_ENCTYPE, enctype);
315             }
316
317             /**
318              * Set DevConf resource properties to be delivered to Enrollee
319              *
320              * @param language IETF language tag using ISO 639X
321              * @param country ISO Country Code (ISO 3166-1 Alpha-2)
322              */
323             void setDevConfProp(string language, string country, string location)
324             {
325                 m_rep.setValue(OC_RSRVD_ES_LANGUAGE, language);
326                 m_rep.setValue(OC_RSRVD_ES_COUNTRY, country);
327                 m_rep.setValue(OC_RSRVD_ES_LOCATION, location);
328             }
329
330             /**
331              * Get a SSID of Enroller
332              *
333              * @return a SSID of enroller
334              */
335             std::string getSsid() const
336             {
337                 if(m_rep.hasAttribute(OC_RSRVD_ES_SSID))
338                 {
339                     return m_rep.getValue<std::string>(OC_RSRVD_ES_SSID);
340                 }
341                 return std::string("");
342             }
343
344             /**
345              * Get a password of Enroller
346              *
347              * @return a password of enroller
348              */
349             std::string getPassword() const
350             {
351                 if(m_rep.hasAttribute(OC_RSRVD_ES_CRED))
352                 {
353                     return m_rep.getValue<std::string>(OC_RSRVD_ES_CRED);
354                 }
355                 return std::string("");
356             }
357
358             /**
359              * Get an auth type of Enroller
360              *
361              * @return an auth type of enroller
362              *
363              * @see WIFI_AUTHTYPE
364              */
365             WIFI_AUTHTYPE getAuthType() const
366             {
367                 if(m_rep.hasAttribute(OC_RSRVD_ES_AUTHTYPE))
368                 {
369                     return static_cast<WIFI_AUTHTYPE>(m_rep.getValue<int>(OC_RSRVD_ES_AUTHTYPE));
370                 }
371                 return NONE_AUTH;
372             }
373
374             /**
375              * Get an encryption type of Enroller
376              *
377              * @return an encryption type of enroller
378              *
379              * @see WIFI_ENCTYPE
380              */
381             WIFI_ENCTYPE getEncType() const
382             {
383                 if(m_rep.hasAttribute(OC_RSRVD_ES_ENCTYPE))
384                 {
385                     return static_cast<WIFI_ENCTYPE>(m_rep.getValue<int>(OC_RSRVD_ES_ENCTYPE));
386                 }
387                 return NONE_ENC;
388             }
389
390             /**
391              * Get a language to be set. A language is expressed in IETF language tag
392              * using ISO 639X.
393              *
394              * @return a language to be set
395              */
396             std::string getLanguage() const
397             {
398                 if(m_rep.hasAttribute(OC_RSRVD_ES_LANGUAGE))
399                 {
400                     return m_rep.getValue<std::string>(OC_RSRVD_ES_LANGUAGE);
401                 }
402                 return std::string("");
403             }
404
405             /**
406              * Get a country to be set. A country is expressed in ISO Country Code
407              * (ISO 3166-1 Alpha-2)
408              *
409              * @return a country to be set
410              */
411             std::string getCountry() const
412             {
413                 if(m_rep.hasAttribute(OC_RSRVD_ES_COUNTRY))
414                 {
415                     return m_rep.getValue<std::string>(OC_RSRVD_ES_COUNTRY);
416                 }
417                 return std::string("");
418             }
419
420             /**
421              * Get a location to be set. A location is GPS information
422              *
423              * @return a country to be set
424              */
425             std::string getLocation() const
426             {
427                 if(m_rep.hasAttribute(OC_RSRVD_ES_LOCATION))
428                 {
429                     return m_rep.getValue<std::string>(OC_RSRVD_ES_LOCATION);
430                 }
431                 return std::string("");
432             }
433
434             /**
435              * Get OCRepresentation object
436              *
437              * @return OCRepresentation object
438              */
439             const OCRepresentation &toOCRepresentation() const
440             {
441                 return m_rep;
442             }
443
444         protected:
445             OCRepresentation m_rep;
446         };
447
448         /**
449          * Security Provisioning Status
450          */
451         class SecProvisioningStatus
452         {
453         public:
454             SecProvisioningStatus(string deviceUUID, ESResult result) :
455                 m_devUUID(deviceUUID), m_result(result)
456             {
457             }
458
459             const string getDeviceUUID()
460             {
461                 return m_devUUID;
462             }
463
464             ESResult getESResult()
465             {
466                 return m_result;
467             }
468         private:
469             string m_devUUID;
470             ESResult m_result;
471         };
472
473         /**
474          * @breif This provide a set of getter APIs from received response for getConfiguration().
475          *        Received information includes a device name, WiFi supported mode, and frequency.
476          *        Additionally, you can know if Enrollee can be access to cloud server with this
477          *        object.
478          */
479         class EnrolleeConf
480         {
481         public:
482             /**
483              * Constructor
484              * The expected OCRepresentation is one for collection resource and has several child
485              * OCRepresentation object corresponding to WiFi, DevConf, and CloudServer resource's
486              * representation.
487              */
488             EnrolleeConf(const OCRepresentation& rep) :
489                 m_ProvRep(rep)
490             {
491             }
492
493             EnrolleeConf(const EnrolleeConf& enrolleeConf) :
494                 m_ProvRep(enrolleeConf.getProvResRep())
495             {
496             }
497
498             EnrolleeConf(const EnrolleeConf&& enrolleeConf) :
499                 m_ProvRep(std::move(enrolleeConf.getProvResRep()))
500             {
501             }
502
503             /**
504              * Get a device name of Enrollee. It is Device's human-friendly name like device model
505              * name.
506              *
507              * @return a device name of Enrollee
508              */
509             std::string getDeviceName() const
510             {
511                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
512                 for(auto child = children.begin(); child != children.end(); ++child)
513                 {
514                     if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos)
515                     {
516                         if(child->hasAttribute(OC_RSRVD_ES_DEVNAME))
517                         {
518                             return child->getValue<std::string>(OC_RSRVD_ES_DEVNAME);
519                         }
520                     }
521                 }
522                 return std::string("");
523             }
524
525             /**
526              * Get a model number of Enrollee.
527              *
528              * @return a model number of Enrollee
529              */
530             std::string getModelNumber() const
531             {
532                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
533                 for(auto child = children.begin(); child != children.end(); ++child)
534                 {
535                     if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos)
536                     {
537                         if(child->hasAttribute(OC_RSRVD_ES_MODELNUMBER))
538                         {
539                             return child->getValue<std::string>(OC_RSRVD_ES_MODELNUMBER);
540                         }
541                     }
542                 }
543                 return std::string("");
544             }
545
546             /**
547              * Get a set of WiFi supported modes of Enrollee
548              *
549              * @return a set of WiFi supported modes of Enrollee
550              *
551              * @see WIFI_MODE
552              */
553             vector<WIFI_MODE> getWiFiModes() const
554             {
555                 vector<WIFI_MODE> modes;
556                 modes.clear();
557
558                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
559                 for(auto child = children.begin(); child != children.end(); ++child)
560                 {
561                     if(child->getUri().find(OC_RSRVD_ES_URI_WIFI) != std::string::npos)
562                     {
563                         if(child->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIMODE))
564                         {
565                             for(auto it : child->getValue
566                                         <std::vector<int>>(OC_RSRVD_ES_SUPPORTEDWIFIMODE))
567                             {
568                                 modes.push_back(static_cast<WIFI_MODE>(it));
569                             }
570                         }
571                     }
572                 }
573                 return modes;
574             }
575
576             /**
577              * Get a WiFi supported frequency of Enrollee
578              *
579              * @return a WiFi supported frequency of Enrollee
580              *
581              * @see WIFI_FREQ
582              */
583             WIFI_FREQ getWiFiFreq() const
584             {
585                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
586                 for(auto child = children.begin(); child != children.end(); ++child)
587                 {
588                     if(child->getUri().find(OC_RSRVD_ES_URI_WIFI) != std::string::npos)
589                     {
590                         if(child->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIFREQ))
591                         {
592                             return static_cast<WIFI_FREQ>(
593                                         child->getValue<int>(OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
594                         }
595                     }
596                 }
597                 return WIFI_FREQ_NONE;
598             }
599
600             /**
601              * Get an accessibility to cloud server of an Enrollee
602              *
603              * @return an accessibility to cloud server of an Enrollee
604              */
605             bool isCloudAccessible() const
606             {
607                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
608                 for(auto child = children.begin(); child != children.end(); ++child)
609                 {
610                     if(child->getUri().find(OC_RSRVD_ES_URI_CLOUDSERVER) != std::string::npos)
611                     {
612                         return true;
613                     }
614                 }
615                 return false;
616             }
617
618             /**
619              * Get OCRepresentation object
620              *
621              * @return OCRepresentation object
622              */
623             const OCRepresentation& getProvResRep() const
624             {
625                 return m_ProvRep;
626             }
627
628         protected:
629             OCRepresentation m_ProvRep;
630         };
631
632         /**
633          * Status object for getStatus API. This object is given to application
634          * when a response for GET request to provisioning resource at Enrollee is arrived.
635          * It returns a result of the API and requested data delivered in the response which includes
636          * a provisioning status and last error code stored in Enrollee.
637          *
638          * @see EnrolleeStatus
639          */
640         class GetEnrolleeStatus
641         {
642         public:
643             /**
644              * Constructor
645              */
646             GetEnrolleeStatus(ESResult result, const EnrolleeStatus& status) :
647                 m_result(result), m_enrolleeStatus(status)
648             {
649             }
650
651             /**
652              * Get a result of getting provisioning status and last error code of Enrollee
653              *
654              * @return a result of getting provisioning status and last error code of Enrollee
655              *
656              * @see ESResult
657              */
658             ESResult getESResult()
659             {
660                 return m_result;
661             }
662
663             /**
664              * Get Enrollee's status and last error code properties
665              *
666              * @return Enrollee's status and last error code properties
667              *
668              * @see EnrolleeStatus
669              */
670             const EnrolleeStatus& getEnrolleeStatus()
671             {
672                 return m_enrolleeStatus;
673             }
674
675         private:
676             ESResult m_result;
677             EnrolleeStatus m_enrolleeStatus;
678         };
679
680         /**
681          * Status object for getConfiguration API. This object is given to application
682          * when a response for GET request to provisioning resource at Enrollee is arrived.
683          * It returns a result of the API and requested data delivered in the response which includes
684          * WiFi configuration and device configuration stored in Enrollee.
685          *
686          * @see EnrolleeConf
687          */
688         class GetConfigurationStatus
689         {
690         public:
691             /**
692              * Constructor
693              */
694             GetConfigurationStatus(ESResult result, const EnrolleeConf& conf) :
695                     m_result(result), m_enrolleeConf(conf)
696             {
697             }
698
699             /**
700              * Get a result of getting preconfiguration of Enrollee
701              *
702              * @return a result of preconfiguration of Enrollee
703              *
704              * @see ESResult
705              */
706             ESResult getESResult()
707             {
708                 return m_result;
709             }
710
711             /**
712              * Get Enrollee's pre-configuration properties
713              *
714              * @return Enrollee's pre-configuration properties
715              *
716              * @see EnrolleeConf
717              */
718             EnrolleeConf& getEnrolleeConf()
719             {
720                 return m_enrolleeConf;
721             }
722
723         private:
724             ESResult m_result;
725             EnrolleeConf m_enrolleeConf;
726         };
727
728         /**
729          * Status object for provisionDeviceProperties API. This object is given to application
730          * when a response for GET request to provisioning resource at Enrollee is arrived.
731          * It returns a result of the request.
732          */
733         class DevicePropProvisioningStatus
734         {
735         public:
736             /**
737              * Constructor
738              */
739             DevicePropProvisioningStatus(ESResult result) :
740                     m_result(result)
741             {
742             }
743
744             /**
745              * Get a result of Device property provisioning
746              *
747              * @return a result of Device property provisioning
748              *
749              * @see ESResult
750              */
751             ESResult getESResult()
752             {
753                 return m_result;
754             }
755
756         private:
757             ESResult m_result;
758         };
759
760         /**
761          * Status object for provisionCloudProperties API. This object is given to application
762          * when a response for GET request to provisioning resource at Enrollee is arrived.
763          * It returns a result of the request and status of this provisioning. The status provides
764          * an information if the enrollee is found in a given network and the provisioning is
765          * successfully done.
766          */
767         class CloudPropProvisioningStatus
768         {
769         public:
770             /**
771              * Constructor
772              */
773             CloudPropProvisioningStatus(ESResult result) :
774                     m_result(result)
775             {
776             }
777
778             /**
779              * Get a result of Cloud property provisioning
780              *
781              * @return a result of Cloud property provisioning
782              *
783              * @see ESResult
784              */
785             ESResult getESResult()
786             {
787                 return m_result;
788             }
789
790         private:
791             ESResult m_result;
792         };
793
794         /**
795          * Callback function definition for providing Enrollee status
796          */
797         typedef function< void(shared_ptr< GetEnrolleeStatus >) > GetStatusCb;
798
799         /**
800          * Callback function definition for providing Enrollee configuration status
801          */
802         typedef function< void(shared_ptr< GetConfigurationStatus >) > GetConfigurationStatusCb;
803
804         /**
805          * Callback function definition for providing Enrollee device property provisioning status
806          */
807         typedef function< void(shared_ptr< DevicePropProvisioningStatus >) > DevicePropProvStatusCb;
808
809         /**
810          * Callback function definition for providing Enrollee cloud property provisioning status
811          */
812         typedef function< void(shared_ptr< CloudPropProvisioningStatus >) > CloudPropProvStatusCb;
813
814         /**
815          * Callback function definition for providing Enrollee security provisioning status
816          */
817         typedef function< void(shared_ptr<SecProvisioningStatus>) > SecurityProvStatusCb;
818
819         /**
820          * Callback definition to be invoked when the security stack expects a pin from application
821          */
822         typedef function< void(string&) > SecurityPinCb;
823
824         /**
825          * Callback definition to be invoked when the stack expects a db path
826          */
827         typedef function< void(string&) > SecProvisioningDbPathCb;
828
829     }
830 }
831 #endif //WITH_ARDUINO
832
833 #endif //ES_COMMON_RICH_H_