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