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