1bd51a0f1872a60bccfac60d49405eeaea24f385
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / camessagehandler.h
1 /* *****************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 /**
22  * @file
23  * This file contains message functionality.
24  */
25
26 #ifndef CA_MESSAGE_HANDLER_H_
27 #define CA_MESSAGE_HANDLER_H_
28
29 #include "cacommon.h"
30 #include "coap.h"
31
32 #define CA_MEMORY_ALLOC_CHECK(arg) { if (NULL == arg) {OIC_LOG(ERROR, TAG, "Out of memory"); \
33 goto memory_error_exit;} }
34
35 typedef enum
36 {
37     SEND_TYPE_MULTICAST = 0,
38     SEND_TYPE_UNICAST
39 } CASendDataType_t;
40
41 typedef enum
42 {
43     CA_REQUEST_DATA = 1,
44     CA_RESPONSE_DATA = 2,
45     CA_ERROR_DATA = 3,
46 } CADataType_t;
47
48 typedef struct
49 {
50     CASendDataType_t type;
51     CAEndpoint_t *remoteEndpoint;
52     CARequestInfo_t *requestInfo;
53     CAResponseInfo_t *responseInfo;
54     CAErrorInfo_t *errorInfo;
55     CADataType_t dataType;
56 } CAData_t;
57
58 #ifdef __cplusplus
59 extern "C"
60 {
61 #endif
62
63 /**
64  * Detaches control from the caller for sending message.
65  * @param[in] endpoint    endpoint information where the data has to be sent.
66  * @param[in] sendMsg     message that needs to be sent.
67  * @param[in] dataType    type of the message(request/response).
68  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
69  */
70 CAResult_t CADetachSendMessage(const CAEndpoint_t *endpoint,
71                                const void *sendMsg,
72                                CADataType_t dataType);
73
74 /**
75  * Detaches control from the caller for sending request.
76  * @param[in] resourceUri    resource uri that needs to  be sent in the request.
77  * @param[in] token          token information of the request.
78  * @param[in] tokenLength    length of the token.
79  * @param[in] options        header options that need to be append in the request.
80  * @param[in] numOptions     number of options be appended.
81  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
82  */
83 CAResult_t CADetachMessageResourceUri(const CAURI_t resourceUri, const CAToken_t token,
84                                       uint8_t tokenLength, const CAHeaderOption_t *options,
85                                       uint8_t numOptions);
86
87 /**
88  * Setting the request and response callbacks for network packets.
89  * @param[in] ReqHandler      callback for receiving the requests.
90  * @param[in] RespHandler     callback for receiving the response.
91  * @param[in] ErrorHandler    callback for receiving error response.
92  */
93 void CASetInterfaceCallbacks(CARequestCallback ReqHandler, CAResponseCallback RespHandler,
94                              CAErrorCallback ErrorHandler);
95
96 /**
97  * Initialize the message handler by starting thread pool and initializing the
98  * send and receive queue.
99  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
100  */
101 CAResult_t CAInitializeMessageHandler();
102
103 /**
104  * Terminate the message handler by stopping  the thread pool and destroying the queues.
105  */
106 void CATerminateMessageHandler();
107
108 /**
109  * Handler for receiving request and response callback in single thread model.
110  */
111 void CAHandleRequestResponseCallbacks();
112
113 /**
114  * To log the PDU data.
115  * @param[in] pdu    pdu data.
116  * @param[in] endpoint  endpoint
117  */
118 void CALogPDUInfo(coap_pdu_t *pdu, const CAEndpoint_t *endpoint);
119
120 #ifdef WITH_BWT
121 /**
122  * Add the data to the send queue thread.
123  * @param[in] data    send data.
124  */
125 void CAAddDataToSendThread(CAData_t *data);
126
127 /**
128  * Add the data to the receive queue thread to notify received data.
129  * @param[in] data    received data.
130  */
131 void CAAddDataToReceiveThread(CAData_t *data);
132 #endif
133
134 #ifdef __cplusplus
135 } /* extern "C" */
136 #endif
137
138 #endif /* CA_MESSAGE_HANDLER_H_ */