• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Weave Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef LIBWEAVE_SRC_PRIVET_PRIVET_TYPES_H_
6 #define LIBWEAVE_SRC_PRIVET_PRIVET_TYPES_H_
7 
8 #include <string>
9 
10 #include <base/logging.h>
11 #include <weave/error.h>
12 #include <weave/settings.h>
13 
14 namespace weave {
15 namespace privet {
16 
17 enum class CryptoType {
18   kSpake_p224,
19 };
20 
21 enum class AuthType {
22   kAnonymous,
23   kPairing,
24   kLocal,
25 };
26 
27 enum class WifiType {
28   kWifi24,
29   kWifi50,
30 };
31 
32 struct UserAppId {
33   UserAppId() = default;
34 
UserAppIdUserAppId35   UserAppId(AuthType auth_type,
36             const std::vector<uint8_t>& user_id,
37             const std::vector<uint8_t>& app_id)
38       : type{auth_type},
39         user{user_id},
40         app{user_id.empty() ? user_id : app_id} {}
41 
IsEmptyUserAppId42   bool IsEmpty() const { return user.empty(); }
43 
44   AuthType type{};
45   std::vector<uint8_t> user;
46   std::vector<uint8_t> app;
47 };
48 
49 inline bool operator==(const UserAppId& l, const UserAppId& r) {
50   return l.user == r.user && l.app == r.app;
51 }
52 
53 inline bool operator!=(const UserAppId& l, const UserAppId& r) {
54   return l.user != r.user || l.app != r.app;
55 }
56 
57 class UserInfo {
58  public:
59   explicit UserInfo(AuthScope scope = AuthScope::kNone,
60                     const UserAppId& id = {})
61       : scope_{scope}, id_{scope == AuthScope::kNone ? UserAppId{} : id} {}
scope()62   AuthScope scope() const { return scope_; }
id()63   const UserAppId& id() const { return id_; }
64 
65  private:
66   AuthScope scope_;
67   UserAppId id_;
68 };
69 
70 class ConnectionState final {
71  public:
72   enum Status {
73     kDisabled,
74     kUnconfigured,
75     kConnecting,
76     kOnline,
77     kOffline,
78   };
79 
ConnectionState(Status status)80   explicit ConnectionState(Status status) : status_(status) {}
ConnectionState(ErrorPtr error)81   explicit ConnectionState(ErrorPtr error)
82       : status_(kOffline), error_(std::move(error)) {}
83 
status()84   Status status() const {
85     CHECK(!error_);
86     return status_;
87   }
88 
IsStatusEqual(Status status)89   bool IsStatusEqual(Status status) const {
90     if (error_)
91       return false;
92     return status_ == status;
93   }
94 
error()95   const Error* error() const { return error_.get(); }
96 
97  private:
98   Status status_;
99   ErrorPtr error_;
100 };
101 
102 class SetupState final {
103  public:
104   enum Status {
105     kNone,
106     kInProgress,
107     kSuccess,
108   };
109 
SetupState(Status status)110   explicit SetupState(Status status) : status_(status) {}
SetupState(ErrorPtr error)111   explicit SetupState(ErrorPtr error)
112       : status_(kNone), error_(std::move(error)) {}
113 
status()114   Status status() const {
115     CHECK(!error_);
116     return status_;
117   }
118 
IsStatusEqual(Status status)119   bool IsStatusEqual(Status status) const {
120     if (error_)
121       return false;
122     return status_ == status;
123   }
124 
error()125   const Error* error() const { return error_.get(); }
126 
127  private:
128   Status status_;
129   ErrorPtr error_;
130 };
131 
132 }  // namespace privet
133 }  // namespace weave
134 
135 #endif  // LIBWEAVE_SRC_PRIVET_PRIVET_TYPES_H_
136