SinricPro Library
SinricProId.h
1 #ifndef _SINRICID_H_
2 #define _SINRICID_H_
3 
4 #define DEVICEID_BINLEN 12 // 12 bytes long
5 #define DEVICEID_STRLEN 24 // string needs to hold 24 characters
6 
7 struct DeviceId_Bin_t {
8  DeviceId_Bin_t() : _data{} {}
9  void fromString(const char * other);
10  String toString() const;
11  uint8_t _data[DEVICEID_BINLEN];
12 };
13 
14 void DeviceId_Bin_t::fromString(const char* other) {
15  char tmp;
16  bool _isValid = (sscanf(other,"%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%c",
17  &_data[11], &_data[10], &_data[9], &_data[8], &_data[7], &_data[6], &_data[5], &_data[4],
18  &_data[3], &_data[2], &_data[1], &_data[0],
19  &tmp
20  ) == sizeof(_data)) && (strlen(other) == DEVICEID_STRLEN);
21  if (!_isValid) memset((void*) &_data, 0, sizeof(_data));
22 }
23 
24 String DeviceId_Bin_t::toString() const {
25  char temp[DEVICEID_STRLEN+1];
26  sprintf(temp, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
27  _data[11], _data[10], _data[9], _data[8], _data[7], _data[6], _data[5], _data[4],
28  _data[3], _data[2], _data[1], _data[0]
29  );
30  return String(temp);
31 }
32 
33 
34 #define APPKEY_BINLEN 16
35 #define APPKEY_STRLEN 36
36 
37 struct AppKey_Bin_t {
38  AppKey_Bin_t() : _data{} {}
39  void fromString(const char * other);
40  String toString() const;
41  uint8_t _data[APPKEY_BINLEN];
42 };
43 
44 void AppKey_Bin_t::fromString(const char* other) {
45  char tmp;
46  bool _isValid = (sscanf(other,"%2hhx%2hhx%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%c",
47  &_data[15], &_data[14], &_data[13], &_data[12], &_data[11], &_data[10], &_data[9], &_data[8],
48  &_data[7], &_data[6], &_data[5], &_data[4], &_data[3], &_data[2], &_data[1], &_data[0],
49  &tmp
50  ) == sizeof(_data)) && (strlen(other) == APPKEY_STRLEN);
51  if (!_isValid) memset((void*) &_data, 0, sizeof(_data));
52 }
53 
54 String AppKey_Bin_t::toString() const {
55  char temp[APPKEY_STRLEN+1];
56  sprintf(temp, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
57  _data[15], _data[14], _data[13], _data[12], _data[11], _data[10], _data[9], _data[8],
58  _data[7], _data[6], _data[5], _data[4], _data[3], _data[2], _data[1], _data[0]
59  );
60  return String(temp);
61 }
62 
63 #define APPSECRET_BINLEN 32
64 #define APPSECRET_STRLEN 73
65 
66 struct AppSecret_Bin_t {
67  AppSecret_Bin_t() : _data{} {}
68  void fromString(const char * other);
69  String toString() const;
70  uint8_t _data[APPSECRET_BINLEN];
71 };
72 
73 void AppSecret_Bin_t::fromString(const char* other) {
74  char tmp;
75  bool _isValid = (sscanf(other, "%2hhx%2hhx%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%c",
76  &_data[31], &_data[30], &_data[29], &_data[28], &_data[27], &_data[26], &_data[25], &_data[24],
77  &_data[23], &_data[22], &_data[21], &_data[20], &_data[19], &_data[18], &_data[17], &_data[16],
78  &_data[15], &_data[14], &_data[13], &_data[12], &_data[11], &_data[10], &_data[ 9], &_data[ 8],
79  &_data[ 7], &_data[ 6], &_data[ 5], &_data[ 4], &_data[ 3], &_data[ 2], &_data[ 1], &_data[ 0],
80  &tmp
81  ) == sizeof(_data)) && (strlen(other) == APPSECRET_STRLEN);
82  if (!_isValid) memset((void*) &_data, 0, sizeof(_data));
83 }
84 
85 String AppSecret_Bin_t::toString() const {
86  char temp[APPSECRET_STRLEN+1];
87  sprintf(temp, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x-%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
88  _data[31], _data[30], _data[29], _data[28], _data[27], _data[26], _data[25], _data[24],
89  _data[23], _data[22], _data[21], _data[20], _data[19], _data[18], _data[17], _data[16],
90  _data[15], _data[14], _data[13], _data[12], _data[11], _data[10], _data[ 9], _data[ 8],
91  _data[ 7], _data[ 6], _data[ 5], _data[ 4], _data[ 3], _data[ 2], _data[ 1], _data[ 0]
92  );
93  return String(temp);
94 }
95 
96 template <class T>
97 class SinricProId {
98  public:
99  SinricProId() : _data() {};
100  SinricProId(const char* other) { _data.fromString(other); }
101  SinricProId(const String &other) { _data.fromString(other.c_str()); }
102  SinricProId(const SinricProId &other) { copy(other); }
103  SinricProId(const T &other) { copy(other); }
104  SinricProId(const uint8_t other[], size_t size) { copy(other, size); }
105 
106  SinricProId operator=(const SinricProId &other) { copy(other); return *this; }
107  SinricProId operator=(const char* other) { fromString(other); return *this; }
108  SinricProId operator=(const String &other) { fromString(other.c_str()); return *this; }
109  SinricProId operator=(const T &other) { copy(other); }
110 
111  bool operator==(const SinricProId &other) const { return compare(other); }
112  bool operator==(const char* other) const { return compare((SinricProId) other); }
113  bool operator==(const String& other) const { return compare((SinricProId) other); }
114  bool operator==(const T &other) const { return compare((SinricProId) other); }
115 
116  bool operator!=(const SinricProId &other) const { return !compare(other); }
117  bool operator!=(const char* other) const { return !compare(other); }
118  bool operator!=(const String &other) const { return !compare(other); }
119  bool operator!=(const T &other) const { return !compare(other); }
120 
121  operator bool() const { return isValid(); }
122  operator String() const { return _data.toString(); }
123 
124  String toString() const { return _data.toString(); };
125  const char* c_str() const { static String str = _data.toString(); return str.c_str(); }
126  bool isValid() const { return !compare(SinricProId<T>()); }
127 
128  private:
129  void fromString(const char * other) { _data.fromString(other); }
130  void copy(const SinricProId &other) { memcpy(_data._data, other._data._data, sizeof(_data._data)); }
131  void copy(const T &other) { memcpy(_data, other, sizeof(_data)); }
132  void copy(const uint8_t other[], size_t size) { memcpy(_data._data, other, min(sizeof(_data._data), size)); }
133  bool compare(const SinricProId &other) const { return memcmp(_data._data, other._data._data, sizeof(_data._data)) == 0;}
134  T _data;
135 };
136 
137 typedef SinricProId<DeviceId_Bin_t> DeviceId;
138 typedef SinricProId<AppKey_Bin_t> AppKey;
139 typedef SinricProId<AppSecret_Bin_t> AppSecret;
140 
141 #endif