Changeset ca97cad in mainline for uspace/lib


Ignore:
Timestamp:
2010-05-06T11:42:55Z (16 years ago)
Author:
Lenka Trochtova <trochtova.lenka@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ab1aa871
Parents:
ba95e8f
Message:

writing to serial port using character interface

Location:
uspace/lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/libc/generic/device/char.c

    rba95e8f rca97cad  
    4040#include <stdio.h>
    4141
    42 
    43 int read_dev(int dev_phone, void *buf, size_t len)
    44 {       
     42/** Read to or write from the device using its character interface.
     43 *
     44 * Helper function.
     45 *
     46 * @param dev_phone phone to the device.
     47 * @param buf the buffer for the data read from or written to the device.
     48 * @param len the maximum length of the data to be read or written.
     49 * @param read read from the device if true, write to it otherwise.
     50 *
     51 * @return non-negative number of bytes actually read from or written to the device on success,
     52 * negative error number otherwise.
     53 *
     54 */
     55static int rw_dev(int dev_phone, void *buf, size_t len, bool read)
     56{
    4557        ipc_call_t answer;
    4658       
    4759        async_serialize_start();
    4860       
    49         aid_t req = async_send_1(dev_phone, DEV_IFACE_ID(CHAR_DEV_IFACE), CHAR_READ_DEV, &answer);
     61        aid_t req;
     62        int rc;
    5063       
    51         int rc = async_data_read_start(dev_phone, buf, len);
     64        if (read) {
     65                req = async_send_1(dev_phone, DEV_IFACE_ID(CHAR_DEV_IFACE), CHAR_READ_DEV, &answer);
     66                rc = async_data_read_start(dev_phone, buf, len);               
     67        } else {
     68                req = async_send_1(dev_phone, DEV_IFACE_ID(CHAR_DEV_IFACE), CHAR_WRITE_DEV, &answer);
     69                rc = async_data_write_start(dev_phone, buf, len);
     70        }
    5271       
    5372        if (rc != EOK) {
     
    7089        }
    7190       
    72         return IPC_GET_ARG1(answer);
     91        return IPC_GET_ARG1(answer);   
    7392}
    7493
     94/** Read from the device using its character interface.
     95 *
     96 * @param dev_phone phone to the device.
     97 * @param buf the output buffer for the data read from the device.
     98 * @param len the maximum length of the data to be read.
     99 *
     100 * @return non-negative number of bytes actually read from the device on success, negative error number otherwise.
     101 */
     102int read_dev(int dev_phone, void *buf, size_t len)
     103{       
     104        return rw_dev(dev_phone, buf, len, true);
     105}
     106
     107/** Write to the device using its character interface.
     108 *
     109 * @param dev_phone phone to the device.
     110 * @param buf the input buffer containg the data to be written to the device.
     111 * @param len the maximum length of the data to be written.
     112 *
     113 * @return non-negative number of bytes actually written to the device on success, negative error number otherwise.
     114 */
    75115int write_dev(int dev_phone, void *buf, size_t len)
    76116{
    77         // TODO
    78         return 0;
     117        return rw_dev(dev_phone, buf, len, false);
    79118}
    80119
  • uspace/lib/libdrv/generic/remote_char.c

    rba95e8f rca97cad  
    4545static void remote_char_write(device_t *dev, void *iface, ipc_callid_t callid, ipc_call_t *call);
    4646
     47/** Remote character interface operations.
     48 */
    4749static remote_iface_func_ptr_t remote_char_iface_ops [] = {
    4850        &remote_char_read,
     
    5052};
    5153 
     54/** Remote character interface structure.
     55 * Interface for processing request from remote clients addressed to the character interface.
     56 */
    5257remote_iface_t remote_char_iface = {
    5358        .method_count = sizeof(remote_char_iface_ops) / sizeof(remote_iface_func_ptr_t),
     
    5560};
    5661
     62/** Process the read request from the remote client.
     63 *
     64 * Receive the read request's parameters from the remote client and pass them to the local interface.
     65 * Return the result of the operation processed by the local interface to the remote client.
     66 *
     67 * @param dev the device from which the data are read.
     68 * @param iface the local interface structure.
     69 */
    5770static void remote_char_read(device_t *dev, void *iface, ipc_callid_t callid, ipc_call_t *call)
    5871{       
     
    8699        }
    87100       
     101        // the operation was successful, return the number of data read
    88102        async_data_read_finalize(cid, buf, ret);
    89103        ipc_answer_1(callid, EOK, ret);
    90104}
    91105
     106/** Process the write request from the remote client.
     107 *
     108 * Receive the write request's parameters from the remote client and pass them to the local interface.
     109 * Return the result of the operation processed by the local interface to the remote client.
     110 *
     111 * @param dev the device to which the data are written.
     112 * @param iface the local interface structure.
     113 */
    92114static void remote_char_write(device_t *dev, void *iface, ipc_callid_t callid, ipc_call_t *call)
    93115{
    94116        char_iface_t *char_iface = (char_iface_t *)iface;
     117        ipc_callid_t cid;
     118        size_t len;
     119       
     120        if (!async_data_write_receive(&cid, &len)) {
     121                // TODO handle protocol error
     122                ipc_answer_0(callid, EINVAL);
     123                return;
     124    }
     125       
    95126        if (!char_iface->write) {
     127                async_data_write_finalize(cid, NULL, 0);
    96128                ipc_answer_0(callid, ENOTSUP);
    97129                return;
    98130        }       
    99        
    100         size_t len;
    101         if (!async_data_write_receive(&callid, &len)) {
    102                 // TODO handle protocol error
    103                 return;
    104     }
    105131       
    106132        if (len > MAX_CHAR_RW_COUNT) {
     
    110136        char buf[MAX_CHAR_RW_COUNT];
    111137       
    112         async_data_write_finalize(callid, buf, len);
     138        async_data_write_finalize(cid, buf, len);
    113139       
    114140        int ret = (*char_iface->write)(dev, buf, len);
     
    116142                ipc_answer_0(callid, ret);
    117143        } else {
     144                // the operation was successful, return the number of data written
    118145                ipc_answer_1(callid, EOK, ret);
    119146        }
Note: See TracChangeset for help on using the changeset viewer.