mbedからGluinサーバへ接続するライブラリです
DxClient.cpp@0:735163979ecf, 2015-02-14 (annotated)
- Committer:
- komoritan
- Date:
- Sat Feb 14 00:28:40 2015 +0000
- Revision:
- 0:735163979ecf
First Release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
komoritan | 0:735163979ecf | 1 | #include "DxClient.h" |
komoritan | 0:735163979ecf | 2 | #include "picojson.h" |
komoritan | 0:735163979ecf | 3 | |
komoritan | 0:735163979ecf | 4 | #if 1 |
komoritan | 0:735163979ecf | 5 | #define DXDBG(x, ...) printf("[DxClient : DBG]"x"\r\n", ##__VA_ARGS__); |
komoritan | 0:735163979ecf | 6 | #define DXWARN(x, ...) printf("[DxClient : WARN]"x"\r\n", ##__VA_ARGS__); |
komoritan | 0:735163979ecf | 7 | #define DXERR(x, ...) printf("[DxClient : ERR]"x"\r\n", ##__VA_ARGS__); |
komoritan | 0:735163979ecf | 8 | #else |
komoritan | 0:735163979ecf | 9 | #define DXDBG(x, ...) |
komoritan | 0:735163979ecf | 10 | #define DXWARN(x, ...) |
komoritan | 0:735163979ecf | 11 | #define DXERR(x, ...) |
komoritan | 0:735163979ecf | 12 | #endif |
komoritan | 0:735163979ecf | 13 | |
komoritan | 0:735163979ecf | 14 | #define DXINFO(x, ...) printf("[DxClient : INFO]"x"\r\n", ##__VA_ARGS__); |
komoritan | 0:735163979ecf | 15 | |
komoritan | 0:735163979ecf | 16 | void generate_randomid( char* out, int len ) |
komoritan | 0:735163979ecf | 17 | { |
komoritan | 0:735163979ecf | 18 | int i; |
komoritan | 0:735163979ecf | 19 | static char* words = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!#$%&()=<>?-_[]{}"; |
komoritan | 0:735163979ecf | 20 | for (i=0;i<len-1;i++) { |
komoritan | 0:735163979ecf | 21 | out[i] = words[ rand()%strlen(words) ]; |
komoritan | 0:735163979ecf | 22 | } |
komoritan | 0:735163979ecf | 23 | out[len-1] = 0; |
komoritan | 0:735163979ecf | 24 | } |
komoritan | 0:735163979ecf | 25 | |
komoritan | 0:735163979ecf | 26 | DxClient::DxClient(char * url, char* deviceid, float seed) |
komoritan | 0:735163979ecf | 27 | { |
komoritan | 0:735163979ecf | 28 | m_ws.set_server( url ); |
komoritan | 0:735163979ecf | 29 | strcpy( m_deviceid, deviceid ); |
komoritan | 0:735163979ecf | 30 | |
komoritan | 0:735163979ecf | 31 | memset( m_user, 0, sizeof(m_user)); |
komoritan | 0:735163979ecf | 32 | memset( m_pass, 0, sizeof(m_pass)); |
komoritan | 0:735163979ecf | 33 | memset( m_dev_name, 0, sizeof(m_dev_name)); |
komoritan | 0:735163979ecf | 34 | memset( m_dev_description, 0, sizeof(m_dev_description)); |
komoritan | 0:735163979ecf | 35 | |
komoritan | 0:735163979ecf | 36 | m_func_get_request = NULL; |
komoritan | 0:735163979ecf | 37 | m_func_set_request = NULL; |
komoritan | 0:735163979ecf | 38 | |
komoritan | 0:735163979ecf | 39 | DXDBG("random seed is %f", seed ); |
komoritan | 0:735163979ecf | 40 | srand(seed); |
komoritan | 0:735163979ecf | 41 | } |
komoritan | 0:735163979ecf | 42 | |
komoritan | 0:735163979ecf | 43 | |
komoritan | 0:735163979ecf | 44 | void DxClient::set_user(char* user, char *pass) |
komoritan | 0:735163979ecf | 45 | { |
komoritan | 0:735163979ecf | 46 | // set auth user and password |
komoritan | 0:735163979ecf | 47 | strncpy( m_user, user, sizeof(m_user) ); |
komoritan | 0:735163979ecf | 48 | strncpy( m_pass, pass, sizeof(m_pass) ); |
komoritan | 0:735163979ecf | 49 | } |
komoritan | 0:735163979ecf | 50 | |
komoritan | 0:735163979ecf | 51 | void DxClient::set_device_description(char* desc) |
komoritan | 0:735163979ecf | 52 | { |
komoritan | 0:735163979ecf | 53 | strncpy( m_dev_description, desc, sizeof(m_dev_description) ); |
komoritan | 0:735163979ecf | 54 | } |
komoritan | 0:735163979ecf | 55 | void DxClient::set_device_name(char* name) |
komoritan | 0:735163979ecf | 56 | { |
komoritan | 0:735163979ecf | 57 | strncpy( m_dev_name, name, sizeof(m_dev_name) ); |
komoritan | 0:735163979ecf | 58 | } |
komoritan | 0:735163979ecf | 59 | |
komoritan | 0:735163979ecf | 60 | |
komoritan | 0:735163979ecf | 61 | bool DxClient::connect() |
komoritan | 0:735163979ecf | 62 | { |
komoritan | 0:735163979ecf | 63 | bool res; |
komoritan | 0:735163979ecf | 64 | res = m_ws.connect(); |
komoritan | 0:735163979ecf | 65 | if (res) { |
komoritan | 0:735163979ecf | 66 | DXDBG("Connected to server by Websocket"); |
komoritan | 0:735163979ecf | 67 | // send user_auth_request |
komoritan | 0:735163979ecf | 68 | res = dx_user_auth_request(); |
komoritan | 0:735163979ecf | 69 | if (res) { |
komoritan | 0:735163979ecf | 70 | DXDBG("...Authorized"); |
komoritan | 0:735163979ecf | 71 | } else { |
komoritan | 0:735163979ecf | 72 | DXDBG("...Login failed"); |
komoritan | 0:735163979ecf | 73 | } |
komoritan | 0:735163979ecf | 74 | } else { |
komoritan | 0:735163979ecf | 75 | DXDBG("Failed to server by Websocket"); |
komoritan | 0:735163979ecf | 76 | } |
komoritan | 0:735163979ecf | 77 | return res; |
komoritan | 0:735163979ecf | 78 | } |
komoritan | 0:735163979ecf | 79 | |
komoritan | 0:735163979ecf | 80 | bool DxClient::close() |
komoritan | 0:735163979ecf | 81 | { |
komoritan | 0:735163979ecf | 82 | return m_ws.close(); |
komoritan | 0:735163979ecf | 83 | } |
komoritan | 0:735163979ecf | 84 | |
komoritan | 0:735163979ecf | 85 | // ============================================== |
komoritan | 0:735163979ecf | 86 | // Send user_auth_request to server |
komoritan | 0:735163979ecf | 87 | // if user is authorized correctly, clinet gets 200 OK response |
komoritan | 0:735163979ecf | 88 | // this request must be sent before any other requset |
komoritan | 0:735163979ecf | 89 | // ============================================== |
komoritan | 0:735163979ecf | 90 | bool DxClient::dx_user_auth_request() |
komoritan | 0:735163979ecf | 91 | { |
komoritan | 0:735163979ecf | 92 | DXDBG("Call dx_user_auth_request"); |
komoritan | 0:735163979ecf | 93 | |
komoritan | 0:735163979ecf | 94 | int i, len; |
komoritan | 0:735163979ecf | 95 | bool res; |
komoritan | 0:735163979ecf | 96 | |
komoritan | 0:735163979ecf | 97 | char message[MAX_MESSAGELEN]; |
komoritan | 0:735163979ecf | 98 | char mesid[MAX_MESSAGEIDLEN]; |
komoritan | 0:735163979ecf | 99 | generate_randomid( mesid, sizeof(mesid) ); |
komoritan | 0:735163979ecf | 100 | len = snprintf(message, sizeof(message), "{" \ |
komoritan | 0:735163979ecf | 101 | "\"type\":\"user-auth-request\"," \ |
komoritan | 0:735163979ecf | 102 | "\"message-id\":\"%s\"," \ |
komoritan | 0:735163979ecf | 103 | "\"username\":\"%s\"," \ |
komoritan | 0:735163979ecf | 104 | "\"password\":\"%s\"" \ |
komoritan | 0:735163979ecf | 105 | "}", |
komoritan | 0:735163979ecf | 106 | mesid, |
komoritan | 0:735163979ecf | 107 | m_user, |
komoritan | 0:735163979ecf | 108 | m_pass |
komoritan | 0:735163979ecf | 109 | ); |
komoritan | 0:735163979ecf | 110 | if (len >= sizeof(message)-1) { |
komoritan | 0:735163979ecf | 111 | DXDBG( "USER_AUTH: message is over MAX LEN"); |
komoritan | 0:735163979ecf | 112 | return false; |
komoritan | 0:735163979ecf | 113 | } |
komoritan | 0:735163979ecf | 114 | DXDBG("send: %s", message); |
komoritan | 0:735163979ecf | 115 | |
komoritan | 0:735163979ecf | 116 | m_ws.send(message); |
komoritan | 0:735163979ecf | 117 | |
komoritan | 0:735163979ecf | 118 | // clear the buffer and wait a sec... |
komoritan | 0:735163979ecf | 119 | memset( message, 0, sizeof(message)); |
komoritan | 0:735163979ecf | 120 | for (i=0, res=false;i<10;i++) { |
komoritan | 0:735163979ecf | 121 | wait(0.5f); |
komoritan | 0:735163979ecf | 122 | if (m_ws.read(message)) { |
komoritan | 0:735163979ecf | 123 | DXDBG("Get Message: %s\n", message); |
komoritan | 0:735163979ecf | 124 | picojson::value v; |
komoritan | 0:735163979ecf | 125 | std::string err; |
komoritan | 0:735163979ecf | 126 | picojson::parse(v, (const char *)message, (const char *)(message + strlen(message)), &err); |
komoritan | 0:735163979ecf | 127 | |
komoritan | 0:735163979ecf | 128 | |
komoritan | 0:735163979ecf | 129 | if (err.empty()) |
komoritan | 0:735163979ecf | 130 | { |
komoritan | 0:735163979ecf | 131 | picojson::object& root = v.get<picojson::object>(); |
komoritan | 0:735163979ecf | 132 | |
komoritan | 0:735163979ecf | 133 | // check auth response |
komoritan | 0:735163979ecf | 134 | if (root["type"].is<std::string>()) { |
komoritan | 0:735163979ecf | 135 | if (strcmp(root["type"].get<std::string>().c_str(), "user-auth-response")) { |
komoritan | 0:735163979ecf | 136 | DXDBG("USER_AUTH: Not user_auth_response\n"); |
komoritan | 0:735163979ecf | 137 | break; |
komoritan | 0:735163979ecf | 138 | } |
komoritan | 0:735163979ecf | 139 | } else { |
komoritan | 0:735163979ecf | 140 | DXDBG("USER_AUTH: there is no type\n"); |
komoritan | 0:735163979ecf | 141 | break; |
komoritan | 0:735163979ecf | 142 | } |
komoritan | 0:735163979ecf | 143 | if (root["message-id"].is<std::string>()) { |
komoritan | 0:735163979ecf | 144 | if (strcmp(root["message-id"].get<std::string>().c_str(), mesid)) { |
komoritan | 0:735163979ecf | 145 | DXDBG("USER_AUTH: Not correct response message-id\n"); |
komoritan | 0:735163979ecf | 146 | break; |
komoritan | 0:735163979ecf | 147 | } |
komoritan | 0:735163979ecf | 148 | } else { |
komoritan | 0:735163979ecf | 149 | DXDBG("USER_AUTH: there is no message-id\n"); |
komoritan | 0:735163979ecf | 150 | break; |
komoritan | 0:735163979ecf | 151 | } |
komoritan | 0:735163979ecf | 152 | if (root["status"].is<std::string>()) { |
komoritan | 0:735163979ecf | 153 | if (strcmp(root["status"].get<std::string>().c_str(), "200 OK")) { |
komoritan | 0:735163979ecf | 154 | DXDBG("USER_AUTH: status error\n"); |
komoritan | 0:735163979ecf | 155 | break; |
komoritan | 0:735163979ecf | 156 | } |
komoritan | 0:735163979ecf | 157 | } else { |
komoritan | 0:735163979ecf | 158 | DXDBG("USER_AUTH: there is no status\n"); |
komoritan | 0:735163979ecf | 159 | break; |
komoritan | 0:735163979ecf | 160 | } |
komoritan | 0:735163979ecf | 161 | |
komoritan | 0:735163979ecf | 162 | DXDBG("USER_AUTH: user auth is success\n"); |
komoritan | 0:735163979ecf | 163 | res = true; |
komoritan | 0:735163979ecf | 164 | } else { |
komoritan | 0:735163979ecf | 165 | DXDBG("USER_AUTH: JSON parse Error\n"); |
komoritan | 0:735163979ecf | 166 | } |
komoritan | 0:735163979ecf | 167 | break; |
komoritan | 0:735163979ecf | 168 | } |
komoritan | 0:735163979ecf | 169 | } |
komoritan | 0:735163979ecf | 170 | return res; |
komoritan | 0:735163979ecf | 171 | } |
komoritan | 0:735163979ecf | 172 | |
komoritan | 0:735163979ecf | 173 | char* dx_type_to_string( dx_prop_type type, char* buf) |
komoritan | 0:735163979ecf | 174 | { |
komoritan | 0:735163979ecf | 175 | char *res = buf; |
komoritan | 0:735163979ecf | 176 | switch( type ) { |
komoritan | 0:735163979ecf | 177 | case DX_STRING: |
komoritan | 0:735163979ecf | 178 | sprintf( res, "string" ); |
komoritan | 0:735163979ecf | 179 | break; |
komoritan | 0:735163979ecf | 180 | case DX_INTEGER: |
komoritan | 0:735163979ecf | 181 | sprintf( res, "integer" ); |
komoritan | 0:735163979ecf | 182 | break; |
komoritan | 0:735163979ecf | 183 | case DX_FLOAT: |
komoritan | 0:735163979ecf | 184 | sprintf( res, "float" ); |
komoritan | 0:735163979ecf | 185 | break; |
komoritan | 0:735163979ecf | 186 | case DX_BOOLEAN: |
komoritan | 0:735163979ecf | 187 | sprintf( res, "boolean" ); |
komoritan | 0:735163979ecf | 188 | break; |
komoritan | 0:735163979ecf | 189 | default: |
komoritan | 0:735163979ecf | 190 | sprintf( res, "unknown" ); |
komoritan | 0:735163979ecf | 191 | break; |
komoritan | 0:735163979ecf | 192 | } |
komoritan | 0:735163979ecf | 193 | return res; |
komoritan | 0:735163979ecf | 194 | } |
komoritan | 0:735163979ecf | 195 | char* dx_direction_to_string( dx_prop_direction dic, char* buf) |
komoritan | 0:735163979ecf | 196 | { |
komoritan | 0:735163979ecf | 197 | char *res = buf; |
komoritan | 0:735163979ecf | 198 | switch( dic ) { |
komoritan | 0:735163979ecf | 199 | case DX_UPONLY: |
komoritan | 0:735163979ecf | 200 | sprintf( res, "uponly" ); |
komoritan | 0:735163979ecf | 201 | break; |
komoritan | 0:735163979ecf | 202 | case DX_DOWNONLY: |
komoritan | 0:735163979ecf | 203 | sprintf( res, "downonly" ); |
komoritan | 0:735163979ecf | 204 | break; |
komoritan | 0:735163979ecf | 205 | case DX_UPDOWN: |
komoritan | 0:735163979ecf | 206 | sprintf( res, "updown" ); |
komoritan | 0:735163979ecf | 207 | break; |
komoritan | 0:735163979ecf | 208 | default: |
komoritan | 0:735163979ecf | 209 | sprintf( res, "unknown" ); |
komoritan | 0:735163979ecf | 210 | break; |
komoritan | 0:735163979ecf | 211 | } |
komoritan | 0:735163979ecf | 212 | return res; |
komoritan | 0:735163979ecf | 213 | } |
komoritan | 0:735163979ecf | 214 | char* dx_mode_to_string( dx_prop_mode mode, char* buf) |
komoritan | 0:735163979ecf | 215 | { |
komoritan | 0:735163979ecf | 216 | char *res = buf; |
komoritan | 0:735163979ecf | 217 | switch( mode ) { |
komoritan | 0:735163979ecf | 218 | case DX_READONLY: |
komoritan | 0:735163979ecf | 219 | sprintf( res, "readonly" ); |
komoritan | 0:735163979ecf | 220 | break; |
komoritan | 0:735163979ecf | 221 | case DX_WRITEONLY: |
komoritan | 0:735163979ecf | 222 | sprintf( res, "writeonly" ); |
komoritan | 0:735163979ecf | 223 | break; |
komoritan | 0:735163979ecf | 224 | case DX_READWRITE: |
komoritan | 0:735163979ecf | 225 | sprintf( res, "readwrite" ); |
komoritan | 0:735163979ecf | 226 | break; |
komoritan | 0:735163979ecf | 227 | default: |
komoritan | 0:735163979ecf | 228 | sprintf( res, "unknown" ); |
komoritan | 0:735163979ecf | 229 | break; |
komoritan | 0:735163979ecf | 230 | } |
komoritan | 0:735163979ecf | 231 | return res; |
komoritan | 0:735163979ecf | 232 | } |
komoritan | 0:735163979ecf | 233 | |
komoritan | 0:735163979ecf | 234 | // ============================================== |
komoritan | 0:735163979ecf | 235 | // |
komoritan | 0:735163979ecf | 236 | // ============================================== |
komoritan | 0:735163979ecf | 237 | bool DxClient::register_device( dx_props *props ) |
komoritan | 0:735163979ecf | 238 | { |
komoritan | 0:735163979ecf | 239 | DXDBG("Call register_device"); |
komoritan | 0:735163979ecf | 240 | |
komoritan | 0:735163979ecf | 241 | bool res; |
komoritan | 0:735163979ecf | 242 | int len; |
komoritan | 0:735163979ecf | 243 | |
komoritan | 0:735163979ecf | 244 | char message[MAX_MESSAGELEN]; |
komoritan | 0:735163979ecf | 245 | char mesid[MAX_MESSAGEIDLEN]; |
komoritan | 0:735163979ecf | 246 | generate_randomid( mesid, sizeof(mesid) ); |
komoritan | 0:735163979ecf | 247 | len = snprintf(message, sizeof(message), "{" \ |
komoritan | 0:735163979ecf | 248 | "\"type\":\"device-register-request\"," \ |
komoritan | 0:735163979ecf | 249 | "\"device-id\":\"%s\"," \ |
komoritan | 0:735163979ecf | 250 | "\"message-id\":\"%s\"," \ |
komoritan | 0:735163979ecf | 251 | "\"name\":\"%s\"," \ |
komoritan | 0:735163979ecf | 252 | "\"description\":\"%s\"," \ |
komoritan | 0:735163979ecf | 253 | "\"props\":{", |
komoritan | 0:735163979ecf | 254 | m_deviceid, |
komoritan | 0:735163979ecf | 255 | mesid, |
komoritan | 0:735163979ecf | 256 | m_dev_name, |
komoritan | 0:735163979ecf | 257 | m_dev_description |
komoritan | 0:735163979ecf | 258 | ); |
komoritan | 0:735163979ecf | 259 | if (len >= sizeof(message)-1) { |
komoritan | 0:735163979ecf | 260 | DXDBG( "REGISTER_DEVICE: message is over MAX LEN"); |
komoritan | 0:735163979ecf | 261 | return false; |
komoritan | 0:735163979ecf | 262 | } |
komoritan | 0:735163979ecf | 263 | |
komoritan | 0:735163979ecf | 264 | int i; |
komoritan | 0:735163979ecf | 265 | for (i=0;i<props->numofprops;i++) { |
komoritan | 0:735163979ecf | 266 | char s_prop[MAX_PROPLEN]; |
komoritan | 0:735163979ecf | 267 | char s_type[16]; |
komoritan | 0:735163979ecf | 268 | char s_direction[16]; |
komoritan | 0:735163979ecf | 269 | char s_mode[16]; |
komoritan | 0:735163979ecf | 270 | |
komoritan | 0:735163979ecf | 271 | char s_val[MAX_PROPSVALLEN]; |
komoritan | 0:735163979ecf | 272 | switch( props->props[i].type ){ |
komoritan | 0:735163979ecf | 273 | case DX_STRING: |
komoritan | 0:735163979ecf | 274 | sprintf( s_val, "\"%s\"", props->props[i].s_val ); |
komoritan | 0:735163979ecf | 275 | break; |
komoritan | 0:735163979ecf | 276 | case DX_INTEGER: |
komoritan | 0:735163979ecf | 277 | sprintf( s_val, "%d", (int)(props->props[i].f_val) ); |
komoritan | 0:735163979ecf | 278 | break; |
komoritan | 0:735163979ecf | 279 | case DX_FLOAT: |
komoritan | 0:735163979ecf | 280 | sprintf( s_val, "%f", props->props[i].f_val ); |
komoritan | 0:735163979ecf | 281 | break; |
komoritan | 0:735163979ecf | 282 | case DX_BOOLEAN: |
komoritan | 0:735163979ecf | 283 | sprintf( s_val, "%s", props->props[i].b_val?"true":"false" ); |
komoritan | 0:735163979ecf | 284 | break; |
komoritan | 0:735163979ecf | 285 | } |
komoritan | 0:735163979ecf | 286 | |
komoritan | 0:735163979ecf | 287 | len = snprintf( s_prop, sizeof(s_prop), "\"%s\": {" \ |
komoritan | 0:735163979ecf | 288 | "\"value\":%s," \ |
komoritan | 0:735163979ecf | 289 | "\"type\":\"%s\"," \ |
komoritan | 0:735163979ecf | 290 | "\"direction\":\"%s\"," \ |
komoritan | 0:735163979ecf | 291 | "\"mode\":\"%s\"" \ |
komoritan | 0:735163979ecf | 292 | "}", |
komoritan | 0:735163979ecf | 293 | props->props[i].name, |
komoritan | 0:735163979ecf | 294 | s_val, |
komoritan | 0:735163979ecf | 295 | dx_type_to_string( props->props[i].type, s_type ), |
komoritan | 0:735163979ecf | 296 | dx_direction_to_string( props->props[i].direction, s_direction ), |
komoritan | 0:735163979ecf | 297 | dx_mode_to_string( props->props[i].mode, s_mode ) |
komoritan | 0:735163979ecf | 298 | ); |
komoritan | 0:735163979ecf | 299 | if (len >= sizeof(s_prop)-1) { |
komoritan | 0:735163979ecf | 300 | DXDBG( "REGISTER_DEVICE: prop is over MAX LEN"); |
komoritan | 0:735163979ecf | 301 | return false; |
komoritan | 0:735163979ecf | 302 | } |
komoritan | 0:735163979ecf | 303 | if (len + strlen(message) >= sizeof(message)-1) { |
komoritan | 0:735163979ecf | 304 | DXDBG( "REGISTER_DEVICE: message is over MAX LEN"); |
komoritan | 0:735163979ecf | 305 | return false; |
komoritan | 0:735163979ecf | 306 | } |
komoritan | 0:735163979ecf | 307 | strcat( message, s_prop ); |
komoritan | 0:735163979ecf | 308 | |
komoritan | 0:735163979ecf | 309 | if (i+1 < props->numofprops) { |
komoritan | 0:735163979ecf | 310 | if (1 + strlen(message) >= sizeof(message)-1) { |
komoritan | 0:735163979ecf | 311 | DXDBG( "REGISTER_DEVICE: message is over MAX LEN"); |
komoritan | 0:735163979ecf | 312 | return false; |
komoritan | 0:735163979ecf | 313 | } |
komoritan | 0:735163979ecf | 314 | strncat( message, ",", sizeof(message) ); |
komoritan | 0:735163979ecf | 315 | } |
komoritan | 0:735163979ecf | 316 | } |
komoritan | 0:735163979ecf | 317 | |
komoritan | 0:735163979ecf | 318 | if (2 + strlen(message) >= sizeof(message)-1) { |
komoritan | 0:735163979ecf | 319 | DXDBG( "REGISTER_DEVICE: message is over MAX LEN"); |
komoritan | 0:735163979ecf | 320 | return false; |
komoritan | 0:735163979ecf | 321 | } |
komoritan | 0:735163979ecf | 322 | strcat( message, "}}" ); |
komoritan | 0:735163979ecf | 323 | |
komoritan | 0:735163979ecf | 324 | DXDBG("send: %s", message); |
komoritan | 0:735163979ecf | 325 | m_ws.send(message); |
komoritan | 0:735163979ecf | 326 | |
komoritan | 0:735163979ecf | 327 | // clear the buffer and wait a sec... |
komoritan | 0:735163979ecf | 328 | memset( message, 0, sizeof(message)); |
komoritan | 0:735163979ecf | 329 | for (i=0, res=false;i<10;i++) { |
komoritan | 0:735163979ecf | 330 | wait(0.5f); |
komoritan | 0:735163979ecf | 331 | if (m_ws.read(message)) { |
komoritan | 0:735163979ecf | 332 | DXDBG("Get Message(%d): %s", strlen(message), message); |
komoritan | 0:735163979ecf | 333 | picojson::value v; |
komoritan | 0:735163979ecf | 334 | std::string err; |
komoritan | 0:735163979ecf | 335 | picojson::parse(v, (const char *)message, (const char *)(message + strlen(message)), &err); |
komoritan | 0:735163979ecf | 336 | |
komoritan | 0:735163979ecf | 337 | if (err.empty()) |
komoritan | 0:735163979ecf | 338 | { |
komoritan | 0:735163979ecf | 339 | picojson::object& root = v.get<picojson::object>(); |
komoritan | 0:735163979ecf | 340 | // check auth response |
komoritan | 0:735163979ecf | 341 | if (root["type"].is<std::string>()) { |
komoritan | 0:735163979ecf | 342 | if (strcmp(root["type"].get<std::string>().c_str(), "device-register-response")) { |
komoritan | 0:735163979ecf | 343 | DXDBG("REGISTER_DEVICE: Not device_register_response"); |
komoritan | 0:735163979ecf | 344 | break; |
komoritan | 0:735163979ecf | 345 | } |
komoritan | 0:735163979ecf | 346 | } else { |
komoritan | 0:735163979ecf | 347 | DXDBG("REGISTER_DEVICE: there is no type"); |
komoritan | 0:735163979ecf | 348 | break; |
komoritan | 0:735163979ecf | 349 | } |
komoritan | 0:735163979ecf | 350 | if (root["message-id"].is<std::string>()) { |
komoritan | 0:735163979ecf | 351 | if (strcmp(root["message-id"].get<std::string>().c_str(), mesid)) { |
komoritan | 0:735163979ecf | 352 | DXDBG("REGISTER_DEVICE: Not correct response message-id"); |
komoritan | 0:735163979ecf | 353 | break; |
komoritan | 0:735163979ecf | 354 | } |
komoritan | 0:735163979ecf | 355 | } else { |
komoritan | 0:735163979ecf | 356 | DXDBG("REGISTER_DEVICE: there is no message-id"); |
komoritan | 0:735163979ecf | 357 | break; |
komoritan | 0:735163979ecf | 358 | } |
komoritan | 0:735163979ecf | 359 | if (root["status"].is<std::string>()) { |
komoritan | 0:735163979ecf | 360 | if (strcmp(root["status"].get<std::string>().c_str(), "200 OK")) { |
komoritan | 0:735163979ecf | 361 | DXDBG("REGISTER_DEVICE: status error"); |
komoritan | 0:735163979ecf | 362 | break; |
komoritan | 0:735163979ecf | 363 | } |
komoritan | 0:735163979ecf | 364 | } else { |
komoritan | 0:735163979ecf | 365 | DXDBG("REGISTER_DEVICE: there is no status"); |
komoritan | 0:735163979ecf | 366 | break; |
komoritan | 0:735163979ecf | 367 | } |
komoritan | 0:735163979ecf | 368 | DXDBG("REGISTER_DEVICE: device register is success"); |
komoritan | 0:735163979ecf | 369 | res = true; |
komoritan | 0:735163979ecf | 370 | } else { |
komoritan | 0:735163979ecf | 371 | DXDBG("REGISTER_DEVICE: Parse Error"); |
komoritan | 0:735163979ecf | 372 | } |
komoritan | 0:735163979ecf | 373 | break; |
komoritan | 0:735163979ecf | 374 | } |
komoritan | 0:735163979ecf | 375 | } |
komoritan | 0:735163979ecf | 376 | |
komoritan | 0:735163979ecf | 377 | return res; |
komoritan | 0:735163979ecf | 378 | } |
komoritan | 0:735163979ecf | 379 | |
komoritan | 0:735163979ecf | 380 | // ============================================== |
komoritan | 0:735163979ecf | 381 | // |
komoritan | 0:735163979ecf | 382 | // ============================================== |
komoritan | 0:735163979ecf | 383 | bool DxClient::deregister_device() |
komoritan | 0:735163979ecf | 384 | { |
komoritan | 0:735163979ecf | 385 | int len; |
komoritan | 0:735163979ecf | 386 | |
komoritan | 0:735163979ecf | 387 | char message[MAX_MESSAGELEN]; |
komoritan | 0:735163979ecf | 388 | char mesid[MAX_MESSAGEIDLEN]; |
komoritan | 0:735163979ecf | 389 | |
komoritan | 0:735163979ecf | 390 | generate_randomid( mesid, sizeof(mesid) ); |
komoritan | 0:735163979ecf | 391 | len = snprintf(message, sizeof(message), "{" \ |
komoritan | 0:735163979ecf | 392 | "\"type\":\"device-deregister-request\"," \ |
komoritan | 0:735163979ecf | 393 | "\"device-id\":\"%s\"," \ |
komoritan | 0:735163979ecf | 394 | "\"message-id\":\"%s\"" \ |
komoritan | 0:735163979ecf | 395 | "}", |
komoritan | 0:735163979ecf | 396 | m_deviceid, |
komoritan | 0:735163979ecf | 397 | mesid |
komoritan | 0:735163979ecf | 398 | ); |
komoritan | 0:735163979ecf | 399 | if (len >= sizeof(message)-1) { |
komoritan | 0:735163979ecf | 400 | DXDBG( "DEREGISTER_DEVICE: message is over MAX LEN"); |
komoritan | 0:735163979ecf | 401 | return false; |
komoritan | 0:735163979ecf | 402 | } |
komoritan | 0:735163979ecf | 403 | |
komoritan | 0:735163979ecf | 404 | DXDBG("send: %s", message); |
komoritan | 0:735163979ecf | 405 | m_ws.send(message); |
komoritan | 0:735163979ecf | 406 | |
komoritan | 0:735163979ecf | 407 | // clear the buffer and wait a sec... |
komoritan | 0:735163979ecf | 408 | bool res; |
komoritan | 0:735163979ecf | 409 | int i; |
komoritan | 0:735163979ecf | 410 | memset( message, 0, sizeof(message)); |
komoritan | 0:735163979ecf | 411 | for (i=0, res=false;i<10;i++) { |
komoritan | 0:735163979ecf | 412 | wait(0.5f); |
komoritan | 0:735163979ecf | 413 | if (m_ws.read(message)) { |
komoritan | 0:735163979ecf | 414 | DXDBG("Get Message(%d): %s", strlen(message), message); |
komoritan | 0:735163979ecf | 415 | picojson::value v; |
komoritan | 0:735163979ecf | 416 | std::string err; |
komoritan | 0:735163979ecf | 417 | picojson::parse(v, (const char *)message, (const char *)(message + strlen(message)), &err); |
komoritan | 0:735163979ecf | 418 | |
komoritan | 0:735163979ecf | 419 | if (err.empty()) |
komoritan | 0:735163979ecf | 420 | { |
komoritan | 0:735163979ecf | 421 | picojson::object& root = v.get<picojson::object>(); |
komoritan | 0:735163979ecf | 422 | // check auth response |
komoritan | 0:735163979ecf | 423 | if (root["type"].is<std::string>()) { |
komoritan | 0:735163979ecf | 424 | if (strcmp(root["type"].get<std::string>().c_str(), "device-deregister-response")) { |
komoritan | 0:735163979ecf | 425 | DXDBG("DEREGISTER_DEVICE: Not device_deregister_response"); |
komoritan | 0:735163979ecf | 426 | break; |
komoritan | 0:735163979ecf | 427 | } |
komoritan | 0:735163979ecf | 428 | } else { |
komoritan | 0:735163979ecf | 429 | DXDBG("DEREGISTER_DEVICE: there is no type"); |
komoritan | 0:735163979ecf | 430 | break; |
komoritan | 0:735163979ecf | 431 | } |
komoritan | 0:735163979ecf | 432 | if (root["message-id"].is<std::string>()) { |
komoritan | 0:735163979ecf | 433 | if (strcmp(root["message-id"].get<std::string>().c_str(), mesid)) { |
komoritan | 0:735163979ecf | 434 | DXDBG("DEREGISTER_DEVICE: Not correct response message-id"); |
komoritan | 0:735163979ecf | 435 | break; |
komoritan | 0:735163979ecf | 436 | } |
komoritan | 0:735163979ecf | 437 | } else { |
komoritan | 0:735163979ecf | 438 | DXDBG("REGISTER_DEVICE: there is no message-id"); |
komoritan | 0:735163979ecf | 439 | break; |
komoritan | 0:735163979ecf | 440 | } |
komoritan | 0:735163979ecf | 441 | if (root["status"].is<std::string>()) { |
komoritan | 0:735163979ecf | 442 | if (strcmp(root["status"].get<std::string>().c_str(), "200 OK")) { |
komoritan | 0:735163979ecf | 443 | DXDBG("DEREGISTER_DEVICE: status error"); |
komoritan | 0:735163979ecf | 444 | break; |
komoritan | 0:735163979ecf | 445 | } |
komoritan | 0:735163979ecf | 446 | } else { |
komoritan | 0:735163979ecf | 447 | DXDBG("DEREGISTER_DEVICE: there is no status"); |
komoritan | 0:735163979ecf | 448 | break; |
komoritan | 0:735163979ecf | 449 | } |
komoritan | 0:735163979ecf | 450 | DXDBG("DEREGISTER_DEVICE: device deregister is success"); |
komoritan | 0:735163979ecf | 451 | res = true; |
komoritan | 0:735163979ecf | 452 | } else { |
komoritan | 0:735163979ecf | 453 | DXDBG("DEREGISTER_DEVICE: Parse Error"); |
komoritan | 0:735163979ecf | 454 | } |
komoritan | 0:735163979ecf | 455 | break; |
komoritan | 0:735163979ecf | 456 | } |
komoritan | 0:735163979ecf | 457 | } |
komoritan | 0:735163979ecf | 458 | |
komoritan | 0:735163979ecf | 459 | return res; |
komoritan | 0:735163979ecf | 460 | } |
komoritan | 0:735163979ecf | 461 | |
komoritan | 0:735163979ecf | 462 | // ============================================== |
komoritan | 0:735163979ecf | 463 | // |
komoritan | 0:735163979ecf | 464 | // ============================================== |
komoritan | 0:735163979ecf | 465 | bool DxClient::update_device( dx_props *props ) |
komoritan | 0:735163979ecf | 466 | { |
komoritan | 0:735163979ecf | 467 | int len; |
komoritan | 0:735163979ecf | 468 | |
komoritan | 0:735163979ecf | 469 | char message[MAX_MESSAGELEN]; |
komoritan | 0:735163979ecf | 470 | char mesid[MAX_MESSAGEIDLEN]; |
komoritan | 0:735163979ecf | 471 | |
komoritan | 0:735163979ecf | 472 | generate_randomid( mesid, sizeof(mesid) ); |
komoritan | 0:735163979ecf | 473 | len = snprintf(message, sizeof(message), "{" \ |
komoritan | 0:735163979ecf | 474 | "\"type\":\"device-update-request\"," \ |
komoritan | 0:735163979ecf | 475 | "\"device-id\":\"%s\"," \ |
komoritan | 0:735163979ecf | 476 | "\"message-id\":\"%s\"," \ |
komoritan | 0:735163979ecf | 477 | "\"props\":{", |
komoritan | 0:735163979ecf | 478 | m_deviceid, |
komoritan | 0:735163979ecf | 479 | mesid |
komoritan | 0:735163979ecf | 480 | ); |
komoritan | 0:735163979ecf | 481 | if (len >= sizeof(message)-1) { |
komoritan | 0:735163979ecf | 482 | DXDBG( "UPDATE_DEVICE: message is over MAX LEN"); |
komoritan | 0:735163979ecf | 483 | return false; |
komoritan | 0:735163979ecf | 484 | } |
komoritan | 0:735163979ecf | 485 | |
komoritan | 0:735163979ecf | 486 | int i; |
komoritan | 0:735163979ecf | 487 | for (i=0;i<props->numofprops;i++) { |
komoritan | 0:735163979ecf | 488 | char s_prop[MAX_PROPLEN]; |
komoritan | 0:735163979ecf | 489 | char s_val[MAX_PROPSVALLEN]; |
komoritan | 0:735163979ecf | 490 | switch( props->props[i].type ){ |
komoritan | 0:735163979ecf | 491 | case DX_STRING: |
komoritan | 0:735163979ecf | 492 | sprintf( s_val, "\"%s\"", props->props[i].s_val ); |
komoritan | 0:735163979ecf | 493 | break; |
komoritan | 0:735163979ecf | 494 | case DX_INTEGER: |
komoritan | 0:735163979ecf | 495 | sprintf( s_val, "%d", (int)(props->props[i].f_val) ); |
komoritan | 0:735163979ecf | 496 | break; |
komoritan | 0:735163979ecf | 497 | case DX_FLOAT: |
komoritan | 0:735163979ecf | 498 | sprintf( s_val, "%f", props->props[i].f_val ); |
komoritan | 0:735163979ecf | 499 | break; |
komoritan | 0:735163979ecf | 500 | case DX_BOOLEAN: |
komoritan | 0:735163979ecf | 501 | sprintf( s_val, "%s", props->props[i].b_val?"true":"false" ); |
komoritan | 0:735163979ecf | 502 | break; |
komoritan | 0:735163979ecf | 503 | } |
komoritan | 0:735163979ecf | 504 | |
komoritan | 0:735163979ecf | 505 | len = snprintf( s_prop, sizeof(s_prop), "\"%s\": {" \ |
komoritan | 0:735163979ecf | 506 | "\"value\":%s" \ |
komoritan | 0:735163979ecf | 507 | "}", |
komoritan | 0:735163979ecf | 508 | props->props[i].name, |
komoritan | 0:735163979ecf | 509 | s_val |
komoritan | 0:735163979ecf | 510 | ); |
komoritan | 0:735163979ecf | 511 | if (len >= sizeof(s_prop)-1) { |
komoritan | 0:735163979ecf | 512 | DXDBG( "UPDATE_DEVICE: prop is over MAX LEN"); |
komoritan | 0:735163979ecf | 513 | return false; |
komoritan | 0:735163979ecf | 514 | } |
komoritan | 0:735163979ecf | 515 | if (len + strlen(message) >= sizeof(message)-1) { |
komoritan | 0:735163979ecf | 516 | DXDBG( "UPDATE_DEVICE: message is over MAX LEN"); |
komoritan | 0:735163979ecf | 517 | return false; |
komoritan | 0:735163979ecf | 518 | } |
komoritan | 0:735163979ecf | 519 | strcat( message, s_prop ); |
komoritan | 0:735163979ecf | 520 | |
komoritan | 0:735163979ecf | 521 | if (i+1 < props->numofprops) { |
komoritan | 0:735163979ecf | 522 | if (1 + strlen(message) >= sizeof(message)-1) { |
komoritan | 0:735163979ecf | 523 | DXDBG( "UPDATE_DEVICE: message is over MAX LEN"); |
komoritan | 0:735163979ecf | 524 | return false; |
komoritan | 0:735163979ecf | 525 | } |
komoritan | 0:735163979ecf | 526 | strncat( message, ",", sizeof(message) ); |
komoritan | 0:735163979ecf | 527 | } |
komoritan | 0:735163979ecf | 528 | } |
komoritan | 0:735163979ecf | 529 | |
komoritan | 0:735163979ecf | 530 | if (2 + strlen(message) >= sizeof(message)-1) { |
komoritan | 0:735163979ecf | 531 | DXDBG( "UPDATE_DEVICE: message is over MAX LEN"); |
komoritan | 0:735163979ecf | 532 | return false; |
komoritan | 0:735163979ecf | 533 | } |
komoritan | 0:735163979ecf | 534 | strcat( message, "}}" ); |
komoritan | 0:735163979ecf | 535 | |
komoritan | 0:735163979ecf | 536 | DXDBG("send: %s", message); |
komoritan | 0:735163979ecf | 537 | m_ws.send(message); |
komoritan | 0:735163979ecf | 538 | |
komoritan | 0:735163979ecf | 539 | bool res=true; |
komoritan | 0:735163979ecf | 540 | /* |
komoritan | 0:735163979ecf | 541 | // clear the buffer and wait a sec... |
komoritan | 0:735163979ecf | 542 | memset( message, 0, sizeof(message)); |
komoritan | 0:735163979ecf | 543 | for (i=0, res=false;i<10;i++) { |
komoritan | 0:735163979ecf | 544 | wait(0.5f); |
komoritan | 0:735163979ecf | 545 | if (m_ws.read(message)) { |
komoritan | 0:735163979ecf | 546 | DXDBG("Get Message(%d): %s", strlen(message), message); |
komoritan | 0:735163979ecf | 547 | picojson::value v; |
komoritan | 0:735163979ecf | 548 | std::string err; |
komoritan | 0:735163979ecf | 549 | picojson::parse(v, (const char *)message, (const char *)(message + strlen(message)), &err); |
komoritan | 0:735163979ecf | 550 | |
komoritan | 0:735163979ecf | 551 | if (err.empty()) |
komoritan | 0:735163979ecf | 552 | { |
komoritan | 0:735163979ecf | 553 | picojson::object& root = v.get<picojson::object>(); |
komoritan | 0:735163979ecf | 554 | // check auth response |
komoritan | 0:735163979ecf | 555 | if (root["type"].is<std::string>()) { |
komoritan | 0:735163979ecf | 556 | if (strcmp(root["type"].get<std::string>().c_str(), "device-update-response")) { |
komoritan | 0:735163979ecf | 557 | DXDBG("UPDATE_DEVICE: Not device_update_response"); |
komoritan | 0:735163979ecf | 558 | break; |
komoritan | 0:735163979ecf | 559 | } |
komoritan | 0:735163979ecf | 560 | } else { |
komoritan | 0:735163979ecf | 561 | DXDBG("UPDATE_DEVICE: there is no type"); |
komoritan | 0:735163979ecf | 562 | break; |
komoritan | 0:735163979ecf | 563 | } |
komoritan | 0:735163979ecf | 564 | if (root["message-id"].is<std::string>()) { |
komoritan | 0:735163979ecf | 565 | if (strcmp(root["message-id"].get<std::string>().c_str(), mesid)) { |
komoritan | 0:735163979ecf | 566 | DXDBG("UPDATE_DEVICE: Not correct response message-id"); |
komoritan | 0:735163979ecf | 567 | break; |
komoritan | 0:735163979ecf | 568 | } |
komoritan | 0:735163979ecf | 569 | } else { |
komoritan | 0:735163979ecf | 570 | DXDBG("UPDATE_DEVICE: there is no message-id"); |
komoritan | 0:735163979ecf | 571 | break; |
komoritan | 0:735163979ecf | 572 | } |
komoritan | 0:735163979ecf | 573 | if (root["status"].is<std::string>()) { |
komoritan | 0:735163979ecf | 574 | if (strcmp(root["status"].get<std::string>().c_str(), "200 OK")) { |
komoritan | 0:735163979ecf | 575 | DXDBG("UPDATE_DEVICE: status error"); |
komoritan | 0:735163979ecf | 576 | break; |
komoritan | 0:735163979ecf | 577 | } |
komoritan | 0:735163979ecf | 578 | } else { |
komoritan | 0:735163979ecf | 579 | DXDBG("UPDATE_DEVICE: there is no status"); |
komoritan | 0:735163979ecf | 580 | break; |
komoritan | 0:735163979ecf | 581 | } |
komoritan | 0:735163979ecf | 582 | DXDBG("UPDATE_DEVICE: device update is success"); |
komoritan | 0:735163979ecf | 583 | res = true; |
komoritan | 0:735163979ecf | 584 | } else { |
komoritan | 0:735163979ecf | 585 | DXDBG("UPDATE_DEVICE: Parse Error"); |
komoritan | 0:735163979ecf | 586 | } |
komoritan | 0:735163979ecf | 587 | break; |
komoritan | 0:735163979ecf | 588 | } |
komoritan | 0:735163979ecf | 589 | } |
komoritan | 0:735163979ecf | 590 | |
komoritan | 0:735163979ecf | 591 | */ |
komoritan | 0:735163979ecf | 592 | return res; |
komoritan | 0:735163979ecf | 593 | } |
komoritan | 0:735163979ecf | 594 | |
komoritan | 0:735163979ecf | 595 | |
komoritan | 0:735163979ecf | 596 | // ============================================== |
komoritan | 0:735163979ecf | 597 | // |
komoritan | 0:735163979ecf | 598 | // ============================================== |
komoritan | 0:735163979ecf | 599 | bool DxClient::keepalive_device() |
komoritan | 0:735163979ecf | 600 | { |
komoritan | 0:735163979ecf | 601 | DXDBG("Call keepalive_device"); |
komoritan | 0:735163979ecf | 602 | |
komoritan | 0:735163979ecf | 603 | int len; |
komoritan | 0:735163979ecf | 604 | |
komoritan | 0:735163979ecf | 605 | char message[MAX_MESSAGELEN]; |
komoritan | 0:735163979ecf | 606 | char mesid[MAX_MESSAGEIDLEN]; |
komoritan | 0:735163979ecf | 607 | generate_randomid( mesid, sizeof(mesid) ); |
komoritan | 0:735163979ecf | 608 | len = snprintf(message, sizeof(message), "{" \ |
komoritan | 0:735163979ecf | 609 | "\"type\":\"keep-alive-request\"," \ |
komoritan | 0:735163979ecf | 610 | "\"device-id\":\"%s\"," \ |
komoritan | 0:735163979ecf | 611 | "\"message-id\":\"%s\"" \ |
komoritan | 0:735163979ecf | 612 | "}", |
komoritan | 0:735163979ecf | 613 | m_deviceid, |
komoritan | 0:735163979ecf | 614 | mesid |
komoritan | 0:735163979ecf | 615 | ); |
komoritan | 0:735163979ecf | 616 | if (len >= sizeof(message)-1) { |
komoritan | 0:735163979ecf | 617 | DXDBG( "KEEPALIVE_DEVICE: message is over MAX LEN"); |
komoritan | 0:735163979ecf | 618 | return false; |
komoritan | 0:735163979ecf | 619 | } |
komoritan | 0:735163979ecf | 620 | DXDBG("send: %s", message); |
komoritan | 0:735163979ecf | 621 | |
komoritan | 0:735163979ecf | 622 | m_ws.send(message); |
komoritan | 0:735163979ecf | 623 | |
komoritan | 0:735163979ecf | 624 | bool res=true; |
komoritan | 0:735163979ecf | 625 | /* |
komoritan | 0:735163979ecf | 626 | int i; |
komoritan | 0:735163979ecf | 627 | // clear the buffer and wait a sec... |
komoritan | 0:735163979ecf | 628 | memset( message, 0, sizeof(message)); |
komoritan | 0:735163979ecf | 629 | for (i=0, res=true;i<10;i++) { |
komoritan | 0:735163979ecf | 630 | wait(0.5f); |
komoritan | 0:735163979ecf | 631 | if (m_ws.read(message)) { |
komoritan | 0:735163979ecf | 632 | DXDBG("Get Message: %s\n", message); |
komoritan | 0:735163979ecf | 633 | picojson::value v; |
komoritan | 0:735163979ecf | 634 | std::string err; |
komoritan | 0:735163979ecf | 635 | picojson::parse(v, (const char *)message, (const char *)(message + strlen(message)), &err); |
komoritan | 0:735163979ecf | 636 | |
komoritan | 0:735163979ecf | 637 | |
komoritan | 0:735163979ecf | 638 | if (err.empty()) |
komoritan | 0:735163979ecf | 639 | { |
komoritan | 0:735163979ecf | 640 | picojson::object& root = v.get<picojson::object>(); |
komoritan | 0:735163979ecf | 641 | |
komoritan | 0:735163979ecf | 642 | // check auth response |
komoritan | 0:735163979ecf | 643 | if (root["type"].is<std::string>()) { |
komoritan | 0:735163979ecf | 644 | if (strcmp(root["type"].get<std::string>().c_str(), "keep-alive-response")) { |
komoritan | 0:735163979ecf | 645 | DXDBG("KEEPALIVE_DEVICE: Not keep-alive-response\n"); |
komoritan | 0:735163979ecf | 646 | break; |
komoritan | 0:735163979ecf | 647 | } |
komoritan | 0:735163979ecf | 648 | } else { |
komoritan | 0:735163979ecf | 649 | DXDBG("KEEPALIVE_DEVICE: there is no type\n"); |
komoritan | 0:735163979ecf | 650 | break; |
komoritan | 0:735163979ecf | 651 | } |
komoritan | 0:735163979ecf | 652 | if (root["message-id"].is<std::string>()) { |
komoritan | 0:735163979ecf | 653 | if (strcmp(root["message-id"].get<std::string>().c_str(), mesid)) { |
komoritan | 0:735163979ecf | 654 | DXDBG("KEEPALIVE_DEVICE: Not correct response message-id\n"); |
komoritan | 0:735163979ecf | 655 | break; |
komoritan | 0:735163979ecf | 656 | } |
komoritan | 0:735163979ecf | 657 | } else { |
komoritan | 0:735163979ecf | 658 | DXDBG("KEEPALIVE_DEVICE: there is no message-id\n"); |
komoritan | 0:735163979ecf | 659 | break; |
komoritan | 0:735163979ecf | 660 | } |
komoritan | 0:735163979ecf | 661 | if (root["status"].is<std::string>()) { |
komoritan | 0:735163979ecf | 662 | if (strcmp(root["status"].get<std::string>().c_str(), "200 OK")) { |
komoritan | 0:735163979ecf | 663 | DXDBG("KEEPALIVE_DEVICE: status error\n"); |
komoritan | 0:735163979ecf | 664 | break; |
komoritan | 0:735163979ecf | 665 | } |
komoritan | 0:735163979ecf | 666 | } else { |
komoritan | 0:735163979ecf | 667 | DXDBG("KEEPALIVE_DEVICE: there is no status\n"); |
komoritan | 0:735163979ecf | 668 | break; |
komoritan | 0:735163979ecf | 669 | } |
komoritan | 0:735163979ecf | 670 | |
komoritan | 0:735163979ecf | 671 | DXDBG("KEEPALIVE_DEVICE: keep alive is success\n"); |
komoritan | 0:735163979ecf | 672 | res = true; |
komoritan | 0:735163979ecf | 673 | } else { |
komoritan | 0:735163979ecf | 674 | DXDBG("KEEPALIVE_DEVICE: JSON parse Error\n"); |
komoritan | 0:735163979ecf | 675 | } |
komoritan | 0:735163979ecf | 676 | break; |
komoritan | 0:735163979ecf | 677 | } |
komoritan | 0:735163979ecf | 678 | } |
komoritan | 0:735163979ecf | 679 | */ |
komoritan | 0:735163979ecf | 680 | return res; |
komoritan | 0:735163979ecf | 681 | } |
komoritan | 0:735163979ecf | 682 | |
komoritan | 0:735163979ecf | 683 | |
komoritan | 0:735163979ecf | 684 | // ============================================== |
komoritan | 0:735163979ecf | 685 | // |
komoritan | 0:735163979ecf | 686 | // ============================================== |
komoritan | 0:735163979ecf | 687 | bool DxClient::handle_messages() |
komoritan | 0:735163979ecf | 688 | { |
komoritan | 0:735163979ecf | 689 | bool res = true; |
komoritan | 0:735163979ecf | 690 | |
komoritan | 0:735163979ecf | 691 | char message[MAX_MESSAGELEN]; |
komoritan | 0:735163979ecf | 692 | |
komoritan | 0:735163979ecf | 693 | |
komoritan | 0:735163979ecf | 694 | // clear the buffer and wait a sec... |
komoritan | 0:735163979ecf | 695 | while(1) { |
komoritan | 0:735163979ecf | 696 | memset( message, 0, sizeof(message)); |
komoritan | 0:735163979ecf | 697 | if (m_ws.read(message)) { |
komoritan | 0:735163979ecf | 698 | DXDBG("Get Message(%d): %s", strlen(message), message); |
komoritan | 0:735163979ecf | 699 | picojson::value v; |
komoritan | 0:735163979ecf | 700 | std::string err; |
komoritan | 0:735163979ecf | 701 | picojson::parse(v, (const char *)message, (const char *)(message + strlen(message)), &err); |
komoritan | 0:735163979ecf | 702 | |
komoritan | 0:735163979ecf | 703 | if (err.empty()) |
komoritan | 0:735163979ecf | 704 | { |
komoritan | 0:735163979ecf | 705 | picojson::object& root = v.get<picojson::object>(); |
komoritan | 0:735163979ecf | 706 | // check auth response |
komoritan | 0:735163979ecf | 707 | if (root["message-id"].is<std::string>()) { |
komoritan | 0:735163979ecf | 708 | // no check message-d |
komoritan | 0:735163979ecf | 709 | } else { |
komoritan | 0:735163979ecf | 710 | DXDBG("HANDLE_MESSAGE: there is no message-id"); |
komoritan | 0:735163979ecf | 711 | continue; |
komoritan | 0:735163979ecf | 712 | } |
komoritan | 0:735163979ecf | 713 | if (root["device-id"].is<std::string>()) { |
komoritan | 0:735163979ecf | 714 | if (strcmp(root["device-id"].get<std::string>().c_str(), m_deviceid)) { |
komoritan | 0:735163979ecf | 715 | DXDBG("HANDLE_MESSAGE: different device-id"); |
komoritan | 0:735163979ecf | 716 | continue; |
komoritan | 0:735163979ecf | 717 | } |
komoritan | 0:735163979ecf | 718 | } else { |
komoritan | 0:735163979ecf | 719 | DXDBG("HANDLE_MESSAGE: there is no device-id"); |
komoritan | 0:735163979ecf | 720 | continue; |
komoritan | 0:735163979ecf | 721 | } |
komoritan | 0:735163979ecf | 722 | |
komoritan | 0:735163979ecf | 723 | if (root["type"].is<std::string>()) { |
komoritan | 0:735163979ecf | 724 | if (!strcmp(root["type"].get<std::string>().c_str(), "device-get-request") |
komoritan | 0:735163979ecf | 725 | || !strcmp(root["type"].get<std::string>().c_str(), "device-set-request")) { |
komoritan | 0:735163979ecf | 726 | DXDBG("HANDLE_MESSAGE: recv %s", root["type"].get<std::string>().c_str()); |
komoritan | 0:735163979ecf | 727 | |
komoritan | 0:735163979ecf | 728 | // if ( 1) { |
komoritan | 0:735163979ecf | 729 | if(root["props"].is<picojson::object>() ){ |
komoritan | 0:735163979ecf | 730 | // picojson::object& props_root = root; |
komoritan | 0:735163979ecf | 731 | picojson::object& props_root = root["props"].get<picojson::object>(); |
komoritan | 0:735163979ecf | 732 | |
komoritan | 0:735163979ecf | 733 | dx_props ps; |
komoritan | 0:735163979ecf | 734 | ps.numofprops = props_root.size(); |
komoritan | 0:735163979ecf | 735 | DXDBG("HANDLE_MESSAGE: prop size: %d", ps.numofprops); |
komoritan | 0:735163979ecf | 736 | |
komoritan | 0:735163979ecf | 737 | ps.props = (dx_prop*)malloc( sizeof(dx_prop) * ps.numofprops ); |
komoritan | 0:735163979ecf | 738 | if (ps.props == NULL) { |
komoritan | 0:735163979ecf | 739 | DXDBG("HANDLE_MESSAGE: No memory"); |
komoritan | 0:735163979ecf | 740 | continue; |
komoritan | 0:735163979ecf | 741 | } |
komoritan | 0:735163979ecf | 742 | memset( ps.props, 0, sizeof(dx_prop) * ps.numofprops ); |
komoritan | 0:735163979ecf | 743 | |
komoritan | 0:735163979ecf | 744 | dx_prop *pr = ps.props; |
komoritan | 0:735163979ecf | 745 | for (picojson::object::const_iterator it = props_root.begin(); it != props_root.end(); it++,pr++) { |
komoritan | 0:735163979ecf | 746 | if (props_root[it->first].is<picojson::object>()) { |
komoritan | 0:735163979ecf | 747 | picojson::object& prop = props_root[it->first].get<picojson::object>(); |
komoritan | 0:735163979ecf | 748 | |
komoritan | 0:735163979ecf | 749 | if (prop["value"].is<std::string>()) { // test only |
komoritan | 0:735163979ecf | 750 | snprintf( pr->s_val, sizeof(pr->s_val), "%s", prop["value"].get<std::string>().c_str() ); |
komoritan | 0:735163979ecf | 751 | } |
komoritan | 0:735163979ecf | 752 | else if (prop["value"].is<double>()) { // integer or float |
komoritan | 0:735163979ecf | 753 | pr->f_val = prop["value"].get<double>(); |
komoritan | 0:735163979ecf | 754 | if ( (int)(pr->f_val) ) { |
komoritan | 0:735163979ecf | 755 | pr->b_val = true; |
komoritan | 0:735163979ecf | 756 | } else { |
komoritan | 0:735163979ecf | 757 | pr->b_val = false; |
komoritan | 0:735163979ecf | 758 | } |
komoritan | 0:735163979ecf | 759 | } |
komoritan | 0:735163979ecf | 760 | else if (prop["value"].is<bool>()) { // integer or float |
komoritan | 0:735163979ecf | 761 | pr->b_val = prop["value"].get<bool>(); |
komoritan | 0:735163979ecf | 762 | if(pr->b_val){ |
komoritan | 0:735163979ecf | 763 | pr->f_val = 1; |
komoritan | 0:735163979ecf | 764 | } else { |
komoritan | 0:735163979ecf | 765 | pr->f_val = 0; |
komoritan | 0:735163979ecf | 766 | } |
komoritan | 0:735163979ecf | 767 | } |
komoritan | 0:735163979ecf | 768 | } |
komoritan | 0:735163979ecf | 769 | snprintf( pr->name, sizeof(pr->name), "%s", it->first.c_str() ); |
komoritan | 0:735163979ecf | 770 | DXDBG("HANDLE_MESSAGE: prop name: %s", pr->name); |
komoritan | 0:735163979ecf | 771 | } |
komoritan | 0:735163979ecf | 772 | |
komoritan | 0:735163979ecf | 773 | if (m_func_get_request && !strcmp(root["type"].get<std::string>().c_str(), "device-get-request")) { |
komoritan | 0:735163979ecf | 774 | if((*m_func_get_request)( &ps )) { |
komoritan | 0:735163979ecf | 775 | dx_device_get_response(&ps, root["message-id"].get<std::string>().c_str()); |
komoritan | 0:735163979ecf | 776 | } else { |
komoritan | 0:735163979ecf | 777 | dx_error_response(root["message-id"].get<std::string>().c_str()); |
komoritan | 0:735163979ecf | 778 | } |
komoritan | 0:735163979ecf | 779 | } |
komoritan | 0:735163979ecf | 780 | if (m_func_set_request && !strcmp(root["type"].get<std::string>().c_str(), "device-set-request")) { |
komoritan | 0:735163979ecf | 781 | if((*m_func_set_request)( &ps )) { |
komoritan | 0:735163979ecf | 782 | dx_device_set_response(&ps, root["message-id"].get<std::string>().c_str()); |
komoritan | 0:735163979ecf | 783 | } else { |
komoritan | 0:735163979ecf | 784 | dx_error_response(root["message-id"].get<std::string>().c_str()); |
komoritan | 0:735163979ecf | 785 | } |
komoritan | 0:735163979ecf | 786 | } |
komoritan | 0:735163979ecf | 787 | free(ps.props); |
komoritan | 0:735163979ecf | 788 | continue; |
komoritan | 0:735163979ecf | 789 | } else { |
komoritan | 0:735163979ecf | 790 | DXDBG("HANDLE_MESSAGE: no props in request"); |
komoritan | 0:735163979ecf | 791 | } |
komoritan | 0:735163979ecf | 792 | // send error message |
komoritan | 0:735163979ecf | 793 | dx_error_response(root["message-id"].get<std::string>().c_str()); |
komoritan | 0:735163979ecf | 794 | } |
komoritan | 0:735163979ecf | 795 | else if (!strcmp(root["type"].get<std::string>().c_str(), "device-update-response")) { |
komoritan | 0:735163979ecf | 796 | DXDBG("HANDLE_MESSAGE: recv device-update-response"); |
komoritan | 0:735163979ecf | 797 | } |
komoritan | 0:735163979ecf | 798 | else if (!strcmp(root["type"].get<std::string>().c_str(), "keep-alive-response")) { |
komoritan | 0:735163979ecf | 799 | DXDBG("HANDLE_MESSAGE: recv keep-alive-response"); |
komoritan | 0:735163979ecf | 800 | |
komoritan | 0:735163979ecf | 801 | } |
komoritan | 0:735163979ecf | 802 | else { |
komoritan | 0:735163979ecf | 803 | DXDBG("HANDLE_MESSAGE: Unknown message type"); |
komoritan | 0:735163979ecf | 804 | } |
komoritan | 0:735163979ecf | 805 | } else { |
komoritan | 0:735163979ecf | 806 | DXDBG("HANDLE_MESSAGE: there is no type"); |
komoritan | 0:735163979ecf | 807 | } |
komoritan | 0:735163979ecf | 808 | } else { |
komoritan | 0:735163979ecf | 809 | DXDBG("HANDLE_MESSAGE: Parse Error"); |
komoritan | 0:735163979ecf | 810 | } |
komoritan | 0:735163979ecf | 811 | } else { |
komoritan | 0:735163979ecf | 812 | break; // no more message |
komoritan | 0:735163979ecf | 813 | } |
komoritan | 0:735163979ecf | 814 | } |
komoritan | 0:735163979ecf | 815 | |
komoritan | 0:735163979ecf | 816 | return res; |
komoritan | 0:735163979ecf | 817 | } |
komoritan | 0:735163979ecf | 818 | |
komoritan | 0:735163979ecf | 819 | |
komoritan | 0:735163979ecf | 820 | bool DxClient::dx_error_response( const char* mesid) |
komoritan | 0:735163979ecf | 821 | { |
komoritan | 0:735163979ecf | 822 | DXDBG("Call dx_error_response"); |
komoritan | 0:735163979ecf | 823 | |
komoritan | 0:735163979ecf | 824 | int len; |
komoritan | 0:735163979ecf | 825 | char message[MAX_MESSAGELEN]; |
komoritan | 0:735163979ecf | 826 | len = snprintf(message, sizeof(message), "{" \ |
komoritan | 0:735163979ecf | 827 | "\"device-id\":\"%s\"," \ |
komoritan | 0:735163979ecf | 828 | "\"message-id\":\"%s\"," \ |
komoritan | 0:735163979ecf | 829 | "\"status\":\"400 Bad Request\"" \ |
komoritan | 0:735163979ecf | 830 | "}", |
komoritan | 0:735163979ecf | 831 | m_deviceid, |
komoritan | 0:735163979ecf | 832 | mesid |
komoritan | 0:735163979ecf | 833 | ); |
komoritan | 0:735163979ecf | 834 | if (len >= sizeof(message)-1) { |
komoritan | 0:735163979ecf | 835 | DXDBG( "ERROR_RESPONSE: message is over MAX LEN"); |
komoritan | 0:735163979ecf | 836 | return false; |
komoritan | 0:735163979ecf | 837 | } |
komoritan | 0:735163979ecf | 838 | DXDBG("send: %s", message); |
komoritan | 0:735163979ecf | 839 | m_ws.send(message); |
komoritan | 0:735163979ecf | 840 | |
komoritan | 0:735163979ecf | 841 | return true; |
komoritan | 0:735163979ecf | 842 | } |
komoritan | 0:735163979ecf | 843 | |
komoritan | 0:735163979ecf | 844 | |
komoritan | 0:735163979ecf | 845 | void DxClient::set_get_requset_handler(REQUEST_HANDLER handler) |
komoritan | 0:735163979ecf | 846 | { |
komoritan | 0:735163979ecf | 847 | m_func_get_request = handler; |
komoritan | 0:735163979ecf | 848 | } |
komoritan | 0:735163979ecf | 849 | void DxClient::set_set_requset_handler(REQUEST_HANDLER handler) |
komoritan | 0:735163979ecf | 850 | { |
komoritan | 0:735163979ecf | 851 | m_func_set_request = handler; |
komoritan | 0:735163979ecf | 852 | } |
komoritan | 0:735163979ecf | 853 | |
komoritan | 0:735163979ecf | 854 | |
komoritan | 0:735163979ecf | 855 | // ============================================== |
komoritan | 0:735163979ecf | 856 | // |
komoritan | 0:735163979ecf | 857 | // ============================================== |
komoritan | 0:735163979ecf | 858 | bool DxClient::dx_device_get_response( dx_props *props, const char* mesid ) |
komoritan | 0:735163979ecf | 859 | { |
komoritan | 0:735163979ecf | 860 | DXDBG("Call dx_device_get_response"); |
komoritan | 0:735163979ecf | 861 | |
komoritan | 0:735163979ecf | 862 | int len; |
komoritan | 0:735163979ecf | 863 | char message[MAX_MESSAGELEN]; |
komoritan | 0:735163979ecf | 864 | |
komoritan | 0:735163979ecf | 865 | len = snprintf(message, sizeof(message), "{" \ |
komoritan | 0:735163979ecf | 866 | "\"type\":\"device-get-response\"," \ |
komoritan | 0:735163979ecf | 867 | "\"device-id\":\"%s\"," \ |
komoritan | 0:735163979ecf | 868 | "\"message-id\":\"%s\"," \ |
komoritan | 0:735163979ecf | 869 | "\"props\":{", |
komoritan | 0:735163979ecf | 870 | m_deviceid, |
komoritan | 0:735163979ecf | 871 | mesid |
komoritan | 0:735163979ecf | 872 | ); |
komoritan | 0:735163979ecf | 873 | if (len >= sizeof(message)-1) { |
komoritan | 0:735163979ecf | 874 | DXDBG( "DEVICE_GET: message is over MAX LEN"); |
komoritan | 0:735163979ecf | 875 | return false; |
komoritan | 0:735163979ecf | 876 | } |
komoritan | 0:735163979ecf | 877 | |
komoritan | 0:735163979ecf | 878 | int i; |
komoritan | 0:735163979ecf | 879 | for (i=0;i<props->numofprops;i++) { |
komoritan | 0:735163979ecf | 880 | char s_prop[MAX_PROPLEN]; |
komoritan | 0:735163979ecf | 881 | char s_val[MAX_PROPSVALLEN]; |
komoritan | 0:735163979ecf | 882 | switch( props->props[i].type ){ |
komoritan | 0:735163979ecf | 883 | case DX_STRING: |
komoritan | 0:735163979ecf | 884 | sprintf( s_val, "\"%s\"", props->props[i].s_val ); |
komoritan | 0:735163979ecf | 885 | break; |
komoritan | 0:735163979ecf | 886 | case DX_INTEGER: |
komoritan | 0:735163979ecf | 887 | sprintf( s_val, "%d", (int)(props->props[i].f_val) ); |
komoritan | 0:735163979ecf | 888 | break; |
komoritan | 0:735163979ecf | 889 | case DX_FLOAT: |
komoritan | 0:735163979ecf | 890 | sprintf( s_val, "%f", props->props[i].f_val ); |
komoritan | 0:735163979ecf | 891 | break; |
komoritan | 0:735163979ecf | 892 | case DX_BOOLEAN: |
komoritan | 0:735163979ecf | 893 | sprintf( s_val, "%s", props->props[i].b_val?"true":"false" ); |
komoritan | 0:735163979ecf | 894 | break; |
komoritan | 0:735163979ecf | 895 | } |
komoritan | 0:735163979ecf | 896 | |
komoritan | 0:735163979ecf | 897 | len = snprintf( s_prop, sizeof(s_prop), "\"%s\": {" \ |
komoritan | 0:735163979ecf | 898 | "\"value\":%s" \ |
komoritan | 0:735163979ecf | 899 | "}", |
komoritan | 0:735163979ecf | 900 | props->props[i].name, |
komoritan | 0:735163979ecf | 901 | s_val |
komoritan | 0:735163979ecf | 902 | ); |
komoritan | 0:735163979ecf | 903 | if (len >= sizeof(s_prop)-1) { |
komoritan | 0:735163979ecf | 904 | DXDBG( "DEVICE_GET: prop is over MAX LEN"); |
komoritan | 0:735163979ecf | 905 | return false; |
komoritan | 0:735163979ecf | 906 | } |
komoritan | 0:735163979ecf | 907 | if (len + strlen(message) >= sizeof(message)-1) { |
komoritan | 0:735163979ecf | 908 | DXDBG( "DEVICE_GET: message is over MAX LEN"); |
komoritan | 0:735163979ecf | 909 | return false; |
komoritan | 0:735163979ecf | 910 | } |
komoritan | 0:735163979ecf | 911 | strcat( message, s_prop ); |
komoritan | 0:735163979ecf | 912 | |
komoritan | 0:735163979ecf | 913 | if (i+1 < props->numofprops) { |
komoritan | 0:735163979ecf | 914 | if (1 + strlen(message) >= sizeof(message)-1) { |
komoritan | 0:735163979ecf | 915 | DXDBG( "DEVICE_GET: message is over MAX LEN"); |
komoritan | 0:735163979ecf | 916 | return false; |
komoritan | 0:735163979ecf | 917 | } |
komoritan | 0:735163979ecf | 918 | strncat( message, ",", sizeof(message) ); |
komoritan | 0:735163979ecf | 919 | } |
komoritan | 0:735163979ecf | 920 | } |
komoritan | 0:735163979ecf | 921 | |
komoritan | 0:735163979ecf | 922 | if (2 + strlen(message) >= sizeof(message)-1) { |
komoritan | 0:735163979ecf | 923 | DXDBG( "DEVICE_GET: message is over MAX LEN"); |
komoritan | 0:735163979ecf | 924 | return false; |
komoritan | 0:735163979ecf | 925 | } |
komoritan | 0:735163979ecf | 926 | strcat( message, "}}" ); |
komoritan | 0:735163979ecf | 927 | |
komoritan | 0:735163979ecf | 928 | DXDBG("send: %s", message); |
komoritan | 0:735163979ecf | 929 | m_ws.send(message); |
komoritan | 0:735163979ecf | 930 | |
komoritan | 0:735163979ecf | 931 | return true; |
komoritan | 0:735163979ecf | 932 | } |
komoritan | 0:735163979ecf | 933 | |
komoritan | 0:735163979ecf | 934 | bool DxClient::dx_device_set_response( dx_props *props, const char* mesid ) |
komoritan | 0:735163979ecf | 935 | { |
komoritan | 0:735163979ecf | 936 | DXDBG("Call dx_device_set_response"); |
komoritan | 0:735163979ecf | 937 | |
komoritan | 0:735163979ecf | 938 | int len; |
komoritan | 0:735163979ecf | 939 | char message[MAX_MESSAGELEN]; |
komoritan | 0:735163979ecf | 940 | len = snprintf(message, sizeof(message), "{" \ |
komoritan | 0:735163979ecf | 941 | "\"type\":\"device-set-response\"," \ |
komoritan | 0:735163979ecf | 942 | "\"device-id\":\"%s\"," \ |
komoritan | 0:735163979ecf | 943 | "\"message-id\":\"%s\"," \ |
komoritan | 0:735163979ecf | 944 | "\"status\":\"200 OK\"" \ |
komoritan | 0:735163979ecf | 945 | "}", |
komoritan | 0:735163979ecf | 946 | m_deviceid, |
komoritan | 0:735163979ecf | 947 | mesid |
komoritan | 0:735163979ecf | 948 | ); |
komoritan | 0:735163979ecf | 949 | if (len >= sizeof(message)-1) { |
komoritan | 0:735163979ecf | 950 | DXDBG( "ERROR_RESPONSE: message is over MAX LEN"); |
komoritan | 0:735163979ecf | 951 | return false; |
komoritan | 0:735163979ecf | 952 | } |
komoritan | 0:735163979ecf | 953 | DXDBG("send: %s", message); |
komoritan | 0:735163979ecf | 954 | m_ws.send(message); |
komoritan | 0:735163979ecf | 955 | |
komoritan | 0:735163979ecf | 956 | return true; |
komoritan | 0:735163979ecf | 957 | } |