[453] | 1 | /*
|
---|
[458] | 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 | * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
| 12 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
| 13 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
| 14 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
| 15 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
| 16 | * スコード中に含まれていること.
|
---|
| 17 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
| 18 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
| 19 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
| 20 | * の無保証規定を掲載すること.
|
---|
| 21 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
| 22 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
| 23 | * と.
|
---|
| 24 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
| 25 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
| 26 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
| 27 | * 報告すること.
|
---|
| 28 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
| 29 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
| 30 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
| 31 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
| 32 | * 免責すること.
|
---|
| 33 | *
|
---|
| 34 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
| 35 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
| 36 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
| 37 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
| 38 | * の責任を負わない.
|
---|
| 39 | *
|
---|
| 40 | * $Id$
|
---|
| 41 | */
|
---|
[453] | 42 | #include <stdio.h>
|
---|
| 43 | #include <stdlib.h>
|
---|
| 44 | #include <stdbool.h>
|
---|
| 45 | #include <string.h>
|
---|
| 46 | #include "azure_c_shared_utility/shared_util_options.h"
|
---|
| 47 |
|
---|
| 48 | extern char* connectionString;
|
---|
| 49 |
|
---|
| 50 | extern bool g_use_proxy;
|
---|
| 51 | extern HTTP_PROXY_OPTIONS g_proxy_options;
|
---|
| 52 |
|
---|
| 53 | enum proxy_parse_state_t {
|
---|
| 54 | proxy_parse_state_schema,
|
---|
| 55 | proxy_parse_state_colonslash,
|
---|
| 56 | proxy_parse_state_colonslashslash,
|
---|
| 57 | proxy_parse_state_username,
|
---|
| 58 | proxy_parse_state_password,
|
---|
| 59 | proxy_parse_state_address,
|
---|
| 60 | proxy_parse_state_port,
|
---|
| 61 | proxy_parse_state_error,
|
---|
| 62 | };
|
---|
| 63 |
|
---|
| 64 | int set_proxy(const char *proxy, bool silent)
|
---|
| 65 | {
|
---|
| 66 | const char *schema = NULL, *address = NULL, *port = NULL, *username = NULL, *password = NULL;
|
---|
| 67 | int schema_len = 0, address_len = 0, port_len = 0, username_len = 0, password_len = 0;
|
---|
| 68 |
|
---|
| 69 | enum proxy_parse_state_t state = proxy_parse_state_schema;
|
---|
| 70 |
|
---|
| 71 | const char *pos = proxy;
|
---|
| 72 | schema = pos;
|
---|
| 73 | for (char c = *pos; c != '\0'; pos++, c = *pos) {
|
---|
| 74 | switch (state) {
|
---|
| 75 | case proxy_parse_state_schema:
|
---|
| 76 | if (c == ':') {
|
---|
| 77 | schema_len = (int)pos - (int)schema;
|
---|
| 78 | username = pos + 3;
|
---|
| 79 | state = proxy_parse_state_colonslash;
|
---|
| 80 | }
|
---|
| 81 | break;
|
---|
| 82 | case proxy_parse_state_colonslash:
|
---|
| 83 | if (c == '/') {
|
---|
| 84 | state = proxy_parse_state_colonslashslash;
|
---|
| 85 | }
|
---|
| 86 | else {
|
---|
| 87 | username = schema;
|
---|
| 88 | username_len = schema_len;
|
---|
| 89 | schema = NULL;
|
---|
| 90 | schema_len = 0;
|
---|
| 91 | password = pos;
|
---|
| 92 | state = proxy_parse_state_password;
|
---|
| 93 | goto case_proxy_parse_state_password;
|
---|
| 94 | }
|
---|
| 95 | break;
|
---|
| 96 | case proxy_parse_state_colonslashslash:
|
---|
| 97 | if (c == '/') {
|
---|
| 98 | state = proxy_parse_state_username;
|
---|
| 99 | }
|
---|
| 100 | else {
|
---|
| 101 | state = proxy_parse_state_error;
|
---|
| 102 | }
|
---|
| 103 | break;
|
---|
| 104 | case proxy_parse_state_username:
|
---|
| 105 | if (c == ':') {
|
---|
| 106 | username_len = (int)pos - (int)username;
|
---|
| 107 | password = pos + 1;
|
---|
| 108 | state = proxy_parse_state_password;
|
---|
| 109 | }
|
---|
| 110 | else if (c == '@') {
|
---|
| 111 | username_len = (int)pos - (int)username;
|
---|
| 112 | address = pos + 1;
|
---|
| 113 | state = proxy_parse_state_address;
|
---|
| 114 | }
|
---|
| 115 | break;
|
---|
| 116 | case proxy_parse_state_password:
|
---|
| 117 | case_proxy_parse_state_password:
|
---|
| 118 | if (c == '@') {
|
---|
| 119 | password_len = (int)pos - (int)password;
|
---|
| 120 | address = pos + 1;
|
---|
| 121 | state = proxy_parse_state_address;
|
---|
| 122 | }
|
---|
| 123 | break;
|
---|
| 124 | case proxy_parse_state_address:
|
---|
| 125 | if (c == ':') {
|
---|
| 126 | address_len = (int)pos - (int)address;
|
---|
| 127 | port = pos + 1;
|
---|
| 128 | state = proxy_parse_state_port;
|
---|
| 129 | }
|
---|
| 130 | break;
|
---|
| 131 | case proxy_parse_state_port:
|
---|
| 132 | if ((c < '0') && (c > '9')) {
|
---|
| 133 | state = proxy_parse_state_error;
|
---|
| 134 | }
|
---|
| 135 | break;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | if (state == proxy_parse_state_error)
|
---|
| 139 | break;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | switch (state)
|
---|
| 143 | {
|
---|
| 144 | case proxy_parse_state_schema:
|
---|
| 145 | case proxy_parse_state_colonslash:
|
---|
| 146 | case proxy_parse_state_colonslashslash:
|
---|
| 147 | goto error;
|
---|
| 148 | case proxy_parse_state_username:
|
---|
| 149 | username_len = (int)pos - (int)username;
|
---|
| 150 | address = username;
|
---|
| 151 | address_len = username_len;
|
---|
| 152 | username = NULL;
|
---|
| 153 | username_len = 0;
|
---|
| 154 | break;
|
---|
| 155 | case proxy_parse_state_password:
|
---|
| 156 | password_len = (int)pos - (int)password;
|
---|
| 157 | address = username;
|
---|
| 158 | address_len = username_len;
|
---|
| 159 | username = NULL;
|
---|
| 160 | username_len = 0;
|
---|
| 161 | port = password;
|
---|
| 162 | port_len = password_len;
|
---|
| 163 | password = NULL;
|
---|
| 164 | password_len = 0;
|
---|
| 165 | break;
|
---|
| 166 | case proxy_parse_state_address:
|
---|
| 167 | address_len = (int)pos - (int)address;
|
---|
| 168 | break;
|
---|
| 169 | case proxy_parse_state_port:
|
---|
| 170 | port_len = (int)pos - (int)port;
|
---|
| 171 | break;
|
---|
| 172 | case proxy_parse_state_error:
|
---|
| 173 | default:
|
---|
| 174 | goto error;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | if (schema_len > 0) {
|
---|
| 178 | if (strncmp(schema, "http", schema_len) != 0)
|
---|
| 179 | goto error;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | if (address_len == 0)
|
---|
| 183 | goto error;
|
---|
| 184 |
|
---|
| 185 | char *buf = malloc(address_len + port_len + username_len + password_len + 4);
|
---|
| 186 | if (buf == NULL) {
|
---|
| 187 | if (!silent)
|
---|
| 188 | printf("Memory error.\n");
|
---|
| 189 | return -1;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | free((char *)g_proxy_options.host_address);
|
---|
| 193 |
|
---|
| 194 | memcpy(buf, address, address_len);
|
---|
| 195 | buf[address_len] = '\0';
|
---|
| 196 | g_proxy_options.host_address = buf;
|
---|
| 197 |
|
---|
| 198 | buf = buf + address_len + 1;
|
---|
| 199 | memcpy(buf, port, port_len);
|
---|
| 200 | buf[port_len] = '\0';
|
---|
| 201 | g_proxy_options.port = port_len == 0 ? 8080 : atoi(buf);
|
---|
| 202 |
|
---|
| 203 | buf = buf + port_len + 1;
|
---|
| 204 | memcpy(buf, username, username_len);
|
---|
| 205 | buf[username_len] = '\0';
|
---|
| 206 | g_proxy_options.username = (username_len == 0) && (password_len == 0) ? NULL : buf;
|
---|
| 207 |
|
---|
| 208 | buf = buf + username_len + 1;
|
---|
| 209 | memcpy(buf, password, password_len);
|
---|
| 210 | buf[password_len] = '\0';
|
---|
| 211 | g_proxy_options.password = (username_len == 0) && (password_len == 0) ? NULL : buf;
|
---|
| 212 |
|
---|
| 213 | return 0;
|
---|
| 214 | error:
|
---|
| 215 | if (!silent)
|
---|
| 216 | printf("proxy string parse error. 'http://[<user>:<password>@]<address>:<port>'\n");
|
---|
| 217 | return -1;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | int set_cs_main(int argc, char **argv)
|
---|
| 221 | {
|
---|
| 222 | if (argc < 2) {
|
---|
| 223 | printf("usage:\n%s <connection string>\n", argv[0]);
|
---|
| 224 | return 0;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | int len = strlen(argv[1]);
|
---|
| 228 | if (len < 70) {
|
---|
| 229 | printf("String containing Hostname, Device Id & Device Key in the format:\n");
|
---|
| 230 | printf(" HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>\n");
|
---|
| 231 | printf(" HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>\n");
|
---|
| 232 | return 0;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | char *conn_str = (char *)malloc(len + 1);
|
---|
| 236 | if (conn_str == NULL) {
|
---|
| 237 | printf("OOM while creating connection string\n");
|
---|
| 238 | return 1;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | memcpy(conn_str, argv[1], len);
|
---|
| 242 | conn_str[len] = 0;
|
---|
| 243 | free(connectionString);
|
---|
| 244 | connectionString = conn_str;
|
---|
| 245 | printf("Connection String:\n%s\n", connectionString);
|
---|
| 246 |
|
---|
| 247 | return 0;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | int set_proxy_main(int argc, char **argv)
|
---|
| 251 | {
|
---|
| 252 | int result;
|
---|
| 253 |
|
---|
| 254 | if (argc < 2) {
|
---|
| 255 | if (g_proxy_options.username != NULL) {
|
---|
| 256 | printf("HTTP_PROXY=http://%s:%s@%s:%d\n",
|
---|
| 257 | g_proxy_options.username,
|
---|
| 258 | g_proxy_options.password,
|
---|
| 259 | g_proxy_options.host_address,
|
---|
| 260 | g_proxy_options.port);
|
---|
| 261 | }
|
---|
| 262 | else if (g_proxy_options.host_address != NULL) {
|
---|
| 263 | printf("HTTP_PROXY=http://%s:%d\n",
|
---|
| 264 | g_proxy_options.host_address,
|
---|
| 265 | g_proxy_options.port);
|
---|
| 266 | }
|
---|
| 267 | else {
|
---|
| 268 | printf("HTTP_PROXY=\n");
|
---|
| 269 | }
|
---|
| 270 | return 0;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | if ((result = set_proxy(argv[1], false)) == 0) {
|
---|
| 274 | g_use_proxy = true;
|
---|
| 275 | }
|
---|
| 276 | else {
|
---|
| 277 | g_use_proxy = false;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | return 0;
|
---|
| 281 | }
|
---|
[458] | 282 |
|
---|
[453] | 283 | int clear_proxy_main(int argc, char **argv)
|
---|
| 284 | {
|
---|
| 285 | free((char *)g_proxy_options.host_address);
|
---|
| 286 | g_proxy_options.host_address = NULL;
|
---|
| 287 | g_proxy_options.port = 0;
|
---|
| 288 | g_proxy_options.username = NULL;
|
---|
| 289 | g_proxy_options.password = NULL;
|
---|
| 290 |
|
---|
| 291 | g_use_proxy = false;
|
---|
| 292 | return 0;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | int set_wifi_main(int argc, char **argv)
|
---|
| 296 | {
|
---|
| 297 | if (argc < 3) {
|
---|
| 298 | return 0;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | set_ssid_pwd(argv[1], argv[2]);
|
---|
| 302 |
|
---|
| 303 | prepare_esp_at();
|
---|
| 304 | }
|
---|