LOM2M  0.3.1
om2m-server-base.h
1 /*
2  Copyright (c) 2013-2021 LAAS-CNRS (www.laas.fr)
3  7 Colonel Roche 31077 Toulouse - France
4 
5  The use of this source code requires an agreement with the CNRS.
6 
7  Initial Contributors:
8  David Gauchard, Guillaume Garzone, Thierry Monteil.
9 */
10 
11 #ifndef OM2M_SERVER_BASE_H
12 #define OM2M_SERVER_BASE_H
13 
14 #include <assert.h>
15 
16 #include <map>
17 
18 #include <ESP8266mDNS.h>
19 #include <FS.h>
20 #include <ESP8266HTTPUpdateServer.h>
21 
22 #include "bsp.h"
23 #include "lom2m.h"
24 #include "gateway.h"
25 #include "Entity.h"
26 #include "restHandler.h"
27 #include "tools.h"
28 #include "ssid.h"
29 #include "IPE.h"
30 #include "global.h"
31 
32 #define HEAP_TRACE_MS 5000 // define to 0 to disable
33 
34 WiFiClient tcp;
35 String mDNSName = MDNSNAME;
36 ESP8266WebServer om2msrv(8282);
37 #if !CORE_MOCK
38 ESP8266HTTPUpdateServer httpUpdater; // <= OTA
39 #endif
40 
41 void user_initial_setup();
42 void user_final_setup();
43 void user_loop();
44 
46 // adm
47 
48 void serveFallback()
49 {
50  log("----> NOT SERVED URI = %s\n", om2msrv.uri().c_str());
51 
52  String uri = om2msrv.uri().c_str();
53  uri.replace(F("//"), F("/"));
54  if (uri.startsWith(String(F("/~/"))))
55  {
56  serveOM2M(uri);
57  }
58 }
59 
60 bool serveFile(String path)
61 {
62  if (path.endsWith("/"))
63  {
64  path += F("index.html");
65  }
66  String pathgz = path + F(".gz");
67  if (SPIFFS.exists(pathgz))
68  {
69  path = pathgz;
70  }
71  if (SPIFFS.exists(path))
72  {
73  File file = SPIFFS.open(path, "r");
74  om2msrv.streamFile(file, contentType(path));
75  file.close();
76  return true;
77  }
78  return false;
79 }
80 
81 #define printHttpCode(code) _printHttpCode(code, #code)
82 HTTPCode _printHttpCode(HTTPCode code, const char* desc)
83 {
84  log("\n----> %s: request returned %d (%s)\n\n", desc, code, HTTPCode2Human(code).c_str());
85  return code;
86 }
87 
88 
89 void setup()
90 {
91  Serial.begin(115200);
92  Serial.printf("\n\nESP826/Arduino - %s\n", ESP.getFullVersion().c_str());
93  Serial.printf("host name = " MDNSNAME "\n");
94  Serial.printf("mac address = %s\n", WiFi.macAddress().c_str());
95  Serial.printf("\n\n---- LOM2M server ----\n\n");
96  Serial.printf("Connecting to '%s'", mySSID);
97  WiFi.hostname(mDNSName);
98  WiFi.begin(mySSID, myPSK);
99  while (WiFi.status() != WL_CONNECTED)
100  {
101  Serial.print(".");
102  delay(1000);
103  }
104  log_setup();
105  log("\nConnected\n");
106 
107  log("Try me at these addresses:");
108  log("(with 'telnet <addr> or 'nc -u <addr> 23')");
109 
110  IP = WiFi.localIP().toString();
111  uint8_t mac[6];
112  char ret[14] ;
113  WiFi.macAddress(mac);
114  sprintf(ret, "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
115  CSE_ID = "mn-cse-" + String(ret);
116  CSE_NAME = "mn-" + String(ret);
117  user_initial_setup();
118 
119 #if 1 //CORE_MOCK
120  log("IP=%s\n", IP.c_str());
121 #else
122  for (auto a : addrList)
123  {
124  log("IF='%s' IPv6=%d local=%d hostname='%s' addr= %s",
125  a.ifname().c_str(),
126  a.isV6(),
127  a.isLocal(),
128  a.ifhostname(),
129  a.toString().c_str());
130 
131  if (a.isLegacy())
132  log(" mask:%s / gw:%s",
133  a.netmask().toString().c_str(),
134  a.gw().toString().c_str());
135  }
136 #endif
137 
138 #if 0
139  MDNS.begin(mDNSName);
140  MDNS.addService("http", "tcp", "port", 8282);
141 #endif
142 
143  const char* headers [] = { "Content-Type", "X-M2M-Origin", "accept", "X-M2M-RI" };
144  om2msrv.collectHeaders(headers, sizeof headers / sizeof headers[0]);
145 
146 #if !CORE_MOCK
147  httpUpdater.setup(&om2msrv); // <= setup OTA before om2msrv.begin();
148 #endif
149 
150  om2msrv.begin();
151  om2msrv.onNotFound([]()
152  {
153  if (!serveFile(om2msrv.uri()))
154  {
155  serveFallback();
156  }
157  });
158 
159  user_final_setup();
160  // TODO send CSE REGISTRATION
161  registerCSE();
162 }
163 
164 
165 #if HEAP_TRACE_MS
166 Millis heapLastMs = 0;
167 #endif
168 
169 void loop()
170 {
171  om2msrv.handleClient();
172 #if 0
173  MDNS.update();
174 #endif
175  user_loop();
176  handleNotifications(3);
177 
178 #if HEAP_TRACE_MS
179 #if DEBUG
180  Millis nowMs = millis();
181  if (nowMs - heapLastMs > HEAP_TRACE_MS)
182  {
183  heapLastMs = nowMs;
184  log("-- free heap: %d bytes", ESP.getFreeHeap());
185  }
186 #endif
187 #endif // HEAP_TRACE_MS
188 }
189 
190 #endif // OM2M_SERVER_BASE_H