network.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //
  2. // Created by janco on 25-2-16.
  3. //
  4. #include <dev/board.h>
  5. #include <dev/debug.h>
  6. #include <sys/timer.h>
  7. #include <sys/confnet.h>
  8. #include <sys/socket.h>
  9. #include <netinet/tcp.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include <net/route.h>
  14. #include <dev/nicrtl.h>
  15. #include <stdio.h>
  16. #include <io.h>
  17. #include <arpa/inet.h>
  18. #include <pro/dhcp.h>
  19. #include <pro/sntp.h>
  20. #include "ntp.h"
  21. #include "network.h"
  22. #include "jsmn.h"
  23. #include "rtc.h"
  24. #include "alarm.h"
  25. #include "buttonPrefix.h"
  26. bool isReceiving;
  27. bool hasNetwork;
  28. void NetworkInit() {
  29. hasNetwork = false;
  30. /* Register de internet controller. */
  31. if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
  32. printf("Registering failed. \n");
  33. }/* Netwerk configureren op dhcp */
  34. else if (NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 0)) {
  35. printf("DHCP failed. \n");
  36. }else {
  37. printf("Ik heb een internet connectie. Ip is: %s \nMac address is: %s\n\n", inet_ntoa(confnet.cdn_ip_addr), ether_ntoa(confnet.cdn_mac));
  38. }
  39. NutSleep(2000);
  40. hasNetwork = true;
  41. }
  42. char* getMacAdress(){
  43. ether_ntoa(confnet.cdn_mac);
  44. }
  45. char* httpGet(char address[]){
  46. isReceiving = true;
  47. printf("\n\n #-- HTTP get -- #\n");
  48. TCPSOCKET* sock = NutTcpCreateSocket();
  49. char buffer[2];
  50. char* content = (char*) calloc(1 , 800);
  51. char enters = 0;
  52. int t = 0;
  53. if(content == 0){
  54. printf("Can't calloc memory\n");
  55. }else if (NutTcpConnect(sock, inet_addr("62.195.226.247"), 80)) {
  56. printf("Can't connect to server\n");
  57. }else{
  58. FILE *stream;
  59. stream = _fdopen((int) sock, "r+b");
  60. //Writing http GET to stream
  61. fprintf(stream, "GET %s HTTP/1.1\r\nHost: saltyradio.jancokock.me \r\n\r\n", address);
  62. fflush(stream);
  63. printf("Headers writed. Now reading.");
  64. NutDelay(500);
  65. //Removing header:
  66. while(fgets(buffer, sizeof(buffer), stream) != NULL) {
  67. if(enters == 4) {
  68. content[t] = buffer[0];
  69. t++;
  70. }else {
  71. if (buffer[0] == '\n' || buffer[0] == '\r') {
  72. enters++;
  73. }
  74. else {
  75. enters = 0;
  76. }
  77. }
  78. }
  79. fclose(stream);
  80. }
  81. NutTcpCloseSocket(sock);
  82. content[t] = '\0';
  83. printf("\nContent size: %d, Content: %s \n", t, content);
  84. isReceiving = false;
  85. return content;
  86. }
  87. int getTimeZone()
  88. {
  89. char* content = httpGet("/gettimezone.php");
  90. int timezone = atoi(content);
  91. free(content);
  92. printf("%d", timezone);
  93. return timezone;
  94. }
  95. void parseAlarmJson(char* content){
  96. int r;
  97. int i;
  98. jsmn_parser p;
  99. jsmntok_t token[150]; /* We expect no more than 128 tokens */
  100. jsmn_init(&p);
  101. r = jsmn_parse(&p, content, strlen(content), token, sizeof(token)/sizeof(token[0]));
  102. if (r <= 0) {
  103. printf("Failed to parse JSON: %d \n", r);
  104. return;
  105. }else{
  106. printf("Aantal tokens found: %d \n", r);
  107. }
  108. int start = 1;
  109. int usedAlarms[maxAlarms()];
  110. int j;
  111. for(j = 0; j < maxAlarms(); j++){
  112. usedAlarms[j] = 0;
  113. }
  114. for(i = 1; i < r; i++)
  115. {
  116. struct _tm time = GetRTCTime();
  117. u_short port;
  118. char url[24];
  119. char ip[24];
  120. char name[16];
  121. memset(url, 0, 24);
  122. memset(ip, 0, 24);
  123. memset(name, 0, 16);
  124. int id;
  125. for (i = i; !((i + start) % 24 == 0); i++) {
  126. if (jsoneq(content, &token[i], "YYYY") == 0) {
  127. time.tm_year= getIntegerToken(content, &token[i + 1]) - 1900;
  128. i++;
  129. }else if (jsoneq(content, &token[i], "MM") == 0) {
  130. time.tm_mon= getIntegerToken(content, &token[i + 1]) - 1;
  131. i++;
  132. }else if (jsoneq(content, &token[i], "DD") == 0) {
  133. time.tm_mday = getIntegerToken(content, &token[i + 1]);
  134. i++;
  135. }else if (jsoneq(content, &token[i], "hh") == 0) {
  136. time.tm_hour = getIntegerToken(content, &token[i + 1]);
  137. i++;
  138. }else if (jsoneq(content, &token[i], "mm") == 0) {
  139. time.tm_min = getIntegerToken(content, &token[i + 1]);
  140. i++;
  141. }else if (jsoneq(content, &token[i], "ss") == 0) {
  142. time.tm_sec = getIntegerToken(content, &token[i + 1]);
  143. i++;
  144. }else if (jsoneq(content, &token[i], "id") == 0) {
  145. id = getIntegerToken(content, &token[i + 1]);
  146. i++;
  147. }else if (jsoneq(content, &token[i], "port") == 0) {
  148. port = getIntegerToken(content, &token[i + 1]);
  149. i++;
  150. }else if (jsoneq(content, &token[i], "ip") == 0) {
  151. getStringToken(content, &token[i + 1], ip);
  152. i++;
  153. }else if (jsoneq(content, &token[i], "url") == 0) {
  154. getStringToken(content, &token[i + 1], url);
  155. i++;
  156. }else if (jsoneq(content, &token[i], "name") == 0) {
  157. getStringToken(content, &token[i + 1], name);
  158. i++;
  159. }
  160. }
  161. start = 0;
  162. int idx = alarmExist(id);
  163. if(idx == -1){
  164. printf("New alarm found!\n");
  165. printf("Alarm time is: %02d:%02d:%02d\n", time.tm_hour, time.tm_min, time.tm_sec);
  166. printf("Alarm date is: %02d.%02d.%02d\n", time.tm_mday, (time.tm_mon + 1), (time.tm_year + 1900));
  167. printf("Alarm stream data is: %s:%d%s\n", ip, port, url);
  168. printf("Alarm id and name is: %d %s\n\n", id, name);
  169. //zoek naar een vrije plaats in de alarm array
  170. for(j = 0; j < maxAlarms(); j++){
  171. if(usedAlarms[j] == 0){ //Dit is een lege plaats, hier kunnen we ons nieuwe alarm plaatsen
  172. setAlarm(time, name, ip, port, url, 5, id, j);
  173. usedAlarms[j] = 1;
  174. j = 10;
  175. }
  176. }
  177. }else{
  178. usedAlarms[idx] = 1; //Alarm bestaat al, dus we houden deze plaats vrij voor dat alarm
  179. }
  180. }
  181. for(j = 0; j < maxAlarms(); j++){ //Alle overige plaatsen, die wij niet gezet hebben, verwijderen.
  182. if(usedAlarms[j] == 0){
  183. deleteAlarm(j);
  184. };
  185. }
  186. }
  187. void parsePrefixJson(char* content){
  188. int r;
  189. int i;
  190. jsmn_parser p;
  191. jsmntok_t token[150]; /* We expect no more than 128 tokens */
  192. jsmn_init(&p);
  193. r = jsmn_parse(&p, content, strlen(content), token, sizeof(token)/sizeof(token[0]));
  194. if (r <= 0) {
  195. printf("Failed to parse JSON: %d \n", r);
  196. return;
  197. }else{
  198. printf("Aantal tokens found: %d \n", r);
  199. }
  200. int start = 1;
  201. //int usedAlarms[maxAlarms()];
  202. //int j;
  203. //for(j = 0; j < maxAlarms(); j++){
  204. // usedAlarms[j] = 0;
  205. //}
  206. for(i = 1; i < r; i++)
  207. {
  208. struct _LeftButtons something;
  209. u_short id;
  210. u_short bid;
  211. char name[16];
  212. char ip[24];
  213. u_short port;
  214. char url[24];
  215. memset(url, 0, 24);
  216. memset(ip, 0, 24);
  217. memset(name, 0, 16);
  218. // for (i = i; !((i + start) % 24 == 0); i++) {
  219. // if (jsoneq(content, &token[i], "YYYY") == 0) {
  220. // time.tm_year= getIntegerToken(content, &token[i + 1]) - 1900;
  221. // i++;
  222. // }else if (jsoneq(content, &token[i], "MM") == 0) {
  223. // time.tm_mon= getIntegerToken(content, &token[i + 1]) - 1;
  224. // i++;
  225. // }else if (jsoneq(content, &token[i], "DD") == 0) {
  226. // time.tm_mday = getIntegerToken(content, &token[i + 1]);
  227. // i++;
  228. // }else if (jsoneq(content, &token[i], "hh") == 0) {
  229. // time.tm_hour = getIntegerToken(content, &token[i + 1]);
  230. // i++;
  231. // }else if (jsoneq(content, &token[i], "mm") == 0) {
  232. // time.tm_min = getIntegerToken(content, &token[i + 1]);
  233. // i++;
  234. // }else if (jsoneq(content, &token[i], "ss") == 0) {
  235. // time.tm_sec = getIntegerToken(content, &token[i + 1]);
  236. // i++;
  237. // }else if (jsoneq(content, &token[i], "id") == 0) {
  238. // id = getIntegerToken(content, &token[i + 1]);
  239. // i++;
  240. // }else if (jsoneq(content, &token[i], "port") == 0) {
  241. // port = getIntegerToken(content, &token[i + 1]);
  242. // i++;
  243. // }else if (jsoneq(content, &token[i], "ip") == 0) {
  244. // getStringToken(content, &token[i + 1], ip);
  245. // i++;
  246. // }else if (jsoneq(content, &token[i], "url") == 0) {
  247. // getStringToken(content, &token[i + 1], url);
  248. // i++;
  249. // }else if (jsoneq(content, &token[i], "name") == 0) {
  250. // getStringToken(content, &token[i + 1], name);
  251. // i++;
  252. // }
  253. // }
  254. start = 0;
  255. int idx = alarmExist(id);
  256. if(idx == -1){
  257. printf("New alarm found!\n");
  258. printf("Alarm stream data is: %s:%d%s\n", ip, port, url);
  259. printf("Alarm id and name is: %d %s\n\n", id, name);
  260. // //zoek naar een vrije plaats in de alarm array
  261. // for(j = 0; j < maxAlarms(); j++){
  262. // if(usedAlarms[j] == 0){ //Dit is een lege plaats, hier kunnen we ons nieuwe alarm plaatsen
  263. // setAlarm(time, name, ip, port, url, 5, id, j);
  264. // usedAlarms[j] = 1;
  265. // j = 10;
  266. // }
  267. // }
  268. // }else{
  269. // usedAlarms[idx] = 1; //Alarm bestaat al, dus we houden deze plaats vrij voor dat alarm
  270. }
  271. }
  272. // for(j = 0; j < maxAlarms(); j++){ //Alle overige plaatsen, die wij niet gezet hebben, verwijderen.
  273. // if(usedAlarms[j] == 0){
  274. // deleteAlarm(j);
  275. // };
  276. // }
  277. }
  278. bool NetworkIsReceiving(void){
  279. return isReceiving;
  280. }
  281. bool hasNetworkConnection(void){
  282. return hasNetwork;
  283. }