aboutsummaryrefslogtreecommitdiffstats
path: root/ipc_unit_test/ipc_unit_test_server.c
blob: f627dcae9e5f00ff996cd8ee7571d1658377d6b7 (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
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highli
/*
 * Copyright (c) 2021, Nippon Seiki Co., Ltd.
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>

#include <ipc.h>

#include "ipc_unit_test_common.h"

static void helpPrint(void);
static void helpWritePrint(void);
static void listPrint(void);
static void writeData(void);
static void writeDataToStruct(int id, long value);

IPC_DATA_IC_SERVICE_S g_dataIcService;

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

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

    ret = ipcServerStart(IPC_USAGE_TYPE_IC_SERVICE);
    if (ret != IPC_RET_OK) {
        printf("ipcServerStart 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 'l':
            listPrint();
            break;
        case 'w':
            writeData();
            break;
        case 's':
            ret = ipcSendMessage(IPC_USAGE_TYPE_IC_SERVICE, (const void*)&g_dataIcService, sizeof(g_dataIcService));
            printf("ipcSendMessage return:%d\n", ret);
            break;
        default:
            break;
        }
    }

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

    return 0;
}

static void helpPrint(void)
{
    printf("\n-----------\n");
    printf("'h' : help\n");
    printf("'q' : quit\n");
    printf("'l' : list of Server data\n");
    printf("'w' : write to Server data\n");
    printf("'s' : send data to Client\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;
        }
    }
}

static void writeData(void)
{
    char command[40];
    char dummy[40];
    bool isRunning = true;
    int i;

    int id;
    long value;

    char *pRetStr;

    while(isRunning) {
        printf("write command (h=help q=goto main menu):");
        pRetStr = fgets(command, 40, stdin);
        if (pRetStr == NULL) {
            continue;
        }
        memcpy(dummy, command, 40);

        while(strlen(dummy) == 39 && dummy[38] != '\n') {
            pRetStr = fgets(dummy, 40, stdin);
            if (pRetStr == NULL) {
                break;
            }
        }

        command[strlen(command)-1] = '\0';
        switch(command[0]) {
        case 'h':
            helpWritePrint();
            break;
        case 'q':
            isRunning = false;
            break;
        case 'l':
            listPrint();
            break;
        default:
            // write data
            id = (int)strtol(command, NULL, 0);
            value = 0;
            for (i = 0; i < strlen(command); i++) {
                if (command[i] == ' ') {
                    value = (int)strtol(&command[i], NULL, 0);
                }
            }
            writeDataToStruct(id, value);
        }
    }
}

static void helpWritePrint(void)
{
    printf("\n-----------\n");
    printf("'h' : help\n");
    printf("'q' : goto main menu\n");
    printf("'l' : list of Server data\n");
    printf("<ID> <value>: write data\n");
    printf("  ex)\n");
    printf("    write command 2 4\n");
    printf("      -> 2: brake = 4\n");
    printf("-----------\n\n");
}

static void writeDataToStruct(int id, long value)
{
    void *pValue;

    if (id < 0 || IC_SERVICE_LIST_NUM < id) {
        return;
    }

    pValue = (void *)&g_dataIcService + IcServiceList[id].offset;
    memcpy(pValue, (void *)&value, IcServiceList[id].size);
}