Home | History | Annotate | Download | only in api
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Use the <code>chrome.serial</code> API to read from and write to a device
      6 // connected to a serial port.
      7 namespace serial {
      8 
      9   callback GetPortsCallback = void (DOMString[] ports);
     10 
     11   dictionary OpenOptions {
     12     // The requested bitrate of the connection to be opened. For compatibility
     13     // with the widest range of hardware, this number should match one of
     14     // commonly-available bitrates, such as 110, 300, 1200, 2400, 4800, 9600,
     15     // 14400, 19200, 38400, 57600, 115200. There is no guarantee, of course,
     16     // that the device connected to the serial port will support the requested
     17     // bitrate, even if the port itself supports that bitrate.
     18     long bitrate;
     19   };
     20 
     21   dictionary OpenInfo {
     22     // The id of the opened connection.
     23     long connectionId;
     24   };
     25 
     26   callback OpenCallback = void (OpenInfo openInfo);
     27 
     28   // Returns true if operation was successful.
     29   callback CloseCallback = void (boolean result);
     30 
     31   dictionary ReadInfo {
     32     // The number of bytes received, or a negative number if an error occurred.
     33     // This number will be smaller than the number of bytes requested in the
     34     // original read call if the call would need to block to read that number
     35     // of bytes.
     36     long bytesRead;
     37 
     38     // The data received.
     39     ArrayBuffer data;
     40   };
     41 
     42   callback ReadCallback = void (ReadInfo readInfo);
     43 
     44   dictionary WriteInfo {
     45     // The number of bytes written.
     46     long bytesWritten;
     47   };
     48 
     49   callback WriteCallback = void (WriteInfo writeInfo);
     50 
     51   // Returns true if operation was successful.
     52   callback FlushCallback = void (boolean result);
     53 
     54   // Boolean true = mark signal (negative serial voltage).
     55   // Boolean false = space signal (positive serial voltage).
     56   //
     57   // For SetControlSignals, include the sendable signals that you wish to
     58   // change. Signals not included in the dictionary will be left unchanged.
     59   //
     60   // GetControlSignals includes all receivable signals.
     61   dictionary ControlSignalOptions {
     62     // Serial control signals that your machine can send. Missing fields will
     63     // be set to false.
     64     boolean? dtr;
     65     boolean? rts;
     66 
     67     // Serial control signals that your machine can receive. If a get operation
     68     // fails, success will be false, and these fields will be absent.
     69     //
     70     // DCD (Data Carrier Detect) is equivalent to RLSD (Receive Line Signal
     71     // Detect) on some platforms.
     72     boolean? dcd;
     73     boolean? cts;
     74   };
     75 
     76   // Returns a snapshot of current control signals.
     77   callback GetControlSignalsCallback = void (ControlSignalOptions options);
     78 
     79   // Returns true if operation was successful.
     80   callback SetControlSignalsCallback = void (boolean result);
     81 
     82   interface Functions {
     83     // Returns names of valid ports on this machine, each of which is likely to
     84     // be valid to pass as the port argument to open(). The list is regenerated
     85     // each time this method is called, as port validity is dynamic.
     86     //
     87     // |callback| : Called with the list of ports.
     88     static void getPorts(GetPortsCallback callback);
     89 
     90     // Opens a connection to the given serial port.
     91     // |port| : The name of the serial port to open.
     92     // |options| : Connection options.
     93     // |callback| : Called when the connection has been opened.
     94     static void open(DOMString port,
     95                      optional OpenOptions options,
     96                      OpenCallback callback);
     97 
     98     // Closes an open connection.
     99     // |connectionId| : The id of the opened connection.
    100     // |callback| : Called when the connection has been closed.
    101     static void close(long connectionId,
    102                       CloseCallback callback);
    103 
    104     // Reads a byte from the given connection.
    105     // |connectionId| : The id of the connection.
    106     // |bytesToRead| : The number of bytes to read.
    107     // |callback| : Called when all the requested bytes have been read or
    108     //              when the read blocks.
    109     static void read(long connectionId,
    110                      long bytesToRead,
    111                      ReadCallback callback);
    112 
    113     // Writes a string to the given connection.
    114     // |connectionId| : The id of the connection.
    115     // |data| : The string to write.
    116     // |callback| : Called when the string has been written.
    117     static void write(long connectionId,
    118                       ArrayBuffer data,
    119                       WriteCallback callback);
    120 
    121     // Flushes all bytes in the given connection's input and output buffers.
    122     // |connectionId| : The id of the connection.
    123     // |callback| : Called when the flush is complete.
    124     static void flush(long connectionId,
    125                       FlushCallback callback);
    126 
    127     static void getControlSignals(long connectionId,
    128                                   GetControlSignalsCallback callback);
    129 
    130     static void setControlSignals(long connectionId,
    131                                   ControlSignalOptions options,
    132                                   SetControlSignalsCallback callback);
    133   };
    134 
    135 };
    136