[457] | 1 | #include <stdio.h>
|
---|
| 2 | #include <stdlib.h>
|
---|
| 3 | #include <stdbool.h>
|
---|
| 4 | #include <string.h>
|
---|
| 5 | #include "azure_c_shared_utility/shared_util_options.h"
|
---|
| 6 | #include "pinkit.h"
|
---|
| 7 |
|
---|
| 8 | extern char* connectionString;
|
---|
| 9 |
|
---|
| 10 | extern bool g_use_proxy;
|
---|
| 11 | extern HTTP_PROXY_OPTIONS g_proxy_options;
|
---|
| 12 |
|
---|
| 13 | enum proxy_parse_state_t {
|
---|
| 14 | proxy_parse_state_schema,
|
---|
| 15 | proxy_parse_state_colonslash,
|
---|
| 16 | proxy_parse_state_colonslashslash,
|
---|
| 17 | proxy_parse_state_username,
|
---|
| 18 | proxy_parse_state_password,
|
---|
| 19 | proxy_parse_state_address,
|
---|
| 20 | proxy_parse_state_port,
|
---|
| 21 | proxy_parse_state_error,
|
---|
| 22 | };
|
---|
| 23 |
|
---|
| 24 | int set_proxy(const char *proxy, bool silent)
|
---|
| 25 | {
|
---|
| 26 | const char *schema = NULL, *address = NULL, *port = NULL, *username = NULL, *password = NULL;
|
---|
| 27 | int schema_len = 0, address_len = 0, port_len = 0, username_len = 0, password_len = 0;
|
---|
| 28 |
|
---|
| 29 | enum proxy_parse_state_t state = proxy_parse_state_schema;
|
---|
| 30 |
|
---|
| 31 | const char *pos = proxy;
|
---|
| 32 | schema = pos;
|
---|
| 33 | for (char c = *pos; c != '\0'; pos++, c = *pos) {
|
---|
| 34 | switch (state) {
|
---|
| 35 | case proxy_parse_state_schema:
|
---|
| 36 | if (c == ':') {
|
---|
| 37 | schema_len = (int)pos - (int)schema;
|
---|
| 38 | username = pos + 3;
|
---|
| 39 | state = proxy_parse_state_colonslash;
|
---|
| 40 | }
|
---|
| 41 | break;
|
---|
| 42 | case proxy_parse_state_colonslash:
|
---|
| 43 | if (c == '/') {
|
---|
| 44 | state = proxy_parse_state_colonslashslash;
|
---|
| 45 | }
|
---|
| 46 | else {
|
---|
| 47 | username = schema;
|
---|
| 48 | username_len = schema_len;
|
---|
| 49 | schema = NULL;
|
---|
| 50 | schema_len = 0;
|
---|
| 51 | password = pos;
|
---|
| 52 | state = proxy_parse_state_password;
|
---|
| 53 | goto case_proxy_parse_state_password;
|
---|
| 54 | }
|
---|
| 55 | break;
|
---|
| 56 | case proxy_parse_state_colonslashslash:
|
---|
| 57 | if (c == '/') {
|
---|
| 58 | state = proxy_parse_state_username;
|
---|
| 59 | }
|
---|
| 60 | else {
|
---|
| 61 | state = proxy_parse_state_error;
|
---|
| 62 | }
|
---|
| 63 | break;
|
---|
| 64 | case proxy_parse_state_username:
|
---|
| 65 | if (c == ':') {
|
---|
| 66 | username_len = (int)pos - (int)username;
|
---|
| 67 | password = pos + 1;
|
---|
| 68 | state = proxy_parse_state_password;
|
---|
| 69 | }
|
---|
| 70 | else if (c == '@') {
|
---|
| 71 | username_len = (int)pos - (int)username;
|
---|
| 72 | address = pos + 1;
|
---|
| 73 | state = proxy_parse_state_address;
|
---|
| 74 | }
|
---|
| 75 | break;
|
---|
| 76 | case proxy_parse_state_password:
|
---|
| 77 | case_proxy_parse_state_password:
|
---|
| 78 | if (c == '@') {
|
---|
| 79 | password_len = (int)pos - (int)password;
|
---|
| 80 | address = pos + 1;
|
---|
| 81 | state = proxy_parse_state_address;
|
---|
| 82 | }
|
---|
| 83 | break;
|
---|
| 84 | case proxy_parse_state_address:
|
---|
| 85 | if (c == ':') {
|
---|
| 86 | address_len = (int)pos - (int)address;
|
---|
| 87 | port = pos + 1;
|
---|
| 88 | state = proxy_parse_state_port;
|
---|
| 89 | }
|
---|
| 90 | break;
|
---|
| 91 | case proxy_parse_state_port:
|
---|
| 92 | if ((c < '0') && (c > '9')) {
|
---|
| 93 | state = proxy_parse_state_error;
|
---|
| 94 | }
|
---|
| 95 | break;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | if (state == proxy_parse_state_error)
|
---|
| 99 | break;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | switch (state)
|
---|
| 103 | {
|
---|
| 104 | case proxy_parse_state_schema:
|
---|
| 105 | case proxy_parse_state_colonslash:
|
---|
| 106 | case proxy_parse_state_colonslashslash:
|
---|
| 107 | goto error;
|
---|
| 108 | case proxy_parse_state_username:
|
---|
| 109 | username_len = (int)pos - (int)username;
|
---|
| 110 | address = username;
|
---|
| 111 | address_len = username_len;
|
---|
| 112 | username = NULL;
|
---|
| 113 | username_len = 0;
|
---|
| 114 | break;
|
---|
| 115 | case proxy_parse_state_password:
|
---|
| 116 | password_len = (int)pos - (int)password;
|
---|
| 117 | address = username;
|
---|
| 118 | address_len = username_len;
|
---|
| 119 | username = NULL;
|
---|
| 120 | username_len = 0;
|
---|
| 121 | port = password;
|
---|
| 122 | port_len = password_len;
|
---|
| 123 | password = NULL;
|
---|
| 124 | password_len = 0;
|
---|
| 125 | break;
|
---|
| 126 | case proxy_parse_state_address:
|
---|
| 127 | address_len = (int)pos - (int)address;
|
---|
| 128 | break;
|
---|
| 129 | case proxy_parse_state_port:
|
---|
| 130 | port_len = (int)pos - (int)port;
|
---|
| 131 | break;
|
---|
| 132 | case proxy_parse_state_error:
|
---|
| 133 | default:
|
---|
| 134 | goto error;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | if (schema_len > 0) {
|
---|
| 138 | if (strncmp(schema, "http", schema_len) != 0)
|
---|
| 139 | goto error;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | if (address_len == 0)
|
---|
| 143 | goto error;
|
---|
| 144 |
|
---|
| 145 | char *buf = malloc(address_len + port_len + username_len + password_len + 4);
|
---|
| 146 | if (buf == NULL) {
|
---|
| 147 | if (!silent)
|
---|
| 148 | printf("Memory error.\n");
|
---|
| 149 | return -1;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | free((char *)g_proxy_options.host_address);
|
---|
| 153 |
|
---|
| 154 | memcpy(buf, address, address_len);
|
---|
| 155 | buf[address_len] = '\0';
|
---|
| 156 | g_proxy_options.host_address = buf;
|
---|
| 157 |
|
---|
| 158 | buf = buf + address_len + 1;
|
---|
| 159 | memcpy(buf, port, port_len);
|
---|
| 160 | buf[port_len] = '\0';
|
---|
| 161 | g_proxy_options.port = port_len == 0 ? 8080 : atoi(buf);
|
---|
| 162 |
|
---|
| 163 | buf = buf + port_len + 1;
|
---|
| 164 | memcpy(buf, username, username_len);
|
---|
| 165 | buf[username_len] = '\0';
|
---|
| 166 | g_proxy_options.username = (username_len == 0) && (password_len == 0) ? NULL : buf;
|
---|
| 167 |
|
---|
| 168 | buf = buf + username_len + 1;
|
---|
| 169 | memcpy(buf, password, password_len);
|
---|
| 170 | buf[password_len] = '\0';
|
---|
| 171 | g_proxy_options.password = (username_len == 0) && (password_len == 0) ? NULL : buf;
|
---|
| 172 |
|
---|
| 173 | return 0;
|
---|
| 174 | error:
|
---|
| 175 | if (!silent)
|
---|
| 176 | printf("proxy string parse error. 'http://[<user>:<password>@]<address>:<port>'\n");
|
---|
| 177 | return -1;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 |
|
---|
| 181 | int set_cs_main(int argc, char **argv)
|
---|
| 182 | {
|
---|
| 183 | if (argc < 2) {
|
---|
| 184 | printf("usage:\n%s <connection string>\n", argv[0]);
|
---|
| 185 | return 0;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | int len = strlen(argv[1]);
|
---|
| 189 | if (len < 70) {
|
---|
| 190 | printf("String containing Hostname, Device Id & Device Key in the format:\n");
|
---|
| 191 | printf(" HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>\n");
|
---|
| 192 | printf(" HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>\n");
|
---|
| 193 | return 0;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | char *conn_str = (char *)malloc(len + 1);
|
---|
| 197 | if (conn_str == NULL) {
|
---|
| 198 | printf("OOM while creating connection string\n");
|
---|
| 199 | return 1;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | memcpy(conn_str, argv[1], len);
|
---|
| 203 | conn_str[len] = 0;
|
---|
| 204 | free(connectionString);
|
---|
| 205 | connectionString = conn_str;
|
---|
| 206 | printf("Connection String:\n%s\n", connectionString);
|
---|
| 207 |
|
---|
| 208 | return 0;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | int set_proxy_main(int argc, char **argv)
|
---|
| 212 | {
|
---|
| 213 | int result;
|
---|
| 214 |
|
---|
| 215 | if (argc < 2) {
|
---|
| 216 | if (g_proxy_options.username != NULL) {
|
---|
| 217 | printf("HTTP_PROXY=http://%s:%s@%s:%d\n",
|
---|
| 218 | g_proxy_options.username,
|
---|
| 219 | g_proxy_options.password,
|
---|
| 220 | g_proxy_options.host_address,
|
---|
| 221 | g_proxy_options.port);
|
---|
| 222 | }
|
---|
| 223 | else if (g_proxy_options.host_address != NULL) {
|
---|
| 224 | printf("HTTP_PROXY=http://%s:%d\n",
|
---|
| 225 | g_proxy_options.host_address,
|
---|
| 226 | g_proxy_options.port);
|
---|
| 227 | }
|
---|
| 228 | else {
|
---|
| 229 | printf("HTTP_PROXY=\n");
|
---|
| 230 | }
|
---|
| 231 | return 0;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | if ((result = set_proxy(argv[1], false)) == 0) {
|
---|
| 235 | g_use_proxy = true;
|
---|
| 236 | }
|
---|
| 237 | else {
|
---|
| 238 | g_use_proxy = false;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | return 0;
|
---|
| 242 | }
|
---|
| 243 | int clear_proxy_main(int argc, char **argv)
|
---|
| 244 | {
|
---|
| 245 | free((char *)g_proxy_options.host_address);
|
---|
| 246 | g_proxy_options.host_address = NULL;
|
---|
| 247 | g_proxy_options.port = 0;
|
---|
| 248 | g_proxy_options.username = NULL;
|
---|
| 249 | g_proxy_options.password = NULL;
|
---|
| 250 |
|
---|
| 251 | g_use_proxy = false;
|
---|
| 252 | return 0;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | int set_wifi_main(int argc, char **argv)
|
---|
| 256 | {
|
---|
| 257 | if (argc < 3) {
|
---|
| 258 | return 0;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | set_ssid_pwd(argv[1], argv[2]);
|
---|
| 262 |
|
---|
| 263 | prepare_esp_at();
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | int pinkit_main(int argc, char **argv)
|
---|
| 267 | {
|
---|
| 268 | for (;;) {
|
---|
| 269 | int x = (int)(pinkit.accel.X * 1000);
|
---|
| 270 | int y = (int)(pinkit.accel.Y * 1000);
|
---|
| 271 | int z = (int)(pinkit.accel.Z * 1000);
|
---|
| 272 | int temp = (int)(pinkit.temperature * 1000);
|
---|
| 273 | int humi = (int)(pinkit.humidity * 1000);
|
---|
| 274 | printf("x:%d.%03d, y:%d.%03d, z:%d.%03d, temperature:%d.%03d, humidity %d.%03d\n",
|
---|
| 275 | x / 1000, x % 1000, y / 1000, y % 1000, z / 1000, z % 1000,
|
---|
| 276 | temp / 1000, temp % 1000, humi / 1000, humi % 1000);
|
---|
| 277 |
|
---|
| 278 | ThreadAPI_Sleep(1000);
|
---|
| 279 | }
|
---|
| 280 | return 0;
|
---|
| 281 | }
|
---|