1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
14 #define TAG PCF("occlient")
18 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
19 void handleSigInt(int signum) {
20 if (signum == SIGINT) {
25 // This is a function called back when a device is discovered
26 OCStackApplicationResult applicationDiscoverCB(
27 OCClientResponse * clientResponse) {
28 uint8_t remoteIpAddr[4];
29 uint16_t remotePortNu;
30 OC_LOG(INFO, TAG, "Entering applicationDiscoverCB (Application Layer CB)");
31 OCDevAddrToIPv4Addr((OCDevAddr *) clientResponse->addr, remoteIpAddr,
32 remoteIpAddr + 1, remoteIpAddr + 2, remoteIpAddr + 3);
33 OCDevAddrToPort((OCDevAddr *) clientResponse->addr, &remotePortNu);
34 OC_LOG_V(INFO, TAG, "Device =============> Discovered %s @ %d.%d.%d.%d:%d",clientResponse->resJSONPayload,remoteIpAddr[0], remoteIpAddr[1], remoteIpAddr[2], remoteIpAddr[3], remotePortNu);
35 //return OC_STACK_DELETE_TRANSACTION;
36 return OC_STACK_KEEP_TRANSACTION;
41 uint16_t port = USE_RANDOM_PORT;
42 uint8_t ifname[] = "eth0";
44 /*Get Ip address on defined interface and initialize coap on it with random port number
45 * this port number will be used as a source port in all coap communications*/
46 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
47 OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
49 /* Initialize OCStack*/
50 if (OCInit((char *) addr, port, OC_CLIENT) != OC_STACK_OK) {
51 OC_LOG(ERROR, TAG, "OCStack init error");
55 /* Start a discovery query*/
56 char szQueryUri[64] = { 0 };
57 strcpy(szQueryUri, OC_EXPLICIT_DEVICE_DISCOVERY_URI);
58 if (OCDoResource(OC_REST_GET, szQueryUri, 0, 0, OC_NON_CONFIRMABLE,
59 applicationDiscoverCB) != OC_STACK_OK) {
60 OC_LOG(ERROR, TAG, "OCStack resource error");
64 // Break from loop with Ctrl+C
65 OC_LOG(INFO, TAG, "Entering occlient main loop...");
66 signal(SIGINT, handleSigInt);
69 if (OCProcess() != OC_STACK_OK) {
70 OC_LOG(ERROR, TAG, "OCStack process error");
75 }OC_LOG(INFO, TAG, "Exiting occlient main loop...");
77 if (OCStop() != OC_STACK_OK) {
78 OC_LOG(ERROR, TAG, "OCStack stop error");