Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/libc/generic/async.c

    rf74392f r8aa42e3  
    11011101}
    11021102
    1103 /** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
    1104  *
    1105  * Ask through phone for a new connection to some service.
    1106  *
    1107  * @param phoneid       Phone handle used for contacting the other side.
    1108  * @param arg1          User defined argument.
    1109  * @param arg2          User defined argument.
    1110  * @param arg3          User defined argument.
    1111  *
    1112  * @return              New phone handle on success or a negative error code.
    1113  */
    1114 int
    1115 async_connect_me_to(int phoneid, ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3)
    1116 {
    1117         int rc;
    1118         ipcarg_t newphid;
    1119 
    1120         rc = async_req_3_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, NULL,
    1121             NULL, NULL, NULL, &newphid);
    1122        
    1123         if (rc != EOK) 
    1124                 return rc;
    1125 
    1126         return newphid;
    1127 }
    1128 
    1129 /** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
    1130  *
    1131  * Ask through phone for a new connection to some service and block until
    1132  * success.
    1133  *
    1134  * @param phoneid       Phone handle used for contacting the other side.
    1135  * @param arg1          User defined argument.
    1136  * @param arg2          User defined argument.
    1137  * @param arg3          User defined argument.
    1138  *
    1139  * @return              New phone handle on success or a negative error code.
    1140  */
    1141 int
    1142 async_connect_me_to_blocking(int phoneid, ipcarg_t arg1, ipcarg_t arg2,
    1143     ipcarg_t arg3)
    1144 {
    1145         int rc;
    1146         ipcarg_t newphid;
    1147 
    1148         rc = async_req_4_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
    1149             IPC_FLAG_BLOCKING, NULL, NULL, NULL, NULL, &newphid);
    1150        
    1151         if (rc != EOK) 
    1152                 return rc;
    1153 
    1154         return newphid;
    1155 }
    1156 
    11571103/** Wrapper for making IPC_M_SHARE_IN calls using the async framework.
    11581104 *
     
    13411287}
    13421288
    1343 /** Wrapper for forwarding any read request
    1344  *
    1345  *
    1346  */
    1347 int async_data_read_forward_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
    1348     ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipc_call_t *dataptr)
    1349 {
    1350         ipc_callid_t callid;
    1351         if (!async_data_read_receive(&callid, NULL)) {
    1352                 ipc_answer_0(callid, EINVAL);
    1353                 return EINVAL;
    1354         }
    1355        
    1356         aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
    1357             dataptr);
    1358         if (msg == 0) {
    1359                 ipc_answer_0(callid, EINVAL);
    1360                 return EINVAL;
    1361         }
    1362        
    1363         int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
    1364             IPC_FF_ROUTE_FROM_ME);
    1365         if (retval != EOK) {
    1366                 async_wait_for(msg, NULL);
    1367                 ipc_answer_0(callid, retval);
    1368                 return retval;
    1369         }
    1370        
    1371         ipcarg_t rc;
    1372         async_wait_for(msg, &rc);
    1373        
    1374         return (int) rc;
    1375 }
    1376 
    13771289/** Wrapper for making IPC_M_DATA_WRITE calls using the async framework.
    13781290 *
    1379  * @param phoneid Phone that will be used to contact the receiving side.
    1380  * @param src     Address of the beginning of the source buffer.
    1381  * @param size    Size of the source buffer.
    1382  *
    1383  * @return Zero on success or a negative error code from errno.h.
    1384  *
     1291 * @param phoneid       Phone that will be used to contact the receiving side.
     1292 * @param src           Address of the beginning of the source buffer.
     1293 * @param size          Size of the source buffer.
     1294 *
     1295 * @return              Zero on success or a negative error code from errno.h.
    13851296 */
    13861297int async_data_write_start(int phoneid, const void *src, size_t size)
     
    13971308 * So far, this wrapper is to be used from within a connection fibril.
    13981309 *
    1399  * @param callid Storage where the hash of the IPC_M_DATA_WRITE call will
    1400  *               be stored.
    1401  * @param size   Storage where the suggested size will be stored. May be
    1402  *               NULL
    1403  *
    1404  * @return Non-zero on success, zero on failure.
    1405  *
     1310 * @param callid        Storage where the hash of the IPC_M_DATA_WRITE call will
     1311 *                      be stored.
     1312 * @param size          Storage where the suggested size will be stored. May be
     1313 *                      NULL
     1314 *
     1315 * @return              Non-zero on success, zero on failure.
    14061316 */
    14071317int async_data_write_receive(ipc_callid_t *callid, size_t *size)
     
    14101320       
    14111321        assert(callid);
    1412        
     1322
    14131323        *callid = async_get_call(&data);
    14141324        if (IPC_GET_METHOD(data) != IPC_M_DATA_WRITE)
    14151325                return 0;
    1416        
    14171326        if (size)
    14181327                *size = (size_t) IPC_GET_ARG2(data);
    1419        
    14201328        return 1;
    14211329}
     
    14261334 * so that the user doesn't have to remember the meaning of each IPC argument.
    14271335 *
    1428  * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
    1429  * @param dst    Final destination address for the IPC_M_DATA_WRITE call.
    1430  * @param size   Final size for the IPC_M_DATA_WRITE call.
     1336 * @param callid        Hash of the IPC_M_DATA_WRITE call to answer.
     1337 * @param dst           Final destination address for the IPC_M_DATA_WRITE call.
     1338 * @param size          Final size for the IPC_M_DATA_WRITE call.
     1339 *
     1340 * @return              Zero on success or a value from @ref errno.h on failure.
     1341 */
     1342int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
     1343{
     1344        return ipc_data_write_finalize(callid, dst, size);
     1345}
     1346
     1347/** Wrapper for receiving blobs via the async_data_write_*
     1348 *
     1349 * This wrapper only makes it more comfortable to use async_data_write_*
     1350 * functions to receive blobs.
     1351 *
     1352 * @param blob     Pointer to data pointer (which should be later disposed
     1353 *                 by free()). If the operation fails, the pointer is not
     1354 *                 touched.
     1355 * @param max_size Maximum size (in bytes) of the blob to receive. 0 means
     1356 *                 no limit.
     1357 * @param received If not NULL, the size of the received data is stored here.
    14311358 *
    14321359 * @return Zero on success or a value from @ref errno.h on failure.
    14331360 *
    14341361 */
    1435 int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
    1436 {
    1437         return ipc_data_write_finalize(callid, dst, size);
    1438 }
    1439 
    1440 /** Wrapper for receiving binary data or strings
    1441  *
    1442  * This wrapper only makes it more comfortable to use async_data_write_*
    1443  * functions to receive binary data or strings.
    1444  *
    1445  * @param data       Pointer to data pointer (which should be later disposed
    1446  *                   by free()). If the operation fails, the pointer is not
    1447  *                   touched.
    1448  * @param nullterm   If true then the received data is always zero terminated.
    1449  *                   This also causes to allocate one extra byte beyond the
    1450  *                   raw transmitted data.
    1451  * @param min_size   Minimum size (in bytes) of the data to receive.
    1452  * @param max_size   Maximum size (in bytes) of the data to receive. 0 means
    1453  *                   no limit.
    1454  * @param granulariy If non-zero then the size of the received data has to
    1455  *                   be divisible by this value.
    1456  * @param received   If not NULL, the size of the received data is stored here.
    1457  *
    1458  * @return Zero on success or a value from @ref errno.h on failure.
    1459  *
    1460  */
    1461 int async_data_write_accept(void **data, const bool nullterm,
    1462     const size_t min_size, const size_t max_size, const size_t granularity,
    1463     size_t *received)
     1362int async_data_blob_receive(char **blob, const size_t max_size, size_t *received)
    14641363{
    14651364        ipc_callid_t callid;
     
    14701369        }
    14711370       
    1472         if (size < min_size) {
    1473                 ipc_answer_0(callid, EINVAL);
    1474                 return EINVAL;
    1475         }
    1476        
    14771371        if ((max_size > 0) && (size > max_size)) {
    14781372                ipc_answer_0(callid, EINVAL);
     
    14801374        }
    14811375       
    1482         if ((granularity > 0) && ((size % granularity) != 0)) {
     1376        char *data = (char *) malloc(size);
     1377        if (data == NULL) {
     1378                ipc_answer_0(callid, ENOMEM);
     1379                return ENOMEM;
     1380        }
     1381       
     1382        int rc = async_data_write_finalize(callid, data, size);
     1383        if (rc != EOK) {
     1384                free(data);
     1385                return rc;
     1386        }
     1387       
     1388        *blob = data;
     1389        if (received != NULL)
     1390                *received = size;
     1391       
     1392        return EOK;
     1393}
     1394
     1395/** Wrapper for receiving strings via the async_data_write_*
     1396 *
     1397 * This wrapper only makes it more comfortable to use async_data_write_*
     1398 * functions to receive strings.
     1399 *
     1400 * @param str      Pointer to string pointer (which should be later disposed
     1401 *                 by free()). If the operation fails, the pointer is not
     1402 *                 touched.
     1403 * @param max_size Maximum size (in bytes) of the string to receive. 0 means
     1404 *                 no limit.
     1405 *
     1406 * @return Zero on success or a value from @ref errno.h on failure.
     1407 *
     1408 */
     1409int async_data_string_receive(char **str, const size_t max_size)
     1410{
     1411        ipc_callid_t callid;
     1412        size_t size;
     1413        if (!async_data_write_receive(&callid, &size)) {
    14831414                ipc_answer_0(callid, EINVAL);
    14841415                return EINVAL;
    14851416        }
    14861417       
    1487         void *_data;
    1488        
    1489         if (nullterm)
    1490                 _data = malloc(size + 1);
    1491         else
    1492                 _data = malloc(size);
    1493        
    1494         if (_data == NULL) {
     1418        if ((max_size > 0) && (size > max_size)) {
     1419                ipc_answer_0(callid, EINVAL);
     1420                return EINVAL;
     1421        }
     1422       
     1423        char *data = (char *) malloc(size + 1);
     1424        if (data == NULL) {
    14951425                ipc_answer_0(callid, ENOMEM);
    14961426                return ENOMEM;
    14971427        }
    14981428       
    1499         int rc = async_data_write_finalize(callid, _data, size);
     1429        int rc = async_data_write_finalize(callid, data, size);
    15001430        if (rc != EOK) {
    1501                 free(_data);
     1431                free(data);
    15021432                return rc;
    15031433        }
    15041434       
    1505         if (nullterm)
    1506                 ((char *) _data)[size] = 0;
    1507        
    1508         *data = _data;
    1509         if (received != NULL)
    1510                 *received = size;
    1511        
     1435        data[size] = 0;
     1436        *str = data;
    15121437        return EOK;
    15131438}
    15141439
    1515 /** Wrapper for voiding any data that is about to be received
    1516  *
    1517  * This wrapper can be used to void any pending data
    1518  *
    1519  * @param retval Error value from @ref errno.h to be returned to the caller.
    1520  *
    1521  */
    1522 void async_data_write_void(const int retval)
    1523 {
    1524         ipc_callid_t callid;
    1525         async_data_write_receive(&callid, NULL);
    1526         ipc_answer_0(callid, retval);
    1527 }
    1528 
    1529 /** Wrapper for forwarding any data that is about to be received
    1530  *
    1531  *
    1532  */
    1533 int async_data_write_forward_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
    1534     ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipc_call_t *dataptr)
    1535 {
    1536         ipc_callid_t callid;
    1537         if (!async_data_write_receive(&callid, NULL)) {
    1538                 ipc_answer_0(callid, EINVAL);
    1539                 return EINVAL;
    1540         }
    1541        
    1542         aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
    1543             dataptr);
    1544         if (msg == 0) {
    1545                 ipc_answer_0(callid, EINVAL);
    1546                 return EINVAL;
    1547         }
    1548        
    1549         int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
    1550             IPC_FF_ROUTE_FROM_ME);
    1551         if (retval != EOK) {
    1552                 async_wait_for(msg, NULL);
    1553                 ipc_answer_0(callid, retval);
    1554                 return retval;
    1555         }
    1556        
    1557         ipcarg_t rc;
    1558         async_wait_for(msg, &rc);
    1559        
    1560         return (int) rc;
    1561 }
    1562 
    15631440/** @}
    15641441 */
Note: See TracChangeset for help on using the changeset viewer.