Remove ESCloudProvState due to duplication
[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          * Security Provisioning Status
422          */
423         class SecProvisioningStatus
424         {
425         public:
426             SecProvisioningStatus(string deviceUUID, ESResult result) :
427                 m_devUUID(deviceUUID), m_result(result)
428             {
429             }
430
431             const string getDeviceUUID()
432             {
433                 return m_devUUID;
434             }
435
436             ESResult getESResult()
437             {
438                 return m_result;
439             }
440         private:
441             string m_devUUID;
442             ESResult m_result;
443         };
444
445         /**
446          * @breif This provide a set of getter APIs from received response for getConfiguration().
447          *        Received information includes a device name, WiFi supported mode, and frequency.
448          *        Additionally, you can know if Enrollee can be access to cloud server with this
449          *        object.
450          */
451         class EnrolleeConf
452         {
453         public:
454             /**
455              * Constructor
456              * The expected OCRepresentation is one for collection resource and has several child
457              * OCRepresentation object corresponding to WiFi, DevConf, and CloudServer resource's
458              * representation.
459              */
460             EnrolleeConf(const OCRepresentation& rep) :
461                 m_ProvRep(rep)
462             {
463             }
464
465             EnrolleeConf(const EnrolleeConf& enrolleeConf) :
466                 m_ProvRep(enrolleeConf.getProvResRep())
467             {
468             }
469
470             EnrolleeConf(const EnrolleeConf&& enrolleeConf) :
471                 m_ProvRep(std::move(enrolleeConf.getProvResRep()))
472             {
473             }
474
475             /**
476              * Get a device name of Enrollee. It is Device's human-friendly name like device model
477              * name.
478              *
479              * @return a device name of Enrollee
480              */
481             std::string getDeviceName() const
482             {
483                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
484                 for(auto child = children.begin(); child != children.end(); ++child)
485                 {
486                     if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos)
487                     {
488                         if(child->hasAttribute(OC_RSRVD_ES_DEVNAME))
489                         {
490                             return child->getValue<std::string>(OC_RSRVD_ES_DEVNAME);
491                         }
492                     }
493                 }
494                 return std::string("");
495             }
496
497             /**
498              * Get a model number of Enrollee.
499              *
500              * @return a model number of Enrollee
501              */
502             std::string getModelNumber() const
503             {
504                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
505                 for(auto child = children.begin(); child != children.end(); ++child)
506                 {
507                     if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos)
508                     {
509                         if(child->hasAttribute(OC_RSRVD_ES_MODELNUMBER))
510                         {
511                             return child->getValue<std::string>(OC_RSRVD_ES_MODELNUMBER);
512                         }
513                     }
514                 }
515                 return std::string("");
516             }
517
518             /**
519              * Get a set of WiFi supported modes of Enrollee
520              *
521              * @return a set of WiFi supported modes of Enrollee
522              *
523              * @see WIFI_MODE
524              */
525             vector<WIFI_MODE> getWiFiModes() const
526             {
527                 vector<WIFI_MODE> modes;
528                 modes.clear();
529
530                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
531                 for(auto child = children.begin(); child != children.end(); ++child)
532                 {
533                     if(child->getUri().find(OC_RSRVD_ES_URI_WIFI) != std::string::npos)
534                     {
535                         if(child->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIMODE))
536                 {
537                             for(auto it : child->getValue
538                                         <std::vector<int>>(OC_RSRVD_ES_SUPPORTEDWIFIMODE))
539                     {
540                         modes.push_back(static_cast<WIFI_MODE>(it));
541                             }
542                         }
543                     }
544                 }
545                 return modes;
546             }
547
548             /**
549              * Get a WiFi supported frequency of Enrollee
550              *
551              * @return a WiFi supported frequency of Enrollee
552              *
553              * @see WIFI_FREQ
554              */
555             WIFI_FREQ getWiFiFreq() const
556             {
557                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
558                 for(auto child = children.begin(); child != children.end(); ++child)
559                 {
560                     if(child->getUri().find(OC_RSRVD_ES_URI_WIFI) != std::string::npos)
561                     {
562                         if(child->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIFREQ))
563                         {
564                             return static_cast<WIFI_FREQ>(
565                                         child->getValue<int>(OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
566                         }
567                     }
568                 }
569                 return WIFI_FREQ_NONE;
570             }
571
572             /**
573              * Get an accessibility to cloud server of an Enrollee
574              *
575              * @return an accessibility to cloud server of an Enrollee
576              */
577             bool isCloudAccessible() const
578             {
579                 std::vector<OCRepresentation> children = m_ProvRep.getChildren();
580                 for(auto child = children.begin(); child != children.end(); ++child)
581                 {
582                     if(child->getUri().find(OC_RSRVD_ES_URI_CLOUDSERVER) != std::string::npos)
583                     {
584                         return true;
585                     }
586                 }
587                 return false;
588             }
589
590             /**
591              * Get OCRepresentation object
592              *
593              * @return OCRepresentation object
594              */
595             const OCRepresentation& getProvResRep() const
596             {
597                 return m_ProvRep;
598             }
599
600         protected:
601             OCRepresentation m_ProvRep;
602         };
603
604         /**
605          * Status object for getStatus API. This object is given to application
606          * when a response for GET request to provisioning resource at Enrollee is arrived.
607          * It returns a result of the API and requested data delivered in the response which includes
608          * a provisioning status and last error code stored in Enrollee.
609          *
610          * @see EnrolleeStatus
611          */
612         class GetEnrolleeStatus
613         {
614         public:
615             GetEnrolleeStatus(ESResult result, const EnrolleeStatus& status) :
616                 m_result(result), m_enrolleeStatus(status)
617             {
618             }
619
620             ESResult getESResult()
621             {
622                 return m_result;
623             }
624
625             const EnrolleeStatus& getEnrolleeStatus()
626             {
627                 return m_enrolleeStatus;
628             }
629
630         private:
631             ESResult m_result;
632             EnrolleeStatus m_enrolleeStatus;
633         };
634
635         /**
636          * Status object for getConfiguration API. This object is given to application
637          * when a response for GET request to provisioning resource at Enrollee is arrived.
638          * It returns a result of the API and requested data delivered in the response which includes
639          * WiFi configuration and device configuration stored in Enrollee.
640          *
641          * @see EnrolleeConf
642          */
643         class GetConfigurationStatus
644         {
645         public:
646             GetConfigurationStatus(ESResult result, const EnrolleeConf& conf) :
647                     m_result(result), m_enrolleeConf(conf)
648             {
649             }
650
651             ESResult getESResult()
652             {
653                 return m_result;
654             }
655
656             EnrolleeConf& getEnrolleeConf()
657             {
658                 return m_enrolleeConf;
659             }
660
661         private:
662             ESResult m_result;
663             EnrolleeConf m_enrolleeConf;
664         };
665
666         /**
667          * Status object for provisionDeviceProperties API. This object is given to application
668          * when a response for GET request to provisioning resource at Enrollee is arrived.
669          * It returns a result of the request.
670          */
671         class DevicePropProvisioningStatus
672         {
673         public:
674             DevicePropProvisioningStatus(ESResult result) :
675                     m_result(result)
676             {
677             }
678
679             ESResult getESResult()
680             {
681                 return m_result;
682             }
683
684         private:
685             ESResult m_result;
686         };
687
688         /**
689          * Status object for provisionCloudProperties API. This object is given to application
690          * when a response for GET request to provisioning resource at Enrollee is arrived.
691          * It returns a result of the request and status of this provisioning. The status provides
692          * an information if the enrollee is found in a given network and the provisioning is
693          * successfully done.
694          */
695         class CloudPropProvisioningStatus
696         {
697         public:
698             CloudPropProvisioningStatus(ESResult result) :
699                     m_result(result)
700             {
701             }
702
703             ESResult getESResult()
704             {
705                 return m_result;
706             }
707
708         private:
709             ESResult m_result;
710         };
711
712         /**
713          * Callback function definition for providing Enrollee status
714          */
715         typedef function< void(shared_ptr< GetEnrolleeStatus >) > GetStatusCb;
716
717         /**
718          * Callback function definition for providing Enrollee configuration status
719          */
720         typedef function< void(shared_ptr< GetConfigurationStatus >) > GetConfigurationStatusCb;
721
722         /**
723          * Callback function definition for providing Enrollee device property provisioning status
724          */
725         typedef function< void(shared_ptr< DevicePropProvisioningStatus >) > DevicePropProvStatusCb;
726
727         /**
728          * Callback function definition for providing Enrollee cloud property provisioning status
729          */
730         typedef function< void(shared_ptr< CloudPropProvisioningStatus >) > CloudPropProvStatusCb;
731
732         /**
733          * Callback function definition for providing Enrollee security provisioning status
734          */
735         typedef function< void(shared_ptr<SecProvisioningStatus>) > SecurityProvStatusCb;
736
737         /**
738          * Callback definition to be invoked when the security stack expects a pin from application
739          */
740         typedef function< void(string&) > SecurityPinCb;
741
742         /**
743          * Callback definition to be invoked when the stack expects a db path
744          */
745         typedef function< void(string&) > SecProvisioningDbPathCb;
746
747     }
748 }
749 #endif //WITH_ARDUINO
750
751 #endif //ES_COMMON_RICH_H_