1f1549183Sopenharmony_ci/*
2f1549183Sopenharmony_ci * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3f1549183Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f1549183Sopenharmony_ci * you may not use this file except in compliance with the License.
5f1549183Sopenharmony_ci * You may obtain a copy of the License at
6f1549183Sopenharmony_ci *
7f1549183Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f1549183Sopenharmony_ci *
9f1549183Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f1549183Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f1549183Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f1549183Sopenharmony_ci * See the License for the specific language governing permissions and
13f1549183Sopenharmony_ci * limitations under the License.
14f1549183Sopenharmony_ci */
15f1549183Sopenharmony_ci
16f1549183Sopenharmony_ci#ifndef CRPC_CLIENT_H
17f1549183Sopenharmony_ci#define CRPC_CLIENT_H
18f1549183Sopenharmony_ci
19f1549183Sopenharmony_ci#include <pthread.h>
20f1549183Sopenharmony_ci#include "context.h"
21f1549183Sopenharmony_ci
22f1549183Sopenharmony_ci#ifdef __cplusplus
23f1549183Sopenharmony_ciextern "C" {
24f1549183Sopenharmony_ci#endif
25f1549183Sopenharmony_ci
26f1549183Sopenharmony_citypedef struct RpcClient RpcClient;
27f1549183Sopenharmony_ci
28f1549183Sopenharmony_ci/*
29f1549183Sopenharmony_ci * RPC CLIENT
30f1549183Sopenharmony_ci * RPC client sends a request and wait for a response from the server,
31f1549183Sopenharmony_ci * and process the callback function initiated by the server.
32f1549183Sopenharmony_ci * so we start a thread to get the server's reply message, judge message type,
33f1549183Sopenharmony_ci * and to deal reply or callback.
34f1549183Sopenharmony_ci * the client may process like this:
35f1549183Sopenharmony_ci * 1. thread: poll read ---> server reply msg ---> notify RemoteCall
36f1549183Sopenharmony_ci *    client: request functions --->RemoteCall ---> Wait Reply ---> Get Return
37f1549183Sopenharmony_ci * 2. thread: poll read ---> server callback msg ---> OnTransact
38f1549183Sopenharmony_ci *    client: OnTransact ---> deal event callback functions
39f1549183Sopenharmony_ci */
40f1549183Sopenharmony_ci
41f1549183Sopenharmony_cistruct RpcClient {
42f1549183Sopenharmony_ci    Context *context;
43f1549183Sopenharmony_ci    int threadRunFlag;
44f1549183Sopenharmony_ci    pthread_t threadId;
45f1549183Sopenharmony_ci    int waitReply;
46f1549183Sopenharmony_ci    pthread_mutex_t mutex;
47f1549183Sopenharmony_ci    pthread_cond_t condW;
48f1549183Sopenharmony_ci    int callLockFlag;
49f1549183Sopenharmony_ci    pthread_mutex_t lockMutex;
50f1549183Sopenharmony_ci    pthread_cond_t lockCond;
51f1549183Sopenharmony_ci};
52f1549183Sopenharmony_ci
53f1549183Sopenharmony_ci/**
54f1549183Sopenharmony_ci * @Description Create a Rpc Client object
55f1549183Sopenharmony_ci *
56f1549183Sopenharmony_ci * @param path - Unix domain communication file
57f1549183Sopenharmony_ci * @return RpcClient* - return client pointer or NULL
58f1549183Sopenharmony_ci */
59f1549183Sopenharmony_ciRpcClient *CreateRpcClient(const char *path);
60f1549183Sopenharmony_ci
61f1549183Sopenharmony_ci/**
62f1549183Sopenharmony_ci * @Description Release RpcClient object
63f1549183Sopenharmony_ci *
64f1549183Sopenharmony_ci * @param client - RpcClient object's pointer
65f1549183Sopenharmony_ci */
66f1549183Sopenharmony_civoid ReleaseRpcClient(RpcClient *client);
67f1549183Sopenharmony_ci
68f1549183Sopenharmony_ci/**
69f1549183Sopenharmony_ci * @Description Lock the RPC Client
70f1549183Sopenharmony_ci *
71f1549183Sopenharmony_ci * @param client - RpcClient object's pointer
72f1549183Sopenharmony_ci */
73f1549183Sopenharmony_civoid LockRpcClient(RpcClient *client);
74f1549183Sopenharmony_ci
75f1549183Sopenharmony_ci/**
76f1549183Sopenharmony_ci * @Description Unlock the RPC Client
77f1549183Sopenharmony_ci *
78f1549183Sopenharmony_ci * @param client - RpcClient object's pointer
79f1549183Sopenharmony_ci */
80f1549183Sopenharmony_civoid UnlockRpcClient(RpcClient *client);
81f1549183Sopenharmony_ci
82f1549183Sopenharmony_ci/**
83f1549183Sopenharmony_ci * @Description Send request message to server and wait response message
84f1549183Sopenharmony_ci *
85f1549183Sopenharmony_ci * @param client - RpcClient object's pointer
86f1549183Sopenharmony_ci * @return int 0 success, other is failed
87f1549183Sopenharmony_ci */
88f1549183Sopenharmony_ciint RemoteCall(RpcClient *client);
89f1549183Sopenharmony_ci
90f1549183Sopenharmony_ci/**
91f1549183Sopenharmony_ci * @Description End dealing response message
92f1549183Sopenharmony_ci *
93f1549183Sopenharmony_ci * @param client - RpcClient object's pointer
94f1549183Sopenharmony_ci */
95f1549183Sopenharmony_civoid ReadClientEnd(RpcClient *client);
96f1549183Sopenharmony_ci
97f1549183Sopenharmony_ci/**
98f1549183Sopenharmony_ci * @Description Deal callback messages. This function must be implemented when the RPC client is implemented
99f1549183Sopenharmony_ci *
100f1549183Sopenharmony_ci * @param context - pointer to the rpc context
101f1549183Sopenharmony_ci * @return int - 0 success; -1 deal message failed
102f1549183Sopenharmony_ci */
103f1549183Sopenharmony_ciint OnTransact(Context *context);
104f1549183Sopenharmony_ci
105f1549183Sopenharmony_ci#ifdef __cplusplus
106f1549183Sopenharmony_ci}
107f1549183Sopenharmony_ci#endif
108f1549183Sopenharmony_ci#endif
109