Merge "Merge remote-tracking branch 'origin/master' into notification-service" into...
[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             }
127
128             CloudProp(const CloudProp& cloudProp) :
129                 m_rep(cloudProp.toOCRepresentation()), m_cloudID(cloudProp.getCloudID())
130             {
131             }
132
133             CloudProp(const CloudProp&& cloudProp) :
134                 m_rep(std::move(cloudProp.toOCRepresentation())), m_cloudID(cloudProp.getCloudID())
135             {
136             }
137
138             /**
139              * Constructor with OCRepresentation object. This is used for JNI communication.
140              */
141             CloudProp(const OCRepresentation &rep)
142             {
143                 m_rep = rep;
144                 m_cloudID = "";
145             }
146
147             /**
148              * Set CloudServer resource properties to be delivered to Enrollee
149              *
150              * @param authCode  Auth code issued by OAuth2.0-compatible account server
151              * @param authProvider Auth provider ID
152              * @param ciServer Cloud interface server URL which an Enrollee is going to registered
153              */
154             void setCloudProp(string authCode, string authProvider, string ciServer)
155             {
156                 m_rep.setValue(OC_RSRVD_ES_AUTHCODE, authCode);
157                 m_rep.setValue(OC_RSRVD_ES_AUTHPROVIDER, authProvider);
158                 m_rep.setValue(OC_RSRVD_ES_CISERVER, ciServer);
159             }
160
161             /**
162              * Set CloudServer's UUID
163              *
164              * @param cloudID Cloud Interface server's UUID
165              */
166             void setCloudID(string cloudID)
167             {
168                 m_cloudID = cloudID;
169             }
170
171             /**
172              * Get an auth code to be delivered.
173              *
174              * @return an auth code to be delivered.
175              */
176             std::string getAuthCode() const
177             {
178                 if(m_rep.hasAttribute(OC_RSRVD_ES_AUTHCODE))
179                 {
180                     return m_rep.getValue<std::string>(OC_RSRVD_ES_AUTHCODE);
181                 }
182                 return std::string("");
183             }
184
185             /**
186              * Get an auth provider which issued an auth code
187              *
188              * @return an auth provider which issued an auth code
189              */
190             std::string getAuthProvider() const
191             {
192                 if(m_rep.hasAttribute(OC_RSRVD_ES_AUTHPROVIDER))
193                 {
194                     return m_rep.getValue<std::string>(OC_RSRVD_ES_AUTHPROVIDER);
195                 }
196                 return std::string("");
197             }
198
199             /**
200              * Get a CI server to be delivered
201              *
202              * @return a CI server to be delivered
203              */
204             std::string getCiServer() const
205             {
206                 if(m_rep.hasAttribute(OC_RSRVD_ES_CISERVER))
207                 {
208                     return m_rep.getValue<std::string>(OC_RSRVD_ES_CISERVER);
209                 }
210                 return std::string("");
211             }
212
213             /**
214              * Get a CI server's Uuid to be delivered
215              *
216              * @return a CI server's Uuid to be delivered
217              */
218             std::string getCloudID() const
219             {
220                 return m_cloudID;
221             }
222
223             /**
224              * Get OCRepresentation object
225              *
226              * @return OCRepresentation object
227              */
228             const OCRepresentation &toOCRepresentation() const
229             {
230                 return m_rep;
231             }
232         protected:
233             OCRepresentation m_rep;
234             std::string m_cloudID;
235         };
236
237         /**
238          * @brief Data class stored for Device property provisioning which includes a WiFi
239          *        and device configuration provisioning
240          */
241         class DeviceProp
242         {
243         public:
244
245             /**
246              * Constructor
247              */
248             DeviceProp()
249             {
250             }
251
252             DeviceProp(const DeviceProp& deviceProp) :
253                 m_rep(deviceProp.toOCRepresentation())
254             {
255             }
256
257             DeviceProp(const DeviceProp&& deviceProp) :
258                 m_rep(std::move(deviceProp.toOCRepresentation()))
259             {
260             }
261
262             /**
263              * Constructor with OCRepresentation object. This is used for JNI communication.
264              */
265             DeviceProp(const OCRepresentation &rep)
266             {
267                 m_rep = rep;
268             }
269
270             /**
271              * Set WiFi resource properties to be delivered to Enrollee
272              *
273              * @param ssid Ssid of the Enroller
274              * @param pwd Pwd of the Enrolle
275              * @param authtype Auth type of the Enroller
276              * @param enctype Encryption type of the Enroller
277              *
278              * @see WIFI_AUTHTYPE
279              * @see WIFI_ENCTYPE
280              */
281             void setWiFiProp(string ssid, string pwd, WIFI_AUTHTYPE authtype, WIFI_ENCTYPE enctype)
282             {
283                 m_rep.setValue(OC_RSRVD_ES_SSID, ssid);
284                 m_rep.setValue(OC_RSRVD_ES_CRED, pwd);
285                 m_rep.setValue(OC_RSRVD_ES_AUTHTYPE, authtype);
286                 m_rep.setValue(OC_RSRVD_ES_ENCTYPE, enctype);
287             }
288
289             /**
290              * Set DevConf resource properties to be delivered to Enrollee
291              *
292              * @param language IETF language tag using ISO 639X
293              * @param country ISO Country Code (ISO 3166-1 Alpha-2)
294              */
295             void setDevConfProp(string language, string country, string location)
296             {
297                 m_rep.setValue(OC_RSRVD_ES_LANGUAGE, language);
298                 m_rep.setValue(OC_RSRVD_ES_COUNTRY, country);
299                 m_rep.setValue(OC_RSRVD_ES_LOCATION, location);
300             }
301
302             /**
303              * Get a SSID of Enroller
304              *
305              * @return a SSID of enroller
306              */
307             std::string getSsid() const
308             {
309                 if(m_rep.hasAttribute(OC_RSRVD_ES_SSID))
310                 {
311                     return m_rep.getValue<std::string>(OC_RSRVD_ES_SSID);
312                 }
313                 return std::string("");
314             }
315
316             /**
317              * Get a password of Enroller
318              *
319              * @return a password of enroller
320              */
321             std::string getPassword() const
322             {
323                 if(m_rep.hasAttribute(OC_RSRVD_ES_CRED))
324                 {
325                     return m_rep.getValue<std::string>(OC_RSRVD_ES_CRED);
326                 }
327                 return std::string("");
328             }
329
330             /**
331              * Get an auth type of Enroller
332              *
333              * @return an auth type of enroller
334              *
335              * @see WIFI_AUTHTYPE
336              */
337             WIFI_AUTHTYPE getAuthType() const
338             {
339                 if(m_rep.hasAttribute(OC_RSRVD_ES_AUTHTYPE))
340                 {
341                     return static_cast<WIFI_AUTHTYPE>(m_rep.getValue<int>(OC_RSRVD_ES_AUTHTYPE));
342                 }
343                 return NONE_AUTH;
344             }
345
346             /**
347              * Get an encryption type of Enroller
348              *
349              * @return an encryption type of enroller
350              *
351              * @see WIFI_ENCTYPE
352              */
353             WIFI_ENCTYPE getEncType() const
354             {
355                 if(m_rep.hasAttribute(OC_RSRVD_ES_ENCTYPE))
356                 {
357                     return static_cast<WIFI_ENCTYPE>(m_rep.getValue<int>(OC_RSRVD_ES_ENCTYPE));
358                 }
359                 return NONE_ENC;
360             }
361
362             /**
363              * Get a language to be set. A language is expressed in IETF language tag
364              * using ISO 639X.
365              *
366              * @return a language to be set
367              */
368             std::string getLanguage() const
369             {
370                 if(m_rep.hasAttribute(OC_RSRVD_ES_LANGUAGE))
371                 {
372                     return m_rep.getValue<std::string>(OC_RSRVD_ES_LANGUAGE);
373                 }
374                 return std::string("");
375             }
376
377             /**
378              * Get a country to be set. A country is expressed in ISO Country Code
379              * (ISO 3166-1 Alpha-2)
380              *
381              * @return a country to be set
382              */
383             std::string getCountry() const
384             {
385                 if(m_rep.hasAttribute(OC_RSRVD_ES_COUNTRY))
386                 {
387                     return m_rep.getValue<std::string>(OC_RSRVD_ES_COUNTRY);
388                 }
389                 return std::string("");
390             }
391
392             /**
393              * Get a location to be set. A location is GPS information
394              *
395              * @return a country to be set
396              */
397             std::string getLocation() const
398             {
399                 if(m_rep.hasAttribute(OC_RSRVD_ES_MODELNUMBER))
400                 {
401                     return m_rep.getValue<std::string>(OC_RSRVD_ES_MODELNUMBER);
402                 }
403                 return std::string("");
404             }
405
406             /**
407              * Get OCRepresentation object
408              *
409              * @return OCRepresentation object
410              */
411             const OCRepresentation &toOCRepresentation() const
412             {
413                 return m_rep;
414             }
415
416         protected:
417             OCRepresentation m_rep;
418         };
419
420         /**
421          * @brief Provisioning state in cloud server property provisioning.
422          */
423         typedef enum
424         {
425             ES_CLOUD_PROVISIONING_ERROR = -1,   /**< An error in cloud provisioning happens **/
426             ES_CLOUD_PROVISIONING_SUCCESS,      /**< Cloud provisioning is successfully done **/
427             ES_CLOUD_ENROLLEE_FOUND,            /**< An enrollee is found in a given network **/
428             ES_CLOUD_ENROLLEE_NOT_FOUND         /**< NO enrollee is found in a given network **/
429         }ESCloudProvState;
430
431         /**
432          * Security Provisioning Status
433          */
434         class SecProvisioningStatus
435         {
436         public:
437             SecProvisioningStatus(string deviceUUID, ESResult result) :
438                 m_devUUID(deviceUUID), m_result(result)
439             {
440             }
441
442             const string getDeviceUUID()
443             {
444                 return m_devUUID;
445             }
446
447             ESResult getESResult()
448             {
449                 return m_result;
450             }
451         private:
452             string m_devUUID;
453             ESResult m_result;
454         };
455
456         /**
457          * @breif This provide a set of getter APIs from received response for getConfiguration().
458          *        Received information includes a device name, WiFi supported mode, and frequency.
459          *        Additionally, you can know if Enrollee can be access to cloud server with this
460          *        object.
461          */
462         class EnrolleeConf
463         {
464         public:
465             /**
466              * Constructor
467              * The expected OCRepresentation is one for collection resource and has several child
468              * OCRepresentation object corresponding to WiFi, DevConf, and CloudServer resource's
469              * representation.
470              */
471             EnrolleeConf(const OCRepresentation& rep) :
472                 m_ProvRep(rep)
473             {
474             }
475
476             EnrolleeConf(const EnrolleeConf& enrolleeConf) :
477                 m_ProvRep(enrolleeConf.getProvResRep())
478             {
479             }
480
481             EnrolleeConf(const EnrolleeConf&& enrolleeConf) :
482                 m_ProvRep(std::move(enrolleeConf.getProvResRep()))
483             {
484             }
485
486             /**
487              * Get a device name of Enrollee. It is Device's human-friendly name like device model
488              * name.
489              *
490              * @return a device name of Enrollee
491              */
492             std::string getDeviceName() const
493             {
494                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
495                 for(auto child = children.begin(); child != children.end(); ++child)
496                 {
497                     if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos)
498                     {
499                         if(child->hasAttribute(OC_RSRVD_ES_DEVNAME))
500                         {
501                             return child->getValue<std::string>(OC_RSRVD_ES_DEVNAME);
502                         }
503                     }
504                 }
505                 return std::string("");
506             }
507
508             /**
509              * Get a model number of Enrollee.
510              *
511              * @return a model number of Enrollee
512              */
513             std::string getModelNumber() const
514             {
515                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
516                 for(auto child = children.begin(); child != children.end(); ++child)
517                 {
518                     if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos)
519                     {
520                         if(child->hasAttribute(OC_RSRVD_ES_MODELNUMBER))
521                         {
522                             return child->getValue<std::string>(OC_RSRVD_ES_MODELNUMBER);
523                         }
524                     }
525                 }
526                 return std::string("");
527             }
528
529             /**
530              * Get a set of WiFi supported modes of Enrollee
531              *
532              * @return a set of WiFi supported modes of Enrollee
533              *
534              * @see WIFI_MODE
535              */
536             vector<WIFI_MODE> getWiFiModes() const
537             {
538                 vector<WIFI_MODE> modes;
539                 modes.clear();
540
541                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
542                 for(auto child = children.begin(); child != children.end(); ++child)
543                 {
544                     if(child->getUri().find(OC_RSRVD_ES_URI_WIFI) != std::string::npos)
545                     {
546                         if(child->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIMODE))
547                 {
548                             for(auto it : child->getValue
549                                         <std::vector<int>>(OC_RSRVD_ES_SUPPORTEDWIFIMODE))
550                     {
551                         modes.push_back(static_cast<WIFI_MODE>(it));
552                             }
553                         }
554                     }
555                 }
556                 return modes;
557             }
558
559             /**
560              * Get a WiFi supported frequency of Enrollee
561              *
562              * @return a WiFi supported frequency of Enrollee
563              *
564              * @see WIFI_FREQ
565              */
566             WIFI_FREQ getWiFiFreq() const
567             {
568                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
569                 for(auto child = children.begin(); child != children.end(); ++child)
570                 {
571                     if(child->getUri().find(OC_RSRVD_ES_URI_WIFI) != std::string::npos)
572                     {
573                         if(child->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIFREQ))
574                         {
575                             return static_cast<WIFI_FREQ>(
576                                         child->getValue<int>(OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
577                         }
578                     }
579                 }
580                 return WIFI_FREQ_NONE;
581             }
582
583             /**
584              * Get an accessibility to cloud server of an Enrollee
585              *
586              * @return an accessibility to cloud server of an Enrollee
587              */
588             bool isCloudAccessible() const
589             {
590                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
591                 for(auto child = children.begin(); child != children.end(); ++child)
592                 {
593                     if(child->getUri().find(OC_RSRVD_ES_URI_CLOUDSERVER) != std::string::npos)
594                     {
595                         return true;
596                     }
597                 }
598                 return false;
599             }
600
601             /**
602              * Get OCRepresentation object
603              *
604              * @return OCRepresentation object
605              */
606             const OCRepresentation& getProvResRep() const
607             {
608                 return m_ProvRep;
609             }
610
611         protected:
612             OCRepresentation m_ProvRep;
613         };
614
615         /**
616          * Status object for getStatus API. This object is given to application
617          * when a response for GET request to provisioning resource at Enrollee is arrived.
618          * It returns a result of the API and requested data delivered in the response which includes
619          * a provisioning status and last error code stored in Enrollee.
620          *
621          * @see EnrolleeStatus
622          */
623         class GetEnrolleeStatus
624         {
625         public:
626             GetEnrolleeStatus(ESResult result, const EnrolleeStatus& status) :
627                 m_result(result), m_enrolleeStatus(status)
628             {
629             }
630
631             ESResult getESResult()
632             {
633                 return m_result;
634             }
635
636             const EnrolleeStatus& getEnrolleeStatus()
637             {
638                 return m_enrolleeStatus;
639             }
640
641         private:
642             ESResult m_result;
643             EnrolleeStatus m_enrolleeStatus;
644         };
645
646         /**
647          * Status object for getConfiguration API. This object is given to application
648          * when a response for GET request to provisioning resource at Enrollee is arrived.
649          * It returns a result of the API and requested data delivered in the response which includes
650          * WiFi configuration and device configuration stored in Enrollee.
651          *
652          * @see EnrolleeConf
653          */
654         class GetConfigurationStatus
655         {
656         public:
657             GetConfigurationStatus(ESResult result, const EnrolleeConf& conf) :
658                     m_result(result), m_enrolleeConf(conf)
659             {
660             }
661
662             ESResult getESResult()
663             {
664                 return m_result;
665             }
666
667             EnrolleeConf& getEnrolleeConf()
668             {
669                 return m_enrolleeConf;
670             }
671
672         private:
673             ESResult m_result;
674             EnrolleeConf m_enrolleeConf;
675         };
676
677         /**
678          * Status object for provisionDeviceProperties API. This object is given to application
679          * when a response for GET request to provisioning resource at Enrollee is arrived.
680          * It returns a result of the request.
681          */
682         class DevicePropProvisioningStatus
683         {
684         public:
685             DevicePropProvisioningStatus(ESResult result) :
686                     m_result(result)
687             {
688             }
689
690             ESResult getESResult()
691             {
692                 return m_result;
693             }
694
695         private:
696             ESResult m_result;
697         };
698
699         /**
700          * Status object for provisionCloudProperties API. This object is given to application
701          * when a response for GET request to provisioning resource at Enrollee is arrived.
702          * It returns a result of the request and status of this provisioning. The status provides
703          * an information if the enrollee is found in a given network and the provisioning is
704          * successfully done.
705          */
706         class CloudPropProvisioningStatus
707         {
708         public:
709             CloudPropProvisioningStatus(ESResult result, ESCloudProvState state) :
710                     m_result(result), m_esCloudState(state)
711             {
712             }
713
714             ESResult getESResult()
715             {
716                 return m_result;
717             }
718
719             ESCloudProvState getESCloudState()
720             {
721                 return m_esCloudState;
722             }
723
724         private:
725             ESResult m_result;
726             ESCloudProvState m_esCloudState;
727         };
728
729         /**
730          * Callback function definition for providing Enrollee status
731          */
732         typedef function< void(shared_ptr< GetEnrolleeStatus >) > GetStatusCb;
733
734         /**
735          * Callback function definition for providing Enrollee configuration status
736          */
737         typedef function< void(shared_ptr< GetConfigurationStatus >) > GetConfigurationStatusCb;
738
739         /**
740          * Callback function definition for providing Enrollee device property provisioning status
741          */
742         typedef function< void(shared_ptr< DevicePropProvisioningStatus >) > DevicePropProvStatusCb;
743
744         /**
745          * Callback function definition for providing Enrollee cloud property provisioning status
746          */
747         typedef function< void(shared_ptr< CloudPropProvisioningStatus >) > CloudPropProvStatusCb;
748
749         /**
750          * Callback function definition for providing Enrollee security provisioning status
751          */
752         typedef function< void(shared_ptr<SecProvisioningStatus>) > SecurityProvStatusCb;
753
754         /**
755          * Callback definition to be invoked when the security stack expects a pin from application
756          */
757         typedef function< void(string&) > SecurityPinCb;
758
759         /**
760          * Callback definition to be invoked when the stack expects a db path
761          */
762         typedef function< void(string&) > SecProvisioningDbPathCb;
763
764     }
765 }
766 #endif //WITH_ARDUINO
767
768 #endif //ES_COMMON_RICH_H_