1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
14 static const char * LEVEL[] __attribute__ ((unused)) = {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
15 char *getResult(OCStackResult result);
16 void printToScreen(LogLevel level, const char * tag, const char * logStr);
17 void printToScreenV(LogLevel level, const char * tag, const char * format, ...);
19 #define TAG PCF("occlient")
23 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
24 void handleSigInt(int signum) {
25 if (signum == SIGINT) {
30 // This is a function called back when a device is discovered
31 //OC_STACK_DELETE_TRANSACTION = 0,
32 //OC_STACK_KEEP_TRANSACTION,
33 OCStackApplicationResult clientApplicationCB(void* ctx, OCClientResponse * clientResponse) {
34 printToScreen(INFO, TAG, "Entering clientApplicationCB (Application Layer CB)");
36 printToScreenV(INFO, TAG, "StackResult: %s", getResult(clientResponse->result));
38 printToScreenV(INFO, TAG, "JSON = %s =============> Discovered", clientResponse->resJSONPayload);
40 if(ctx == (void*)CTX_VAL) {
41 printToScreenV(INFO, TAG, "Callback Context recvd successfully");
45 return OC_STACK_KEEP_TRANSACTION;
47 //This function is called back when a resource is discovered.
50 uint8_t addr[20] = {0};
51 uint16_t port = USE_RANDOM_PORT;
52 uint8_t ifname[] = "eth0";
53 OCCallbackData cbData;
55 /*Get Ip address on defined interface and initialize coap on it with random port number
56 * this port number will be used as a source port in all coap communications*/
57 OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr, sizeof(addr));
58 printToScreenV(INFO, TAG, "Starting occlient on address %s",addr);
60 /* Initialize OCStack*/
61 if (OCInit((char *) addr, port, OC_CLIENT) != OC_STACK_OK) {
62 printToScreen(ERROR, TAG, "OCStack init error");
66 /* Start a discovery query*/
67 char szQueryUri[64] = { 0 };
68 strcpy(szQueryUri, OC_EXPLICIT_DEVICE_DISCOVERY_URI);
69 cbData.cb = clientApplicationCB;
70 cbData.context = (void*)CTX_VAL;
71 if (OCDoResource(OC_REST_GET, szQueryUri, 0, 0, OC_NON_CONFIRMABLE, &cbData)
73 printToScreen(ERROR, TAG, "OCStack resource error");
77 // Break from loop with Ctrl+C
78 printToScreen(INFO, TAG, "Entering occlient main loop...");
79 signal(SIGINT, handleSigInt);
82 if (OCProcess() != OC_STACK_OK) {
83 printToScreen(ERROR, TAG, "OCStack process error");
89 printToScreen(INFO, TAG, "Exiting occlient main loop...");
91 if (OCStop() != OC_STACK_OK) {
92 printToScreen(ERROR, TAG, "OCStack stop error");
98 char *getResult(OCStackResult result) {
99 char resString[40] = {0};
100 strcpy(resString, "Result: ");
103 strcat(resString, "OC_STACK_OK");
105 case OC_STACK_INVALID_URI:
106 strcat(resString, "OC_STACK_INVALID_URI");
108 case OC_STACK_INVALID_IP:
109 strcat(resString, "OC_STACK_INVALID_IP");
111 case OC_STACK_INVALID_PORT:
112 strcat(resString, "OC_STACK_INVALID_PORT");
114 case OC_STACK_INVALID_CALLBACK:
115 strcat(resString, "OC_STACK_INVALID_CALLBACK");
117 case OC_STACK_INVALID_METHOD:
118 strcat(resString, "OC_STACK_INVALID_METHOD");
121 strcat(resString, "OC_STACK_ERROR");
124 strcat(resString, "UNKNOWN");
128 void printToScreen(LogLevel level, const char * tag, const char * logStr) {
129 printf("%s: %s: %s\n", LEVEL[level], tag, logStr);
131 void printToScreenV(LogLevel level, const char * tag, const char * format, ...) {
132 if (!format || !tag) {
135 char buffer[MAX_LOG_V_BUFFER_SIZE];
136 memset(buffer, 0, sizeof buffer);
138 va_start(args, format);
139 vsnprintf(buffer, sizeof buffer - 1, format, args);
141 printToScreen(level, tag, buffer);