[453] | 1 | /*
|
---|
| 2 | * TOPPERS/ASP Kernel
|
---|
| 3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
| 4 | * Advanced Standard Profile Kernel
|
---|
| 5 | *
|
---|
| 6 | * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
|
---|
| 7 | * Toyohashi Univ. of Technology, JAPAN
|
---|
| 8 | * Copyright (C) 2004-2012 by Embedded and Real-Time Systems Laboratory
|
---|
| 9 | * Graduate School of Information Science, Nagoya Univ., JAPAN
|
---|
| 10 | *
|
---|
| 11 | * ãLì ÒÍCȺÌ(1)`(4)Ìðð½·êÉÀèC{\tgEF
|
---|
| 12 | * Ai{\tgEFAðüϵ½àÌðÜÞDȺ¯¶jðgpE¡»Eü
|
---|
| 13 | * ÏEÄzziȺCpÆÄÔj·é±Æð³Åø·éD
|
---|
| 14 | * (1) {\tgEFAð\[XR[hÌ`Åp·éêÉÍCãLÌì
|
---|
| 15 | * \¦C±Ìpð¨æѺL̳ÛØKèªC»ÌÜÜÌ`Å\[
|
---|
| 16 | * XR[hÉÜÜêÄ¢é±ÆD
|
---|
| 17 | * (2) {\tgEFAðCCu`®ÈÇC¼Ì\tgEFAJÉg
|
---|
| 18 | * pÅ«é`ÅÄzz·éêÉÍCÄzzɺ¤hL
|
---|
| 19 | gip
|
---|
| 20 | * Ò}j
|
---|
| 21 | AÈÇjÉCãLÌì \¦C±Ìpð¨æѺL
|
---|
| 22 | * ̳ÛØKèðfÚ·é±ÆD
|
---|
| 23 | * (3) {\tgEFAðC@íÉgÝÞÈÇC¼Ì\tgEFAJÉg
|
---|
| 24 | * pÅ«È¢`ÅÄzz·éêÉÍCÌ¢¸ê©Ìðð½·±
|
---|
| 25 | * ÆD
|
---|
| 26 | * (a) Äzzɺ¤hL
|
---|
| 27 | gipÒ}j
|
---|
| 28 | AÈÇjÉCãLÌ
|
---|
| 29 | * ì \¦C±Ìpð¨æѺL̳ÛØKèðfÚ·é±ÆD
|
---|
| 30 | * (b) ÄzzÌ`ÔðCÊÉèßéû@ÉæÁÄCTOPPERSvWFNgÉ
|
---|
| 31 | * ñ·é±ÆD
|
---|
| 32 | * (4) {\tgEFAÌpÉæè¼ÚIܽÍÔÚIɶ¶é¢©Èé¹
|
---|
| 33 | * Q©çàCãLì Ò¨æÑTOPPERSvWFNgðÆÓ·é±ÆD
|
---|
| 34 | * ܽC{\tgEFAÌ[UܽÍGh[U©çÌ¢©Èé
|
---|
| 35 | * RÉîÿ©çàCãLì Ò¨æÑTOPPERSvWFNgð
|
---|
| 36 | * ÆÓ·é±ÆD
|
---|
| 37 | *
|
---|
| 38 | * {\tgEFAÍC³ÛØÅñ³êÄ¢éàÌÅ éDãLì Ò¨
|
---|
| 39 | * æÑTOPPERSvWFNgÍC{\tgEFAÉÖµÄCÁèÌgpÚI
|
---|
| 40 | * ÉηéK«àÜßÄC¢©ÈéÛØàsíÈ¢DܽC{\tgEF
|
---|
| 41 | * AÌpÉæè¼ÚIܽÍÔÚIɶ¶½¢©Èé¹QÉÖµÄàC»
|
---|
| 42 | * ÌÓCðíÈ¢D
|
---|
| 43 | *
|
---|
| 44 | * $Id$
|
---|
| 45 | */
|
---|
| 46 | #include <stdio.h>
|
---|
| 47 | #include <stdlib.h>
|
---|
| 48 | #include <stdbool.h>
|
---|
| 49 | #include <string.h>
|
---|
| 50 | #include "azure_c_shared_utility/shared_util_options.h"
|
---|
| 51 |
|
---|
| 52 | extern char* connectionString;
|
---|
| 53 |
|
---|
| 54 | extern bool g_use_proxy;
|
---|
| 55 | extern HTTP_PROXY_OPTIONS g_proxy_options;
|
---|
| 56 |
|
---|
| 57 | enum proxy_parse_state_t {
|
---|
| 58 | proxy_parse_state_schema,
|
---|
| 59 | proxy_parse_state_colonslash,
|
---|
| 60 | proxy_parse_state_colonslashslash,
|
---|
| 61 | proxy_parse_state_username,
|
---|
| 62 | proxy_parse_state_password,
|
---|
| 63 | proxy_parse_state_address,
|
---|
| 64 | proxy_parse_state_port,
|
---|
| 65 | proxy_parse_state_error,
|
---|
| 66 | };
|
---|
| 67 |
|
---|
| 68 | int set_proxy(const char *proxy, bool silent)
|
---|
| 69 | {
|
---|
| 70 | const char *schema = NULL, *address = NULL, *port = NULL, *username = NULL, *password = NULL;
|
---|
| 71 | int schema_len = 0, address_len = 0, port_len = 0, username_len = 0, password_len = 0;
|
---|
| 72 |
|
---|
| 73 | enum proxy_parse_state_t state = proxy_parse_state_schema;
|
---|
| 74 |
|
---|
| 75 | const char *pos = proxy;
|
---|
| 76 | schema = pos;
|
---|
| 77 | for (char c = *pos; c != '\0'; pos++, c = *pos) {
|
---|
| 78 | switch (state) {
|
---|
| 79 | case proxy_parse_state_schema:
|
---|
| 80 | if (c == ':') {
|
---|
| 81 | schema_len = (int)pos - (int)schema;
|
---|
| 82 | username = pos + 3;
|
---|
| 83 | state = proxy_parse_state_colonslash;
|
---|
| 84 | }
|
---|
| 85 | break;
|
---|
| 86 | case proxy_parse_state_colonslash:
|
---|
| 87 | if (c == '/') {
|
---|
| 88 | state = proxy_parse_state_colonslashslash;
|
---|
| 89 | }
|
---|
| 90 | else {
|
---|
| 91 | username = schema;
|
---|
| 92 | username_len = schema_len;
|
---|
| 93 | schema = NULL;
|
---|
| 94 | schema_len = 0;
|
---|
| 95 | password = pos;
|
---|
| 96 | state = proxy_parse_state_password;
|
---|
| 97 | goto case_proxy_parse_state_password;
|
---|
| 98 | }
|
---|
| 99 | break;
|
---|
| 100 | case proxy_parse_state_colonslashslash:
|
---|
| 101 | if (c == '/') {
|
---|
| 102 | state = proxy_parse_state_username;
|
---|
| 103 | }
|
---|
| 104 | else {
|
---|
| 105 | state = proxy_parse_state_error;
|
---|
| 106 | }
|
---|
| 107 | break;
|
---|
| 108 | case proxy_parse_state_username:
|
---|
| 109 | if (c == ':') {
|
---|
| 110 | username_len = (int)pos - (int)username;
|
---|
| 111 | password = pos + 1;
|
---|
| 112 | state = proxy_parse_state_password;
|
---|
| 113 | }
|
---|
| 114 | else if (c == '@') {
|
---|
| 115 | username_len = (int)pos - (int)username;
|
---|
| 116 | address = pos + 1;
|
---|
| 117 | state = proxy_parse_state_address;
|
---|
| 118 | }
|
---|
| 119 | break;
|
---|
| 120 | case proxy_parse_state_password:
|
---|
| 121 | case_proxy_parse_state_password:
|
---|
| 122 | if (c == '@') {
|
---|
| 123 | password_len = (int)pos - (int)password;
|
---|
| 124 | address = pos + 1;
|
---|
| 125 | state = proxy_parse_state_address;
|
---|
| 126 | }
|
---|
| 127 | break;
|
---|
| 128 | case proxy_parse_state_address:
|
---|
| 129 | if (c == ':') {
|
---|
| 130 | address_len = (int)pos - (int)address;
|
---|
| 131 | port = pos + 1;
|
---|
| 132 | state = proxy_parse_state_port;
|
---|
| 133 | }
|
---|
| 134 | break;
|
---|
| 135 | case proxy_parse_state_port:
|
---|
| 136 | if ((c < '0') && (c > '9')) {
|
---|
| 137 | state = proxy_parse_state_error;
|
---|
| 138 | }
|
---|
| 139 | break;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | if (state == proxy_parse_state_error)
|
---|
| 143 | break;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | switch (state)
|
---|
| 147 | {
|
---|
| 148 | case proxy_parse_state_schema:
|
---|
| 149 | case proxy_parse_state_colonslash:
|
---|
| 150 | case proxy_parse_state_colonslashslash:
|
---|
| 151 | goto error;
|
---|
| 152 | case proxy_parse_state_username:
|
---|
| 153 | username_len = (int)pos - (int)username;
|
---|
| 154 | address = username;
|
---|
| 155 | address_len = username_len;
|
---|
| 156 | username = NULL;
|
---|
| 157 | username_len = 0;
|
---|
| 158 | break;
|
---|
| 159 | case proxy_parse_state_password:
|
---|
| 160 | password_len = (int)pos - (int)password;
|
---|
| 161 | address = username;
|
---|
| 162 | address_len = username_len;
|
---|
| 163 | username = NULL;
|
---|
| 164 | username_len = 0;
|
---|
| 165 | port = password;
|
---|
| 166 | port_len = password_len;
|
---|
| 167 | password = NULL;
|
---|
| 168 | password_len = 0;
|
---|
| 169 | break;
|
---|
| 170 | case proxy_parse_state_address:
|
---|
| 171 | address_len = (int)pos - (int)address;
|
---|
| 172 | break;
|
---|
| 173 | case proxy_parse_state_port:
|
---|
| 174 | port_len = (int)pos - (int)port;
|
---|
| 175 | break;
|
---|
| 176 | case proxy_parse_state_error:
|
---|
| 177 | default:
|
---|
| 178 | goto error;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | if (schema_len > 0) {
|
---|
| 182 | if (strncmp(schema, "http", schema_len) != 0)
|
---|
| 183 | goto error;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | if (address_len == 0)
|
---|
| 187 | goto error;
|
---|
| 188 |
|
---|
| 189 | char *buf = malloc(address_len + port_len + username_len + password_len + 4);
|
---|
| 190 | if (buf == NULL) {
|
---|
| 191 | if (!silent)
|
---|
| 192 | printf("Memory error.\n");
|
---|
| 193 | return -1;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | free((char *)g_proxy_options.host_address);
|
---|
| 197 |
|
---|
| 198 | memcpy(buf, address, address_len);
|
---|
| 199 | buf[address_len] = '\0';
|
---|
| 200 | g_proxy_options.host_address = buf;
|
---|
| 201 |
|
---|
| 202 | buf = buf + address_len + 1;
|
---|
| 203 | memcpy(buf, port, port_len);
|
---|
| 204 | buf[port_len] = '\0';
|
---|
| 205 | g_proxy_options.port = port_len == 0 ? 8080 : atoi(buf);
|
---|
| 206 |
|
---|
| 207 | buf = buf + port_len + 1;
|
---|
| 208 | memcpy(buf, username, username_len);
|
---|
| 209 | buf[username_len] = '\0';
|
---|
| 210 | g_proxy_options.username = (username_len == 0) && (password_len == 0) ? NULL : buf;
|
---|
| 211 |
|
---|
| 212 | buf = buf + username_len + 1;
|
---|
| 213 | memcpy(buf, password, password_len);
|
---|
| 214 | buf[password_len] = '\0';
|
---|
| 215 | g_proxy_options.password = (username_len == 0) && (password_len == 0) ? NULL : buf;
|
---|
| 216 |
|
---|
| 217 | return 0;
|
---|
| 218 | error:
|
---|
| 219 | if (!silent)
|
---|
| 220 | printf("proxy string parse error. 'http://[<user>:<password>@]<address>:<port>'\n");
|
---|
| 221 | return -1;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 |
|
---|
| 225 | int set_cs_main(int argc, char **argv)
|
---|
| 226 | {
|
---|
| 227 | if (argc < 2) {
|
---|
| 228 | printf("usage:\n%s <connection string>\n", argv[0]);
|
---|
| 229 | return 0;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | int len = strlen(argv[1]);
|
---|
| 233 | if (len < 70) {
|
---|
| 234 | printf("String containing Hostname, Device Id & Device Key in the format:\n");
|
---|
| 235 | printf(" HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>\n");
|
---|
| 236 | printf(" HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>\n");
|
---|
| 237 | return 0;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | char *conn_str = (char *)malloc(len + 1);
|
---|
| 241 | if (conn_str == NULL) {
|
---|
| 242 | printf("OOM while creating connection string\n");
|
---|
| 243 | return 1;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | memcpy(conn_str, argv[1], len);
|
---|
| 247 | conn_str[len] = 0;
|
---|
| 248 | free(connectionString);
|
---|
| 249 | connectionString = conn_str;
|
---|
| 250 | printf("Connection String:\n%s\n", connectionString);
|
---|
| 251 |
|
---|
| 252 | return 0;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | int set_proxy_main(int argc, char **argv)
|
---|
| 256 | {
|
---|
| 257 | int result;
|
---|
| 258 |
|
---|
| 259 | if (argc < 2) {
|
---|
| 260 | if (g_proxy_options.username != NULL) {
|
---|
| 261 | printf("HTTP_PROXY=http://%s:%s@%s:%d\n",
|
---|
| 262 | g_proxy_options.username,
|
---|
| 263 | g_proxy_options.password,
|
---|
| 264 | g_proxy_options.host_address,
|
---|
| 265 | g_proxy_options.port);
|
---|
| 266 | }
|
---|
| 267 | else if (g_proxy_options.host_address != NULL) {
|
---|
| 268 | printf("HTTP_PROXY=http://%s:%d\n",
|
---|
| 269 | g_proxy_options.host_address,
|
---|
| 270 | g_proxy_options.port);
|
---|
| 271 | }
|
---|
| 272 | else {
|
---|
| 273 | printf("HTTP_PROXY=\n");
|
---|
| 274 | }
|
---|
| 275 | return 0;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | if ((result = set_proxy(argv[1], false)) == 0) {
|
---|
| 279 | g_use_proxy = true;
|
---|
| 280 | }
|
---|
| 281 | else {
|
---|
| 282 | g_use_proxy = false;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | return 0;
|
---|
| 286 | }
|
---|
| 287 | int clear_proxy_main(int argc, char **argv)
|
---|
| 288 | {
|
---|
| 289 | free((char *)g_proxy_options.host_address);
|
---|
| 290 | g_proxy_options.host_address = NULL;
|
---|
| 291 | g_proxy_options.port = 0;
|
---|
| 292 | g_proxy_options.username = NULL;
|
---|
| 293 | g_proxy_options.password = NULL;
|
---|
| 294 |
|
---|
| 295 | g_use_proxy = false;
|
---|
| 296 | return 0;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | int set_wifi_main(int argc, char **argv)
|
---|
| 300 | {
|
---|
| 301 | if (argc < 3) {
|
---|
| 302 | return 0;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | set_ssid_pwd(argv[1], argv[2]);
|
---|
| 306 |
|
---|
| 307 | prepare_esp_at();
|
---|
| 308 | }
|
---|