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 * TEST_APP_UNICAST_DISCOVERY_QUERY = "coap://0.0.0.0:5683/oc/core";
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";
42 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
43 void handleSigInt(int signum)
51 static void PrintUsage()
53 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3>");
54 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
55 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
56 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
57 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Confirmable Get Request");
60 OCStackResult InvokeOCDoResource(std::ostringstream &query,
61 OCMethod method, OCQualityOfService qos,
62 OCClientResponseHandler cb, OCHeaderOption * options, uint8_t numOptions)
65 OCCallbackData cbData;
69 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
73 // TODO-CA: The adapter type is set to WiFi but should be configurable - add as API param
74 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
75 NULL, OC_WIFI, qos, &cbData, options, numOptions);
77 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
78 NULL, qos, &cbData, options, numOptions);
81 if (ret != OC_STACK_OK)
83 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
89 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
91 if(clientResponse == NULL)
93 OC_LOG(INFO, TAG, "The clientResponse is NULL");
94 return OC_STACK_DELETE_TRANSACTION;
96 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
98 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
101 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
102 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
103 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response",
104 clientResponse->resJSONPayload);
106 if(clientResponse->rcvdVendorSpecificHeaderOptions &&
107 clientResponse->numRcvdVendorSpecificHeaderOptions)
109 OC_LOG (INFO, TAG, "Received vendor specific options");
111 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
112 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
114 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
116 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
117 ((OCHeaderOption)rcvdOptions[i]).optionID );
118 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
119 ((OCHeaderOption)rcvdOptions[i]).optionLength);
123 return OC_STACK_DELETE_TRANSACTION;
126 // This is a function called back when a device is discovered
127 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
128 OCClientResponse * clientResponse)
130 uint8_t remoteIpAddr[4];
131 uint16_t remotePortNu;
133 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
135 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
140 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
142 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
143 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
144 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
147 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
148 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
149 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
151 parseClientResponse(clientResponse);
155 case TEST_NON_CON_OP:
156 InitGetRequest(OC_LOW_QOS);
159 InitGetRequest(OC_HIGH_QOS);
167 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
171 int InitGetRequest(OCQualityOfService qos)
173 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
174 std::ostringstream query;
175 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
177 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)?
178 OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
184 OCCallbackData cbData;
186 /* Start a discovery query*/
187 char szQueryUri[64] = { 0 };
188 if (UNICAST_DISCOVERY)
190 strcpy(szQueryUri, TEST_APP_UNICAST_DISCOVERY_QUERY);
194 strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
196 cbData.cb = discoveryReqCB;
197 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
200 // TODO-CA: The adapter type is set to all but should be configurable - add as API param
201 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, (OC_ETHERNET | OC_WIFI),
202 OC_LOW_QOS, &cbData, NULL, 0);
204 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0);
206 if (ret != OC_STACK_OK)
208 OC_LOG(ERROR, TAG, "OCStack resource error");
213 int main(int argc, char* argv[])
215 uint8_t addr[20] = {0};
216 uint8_t* paddr = NULL;
217 uint16_t port = USE_RANDOM_PORT;
218 uint8_t ifname[] = "eth0";
221 while ((opt = getopt(argc, argv, "u:t:")) != -1)
226 UNICAST_DISCOVERY = atoi(optarg);
229 TEST_CASE = atoi(optarg);
237 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
238 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
245 /*Get Ip address on defined interface and initialize coap on it with random port number
246 * this port number will be used as a source port in all coap communications*/
247 if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
248 sizeof(addr)) == ERR_SUCCESS)
250 OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
254 /* Initialize OCStack*/
255 if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK)
257 OC_LOG(ERROR, TAG, "OCStack init error");
263 // Break from loop with Ctrl+C
264 OC_LOG(INFO, TAG, "Entering occlient main loop...");
265 signal(SIGINT, handleSigInt);
268 if (OCProcess() != OC_STACK_OK)
270 OC_LOG(ERROR, TAG, "OCStack process error");
276 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
278 if (OCStop() != OC_STACK_OK)
280 OC_LOG(ERROR, TAG, "OCStack stop error");
286 std::string getIPAddrTBServer(OCClientResponse * clientResponse)
288 if(!clientResponse) return "";
289 if(!clientResponse->addr) return "";
290 uint8_t a, b, c, d = 0;
291 if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return "";
293 char ipaddr[16] = {'\0'};
294 // ostringstream not working correctly here, hence snprintf
295 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d);
296 return std::string (ipaddr);
300 std::string getPortTBServer(OCClientResponse * clientResponse)
302 if(!clientResponse) return "";
303 if(!clientResponse->addr) return "";
305 if(0 != OCDevAddrToPort(clientResponse->addr, &p) ) return "";
306 std::ostringstream ss;
311 std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
316 void parseClientResponse(OCClientResponse * clientResponse)
318 coapServerIP = getIPAddrTBServer(clientResponse);
319 coapServerPort = getPortTBServer(clientResponse);
320 coapServerResource = getQueryStrForGetPut(clientResponse);