replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / linux / SimpleClientServer / occlientbasicops.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 #ifndef OCCLIENT_BASICOPS_H_
22 #define OCCLIENT_BASICOPS_H_
23
24 #include "ocstack.h"
25
26 //-----------------------------------------------------------------------------
27 // Defines
28 //-----------------------------------------------------------------------------
29 #define TAG "occlientbasicops"
30 #define DEFAULT_CONTEXT_VALUE 0x99
31
32 //-----------------------------------------------------------------------------
33 // Typedefs
34 //-----------------------------------------------------------------------------
35
36 /**
37  * List of methods that can be inititated from the client
38  */
39 typedef enum {
40     TEST_DISCOVER_REQ = 1,
41     TEST_NON_CON_OP,
42     TEST_CON_OP,
43     MAX_TESTS
44 } CLIENT_TEST;
45
46 /**
47  * List of connectivity types that can be initiated from the client
48  * Required for user input validation
49  */
50 typedef enum {
51     CT_ADAPTER_DEFAULT = 0,
52     CT_IP,
53     MAX_CT
54 } CLIENT_CONNECTIVITY_TYPE;
55
56 //-----------------------------------------------------------------------------
57 //ResourceNode
58 //-----------------------------------------------------------------------------
59 struct ResourceNode
60 {
61     const char * sid;
62     const char * uri;
63     OCDevAddr endpoint;
64     ResourceNode * next;
65 };
66
67 //-----------------------------------------------------------------------------
68 // Function prototype
69 //-----------------------------------------------------------------------------
70
71 /* call getResult in common.cpp to get the result in string format. */
72 const char *getResult(OCStackResult result);
73
74 /* Performs GET/PUT/POST query on most recently discovered resource*/
75 void queryResource();
76
77 /* Parses JSON payload received in the clientResponse to extract sid and resource uri information.
78  * Populates uri_c array with uris of the resources discovered and assigns sid_c with the server
79  * id received in the clientResponse.
80  */
81 int parseJSON(unsigned  const char * resJSONPayload, char ** sid_c,
82               char *** uri_c, int * totalRes);
83
84 /*
85  * Collect unique resource(sid:uri), regardless of the transport it arrives on.
86  */
87 void collectUniqueResource(const OCClientResponse * clientResponse);
88
89 /*
90  * Insert the newly discovered unique resource(sid:uri) in the front of the resourceList
91  *
92  */
93
94 int insertResource(const char * sid, char const * uri,
95         const OCClientResponse * clientResponse);
96
97 /*
98  * Returns most recently discovered resource
99  */
100 const ResourceNode * getResource();
101
102 /*
103  * Frees the ResourceList
104  */
105 void freeResourceList();
106
107 /* Following are initialization functions for GET, PUT
108  * POST & Discovery operations
109  */
110 int InitPutRequest(OCQualityOfService qos);
111 int InitGetRequest(OCQualityOfService qos);
112 int InitPostRequest(OCQualityOfService qos);
113 int InitDiscovery();
114
115 /* Function to retrieve ip address, port no. of the server
116  *  and query for the operations to be performed.
117  */
118 void parseClientResponse(OCClientResponse * clientResponse);
119
120 /* This method calls OCDoResource() which in turn makes calls
121  * to the lower layers
122  */
123 OCStackResult InvokeOCDoResource(std::ostringstream &query,
124         OCMethod method, OCDevAddr *dest, OCQualityOfService qos,
125         OCClientResponseHandler cb, OCHeaderOption * options, uint8_t numOptions);
126
127 /*
128  * SIGINT handler: set gQuitFlag to 1 for graceful termination
129  */
130 void handleSigInt(int signum);
131
132 /*
133  * Printing helper functions
134  */
135 static void PrintUsage();
136 void printResourceList();
137
138
139
140 //-----------------------------------------------------------------------------
141 // Callback functions
142 //-----------------------------------------------------------------------------
143
144 /* Following are callback functions for the  GET, PUT
145  * POST & Discovery operations
146  */
147
148 OCStackApplicationResult putReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse);
149
150 OCStackApplicationResult postReqCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse);
151
152 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse);
153
154 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
155         OCClientResponse * clientResponse);
156 void StripNewLineChar(char* str);
157 #endif
158