1 🚀 OpenTTY Developer Documentation
Lima edited this page 2025-10-20 10:35:27 -03:00
This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

🧭 Overview

OpenTTY is a feature-rich MIDlet application that provides a Unix-like shell environment for Java ME (J2ME) devices. It implements a comprehensive command-line interface with file system management, process control, networking capabilities, and UI components.

🏗️ MIDlet Loading Process

🔄 Initialization Sequence

public void startApp() {
    // 1. Set system attributes
    attributes.put("PATCH", "Release code-name");
    attributes.put("VERSION", getAppProperty("MIDlet-Version"));
    // ... more attributes
    
    // 2. Initialize system properties
    String[] KEYS = { "TYPE", "CONFIG", "PROFILE", "LOCALE" };
    String[] SYS = { "platform", "configuration", "profiles", "locale" };
    
    // 3. Set up Nano editor commands
    Command[] NANO_CMDS = { BACK, CLEAR, RUNS, VIEW };
    
    // 4. Execute initialization script
    if (runScript(read("/etc/init"), 0) != 0) { 
        destroyApp(true); // If an error ocurred here, MIDlet crashes
    }
    
    // 5. Check user authentication
    if (username.equals("") || MIDletControl.passwd().equals("")) { 
        new MIDletControl(null); // not authenticated, go to login form
    } else { 
        run("/home/.initrc", new String[] { "/home/.initrc" }, 1000); // else run user init script
    }
}

🎮 Command Processing System

⚙️ processCommand Method

The processCommand method is the core command interpreter that handles all user input and system commands.

📝 Method Signature

public int processCommand(String command, boolean ignore, int id)

🔄 Processing Flow

  1. Command Parsing:

    • Extracts main command and arguments
    • Handles environment variable substitution via env()
    • Supports background execution with & suffix
  2. Command Resolution:

    • Checks shell aliases (aliases hashtable)
    • Looks up shell functions (functions hashtable)
    • Searches classpath binaries (/bin/ directory)
    • Falls back to built-in commands
  3. Classpath System:

    • When classpath = true, automatically executes binaries from /bin/
    • Bypasses for shell builtins (sh, lua) and hidden commands
    • Enables extensibility through external scripts

🔧 Argument Handling

  • getCommand(String input): Extracts the first word as command
  • getArgument(String input): Returns everything after the command
  • splitArgs(String content): Splits arguments respecting quotes

🗂️ Standard Tables & Data Structures

💾 Core Hashtables

public Hashtable attributes = new Hashtable();    // Environment variables
public Hashtable paths = new Hashtable();         // Virtual file system
public Hashtable trace = new Hashtable();         // Process structure
public Hashtable aliases = new Hashtable();       // Command aliases
public Hashtable shell = new Hashtable();         // Applications shells
public Hashtable functions = new Hashtable();     // User-defined functions
public Hashtable tmp = new Hashtable();           // Temporary storage (/tmp/)

📊 Process Table (trace)

  • Key: Process ID (PID)
  • Value: Hashtable containing:
    • "name": Process name
    • "owner": User who started process
    • "collector": Cleanup command
    • "screen": Associated UI component
    • Process-specific data

🔄 Process Management System

📈 Process Lifecycle

🚀 Starting Processes

public int start(String app, String pid, String collector, int id)
  • Generates process hashtable via genprocess()
  • Special handling for system processes (sh, x11-wm)
  • Assigns unique PID or uses reserved IDs for system processes

🏷️ Process Types

  • System Processes: PID "1" (shell), "2" (X11 window manager), "3" (Audio Codec)
  • User Processes: Random 4-digit PIDs (1000-9999)
  • Background Processes: Created with bg command
  • Network Processes: Socket servers, listeners

⚠️ Process Control

public int kill(String pid, boolean print, int id) // Kill by pid
public int stop(String app, int id) // Kill all process with name equals app

Process Information

  • getprocess(String pid): Retrieve process data
  • getobject(String pid, String item): Access process attributes
  • getpid(String name): Find PID by process name (return null if not found)
  • getowner(String pid): Get process owner

⌨️ Input/Output System

📥 Standard Input (stdin)

public TextField stdin = new TextField("Command", "", 256, TextField.ANY);
  • Type: TextField component
  • Purpose: User command input
  • Integration: Bound to EXECUTE command

📤 Standard Output (stdout)

public StringItem stdout = new StringItem("", "");
  • Type: StringItem component
  • Purpose: Command output display
  • Buffer Management: Controlled by TTY_MAX_LEN

🎯 Output Handling

public void echoCommand(String message, StringItem console)
  • Appends to console with newline separation
  • Respects TTY_MAX_LEN for buffer limits
  • Updates attributes.put("OUTPUT", message) for scripting

🎛️ MIDletControl Class

🎯 Multi-purpose Controller

The MIDletControl class handles various UI modes and background operations through different constructors.

🚀 Operation Modes

public static final int 
    HISTORY = 1,        // Command history browser
    EXPLORER = 2,       // File system browser  
    MONITOR = 3,        // System monitor
    PROCESS = 4,        // Process manager
    SIGNUP = 5,         // User registration
    REQUEST = 7,        // Sudo password prompt
    NC = 9,             // Netcat client
    PRSCAN = 10,        // Port scanner
    GOBUSTER = 11,      // Directory brute forcer
    BIND = 12,          // Bind shell server
    SCREEN = 13,        // Custom screen builder
    LIST = 14,          // List selector
    QUEST = 15,         // Question/input dialog
    WEDIT = 16,         // Text editor
    BG = 17,            // Background process
    ADDON = 18;         // Add-on process

🔑 Key Features

📁 File System Browser (EXPLORER)

  • Navigates virtual file system paths
  • File operations: open, delete, run, properties
  • Integration with physical file system via /mnt/

📊 Process Manager (PROCESS)

  • Lists all running processes with PIDs
  • Process control: kill, view info, load screen
  • Filtering capability

🌐 Network Tools

  • Netcat Client: Raw socket communication
  • Port Scanner: TCP port discovery
  • GoBuster: Web directory brute forcing
  • Bind Server: Command server with socket binding

🛠️ Command Categories

⚙️ System Management

  • ps, top, trace - Process monitoring
  • start, stop, kill - Process control
  • gc - Memory management

📁 File Operations

  • ls, dir, cd - Navigation
  • cat, nano, view - File viewing/editing
  • rm, cp, mkdir - File manipulation

🌐 Networking

  • ping, pong - Connectivity testing
  • curl, wget - HTTP requests
  • nc, prscan - Socket tools

👥 User Management

  • whoami, id - User information
  • sudo, su - Privilege escalation
  • passwd - Password management

📜 Scripting & Automation

  • if, for, case - Control structures
  • function - User-defined functions
  • import - Package loading
  • eval - Dynamic execution

📦 Package System

📥 Import Mechanism

private int importScript(String script, int id)

🏗️ Package Structure

  • Configuration: Properties-style format
  • API Versioning: Compatibility checking
  • Dependencies: include directive for other packages
  • Process Definition: Background services
  • Command Registration: Shell command extensions
  • File Deployment: Resource file installation

📚 Read the Wiki page of OpenTTY Applications (Native)

This comprehensive system makes OpenTTY a powerful platform for mobile device automation and scripting in constrained J2ME environments.