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