Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / csdk / stack / test / arduino / ArduinoStackTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6
7 // Do not remove the include below
8 #include "ArduinoStackTest.h"
9
10 #include "logger.h"
11 #include "ocstack.h"
12 #include "ocstackinternal.h"
13 #include <string.h>
14
15 #define PCF(str) ((const prog_char*)(F(str)))
16
17 const prog_char TAG[] PROGMEM = "Arduino";
18 static OCUri SERVICE_URI = "coap://127.0.0.1:5683/";
19
20 #if 0  // Turn off logger test stuff
21 const prog_char tag[] PROGMEM = "Arduino";
22 const prog_char msg[] PROGMEM = "Arduino Logger Test";
23
24 const prog_char debugMsg[] PROGMEM = "this is a DEBUG message";
25 const prog_char infoMsg[] PROGMEM = "this is a INFO message";
26 const prog_char warningMsg[] PROGMEM = "this is a WARNING message";
27 const prog_char errorMsg[] PROGMEM = "this is a ERROR message";
28 const prog_char fatalMsg[] PROGMEM = "this is a FATAL message";
29
30 const prog_char multiLineMsg[] PROGMEM = "this is a DEBUG message\non multiple\nlines";
31 #endif
32
33 void EXPECT_EQ(int a, int b)  {
34   if (a == b) {
35     OC_LOG(INFO, TAG, PCF("PASS"));
36   } else {
37     OC_LOG(ERROR, TAG, PCF("FAIL"));
38   }
39 }
40
41 void EXPECT_STREQ(const char *a, const char *b)  {
42   if (strcmp(a, b) == 0) {
43     OC_LOG(INFO, TAG, "PASS");
44   } else {
45     OC_LOG(ERROR, TAG, "FAIL");
46   }
47 }
48 //-----------------------------------------------------------------------------
49 //  Tests
50 //-----------------------------------------------------------------------------
51 #if 0  // Turn off logger tests
52 void test0() {
53     OC_LOG(INFO, tag, msg);
54 }
55
56 void test1() {
57     OC_LOG(INFO, 0, msg);
58 }
59
60 void test2() {
61     OC_LOG(INFO, tag, 0);
62 }
63
64 void test3() {
65     OC_LOG(INFO, 0, 0);
66 }
67
68 void test4() {
69     OC_LOG(DEBUG, tag, debugMsg);
70     OC_LOG(INFO, tag, infoMsg);
71     OC_LOG(WARNING, tag, warningMsg);
72     OC_LOG(ERROR, tag, errorMsg);
73     OC_LOG(FATAL, tag, fatalMsg);
74 }
75
76 void test5() {
77     OC_LOG(DEBUG, tag, multiLineMsg);
78 }
79
80
81 void test6() {
82     // Log buffer
83     uint8_t buffer[50];
84     for (int i = 0; i < (int)(sizeof buffer); i++) {
85         buffer[i] = i;
86     }
87     OC_LOG_BUFFER(DEBUG, tag, buffer, sizeof buffer);
88
89     // Log buffer, 128 bytes is a good boundary (8 rows of 16 values)
90     uint8_t buffer1[128];
91     for (int i = 0; i < (int)(sizeof buffer1); i++) {
92         buffer1[i] = i;
93     }
94     OC_LOG_BUFFER(DEBUG, tag, buffer1, sizeof buffer1);
95
96     // 1 below 128 byte boundary
97     uint8_t buffer2[127];
98     for (int i = 0; i < (int)(sizeof buffer2); i++) {
99         buffer2[i] = i;
100     }
101     OC_LOG_BUFFER(DEBUG, tag, buffer2, sizeof buffer2);
102
103     // 1 above 128 byte boundary
104     uint8_t buffer3[129];
105     for (int i = 0; i < (int)(sizeof buffer3); i++) {
106         buffer3[i] = i;
107     }
108     OC_LOG_BUFFER(DEBUG, tag, buffer3, sizeof buffer3);
109 }
110 #endif
111
112 extern "C" void asyncDoResourcesCallback(OCStackResult result, OCRepresentationHandle representation) {
113     OC_LOG(INFO, TAG, PCF("Entering asyncDoResourcesCallback"));
114
115     EXPECT_EQ(OC_STACK_OK, result);
116     OCResource *resource = (OCResource *)representation;
117     OC_LOG_V(INFO, TAG, "URI = %s", resource->uri);
118     EXPECT_STREQ(SERVICE_URI, resource->uri);
119 }
120
121 void test0() {
122     OC_LOG(INFO, TAG, PCF("test0"));
123     EXPECT_EQ(OC_STACK_OK, OCInit(0, 5683, OC_SERVER)); 
124 }
125
126 void test1() {
127     OC_LOG(INFO, TAG, PCF("test1"));
128     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 0, OC_SERVER));
129 }
130
131 void test2() {
132     OC_LOG(INFO, TAG, PCF("test2"));
133     EXPECT_EQ(OC_STACK_OK, OCInit(0, 0, OC_SERVER));
134 }
135
136 void test3() {
137     OC_LOG(INFO, TAG, PCF("test3"));
138     EXPECT_EQ(OC_STACK_ERROR, OCInit(0, 0, (OCMode)10));
139 }
140
141 void test4() {
142     OC_LOG(INFO, TAG, PCF("test4"));
143     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT));
144     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_SERVER));
145     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT_SERVER));
146 }
147
148 void test5() {
149     OC_LOG(INFO, TAG, PCF("test5"));
150     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT));
151     EXPECT_EQ(OC_STACK_OK, OCDoResource(OC_REST_GET, OC_EXPLICIT_DEVICE_DISCOVERY_URI, 0, 0, asyncDoResourcesCallback));
152     EXPECT_EQ(OC_STACK_OK, OCUpdateResources(SERVICE_URI));
153     EXPECT_EQ(OC_STACK_OK, OCStop());
154 }
155
156 void test6() {
157     OC_LOG(INFO, TAG, PCF("test6"));
158     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT));
159     EXPECT_EQ(OC_STACK_OK, OCStop());
160     EXPECT_EQ(OC_STACK_ERROR, OCStop());
161 }
162
163 void test7() {
164     OC_LOG(INFO, TAG, PCF("test7"));
165     EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT));
166     EXPECT_EQ(OC_STACK_OK, OCDoResource(OC_REST_GET, OC_EXPLICIT_DEVICE_DISCOVERY_URI, 0, 0, asyncDoResourcesCallback));
167     EXPECT_EQ(OC_STACK_INVALID_URI, OCUpdateResources(0));
168     EXPECT_EQ(OC_STACK_OK, OCStop());
169 }
170
171
172
173 //The setup function is called once at startup of the sketch
174 void setup()
175 {
176     // Add your initialization code here
177     OC_LOG_INIT();
178
179     test0();
180     delay(2000);
181     test1();
182     delay(2000);
183     test2();
184     delay(2000);
185     test3();
186     delay(2000);
187     test4();
188     delay(2000);
189     test5();
190     delay(2000);
191     test6();
192     delay(2000);
193
194 #if 1
195     test7();
196     delay(2000);
197 #endif
198
199 }
200
201 // The loop function is called in an endless loop
202 void loop()
203 {
204     delay(2000);
205 }