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