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";
41 //The following variable determines the interface (wifi, ethernet etc.)
42 //to be used for sending unicast messages. Default set to WIFI.
43 static OCConnectivityType CA_CONNTYPE = OC_WIFI;
44 static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oc/core";
49 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
50 void handleSigInt(int signum)
58 static void PrintUsage()
61 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3> -c <0|1>");
62 OC_LOG(INFO, TAG, "-c <0|1> : Send unicast messages over Ethernet or WIFI");
64 OC_LOG(INFO, TAG, "Usage : occlient -u <0|1> -t <1|2|3>");
66 OC_LOG(INFO, TAG, "-u <0|1> : Perform multicast/unicast discovery of resources");
67 OC_LOG(INFO, TAG, "-t 1 : Discover Resources");
68 OC_LOG(INFO, TAG, "-t 2 : Discover Resources and Initiate Nonconfirmable Get Request");
69 OC_LOG(INFO, TAG, "-t 3 : Discover Resources and Initiate Confirmable Get Request");
72 OCStackResult InvokeOCDoResource(std::ostringstream &query,
73 OCMethod method, OCQualityOfService qos,
74 OCClientResponseHandler cb, OCHeaderOption * options, uint8_t numOptions)
77 OCCallbackData cbData;
81 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
85 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
86 NULL, CA_CONNTYPE, qos, &cbData, options, numOptions);
88 ret = OCDoResource(&handle, method, query.str().c_str(), 0,
89 NULL, qos, &cbData, options, numOptions);
92 if (ret != OC_STACK_OK)
94 OC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, method);
100 OCStackApplicationResult getReqCB(void* ctx, OCDoHandle handle, OCClientResponse * clientResponse)
102 if(clientResponse == NULL)
104 OC_LOG(INFO, TAG, "The clientResponse is NULL");
105 return OC_STACK_DELETE_TRANSACTION;
107 if(ctx == (void*)DEFAULT_CONTEXT_VALUE)
109 OC_LOG(INFO, TAG, "Callback Context for GET query recvd successfully");
112 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
113 OC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
114 OC_LOG_V(INFO, TAG, "JSON = %s =============> Get Response",
115 clientResponse->resJSONPayload);
117 if(clientResponse->rcvdVendorSpecificHeaderOptions &&
118 clientResponse->numRcvdVendorSpecificHeaderOptions)
120 OC_LOG (INFO, TAG, "Received vendor specific options");
122 OCHeaderOption * rcvdOptions = clientResponse->rcvdVendorSpecificHeaderOptions;
123 for( i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
125 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
127 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
128 ((OCHeaderOption)rcvdOptions[i]).optionID );
129 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
130 ((OCHeaderOption)rcvdOptions[i]).optionLength);
134 return OC_STACK_DELETE_TRANSACTION;
137 // This is a function called back when a device is discovered
138 OCStackApplicationResult discoveryReqCB(void* ctx, OCDoHandle handle,
139 OCClientResponse * clientResponse)
141 uint8_t remoteIpAddr[4];
142 uint16_t remotePortNu;
144 if (ctx == (void*) DEFAULT_CONTEXT_VALUE)
146 OC_LOG(INFO, TAG, "Callback Context for DISCOVER query recvd successfully");
151 OC_LOG_V(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
153 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
154 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
155 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
158 "Device =============> Discovered %s @ %d.%d.%d.%d:%d",
159 clientResponse->resJSONPayload, remoteIpAddr[0], remoteIpAddr[1],
160 remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
162 parseClientResponse(clientResponse);
166 case TEST_NON_CON_OP:
167 InitGetRequest(OC_LOW_QOS);
170 InitGetRequest(OC_HIGH_QOS);
178 return (UNICAST_DISCOVERY) ? OC_STACK_DELETE_TRANSACTION : OC_STACK_KEEP_TRANSACTION ;
182 int InitGetRequest(OCQualityOfService qos)
184 OC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
185 std::ostringstream query;
186 query << "coap://" << coapServerIP << ":" << coapServerPort << coapServerResource;
188 return (InvokeOCDoResource(query, OC_REST_GET, (qos == OC_HIGH_QOS)?
189 OC_HIGH_QOS:OC_LOW_QOS, getReqCB, NULL, 0));
195 OCCallbackData cbData;
197 /* Start a discovery query*/
198 char szQueryUri[64] = { 0 };
199 if (UNICAST_DISCOVERY)
201 strcpy(szQueryUri, TEST_APP_UNICAST_DISCOVERY_QUERY);
206 strcpy(szQueryUri, MULTICAST_RESOURCE_DISCOVERY_QUERY);
208 strcpy(szQueryUri, OC_WELL_KNOWN_QUERY);
211 cbData.cb = discoveryReqCB;
212 cbData.context = (void*)DEFAULT_CONTEXT_VALUE;
215 if(UNICAST_DISCOVERY)
217 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, CA_CONNTYPE,
218 OC_LOW_QOS, &cbData, NULL, 0);
222 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_ALL,
223 OC_LOW_QOS, &cbData, NULL, 0);
226 ret = OCDoResource(&handle, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS, &cbData, NULL, 0);
228 if (ret != OC_STACK_OK)
230 OC_LOG(ERROR, TAG, "OCStack resource error");
235 int main(int argc, char* argv[])
237 uint8_t addr[20] = {0};
238 uint8_t* paddr = NULL;
239 uint16_t port = USE_RANDOM_PORT;
240 uint8_t ifname[] = "eth0";
244 while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
246 while ((opt = getopt(argc, argv, "u:t:")) != -1)
252 UNICAST_DISCOVERY = atoi(optarg);
255 TEST_CASE = atoi(optarg);
259 CA_CONNTYPE = OCConnectivityType(atoi(optarg));
268 if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
269 (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
276 /*Get Ip address on defined interface and initialize coap on it with random port number
277 * this port number will be used as a source port in all coap communications*/
278 if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
279 sizeof(addr)) == ERR_SUCCESS)
281 OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
285 /* Initialize OCStack*/
286 if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK)
288 OC_LOG(ERROR, TAG, "OCStack init error");
294 // Break from loop with Ctrl+C
295 OC_LOG(INFO, TAG, "Entering occlient main loop...");
296 signal(SIGINT, handleSigInt);
299 if (OCProcess() != OC_STACK_OK)
301 OC_LOG(ERROR, TAG, "OCStack process error");
307 OC_LOG(INFO, TAG, "Exiting occlient main loop...");
309 if (OCStop() != OC_STACK_OK)
311 OC_LOG(ERROR, TAG, "OCStack stop error");
317 std::string getIPAddrTBServer(OCClientResponse * clientResponse)
319 if(!clientResponse) return "";
320 if(!clientResponse->addr) return "";
321 uint8_t a, b, c, d = 0;
322 if(0 != OCDevAddrToIPv4Addr(clientResponse->addr, &a, &b, &c, &d) ) return "";
324 char ipaddr[16] = {'\0'};
325 // ostringstream not working correctly here, hence snprintf
326 snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", a,b,c,d);
327 return std::string (ipaddr);
331 std::string getPortTBServer(OCClientResponse * clientResponse)
333 if(!clientResponse) return "";
334 if(!clientResponse->addr) return "";
336 if(0 != OCDevAddrToPort(clientResponse->addr, &p) ) return "";
337 std::ostringstream ss;
342 std::string getQueryStrForGetPut(OCClientResponse * clientResponse)
347 void parseClientResponse(OCClientResponse * clientResponse)
349 coapServerIP = getIPAddrTBServer(clientResponse);
350 coapServerPort = getPortTBServer(clientResponse);
351 coapServerResource = getQueryStrForGetPut(clientResponse);