| 1 | /*
|
|---|
| 2 | * Copyright (c) 2012 Maurizio Lombardi
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | #include <stdio.h>
|
|---|
| 31 | #include <devman.h>
|
|---|
| 32 | #include <device/clock_dev.h>
|
|---|
| 33 | #include <errno.h>
|
|---|
| 34 | #include <loc.h>
|
|---|
| 35 | #include <time.h>
|
|---|
| 36 |
|
|---|
| 37 | #define NAME "date"
|
|---|
| 38 |
|
|---|
| 39 | int
|
|---|
| 40 | main(int argc, char **argv)
|
|---|
| 41 | {
|
|---|
| 42 | int rc;
|
|---|
| 43 | category_id_t cat_id;
|
|---|
| 44 | size_t svc_cnt;
|
|---|
| 45 | service_id_t *svc_ids = NULL;
|
|---|
| 46 | char *svc_name = NULL;
|
|---|
| 47 | char *devpath;
|
|---|
| 48 | devman_handle_t devh;
|
|---|
| 49 | struct tm t;
|
|---|
| 50 |
|
|---|
| 51 | /* Get the id of the clock category */
|
|---|
| 52 | rc = loc_category_get_id("clock", &cat_id, IPC_FLAG_BLOCKING);
|
|---|
| 53 | if (rc != EOK) {
|
|---|
| 54 | printf(NAME ": Cannot get clock category id\n");
|
|---|
| 55 | goto exit;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | /* Get the list of available services in the clock category */
|
|---|
| 59 | rc = loc_category_get_svcs(cat_id, &svc_ids, &svc_cnt);
|
|---|
| 60 | if (rc != EOK) {
|
|---|
| 61 | printf(NAME ": Cannot get the list of services in category \
|
|---|
| 62 | clock\n");
|
|---|
| 63 | goto exit;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /* Check if the are available services in the clock category */
|
|---|
| 67 | if (svc_cnt == 0) {
|
|---|
| 68 | printf(NAME ": No available service found in \
|
|---|
| 69 | the clock category\n");
|
|---|
| 70 | goto exit;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /* Get the name of the clock service */
|
|---|
| 74 | rc = loc_service_get_name(svc_ids[0], &svc_name);
|
|---|
| 75 | if (rc != EOK) {
|
|---|
| 76 | printf(NAME ": Cannot get the name of the service\n");
|
|---|
| 77 | goto exit;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | const char delim = '/';
|
|---|
| 81 | devpath = str_chr(svc_name, delim);
|
|---|
| 82 |
|
|---|
| 83 | if (!devpath) {
|
|---|
| 84 | printf(NAME ": Device name format not recognized\n");
|
|---|
| 85 | goto exit;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /* Skip the delimiter */
|
|---|
| 89 | devpath++;
|
|---|
| 90 |
|
|---|
| 91 | printf("Found device %s\n", devpath);
|
|---|
| 92 |
|
|---|
| 93 | /* Get the device's handle */
|
|---|
| 94 | rc = devman_fun_get_handle("/hw/pci0/00:01.0/cmos-rtc/a", &devh, IPC_FLAG_BLOCKING);
|
|---|
| 95 | if (rc != EOK) {
|
|---|
| 96 | printf(NAME ": Cannot open the device\n");
|
|---|
| 97 | goto exit;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | printf("OPEN!\n");
|
|---|
| 101 |
|
|---|
| 102 | /* Now connect to the device */
|
|---|
| 103 | async_sess_t *sess = devman_device_connect(EXCHANGE_SERIALIZE,
|
|---|
| 104 | devh, IPC_FLAG_BLOCKING);
|
|---|
| 105 | if (!sess) {
|
|---|
| 106 | printf(NAME ": Cannot connect to the device\n");
|
|---|
| 107 | goto exit;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | printf("CONNECTED!\n");
|
|---|
| 111 |
|
|---|
| 112 | /* Read the current date */
|
|---|
| 113 | rc = clock_dev_time_get(sess, &t);
|
|---|
| 114 | if (rc != EOK) {
|
|---|
| 115 | printf(NAME ": Cannot read the current time\n");
|
|---|
| 116 | goto exit;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | printf("SUCCESS!\n");
|
|---|
| 120 | fflush(stdout);
|
|---|
| 121 |
|
|---|
| 122 | printf("%d:%d:%d\n", t.tm_hour, t.tm_min, t.tm_sec);
|
|---|
| 123 |
|
|---|
| 124 | exit:
|
|---|
| 125 | free(svc_name);
|
|---|
| 126 | free(svc_ids);
|
|---|
| 127 | return rc;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|