summaryrefslogtreecommitdiffstats
path: root/ipc_unit_test/ipc_unit_test_client.c
blob: 4a49b223448d49cb843b0ee66323d58e2fb5086d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <ipc.h>

#include "ipc_unit_test_common.h"

IPC_DATA_IC_SERVICE_S g_dataIcService;

static void changeNotifyCb(void* pData, signed int size, int kind);
static void helpPrint(void);
static void listPrint(void);

int main(void)
{
    IPC_RET_E ret;
    char command[10];
    char dummy[10];
    bool isRunning = true;
    signed int size;
    char *pRetStr;

    memset(&g_dataIcService, 0, sizeof(g_dataIcService));

    ret = ipcClientStart(IPC_USAGE_TYPE_IC_SERVICE);
    if (ret != IPC_RET_OK) {
        printf("ipcClientStart Error:%d\n", ret);
        goto end;
    }

    ret = ipcRegisterCallback(IPC_USAGE_TYPE_IC_SERVICE, changeNotifyCb);
    if (ret != IPC_RET_OK) {
        printf("ipcRegisterCallback Error:%d\n", ret);
        goto end;
    }

    while(isRunning) {
        printf("command (h=help, q=quit):");
        pRetStr = fgets(command, 10, stdin);
        if (pRetStr == NULL) {
            continue;
        }
        memcpy(dummy, command, 10);

        while(strlen(dummy) == 9 && dummy[8] != '\n') {
            pRetStr = fgets(dummy, 10, stdin);
            if (pRetStr == NULL) {
                break;
            }
        }

        command[strlen(command)-1] = '\0';

        switch(command[0]) {
        case 'h':
            helpPrint();
            break;
        case 'q':
            isRunning = false;
            break;
        case 'r':
            size = sizeof(g_dataIcService);
            ret = ipcReadDataPool(IPC_USAGE_TYPE_IC_SERVICE, &g_dataIcService, &size);
            printf("ipcReadDataPool return:%d\n", ret);
            listPrint();
            break;
        default:
            break;
        }
    }

end:
    ipcClientStop(IPC_USAGE_TYPE_IC_SERVICE);
    printf("bye...\n");
    sleep(1);

    return 0;
}

static void changeNotifyCb(void* pData, signed int size, int kind)
{
    signed long longVal;
    signed int intVal;
    signed short shortVal;
    signed char charVal;

    printf("Enter %s\n", __func__);

    switch(size) {
    case 8:
        longVal = *((signed long*)pData);
        printf("kind = %d, size = %d, data=%ld\n", kind, size, longVal);
        break;
    case 4:
        intVal = *((signed int*)pData);
        printf("kind = %d, size = %d, data=%d\n", kind, size, intVal);
        break;
    case 2:
        shortVal = *((signed short *)pData);
        printf("kind = %d, size = %d, data=%d\n", kind, size, shortVal);
        break;
    default:
        charVal = *((signed char *)pData);
        printf("kind = %d, size = %d, data=%d\n", kind, size, charVal);
        break;
    }
    printf("Leave %s\n", __func__);
    return;
}

static void helpPrint(void)
{
    printf("\n-----------\n");
    printf("'h' : help\n");
    printf("'q' : quit\n");
    printf("'r' : read from Client data pool\n");
    printf("-----------\n\n");
}

static void listPrint(void)
{
    int i;
    signed long longVal;
    signed int intVal;
    signed short shortVal;
    signed char charVal;
    void *pValue;

    for (i = 0; i < IC_SERVICE_LIST_NUM; i++) {
        pValue = (void *)&g_dataIcService + IcServiceList[i].offset;
        switch(IcServiceList[i].size) {
        case 8:
            longVal = *((signed long*)pValue);
            printf("%3d: %s(%d) = %ld\n", i, IcServiceList[i].name, IcServiceList[i].size, longVal);
            break;
        case 4:
            intVal = *((signed int*)pValue);
            printf("%3d: %s(%d) = %d\n", i, IcServiceList[i].name, IcServiceList[i].size, intVal);
            break;
        case 2:
            shortVal = *((signed short *)pValue);
            printf("%3d: %s(%d) = %d\n", i, IcServiceList[i].name, IcServiceList[i].size, shortVal);
            break;
        default:
            charVal = *((signed char *)pValue);
            printf("%3d: %s(%d) = %d\n", i, IcServiceList[i].name, IcServiceList[i].size, charVal);
            break;
        }
    }
}