aboutsummaryrefslogtreecommitdiff
path: root/src/privet/privet_types.h
blob: 2a290d4f1d5f7bcd2583cbad333eac078cf5df09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2015 The Weave Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef LIBWEAVE_SRC_PRIVET_PRIVET_TYPES_H_
#define LIBWEAVE_SRC_PRIVET_PRIVET_TYPES_H_

#include <string>
#include <vector>

#include <base/logging.h>
#include <weave/error.h>
#include <weave/settings.h>

namespace weave {
namespace privet {

enum class CryptoType {
  kSpake_p224,
};

enum class AuthType {
  kAnonymous,
  kPairing,
  kLocal,
};

enum class WifiType {
  kWifi24,
  kWifi50,
};

struct UserAppId {
  UserAppId() = default;

  UserAppId(AuthType auth_type,
            const std::vector<uint8_t>& user_id,
            const std::vector<uint8_t>& app_id)
      : type{auth_type},
        user{user_id},
        app{user_id.empty() ? user_id : app_id} {}

  bool IsEmpty() const { return user.empty(); }

  AuthType type{};
  std::vector<uint8_t> user;
  std::vector<uint8_t> app;
};

inline bool operator==(const UserAppId& l, const UserAppId& r) {
  return l.user == r.user && l.app == r.app;
}

inline bool operator!=(const UserAppId& l, const UserAppId& r) {
  return l.user != r.user || l.app != r.app;
}

class UserInfo {
 public:
  explicit UserInfo(AuthScope scope = AuthScope::kNone,
                    const UserAppId& id = {})
      : scope_{scope}, id_{scope == AuthScope::kNone ? UserAppId{} : id} {}
  AuthScope scope() const { return scope_; }
  const UserAppId& id() const { return id_; }

  bool operator==(const UserInfo& rhs) const {
    return scope_ == rhs.scope_ && id_ == rhs.id_;
  }

  bool operator!=(const UserInfo& rhs) const {
    return scope_ != rhs.scope_ || id_ != rhs.id_;
  }

 private:
  AuthScope scope_;
  UserAppId id_;
};

class ConnectionState final {
 public:
  enum Status {
    kDisabled,
    kUnconfigured,
    kConnecting,
    kOnline,
    kOffline,
  };

  explicit ConnectionState(Status status) : status_(status) {}
  explicit ConnectionState(ErrorPtr error)
      : status_(kOffline), error_(std::move(error)) {}

  Status status() const {
    CHECK(!error_);
    return status_;
  }

  bool IsStatusEqual(Status status) const {
    if (error_)
      return false;
    return status_ == status;
  }

  const Error* error() const { return error_.get(); }

 private:
  Status status_;
  ErrorPtr error_;
};

class SetupState final {
 public:
  enum Status {
    kNone,
    kInProgress,
    kSuccess,
  };

  explicit SetupState(Status status) : status_(status) {}
  explicit SetupState(ErrorPtr error)
      : status_(kNone), error_(std::move(error)) {}

  Status status() const {
    CHECK(!error_);
    return status_;
  }

  bool IsStatusEqual(Status status) const {
    if (error_)
      return false;
    return status_ == status;
  }

  const Error* error() const { return error_.get(); }

 private:
  Status status_;
  ErrorPtr error_;
};

}  // namespace privet
}  // namespace weave

#endif  // LIBWEAVE_SRC_PRIVET_PRIVET_TYPES_H_