Changeset 47d060d in mainline for uspace/srv/hid/remcons/user.c
- Timestamp:
- 2024-10-04T19:23:16Z (8 months ago)
- Branches:
- master
- Children:
- c7ecd290
- Parents:
- 5132379
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/hid/remcons/user.c
r5132379 r47d060d 58 58 static LIST_INITIALIZE(users); 59 59 60 static errno_t telnet_user_send_raw_locked(telnet_user_t *, const void *, 61 size_t); 62 static errno_t telnet_user_flush_locked(telnet_user_t *); 63 60 64 /** Create new telnet user. 61 65 * 62 66 * @param conn Incoming connection. 67 * @param cb Callback functions 68 * @param arg Argument to callback functions 63 69 * @return New telnet user or NULL when out of memory. 64 70 */ 65 telnet_user_t *telnet_user_create(tcp_conn_t *conn )71 telnet_user_t *telnet_user_create(tcp_conn_t *conn, telnet_cb_t *cb, void *arg) 66 72 { 67 73 static int telnet_user_id_counter = 0; … … 72 78 } 73 79 80 user->cb = cb; 81 user->arg = arg; 74 82 user->id = ++telnet_user_id_counter; 75 83 … … 216 224 * @return EOK on success or an error code 217 225 */ 218 static errno_t telnet_user_recv_next_byte_locked(telnet_user_t *user, char *byte) 226 static errno_t telnet_user_recv_next_byte_locked(telnet_user_t *user, 227 uint8_t *byte) 219 228 { 220 229 errno_t rc; … … 227 236 } 228 237 229 *byte = user->socket_buffer[user->socket_buffer_pos++];238 *byte = (uint8_t)user->socket_buffer[user->socket_buffer_pos++]; 230 239 return EOK; 231 240 } … … 241 250 } 242 251 243 /** Process telnet command (currently only print to screen). 252 static errno_t telnet_user_send_opt(telnet_user_t *user, telnet_cmd_t cmd, 253 telnet_cmd_t opt) 254 { 255 uint8_t cmdb[3]; 256 257 cmdb[0] = TELNET_IAC; 258 cmdb[1] = cmd; 259 cmdb[2] = opt; 260 261 return telnet_user_send_raw_locked(user, (char *)cmdb, sizeof(cmdb)); 262 } 263 264 /** Process telnet WILL NAWS command. 265 * 266 * @param user Telnet user structure. 267 * @param cmd Telnet command. 268 */ 269 static void process_telnet_will_naws(telnet_user_t *user) 270 { 271 telnet_user_log(user, "WILL NAWS"); 272 /* Send DO NAWS */ 273 (void) telnet_user_send_opt(user, TELNET_DO, TELNET_NAWS); 274 (void) telnet_user_flush_locked(user); 275 } 276 277 /** Process telnet SB NAWS command. 278 * 279 * @param user Telnet user structure. 280 * @param cmd Telnet command. 281 */ 282 static void process_telnet_sb_naws(telnet_user_t *user) 283 { 284 uint8_t chi, clo; 285 uint8_t rhi, rlo; 286 uint16_t cols; 287 uint16_t rows; 288 uint8_t iac; 289 uint8_t se; 290 errno_t rc; 291 292 telnet_user_log(user, "SB NAWS..."); 293 294 rc = telnet_user_recv_next_byte_locked(user, &chi); 295 if (rc != EOK) 296 return; 297 rc = telnet_user_recv_next_byte_locked(user, &clo); 298 if (rc != EOK) 299 return; 300 301 rc = telnet_user_recv_next_byte_locked(user, &rhi); 302 if (rc != EOK) 303 return; 304 rc = telnet_user_recv_next_byte_locked(user, &rlo); 305 if (rc != EOK) 306 return; 307 308 rc = telnet_user_recv_next_byte_locked(user, &iac); 309 if (rc != EOK) 310 return; 311 rc = telnet_user_recv_next_byte_locked(user, &se); 312 if (rc != EOK) 313 return; 314 315 cols = (chi << 8) | clo; 316 rows = (rhi << 8) | rlo; 317 318 telnet_user_log(user, "cols=%u rows=%u\n", cols, rows); 319 320 if (cols < 1 || rows < 1) { 321 telnet_user_log(user, "Ignoring invalid window size update."); 322 return; 323 } 324 325 user->cb->ws_update(user->arg, cols, rows); 326 } 327 328 /** Process telnet WILL command. 329 * 330 * @param user Telnet user structure. 331 * @param opt Option code. 332 */ 333 static void process_telnet_will(telnet_user_t *user, telnet_cmd_t opt) 334 { 335 telnet_user_log(user, "WILL"); 336 switch (opt) { 337 case TELNET_NAWS: 338 process_telnet_will_naws(user); 339 return; 340 } 341 342 telnet_user_log(user, "Ignoring telnet command %u %u %u.", 343 TELNET_IAC, TELNET_WILL, opt); 344 } 345 346 /** Process telnet SB command. 347 * 348 * @param user Telnet user structure. 349 * @param opt Option code. 350 */ 351 static void process_telnet_sb(telnet_user_t *user, telnet_cmd_t opt) 352 { 353 telnet_user_log(user, "SB"); 354 switch (opt) { 355 case TELNET_NAWS: 356 process_telnet_sb_naws(user); 357 return; 358 } 359 360 telnet_user_log(user, "Ignoring telnet command %u %u %u.", 361 TELNET_IAC, TELNET_SB, opt); 362 } 363 364 /** Process telnet command. 244 365 * 245 366 * @param user Telnet user structure. … … 250 371 telnet_cmd_t option_code, telnet_cmd_t cmd) 251 372 { 373 switch (option_code) { 374 case TELNET_SB: 375 process_telnet_sb(user, cmd); 376 return; 377 case TELNET_WILL: 378 process_telnet_will(user, cmd); 379 return; 380 } 381 252 382 if (option_code != 0) { 253 383 telnet_user_log(user, "Ignoring telnet command %u %u %u.", … … 277 407 278 408 do { 279 charnext_byte = 0;409 uint8_t next_byte = 0; 280 410 bool inside_telnet_command = false; 281 411 … … 290 420 return rc; 291 421 } 292 uint8_t byte = (uint8_t)next_byte;422 uint8_t byte = next_byte; 293 423 294 424 /* Skip telnet commands. */ … … 296 426 inside_telnet_command = false; 297 427 next_byte = 0; 298 if (TELNET_IS_OPTION_CODE(byte)) { 428 if (TELNET_IS_OPTION_CODE(byte) || 429 byte == TELNET_SB) { 299 430 telnet_option_code = byte; 300 431 inside_telnet_command = true; … … 430 561 } 431 562 563 static errno_t telnet_user_flush_locked(telnet_user_t *user) 564 { 565 errno_t rc; 566 567 rc = tcp_conn_send(user->conn, user->send_buf, user->send_buf_used); 568 if (rc != EOK) 569 return rc; 570 571 user->send_buf_used = 0; 572 return EOK; 573 } 574 432 575 errno_t telnet_user_flush(telnet_user_t *user) 433 576 { … … 435 578 436 579 fibril_mutex_lock(&user->guard); 437 rc = tcp_conn_send(user->conn, user->send_buf, user->send_buf_used); 438 439 if (rc != EOK) { 440 fibril_mutex_unlock(&user->guard); 441 return rc; 442 } 443 444 user->send_buf_used = 0; 580 rc = telnet_user_flush_locked(user); 445 581 fibril_mutex_unlock(&user->guard); 446 return EOK;582 return rc; 447 583 } 448 584 … … 467 603 } 468 604 605 /** Resize telnet session. 606 * 607 * @param user Telnet user 608 * @param cols New number of columns 609 * @param rows New number of rows 610 */ 611 void telnet_user_resize(telnet_user_t *user, unsigned cols, unsigned rows) 612 { 613 user->cols = cols; 614 user->rows = rows; 615 if ((unsigned)user->cursor_x > cols - 1) 616 user->cursor_x = cols - 1; 617 if ((unsigned)user->cursor_y > rows - 1) 618 user->cursor_y = rows - 1; 619 } 620 469 621 /** 470 622 * @}
Note:
See TracChangeset
for help on using the changeset viewer.