1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
30 #include "occlientslow.h"
32 static int UNICAST_DISCOVERY = 0;
33 static int TEST_CASE = 0;
34 static const char * UNICAST_DISCOVERY_QUERY = "coap://%s:6298/oic/res";
35 static std::string putPayload = "{\"state\":\"off\",\"power\":10}";
36 static std::string coapServerIP = "255.255.255.255";
37 static std::string coapServerPort = "5683";
38 static std::string coapServerResource = "/a/led";
40 //The following variable determines the interface protocol (IP, etc)
41 //to be used for sending unicast messages. Default set to IP.
42 static OCConnectivityType OC_CONNTYPE = CT_ADAPTER_IP;
43 static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oic/res";
44 static int IPV4_ADDR_SIZE = 16;
45 void StripNewLineChar(char* str);
49 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
50 void handleSigInt(int signum)
58 static void PrintUsage()
60 OC_LOG(INFO, TAG, "Usage : occlient -c <0|1> -u <0|1> -t <1|2|3>");
61 OC_LOG(INFO, TAG, "-c <0|1> : IPv4/IPv6 (IPv6 not currently supported)");
62 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
63 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
64 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
65 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Confirmable Get Request");
68 OCStackResult InvokeOCDoResource(std::ostringstream &query,
69 OCMethod method, OCQualityOfService qos,
70 OCClientResponseHandler cb, OCHeaderOption * options, uint8_t numOptions)
73 OCCallbackData cbData;
76 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
79 ret = OCDoResource(NULL, method, query.str().c_str(), 0,
80 NULL, OC_CONNTYPE, qos, &cbData, options, numOptions);
82 if (ret != OC_STACK_OK)
84 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
90 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
92 if(clientResponse == NULL)
94 OC_LOG(INFO, TAG, "The clientResponse is NULL");
95 return OC_STACK_DELETE_TRANSACTION;
97 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
99 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
102 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
103 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
104 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response",
105 clientResponse->resJSONPayload);
107 if(clientResponse->rcvdVendorSpecificHeaderOptions &&
108 clientResponse->numRcvdVendorSpecificHeaderOptions)
110 OC_LOG (INFO, TAG, "Received vendor specific options");
112 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
113 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
115 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
117 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
118 ((OCHeaderOption)rcvdOptions[i]).optionID );
120 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
121 MAX_HEADER_OPTION_DATA_LENGTH);
125 return OC_STACK_DELETE_TRANSACTION;
128 // This is a function called back when a device is discovered
129 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
130 OCClientResponse * clientResponse)
132 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
134 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
139 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
142 "Device =============> Discovered %s @ %s:%d",
143 clientResponse->resJSONPayload, clientResponse->devAddr.addr, clientResponse->devAddr.port);
145 parseClientResponse(clientResponse);
149 case TEST_NON_CON_OP:
150 InitGetRequest(OC_LOW_QOS);
153 InitGetRequest(OC_HIGH_QOS);
161 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
165 int InitGetRequest(OCQualityOfService qos)
167 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
168 std::ostringstream query;
169 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
171 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)?
172 OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
178 OCCallbackData cbData;
179 /* Start a discovery query*/
180 char szQueryUri[64] = { 0 };
181 if (UNICAST_DISCOVERY)
183 char ipv4addr[IPV4_ADDR_SIZE];
184 printf("Enter IPv4 address of the Server hosting resource (Ex: 192.168.0.15)\n");
185 if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
187 //Strip newline char from ipv4addr
188 StripNewLineChar(ipv4addr);
189 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_DISCOVERY_QUERY, ipv4addr);
193 OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
194 return OC_STACK_INVALID_PARAM;
199 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
201 cbData.cb = discoveryReqCB;
202 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
204 if(UNICAST_DISCOVERY)
206 ret = OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_CONNTYPE,
207 OC_LOW_QOS, &cbData, NULL, 0);
211 ret = OCDoResource(NULL, OC_REST_DISCOVER, szQueryUri, 0, 0, CT_DEFAULT,
212 OC_LOW_QOS, &cbData, NULL, 0);
214 if (ret != OC_STACK_OK)
216 OC_LOG(ERROR, TAG, "OCStack resource error");
221 int main(int argc, char* argv[])
225 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
230 UNICAST_DISCOVERY = atoi(optarg);
233 TEST_CASE = atoi(optarg);
236 OC_CONNTYPE = CT_ADAPTER_IP;
244 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
245 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
251 /* Initialize OCStack*/
252 if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
254 OC_LOG(ERROR, TAG, "OCStack init error");
260 // Break from loop with Ctrl+C
261 OC_LOG(INFO, TAG, "Entering occlient main loop...");
262 signal(SIGINT, handleSigInt);
265 if (OCProcess() != OC_STACK_OK)
267 OC_LOG(ERROR, TAG, "OCStack process error");
273 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
275 if (OCStop() != OC_STACK_OK)
277 OC_LOG(ERROR, TAG, "OCStack stop error");
283 std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
288 void parseClientResponse(OCClientResponse * clientResponse)
290 coapServerIP = clientResponse->devAddr.addr;
291 coapServerPort = clientResponse->devAddr.port;
292 coapServerResource = getQueryStrForGetPut(clientResponse);