mbedからGluinサーバへ接続するライブラリです
DxClient.h@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 | /** |
komoritan | 0:735163979ecf | 2 | * @author Satoshi Komorita |
komoritan | 0:735163979ecf | 3 | * |
komoritan | 0:735163979ecf | 4 | * @section LICENSE |
komoritan | 0:735163979ecf | 5 | * |
komoritan | 0:735163979ecf | 6 | * Copyright (c) 2014 Satoshi Komorita |
komoritan | 0:735163979ecf | 7 | * |
komoritan | 0:735163979ecf | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
komoritan | 0:735163979ecf | 9 | * of this software and associated documentation files (the "Software"), to deal |
komoritan | 0:735163979ecf | 10 | * in the Software without restriction, including without limitation the rights |
komoritan | 0:735163979ecf | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
komoritan | 0:735163979ecf | 12 | * copies of the Software, and to permit persons to whom the Software is |
komoritan | 0:735163979ecf | 13 | * furnished to do so, subject to the following conditions: |
komoritan | 0:735163979ecf | 14 | * |
komoritan | 0:735163979ecf | 15 | * The above copyright notice and this permission notice shall be included in |
komoritan | 0:735163979ecf | 16 | * all copies or substantial portions of the Software. |
komoritan | 0:735163979ecf | 17 | * |
komoritan | 0:735163979ecf | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
komoritan | 0:735163979ecf | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
komoritan | 0:735163979ecf | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
komoritan | 0:735163979ecf | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
komoritan | 0:735163979ecf | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
komoritan | 0:735163979ecf | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
komoritan | 0:735163979ecf | 24 | * THE SOFTWARE. |
komoritan | 0:735163979ecf | 25 | * |
komoritan | 0:735163979ecf | 26 | * @section DESCRIPTION |
komoritan | 0:735163979ecf | 27 | * Simple websocket client |
komoritan | 0:735163979ecf | 28 | * |
komoritan | 0:735163979ecf | 29 | */ |
komoritan | 0:735163979ecf | 30 | |
komoritan | 0:735163979ecf | 31 | #ifndef DXCLIENT_H |
komoritan | 0:735163979ecf | 32 | #define DXCLIENT_H |
komoritan | 0:735163979ecf | 33 | |
komoritan | 0:735163979ecf | 34 | #include "mbed.h" |
komoritan | 0:735163979ecf | 35 | #include "EthernetInterface.h" |
komoritan | 0:735163979ecf | 36 | #include "Websocket.h" |
komoritan | 0:735163979ecf | 37 | |
komoritan | 0:735163979ecf | 38 | #define MAX_USERNAME 32 |
komoritan | 0:735163979ecf | 39 | #define MAX_PASSWORD 32 |
komoritan | 0:735163979ecf | 40 | #define MAX_DEVICEID 64 |
komoritan | 0:735163979ecf | 41 | |
komoritan | 0:735163979ecf | 42 | #define MAX_PROPLEN 128 |
komoritan | 0:735163979ecf | 43 | #define MAX_PROPNAMELEN 32 |
komoritan | 0:735163979ecf | 44 | #define MAX_PROPSVALLEN 64 |
komoritan | 0:735163979ecf | 45 | |
komoritan | 0:735163979ecf | 46 | #define MAX_MESSAGELEN 512 |
komoritan | 0:735163979ecf | 47 | #define MAX_MESSAGEIDLEN 32 |
komoritan | 0:735163979ecf | 48 | |
komoritan | 0:735163979ecf | 49 | #define MAX_DEV_DESCRIPTION 64 |
komoritan | 0:735163979ecf | 50 | #define MAX_DEV_NAME 32 |
komoritan | 0:735163979ecf | 51 | |
komoritan | 0:735163979ecf | 52 | typedef enum { |
komoritan | 0:735163979ecf | 53 | DX_STRING, |
komoritan | 0:735163979ecf | 54 | DX_INTEGER, |
komoritan | 0:735163979ecf | 55 | DX_FLOAT, |
komoritan | 0:735163979ecf | 56 | DX_BOOLEAN |
komoritan | 0:735163979ecf | 57 | } dx_prop_type; |
komoritan | 0:735163979ecf | 58 | typedef enum { |
komoritan | 0:735163979ecf | 59 | DX_UPONLY, |
komoritan | 0:735163979ecf | 60 | DX_DOWNONLY, |
komoritan | 0:735163979ecf | 61 | DX_UPDOWN |
komoritan | 0:735163979ecf | 62 | } dx_prop_direction; |
komoritan | 0:735163979ecf | 63 | typedef enum { |
komoritan | 0:735163979ecf | 64 | DX_WRITEONLY, |
komoritan | 0:735163979ecf | 65 | DX_READONLY, |
komoritan | 0:735163979ecf | 66 | DX_READWRITE |
komoritan | 0:735163979ecf | 67 | } dx_prop_mode; |
komoritan | 0:735163979ecf | 68 | typedef struct { |
komoritan | 0:735163979ecf | 69 | char name[MAX_PROPNAMELEN]; |
komoritan | 0:735163979ecf | 70 | dx_prop_type type; |
komoritan | 0:735163979ecf | 71 | dx_prop_mode mode; |
komoritan | 0:735163979ecf | 72 | dx_prop_direction direction; |
komoritan | 0:735163979ecf | 73 | char s_val[MAX_PROPSVALLEN]; |
komoritan | 0:735163979ecf | 74 | float f_val; |
komoritan | 0:735163979ecf | 75 | bool b_val; |
komoritan | 0:735163979ecf | 76 | } dx_prop; |
komoritan | 0:735163979ecf | 77 | |
komoritan | 0:735163979ecf | 78 | typedef struct { |
komoritan | 0:735163979ecf | 79 | int numofprops; |
komoritan | 0:735163979ecf | 80 | dx_prop *props; |
komoritan | 0:735163979ecf | 81 | } dx_props; |
komoritan | 0:735163979ecf | 82 | |
komoritan | 0:735163979ecf | 83 | typedef bool ( *REQUEST_HANDLER )( dx_props *props); |
komoritan | 0:735163979ecf | 84 | |
komoritan | 0:735163979ecf | 85 | class DxClient |
komoritan | 0:735163979ecf | 86 | { |
komoritan | 0:735163979ecf | 87 | public: |
komoritan | 0:735163979ecf | 88 | |
komoritan | 0:735163979ecf | 89 | /** |
komoritan | 0:735163979ecf | 90 | * Constructor |
komoritan | 0:735163979ecf | 91 | * @param url The DxClient url in the form "ws://ip_domain[:port]/path" (by default: port = 80) |
komoritan | 0:735163979ecf | 92 | * @param deviceid is uniqued ID identifing this device. |
komoritan | 0:735163979ecf | 93 | */ |
komoritan | 0:735163979ecf | 94 | DxClient(char * url, char* deviceid, float seed); |
komoritan | 0:735163979ecf | 95 | |
komoritan | 0:735163979ecf | 96 | /* |
komoritan | 0:735163979ecf | 97 | * Set Auth User and Password |
komoritan | 0:735163979ecf | 98 | * this method should be called before connect(); |
komoritan | 0:735163979ecf | 99 | */ |
komoritan | 0:735163979ecf | 100 | void set_user(char* user, char *pass); |
komoritan | 0:735163979ecf | 101 | |
komoritan | 0:735163979ecf | 102 | void set_device_description(char* desc); |
komoritan | 0:735163979ecf | 103 | void set_device_name(char* name); |
komoritan | 0:735163979ecf | 104 | |
komoritan | 0:735163979ecf | 105 | /** |
komoritan | 0:735163979ecf | 106 | * Connect to the websocket url |
komoritan | 0:735163979ecf | 107 | *@return true if the connection is established, false otherwise |
komoritan | 0:735163979ecf | 108 | */ |
komoritan | 0:735163979ecf | 109 | bool connect(); |
komoritan | 0:735163979ecf | 110 | |
komoritan | 0:735163979ecf | 111 | /** |
komoritan | 0:735163979ecf | 112 | * close websocket connection |
komoritan | 0:735163979ecf | 113 | *@return true if the connection is established, false otherwise |
komoritan | 0:735163979ecf | 114 | */ |
komoritan | 0:735163979ecf | 115 | bool close(); |
komoritan | 0:735163979ecf | 116 | |
komoritan | 0:735163979ecf | 117 | /* |
komoritan | 0:735163979ecf | 118 | * send device_register_requsest message to server |
komoritan | 0:735163979ecf | 119 | */ |
komoritan | 0:735163979ecf | 120 | bool register_device(dx_props* props); |
komoritan | 0:735163979ecf | 121 | |
komoritan | 0:735163979ecf | 122 | /* |
komoritan | 0:735163979ecf | 123 | * send device_unregister_requsest message to server |
komoritan | 0:735163979ecf | 124 | */ |
komoritan | 0:735163979ecf | 125 | bool deregister_device(); |
komoritan | 0:735163979ecf | 126 | |
komoritan | 0:735163979ecf | 127 | /* |
komoritan | 0:735163979ecf | 128 | * send device_update_request to server |
komoritan | 0:735163979ecf | 129 | */ |
komoritan | 0:735163979ecf | 130 | bool update_device(dx_props *props); |
komoritan | 0:735163979ecf | 131 | |
komoritan | 0:735163979ecf | 132 | /* |
komoritan | 0:735163979ecf | 133 | * send device_keepalive_request to server |
komoritan | 0:735163979ecf | 134 | */ |
komoritan | 0:735163979ecf | 135 | bool keepalive_device(void); |
komoritan | 0:735163979ecf | 136 | |
komoritan | 0:735163979ecf | 137 | /* |
komoritan | 0:735163979ecf | 138 | * handle device_get_request from server |
komoritan | 0:735163979ecf | 139 | */ |
komoritan | 0:735163979ecf | 140 | void set_get_requset_handler(REQUEST_HANDLER handler); |
komoritan | 0:735163979ecf | 141 | |
komoritan | 0:735163979ecf | 142 | /* |
komoritan | 0:735163979ecf | 143 | * handle device_get_request from server |
komoritan | 0:735163979ecf | 144 | */ |
komoritan | 0:735163979ecf | 145 | void set_set_requset_handler(REQUEST_HANDLER handler); |
komoritan | 0:735163979ecf | 146 | |
komoritan | 0:735163979ecf | 147 | |
komoritan | 0:735163979ecf | 148 | bool handle_messages(void); |
komoritan | 0:735163979ecf | 149 | |
komoritan | 0:735163979ecf | 150 | private: |
komoritan | 0:735163979ecf | 151 | |
komoritan | 0:735163979ecf | 152 | Websocket m_ws; |
komoritan | 0:735163979ecf | 153 | |
komoritan | 0:735163979ecf | 154 | char m_user[MAX_USERNAME]; |
komoritan | 0:735163979ecf | 155 | char m_pass[MAX_PASSWORD]; |
komoritan | 0:735163979ecf | 156 | char m_deviceid[MAX_DEVICEID]; |
komoritan | 0:735163979ecf | 157 | char m_dev_description[MAX_DEV_DESCRIPTION]; |
komoritan | 0:735163979ecf | 158 | char m_dev_name[MAX_DEV_NAME]; |
komoritan | 0:735163979ecf | 159 | |
komoritan | 0:735163979ecf | 160 | REQUEST_HANDLER m_func_get_request; |
komoritan | 0:735163979ecf | 161 | REQUEST_HANDLER m_func_set_request; |
komoritan | 0:735163979ecf | 162 | // bool (*m_func_set_handler)(dx_props *props); |
komoritan | 0:735163979ecf | 163 | |
komoritan | 0:735163979ecf | 164 | bool dx_user_auth_request(); |
komoritan | 0:735163979ecf | 165 | bool dx_device_set_response( dx_props *props, const char* mesid); |
komoritan | 0:735163979ecf | 166 | bool dx_device_get_response( dx_props *props, const char* mesid); |
komoritan | 0:735163979ecf | 167 | bool dx_error_response( const char* mesid); |
komoritan | 0:735163979ecf | 168 | |
komoritan | 0:735163979ecf | 169 | |
komoritan | 0:735163979ecf | 170 | }; |
komoritan | 0:735163979ecf | 171 | |
komoritan | 0:735163979ecf | 172 | |
komoritan | 0:735163979ecf | 173 | #endif |